diff --git a/docs/README.md b/docs/README.md index c33823a7..b3c66e12 100644 --- a/docs/README.md +++ b/docs/README.md @@ -203,7 +203,7 @@ option during build time, use the `-D OPTION` compiler flag: ```sh $ cc app0.c mongoose.c # Use defaults! $ cc app1.c mongoose.c -D MG_ENABLE_IPV6=1 # Build with IPv6 enabled -$ cc app2.c mongoose.c -D MG_ARCH=MG_ARCH_UNIX # Set UNIX architecture +$ cc app2.c mongoose.c -D MG_ARCH=MG_ARCH_FREERTOS_LWIP # Set architecture $ cc app3.c mongoose.c -D MG_ENABLE_FS=0 -D MG_ENABLE_LOG=0 # Multiple options ``` @@ -219,6 +219,7 @@ architecture is guessed during the build, so setting it is not usually required. |MG_ARCH_WIN32 | Windows systems | |MG_ARCH_ESP32 | Espressif's ESP32 | |MG_ARCH_ESP8266 | Espressif's ESP8266 | +|MG_ARCH_FREERTOS_LWIP | All systems with FreeRTOS kernel and LwIP IP stack | |MG_ARCH_FREERTOS_TCP | All systems with FreeRTOS kernel and FreeRTOS-Plus-TCP IP stack | |MG_ARCH_CUSTOM | A custom architecture, discussed in the next section | @@ -264,34 +265,12 @@ systems, follow the guideline outlined below: 2. Create a file called `mongoose_custom.h`, with defines and includes that are relevant to your platform. Mongoose uses `bool` type, `MG_DIRSEP` define, and optionally other structures like `DIR *` depending on the functionality -you have enabled - see previous section. Below is an example for FreeRTOS+LWIP: - -```c -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#define MG_INT64_FMT "%lld" -#define MG_DIRSEP '/' -``` - -And this one is for bare metal and custom TCP/IP stack: +you have enabled - see previous section. Below is an example: ```c #include // For bool -#include +#include + #define MG_DIRSEP '/' #define MG_INT64_FMT "%lld" #define MG_ENABLE_SOCKET 0 // Disable BSD socket API, implement your own diff --git a/mongoose.c b/mongoose.c index 5eb2970c..ec0b1acd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4198,7 +4198,7 @@ void mg_usleep(unsigned long usecs) { Sleep(usecs / 1000); #elif MG_ARCH == MG_ARCH_ESP8266 ets_delay_us(usecs); -#elif MG_ARCH == MG_ARCH_FREERTOS_TCP +#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP vTaskDelay(pdMS_TO_TICKS(usecs / 1000)); #else usleep((useconds_t) usecs); @@ -4212,7 +4212,7 @@ unsigned long mg_millis(void) { return esp_timer_get_time() / 1000; #elif MG_ARCH == MG_ARCH_ESP8266 return xTaskGetTickCount() * portTICK_PERIOD_MS; -#elif MG_ARCH == MG_ARCH_FREERTOS_TCP +#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP return xTaskGetTickCount() * portTICK_PERIOD_MS; #else struct timespec ts; diff --git a/mongoose.h b/mongoose.h index 2c92805a..4fe646a1 100644 --- a/mongoose.h +++ b/mongoose.h @@ -24,6 +24,7 @@ #define MG_ARCH_ESP32 3 #define MG_ARCH_ESP8266 4 #define MG_ARCH_FREERTOS_TCP 5 +#define MG_ARCH_FREERTOS_LWIP 6 #if !defined(MG_ARCH) #if defined(__unix__) || defined(__APPLE__) @@ -66,6 +67,7 @@ + #if MG_ARCH == MG_ARCH_ESP32 #include @@ -119,6 +121,41 @@ #endif +#if MG_ARCH == MG_ARCH_FREERTOS_LWIP + +#include +#include +#include +#include +#include + +#if MG_ENABLE_FS +#include +#endif + +#include +#include + +#include + +#define MG_INT64_FMT "%lld" +#define MG_DIRSEP '/' + +// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib +static inline void *mg_calloc(int cnt, size_t size) { + void *p = pvPortMalloc(cnt * size); + if (p != NULL) memset(p, 0, size); + return p; +} +#define calloc(a, b) mg_calloc((a), (b)) +#define free(a) vPortFree(a) +#define malloc(a) pvPortMalloc(a) + +#define gmtime_r(a, b) gmtime(a) + +#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP + + #if MG_ARCH == MG_ARCH_FREERTOS_TCP #include diff --git a/src/arch.h b/src/arch.h index 774bc484..da10088a 100644 --- a/src/arch.h +++ b/src/arch.h @@ -6,6 +6,7 @@ #define MG_ARCH_ESP32 3 #define MG_ARCH_ESP8266 4 #define MG_ARCH_FREERTOS_TCP 5 +#define MG_ARCH_FREERTOS_LWIP 6 #if !defined(MG_ARCH) #if defined(__unix__) || defined(__APPLE__) @@ -43,6 +44,7 @@ #include "arch_esp32.h" #include "arch_esp8266.h" +#include "arch_freertos_lwip.h" #include "arch_freertos_tcp.h" #include "arch_unix.h" #include "arch_win32.h" diff --git a/src/arch_freertos_lwip.h b/src/arch_freertos_lwip.h new file mode 100644 index 00000000..0c60d295 --- /dev/null +++ b/src/arch_freertos_lwip.h @@ -0,0 +1,44 @@ +#pragma once + +#if MG_ARCH == MG_ARCH_FREERTOS_LWIP + +#include +#include +#include +#include + +#if defined(__GNUC__) +#include +#else +typedef long suseconds_t; +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; +#endif + +#if MG_ENABLE_FS +#include +#endif + +#include +#include + +#include + +#define MG_INT64_FMT "%lld" +#define MG_DIRSEP '/' + +// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib +static inline void *mg_calloc(int cnt, size_t size) { + void *p = pvPortMalloc(cnt * size); + if (p != NULL) memset(p, 0, size); + return p; +} +#define calloc(a, b) mg_calloc((a), (b)) +#define free(a) vPortFree(a) +#define malloc(a) pvPortMalloc(a) + +#define gmtime_r(a, b) gmtime(a) + +#endif // MG_ARCH == MG_ARCH_FREERTOS_LWIP diff --git a/src/util.c b/src/util.c index cb64acb1..addbaf98 100644 --- a/src/util.c +++ b/src/util.c @@ -323,7 +323,7 @@ void mg_usleep(unsigned long usecs) { Sleep(usecs / 1000); #elif MG_ARCH == MG_ARCH_ESP8266 ets_delay_us(usecs); -#elif MG_ARCH == MG_ARCH_FREERTOS_TCP +#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP vTaskDelay(pdMS_TO_TICKS(usecs / 1000)); #else usleep((useconds_t) usecs); @@ -337,7 +337,7 @@ unsigned long mg_millis(void) { return esp_timer_get_time() / 1000; #elif MG_ARCH == MG_ARCH_ESP8266 return xTaskGetTickCount() * portTICK_PERIOD_MS; -#elif MG_ARCH == MG_ARCH_FREERTOS_TCP +#elif MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_FREERTOS_LWIP return xTaskGetTickCount() * portTICK_PERIOD_MS; #else struct timespec ts;