1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include "stdint.h"
6 #include "aiot_mqtt_api.h"
7 
8 /* device active info report */
9 #define APPLICATION "soundbox"                  // product of application
10 #define MODULE_NAME aos_get_platform_type()     // module type
11 
12 /* device info report format */
13 #define DEVICE_INFO_UPDATE_FMT \
14         "[" \
15             "{\"attrKey\":\"SYS_SDK_LANGUAGE\",\"attrValue\":\"C\",\"domain\":\"SYSTEM\"}" \
16             "{\"attrKey\":\"SYS_LP_SDK_VERSION\",\"attrValue\":\"aos-r-3.0.0\",\"domain\":\"SYSTEM\"}" \
17             "{\"attrKey\":\"SYS_PARTNER_ID\",\"attrValue\":\"AliOS Things Team\",\"domain\":\"SYSTEM\"}" \
18             "{\"attrKey\":\"SYS_MODULE_ID\",\"attrValue\":\"haas-amp-%s@%s\",\"domain\":\"SYSTEM\"}" \
19         "]"
20 
21 typedef enum {
22     AIOT_MQTT_CONNECT,
23     AIOT_MQTT_RECONNECT,
24     AIOT_MQTT_DISCONNECT,
25     AIOT_MQTT_MESSAGE
26 }aiot_mqtt_res_type_t;
27 
28 /**
29  * @brief dev模块内部发生值得用户关注的状态变化时, 通知用户的事件类型
30  */
31 typedef enum {
32     /**
33      * @brief 非法的应答报文
34      */
35     AIOT_DEV_JSCALLBACK_INVALID_REF,
36     /**
37      * @brief 应答报文的id字段非法
38      */
39     AIOT_DEV_JSCALLBACK_CREATE_DEV_REF,
40     /**
41      * @brief 应答报文的id字段非法
42      */
43     AIOT_DEV_JSCALLBACK_SUBSCRIBE_REF,
44     /**
45      * @brief 应答报文的id字段非法
46      */
47     AIOT_DEV_JSCALLBACK_UNSUBSCRIBE_REF,
48     /**
49      * @brief 应答报文的id字段非法
50      */
51     AIOT_DEV_JSCALLBACK_PUBLISH_REF,
52     /**
53      * @brief 应答报文的id字段非法
54      */
55     AIOT_DEV_JSCALLBACK_POST_PROPS_REF,
56     /**
57      * @brief 应答报文的id字段非法
58      */
59     AIOT_DEV_JSCALLBACK_POST_EVENT_REF,
60     /**
61      * @brief 应答报文的id字段非法
62      */
63     AIOT_DEV_JSCALLBACK_ONPROPS_REF,
64     /**
65      * @brief 应答报文的id字段非法
66      */
67     AIOT_DEV_JSCALLBACK_ONSERVICE_REF,
68     /**
69      * @brief 应答报文的id字段非法
70      */
71     AIOT_DEV_JSCALLBACK_REGISTER_DEV_REF,
72     /**
73      * @brief 应答报文的id字段非法
74      */
75     AIOT_DEV_JSCALLBACK_END_CLIENT_REF,
76     /**
77      * @brief 应答报文的code字段非法
78      */
79     AIOT_DEV_JSCALLBACK_INVALID_CODE
80 } aiot_dev_jscallback_type_t;
81 
82 /**
83  * @brief subdev模块内部发生值得用户关注的状态变化时, 通知用户的事件类型
84  */
85 typedef enum {
86     /**
87      * @brief 非法的应答报文
88      */
89     AIOT_SUBDEV_JSCALLBACK_INVALID_REF,
90     /**
91      * @brief 应答报文的id字段非法
92      */
93     AIOT_SUBDEV_JSCALLBACK_CREATE_GATEWAY_REF,
94     /**
95      * @brief 应答报文的id字段非法
96      */
97     AIOT_SUBDEV_JSCALLBACK_ADD_TOPO_REF,
98     /**
99      * @brief 应答报文的id字段非法
100      */
101     AIOT_SUBDEV_JSCALLBACK_GET_TOPO_REF,
102     /**
103      * @brief 应答报文的id字段非法
104      */
105     AIOT_SUBDEV_JSCALLBACK_REMOVE_TOPO_REF,
106     /**
107      * @brief 应答报文的id字段非法
108      */
109     AIOT_SUBDEV_JSCALLBACK_LOGIN_REF,
110     /**
111      * @brief 应答报文的id字段非法
112      */
113     AIOT_SUBDEV_JSCALLBACK_LOGOUT_REF,
114     /**
115      * @brief 应答报文的id字段非法
116      */
117     AIOT_SUBDEV_JSCALLBACK_ON_MQTT_MESSAGE_REF,
118     /**
119      * @brief 应答报文的code字段非法
120      */
121     AIOT_SUBDEV_JSCALLBACK_REGISTER_SUBDEV_REF,
122     /**
123      * @brief 应答报文的id字段非法
124      */
125     AIOT_SUBDEV_JSCALLBACK_SUBSCRIBE_REF,
126 
127     /**
128      * @brief 应答报文的id字段非法
129      */
130     AIOT_SUBDEV_JSCALLBACK_UNSUBSCRIBE_REF,
131 
132     /**
133      * @brief 应答报文的id字段非法
134      */
135     AIOT_SUBDEV_JSCALLBACK_PUBLISH_REF,
136 
137     /**
138      * @brief 应答报文的code字段非法
139      */
140     AIOT_SUBDEV_JSCALLBACK_INVALID_CODE
141 } aiot_subdev_jscallback_type_t;
142 
143 typedef struct iot_device_hanlde{
144     void *mqtt_handle;
145     void *dm_handle;
146     char *region;
147     int js_cb_ref[AIOT_DEV_JSCALLBACK_INVALID_CODE];
148     uint16_t keepaliveSec;
149     int res;
150 }iot_device_handle_t;
151 
152 typedef struct iot_gateway_handle{
153     void *mqtt_handle;
154     void *dm_handle;
155     void *subdev_handle;
156     int js_cb_ref[AIOT_SUBDEV_JSCALLBACK_INVALID_CODE];
157     uint16_t keepaliveSec;
158 }iot_gateway_handle_t;
159 
160 typedef struct iot_gateway_response{
161     int js_cb_ref;
162     int msg_id;
163     int code;
164     char productkey[IOTX_PRODUCT_KEY_LEN];
165     char devicename[IOTX_DEVICE_NAME_LEN];
166     char message[128];
167 }iot_gateway_response_t;
168 
169 typedef struct {
170     aiot_mqtt_recv_type_t type;
171     int code;
172     int topic_len;
173     int payload_len;
174     char *topic;
175     char *payload;
176 } iot_mqtt_recv_t;
177 
178 typedef struct {
179     aiot_mqtt_event_type_t type;
180     int code;
181 } iot_mqtt_event_t;
182 
183 typedef struct {
184     aiot_mqtt_option_t option;
185     iot_mqtt_recv_t recv;
186     iot_mqtt_event_t event;
187 }iot_mqtt_message_t;
188 
189 typedef struct {
190     void (*callback)(iot_mqtt_message_t *message, void *userdata);
191     void *handle;
192 }iot_mqtt_userdata_t;
193 
194 /* create mqtt client */
195 int32_t aiot_mqtt_client_start(void **handle, int keepaliveSec, iot_mqtt_userdata_t *userdata);
196 
197 /* destroy mqtt client */
198 int32_t aiot_mqtt_client_stop(void **handle);
199 
200 /* app mqtt process thread */
201 void *aiot_app_mqtt_process_thread(void *args);
202 
203 /* app mqtt recv thread */
204 void *aiot_app_mqtt_recv_thread(void *args);
205 
206 /* property post */
207 int32_t aiot_app_send_property_post(void *dm_handle, char *params);
208 
209 /* event post */
210 int32_t aiot_app_send_event_post(void *dm_handle, char *event_id, char *params);
211 
212 /* device dynmic register */
213 int32_t aiot_dynreg_http(int js_cb_ref);
214 
215 /* device active info report */
216 int32_t amp_app_devinfo_report(void *mqtt_handle);
217