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

update: 添加libcoap.hcode()方法,返回更友好的类http statue code
update: libcoap解析长度在13~298之间的opt数据
update: 更新coap的demo

Wendal Chen 5 лет назад
Родитель
Сommit
9eaa08f53e

+ 1 - 0
luat/include/netclient.h

@@ -50,6 +50,7 @@ typedef void (*tpc_cb_t)(netc_ent_t* ent);
 typedef struct netclient
 {
     int id;
+    char idStr[10];
     char hostname[64];
     uint32_t ipv4;
     int port;

+ 66 - 9
luat/modules/luat_lib_libcoap.c

@@ -12,6 +12,39 @@
 #define LUAT_LOG_TAG "luat.libcoap"
 #include "luat_log.h"
 
+//---------------------------
+#ifndef COAP_OPTION_IF_MATCH
+
+#define COAP_OPTION_IF_MATCH        1 /* C, opaque, 0-8 B, (none) */
+#define COAP_OPTION_URI_HOST        3 /* C, String, 1-255 B, destination address */
+#define COAP_OPTION_ETAG            4 /* E, opaque, 1-8 B, (none) */
+#define COAP_OPTION_IF_NONE_MATCH   5 /* empty, 0 B, (none) */
+#define COAP_OPTION_URI_PORT        7 /* C, uint, 0-2 B, destination port */
+#define COAP_OPTION_LOCATION_PATH   8 /* E, String, 0-255 B, - */
+#define COAP_OPTION_URI_PATH       11 /* C, String, 0-255 B, (none) */
+#define COAP_OPTION_CONTENT_FORMAT 12 /* E, uint, 0-2 B, (none) */
+#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
+#define COAP_OPTION_MAXAGE         14 /* E, uint, 0--4 B, 60 Seconds */
+#define COAP_OPTION_URI_QUERY      15 /* C, String, 1-255 B, (none) */
+#define COAP_OPTION_ACCEPT         17 /* C, uint,   0-2 B, (none) */
+#define COAP_OPTION_LOCATION_QUERY 20 /* E, String,   0-255 B, (none) */
+#define COAP_OPTION_SIZE2          28 /* E, uint, 0-4 B, (none) */
+#define COAP_OPTION_PROXY_URI      35 /* C, String, 1-1034 B, (none) */
+#define COAP_OPTION_PROXY_SCHEME   39 /* C, String, 1-255 B, (none) */
+#define COAP_OPTION_SIZE1          60 /* E, uint, 0-4 B, (none) */
+
+#define COAP_MEDIATYPE_TEXT_PLAIN                 0 /* text/plain (UTF-8) */
+#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT   40 /* application/link-format */
+#define COAP_MEDIATYPE_APPLICATION_XML           41 /* application/xml */
+#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM  42 /* application/octet-stream */
+#define COAP_MEDIATYPE_APPLICATION_RDF_XML       43 /* application/rdf+xml */
+#define COAP_MEDIATYPE_APPLICATION_EXI           47 /* application/exi  */
+#define COAP_MEDIATYPE_APPLICATION_JSON          50 /* application/json  */
+#define COAP_MEDIATYPE_APPLICATION_CBOR          60 /* application/cbor  */
+
+#endif
+//---------------------------
+
 
 typedef struct libcoap_header
 {
@@ -51,19 +84,23 @@ static void addopt(luat_lib_libcoap_t* _coap, uint8_t opt_type, const char* valu
 
     // LLOGD("opt type=%d value len=%d", opt_type, len);
     // LLOGD("opt optSize cur=%d", _coap->optSize);
-    _coap->opt[_coap->optSize] = (cur_opt << 4) + (len & 0xF);
+    size_t idx = _coap->optSize;
+    if (len <= 12) {
+        _coap->opt[idx++] = (cur_opt << 4) + (len & 0xF);
+    }
+    else if (len <= 268) {
+        _coap->opt[idx++] = (cur_opt << 4) + (0x0D);
+        _coap->opt[idx++] = len - 0x0D;
+    } else {
+        LLOGW("coap opt is too large!!! ignore!!!");
+        return;
+    }
     for (size_t i = 0; i < len; i++)
     {
-        _coap->opt[_coap->optSize + 1 + i] = *(value + i);
+        _coap->opt[idx++] = *(value + i);
     }
-    
-    // LLOGD("opt opt first=%d", _coap->opt[_coap->optSize]);
-    // char* opt_ptr = _coap->opt;
-    // opt_ptr +=  _coap->optSize + 1;
-    // memcpy(opt_ptr, value, len);
-    _coap->optSize += len + 1;
 
-    
+    _coap->optSize += idx;
 }
 
 // libcoap.new(libcoap.GET, "time", {}, nil)
@@ -153,6 +190,13 @@ static int l_libcoap_parse(lua_State* L) {
             if (opt_header->opt_delta == 0xF)
                 break;
             LLOGD("found opt %d %d", opt_header->opt_delta, opt_header->opt_len);
+            if (opt_header->opt_len <= 12) {
+                // nop
+            }
+            else if (opt_header->opt_len == 13) {
+                opt_header->opt_len += (unsigned int)(*(ptr+1));
+            }
+            
             ptr += opt_header->opt_len + 1;
             idx += opt_header->opt_len + 1;
             _coap->optSize += opt_header->opt_len + 1;
@@ -259,6 +303,18 @@ static int libcoap_code(lua_State *L) {
     return 1;
 }
 
+static int libcoap_httpcode(lua_State *L) {
+    luat_lib_libcoap_t *_coap = tocoap(L);
+    if (_coap == NULL) {
+        LLOGE("coap sturt is NULL");
+        lua_pushstring(L, "coap sturt is NULL");
+        lua_error(L);
+        return 0;
+    }
+    lua_pushinteger(L, (_coap->header.code >> 5) * 100 + (_coap->header.code & 0xF));
+    return 1;
+}
+
 static int libcoap_type(lua_State *L) {
     luat_lib_libcoap_t *_coap = tocoap(L);
     if (_coap == NULL) {
@@ -288,6 +344,7 @@ static const luaL_Reg lib_libcoap[] = {
     {"msgid",       libcoap_msgid},
     {"token",       libcoap_token},
     {"code",        libcoap_code},
+    {"hcode",        libcoap_httpcode},
     {"rawdata",     libcoap_rawdata},
     {"data",        libcoap_data},
     //{"__gc",        libcoap_gc},

+ 4 - 9
luat/modules/luat_lib_socket.c

@@ -84,7 +84,7 @@ static int luat_lib_netc_msg_handler(lua_State* L, void* ptr) {
         lua_getglobal(L, "sys_pub");
         if (lua_isfunction(L, -1)) {
             char buff[32] = {0};
-            sprintf(buff, "NETC_END_%ld", ent->netc_id);
+            sprintf(buff, "NETC_END_%x", ent->netc_id);
             //LLOGD("FUCK [%s]", buff);
             lua_pushstring(L, buff);
             lua_call(L, 1, 0);
@@ -212,6 +212,7 @@ static int luat_lib_socket_new(lua_State* L, int netc_type) {
     //rt_memset(thiz->hostname, 0, sizeof(thiz->hostname));
     thiz->hostname[0] = '_';
     thiz->id = netc_next_no();
+    sprintf(thiz->idStr, "%x", thiz->id);
 
     luaL_setmetatable(L, LUAT_NETC_HANDLE);
 
@@ -333,12 +334,12 @@ static int netc_tostring(lua_State *L) {
 /*
 获取socket对象的id
 @function    so:id()
-@return int 对象id,自增量,全局唯一
+@return string 对象id,全局唯一
 -- 参考socket.tcp的说明, 并查阅demo
 */
 static int netc_id(lua_State *L) {
     netclient_t *netc = tonetc(L);
-    lua_pushinteger(L, netc->id);
+    lua_pushstring(L, netc->idStr);
     return 1;
 }
 
@@ -499,12 +500,6 @@ static const rotable_Reg reg_socket[] =
 {
     { "tcp", luat_lib_socket_tcp, 0},
     { "udp", luat_lib_socket_udp, 0},
-    // { "connect", luat_lib_socket_connect, 0},
-    // { "close", luat_lib_socket_close, 0},
-    // { "send", luat_lib_socket_send, 0},
-
-
-
     { "tsend" ,  sal_tls_test , 0},
     { "ntpSync", socket_ntp_sync, 0}, // TODO 改成平台无关的UDP实现?
     { "isReady", l_socket_is_ready, 0},

+ 5 - 3
script/demo/84.udp_coap/main.lua

@@ -27,10 +27,12 @@ sys.taskInit(function()
                 if #data >= 4 then
                     local _coap = libcoap.parse(data)
                     if _coap then
-                        log.info("coap", "type", _coap:tp())
-                        log.info("coap", "code", _coap:code())
-                        log.info("coap", "msgid", _coap:msgid())
+                        log.info("coap", "type", _coap:tp(), "code", _coap:code(), "msgid", _coap:msgid())
                         log.info("coap", "data", _coap:data())
+                        log.info("coap", "http statue code", _coap:hcode())
+                        if _coap:tp() == 2 and _coap:hcode() == 205 then
+                            log.info("coap", "http resp ACK and 205")
+                        end
                     end
                 end
             end)