mqtt.c 50 KB

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