Skip to content

Commit d06e246

Browse files
NeroReflexDenis Benato
authored andcommitted
Split Platform primitives from networking
Platforms using LwIP needs to use the "linux" implementation of networking. Not every platform using LwIP has support for every linux primitives/syscalls (such as signal). To allow the adoption in these kind of devices split the network from OS primitives. Signed-off-by: Denis Benato <[email protected]>
1 parent ad7f9e6 commit d06e246

11 files changed

+380
-198
lines changed

MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.h

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,7 @@
1717
#if !defined(MQTTFreeRTOS_H)
1818
#define MQTTFreeRTOS_H
1919

20-
#include "FreeRTOS.h"
21-
#include "FreeRTOS_Sockets.h"
22-
#include "FreeRTOS_IP.h"
23-
#include "semphr.h"
24-
#include "task.h"
25-
26-
typedef struct Timer
27-
{
28-
TickType_t xTicksToWait;
29-
TimeOut_t xTimeOut;
30-
} Timer;
31-
32-
typedef struct Network Network;
33-
34-
struct Network
35-
{
36-
xSocket_t my_socket;
37-
int (*mqttread) (Network*, unsigned char*, int, int);
38-
int (*mqttwrite) (Network*, unsigned char*, int, int);
39-
void (*disconnect) (Network*);
40-
};
41-
42-
void TimerInit(Timer*);
43-
char TimerIsExpired(Timer*);
44-
void TimerCountdownMS(Timer*, unsigned int);
45-
void TimerCountdown(Timer*, unsigned int);
46-
int TimerLeftMS(Timer*);
47-
48-
typedef struct Mutex
49-
{
50-
SemaphoreHandle_t sem;
51-
} Mutex;
52-
53-
void MutexInit(Mutex*);
54-
int MutexLock(Mutex*);
55-
int MutexUnlock(Mutex*);
56-
57-
typedef struct Thread
58-
{
59-
TaskHandle_t task;
60-
} Thread;
61-
62-
int ThreadStart(Thread*, void (*fn)(void*), void* arg);
63-
64-
int FreeRTOS_read(Network*, unsigned char*, int, int);
65-
int FreeRTOS_write(Network*, unsigned char*, int, int);
66-
void FreeRTOS_disconnect(Network*);
67-
68-
void NetworkInit(Network*);
69-
int NetworkConnect(Network*, char*, int);
70-
/*int NetworkConnectTLS(Network*, char*, int, SlSockSecureFiles_t*, unsigned char, unsigned int, char);*/
20+
#include "FreeRTOSPrimitives.h"
21+
#include "FreeRTOSNetwork.h"
7122

7223
#endif

MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c renamed to MQTTClient-C/src/FreeRTOS/MQTTFreeRTOSNetwork.c

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,10 @@
1515
* Ian Craggs - convert to FreeRTOS
1616
*******************************************************************************/
1717

18-
#include "MQTTFreeRTOS.h"
18+
#include "MQTTFreeRTOSNetwork.h"
1919

2020

21-
int ThreadStart(Thread* thread, void (*fn)(void*), void* arg)
22-
{
23-
int rc = 0;
24-
uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5);
25-
UBaseType_t uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/
26-
27-
rc = xTaskCreate(fn, /* The function that implements the task. */
28-
"MQTTTask", /* Just a text name for the task to aid debugging. */
29-
usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
30-
arg, /* The task parameter, not used in this case. */
31-
uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
32-
&thread->task); /* The task handle is not used. */
33-
34-
return rc;
35-
}
36-
37-
38-
void MutexInit(Mutex* mutex)
39-
{
40-
mutex->sem = xSemaphoreCreateMutex();
41-
}
42-
43-
int MutexLock(Mutex* mutex)
44-
{
45-
return xSemaphoreTake(mutex->sem, portMAX_DELAY);
46-
}
47-
48-
int MutexUnlock(Mutex* mutex)
49-
{
50-
return xSemaphoreGive(mutex->sem);
51-
}
52-
53-
54-
void TimerCountdownMS(Timer* timer, unsigned int timeout_ms)
55-
{
56-
timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
57-
vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */
58-
}
59-
60-
61-
void TimerCountdown(Timer* timer, unsigned int timeout)
62-
{
63-
TimerCountdownMS(timer, timeout * 1000);
64-
}
65-
66-
67-
int TimerLeftMS(Timer* timer)
68-
{
69-
xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */
70-
return (timer->xTicksToWait < 0) ? 0 : (timer->xTicksToWait * portTICK_PERIOD_MS);
71-
}
72-
73-
74-
char TimerIsExpired(Timer* timer)
75-
{
76-
return xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait) == pdTRUE;
77-
}
78-
79-
80-
void TimerInit(Timer* timer)
81-
{
82-
timer->xTicksToWait = 0;
83-
memset(&timer->xTimeOut, '\0', sizeof(timer->xTimeOut));
84-
}
85-
86-
87-
int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
21+
static int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
8822
{
8923
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
9024
TimeOut_t xTimeOut;
@@ -110,7 +44,7 @@ int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
11044
}
11145

11246

113-
int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
47+
static int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
11448
{
11549
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
11650
TimeOut_t xTimeOut;
@@ -136,7 +70,7 @@ int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
13670
}
13771

13872

139-
void FreeRTOS_disconnect(Network* n)
73+
static void FreeRTOS_disconnect(Network* n)
14074
{
14175
FreeRTOS_closesocket(n->my_socket);
14276
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2015 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
15+
*******************************************************************************/
16+
17+
#if !defined(MQTTFreeRTOSNetwork_H)
18+
#define MQTTFreeRTOS_H
19+
20+
#include "FreeRTOS.h"
21+
#include "FreeRTOS_Sockets.h"
22+
#include "FreeRTOS_IP.h"
23+
#include "semphr.h"
24+
#include "task.h"
25+
26+
typedef struct Network Network;
27+
28+
struct Network
29+
{
30+
xSocket_t my_socket;
31+
int (*mqttread) (Network*, unsigned char*, int, int);
32+
int (*mqttwrite) (Network*, unsigned char*, int, int);
33+
void (*disconnect) (Network*);
34+
};
35+
36+
void NetworkInit(Network*);
37+
int NetworkConnect(Network*, char*, int);
38+
/*int NetworkConnectTLS(Network*, char*, int, SlSockSecureFiles_t*, unsigned char, unsigned int, char);*/
39+
40+
#endif
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2015 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
15+
* Ian Craggs - convert to FreeRTOS
16+
*******************************************************************************/
17+
18+
#include "MQTTFreeRTOSPrimitives.h"
19+
20+
#include <string.h>
21+
22+
23+
int ThreadStart(Thread* thread, void (*fn)(void*), void* arg)
24+
{
25+
int rc = 0;
26+
uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5);
27+
UBaseType_t uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/
28+
29+
rc = xTaskCreate(fn, /* The function that implements the task. */
30+
"MQTTTask", /* Just a text name for the task to aid debugging. */
31+
usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
32+
arg, /* The task parameter, not used in this case. */
33+
uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
34+
&thread->task); /* The task handle is not used. */
35+
36+
return rc;
37+
}
38+
39+
40+
void MutexInit(Mutex* mutex)
41+
{
42+
mutex->sem = xSemaphoreCreateMutex();
43+
}
44+
45+
int MutexLock(Mutex* mutex)
46+
{
47+
return xSemaphoreTake(mutex->sem, portMAX_DELAY);
48+
}
49+
50+
int MutexUnlock(Mutex* mutex)
51+
{
52+
return xSemaphoreGive(mutex->sem);
53+
}
54+
55+
56+
void TimerCountdownMS(Timer* timer, unsigned int timeout_ms)
57+
{
58+
timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
59+
vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */
60+
}
61+
62+
63+
void TimerCountdown(Timer* timer, unsigned int timeout)
64+
{
65+
TimerCountdownMS(timer, timeout * 1000);
66+
}
67+
68+
69+
int TimerLeftMS(Timer* timer)
70+
{
71+
xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */
72+
return (timer->xTicksToWait < 0) ? 0 : (timer->xTicksToWait * portTICK_PERIOD_MS);
73+
}
74+
75+
76+
char TimerIsExpired(Timer* timer)
77+
{
78+
return xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait) == pdTRUE;
79+
}
80+
81+
82+
void TimerInit(Timer* timer)
83+
{
84+
timer->xTicksToWait = 0;
85+
memset(&timer->xTimeOut, '\0', sizeof(timer->xTimeOut));
86+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014, 2015 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
15+
*******************************************************************************/
16+
17+
#if !defined(MQTTFreeRTOS_H)
18+
#define MQTTFreeRTOS_H
19+
20+
#if defined(ESP_PLATFORM)
21+
#include <freertos/FreeRTOS.h>
22+
#include <freertos/semphr.h>
23+
#include <freertos/task.h>
24+
#else
25+
#include "FreeRTOS.h"
26+
#include "semphr.h"
27+
#include "task.h"
28+
#endif
29+
30+
typedef struct Timer
31+
{
32+
TickType_t xTicksToWait;
33+
TimeOut_t xTimeOut;
34+
} Timer;
35+
36+
void TimerInit(Timer*);
37+
char TimerIsExpired(Timer*);
38+
void TimerCountdownMS(Timer*, unsigned int);
39+
void TimerCountdown(Timer*, unsigned int);
40+
int TimerLeftMS(Timer*);
41+
42+
typedef struct Mutex
43+
{
44+
SemaphoreHandle_t sem;
45+
} Mutex;
46+
47+
void MutexInit(Mutex*);
48+
int MutexLock(Mutex*);
49+
int MutexUnlock(Mutex*);
50+
51+
typedef struct Thread
52+
{
53+
TaskHandle_t task;
54+
} Thread;
55+
56+
int ThreadStart(Thread*, void (*fn)(void*), void* arg);
57+
58+
#endif

MQTTClient-C/src/linux/MQTTLinux.h

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@
1717
#if !defined(__MQTT_LINUX_)
1818
#define __MQTT_LINUX_
1919

20-
#if defined(WIN32_DLL) || defined(WIN64_DLL)
21-
#define DLLImport __declspec(dllimport)
22-
#define DLLExport __declspec(dllexport)
23-
#elif defined(LINUX_SO)
24-
#define DLLImport extern
25-
#define DLLExport __attribute__ ((visibility ("default")))
26-
#else
27-
#define DLLImport
28-
#define DLLExport
29-
#endif
20+
#include "MQTTLinuxDllexport.h"
3021

3122
#include <sys/types.h>
3223
#include <sys/socket.h>
@@ -46,29 +37,7 @@
4637
#include <string.h>
4738
#include <signal.h>
4839

49-
typedef struct Timer
50-
{
51-
struct timeval end_time;
52-
} Timer;
53-
54-
void TimerInit(Timer*);
55-
char TimerIsExpired(Timer*);
56-
void TimerCountdownMS(Timer*, unsigned int);
57-
void TimerCountdown(Timer*, unsigned int);
58-
int TimerLeftMS(Timer*);
59-
60-
typedef struct Network
61-
{
62-
int my_socket;
63-
int (*mqttread) (struct Network*, unsigned char*, int, int);
64-
int (*mqttwrite) (struct Network*, unsigned char*, int, int);
65-
} Network;
66-
67-
int linux_read(Network*, unsigned char*, int, int);
68-
int linux_write(Network*, unsigned char*, int, int);
69-
70-
DLLExport void NetworkInit(Network*);
71-
DLLExport int NetworkConnect(Network*, char*, int);
72-
DLLExport void NetworkDisconnect(Network*);
40+
#include "MQTTLinuxNetwork.h"
41+
#include "MQTTLinuxPrimitives.h"
7342

7443
#endif

0 commit comments

Comments
 (0)