Просмотр исходного кода

add: socket支持UDP通信

refer: https://github.com/openLuat/LuatOS/issues/25
Wendal Chen 5 лет назад
Родитель
Сommit
6788944860

+ 1 - 1
luat/freertos/luat_msgbus_freertos.c

@@ -5,7 +5,7 @@
 
 #define LUAT_MSGBUS_MAXCOUNT 0xFF
 //#define LUAT_MSGBUS_MAXSIZE 8
-static osMessageQueueId_t queue; 
+static osMessageQueueId_t queue = {0}; 
 
 void luat_msgbus_init(void) {
     if (!queue) {

+ 1 - 1
luat/freertos/luat_timer_freertos.c

@@ -9,7 +9,7 @@
 #include "task.h"
 
 #define FREERTOS_TIMER_COUNT 32
-static luat_timer_t* timers[FREERTOS_TIMER_COUNT];
+static luat_timer_t* timers[FREERTOS_TIMER_COUNT] = {0};
 
 static void luat_timer_callback(void* param) {
     //luat_log_debug("luat.timer", "timer callback");

+ 1 - 0
luat/include/netclient.h

@@ -51,6 +51,7 @@ typedef struct netclient
 {
     int id;
     char hostname[64];
+    uint32_t ipv4;
     int port;
     int type;
     int closed;

+ 9 - 9
luat/modules/luat_lib_pm.c

@@ -16,14 +16,14 @@ static int l_pm_request(lua_State *L) {
     return 1;
 }
 
-static int l_pm_release(lua_State *L) {
-    int mode = luaL_checkinteger(L, 1);
-    if (luat_pm_release(mode) == 0)
-        lua_pushboolean(L, 1);
-    else
-        lua_pushboolean(L, 0);
-    return 1;
-}
+// static int l_pm_release(lua_State *L) {
+//     int mode = luaL_checkinteger(L, 1);
+//     if (luat_pm_release(mode) == 0)
+//         lua_pushboolean(L, 1);
+//     else
+//         lua_pushboolean(L, 0);
+//     return 1;
+// }
 
 static int l_pm_dtimer_start(lua_State *L) {
     int dtimer_id = luaL_checkinteger(L, 1);
@@ -90,7 +90,7 @@ void luat_pm_cb(int event, int arg, void* args) {
 static const rotable_Reg reg_pm[] =
 {
     { "request" ,       l_pm_request , 0},
-    { "release" ,       l_pm_release,  0},
+    // { "release" ,       l_pm_release,  0},
     { "dtimerStart",    l_pm_dtimer_start,0},
     { "dtimerStop" ,    l_pm_dtimer_stop, 0},
     { "on",             l_pm_on,   0},

+ 1 - 1
luat/modules/luat_lib_socket.c

@@ -82,7 +82,7 @@ static int luat_lib_netc_msg_handler(lua_State* L, void* ptr) {
     if (ent->event == NETC_EVENT_END) {
         lua_getglobal(L, "sys_pub");
         if (lua_isfunction(L, -1)) {
-            lua_pushfstring(L, "NETC_END_%d", ent->netc_id);
+            lua_pushfstring(L, "NETC_END_%ld", ent->netc_id);
             lua_call(L, 1, 0);
         }
         goto exit;

+ 26 - 4
luat/rtt/luat_netclient_rtt.c

@@ -129,7 +129,13 @@ static rt_int32_t socket_init(netclient_t *thiz, const char *url, int port)
     if (thiz == RT_NULL)
         return -1;
 
-    thiz->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
+    if (thiz->type == NETC_TYPE_TCP)
+        thiz->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
+    else
+    {
+        thiz->sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
+    }
+    
     if (thiz->sock_fd == -1)
     {
         LOG_E("netc[%ld] socket init : socket create failed", thiz->id);
@@ -146,6 +152,7 @@ static rt_int32_t socket_init(netclient_t *thiz, const char *url, int port)
             return -1;
         }
     }
+    thiz->ipv4 = (*((struct in_addr *)hostname->h_addr)).s_addr;
 
     // TODO: print ip for hostname
     //LOG_I("socket host=%s port=%d", hostname, port);
@@ -162,7 +169,7 @@ static rt_int32_t socket_init(netclient_t *thiz, const char *url, int port)
         return -1;
     }
 
-    LOG_I("netc[%ld] connected succeed", thiz->id);
+    LOG_I("netc[%ld] connect succeed", thiz->id);
 
     //send(thiz->sock_fd, str, strlen(str), 0);
 
@@ -287,6 +294,7 @@ static void select_handle(netclient_t *thiz, char *sock_buff)
 {
     fd_set fds;
     rt_int32_t max_fd = 0, res = 0;
+    struct sockaddr_in from = {0};
 
     max_fd = MAX_VAL(thiz->sock_fd, thiz->pipe_read_fd) + 1;
     FD_ZERO(&fds);
@@ -308,7 +316,13 @@ static void select_handle(netclient_t *thiz, char *sock_buff)
         if (FD_ISSET(thiz->sock_fd, &fds))
         {
             #if 1
-            res = recv(thiz->sock_fd, sock_buff, BUFF_SIZE, 0);
+            if (thiz->type == NETC_TYPE_TCP)
+                res = recv(thiz->sock_fd, sock_buff, BUFF_SIZE, 0);
+            else
+            {
+                res = recvfrom(thiz->sock_fd, sock_buff, BUFF_SIZE, 0, &from, sizeof(struct sockaddr_in));
+            }
+            
 
             if (res > 0) {
                 LOG_I("netc[%ld] data recv len=%d", thiz->id, res);
@@ -341,7 +355,15 @@ static void select_handle(netclient_t *thiz, char *sock_buff)
                 goto exit;
             }
             else if (res > 0) {
-                send(thiz->sock_fd, sock_buff, res, 0);
+                if (thiz->type == NETC_TYPE_TCP)
+                    send(thiz->sock_fd, sock_buff, res, 0);
+                else
+                {
+                    from.sin_addr.s_addr = thiz->ipv4;
+                    from.sin_port = htons(thiz->port);
+                    from.sin_family = AF_INET;
+                    sendto(thiz->sock_fd, sock_buff, res, 0, &from, sizeof(struct sockaddr_in));
+                }
             }
         }
     }