1 #include "iotx_dm_internal.h"
2 
3 #ifdef ALCS_ENABLED
4 
5 #ifdef LOG_REPORT_TO_CLOUD
6 #include "iotx_log_report.h"
7 #endif
_dm_server_malloc_context(_IN_ NetworkAddr * remote,_IN_ CoAPMessage * message,_OU_ dm_server_alcs_context_t ** context)8 static int _dm_server_malloc_context(_IN_ NetworkAddr *remote,
9                                      _IN_ CoAPMessage *message,
10                                      _OU_ dm_server_alcs_context_t **context)
11 {
12     dm_server_alcs_context_t *alcs_context = NULL;
13 
14     alcs_context = DM_malloc(sizeof(dm_server_alcs_context_t));
15     if (alcs_context == NULL) {
16         return FAIL_RETURN;
17     }
18     memset(alcs_context, 0, sizeof(dm_server_alcs_context_t));
19 
20     alcs_context->ip = DM_malloc(strlen((char *)remote->addr) + 1);
21     if (alcs_context->ip == NULL) {
22         DM_free(alcs_context);
23         return FAIL_RETURN;
24     }
25     memset(alcs_context->ip, 0, strlen((char *)remote->addr) + 1);
26     memcpy(alcs_context->ip, (char *)remote->addr,
27            strlen((char *)remote->addr) + 1);
28 
29     alcs_context->port = remote->port;
30     dm_log_info("alcs_context->ip: %s", alcs_context->ip);
31     dm_log_info("alcs_context->port: %d", alcs_context->port);
32 
33     alcs_context->token = DM_malloc(message->header.tokenlen);
34     if (alcs_context->token == NULL) {
35         DM_free(alcs_context->ip);
36         DM_free(alcs_context);
37         return FAIL_RETURN;
38     }
39     memset(alcs_context->token, 0, message->header.tokenlen);
40     memcpy(alcs_context->token, message->token, message->header.tokenlen);
41 
42     alcs_context->token_len = message->header.tokenlen;
43 
44     *context = alcs_context;
45 
46     return SUCCESS_RETURN;
47 }
48 
dm_server_free_context(_IN_ void * ctx)49 void dm_server_free_context(_IN_ void *ctx)
50 {
51     dm_server_alcs_context_t *context = (dm_server_alcs_context_t *)ctx;
52     DM_free(context->ip);
53     DM_free(context->token);
54     DM_free(context);
55 }
56 
57 static dm_server_uri_map_t g_dm_server_uri_map[] = {
58 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
59     { DM_URI_THING_SERVICE_PROPERTY_SET, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH,
60       dm_server_thing_service_property_set },
61     { DM_URI_THING_SERVICE_PROPERTY_GET, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH,
62       dm_server_thing_service_property_get },
63     { DM_URI_THING_EVENT_PROPERTY_POST, DM_URI_SYS_PREFIX, IOTX_DM_LOCAL_AUTH,
64       dm_server_thing_service_property_post },
65     { DM_URI_THING_SERVICE_REQUEST_WILDCARD2, DM_URI_SYS_PREFIX,
66       IOTX_DM_LOCAL_AUTH, dm_server_thing_service_request },
67 #endif
68     { DM_URI_DEV_CORE_SERVICE_DEV, NULL, IOTX_DM_LOCAL_NO_AUTH,
69       dm_server_thing_dev_core_service_dev },
70 };
71 
dm_server_subscribe_all(char product_key[IOTX_PRODUCT_KEY_LEN+1],char device_name[IOTX_DEVICE_NAME_LEN+1])72 int dm_server_subscribe_all(char product_key[IOTX_PRODUCT_KEY_LEN + 1],
73                             char device_name[IOTX_DEVICE_NAME_LEN + 1])
74 {
75     int res = 0, index = 0, auth = 0;
76     int number = sizeof(g_dm_server_uri_map) / sizeof(dm_server_uri_map_t);
77     char *uri = NULL;
78 
79     for (index = 0; index < number; index++) {
80         res =
81             dm_utils_service_name((char *)g_dm_server_uri_map[index].uri_prefix,
82                                   (char *)g_dm_server_uri_map[index].uri_name,
83                                   product_key, device_name, &uri);
84         if (res < SUCCESS_RETURN) {
85             index--;
86             continue;
87         }
88 
89         auth =
90             (g_dm_server_uri_map[index].auth_type & IOTX_DM_SERVICE_LOCAL_AUTH)
91                 ? (IOTX_DM_MESSAGE_AUTH)
92                 : (IOTX_DM_MESSAGE_NO_AUTH);
93         res = dm_server_subscribe(
94             uri, (void *)g_dm_server_uri_map[index].callback, auth);
95         if (res < SUCCESS_RETURN) {
96             index--;
97             DM_free(uri);
98             continue;
99         }
100         DM_free(uri);
101     }
102 
103     return SUCCESS_RETURN;
104 }
105 
dm_server_alcs_event_handler(void * pcontext,void * phandle,iotx_alcs_event_msg_t * msg)106 void dm_server_alcs_event_handler(void *pcontext, void *phandle,
107                                   iotx_alcs_event_msg_t *msg)
108 {
109 }
110 
111 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
dm_server_thing_service_property_set(CoAPContext * context,const char * paths,NetworkAddr * remote,CoAPMessage * message)112 void dm_server_thing_service_property_set(CoAPContext *context,
113                                           const char *paths,
114                                           NetworkAddr *remote,
115                                           CoAPMessage *message)
116 {
117     int res = 0;
118     dm_msg_source_t source;
119     dm_msg_dest_t dest;
120     dm_msg_request_payload_t request;
121     dm_msg_response_t response;
122     dm_server_alcs_context_t *alcs_context = NULL;
123 
124     res = _dm_server_malloc_context(remote, message, &alcs_context);
125     if (res != SUCCESS_RETURN) {
126         return;
127     }
128 
129     memset(&source, 0, sizeof(dm_msg_source_t));
130     memset(&dest, 0, sizeof(dm_msg_dest_t));
131     memset(&request, 0, sizeof(dm_msg_request_payload_t));
132     memset(&response, 0, sizeof(dm_msg_response_t));
133 
134     source.uri = paths;
135     source.payload = (unsigned char *)message->payload;
136     source.payload_len = message->payloadlen;
137     source.context = alcs_context;
138 
139     dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_SET;
140 
141     res = dm_msg_proc_thing_service_property_set(&source, &dest, &request,
142                                                  &response);
143     if (res < SUCCESS_RETURN) {
144         dm_server_free_context(alcs_context);
145         return;
146     }
147 
148 #ifdef LOG_REPORT_TO_CLOUD
149     {
150         extern void send_permance_info(char *input, int input_len,
151                                        char *comments, int report_format);
152         if (SUCCESS_RETURN ==
153             check_target_msg(request.id.value, request.id.value_length)) {
154             send_permance_info(request.id.value, request.id.value_length, "2",
155                                1);
156         }
157     }
158 #endif
159 
160     dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, "{}", strlen("{}"),
161                     (void *)alcs_context);
162     dm_server_free_context(alcs_context);
163 }
164 
dm_server_thing_service_request(CoAPContext * context,const char * paths,NetworkAddr * remote,CoAPMessage * message)165 void dm_server_thing_service_request(CoAPContext *context, const char *paths,
166                                      NetworkAddr *remote, CoAPMessage *message)
167 {
168     int res = 0;
169     dm_msg_source_t source;
170 
171     dm_server_alcs_context_t *alcs_context = NULL;
172 
173     res = _dm_server_malloc_context(remote, message, &alcs_context);
174     if (res != SUCCESS_RETURN) {
175         return;
176     }
177 
178     memset(&source, 0, sizeof(dm_msg_source_t));
179 
180     source.uri = paths;
181     source.payload = (unsigned char *)message->payload;
182     source.payload_len = message->payloadlen;
183     source.context = alcs_context;
184 
185     if (dm_msg_proc_thing_service_request(&source) < 0) {
186         dm_server_free_context(alcs_context);
187     }
188 }
189 
dm_server_thing_service_property_get(CoAPContext * context,const char * paths,NetworkAddr * remote,CoAPMessage * message)190 void dm_server_thing_service_property_get(CoAPContext *context,
191                                           const char *paths,
192                                           NetworkAddr *remote,
193                                           CoAPMessage *message)
194 {
195     int res = 0;
196     dm_server_alcs_context_t *alcs_context = NULL;
197     dm_msg_source_t source;
198     dm_msg_dest_t dest;
199     dm_msg_request_payload_t request;
200     dm_msg_response_t response;
201     unsigned char *data = NULL;
202     int data_len = 0;
203 
204     res = _dm_server_malloc_context(remote, message, &alcs_context);
205     if (res != SUCCESS_RETURN) {
206         return;
207     }
208 
209     memset(&source, 0, sizeof(dm_msg_source_t));
210     memset(&dest, 0, sizeof(dm_msg_dest_t));
211     memset(&request, 0, sizeof(dm_msg_request_payload_t));
212     memset(&response, 0, sizeof(dm_msg_response_t));
213 
214     source.uri = paths;
215     source.payload = (unsigned char *)message->payload;
216     source.payload_len = message->payloadlen;
217     source.context = alcs_context;
218 
219     dest.uri_name = DM_URI_THING_SERVICE_PROPERTY_GET;
220 
221     dm_msg_proc_thing_service_property_get(&source, &dest, &request, &response,
222                                            &data, &data_len);
223 
224 #ifdef DEPRECATED_LINKKIT
225     dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, (char *)data,
226                     data_len, alcs_context);
227     DM_free(data);
228     dm_server_free_context(alcs_context);
229 #endif
230 }
231 
dm_server_thing_service_property_post(CoAPContext * context,const char * paths,NetworkAddr * remote,CoAPMessage * message)232 void dm_server_thing_service_property_post(CoAPContext *context,
233                                            const char *paths,
234                                            NetworkAddr *remote,
235                                            CoAPMessage *message)
236 {
237     int res = 0;
238     dm_server_alcs_context_t *alcs_context = NULL;
239     dm_msg_source_t source;
240     dm_msg_dest_t dest;
241     dm_msg_request_payload_t request;
242     dm_msg_response_t response;
243 
244     res = _dm_server_malloc_context(remote, message, &alcs_context);
245     if (res != SUCCESS_RETURN) {
246         return;
247     }
248 
249     memset(&source, 0, sizeof(dm_msg_source_t));
250     memset(&dest, 0, sizeof(dm_msg_dest_t));
251     memset(&request, 0, sizeof(dm_msg_request_payload_t));
252     memset(&response, 0, sizeof(dm_msg_response_t));
253 
254     source.uri = paths;
255     source.payload = (unsigned char *)message->payload;
256     source.payload_len = message->payloadlen;
257     source.context = alcs_context;
258 
259     dest.uri_name = DM_URI_THING_EVENT_PROPERTY_POST;
260 
261     dm_msg_proc_thing_service_property_post(&source, &dest, &request,
262                                             &response);
263 
264     dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, "{}", strlen("{}"),
265                     alcs_context);
266     dm_server_free_context(alcs_context);
267 }
268 
269 #endif
dm_server_thing_dev_core_service_dev(CoAPContext * context,const char * paths,NetworkAddr * remote,CoAPMessage * message)270 void dm_server_thing_dev_core_service_dev(CoAPContext *context,
271                                           const char *paths,
272                                           NetworkAddr *remote,
273                                           CoAPMessage *message)
274 {
275     int res = 0;
276     dm_server_alcs_context_t *alcs_context = NULL;
277     dm_msg_source_t source;
278     dm_msg_dest_t dest;
279     dm_msg_request_payload_t request;
280     dm_msg_response_t response;
281     unsigned char *data = NULL;
282     int data_len = 0;
283 
284     res = _dm_server_malloc_context(remote, message, &alcs_context);
285     if (res != SUCCESS_RETURN) {
286         return;
287     }
288 
289     memset(&source, 0, sizeof(dm_msg_source_t));
290     memset(&dest, 0, sizeof(dm_msg_dest_t));
291     memset(&request, 0, sizeof(dm_msg_request_payload_t));
292     memset(&response, 0, sizeof(dm_msg_response_t));
293 
294     source.uri = paths;
295     source.payload = (unsigned char *)message->payload;
296     source.payload_len = message->payloadlen;
297     source.context = alcs_context;
298 
299     dest.uri_name = DM_URI_DEV_CORE_SERVICE_DEV;
300 
301     res = dm_msg_proc_thing_dev_core_service_dev(&source, &dest, &request,
302                                                  &response, &data, &data_len);
303     if (res < SUCCESS_RETURN) {
304         dm_server_free_context(alcs_context);
305         return;
306     }
307 
308     dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, (char *)data,
309                     data_len, alcs_context);
310 
311     if (response.code == IOTX_DM_ERR_CODE_SUCCESS) {
312         DM_free(data);
313     }
314     dm_server_free_context(alcs_context);
315 }
316 #endif
317