1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 */
4
5 #include "dev_reset_internal.h"
6
7 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
8 extern "C" {
9 #endif
10
11 static int g_dev_reset_sub_flag = 0;
12 iotx_devrst_evt_handle_t g_devrst_event_handle = NULL;
13
14 #include "linkkit/mqtt_api.h"
mqtt_sub_handle(void * pcontext,void * pclient,iotx_mqtt_event_msg_pt msg)15 static void mqtt_sub_handle(void *pcontext, void *pclient,
16 iotx_mqtt_event_msg_pt msg)
17 {
18 switch (msg->event_type) {
19 case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
20 {
21 iotx_mqtt_topic_info_t *packet_info =
22 (iotx_mqtt_topic_info_t *)msg->msg;
23 if (g_devrst_event_handle) {
24 iotx_devrst_evt_recv_msg_t recv_msg;
25 memset(&recv_msg, 0, sizeof(iotx_devrst_evt_recv_msg_t));
26 recv_msg.msgid = packet_info->packet_id;
27 recv_msg.payload = (char *)packet_info->payload;
28 recv_msg.payload_len = packet_info->payload_len;
29 g_devrst_event_handle(IOTX_DEVRST_EVT_RECEIVED,
30 (void *)&recv_msg);
31 }
32 }
33 break;
34
35 default:
36 break;
37 }
38 }
39
IOT_DevReset_Report(iotx_dev_meta_info_t * meta_info,iotx_devrst_evt_handle_t handle,void * extended)40 int IOT_DevReset_Report(iotx_dev_meta_info_t *meta_info,
41 iotx_devrst_evt_handle_t handle, void *extended)
42 {
43 int res = 0;
44 const char *reset_fmt = "/sys/%s/%s/thing/reset";
45 const char *reset_reply_fmt = "/sys/%s/%s/thing/reset_reply";
46 const char *payload_fmt =
47 "{\"id\":%d, \"version\":\"1.0\", \"method\":\"thing.reset\", "
48 "\"params\":{}}";
49 char topic[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30] = { 0 };
50 char payload[128] = { 0 };
51
52 if (meta_info == NULL || handle == NULL) {
53 return FAIL_RETURN;
54 }
55 g_devrst_event_handle = handle;
56
57 memset(topic, 0, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30);
58 HAL_Snprintf(topic, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30,
59 reset_reply_fmt, meta_info->product_key,
60 meta_info->device_name);
61
62 if (g_dev_reset_sub_flag == 0) {
63 #ifdef MQTT_AUTO_SUBSCRIBE
64 res = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS3_SUB_LOCAL,
65 mqtt_sub_handle, NULL, 5000);
66 #else
67 res = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS0,
68 mqtt_sub_handle, NULL, 5000);
69 #endif
70 if (res < 0) {
71 return FAIL_RETURN;
72 }
73 g_dev_reset_sub_flag = 1;
74 }
75
76 memset(topic, 0, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30);
77 HAL_Snprintf(topic, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30,
78 reset_fmt, meta_info->product_key, meta_info->device_name);
79
80 memset(payload, 0, 128);
81 HAL_Snprintf(payload, 128, payload_fmt, iotx_report_id());
82
83 res = IOT_MQTT_Publish_Simple(NULL, topic, IOTX_MQTT_QOS0, payload,
84 strlen(payload));
85
86 return res;
87 }
88
89 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
90 }
91 #endif
92