Răsfoiți Sursa

add; 继续为air001提交实现代码

Wendal Chen 5 ani în urmă
părinte
comite
d3289136c5

+ 3 - 0
bsp/air001/CMakeLists.txt

@@ -46,4 +46,7 @@ target_link_libraries(air001 printf)
 target_link_libraries(air001 cjson)
 target_link_libraries(air001 minmea)
 target_link_libraries(air001 bget)
+
+target_link_libraries(air001 pthread)
+
 #set_target_properties(air001 PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

+ 7 - 0
bsp/air001/impl/luat_base_air001.c

@@ -6,6 +6,7 @@
 #include "luat_malloc.h"
 
 luat_vdev_t vdev;
+int luat_timer_init(void);
 
 int luat_vdev_init() {
     printf("Go\r\n");
@@ -23,6 +24,12 @@ int luat_vdev_init() {
     // 初始化Lua VM内存
     luat_heap_init();
 
+    // 初始化msgbus
+    luat_msgbus_init();
+
+    // 初始化定时器
+    luat_timer_init();
+
     return 0;
 }
 

+ 3 - 0
bsp/air001/impl/luat_fs_air001.c

@@ -2,6 +2,9 @@
 #include "luat_base.h"
 #include "luat_fs.h"
 
+#define LUAT_LOG_TAG "luat.log"
+#include "luat_log.h"
+
 int luat_fs_init(void) {
     return 0;
 }

+ 58 - 4
bsp/air001/impl/luat_msgbus_air001.c

@@ -1,17 +1,71 @@
 #include "luat_base.h"
 #include "luat_msgbus.h"
 
+#define LUAT_LOG_TAG "luat.msgbus"
+#include "luat_log.h"
+
+#include <pthread.h>
+#include <errno.h>
+#include <unistd.h>
+
+
+static pthread_cond_t msgbus_cnd;
+static pthread_mutex_t msgbus_mutex;
+
+static int msgbus_pos = 0;
+static int msgbus_last = 0;
+static rtos_msg_t msgs[256] = {0};
+
 // 定义接口方法
 void luat_msgbus_init(void) {
-
+    pthread_mutex_init(&msgbus_mutex, NULL);
+    pthread_cond_init(&msgbus_cnd, NULL);
 }
-//void* luat_msgbus_data();
+
 uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
-    return -1;
+
+    pthread_mutex_lock(&msgbus_mutex);
+
+    if (msg[msgbus_last].handler != NULL) {
+        LLOGE("over flow!!!");
+        return 1;
+    } else {
+        memcpy(&(msg[msgbus_last]), msg, sizeof(rtos_msg_t));
+        if (msgbus_last == 255) {
+            msgbus_last = 0;
+        }
+        else {
+            msgbus_last ++;
+        }
+        pthread_mutex_unlock(&msgbus_mutex);
+        pthread_cond_broadcast(&msgbus_cnd);
+        return 0;
+    }
 }
+
+
 uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
-    return -1;
+    pthread_cond_wait(&msgbus_cnd, &msgbus_mutex);
+
+    pthread_mutex_lock(&msgbus_mutex);
+    if (msgs[msgbus_pos].handler) {
+        memcpy(msg, &(msgs[msgbus_pos]), sizeof(rtos_msg_t));
+        msgs[msgbus_pos].handler = NULL;
+    }
+    else {
+        msg->handler = NULL;
+    }
+    if (msgbus_pos == 255) {
+        msgbus_pos = 0;
+    }
+    else {
+        msgbus_pos ++;
+    }
+    pthread_mutex_unlock(&msgbus_mutex);
+
+    return msg->handler == NULL ? 1 : 0;
 }
+
 uint32_t luat_msgbus_freesize(void) {
     return 1;
 }

+ 116 - 2
bsp/air001/impl/luat_timer_air001.c

@@ -2,17 +2,131 @@
 #include "luat_base.h"
 #include "luat_timer.h"
 
+#define LUAT_LOG_TAG "luat.timer"
+#include "luat_log.h"
+
+#include <pthread.h>
+#include <errno.h>
 #include <unistd.h>
+#include <sys/time.h>
+
+#define TIMER_COUNT 1024
+typedef struct luat_timer_ext
+{
+    luat_timer_t timer;
+    struct timeval tv;
+}luat_timer_ext_t;
+
+static luat_timer_ext_t     timers[TIMER_COUNT] = {0};
+
+static pthread_cond_t       timer_cnd;
+static pthread_mutex_t      timer_mutex;
+static pthread_t            timer_pid;
+
+void* timer_pthread(void* params) {
+    LLOGD("timer thread by pthread");
+    struct timeval tv;
+    while (1) {
+        pthread_mutex_lock(&timer_mutex);
+        gettimeofday(&tv,NULL);
+        long long tnow = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
+        long long minsleep = 60*1000;
+        for (size_t i = 0; i < TIMER_COUNT; i++)
+        {
+            if (timers[i].tv.tv_sec != 0) {
+                long long ctime = timers[i].tv.tv_sec * 1000LL + timers[i].tv.tv_usec / 1000;
+                if (timers[i].timer.repeat > 0) {
+                    timers[i].timer.repeat --;
+                }
+                long long diff = tnow - ctime;
+                if (diff < timers[i].timer.timeout) {
+                    // 触发timer事件
+
+                    if (timers[i].timer.repeat == 0) {
+                        timers[i].tv.tv_sec = 0; // 已经完成使命,将其删除
+                    }
+                    else {
+                        memcpy(&(timers[i].tv), &tv, sizeof(tv));
+                    }
+                }
+                else if (diff < minsleep) {
+                    minsleep = diff;
+                }
+            }
+        }
+        pthread_mutex_unlock(&timer_mutex);
+        struct timespec ts;
+        ts.tv_sec = tv.tv_sec + minsleep/1000;
+        ts.tv_nsec = tv.tv_usec += (minsleep % 1000) * 1000;
+        pthread_cond_timedwait(&timer_cnd, &timer_mutex, &ts);
+    }
+
+    return NULL;
+}
+
+int luat_timer_init(void) {
+    pthread_mutex_init(&timer_mutex, NULL);
+    pthread_cond_init(&timer_cnd, NULL);
+
+    // 启动 timer线程
+
+    pthread_create(&timer_pid, NULL, timer_pthread, NULL);
+}
 
 int luat_timer_start(luat_timer_t* timer) {
-    return -1;
+    pthread_mutex_lock(&timer_mutex);
+    int flag = 1;
+    for (size_t i = 0; i < TIMER_COUNT; i++)
+    {
+        if (timers[i].tv.tv_sec == 0) {
+            memcpy(&(timers[i].timer), timer, sizeof(luat_timer_t));
+            gettimeofday(&(timers[i].tv),NULL);
+            flag = 0;
+        }
+    }
+    pthread_mutex_unlock(&timer_mutex);
+
+    if (flag) {
+        LLOGE("too many timer!!!");
+        return 1;
+    }
+
+    // 通知timer线程, 重新计算休眠时间
+    pthread_cond_broadcast(&timer_cnd);
+
+    return 0;
 }
 
 int luat_timer_stop(luat_timer_t* timer) {
-    return -1;
+    pthread_mutex_lock(&timer_mutex);
+    for (size_t i = 0; i < TIMER_COUNT; i++)
+    {
+        if (timers[i].timer.id == timer->id && timers[i].tv.tv_sec) {
+            timers[i].tv.tv_sec = 0;
+            break;
+        }
+    }
+
+    pthread_mutex_unlock(&timer_mutex);
+
+    // 通知timer线程, 重新计算休眠时间
+    pthread_cond_broadcast(&timer_cnd);
+
+    return 0;
 }
 
 luat_timer_t* luat_timer_get(size_t timer_id) {
+
+    pthread_mutex_lock(&timer_mutex);
+    for (size_t i = 0; i < TIMER_COUNT; i++)
+    {
+        if (timers[i].timer.id == timer_id && timers[i].tv.tv_sec) {
+            return &(timers[i].timer);
+        }
+    }
+
+    pthread_mutex_unlock(&timer_mutex);
+    
     return NULL;
 }