// 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); }
struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi) { struct http_request *req = &cl->request; struct blob_attr *data = cl->hdr.head; struct env_var *vars = (void *) uh_buf; struct blob_attr *tb[__HDR_MAX]; const char *url; int len; int i; url = blobmsg_data(blob_data(cl->hdr.head)); len = ARRAY_SIZE(proc_header_env); len += ARRAY_SIZE(extra_vars); len *= sizeof(struct env_var); BUILD_BUG_ON(sizeof(uh_buf) < len); extra_vars[VAR_SCRIPT_NAME].value = pi->name; extra_vars[VAR_SCRIPT_FILE].value = pi->phys; extra_vars[VAR_DOCROOT].value = pi->root; extra_vars[VAR_QUERY].value = pi->query ? pi->query : ""; extra_vars[VAR_REQUEST].value = url; extra_vars[VAR_PROTO].value = http_versions[req->version]; extra_vars[VAR_METHOD].value = http_methods[req->method]; extra_vars[VAR_PATH_INFO].value = pi->info; extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL; extra_vars[VAR_HTTPS].value = cl->tls ? "on" : NULL; snprintf(redirect_status, sizeof(redirect_status), "%d", req->redirect_status); inet_ntop(cl->srv_addr.family, &cl->srv_addr.in, local_addr, sizeof(local_addr)); snprintf(local_port, sizeof(local_port), "%d", cl->srv_addr.port); inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr)); snprintf(remote_port, sizeof(remote_port), "%d", cl->peer_addr.port); blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data)); for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) { struct blob_attr *cur; cur = tb[proc_header_env[i].idx]; vars[i].name = proc_header_env[i].name; vars[i].value = cur ? blobmsg_data(cur) : ""; } memcpy(&vars[i], extra_vars, sizeof(extra_vars)); i += ARRAY_SIZE(extra_vars); vars[i].name = NULL; vars[i].value = NULL; return vars; }
static void create_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"); u16 vid = json_object_get_int(o); new_vlan_software(vid); 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 rpc_juci_backup_config_set(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { FILE *f; struct blob_attr *tb[__RPC_D_MAX]; blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb, blob_data(msg), blob_len(msg)); if (!tb[RPC_D_DATA]) return UBUS_STATUS_INVALID_ARGUMENT; if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048) return UBUS_STATUS_NOT_SUPPORTED; if (!(f = fopen("/etc/sysupgrade.conf", "w"))) return rpc_errno_status(); fwrite(blobmsg_data(tb[RPC_D_DATA]), blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f); fclose(f); return 0; }
static void put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name) { void *c = blobmsg_open_table(buf, name); blob_put_raw(buf, blob_data(attr), blob_len(attr)); blobmsg_close_table(buf, c); }
static int handle_notify(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { char *str; int msg_level; const char *info; struct blob_attr *tb[__MAN_ALERT_MAX]; blobmsg_parse(alert_policy, ARRAY_SIZE(alert_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[MAN_ALERT_MSG_LEVEL] || !tb[MAN_ALERT_MSG_INFO]){ fprintf(stderr, "Received illegal notification\n"); return -1; } msg_level = blobmsg_get_u16(tb[MAN_ALERT_MSG_LEVEL]); info = blobmsg_get_string(tb[MAN_ALERT_MSG_INFO]); /* str = blobmsg_format_json(msg, true); fprintf(stderr, "Received notification '%s': %s\n", method, str); free(str); */ asyncResult->notify_cb( method, msg_level, info); return 0; }
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 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 void dump_cb(struct ubus_request *req, int type, struct blob_attr *msg) { static const struct blobmsg_policy policy = { "interface", BLOBMSG_TYPE_ARRAY }; struct blob_attr *cur; blobmsg_parse(&policy, 1, &cur, blob_data(msg), blob_len(msg)); if (cur) interfaces = blob_memdup(cur); }
bool LinuxIPCHost::OnSetCharacteristicValue(const std::string& service_uuid, const std::string& characteristic_uuid, const std::string& value) { std::string decoded_data; base::Base64Decode(value, &decoded_data); std::vector<uint8_t> blob_data(decoded_data.begin(), decoded_data.end()); gatt_servers_[service_uuid]->SetCharacteristicValue(UUID(characteristic_uuid), blob_data); return true; }
static void dump_table(struct blob_attr *head, int len, int indent, bool array) { struct blob_attr *attr; struct blobmsg_hdr *hdr; indent_printf(indent, "{\n"); __blob_for_each_attr(attr, head, len) { hdr = blob_data(attr); if (!array) indent_printf(indent + 1, "%s : ", hdr->name); dump_attr_data(blobmsg_data(attr), blobmsg_data_len(attr), blob_id(attr), 0, indent + 1); }
static int easycwmpd_handle_command(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__COMMAND_MAX]; blobmsg_parse(command_policy, ARRAY_SIZE(command_policy), tb, blob_data(msg), blob_len(msg)); if (!tb[COMMAND_NAME]) return UBUS_STATUS_INVALID_ARGUMENT; blob_buf_init(&b, 0); char *cmd = blobmsg_data(tb[COMMAND_NAME]); char *info; if (!strcmp("reload", cmd)) { log_message(NAME, L_NOTICE, "triggered ubus reload\n"); easycwmp_reload(); blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "easycwmpd reloaded") == -1) goto error; } else if (!strcmp("stop", cmd)) { log_message(NAME, L_NOTICE, "triggered ubus stop\n"); ubus_timer.cb = ubus_easycwmpd_stop_callback; uloop_timeout_set(&ubus_timer, 1000); blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "easycwmpd stopped") == -1) goto error; } else { blobmsg_add_u32(&b, "status", -1); if (asprintf(&info, "%s command is not supported", cmd) == -1) goto error; } blobmsg_add_string(&b, "info", info); free(info); ubus_send_reply(ctx, req, b.head); blob_buf_free(&b); return 0; error: blob_buf_free(&b); return -1; }
static int vlan_new(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_NEW_MAX]; int vid; u8 mstid = 0; int resCode = LR_ERR_NONE; const char *pmsg = getJsonMsg(LR_INVALID_PARAM); json_object *result = NULL; blobmsg_parse(vlan_new_policy, ARRAY_SIZE(vlan_new_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[VLAN_NEW_ID]){ resCode = LR_INVALID_PARAM; goto returnMsg; } if(tb[VLAN_NEW_MSTID]){ mstid = blobmsg_get_u8(tb[VLAN_NEW_MSTID]); } vid = blobmsg_get_u16(tb[VLAN_NEW_ID]); //resCode = create_vlan_have_none_port(vid); resCode = create_vlan_have_none_port_ex(vid, mstid); if(resCode != LR_ERR_NONE){ pmsg = getJsonMsg(resCode); }else{ result = json_object_new_object(); json_object_object_add(result, "vlanID", json_object_new_int(vid)); 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 nand_set(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__NAND_MAX]; if (!msg) return UBUS_STATUS_INVALID_ARGUMENT; blobmsg_parse(nand_policy, __NAND_MAX, tb, blob_data(msg), blob_len(msg)); if (!tb[NAND_PATH]) return UBUS_STATUS_INVALID_ARGUMENT; procd_spawn_upgraded(blobmsg_get_string(tb[NAND_PATH])); fprintf(stderr, "Yikees, something went wrong. no /sbin/upgraded ?\n"); return 0; }
static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__SIGNAL_MAX]; if (!msg) return UBUS_STATUS_INVALID_ARGUMENT; blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg)); if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]]) return UBUS_STATUS_INVALID_ARGUMENT; kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM])); return 0; }
static int aggregation_create_group(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_MAX]; char *dev = NULL; int aggID; char *aggType = NULL; char *aggDesc = NULL; blobmsg_parse(aggregation_group_policy, ARRAY_SIZE(aggregation_group_policy), tb, blob_data(msg), blob_len(msg)); if(!tb[AGGREGATION_GROUP_ID]) return UBUS_STATUS_INVALID_ARGUMENT; aggID = blobmsg_get_u32(tb[AGGREGATION_GROUP_ID]); if(tb[AGGREGATION_GROUP_DEVICE]) dev = blobmsg_get_string(tb[AGGREGATION_GROUP_DEVICE]); if(tb[AGGREGATION_GROUP_TYPE]) aggType = blobmsg_get_string(tb[AGGREGATION_GROUP_TYPE]); if(tb[AGGREGATION_GROUP_DESC]) aggDesc = blobmsg_get_string(tb[AGGREGATION_GROUP_DESC]); // TODO:do create new aggregation group. // dev used when system have more 1 VxBOX device. DPRINTF("%s received create aggregation group message: \ dev = %s, aggid = %d, aggType=%s, aggDesc=%s \n", obj->name, dev, aggID,aggType, aggDesc); blob_buf_init(&b, 0); blobmsg_add_u16(&b, "aggregation id",aggID); if (aggType) blobmsg_add_string(&b, "aggregation type",aggType); 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 struct json_script_file * handle_file(struct json_script_ctx *ctx, const char *filename) { struct json *obj; obj = json_object_from_file(filename); if (!obj) { fprintf(stderr, "load JSON data from %s failed.\n", filename); return NULL; } blob_buf_init(&b_script, 0); blobmsg_add_json_element(&b_script, "", obj); json_object_put(obj); return json_script_file_from_blobmsg(filename, blob_data(b_script.head), blob_len(b_script.head)); }
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 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 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; }
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, int len) { struct blobmsg_hdr *hdr; struct blob_attr *attr; uint8_t *pslen; int i; memset(tb, 0, policy_len * sizeof(*tb)); pslen = alloca(policy_len); for (i = 0; i < policy_len; i++) { if (!policy[i].name) continue; pslen[i] = strlen(policy[i].name); } __blob_for_each_attr(attr, data, len) { hdr = blob_data(attr); for (i = 0; i < policy_len; i++) { if (!policy[i].name) continue; if (policy[i].type != BLOBMSG_TYPE_UNSPEC && blob_id(attr) != policy[i].type) continue; if (blobmsg_namelen(hdr) != pslen[i]) continue; if (!blobmsg_check_attr(attr, true)) return -1; if (tb[i]) continue; if (strcmp(policy[i].name, (char *) hdr->name) != 0) continue; tb[i] = attr; } }
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 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 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_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 void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr, struct ubus_object *obj, struct blob_attr **attrbuf) { printf("-> [obj] ubus_process_invoke\n"); struct ubus_request_data req = { .fd = -1, }; int method; int ret; bool no_reply = false; if (!obj) { ret = UBUS_STATUS_NOT_FOUND; goto send; } if (!attrbuf[UBUS_ATTR_METHOD]) { ret = UBUS_STATUS_INVALID_ARGUMENT; goto send; } if (attrbuf[UBUS_ATTR_NO_REPLY]) no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]); req.peer = hdr->peer; req.seq = hdr->seq; req.object = obj->id; if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) { req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]); req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]); req.acl.object = obj->name; } for (method = 0; method < obj->n_methods; method++) if (!obj->methods[method].name || !strcmp(obj->methods[method].name, blob_data(attrbuf[UBUS_ATTR_METHOD]))) goto found; /* not found */ ret = UBUS_STATUS_METHOD_NOT_FOUND; goto send; found: ret = obj->methods[method].handler(ctx, obj, &req, blob_data(attrbuf[UBUS_ATTR_METHOD]), attrbuf[UBUS_ATTR_DATA]); if (req.deferred || no_reply) return; send: ubus_complete_deferred_request(ctx, &req, ret); } void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf) { void (*cb)(struct ubus_context *, struct ubus_msghdr *, struct ubus_object *, struct blob_attr **); struct ubus_msghdr *hdr = &buf->hdr; struct blob_attr **attrbuf; struct ubus_object *obj; uint32_t objid; void *prev_data = NULL; attrbuf = ubus_parse_msg(buf->data); if (!attrbuf[UBUS_ATTR_OBJID]) return; objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]); obj = avl_find_element(&ctx->objects, &objid, obj, avl); switch (hdr->type) { case UBUS_MSG_INVOKE: cb = ubus_process_invoke; break; case UBUS_MSG_UNSUBSCRIBE: cb = ubus_process_unsubscribe; break; case UBUS_MSG_NOTIFY: cb = ubus_process_notify; break; default: return; } if (buf == &ctx->msgbuf) { prev_data = buf->data; buf->data = NULL; } cb(ctx, hdr, obj, attrbuf); if (prev_data) { if (buf->data) free(prev_data); else buf->data = prev_data; } }
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 watchdog_set(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__WDT_MAX]; const char *status; if (!msg) return UBUS_STATUS_INVALID_ARGUMENT; blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg)); if (tb[WDT_FREQUENCY]) { unsigned int timeout = watchdog_timeout(0); unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]); if (freq) { if (freq > timeout / 2) freq = timeout / 2; watchdog_frequency(freq); } } if (tb[WDT_TIMEOUT]) { unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]); unsigned int frequency = watchdog_frequency(0); if (timeout <= frequency) timeout = frequency * 2; watchdog_timeout(timeout); } if (tb[WDT_STOP]) watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP])); if (watchdog_fd() == NULL) status = "offline"; else if (watchdog_get_stopped()) status = "stopped"; else status = "running"; blob_buf_init(&b, 0); blobmsg_add_string(&b, "status", status); blobmsg_add_u32(&b, "timeout", watchdog_timeout(0)); blobmsg_add_u32(&b, "frequency", watchdog_frequency(0)); ubus_send_reply(ctx, req, b.head); return 0; }