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

add: 添加mqttcore.packCONNECT

Wendal Chen 5 лет назад
Родитель
Сommit
b7311df540
2 измененных файлов с 145 добавлено и 26 удалено
  1. 30 23
      bsp/air302/lib/mqtt.lua
  2. 115 3
      luat/modules/luat_lib_mqttcore.c

+ 30 - 23
bsp/air302/lib/mqtt.lua

@@ -32,30 +32,37 @@ local encodeLen = mqttcore.encodeLen
 --     return s
 -- end
 
-local function encodeUTF8(s)
-    if not s or #s == 0 then
-        return ""
-    else
-        return pack.pack(">P", s)
-    end
-end
+local encodeUTF8 = mqttcore.encodeUTF8
+-- local function encodeUTF8(s)
+--     if not s or #s == 0 then
+--         return ""
+--     else
+--         return pack.pack(">P", s)
+--     end
+-- end
 
-local function packCONNECT(clientId, keepAlive, username, password, cleanSession, will, version)
-    local content = pack.pack(">PbbHPAAAA",
-        version == "3.1" and "MQIsdp" or "MQTT",
-        version == "3.1" and 3 or 4,
-        (#username == 0 and 0 or 1) * 128 + (#password == 0 and 0 or 1) * 64 + will.retain * 32 + will.qos * 8 + will.flag * 4 + cleanSession * 2,
-        keepAlive,
-        clientId,
-        encodeUTF8(will.topic),
-        encodeUTF8(will.payload),
-        encodeUTF8(username),
-        encodeUTF8(password))
-    return pack.pack(">bAA",
-        CONNECT * 16,
-        encodeLen(string.len(content)),
-        content)
-end
+local packCONNECT = mqttcore.packCONNECT
+
+-- local function packCONNECT(clientId, keepAlive, username, password, cleanSession, will, version)
+--     local content = pack.pack(">PbbHPAAAA",
+--         version == "3.1" and "MQIsdp" or "MQTT",
+--         version == "3.1" and 3 or 4,
+--         (#username == 0 and 0 or 1) * 128 + (#password == 0 and 0 or 1) * 64 + will.retain * 32 + will.qos * 8 + will.flag * 4 + cleanSession * 2,
+--         keepAlive,
+--         clientId,
+--         encodeUTF8(will.topic),
+--         encodeUTF8(will.payload),
+--         encodeUTF8(username),
+--         encodeUTF8(password))
+--     local mydata = pack.pack(">bAA",
+--         CONNECT * 16,
+--         encodeLen(string.len(content)),
+--         content)
+--     local tdata = mqttcore.packCONNECT(clientId, keepAlive, username, password, cleanSession, will, version)
+--     log.info("mqtt", "true", mydata:toHex())
+--     log.info("mqtt", "false", tdata:toHex())
+--     return mydata
+-- end
 
 local function packSUBSCRIBE(dup, packetId, topics)
     local header = SUBSCRIBE * 16 + dup * 8 + 2

+ 115 - 3
luat/modules/luat_lib_mqttcore.c

@@ -353,15 +353,126 @@ static int l_mqttcore_encodeLen(lua_State *L) {
     return 1;
 }
 
+static void _add_mqtt_str(luaL_Buffer *buff, const char* str, size_t len) {
+	if (len == 0) return;
+	luaL_addchar(buff, len / 256);
+	luaL_addchar(buff, len % 256);
+	luaL_addlstring(buff, str, len);
+}
 
 static int l_mqttcore_encodeUTF8(lua_State *L) {
     if(!lua_isstring(L, 1) || 0 == lua_rawlen(L, 1)) {
 		lua_pushlstring(L, "", 0);
 		return 1;
 	}
-	lua_pushlstring(L, ">P", 2);
-	lua_insert(L, 1);
-	return luat_pack(L);
+	luaL_Buffer buff;
+	luaL_buffinit(L, &buff);
+
+	size_t len = 0;
+	const char* str = lua_tolstring(L, 1, &len);
+	_add_mqtt_str(&buff, str, len);
+	luaL_pushresult(&buff);
+	
+	return 1;
+}
+
+static int l_mqttcore_packCONNECT(lua_State *L) {
+	luaL_Buffer buff;
+	luaL_buffinit(L, &buff);
+
+	// 把参数取一下
+	// 1         2          3         4         5             6     7
+	// clientId, keepAlive, username, password, cleanSession, will, version
+	const char* clientId = luaL_checkstring(L, 1);
+	int keepAlive = luaL_optinteger(L, 2, 240);
+	const char* username = luaL_optstring(L, 3, "");
+	const char* password = luaL_optstring(L, 4, "");
+	int cleanSession = luaL_optinteger(L, 5, 1);
+
+	// 处理will
+	// topic payload  retain  qos flag
+	lua_pushstring(L, "topic");
+	lua_gettable(L, 6);
+	const char* will_topic = luaL_checkstring(L, -1);
+	lua_pop(L, 1);
+
+	lua_pushstring(L, "payload");
+	lua_gettable(L, 6);
+	const char* will_payload = luaL_checkstring(L, -1);
+	lua_pop(L, 1);
+
+	lua_pushstring(L, "retain");
+	lua_gettable(L, 6);
+	uint8_t will_retain = luaL_checkinteger(L, -1);
+	lua_pop(L, 1);
+
+	lua_pushstring(L, "qos");
+	lua_gettable(L, 6);
+	uint8_t will_qos = luaL_checkinteger(L, -1);
+	lua_pop(L, 1);
+
+	lua_pushstring(L, "flag");
+	lua_gettable(L, 6);
+	uint8_t will_flag = luaL_checkinteger(L, -1);
+	lua_pop(L, 1);
+
+	// ----- 结束处理will
+
+
+	// 添加固定头 MQTT
+	luaL_addlstring(&buff, "\0\4MQTT", 6);
+
+	// 版本号 4
+	luaL_addchar(&buff, 4);
+
+	// flags
+	uint8_t flags = 0;
+	if (strlen(username) > 0) flags += 128;
+	if (strlen(password) > 0) flags += 64;
+	if (will_retain) flags += 32;
+	if (will_qos) flags += will_qos*8;
+	if (will_flag) flags += 4;
+	if (cleanSession) flags += 2;
+	luaL_addchar(&buff, flags);
+
+	// keepalive
+	luaL_addchar(&buff, keepAlive / 256);
+	luaL_addchar(&buff, keepAlive % 256);
+
+	// client id
+	_add_mqtt_str(&buff, clientId, strlen(clientId));
+
+	// will_topic
+	_add_mqtt_str(&buff, will_topic, strlen(will_topic));
+
+	// will_topic
+	_add_mqtt_str(&buff, will_payload, strlen(will_payload));
+
+	// username and password
+	_add_mqtt_str(&buff, username, strlen(username));
+	_add_mqtt_str(&buff, password, strlen(password));
+
+	// 然后计算总长度,坑呀
+
+	luaL_Buffer buff2;
+	luaL_buffinitsize(L, &buff2, buff.n + 5);
+
+	// 标识 CONNECT
+	luaL_addchar(&buff2, CONNECT * 16);
+	// 剩余长度
+    char buf[4];
+    int rc = MQTTPacket_encode(buf, buff.n);
+    luaL_addlstring(&buff2, buf, rc);
+
+	luaL_addlstring(&buff2, buff.b, buff.n);
+
+	// 清理掉
+	luaL_pushresult(&buff);
+	lua_pop(L, 1);
+
+
+	luaL_pushresult(&buff2);
+	return 1;
 }
 
 #include "rotable.h"
@@ -369,6 +480,7 @@ static const rotable_Reg reg_mqttcore[] =
 {
     { "encodeLen", l_mqttcore_encodeLen, 0},
     { "encodeUTF8",l_mqttcore_encodeUTF8,0},
+	{ "packCONNECT", l_mqttcore_packCONNECT,0},
 	{ NULL, NULL }
 };