1 #include "iotx_dm_internal.h"
2
3 #ifdef ALCS_ENABLED
4 #include "CoAPServer.h"
5
6 #define ALCS_NOTIFY_PORT (5683)
7 #define ALCS_NOTIFY_HOST "255.255.255.255"
8 #define ALCS_NOTIFY_METHOD "core.service.dev.notify"
9
10 const char DM_URI_DEV_CORE_SERVICE_DEV_NOTIFY[] DM_READ_ONLY =
11 "/dev/core/service/dev/notify";
12
13 extern const char DM_MSG_REQUEST[];
14
15 static dm_server_ctx_t g_dm_server_ctx = { 0 };
16
dm_server_get_ctx(void)17 static dm_server_ctx_t *dm_server_get_ctx(void)
18 {
19 return &g_dm_server_ctx;
20 }
21
_dm_server_dev_notify(void * handle)22 static int _dm_server_dev_notify(void *handle)
23 {
24 int ret, i;
25 char *data = NULL;
26 char *payload = NULL;
27 int data_len = 0;
28 int payload_len = 0;
29 NetworkAddr notify_sa;
30 CoAPContext *g_coap_ctx = CoAPServer_init();
31
32 dm_msg_dev_core_service_dev(&data, &data_len);
33
34 payload_len = strlen(DM_MSG_REQUEST) + 10 + strlen(DM_MSG_VERSION) +
35 data_len + strlen(ALCS_NOTIFY_METHOD) + 1;
36
37 payload = DM_malloc(payload_len);
38 if (payload == NULL) {
39 DM_free(data);
40 return DM_MEMORY_NOT_ENOUGH;
41 }
42 memset(payload, 0, payload_len);
43 HAL_Snprintf(payload, payload_len, DM_MSG_REQUEST, iotx_report_id(),
44 DM_MSG_VERSION, data_len, data, ALCS_NOTIFY_METHOD);
45
46 memset(¬ify_sa, 0, sizeof(notify_sa));
47 memcpy(notify_sa.addr, ALCS_NOTIFY_HOST, strlen(ALCS_NOTIFY_HOST));
48 notify_sa.port = ALCS_NOTIFY_PORT;
49
50 dm_log_debug("notify path:%s; payload = %s",
51 DM_URI_DEV_CORE_SERVICE_DEV_NOTIFY, payload);
52
53 for (i = 0; i < 2; i++) {
54 ret = CoAPServerMultiCast_send(
55 g_coap_ctx, ¬ify_sa, DM_URI_DEV_CORE_SERVICE_DEV_NOTIFY,
56 (uint8_t *)payload, (uint16_t)payload_len, NULL, NULL);
57 }
58 DM_free(payload);
59 DM_free(data);
60 return ret;
61 }
62
dm_server_open(void)63 int dm_server_open(void)
64 {
65 dm_server_ctx_t *ctx = dm_server_get_ctx();
66 iotx_alcs_param_t alcs_param;
67 iotx_alcs_event_handle_t event_handle;
68
69 memset(&alcs_param, 0x0, sizeof(iotx_alcs_param_t));
70 memset(&event_handle, 0x0, sizeof(iotx_alcs_event_handle_t));
71
72 alcs_param.group = (char *)DM_SERVER_ALCS_ADDR;
73 alcs_param.port = DM_SERVER_ALCS_PORT;
74 alcs_param.send_maxcount = DM_SERVER_ALCS_SEND_MAXCOUNT;
75 alcs_param.waittime = DM_SERVER_ALCS_WAITTIME;
76 alcs_param.obs_maxcount = DM_SERVER_ALCS_OBS_MAXCOUNT;
77 alcs_param.res_maxcount = DM_SERVER_ALCS_RES_MAXCOUNT;
78 alcs_param.role = IOTX_ALCS_ROLE_CLIENT | IOTX_ALCS_ROLE_SERVER;
79 event_handle.h_fp = dm_server_alcs_event_handler;
80 event_handle.pcontext = NULL;
81
82 alcs_param.handle_event = &event_handle;
83
84 ctx->conn_handle = iotx_alcs_construct(&alcs_param);
85 if (ctx->conn_handle == NULL) {
86 return FAIL_RETURN;
87 }
88
89 _dm_server_dev_notify(ctx->conn_handle);
90
91 return SUCCESS_RETURN;
92 }
93
dm_server_connect(void)94 int dm_server_connect(void)
95 {
96 dm_server_ctx_t *ctx = dm_server_get_ctx();
97 return iotx_alcs_cloud_init(ctx->conn_handle);
98 }
99
dm_server_close(void)100 int dm_server_close(void)
101 {
102 dm_server_ctx_t *ctx = dm_server_get_ctx();
103
104 return iotx_alcs_destroy(&ctx->conn_handle);
105 }
106
dm_server_send(char * uri,unsigned char * payload,int payload_len,void * context)107 int dm_server_send(char *uri, unsigned char *payload, int payload_len,
108 void *context)
109 {
110 int res = 0;
111 dm_server_ctx_t *ctx = dm_server_get_ctx();
112 iotx_alcs_msg_t alcs_msg;
113 dm_server_alcs_context_t *alcs_context =
114 (dm_server_alcs_context_t *)context;
115
116 memset(&alcs_msg, 0, sizeof(iotx_alcs_msg_t));
117
118 alcs_msg.group_id = 0;
119 alcs_msg.ip = alcs_context ? alcs_context->ip : NULL;
120 alcs_msg.port = alcs_context ? alcs_context->port : 0;
121 alcs_msg.msg_code =
122 (alcs_context && alcs_context->token_len && alcs_context->token)
123 ? ITOX_ALCS_COAP_MSG_CODE_205_CONTENT
124 : ITOX_ALCS_COAP_MSG_CODE_GET;
125 alcs_msg.msg_type = IOTX_ALCS_MESSAGE_TYPE_CON;
126 alcs_msg.uri = uri;
127 alcs_msg.payload = payload;
128 alcs_msg.payload_len = payload_len;
129
130 if (alcs_context == NULL) {
131 res = iotx_alcs_observe_notify(ctx->conn_handle, alcs_msg.uri,
132 alcs_msg.payload_len, alcs_msg.payload);
133 dm_log_info("Send Observe Notify Result %d", res);
134 } else if (alcs_context->ip && alcs_context->port &&
135 NULL == alcs_context->token) {
136 res = iotx_alcs_send(ctx->conn_handle, &alcs_msg);
137 dm_log_info("Send Result %d", res);
138 } else if (alcs_context->ip && alcs_context->port &&
139 alcs_context->token_len && alcs_context->token) {
140 res = iotx_alcs_send_Response(ctx->conn_handle, &alcs_msg,
141 (uint8_t)alcs_context->token_len,
142 (uint8_t *)alcs_context->token);
143 dm_log_info("Send Response Result %d", res);
144 }
145
146 return res;
147 }
148
dm_server_subscribe(char * uri,CoAPRecvMsgHandler callback,int auth_type)149 int dm_server_subscribe(char *uri, CoAPRecvMsgHandler callback, int auth_type)
150 {
151 int res = 0;
152 dm_server_ctx_t *ctx = dm_server_get_ctx();
153 iotx_alcs_res_t alcs_res;
154
155 memset(&alcs_res, 0, sizeof(iotx_alcs_res_t));
156
157 alcs_res.uri = uri;
158 alcs_res.msg_ct = IOTX_ALCS_MESSAGE_CT_APP_JSON;
159 alcs_res.msg_perm = IOTX_ALCS_MESSAGE_PERM_GET;
160 alcs_res.maxage = 60;
161 alcs_res.need_auth = auth_type;
162 alcs_res.callback = callback;
163
164 res = iotx_alcs_register_resource(ctx->conn_handle, &alcs_res);
165
166 dm_log_info("Register Resource Result: %d", res);
167
168 return res;
169 }
170
dm_server_add_device(char product_key[IOTX_PRODUCT_KEY_LEN+1],char device_name[IOTX_DEVICE_NAME_LEN+1])171 int dm_server_add_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1],
172 char device_name[IOTX_DEVICE_NAME_LEN + 1])
173 {
174 int res = 0;
175 dm_server_ctx_t *ctx = dm_server_get_ctx();
176
177 res = iotx_alcs_add_sub_device(ctx->conn_handle, (const char *)product_key,
178 (const char *)device_name);
179 dm_log_info("Add Device Result: %d, Product Key: %s, Device Name: %s", res,
180 product_key, device_name);
181
182 return res;
183 }
184
dm_server_del_device(char product_key[IOTX_PRODUCT_KEY_LEN+1],char device_name[IOTX_DEVICE_NAME_LEN+1])185 int dm_server_del_device(char product_key[IOTX_PRODUCT_KEY_LEN + 1],
186 char device_name[IOTX_DEVICE_NAME_LEN + 1])
187 {
188 int res = 0;
189 dm_server_ctx_t *ctx = dm_server_get_ctx();
190
191 res = iotx_alcs_remove_sub_device(
192 ctx->conn_handle, (const char *)product_key, (const char *)device_name);
193 dm_log_info("Del Device Result: %d, Product Key: %s, Device Name: %s", res,
194 product_key, device_name);
195
196 return res;
197 }
198
dm_server_yield(void)199 int dm_server_yield(void)
200 {
201 dm_server_ctx_t *ctx = dm_server_get_ctx();
202
203 return iotx_alcs_yield(ctx->conn_handle);
204 }
205 #endif
206