subdev-example.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "iot_import.h"
  5. #include "iot_export.h"
  6. #include "subdev_example_api.h"
  7. extern void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len);
  8. extern void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len);
  9. #define PRODUCT_KEY "a1BGmFnszAj"
  10. #define DEVICE_NAME "gateway"
  11. #define DEVICE_SECRET "Z9lPWO48TyOg90ykmxfkYK4TdJTiZrDF"
  12. #define SUB_1_PRODUCT_KEY "a1BGmFnszAj"
  13. #define SUB_1_DEVICE_NAME "subdev_5"
  14. #define SUB_1_DEVICE_SECRET "iJJcce70yMvXaeJzdRGwBzvNXBpI6cxR"
  15. #define SUB_2_PRODUCT_KEY "a1BGmFnszAj"
  16. #define SUB_2_DEVICE_NAME "subdev_6"
  17. #define SUB_2_DEVICE_SECRET "N7OyAHhVFHBp4NFfLHLz2nEBXyzrjDEC"
  18. #define MSG_LEN_MAX (1024 * 4)
  19. char g_product_key[PRODUCT_KEY_LEN + 1];
  20. char g_product_secret[PRODUCT_SECRET_LEN + 1];
  21. char g_device_name[DEVICE_NAME_LEN + 1];
  22. char g_device_secret[DEVICE_SECRET_LEN + 1];
  23. #define TOPIC_GATEWAY_UPDATE "/"PRODUCT_KEY"/"DEVICE_NAME"/update"
  24. #define TOPIC_SUBDEVICE1_UPDATE "/"SUB_1_PRODUCT_KEY"/"SUB_1_DEVICE_NAME"/update"
  25. #define TOPIC_SUBDEVICE2_UPDATE "/"SUB_2_PRODUCT_KEY"/"SUB_2_DEVICE_NAME"/updae"
  26. static void* g_thing_t = NULL;
  27. static void* g_gateway_t = NULL;
  28. static int _calc_sign(const char* product_key,
  29. const char* device_name,
  30. const char* device_secret,
  31. char* hmac_sigbuf,
  32. const int hmac_buflen,
  33. const char* sign_method,
  34. const char *client_id,
  35. const char *timestamp_str)
  36. {
  37. char signature[64];
  38. char hmac_source[256];
  39. memset(signature, 0, sizeof(signature));
  40. memset(hmac_source, 0, sizeof(hmac_source));
  41. HAL_Snprintf(hmac_source,
  42. sizeof(hmac_source),
  43. "clientId%s" "deviceName%s" "productKey%s" "timestamp%s",
  44. client_id,
  45. device_name,
  46. product_key,
  47. timestamp_str);
  48. if (0 == strncmp(sign_method, "hmacsha1", strlen("hmacsha1"))) {
  49. utils_hmac_sha1(hmac_source, strlen(hmac_source),
  50. signature,
  51. device_secret,
  52. strlen(device_secret));
  53. } else if (0 == strncmp(sign_method, "hmacmd5", strlen("hmacmd5"))){
  54. utils_hmac_md5(hmac_source, strlen(hmac_source),
  55. signature,
  56. device_secret,
  57. strlen(device_secret));
  58. }
  59. memcpy(hmac_sigbuf, signature, hmac_buflen);
  60. return 0;
  61. }
  62. void rrpc_request_handler(void* gateway,
  63. const char* product_key,
  64. const char* device_name,
  65. const char* message_id,
  66. const char* payload)
  67. {
  68. printf("rrpc_request_callback, product_key [%s], device_name [%s], message_id [%s], payload:%s \n",
  69. product_key,
  70. device_name,
  71. message_id,
  72. payload);
  73. if (IOT_Gateway_RRPC_Response(gateway,
  74. product_key,
  75. device_name,
  76. message_id,
  77. "aa00090000ff00003039001b") >= 0) {
  78. printf("gateway rrpc repose ok success\n");
  79. } else {
  80. printf("gateway rrpc repose ok fail\n");
  81. }
  82. }
  83. void service_request_handler(void* thing_t,
  84. const char* product_key,
  85. const char* device_name,
  86. iotx_thing_service_type_t service_type,
  87. uint32_t message_id,
  88. char* params,
  89. uint32_t params_length,
  90. const char* service_id) // preperty's serviceid is NULL
  91. {
  92. printf("service request callback product_key[%s]\n device_name[%s]\n message_id[%d]\n"
  93. "service_type[%d]\n service_id[%s]\n params[%s]\n\n",
  94. product_key, device_name, message_id, service_type, service_id, params);
  95. switch(service_type) {
  96. case IOTX_Thing_SERVICE_TYPE_PROPERTY_SET:
  97. if (FAIL_RETURN == IOT_Thing_Set_Property_Response(thing_t,
  98. product_key,
  99. device_name,
  100. message_id,
  101. 200)) {
  102. printf("service reponse fail\n");
  103. } else {
  104. printf("service reponse success\n");
  105. }
  106. break;
  107. case IOTX_Thing_SERVICE_TYPE_PROPERTY_GET:
  108. if (FAIL_RETURN == IOT_Thing_Get_Property_Response(thing_t,
  109. product_key,
  110. device_name,
  111. message_id,
  112. "Property_get",
  113. 200)) {
  114. printf("service reponse fail\n");
  115. } else {
  116. printf("service reponse success\n");
  117. }
  118. break;
  119. case IOTX_Thing_SERVICE_TYPE_UNDEFINED:
  120. if (FAIL_RETURN == IOT_Thing_Service_Response(thing_t,
  121. product_key,
  122. device_name,
  123. service_id,
  124. message_id,
  125. 200)) {
  126. printf("service reponse fail\n");
  127. } else {
  128. printf("service reponse success\n");
  129. }
  130. break;
  131. default:
  132. printf("service_type error\n");
  133. break;
  134. }
  135. }
  136. void down_raw_handler(void* thing_t,
  137. const char* product_key,
  138. const char* device_name,
  139. const char* raw_data,
  140. uint32_t raw_data_length)
  141. {
  142. printf("service request callback product_key[%s], device_name[%s], raw_data[%d][%s]\n",
  143. product_key, device_name, raw_data_length, raw_data);
  144. if (FAIL_RETURN == IOT_Tmp_Down_Raw_Response(thing_t,
  145. product_key,
  146. device_name,
  147. "down raw reply")){
  148. printf("down raw reply fail");
  149. return;
  150. }
  151. if (SUCCESS_RETURN == IOT_Thing_Publish_Rawdata(thing_t,
  152. product_key,
  153. device_name,
  154. "up raw data test",
  155. strlen("up raw data test"))) {
  156. printf("publish raw data success");
  157. } else {
  158. printf("publish raw data fail");
  159. }
  160. }
  161. void thing_control_handler(void* thing_t,
  162. const char* product_key,
  163. const char* device_name,
  164. iotx_thing_control_type_t thing_control_type,
  165. uint32_t message_id)
  166. {
  167. printf("thing control callback product_key[%s], device_name[%s], thing_control_type[%d] message_id[%d]\n",
  168. product_key, device_name, thing_control_type, message_id);
  169. if (SUCCESS_RETURN == IOT_Thing_Control_Response(thing_t,
  170. product_key,
  171. device_name,
  172. message_id,
  173. 200,
  174. thing_control_type)) {
  175. printf("thing control success");
  176. } else {
  177. printf("thing control fail");
  178. }
  179. }
  180. // 示例如何使用subdev的API直接进行开发
  181. int demo_gateway_function(char *msg_buf, char *msg_readbuf)
  182. {
  183. int rc, msg_len, cnt = 0;;
  184. iotx_conn_info_pt puser_info;
  185. iotx_mqtt_param_t mqtt_t;
  186. iotx_gateway_param_t gateway_param;
  187. iotx_mqtt_topic_info_t topic_msg;
  188. char msg_pub[128] = {0};
  189. char sign[41] = {0};
  190. char timestamp[20] = {0};
  191. char client_id[64/*32*/] = {0};
  192. /**< get device info*/
  193. HAL_GetProductKey(g_product_key);
  194. HAL_GetDeviceName(g_device_name);
  195. HAL_GetDeviceSecret(g_device_secret);
  196. /**< end*/
  197. /* Device AUTH */
  198. rc = IOT_SetupConnInfo(g_product_key, g_device_name, g_device_secret, (void **)&puser_info);
  199. if (SUCCESS_RETURN != rc) {
  200. printf("rc = IOT_SetupConnInfo() = %d\n", rc);
  201. return rc;
  202. }
  203. /* Construct a master-slave */
  204. memset(&mqtt_t, 0, sizeof(iotx_mqtt_param_t));
  205. memset(&gateway_param, 0, sizeof(iotx_gateway_param_t));
  206. gateway_param.mqtt = &mqtt_t;
  207. gateway_param.mqtt->port = puser_info->port;
  208. gateway_param.mqtt->host = puser_info->host_name;
  209. gateway_param.mqtt->client_id = puser_info->client_id;
  210. gateway_param.mqtt->username = puser_info->username;
  211. gateway_param.mqtt->password = puser_info->password;
  212. gateway_param.mqtt->pub_key = puser_info->pub_key;
  213. gateway_param.mqtt->request_timeout_ms = 2000;
  214. gateway_param.mqtt->clean_session = 0;
  215. gateway_param.mqtt->keepalive_interval_ms = 60000;
  216. gateway_param.mqtt->pread_buf = msg_readbuf;
  217. gateway_param.mqtt->read_buf_size = MSG_LEN_MAX;
  218. gateway_param.mqtt->pwrite_buf = msg_buf;
  219. gateway_param.mqtt->write_buf_size = MSG_LEN_MAX;
  220. gateway_param.mqtt->handle_event.h_fp = NULL;
  221. gateway_param.mqtt->handle_event.pcontext = NULL;
  222. printf(" ~~~~~~~~~~~~~~~~ start test ~~~~~~~~~~~~~~~~~ \n");
  223. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  224. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  225. /* construct */
  226. printf(" ~~~~~~~~~~~~~~ start construct ~~~~~~~~~~~~~ \n");
  227. g_gateway_t = IOT_Gateway_Construct(&gateway_param);
  228. if (NULL == g_gateway_t) {
  229. printf("construct Gateway failed!\n");
  230. return rc;
  231. }
  232. if (FAIL_RETURN == IOT_Gateway_RRPC_Register(g_gateway_t,
  233. PRODUCT_KEY,
  234. DEVICE_NAME,
  235. rrpc_request_handler)) {
  236. printf("rrpc register error\n");
  237. IOT_Gateway_Destroy(&g_gateway_t);
  238. return FAIL_RETURN;
  239. }
  240. printf(" ~~~~~~~~~~~~ construct success ~~~~~~~~~~~~~ \n");
  241. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  242. printf(" ~~~~~~~~~~~~~~~ start login ~~~~~~~~~~~~~~~~ \n");
  243. /* timestamp */
  244. strncpy(timestamp, "2524608000000", strlen("2524608000000") + 1);
  245. /* client id */
  246. HAL_Snprintf(client_id, /*32*/64, "%s.%s", SUB_1_PRODUCT_KEY, SUB_1_DEVICE_NAME);
  247. /* sign */
  248. if (FAIL_RETURN == _calc_sign(SUB_1_PRODUCT_KEY,
  249. SUB_1_DEVICE_NAME,
  250. SUB_1_DEVICE_SECRET,
  251. sign,
  252. 41,
  253. "hmacsha1",
  254. client_id,
  255. timestamp)) {
  256. printf("sign fail \n");
  257. return FAIL_RETURN;
  258. }
  259. printf(" ~~~~~~~~~~~~~~ start register ~~~~~~~~~~~~~~~ \n");
  260. if (FAIL_RETURN == (rc = IOT_Subdevice_Register(g_gateway_t,
  261. IOTX_Thing_REGISTER_TYPE_STATIC,
  262. SUB_1_PRODUCT_KEY,
  263. SUB_1_DEVICE_NAME,
  264. timestamp,
  265. client_id,
  266. sign,
  267. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA))) {
  268. printf("register fail \n");
  269. IOT_Gateway_Destroy(&g_gateway_t);
  270. return FAIL_RETURN;
  271. }
  272. rc = IOT_Subdevice_Login(g_gateway_t,
  273. SUB_1_PRODUCT_KEY,
  274. SUB_1_DEVICE_NAME,
  275. timestamp,
  276. client_id,
  277. sign,
  278. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA,
  279. IOTX_SUBDEV_CLEAN_SESSION_TRUE);
  280. if (SUCCESS_RETURN != rc) {
  281. printf("1 rc = IOT_Subdevice_Login() = %d\n", rc);
  282. IOT_Gateway_Destroy((void**)&g_gateway_t);
  283. return rc;
  284. }
  285. if (FAIL_RETURN == IOT_Gateway_RRPC_Register(g_gateway_t,
  286. SUB_1_PRODUCT_KEY,
  287. SUB_1_DEVICE_NAME,
  288. rrpc_request_handler)) {
  289. printf("rrpc register error\n");
  290. return FAIL_RETURN;
  291. }
  292. printf(" ~~~~~~~~~~~~~~ start register ~~~~~~~~~~~~~~~ \n");
  293. // 如果想测试动态注册,请将如下代码打开,并且将SUB_2_DEVICE_NAME重新定义一个值,例如“subdev_6”
  294. #if 0
  295. printf(" ~~~~~~~~~~~~~ dynamic register ~~~~~~~~~~~~~~ \n");
  296. if (FAIL_RETURN == (rc = IOT_Subdevice_Register(g_gateway_t,
  297. IOTX_Thing_REGISTER_TYPE_DYNAMIC,
  298. SUB_2_PRODUCT_KEY,
  299. SUB_2_DEVICE_NAME,
  300. NULL,
  301. NULL,
  302. NULL,
  303. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA))) {
  304. printf("2 register fail \n");
  305. IOT_Gateway_Destroy(&g_gateway_t);
  306. return FAIL_RETURN;
  307. }
  308. rc = IOT_Subdevice_Login(g_gateway_t,
  309. SUB_2_PRODUCT_KEY,
  310. SUB_2_DEVICE_NAME,
  311. NULL,
  312. NULL,
  313. NULL,
  314. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA,
  315. IOTX_SUBDEV_CLEAN_SESSION_FALSE);
  316. if (SUCCESS_RETURN != rc) {
  317. printf("2 rc = IOT_Subdevice_Login() = %d\n", rc);
  318. IOT_Gateway_Destroy((void**)&g_gateway_t);
  319. return rc;
  320. }
  321. if (FAIL_RETURN == IOT_Gateway_RRPC_Register(g_gateway_t,
  322. SUB_2_PRODUCT_KEY,
  323. SUB_2_DEVICE_NAME,
  324. rrpc_request_handler)) {
  325. printf("rrpc register error\n");
  326. return FAIL_RETURN;
  327. }
  328. #else
  329. memset(client_id, 0x0, 64);
  330. /* client id */
  331. HAL_Snprintf(client_id, /*32*/64, "%s.%s", SUB_2_PRODUCT_KEY, SUB_2_DEVICE_NAME);
  332. memset(sign, 0x0, 41);
  333. /* sign */
  334. if (FAIL_RETURN == _calc_sign(SUB_2_PRODUCT_KEY,
  335. SUB_2_DEVICE_NAME,
  336. SUB_2_DEVICE_SECRET,
  337. sign,
  338. 41,
  339. "hmacsha1",
  340. client_id,
  341. timestamp)) {
  342. printf("sign fail \n");
  343. return FAIL_RETURN;
  344. }
  345. if (FAIL_RETURN == (rc = IOT_Subdevice_Register(g_gateway_t,
  346. IOTX_Thing_REGISTER_TYPE_STATIC,
  347. SUB_2_PRODUCT_KEY,
  348. SUB_2_DEVICE_NAME,
  349. timestamp,
  350. client_id,
  351. sign,
  352. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA))) {
  353. printf("2 register fail \n");
  354. IOT_Gateway_Destroy(&g_gateway_t);
  355. return FAIL_RETURN;
  356. }
  357. rc = IOT_Subdevice_Login(g_gateway_t,
  358. SUB_2_PRODUCT_KEY,
  359. SUB_2_DEVICE_NAME,
  360. timestamp,
  361. client_id,
  362. sign,
  363. IOTX_SUBDEV_SIGN_METHOD_TYPE_SHA,
  364. IOTX_SUBDEV_CLEAN_SESSION_FALSE);
  365. if (SUCCESS_RETURN != rc) {
  366. printf("2 rc = IOT_Subdevice_Login() = %d\n", rc);
  367. IOT_Gateway_Destroy((void**)&g_gateway_t);
  368. return rc;
  369. }
  370. if (FAIL_RETURN == IOT_Gateway_RRPC_Register(g_gateway_t,
  371. SUB_2_PRODUCT_KEY,
  372. SUB_2_DEVICE_NAME,
  373. rrpc_request_handler)) {
  374. printf("rrpc register error\n");
  375. return FAIL_RETURN;
  376. }
  377. #endif
  378. printf(" ~~~~~~~~~~~~~~ login success ~~~~~~~~~~~~~~ \n");
  379. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  380. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  381. printf(" ~~~~~~~~~~~~~ start publish ~~~~~~~~~~~~~~~ \n");
  382. printf(" ~~~~~~~~~~~~ gateway publish ~~~~~~~~~~~~~~ \n");
  383. /* Initialize topic information */
  384. memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
  385. strcpy(msg_pub, "message: hello! start!");
  386. topic_msg.qos = IOTX_MQTT_QOS1;
  387. topic_msg.retain = 0;
  388. topic_msg.dup = 0;
  389. topic_msg.payload = (void *)msg_pub;
  390. topic_msg.payload_len = strlen(msg_pub);
  391. rc = IOT_Gateway_Publish(g_gateway_t, TOPIC_GATEWAY_UPDATE, &topic_msg);
  392. printf("gateway rc = IOT_Gateway_Publish() = %d\n", rc);
  393. do {
  394. /* Generate topic message */
  395. cnt++;
  396. printf(" ~~~~~~~~~~~ subdevice1 publish ~~~~~~~~~~~~ \n");
  397. msg_len = snprintf(msg_pub, sizeof(msg_pub), "subdevice1, hello");
  398. if (msg_len < 0) {
  399. printf("Error occur! Exit program\n");
  400. IOT_Gateway_Destroy((void**)&g_gateway_t);
  401. return -1;
  402. }
  403. topic_msg.payload = (void *)msg_pub;
  404. topic_msg.payload_len = msg_len;
  405. rc = IOT_Gateway_Publish(g_gateway_t, TOPIC_SUBDEVICE1_UPDATE, &topic_msg);
  406. if (rc < 0) {
  407. printf("error occur when publish\n");
  408. IOT_Gateway_Destroy((void**)&g_gateway_t);
  409. return -1;
  410. }
  411. printf("subdevice1 packet_id=%u, publish topic msg=%s\n", (uint32_t)rc, msg_pub);
  412. /* handle the MQTT packet received from TCP or SSL connection */
  413. IOT_Gateway_Yield(g_gateway_t, 200);
  414. printf(" ~~~~~~~~~~~ subdevice2 publish ~~~~~~~~~~~~ \n");
  415. memset(msg_pub, 0x0, 128);
  416. msg_len = snprintf(msg_pub, sizeof(msg_pub), "subdevice2, hello");
  417. topic_msg.qos = IOTX_MQTT_QOS0;
  418. topic_msg.payload = (void *)msg_pub;
  419. topic_msg.payload_len = strlen(msg_pub);
  420. rc = IOT_Gateway_Publish(g_gateway_t, TOPIC_SUBDEVICE2_UPDATE, &topic_msg);
  421. if (rc < 0) {
  422. printf("error occur when publish\n");
  423. IOT_Gateway_Destroy((void**)&g_gateway_t);
  424. return -1;
  425. }
  426. printf("subdevice2 packet_id=%u, publish topic msg=%s\n", (uint32_t)rc, msg_pub);
  427. } while (0);
  428. printf(" ~~~~~~~~~~~~ publish success ~~~~~~~~~~~~~~ \n");
  429. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  430. /* handle the MQTT packet received from TCP or SSL connection */
  431. while (cnt < 30) {
  432. IOT_Gateway_Yield(g_gateway_t, 200);
  433. cnt++;
  434. }
  435. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  436. HAL_SleepMs(200);
  437. printf(" ~~~~~~~~~~~~~ start logout ~~~~~~~~~~~~~~~~ \n");
  438. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  439. rc = IOT_Subdevice_Logout(g_gateway_t, SUB_1_PRODUCT_KEY, SUB_1_DEVICE_NAME);
  440. if (SUCCESS_RETURN != rc) {
  441. printf("1 rc = IOT_Subdevice_Logout() = %d\n", rc);
  442. IOT_Gateway_Destroy((void**)&g_gateway_t);
  443. return rc;
  444. }
  445. rc = IOT_Subdevice_Logout(g_gateway_t, SUB_2_PRODUCT_KEY, SUB_2_DEVICE_NAME);
  446. if (SUCCESS_RETURN != rc) {
  447. printf("2 rc = IOT_Subdevice_Logout() = %d\n", rc);
  448. IOT_Gateway_Destroy((void**)&g_gateway_t);
  449. return rc;
  450. }
  451. printf(" ~~~~~~~~~~~~~ logout success ~~~~~~~~~~~~~~ \n");
  452. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  453. // 请勿随意unregister 一个设备,如果该设备被unregister后,该设备就不能再使用,请注意
  454. #if 0
  455. rc = IOT_Subdevice_Unregister(g_gateway_t, SUB_1_PRODUCT_KEY, SUB_1_DEVICE_NAME);
  456. if (SUCCESS_RETURN != rc) {
  457. printf("1 rc = IOT_Subdevice_Unregister() = %d\n", rc);
  458. IOT_Gateway_Destroy((void**)&g_gateway_t);
  459. return rc;
  460. }
  461. rc = IOT_Subdevice_Unregister(g_gateway_t, SUB_2_PRODUCT_KEY, SUB_2_DEVICE_NAME);
  462. if (SUCCESS_RETURN != rc) {
  463. printf("2 rc = IOT_Subdevice_Unregister() = %d\n", rc);
  464. IOT_Gateway_Destroy((void**)&g_gateway_t);
  465. return rc;
  466. }
  467. #endif
  468. IOT_Gateway_Destroy((void**)&g_gateway_t);
  469. printf(" ~~~~~~~~~~~~ destory success ~~~~~~~~~~~~~~ \n");
  470. return SUCCESS_RETURN;
  471. }
  472. /* test for only one device */
  473. // 示例使用subdev_example_api.h中对topic进行封装的API进行单品设备开发
  474. int demo_only_one_device(char *msg_buf, char *msg_readbuf)
  475. {
  476. int rc, cnt = 0;
  477. uint32_t length = 0;
  478. iotx_conn_info_pt puser_info;
  479. iotx_mqtt_param_t mqtt_t;
  480. iotx_thing_param_t thing_param;
  481. //具体取值请根据自行创建的产品模型进行调整,可以参考运行时日志会提示dsl具体大小,取值需比dsl大小大即可
  482. char sub_dsltemplate_get[1024 * 4] = {0} ;
  483. /**< get device info*/
  484. HAL_GetProductKey(g_product_key);
  485. HAL_GetDeviceName(g_device_name);
  486. HAL_GetDeviceSecret(g_device_secret);
  487. /**< end*/
  488. /* Device AUTH */
  489. rc = IOT_SetupConnInfo(g_product_key, g_device_name, g_device_secret, (void **)&puser_info);
  490. if (SUCCESS_RETURN != rc) {
  491. printf("rc = IOT_SetupConnInfo() = %d\n", rc);
  492. return rc;
  493. }
  494. /* Construct a master-slave */
  495. memset(&mqtt_t, 0, sizeof(iotx_mqtt_param_t));
  496. memset(&thing_param, 0, sizeof(iotx_thing_param_t));
  497. thing_param.mqtt = &mqtt_t;
  498. thing_param.mqtt->port = puser_info->port;
  499. thing_param.mqtt->host = puser_info->host_name;
  500. thing_param.mqtt->client_id = puser_info->client_id;
  501. thing_param.mqtt->username = puser_info->username;
  502. thing_param.mqtt->password = puser_info->password;
  503. thing_param.mqtt->pub_key = puser_info->pub_key;
  504. thing_param.mqtt->request_timeout_ms = 2000;
  505. thing_param.mqtt->clean_session = 0;
  506. thing_param.mqtt->keepalive_interval_ms = 60000;
  507. thing_param.mqtt->pread_buf = msg_readbuf;
  508. thing_param.mqtt->read_buf_size = MSG_LEN_MAX;
  509. thing_param.mqtt->pwrite_buf = msg_buf;
  510. thing_param.mqtt->write_buf_size = MSG_LEN_MAX;
  511. thing_param.mqtt->handle_event.h_fp = NULL;
  512. thing_param.mqtt->handle_event.pcontext = NULL;
  513. printf(" ~~~~~~~~~~~~~~~~ start test ~~~~~~~~~~~~~~~~~ \n");
  514. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  515. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  516. /* construct */
  517. printf(" ~~~~~~~~~~~~~~ start construct ~~~~~~~~~~~~~ \n");
  518. g_thing_t = IOT_Thing_Construct(&thing_param);
  519. if (NULL == g_thing_t) {
  520. printf("construct device Thing failed!\n");
  521. return rc;
  522. }
  523. if (FAIL_RETURN == IOT_Thing_RRPC_Register(g_thing_t,
  524. PRODUCT_KEY,
  525. DEVICE_NAME,
  526. rrpc_request_handler)) {
  527. printf("rrpc register error\n");
  528. return FAIL_RETURN;
  529. }
  530. printf(" ~~~~~~~~~~~~ construct success ~~~~~~~~~~~~~ \n");
  531. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  532. printf(" ~~~~~~~~ start register callback ~~~~~~~~~~ \n");
  533. printf(" ~~~~~~~~~~~~ service register ~~~~~~~~~~~~~ \n");
  534. rc = IOT_Thing_Service_Register(g_thing_t, service_request_handler);
  535. if (SUCCESS_RETURN != rc) {
  536. printf("service register rc = IOT_Thing_Service_Register() = %d\n", rc);
  537. goto exit;
  538. }
  539. printf(" ~~~~~~~~~~~~ down raw register ~~~~~~~~~~~~ \n");
  540. rc = IOT_Thing_Down_Raw_Register(g_thing_t, down_raw_handler);
  541. if (SUCCESS_RETURN != rc) {
  542. printf("raw register rc = IOT_Thing_Down_Raw_Register() = %d\n", rc);
  543. goto exit;
  544. }
  545. printf(" ~~~~~~~~~~ thing control register ~~~~~~~~~~ \n");
  546. rc = IOT_Thing_Control_Register(g_thing_t, thing_control_handler);
  547. if (SUCCESS_RETURN != rc) {
  548. printf("raw register rc = IOT_Thing_Down_Raw_Register() = %d\n", rc);
  549. goto exit;
  550. }
  551. printf(" ~~~~~~~~ register callback success ~~~~~~~~ \n");
  552. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  553. length = 1024 * 4; //具体取值请根据自行创建的产品模型进行调整,可以参考运行时日志会提示dsl具体大小,取值需比dsl大小大即可
  554. printf(" ~~~~~~~~~~~~ start dsl template ~~~~~~~~~~~ \n");
  555. printf(" ~~~~~~~~~~~~~~~~~~~~ get ~~~~~~~~~~~~~~~~~~ \n");
  556. rc = IOT_Thing_Get_Dsl_Template(g_thing_t,
  557. PRODUCT_KEY,
  558. DEVICE_NAME,
  559. sub_dsltemplate_get,
  560. &length);
  561. if (SUCCESS_RETURN != rc) {
  562. printf("dsl template get rc = IOT_Thing_Dsl_Template() = %d\n", rc);
  563. goto exit;
  564. }
  565. printf("sub_dsltemplate_get: length[%d]:[%s]\n", length, sub_dsltemplate_get);
  566. printf(" ~~~~~~~~~~~ dsl template success ~~~~~~~~~~ \n");
  567. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  568. //具体参数请自行参考云端控制台提示
  569. printf(" ~~~~~~~~~~~ start thing property ~~~~~~~~~~ \n");
  570. printf(" ~~~~~~~~~~~~~~~~~~~ post ~~~~~~~~~~~~~~~~~~ \n");
  571. rc = IOT_Thing_Post_Property(g_thing_t,
  572. PRODUCT_KEY,
  573. DEVICE_NAME,
  574. "{\"afabwa\":\"sfs\"}");
  575. printf(" ~~~~~~~~~~ thing property success ~~~~~~~~~ \n");
  576. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  577. //具体参数请自行参考云端控制台提示
  578. printf(" ~~~~~~~~~~~ start trigger event ~~~~~~~~~~~ \n");
  579. rc = IOT_Thing_Trigger_Event(g_thing_t,
  580. PRODUCT_KEY,
  581. DEVICE_NAME,
  582. "{\"dafd\":0.90}",
  583. "propertyIdentifier20");
  584. //具体参数请自行参考云端控制台提示
  585. printf(" ~~~~~~~~~~~~~ start deviceinfo ~~~~~~~~~~~~ \n");
  586. printf(" ~~~~~~~~~~~~~~~~~~ update ~~~~~~~~~~~~~~~~~ \n");
  587. rc = IOT_Thing_Update_Deviceinfo(g_thing_t,
  588. PRODUCT_KEY,
  589. DEVICE_NAME,
  590. "{\"attrKey\":\"Temperature\",\"attrValue\":36.8}");
  591. //具体参数请自行参考云端控制台提示
  592. printf(" ~~~~~~~~~~~~~~~~~~ delete ~~~~~~~~~~~~~~~~~ \n");
  593. rc = IOT_Thing_Delete_Deviceinfo(g_thing_t,
  594. PRODUCT_KEY,
  595. DEVICE_NAME,
  596. "{\"attrKey\":\"Temperature\",\"attrValue\":\"36.8\"}");
  597. printf(" ~~~~~~~~~~~~ deviceinfo success ~~~~~~~~~~~ \n");
  598. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  599. //具体参数请自行参考云端控制台提示
  600. printf(" ~~~~~~~~~~ start publish rawdata ~~~~~~~~~~ \n");
  601. rc = IOT_Thing_Publish_Rawdata(g_thing_t,
  602. PRODUCT_KEY,
  603. DEVICE_NAME,
  604. "aa00090000ff00003039001b",
  605. strlen("aa00090000ff00003039001b"));
  606. printf(" ~~~~~~~~~ publish rawdata success ~~~~~~~~~ \n");
  607. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  608. printf(" ~~~~~~~~~~~~~~~ start yield ~~~~~~~~~~~~~~~ \n");
  609. while(cnt < 30) {
  610. IOT_Thing_Yield(g_thing_t, 200);
  611. cnt++;
  612. }
  613. exit:
  614. HAL_SleepMs(200);
  615. IOT_Thing_Destroy((void**)&g_thing_t);
  616. printf(" ~~~~~~~~~~~~ destory success ~~~~~~~~~~~~~~ \n");
  617. return SUCCESS_RETURN;
  618. }
  619. // 示例使用subdev_example_api.h中对topic进行封装的API进行网关开发
  620. int demo_thing_function(char *msg_buf, char *msg_readbuf)
  621. {
  622. int rc, cnt = 0;
  623. uint32_t length = 0;
  624. uint32_t topo_length = 0;
  625. iotx_conn_info_pt puser_info;
  626. iotx_mqtt_param_t mqtt_t;
  627. iotx_thing_param_t thing_param;
  628. //具体取值请根据自行创建的产品模型进行调整,可以参考运行时日志会提示dsl具体大小,取值需比dsl大小大即可
  629. char sub_dsltemplate_get[1024 * 4] = {0} ;
  630. char dsltemplate_printf[512] = {0};
  631. int printf_num = 0;
  632. char sign[41] = {0};
  633. char timestamp[20] = {0};
  634. char client_id[32] = {0}; //请根据devicename和productkey调整大小
  635. char get_topo_reply[512] = {0};
  636. /**< get device info*/
  637. HAL_GetProductKey(g_product_key);
  638. HAL_GetDeviceName(g_device_name);
  639. HAL_GetDeviceSecret(g_device_secret);
  640. /**< end*/
  641. /* Device AUTH */
  642. rc = IOT_SetupConnInfo(g_product_key, g_device_name, g_device_secret, (void **)&puser_info);
  643. if (SUCCESS_RETURN != rc) {
  644. printf("rc = IOT_SetupConnInfo() = %d\n", rc);
  645. return rc;
  646. }
  647. /* Construct a master-slave */
  648. memset(&mqtt_t, 0, sizeof(iotx_mqtt_param_t));
  649. memset(&thing_param, 0, sizeof(iotx_thing_param_t));
  650. thing_param.mqtt = &mqtt_t;
  651. thing_param.mqtt->port = puser_info->port;
  652. thing_param.mqtt->host = puser_info->host_name;
  653. thing_param.mqtt->client_id = puser_info->client_id;
  654. thing_param.mqtt->username = puser_info->username;
  655. thing_param.mqtt->password = puser_info->password;
  656. thing_param.mqtt->pub_key = puser_info->pub_key;
  657. thing_param.mqtt->request_timeout_ms = 2000;
  658. thing_param.mqtt->clean_session = 0;
  659. thing_param.mqtt->keepalive_interval_ms = 60000;
  660. thing_param.mqtt->pread_buf = msg_readbuf;
  661. thing_param.mqtt->read_buf_size = MSG_LEN_MAX;
  662. thing_param.mqtt->pwrite_buf = msg_buf;
  663. thing_param.mqtt->write_buf_size = MSG_LEN_MAX;
  664. thing_param.mqtt->handle_event.h_fp = NULL;
  665. thing_param.mqtt->handle_event.pcontext = NULL;
  666. printf(" ~~~~~~~~~~~~~~~~ start test ~~~~~~~~~~~~~~~~~ \n");
  667. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  668. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  669. /* construct */
  670. printf(" ~~~~~~~~~~~~~~ start construct ~~~~~~~~~~~~~ \n");
  671. g_thing_t = IOT_Thing_Construct(&thing_param);
  672. if (NULL == g_thing_t) {
  673. printf("construct device Thing failed!\n");
  674. return rc;
  675. }
  676. if (FAIL_RETURN == IOT_Thing_RRPC_Register(g_thing_t, //g_thing_t,
  677. PRODUCT_KEY,
  678. DEVICE_NAME,
  679. rrpc_request_handler)) {
  680. printf("rrpc register error\n");
  681. return FAIL_RETURN;
  682. }
  683. printf(" ~~~~~~~~~~~~ construct success ~~~~~~~~~~~~~ \n");
  684. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  685. // 静态注册子设备
  686. printf(" ~~~~~~~~~~~~~~ start register ~~~~~~~~~~~~~~ \n");
  687. printf(" ~~~~~~~~~~~~~~~~~~ static ~~~~~~~~~~~~~~~~~~ \n");
  688. printf(" ~~~~~~~~~~~~~~~~~~ sub1 ~~~~~~~~~~~~~~~~~~~ \n");
  689. /* timestamp */
  690. strncpy(timestamp, "2524608000000", strlen("2524608000000") + 1);
  691. /* client id */
  692. HAL_Snprintf(client_id, 32, "%s.%s",SUB_1_PRODUCT_KEY, SUB_1_DEVICE_NAME);
  693. /* sign */
  694. if (FAIL_RETURN == _calc_sign(SUB_1_PRODUCT_KEY,
  695. SUB_1_DEVICE_NAME,
  696. SUB_1_DEVICE_SECRET,
  697. sign,
  698. 41,
  699. "hmacsha1",
  700. client_id,
  701. timestamp)) {
  702. printf("sign fail \n");
  703. return FAIL_RETURN;
  704. }
  705. rc = IOT_Thing_Register(g_thing_t,
  706. IOTX_Thing_REGISTER_TYPE_STATIC,
  707. SUB_1_PRODUCT_KEY,
  708. SUB_1_DEVICE_NAME,
  709. timestamp,
  710. client_id,
  711. sign,
  712. IOTX_Thing_SIGN_METHOD_TYPE_SHA);
  713. if (SUCCESS_RETURN != rc) {
  714. printf("static rc = IOT_Thing_Register() = %d\n", rc);
  715. IOT_Thing_Destroy((void**)&g_thing_t);
  716. return rc;
  717. }
  718. // login
  719. printf(" ~~~~~~~~~~~~~~~ start login ~~~~~~~~~~~~~~~ \n");
  720. rc = IOT_Thing_Login(g_thing_t,
  721. SUB_1_PRODUCT_KEY,
  722. SUB_1_DEVICE_NAME,
  723. timestamp,
  724. client_id,
  725. sign,
  726. IOTX_Thing_SIGN_METHOD_TYPE_SHA,
  727. IOTX_Thing_CLEAN_SESSION_TYPE_TRUE);
  728. if (SUCCESS_RETURN != rc) {
  729. printf("login sub1 rc = IOT_Thing_Login() = %d\n", rc);
  730. goto exit;
  731. return rc;
  732. }
  733. printf(" ~~~~~~~~~~~~~~ login success ~~~~~~~~~~~~~~ \n");
  734. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  735. if (FAIL_RETURN == (rc = IOT_Thing_RRPC_Register(g_thing_t,
  736. SUB_1_PRODUCT_KEY,
  737. SUB_1_DEVICE_NAME,
  738. rrpc_request_handler))) {
  739. printf("rrpc register error\n");
  740. goto exit;
  741. }
  742. // 动态注册子设备请查看demo_gateway_function中的示例
  743. printf(" ~~~~~~~~~~~~~~~~~~ sub2 ~~~~~~~~~~~~~~~~~~~ \n");
  744. /* client id */
  745. memset(client_id, 0x0, 32);
  746. HAL_Snprintf(client_id, 32, "%s.%s", SUB_2_PRODUCT_KEY, SUB_2_DEVICE_NAME);
  747. /* sign */
  748. memset(sign, 0x0, 41);
  749. if (FAIL_RETURN == _calc_sign(SUB_2_PRODUCT_KEY,
  750. SUB_2_DEVICE_NAME,
  751. SUB_2_DEVICE_SECRET,
  752. sign,
  753. 41,
  754. "hmacmd5",
  755. client_id,
  756. timestamp)) {
  757. printf("sign fail \n");
  758. return FAIL_RETURN;
  759. }
  760. rc = IOT_Thing_Register(g_thing_t,
  761. IOTX_Thing_REGISTER_TYPE_STATIC,
  762. SUB_2_PRODUCT_KEY,
  763. SUB_2_DEVICE_NAME,
  764. timestamp,
  765. client_id,
  766. sign,
  767. IOTX_Thing_SIGN_METHOD_TYPE_MD5);
  768. if (SUCCESS_RETURN != rc) {
  769. printf("dynamic rc = IOT_Thing_Register() = %d\n", rc);
  770. IOT_Thing_Destroy((void**)&g_thing_t);
  771. return rc;
  772. }
  773. //printf("dynamic register, device_secret = %s\n", sub2_devcie_secret);
  774. printf(" ~~~~~~~~~~~~ register success ~~~~~~~~~~~~~ \n");
  775. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  776. printf(" ~~~~~~~~~~~~~~~ start login ~~~~~~~~~~~~~~~ \n");
  777. rc = IOT_Thing_Login(g_thing_t,
  778. SUB_2_PRODUCT_KEY,
  779. SUB_2_DEVICE_NAME,
  780. timestamp,
  781. client_id,
  782. sign,
  783. IOTX_Thing_SIGN_METHOD_TYPE_MD5,
  784. IOTX_Thing_CLEAN_SESSION_TYPE_FALSE);
  785. if (SUCCESS_RETURN != rc) {
  786. printf("login sub2 rc = IOT_Thing_Login() = %d\n", rc);
  787. goto exit;
  788. }
  789. if (FAIL_RETURN == ( rc = IOT_Thing_RRPC_Register(g_thing_t,
  790. SUB_2_PRODUCT_KEY,
  791. SUB_2_DEVICE_NAME,
  792. rrpc_request_handler))) {
  793. printf("rrpc register error\n");
  794. goto exit;
  795. }
  796. printf(" ~~~~~~~~~~~~~~ login success ~~~~~~~~~~~~~~ \n");
  797. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  798. printf(" ~~~~~~~~ start register callback ~~~~~~~~~~ \n");
  799. printf(" ~~~~~~~~~~~~ service register ~~~~~~~~~~~~~ \n");
  800. rc = IOT_Thing_Service_Register(g_thing_t, service_request_handler);
  801. if (SUCCESS_RETURN != rc) {
  802. printf("service register rc = IOT_Thing_Service_Register() = %d\n", rc);
  803. goto exit;
  804. }
  805. printf(" ~~~~~~~~~~~~ down raw register ~~~~~~~~~~~~ \n");
  806. rc = IOT_Thing_Down_Raw_Register(g_thing_t, down_raw_handler);
  807. if (SUCCESS_RETURN != rc) {
  808. printf("raw register rc = IOT_Thing_Down_Raw_Register() = %d\n", rc);
  809. goto exit;
  810. }
  811. printf(" ~~~~~~~~~~ thing control register ~~~~~~~~~~ \n");
  812. rc = IOT_Thing_Control_Register(g_thing_t, thing_control_handler);
  813. if (SUCCESS_RETURN != rc) {
  814. printf("raw register rc = IOT_Thing_Down_Raw_Register() = %d\n", rc);
  815. goto exit;
  816. }
  817. printf(" ~~~~~~~~ register callback success ~~~~~~~~ \n");
  818. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  819. length = 1024 * 4; //具体取值请根据自行创建的产品模型进行调整,可以参考运行时日志会提示dsl具体大小,取值需比dsl大小大即可
  820. printf(" ~~~~~~~~~~~~ start dsl template ~~~~~~~~~~~ \n");
  821. printf(" ~~~~~~~~~~~~~~~~~~~~ get ~~~~~~~~~~~~~~~~~~ \n");
  822. rc = IOT_Thing_Get_Dsl_Template(g_thing_t,
  823. SUB_1_PRODUCT_KEY,
  824. SUB_1_DEVICE_NAME,
  825. sub_dsltemplate_get,
  826. &length);
  827. printf("sub_dsltemplate_get: length[%d]", length);
  828. while(printf_num < length) {
  829. memset(dsltemplate_printf, 0x0, 512);
  830. if (length-printf_num > 500) {
  831. strncpy(dsltemplate_printf, &sub_dsltemplate_get[printf_num], 500);
  832. printf_num += 500;
  833. } else if (length - printf_num <= 500){
  834. strncpy(dsltemplate_printf, &sub_dsltemplate_get[printf_num], length-printf_num);
  835. printf_num = length;
  836. }
  837. printf("%s", dsltemplate_printf);
  838. }
  839. printf("\n");
  840. printf(" ~~~~~~~~~~~ dsl template success ~~~~~~~~~~ \n");
  841. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  842. topo_length = 512;
  843. if (FAIL_RETURN == ( rc = IOT_Thing_Get_TOPO(g_thing_t, get_topo_reply, &topo_length))) {
  844. printf("IOT_Thing_Get_TOPO error\n");
  845. goto exit;
  846. }
  847. printf(" get_topo_reply length %d \n", topo_length);
  848. printf(" get_topo_reply %s \n", get_topo_reply);
  849. //具体参数请自行参考云端控制台提示
  850. printf(" ~~~~~~~~~~~ start thing property ~~~~~~~~~~ \n");
  851. printf(" ~~~~~~~~~~~~~~~~~~~ post ~~~~~~~~~~~~~~~~~~ \n");
  852. rc = IOT_Thing_Post_Property(g_thing_t,
  853. SUB_1_PRODUCT_KEY,//SUB_2_PRODUCT_KEY,
  854. SUB_1_DEVICE_NAME,//SUB_2_DEVICE_NAME,
  855. "{\"afabwa\":\"sfs\"}");
  856. printf(" ~~~~~~~~~~ thing property success ~~~~~~~~~ \n");
  857. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  858. //具体参数请自行参考云端控制台提示
  859. printf(" ~~~~~~~~~~~ start trigger event ~~~~~~~~~~~ \n");
  860. rc = IOT_Thing_Trigger_Event(g_thing_t,
  861. SUB_1_PRODUCT_KEY,
  862. SUB_1_DEVICE_NAME,
  863. "{\"propertyIdentifier20\":\"0.90\"}",
  864. "propertyIdentifier20");
  865. printf(" ~~~~~~~~~~~ trigger event success ~~~~~~~~~ \n");
  866. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  867. //具体参数请自行参考云端控制台提示
  868. printf(" ~~~~~~~~~~~~~ start deviceinfo ~~~~~~~~~~~~ \n");
  869. printf(" ~~~~~~~~~~~~~~~~~~ update ~~~~~~~~~~~~~~~~~ \n");
  870. rc = IOT_Thing_Update_Deviceinfo(g_thing_t,
  871. SUB_1_PRODUCT_KEY,
  872. SUB_1_DEVICE_NAME,
  873. "[{\"attrKey\":\"Temperature\",\"attrValue\":\"36.8\"}]");
  874. //具体参数请自行参考云端控制台提示
  875. printf(" ~~~~~~~~~~~~~~~~~~ delete ~~~~~~~~~~~~~~~~~ \n");
  876. rc = IOT_Thing_Delete_Deviceinfo(g_thing_t,
  877. SUB_1_PRODUCT_KEY,
  878. SUB_1_DEVICE_NAME,
  879. "[{\"attrKey\":\"Temperature\",\"attrValue\":\"36.8\"}]");
  880. printf(" ~~~~~~~~~~~~ deviceinfo success ~~~~~~~~~~~ \n");
  881. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  882. //具体参数请自行参考云端控制台提示
  883. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  884. printf(" ~~~~~~~~~~ start publish rawdata ~~~~~~~~~~ \n");
  885. rc = IOT_Thing_Publish_Rawdata(g_thing_t,
  886. SUB_1_PRODUCT_KEY,
  887. SUB_1_DEVICE_NAME,
  888. "aa00090000ff00003039001b",
  889. strlen("aa00090000ff00003039001b"));
  890. printf(" ~~~~~~~~~ publish rawdata success ~~~~~~~~~ \n");
  891. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  892. printf(" ~~~~~~~~~~~~~~~ start yield ~~~~~~~~~~~~~~~ \n");
  893. while(cnt < 30) {
  894. IOT_Thing_Yield(g_thing_t, 200);
  895. cnt++;
  896. }
  897. exit:
  898. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  899. IOT_Thing_Logout(g_thing_t, SUB_1_PRODUCT_KEY, SUB_1_DEVICE_NAME);
  900. IOT_Thing_Logout(g_thing_t, SUB_2_PRODUCT_KEY, SUB_2_DEVICE_NAME);
  901. printf(" ~~~~~~~~~~~~~ logout success ~~~~~~~~~~~~~~ \n");
  902. printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n");
  903. HAL_SleepMs(200);
  904. IOT_Thing_Destroy((void**)&g_thing_t);
  905. printf(" ~~~~~~~~~~~~ destory success ~~~~~~~~~~~~~~ \n");
  906. return SUCCESS_RETURN;
  907. }
  908. int main()
  909. {
  910. char msg_buf[MSG_LEN_MAX] ={0};
  911. char msg_readbuf[MSG_LEN_MAX] ={0};
  912. IOT_OpenLog("masterslave");
  913. IOT_SetLogLevel(IOT_LOG_DEBUG);
  914. /**< set device info*/
  915. HAL_SetProductKey(PRODUCT_KEY);
  916. HAL_SetDeviceName(DEVICE_NAME);
  917. HAL_SetDeviceSecret(DEVICE_SECRET);
  918. /**< end*/
  919. // 示例如何使用subdev的API直接进行开发
  920. //demo_gateway_function(msg_buf, msg_readbuf);
  921. // 示例使用subdev_example_api.h中对topic进行封装的API进行网关开发
  922. demo_thing_function(msg_buf, msg_readbuf);
  923. // 示例使用subdev_example_api.h中对topic进行封装的API进行单品设备开发
  924. //demo_only_one_device(msg_buf, msg_readbuf);
  925. printf("out of demo!\n");
  926. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  927. IOT_CloseLog();
  928. return 0;
  929. }