1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 #include "dev_bind_internal.h"
5 
6 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
7 extern "C" {
8 #endif
9 
10 static char online_init = 0;
11 
awss_cmp_mqtt_register_cb(char * topic,void * cb)12 int awss_cmp_mqtt_register_cb(char *topic, void *cb)
13 {
14     if (topic == NULL) {
15         return -1;
16     }
17 #ifdef MQTT_AUTO_SUBSCRIBE
18     return IOT_MQTT_Subscribe(NULL, topic, IOTX_MQTT_QOS3_SUB_LOCAL,
19                               (iotx_mqtt_event_handle_func_fpt)cb, NULL);
20 #else
21     return IOT_MQTT_Subscribe(NULL, topic, IOTX_MQTT_QOS0,
22                               (iotx_mqtt_event_handle_func_fpt)cb, NULL);
23 #endif
24 }
25 
awss_cmp_mqtt_unregister_cb(char * topic)26 int awss_cmp_mqtt_unregister_cb(char *topic)
27 {
28     return IOT_MQTT_Unsubscribe(NULL, topic);
29 }
30 
awss_cmp_mqtt_send(char * topic,void * data,int len,int qos)31 int awss_cmp_mqtt_send(char *topic, void *data, int len, int qos)
32 {
33     return IOT_MQTT_Publish_Simple(NULL, topic, qos, data,
34                                    len); /* IOTX_MQTT_QOS1 or IOTX_MQTT_QOS1 */
35 }
36 
37 const struct awss_cmp_couple awss_online_couple[] = {
38     { -1, TOPIC_MATCH_REPORT_REPLY, awss_report_token_reply },
39 #ifdef WIFI_PROVISION_ENABLED
40 #ifndef AWSS_DISABLE_REGISTRAR
41     { -1, TOPIC_ZC_CHECKIN, awss_enrollee_checkin },
42     { -1, TOPIC_ZC_ENROLLEE_REPLY, awss_report_enrollee_reply },
43     { -1, TOPIC_ZC_CIPHER_REPLY, awss_get_cipher_reply },
44 #endif
45     { -1, TOPIC_SWITCHAP, awss_online_switchap }
46 #endif
47 };
48 
awss_cmp_online_init()49 int awss_cmp_online_init()
50 {
51     int i;
52     char topic[TOPIC_LEN_MAX] = { 0 };
53     if (online_init) {
54         return 0;
55     }
56 
57     for (i = 0; i < sizeof(awss_online_couple) / sizeof(awss_online_couple[0]);
58          i++) {
59         int res = -1;
60         memset(topic, 0, sizeof(topic));
61         awss_build_topic(awss_online_couple[i].topic, topic, TOPIC_LEN_MAX);
62         res = awss_cmp_mqtt_register_cb(topic, awss_online_couple[i].cb);
63         awss_debug("sub %s %s\n", topic, res < 0 ? "fail" : "success");
64     }
65 
66     online_init = 1;
67 
68     return 0;
69 }
70 
awss_cmp_online_deinit()71 int awss_cmp_online_deinit()
72 {
73     uint8_t i;
74     char topic[TOPIC_LEN_MAX] = { 0 };
75 
76     if (!online_init) {
77         return 0;
78     }
79 #ifndef DEV_BIND_DISABLE_NOTIFY
80     awss_dev_bind_notify_stop();
81 #endif
82     for (i = 0; i < sizeof(awss_online_couple) / sizeof(awss_online_couple[0]);
83          i++) {
84         memset(topic, 0, sizeof(topic));
85         awss_build_topic(awss_online_couple[i].topic, topic, TOPIC_LEN_MAX);
86         awss_cmp_mqtt_unregister_cb(topic);
87     }
88 
89     online_init = 0;
90 
91     return 0;
92 }
93 
awss_cmp_mqtt_get_payload(void * mesg,char ** payload,uint32_t * playload_len)94 int awss_cmp_mqtt_get_payload(void *mesg, char **payload,
95                               uint32_t *playload_len)
96 {
97     iotx_mqtt_event_msg_pt msg;
98     iotx_mqtt_topic_info_pt ptopic_info;
99     if (mesg == NULL || payload == NULL || playload_len == NULL) {
100         return -1;
101     }
102 
103     msg = (iotx_mqtt_event_msg_pt)mesg;
104     ptopic_info = (iotx_mqtt_topic_info_pt)msg->msg;
105 
106     switch (msg->event_type) {
107     case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
108         *playload_len = ptopic_info->payload_len;
109         *payload = (char *)ptopic_info->payload;
110         break;
111     default:
112         return -1;
113     }
114     return 0;
115 }
116 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
117 }
118 #endif
119