mqtt.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. // Module for mqtt
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "c_string.h"
  6. #include "c_stdlib.h"
  7. #include "c_types.h"
  8. #include "mem.h"
  9. #include "lwip/ip_addr.h"
  10. #include "espconn.h"
  11. #include "mqtt_msg.h"
  12. #include "msg_queue.h"
  13. #include "user_interface.h"
  14. #define MQTT_BUF_SIZE 1024
  15. #define MQTT_DEFAULT_KEEPALIVE 60
  16. #define MQTT_MAX_CLIENT_LEN 64
  17. #define MQTT_MAX_USER_LEN 64
  18. #define MQTT_MAX_PASS_LEN 64
  19. #define MQTT_SEND_TIMEOUT 5
  20. #define MQTT_CONNECT_TIMEOUT 5
  21. typedef enum {
  22. MQTT_INIT,
  23. MQTT_CONNECT_SENT,
  24. MQTT_CONNECT_SENDING,
  25. MQTT_DATA
  26. } tConnState;
  27. typedef struct mqtt_event_data_t
  28. {
  29. uint8_t type;
  30. const char* topic;
  31. const char* data;
  32. uint16_t topic_length;
  33. uint16_t data_length;
  34. uint16_t data_offset;
  35. } mqtt_event_data_t;
  36. typedef struct mqtt_state_t
  37. {
  38. uint16_t port;
  39. int auto_reconnect;
  40. mqtt_connect_info_t* connect_info;
  41. uint16_t message_length;
  42. uint16_t message_length_read;
  43. mqtt_connection_t mqtt_connection;
  44. msg_queue_t* pending_msg_q;
  45. } mqtt_state_t;
  46. typedef struct lmqtt_userdata
  47. {
  48. struct espconn *pesp_conn;
  49. int self_ref;
  50. int cb_connect_ref;
  51. int cb_connect_fail_ref;
  52. int cb_disconnect_ref;
  53. int cb_message_ref;
  54. int cb_suback_ref;
  55. int cb_unsuback_ref;
  56. int cb_puback_ref;
  57. mqtt_state_t mqtt_state;
  58. mqtt_connect_info_t connect_info;
  59. uint16_t keep_alive_tick;
  60. uint32_t event_timeout;
  61. #ifdef CLIENT_SSL_ENABLE
  62. uint8_t secure;
  63. #endif
  64. bool connected; // indicate socket connected, not mqtt prot connected.
  65. bool keepalive_sent;
  66. ETSTimer mqttTimer;
  67. tConnState connState;
  68. }lmqtt_userdata;
  69. static sint8 socket_connect(struct espconn *pesp_conn);
  70. static void mqtt_socket_reconnected(void *arg, sint8_t err);
  71. static void mqtt_socket_connected(void *arg);
  72. static void mqtt_connack_fail(lmqtt_userdata * mud, int reason_code);
  73. static void mqtt_socket_disconnected(void *arg) // tcp only
  74. {
  75. NODE_DBG("enter mqtt_socket_disconnected.\n");
  76. struct espconn *pesp_conn = arg;
  77. bool call_back = false;
  78. if(pesp_conn == NULL)
  79. return;
  80. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  81. if(mud == NULL)
  82. return;
  83. os_timer_disarm(&mud->mqttTimer);
  84. lua_State *L = lua_getstate();
  85. if(mud->connected){ // call back only called when socket is from connection to disconnection.
  86. mud->connected = false;
  87. if((mud->cb_disconnect_ref != LUA_NOREF) && (mud->self_ref != LUA_NOREF)) {
  88. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
  89. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata(client) to callback func in lua
  90. call_back = true;
  91. }
  92. }
  93. if(mud->mqtt_state.auto_reconnect){
  94. mud->pesp_conn->reverse = mud;
  95. mud->pesp_conn->type = ESPCONN_TCP;
  96. mud->pesp_conn->state = ESPCONN_NONE;
  97. mud->connected = false;
  98. mud->pesp_conn->proto.tcp->remote_port = mud->mqtt_state.port;
  99. mud->pesp_conn->proto.tcp->local_port = espconn_port();
  100. espconn_regist_connectcb(mud->pesp_conn, mqtt_socket_connected);
  101. espconn_regist_reconcb(mud->pesp_conn, mqtt_socket_reconnected);
  102. socket_connect(pesp_conn);
  103. } else {
  104. if(mud->pesp_conn){
  105. mud->pesp_conn->reverse = NULL;
  106. if(mud->pesp_conn->proto.tcp)
  107. c_free(mud->pesp_conn->proto.tcp);
  108. mud->pesp_conn->proto.tcp = NULL;
  109. c_free(mud->pesp_conn);
  110. mud->pesp_conn = NULL;
  111. }
  112. mud->connected = false;
  113. luaL_unref(L, LUA_REGISTRYINDEX, mud->self_ref);
  114. mud->self_ref = LUA_NOREF; // unref this, and the mqtt.socket userdata will delete it self
  115. }
  116. if(call_back){
  117. lua_call(L, 1, 0);
  118. }
  119. NODE_DBG("leave mqtt_socket_disconnected.\n");
  120. }
  121. static void mqtt_socket_reconnected(void *arg, sint8_t err)
  122. {
  123. NODE_DBG("enter mqtt_socket_reconnected.\n");
  124. // mqtt_socket_disconnected(arg);
  125. struct espconn *pesp_conn = arg;
  126. if(pesp_conn == NULL)
  127. return;
  128. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  129. if(mud == NULL)
  130. return;
  131. os_timer_disarm(&mud->mqttTimer);
  132. mud->event_timeout = 0; // no need to count anymore
  133. if(mud->mqtt_state.auto_reconnect){
  134. pesp_conn->proto.tcp->remote_port = mud->mqtt_state.port;
  135. pesp_conn->proto.tcp->local_port = espconn_port();
  136. socket_connect(pesp_conn);
  137. } else {
  138. #ifdef CLIENT_SSL_ENABLE
  139. if (mud->secure) {
  140. espconn_secure_disconnect(pesp_conn);
  141. } else
  142. #endif
  143. {
  144. espconn_disconnect(pesp_conn);
  145. }
  146. mqtt_socket_disconnected(arg);
  147. }
  148. NODE_DBG("leave mqtt_socket_reconnected.\n");
  149. }
  150. static void deliver_publish(lmqtt_userdata * mud, uint8_t* message, int length)
  151. {
  152. NODE_DBG("enter deliver_publish.\n");
  153. if(mud == NULL)
  154. return;
  155. mqtt_event_data_t event_data;
  156. event_data.topic_length = length;
  157. event_data.topic = mqtt_get_publish_topic(message, &event_data.topic_length);
  158. event_data.data_length = length;
  159. event_data.data = mqtt_get_publish_data(message, &event_data.data_length);
  160. if(mud->cb_message_ref == LUA_NOREF)
  161. return;
  162. if(mud->self_ref == LUA_NOREF)
  163. return;
  164. lua_State *L = lua_getstate();
  165. if(event_data.topic && (event_data.topic_length > 0)){
  166. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
  167. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata to callback func in lua
  168. lua_pushlstring(L, event_data.topic, event_data.topic_length);
  169. } else {
  170. NODE_DBG("get wrong packet.\n");
  171. return;
  172. }
  173. if(event_data.data && (event_data.data_length > 0)){
  174. lua_pushlstring(L, event_data.data, event_data.data_length);
  175. lua_call(L, 3, 0);
  176. } else {
  177. lua_call(L, 2, 0);
  178. }
  179. NODE_DBG("leave deliver_publish.\n");
  180. }
  181. static void mqtt_connack_fail(lmqtt_userdata * mud, int reason_code)
  182. {
  183. if(mud->cb_connect_fail_ref == LUA_NOREF || mud->self_ref == LUA_NOREF)
  184. {
  185. return;
  186. }
  187. lua_State *L = lua_getstate();
  188. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
  189. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata(client) to callback func in lua
  190. lua_pushinteger(L, reason_code);
  191. lua_call(L, 2, 0);
  192. }
  193. static sint8 mqtt_send_if_possible(struct espconn *pesp_conn)
  194. {
  195. if(pesp_conn == NULL)
  196. return ESPCONN_OK;
  197. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  198. if(mud == NULL)
  199. return ESPCONN_OK;
  200. sint8 espconn_status = ESPCONN_OK;
  201. // This indicates if we have sent something and are waiting for something to
  202. // happen
  203. if (mud->event_timeout == 0) {
  204. msg_queue_t *pending_msg = msg_peek(&(mud->mqtt_state.pending_msg_q));
  205. if (pending_msg) {
  206. mud->event_timeout = MQTT_SEND_TIMEOUT;
  207. NODE_DBG("Sent: %d\n", pending_msg->msg.length);
  208. #ifdef CLIENT_SSL_ENABLE
  209. if( mud->secure )
  210. {
  211. espconn_status = espconn_secure_send( pesp_conn, pending_msg->msg.data, pending_msg->msg.length );
  212. }
  213. else
  214. #endif
  215. {
  216. espconn_status = espconn_send( pesp_conn, pending_msg->msg.data, pending_msg->msg.length );
  217. }
  218. mud->keep_alive_tick = 0;
  219. }
  220. }
  221. NODE_DBG("send_if_poss, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  222. return espconn_status;
  223. }
  224. static void mqtt_socket_received(void *arg, char *pdata, unsigned short len)
  225. {
  226. NODE_DBG("enter mqtt_socket_received.\n");
  227. uint8_t msg_type;
  228. uint8_t msg_qos;
  229. uint16_t msg_id;
  230. int length = (int)len;
  231. // uint8_t in_buffer[MQTT_BUF_SIZE];
  232. uint8_t *in_buffer = (uint8_t *)pdata;
  233. struct espconn *pesp_conn = arg;
  234. if(pesp_conn == NULL)
  235. return;
  236. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  237. if(mud == NULL)
  238. return;
  239. READPACKET:
  240. if(length > MQTT_BUF_SIZE || length <= 0)
  241. return;
  242. // c_memcpy(in_buffer, pdata, length);
  243. uint8_t temp_buffer[MQTT_BUF_SIZE];
  244. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  245. mqtt_message_t *temp_msg = NULL;
  246. lua_State *L = lua_getstate();
  247. switch(mud->connState){
  248. case MQTT_CONNECT_SENDING:
  249. case MQTT_CONNECT_SENT:
  250. mud->event_timeout = 0;
  251. if(mqtt_get_type(in_buffer) != MQTT_MSG_TYPE_CONNACK){
  252. NODE_DBG("MQTT: Invalid packet\r\n");
  253. mud->connState = MQTT_INIT;
  254. #ifdef CLIENT_SSL_ENABLE
  255. if(mud->secure)
  256. {
  257. espconn_secure_disconnect(pesp_conn);
  258. }
  259. else
  260. #endif
  261. {
  262. espconn_disconnect(pesp_conn);
  263. }
  264. mqtt_connack_fail(mud, MQTT_CONN_FAIL_NOT_A_CONNACK_MSG);
  265. break;
  266. } else if (mqtt_get_connect_ret_code(in_buffer) != MQTT_CONNACK_ACCEPTED) {
  267. NODE_DBG("MQTT: CONNACK REFUSED (CODE: %d)\n", mqtt_get_connect_ret_code(in_buffer));
  268. mud->connState = MQTT_INIT;
  269. #ifdef CLIENT_SSL_ENABLE
  270. if(mud->secure)
  271. {
  272. espconn_secure_disconnect(pesp_conn);
  273. }
  274. else
  275. #endif
  276. {
  277. espconn_disconnect(pesp_conn);
  278. }
  279. mqtt_connack_fail(mud, mqtt_get_connect_ret_code(in_buffer));
  280. break;
  281. } else {
  282. mud->connState = MQTT_DATA;
  283. NODE_DBG("MQTT: Connected\r\n");
  284. if(mud->cb_connect_ref == LUA_NOREF)
  285. break;
  286. if(mud->self_ref == LUA_NOREF)
  287. break;
  288. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
  289. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata(client) to callback func in lua
  290. lua_call(L, 1, 0);
  291. break;
  292. }
  293. break;
  294. case MQTT_DATA:
  295. mud->mqtt_state.message_length_read = length;
  296. mud->mqtt_state.message_length = mqtt_get_total_length(in_buffer, mud->mqtt_state.message_length_read);
  297. msg_type = mqtt_get_type(in_buffer);
  298. msg_qos = mqtt_get_qos(in_buffer);
  299. msg_id = mqtt_get_id(in_buffer, mud->mqtt_state.message_length);
  300. msg_queue_t *pending_msg = msg_peek(&(mud->mqtt_state.pending_msg_q));
  301. NODE_DBG("MQTT_DATA: type: %d, qos: %d, msg_id: %d, pending_id: %d\r\n",
  302. msg_type,
  303. msg_qos,
  304. msg_id,
  305. (pending_msg)?pending_msg->msg_id:0);
  306. switch(msg_type)
  307. {
  308. case MQTT_MSG_TYPE_SUBACK:
  309. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg->msg_id == msg_id){
  310. NODE_DBG("MQTT: Subscribe successful\r\n");
  311. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  312. if (mud->cb_suback_ref == LUA_NOREF)
  313. break;
  314. if (mud->self_ref == LUA_NOREF)
  315. break;
  316. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_suback_ref);
  317. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref);
  318. lua_call(L, 1, 0);
  319. }
  320. break;
  321. case MQTT_MSG_TYPE_UNSUBACK:
  322. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg->msg_id == msg_id){
  323. NODE_DBG("MQTT: UnSubscribe successful\r\n");
  324. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  325. if (mud->cb_unsuback_ref == LUA_NOREF)
  326. break;
  327. if (mud->self_ref == LUA_NOREF)
  328. break;
  329. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref);
  330. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref);
  331. lua_call(L, 1, 0);
  332. }
  333. break;
  334. case MQTT_MSG_TYPE_PUBLISH:
  335. if(msg_qos == 1){
  336. temp_msg = mqtt_msg_puback(&mud->mqtt_state.mqtt_connection, msg_id);
  337. msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  338. msg_id, MQTT_MSG_TYPE_PUBACK, (int)mqtt_get_qos(temp_msg->data) );
  339. }
  340. else if(msg_qos == 2){
  341. temp_msg = mqtt_msg_pubrec(&mud->mqtt_state.mqtt_connection, msg_id);
  342. msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  343. msg_id, MQTT_MSG_TYPE_PUBREC, (int)mqtt_get_qos(temp_msg->data) );
  344. }
  345. if(msg_qos == 1 || msg_qos == 2){
  346. NODE_DBG("MQTT: Queue response QoS: %d\r\n", msg_qos);
  347. }
  348. deliver_publish(mud, in_buffer, mud->mqtt_state.message_length);
  349. break;
  350. case MQTT_MSG_TYPE_PUBACK:
  351. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg->msg_id == msg_id){
  352. NODE_DBG("MQTT: Publish with QoS = 1 successful\r\n");
  353. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  354. if(mud->cb_puback_ref == LUA_NOREF)
  355. break;
  356. if(mud->self_ref == LUA_NOREF)
  357. break;
  358. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
  359. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata to callback func in lua
  360. lua_call(L, 1, 0);
  361. }
  362. break;
  363. case MQTT_MSG_TYPE_PUBREC:
  364. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg->msg_id == msg_id){
  365. NODE_DBG("MQTT: Publish with QoS = 2 Received PUBREC\r\n");
  366. // Note: actually, should not destroy the msg until PUBCOMP is received.
  367. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  368. temp_msg = mqtt_msg_pubrel(&mud->mqtt_state.mqtt_connection, msg_id);
  369. msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  370. msg_id, MQTT_MSG_TYPE_PUBREL, (int)mqtt_get_qos(temp_msg->data) );
  371. NODE_DBG("MQTT: Response PUBREL\r\n");
  372. }
  373. break;
  374. case MQTT_MSG_TYPE_PUBREL:
  375. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBREC && pending_msg->msg_id == msg_id){
  376. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  377. temp_msg = mqtt_msg_pubcomp(&mud->mqtt_state.mqtt_connection, msg_id);
  378. msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  379. msg_id, MQTT_MSG_TYPE_PUBCOMP, (int)mqtt_get_qos(temp_msg->data) );
  380. NODE_DBG("MQTT: Response PUBCOMP\r\n");
  381. }
  382. break;
  383. case MQTT_MSG_TYPE_PUBCOMP:
  384. if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBREL && pending_msg->msg_id == msg_id){
  385. NODE_DBG("MQTT: Publish with QoS = 2 successful\r\n");
  386. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  387. if(mud->cb_puback_ref == LUA_NOREF)
  388. break;
  389. if(mud->self_ref == LUA_NOREF)
  390. break;
  391. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
  392. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata to callback func in lua
  393. lua_call(L, 1, 0);
  394. }
  395. break;
  396. case MQTT_MSG_TYPE_PINGREQ:
  397. temp_msg = mqtt_msg_pingresp(&mud->mqtt_state.mqtt_connection);
  398. msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  399. msg_id, MQTT_MSG_TYPE_PINGRESP, (int)mqtt_get_qos(temp_msg->data) );
  400. NODE_DBG("MQTT: Response PINGRESP\r\n");
  401. break;
  402. case MQTT_MSG_TYPE_PINGRESP:
  403. // Ignore
  404. mud->keepalive_sent = 0;
  405. NODE_DBG("MQTT: PINGRESP received\r\n");
  406. break;
  407. }
  408. // NOTE: this is done down here and not in the switch case above
  409. // because the PSOCK_READBUF_LEN() won't work inside a switch
  410. // statement due to the way protothreads resume.
  411. if(msg_type == MQTT_MSG_TYPE_PUBLISH)
  412. {
  413. length = mud->mqtt_state.message_length_read;
  414. if(mud->mqtt_state.message_length < mud->mqtt_state.message_length_read)
  415. {
  416. length -= mud->mqtt_state.message_length;
  417. in_buffer += mud->mqtt_state.message_length;
  418. NODE_DBG("Get another published message\r\n");
  419. goto READPACKET;
  420. }
  421. }
  422. break;
  423. }
  424. mqtt_send_if_possible(pesp_conn);
  425. NODE_DBG("leave mqtt_socket_received.\n");
  426. return;
  427. }
  428. static void mqtt_socket_sent(void *arg)
  429. {
  430. NODE_DBG("enter mqtt_socket_sent.\n");
  431. struct espconn *pesp_conn = arg;
  432. if(pesp_conn == NULL)
  433. return;
  434. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  435. if(mud == NULL)
  436. return;
  437. if(!mud->connected)
  438. return;
  439. // call mqtt_sent()
  440. mud->event_timeout = 0;
  441. mud->keep_alive_tick = 0;
  442. if(mud->connState == MQTT_CONNECT_SENDING){
  443. mud->connState = MQTT_CONNECT_SENT;
  444. mud->event_timeout = MQTT_SEND_TIMEOUT;
  445. // MQTT_CONNECT not queued.
  446. return;
  447. }
  448. NODE_DBG("sent1, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  449. uint8_t try_send = 1;
  450. // qos = 0, publish and forgot.
  451. msg_queue_t *node = msg_peek(&(mud->mqtt_state.pending_msg_q));
  452. if(node && node->msg_type == MQTT_MSG_TYPE_PUBLISH && node->publish_qos == 0) {
  453. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  454. if(mud->cb_puback_ref != LUA_NOREF && mud->self_ref != LUA_NOREF) {
  455. lua_State *L = lua_getstate();
  456. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
  457. lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref); // pass the userdata to callback func in lua
  458. lua_call(L, 1, 0);
  459. }
  460. } else if(node && node->msg_type == MQTT_MSG_TYPE_PUBACK) {
  461. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  462. } else if(node && node->msg_type == MQTT_MSG_TYPE_PUBCOMP) {
  463. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  464. } else if(node && node->msg_type == MQTT_MSG_TYPE_PINGREQ) {
  465. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  466. } else {
  467. try_send = 0;
  468. }
  469. if (try_send) {
  470. mqtt_send_if_possible(mud->pesp_conn);
  471. }
  472. NODE_DBG("sent2, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  473. NODE_DBG("leave mqtt_socket_sent.\n");
  474. }
  475. static void mqtt_socket_connected(void *arg)
  476. {
  477. NODE_DBG("enter mqtt_socket_connected.\n");
  478. struct espconn *pesp_conn = arg;
  479. if(pesp_conn == NULL)
  480. return;
  481. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  482. if(mud == NULL)
  483. return;
  484. mud->connected = true;
  485. espconn_regist_recvcb(pesp_conn, mqtt_socket_received);
  486. espconn_regist_sentcb(pesp_conn, mqtt_socket_sent);
  487. espconn_regist_disconcb(pesp_conn, mqtt_socket_disconnected);
  488. uint8_t temp_buffer[MQTT_BUF_SIZE];
  489. // call mqtt_connect() to start a mqtt connect stage.
  490. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  491. mqtt_message_t* temp_msg = mqtt_msg_connect(&mud->mqtt_state.mqtt_connection, mud->mqtt_state.connect_info);
  492. NODE_DBG("Send MQTT connection infomation, data len: %d, d[0]=%d \r\n", temp_msg->length, temp_msg->data[0]);
  493. mud->event_timeout = MQTT_SEND_TIMEOUT;
  494. // not queue this message. should send right now. or should enqueue this before head.
  495. #ifdef CLIENT_SSL_ENABLE
  496. if(mud->secure)
  497. {
  498. espconn_secure_send(pesp_conn, temp_msg->data, temp_msg->length);
  499. }
  500. else
  501. #endif
  502. {
  503. espconn_send(pesp_conn, temp_msg->data, temp_msg->length);
  504. }
  505. mud->keep_alive_tick = 0;
  506. mud->connState = MQTT_CONNECT_SENDING;
  507. NODE_DBG("leave mqtt_socket_connected.\n");
  508. return;
  509. }
  510. void mqtt_socket_timer(void *arg)
  511. {
  512. NODE_DBG("enter mqtt_socket_timer.\n");
  513. lmqtt_userdata *mud = (lmqtt_userdata*) arg;
  514. if(mud == NULL)
  515. return;
  516. if(mud->pesp_conn == NULL){
  517. NODE_DBG("mud->pesp_conn is NULL.\n");
  518. os_timer_disarm(&mud->mqttTimer);
  519. return;
  520. }
  521. NODE_DBG("timer, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  522. if(mud->event_timeout > 0){
  523. NODE_DBG("event_timeout: %d.\n", mud->event_timeout);
  524. mud->event_timeout --;
  525. if(mud->event_timeout > 0){
  526. return;
  527. } else {
  528. NODE_DBG("event timeout. \n");
  529. if(mud->connState == MQTT_DATA)
  530. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  531. // should remove the head of the queue and re-send with DUP = 1
  532. // Not implemented yet.
  533. }
  534. }
  535. if(mud->connState == MQTT_INIT){ // socket connect time out.
  536. NODE_DBG("Can not connect to broker.\n");
  537. os_timer_disarm(&mud->mqttTimer);
  538. mqtt_connack_fail(mud, MQTT_CONN_FAIL_SERVER_NOT_FOUND);
  539. } else if(mud->connState == MQTT_CONNECT_SENDING){ // MQTT_CONNECT send time out.
  540. NODE_DBG("sSend MQTT_CONNECT failed.\n");
  541. mud->connState = MQTT_INIT;
  542. mqtt_connack_fail(mud, MQTT_CONN_FAIL_TIMEOUT_SENDING);
  543. #ifdef CLIENT_SSL_ENABLE
  544. if(mud->secure)
  545. {
  546. espconn_secure_disconnect(mud->pesp_conn);
  547. }
  548. else
  549. #endif
  550. {
  551. espconn_disconnect(mud->pesp_conn);
  552. }
  553. mud->keep_alive_tick = 0; // not need count anymore
  554. } else if(mud->connState == MQTT_CONNECT_SENT) { // wait for CONACK time out.
  555. NODE_DBG("MQTT_CONNECT timeout.\n");
  556. mud->connState = MQTT_INIT;
  557. #ifdef CLIENT_SSL_ENABLE
  558. if(mud->secure)
  559. {
  560. espconn_secure_disconnect(mud->pesp_conn);
  561. }
  562. else
  563. #endif
  564. {
  565. espconn_disconnect(mud->pesp_conn);
  566. }
  567. mqtt_connack_fail(mud, MQTT_CONN_FAIL_TIMEOUT_RECEIVING);
  568. } else if(mud->connState == MQTT_DATA){
  569. msg_queue_t *pending_msg = msg_peek(&(mud->mqtt_state.pending_msg_q));
  570. if(pending_msg){
  571. mqtt_send_if_possible(mud->pesp_conn);
  572. } else {
  573. // no queued event.
  574. mud->keep_alive_tick ++;
  575. if(mud->keep_alive_tick > mud->mqtt_state.connect_info->keepalive){
  576. if (mud->keepalive_sent) {
  577. // Oh dear -- keepalive timer expired and still no ack of previous message
  578. mqtt_socket_reconnected(mud->pesp_conn, 0);
  579. } else {
  580. uint8_t temp_buffer[MQTT_BUF_SIZE];
  581. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  582. NODE_DBG("\r\nMQTT: Send keepalive packet\r\n");
  583. mqtt_message_t* temp_msg = mqtt_msg_pingreq(&mud->mqtt_state.mqtt_connection);
  584. msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
  585. 0, MQTT_MSG_TYPE_PINGREQ, (int)mqtt_get_qos(temp_msg->data) );
  586. mud->keepalive_sent = 1;
  587. mud->keep_alive_tick = 0; // Need to reset to zero in case flow control stopped.
  588. mqtt_send_if_possible(mud->pesp_conn);
  589. }
  590. }
  591. }
  592. }
  593. NODE_DBG("keep_alive_tick: %d\n", mud->keep_alive_tick);
  594. NODE_DBG("leave mqtt_socket_timer.\n");
  595. }
  596. // Lua: mqtt.Client(clientid, keepalive, user, pass, clean_session)
  597. static int mqtt_socket_client( lua_State* L )
  598. {
  599. NODE_DBG("enter mqtt_socket_client.\n");
  600. lmqtt_userdata *mud;
  601. char tempid[20] = {0};
  602. c_sprintf(tempid, "%s%x", "NodeMCU_", system_get_chip_id() );
  603. NODE_DBG(tempid);
  604. NODE_DBG("\n");
  605. const char *clientId = tempid, *username = NULL, *password = NULL;
  606. size_t idl = c_strlen(tempid);
  607. size_t unl = 0, pwl = 0;
  608. int keepalive = 0;
  609. int stack = 1;
  610. int clean_session = 1;
  611. int top = lua_gettop(L);
  612. // create a object
  613. mud = (lmqtt_userdata *)lua_newuserdata(L, sizeof(lmqtt_userdata));
  614. c_memset(mud, 0, sizeof(*mud));
  615. // pre-initialize it, in case of errors
  616. mud->self_ref = LUA_NOREF;
  617. mud->cb_connect_ref = LUA_NOREF;
  618. mud->cb_connect_fail_ref = LUA_NOREF;
  619. mud->cb_disconnect_ref = LUA_NOREF;
  620. mud->cb_message_ref = LUA_NOREF;
  621. mud->cb_suback_ref = LUA_NOREF;
  622. mud->cb_unsuback_ref = LUA_NOREF;
  623. mud->cb_puback_ref = LUA_NOREF;
  624. mud->connState = MQTT_INIT;
  625. // set its metatable
  626. luaL_getmetatable(L, "mqtt.socket");
  627. lua_setmetatable(L, -2);
  628. if( lua_isstring(L,stack) ) // deal with the clientid string
  629. {
  630. clientId = luaL_checklstring( L, stack, &idl );
  631. stack++;
  632. }
  633. if(lua_isnumber( L, stack ))
  634. {
  635. keepalive = luaL_checkinteger( L, stack);
  636. stack++;
  637. }
  638. if(keepalive == 0){
  639. keepalive = MQTT_DEFAULT_KEEPALIVE;
  640. }
  641. if(lua_isstring( L, stack )){
  642. username = luaL_checklstring( L, stack, &unl );
  643. stack++;
  644. }
  645. if(username == NULL)
  646. unl = 0;
  647. NODE_DBG("length username: %d\r\n", unl);
  648. if(lua_isstring( L, stack )){
  649. password = luaL_checklstring( L, stack, &pwl );
  650. stack++;
  651. }
  652. if(password == NULL)
  653. pwl = 0;
  654. NODE_DBG("length password: %d\r\n", pwl);
  655. if(lua_isnumber( L, stack ))
  656. {
  657. clean_session = luaL_checkinteger( L, stack);
  658. stack++;
  659. }
  660. if(clean_session > 1){
  661. clean_session = 1;
  662. }
  663. // TODO: check the zalloc result.
  664. mud->connect_info.client_id = (uint8_t *)c_zalloc(idl+1);
  665. mud->connect_info.username = (uint8_t *)c_zalloc(unl + 1);
  666. mud->connect_info.password = (uint8_t *)c_zalloc(pwl + 1);
  667. if(!mud->connect_info.client_id || !mud->connect_info.username || !mud->connect_info.password){
  668. if(mud->connect_info.client_id) {
  669. c_free(mud->connect_info.client_id);
  670. mud->connect_info.client_id = NULL;
  671. }
  672. if(mud->connect_info.username) {
  673. c_free(mud->connect_info.username);
  674. mud->connect_info.username = NULL;
  675. }
  676. if(mud->connect_info.password) {
  677. c_free(mud->connect_info.password);
  678. mud->connect_info.password = NULL;
  679. }
  680. return luaL_error(L, "not enough memory");
  681. }
  682. c_memcpy(mud->connect_info.client_id, clientId, idl);
  683. mud->connect_info.client_id[idl] = 0;
  684. c_memcpy(mud->connect_info.username, username, unl);
  685. mud->connect_info.username[unl] = 0;
  686. c_memcpy(mud->connect_info.password, password, pwl);
  687. mud->connect_info.password[pwl] = 0;
  688. NODE_DBG("MQTT: Init info: %s, %s, %s\r\n", mud->connect_info.client_id, mud->connect_info.username, mud->connect_info.password);
  689. mud->connect_info.clean_session = clean_session;
  690. mud->connect_info.will_qos = 0;
  691. mud->connect_info.will_retain = 0;
  692. mud->connect_info.keepalive = keepalive;
  693. mud->mqtt_state.pending_msg_q = NULL;
  694. mud->mqtt_state.auto_reconnect = 0;
  695. mud->mqtt_state.port = 1883;
  696. mud->mqtt_state.connect_info = &mud->connect_info;
  697. NODE_DBG("leave mqtt_socket_client.\n");
  698. return 1;
  699. }
  700. // Lua: mqtt.delete( socket )
  701. // call close() first
  702. // socket: unref everything
  703. static int mqtt_delete( lua_State* L )
  704. {
  705. NODE_DBG("enter mqtt_delete.\n");
  706. lmqtt_userdata *mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
  707. luaL_argcheck(L, mud, 1, "mqtt.socket expected");
  708. if(mud==NULL){
  709. NODE_DBG("userdata is nil.\n");
  710. return 0;
  711. }
  712. os_timer_disarm(&mud->mqttTimer);
  713. mud->connected = false;
  714. // ---- alloc-ed in mqtt_socket_connect()
  715. if(mud->pesp_conn){ // for client connected to tcp server, this should set NULL in disconnect cb
  716. mud->pesp_conn->reverse = NULL;
  717. if(mud->pesp_conn->proto.tcp)
  718. c_free(mud->pesp_conn->proto.tcp);
  719. mud->pesp_conn->proto.tcp = NULL;
  720. c_free(mud->pesp_conn);
  721. mud->pesp_conn = NULL; // for socket, it will free this when disconnected
  722. }
  723. while(mud->mqtt_state.pending_msg_q) {
  724. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  725. }
  726. // ---- alloc-ed in mqtt_socket_lwt()
  727. if(mud->connect_info.will_topic){
  728. c_free(mud->connect_info.will_topic);
  729. mud->connect_info.will_topic = NULL;
  730. }
  731. if(mud->connect_info.will_message){
  732. c_free(mud->connect_info.will_message);
  733. mud->connect_info.will_message = NULL;
  734. }
  735. // ----
  736. //--------- alloc-ed in mqtt_socket_client()
  737. if(mud->connect_info.client_id){
  738. c_free(mud->connect_info.client_id);
  739. mud->connect_info.client_id = NULL;
  740. }
  741. if(mud->connect_info.username){
  742. c_free(mud->connect_info.username);
  743. mud->connect_info.username = NULL;
  744. }
  745. if(mud->connect_info.password){
  746. c_free(mud->connect_info.password);
  747. mud->connect_info.password = NULL;
  748. }
  749. // -------
  750. // free (unref) callback ref
  751. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
  752. mud->cb_connect_ref = LUA_NOREF;
  753. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
  754. mud->cb_connect_fail_ref = LUA_NOREF;
  755. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
  756. mud->cb_disconnect_ref = LUA_NOREF;
  757. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
  758. mud->cb_message_ref = LUA_NOREF;
  759. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_suback_ref);
  760. mud->cb_suback_ref = LUA_NOREF;
  761. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref);
  762. mud->cb_unsuback_ref = LUA_NOREF;
  763. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
  764. mud->cb_puback_ref = LUA_NOREF;
  765. lua_gc(L, LUA_GCSTOP, 0);
  766. luaL_unref(L, LUA_REGISTRYINDEX, mud->self_ref);
  767. mud->self_ref = LUA_NOREF;
  768. lua_gc(L, LUA_GCRESTART, 0);
  769. NODE_DBG("leave mqtt_delete.\n");
  770. return 0;
  771. }
  772. static sint8 socket_connect(struct espconn *pesp_conn)
  773. {
  774. NODE_DBG("enter socket_connect.\n");
  775. sint8 espconn_status;
  776. if(pesp_conn == NULL)
  777. return ESPCONN_CONN;
  778. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  779. if(mud == NULL)
  780. return ESPCONN_ARG;
  781. mud->event_timeout = MQTT_CONNECT_TIMEOUT;
  782. mud->connState = MQTT_INIT;
  783. #ifdef CLIENT_SSL_ENABLE
  784. if(mud->secure)
  785. {
  786. espconn_status = espconn_secure_connect(pesp_conn);
  787. }
  788. else
  789. #endif
  790. {
  791. espconn_status = espconn_connect(pesp_conn);
  792. }
  793. os_timer_arm(&mud->mqttTimer, 1000, 1);
  794. NODE_DBG("leave socket_connect, heap = %u.\n", system_get_free_heap_size());
  795. return espconn_status;
  796. }
  797. static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg);
  798. static int dns_reconn_count = 0;
  799. static ip_addr_t host_ip; // for dns
  800. /* wrapper for using socket_dns_found() as callback function */
  801. static void socket_dns_foundcb(const char *name, ip_addr_t *ipaddr, void *arg)
  802. {
  803. socket_dns_found(name, ipaddr, arg);
  804. }
  805. static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  806. {
  807. NODE_DBG("enter socket_dns_found.\n");
  808. sint8 espconn_status = ESPCONN_OK;
  809. struct espconn *pesp_conn = arg;
  810. if(pesp_conn == NULL){
  811. NODE_DBG("pesp_conn null.\n");
  812. return -1;
  813. }
  814. if(ipaddr == NULL)
  815. {
  816. dns_reconn_count++;
  817. if( dns_reconn_count >= 5 ){
  818. NODE_ERR( "DNS Fail!\n" );
  819. // Note: should delete the pesp_conn or unref self_ref here.
  820. struct espconn *pesp_conn = arg;
  821. if(pesp_conn != NULL) {
  822. lmqtt_userdata *mud = (lmqtt_userdata *)pesp_conn->reverse;
  823. if(mud != NULL) {
  824. mqtt_connack_fail(mud, MQTT_CONN_FAIL_DNS);
  825. }
  826. }
  827. mqtt_socket_disconnected(arg); // although not connected, but fire disconnect callback to release every thing.
  828. return -1;
  829. }
  830. NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
  831. host_ip.addr = 0;
  832. return espconn_gethostbyname(pesp_conn, name, &host_ip, socket_dns_foundcb);
  833. }
  834. // ipaddr->addr is a uint32_t ip
  835. if(ipaddr->addr != 0)
  836. {
  837. dns_reconn_count = 0;
  838. c_memcpy(pesp_conn->proto.tcp->remote_ip, &(ipaddr->addr), 4);
  839. NODE_DBG("TCP ip is set: ");
  840. NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
  841. NODE_DBG("\n");
  842. espconn_status = socket_connect(pesp_conn);
  843. }
  844. NODE_DBG("leave socket_dns_found.\n");
  845. return espconn_status;
  846. }
  847. // Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client), function(client, connect_return_code) )
  848. static int mqtt_socket_connect( lua_State* L )
  849. {
  850. NODE_DBG("enter mqtt_socket_connect.\n");
  851. lmqtt_userdata *mud = NULL;
  852. unsigned port = 1883;
  853. size_t il;
  854. ip_addr_t ipaddr;
  855. const char *domain;
  856. int stack = 1;
  857. unsigned secure = 0, auto_reconnect = 0;
  858. int top = lua_gettop(L);
  859. sint8 espconn_status;
  860. mud = (lmqtt_userdata *)luaL_checkudata(L, stack, "mqtt.socket");
  861. luaL_argcheck(L, mud, stack, "mqtt.socket expected");
  862. stack++;
  863. if(mud == NULL)
  864. return 0;
  865. if(mud->connected){
  866. return luaL_error(L, "already connected");
  867. }
  868. struct espconn *pesp_conn = mud->pesp_conn;
  869. if(!pesp_conn) {
  870. pesp_conn = mud->pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn));
  871. } else {
  872. espconn_delete(pesp_conn);
  873. }
  874. if(!pesp_conn)
  875. return luaL_error(L, "not enough memory");
  876. if (!pesp_conn->proto.tcp)
  877. pesp_conn->proto.tcp = (esp_tcp *)c_zalloc(sizeof(esp_tcp));
  878. if(!pesp_conn->proto.tcp){
  879. c_free(pesp_conn);
  880. pesp_conn = mud->pesp_conn = NULL;
  881. return luaL_error(L, "not enough memory");
  882. }
  883. // reverse is for the callback function
  884. pesp_conn->reverse = mud;
  885. pesp_conn->type = ESPCONN_TCP;
  886. pesp_conn->state = ESPCONN_NONE;
  887. mud->connected = false;
  888. if( (stack<=top) && lua_isstring(L,stack) ) // deal with the domain string
  889. {
  890. domain = luaL_checklstring( L, stack, &il );
  891. stack++;
  892. if (domain == NULL)
  893. {
  894. domain = "127.0.0.1";
  895. }
  896. ipaddr.addr = ipaddr_addr(domain);
  897. c_memcpy(pesp_conn->proto.tcp->remote_ip, &ipaddr.addr, 4);
  898. NODE_DBG("TCP ip is set: ");
  899. NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
  900. NODE_DBG("\n");
  901. }
  902. if ( (stack<=top) && lua_isnumber(L, stack) )
  903. {
  904. port = lua_tointeger(L, stack);
  905. stack++;
  906. NODE_DBG("TCP port is set: %d.\n", port);
  907. }
  908. pesp_conn->proto.tcp->remote_port = port;
  909. if (pesp_conn->proto.tcp->local_port == 0)
  910. pesp_conn->proto.tcp->local_port = espconn_port();
  911. mud->mqtt_state.port = port;
  912. if ( (stack<=top) && lua_isnumber(L, stack) )
  913. {
  914. secure = lua_tointeger(L, stack);
  915. stack++;
  916. if ( secure != 0 && secure != 1 ){
  917. secure = 0; // default to 0
  918. }
  919. } else {
  920. secure = 0; // default to 0
  921. }
  922. #ifdef CLIENT_SSL_ENABLE
  923. mud->secure = secure; // save
  924. #else
  925. if ( secure )
  926. {
  927. return luaL_error(L, "ssl not available");
  928. }
  929. #endif
  930. if ( (stack<=top) && lua_isnumber(L, stack) )
  931. {
  932. auto_reconnect = lua_tointeger(L, stack);
  933. stack++;
  934. if ( auto_reconnect != 0 && auto_reconnect != 1 ){
  935. auto_reconnect = 0; // default to 0
  936. }
  937. } else {
  938. auto_reconnect = 0; // default to 0
  939. }
  940. mud->mqtt_state.auto_reconnect = auto_reconnect;
  941. // call back function when a connection is obtained, tcp only
  942. if ((stack<=top) && (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)){
  943. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  944. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
  945. mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  946. }
  947. stack++;
  948. // call back function when a connection fails
  949. if ((stack<=top) && (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)){
  950. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  951. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
  952. mud->cb_connect_fail_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  953. stack++;
  954. }
  955. lua_pushvalue(L, 1); // copy userdata to the top of stack
  956. luaL_unref(L, LUA_REGISTRYINDEX, mud->self_ref);
  957. mud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  958. espconn_status = espconn_regist_connectcb(pesp_conn, mqtt_socket_connected);
  959. espconn_status |= espconn_regist_reconcb(pesp_conn, mqtt_socket_reconnected);
  960. os_timer_disarm(&mud->mqttTimer);
  961. os_timer_setfn(&mud->mqttTimer, (os_timer_func_t *)mqtt_socket_timer, mud);
  962. // timer started in socket_connect()
  963. if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0))
  964. {
  965. host_ip.addr = 0;
  966. dns_reconn_count = 0;
  967. if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, socket_dns_foundcb)){
  968. espconn_status |= socket_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip.
  969. }
  970. }
  971. else
  972. {
  973. espconn_status |= socket_connect(pesp_conn);
  974. }
  975. NODE_DBG("leave mqtt_socket_connect.\n");
  976. if (espconn_status == ESPCONN_OK) {
  977. lua_pushboolean(L, 1);
  978. } else {
  979. lua_pushboolean(L, 0);
  980. }
  981. return 1;
  982. }
  983. // Lua: mqtt:close()
  984. // client disconnect and unref itself
  985. static int mqtt_socket_close( lua_State* L )
  986. {
  987. NODE_DBG("enter mqtt_socket_close.\n");
  988. int i = 0;
  989. lmqtt_userdata *mud = NULL;
  990. mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
  991. luaL_argcheck(L, mud, 1, "mqtt.socket expected");
  992. if (mud == NULL || mud->pesp_conn == NULL) {
  993. lua_pushboolean(L, 0);
  994. return 1;
  995. }
  996. mud->mqtt_state.auto_reconnect = 0; // stop auto reconnect.
  997. sint8 espconn_status = ESPCONN_CONN;
  998. if (mud->connected) {
  999. // Send disconnect message
  1000. mqtt_message_t* temp_msg = mqtt_msg_disconnect(&mud->mqtt_state.mqtt_connection);
  1001. NODE_DBG("Send MQTT disconnect infomation, data len: %d, d[0]=%d \r\n", temp_msg->length, temp_msg->data[0]);
  1002. #ifdef CLIENT_SSL_ENABLE
  1003. if(mud->secure) {
  1004. espconn_status = espconn_secure_send(mud->pesp_conn, temp_msg->data, temp_msg->length);
  1005. if(mud->pesp_conn->proto.tcp->remote_port || mud->pesp_conn->proto.tcp->local_port)
  1006. espconn_status |= espconn_secure_disconnect(mud->pesp_conn);
  1007. } else
  1008. #endif
  1009. {
  1010. espconn_status = espconn_send(mud->pesp_conn, temp_msg->data, temp_msg->length);
  1011. if(mud->pesp_conn->proto.tcp->remote_port || mud->pesp_conn->proto.tcp->local_port)
  1012. espconn_status |= espconn_disconnect(mud->pesp_conn);
  1013. }
  1014. }
  1015. mud->connected = 0;
  1016. while (mud->mqtt_state.pending_msg_q) {
  1017. msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
  1018. }
  1019. NODE_DBG("leave mqtt_socket_close.\n");
  1020. if (espconn_status == ESPCONN_OK) {
  1021. lua_pushboolean(L, 1);
  1022. } else {
  1023. lua_pushboolean(L, 0);
  1024. }
  1025. return 1;
  1026. }
  1027. // Lua: mqtt:on( "method", function() )
  1028. static int mqtt_socket_on( lua_State* L )
  1029. {
  1030. NODE_DBG("enter mqtt_socket_on.\n");
  1031. lmqtt_userdata *mud;
  1032. size_t sl;
  1033. mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
  1034. luaL_argcheck(L, mud, 1, "mqtt.socket expected");
  1035. if(mud==NULL){
  1036. NODE_DBG("userdata is nil.\n");
  1037. return 0;
  1038. }
  1039. const char *method = luaL_checklstring( L, 2, &sl );
  1040. if (method == NULL)
  1041. return luaL_error( L, "wrong arg type" );
  1042. luaL_checkanyfunction(L, 3);
  1043. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  1044. if( sl == 7 && c_strcmp(method, "connect") == 0){
  1045. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
  1046. mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1047. }else if( sl == 7 && c_strcmp(method, "offline") == 0){
  1048. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
  1049. mud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1050. }else if( sl == 7 && c_strcmp(method, "message") == 0){
  1051. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
  1052. mud->cb_message_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1053. }else{
  1054. lua_pop(L, 1);
  1055. return luaL_error( L, "method not supported" );
  1056. }
  1057. NODE_DBG("leave mqtt_socket_on.\n");
  1058. return 0;
  1059. }
  1060. // Lua: bool = mqtt:unsubscribe(topic, function())
  1061. static int mqtt_socket_unsubscribe( lua_State* L ) {
  1062. NODE_DBG("enter mqtt_socket_unsubscribe.\n");
  1063. uint8_t stack = 1;
  1064. uint16_t msg_id = 0;
  1065. const char *topic;
  1066. size_t il;
  1067. lmqtt_userdata *mud;
  1068. mud = (lmqtt_userdata *) luaL_checkudata( L, stack, "mqtt.socket" );
  1069. luaL_argcheck( L, mud, stack, "mqtt.socket expected" );
  1070. stack++;
  1071. if(mud==NULL){
  1072. NODE_DBG("userdata is nil.\n");
  1073. lua_pushboolean(L, 0);
  1074. return 1;
  1075. }
  1076. if(mud->pesp_conn == NULL){
  1077. NODE_DBG("mud->pesp_conn is NULL.\n");
  1078. lua_pushboolean(L, 0);
  1079. return 1;
  1080. }
  1081. if(!mud->connected){
  1082. luaL_error( L, "not connected" );
  1083. lua_pushboolean(L, 0);
  1084. return 1;
  1085. }
  1086. uint8_t temp_buffer[MQTT_BUF_SIZE];
  1087. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  1088. mqtt_message_t *temp_msg = NULL;
  1089. if( lua_istable( L, stack ) ) {
  1090. NODE_DBG("unsubscribe table\n");
  1091. lua_pushnil( L ); /* first key */
  1092. int topic_count = 0;
  1093. uint8_t overflow = 0;
  1094. while( lua_next( L, stack ) != 0 ) {
  1095. topic = luaL_checkstring( L, -2 );
  1096. if (topic_count == 0) {
  1097. temp_msg = mqtt_msg_unsubscribe_init( &mud->mqtt_state.mqtt_connection, &msg_id );
  1098. }
  1099. temp_msg = mqtt_msg_unsubscribe_topic( &mud->mqtt_state.mqtt_connection, topic );
  1100. topic_count++;
  1101. NODE_DBG("topic: %s - length: %d\n", topic, temp_msg->length);
  1102. if (temp_msg->length == 0) {
  1103. lua_pop(L, 1);
  1104. overflow = 1;
  1105. break; // too long message for the outbuffer.
  1106. }
  1107. lua_pop( L, 1 );
  1108. }
  1109. if (topic_count == 0){
  1110. return luaL_error( L, "no topics found" );
  1111. }
  1112. if (overflow != 0){
  1113. return luaL_error( L, "buffer overflow, can't enqueue all unsubscriptions" );
  1114. }
  1115. temp_msg = mqtt_msg_unsubscribe_fini( &mud->mqtt_state.mqtt_connection );
  1116. if (temp_msg->length == 0) {
  1117. return luaL_error( L, "buffer overflow, can't enqueue all unsubscriptions" );
  1118. }
  1119. stack++;
  1120. } else {
  1121. NODE_DBG("unsubscribe string\n");
  1122. topic = luaL_checklstring( L, stack, &il );
  1123. stack++;
  1124. if( topic == NULL ){
  1125. return luaL_error( L, "need topic name" );
  1126. }
  1127. temp_msg = mqtt_msg_unsubscribe( &mud->mqtt_state.mqtt_connection, topic, &msg_id );
  1128. }
  1129. if( lua_type( L, stack ) == LUA_TFUNCTION || lua_type( L, stack ) == LUA_TLIGHTFUNCTION ) { // TODO: this will overwrite the previous one.
  1130. lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
  1131. luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref );
  1132. mud->cb_unsuback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
  1133. }
  1134. msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
  1135. msg_id, MQTT_MSG_TYPE_UNSUBSCRIBE, (int)mqtt_get_qos(temp_msg->data) );
  1136. NODE_DBG("topic: %s - id: %d - qos: %d, length: %d\n", topic, node->msg_id, node->publish_qos, node->msg.length);
  1137. NODE_DBG("msg_size: %d, event_timeout: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)), mud->event_timeout);
  1138. sint8 espconn_status = ESPCONN_IF;
  1139. espconn_status = mqtt_send_if_possible(mud->pesp_conn);
  1140. if(!node || espconn_status != ESPCONN_OK){
  1141. lua_pushboolean(L, 0);
  1142. } else {
  1143. lua_pushboolean(L, 1); // enqueued succeed.
  1144. }
  1145. NODE_DBG("unsubscribe, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  1146. NODE_DBG("leave mqtt_socket_unsubscribe.\n");
  1147. return 1;
  1148. }
  1149. // Lua: bool = mqtt:subscribe(topic, qos, function())
  1150. static int mqtt_socket_subscribe( lua_State* L ) {
  1151. NODE_DBG("enter mqtt_socket_subscribe.\n");
  1152. uint8_t stack = 1, qos = 0;
  1153. uint16_t msg_id = 0;
  1154. const char *topic;
  1155. size_t il;
  1156. lmqtt_userdata *mud;
  1157. mud = (lmqtt_userdata *) luaL_checkudata( L, stack, "mqtt.socket" );
  1158. luaL_argcheck( L, mud, stack, "mqtt.socket expected" );
  1159. stack++;
  1160. if(mud==NULL){
  1161. NODE_DBG("userdata is nil.\n");
  1162. lua_pushboolean(L, 0);
  1163. return 1;
  1164. }
  1165. if(mud->pesp_conn == NULL){
  1166. NODE_DBG("mud->pesp_conn is NULL.\n");
  1167. lua_pushboolean(L, 0);
  1168. return 1;
  1169. }
  1170. if(!mud->connected){
  1171. luaL_error( L, "not connected" );
  1172. lua_pushboolean(L, 0);
  1173. return 1;
  1174. }
  1175. uint8_t temp_buffer[MQTT_BUF_SIZE];
  1176. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  1177. mqtt_message_t *temp_msg = NULL;
  1178. if( lua_istable( L, stack ) ) {
  1179. NODE_DBG("subscribe table\n");
  1180. lua_pushnil( L ); /* first key */
  1181. int topic_count = 0;
  1182. uint8_t overflow = 0;
  1183. while( lua_next( L, stack ) != 0 ) {
  1184. topic = luaL_checkstring( L, -2 );
  1185. qos = luaL_checkinteger( L, -1 );
  1186. if (topic_count == 0) {
  1187. temp_msg = mqtt_msg_subscribe_init( &mud->mqtt_state.mqtt_connection, &msg_id );
  1188. }
  1189. temp_msg = mqtt_msg_subscribe_topic( &mud->mqtt_state.mqtt_connection, topic, qos );
  1190. topic_count++;
  1191. NODE_DBG("topic: %s - qos: %d, length: %d\n", topic, qos, temp_msg->length);
  1192. if (temp_msg->length == 0) {
  1193. lua_pop(L, 1);
  1194. overflow = 1;
  1195. break; // too long message for the outbuffer.
  1196. }
  1197. lua_pop( L, 1 );
  1198. }
  1199. if (topic_count == 0){
  1200. return luaL_error( L, "no topics found" );
  1201. }
  1202. if (overflow != 0){
  1203. return luaL_error( L, "buffer overflow, can't enqueue all subscriptions" );
  1204. }
  1205. temp_msg = mqtt_msg_subscribe_fini( &mud->mqtt_state.mqtt_connection );
  1206. if (temp_msg->length == 0) {
  1207. return luaL_error( L, "buffer overflow, can't enqueue all subscriptions" );
  1208. }
  1209. stack++;
  1210. } else {
  1211. NODE_DBG("subscribe string\n");
  1212. topic = luaL_checklstring( L, stack, &il );
  1213. stack++;
  1214. if( topic == NULL ){
  1215. return luaL_error( L, "need topic name" );
  1216. }
  1217. qos = luaL_checkinteger( L, stack );
  1218. temp_msg = mqtt_msg_subscribe( &mud->mqtt_state.mqtt_connection, topic, qos, &msg_id );
  1219. stack++;
  1220. }
  1221. if( lua_type( L, stack ) == LUA_TFUNCTION || lua_type( L, stack ) == LUA_TLIGHTFUNCTION ) { // TODO: this will overwrite the previous one.
  1222. lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
  1223. luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_suback_ref );
  1224. mud->cb_suback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
  1225. }
  1226. msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
  1227. msg_id, MQTT_MSG_TYPE_SUBSCRIBE, (int)mqtt_get_qos(temp_msg->data) );
  1228. NODE_DBG("topic: %s - id: %d - qos: %d, length: %d\n", topic, node->msg_id, node->publish_qos, node->msg.length);
  1229. NODE_DBG("msg_size: %d, event_timeout: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)), mud->event_timeout);
  1230. sint8 espconn_status = ESPCONN_IF;
  1231. espconn_status = mqtt_send_if_possible(mud->pesp_conn);
  1232. if(!node || espconn_status != ESPCONN_OK){
  1233. lua_pushboolean(L, 0);
  1234. } else {
  1235. lua_pushboolean(L, 1); // enqueued succeed.
  1236. }
  1237. NODE_DBG("subscribe, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  1238. NODE_DBG("leave mqtt_socket_subscribe.\n");
  1239. return 1;
  1240. }
  1241. // Lua: bool = mqtt:publish( topic, payload, qos, retain, function() )
  1242. static int mqtt_socket_publish( lua_State* L )
  1243. {
  1244. NODE_DBG("enter mqtt_socket_publish.\n");
  1245. struct espconn *pesp_conn = NULL;
  1246. lmqtt_userdata *mud;
  1247. size_t l;
  1248. uint8_t stack = 1;
  1249. uint16_t msg_id = 0;
  1250. mud = (lmqtt_userdata *)luaL_checkudata(L, stack, "mqtt.socket");
  1251. luaL_argcheck(L, mud, stack, "mqtt.socket expected");
  1252. stack++;
  1253. if(mud==NULL){
  1254. NODE_DBG("userdata is nil.\n");
  1255. lua_pushboolean(L, 0);
  1256. return 1;
  1257. }
  1258. if(mud->pesp_conn == NULL){
  1259. NODE_DBG("mud->pesp_conn is NULL.\n");
  1260. lua_pushboolean(L, 0);
  1261. return 1;
  1262. }
  1263. if(!mud->connected){
  1264. return luaL_error( L, "not connected" );
  1265. }
  1266. const char *topic = luaL_checklstring( L, stack, &l );
  1267. stack ++;
  1268. if (topic == NULL){
  1269. return luaL_error( L, "need topic" );
  1270. }
  1271. const char *payload = luaL_checklstring( L, stack, &l );
  1272. stack ++;
  1273. uint8_t qos = luaL_checkinteger( L, stack);
  1274. stack ++;
  1275. uint8_t retain = luaL_checkinteger( L, stack);
  1276. stack ++;
  1277. uint8_t temp_buffer[MQTT_BUF_SIZE];
  1278. mqtt_msg_init(&mud->mqtt_state.mqtt_connection, temp_buffer, MQTT_BUF_SIZE);
  1279. mqtt_message_t *temp_msg = mqtt_msg_publish(&mud->mqtt_state.mqtt_connection,
  1280. topic, payload, l,
  1281. qos, retain,
  1282. &msg_id);
  1283. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  1284. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  1285. luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
  1286. mud->cb_puback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1287. }
  1288. msg_queue_t *node = msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
  1289. msg_id, MQTT_MSG_TYPE_PUBLISH, (int)qos );
  1290. sint8 espconn_status = ESPCONN_OK;
  1291. espconn_status = mqtt_send_if_possible(mud->pesp_conn);
  1292. if(!node || espconn_status != ESPCONN_OK){
  1293. lua_pushboolean(L, 0);
  1294. } else {
  1295. lua_pushboolean(L, 1); // enqueued succeed.
  1296. }
  1297. NODE_DBG("publish, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
  1298. NODE_DBG("leave mqtt_socket_publish.\n");
  1299. return 1;
  1300. }
  1301. // Lua: mqtt:lwt( topic, message, qos, retain, function(client) )
  1302. static int mqtt_socket_lwt( lua_State* L )
  1303. {
  1304. NODE_DBG("enter mqtt_socket_lwt.\n");
  1305. uint8_t stack = 1;
  1306. size_t topicSize, msgSize;
  1307. NODE_DBG("mqtt_socket_lwt.\n");
  1308. lmqtt_userdata *mud = NULL;
  1309. const char *lwtTopic, *lwtMsg;
  1310. uint8_t lwtQoS, lwtRetain;
  1311. mud = (lmqtt_userdata *)luaL_checkudata( L, stack, "mqtt.socket" );
  1312. luaL_argcheck( L, mud, stack, "mqtt.socket expected" );
  1313. if(mud == NULL)
  1314. return 0;
  1315. stack++;
  1316. lwtTopic = luaL_checklstring( L, stack, &topicSize );
  1317. if (lwtTopic == NULL)
  1318. {
  1319. return luaL_error( L, "need lwt topic");
  1320. }
  1321. stack++;
  1322. lwtMsg = luaL_checklstring( L, stack, &msgSize );
  1323. if (lwtMsg == NULL)
  1324. {
  1325. return luaL_error( L, "need lwt message");
  1326. }
  1327. stack++;
  1328. if(mud->connect_info.will_topic){ // free the previous one if there is any
  1329. c_free(mud->connect_info.will_topic);
  1330. mud->connect_info.will_topic = NULL;
  1331. }
  1332. if(mud->connect_info.will_message){
  1333. c_free(mud->connect_info.will_message);
  1334. mud->connect_info.will_message = NULL;
  1335. }
  1336. mud->connect_info.will_topic = (uint8_t*) c_zalloc( topicSize + 1 );
  1337. mud->connect_info.will_message = (uint8_t*) c_zalloc( msgSize + 1 );
  1338. if(!mud->connect_info.will_topic || !mud->connect_info.will_message){
  1339. if(mud->connect_info.will_topic){
  1340. c_free(mud->connect_info.will_topic);
  1341. mud->connect_info.will_topic = NULL;
  1342. }
  1343. if(mud->connect_info.will_message){
  1344. c_free(mud->connect_info.will_message);
  1345. mud->connect_info.will_message = NULL;
  1346. }
  1347. return luaL_error( L, "not enough memory");
  1348. }
  1349. c_memcpy(mud->connect_info.will_topic, lwtTopic, topicSize);
  1350. mud->connect_info.will_topic[topicSize] = 0;
  1351. c_memcpy(mud->connect_info.will_message, lwtMsg, msgSize);
  1352. mud->connect_info.will_message[msgSize] = 0;
  1353. if ( lua_isnumber(L, stack) )
  1354. {
  1355. mud->connect_info.will_qos = lua_tointeger(L, stack);
  1356. stack++;
  1357. }
  1358. if ( lua_isnumber(L, stack) )
  1359. {
  1360. mud->connect_info.will_retain = lua_tointeger(L, stack);
  1361. stack++;
  1362. }
  1363. NODE_DBG("mqtt_socket_lwt: topic: %s, message: %s, qos: %d, retain: %d\n",
  1364. mud->connect_info.will_topic,
  1365. mud->connect_info.will_message,
  1366. mud->connect_info.will_qos,
  1367. mud->connect_info.will_retain);
  1368. NODE_DBG("leave mqtt_socket_lwt.\n");
  1369. return 0;
  1370. }
  1371. // Module function map
  1372. static const LUA_REG_TYPE mqtt_socket_map[] = {
  1373. { LSTRKEY( "connect" ), LFUNCVAL( mqtt_socket_connect ) },
  1374. { LSTRKEY( "close" ), LFUNCVAL( mqtt_socket_close ) },
  1375. { LSTRKEY( "publish" ), LFUNCVAL( mqtt_socket_publish ) },
  1376. { LSTRKEY( "subscribe" ), LFUNCVAL( mqtt_socket_subscribe ) },
  1377. { LSTRKEY( "unsubscribe" ), LFUNCVAL( mqtt_socket_unsubscribe ) },
  1378. { LSTRKEY( "lwt" ), LFUNCVAL( mqtt_socket_lwt ) },
  1379. { LSTRKEY( "on" ), LFUNCVAL( mqtt_socket_on ) },
  1380. { LSTRKEY( "__gc" ), LFUNCVAL( mqtt_delete ) },
  1381. { LSTRKEY( "__index" ), LROVAL( mqtt_socket_map ) },
  1382. { LNILKEY, LNILVAL }
  1383. };
  1384. static const LUA_REG_TYPE mqtt_map[] = {
  1385. { LSTRKEY( "Client" ), LFUNCVAL( mqtt_socket_client ) },
  1386. { LSTRKEY( "CONN_FAIL_SERVER_NOT_FOUND" ), LNUMVAL( MQTT_CONN_FAIL_SERVER_NOT_FOUND ) },
  1387. { LSTRKEY( "CONN_FAIL_NOT_A_CONNACK_MSG" ), LNUMVAL( MQTT_CONN_FAIL_NOT_A_CONNACK_MSG ) },
  1388. { LSTRKEY( "CONN_FAIL_DNS" ), LNUMVAL( MQTT_CONN_FAIL_DNS ) },
  1389. { LSTRKEY( "CONN_FAIL_TIMEOUT_RECEIVING" ), LNUMVAL( MQTT_CONN_FAIL_TIMEOUT_RECEIVING ) },
  1390. { LSTRKEY( "CONN_FAIL_TIMEOUT_SENDING" ), LNUMVAL( MQTT_CONN_FAIL_TIMEOUT_SENDING ) },
  1391. { LSTRKEY( "CONNACK_ACCEPTED" ), LNUMVAL( MQTT_CONNACK_ACCEPTED ) },
  1392. { LSTRKEY( "CONNACK_REFUSED_PROTOCOL_VER" ), LNUMVAL( MQTT_CONNACK_REFUSED_PROTOCOL_VER ) },
  1393. { LSTRKEY( "CONNACK_REFUSED_ID_REJECTED" ), LNUMVAL( MQTT_CONNACK_REFUSED_ID_REJECTED ) },
  1394. { LSTRKEY( "CONNACK_REFUSED_SERVER_UNAVAILABLE" ), LNUMVAL( MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE ) },
  1395. { LSTRKEY( "CONNACK_REFUSED_BAD_USER_OR_PASS" ), LNUMVAL( MQTT_CONNACK_REFUSED_BAD_USER_OR_PASS ) },
  1396. { LSTRKEY( "CONNACK_REFUSED_NOT_AUTHORIZED" ), LNUMVAL( MQTT_CONNACK_REFUSED_NOT_AUTHORIZED ) },
  1397. { LSTRKEY( "__metatable" ), LROVAL( mqtt_map ) },
  1398. { LNILKEY, LNILVAL }
  1399. };
  1400. int luaopen_mqtt( lua_State *L )
  1401. {
  1402. luaL_rometatable(L, "mqtt.socket", (void *)mqtt_socket_map); // create metatable for mqtt.socket
  1403. return 0;
  1404. }
  1405. NODEMCU_MODULE(MQTT, "mqtt", mqtt_map, luaopen_mqtt);