subdev_example_api.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "iot_import.h"
  5. #include "lite-utils.h"
  6. #include "subdev_example_api.h"
  7. iotx_thing_masterlave_pt g_thing_masterlave_t = NULL;
  8. #define GET_JSON_VALUE(node, param, buf) \
  9. do { \
  10. (node) = LITE_json_value_of((param), (buf)); \
  11. if((node) == NULL) { \
  12. log_err("get json value error!"); \
  13. return FAIL_RETURN; \
  14. } \
  15. } while(0)
  16. #define GET_JSON_VALUE_WHIH_FREE(node, param, buf) \
  17. do { \
  18. LITE_free(node); \
  19. (node) = LITE_json_value_of((param), (buf)); \
  20. if((node) == NULL) { \
  21. log_err("get json value error!"); \
  22. return FAIL_RETURN; \
  23. } \
  24. } while(0)
  25. #define MALLOC_MEMORY_WITH_RESULT(buffer, length, result) \
  26. do { \
  27. if (buffer) \
  28. LITE_free(buffer); \
  29. (buffer) = (void*)LITE_malloc(length); \
  30. if (NULL == (buffer)) { \
  31. log_err("Not enough memory"); \
  32. return (result); \
  33. } \
  34. memset((buffer), 0x0, (length)); \
  35. } while(0)
  36. typedef struct iotx_thing_subscribe_topic_st {
  37. char* format;
  38. char* params_1;
  39. char* params_2;
  40. char* params_3;
  41. }iotx_thing_subscribe_topic_t, *iotx_thing_subscribe_topic_pt;
  42. extern void iotx_thing_event_handle(void *pcontext, void *pclient, void* msg);
  43. extern iotx_device_info_pt iotx_device_info_get(void);
  44. char* iotx_thing_splice_common_event_packet(const char* params,
  45. const char* event_id,
  46. int32_t* msg_id)
  47. {
  48. #define EVENT_PACKET_FMT "{\"id\":%d,\"version\":\"1.0\",\"params\":%s,\"method\":\"thing.event.%s.post\"}"
  49. int len, ret;
  50. char* msg = NULL;
  51. uint32_t id = 0;
  52. if (params == NULL || msg_id == NULL) {
  53. log_err("input params error!");
  54. return NULL;
  55. }
  56. /* sum the string length */
  57. len = strlen(EVENT_PACKET_FMT) + strlen(params) + strlen(event_id) + 12;
  58. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  59. id = IOT_Gateway_Generate_Message_ID();
  60. ret = HAL_Snprintf(msg,
  61. len,
  62. EVENT_PACKET_FMT,
  63. id,
  64. params,
  65. event_id);
  66. if (ret < 0) {
  67. log_err("splice packet error!");
  68. LITE_free(msg);
  69. return NULL;
  70. }
  71. *msg_id = id;
  72. return msg;
  73. }
  74. char* iotx_thing_splice_common_deviceinfo_packet(const char* deviceinfo,
  75. const char* method,
  76. int32_t* msg_id)
  77. {
  78. #define DEVICEINFO_PACKET_FMT "{\"id\":%d,\"version\":\"1.0\",\"params\":%s,\"method\":\"thing.deviceinfo.%s\"}"
  79. int len, ret;
  80. char* msg = NULL;
  81. uint32_t id = 0;
  82. if (deviceinfo == NULL || method == NULL || msg_id == NULL) {
  83. log_err("input params error!");
  84. return NULL;
  85. }
  86. /* sum the string length */
  87. len = strlen(EVENT_PACKET_FMT) + strlen(deviceinfo) + strlen(method) + 12;
  88. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  89. id = IOT_Gateway_Generate_Message_ID();
  90. ret = HAL_Snprintf(msg,
  91. len,
  92. DEVICEINFO_PACKET_FMT,
  93. id,
  94. deviceinfo,
  95. method);
  96. if (ret < 0) {
  97. log_err("splice packet error!");
  98. LITE_free(msg);
  99. return NULL;
  100. }
  101. *msg_id = id;
  102. return msg;
  103. }
  104. char *iotx_thing_splice_default_reply_get_packet(int32_t msg_id,
  105. uint32_t code,
  106. const char* data)
  107. {
  108. #define SET_DEFAULT_REPLY_GET_PACKET_FMT "{\"id\":%d,\"code\":%d,\"data\":{%s}}"
  109. int len, ret;
  110. char* msg = NULL;
  111. PARAMETER_NULL_CHECK_WITH_RESULT(data, NULL);
  112. /* sum the string length */
  113. len = strlen(SET_DEFAULT_REPLY_GET_PACKET_FMT) + 12 + 4 + strlen(data);
  114. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  115. ret = HAL_Snprintf(msg,
  116. len,
  117. SET_DEFAULT_REPLY_GET_PACKET_FMT,
  118. msg_id,
  119. code,
  120. data);
  121. if (ret < 0) {
  122. log_err("splice packet error!");
  123. LITE_free(msg);
  124. return NULL;
  125. }
  126. return msg;
  127. }
  128. char *iotx_thing_splice_default_reply_packet(int32_t msg_id, uint32_t code)
  129. {
  130. #define SET_DEFAULT_REPLY_PACKET_FMT "{\"id\":%d,\"code\":%d,\"data\":{}}"
  131. int len, ret;
  132. char* msg = NULL;
  133. /* sum the string length */
  134. len = strlen(SET_DEFAULT_REPLY_PACKET_FMT) + 12 + 4;
  135. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  136. ret = HAL_Snprintf(msg,
  137. len,
  138. SET_DEFAULT_REPLY_PACKET_FMT,
  139. msg_id,
  140. code);
  141. if (ret < 0) {
  142. log_err("splice packet error!");
  143. LITE_free(msg);
  144. return NULL;
  145. }
  146. return msg;
  147. }
  148. char *iotx_thing_splice_topo_add_packet(const char* product_key,
  149. const char* device_name,
  150. const char* sign,
  151. const char* sign_method,
  152. const char* timestamp,
  153. const char* client_id,
  154. int32_t* msg_id)
  155. {
  156. #define TOPO_ADD_PACKET_FMT "{\"id\":%d,\"version\":\"1.0\",\"params\":[{\"deviceName\":\"%s\",\"productKey\":\"%s\",\"sign\":\"%s\",\"signMethod\":\"%s\",\"timestamp\":\"%s\",\"clientId\":\"%s\"}],\"method\":\"thing.topo.add\"}"
  157. int len, ret;
  158. char* msg = NULL;
  159. uint32_t id = 0;
  160. if (device_name == NULL || product_key == NULL || sign == NULL || sign_method == NULL || timestamp == NULL || client_id == NULL || msg_id == NULL) {
  161. log_err("input params error!");
  162. return NULL;
  163. }
  164. /* sum the string length */
  165. len = strlen(TOPO_ADD_PACKET_FMT) + strlen(device_name) + strlen(product_key) + strlen(sign)
  166. + strlen(sign_method) + strlen(timestamp) + strlen(client_id) + 12;
  167. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  168. id = IOT_Gateway_Generate_Message_ID();
  169. ret = HAL_Snprintf(msg,
  170. len,
  171. TOPO_ADD_PACKET_FMT,
  172. id,
  173. device_name,
  174. product_key,
  175. sign,
  176. sign_method,
  177. timestamp,
  178. client_id);
  179. if(ret < 0) {
  180. log_err("splice packet error!");
  181. LITE_free(msg);
  182. return NULL;
  183. }
  184. *msg_id = id;
  185. return msg;
  186. }
  187. char* iotx_thing_splice_common_packet(const char* product_key,
  188. const char* device_name,
  189. int32_t* msg_id,
  190. const char* param1,
  191. const char* param2,
  192. int flag)
  193. {
  194. #define COMMON_PACKET_FMT_1 "{\"id\":%d,\"version\":\"1.0\",\"params\":{},\"method\":\"thing.%s.%s\"}"
  195. #define COMMON_PACKET_FMT_2 "{\"id\":%d,\"version\":\"1.0\",\"params\":[{\"deviceName\":\"%s\",\"productKey\":\"%s\"}],\"method\":\"thing.%s.%s\"}"
  196. int len, ret = -1;
  197. char* msg = NULL;
  198. int32_t id = 0;
  199. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, NULL);
  200. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, NULL);
  201. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param1, NULL);
  202. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param2, NULL);
  203. PARAMETER_NULL_CHECK_WITH_RESULT(msg_id, NULL);
  204. /* sum the string length */
  205. len = strlen(COMMON_PACKET_FMT_2) + strlen(device_name) +
  206. strlen(product_key) + 12 + strlen("unregister") + strlen("dsltemplate");
  207. MALLOC_MEMORY_WITH_RESULT(msg, len, NULL);
  208. id = IOT_Gateway_Generate_Message_ID();
  209. if (flag == 0) {
  210. ret = HAL_Snprintf(msg,
  211. len,
  212. COMMON_PACKET_FMT_1,
  213. id,
  214. param1,
  215. param2);
  216. } else {
  217. ret = HAL_Snprintf(msg,
  218. len,
  219. COMMON_PACKET_FMT_2,
  220. id,
  221. device_name,
  222. product_key,
  223. param1,
  224. param2);
  225. }
  226. if(ret < 0) {
  227. log_err("splice packet error!");
  228. LITE_free(msg);
  229. return NULL;
  230. }
  231. *msg_id = id;
  232. return msg;
  233. }
  234. int iotx_thing_publish_common_packet(void* handle,
  235. const char* topic_product_key,
  236. const char* topic_device_name,
  237. const char* packet_product_key,
  238. const char* packet_device_name,
  239. const char* param1,
  240. const char* param2,
  241. const char* param3,
  242. iotx_thing_reply_type_t reply_type)
  243. {
  244. int rc = 0;
  245. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  246. int32_t msg_id = 0;
  247. iotx_mqtt_topic_info_t topic_msg;
  248. char* packet = NULL;
  249. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  250. PARAMETER_Thing_CHECK(thing_t);
  251. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic_product_key, FAIL_RETURN);
  252. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic_device_name, FAIL_RETURN);
  253. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(packet_product_key, FAIL_RETURN);
  254. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(packet_device_name, FAIL_RETURN);
  255. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param1, FAIL_RETURN);
  256. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param2, FAIL_RETURN);
  257. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param3, FAIL_RETURN);
  258. /* 组装topic */
  259. HAL_Snprintf(topic,
  260. GATEWAY_TOPIC_LEN_MAX,
  261. TOPIC_Thing_COMMON_FMT,
  262. topic_product_key,
  263. topic_device_name,
  264. param1,
  265. param2);
  266. memset(thing_t->thing_data_t->replys[reply_type].topic, 0x0, GATEWAY_TOPIC_LEN_MAX);
  267. HAL_Snprintf(thing_t->thing_data_t->replys[reply_type].topic,
  268. GATEWAY_TOPIC_LEN_MAX,
  269. TOPIC_Thing_COMMON_FMT,
  270. topic_product_key,
  271. topic_device_name,
  272. param1,
  273. param3);
  274. /* 组装packet */
  275. if (reply_type == IOTX_Thing_REPLY_DSL_GET) {
  276. packet = iotx_thing_splice_common_packet(packet_product_key, packet_device_name, &msg_id, param1, param2, 0);
  277. } else {
  278. packet = iotx_thing_splice_common_packet(packet_product_key, packet_device_name, &msg_id, param1, param2, 1);
  279. }
  280. /* 组装mqtt packet */
  281. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  282. topic_msg.qos = IOTX_MQTT_QOS0;
  283. topic_msg.retain = 0;
  284. topic_msg.dup = 0;
  285. topic_msg.payload = (void *)packet;
  286. topic_msg.payload_len = strlen(packet);
  287. topic_msg.packet_id = 0;
  288. /* 发送packet */
  289. if (SUCCESS_RETURN != (rc = iotx_thing_publish_topic_sync(thing_t,
  290. msg_id,
  291. topic,
  292. &topic_msg,
  293. reply_type))) {
  294. LITE_free(packet);
  295. return rc;
  296. }
  297. LITE_free(packet);
  298. if (200 == thing_t->thing_data_t->replys[reply_type].code) {
  299. log_info("common publish successfully [%s]", thing_t->thing_data_t->replys[reply_type].topic);
  300. } else {
  301. log_info("common publish error [%s]\n code[%d]", thing_t->thing_data_t->replys[reply_type].topic, thing_t->thing_data_t->replys[reply_type].code);
  302. if (thing_t->thing_data_t->replys[reply_type].data)
  303. LITE_free(thing_t->thing_data_t->replys[reply_type].data);
  304. return (~thing_t->thing_data_t->replys[reply_type].code + 1);
  305. }
  306. return SUCCESS_RETURN;
  307. }
  308. int iotx_thing_publish_common_event_packet(void* handle,
  309. const char* product_key,
  310. const char* device_name,
  311. const char* topic_params,
  312. const char* packet_params,
  313. iotx_thing_reply_type_t reply_type)
  314. {
  315. int rc = 0;
  316. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  317. int32_t msg_id = 0;
  318. iotx_mqtt_topic_info_t topic_msg;
  319. char* packet = NULL;
  320. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  321. PARAMETER_Thing_CHECK(thing_t);
  322. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  323. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  324. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic_params, FAIL_RETURN);
  325. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(packet_params, FAIL_RETURN);
  326. /* 组装topic */
  327. HAL_Snprintf(topic,
  328. GATEWAY_TOPIC_LEN_MAX,
  329. TOPIC_Thing_COMMON_EXT_FMT,
  330. product_key,
  331. device_name,
  332. "event",
  333. topic_params,
  334. "post");
  335. memset(thing_t->thing_data_t->replys[reply_type].topic, 0x0, GATEWAY_TOPIC_LEN_MAX);
  336. HAL_Snprintf(thing_t->thing_data_t->replys[reply_type].topic,
  337. GATEWAY_TOPIC_LEN_MAX,
  338. TOPIC_Thing_COMMON_EXT_FMT,
  339. product_key,
  340. device_name,
  341. "event",
  342. topic_params,
  343. "post_reply");
  344. /* 组装packet */
  345. packet = iotx_thing_splice_common_event_packet(packet_params, topic_params, &msg_id);
  346. /* 组装mqtt packet */
  347. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  348. topic_msg.qos = IOTX_MQTT_QOS0;
  349. topic_msg.retain = 0;
  350. topic_msg.dup = 0;
  351. topic_msg.payload = (void *)packet;
  352. topic_msg.payload_len = strlen(packet);
  353. topic_msg.packet_id = 0;
  354. /* 发送 packet */
  355. if (SUCCESS_RETURN != (rc = iotx_thing_publish_topic_sync(thing_t,
  356. msg_id,
  357. topic,
  358. &topic_msg,
  359. reply_type))) {
  360. LITE_free(packet);
  361. return rc;
  362. }
  363. LITE_free(packet);
  364. if (200 == thing_t->thing_data_t->replys[reply_type].code) {
  365. log_info("common publish successfully [%s]", thing_t->thing_data_t->replys[reply_type].topic);
  366. } else {
  367. log_info("common publish error [%s]\n code[%d]", thing_t->thing_data_t->replys[reply_type].topic, thing_t->thing_data_t->replys[reply_type].code);
  368. if (thing_t->thing_data_t->replys[reply_type].data)
  369. LITE_free(thing_t->thing_data_t->replys[reply_type].data);
  370. return(~thing_t->thing_data_t->replys[reply_type].code + 1);
  371. }
  372. return SUCCESS_RETURN;
  373. }
  374. int iotx_thing_publish_common_deviceinfo_packet(void* handle,
  375. const char* product_key,
  376. const char* device_name,
  377. const char* deviceinfo,
  378. const char* param1,
  379. const char* param2,
  380. iotx_thing_reply_type_t reply_type)
  381. {
  382. int rc = 0;
  383. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  384. int32_t msg_id = 0;
  385. iotx_mqtt_topic_info_t topic_msg;
  386. char* packet = NULL;
  387. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  388. PARAMETER_Thing_CHECK(thing_t);
  389. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  390. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  391. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(deviceinfo, FAIL_RETURN);
  392. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param1, FAIL_RETURN);
  393. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(param2, FAIL_RETURN);
  394. /* 组装topic */
  395. HAL_Snprintf(topic,
  396. GATEWAY_TOPIC_LEN_MAX,
  397. TOPIC_Thing_COMMON_FMT,
  398. product_key,
  399. device_name,
  400. "deviceinfo",
  401. param1);
  402. memset(thing_t->thing_data_t->replys[reply_type].topic, 0x0, GATEWAY_TOPIC_LEN_MAX);
  403. HAL_Snprintf(thing_t->thing_data_t->replys[reply_type].topic,
  404. GATEWAY_TOPIC_LEN_MAX,
  405. TOPIC_Thing_COMMON_FMT,
  406. product_key,
  407. device_name,
  408. "deviceinfo",
  409. param2);
  410. /* 组装packet */
  411. packet = iotx_thing_splice_common_deviceinfo_packet(deviceinfo, param1, &msg_id);
  412. /* 组装mqtt packet */
  413. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  414. topic_msg.qos = IOTX_MQTT_QOS0;
  415. topic_msg.retain = 0;
  416. topic_msg.dup = 0;
  417. topic_msg.payload = (void *)packet;
  418. topic_msg.payload_len = strlen(packet);
  419. topic_msg.packet_id = 0;
  420. /* 发送packet */
  421. if (SUCCESS_RETURN != (rc = iotx_thing_publish_topic_sync(thing_t,
  422. msg_id,
  423. topic,
  424. &topic_msg,
  425. reply_type))) {
  426. LITE_free(packet);
  427. return rc;
  428. }
  429. LITE_free(packet);
  430. if (200 == thing_t->thing_data_t->replys[reply_type].code) {
  431. log_info("common publish successfully [%s]", thing_t->thing_data_t->replys[reply_type].topic);
  432. } else {
  433. log_info("common publish error [%s]\n code[%d]", thing_t->thing_data_t->replys[reply_type].topic, thing_t->thing_data_t->replys[reply_type].code);
  434. if (thing_t->thing_data_t->replys[reply_type].data)
  435. LITE_free(thing_t->thing_data_t->replys[reply_type].data);
  436. return (~thing_t->thing_data_t->replys[reply_type].code + 1);
  437. }
  438. return SUCCESS_RETURN;
  439. }
  440. int iotx_thing_publish_topic_sync(iotx_thing_masterlave_pt thing_t,
  441. int32_t msg_id,
  442. const char* topic,
  443. iotx_mqtt_topic_info_pt topic_msg,
  444. iotx_thing_reply_type_t reply)
  445. {
  446. int rc = 0;
  447. int yiled_count = 0;
  448. PARAMETER_Thing_CHECK(thing_t);
  449. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  450. PARAMETER_NULL_CHECK_WITH_RESULT(topic_msg, FAIL_RETURN);
  451. log_info("topic: [%s] \n", topic);
  452. thing_t->thing_data_t->replys[reply].id = msg_id;
  453. if ((rc = IOT_Gateway_Publish(thing_t->gateway, topic, topic_msg)) < 0) {
  454. log_err("Gateway Publish error!");
  455. return rc;
  456. }
  457. while (msg_id == thing_t->thing_data_t->replys[reply].id) {
  458. if (yiled_count > IOT_GATEWAY_YIELD_MAX_COUNT) {
  459. log_info("yiled max count, time out");
  460. return FAIL_RETURN;
  461. }
  462. IOT_Thing_Yield(thing_t, 200);
  463. yiled_count++;
  464. }
  465. if (thing_t->thing_data_t->replys[reply].id == 0)
  466. return SUCCESS_RETURN;
  467. if (thing_t->thing_data_t->replys[reply].data)
  468. LITE_free(thing_t->thing_data_t->replys[reply].data);
  469. return FAIL_RETURN;
  470. }
  471. int iotx_thing_subscribe_unsubscribe_topic(iotx_thing_masterlave_pt thing_t,
  472. const char* topic,
  473. iotx_thing_subcribe_unsubscribe_types_t is_subscribe)
  474. {
  475. int ret = 0;
  476. int yiled_count = 0;
  477. PARAMETER_Thing_CHECK(thing_t);
  478. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  479. if (IOTX_Thing_SUBSCRIBE_TYPE == is_subscribe) {
  480. ret = IOT_Gateway_Subscribe(thing_t->gateway,
  481. topic,
  482. IOTX_MQTT_QOS0,
  483. iotx_thing_event_handle,
  484. thing_t);
  485. } else {
  486. ret = IOT_Gateway_Unsubscribe(thing_t->gateway, topic);
  487. }
  488. thing_t->thing_data_t->sync_status = ret;
  489. while (ret == thing_t->thing_data_t->sync_status) {
  490. if (yiled_count > IOT_GATEWAY_YIELD_MAX_COUNT) {
  491. log_info("yiled max count, time out");
  492. return FAIL_RETURN;
  493. }
  494. IOT_Thing_Yield(thing_t, 200);
  495. yiled_count++;
  496. }
  497. if (0 == thing_t->thing_data_t->sync_status) {
  498. log_info(" %s successfully", topic);
  499. } else {
  500. log_info(" %s error!", topic);
  501. return FAIL_RETURN;
  502. }
  503. return SUCCESS_RETURN;
  504. }
  505. int iotx_thing_subscribe_unsubscribe_basic(iotx_thing_masterlave_pt thing_t,
  506. iotx_thing_subcribe_unsubscribe_types_t is_subscribe)
  507. {
  508. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  509. int i = 0;
  510. iotx_device_info_pt pdevice_info = iotx_device_info_get();
  511. iotx_thing_subscribe_topic_t basic_subscribe_topic[7] = {
  512. {TOPIC_Thing_COMMON_FMT, "sub", "register_reply", ""},
  513. {TOPIC_Thing_COMMON_FMT, "sub", "unregister_reply", ""},
  514. {TOPIC_Thing_COMMON_FMT, "topo", "add_reply", ""},
  515. {TOPIC_Thing_COMMON_FMT, "topo", "delete_reply", ""},
  516. {TOPIC_Thing_COMMON_FMT, "topo", "get_reply", ""},
  517. {TOPIC_Thing_COMMON_FMT, "config", "get_reply", ""},
  518. {TOPIC_Thing_COMMON_FMT, "list", "found_reply", ""},
  519. };
  520. PARAMETER_Thing_CHECK(thing_t);
  521. for (i = 0; i < 4; i++) {
  522. memset(topic, 0X0, GATEWAY_TOPIC_LEN_MAX);
  523. HAL_Snprintf(topic,
  524. GATEWAY_TOPIC_LEN_MAX,
  525. basic_subscribe_topic[i].format,
  526. pdevice_info->product_key,
  527. pdevice_info->device_name,
  528. basic_subscribe_topic[i].params_1,
  529. basic_subscribe_topic[i].params_2,
  530. basic_subscribe_topic[i].params_3);
  531. if (SUCCESS_RETURN != iotx_thing_subscribe_unsubscribe_topic(thing_t, topic, is_subscribe))
  532. return FAIL_RETURN;
  533. }
  534. return SUCCESS_RETURN;
  535. }
  536. int iotx_thing_subscribe_unsubscribe_enhance(iotx_thing_masterlave_pt thing_t,
  537. const char* product_key,
  538. const char* device_name,
  539. iotx_thing_subcribe_unsubscribe_types_t is_subscribe)
  540. {
  541. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  542. int i = 0;
  543. iotx_thing_subscribe_topic_t enhance_subscribe_topic[6] = {
  544. {TOPIC_Thing_COMMON_FMT, "service", "+", ""},
  545. {TOPIC_Thing_COMMON_EXT_FMT, "event", "+", "post_reply"},
  546. {TOPIC_Thing_COMMON_EXT_FMT, "event", "property", "post_reply"},
  547. {TOPIC_Thing_COMMON_EXT_FMT, "service", "property", "set"},
  548. {TOPIC_Thing_COMMON_EXT_FMT, "service", "property", "get"},
  549. {TOPIC_Thing_COMMON_EXT_FMT, "topo", "add", "notify"}
  550. };
  551. PARAMETER_Thing_CHECK(thing_t);
  552. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  553. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  554. for (i = 0; i < 6; i++) {
  555. memset(topic, 0X0, GATEWAY_TOPIC_LEN_MAX);
  556. HAL_Snprintf(topic,
  557. GATEWAY_TOPIC_LEN_MAX,
  558. enhance_subscribe_topic[i].format,
  559. product_key,
  560. device_name,
  561. enhance_subscribe_topic[i].params_1,
  562. enhance_subscribe_topic[i].params_2,
  563. enhance_subscribe_topic[i].params_3);
  564. if (SUCCESS_RETURN != iotx_thing_subscribe_unsubscribe_topic(thing_t, topic, is_subscribe))
  565. return FAIL_RETURN;
  566. }
  567. return SUCCESS_RETURN;
  568. }
  569. /* parse common_reply result */
  570. static int iotx_thing_common_reply_proc(iotx_thing_masterlave_pt thing_t,
  571. const char* topic,
  572. char* payload,
  573. iotx_thing_reply_type_t type)
  574. {
  575. char* node = NULL;
  576. /* check parameter */
  577. PARAMETER_Thing_CHECK(thing_t);
  578. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(payload, FAIL_RETURN);
  579. /* parse result */
  580. /* there is no "id" in UP_RAW */
  581. if (IOTX_Thing_REPLY_UP_RAW == type) {
  582. log_info("recv up raw reply");
  583. thing_t->thing_data_t->replys[type].id = 0; /* raw data */
  584. GET_JSON_VALUE(node, "code", payload);
  585. thing_t->thing_data_t->replys[type].code = atoi(node);
  586. } else {
  587. GET_JSON_VALUE(node, "id", payload);
  588. if (thing_t->thing_data_t->replys[type].id == atoi(node)) {
  589. thing_t->thing_data_t->replys[type].id = 0;
  590. }
  591. GET_JSON_VALUE_WHIH_FREE(node, "code", payload);
  592. thing_t->thing_data_t->replys[type].code = atoi(node);
  593. }
  594. GET_JSON_VALUE_WHIH_FREE(node, "data", payload);
  595. MALLOC_MEMORY_WITH_FREE_AND_RESULT(thing_t->thing_data_t->replys[type].data, strlen(node) + 1, node, FAIL_RETURN);
  596. strncpy(thing_t->thing_data_t->replys[type].data, node, strlen(node));
  597. LITE_free(node);
  598. node = NULL;
  599. return SUCCESS_RETURN;
  600. }
  601. /* parse product_key and device_name form topic */
  602. static int iotx_thing_parse_topic(char* topic, char* product_key, char* device_name)
  603. {
  604. char* temp = NULL;
  605. char* start = NULL;
  606. /* check parameter */
  607. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  608. PARAMETER_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  609. PARAMETER_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  610. if (0 == strncmp(topic, "/sys/", strlen("/sys/"))) {
  611. /* productKey */
  612. start = topic + strlen("/sys/");
  613. temp = strchr(start, '/');
  614. if (temp == NULL || temp - start > PRODUCT_KEY_LEN) {
  615. log_info("productKey parse error");
  616. return FAIL_RETURN;
  617. }
  618. strncpy(product_key, start, temp - start);
  619. product_key[temp - start] = '\0';
  620. /* deviceName */
  621. start = temp + 1;
  622. temp = strchr(start, '/');
  623. if (temp == NULL || temp - start > DEVICE_NAME_LEN) {
  624. log_info("deviceName parse error");
  625. return FAIL_RETURN;
  626. }
  627. strncpy(device_name, start, temp - start);
  628. device_name[temp - start] = '\0';
  629. return SUCCESS_RETURN;
  630. } else {
  631. log_info("topic error");
  632. return FAIL_RETURN;
  633. }
  634. }
  635. /* handle service request */
  636. static int iotx_thing_service_request_proc(iotx_thing_masterlave_pt thing_t,
  637. char* topic,
  638. char* payload)
  639. {
  640. char product_key[PRODUCT_KEY_LEN] = {0};
  641. char device_name[DEVICE_NAME_LEN] = {0};
  642. iotx_thing_service_type_t service_type = IOTX_Thing_SERVICE_TYPE_PROPERTY_GET;
  643. char* service_id = NULL, *temp = NULL, *node = NULL, *params = NULL;
  644. int message_id = 0;
  645. /* parameter check */
  646. PARAMETER_Thing_CHECK(thing_t);
  647. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  648. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(payload, FAIL_RETURN);
  649. log_info("receive service request");
  650. /* parse */
  651. if (SUCCESS_RETURN == iotx_thing_parse_topic(topic, product_key, device_name) &&
  652. (NULL != thing_t->service_callback)) {
  653. temp = strstr(topic, "/thing/service");
  654. if (temp == NULL) {
  655. log_err("parse error");
  656. return FAIL_RETURN;
  657. }
  658. /* service type */
  659. temp = strstr(topic, "/thing/service/property");
  660. if (temp) {
  661. temp = temp + strlen("/thing/service/property");
  662. if (0 == strncmp(temp, "/set", strlen("/set"))) {
  663. service_type = IOTX_Thing_SERVICE_TYPE_PROPERTY_SET;
  664. } else if(0 == strncmp(temp, "/get", strlen("/get"))) {
  665. service_type = IOTX_Thing_SERVICE_TYPE_PROPERTY_GET;
  666. }else {
  667. log_info("service topic error [%s]", topic);
  668. return FAIL_RETURN;
  669. }
  670. } else {
  671. temp = strstr(topic, "/thing/service");
  672. temp = temp + strlen("/thing/service/");
  673. service_type = IOTX_Thing_SERVICE_TYPE_UNDEFINED;
  674. service_id = temp;
  675. }
  676. /* parse payload */
  677. GET_JSON_VALUE(node, "id", payload);
  678. message_id = atoi(node);
  679. GET_JSON_VALUE_WHIH_FREE(node, "params", payload);
  680. MALLOC_MEMORY_WITH_RESULT(params, strlen(node) + 1, FAIL_RETURN);
  681. strncpy(params, node, strlen(node));
  682. LITE_free(node);
  683. node = NULL;
  684. thing_t->service_callback(thing_t,
  685. product_key,
  686. device_name,
  687. service_type,
  688. message_id,
  689. params,
  690. strlen(params),
  691. service_id);
  692. LITE_free(params);
  693. return SUCCESS_RETURN;
  694. }
  695. return FAIL_RETURN;
  696. }
  697. /* handle service request */
  698. static int iotx_thing_topo_update_proc(iotx_thing_masterlave_pt thing_t,
  699. char* topic,
  700. char* payload)
  701. {
  702. char product_key[PRODUCT_KEY_LEN] = {0};
  703. char device_name[DEVICE_NAME_LEN] = {0};
  704. iotx_thing_service_type_t service_type = IOTX_Thing_SERVICE_TYPE_PROPERTY_GET;
  705. char* service_id = NULL, *temp = NULL, *node = NULL, *params = NULL;
  706. int message_id = 0;
  707. /* parameter check */
  708. PARAMETER_Thing_CHECK(thing_t);
  709. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  710. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(payload, FAIL_RETURN);
  711. log_info("receive topo update");
  712. /* parse */
  713. if (SUCCESS_RETURN == iotx_thing_parse_topic(topic, product_key, device_name) &&
  714. (NULL != thing_t->service_callback)) { // reuse service callback
  715. temp = strstr(topic, "/thing/topo");
  716. if (temp == NULL) {
  717. log_err("parse error");
  718. return FAIL_RETURN;
  719. }
  720. /* service type */
  721. temp = strstr(topic, "/thing/topo/add/notify");
  722. if (temp) {
  723. service_type = IOTX_Thing_SERVICE_TYPE_TOPO_UPDATE;
  724. service_id = "add/notify";
  725. } else {
  726. log_info("topo topic error [%s]", topic);
  727. return FAIL_RETURN;
  728. }
  729. /* parse payload */
  730. GET_JSON_VALUE(node, "id", payload);
  731. message_id = atoi(node);
  732. GET_JSON_VALUE_WHIH_FREE(node, "params", payload);
  733. MALLOC_MEMORY_WITH_RESULT(params, strlen(node) + 1, FAIL_RETURN);
  734. strncpy(params, node, strlen(node));
  735. LITE_free(node);
  736. node = NULL;
  737. thing_t->service_callback(thing_t,
  738. product_key,
  739. device_name,
  740. service_type,
  741. message_id,
  742. params,
  743. strlen(params),
  744. service_id);
  745. LITE_free(params);
  746. return SUCCESS_RETURN;
  747. }
  748. return FAIL_RETURN;
  749. }
  750. /* handle down_raw */
  751. static int iotx_thing_down_raw_proc(iotx_thing_masterlave_pt thing_t,
  752. char* topic,
  753. char* payload)
  754. {
  755. char product_key[PRODUCT_KEY_LEN] = {0};
  756. char device_name[DEVICE_NAME_LEN] = {0};
  757. /* parameter check */
  758. PARAMETER_Thing_CHECK(thing_t);
  759. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  760. PARAMETER_NULL_CHECK_WITH_RESULT(payload, FAIL_RETURN);
  761. log_info("receive down raw");
  762. if (SUCCESS_RETURN == iotx_thing_parse_topic(topic, product_key, device_name)) {
  763. if (thing_t->raw_callback) {
  764. thing_t->raw_callback(thing_t,
  765. product_key,
  766. device_name,
  767. payload,
  768. strlen(payload));
  769. /* pulish a down_raw_reply packet */
  770. return SUCCESS_RETURN;
  771. }
  772. }
  773. return FAIL_RETURN;
  774. }
  775. /* handle thing control request */
  776. static int iotx_thing_proc(iotx_thing_masterlave_pt thing_t,
  777. char* topic,
  778. char* payload,
  779. iotx_thing_control_type_t control_type)
  780. {
  781. char product_key[PRODUCT_KEY_LEN] = {0};
  782. char device_name[DEVICE_NAME_LEN] = {0};
  783. int message_id = 0;
  784. char *node = NULL;
  785. /* parameter check */
  786. PARAMETER_Thing_CHECK(thing_t);
  787. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(topic, FAIL_RETURN);
  788. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(payload, FAIL_RETURN);
  789. log_info("receive thing control");
  790. GET_JSON_VALUE(node, "id", payload);
  791. message_id = atoi(node);
  792. if (SUCCESS_RETURN == iotx_thing_parse_topic(topic, product_key, device_name)) {
  793. if (thing_t->thing_callback) {
  794. thing_t->thing_callback(thing_t, product_key, device_name, message_id, control_type);
  795. return SUCCESS_RETURN;
  796. }
  797. }
  798. return FAIL_RETURN;
  799. }
  800. #ifdef SUBDEV_VIA_CLOUD_CONN
  801. static int iotx_thing_receive_date(iotx_thing_masterlave_pt thing_t,
  802. iotx_cloud_connection_msg_rsp_pt msg_rsp)
  803. {
  804. iotx_thing_reply_type_t reply_type = IOTX_Thing_REPLY_REGISTER;
  805. char* temp = NULL;
  806. char* publish_topic = NULL;
  807. char* publish_payload = NULL;
  808. /* parameter check */
  809. PARAMETER_Thing_CHECK(thing_t);
  810. PARAMETER_NULL_CHECK_WITH_RESULT(msg_rsp, FAIL_RETURN);
  811. log_info("recv publish message");
  812. MALLOC_MEMORY_WITH_RESULT(publish_topic, msg_rsp->URI_length, FAIL_RETURN);
  813. MALLOC_MEMORY_WITH_FREE_AND_RESULT(publish_payload, msg_rsp->payload_length, publish_topic, FAIL_RETURN);
  814. strncpy(publish_topic, msg_rsp->URI, msg_rsp->URI_length);
  815. strncpy(publish_payload, msg_rsp->payload, msg_rsp->payload_length);
  816. /* reply topic array */
  817. for (; reply_type < IOTX_Thing_REPLY_MAX; reply_type++) {
  818. if (msg_rsp->URI_length == strlen(thing_t->thing_data_t->replys[reply_type].topic) &&
  819. (0 == strncmp(msg_rsp->URI, thing_t->thing_data_t->replys[reply_type].topic, msg_rsp->URI_length))) {
  820. iotx_thing_common_reply_proc(thing_t, publish_topic, publish_payload, reply_type);
  821. LITE_free(publish_topic);
  822. LITE_free(publish_payload);
  823. return SUCCESS_RETURN;
  824. }
  825. }
  826. /* sys topic */
  827. if (0 == strncmp(msg_rsp->URI, "/sys/", strlen("/sys/"))) {
  828. /* service */
  829. temp = strstr(msg_rsp->URI, "/thing/service/");
  830. if (temp != NULL) {
  831. iotx_thing_service_request_proc(thing_t, publish_topic, publish_payload);
  832. LITE_free(publish_topic);
  833. LITE_free(publish_payload);
  834. return SUCCESS_RETURN;
  835. }
  836. /* raw data */
  837. temp = strstr(msg_rsp->URI, "/thing/model/down_raw");
  838. if (temp != NULL) {
  839. iotx_thing_down_raw_proc(thing_t, publish_topic, publish_payload);
  840. LITE_free(publish_topic);
  841. LITE_free(publish_payload);
  842. return SUCCESS_RETURN;
  843. }
  844. /* thing control - disable */
  845. temp = strstr(msg_rsp->URI, "/thing/disable");
  846. if (temp != NULL) {
  847. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_DISABLE);
  848. LITE_free(publish_topic);
  849. LITE_free(publish_payload);
  850. return SUCCESS_RETURN;
  851. }
  852. /* thing control - delete */
  853. temp = strstr(msg_rsp->URI, "/thing/delete");
  854. if (temp != NULL) {
  855. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_DELETE);
  856. LITE_free(publish_topic);
  857. LITE_free(publish_payload);
  858. return SUCCESS_RETURN;
  859. }
  860. /* thing control - enable */
  861. temp = strstr(msg_rsp->URI, "/thing/enable");
  862. if (temp != NULL) {
  863. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_ENABLE);
  864. LITE_free(publish_topic);
  865. LITE_free(publish_payload);
  866. return SUCCESS_RETURN;
  867. }
  868. }
  869. log_info("can not find the topic!");
  870. LITE_free(publish_topic);
  871. LITE_free(publish_payload);
  872. return FAIL_RETURN;
  873. }
  874. #else
  875. /* publish callback */
  876. static int iotx_thing_recv_publish_callback(iotx_thing_masterlave_pt thing_t,
  877. iotx_mqtt_event_msg_pt msg)
  878. {
  879. iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
  880. iotx_thing_reply_type_t reply_type = IOTX_Thing_REPLY_REGISTER;
  881. char* temp = NULL;
  882. char* publish_topic = NULL;
  883. char* publish_payload = NULL;
  884. /* parameter check */
  885. PARAMETER_Thing_CHECK(thing_t);
  886. PARAMETER_NULL_CHECK_WITH_RESULT(topic_info, FAIL_RETURN);
  887. log_info("recv publish message");
  888. MALLOC_MEMORY_WITH_RESULT(publish_topic, topic_info->topic_len + 1, FAIL_RETURN);
  889. MALLOC_MEMORY_WITH_FREE_AND_RESULT(publish_payload, topic_info->payload_len + 1, publish_topic, FAIL_RETURN);
  890. strncpy(publish_topic, topic_info->ptopic, topic_info->topic_len);
  891. strncpy(publish_payload, topic_info->payload, topic_info->payload_len);
  892. /* reply topic array */
  893. for (; reply_type < IOTX_Thing_REPLY_MAX; reply_type++) {
  894. if (topic_info->topic_len == strlen(thing_t->thing_data_t->replys[reply_type].topic) &&
  895. (0 == strncmp(topic_info->ptopic, thing_t->thing_data_t->replys[reply_type].topic, topic_info->topic_len))) {
  896. iotx_thing_common_reply_proc(thing_t, publish_topic, publish_payload, reply_type);
  897. LITE_free(publish_topic);
  898. LITE_free(publish_payload);
  899. return SUCCESS_RETURN;
  900. }
  901. }
  902. /* sys topic */
  903. if (0 == strncmp(topic_info->ptopic, "/sys/", strlen("/sys/"))) {
  904. /* service */
  905. temp = strstr(topic_info->ptopic, "/thing/service/");
  906. if (temp != NULL) {
  907. iotx_thing_service_request_proc(thing_t, publish_topic, publish_payload);
  908. LITE_free(publish_topic);
  909. LITE_free(publish_payload);
  910. return SUCCESS_RETURN;
  911. }
  912. /* raw data */
  913. temp = strstr(topic_info->ptopic, "/thing/model/down_raw");
  914. if (temp != NULL) {
  915. iotx_thing_down_raw_proc(thing_t, publish_topic, publish_payload);
  916. LITE_free(publish_topic);
  917. LITE_free(publish_payload);
  918. return SUCCESS_RETURN;
  919. }
  920. /* thing control - disable */
  921. temp = strstr(topic_info->ptopic, "/thing/disable");
  922. if (temp != NULL) {
  923. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_DISABLE);
  924. LITE_free(publish_topic);
  925. LITE_free(publish_payload);
  926. return SUCCESS_RETURN;
  927. }
  928. /* thing control - delete */
  929. temp = strstr(topic_info->ptopic, "/thing/delete");
  930. if (temp != NULL) {
  931. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_DELETE);
  932. LITE_free(publish_topic);
  933. LITE_free(publish_payload);
  934. return SUCCESS_RETURN;
  935. }
  936. /* thing control - enable */
  937. temp = strstr(topic_info->ptopic, "/thing/enable");
  938. if (temp != NULL) {
  939. iotx_thing_proc(thing_t, publish_topic, publish_payload, IOTX_Thing_CONTROL_TYPE_ENABLE);
  940. LITE_free(publish_topic);
  941. LITE_free(publish_payload);
  942. return SUCCESS_RETURN;
  943. }
  944. /* topo update */
  945. temp = strstr(topic_info->ptopic, "/thing/topo");
  946. if (temp != NULL) {
  947. iotx_thing_topo_update_proc(thing_t, publish_topic, publish_payload);
  948. LITE_free(publish_topic);
  949. LITE_free(publish_payload);
  950. return SUCCESS_RETURN;
  951. }
  952. }
  953. log_info("can not find the topic!");
  954. LITE_free(publish_topic);
  955. LITE_free(publish_payload);
  956. return FAIL_RETURN;
  957. }
  958. #endif
  959. /* event callback */
  960. void iotx_thing_event_handle(void *pcontext, void *pclient, void* msg_pt)
  961. {
  962. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)pcontext;
  963. #ifdef SUBDEV_VIA_CLOUD_CONN
  964. iotx_cloud_connection_msg_rsp_pt msg = (iotx_cloud_connection_msg_rsp_pt)msg_pt;
  965. #else
  966. iotx_mqtt_event_msg_pt msg = (iotx_mqtt_event_msg_pt)msg_pt;
  967. iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)(msg->msg);
  968. uintptr_t packet_id = (uintptr_t)msg->msg;
  969. #endif
  970. /* parameter check */
  971. if (thing_t == NULL) {
  972. log_info("param error");
  973. return;
  974. }
  975. if (thing_t->gateway == NULL) {
  976. log_info("param error");
  977. return;
  978. }
  979. if (thing_t->thing_data_t == NULL) {
  980. log_info("param error");
  981. return;
  982. }
  983. #ifdef SUBDEV_VIA_CLOUD_CONN
  984. log_info("event type %d", msg->rsp_type);
  985. switch (msg->rsp_type)
  986. {
  987. case IOTX_CLOUD_CONNECTION_RESPONSE_SUBSCRIBE_SUCCESS:
  988. case IOTX_CLOUD_CONNECTION_RESPONSE_UNSUBSCRIBE_SUCCESS:
  989. thing_t->thing_data_t->sync_status = 0;
  990. break;
  991. case IOTX_CLOUD_CONNECTION_RESPONSE_SUBSCRIBE_FAIL:
  992. case IOTX_CLOUD_CONNECTION_RESPONSE_UNSUBSCRIBE_FAIL:
  993. thing_t->thing_data_t->sync_status = -1;
  994. break;
  995. case IOTX_CLOUD_CONNECTION_RESPONSE_SEND_SUCCESS:
  996. case IOTX_CLOUD_CONNECTION_RESPONSE_NEW_DATA:
  997. iotx_thing_receive_date(thing_t, msg);
  998. break;
  999. case IOTX_CLOUD_CONNECTION_RESPONSE_SEND_FAIL:
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. #else
  1005. PARAMETER_NULL_CHECK(topic_info);
  1006. log_info("event type %d", msg->event_type);
  1007. switch (msg->event_type) {
  1008. /* success */
  1009. case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS:
  1010. case IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS:
  1011. if (thing_t->thing_data_t->sync_status == packet_id) {
  1012. thing_t->thing_data_t->sync_status = 0;
  1013. }
  1014. break;
  1015. /* fail */
  1016. case IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT:
  1017. case IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
  1018. case IOTX_MQTT_EVENT_SUBCRIBE_NACK:
  1019. case IOTX_MQTT_EVENT_UNSUBCRIBE_NACK:
  1020. if (thing_t->thing_data_t->sync_status == packet_id) {
  1021. thing_t->thing_data_t->sync_status = -1;
  1022. }
  1023. break;
  1024. /* publish */
  1025. case IOTX_MQTT_EVENT_PUBLISH_RECVEIVED:
  1026. iotx_thing_recv_publish_callback(thing_t, msg);
  1027. break;
  1028. /* reconnect */
  1029. case IOTX_MQTT_EVENT_RECONNECT:
  1030. /* CMP will handle the reconnect event */
  1031. log_info("mqtt reconnect");
  1032. break;
  1033. default:
  1034. log_warning("unknown event");
  1035. break;
  1036. }
  1037. #endif
  1038. return;
  1039. }
  1040. /* constuct Thing instanct, create gateway and MQTT, subscribe gateway topic */
  1041. void* IOT_Thing_Construct(iotx_thing_param_pt param)
  1042. {
  1043. iotx_gateway_param_t gateway_param;
  1044. iotx_thing_masterlave_pt thing_t = NULL;
  1045. void* gateway = NULL;
  1046. iotx_device_info_pt pdevice_info = iotx_device_info_get();
  1047. /* parameter check */
  1048. PARAMETER_NULL_CHECK_WITH_RESULT(param, NULL);
  1049. PARAMETER_NULL_CHECK_WITH_RESULT(param->mqtt, NULL);
  1050. if (g_thing_masterlave_t != NULL) {
  1051. log_err("thing have been construct");
  1052. return NULL;
  1053. }
  1054. MALLOC_MEMORY_WITH_RESULT(thing_t, sizeof(iotx_thing_masterlave_t), NULL);
  1055. MALLOC_MEMORY_WITH_FREE_AND_RESULT(thing_t->thing_data_t, sizeof(iotx_thing_masterlave_data_t), thing_t, NULL);
  1056. memset(&gateway_param, 0x0, sizeof(iotx_gateway_param_t));
  1057. gateway_param.mqtt = param->mqtt;
  1058. gateway_param.event_handler = iotx_thing_event_handle; /* set MQTT event callback */
  1059. gateway_param.event_pcontext = thing_t; /* callback userdata */
  1060. /* create gateway */
  1061. if (NULL == (gateway = IOT_Gateway_Construct(&gateway_param))) {
  1062. log_info("gateway construct fail");
  1063. LITE_free(thing_t->thing_data_t);
  1064. LITE_free(thing_t);
  1065. return NULL;
  1066. }
  1067. thing_t->gateway = gateway;
  1068. g_thing_masterlave_t = thing_t;
  1069. /* subscribe register and topo topic
  1070. if (FAIL_RETURN == iotx_thing_subscribe_unsubscribe_basic(thing_t, IOTX_Thing_SUBSCRIBE_TYPE)) {
  1071. if (FAIL_RETURN == IOT_Thing_Destroy((void**)&thing_t)) {
  1072. LITE_free(thing_t->thing_data_t);
  1073. LITE_free(thing_t);
  1074. }
  1075. return NULL;
  1076. } */
  1077. /* subscribe thing template topic */
  1078. if (SUCCESS_RETURN != iotx_thing_subscribe_unsubscribe_enhance(thing_t,
  1079. pdevice_info->product_key,
  1080. pdevice_info->device_name,
  1081. IOTX_Thing_SUBSCRIBE_TYPE)) {
  1082. if (SUCCESS_RETURN != IOT_Thing_Destroy((void**)&thing_t)) {
  1083. LITE_free(thing_t->thing_data_t);
  1084. LITE_free(thing_t);
  1085. }
  1086. return NULL;
  1087. }
  1088. return thing_t;
  1089. }
  1090. /* destory Thing: MQTT disconnect, free memory */
  1091. int IOT_Thing_Destroy(void** handle)
  1092. {
  1093. iotx_thing_masterlave_pt thing_t = NULL;
  1094. iotx_device_info_pt pdevice_info = iotx_device_info_get();
  1095. /* parameter chcek */
  1096. PARAMETER_NULL_CHECK_WITH_RESULT(handle, FAIL_RETURN);
  1097. thing_t = (iotx_thing_masterlave_pt)(*handle);
  1098. PARAMETER_Thing_CHECK(thing_t);
  1099. /* unsubscribe register and topo topic
  1100. if (FAIL_RETURN == iotx_thing_subscribe_unsubscribe_basic(thing_t, IOTX_Thing_UNSUBSCRIBE_TYPE)) {
  1101. log_info("unsubscribe basic topic fail");
  1102. }*/
  1103. /* unsubscribe thing template topic */
  1104. if (SUCCESS_RETURN != iotx_thing_subscribe_unsubscribe_enhance(thing_t,
  1105. pdevice_info->product_key,
  1106. pdevice_info->device_name,
  1107. IOTX_Thing_UNSUBSCRIBE_TYPE)){
  1108. log_info("unsubscribe enhance topic fail");
  1109. }
  1110. /* gateway destory */
  1111. if (SUCCESS_RETURN != IOT_Gateway_Destroy((void**)&(thing_t->gateway))) {
  1112. log_info("Gateway destory fail");
  1113. return FAIL_RETURN;
  1114. }
  1115. LITE_free(thing_t->thing_data_t);
  1116. LITE_free(thing_t);
  1117. *handle = NULL;
  1118. g_thing_masterlave_t = NULL;
  1119. return SUCCESS_RETURN;
  1120. }
  1121. /* Register: static and dynamic */
  1122. int IOT_Thing_Register(void* handle,
  1123. iotx_thing_register_types_t type,
  1124. const char* product_key,
  1125. const char* device_name,
  1126. char* timestamp,
  1127. char* client_id,
  1128. char* sign,
  1129. iotx_thing_sign_method_types_t sign_type)
  1130. {
  1131. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1132. /* parameter check */
  1133. PARAMETER_Thing_CHECK(thing_t);
  1134. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1135. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1136. return IOT_Subdevice_Register(thing_t->gateway, type, product_key, device_name, timestamp, client_id, sign, sign_type);
  1137. }
  1138. /* unregister: topo delete first, then unregister */
  1139. int IOT_Thing_Unregister(void* handle,
  1140. const char* product_key,
  1141. const char* device_name)
  1142. {
  1143. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1144. /* parameter check */
  1145. PARAMETER_Thing_CHECK(thing_t);
  1146. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1147. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1148. return IOT_Subdevice_Unregister(thing_t->gateway, product_key, device_name);
  1149. }
  1150. /* login */
  1151. int IOT_Thing_Login(void* handle,
  1152. const char* product_key,
  1153. const char* device_name,
  1154. char* timestamp,
  1155. char* client_id,
  1156. char* sign,
  1157. iotx_thing_sign_method_types_t sign_method,
  1158. iotx_thing_clean_session_types_t clean_session)
  1159. {
  1160. int rc = 0;
  1161. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1162. /* parameter check */
  1163. PARAMETER_Thing_CHECK(thing_t);
  1164. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1165. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1166. /* subdevice login */
  1167. if (SUCCESS_RETURN != (rc = IOT_Subdevice_Login(thing_t->gateway,
  1168. product_key,
  1169. device_name,
  1170. timestamp,
  1171. client_id,
  1172. sign,
  1173. sign_method,
  1174. clean_session))) {
  1175. log_info("subdevice login fail");
  1176. return rc;
  1177. }
  1178. /* subscribe thing template topic */
  1179. if (SUCCESS_RETURN != (rc = iotx_thing_subscribe_unsubscribe_enhance(thing_t,
  1180. product_key,
  1181. device_name,
  1182. IOTX_Thing_SUBSCRIBE_TYPE))) {
  1183. IOT_Subdevice_Logout(thing_t->gateway, product_key, device_name);
  1184. log_info("subscribe topic fail");
  1185. return rc;
  1186. }
  1187. return SUCCESS_RETURN;
  1188. }
  1189. /* logout */
  1190. int IOT_Thing_Logout(void* handle,
  1191. const char* product_key,
  1192. const char* device_name)
  1193. {
  1194. int rc = 0;
  1195. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1196. /* parameter check */
  1197. PARAMETER_Thing_CHECK(thing_t);
  1198. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1199. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1200. /* subdevice logout */
  1201. if (SUCCESS_RETURN != (rc = IOT_Subdevice_Logout(thing_t->gateway, product_key, device_name))) {
  1202. log_info("subdevice logout fail");
  1203. return rc;
  1204. }
  1205. /* unsubscribe thing template topic */
  1206. if (SUCCESS_RETURN != (rc = iotx_thing_subscribe_unsubscribe_enhance(thing_t,
  1207. product_key,
  1208. device_name,
  1209. IOTX_Thing_UNSUBSCRIBE_TYPE))) {
  1210. log_info("unsubscribe topic fail");
  1211. return rc;
  1212. }
  1213. return SUCCESS_RETURN;
  1214. }
  1215. int IOT_Thing_Get_TOPO(void* handle,
  1216. char* get_toop_reply,
  1217. uint32_t* length)
  1218. {
  1219. int rc = 0;
  1220. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1221. /* parameter check */
  1222. PARAMETER_Thing_CHECK(thing_t);
  1223. PARAMETER_NULL_CHECK_WITH_RESULT(get_toop_reply, FAIL_RETURN);
  1224. /* get topo */
  1225. if (SUCCESS_RETURN != (rc = IOT_Gateway_Get_TOPO(thing_t->gateway, get_toop_reply, length))) {
  1226. log_info("get topo fail");
  1227. return rc;
  1228. }
  1229. return SUCCESS_RETURN;
  1230. }
  1231. int IOT_Thing_Get_Config(void* handle,
  1232. char* get_config_reply,
  1233. uint32_t* length)
  1234. {
  1235. int rc = 0;
  1236. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1237. /* parameter check */
  1238. PARAMETER_Thing_CHECK(thing_t);
  1239. PARAMETER_NULL_CHECK_WITH_RESULT(get_config_reply, FAIL_RETURN);
  1240. /* get config */
  1241. if (SUCCESS_RETURN != (rc = IOT_Gateway_Get_Config(thing_t->gateway, get_config_reply, length))) {
  1242. log_info("get config fail");
  1243. return rc;
  1244. }
  1245. return SUCCESS_RETURN;
  1246. }
  1247. int IOT_Thing_Publish_Found_List(void* handle, const char* product_key,
  1248. const char* device_name)
  1249. {
  1250. int rc = 0;
  1251. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1252. /* parameter check */
  1253. PARAMETER_Thing_CHECK(thing_t);
  1254. /* publish new subdevice found list */
  1255. if (SUCCESS_RETURN != (rc = IOT_Gateway_Publish_Found_List(thing_t->gateway, product_key, device_name))) {
  1256. log_info("publish new dev found list fail");
  1257. return rc;
  1258. }
  1259. return SUCCESS_RETURN;
  1260. }
  1261. /* get dsl template */
  1262. int IOT_Thing_Get_Dsl_Template(void* handle,
  1263. const char* product_key,
  1264. const char* device_name,
  1265. char* dsl_template,
  1266. uint32_t* length)
  1267. {
  1268. int rc = 0;
  1269. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1270. /* parameter check */
  1271. PARAMETER_Thing_CHECK(thing_t);
  1272. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1273. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1274. PARAMETER_NULL_CHECK_WITH_RESULT(dsl_template, FAIL_RETURN);
  1275. if (SUCCESS_RETURN != (rc = iotx_thing_publish_common_packet(thing_t,
  1276. product_key,
  1277. device_name,
  1278. product_key,
  1279. device_name,
  1280. "dsltemplate",
  1281. "get",
  1282. "get_reply",
  1283. IOTX_Thing_REPLY_DSL_GET))) {
  1284. log_info("publish common packet error");
  1285. return rc;
  1286. }
  1287. /* check length */
  1288. if (*length < strlen(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DSL_GET].data)) {
  1289. log_info("dsltemplate length is too small");
  1290. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DSL_GET].data);
  1291. return FAIL_RETURN;
  1292. }
  1293. memset(dsl_template, 0x0, *length);
  1294. strncpy(dsl_template,
  1295. thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DSL_GET].data,
  1296. strlen(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DSL_GET].data));
  1297. (*length) = strlen(dsl_template);
  1298. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DSL_GET].data);
  1299. return SUCCESS_RETURN;
  1300. }
  1301. /* post property */
  1302. int IOT_Thing_Post_Property(void* handle,
  1303. const char* product_key,
  1304. const char* device_name,
  1305. const char* property)
  1306. {
  1307. int rc = 0;
  1308. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1309. /* parameter check */
  1310. PARAMETER_Thing_CHECK(thing_t);
  1311. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1312. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1313. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(property, FAIL_RETURN);
  1314. if (SUCCESS_RETURN != (rc = iotx_thing_publish_common_event_packet(thing_t,
  1315. product_key,
  1316. device_name,
  1317. "property",
  1318. property,
  1319. IOTX_Thing_REPLY_PROPERTY_POST))) {
  1320. log_info("publish common event packet error");
  1321. return rc;
  1322. }
  1323. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_PROPERTY_POST].data);
  1324. return SUCCESS_RETURN;
  1325. }
  1326. /* update device info */
  1327. int IOT_Thing_Update_Deviceinfo(void* handle,
  1328. const char* product_key,
  1329. const char* device_name,
  1330. const char* deviceinfo)
  1331. {
  1332. int rc = 0;
  1333. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1334. /* parameter check */
  1335. PARAMETER_Thing_CHECK(thing_t);
  1336. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1337. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1338. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(deviceinfo, FAIL_RETURN);
  1339. if (SUCCESS_RETURN != (rc = iotx_thing_publish_common_deviceinfo_packet(thing_t,
  1340. product_key,
  1341. device_name,
  1342. deviceinfo,
  1343. "update",
  1344. "update_reply",
  1345. IOTX_Thing_REPLY_DEVICEINFO_UPDATE))) {
  1346. log_info("publish common packet error");
  1347. return rc;
  1348. }
  1349. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DEVICEINFO_UPDATE].data);
  1350. return SUCCESS_RETURN;
  1351. }
  1352. /* delete device info */
  1353. int IOT_Thing_Delete_Deviceinfo(void* handle,
  1354. const char* product_key,
  1355. const char* device_name,
  1356. const char* deviceinfo)
  1357. {
  1358. int rc = 0;
  1359. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1360. /* parameter check */
  1361. PARAMETER_Thing_CHECK(thing_t);
  1362. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1363. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1364. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(deviceinfo, FAIL_RETURN);
  1365. if (SUCCESS_RETURN != (rc = iotx_thing_publish_common_deviceinfo_packet(thing_t,
  1366. product_key,
  1367. device_name,
  1368. deviceinfo,
  1369. "delete",
  1370. "delete_reply",
  1371. IOTX_Thing_REPLY_DEVICEINFO_DELETE))) {
  1372. log_info("publish common packet error");
  1373. return rc;
  1374. }
  1375. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_DEVICEINFO_DELETE].data);
  1376. return SUCCESS_RETURN;
  1377. }
  1378. /* response for set property */
  1379. int IOT_Thing_Set_Property_Response(void* handle,
  1380. const char* product_key,
  1381. const char* device_name,
  1382. uint32_t msg_id,
  1383. uint32_t code)
  1384. {
  1385. int rc = 0;
  1386. iotx_mqtt_topic_info_t topic_msg;
  1387. char * property_packet = NULL;
  1388. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1389. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1390. /* parameter check */
  1391. PARAMETER_Thing_CHECK(thing_t);
  1392. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1393. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1394. log_info("IOT_Thing_Set_Property_Response");
  1395. HAL_Snprintf(topic,
  1396. GATEWAY_TOPIC_LEN_MAX,
  1397. TOPIC_Thing_COMMON_EXT_FMT,
  1398. product_key,
  1399. device_name,
  1400. "service",
  1401. "property",
  1402. "set_reply");
  1403. property_packet = iotx_thing_splice_default_reply_packet(msg_id, code);
  1404. /* mqtt packet */
  1405. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1406. topic_msg.qos = IOTX_MQTT_QOS0;
  1407. topic_msg.retain = 0;
  1408. topic_msg.dup = 0;
  1409. topic_msg.payload = (void *)property_packet;
  1410. topic_msg.payload_len = strlen(property_packet);
  1411. topic_msg.packet_id = 0;
  1412. if ((rc = IOT_Gateway_Publish(thing_t->gateway, topic, &topic_msg)) < 0) {
  1413. log_err("Gateway Publish error!");
  1414. LITE_free(property_packet);
  1415. return rc;
  1416. }
  1417. LITE_free(property_packet);
  1418. return SUCCESS_RETURN;
  1419. }
  1420. /* response for get property */
  1421. int IOT_Thing_Get_Property_Response(void* handle,
  1422. const char* product_key,
  1423. const char* device_name,
  1424. uint32_t msg_id,
  1425. const char* property_data,
  1426. uint32_t code)
  1427. {
  1428. int rc = 0;
  1429. iotx_mqtt_topic_info_t topic_msg;
  1430. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1431. char * property_packet = NULL;
  1432. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1433. /* parameter check */
  1434. PARAMETER_Thing_CHECK(thing_t);
  1435. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1436. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1437. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(property_data, FAIL_RETURN);
  1438. HAL_Snprintf(topic,
  1439. GATEWAY_TOPIC_LEN_MAX,
  1440. TOPIC_Thing_COMMON_EXT_FMT,
  1441. product_key,
  1442. device_name,
  1443. "service",
  1444. "property",
  1445. "get_reply");
  1446. property_packet = iotx_thing_splice_default_reply_get_packet(msg_id, code, property_data);
  1447. /* 组装mqtt packet */
  1448. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1449. topic_msg.qos = IOTX_MQTT_QOS0;
  1450. topic_msg.retain = 0;
  1451. topic_msg.dup = 0;
  1452. topic_msg.payload = (void *)property_packet;
  1453. topic_msg.payload_len = strlen(property_packet);
  1454. topic_msg.packet_id = 0;
  1455. if ((rc = IOT_Gateway_Publish(thing_t->gateway, topic, &topic_msg)) < 0) {
  1456. log_err("Gateway Publish error!");
  1457. LITE_free(property_packet);
  1458. return rc;
  1459. }
  1460. LITE_free(property_packet);
  1461. return SUCCESS_RETURN;
  1462. }
  1463. /* register callback for service */
  1464. int IOT_Thing_Service_Register(void* handle,
  1465. service_request_callback service_callback)
  1466. {
  1467. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1468. /* parameter check */
  1469. PARAMETER_Thing_CHECK(thing_t);
  1470. PARAMETER_NULL_CHECK_WITH_RESULT(service_callback, FAIL_RETURN);
  1471. if (thing_t->service_callback != NULL) {
  1472. log_info("service_callback have been set, can not set again");
  1473. return FAIL_RETURN;
  1474. }
  1475. thing_t->service_callback = service_callback;
  1476. return SUCCESS_RETURN;
  1477. }
  1478. /* register callback for raw data */
  1479. int IOT_Thing_Down_Raw_Register(void* handle, down_raw_callback raw_callback)
  1480. {
  1481. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1482. /* parameter check */
  1483. PARAMETER_Thing_CHECK(thing_t);
  1484. PARAMETER_NULL_CHECK_WITH_RESULT(raw_callback, FAIL_RETURN);
  1485. if (thing_t->raw_callback != NULL) {
  1486. log_info("raw_callback have been set, can not set again");
  1487. return FAIL_RETURN;
  1488. }
  1489. thing_t->raw_callback = raw_callback;
  1490. return SUCCESS_RETURN;
  1491. }
  1492. /* reply a down_raw_reply to server */
  1493. int IOT_Tmp_Down_Raw_Response(void* handle,
  1494. const char* product_key,
  1495. const char* device_name,
  1496. const char* response)
  1497. {
  1498. int rc = 0;
  1499. iotx_mqtt_topic_info_t topic_msg;
  1500. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1501. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1502. /* parameter check */
  1503. PARAMETER_Thing_CHECK(thing_t);
  1504. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1505. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1506. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(response, FAIL_RETURN);
  1507. /* topic */
  1508. HAL_Snprintf(topic,
  1509. GATEWAY_TOPIC_LEN_MAX,
  1510. TOPIC_Thing_COMMON_FMT,
  1511. product_key,
  1512. device_name,
  1513. "model",
  1514. "down_raw_reply");
  1515. /* mqtt packet */
  1516. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1517. topic_msg.qos = IOTX_MQTT_QOS0;
  1518. topic_msg.retain = 0;
  1519. topic_msg.dup = 0;
  1520. topic_msg.payload = (void *)response;
  1521. topic_msg.payload_len = strlen(response);
  1522. topic_msg.packet_id = 0;
  1523. /* publish mqtt packet */
  1524. if (SUCCESS_RETURN != (rc = IOT_Gateway_Publish(thing_t->gateway, topic, &topic_msg))) {
  1525. log_info("Gateway publish fail");
  1526. return rc;
  1527. }
  1528. return SUCCESS_RETURN;
  1529. }
  1530. /* register callback for thing control */
  1531. int IOT_Thing_Control_Register(void* handle,
  1532. thing_control_callback thing_callback)
  1533. {
  1534. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1535. /* parameter check */
  1536. PARAMETER_Thing_CHECK(thing_t);
  1537. PARAMETER_NULL_CHECK_WITH_RESULT(thing_callback, FAIL_RETURN);
  1538. thing_t->thing_callback = thing_callback;
  1539. return SUCCESS_RETURN;
  1540. }
  1541. /* register callback for RRPC */
  1542. int IOT_Thing_RRPC_Register( void* handle,
  1543. const char* product_key,
  1544. const char* device_name,
  1545. rrpc_request_callback rrpc_callback)
  1546. {
  1547. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1548. /* parameter check */
  1549. PARAMETER_Thing_CHECK(thing_t);
  1550. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1551. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1552. PARAMETER_NULL_CHECK_WITH_RESULT(rrpc_callback, FAIL_RETURN);
  1553. return IOT_Gateway_RRPC_Register(thing_t->gateway,
  1554. product_key,
  1555. device_name,
  1556. rrpc_callback);
  1557. }
  1558. /* response for RRPC */
  1559. int IOT_Thing_RRPC_Response(void* handle,
  1560. const char* product_key,
  1561. const char* device_name,
  1562. const char* message_id,
  1563. const char* response)
  1564. {
  1565. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1566. /* parameter check */
  1567. PARAMETER_Thing_CHECK(thing_t);
  1568. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1569. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1570. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(message_id, FAIL_RETURN);
  1571. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(response, FAIL_RETURN);
  1572. return IOT_Gateway_RRPC_Response(thing_t->gateway,
  1573. product_key,
  1574. device_name,
  1575. message_id,
  1576. response);
  1577. }
  1578. /* response for service */
  1579. int IOT_Thing_Service_Response(void* handle,
  1580. const char* product_key,
  1581. const char* device_name,
  1582. const char* service_id,
  1583. uint32_t message_id,
  1584. uint32_t code)
  1585. {
  1586. int rc = 0;
  1587. char* response_packet = NULL;
  1588. char response_reply_topic[20] = {0};
  1589. iotx_mqtt_topic_info_t topic_msg;
  1590. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1591. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1592. /* parameter check */
  1593. PARAMETER_Thing_CHECK(thing_t);
  1594. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1595. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1596. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(service_id, FAIL_RETURN);
  1597. HAL_Snprintf(response_reply_topic, 20, "%s_reply", service_id);
  1598. /* topic */
  1599. HAL_Snprintf(topic,
  1600. GATEWAY_TOPIC_LEN_MAX,
  1601. TOPIC_Thing_COMMON_FMT,
  1602. product_key,
  1603. device_name,
  1604. "service",
  1605. response_reply_topic);
  1606. /* packet */
  1607. response_packet = iotx_thing_splice_default_reply_packet(message_id, code);
  1608. /* mqtt packet */
  1609. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1610. topic_msg.qos = IOTX_MQTT_QOS0;
  1611. topic_msg.retain = 0;
  1612. topic_msg.dup = 0;
  1613. topic_msg.payload = (void *)response_packet;
  1614. topic_msg.payload_len = strlen(response_packet);
  1615. topic_msg.packet_id = 0;
  1616. /* publish mqtt packet */
  1617. if (SUCCESS_RETURN != (rc = IOT_Gateway_Publish(thing_t->gateway, topic, &topic_msg))) {
  1618. log_info("Gateway publish fail");
  1619. LITE_free(response_packet);
  1620. return rc;
  1621. }
  1622. LITE_free(response_packet);
  1623. return SUCCESS_RETURN;
  1624. }
  1625. /* response for thing control */
  1626. int IOT_Thing_Control_Response(void* handle,
  1627. const char* product_key,
  1628. const char* device_name,
  1629. uint32_t message_id,
  1630. uint32_t code,
  1631. iotx_thing_control_type_t control_type)
  1632. {
  1633. int rc = 0;
  1634. char* response_packet = NULL;
  1635. iotx_mqtt_topic_info_t topic_msg;
  1636. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1637. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1638. /* parameter check */
  1639. PARAMETER_Thing_CHECK(thing_t);
  1640. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1641. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1642. if (control_type == IOTX_Thing_CONTROL_TYPE_ENABLE) {
  1643. /* topic */
  1644. HAL_Snprintf(topic,
  1645. GATEWAY_TOPIC_LEN_MAX,
  1646. TOPIC_Thing_COMMON_EXT_EXT_FMT,
  1647. product_key,
  1648. device_name,
  1649. "enable_reply");
  1650. } else if (control_type == IOTX_Thing_CONTROL_TYPE_DISABLE) {
  1651. /* topic */
  1652. HAL_Snprintf(topic,
  1653. GATEWAY_TOPIC_LEN_MAX,
  1654. TOPIC_Thing_COMMON_EXT_EXT_FMT,
  1655. product_key,
  1656. device_name,
  1657. "disable_reply");
  1658. } else if (control_type == IOTX_Thing_CONTROL_TYPE_DELETE) {
  1659. /* topic */
  1660. HAL_Snprintf(topic,
  1661. GATEWAY_TOPIC_LEN_MAX,
  1662. TOPIC_Thing_COMMON_EXT_EXT_FMT,
  1663. product_key,
  1664. device_name,
  1665. "delete_reply");
  1666. }
  1667. /* packet */
  1668. response_packet = iotx_thing_splice_default_reply_packet(message_id, code);
  1669. /* mqtt packet */
  1670. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1671. topic_msg.qos = IOTX_MQTT_QOS0;
  1672. topic_msg.retain = 0;
  1673. topic_msg.dup = 0;
  1674. topic_msg.payload = (void *)response_packet;
  1675. topic_msg.payload_len = strlen(response_packet);
  1676. topic_msg.packet_id = 0;
  1677. /* publish mqtt packet */
  1678. if (SUCCESS_RETURN != (rc = IOT_Gateway_Publish(thing_t->gateway, topic, &topic_msg))) {
  1679. log_info("Gateway publish fail");
  1680. LITE_free(response_packet);
  1681. return FAIL_RETURN;
  1682. }
  1683. LITE_free(response_packet);
  1684. return SUCCESS_RETURN;
  1685. }
  1686. /* trigger event */
  1687. int IOT_Thing_Trigger_Event(void* handle,
  1688. const char* product_key,
  1689. const char* device_name,
  1690. const char* params,
  1691. const char* event_id)
  1692. {
  1693. int rc = 0;
  1694. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1695. /* parameter check */
  1696. PARAMETER_Thing_CHECK(thing_t);
  1697. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1698. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1699. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(params, FAIL_RETURN);
  1700. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(event_id, FAIL_RETURN);
  1701. if (SUCCESS_RETURN != (rc = iotx_thing_publish_common_event_packet(thing_t,
  1702. product_key,
  1703. device_name,
  1704. event_id,
  1705. params,
  1706. IOTX_Thing_REPLY_EVENT_POST))) {
  1707. log_info("publish common event packet error");
  1708. return rc;
  1709. }
  1710. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_EVENT_POST].data);
  1711. return SUCCESS_RETURN;
  1712. }
  1713. /* publish raw data */
  1714. int IOT_Thing_Publish_Rawdata(void* handle,
  1715. const char* product_key,
  1716. const char* device_name,
  1717. const char* raw_data,
  1718. uint32_t raw_data_length)
  1719. {
  1720. int32_t msg_id = -2; /* for raw data */
  1721. iotx_mqtt_topic_info_t topic_msg;
  1722. char topic[GATEWAY_TOPIC_LEN_MAX] = {0};
  1723. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1724. /* parameter check */
  1725. PARAMETER_Thing_CHECK(thing_t);
  1726. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(product_key, FAIL_RETURN);
  1727. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(device_name, FAIL_RETURN);
  1728. PARAMETER_STRING_NULL_CHECK_WITH_RESULT(raw_data, FAIL_RETURN);
  1729. HAL_Snprintf(topic,
  1730. GATEWAY_TOPIC_LEN_MAX,
  1731. TOPIC_Thing_COMMON_FMT,
  1732. product_key,
  1733. device_name,
  1734. "model",
  1735. "up_raw");
  1736. memset(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_UP_RAW].topic, 0x0, GATEWAY_TOPIC_LEN_MAX);
  1737. HAL_Snprintf(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_UP_RAW].topic,
  1738. GATEWAY_TOPIC_LEN_MAX,
  1739. TOPIC_Thing_COMMON_FMT,
  1740. product_key,
  1741. device_name,
  1742. "model",
  1743. "up_raw_reply");
  1744. /* mqtt packet */
  1745. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  1746. topic_msg.qos = IOTX_MQTT_QOS0;
  1747. topic_msg.retain = 0;
  1748. topic_msg.dup = 0;
  1749. topic_msg.payload = (void *)raw_data;
  1750. topic_msg.payload_len = raw_data_length;
  1751. topic_msg.packet_id = 0;
  1752. /* publish packet */
  1753. if (FAIL_RETURN == iotx_thing_publish_topic_sync(thing_t,
  1754. msg_id,
  1755. topic,
  1756. &topic_msg,
  1757. IOTX_Thing_REPLY_UP_RAW)) {
  1758. return FAIL_RETURN;
  1759. }
  1760. LITE_free(thing_t->thing_data_t->replys[IOTX_Thing_REPLY_UP_RAW].data);
  1761. if (200 == thing_t->thing_data_t->replys[IOTX_Thing_REPLY_UP_RAW].code) {
  1762. log_info("up raw success");
  1763. } else {
  1764. log_info("up raw replay error!code:%d", thing_t->thing_data_t->replys[IOTX_Thing_REPLY_UP_RAW].code);
  1765. return FAIL_RETURN;
  1766. }
  1767. return SUCCESS_RETURN;
  1768. }
  1769. int IOT_Thing_Yield(void* handle, uint32_t timeout)
  1770. {
  1771. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1772. /* parameter check */
  1773. PARAMETER_Thing_CHECK(thing_t);
  1774. return IOT_Gateway_Yield(thing_t->gateway, timeout);
  1775. }
  1776. int IOT_Thing_Subscribe(void* handle,
  1777. const char *topic_filter,
  1778. int qos,
  1779. iotx_subdev_event_handle_func_fpt topic_handle_func,
  1780. void *pcontext)
  1781. {
  1782. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1783. /* parameter check */
  1784. PARAMETER_Thing_CHECK(thing_t);
  1785. return IOT_Gateway_Subscribe(thing_t->gateway, topic_filter, qos, topic_handle_func, pcontext);
  1786. }
  1787. int IOT_Thing_Unsubscribe(void* handle, const char *topic_filter)
  1788. {
  1789. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1790. /* parameter check */
  1791. PARAMETER_Thing_CHECK(thing_t);
  1792. return IOT_Gateway_Unsubscribe(thing_t->gateway, topic_filter);
  1793. }
  1794. int IOT_Thing_Publish(void* handle,
  1795. const char *topic_name,
  1796. iotx_mqtt_topic_info_pt topic_msg)
  1797. {
  1798. iotx_thing_masterlave_pt thing_t = (iotx_thing_masterlave_pt)handle;
  1799. /* parameter check */
  1800. PARAMETER_Thing_CHECK(thing_t);
  1801. return IOT_Gateway_Publish(thing_t->gateway, topic_name, topic_msg);
  1802. }