// ubus call test_ubus helloworld '{"id":1,"msg":"hello","array":["a","b"]}' static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__HELLO_MAX]; int tmp_id; char *tmp_msg = NULL; int len; struct blob_attr *attr; void *arr; blobmsg_parse(hello_policy, __HELLO_MAX, tb, blob_data(msg), blob_len(msg)); blob_buf_init(&b, 0); if(tb[HELLO_ID]) { tmp_id = blobmsg_get_u32(tb[HELLO_ID]); blobmsg_add_u32(&b, "id", tmp_id); } if(tb[HELLO_MSG]) { tmp_msg = blobmsg_get_string(tb[HELLO_MSG]); blobmsg_add_string(&b, "msg", tmp_msg); } if(tb[HELLO_ARRAY] && blobmsg_type(tb[HELLO_ARRAY]) == BLOBMSG_TYPE_ARRAY) { arr = blobmsg_open_array(&b, "array"); len = blobmsg_data_len(tb[HELLO_ARRAY]); __blob_for_each_attr(attr, blobmsg_data(tb[HELLO_ARRAY]), len) { if (blobmsg_type(attr) == BLOBMSG_TYPE_STRING) { char *tmp = blobmsg_get_string(attr); blobmsg_add_blob(&b, attr); printf("array=%s\n", tmp); } } blobmsg_close_array(&b, arr); }
static int easycwmpd_handle_inform(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { int tmp; struct blob_attr *tb[__INFORM_MAX]; blobmsg_parse(inform_policy, ARRAY_SIZE(inform_policy), tb, blob_data(msg), blob_len(msg)); if (!tb[INFORM_EVENT]) return UBUS_STATUS_INVALID_ARGUMENT; log_message(NAME, L_NOTICE, "triggered ubus inform %s\n", blobmsg_data(tb[INFORM_EVENT])); tmp = cwmp_get_int_event_code(blobmsg_data(tb[INFORM_EVENT])); cwmp_connection_request(tmp); return 0; }
static int rpc_juci_backup_restore(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__RPC_BACKUP_MAX]; blobmsg_parse(rpc_backup_policy, __RPC_BACKUP_MAX, tb, blob_data(msg), blob_len(msg)); struct blob_attr *pass = tb[RPC_BACKUP_PASSWORD]; if (pass && blobmsg_data_len(pass) > 0 && blobmsg_data(pass) && strlen(blobmsg_data(pass)) > 0){ const char *cmd[] = { "sysupgrade", "--restore-backup", "/tmp/backup.tar.gz", "--password", blobmsg_data(pass), NULL }; return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req); } const char *cmd[] = { "sysupgrade", "--restore-backup", "/tmp/backup.tar.gz", NULL }; return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req); }
static int freecwmpd_handle_notify(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__NOTIFY_MAX]; blobmsg_parse(notify_policy, ARRAY_SIZE(notify_policy), tb, blob_data(msg), blob_len(msg)); if (!tb[NOTIFY_PARAM]) return UBUS_STATUS_INVALID_ARGUMENT; if (!tb[NOTIFY_VALUE]) return UBUS_STATUS_INVALID_ARGUMENT; freecwmp_log_message(NAME, L_NOTICE, "triggered ubus notification parameter %s\n", blobmsg_data(tb[NOTIFY_PARAM])); cwmp_add_notification(blobmsg_data(tb[NOTIFY_PARAM]), blobmsg_data(tb[NOTIFY_VALUE])); return 0; }
static int vlan_mstp(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__VLAN_MSTP_MAX]; int resCode = LR_SUCCESS; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); u8 mstid = 0; u8 portid = 0; u8 portState = 0; bool newMSTI = false; bool delMSTI = false; bool setPortState = false; bool flushFids = false; blobmsg_parse(vlan_mstp_policy, ARRAY_SIZE(vlan_mstp_policy), tb, blob_data(msg), blob_len(msg)); if(tb[VLAN_MSTP_FLUSH_FIDS]){ flushFids = blobmsg_get_bool(tb[VLAN_MSTP_FLUSH_FIDS]); if(flushFids){ /* flush all FIDs */ resCode = delete_all_vlan_in_vlanDatabase(); goto process3; } } if(!tb[VLAN_MSTP_MSTID]){ resCode = LR_INVALID_PARAM; goto returnMsg; } mstid = blobmsg_get_u8(tb[VLAN_MSTP_MSTID]); if(tb[VLAN_MSTP_NEW_MSTI]){ newMSTI = blobmsg_get_bool(tb[VLAN_MSTP_NEW_MSTI]); if(newMSTI){ /* create new msti */ resCode = add_msti_to_hw(mstid); goto process2; } } if(tb[VLAN_MSTP_DELETE_MSTI]){ delMSTI = blobmsg_get_bool(tb[VLAN_MSTP_DELETE_MSTI]); if(delMSTI){ /* delete msti */ resCode = delete_msti_to_hw(mstid); } } process2: if(tb[VLAN_MSTP_SET_PORT_STATE]){ setPortState = blobmsg_get_bool(tb[VLAN_MSTP_SET_PORT_STATE]); if(setPortState){ /* set port state for msti */ if(!tb[VLAN_MSTP_PORTID] || !tb[VLAN_MSTP_PORT_STATE]){ resCode = LR_INVALID_PARAM; goto returnMsg; } portid = blobmsg_get_u8(tb[VLAN_MSTP_PORTID]); portState = blobmsg_get_u8(tb[VLAN_MSTP_PORT_STATE]); resCode = set_port_state_for_msti(mstid, portid, portState); } } process3: pmsg = getJsonMsg(resCode); returnMsg: blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg",pmsg); blobmsg_add_u16(&b,"resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); return UBUS_STATUS_OK; }
static int vlan_port(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__VLAN_PORT_MAX]; int resCode = LR_SUCCESS; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); u16 vid = 0; u8 portid = 0; bool bind = false; blobmsg_parse(vlan_port_policy, ARRAY_SIZE(vlan_port_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[VLAN_PORT_VLANID] || !tb[VLAN_PORT_PORTID]){ resCode = LR_INVALID_PARAM; goto returnMsg; } portid = blobmsg_get_u8(tb[VLAN_PORT_PORTID]); vid = blobmsg_get_u16(tb[VLAN_PORT_VLANID]); if(tb[VLAN_PORT_BIND]){ bind = blobmsg_get_bool(tb[VLAN_PORT_BIND]); if(bind){ BASE_VLAN_PORT port; PORT_LINK_TYPE linktype; u16 pvid = 1; if(!tb[VLAN_PORT_LINKTYPE]){ linktype = (gDevPorts+portid)->linktype; }else{ linktype = blobmsg_get_u8(tb[VLAN_PORT_LINKTYPE]); } get_port_pvid(portid, &pvid); port.portid = portid; if(linktype == PORT_LINK_TYPE_ACCESS){ if(vid != pvid){ resCode = LR_ILLEGAL_OPR; goto process2; } port.portAttr.egress = EGRESS_UNTAGGED; }else if(linktype == PORT_LINK_TYPE_TRUNK){ if(vid == pvid) port.portAttr.egress = EGRESS_UNTAGGED; else port.portAttr.egress = EGRESS_TAGGED; }else if(linktype == PORT_LINK_TYPE_HYBRID){ //TODO: hybrid mode }else if(linktype == PORT_LINK_TYPE_QINQ){ //TODO: QinQ mode }else{ resCode = LR_INVALID_PARAM; goto returnMsg; } resCode = bind_vlan_to_port(vid, port); goto process2; }else{ resCode = unbind_vlan_from_port(vid, portid); goto process2; } } process2: pmsg = getJsonMsg(resCode); returnMsg: blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg",pmsg); blobmsg_add_u16(&b,"resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); return UBUS_STATUS_OK; }
static int vlan_delete(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__VLAN_DELETE_MAX]; u8 mode = 0; int resCode = LR_SUCCESS; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); json_object *result = NULL; result = json_object_new_object(); blobmsg_parse(vlan_delete_policy, ARRAY_SIZE(vlan_delete_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[VLAN_DELETE_MODE]){ resCode = LR_INVALID_PARAM; goto returnMsg; } mode = blobmsg_get_u8(tb[VLAN_DELETE_MODE]); if(mode){ int vid; if(!tb[VLAN_DELETE_ID]){ resCode = LR_INVALID_PARAM; goto returnMsg; } vid = blobmsg_get_u16(tb[VLAN_DELETE_ID]); resCode = delete_vlan_in_vlanDatabase(vid); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); goto returnMsg; } json_object_object_add(result, "vlanID", json_object_new_int(vid)); }else{ /* delete all vlan */ resCode = delete_all_vlan_in_vlanDatabase(); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); goto returnMsg; } json_object_object_add(result, "flush", json_object_new_int(1)); } pmsg = json_object_to_json_string(result); returnMsg: blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg",pmsg); blobmsg_add_u16(&b,"resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); if(result != NULL) json_object_put(result); return UBUS_STATUS_OK; }
static int vlan_find(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__VLAN_FIND_MAX]; u8 mode = 0; int resCode = LR_SUCCESS; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); json_object *result = NULL; blobmsg_parse(vlan_find_policy, ARRAY_SIZE(vlan_find_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[VLAN_FIND_MODE]){ resCode = LR_INVALID_PARAM; goto returnMsg; } mode = blobmsg_get_u8(tb[VLAN_FIND_MODE]); if(1 == mode){ /* find a special VLAN ID */ int vid; bool found; VLAN_ENTRY vlanEntry; if(!tb[VLAN_FIND_ID]){ resCode = LR_INVALID_PARAM; goto returnMsg; } vid = blobmsg_get_u16(tb[VLAN_FIND_ID]); resCode = find_vlan_in_vlanDatabase(vid, &vlanEntry, &found); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); goto returnMsg; } if(found){ resCode = LR_FOUND; result = gen_json_object_vlan_entry(&vlanEntry); pmsg = json_object_to_json_string(result); }else{ resCode = LR_NOT_FOUND; pmsg = getJsonMsg(LR_NOT_EXIST); } }else if( 3== mode){ /* find all VLAN ID in the hardware VLAN LIST , include port attribute*/ VLAN_ENTRY *vlanEntry = NULL; int vlanEntryCnt; resCode = find_all_vlan_in_vlanDatabase(&vlanEntry, &vlanEntryCnt); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); goto returnMsg; } json_object *vidList = json_object_new_array(); result = json_object_new_object(); for(int i=0; i< vlanEntryCnt; i++){ json_object *vlnEntry = gen_json_object_vlan_entry(vlanEntry+i); json_object_array_add(vidList, vlnEntry); } json_object_object_add(result, "vlanlist", vidList); pmsg = json_object_to_json_string(result); //free vlan entry free_VLAN_ENTRY_pointer(vlanEntry, vlanEntryCnt); }else{ /* find all VLAN ID in the hardware VLAN LIST */ u8 vids[4095] = {0}; int vidCnt; int i; memset(vids, 0, 4095); resCode = find_all_vlan_in_vlanDatabase_summary((u8 *)&vids, &vidCnt); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); goto returnMsg; } json_object *vidList = json_object_new_array(); result = json_object_new_object(); for(i=1; i< 4095; i++){ if(vids[i] == 1){ json_object_array_add(vidList, json_object_new_int(i)); } } json_object_object_add(result, "vlanlist", vidList); pmsg = json_object_to_json_string(result); } returnMsg: blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg",pmsg); blobmsg_add_u16(&b,"resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); if(result != NULL) json_object_put(result); return UBUS_STATUS_OK; }
static int handle_loglevel_update(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__LOGLEVEL_MAX]; char *log_buf = NULL; char cmd[128] = {0}; cloudc_debug("%s[%d]: Enter ", __func__, __LINE__); blobmsg_parse(handle_loglevel_policy, ARRAY_SIZE(handle_loglevel_policy), tb, blob_data(msg), blob_len(msg)); if (tb[LOGLEVEL]) { log_buf = blobmsg_data(tb[LOGLEVEL]); if ( NULL != strcasestr(log_buf, "debug")) { cloudc_log_level_int = LOG_LEVEL_DEBUG; } else if ( NULL != strcasestr(log_buf, "error")) { cloudc_log_level_int = LOG_LEVEL_ERR; } else { cloudc_error("%s[%d]: parameter should only be debug or error, please double check", __func__, __LINE__); } snprintf(cmd, sizeof(cmd), "uci set cloudc.global.loglevel=\"%s\"",log_buf); system(cmd); system("uci commit"); cloudc_debug("%s[%d]: cmd = %s\n", __func__, __LINE__, cmd); cloudc_debug("%s[%d]: cloudc_log_level_int = %d", __func__, __LINE__, cloudc_log_level_int); } else { cloudc_debug("%s[%d]: wrong parameter,please double check", __func__, __LINE__); } cloudc_debug("%s[%d]: Exit ", __func__, __LINE__); return 0; }
static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg) { struct blob_attr *tb[__INIT_MAX]; char *token, *ssid, *ssid_pwd; int len, ret; if (!msg) return; ret = 0; blobmsg_parse(init_policy, __INIT_MAX, tb, blob_data(msg), blob_len(msg)); if(tb[INIT_TOKEN] && tb[INIT_SSID] && tb[INIT_SSID_PWD]){ token = blobmsg_data(tb[INIT_TOKEN]); ssid = blobmsg_data(tb[INIT_SSID]); ssid_pwd = blobmsg_data(tb[INIT_SSID_PWD]); // todo dlog("recv token[%s], ssid[%s] ssid_pwd[%s]\n", token, ssid, ssid_pwd); // printf("recv token[%s], ssid[%s] ssid_pwd[%s]\n", token, ssid, ssid_pwd); char *base64 = malloc(MAX_ALLOC_LEN); char *router_key_iv = malloc(MAX_ALLOC_LEN); char *r_ssid = malloc(MAX_ALLOC_LEN); char *r_pwd = malloc(MAX_ALLOC_LEN); char base64_ssid[MAX_ALLOC_LEN] = {0}; char base64_pwd[MAX_ALLOC_LEN] = {0}; // get router key, iv memset(base64, 0, MAX_ALLOC_LEN); int base64_length = base64Decode(base64, token, strlen(token)); // printf("json base64 len: %d\n", base64_length); AES128_CBC_decrypt_buffer(router_key_iv, base64, base64_length, repeater_key, repeater_iv); memcpy(router_key, router_key_iv, 16); memcpy(router_iv, router_key_iv+16, 16); // ssid memset(base64_ssid, 0, MAX_ALLOC_LEN); memset(r_ssid, 0, MAX_ALLOC_LEN); memset(rr_ssid, 0, MAX_ALLOC_LEN); base64_length = base64Decode(base64_ssid, ssid, strlen(ssid)); AES128_CBC_decrypt_buffer(r_ssid, base64_ssid, base64_length, router_key, router_iv); memcpy(rr_ssid, r_ssid, strlen(r_ssid)); // printf("rr_ssid:%s\n", rr_ssid); // ssid pwd memset(base64_pwd, 0, MAX_ALLOC_LEN); memset(r_pwd, 0, MAX_ALLOC_LEN); memset(rr_pwd, 0, MAX_ALLOC_LEN); base64_length = base64Decode(base64_pwd, ssid_pwd, strlen(ssid_pwd)); AES128_CBC_decrypt_buffer(r_pwd, base64_pwd, base64_length, router_key, router_iv); memcpy(rr_pwd, r_pwd, strlen(r_pwd)); // printf("rr_pwd:%s\n", rr_pwd); flag_ssid_pwd = 1; /* Succeed to get password and ssid, so clear hide_mode flag */ set_hide_mode(0); if (router_key_iv) free(router_key_iv); if (base64) free(base64); if (r_ssid) free(r_ssid); if (r_pwd) free(r_pwd); }else{ dlog("recv DATA INVALID\n"); ret = 1; } if (ret){ printf("ecos pair verify failed!\n"); dlog("ret:%d wait %d try again\n", ret, TRAFFICD_INIT_LOOP_TIME / 1000); uloop_timeout_set(&sd->timeout_init, TRAFFICD_INIT_LOOP_TIME); }else{ printf("ssid:%s\n", rr_ssid); printf("ecos pair verify ok!\n"); #ifdef __ECOS #if 0 CFG_set(CFG_TRAFFIC_INIT, 0); sys->init_mode = 0; uloop_timeout_set(&sd->timeout_mat, TRAFFICD_WIFIAP_LOOP_TIME); #endif #endif } }
/* * add ports to aggregation group. * msg : {"aggid":xxx, * "ports": [ * {"portNumber":x}, * {"portNumber":x}, * {"portNumber":x} * ] * } * */ static int aggregation_add_port(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__AGGREGATION_ADD_PORT_MAX]; //struct blob_attr *tbPortList[__AGGREGATION_ADD_PORT_LIST_MAX]; char *dev = NULL; int aggID; struct blob_attr *cur; int portCnt = 0; int rem; struct portAttr port; //TODO: blobmsg_parse(aggregation_add_port_policy, ARRAY_SIZE(aggregation_add_port_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[AGGREGATION_ADD_PORT_GROUP_ID] || !tb[AGGREGATION_ADD_PORT_PORTS]) return UBUS_STATUS_INVALID_ARGUMENT; aggID = blobmsg_get_u32(tb[AGGREGATION_ADD_PORT_GROUP_ID]); if(tb[AGGREGATION_ADD_PORT_DEVICE]) dev = blobmsg_get_string(tb[AGGREGATION_ADD_PORT_DEVICE]); cur = tb[AGGREGATION_ADD_PORT_GROUP_ID]; blobmsg_for_each_attr(cur,tb[AGGREGATION_ADD_PORT_PORTS], rem) { //get port number get_port_attr(&port, cur); // printf("%s, port = %x\n",__func__, port.number); // TODO: add port to aggid }
static void vlanlist_checkout_callback(struct ubus_request *req, int type, struct blob_attr *msg) { char *str = NULL; struct Rmsg; struct blob_attr *tb[__FRAMEWORK_MAX]; struct json_object *new_obj; blobmsg_parse(framework_result_policy, ARRAY_SIZE(framework_result_policy), tb, blob_data(msg), blob_len(msg)); Rmsg.msg = blobmsg_get_string(tb[FRAMEWORK_RESULT_MSG]); Rmsg.resCode = (int)blobmsg_get_u16(tb[FRAMEWORK_RESULT_CODE]); //printf("%s\n", Rmsg.msg); new_obj = json_tokener_parse(Rmsg.msg); if(Rmsg.resCode != LR_ERR_NONE){ if(new_obj){ const char *errmsg = NULL; struct json_object *o = json_object_object_get(new_obj,"ERROR"); errmsg = json_object_get_string(o); if(errmsg != NULL) fprintf(stderr,"%s",errmsg); json_object_put(o); json_object_put(new_obj); } }else{ if(new_obj){ struct json_object *o = json_object_object_get(new_obj, "vlanlist"); // vlanlist is a array if(json_object_is_type(o, json_type_array)){ int length=json_object_array_length(o); int i = 0; u16 *pVids = (u16 *)malloc(sizeof(u16) * length); for(i=0; i<length; i++) { json_object *val=json_object_array_get_idx(o,i); *(pVids+i) = json_object_get_int(val); } copy_vlanlist_software(pVids, length); free(pVids); } } } }
static void swVlan_result_data(struct ubus_request *req, int type, struct blob_attr *msg) { char *str = NULL; struct Rmsg; struct blob_attr *tb[__FRAMEWORK_MAX]; //pthread_mutex_lock(&gClient->client_cb_mutex); blobmsg_parse(framework_result_policy, ARRAY_SIZE(framework_result_policy), tb, blob_data(msg), blob_len(msg)); Rmsg.msg = blobmsg_get_string(tb[FRAMEWORK_RESULT_MSG]); Rmsg.resCode = blobmsg_get_u16(tb[FRAMEWORK_RESULT_CODE]); if((asyncResult == NULL) || (asyncResult->result_data_cb == NULL)){ str = blobmsg_format_json(msg, true); printf("%s\n",str); free(str); }else{ asyncResult->result_data_cb(&Rmsg); } //pthread_mutex_unlock(&gClient->client_cb_mutex); return; }
static int apClient_connect(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__CONFIG_MAX]; int try_count = 0; int wait_count = 3; const char *apname; const char *staname; const char *ssid; const char *passwd; const char *channel; const char *security; const char *bssid; char *crypto; char cmd[100]; blobmsg_parse(connect_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg)); if (!tb[CONFIG_APNAME]) return UBUS_STATUS_INVALID_ARGUMENT; blob_buf_init(&buf, 0); apname = blobmsg_data(tb[CONFIG_APNAME]); staname = blobmsg_data(tb[CONFIG_STANAME]); ssid = blobmsg_data(tb[CONFIG_SSID]); passwd = blobmsg_data(tb[CONFIG_PASSWD]); channel = blobmsg_data(tb[CONFIG_CHANNEL]); security = blobmsg_data(tb[CONFIG_SECURITY]); crypto = strstr(security, "/"); if (crypto) { *crypto = '\0'; crypto++; } wifi_site_survey(apname,NULL,0); wifi_repeater_start(apname, staname, channel, ssid, passwd, security, crypto); /*ifconfig staname down*/ snprintf(cmd, lengthof(cmd) - 1, "ifconfig %s down", staname); system(cmd); /*ifconfig staname down*/ snprintf(cmd, lengthof(cmd) - 1, "ifconfig %s up", staname); system(cmd); /*use uci set ssid*/ snprintf(cmd, lengthof(cmd) - 1, "uci set wireless.sta.ApCliSsid=%s", ssid); system(cmd); /*use uci set key*/ snprintf(cmd, lengthof(cmd) - 1, "uci set wireless.sta.ApCliWPAPSK=%s", passwd); system(cmd); /*uci commit*/ snprintf(cmd, lengthof(cmd) - 1, "uci commit"); system(cmd); /*udhcpc -i apcli0*/ snprintf(cmd, lengthof(cmd) - 1, "udhcpc -n -q -i apcli0"); system(cmd); while (wait_count--) { if (isStaGetIP(staname)) { blobmsg_add_string(&buf, "result", "success"); break; } sleep(1); } if (wait_count == -1) { blobmsg_add_string(&buf, "result", "failed"); } ubus_send_reply(ctx, req, buf.head); return UBUS_STATUS_OK; }
static int log_notify(struct blob_attr *msg) { struct blob_attr *tb[__LOG_MAX]; struct stat s; char buf[512]; uint32_t p; char *str; time_t t; char *c, *m; int ret = 0; if (sender.fd < 0) return 0; blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg)); if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG]) return 1; if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size)) { char *old = malloc(strlen(log_file) + 5); close(sender.fd); if (old) { sprintf(old, "%s.old", log_file); rename(log_file, old); free(old); } sender.fd = open(log_file, O_CREAT | O_WRONLY | O_APPEND, 0600); if (sender.fd < 0) { fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); exit(-1); } } m = blobmsg_get_string(tb[LOG_MSG]); t = blobmsg_get_u64(tb[LOG_TIME]) / 1000; c = ctime(&t); p = blobmsg_get_u32(tb[LOG_PRIO]); c[strlen(c) - 1] = '\0'; str = blobmsg_format_json(msg, true); if (log_type == LOG_NET) { int err; snprintf(buf, sizeof(buf), "<%u>", p); strncat(buf, c + 4, 16); if (hostname) { strncat(buf, hostname, sizeof(buf) - strlen(buf) - 1); strncat(buf, " ", sizeof(buf) - strlen(buf) - 1); } if (log_prefix) { strncat(buf, log_prefix, sizeof(buf) - strlen(buf) - 1); strncat(buf, ": ", sizeof(buf) - strlen(buf) - 1); } if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG) strncat(buf, "kernel: ", sizeof(buf) - strlen(buf) - 1); strncat(buf, m, sizeof(buf) - strlen(buf) - 1); if (log_udp) err = write(sender.fd, buf, strlen(buf)); else { size_t buflen = strlen(buf); if (!log_trailer_null) buf[buflen] = '\n'; err = send(sender.fd, buf, buflen + 1, 0); } if (err < 0) { syslog(LOG_INFO, "failed to send log data to %s:%s via %s\n", log_ip, log_port, (log_udp) ? ("udp") : ("tcp")); uloop_fd_delete(&sender); close(sender.fd); sender.fd = -1; uloop_timeout_set(&retry, 1000); } } else { snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n", c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames), (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m); ret = write(sender.fd, buf, strlen(buf)); } free(str); if (log_type == LOG_FILE) fsync(sender.fd); return ret; }
static int aggregation_enable(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__AGGREGATION_MAX]; bool en; int fds[2]; blobmsg_parse(aggregation_enable_policy, ARRAY_SIZE(aggregation_enable_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[AGGREGATION_MODULE_ENABLE]) return UBUS_STATUS_INVALID_ARGUMENT; en = blobmsg_get_bool(tb[AGGREGATION_MODULE_ENABLE]); // TODO:check aggregation module is found, set enable status to require. DEBUG("%s received enable message:%s\n", obj->name, en?"true":"false"); { int resCode = LR_SUCCESS; char msg[100]; char *pmsg = msg; // TODO: add other attr sprintf(msg,"set aggregation module to %s", en ? "true":"false"); blob_buf_init(&b, 0); //blobmsg_add_u8(&b, "module_en",en); blobmsg_add_string(&b, "msg", pmsg); blobmsg_add_u16(&b, "resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); } #if 0 if(pipe(fds) == -1) { fprintf(stderr, "Failed to create pipe\n"); return -1; } ubus_request_set_fd(ctx, req, fds[0]); ubus_complete_deferred_request(ctx, req, 0); { char data[100]={0}; sprintf(data, "module %s\n", en ? "enable" : "disable" ); if(write(fds[1], data, strlen(data) ) < 0) { close(fds[1]); free(req); return 0; } } #endif return 0; }
static int aggregation_set_sys_pri(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__AGGREGATION_PRI_MAX]; char *dev = NULL; int sysPri; int fds[2]; blobmsg_parse(aggregation_sys_pri_policy, ARRAY_SIZE(aggregation_sys_pri_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[AGGREGATION_SYS_PRI]) return UBUS_STATUS_INVALID_ARGUMENT; if(tb[AGGREGATION_DEVICE]) dev = blobmsg_get_string(tb[AGGREGATION_DEVICE]); sysPri = blobmsg_get_u32(tb[AGGREGATION_SYS_PRI]); // TODO:do set aggregation system priority. // dev used when system have more 1 VxBOX device. DPRINTF("%s received set aggregation system priority message: \ dev = %s, system Priority = %d \n", obj->name, dev, sysPri); // ubus_defer_request(ctx, req, &hreq->req); blob_buf_init(&b, 0); // blobmsg_add_string(&b, "device",dev); blobmsg_add_u16(&b, "system priority",sysPri); // TODO: add other attr req->deferred = false; ubus_send_reply(ctx, req, b.head); return 0; }
static void delete_vlan_callback(struct ubus_request *req, int type, struct blob_attr *msg) { char *str = NULL; struct Rmsg; struct blob_attr *tb[__FRAMEWORK_MAX]; struct json_object *new_obj; blobmsg_parse(framework_result_policy, ARRAY_SIZE(framework_result_policy), tb, blob_data(msg), blob_len(msg)); Rmsg.msg = blobmsg_get_string(tb[FRAMEWORK_RESULT_MSG]); Rmsg.resCode = (int)blobmsg_get_u16(tb[FRAMEWORK_RESULT_CODE]); new_obj = json_tokener_parse(Rmsg.msg); if(Rmsg.resCode == LR_ERR_NONE){ /* success add new vlan entry */ if(new_obj){ struct json_object *o = json_object_object_get(new_obj,"vlanID"); if(o != NULL){ u16 vid = json_object_get_int(o); del_vlan_software(vid); json_object_put(o); }else{ o = json_object_object_get(new_obj, "flush"); if( o != NULL){ flush_vlan_software(); json_object_put(o); } } json_object_put(new_obj); Rmsg.msg = ""; } } if((asyncResult == NULL) || (asyncResult->result_data_cb == NULL)){ str = blobmsg_format_json(msg, true); printf("%s\n",str); free(str); }else{ asyncResult->result_data_cb(&Rmsg); } }
static int aggregation_set_group_desc(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__AGGREGATION_GROUP_DESC_MAX]; char *dev = NULL; int aggID; char *aggDesc = NULL; blobmsg_parse(aggregation_group_desc_policy, ARRAY_SIZE(aggregation_group_desc_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[AGGREGATION_GROUP_DESC_ID]) return UBUS_STATUS_INVALID_ARGUMENT; aggID = blobmsg_get_u32(tb[AGGREGATION_GROUP_DESC_ID]); if(tb[AGGREGATION_GROUP_DESC_DEVICE]) dev = blobmsg_get_string(tb[AGGREGATION_GROUP_DESC_DEVICE]); if(tb[AGGREGATION_GROUP_DESC]) aggDesc = blobmsg_get_string(tb[AGGREGATION_GROUP_DESC_DESC]); // TODO:do set aggregation group description. // dev used when system have more 1 VxBOX device. DPRINTF("%s received create aggregation group message: \ dev = %s, aggid = %d, aggDesc=%s \n", obj->name, dev, aggID, aggDesc); blob_buf_init(&b, 0); blobmsg_add_u16(&b, "aggregation id",aggID); if (aggDesc) blobmsg_add_string(&b, "aggregation description",aggDesc); // TODO: add other attr req->deferred = false; ubus_send_reply(ctx, req, b.head); return 0; }
static int rpc_rrdns_lookup(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { int port = 53, limit = RRDNS_DEF_LIMIT, timeout = RRDNS_DEF_TIMEOUT; struct blob_attr *tb[__RPC_L_MAX]; struct rrdns_context *rctx; const char *server = NULL; blobmsg_parse(rpc_lookup_policy, __RPC_L_MAX, tb, blob_data(msg), blob_len(msg)); if (tb[RPC_L_PORT]) port = blobmsg_get_u16(tb[RPC_L_PORT]); if (tb[RPC_L_LIMIT]) limit = blobmsg_get_u32(tb[RPC_L_LIMIT]); if (tb[RPC_L_TIMEOUT]) timeout = blobmsg_get_u32(tb[RPC_L_TIMEOUT]); if (tb[RPC_L_SERVER]) server = blobmsg_get_string(tb[RPC_L_SERVER]); if (!tb[RPC_L_ADDRS]) return UBUS_STATUS_INVALID_ARGUMENT; if (port <= 0) return UBUS_STATUS_INVALID_ARGUMENT; if (limit <= 0 || limit > RRDNS_MAX_LIMIT) return UBUS_STATUS_INVALID_ARGUMENT; if (timeout <= 0 || timeout > RRDNS_MAX_TIMEOUT) return UBUS_STATUS_INVALID_ARGUMENT; if (!server || !*server) server = rrdns_find_nameserver(); if (!server) return UBUS_STATUS_NOT_FOUND; rctx = calloc(1, sizeof(*rctx)); if (!rctx) return UBUS_STATUS_UNKNOWN_ERROR; rctx->socket.fd = usock(USOCK_UDP, server, usock_port(port)); if (rctx->socket.fd < 0) { free(rctx); return UBUS_STATUS_UNKNOWN_ERROR; } rctx->context = ctx; rctx->addr_cur = blobmsg_data(tb[RPC_L_ADDRS]); rctx->addr_rem = blobmsg_data_len(tb[RPC_L_ADDRS]); avl_init(&rctx->request_ids, rrdns_cmp_id, false, NULL); avl_init(&rctx->request_addrs, rrdns_cmp_addr, false, NULL); rctx->timeout.cb = rrdns_handle_timeout; uloop_timeout_set(&rctx->timeout, timeout); rctx->socket.cb = rrdns_handle_response; uloop_fd_add(&rctx->socket, ULOOP_READ); blob_buf_init(&rctx->blob, 0); while (limit--) rrdns_next_query(rctx); ubus_defer_request(ctx, req, &rctx->request); return UBUS_STATUS_OK; }
static int sw_reg_write(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__SW_REG_WRITE_MAX]; u8 phyAddr; u8 regAddr; u16 data; int resCode = LR_SUCCESS; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); blobmsg_parse(sw_reg_write_policy, ARRAY_SIZE(sw_reg_write_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[SW_REG_WRITE_REG_ADDR] || !tb[SW_REG_WRITE_ADDR] || !tb[SW_REG_WRITE_DATA]){ resCode = LR_INVALID_PARAM; goto returnMsg; } phyAddr = blobmsg_get_u8(tb[SW_REG_WRITE_ADDR]); regAddr = blobmsg_get_u8(tb[SW_REG_WRITE_REG_ADDR]); data = blobmsg_get_u16(tb[SW_REG_WRITE_DATA]); resCode = sw_chip_register_write_reg(phyAddr, regAddr, (int)data); char buff[100]; sprintf(buff, "{\"phyAddr\":0x%2x, \"regAddr\":0x%2x, \"value\":0x%4x}", phyAddr,regAddr, data); pmsg = buff; //pmsg= getJsonMsg(resCode); returnMsg: blob_buf_init(&b, 0); blobmsg_add_string(&b, "msg",pmsg); blobmsg_add_u16(&b,"resCode", resCode); req->deferred = false; ubus_send_reply(ctx, req, b.head); return UBUS_STATUS_OK; }