subdev_example_api.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. #ifndef SRC_SUBDEVICE_Thing_UTIL_H_
  2. #define SRC_SUBDEVICE_Thing_UTIL_H_
  3. #include "exports/iot_export_subdev.h"
  4. /* The fomat of thing topic */
  5. #define TOPIC_Thing_COMMON_FMT "/sys/%s/%s/thing/%s/%s"
  6. /* The fomat of thing topic */
  7. #define TOPIC_Thing_COMMON_EXT_FMT "/sys/%s/%s/thing/%s/%s/%s"
  8. /* The fomat of thing topic */
  9. #define TOPIC_Thing_COMMON_EXT_EXT_FMT "/sys/%s/%s/thing/%s"
  10. /* Register type */
  11. typedef enum IOTX_Thing_REGISTER_TYPES {
  12. /* static, via web get device_secert */
  13. IOTX_Thing_REGISTER_TYPE_STATIC,
  14. /* dynamic, via register API get device_secert */
  15. IOTX_Thing_REGISTER_TYPE_DYNAMIC,
  16. IOTX_Thing_REGISTER_TYPE_MAX
  17. }iotx_thing_register_types_t;
  18. /* Sign method */
  19. typedef enum IOTX_Thing_SIGN_METHOD_TYPES {
  20. /* HmacSha1 */
  21. IOTX_Thing_SIGN_METHOD_TYPE_SHA,
  22. /* HmacMd5 */
  23. IOTX_Thing_SIGN_METHOD_TYPE_MD5,
  24. /* Undefined */
  25. IOTX_Thing_SIGN_METHOD_TYPE_UNDEFINED,
  26. /* Maximum number of sign method */
  27. IOTX_Thing_SIGN_METHOD_TYPE_MAX
  28. }iotx_thing_sign_method_types_t;
  29. /* Login clean seesion type */
  30. typedef enum IOTX_Thing_CLEAN_SESSION_TYPES {
  31. /* True */
  32. IOTX_Thing_CLEAN_SESSION_TYPE_TRUE,
  33. /* False */
  34. IOTX_Thing_CLEAN_SESSION_TYPE_FALSE,
  35. /* Undefined */
  36. IOTX_Thing_CLEAN_SESSION_TYPE_UNDEFINED,
  37. /* Maximum number of login clean seesion type */
  38. IOTX_Thing_CLEAN_SESSION_TYPE_MAX
  39. }iotx_thing_clean_session_types_t;
  40. /* reply type */
  41. typedef enum IOTX_Thing_REPLY_ENUM {
  42. /* thing/sub/register */
  43. IOTX_Thing_REPLY_REGISTER,
  44. /* thing/sub/unregister */
  45. IOTX_Thing_REPLY_UNREGISTER,
  46. /* thing/topo/add */
  47. IOTX_Thing_REPLY_TOPO_ADD,
  48. /* thing/topo/delete */
  49. IOTX_Thing_REPLY_TOPO_DELETE,
  50. /* thing/event/property/post */
  51. IOTX_Thing_REPLY_PROPERTY_POST,
  52. /* thing/dsltemplate/get */
  53. IOTX_Thing_REPLY_DSL_GET,
  54. /* thing/deviceinfo/update*/
  55. IOTX_Thing_REPLY_DEVICEINFO_UPDATE,
  56. /* thing/deviceinfo/delete*/
  57. IOTX_Thing_REPLY_DEVICEINFO_DELETE,
  58. /* thing/keyelement/post */
  59. IOTX_Thing_REPLY_KEYELEMENT_POST,
  60. /* thing/up_raw */
  61. IOTX_Thing_REPLY_UP_RAW,
  62. /* thing/event/+/post */
  63. IOTX_Thing_REPLY_EVENT_POST,
  64. /* Maximum number of reply type */
  65. IOTX_Thing_REPLY_MAX
  66. }iotx_thing_reply_type_t;
  67. /* service type */
  68. typedef enum IOTX_Thing_SERVICE_TYPES {
  69. /* property/set*/
  70. IOTX_Thing_SERVICE_TYPE_PROPERTY_SET,
  71. /* property/get*/
  72. IOTX_Thing_SERVICE_TYPE_PROPERTY_GET,
  73. /* Undefined service type */
  74. IOTX_Thing_SERVICE_TYPE_UNDEFINED,
  75. IOTX_Thing_SERVICE_TYPE_TOPO_UPDATE,
  76. /* Maximum number of service type */
  77. IOTX_Thing_SERVICE_TYPE_MAX
  78. }iotx_thing_service_type_t;
  79. /* thing control type */
  80. typedef enum IOTX_Thing_CONTROL_TYPES {
  81. /* thing/enable*/
  82. IOTX_Thing_CONTROL_TYPE_ENABLE,
  83. /* thing/disable*/
  84. IOTX_Thing_CONTROL_TYPE_DISABLE,
  85. /* thing/delete*/
  86. IOTX_Thing_CONTROL_TYPE_DELETE,
  87. /* Maximum number of thind control type */
  88. IOTX_Thing_CONTROL_TYPE_MAX
  89. }iotx_thing_control_type_t;
  90. /**
  91. * @brief It define a datatype of function pointer.
  92. * This type of function will be called when a service occur.
  93. *
  94. * @param thing, the thing context
  95. * @param product_key, the product key
  96. * @param deveice_name, the deveice name
  97. * @param service_type, the service type
  98. * @param message_id, the service's message id
  99. * @param params, the service's params
  100. * @param params_length, the length of service's params
  101. * @param service_id, the service's id, defined by dsl template *
  102. *
  103. * @return none
  104. */
  105. typedef void (*service_request_callback)(void* thing_t,
  106. const char* product_key,
  107. const char* device_name,
  108. iotx_thing_service_type_t service_type,
  109. uint32_t message_id,
  110. char* params,
  111. uint32_t params_length,
  112. const char* service_id);
  113. /**
  114. * @brief It define a datatype of function pointer.
  115. * This type of function will be called when a raw data down occur.
  116. *
  117. * @param thing, the thing context
  118. * @param product_key, the product key
  119. * @param deveice_name, the deveice name
  120. * @param raw_data, the raw data
  121. * @param raw_data_length, the length of raw data
  122. *
  123. * @return none
  124. */
  125. typedef void (*down_raw_callback)(void* thing_t,
  126. const char* product_key,
  127. const char* device_name,
  128. const char* raw_data,
  129. uint32_t raw_data_length);
  130. /**
  131. * @brief It define a datatype of function pointer.
  132. * This type of function will be called when a thing control occur.
  133. *
  134. * @param thing, the thing context
  135. * @param product_key, the product key
  136. * @param deveice_name, the deveice name
  137. * @param raw_data, the raw data
  138. * @param raw_data_length, the length of raw data
  139. *
  140. * @return none
  141. */
  142. typedef void (*thing_control_callback)(void* thing_t,
  143. const char* product_key,
  144. const char* device_name,
  145. iotx_thing_control_type_t thing_control_type,
  146. uint32_t message_id);
  147. /* The structure of thing parameter */
  148. typedef struct {
  149. iotx_mqtt_param_pt mqtt;
  150. } iotx_thing_param_t, *iotx_thing_param_pt;
  151. /**
  152. * @brief Create a Thing construction
  153. * This function used to create a Thing construction.
  154. *
  155. * @param parameter.
  156. *
  157. * @return 0, Create success; -1, Create fail.
  158. */
  159. void* IOT_Thing_Construct(iotx_thing_param_pt param);
  160. /**
  161. * @brief Destory Thing construction
  162. *
  163. * @param pointer of handle, specify the Thing construction.
  164. *
  165. * @return 0, Destory success; -1, Destory fail.
  166. */
  167. int IOT_Thing_Destroy(void** handle);
  168. /**
  169. * @brief Device register
  170. * This function used to register device.
  171. *
  172. * @param pointer of handle, specify the Thing construction.
  173. * @param register type.
  174. * IOTX_Thing_REGISTER_TYPE_STATIC
  175. * IOTX_Thing_REGISTER_TYPE_DYNAMIC
  176. * @param product key.
  177. * @param device name.
  178. * @param timestamp. [if type = dynamic, must be NULL ]
  179. * @param client_id. [if type = dynamic, must be NULL ]
  180. * @param sign. [if type = dynamic, must be NULL ]
  181. * @param sign_method.
  182. * IOTX_Thing_SIGN_METHOD_TYPE_SHA
  183. * IOTX_Thing_SIGN_METHOD_TYPE_MD5
  184. *
  185. * @return 0, Logout success; -1, Logout fail.
  186. */
  187. int IOT_Thing_Register(void* handle,
  188. iotx_thing_register_types_t type,
  189. const char* product_key,
  190. const char* device_name,
  191. char* timestamp,
  192. char* client_id,
  193. char* sign,
  194. iotx_thing_sign_method_types_t sign_type);
  195. /**
  196. * @brief Device unregister
  197. * This function used to unregister device.
  198. * The device must dynamic register again if it want to use after unregister.
  199. *
  200. * @param pointer of handle, specify the Thing construction.
  201. * @param product key.
  202. * @param device name.
  203. *
  204. * @return 0, Unregister success; -1, Unregister fail.
  205. */
  206. int IOT_Thing_Unregister(void* handle,
  207. const char* product_key,
  208. const char* device_name);
  209. /**
  210. * @brief Device login
  211. * This function used to login device.
  212. *
  213. * @param pointer of handle, specify the Thing construction.
  214. * @param product key.
  215. * @param device name.
  216. * @param timestamp. [if register type = dynamic, NULL ]
  217. * @param client_id. [if register type = dynamic, NULL ]
  218. * @param sign. [if register type = dynamic, NULL ]
  219. * @param sign method. [if register type = dynamic, NO mean ]
  220. * IOTX_Thing_SIGN_METHOD_TYPE_SHA
  221. * IOTX_Thing_SIGN_METHOD_TYPE_MD5
  222. * @param clean session set.
  223. * IOTX_Thing_CLEAN_SESSION_TYPE_TRUE
  224. * IOTX_Thing_CLEAN_SESSION_TYPE_FALSE
  225. *
  226. * @return 0, Login success; -1, Login fail.
  227. */
  228. int IOT_Thing_Login(void* handle,
  229. const char* product_key,
  230. const char* device_name,
  231. char* timestamp,
  232. char* client_id,
  233. char* sign,
  234. iotx_thing_sign_method_types_t sign_method,
  235. iotx_thing_clean_session_types_t clean_session);
  236. /**
  237. * @brief Device logout
  238. * This function used to logout device.
  239. *
  240. * @param pointer of handle, specify the Thing construction.
  241. * @param product key.
  242. * @param device name.
  243. *
  244. * @return 0, Logout success; -1, Logout fail.
  245. */
  246. int IOT_Thing_Logout(void* handle,
  247. const char* product_key,
  248. const char* device_name);
  249. /**
  250. * @brief Get topo
  251. * This function publish a packet with topo/get topic and wait for the reply (with TOPO_GET_REPLY topic).
  252. *
  253. * @param pointer of handle, specify the Thing construction.
  254. * @param get_toop_reply.
  255. * @param length [in/out]. in -- get_topo_reply buffer length, out -- reply length
  256. *
  257. * @return 0, logout success; -1, logout failed.
  258. */
  259. int IOT_Thing_Get_TOPO(void* handle,
  260. char* get_toop_reply,
  261. uint32_t* length);
  262. /**
  263. * @brief Get config
  264. * This function publish a packet with config/get topic and wait for the reply (with CONFIG_GET_REPLY topic).
  265. *
  266. * @param pointer of handle, specify the Thing construction.
  267. * @param get_config_reply.
  268. * @param length [in/out]. in -- get_config_reply buffer length, out -- reply length
  269. *
  270. * @return 0, logout success; -1, logout failed.
  271. */
  272. int IOT_Thing_Get_Config(void* handle,
  273. char* get_config_reply,
  274. uint32_t* length);
  275. /**
  276. * @brief Publish Found List
  277. * This function publish a packet with new subdevice found list.
  278. *
  279. * @return 0, publish success; -1, publish failed.
  280. */
  281. int IOT_Thing_Publish_Found_List(void* handle, const char* product_key,
  282. const char* device_name);
  283. /**
  284. * @brief Get DSL template
  285. * This function used to get DSL template.
  286. *
  287. * @param pointer of handle, specify the Thing construction.
  288. * @param product key.
  289. * @param device name.
  290. * @param DSL template buffer [output].
  291. * @param buffer size [output].
  292. *
  293. * @return 0, Get success; -1, Get fail.
  294. */
  295. int IOT_Thing_Get_Dsl_Template(void* handle,
  296. const char* product_key,
  297. const char* device_name,
  298. char* dsl_template,
  299. uint32_t* length);
  300. /**
  301. * @brief Post key element
  302. * This function used to post key element to set specify template.
  303. *
  304. * @param pointer of handle, specify the Thing construction.
  305. * @param product key.
  306. * @param device name.
  307. * @param event.
  308. * @param property.
  309. * @param service.
  310. *
  311. * @return 0, Post success; -1, Post fail.
  312. */
  313. int IOT_Thing_Post_Property(void* handle,
  314. const char* product_key,
  315. const char* device_name,
  316. const char* property);
  317. /**
  318. * @brief Update device information
  319. * This function used to update device information.
  320. *
  321. * @param pointer of handle, specify the Thing construction.
  322. * @param product key.
  323. * @param device name.
  324. * @param device information.
  325. *
  326. * @return 0, Update success; -1, Update fail.
  327. */
  328. int IOT_Thing_Update_Deviceinfo(void* handle,
  329. const char* product_key,
  330. const char* device_name,
  331. const char* deviceinfo);
  332. /**
  333. * @brief Delete device information
  334. * This function used to delete device information.
  335. *
  336. * @param pointer of handle, specify the Thing construction.
  337. * @param product key.
  338. * @param device name.
  339. * @param device information.
  340. *
  341. * @return 0, Delete success; -1, Delete fail.
  342. */
  343. int IOT_Thing_Delete_Deviceinfo(void* handle,
  344. const char* product_key,
  345. const char* device_name,
  346. const char* deviceinfo);
  347. /**
  348. * @brief Post key element
  349. * This function used to post key element to set specify template.
  350. *
  351. * @param pointer of handle, specify the Thing construction.
  352. * @param product key.
  353. * @param device name.
  354. * @param event.
  355. * @param property.
  356. * @param service.
  357. *
  358. * @return 0, Post success; -1, Post fail.
  359. */
  360. int IOT_Thing_Post_Keyelement(void* handle,
  361. const char* product_key,
  362. const char* device_name,
  363. const char* evnet,
  364. const char* property,
  365. const char* service);
  366. /**
  367. * @brief Response set propety request
  368. * This function used to response set propety request.
  369. *
  370. * @param pointer of handle, specify the Thing construction.
  371. * @param product key.
  372. * @param device name.
  373. * @param set propety request message id.
  374. * @param response code.
  375. *
  376. * @return 0, Response success; -1, Response fail.
  377. */
  378. int IOT_Thing_Set_Property_Response(void* handle,
  379. const char* product_key,
  380. const char* device_name,
  381. uint32_t msg_id,
  382. uint32_t code);
  383. /**
  384. * @brief Response get propety request
  385. * This function used to response get propety request.
  386. *
  387. * @param pointer of handle, specify the Thing construction.
  388. * @param product key.
  389. * @param device name.
  390. * @param get propety request message id.
  391. * @param property data.
  392. * @param response code.
  393. *
  394. * @return 0, Response success; -1, Response fail.
  395. */
  396. int IOT_Thing_Get_Property_Response(void* handle,
  397. const char* product_key,
  398. const char* device_name,
  399. uint32_t msg_id,
  400. const char* property_data,
  401. uint32_t code);
  402. /**
  403. * @brief Register service request callback
  404. * This function used to register service request callback.
  405. *
  406. * @param pointer of handle, specify the Thing construction.
  407. * @param callback.
  408. *
  409. * @return 0, Register success; -1, Register fail.
  410. */
  411. int IOT_Thing_Service_Register(void* handle,
  412. service_request_callback service_callback);
  413. /**
  414. * @brief Response service request request
  415. * This function used to response service request request.
  416. *
  417. * @param pointer of handle, specify the Thing construction.
  418. * @param product key.
  419. * @param device name.
  420. * @param service request service id.
  421. * @param service request message id.
  422. * @param response code.
  423. *
  424. * @return 0, Response success; -1, Response fail.
  425. */
  426. int IOT_Thing_Service_Response(void* handle,
  427. const char* product_key,
  428. const char* device_name,
  429. const char* service_id,
  430. uint32_t message_id,
  431. uint32_t code);
  432. /**
  433. * @brief Register thing control callback
  434. * This function used to register thing control callback.
  435. *
  436. * @param pointer of handle, specify the Thing construction.
  437. * @param callback.
  438. *
  439. * @return 0, Register success; -1, Register fail.
  440. */
  441. int IOT_Thing_Control_Register(void* handle,
  442. thing_control_callback thing_callback);
  443. /**
  444. * @brief Response thing control request
  445. * This function used to response thing control request.
  446. *
  447. * @param pointer of handle, specify the Thing construction.
  448. * @param product key.
  449. * @param device name.
  450. * @param thing control request message id.
  451. * @param response code.
  452. * @param thing control type.
  453. * IOTX_THING_CONTROL_TYPE_ENABLE
  454. * IOTX_THING_CONTROL_TYPE_DISABLE
  455. * IOTX_THING_CONTROL_TYPE_DELETE
  456. *
  457. * @return 0, Response success; -1, Response fail.
  458. */
  459. int IOT_Thing_Control_Response(void* handle,
  460. const char* product_key,
  461. const char* device_name,
  462. uint32_t message_id,
  463. uint32_t code,
  464. iotx_thing_control_type_t control_type);
  465. /**
  466. * @brief Register down raw callback
  467. * This function used to register down raw callback.
  468. *
  469. * @param pointer of handle, specify the Thing construction.
  470. * @param callback.
  471. *
  472. * @return 0, Register success; -1, Register fail.
  473. */
  474. int IOT_Thing_Down_Raw_Register(void* handle,
  475. down_raw_callback raw_callback);
  476. /**
  477. * @brief Response Down Raw
  478. * This function used to response the raw data down.
  479. *
  480. * @param pointer of handle, specify the Thing construction.
  481. * @param product key.
  482. * @param device name.
  483. * @param Response data.
  484. *
  485. * @return 0, Response success; -1, Response fail.
  486. */
  487. int IOT_Tmp_Down_Raw_Response(void* handle,
  488. const char* product_key,
  489. const char* device_name,
  490. const char* response);
  491. /**
  492. * @brief Register RRPC callback
  493. * This function used to register one RRPC callback.
  494. *
  495. * @param pointer of handle, specify the Thing construction.
  496. * @param product key.
  497. * @param device name.
  498. * @param rrpc callback function.
  499. * every device has RRPC callback by itself.
  500. * RRPC's payload is consists by message id, method and params
  501. * for example:
  502. * "method":"thing.service.property.set";
  503. * "method":"thing.service.property.get";
  504. * "method":"thing.service.{dsl.service.identifer}";
  505. * "method":"thing.service.disable";
  506. * "method":"thing.service.delete";
  507. * "method":"thing.service.enable";
  508. *
  509. *
  510. * @return 0, Register success; -1, Register fail.
  511. */
  512. int IOT_Thing_RRPC_Register( void* handle,
  513. const char* product_key,
  514. const char* device_name,
  515. rrpc_request_callback rrpc_callback);
  516. /**
  517. * @brief Response RRPC request
  518. * This function used to response the RRPC request.
  519. *
  520. * @param pointer of handle, specify the Thing construction.
  521. * @param product key.
  522. * @param device name.
  523. * @param The message id of RRPC request.
  524. * @param Response data.
  525. *
  526. * @return 0, Response success; -1, Response fail.
  527. */
  528. int IOT_Thing_RRPC_Response(void* handle,
  529. const char* product_key,
  530. const char* device_name,
  531. const char* message_id,
  532. const char* response);
  533. /**
  534. * @brief trigger event
  535. * This function used to trigger an event.
  536. *
  537. * @param pointer of handle, specify the Thing construction.
  538. * @param product key.
  539. * @param device name.
  540. * @param event parameter.
  541. * @param event id.
  542. *
  543. * @return 0, trigger success; -1, trigger fail.
  544. */
  545. int IOT_Thing_Trigger_Event(void* handle,
  546. const char* product_key,
  547. const char* device_name,
  548. const char* params,
  549. const char* event_id);
  550. /**
  551. * @brief publish a raw data
  552. * This function used to publish a raw data.
  553. *
  554. * @param pointer of handle, specify the Thing construction.
  555. * @param product key.
  556. * @param device name.
  557. * @param raw data.
  558. * @param raw data length.
  559. *
  560. * @return 0, publish success; -1, publish fail.
  561. */
  562. int IOT_Thing_Publish_Rawdata(void* handle,
  563. const char* product_key,
  564. const char* device_name,
  565. const char* raw_data,
  566. uint32_t raw_data_length);
  567. /**
  568. * @brief yield
  569. * This function used to receive data.
  570. *
  571. * @param pointer of handle, specify the Thing construction.
  572. * @param timeout.
  573. *
  574. * @return 0, recieve success; -1, recieve fail. */
  575. int IOT_Thing_Yield(void* handle, uint32_t timeout);
  576. /**
  577. * @brief subscribe
  578. * This function used to subscribe a topic.
  579. *
  580. * @param pointer of handle, specify the Thing construction.
  581. * @param topic.
  582. * @param qos.
  583. * @param received data callback.
  584. * @param callback user data.
  585. *
  586. * @return 0, subscribe success; -1, subscribe fail.
  587. */
  588. int IOT_Thing_Subscribe(void* handle,
  589. const char *topic_filter,
  590. int qos,
  591. iotx_subdev_event_handle_func_fpt topic_handle_func,
  592. void *pcontext);
  593. /**
  594. * @brief unsubscribe
  595. * This function used to unsubscribe a topic.
  596. *
  597. * @param pointer of handle, specify the Thing construction.
  598. * @param topic.
  599. *
  600. * @return 0, unsubscribe success; -1, unsubscribe fail.
  601. */
  602. int IOT_Thing_Unsubscribe(void* handle,
  603. const char *topic_filter);
  604. /**
  605. * @brief unsubscribe
  606. * This function used to publish a mqtt packet.
  607. *
  608. * @param pointer of handle, specify the Thing construction.
  609. * @param topic.
  610. * @param mqtt message.
  611. *
  612. * @return 0, publish success; -1, publish fail.
  613. */
  614. int IOT_Thing_Publish(void* handle,
  615. const char *topic_name,
  616. iotx_mqtt_topic_info_pt topic_msg);
  617. /* The structure of common reply data */
  618. typedef struct iotx_thing_common_reply_data_st{
  619. int32_t id;
  620. uint32_t code;
  621. char topic[GATEWAY_TOPIC_LEN_MAX];
  622. char* data; /* the data size is dynamic, so data is a ptr, dynamic malloc buffer */
  623. }iotx_thing_common_reply_data_t,*iotx_thing_common_reply_data_pt;
  624. /* The structure of thing data */
  625. typedef struct iotx_thing_masterlave_data_st {
  626. uint32_t sync_status;
  627. iotx_thing_common_reply_data_t replys[IOTX_Thing_REPLY_MAX];
  628. } iotx_thing_masterlave_data_t, *iotx_thing_masterlave_data_pt;
  629. /* The structure of thing context */
  630. typedef struct iotx_thing_masterlave_st {
  631. void* gateway;
  632. iotx_thing_masterlave_data_pt thing_data_t;
  633. service_request_callback service_callback;
  634. down_raw_callback raw_callback;
  635. thing_control_callback thing_callback;
  636. } iotx_thing_masterlave_t, *iotx_thing_masterlave_pt;
  637. extern iotx_thing_masterlave_pt g_thing_masterlave_t;
  638. #define PARAMETER_Thing_CHECK(thing_t) \
  639. do { \
  640. if ((thing_t) == NULL) { \
  641. log_info("param error"); \
  642. return FAIL_RETURN; \
  643. } \
  644. if ((thing_t) != g_thing_masterlave_t) { \
  645. log_info("param error"); \
  646. return FAIL_RETURN; \
  647. } \
  648. if ((thing_t)->gateway == NULL) { \
  649. log_info("param error"); \
  650. return FAIL_RETURN; \
  651. } \
  652. if ((thing_t)->thing_data_t == NULL) { \
  653. log_info("param error"); \
  654. return FAIL_RETURN; \
  655. } \
  656. } while(0)
  657. #define PARAMETER_NULL_CHECK_WITH_RESULT(param, result) \
  658. do { \
  659. if ((param) == NULL) { \
  660. log_info("param error"); \
  661. return (result); \
  662. } \
  663. } while(0)
  664. #define PARAMETER_STRING_NULL_CHECK_WITH_RESULT(ptr, result) \
  665. do { \
  666. if (NULL == (ptr)) { \
  667. log_err("Invalid argument, %s = %p", #ptr, (ptr)); \
  668. return (result); \
  669. } \
  670. if (0 == strlen((ptr))) { \
  671. log_err("Invalid argument, %s = '%s'", #ptr, (ptr)); \
  672. return (result); \
  673. } \
  674. } while(0)
  675. #define MALLOC_MEMORY_WITH_FREE_AND_RESULT(buffer, length, free_buffer, result) \
  676. do { \
  677. if (buffer) \
  678. LITE_free(buffer); \
  679. (buffer) = (void*)LITE_malloc(length); \
  680. if (NULL == (buffer)) { \
  681. log_err("Not enough memory"); \
  682. LITE_free(free_buffer); \
  683. return (result); \
  684. } \
  685. memset((buffer), 0x0, (length)); \
  686. } while(0)
  687. #define PARAMETER_NULL_CHECK(param) \
  688. do { \
  689. if ((param) == NULL) { \
  690. log_info("param error"); \
  691. return; \
  692. } \
  693. } while(0)
  694. typedef enum IOTX_Thing_SUBSCRIBE_UNSUBSCRIBE_TYPES {
  695. IOTX_Thing_SUBSCRIBE_TYPE,
  696. IOTX_Thing_UNSUBSCRIBE_TYPE
  697. }iotx_thing_subcribe_unsubscribe_types_t;
  698. char* iotx_thing_splice_common_event_packet(
  699. const char* params,
  700. const char* event_id,
  701. int32_t* msg_id);
  702. char* iotx_thing_splice_common_deviceinfo_packet(const char* deviceinfo,
  703. const char* method,
  704. int32_t* msg_id);
  705. char *iotx_thing_splice_default_reply_get_packet(int32_t msg_id,
  706. uint32_t code,
  707. const char* data);
  708. char *iotx_thing_splice_default_reply_packet(int32_t msg_id, uint32_t code);
  709. char *iotx_thing_splice_topo_add_packet(const char* product_key,
  710. const char* device_name,
  711. const char* sign,
  712. const char* sign_method,
  713. const char* timestamp,
  714. const char* client_id,
  715. int32_t* msg_id);
  716. char* iotx_thing_splice_common_packet(const char* product_key,
  717. const char* device_name,
  718. int32_t* msg_id,
  719. const char* param1,
  720. const char* param2,
  721. int flag);
  722. int iotx_thing_publish_common_packet(void* handle,
  723. const char* topic_product_key,
  724. const char* topic_device_name,
  725. const char* packet_product_key,
  726. const char* packet_device_name,
  727. const char* param1,
  728. const char* param2,
  729. const char* param3,
  730. iotx_thing_reply_type_t reply_type);
  731. int iotx_thing_publish_common_event_packet(void* handle,
  732. const char* product_key,
  733. const char* device_name,
  734. const char* topic_params,
  735. const char* packet_params,
  736. iotx_thing_reply_type_t reply_type);
  737. int iotx_thing_publish_common_deviceinfo_packet(void* handle,
  738. const char* product_key,
  739. const char* device_name,
  740. const char* deviceinfo,
  741. const char* param1,
  742. const char* param2,
  743. iotx_thing_reply_type_t reply_type);
  744. int iotx_thing_publish_topic_sync(iotx_thing_masterlave_pt thing_t,
  745. int32_t msg_id,
  746. const char* topic,
  747. iotx_mqtt_topic_info_pt topic_msg,
  748. iotx_thing_reply_type_t reply);
  749. int iotx_thing_subscribe_unsubscribe_topic(iotx_thing_masterlave_pt thing_t,
  750. const char* topic,
  751. iotx_thing_subcribe_unsubscribe_types_t is_subscribe);
  752. int iotx_thing_subscribe_unsubscribe_basic(iotx_thing_masterlave_pt thing_t,
  753. iotx_thing_subcribe_unsubscribe_types_t is_subscribe);
  754. int iotx_thing_subscribe_unsubscribe_enhance(iotx_thing_masterlave_pt thing_t,
  755. const char* product_key,
  756. const char* device_name,
  757. iotx_thing_subcribe_unsubscribe_types_t is_subscribe);
  758. void iotx_thing_splice_device_cloud_id(char* device_cloud_id,
  759. const char* product_key,
  760. const char* device_name);
  761. #endif /* SRC_SUBDEVICE_Thing_UTIL_H_ */