1 #include "linkkit/infra/infra_config.h"
2 
3 #ifdef INFRA_COMPAT
4 #include <string.h>
5 
6 #include "linkkit/infra/infra_types.h"
7 #include "linkkit/infra/infra_defs.h"
8 #include "linkkit/infra/infra_compat.h"
9 #include "linkkit/wrappers/wrappers.h"
10 
11 #if !defined(INFRA_LOG)
IOT_SetLogLevel(IOT_LogLevel level)12 void IOT_SetLogLevel(IOT_LogLevel level)
13 {
14 }
15 #endif
16 
17 #if defined(WIFI_PROVISION_ENABLED)
18 extern void awss_set_press_timeout_ms(unsigned int timeout_ms);
19 extern void awss_set_channel_scan_interval_ms(uint32_t timeout_ms);
20 #endif
21 
22 #ifdef MQTT_COMM_ENABLED
23 #include "linkkit/dev_sign_api.h"
24 #include "linkkit/mqtt_api.h"
25 
26 #ifdef INFRA_LOG
27 #include "linkkit/infra/infra_log.h"
28 #define sdk_err(...)  log_err("infra_compat", __VA_ARGS__)
29 #define sdk_info(...) log_info("infra_compat", __VA_ARGS__)
30 #else
31 #define sdk_err(...)
32 #define sdk_info(...)
33 #endif
34 
35 sdk_impl_ctx_t g_sdk_impl_ctx = { 0 };
36 /* global variable for mqtt construction */
37 static iotx_conn_info_t g_iotx_conn_info = { 0 };
38 static char g_empty_string[1] = "";
39 
IOT_SetupConnInfo(const char * product_key,const char * device_name,const char * device_secret,void ** info_ptr)40 int IOT_SetupConnInfo(const char *product_key, const char *device_name,
41                       const char *device_secret, void **info_ptr)
42 {
43     if (product_key == NULL || device_name == NULL || device_secret == NULL ||
44         strlen(product_key) > IOTX_PRODUCT_KEY_LEN ||
45         strlen(device_name) > IOTX_DEVICE_NAME_LEN ||
46         strlen(device_secret) > IOTX_DEVICE_SECRET_LEN) {
47         return NULL_VALUE_ERROR;
48     }
49 
50     if (info_ptr) {
51         memset(&g_iotx_conn_info, 0, sizeof(iotx_conn_info_t));
52         g_iotx_conn_info.host_name = g_empty_string;
53         g_iotx_conn_info.client_id = g_empty_string;
54         g_iotx_conn_info.username = g_empty_string;
55         g_iotx_conn_info.password = g_empty_string;
56         g_iotx_conn_info.pub_key = g_empty_string;
57 
58         *info_ptr = &g_iotx_conn_info;
59     }
60     return SUCCESS_RETURN;
61 }
62 #endif /* #ifdef MQTT_COMM_ENABLED */
63 
64 #if defined(DEVICE_MODEL_ENABLED)
65 #include "dm_opt.h"
66 #include "iotx_dm.h"
67 #endif
68 
69 #if defined(DEVICE_MODEL_GATEWAY)
70 extern int iot_linkkit_subdev_query_id(
71     char product_key[IOTX_PRODUCT_KEY_LEN + 1],
72     char device_name[IOTX_DEVICE_NAME_LEN + 1]);
73 #endif
74 
IOT_Ioctl(int option,void * data)75 int IOT_Ioctl(int option, void *data)
76 {
77     int res = SUCCESS_RETURN;
78     sdk_impl_ctx_t *ctx = NULL;
79 
80     ctx = &g_sdk_impl_ctx;
81 
82     if (option < 0 || data == NULL) {
83         return FAIL_RETURN;
84     }
85 
86     switch (option) {
87     case IOTX_IOCTL_SET_REGION:
88         {
89             ctx->domain_type = *(iotx_mqtt_region_types_t *)data;
90             /* iotx_guider_set_region(*(int *)data); */
91 
92             res = SUCCESS_RETURN;
93         }
94         break;
95     case IOTX_IOCTL_GET_REGION:
96         {
97             *(iotx_mqtt_region_types_t *)data = ctx->domain_type;
98 
99             res = SUCCESS_RETURN;
100         }
101         break;
102     case IOTX_IOCTL_SET_MQTT_DOMAIN:
103         {
104             ctx->domain_type = IOTX_CLOUD_REGION_CUSTOM;
105 
106             if (strlen(data) > IOTX_DOMAIN_MAX_LEN) {
107                 return FAIL_RETURN;
108             }
109             memset(ctx->cloud_custom_domain, 0, strlen((char *)data) + 1);
110             memcpy(ctx->cloud_custom_domain, data, strlen((char *)data));
111             g_infra_mqtt_domain[IOTX_CLOUD_REGION_CUSTOM] =
112                 (const char *)ctx->cloud_custom_domain;
113             res = SUCCESS_RETURN;
114         }
115         break;
116     case IOTX_IOCTL_SET_HTTP_DOMAIN:
117         {
118             ctx->domain_type = IOTX_HTTP_REGION_CUSTOM;
119 
120             if (strlen(data) > IOTX_DOMAIN_MAX_LEN) {
121                 return FAIL_RETURN;
122             }
123             memset(ctx->http_custom_domain, 0, strlen((char *)data) + 1);
124             memcpy(ctx->http_custom_domain, data, strlen((char *)data));
125             g_infra_http_domain[IOTX_CLOUD_REGION_CUSTOM] =
126                 (const char *)ctx->http_custom_domain;
127             res = SUCCESS_RETURN;
128         }
129         break;
130     case IOTX_IOCTL_SET_DYNAMIC_REGISTER:
131         {
132             ctx->dynamic_register = *(int *)data;
133 
134             res = SUCCESS_RETURN;
135         }
136         break;
137     case IOTX_IOCTL_GET_DYNAMIC_REGISTER:
138         {
139             *(int *)data = ctx->dynamic_register;
140 
141             res = SUCCESS_RETURN;
142         }
143         break;
144 #if defined(DEVICE_MODEL_ENABLED) && !defined(DEPRECATED_LINKKIT)
145 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
146     case IOTX_IOCTL_RECV_EVENT_REPLY:
147     case IOTX_IOCTL_RECV_PROP_REPLY:
148         {
149             res = iotx_dm_set_opt(IMPL_LINKKIT_IOCTL_SWITCH_EVENT_POST_REPLY,
150                                   data);
151         }
152         break;
153     case IOTX_IOCTL_SEND_PROP_SET_REPLY:
154         {
155             res = iotx_dm_set_opt(IMPL_LINKKIT_IOCTL_SWITCH_PROPERTY_SET_REPLY,
156                                   data);
157         }
158         break;
159 #endif
160     case IOTX_IOCTL_SET_SUBDEV_SIGN:
161         {
162             /* todo */
163         }
164         break;
165     case IOTX_IOCTL_GET_SUBDEV_LOGIN:
166         {
167             /* todo */
168         }
169         break;
170 #if defined(DEVICE_MODEL_GATEWAY)
171 #ifdef DEVICE_MODEL_SUBDEV_OTA
172     case IOTX_IOCTL_SET_OTA_DEV_ID:
173         {
174             int devid = *(int *)(data);
175             res = iotx_dm_ota_switch_device(devid);
176         }
177         break;
178 #endif
179 #endif
180 #else
181     case IOTX_IOCTL_RECV_EVENT_REPLY:
182     case IOTX_IOCTL_RECV_PROP_REPLY:
183     case IOTX_IOCTL_SEND_PROP_SET_REPLY:
184     case IOTX_IOCTL_GET_SUBDEV_LOGIN:
185         {
186             res = SUCCESS_RETURN;
187         }
188         break;
189 #endif
190 #if defined(DEVICE_MODEL_ENABLED)
191     case IOTX_IOCTL_FOTA_TIMEOUT_MS:
192         {
193             res = iotx_dm_set_opt(DM_OPT_FOTA_RETRY_TIMEOUT_MS, data);
194         }
195         break;
196 #endif
197     case IOTX_IOCTL_SET_CUSTOMIZE_INFO:
198         {
199             if (strlen(data) > IOTX_CUSTOMIZE_INFO_LEN) {
200                 return FAIL_RETURN;
201             }
202             memset(ctx->mqtt_customzie_info, 0, strlen((char *)data) + 1);
203             memcpy(ctx->mqtt_customzie_info, data, strlen((char *)data));
204             res = SUCCESS_RETURN;
205         }
206         break;
207     case IOTX_IOCTL_SET_MQTT_PORT:
208         {
209             ctx->mqtt_port_num = *(uint16_t *)data;
210             res = SUCCESS_RETURN;
211         }
212         break;
213 #if defined(DEVICE_MODEL_GATEWAY) && !defined(DEPRECATED_LINKKIT)
214     case IOTX_IOCTL_SET_PROXY_REGISTER:
215         {
216             res = iotx_dm_set_opt(DM_OPT_PROXY_PRODUCT_REGISTER, data);
217         }
218         break;
219     case IOTX_IOCTL_QUERY_DEVID:
220         {
221             iotx_dev_meta_info_t *dev_info = (iotx_dev_meta_info_t *)data;
222 
223             res = iot_linkkit_subdev_query_id(dev_info->product_key,
224                                               dev_info->device_name);
225         }
226         break;
227 #endif
228 #if defined(WIFI_PROVISION_ENABLED)
229     case IOTX_IOCTL_SET_AWSS_ENABLE_INTERVAL:
230         {
231             uint32_t timeout = *(uint32_t *)data;
232             awss_set_press_timeout_ms(timeout);
233         }
234         break;
235     case IOTX_IOCTL_SET_AWSS_CHANNEL_SCAN_INTERVAL:
236         {
237             uint32_t timeout = *(uint32_t *)data;
238             awss_set_channel_scan_interval_ms(timeout);
239         }
240         break;
241 #endif
242 #if defined(DEVICE_MODEL_ENABLED)
243     case IOTX_IOCTL_SUB_USER_TOPIC:
244         {
245             iotx_user_subscribe_context *context =
246                 (iotx_user_subscribe_context *)data;
247             iotx_dm_subscribe_user_topic((char *)context->topic,
248                                          (void *)context->callback);
249         }
250         break;
251 #endif
252     default:
253         {
254             res = FAIL_RETURN;
255         }
256         break;
257     }
258 
259     return res;
260 }
261 
IOT_DumpMemoryStats(IOT_LogLevel level)262 void IOT_DumpMemoryStats(IOT_LogLevel level)
263 {
264 #ifdef INFRA_MEM_STATS
265     int lvl = (int)level;
266 
267     if (lvl > LOG_DEBUG_LEVEL) {
268         lvl = LOG_DEBUG_LEVEL;
269         HAL_Printf("Invalid input level, using default: %d => %d", level, lvl);
270     }
271 
272     LITE_dump_malloc_free_stats(lvl);
273 #endif
274 }
275 
276 static void *g_event_monitor = NULL;
277 
iotx_event_regist_cb(void (* monitor_cb)(int event))278 int iotx_event_regist_cb(void (*monitor_cb)(int event))
279 {
280     g_event_monitor = (void *)monitor_cb;
281     return 0;
282 }
283 
iotx_event_post(int event)284 int iotx_event_post(int event)
285 {
286     if (g_event_monitor == NULL) {
287         return -1;
288     }
289     ((void (*)(int))g_event_monitor)(event);
290     return 0;
291 }
292 
293 typedef struct {
294     int eventid;
295     void *callback;
296 } impl_event_map_t;
297 
298 static impl_event_map_t g_impl_event_map[] = {
299     { ITE_AWSS_STATUS, NULL },
300     { ITE_CONNECT_SUCC, NULL },
301     { ITE_CONNECT_FAIL, NULL },
302     { ITE_DISCONNECTED, NULL },
303     { ITE_RAWDATA_ARRIVED, NULL },
304     { ITE_SERVICE_REQUEST, NULL },
305     { ITE_SERVICE_REQUEST_EXT, NULL },
306     { ITE_PROPERTY_SET, NULL },
307     { ITE_PROPERTY_GET, NULL },
308 #ifdef DEVICE_MODEL_SHADOW
309     { ITE_PROPERTY_DESIRED_GET_REPLY, NULL },
310 #endif
311     { ITE_REPORT_REPLY, NULL },
312     { ITE_TRIGGER_EVENT_REPLY, NULL },
313     { ITE_TIMESTAMP_REPLY, NULL },
314     { ITE_TOPOLIST_REPLY, NULL },
315     { ITE_PERMIT_JOIN, NULL },
316     { ITE_INITIALIZE_COMPLETED, NULL },
317     { ITE_FOTA, NULL },
318     { ITE_COTA, NULL },
319     { ITE_MQTT_CONNECT_SUCC, NULL },
320     { ITE_CLOUD_ERROR, NULL },
321 };
322 
iotx_event_callback(int evt)323 void *iotx_event_callback(int evt)
324 {
325     if (evt < 0 || evt >= sizeof(g_impl_event_map) / sizeof(impl_event_map_t)) {
326         return NULL;
327     }
328     return g_impl_event_map[evt].callback;
329 }
330 
331 DEFINE_EVENT_CALLBACK(ITE_AWSS_STATUS, int (*callback)(int))
332 DEFINE_EVENT_CALLBACK(ITE_CONNECT_SUCC, int (*callback)(void))
333 DEFINE_EVENT_CALLBACK(ITE_CONNECT_FAIL, int (*callback)(void))
334 DEFINE_EVENT_CALLBACK(ITE_DISCONNECTED, int (*callback)(void))
335 DEFINE_EVENT_CALLBACK(ITE_RAWDATA_ARRIVED,
336                       int (*callback)(const int, const unsigned char *,
337                                       const int))
338 DEFINE_EVENT_CALLBACK(ITE_SERVICE_REQUEST,
339                       int (*callback)(const int, const char *, const int,
340                                       const char *, const int, char **, int *))
341 DEFINE_EVENT_CALLBACK(ITE_SERVICE_REQUEST_EXT,
342                       int (*callback)(int, const char *, int, const char *, int,
343                                       const char *, int, void *))
344 DEFINE_EVENT_CALLBACK(ITE_PROPERTY_SET,
345                       int (*callback)(const int, const char *, const int))
346 #ifdef DEVICE_MODEL_SHADOW
347 DEFINE_EVENT_CALLBACK(ITE_PROPERTY_DESIRED_GET_REPLY,
348                       int (*callback)(const char *, const int))
349 #endif
350 DEFINE_EVENT_CALLBACK(ITE_PROPERTY_GET,
351                       int (*callback)(const int, const char *, const int,
352                                       char **, int *))
353 DEFINE_EVENT_CALLBACK(ITE_REPORT_REPLY,
354                       int (*callback)(const int, const int, const int,
355                                       const char *, const int))
356 DEFINE_EVENT_CALLBACK(ITE_TRIGGER_EVENT_REPLY,
357                       int (*callback)(const int, const int, const int,
358                                       const char *, const int, const char *,
359                                       const int))
360 DEFINE_EVENT_CALLBACK(ITE_TIMESTAMP_REPLY, int (*callback)(const char *))
361 DEFINE_EVENT_CALLBACK(ITE_TOPOLIST_REPLY,
362                       int (*callback)(const int, const int, const int,
363                                       const char *, const int))
364 DEFINE_EVENT_CALLBACK(ITE_PERMIT_JOIN, int (*callback)(const char *, int))
365 DEFINE_EVENT_CALLBACK(ITE_INITIALIZE_COMPLETED, int (*callback)(const int))
366 DEFINE_EVENT_CALLBACK(ITE_FOTA, int (*callback)(const int, const char *))
367 DEFINE_EVENT_CALLBACK(ITE_COTA, int (*callback)(const int, const char *, int,
368                                                 const char *, const char *,
369                                                 const char *, const char *))
370 DEFINE_EVENT_CALLBACK(ITE_MQTT_CONNECT_SUCC, int (*callback)(void))
371 DEFINE_EVENT_CALLBACK(ITE_CLOUD_ERROR,
372                       int (*callback)(const int, const char *, const char *))
373 
374 #endif
375