blob: 1e1d5c16c5ea0ad299d4966f106ffd89ebd31c97 [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libubus.h>
#include <unistd.h>
#include <libubox/usock.h>
#include <pthread.h>
#include <sys/socket.h>
#include <rilutil.h>
#include <telephony/ril.h>
#include "../ciscore/cis_def.h"
#include "../ciscore/cis_api.h"
#include "../ciscore/cis_if_sys.h"
#include "../ciscore/cis_api.h"
#include "../ciscore/cis_internals.h"
#include "cis_sample_entry.h"
#define LWM2M_INNER_CONN_SOCKET "/tmp/lwm2m_socket.sock"
#define RESULT_TO_RESPONSE(R) \
((R) == MIPLRS_205_CONTENT ? CIS_COAP_205_CONTENT : \
((R) == MIPLRS_204_CHANGED ? CIS_COAP_204_CHANGED : \
((R) == MIPLRS_400_BAD_REQUEST ? CIS_COAP_400_BAD_REQUEST : \
((R) == MIPLRS_401_UNAUTHORIZED ? CIS_COAP_401_UNAUTHORIZED : \
((R) == MIPLRS_404_NOT_FOUND ? CIS_COAP_404_NOT_FOUND : \
((R) == MIPLRS_405_METHOD_NOT_ALLOWED ? CIS_COAP_405_METHOD_NOT_ALLOWED : \
((R) == MIPLRS_406_NOT_ACCEPTABLE ? CIS_COAP_406_NOT_ACCEPTABLE : \
CIS_COAP_503_SERVICE_UNAVAILABLE)))))))
static void* g_context = NULL;
struct ubus_context *lwm2m_ubus_ctx;
static const struct blobmsg_policy lwm2m_create_policy[] = {
[0] = {.name = "total_size", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
[2] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
[3] = {.name = "current_size", .type = BLOBMSG_TYPE_INT32},
[4] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_delete_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_addobj_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "objectId", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "instanceCount", .type = BLOBMSG_TYPE_INT32},
[3] = {.name = "instanceBitMap", .type = BLOBMSG_TYPE_STRING},
[4] = {.name = "attributeCount", .type = BLOBMSG_TYPE_INT32},
[5] = {.name = "actionCount", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_delobj_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "objectId", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_open_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "lifetime", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "timeout", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_update_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "lifetime", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "objflag", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_discoverresp_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
[3] = {.name = "length", .type = BLOBMSG_TYPE_INT32},
[4] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
};
static const struct blobmsg_policy lwm2m_readrsp_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
[3] = {.name = "objectid", .type = BLOBMSG_TYPE_INT32},
[4] = {.name = "instanceid", .type = BLOBMSG_TYPE_INT32},
[5] = {.name = "resourceid", .type = BLOBMSG_TYPE_INT32},
[6] = {.name = "valuetype", .type = BLOBMSG_TYPE_INT32},
[7] = {.name = "len", .type = BLOBMSG_TYPE_INT32},
[8] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
[9] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
[10] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_writersp_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "result", .type = BLOBMSG_TYPE_INT32},
};
static const struct blobmsg_policy lwm2m_notify_policy[] = {
[0] = {.name = "ref", .type = BLOBMSG_TYPE_INT32},
[1] = {.name = "msgId", .type = BLOBMSG_TYPE_INT32},
[2] = {.name = "objectid", .type = BLOBMSG_TYPE_INT32},
[3] = {.name = "instanceid", .type = BLOBMSG_TYPE_INT32},
[4] = {.name = "resourceid", .type = BLOBMSG_TYPE_INT32},
[5] = {.name = "valuetype", .type = BLOBMSG_TYPE_INT32},
[6] = {.name = "len", .type = BLOBMSG_TYPE_INT32},
[7] = {.name = "context", .type = BLOBMSG_TYPE_STRING},
[8] = {.name = "index", .type = BLOBMSG_TYPE_INT32},
[9] = {.name = "flag", .type = BLOBMSG_TYPE_INT32},
[10] = {.name = "ackid", .type = BLOBMSG_TYPE_INT32},
};
static int lwm2mgetversion_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mcreate_default_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mcreate_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mdelete_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2maddobj_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mdelobj_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mopen_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mupdate_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mclose_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mdiscoverresp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mreadrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mwritersp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mexcutersp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mparameterrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mobserverrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
static int lwm2mnotify_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg);
//static void lwm2m_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj);
int lwm2m_invoke_reply(struct ubus_context *ctx, const char *service,
const char *method, struct blob_attr *msg,int timeout);
void init_lwm2m_context();
cis_ret_t lwm2m_on_read(void* context,cis_uri_t* uri,cis_mid_t mid);
cis_ret_t lwm2m_on_discover(void* context,cis_uri_t* uri,cis_mid_t mid);
cis_ret_t lwm2m_on_write(void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t attrcount,cis_mid_t mid);
cis_ret_t lwm2m_on_exec(void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid);
cis_ret_t lwm2m_on_observe(void* context,cis_uri_t* uri,BOOL flag,cis_mid_t mid);
cis_ret_t lwm2m_on_params(void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid);
cis_ret_t lwm2m_on_params(void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid);
void lwm2m_on_event(void* context,cis_evt_t eid,void* param);
int32_t lwm2m_init();
void lwm2m_deinit();
int32_t parase_resource_from_value(char *value,int index);
int32_t get_resource_count_from_value(char *value);
void lwm2m_pump();
int lwm2m_ubus_init(void);
void lwm2m_get_imsi();
void lwm2m_get_imei();
static const struct ubus_method lwm2m_methods[] = {
UBUS_METHOD_NOARG("version", lwm2mgetversion_method),
UBUS_METHOD_NOARG("createdefault", lwm2mcreate_default_method),
UBUS_METHOD("create", lwm2mcreate_method, lwm2m_create_policy),
UBUS_METHOD("delete", lwm2mdelete_method, lwm2m_delete_policy),
UBUS_METHOD("addobj", lwm2maddobj_method, lwm2m_addobj_policy),
UBUS_METHOD("delobj", lwm2mdelobj_method, lwm2m_delobj_policy),
UBUS_METHOD("open", lwm2mopen_method, lwm2m_open_policy),
UBUS_METHOD("update", lwm2mupdate_method, lwm2m_update_policy),
UBUS_METHOD("close", lwm2mclose_method, lwm2m_delete_policy),
UBUS_METHOD("discoverresp", lwm2mdiscoverresp_method, lwm2m_discoverresp_policy),
UBUS_METHOD("readrsp", lwm2mreadrsp_method, lwm2m_readrsp_policy),
UBUS_METHOD("writersp", lwm2mwritersp_method, lwm2m_writersp_policy),
UBUS_METHOD("excutersp", lwm2mexcutersp_method, lwm2m_writersp_policy),
UBUS_METHOD("parameterrsp", lwm2mparameterrsp_method, lwm2m_writersp_policy),
UBUS_METHOD("observerrsp", lwm2mobserverrsp_method, lwm2m_writersp_policy),
UBUS_METHOD("notify", lwm2mnotify_method, lwm2m_notify_policy),
};
static struct ubus_object_type lwm2m_object_type = UBUS_OBJECT_TYPE("lwm2m", lwm2m_methods);
static struct ubus_object lwm2m_object = {
.name = "lwm2m",
.type = &lwm2m_object_type,
.methods = lwm2m_methods,
.n_methods = ARRAY_SIZE(lwm2m_methods),
};
//static struct ubus_subscriber lwm2m_ind_event;
static pthread_t s_tid_lwm2mMainTask;
static int lwm2m_client_fd=-1;
/*
static struct ubus_object_type lwm2m_notify_object_type;
static struct ubus_object lwm2m_notify_obj[1] =
{
{.name = "lwm2m_ind", .type = &lwm2m_notify_object_type, .subscribe_cb = lwm2m_subscribe_cb},
};
*/
static const uint8_t config_hex[] = {0x13,0x00,0x3b,
0xf1,0x00,0x03,
0xf2,0x00,0x29,0x05,0x00/*mtu*/,0x11/*Link&bind type*/,0x80/*BS ENABLED*/,0x00,0x00/*vpn*/,0x00,0x00/*username*/,0x00,0x00/*password*/,
0x00,0x14/*host length*/,0x6e,0x62,0x69,0x6f,0x74,0x62,0x74,0x2e,0x68,0x65,0x63,0x6c,0x6f,0x75,0x64,0x73,0x2e,0x63,
0x6f,0x6d,0x00,0x04,0x4e,0x55,0x4c,0x4c,0xf3,0x00,0x0c,0xea,0x04,0x00,0x00,0x04,0x4e,0x55,0x4c,0x4c};
struct miplContext lwm2m_context = {0};
static INT32 lwm2m_clear_register_flag();
extern void *lwm2m_recv_thread(void* argv);
/*
static void lwm2m_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
{
UNUSED(ctx);
lwm2m_printf("lwm2m Subscribers active: %d\n", obj->has_subscribers);
//RIL_onNewConnect();
}
*/
int utils_findDataType(cis_data_t * dataP)
{
int res = dataP->type;
LOGD("resource id is %d",dataP->id);
switch(dataP->id){
//boolean
case digital_input_state:
case digital_input_polarity:
case increase_input_state:
case decrease_input_state:
case digital_state:
case digital_output_state:
case digital_output_polarity:
case on_off:
res = cis_data_type_bool;
utils_opaqueToInt(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asInteger);
break;
//integer
case digital_input_counter:
case digital_input_debounce:
case digital_input_edge_selection:
case mode:
case x_coordinate:
case y_coordinate:
case counter:
case up_counter:
case down_counter:
case max_x_coordinate:
case max_y_coordinate:
case multi_state_input:
case criticality_level:
case duty_cycle:
case dimmer:
case on_time:
case off_time:
case busy_to_clear_delay:
case clear_to_busy_delay:
case bitmap_input:
res = cis_data_type_integer;
utils_opaqueToInt(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asInteger);
break;
//float
case fractional_time:
case min_x_value:
case max_x_value:
case min_y_value:
case max_y_value:
case min_z_value:
case max_z_value:
case min_limit:
case max_limit:
case delay_duration:
case duration:
case minimum_off_time:
case contrast:
case current_position:
case transition_time:
case remaining_time:
case cumulative_time:
case level:
case analog_input_state:
case min_measured_value:
case max_measured_value:
case min_range_value:
case max_range_value:
case analog_output_current_value:
case sensor_value:
case x_value:
case y_value:
case z_value:
case compass_direction:
case instantaneous_active_power:
case min_measured_active_power:
case max_measured_active_power:
case min_range_active_power:
case max_range_active_power:
case cumulative_active_power:
case active_power_calibration:
case instantaneous_reactive_power:
case min_measured_reactive_power:
case max_measured_reactive_power:
case min_range_reactive_power:
case max_range_reactive_power:
case cumulative_reactive_power:
case reactive_power_calibration:
case power_factor:
case current_calibration:
case start_time:
case duration_in_min:
case set_point_value:
res = cis_data_type_float;
utils_opaqueToFloat(dataP->asBuffer.buffer,dataP->asBuffer.length,&dataP->value.asFloat);
break;
//string
case latitude:
case longitude:
case uncertainty:
case text:
case sensor_units:
case colour:
case application_type:
case sensor_type:
case event_identifier:
case avg_load_adj_pct:
case muti_state_output:
case element_description:
case uuid:
res = cis_data_type_string;
break;
default:
break;
}
return res;
}
void init_lwm2m_context()
{
memset(&lwm2m_context,0,sizeof(struct miplContext));
}
cis_ret_t lwm2m_on_read (void* context,cis_uri_t* uri,cis_mid_t mid)
{
UNUSED(context);
int ref=0;
int msgid=0;
int objectid=0;
int instanceid=0;
int resourceid=0;
int res;
lwm2m_printf("lwm2m_on_read:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
if(CIS_URI_IS_SET_RESOURCE(uri)){
ref = 0;
msgid = mid;
objectid = uri->objectId;
instanceid = uri->instanceId;
resourceid = uri->resourceId;
}else if(!CIS_URI_IS_SET_RESOURCE(uri)&& CIS_URI_IS_SET_INSTANCE(uri)){
ref = 0;
msgid = mid;
objectid = uri->objectId;
instanceid = uri->instanceId;
resourceid = -1;
}else if(!CIS_URI_IS_SET_RESOURCE(uri)&& !CIS_URI_IS_SET_INSTANCE(uri)){
ref = 0;
msgid = mid;
objectid = uri->objectId;
instanceid = -1;
resourceid = -1;
}
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
res=snprintf(msg.msgData, 256, "+MIPLREAD: %d,%d,%d,%d,%d",
ref,msgid,objectid,instanceid,resourceid);
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
return CIS_CALLBACK_CONFORM;
}
cis_ret_t lwm2m_on_discover (void* context,cis_uri_t* uri,cis_mid_t mid)
{
int res;
UNUSED(context);
if(CIS_URI_IS_SET_RESOURCE(uri)){
lwm2m_printf("lwm2m_on_discover:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
}
else if(CIS_URI_IS_SET_INSTANCE(uri)){
lwm2m_printf("lwm2m_on_discover:(%d/%d)\r\n",uri->objectId,uri->instanceId);
}
else{
lwm2m_printf("lwm2m_on_discover:(%d)\r\n",uri->objectId);
}
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
res=snprintf(msg.msgData, 256, "+MIPLDISCOVER: %d,%d,%d",
0,mid,uri->objectId);
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
return CIS_CALLBACK_CONFORM;
}
cis_ret_t lwm2m_on_write (void* context,cis_uri_t* uri,const cis_data_t* value,cis_attrcount_t attrcount,cis_mid_t mid)
{
UNUSED(context);
int i,j;
const cis_data_t* temp_value = NULL;
int ref=0;
int msgid=0;
int objectid=0;
int instanceid=0;
int resourceid=0;
int valuetype=0;
int index=0;
int flag=0;
int length=0;
char buf[512] ={0};
int res;
if(CIS_URI_IS_SET_RESOURCE(uri)){
lwm2m_printf("lwm2m_on_write:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
}
else{
lwm2m_printf("lwm2m_on_write:(%d/%d)\r\n",uri->objectId,uri->instanceId);
}
int temp_index = attrcount;
lwm2m_printf("lwm2m_on_write: attrcount(%d)\r\n",attrcount);
for(i = 0;i<attrcount;i++)
{
temp_value = value+i;
ref = 0;
msgid = mid;
objectid = uri->objectId;
instanceid = uri->instanceId;
resourceid = temp_value->id;
valuetype = temp_value->type;
index = temp_index-1;
if(index==0){
flag=0;
}else if(i==0){
flag=1;
}else{
flag=2;
}
lwm2m_printf("lwm2m_on_write: valuetype(%d)\r\n",valuetype);
/*
res = writeInd->valuetype;
if(writeInd->valuetype == cis_data_type_opaque){
lwm2m_printf("lwm2m_on_write: length(%d)\n",temp_value->asBuffer.length);
LOG_BUF("buffer",temp_value->asBuffer.buffer,temp_value->asBuffer.length);
res=utils_findDataType(temp_value);
}
lwm2m_printf("lwm2m_on_write: res(%d)\n",res);
*/
if(temp_value->type == cis_data_type_string ){
//lwm2m_printf("lwm2m_on_write: length(%d)\n",temp_value->asBuffer.length);
length = temp_value->asBuffer.length;
//lwm2m_printf("lwm2m_on_write: value(%s)\n",temp_value->asBuffer.buffer);
//char *temp=value->value.asChildren.array[i].asBuffer.buffer;
//LOG_BUF("buffer",temp,writeInd->length+1);
cissys_memcpy(buf,temp_value->asBuffer.buffer,length);
}else if(temp_value->type == cis_data_type_integer){
int size;
//temp_value->value.asInteger = 0;
//cissys_memcpy(&temp_value->value.asInteger,temp_value->asBuffer.buffer,temp_value->asBuffer.length);
size = snprintf(buf, 512, "%lld",temp_value->value.asInteger);
length = size;
}else if(temp_value->type == cis_data_type_float){
int size;
size = snprintf(buf, 512, "%f",temp_value->value.asFloat);
length = size;
}else if(temp_value->type == cis_data_type_bool){
int size;
lwm2m_printf("lwm2m_on_write: value(%lld)\r\n",temp_value->value.asInteger);
size = snprintf(buf, 512, "%lld",temp_value->value.asInteger);
length = size;
}else if(temp_value->type == cis_data_type_opaque){
lwm2m_printf("lwm2m_on_write: length(%d)\r\n",temp_value->asBuffer.length);
length = temp_value->asBuffer.length;
uint8_t *temp=temp_value->asBuffer.buffer;
LOG_BUF("buffer",temp,length);
for(j=0;j<length;j++)
{
snprintf(buf+j,512,"%02d",*(temp+j));
}
}
if(valuetype != cis_data_type_undefine){
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(512);
memset(msg.msgData,0,512);
res=snprintf(msg.msgData, 512, "+MIPLWRITE: %d,%d,%d,%d,%d,%d,%d,%s,%d,%d",
ref,msgid,objectid,instanceid,resourceid,
valuetype,length,buf,flag,index);
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
}
temp_index --;
}
return CIS_CALLBACK_CONFORM;
}
cis_ret_t lwm2m_on_exec (void* context,cis_uri_t* uri,const uint8_t* value,uint32_t length,cis_mid_t mid)
{
UNUSED(context);
int res;
if(CIS_URI_IS_SET_RESOURCE(uri)){
lwm2m_printf("lwm2m_on_exec:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
}
else{
return CIS_CALLBACK_METHOD_NOT_ALLOWED;
}
if(!CIS_URI_IS_SET_INSTANCE(uri))
{
return CIS_CALLBACK_BAD_REQUEST;
}
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
if(length==0){
res=snprintf(msg.msgData, 256, "+MIPLEXECUTE: %d,%d,%d,%d,%d",
0,mid,uri->objectId,uri->instanceId,uri->resourceId);
}else{
res=snprintf(msg.msgData, 256, "+MIPLEXECUTE: %d,%d,%d,%d,%d,%d,\"%s\"",
0,mid,uri->objectId,uri->instanceId,uri->resourceId,length,value);
}
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
return CIS_CALLBACK_CONFORM;
}
cis_ret_t lwm2m_on_observe (void* context,cis_uri_t* uri,BOOL flag,cis_mid_t mid)
{
UNUSED(context);
int flag_value;
int resourceid;
int instanceid;
int res;
if(flag){
LOGD("cis_on_observe set: %d/%d/%d\n",
uri->objectId,
CIS_URI_IS_SET_INSTANCE(uri)?uri->instanceId:-1,
CIS_URI_IS_SET_RESOURCE(uri)?uri->resourceId:-1);
}else{
LOGD("cis_on_observe cancel: %d/%d/%d\n",
uri->objectId,
CIS_URI_IS_SET_INSTANCE(uri)?uri->instanceId:-1,
CIS_URI_IS_SET_RESOURCE(uri)?uri->resourceId:-1);
}
if(flag){
flag_value = 1;
}else{
flag_value = 0;
}
LOGD("cis_on_observe mid: %d objectid: %d\n",mid,uri->objectId);
if(CIS_URI_IS_SET_RESOURCE(uri)){
resourceid = uri->resourceId;
}else{
resourceid =-1;
}
if(CIS_URI_IS_SET_INSTANCE(uri)){
instanceid = uri->instanceId;
}else{
instanceid =-1;
}
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
res=snprintf(msg.msgData, 256, "+MIPLOBSERVE: %d,%d,%d,%d,%d,%d",
0,mid,flag_value,uri->objectId,instanceid,resourceid);
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
return CIS_CALLBACK_CONFORM;
}
cis_ret_t lwm2m_on_params (void* context,cis_uri_t* uri,cis_observe_attr_t parameters,cis_mid_t mid)
{
UNUSED(context);
char value[128]={0};
int res;
if(CIS_URI_IS_SET_RESOURCE(uri)){
lwm2m_printf("cis_on_params:(%d/%d/%d)\r\n",uri->objectId,uri->instanceId,uri->resourceId);
}
else{
return CIS_CALLBACK_METHOD_NOT_ALLOWED;
}
if(!CIS_URI_IS_SET_INSTANCE(uri))
{
return CIS_CALLBACK_BAD_REQUEST;
}
int size;
size = snprintf(value, 512, "pmin=%d;pmax=%d;lt=%f;gt=%f;stp=%f",
parameters.minPeriod, parameters.maxPeriod,parameters.lessThan,parameters.greaterThan,parameters.step);
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
res=snprintf(msg.msgData, 256, "+MIPLPARAMETER: %d,%d,%d,%d,%d,%d,\"%s\"",
0,mid,uri->objectId,uri->instanceId,uri->resourceId,size,value);
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
return CIS_CALLBACK_CONFORM;
}
void lwm2m_on_event (void* context,cis_evt_t eid,void* param)
{
int value;
int res;
lwm2m_printf("cis_on_event(%d):%s\r\n",eid,STR_EVENT_CODE(eid));
switch(eid)
{
case CIS_EVENT_RESPONSE_FAILED:
lwm2m_printf("cis_on_event response failed mid:%d\r\n",(int32_t)param);
break;
case CIS_EVENT_NOTIFY_FAILED:
lwm2m_printf("cis_on_event notify failed mid:%d\r\n",(int32_t)param);
break;
case CIS_EVENT_UPDATE_NEED:
lwm2m_printf("cis_on_event need to update,reserve time:%ds\r\n",(int32_t)param);
cis_update_reg(context,LIFETIME_INVALID,false);
break;
case CIS_EVENT_REG_SUCCESS:
{
/*
struct st_observe_info* delnode;
while(g_observeList != NULL){
g_observeList =(st_observe_info *)CIS_LIST_RM((cis_list_t *)g_observeList,g_observeList->mid,(cis_list_t **)&delnode);
free(delnode);
}
*/
}
break;
case CIS_EVENT_UNREG_DONE:
lwm2m_printf("cis_on_event close done\r\n");
lwm2m_clear_register_flag();
break;
default:
break;
}
struct lwm2m_usock_head msg;
msg.msgId = 0x2795;
msg.msgData = (char *)malloc(256);
memset(msg.msgData,0,256);
value = (int)param;
if(param){
res=snprintf(msg.msgData, 256, "+MIPLEVENT: %d,%d,%d",
0,eid,value);
}else{
res=snprintf(msg.msgData, 256, "+MIPLEVENT: %d,%d",
0,eid);
}
msg.msgLen = res;
if(lwm2m_client_fd){
write(lwm2m_client_fd, &msg, sizeof(struct lwm2m_usock_head));
write(lwm2m_client_fd, msg.msgData, msg.msgLen);
}
free(msg.msgData);
}
int32_t lwm2m_init()
{
cis_version_t ver;
lwm2m_context.g_lifetime = 20;
/*init sample data*/
//prv_make_sample_data();
cis_version(&ver);
lwm2m_printf("CIS SDK Version:%u.%u.%u\r\n",ver.major,ver.minor,ver.micro);
if(cis_init(&g_context,lwm2m_context.config,lwm2m_context.config_size,NULL,NULL) != CIS_RET_OK){
lwm2m_printf("%s init failed.\r\n",__func__);
return LWM2MRC_FAIL;
}
return LWM2MRC_SUCCESS;
}
void lwm2m_deinit()
{
cis_deinit(&g_context);
}
static int32_t lwm2m_clear_register_flag()
{
lwm2m_context.register_flag = 0;
return LWM2MRC_SUCCESS;
}
static int lwm2m_get_boolean(const char * value)
{
if (value) {
if (!strcmp(value, "true")) {
return 1;
} else if (!strcmp(value, "on")) {
return 1;
} else if (!strcmp(value, "1")) {
return 1;
} else if (!strcmp(value, "yes")) {
return 1;
}
}
return 0;
}
int32_t parase_resource_from_value(char *value,int index)
{
int i,len;
char *p=NULL;
char *q=NULL;
char *t=NULL;
if(value==NULL){
return -1;
}
len=strlen(value);
if(len<=0){
return -1;
}
q=(char *)malloc(len+10);
t=q;
memset(q,0,len+10);
memcpy(q,value,len);
int res=-1;
for(i=0;i<index;i++){
p= strstr(q,";");
if(p==NULL && (i+1)==index){
res = atoi(q);
goto out;
}else if(p!=NULL && (i+1)==index){
*p = 0;
res = atoi(q);
goto out;
}else if(p!=NULL && (i+1)!=index){
q=p+1;
}
}
out:
free(t);
return res;
}
int32_t get_resource_count_from_value(char *value)
{
int i,len;
int count=1;
if(value==NULL){
return 0;
}
len=strlen(value);
if(len<=0){
return 0;
}
for(i=0;i<len;i++){
if(*(value+i)==';'){
count++;
}
}
return count;
}
/*
static BOOL lwm2m_getInstId(uint8_t* inst_bitmap,uint8_t index,cis_iid_t* iid)
{
if (inst_bitmap == NULL)
{
return 0;
}
uint8_t bytes = index / 8;
uint8_t bits = index % 8;
if ((*(inst_bitmap + bytes) >> (7 - bits)) & 0x01) {
if(iid != NULL)*iid = index;
return true;
}
return false;
}
*/
void lwm2m_pump()
{
//int i = 0;
/*pump function*/
cis_pump(g_context);
/*update lifetime*/
/*
uint32_t nowtime = cissys_gettime();
if(nowtime - g_lifetime_last > ((g_lifetime-5) * 1000)){
g_lifetime_last = cissys_gettime();
cis_update_reg(g_context,LIFETIME_INVALID,false);
}
if(g_callback.flag!=0){
if((nowtime - g_callback.time)*200 > 10){
prv_reset_callback_info();
}
}
*/
}
static void * lwm2m_main(void * param)
{
UNUSED(param);
struct timespec abstimeout;
int ret;
uint32_t pumpRet;
/*update lifetime*/
struct timeval nowtime ;
uint32_t count;
while(1){
pthread_mutex_lock(&lwm2m_context.mutex);
if(lwm2m_context.register_flag){
/*pump function*/
pumpRet = cis_pump(g_context);
if(pumpRet == PUMP_RET_NOSLEEP)
{
lwm2m_printf("%s,pump no sleep\r\n",__func__);
//memset(&apiMsg,0,sizeof(struct miApiMsg));
pthread_mutex_unlock(&lwm2m_context.mutex);
continue;
}else if(pumpRet == PUMP_RET_TRANSSLEEP){
gettimeofday(&nowtime, NULL);
lwm2m_printf("%s,[%d]nowtime.tv_sec[%ld]\r\n",__func__,
lwm2m_context.register_flag,
nowtime.tv_sec);
//timeout = 200;
abstimeout.tv_sec=nowtime.tv_sec+1;
abstimeout.tv_nsec = nowtime.tv_usec * 1000;
}else{
gettimeofday(&nowtime, NULL);
count=(uint32_t)(nowtime.tv_sec - lwm2m_context.g_lifetime_last.tv_sec);
if(count >= (lwm2m_context.g_lifetime-5)){
//lwm2m_context.g_lifetime_last = cissys_gettime();
gettimeofday(&lwm2m_context.g_lifetime_last, NULL);
cis_update_reg(g_context,LIFETIME_INVALID,false);
//abstimeout.tv_sec = nowtime.tv_sec+5;
//abstimeout.tv_nsec = nowtime.tv_usec * 1000;
//timeout=(lwm2m_context.g_lifetime -5)*200;
//memset(&apiMsg,0,sizeof(struct miApiMsg));
pthread_mutex_unlock(&lwm2m_context.mutex);
continue;
}else{
abstimeout.tv_sec=nowtime.tv_sec+((lwm2m_context.g_lifetime_last.tv_sec+lwm2m_context.g_lifetime-5) - nowtime.tv_sec);
abstimeout.tv_nsec = nowtime.tv_usec * 1000;
//timeout= (lwm2m_context.g_lifetime-5) * 1000 - (nowtime - lwm2m_context.g_lifetime_last);
//if(timeout/5){
// timeout = timeout/5;
//}else{
// timeout = 1;
//}
}
}
}
if (lwm2m_context.register_flag){
lwm2m_printf("%s,[%d]abstimeout.tv_sec[%ld]\r\n",__func__,
lwm2m_context.register_flag,
abstimeout.tv_sec);
ret = pthread_cond_timedwait(&lwm2m_context.cond, &lwm2m_context.mutex, &abstimeout);
if (ret == ETIMEDOUT) {
lwm2m_printf("%s,pthread_cond_timedwait timeout\r\n",__func__);
}
}else{
pthread_cond_wait(&lwm2m_context.cond, &lwm2m_context.mutex);
}
pthread_mutex_unlock(&lwm2m_context.mutex);
}
return NULL;
}
static int lwm2mgetversion_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
char version[64]={0};
/*
int ret;
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "event", "test");
ret=ubus_notify(lwm2m_ubus_ctx, &lwm2m_object_type, "lwm2m", b.head, -1);
if (ret)
lwm2m_printf("%s: ubus notify error: ret=%d\n", __FUNCTION__, ret);
ubus_notify_async
blobmsg_buf_init(&wifi_buf);
blobmsg_add_u32(&wifi_buf, "id", SSID_CHANGE_NOTIFY_ID);
blobmsg_add_string(&wifi_buf, "new_ssid", new_ssid);
if (ubus_notify_async(ctx, &wifi_service_object, "ssid_change", wifi_buf.head, &nreq))
return 0;
blob_buf_free(&wifi_buf);
nreq.complete_cb = (void *)&ssid_change_notify_complete_cb;
ubus_complete_request(ctx, &nreq.req, 100);
*/
cis_version_t ver;
cis_version(&ver);
snprintf(version,64,"%u.%u.%u",ver.major,ver.minor,ver.micro);
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_string(&b, "version", version);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mcreate_default_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
int len;
static struct blob_buf b;
unsigned long result;
int retry=5;
len=sizeof(config_hex)+1;
lwm2m_context.config = (CHAR *)malloc(len);
memset(lwm2m_context.config,0,len);
memcpy(lwm2m_context.config,config_hex,sizeof(config_hex));
lwm2m_context.config_size = sizeof(config_hex);
result = lwm2m_init();
if(result!=LWM2MRC_SUCCESS){
lwm2m_printf("lwm2m_init failed\r\n");
}else{
start:
if(lwm2m_client_fd<0){
lwm2m_client_fd = usock(USOCK_UNIX, LWM2M_INNER_CONN_SOCKET, NULL);
if (lwm2m_client_fd < 0) {
if(retry){
sleep(1);
retry--;
goto start;
}
lwm2m_printf("%s,Failed to connect to server\r\n",__FUNCTION__);
result = LWM2MRC_FAIL;
}
}
}
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mcreate_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[5];
int total_size=0;
char * context=NULL;
int index=0;
int current_size=0;
int flag=0;
int result;
int ref=0;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_create_policy, 5, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
total_size = blobmsg_get_u32(tb[0]);
context = blobmsg_get_string(tb[1]);
index = blobmsg_get_u32(tb[2]);
current_size = blobmsg_get_u32(tb[3]);
flag = blobmsg_get_u32(tb[4]);
lwm2m_printf("lwm2mcreate_method total_size[%d] index[%d] current_size[%d] flag[%d]\r\n",
total_size,
index,
current_size,
flag);
if(flag == 1){
if(lwm2m_context.config){
free(lwm2m_context.config);
lwm2m_context.config =NULL;
}
lwm2m_context.config = (CHAR *)malloc(total_size + 10);
memset(lwm2m_context.config,0,total_size +10);
if(current_size > total_size) lwm2m_assert(0);
memcpy(lwm2m_context.config,context,current_size);
lwm2m_context.config_size = current_size;
}else{
if(lwm2m_context.config==NULL)lwm2m_assert(0);
if((lwm2m_context.config_size + current_size) > total_size)lwm2m_assert(0);
memcpy(lwm2m_context.config+lwm2m_context.config_size,context,current_size);
lwm2m_context.config_size +=current_size;
}
result = LWM2MRC_SUCCESS;
lwm2m_context.config_flag = flag;
ref = 1;
if(flag==0){
result=lwm2m_init();
if(result == LWM2MRC_SUCCESS){
ref = 0;
}
}
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
blobmsg_add_u32(&b, "ref", ref);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mdelete_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[1];
int result;
int ref;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_delete_policy, 1, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
if(lwm2m_context.config && lwm2m_context.ref == ref){
free(lwm2m_context.config);
lwm2m_context.config =NULL;
lwm2m_deinit();
result = LWM2MRC_SUCCESS;
}else{
result = LWM2MRC_FAIL;
}
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2maddobj_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[6];
int ref;
int objectId;
int instanceCount;
char * instanceBitMap;
int attributeCount;
int actionCount;
int result;
cis_ret_t ret;
cis_res_count_t rescount;
cis_inst_bitmap_t bitmap;
uint16_t instCount,instBytes;
const char* instAsciiPtr;
uint8_t * instPtr;
cis_oid_t oid;
uint16_t i;
lwm2m_printf("entry lwm2maddobj_method\r\n");
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_addobj_policy, 6, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
objectId = blobmsg_get_u32(tb[1]);
instanceCount = blobmsg_get_u32(tb[2]);
instanceBitMap = blobmsg_get_string(tb[3]);
attributeCount = blobmsg_get_u32(tb[4]);
actionCount = blobmsg_get_u32(tb[5]);
if(lwm2m_context.config==NULL || lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
oid = objectId;
//instCount = strlen(instanceBitMap);
instCount = instanceCount;
instBytes = (instCount - 1) / 8 + 1;
instAsciiPtr = instanceBitMap;
lwm2m_printf("instCount[%d],instBytes[%d]\r\n",instCount,instBytes);
instPtr = (uint8_t*)cis_malloc(instBytes);
memset(instPtr,0,instBytes);
for (i = 0;i < instCount;i++)
{
uint8_t instBytePos = i / 8;
uint8_t instByteOffset = 7 - (i % 8);
if(instAsciiPtr[i] == '1'){
instPtr[instBytePos] += 0x01 << instByteOffset;
}
}
rescount.attrCount = attributeCount;
//resconfig.actionCount = addobj_req->instancecount;
rescount.actCount = actionCount;
bitmap.instanceCount = instCount;
bitmap.instanceBitmap = instPtr;
bitmap.instanceBytes = instBytes;
lwm2m_printf("attrCount[%d],actCount[%d],instanceCount[%d],instanceBytes[%d]\r\n",
rescount.attrCount,rescount.actCount,bitmap.instanceCount,bitmap.instanceBytes);
ret = cis_addobject(g_context,oid,&bitmap,&rescount);
cis_free(instPtr);
if(ret == CIS_RET_NO_ERROR){
result = LWM2MRC_SUCCESS;
}else{
result = LWM2MRC_FAIL;
}
lwm2m_printf("lwm2maddobj_method finish\r\n");
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mdelobj_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[2];
int ref;
int objectId;
int result;
cis_ret_t ret;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_delobj_policy, 2, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
objectId = blobmsg_get_u32(tb[1]);
if(lwm2m_context.config==NULL || lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
ret = cis_delobject(g_context,objectId);
if(ret == CIS_RET_NO_ERROR){
return LWM2MRC_SUCCESS;
}else{
return LWM2MRC_FAIL;
}
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mopen_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int lifetime;
int timeout;
int result;
cis_ret_t ret;
lwm2m_printf("%s\r\n",__func__);
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_open_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
lifetime = blobmsg_get_u32(tb[1]);
timeout = blobmsg_get_u32(tb[2]);
UNUSED(timeout);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref
|| lwm2m_context.config_flag!=0){
result = LWM2MRC_FAIL;
goto out;
}
cis_callback_t callback;
callback.onRead = (cis_read_callback_t)lwm2m_on_read;
callback.onWrite = (cis_write_callback_t)lwm2m_on_write;
callback.onExec = (cis_exec_callback_t)lwm2m_on_exec;
callback.onObserve = (cis_observe_callback_t)lwm2m_on_observe;
callback.onSetParams = (cis_set_params_callback_t)lwm2m_on_params;
callback.onEvent = (cis_event_callback_t)lwm2m_on_event;
callback.onDiscover = (cis_discover_callback_t)lwm2m_on_discover;
//lwm2m_context.g_lifetime_last = cissys_gettime();
gettimeofday(&lwm2m_context.g_lifetime_last, NULL);
ret = cis_register(g_context,lifetime,&callback);
if(ret!=CIS_RET_OK){
lwm2m_printf("%s register failed.\r\n",__func__);
result = LWM2MRC_FAIL;
goto out;
}
lwm2m_context.register_flag = 1;
result = LWM2MRC_SUCCESS;
lwm2m_printf("%s,register_flag success\r\n",__func__);
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mupdate_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int lifetime;
int objflag;
int result;
cis_ret_t ret;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_update_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
lifetime = blobmsg_get_u32(tb[1]);
objflag = blobmsg_get_u32(tb[2]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
ret=cis_update_reg(g_context,lifetime,objflag);
if(ret!=COAP_NO_ERROR){
result = LWM2MRC_FAIL;
goto out;
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mclose_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[1];
int ref;
int result;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_delete_policy, 1, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
cis_unregister(g_context);
result = LWM2MRC_SUCCESS;
lwm2m_context.register_flag = 0;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mdiscoverresp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[5];
int ref=0;
int msgId=0;
int res=0;
int length=0;
char *buf=NULL;
int result=0;
int count=0;
int i;
cis_uri_t temp_uri;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_discoverresp_policy, 5, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
length = blobmsg_get_u32(tb[3]);
buf = blobmsg_get_string(tb[4]);
UNUSED(length);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
lwm2m_printf("lwm2m_discover_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_205_CONTENT){
result=RESULT_TO_RESPONSE(res);
cis_response(g_context,NULL,NULL,msgId,result);
}else{
count=get_resource_count_from_value(buf);
lwm2m_printf("lwm2m_discover_rsp: get count(%d)\r\n",count);
for(i=0;i<count;i++){
memset(&temp_uri,0,sizeof(cis_uri_t));
temp_uri.objectId = URI_INVALID;
temp_uri.instanceId = URI_INVALID;
temp_uri.resourceId = parase_resource_from_value(buf,i+1);
cis_uri_update(&temp_uri);
cis_response(g_context,&temp_uri,NULL,msgId,CIS_RESPONSE_CONTINUE);
}
cis_response(g_context,NULL,NULL,msgId,CIS_RESPONSE_DISCOVER);
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mreadrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[11];
int ref;
int msgId;
int res;
int objectid;
int instanceid;
int resourceid;
int valuetype;
int len;
char *buf;
int index;
int flag;
int result;
st_context_t *context = (st_context_t*)g_context;
cis_uri_t uri={0};
cis_data_t tmpdata={0};
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_readrsp_policy, 11, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
objectid = blobmsg_get_u32(tb[3]);
instanceid = blobmsg_get_u32(tb[4]);
resourceid = blobmsg_get_u32(tb[5]);
valuetype = blobmsg_get_u32(tb[6]);
len = blobmsg_get_u32(tb[7]);
buf = blobmsg_get_string(tb[8]);
index = blobmsg_get_u32(tb[9]);
flag = blobmsg_get_u32(tb[10]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
lwm2m_printf("lwm2m_read_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_205_CONTENT){
if(index!=0){
cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_READ);
}else{
result=RESULT_TO_RESPONSE(res);
cis_response(context,NULL,NULL,msgId,result);
}
}else{
uri.objectId = objectid;
uri.instanceId = instanceid;
uri.resourceId = resourceid;
if(valuetype == cis_data_type_bool){
tmpdata.type = cis_data_type_bool;
tmpdata.value.asBoolean = lwm2m_get_boolean(buf);
}else if(valuetype == cis_data_type_integer){
tmpdata.type = cis_data_type_integer;
tmpdata.value.asInteger = atoi(buf);
}else if(valuetype == cis_data_type_float){
tmpdata.type = cis_data_type_float;
tmpdata.value.asFloat = atof(buf);
}else if(valuetype == cis_data_type_string){
tmpdata.type = cis_data_type_string;
tmpdata.asBuffer.length = len;
tmpdata.asBuffer.buffer = (uint8_t*)strdup(buf);
}else{
//need check value type
}
if(index==0&& flag==0){
cis_uri_update(&uri);
cis_response(context,&uri,&tmpdata,msgId,CIS_RESPONSE_READ);
}else{
cis_uri_update(&uri);
cis_response(context,&uri,&tmpdata,msgId,CIS_RESPONSE_CONTINUE);
}
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mwritersp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int msgId;
int res;
int result;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
st_context_t *context = (st_context_t*)g_context;
lwm2m_printf("lwm2m_write_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_204_CHANGED){
result=RESULT_TO_RESPONSE(res);
cis_response(context,NULL,NULL,msgId,result);
}else{
cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_WRITE);
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mexcutersp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int msgId;
int res;
int result;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
st_context_t *context = (st_context_t*)g_context;
lwm2m_printf("lwm2m_execute_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_204_CHANGED){
result=RESULT_TO_RESPONSE(res);
cis_response(context,NULL,NULL,msgId,result);
}else{
cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_EXECUTE);
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mparameterrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int msgId;
int res;
int result;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
st_context_t *context = (st_context_t*)g_context;
lwm2m_printf("lwm2m_param_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_205_CONTENT){
result=RESULT_TO_RESPONSE(res);
cis_response(context,NULL,NULL,msgId,result);
}else{
cis_response(context,NULL,NULL,msgId,CIS_RESPONSE_OBSERVE_PARAMS);
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mobserverrsp_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[3];
int ref;
int msgId;
int res;
int result;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_writersp_policy, 3, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
res = blobmsg_get_u32(tb[2]);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
st_context_t *context = (st_context_t*)g_context;
lwm2m_printf("lwm2m_observe_rsp: ref(%d),msgid(%d),result(%d)\r\n",ref,msgId,res);
if(res!=MIPLRS_205_CONTENT){
result=RESULT_TO_RESPONSE(res);
cis_response(context,NULL,NULL,msgId,result);
}else{
cis_response(g_context,NULL,NULL,msgId,CIS_RESPONSE_OBSERVE);
}
result = LWM2MRC_SUCCESS;
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static int lwm2mnotify_method(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
UNUSED(obj);
UNUSED(msg);
UNUSED(method);
static struct blob_buf b;
struct blob_attr *tb[11];
int ref;
int msgId;
int objectid;
int instanceid;
int resourceid;
int valuetype;
int len;
char *buf;
int index;
int flag;
int ackid;
int result;
cis_uri_t uri={0};
cis_data_t dataP;
/*parsing blob to be accessed easily with tb array - parse "1" argument*/
result = blobmsg_parse(lwm2m_notify_policy, 11, tb, blob_data(msg), blob_len(msg));
if (result < 0) {
result = LWM2MRC_FAIL;
goto out;
}
ref = blobmsg_get_u32(tb[0]);
msgId = blobmsg_get_u32(tb[1]);
objectid = blobmsg_get_u32(tb[2]);
instanceid = blobmsg_get_u32(tb[3]);
resourceid = blobmsg_get_u32(tb[4]);
valuetype = blobmsg_get_u32(tb[5]);
len = blobmsg_get_u32(tb[6]);
buf = blobmsg_get_string(tb[7]);
index = blobmsg_get_u32(tb[8]);
flag = blobmsg_get_u32(tb[9]);
ackid = blobmsg_get_u32(tb[10]);
UNUSED(flag);
if(lwm2m_context.config==NULL
|| lwm2m_context.ref != ref){
result = LWM2MRC_FAIL;
goto out;
}
uri.objectId = objectid;
uri.instanceId = instanceid;
uri.resourceId = resourceid;
uri.flag = 0;
memset(&dataP, 0, sizeof(cis_data_t));
dataP.id = resourceid;
if(valuetype == cis_data_type_bool){
dataP.type = cis_data_type_bool;
dataP.value.asBoolean = lwm2m_get_boolean(buf);
}else if(valuetype == cis_data_type_integer){
dataP.type = cis_data_type_integer;
dataP.value.asInteger = atoi(buf);
}else if(valuetype == cis_data_type_float){
dataP.type = cis_data_type_float;
dataP.value.asFloat = atof(buf);
}else if(valuetype == cis_data_type_string){
dataP.type = cis_data_type_string;
dataP.asBuffer.length = len;
dataP.asBuffer.buffer = (uint8_t*)strdup(buf);
}else{
//need check value type
}
if(index==0&& ackid==0){
cis_uri_update(&uri);
result=cis_notify(g_context,&uri,&dataP,msgId,CIS_NOTIFY_CONTENT,false);
}else{
cis_uri_update(&uri);
result=cis_notify(g_context,&uri,&dataP,msgId,CIS_NOTIFY_CONTINUE,false);
}
if(result != CIS_RET_OK){
lwm2m_printf("%s failed:(%d)\r\n",__func__,result);
result = LWM2MRC_FAIL;
}else{
result = LWM2MRC_SUCCESS;
}
pthread_mutex_lock(&lwm2m_context.mutex);
pthread_cond_signal(&lwm2m_context.cond);
pthread_mutex_unlock(&lwm2m_context.mutex);
out:
memset(&b, 0, sizeof(b));
blob_buf_init(&b, 0);
blobmsg_add_u32(&b, "result", result);
ubus_send_reply(ctx, req, b.head);
return 0;
}
static void lwm2m_get_imei_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
UNUSED(req);
UNUSED(type);
unsigned int requestid;
unsigned int rilerrno;
char *response_data = NULL;
int response_len = 0;
if (rilutil_parseResponse(msg, &requestid, &rilerrno, (void *)&response_data, &response_len)) {
lwm2m_printf("%s,Parse failed\r\n",__func__);
return;
}
if (response_data && (response_len > 0)) {
if (rilerrno != RIL_E_SUCCESS) {
lwm2m_printf("%s,Ril error %d\r\n",__func__, rilerrno);
rilutil_freeResponseData(requestid, response_data, response_len);
return;
}
if (requestid == RIL_REQUEST_GET_IMEI) {
lwm2m_printf("%s,len[%d],get IMEI=%s\n",__func__,response_len,response_data);
if(response_len<20){
memset(lwm2m_context.IMEI,0,20);
memcpy(lwm2m_context.IMEI,response_data,response_len);
}else{
lwm2m_printf("%s,get IMEI abnormal\n",__func__);
}
}else{
lwm2m_printf("%s,get requestid=%d\r\n",__func__,requestid);
}
rilutil_freeResponseData(requestid, response_data, response_len);
} else {
if (response_data) {
rilutil_freeResponseData(requestid, response_data, response_len);
}
lwm2m_printf("%s,no response data,rilerror code %d\n", __func__,rilerrno);
}
}
void lwm2m_get_imei()
{
int ret;
static struct blob_buf b;
unsigned int ril_id=0;
if(ubus_lookup_id(lwm2m_ubus_ctx,"ril",&ril_id)){
lwm2m_printf("%s,ubus_lookup_id RIL failed\r\n",__func__);
return;
}
memset(&b, 0, sizeof(b));
blob_buf_init(&b,0);
rilutil_makeRequestBlob(&b, RIL_REQUEST_GET_IMEI, NULL, 0);
ret =ubus_invoke(lwm2m_ubus_ctx, ril_id, "ril_request", b.head, lwm2m_get_imei_cb, 0,0);
if(ret != 0){
lwm2m_printf("lwm2m_get_imei,ubus_invoke Failed %s\r\n", ubus_strerror(ret));
}
return;
}
static void lwm2m_get_imsi_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
UNUSED(req);
UNUSED(type);
unsigned int requestid;
unsigned int rilerrno;
char *response_data = NULL;
int response_len = 0;
if (rilutil_parseResponse(msg, &requestid, &rilerrno, (void *)&response_data, &response_len)) {
lwm2m_printf("%s,Parse failed\r\n",__func__);
return;
}
if (response_data && (response_len > 0)) {
if (rilerrno != RIL_E_SUCCESS) {
lwm2m_printf("%s,Ril error %d\r\n",__func__, rilerrno);
rilutil_freeResponseData(requestid, response_data, response_len);
return;
}
if (requestid == RIL_REQUEST_GET_IMSI) {
lwm2m_printf("%s,len[%d],get IMSI=%s\n",__func__,response_len,response_data);
if(response_len<20){
memset(lwm2m_context.IMSI,0,20);
memcpy(lwm2m_context.IMSI,response_data,response_len);
}else{
lwm2m_printf("%s,get IMSI abnormal\n",__func__);
}
}else{
lwm2m_printf("%s,get requestid=%d\r\n",__func__,requestid);
}
rilutil_freeResponseData(requestid, response_data, response_len);
} else {
if (response_data) {
rilutil_freeResponseData(requestid, response_data, response_len);
}
lwm2m_printf("%s,no response data,rilerror code %d\n", __func__,rilerrno);
}
}
void lwm2m_get_imsi()
{
int ret;
static struct blob_buf b;
unsigned int ril_id=0;
if(ubus_lookup_id(lwm2m_ubus_ctx,"ril",&ril_id)){
lwm2m_printf("%s,ubus_lookup_id RIL failed\r\n",__func__);
return;
}
memset(&b, 0, sizeof(b));
blob_buf_init(&b,0);
rilutil_makeRequestBlob(&b, RIL_REQUEST_GET_IMSI, NULL, 0);
ret =ubus_invoke(lwm2m_ubus_ctx, ril_id, "ril_request", b.head, lwm2m_get_imsi_cb, 0,0);
if(ret != 0){
lwm2m_printf("lwm2m_get_imei,ubus_invoke Failed %s\r\n", ubus_strerror(ret));
}
return;
}
int lwm2m_ubus_init(void)
{
int ret = 0;
ret = ubus_add_object(lwm2m_ubus_ctx, &lwm2m_object);
if (ret){
lwm2m_printf("lwm2m_main,Failed to add object: %s\r\n", ubus_strerror(ret));
return -1;
}
ret = pthread_cond_init(&lwm2m_context.cond, NULL);
if (ret != 0)
return ret;
ret = pthread_mutex_init(&lwm2m_context.mutex, NULL);
if (ret != 0) {
pthread_cond_destroy(&lwm2m_context.cond);
return ret;
}
ret = pthread_create(&s_tid_lwm2mMainTask, NULL, lwm2m_main, NULL);
if(ret < 0){
lwm2m_printf("Failed to create lwm2m main thread, ret:%d\r\n",ret);
return -1;
}
lwm2m_get_imei();
lwm2m_get_imsi();
return 0;
}
int main(int argc,char **argv)
{
UNUSED(argc);
UNUSED(argv);
const char *ubus_socket = NULL;
Lwm2mMutexCreate();
init_lwm2m_context();
uloop_init();
lwm2m_ubus_ctx = ubus_connect(ubus_socket);
if(!lwm2m_ubus_ctx) {
lwm2m_printf("%s: Failed to connect to ubus\r\n",__FUNCTION__);
goto out;
}
ubus_add_uloop(lwm2m_ubus_ctx);
int res=lwm2m_ubus_init();
if(res){
lwm2m_printf("%s: embms_main error!\r\n",__FUNCTION__);
goto fail;
}
uloop_run();
fail:
ubus_free(lwm2m_ubus_ctx);
out:
uloop_done();
return 0;
}