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