support linux/freertos/weak for mqtt platform

This commit is contained in:
pikastech 2022-11-12 15:20:51 +08:00
parent e88fbd2f6a
commit a67a71f944
16 changed files with 338 additions and 83 deletions

View File

@ -8,7 +8,6 @@
*/
#ifndef _PLATFORM_MEMORY_H_
#define _PLATFORM_MEMORY_H_
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"

View File

@ -11,6 +11,9 @@
PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_init(&(m->mutex), NULL);
#elif PIKA_FREERTOS_ENABLE
m->mutex = xSemaphoreCreateMutex();
return 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -19,6 +22,8 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) {
PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_lock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreTake(m->mutex, portMAX_DELAY);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -27,6 +32,8 @@ PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) {
PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_trylock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreTake(m->mutex, 0);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -35,6 +42,8 @@ PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) {
int platform_mutex_unlock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_unlock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreGive(m->mutex);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -43,6 +52,9 @@ int platform_mutex_unlock(platform_mutex_t* m) {
int platform_mutex_destroy(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_destroy(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
vSemaphoreDelete(m->mutex);
return 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif

View File

@ -8,22 +8,31 @@
*/
#ifndef _PLATFORM_MUTEX_H_
#define _PLATFORM_MUTEX_H_
#include "PikaPlatform.h"
#include "PikaObj.h"
#ifdef __linux
#include <pthread.h>
#include <pthread.h>
typedef struct platform_mutex {
pthread_mutex_t mutex;
} platform_mutex_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "semphr.h"
typedef struct platform_mutex {
SemaphoreHandle_t mutex;
} platform_mutex_t;
#else
#include "__platform_thread.h"
/*
You need to create the __platform_thread.h for your platform.
For example:
You can #include <rtthread.h> in the __platform_thread.h
*/
#include "__platform_thread.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __linux
typedef struct platform_mutex {
pthread_mutex_t mutex;
} platform_mutex_t;
#endif
int platform_mutex_init(platform_mutex_t* m);
int platform_mutex_lock(platform_mutex_t* m);

View File

@ -9,12 +9,13 @@
#include "platform_thread.h"
#include "platform_memory.h"
platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,
unsigned int stack_size,
unsigned int priority,
unsigned int tick) {
PIKA_WEAK platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,
unsigned int stack_size,
unsigned int priority,
unsigned int tick) {
#ifdef __linux
int res;
platform_thread_t* thread;
void* (*thread_entry)(void*);
@ -31,25 +32,64 @@ platform_thread_t* platform_thread_init(const char* name,
thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
return thread;
#elif PIKA_FREERTOS_ENABLE
BaseType_t err;
platform_thread_t* thread;
thread = platform_memory_alloc(sizeof(platform_thread_t));
(void)tick;
err = xTaskCreate(entry, name, stack_size, param, priority, thread->thread);
if (pdPASS != err) {
platform_memory_free(thread);
return NULL;
}
return thread;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_startup(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_startup(platform_thread_t* thread) {
(void)thread;
}
void platform_thread_stop(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_stop(platform_thread_t* thread) {
#ifdef __linux
pthread_mutex_lock(&(thread->mutex));
pthread_cond_wait(&(thread->cond), &(thread->mutex));
pthread_mutex_unlock(&(thread->mutex));
#elif PIKA_FREERTOS_ENABLE
vTaskSuspend(thread->thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_start(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_start(platform_thread_t* thread) {
#ifdef __linux
pthread_mutex_lock(&(thread->mutex));
pthread_cond_signal(&(thread->cond));
pthread_mutex_unlock(&(thread->mutex));
#elif PIKA_FREERTOS_ENABLE
vTaskResume(thread->thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_destroy(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_destroy(platform_thread_t* thread) {
#ifdef __linux
if (NULL != thread)
pthread_detach(thread->thread);
#elif PIKA_FREERTOS_ENABLE
if (NULL != thread)
vTaskDelete(thread->thread);
platform_memory_free(thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}

View File

@ -12,7 +12,23 @@
#include "PikaObj.h"
#ifdef __linux
#include <pthread.h>
typedef struct platform_thread {
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
} platform_thread_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "task.h"
typedef struct platform_thread {
TaskHandle_t thread;
} platform_thread_t;
#else
/*
You need to create the __platform_thread.h for your platform.
For example:
You can #include <rtthread.h> in the __platform_thread.h
*/
#include "__platform_thread.h"
#endif
@ -20,14 +36,6 @@
extern "C" {
#endif
#ifdef __linux
typedef struct platform_thread {
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
} platform_thread_t;
#endif
platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,

View File

@ -9,35 +9,85 @@
#include "platform_timer.h"
void platform_timer_init(platform_timer_t* timer) {
PIKA_WEAK void platform_timer_init(platform_timer_t* timer) {
#ifdef __linux
timer->time = (struct timeval){0, 0};
#elif PIKA_FREERTOS_ENABLE
timer->time = 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) {
PIKA_WEAK void platform_timer_cutdown(platform_timer_t* timer,
unsigned int timeout) {
#ifdef __linux
struct timeval now;
gettimeofday(&now, NULL);
struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
timeradd(&now, &interval, &timer->time);
#elif PIKA_FREERTOS_ENABLE
timer->time = platform_uptime_ms();
timer->time += timeout;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
char platform_timer_is_expired(platform_timer_t* timer) {
PIKA_WEAK char platform_timer_is_expired(platform_timer_t* timer) {
#ifdef __linux
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->time, &now, &res);
return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
#elif PIKA_FREERTOS_ENABLE
return platform_uptime_ms() > timer->time ? 1 : 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
int platform_timer_remain(platform_timer_t* timer) {
PIKA_WEAK int platform_timer_remain(platform_timer_t* timer) {
#ifdef __linux
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->time, &now, &res);
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
#elif PIKA_FREERTOS_ENABLE
uint32_t now;
now = platform_uptime_ms();
if (timer->time <= now) {
return 0;
}
return timer->time - now;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
unsigned long platform_timer_now(void) {
PIKA_WEAK unsigned long platform_timer_now(void) {
#ifdef __linux
return (unsigned long)time(NULL);
#elif PIKA_FREERTOS_ENABLE
return (unsigned long)platform_uptime_ms();
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_timer_usleep(unsigned long usec) {
PIKA_WEAK void platform_timer_usleep(unsigned long usec) {
#ifdef __linux
usleep(usec);
#elif PIKA_FREERTOS_ENABLE
TickType_t tick;
if (usec != 0) {
tick = usec / portTICK_PERIOD_MS;
if (tick == 0)
tick = 1;
}
vTaskDelay(tick);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}

View File

@ -11,9 +11,21 @@
#include <stdio.h>
#ifdef __linux
#include <sys/time.h>
#include <sys/time.h>
typedef struct platform_timer {
struct timeval time;
} platform_timer_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "task.h"
typedef struct platform_timer {
uint32_t time;
} platform_timer_t;
#else
#include "__platform_time.h"
/*
You need to create the __platform_timer.h for your platform.
*/
#include "__platform_time.h"
#endif
#include <time.h>
#include <unistd.h>
@ -23,12 +35,6 @@
extern "C" {
#endif
#ifdef __linux
typedef struct platform_timer {
struct timeval time;
} platform_timer_t;
#endif
void platform_timer_init(platform_timer_t* timer);
void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout);
char platform_timer_is_expired(platform_timer_t* timer);

View File

@ -81,7 +81,8 @@
"PikaPlatform_socket.h": "c",
"time.h": "c",
"platform_thread.h": "c",
"platform_timer.h": "c"
"platform_timer.h": "c",
"stddef.h": "c"
},
"python.formatting.provider": "autopep8",
"C_Cpp.errorSquiggles": "Disabled"

View File

@ -8,7 +8,6 @@
*/
#ifndef _PLATFORM_MEMORY_H_
#define _PLATFORM_MEMORY_H_
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "PikaObj.h"

View File

@ -11,6 +11,9 @@
PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_init(&(m->mutex), NULL);
#elif PIKA_FREERTOS_ENABLE
m->mutex = xSemaphoreCreateMutex();
return 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -19,6 +22,8 @@ PIKA_WEAK int platform_mutex_init(platform_mutex_t* m) {
PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_lock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreTake(m->mutex, portMAX_DELAY);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -27,6 +32,8 @@ PIKA_WEAK int platform_mutex_lock(platform_mutex_t* m) {
PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_trylock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreTake(m->mutex, 0);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -35,6 +42,8 @@ PIKA_WEAK int platform_mutex_trylock(platform_mutex_t* m) {
int platform_mutex_unlock(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_unlock(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
return xSemaphoreGive(m->mutex);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
@ -43,6 +52,9 @@ int platform_mutex_unlock(platform_mutex_t* m) {
int platform_mutex_destroy(platform_mutex_t* m) {
#ifdef __linux
return pthread_mutex_destroy(&(m->mutex));
#elif PIKA_FREERTOS_ENABLE
vSemaphoreDelete(m->mutex);
return 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif

View File

@ -8,22 +8,31 @@
*/
#ifndef _PLATFORM_MUTEX_H_
#define _PLATFORM_MUTEX_H_
#include "PikaPlatform.h"
#include "PikaObj.h"
#ifdef __linux
#include <pthread.h>
#include <pthread.h>
typedef struct platform_mutex {
pthread_mutex_t mutex;
} platform_mutex_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "semphr.h"
typedef struct platform_mutex {
SemaphoreHandle_t mutex;
} platform_mutex_t;
#else
#include "__platform_thread.h"
/*
You need to create the __platform_thread.h for your platform.
For example:
You can #include <rtthread.h> in the __platform_thread.h
*/
#include "__platform_thread.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __linux
typedef struct platform_mutex {
pthread_mutex_t mutex;
} platform_mutex_t;
#endif
int platform_mutex_init(platform_mutex_t* m);
int platform_mutex_lock(platform_mutex_t* m);

View File

@ -9,12 +9,13 @@
#include "platform_thread.h"
#include "platform_memory.h"
platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,
unsigned int stack_size,
unsigned int priority,
unsigned int tick) {
PIKA_WEAK platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,
unsigned int stack_size,
unsigned int priority,
unsigned int tick) {
#ifdef __linux
int res;
platform_thread_t* thread;
void* (*thread_entry)(void*);
@ -31,25 +32,64 @@ platform_thread_t* platform_thread_init(const char* name,
thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
return thread;
#elif PIKA_FREERTOS_ENABLE
BaseType_t err;
platform_thread_t* thread;
thread = platform_memory_alloc(sizeof(platform_thread_t));
(void)tick;
err = xTaskCreate(entry, name, stack_size, param, priority, thread->thread);
if (pdPASS != err) {
platform_memory_free(thread);
return NULL;
}
return thread;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_startup(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_startup(platform_thread_t* thread) {
(void)thread;
}
void platform_thread_stop(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_stop(platform_thread_t* thread) {
#ifdef __linux
pthread_mutex_lock(&(thread->mutex));
pthread_cond_wait(&(thread->cond), &(thread->mutex));
pthread_mutex_unlock(&(thread->mutex));
#elif PIKA_FREERTOS_ENABLE
vTaskSuspend(thread->thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_start(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_start(platform_thread_t* thread) {
#ifdef __linux
pthread_mutex_lock(&(thread->mutex));
pthread_cond_signal(&(thread->cond));
pthread_mutex_unlock(&(thread->mutex));
#elif PIKA_FREERTOS_ENABLE
vTaskResume(thread->thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_thread_destroy(platform_thread_t* thread) {
PIKA_WEAK void platform_thread_destroy(platform_thread_t* thread) {
#ifdef __linux
if (NULL != thread)
pthread_detach(thread->thread);
#elif PIKA_FREERTOS_ENABLE
if (NULL != thread)
vTaskDelete(thread->thread);
platform_memory_free(thread);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}

View File

@ -12,7 +12,23 @@
#include "PikaObj.h"
#ifdef __linux
#include <pthread.h>
typedef struct platform_thread {
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
} platform_thread_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "task.h"
typedef struct platform_thread {
TaskHandle_t thread;
} platform_thread_t;
#else
/*
You need to create the __platform_thread.h for your platform.
For example:
You can #include <rtthread.h> in the __platform_thread.h
*/
#include "__platform_thread.h"
#endif
@ -20,14 +36,6 @@
extern "C" {
#endif
#ifdef __linux
typedef struct platform_thread {
pthread_t thread;
pthread_mutex_t mutex;
pthread_cond_t cond;
} platform_thread_t;
#endif
platform_thread_t* platform_thread_init(const char* name,
void (*entry)(void*),
void* const param,

View File

@ -9,35 +9,85 @@
#include "platform_timer.h"
void platform_timer_init(platform_timer_t* timer) {
PIKA_WEAK void platform_timer_init(platform_timer_t* timer) {
#ifdef __linux
timer->time = (struct timeval){0, 0};
#elif PIKA_FREERTOS_ENABLE
timer->time = 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout) {
PIKA_WEAK void platform_timer_cutdown(platform_timer_t* timer,
unsigned int timeout) {
#ifdef __linux
struct timeval now;
gettimeofday(&now, NULL);
struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
timeradd(&now, &interval, &timer->time);
#elif PIKA_FREERTOS_ENABLE
timer->time = platform_uptime_ms();
timer->time += timeout;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
char platform_timer_is_expired(platform_timer_t* timer) {
PIKA_WEAK char platform_timer_is_expired(platform_timer_t* timer) {
#ifdef __linux
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->time, &now, &res);
return ((res.tv_sec < 0) || (res.tv_sec == 0 && res.tv_usec <= 0));
#elif PIKA_FREERTOS_ENABLE
return platform_uptime_ms() > timer->time ? 1 : 0;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
int platform_timer_remain(platform_timer_t* timer) {
PIKA_WEAK int platform_timer_remain(platform_timer_t* timer) {
#ifdef __linux
struct timeval now, res;
gettimeofday(&now, NULL);
timersub(&timer->time, &now, &res);
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
#elif PIKA_FREERTOS_ENABLE
uint32_t now;
now = platform_uptime_ms();
if (timer->time <= now) {
return 0;
}
return timer->time - now;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
unsigned long platform_timer_now(void) {
PIKA_WEAK unsigned long platform_timer_now(void) {
#ifdef __linux
return (unsigned long)time(NULL);
#elif PIKA_FREERTOS_ENABLE
return (unsigned long)platform_uptime_ms();
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
void platform_timer_usleep(unsigned long usec) {
PIKA_WEAK void platform_timer_usleep(unsigned long usec) {
#ifdef __linux
usleep(usec);
#elif PIKA_FREERTOS_ENABLE
TickType_t tick;
if (usec != 0) {
tick = usec / portTICK_PERIOD_MS;
if (tick == 0)
tick = 1;
}
vTaskDelay(tick);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}

View File

@ -11,9 +11,21 @@
#include <stdio.h>
#ifdef __linux
#include <sys/time.h>
#include <sys/time.h>
typedef struct platform_timer {
struct timeval time;
} platform_timer_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "task.h"
typedef struct platform_timer {
uint32_t time;
} platform_timer_t;
#else
#include "__platform_time.h"
/*
You need to create the __platform_timer.h for your platform.
*/
#include "__platform_time.h"
#endif
#include <time.h>
#include <unistd.h>
@ -23,12 +35,6 @@
extern "C" {
#endif
#ifdef __linux
typedef struct platform_timer {
struct timeval time;
} platform_timer_t;
#endif
void platform_timer_init(platform_timer_t* timer);
void platform_timer_cutdown(platform_timer_t* timer, unsigned int timeout);
char platform_timer_is_expired(platform_timer_t* timer);

View File

@ -5,6 +5,12 @@
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#elif PIKA_LWIP_ENABLE
#include <lwip/sockets.h>
#include "lwip/api.h"
#include "lwip/netdb.h"
#include "lwip/opt.h"
#include "lwip/sys.h"
#else
/*
You need to create the __platform_socket.h for your platform.