blob: 1e1d5c16c5ea0ad299d4966f106ffd89ebd31c97 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <libubus.h>
5#include <unistd.h>
6#include <libubox/usock.h>
7#include <pthread.h>
8#include <sys/socket.h>
9#include <rilutil.h>
10
11#include <telephony/ril.h>
12
13#include "../ciscore/cis_def.h"
14#include "../ciscore/cis_api.h"
15#include "../ciscore/cis_if_sys.h"
16#include "../ciscore/cis_api.h"
17#include "../ciscore/cis_internals.h"
18#include "cis_sample_entry.h"
19
20#define LWM2M_INNER_CONN_SOCKET "/tmp/lwm2m_socket.sock"
21#define RESULT_TO_RESPONSE(R) \
22 ((R) == MIPLRS_205_CONTENT ? CIS_COAP_205_CONTENT : \
23 ((R) == MIPLRS_204_CHANGED ? CIS_COAP_204_CHANGED : \
24 ((R) == MIPLRS_400_BAD_REQUEST ? CIS_COAP_400_BAD_REQUEST : \
25 ((R) == MIPLRS_401_UNAUTHORIZED ? CIS_COAP_401_UNAUTHORIZED : \
26 ((R) == MIPLRS_404_NOT_FOUND ? CIS_COAP_404_NOT_FOUND : \
27 ((R) == MIPLRS_405_METHOD_NOT_ALLOWED ? CIS_COAP_405_METHOD_NOT_ALLOWED : \
28 ((R) == MIPLRS_406_NOT_ACCEPTABLE ? CIS_COAP_406_NOT_ACCEPTABLE : \
29 CIS_COAP_503_SERVICE_UNAVAILABLE)))))))
30
31static void* g_context = NULL;
32
33struct ubus_context *lwm2m_ubus_ctx;
34static const struct blobmsg_policy lwm2m_create_policy[] = {
35 [0] = {.name = "total_size", .type = BLOBMSG_TYPE_INT32},
36 [1] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
37 [2] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
38 [3] = {.name = "current_size", .type = BLOBMSG_TYPE_INT32},
39 [4] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
40};
41
42static const struct blobmsg_policy lwm2m_delete_policy[] = {
43 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
44};
45
46static const struct blobmsg_policy lwm2m_addobj_policy[] = {
47 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
48 [1] = {.name = "objectId", .type = BLOBMSG_TYPE_INT32},
49 [2] = {.name = "instanceCount", .type = BLOBMSG_TYPE_INT32},
50 [3] = {.name = "instanceBitMap", .type = BLOBMSG_TYPE_STRING},
51 [4] = {.name = "attributeCount", .type = BLOBMSG_TYPE_INT32},
52 [5] = {.name = "actionCount", .type = BLOBMSG_TYPE_INT32},
53};
54
55static const struct blobmsg_policy lwm2m_delobj_policy[] = {
56 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
57 [1] = {.name = "objectId", .type = BLOBMSG_TYPE_INT32},
58};
59
60static const struct blobmsg_policy lwm2m_open_policy[] = {
61 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
62 [1] = {.name = "lifetime", .type = BLOBMSG_TYPE_INT32},
63 [2] = {.name = "timeout", .type = BLOBMSG_TYPE_INT32},
64};
65
66static const struct blobmsg_policy lwm2m_update_policy[] = {
67 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
68 [1] = {.name = "lifetime", .type = BLOBMSG_TYPE_INT32},
69 [2] = {.name = "objflag", .type = BLOBMSG_TYPE_INT32},
70};
71
72static const struct blobmsg_policy lwm2m_discoverresp_policy[] = {
73 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
74 [1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
75 [2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
76 [3] = {.name = "length", .type = BLOBMSG_TYPE_INT32},
77 [4] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
78};
79
80static const struct blobmsg_policy lwm2m_readrsp_policy[] = {
81 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
82 [1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
83 [2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
84 [3] = {.name = "objectid", .type = BLOBMSG_TYPE_INT32},
85 [4] = {.name = "instanceid", .type = BLOBMSG_TYPE_INT32},
86 [5] = {.name = "resourceid", .type = BLOBMSG_TYPE_INT32},
87 [6] = {.name = "valuetype", .type = BLOBMSG_TYPE_INT32},
88 [7] = {.name = "len", .type = BLOBMSG_TYPE_INT32},
89 [8] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
90 [9] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
91 [10] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
92};
93
94
95static const struct blobmsg_policy lwm2m_writersp_policy[] = {
96 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
97 [1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
98 [2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
99};
100
101
102static const struct blobmsg_policy lwm2m_notify_policy[] = {
103 [0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
104 [1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
105 [2] = {.name = "objectid", .type = BLOBMSG_TYPE_INT32},
106 [3] = {.name = "instanceid", .type = BLOBMSG_TYPE_INT32},
107 [4] = {.name = "resourceid", .type = BLOBMSG_TYPE_INT32},
108 [5] = {.name = "valuetype", .type = BLOBMSG_TYPE_INT32},
109 [6] = {.name = "len", .type = BLOBMSG_TYPE_INT32},
110 [7] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
111 [8] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
112 [9] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
113 [10] = {.name = "ackid", .type = BLOBMSG_TYPE_INT32},
114};
115
116static int lwm2mgetversion_method(struct ubus_context *ctx, struct ubus_object *obj,
117 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
118
119static int lwm2mcreate_default_method(struct ubus_context *ctx, struct ubus_object *obj,
120 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
121
122static int lwm2mcreate_method(struct ubus_context *ctx, struct ubus_object *obj,
123 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
124
125static int lwm2mdelete_method(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
127
128static int lwm2maddobj_method(struct ubus_context *ctx, struct ubus_object *obj,
129 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
130
131static int lwm2mdelobj_method(struct ubus_context *ctx, struct ubus_object *obj,
132 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
133
134static int lwm2mopen_method(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
136
137static int lwm2mupdate_method(struct ubus_context *ctx, struct ubus_object *obj,
138 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
139
140static int lwm2mclose_method(struct ubus_context *ctx, struct ubus_object *obj,
141 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
142
143static int lwm2mdiscoverresp_method(struct ubus_context *ctx, struct ubus_object *obj,
144 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
145
146static int lwm2mreadrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
147 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
148
149static int lwm2mwritersp_method(struct ubus_context *ctx, struct ubus_object *obj,
150 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
151
152static int lwm2mexcutersp_method(struct ubus_context *ctx, struct ubus_object *obj,
153 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
154
155static int lwm2mparameterrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
156 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
157
158static int lwm2mobserverrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
159 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
160
161static int lwm2mnotify_method(struct ubus_context *ctx, struct ubus_object *obj,
162 struct ubus_request_data *req, const char *method, struct blob_attr *msg);
163
164//static void lwm2m_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj);
165int lwm2m_invoke_reply(struct ubus_context *ctx, const char *service,
166 const char *method, struct blob_attr *msg,int timeout);
167
168void init_lwm2m_context();
169cis_ret_t lwm2m_on_read(void* context,cis_uri_t* uri,cis_mid_t mid);
170cis_ret_t lwm2m_on_discover(void* context,cis_uri_t* uri,cis_mid_t mid);
171cis_ret_t lwm2m_on_write(void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t attrcount,cis_mid_t mid);
172cis_ret_t lwm2m_on_exec(void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid);
173cis_ret_t lwm2m_on_observe(void* context,cis_uri_t* uri,BOOL flag,cis_mid_t mid);
174cis_ret_t lwm2m_on_params(void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid);
175cis_ret_t lwm2m_on_params(void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid);
176void lwm2m_on_event(void* context,cis_evt_t eid,void* param);
177int32_t lwm2m_init();
178void lwm2m_deinit();
179int32_t parase_resource_from_value(char *value,int index);
180int32_t get_resource_count_from_value(char *value);
181void lwm2m_pump();
182int lwm2m_ubus_init(void);
183void lwm2m_get_imsi();
184void lwm2m_get_imei();
185
186
187
188static const struct ubus_method lwm2m_methods[] = {
189 UBUS_METHOD_NOARG("version", lwm2mgetversion_method),
190 UBUS_METHOD_NOARG("createdefault", lwm2mcreate_default_method),
191 UBUS_METHOD("create", lwm2mcreate_method, lwm2m_create_policy),
192 UBUS_METHOD("delete", lwm2mdelete_method, lwm2m_delete_policy),
193 UBUS_METHOD("addobj", lwm2maddobj_method, lwm2m_addobj_policy),
194 UBUS_METHOD("delobj", lwm2mdelobj_method, lwm2m_delobj_policy),
195 UBUS_METHOD("open", lwm2mopen_method, lwm2m_open_policy),
196 UBUS_METHOD("update", lwm2mupdate_method, lwm2m_update_policy),
197 UBUS_METHOD("close", lwm2mclose_method, lwm2m_delete_policy),
198 UBUS_METHOD("discoverresp", lwm2mdiscoverresp_method, lwm2m_discoverresp_policy),
199 UBUS_METHOD("readrsp", lwm2mreadrsp_method, lwm2m_readrsp_policy),
200 UBUS_METHOD("writersp", lwm2mwritersp_method, lwm2m_writersp_policy),
201 UBUS_METHOD("excutersp", lwm2mexcutersp_method, lwm2m_writersp_policy),
202 UBUS_METHOD("parameterrsp", lwm2mparameterrsp_method, lwm2m_writersp_policy),
203 UBUS_METHOD("observerrsp", lwm2mobserverrsp_method, lwm2m_writersp_policy),
204 UBUS_METHOD("notify", lwm2mnotify_method, lwm2m_notify_policy),
205
206};
207static struct ubus_object_type lwm2m_object_type = UBUS_OBJECT_TYPE("lwm2m", lwm2m_methods);
208
209static struct ubus_object lwm2m_object = {
210 .name = "lwm2m",
211 .type = &lwm2m_object_type,
212 .methods = lwm2m_methods,
213 .n_methods = ARRAY_SIZE(lwm2m_methods),
214};
215//static struct ubus_subscriber lwm2m_ind_event;
216static pthread_t s_tid_lwm2mMainTask;
217
218
219static int lwm2m_client_fd=-1;
220
221
222/*
223static struct ubus_object_type lwm2m_notify_object_type;
224
225static struct ubus_object lwm2m_notify_obj[1] =
226{
227 {.name = "lwm2m_ind", .type = &lwm2m_notify_object_type, .subscribe_cb = lwm2m_subscribe_cb},
228};
229*/
230
231
232
233static const uint8_t config_hex[] = {0x13,0x00,0x3b,
234 0xf1,0x00,0x03,
235 0xf2,0x00,0x29,0x05,0x00/*mtu*/,0x11/*Link&bind type*/,0x80/*BS ENABLED*/,0x00,0x00/*vpn*/,0x00,0x00/*username*/,0x00,0x00/*password*/,
236 0x00,0x14/*host length*/,0x6e,0x62,0x69,0x6f,0x74,0x62,0x74,0x2e,0x68,0x65,0x63,0x6c,0x6f,0x75,0x64,0x73,0x2e,0x63,
237 0x6f,0x6d,0x00,0x04,0x4e,0x55,0x4c,0x4c,0xf3,0x00,0x0c,0xea,0x04,0x00,0x00,0x04,0x4e,0x55,0x4c,0x4c};
238
239
240
241
242struct miplContext lwm2m_context = {0};
243static INT32 lwm2m_clear_register_flag();
244extern void *lwm2m_recv_thread(void* argv);
245
246
247/*
248static void lwm2m_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
249{
250 UNUSED(ctx);
251 lwm2m_printf("lwm2m Subscribers active: %d\n", obj->has_subscribers);
252 //RIL_onNewConnect();
253
254
255}
256*/
257
258int utils_findDataType(cis_data_t * dataP)
259{
260 int res = dataP->type;
261 LOGD("resource id is %d",dataP->id);
262 switch(dataP->id){
263 //boolean
264 case digital_input_state:
265 case digital_input_polarity:
266 case increase_input_state:
267 case decrease_input_state:
268 case digital_state:
269 case digital_output_state:
270 case digital_output_polarity:
271 case on_off:
272 res = cis_data_type_bool;
273 utils_opaqueToInt(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asInteger);
274 break;
275 //integer
276 case digital_input_counter:
277 case digital_input_debounce:
278 case digital_input_edge_selection:
279 case mode:
280 case x_coordinate:
281 case y_coordinate:
282 case counter:
283 case up_counter:
284 case down_counter:
285 case max_x_coordinate:
286 case max_y_coordinate:
287 case multi_state_input:
288 case criticality_level:
289 case duty_cycle:
290 case dimmer:
291 case on_time:
292 case off_time:
293 case busy_to_clear_delay:
294 case clear_to_busy_delay:
295 case bitmap_input:
296 res = cis_data_type_integer;
297 utils_opaqueToInt(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asInteger);
298 break;
299 //float
300 case fractional_time:
301 case min_x_value:
302 case max_x_value:
303 case min_y_value:
304 case max_y_value:
305 case min_z_value:
306 case max_z_value:
307 case min_limit:
308 case max_limit:
309 case delay_duration:
310 case duration:
311 case minimum_off_time:
312 case contrast:
313 case current_position:
314 case transition_time:
315 case remaining_time:
316 case cumulative_time:
317 case level:
318 case analog_input_state:
319 case min_measured_value:
320 case max_measured_value:
321 case min_range_value:
322 case max_range_value:
323 case analog_output_current_value:
324 case sensor_value:
325 case x_value:
326 case y_value:
327 case z_value:
328 case compass_direction:
329 case instantaneous_active_power:
330 case min_measured_active_power:
331 case max_measured_active_power:
332 case min_range_active_power:
333 case max_range_active_power:
334 case cumulative_active_power:
335 case active_power_calibration:
336 case instantaneous_reactive_power:
337 case min_measured_reactive_power:
338 case max_measured_reactive_power:
339 case min_range_reactive_power:
340 case max_range_reactive_power:
341 case cumulative_reactive_power:
342 case reactive_power_calibration:
343 case power_factor:
344 case current_calibration:
345 case start_time:
346 case duration_in_min:
347 case set_point_value:
348 res = cis_data_type_float;
349 utils_opaqueToFloat(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asFloat);
350 break;
351 //string
352 case latitude:
353 case longitude:
354 case uncertainty:
355 case text:
356 case sensor_units:
357 case colour:
358 case application_type:
359 case sensor_type:
360 case event_identifier:
361 case avg_load_adj_pct:
362 case muti_state_output:
363 case element_description:
364 case uuid:
365 res = cis_data_type_string;
366 break;
367
368
369 default:
370 break;
371 }
372
373 return res;
374}
375
376void init_lwm2m_context()
377{
378 memset(&lwm2m_context,0,sizeof(struct miplContext));
379}
380
381cis_ret_t lwm2m_on_read (void* context,cis_uri_t* uri,cis_mid_t mid)
382{
383 UNUSED(context);
384 int ref=0;
385 int msgid=0;
386 int objectid=0;
387 int instanceid=0;
388 int resourceid=0;
389 int res;
390
391 lwm2m_printf("lwm2m_on_read:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
392 if(CIS_URI_IS_SET_RESOURCE(uri)){
393 ref = 0;
394 msgid = mid;
395 objectid = uri->objectId;
396 instanceid = uri->instanceId;
397 resourceid = uri->resourceId;
398 }else if(!CIS_URI_IS_SET_RESOURCE(uri)&& CIS_URI_IS_SET_INSTANCE(uri)){
399 ref = 0;
400 msgid = mid;
401 objectid = uri->objectId;
402 instanceid = uri->instanceId;
403 resourceid = -1;
404 }else if(!CIS_URI_IS_SET_RESOURCE(uri)&& !CIS_URI_IS_SET_INSTANCE(uri)){
405 ref = 0;
406 msgid = mid;
407 objectid = uri->objectId;
408 instanceid = -1;
409 resourceid = -1;
410 }
411
412 struct lwm2m_usock_head msg;
413 msg.msgId = 0x2795;
414 msg.msgData = (char *)malloc(256);
415 memset(msg.msgData,0,256);
416 res=snprintf(msg.msgData, 256, "+MIPLREAD: %d,%d,%d,%d,%d",
417 ref,msgid,objectid,instanceid,resourceid);
418 msg.msgLen = res;
419 if(lwm2m_client_fd){
420 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
421 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
422 }
423
424 free(msg.msgData);
425
426
427 return CIS_CALLBACK_CONFORM;
428
429}
430
431
432cis_ret_t lwm2m_on_discover (void* context,cis_uri_t* uri,cis_mid_t mid)
433{
434 int res;
435 UNUSED(context);
436 if(CIS_URI_IS_SET_RESOURCE(uri)){
437 lwm2m_printf("lwm2m_on_discover:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
438 }
439 else if(CIS_URI_IS_SET_INSTANCE(uri)){
440 lwm2m_printf("lwm2m_on_discover:(%d/%d)\r\n",uri->objectId,uri->instanceId);
441 }
442 else{
443 lwm2m_printf("lwm2m_on_discover:(%d)\r\n",uri->objectId);
444 }
445
446 struct lwm2m_usock_head msg;
447 msg.msgId = 0x2795;
448 msg.msgData = (char *)malloc(256);
449 memset(msg.msgData,0,256);
450 res=snprintf(msg.msgData, 256, "+MIPLDISCOVER: %d,%d,%d",
451 0,mid,uri->objectId);
452 msg.msgLen = res;
453
454 if(lwm2m_client_fd){
455 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
456 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
457 }
458
459 free(msg.msgData);
460
461 return CIS_CALLBACK_CONFORM;
462}
463
464
465cis_ret_t lwm2m_on_write (void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t attrcount,cis_mid_t mid)
466{
467 UNUSED(context);
468 int i,j;
469 const cis_data_t* temp_value = NULL;
470
471 int ref=0;
472 int msgid=0;
473 int objectid=0;
474 int instanceid=0;
475 int resourceid=0;
476 int valuetype=0;
477 int index=0;
478 int flag=0;
479 int length=0;
480 char buf[512] ={0};
481 int res;
482
483
484 if(CIS_URI_IS_SET_RESOURCE(uri)){
485 lwm2m_printf("lwm2m_on_write:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
486 }
487 else{
488 lwm2m_printf("lwm2m_on_write:(%d/%d)\r\n",uri->objectId,uri->instanceId);
489 }
490
491 int temp_index = attrcount;
492 lwm2m_printf("lwm2m_on_write: attrcount(%d)\r\n",attrcount);
493 for(i = 0;i<attrcount;i++)
494 {
495 temp_value = value+i;
496
497 ref = 0;
498 msgid = mid;
499 objectid = uri->objectId;
500 instanceid = uri->instanceId;
501 resourceid = temp_value->id;
502 valuetype = temp_value->type;
503 index = temp_index-1;
504 if(index==0){
505 flag=0;
506 }else if(i==0){
507 flag=1;
508 }else{
509 flag=2;
510 }
511 lwm2m_printf("lwm2m_on_write: valuetype(%d)\r\n",valuetype);
512 /*
513 res = writeInd->valuetype;
514 if(writeInd->valuetype == cis_data_type_opaque){
515 lwm2m_printf("lwm2m_on_write: length(%d)\n",temp_value->asBuffer.length);
516 LOG_BUF("buffer",temp_value->asBuffer.buffer,temp_value->asBuffer.length);
517 res=utils_findDataType(temp_value);
518 }
519 lwm2m_printf("lwm2m_on_write: res(%d)\n",res);
520 */
521 if(temp_value->type == cis_data_type_string ){
522 //lwm2m_printf("lwm2m_on_write: length(%d)\n",temp_value->asBuffer.length);
523 length = temp_value->asBuffer.length;
524 //lwm2m_printf("lwm2m_on_write: value(%s)\n",temp_value->asBuffer.buffer);
525 //char *temp=value->value.asChildren.array[i].asBuffer.buffer;
526 //LOG_BUF("buffer",temp,writeInd->length+1);
527 cissys_memcpy(buf,temp_value->asBuffer.buffer,length);
528 }else if(temp_value->type == cis_data_type_integer){
529 int size;
530 //temp_value->value.asInteger = 0;
531 //cissys_memcpy(&temp_value->value.asInteger,temp_value->asBuffer.buffer,temp_value->asBuffer.length);
532 size = snprintf(buf, 512, "%lld",temp_value->value.asInteger);
533 length = size;
534 }else if(temp_value->type == cis_data_type_float){
535 int size;
536 size = snprintf(buf, 512, "%f",temp_value->value.asFloat);
537 length = size;
538 }else if(temp_value->type == cis_data_type_bool){
539 int size;
540 lwm2m_printf("lwm2m_on_write: value(%lld)\r\n",temp_value->value.asInteger);
541 size = snprintf(buf, 512, "%lld",temp_value->value.asInteger);
542 length = size;
543 }else if(temp_value->type == cis_data_type_opaque){
544
545 lwm2m_printf("lwm2m_on_write: length(%d)\r\n",temp_value->asBuffer.length);
546 length = temp_value->asBuffer.length;
547 uint8_t *temp=temp_value->asBuffer.buffer;
548 LOG_BUF("buffer",temp,length);
549 for(j=0;j<length;j++)
550 {
551 snprintf(buf+j,512,"%02d",*(temp+j));
552 }
553
554 }
555
556 if(valuetype != cis_data_type_undefine){
557
558 struct lwm2m_usock_head msg;
559 msg.msgId = 0x2795;
560 msg.msgData = (char *)malloc(512);
561 memset(msg.msgData,0,512);
562 res=snprintf(msg.msgData, 512, "+MIPLWRITE: %d,%d,%d,%d,%d,%d,%d,%s,%d,%d",
563 ref,msgid,objectid,instanceid,resourceid,
564 valuetype,length,buf,flag,index);
565 msg.msgLen = res;
566 if(lwm2m_client_fd){
567 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
568 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
569 }
570 free(msg.msgData);
571
572
573
574 }
575
576 temp_index --;
577 }
578
579 return CIS_CALLBACK_CONFORM;
580
581}
582
583cis_ret_t lwm2m_on_exec (void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid)
584{
585 UNUSED(context);
586 int res;
587 if(CIS_URI_IS_SET_RESOURCE(uri)){
588 lwm2m_printf("lwm2m_on_exec:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
589 }
590 else{
591 return CIS_CALLBACK_METHOD_NOT_ALLOWED;
592 }
593
594 if(!CIS_URI_IS_SET_INSTANCE(uri))
595 {
596 return CIS_CALLBACK_BAD_REQUEST;
597 }
598
599
600
601
602 struct lwm2m_usock_head msg;
603 msg.msgId = 0x2795;
604 msg.msgData = (char *)malloc(256);
605 memset(msg.msgData,0,256);
606
607 if(length==0){
608 res=snprintf(msg.msgData, 256, "+MIPLEXECUTE: %d,%d,%d,%d,%d",
609 0,mid,uri->objectId,uri->instanceId,uri->resourceId);
610
611 }else{
612 res=snprintf(msg.msgData, 256, "+MIPLEXECUTE: %d,%d,%d,%d,%d,%d,\"%s\"",
613 0,mid,uri->objectId,uri->instanceId,uri->resourceId,length,value);
614
615 }
616 msg.msgLen = res;
617
618 if(lwm2m_client_fd){
619 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
620 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
621 }
622 free(msg.msgData);
623
624
625
626 return CIS_CALLBACK_CONFORM;
627
628}
629
630
631
632cis_ret_t lwm2m_on_observe (void* context,cis_uri_t* uri,BOOL flag,cis_mid_t mid)
633{
634 UNUSED(context);
635 int flag_value;
636 int resourceid;
637 int instanceid;
638 int res;
639
640 if(flag){
641 LOGD("cis_on_observe set: %d/%d/%d\n",
642 uri->objectId,
643 CIS_URI_IS_SET_INSTANCE(uri)?uri->instanceId:-1,
644 CIS_URI_IS_SET_RESOURCE(uri)?uri->resourceId:-1);
645
646 }else{
647 LOGD("cis_on_observe cancel: %d/%d/%d\n",
648 uri->objectId,
649 CIS_URI_IS_SET_INSTANCE(uri)?uri->instanceId:-1,
650 CIS_URI_IS_SET_RESOURCE(uri)?uri->resourceId:-1);
651 }
652
653 if(flag){
654 flag_value = 1;
655 }else{
656 flag_value = 0;
657 }
658 LOGD("cis_on_observe mid: %d objectid: %d\n",mid,uri->objectId);
659
660 if(CIS_URI_IS_SET_RESOURCE(uri)){
661 resourceid = uri->resourceId;
662 }else{
663 resourceid =-1;
664 }
665 if(CIS_URI_IS_SET_INSTANCE(uri)){
666 instanceid = uri->instanceId;
667 }else{
668 instanceid =-1;
669 }
670
671
672
673
674 struct lwm2m_usock_head msg;
675 msg.msgId = 0x2795;
676 msg.msgData = (char *)malloc(256);
677 memset(msg.msgData,0,256);
678 res=snprintf(msg.msgData, 256, "+MIPLOBSERVE: %d,%d,%d,%d,%d,%d",
679 0,mid,flag_value,uri->objectId,instanceid,resourceid);
680 msg.msgLen = res;
681
682
683 if(lwm2m_client_fd){
684 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
685 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
686 }
687 free(msg.msgData);
688
689
690 return CIS_CALLBACK_CONFORM;
691
692}
693
694
695cis_ret_t lwm2m_on_params (void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid)
696{
697 UNUSED(context);
698 char value[128]={0};
699 int res;
700
701
702 if(CIS_URI_IS_SET_RESOURCE(uri)){
703 lwm2m_printf("cis_on_params:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
704 }
705 else{
706 return CIS_CALLBACK_METHOD_NOT_ALLOWED;
707 }
708
709 if(!CIS_URI_IS_SET_INSTANCE(uri))
710 {
711 return CIS_CALLBACK_BAD_REQUEST;
712 }
713
714
715 int size;
716
717 size = snprintf(value, 512, "pmin=%d;pmax=%d;lt=%f;gt=%f;stp=%f",
718 parameters.minPeriod, parameters.maxPeriod,parameters.lessThan,parameters.greaterThan,parameters.step);
719
720
721 struct lwm2m_usock_head msg;
722 msg.msgId = 0x2795;
723 msg.msgData = (char *)malloc(256);
724 memset(msg.msgData,0,256);
725 res=snprintf(msg.msgData, 256, "+MIPLPARAMETER: %d,%d,%d,%d,%d,%d,\"%s\"",
726 0,mid,uri->objectId,uri->instanceId,uri->resourceId,size,value);
727 msg.msgLen = res;
728 if(lwm2m_client_fd){
729 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
730 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
731 }
732 free(msg.msgData);
733
734
735
736 return CIS_CALLBACK_CONFORM;
737}
738
739
740
741void lwm2m_on_event (void* context,cis_evt_t eid,void* param)
742{
743 int value;
744 int res;
745 lwm2m_printf("cis_on_event(%d):%s\r\n",eid,STR_EVENT_CODE(eid));
746 switch(eid)
747 {
748 case CIS_EVENT_RESPONSE_FAILED:
749 lwm2m_printf("cis_on_event response failed mid:%d\r\n",(int32_t)param);
750 break;
751 case CIS_EVENT_NOTIFY_FAILED:
752 lwm2m_printf("cis_on_event notify failed mid:%d\r\n",(int32_t)param);
753 break;
754 case CIS_EVENT_UPDATE_NEED:
755 lwm2m_printf("cis_on_event need to update,reserve time:%ds\r\n",(int32_t)param);
756 cis_update_reg(context,LIFETIME_INVALID,false);
757 break;
758 case CIS_EVENT_REG_SUCCESS:
759 {
760 /*
761 struct st_observe_info* delnode;
762 while(g_observeList != NULL){
763 g_observeList =(st_observe_info *)CIS_LIST_RM((cis_list_t *)g_observeList,g_observeList->mid,(cis_list_t **)&delnode);
764 free(delnode);
765 }
766 */
767 }
768 break;
769 case CIS_EVENT_UNREG_DONE:
770 lwm2m_printf("cis_on_event close done\r\n");
771 lwm2m_clear_register_flag();
772 break;
773 default:
774 break;
775 }
776
777
778 struct lwm2m_usock_head msg;
779 msg.msgId = 0x2795;
780 msg.msgData = (char *)malloc(256);
781 memset(msg.msgData,0,256);
782 value = (int)param;
783 if(param){
784 res=snprintf(msg.msgData, 256, "+MIPLEVENT: %d,%d,%d",
785 0,eid,value);
786
787 }else{
788 res=snprintf(msg.msgData, 256, "+MIPLEVENT: %d,%d",
789 0,eid);
790
791 }
792 msg.msgLen = res;
793 if(lwm2m_client_fd){
794 write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
795 write(lwm2m_client_fd, msg.msgData, msg.msgLen);
796 }
797 free(msg.msgData);
798
799
800}
801
802
803int32_t lwm2m_init()
804{
805 cis_version_t ver;
806 lwm2m_context.g_lifetime = 20;
807
808 /*init sample data*/
809 //prv_make_sample_data();
810 cis_version(&ver);
811 lwm2m_printf("CIS SDK Version:%u.%u.%u\r\n",ver.major,ver.minor,ver.micro);
812
813 if(cis_init(&g_context,lwm2m_context.config,lwm2m_context.config_size,NULL,NULL) != CIS_RET_OK){
814 lwm2m_printf("%s init failed.\r\n",__func__);
815 return LWM2MRC_FAIL;
816 }
817
818 return LWM2MRC_SUCCESS;
819
820
821}
822
823void lwm2m_deinit()
824{
825 cis_deinit(&g_context);
826
827}
828
829
830
831static int32_t lwm2m_clear_register_flag()
832{
833 lwm2m_context.register_flag = 0;
834
835 return LWM2MRC_SUCCESS;
836
837}
838
839
840static int lwm2m_get_boolean(const char * value)
841{
842 if (value) {
843 if (!strcmp(value, "true")) {
844 return 1;
845 } else if (!strcmp(value, "on")) {
846 return 1;
847 } else if (!strcmp(value, "1")) {
848 return 1;
849 } else if (!strcmp(value, "yes")) {
850 return 1;
851 }
852 }
853 return 0;
854}
855
856int32_t parase_resource_from_value(char *value,int index)
857{
858 int i,len;
859 char *p=NULL;
860 char *q=NULL;
861 char *t=NULL;
862 if(value==NULL){
863 return -1;
864 }
865
866 len=strlen(value);
867 if(len<=0){
868 return -1;
869 }
870 q=(char *)malloc(len+10);
871 t=q;
872 memset(q,0,len+10);
873 memcpy(q,value,len);
874 int res=-1;
875 for(i=0;i<index;i++){
876 p= strstr(q,";");
877 if(p==NULL && (i+1)==index){
878 res = atoi(q);
879 goto out;
880 }else if(p!=NULL && (i+1)==index){
881 *p = 0;
882 res = atoi(q);
883 goto out;
884 }else if(p!=NULL && (i+1)!=index){
885 q=p+1;
886 }
887 }
888out:
889 free(t);
890 return res;
891
892}
893
894int32_t get_resource_count_from_value(char *value)
895{
896 int i,len;
897 int count=1;
898 if(value==NULL){
899 return 0;
900 }
901
902 len=strlen(value);
903 if(len<=0){
904 return 0;
905 }
906 for(i=0;i<len;i++){
907 if(*(value+i)==';'){
908 count++;
909 }
910 }
911
912 return count;
913
914}
915
916/*
917static BOOL lwm2m_getInstId(uint8_t* inst_bitmap,uint8_t index,cis_iid_t* iid)
918{
919 if (inst_bitmap == NULL)
920 {
921 return 0;
922 }
923
924 uint8_t bytes = index / 8;
925 uint8_t bits = index % 8;
926
927 if ((*(inst_bitmap + bytes) >> (7 - bits)) & 0x01) {
928 if(iid != NULL)*iid = index;
929 return true;
930 }
931 return false;
932}
933*/
934
935
936void lwm2m_pump()
937{
938 //int i = 0;
939
940 /*pump function*/
941 cis_pump(g_context);
942
943 /*update lifetime*/
944 /*
945 uint32_t nowtime = cissys_gettime();
946 if(nowtime - g_lifetime_last > ((g_lifetime-5) * 1000)){
947 g_lifetime_last = cissys_gettime();
948 cis_update_reg(g_context,LIFETIME_INVALID,false);
949 }
950
951 if(g_callback.flag!=0){
952 if((nowtime - g_callback.time)*200 > 10){
953 prv_reset_callback_info();
954
955 }
956
957 }
958 */
959
960}
961
962
963static void * lwm2m_main(void * param)
964{
965 UNUSED(param);
966 struct timespec abstimeout;
967 int ret;
968 uint32_t pumpRet;
969 /*update lifetime*/
970 struct timeval nowtime ;
971 uint32_t count;
972
973 while(1){
974
975 pthread_mutex_lock(&lwm2m_context.mutex);
976 if(lwm2m_context.register_flag){
977 /*pump function*/
978 pumpRet = cis_pump(g_context);
979 if(pumpRet == PUMP_RET_NOSLEEP)
980 {
981 lwm2m_printf("%s,pump no sleep\r\n",__func__);
982 //memset(&apiMsg,0,sizeof(struct miApiMsg));
983 pthread_mutex_unlock(&lwm2m_context.mutex);
984 continue;
985 }else if(pumpRet == PUMP_RET_TRANSSLEEP){
986 gettimeofday(&nowtime, NULL);
987 lwm2m_printf("%s,[%d]nowtime.tv_sec[%ld]\r\n",__func__,
988 lwm2m_context.register_flag,
989 nowtime.tv_sec);
990 //timeout = 200;
991 abstimeout.tv_sec=nowtime.tv_sec+1;
992 abstimeout.tv_nsec = nowtime.tv_usec * 1000;
993 }else{
994 gettimeofday(&nowtime, NULL);
995 count=(uint32_t)(nowtime.tv_sec - lwm2m_context.g_lifetime_last.tv_sec);
996 if(count >= (lwm2m_context.g_lifetime-5)){
997 //lwm2m_context.g_lifetime_last = cissys_gettime();
998 gettimeofday(&lwm2m_context.g_lifetime_last, NULL);
999 cis_update_reg(g_context,LIFETIME_INVALID,false);
1000 //abstimeout.tv_sec = nowtime.tv_sec+5;
1001 //abstimeout.tv_nsec = nowtime.tv_usec * 1000;
1002 //timeout=(lwm2m_context.g_lifetime -5)*200;
1003 //memset(&apiMsg,0,sizeof(struct miApiMsg));
1004 pthread_mutex_unlock(&lwm2m_context.mutex);
1005 continue;
1006 }else{
1007 abstimeout.tv_sec=nowtime.tv_sec+((lwm2m_context.g_lifetime_last.tv_sec+lwm2m_context.g_lifetime-5) - nowtime.tv_sec);
1008 abstimeout.tv_nsec = nowtime.tv_usec * 1000;
1009 //timeout= (lwm2m_context.g_lifetime-5) * 1000 - (nowtime - lwm2m_context.g_lifetime_last);
1010 //if(timeout/5){
1011 // timeout = timeout/5;
1012 //}else{
1013 // timeout = 1;
1014 //}
1015 }
1016
1017 }
1018
1019 }
1020
1021 if (lwm2m_context.register_flag){
1022
1023 lwm2m_printf("%s,[%d]abstimeout.tv_sec[%ld]\r\n",__func__,
1024 lwm2m_context.register_flag,
1025 abstimeout.tv_sec);
1026 ret = pthread_cond_timedwait(&lwm2m_context.cond, &lwm2m_context.mutex, &abstimeout);
1027 if (ret == ETIMEDOUT) {
1028 lwm2m_printf("%s,pthread_cond_timedwait timeout\r\n",__func__);
1029 }
1030
1031 }else{
1032 pthread_cond_wait(&lwm2m_context.cond, &lwm2m_context.mutex);
1033 }
1034
1035 pthread_mutex_unlock(&lwm2m_context.mutex);
1036
1037 }
1038
1039 return NULL;
1040}
1041
1042
1043
1044
1045static int lwm2mgetversion_method(struct ubus_context *ctx, struct ubus_object *obj,
1046 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1047{
1048 UNUSED(obj);
1049 UNUSED(msg);
1050 UNUSED(method);
1051
1052 static struct blob_buf b;
1053 char version[64]={0};
1054/*
1055 int ret;
1056 memset(&b, 0, sizeof(b));
1057 blob_buf_init(&b, 0);
1058 blobmsg_add_string(&b, "event", "test");
1059 ret=ubus_notify(lwm2m_ubus_ctx, &lwm2m_object_type, "lwm2m", b.head, -1);
1060 if (ret)
1061 lwm2m_printf("%s: ubus notify error: ret=%d\n", __FUNCTION__, ret);
1062
1063
1064 ubus_notify_async
1065
1066 blobmsg_buf_init(&wifi_buf);
1067 blobmsg_add_u32(&wifi_buf, "id", SSID_CHANGE_NOTIFY_ID);
1068 blobmsg_add_string(&wifi_buf, "new_ssid", new_ssid);
1069 if (ubus_notify_async(ctx, &wifi_service_object, "ssid_change", wifi_buf.head, &nreq))
1070 return 0;
1071 blob_buf_free(&wifi_buf);
1072 nreq.complete_cb = (void *)&ssid_change_notify_complete_cb;
1073 ubus_complete_request(ctx, &nreq.req, 100);
1074
1075*/
1076 cis_version_t ver;
1077 cis_version(&ver);
1078 snprintf(version,64,"%u.%u.%u",ver.major,ver.minor,ver.micro);
1079
1080 memset(&b, 0, sizeof(b));
1081 blob_buf_init(&b, 0);
1082 blobmsg_add_string(&b, "version", version);
1083
1084 ubus_send_reply(ctx, req, b.head);
1085 return 0;
1086}
1087
1088static int lwm2mcreate_default_method(struct ubus_context *ctx, struct ubus_object *obj,
1089 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1090{
1091 UNUSED(obj);
1092 UNUSED(msg);
1093 UNUSED(method);
1094 int len;
1095 static struct blob_buf b;
1096 unsigned long result;
1097 int retry=5;
1098
1099 len=sizeof(config_hex)+1;
1100 lwm2m_context.config = (CHAR *)malloc(len);
1101 memset(lwm2m_context.config,0,len);
1102 memcpy(lwm2m_context.config,config_hex,sizeof(config_hex));
1103 lwm2m_context.config_size = sizeof(config_hex);
1104 result = lwm2m_init();
1105
1106 if(result!=LWM2MRC_SUCCESS){
1107 lwm2m_printf("lwm2m_init failed\r\n");
1108 }else{
1109
1110 start:
1111 if(lwm2m_client_fd<0){
1112 lwm2m_client_fd = usock(USOCK_UNIX, LWM2M_INNER_CONN_SOCKET, NULL);
1113 if (lwm2m_client_fd < 0) {
1114 if(retry){
1115 sleep(1);
1116 retry--;
1117 goto start;
1118 }
1119 lwm2m_printf("%s,Failed to connect to server\r\n",__FUNCTION__);
1120 result = LWM2MRC_FAIL;
1121 }
1122 }
1123
1124
1125 }
1126
1127 memset(&b, 0, sizeof(b));
1128 blob_buf_init(&b, 0);
1129 blobmsg_add_u32(&b, "result", result);
1130
1131 ubus_send_reply(ctx, req, b.head);
1132 return 0;
1133}
1134
1135
1136static int lwm2mcreate_method(struct ubus_context *ctx, struct ubus_object *obj,
1137 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1138{
1139 UNUSED(obj);
1140 UNUSED(msg);
1141 UNUSED(method);
1142
1143 static struct blob_buf b;
1144
1145 struct blob_attr *tb[5];
1146 int total_size=0;
1147 char * context=NULL;
1148 int index=0;
1149 int current_size=0;
1150 int flag=0;
1151
1152 int result;
1153 int ref=0;
1154
1155 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1156 result = blobmsg_parse(lwm2m_create_policy, 5, tb, blob_data(msg), blob_len(msg));
1157 if (result < 0) {
1158 result = LWM2MRC_FAIL;
1159 goto out;
1160 }
1161
1162 total_size = blobmsg_get_u32(tb[0]);
1163 context = blobmsg_get_string(tb[1]);
1164 index = blobmsg_get_u32(tb[2]);
1165 current_size = blobmsg_get_u32(tb[3]);
1166 flag = blobmsg_get_u32(tb[4]);
1167 lwm2m_printf("lwm2mcreate_method total_size[%d] index[%d] current_size[%d] flag[%d]\r\n",
1168 total_size,
1169 index,
1170 current_size,
1171 flag);
1172
1173 if(flag == 1){
1174 if(lwm2m_context.config){
1175 free(lwm2m_context.config);
1176 lwm2m_context.config =NULL;
1177 }
1178 lwm2m_context.config = (CHAR *)malloc(total_size + 10);
1179 memset(lwm2m_context.config,0,total_size +10);
1180 if(current_size > total_size) lwm2m_assert(0);
1181 memcpy(lwm2m_context.config,context,current_size);
1182 lwm2m_context.config_size = current_size;
1183 }else{
1184 if(lwm2m_context.config==NULL)lwm2m_assert(0);
1185 if((lwm2m_context.config_size + current_size) > total_size)lwm2m_assert(0);
1186 memcpy(lwm2m_context.config+lwm2m_context.config_size,context,current_size);
1187 lwm2m_context.config_size +=current_size;
1188 }
1189
1190 result = LWM2MRC_SUCCESS;
1191 lwm2m_context.config_flag = flag;
1192 ref = 1;
1193
1194 if(flag==0){
1195 result=lwm2m_init();
1196 if(result == LWM2MRC_SUCCESS){
1197 ref = 0;
1198 }
1199 }
1200
1201out:
1202 memset(&b, 0, sizeof(b));
1203 blob_buf_init(&b, 0);
1204 blobmsg_add_u32(&b, "result", result);
1205 blobmsg_add_u32(&b, "ref", ref);
1206
1207 ubus_send_reply(ctx, req, b.head);
1208 return 0;
1209}
1210
1211
1212static int lwm2mdelete_method(struct ubus_context *ctx, struct ubus_object *obj,
1213 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1214{
1215 UNUSED(obj);
1216 UNUSED(msg);
1217 UNUSED(method);
1218
1219 static struct blob_buf b;
1220
1221 struct blob_attr *tb[1];
1222
1223 int result;
1224 int ref;
1225
1226 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1227 result = blobmsg_parse(lwm2m_delete_policy, 1, tb, blob_data(msg), blob_len(msg));
1228 if (result < 0) {
1229 result = LWM2MRC_FAIL;
1230 goto out;
1231 }
1232
1233 ref = blobmsg_get_u32(tb[0]);
1234
1235 if(lwm2m_context.config && lwm2m_context.ref == ref){
1236 free(lwm2m_context.config);
1237 lwm2m_context.config =NULL;
1238 lwm2m_deinit();
1239 result = LWM2MRC_SUCCESS;
1240 }else{
1241 result = LWM2MRC_FAIL;
1242 }
1243
1244
1245out:
1246 memset(&b, 0, sizeof(b));
1247 blob_buf_init(&b, 0);
1248 blobmsg_add_u32(&b, "result", result);
1249
1250 ubus_send_reply(ctx, req, b.head);
1251 return 0;
1252}
1253
1254static int lwm2maddobj_method(struct ubus_context *ctx, struct ubus_object *obj,
1255 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1256{
1257 UNUSED(obj);
1258 UNUSED(msg);
1259 UNUSED(method);
1260
1261 static struct blob_buf b;
1262
1263 struct blob_attr *tb[6];
1264 int ref;
1265 int objectId;
1266 int instanceCount;
1267 char * instanceBitMap;
1268 int attributeCount;
1269 int actionCount;
1270
1271 int result;
1272
1273 cis_ret_t ret;
1274 cis_res_count_t rescount;
1275 cis_inst_bitmap_t bitmap;
1276
1277 uint16_t instCount,instBytes;
1278 const char* instAsciiPtr;
1279 uint8_t * instPtr;
1280 cis_oid_t oid;
1281 uint16_t i;
1282 lwm2m_printf("entry lwm2maddobj_method\r\n");
1283
1284 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1285 result = blobmsg_parse(lwm2m_addobj_policy, 6, tb, blob_data(msg), blob_len(msg));
1286 if (result < 0) {
1287 result = LWM2MRC_FAIL;
1288 goto out;
1289 }
1290
1291 ref = blobmsg_get_u32(tb[0]);
1292 objectId = blobmsg_get_u32(tb[1]);
1293 instanceCount = blobmsg_get_u32(tb[2]);
1294 instanceBitMap = blobmsg_get_string(tb[3]);
1295 attributeCount = blobmsg_get_u32(tb[4]);
1296 actionCount = blobmsg_get_u32(tb[5]);
1297
1298
1299 if(lwm2m_context.config==NULL || lwm2m_context.ref != ref){
1300 result = LWM2MRC_FAIL;
1301 goto out;
1302 }
1303
1304 oid = objectId;
1305 //instCount = strlen(instanceBitMap);
1306 instCount = instanceCount;
1307 instBytes = (instCount - 1) / 8 + 1;
1308 instAsciiPtr = instanceBitMap;
1309 lwm2m_printf("instCount[%d],instBytes[%d]\r\n",instCount,instBytes);
1310
1311 instPtr = (uint8_t*)cis_malloc(instBytes);
1312 memset(instPtr,0,instBytes);
1313
1314 for (i = 0;i < instCount;i++)
1315 {
1316 uint8_t instBytePos = i / 8;
1317 uint8_t instByteOffset = 7 - (i % 8);
1318 if(instAsciiPtr[i] == '1'){
1319 instPtr[instBytePos] += 0x01 << instByteOffset;
1320 }
1321 }
1322
1323 rescount.attrCount = attributeCount;
1324 //resconfig.actionCount = addobj_req->instancecount;
1325 rescount.actCount = actionCount;
1326 bitmap.instanceCount = instCount;
1327 bitmap.instanceBitmap = instPtr;
1328 bitmap.instanceBytes = instBytes;
1329
1330 lwm2m_printf("attrCount[%d],actCount[%d],instanceCount[%d],instanceBytes[%d]\r\n",
1331 rescount.attrCount,rescount.actCount,bitmap.instanceCount,bitmap.instanceBytes);
1332
1333 ret = cis_addobject(g_context,oid,&bitmap,&rescount);
1334 cis_free(instPtr);
1335 if(ret == CIS_RET_NO_ERROR){
1336 result = LWM2MRC_SUCCESS;
1337 }else{
1338 result = LWM2MRC_FAIL;
1339 }
1340 lwm2m_printf("lwm2maddobj_method finish\r\n");
1341out:
1342 memset(&b, 0, sizeof(b));
1343 blob_buf_init(&b, 0);
1344 blobmsg_add_u32(&b, "result", result);
1345
1346 ubus_send_reply(ctx, req, b.head);
1347 return 0;
1348}
1349
1350
1351static int lwm2mdelobj_method(struct ubus_context *ctx, struct ubus_object *obj,
1352 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1353{
1354 UNUSED(obj);
1355 UNUSED(msg);
1356 UNUSED(method);
1357
1358 static struct blob_buf b;
1359
1360 struct blob_attr *tb[2];
1361 int ref;
1362 int objectId;
1363
1364
1365 int result;
1366 cis_ret_t ret;
1367
1368
1369 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1370 result = blobmsg_parse(lwm2m_delobj_policy, 2, tb, blob_data(msg), blob_len(msg));
1371 if (result < 0) {
1372 result = LWM2MRC_FAIL;
1373 goto out;
1374 }
1375
1376 ref = blobmsg_get_u32(tb[0]);
1377 objectId = blobmsg_get_u32(tb[1]);
1378
1379
1380
1381 if(lwm2m_context.config==NULL || lwm2m_context.ref != ref){
1382 result = LWM2MRC_FAIL;
1383 goto out;
1384 }
1385
1386
1387 ret = cis_delobject(g_context,objectId);
1388 if(ret == CIS_RET_NO_ERROR){
1389 return LWM2MRC_SUCCESS;
1390 }else{
1391 return LWM2MRC_FAIL;
1392 }
1393
1394
1395out:
1396 memset(&b, 0, sizeof(b));
1397 blob_buf_init(&b, 0);
1398 blobmsg_add_u32(&b, "result", result);
1399
1400 ubus_send_reply(ctx, req, b.head);
1401 return 0;
1402}
1403
1404static int lwm2mopen_method(struct ubus_context *ctx, struct ubus_object *obj,
1405 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1406{
1407 UNUSED(obj);
1408 UNUSED(msg);
1409 UNUSED(method);
1410
1411 static struct blob_buf b;
1412
1413 struct blob_attr *tb[3];
1414 int ref;
1415 int lifetime;
1416 int timeout;
1417
1418
1419 int result;
1420 cis_ret_t ret;
1421
1422 lwm2m_printf("%s\r\n",__func__);
1423
1424 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1425 result = blobmsg_parse(lwm2m_open_policy, 3, tb, blob_data(msg), blob_len(msg));
1426 if (result < 0) {
1427 result = LWM2MRC_FAIL;
1428 goto out;
1429 }
1430
1431 ref = blobmsg_get_u32(tb[0]);
1432 lifetime = blobmsg_get_u32(tb[1]);
1433 timeout = blobmsg_get_u32(tb[2]);
1434 UNUSED(timeout);
1435
1436 if(lwm2m_context.config==NULL
1437 || lwm2m_context.ref != ref
1438 || lwm2m_context.config_flag!=0){
1439 result = LWM2MRC_FAIL;
1440 goto out;
1441 }
1442
1443 cis_callback_t callback;
1444 callback.onRead = (cis_read_callback_t)lwm2m_on_read;
1445 callback.onWrite = (cis_write_callback_t)lwm2m_on_write;
1446 callback.onExec = (cis_exec_callback_t)lwm2m_on_exec;
1447 callback.onObserve = (cis_observe_callback_t)lwm2m_on_observe;
1448 callback.onSetParams = (cis_set_params_callback_t)lwm2m_on_params;
1449 callback.onEvent = (cis_event_callback_t)lwm2m_on_event;
1450 callback.onDiscover = (cis_discover_callback_t)lwm2m_on_discover;
1451
1452
1453 //lwm2m_context.g_lifetime_last = cissys_gettime();
1454 gettimeofday(&lwm2m_context.g_lifetime_last, NULL);
1455 ret = cis_register(g_context,lifetime,&callback);
1456 if(ret!=CIS_RET_OK){
1457 lwm2m_printf("%s register failed.\r\n",__func__);
1458 result = LWM2MRC_FAIL;
1459 goto out;
1460 }
1461
1462 lwm2m_context.register_flag = 1;
1463 result = LWM2MRC_SUCCESS;
1464
1465 lwm2m_printf("%s,register_flag success\r\n",__func__);
1466
1467 pthread_mutex_lock(&lwm2m_context.mutex);
1468 pthread_cond_signal(&lwm2m_context.cond);
1469 pthread_mutex_unlock(&lwm2m_context.mutex);
1470
1471
1472out:
1473 memset(&b, 0, sizeof(b));
1474 blob_buf_init(&b, 0);
1475 blobmsg_add_u32(&b, "result", result);
1476
1477 ubus_send_reply(ctx, req, b.head);
1478 return 0;
1479}
1480
1481static int lwm2mupdate_method(struct ubus_context *ctx, struct ubus_object *obj,
1482 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1483{
1484 UNUSED(obj);
1485 UNUSED(msg);
1486 UNUSED(method);
1487
1488 static struct blob_buf b;
1489
1490 struct blob_attr *tb[3];
1491 int ref;
1492 int lifetime;
1493 int objflag;
1494
1495
1496 int result;
1497 cis_ret_t ret;
1498
1499
1500 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1501 result = blobmsg_parse(lwm2m_update_policy, 3, tb, blob_data(msg), blob_len(msg));
1502 if (result < 0) {
1503 result = LWM2MRC_FAIL;
1504 goto out;
1505 }
1506 ref = blobmsg_get_u32(tb[0]);
1507 lifetime = blobmsg_get_u32(tb[1]);
1508 objflag = blobmsg_get_u32(tb[2]);
1509
1510 if(lwm2m_context.config==NULL
1511 || lwm2m_context.ref != ref){
1512 result = LWM2MRC_FAIL;
1513 goto out;
1514 }
1515
1516 ret=cis_update_reg(g_context,lifetime,objflag);
1517 if(ret!=COAP_NO_ERROR){
1518 result = LWM2MRC_FAIL;
1519 goto out;
1520 }
1521 result = LWM2MRC_SUCCESS;
1522
1523 pthread_mutex_lock(&lwm2m_context.mutex);
1524 pthread_cond_signal(&lwm2m_context.cond);
1525 pthread_mutex_unlock(&lwm2m_context.mutex);
1526
1527
1528out:
1529 memset(&b, 0, sizeof(b));
1530 blob_buf_init(&b, 0);
1531 blobmsg_add_u32(&b, "result", result);
1532
1533 ubus_send_reply(ctx, req, b.head);
1534 return 0;
1535}
1536
1537
1538static int lwm2mclose_method(struct ubus_context *ctx, struct ubus_object *obj,
1539 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1540{
1541 UNUSED(obj);
1542 UNUSED(msg);
1543 UNUSED(method);
1544
1545 static struct blob_buf b;
1546
1547 struct blob_attr *tb[1];
1548 int ref;
1549 int result;
1550
1551
1552 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1553 result = blobmsg_parse(lwm2m_delete_policy, 1, tb, blob_data(msg), blob_len(msg));
1554 if (result < 0) {
1555 result = LWM2MRC_FAIL;
1556 goto out;
1557 }
1558
1559 ref = blobmsg_get_u32(tb[0]);
1560
1561
1562 if(lwm2m_context.config==NULL
1563 || lwm2m_context.ref != ref){
1564 result = LWM2MRC_FAIL;
1565 goto out;
1566 }
1567
1568 cis_unregister(g_context);
1569
1570 result = LWM2MRC_SUCCESS;
1571 lwm2m_context.register_flag = 0;
1572
1573 pthread_mutex_lock(&lwm2m_context.mutex);
1574 pthread_cond_signal(&lwm2m_context.cond);
1575 pthread_mutex_unlock(&lwm2m_context.mutex);
1576
1577
1578out:
1579 memset(&b, 0, sizeof(b));
1580 blob_buf_init(&b, 0);
1581 blobmsg_add_u32(&b, "result", result);
1582
1583 ubus_send_reply(ctx, req, b.head);
1584 return 0;
1585}
1586
1587
1588static int lwm2mdiscoverresp_method(struct ubus_context *ctx, struct ubus_object *obj,
1589 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1590{
1591 UNUSED(obj);
1592 UNUSED(msg);
1593 UNUSED(method);
1594
1595 static struct blob_buf b;
1596
1597 struct blob_attr *tb[5];
1598 int ref=0;
1599 int msgId=0;
1600 int res=0;
1601 int length=0;
1602 char *buf=NULL;
1603
1604
1605 int result=0;
1606 int count=0;
1607 int i;
1608 cis_uri_t temp_uri;
1609
1610 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1611 result = blobmsg_parse(lwm2m_discoverresp_policy, 5, tb, blob_data(msg), blob_len(msg));
1612 if (result < 0) {
1613 result = LWM2MRC_FAIL;
1614 goto out;
1615 }
1616
1617 ref = blobmsg_get_u32(tb[0]);
1618 msgId = blobmsg_get_u32(tb[1]);
1619 res = blobmsg_get_u32(tb[2]);
1620 length = blobmsg_get_u32(tb[3]);
1621 buf = blobmsg_get_string(tb[4]);
1622 UNUSED(length);
1623
1624 if(lwm2m_context.config==NULL
1625 || lwm2m_context.ref != ref){
1626 result = LWM2MRC_FAIL;
1627 goto out;
1628 }
1629
1630 lwm2m_printf("lwm2m_discover_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1631 if(res!=MIPLRS_205_CONTENT){
1632 result=RESULT_TO_RESPONSE(res);
1633 cis_response(g_context,NULL,NULL,msgId,result);
1634
1635 }else{
1636 count=get_resource_count_from_value(buf);
1637 lwm2m_printf("lwm2m_discover_rsp: get count(%d)\r\n",count);
1638 for(i=0;i<count;i++){
1639 memset(&temp_uri,0,sizeof(cis_uri_t));
1640 temp_uri.objectId = URI_INVALID;
1641 temp_uri.instanceId = URI_INVALID;
1642 temp_uri.resourceId = parase_resource_from_value(buf,i+1);
1643 cis_uri_update(&temp_uri);
1644 cis_response(g_context,&temp_uri,NULL,msgId,CIS_RESPONSE_CONTINUE);
1645 }
1646 cis_response(g_context,NULL,NULL,msgId,CIS_RESPONSE_DISCOVER);
1647
1648 }
1649
1650 result = LWM2MRC_SUCCESS;
1651
1652 pthread_mutex_lock(&lwm2m_context.mutex);
1653 pthread_cond_signal(&lwm2m_context.cond);
1654 pthread_mutex_unlock(&lwm2m_context.mutex);
1655
1656
1657out:
1658 memset(&b, 0, sizeof(b));
1659 blob_buf_init(&b, 0);
1660 blobmsg_add_u32(&b, "result", result);
1661
1662 ubus_send_reply(ctx, req, b.head);
1663 return 0;
1664}
1665
1666
1667
1668static int lwm2mreadrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
1669 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1670{
1671 UNUSED(obj);
1672 UNUSED(msg);
1673 UNUSED(method);
1674
1675 static struct blob_buf b;
1676
1677 struct blob_attr *tb[11];
1678 int ref;
1679 int msgId;
1680 int res;
1681 int objectid;
1682 int instanceid;
1683 int resourceid;
1684 int valuetype;
1685 int len;
1686 char *buf;
1687 int index;
1688 int flag;
1689
1690
1691 int result;
1692 st_context_t *context = (st_context_t*)g_context;
1693 cis_uri_t uri={0};
1694 cis_data_t tmpdata={0};
1695
1696 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1697 result = blobmsg_parse(lwm2m_readrsp_policy, 11, tb, blob_data(msg), blob_len(msg));
1698 if (result < 0) {
1699 result = LWM2MRC_FAIL;
1700 goto out;
1701 }
1702
1703 ref = blobmsg_get_u32(tb[0]);
1704 msgId = blobmsg_get_u32(tb[1]);
1705 res = blobmsg_get_u32(tb[2]);
1706 objectid = blobmsg_get_u32(tb[3]);
1707 instanceid = blobmsg_get_u32(tb[4]);
1708 resourceid = blobmsg_get_u32(tb[5]);
1709 valuetype = blobmsg_get_u32(tb[6]);
1710 len = blobmsg_get_u32(tb[7]);
1711 buf = blobmsg_get_string(tb[8]);
1712 index = blobmsg_get_u32(tb[9]);
1713 flag = blobmsg_get_u32(tb[10]);
1714
1715
1716
1717 if(lwm2m_context.config==NULL
1718 || lwm2m_context.ref != ref){
1719 result = LWM2MRC_FAIL;
1720 goto out;
1721 }
1722
1723
1724 lwm2m_printf("lwm2m_read_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1725 if(res!=MIPLRS_205_CONTENT){
1726 if(index!=0){
1727 cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_READ);
1728 }else{
1729 result=RESULT_TO_RESPONSE(res);
1730 cis_response(context,NULL,NULL,msgId,result);
1731 }
1732 }else{
1733 uri.objectId = objectid;
1734 uri.instanceId = instanceid;
1735 uri.resourceId = resourceid;
1736
1737 if(valuetype == cis_data_type_bool){
1738 tmpdata.type = cis_data_type_bool;
1739 tmpdata.value.asBoolean = lwm2m_get_boolean(buf);
1740 }else if(valuetype == cis_data_type_integer){
1741 tmpdata.type = cis_data_type_integer;
1742 tmpdata.value.asInteger = atoi(buf);
1743 }else if(valuetype == cis_data_type_float){
1744 tmpdata.type = cis_data_type_float;
1745 tmpdata.value.asFloat = atof(buf);
1746 }else if(valuetype == cis_data_type_string){
1747 tmpdata.type = cis_data_type_string;
1748 tmpdata.asBuffer.length = len;
1749 tmpdata.asBuffer.buffer = (uint8_t*)strdup(buf);
1750 }else{
1751 //need check value type
1752 }
1753 if(index==0&& flag==0){
1754
1755 cis_uri_update(&uri);
1756 cis_response(context,&uri,&tmpdata,msgId,CIS_RESPONSE_READ);
1757 }else{
1758 cis_uri_update(&uri);
1759 cis_response(context,&uri,&tmpdata,msgId,CIS_RESPONSE_CONTINUE);
1760 }
1761
1762 }
1763
1764 result = LWM2MRC_SUCCESS;
1765
1766 pthread_mutex_lock(&lwm2m_context.mutex);
1767 pthread_cond_signal(&lwm2m_context.cond);
1768 pthread_mutex_unlock(&lwm2m_context.mutex);
1769
1770
1771out:
1772 memset(&b, 0, sizeof(b));
1773 blob_buf_init(&b, 0);
1774 blobmsg_add_u32(&b, "result", result);
1775
1776 ubus_send_reply(ctx, req, b.head);
1777 return 0;
1778}
1779
1780
1781static int lwm2mwritersp_method(struct ubus_context *ctx, struct ubus_object *obj,
1782 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1783{
1784 UNUSED(obj);
1785 UNUSED(msg);
1786 UNUSED(method);
1787
1788 static struct blob_buf b;
1789
1790 struct blob_attr *tb[3];
1791 int ref;
1792 int msgId;
1793 int res;
1794
1795
1796 int result;
1797
1798
1799
1800 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1801 result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
1802 if (result < 0) {
1803 result = LWM2MRC_FAIL;
1804 goto out;
1805 }
1806 ref = blobmsg_get_u32(tb[0]);
1807 msgId = blobmsg_get_u32(tb[1]);
1808 res = blobmsg_get_u32(tb[2]);
1809
1810 if(lwm2m_context.config==NULL
1811 || lwm2m_context.ref != ref){
1812 result = LWM2MRC_FAIL;
1813 goto out;
1814 }
1815
1816 st_context_t *context = (st_context_t*)g_context;
1817 lwm2m_printf("lwm2m_write_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1818 if(res!=MIPLRS_204_CHANGED){
1819 result=RESULT_TO_RESPONSE(res);
1820 cis_response(context,NULL,NULL,msgId,result);
1821 }else{
1822 cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_WRITE);
1823 }
1824
1825
1826 result = LWM2MRC_SUCCESS;
1827
1828 pthread_mutex_lock(&lwm2m_context.mutex);
1829 pthread_cond_signal(&lwm2m_context.cond);
1830 pthread_mutex_unlock(&lwm2m_context.mutex);
1831
1832
1833out:
1834 memset(&b, 0, sizeof(b));
1835 blob_buf_init(&b, 0);
1836 blobmsg_add_u32(&b, "result", result);
1837
1838 ubus_send_reply(ctx, req, b.head);
1839 return 0;
1840}
1841
1842static int lwm2mexcutersp_method(struct ubus_context *ctx, struct ubus_object *obj,
1843 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1844{
1845 UNUSED(obj);
1846 UNUSED(msg);
1847 UNUSED(method);
1848
1849 static struct blob_buf b;
1850
1851 struct blob_attr *tb[3];
1852 int ref;
1853 int msgId;
1854 int res;
1855
1856
1857 int result;
1858
1859
1860 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1861 result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
1862 if (result < 0) {
1863 result = LWM2MRC_FAIL;
1864 goto out;
1865 }
1866 ref = blobmsg_get_u32(tb[0]);
1867 msgId = blobmsg_get_u32(tb[1]);
1868 res = blobmsg_get_u32(tb[2]);
1869
1870 if(lwm2m_context.config==NULL
1871 || lwm2m_context.ref != ref){
1872 result = LWM2MRC_FAIL;
1873 goto out;
1874 }
1875
1876
1877 st_context_t *context = (st_context_t*)g_context;
1878 lwm2m_printf("lwm2m_execute_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1879 if(res!=MIPLRS_204_CHANGED){
1880 result=RESULT_TO_RESPONSE(res);
1881 cis_response(context,NULL,NULL,msgId,result);
1882 }else{
1883 cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_EXECUTE);
1884 }
1885
1886 result = LWM2MRC_SUCCESS;
1887
1888 pthread_mutex_lock(&lwm2m_context.mutex);
1889 pthread_cond_signal(&lwm2m_context.cond);
1890 pthread_mutex_unlock(&lwm2m_context.mutex);
1891
1892
1893out:
1894 memset(&b, 0, sizeof(b));
1895 blob_buf_init(&b, 0);
1896 blobmsg_add_u32(&b, "result", result);
1897
1898 ubus_send_reply(ctx, req, b.head);
1899 return 0;
1900}
1901
1902
1903static int lwm2mparameterrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
1904 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1905{
1906 UNUSED(obj);
1907 UNUSED(msg);
1908 UNUSED(method);
1909
1910 static struct blob_buf b;
1911
1912 struct blob_attr *tb[3];
1913 int ref;
1914 int msgId;
1915 int res;
1916
1917
1918 int result;
1919
1920
1921 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1922 result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
1923 if (result < 0) {
1924 result = LWM2MRC_FAIL;
1925 goto out;
1926 }
1927 ref = blobmsg_get_u32(tb[0]);
1928 msgId = blobmsg_get_u32(tb[1]);
1929 res = blobmsg_get_u32(tb[2]);
1930
1931 if(lwm2m_context.config==NULL
1932 || lwm2m_context.ref != ref){
1933 result = LWM2MRC_FAIL;
1934 goto out;
1935 }
1936
1937 st_context_t *context = (st_context_t*)g_context;
1938 lwm2m_printf("lwm2m_param_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1939 if(res!=MIPLRS_205_CONTENT){
1940 result=RESULT_TO_RESPONSE(res);
1941 cis_response(context,NULL,NULL,msgId,result);
1942 }else{
1943 cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_OBSERVE_PARAMS);
1944 }
1945
1946 result = LWM2MRC_SUCCESS;
1947
1948 pthread_mutex_lock(&lwm2m_context.mutex);
1949 pthread_cond_signal(&lwm2m_context.cond);
1950 pthread_mutex_unlock(&lwm2m_context.mutex);
1951
1952
1953out:
1954 memset(&b, 0, sizeof(b));
1955 blob_buf_init(&b, 0);
1956 blobmsg_add_u32(&b, "result", result);
1957
1958 ubus_send_reply(ctx, req, b.head);
1959 return 0;
1960}
1961
1962
1963static int lwm2mobserverrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
1964 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
1965{
1966 UNUSED(obj);
1967 UNUSED(msg);
1968 UNUSED(method);
1969
1970 static struct blob_buf b;
1971
1972 struct blob_attr *tb[3];
1973 int ref;
1974 int msgId;
1975 int res;
1976
1977
1978 int result;
1979
1980 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
1981 result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
1982 if (result < 0) {
1983 result = LWM2MRC_FAIL;
1984 goto out;
1985 }
1986 ref = blobmsg_get_u32(tb[0]);
1987 msgId = blobmsg_get_u32(tb[1]);
1988 res = blobmsg_get_u32(tb[2]);
1989
1990 if(lwm2m_context.config==NULL
1991 || lwm2m_context.ref != ref){
1992 result = LWM2MRC_FAIL;
1993 goto out;
1994 }
1995
1996
1997 st_context_t *context = (st_context_t*)g_context;
1998 lwm2m_printf("lwm2m_observe_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
1999 if(res!=MIPLRS_205_CONTENT){
2000 result=RESULT_TO_RESPONSE(res);
2001 cis_response(context,NULL,NULL,msgId,result);
2002 }else{
2003 cis_response(g_context,NULL,NULL,msgId,CIS_RESPONSE_OBSERVE);
2004 }
2005
2006 result = LWM2MRC_SUCCESS;
2007
2008 pthread_mutex_lock(&lwm2m_context.mutex);
2009 pthread_cond_signal(&lwm2m_context.cond);
2010 pthread_mutex_unlock(&lwm2m_context.mutex);
2011
2012
2013out:
2014 memset(&b, 0, sizeof(b));
2015 blob_buf_init(&b, 0);
2016 blobmsg_add_u32(&b, "result", result);
2017
2018 ubus_send_reply(ctx, req, b.head);
2019 return 0;
2020}
2021
2022
2023static int lwm2mnotify_method(struct ubus_context *ctx, struct ubus_object *obj,
2024 struct ubus_request_data *req, const char *method, struct blob_attr *msg)
2025{
2026 UNUSED(obj);
2027 UNUSED(msg);
2028 UNUSED(method);
2029
2030 static struct blob_buf b;
2031
2032 struct blob_attr *tb[11];
2033 int ref;
2034 int msgId;
2035
2036 int objectid;
2037 int instanceid;
2038 int resourceid;
2039 int valuetype;
2040 int len;
2041 char *buf;
2042 int index;
2043 int flag;
2044 int ackid;
2045
2046
2047 int result;
2048 cis_uri_t uri={0};
2049 cis_data_t dataP;
2050
2051
2052 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
2053 result = blobmsg_parse(lwm2m_notify_policy, 11, tb, blob_data(msg), blob_len(msg));
2054 if (result < 0) {
2055 result = LWM2MRC_FAIL;
2056 goto out;
2057 }
2058
2059 ref = blobmsg_get_u32(tb[0]);
2060 msgId = blobmsg_get_u32(tb[1]);
2061
2062 objectid = blobmsg_get_u32(tb[2]);
2063 instanceid = blobmsg_get_u32(tb[3]);
2064 resourceid = blobmsg_get_u32(tb[4]);
2065 valuetype = blobmsg_get_u32(tb[5]);
2066 len = blobmsg_get_u32(tb[6]);
2067 buf = blobmsg_get_string(tb[7]);
2068 index = blobmsg_get_u32(tb[8]);
2069 flag = blobmsg_get_u32(tb[9]);
2070 ackid = blobmsg_get_u32(tb[10]);
2071
2072 UNUSED(flag);
2073
2074
2075 if(lwm2m_context.config==NULL
2076 || lwm2m_context.ref != ref){
2077 result = LWM2MRC_FAIL;
2078 goto out;
2079 }
2080
2081
2082
2083
2084
2085 uri.objectId = objectid;
2086 uri.instanceId = instanceid;
2087 uri.resourceId = resourceid;
2088 uri.flag = 0;
2089 memset(&dataP, 0, sizeof(cis_data_t));
2090
2091 dataP.id = resourceid;
2092 if(valuetype == cis_data_type_bool){
2093 dataP.type = cis_data_type_bool;
2094 dataP.value.asBoolean = lwm2m_get_boolean(buf);
2095 }else if(valuetype == cis_data_type_integer){
2096 dataP.type = cis_data_type_integer;
2097 dataP.value.asInteger = atoi(buf);
2098 }else if(valuetype == cis_data_type_float){
2099 dataP.type = cis_data_type_float;
2100 dataP.value.asFloat = atof(buf);
2101 }else if(valuetype == cis_data_type_string){
2102 dataP.type = cis_data_type_string;
2103 dataP.asBuffer.length = len;
2104 dataP.asBuffer.buffer = (uint8_t*)strdup(buf);
2105 }else{
2106 //need check value type
2107 }
2108
2109 if(index==0&& ackid==0){
2110 cis_uri_update(&uri);
2111 result=cis_notify(g_context,&uri,&dataP,msgId,CIS_NOTIFY_CONTENT,false);
2112 }else{
2113 cis_uri_update(&uri);
2114 result=cis_notify(g_context,&uri,&dataP,msgId,CIS_NOTIFY_CONTINUE,false);
2115
2116 }
2117
2118 if(result != CIS_RET_OK){
2119 lwm2m_printf("%s failed:(%d)\r\n",__func__,result);
2120 result = LWM2MRC_FAIL;
2121 }else{
2122 result = LWM2MRC_SUCCESS;
2123 }
2124
2125 pthread_mutex_lock(&lwm2m_context.mutex);
2126 pthread_cond_signal(&lwm2m_context.cond);
2127 pthread_mutex_unlock(&lwm2m_context.mutex);
2128
2129
2130out:
2131 memset(&b, 0, sizeof(b));
2132 blob_buf_init(&b, 0);
2133 blobmsg_add_u32(&b, "result", result);
2134
2135 ubus_send_reply(ctx, req, b.head);
2136 return 0;
2137}
2138
2139
2140static void lwm2m_get_imei_cb(struct ubus_request *req, int type, struct blob_attr *msg)
2141{
2142 UNUSED(req);
2143 UNUSED(type);
2144 unsigned int requestid;
2145 unsigned int rilerrno;
2146 char *response_data = NULL;
2147 int response_len = 0;
2148
2149 if (rilutil_parseResponse(msg, &requestid, &rilerrno, (void *)&response_data, &response_len)) {
2150 lwm2m_printf("%s,Parse failed\r\n",__func__);
2151 return;
2152 }
2153 if (response_data && (response_len > 0)) {
2154
2155 if (rilerrno != RIL_E_SUCCESS) {
2156 lwm2m_printf("%s,Ril error %d\r\n",__func__, rilerrno);
2157 rilutil_freeResponseData(requestid, response_data, response_len);
2158 return;
2159 }
2160
2161 if (requestid == RIL_REQUEST_GET_IMEI) {
2162 lwm2m_printf("%s,len[%d],get IMEI=%s\n",__func__,response_len,response_data);
2163 if(response_len<20){
2164 memset(lwm2m_context.IMEI,0,20);
2165 memcpy(lwm2m_context.IMEI,response_data,response_len);
2166 }else{
2167 lwm2m_printf("%s,get IMEI abnormal\n",__func__);
2168 }
2169
2170
2171 }else{
2172 lwm2m_printf("%s,get requestid=%d\r\n",__func__,requestid);
2173 }
2174 rilutil_freeResponseData(requestid, response_data, response_len);
2175 } else {
2176 if (response_data) {
2177 rilutil_freeResponseData(requestid, response_data, response_len);
2178 }
2179 lwm2m_printf("%s,no response data,rilerror code %d\n", __func__,rilerrno);
2180 }
2181
2182
2183}
2184
2185
2186
2187
2188void lwm2m_get_imei()
2189{
2190 int ret;
2191 static struct blob_buf b;
2192 unsigned int ril_id=0;
2193 if(ubus_lookup_id(lwm2m_ubus_ctx,"ril",&ril_id)){
2194 lwm2m_printf("%s,ubus_lookup_id RIL failed\r\n",__func__);
2195 return;
2196 }
2197
2198 memset(&b, 0, sizeof(b));
2199 blob_buf_init(&b,0);
2200 rilutil_makeRequestBlob(&b, RIL_REQUEST_GET_IMEI, NULL, 0);
2201 ret =ubus_invoke(lwm2m_ubus_ctx, ril_id, "ril_request", b.head, lwm2m_get_imei_cb, 0,0);
2202 if(ret != 0){
2203 lwm2m_printf("lwm2m_get_imei,ubus_invoke Failed %s\r\n", ubus_strerror(ret));
2204 }
2205 return;
2206
2207}
2208
2209
2210static void lwm2m_get_imsi_cb(struct ubus_request *req, int type, struct blob_attr *msg)
2211{
2212 UNUSED(req);
2213 UNUSED(type);
2214 unsigned int requestid;
2215 unsigned int rilerrno;
2216 char *response_data = NULL;
2217 int response_len = 0;
2218
2219 if (rilutil_parseResponse(msg, &requestid, &rilerrno, (void *)&response_data, &response_len)) {
2220 lwm2m_printf("%s,Parse failed\r\n",__func__);
2221 return;
2222 }
2223 if (response_data && (response_len > 0)) {
2224
2225 if (rilerrno != RIL_E_SUCCESS) {
2226 lwm2m_printf("%s,Ril error %d\r\n",__func__, rilerrno);
2227 rilutil_freeResponseData(requestid, response_data, response_len);
2228 return;
2229 }
2230 if (requestid == RIL_REQUEST_GET_IMSI) {
2231 lwm2m_printf("%s,len[%d],get IMSI=%s\n",__func__,response_len,response_data);
2232 if(response_len<20){
2233 memset(lwm2m_context.IMSI,0,20);
2234 memcpy(lwm2m_context.IMSI,response_data,response_len);
2235 }else{
2236 lwm2m_printf("%s,get IMSI abnormal\n",__func__);
2237
2238 }
2239
2240 }else{
2241 lwm2m_printf("%s,get requestid=%d\r\n",__func__,requestid);
2242 }
2243 rilutil_freeResponseData(requestid, response_data, response_len);
2244 } else {
2245 if (response_data) {
2246 rilutil_freeResponseData(requestid, response_data, response_len);
2247 }
2248 lwm2m_printf("%s,no response data,rilerror code %d\n", __func__,rilerrno);
2249 }
2250
2251
2252}
2253
2254void lwm2m_get_imsi()
2255{
2256 int ret;
2257 static struct blob_buf b;
2258
2259 unsigned int ril_id=0;
2260 if(ubus_lookup_id(lwm2m_ubus_ctx,"ril",&ril_id)){
2261 lwm2m_printf("%s,ubus_lookup_id RIL failed\r\n",__func__);
2262 return;
2263 }
2264
2265 memset(&b, 0, sizeof(b));
2266 blob_buf_init(&b,0);
2267 rilutil_makeRequestBlob(&b, RIL_REQUEST_GET_IMSI, NULL, 0);
2268 ret =ubus_invoke(lwm2m_ubus_ctx, ril_id, "ril_request", b.head, lwm2m_get_imsi_cb, 0,0);
2269 if(ret != 0){
2270 lwm2m_printf("lwm2m_get_imei,ubus_invoke Failed %s\r\n", ubus_strerror(ret));
2271 }
2272 return;
2273}
2274
2275
2276int lwm2m_ubus_init(void)
2277{
2278
2279 int ret = 0;
2280
2281 ret = ubus_add_object(lwm2m_ubus_ctx, &lwm2m_object);
2282 if (ret){
2283 lwm2m_printf("lwm2m_main,Failed to add object: %s\r\n", ubus_strerror(ret));
2284 return -1;
2285 }
2286
2287
2288 ret = pthread_cond_init(&lwm2m_context.cond, NULL);
2289 if (ret != 0)
2290 return ret;
2291
2292 ret = pthread_mutex_init(&lwm2m_context.mutex, NULL);
2293 if (ret != 0) {
2294 pthread_cond_destroy(&lwm2m_context.cond);
2295 return ret;
2296 }
2297
2298 ret = pthread_create(&s_tid_lwm2mMainTask, NULL, lwm2m_main, NULL);
2299 if(ret < 0){
2300 lwm2m_printf("Failed to create lwm2m main thread, ret:%d\r\n",ret);
2301 return -1;
2302 }
2303
2304 lwm2m_get_imei();
2305 lwm2m_get_imsi();
2306
2307 return 0;
2308}
2309
2310int main(int argc,char **argv)
2311{
2312 UNUSED(argc);
2313 UNUSED(argv);
2314 const char *ubus_socket = NULL;
2315
2316 Lwm2mMutexCreate();
2317 init_lwm2m_context();
2318 uloop_init();
2319
2320 lwm2m_ubus_ctx = ubus_connect(ubus_socket);
2321 if(!lwm2m_ubus_ctx) {
2322 lwm2m_printf("%s: Failed to connect to ubus\r\n",__FUNCTION__);
2323 goto out;
2324 }
2325
2326 ubus_add_uloop(lwm2m_ubus_ctx);
2327
2328 int res=lwm2m_ubus_init();
2329 if(res){
2330 lwm2m_printf("%s: embms_main error!\r\n",__FUNCTION__);
2331 goto fail;
2332 }
2333 uloop_run();
2334fail:
2335 ubus_free(lwm2m_ubus_ctx);
2336out:
2337 uloop_done();
2338 return 0;
2339
2340}