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 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 void blobmsg_format_element(struct blob_attr *data, int next_indent) { switch (blobmsg_type(data)) { case BLOBMSG_TYPE_UNSPEC: printf("null\n"); break; case BLOBMSG_TYPE_STRING: printf("%s\n", blobmsg_get_string(data)); break; case BLOBMSG_TYPE_BOOL: printf("%s\n", blobmsg_get_u8(data) ? "true" : "false"); break; case BLOBMSG_TYPE_INT16: printf("%d\n", blobmsg_get_u16(data)); break; case BLOBMSG_TYPE_INT32: printf("%d\n", blobmsg_get_u32(data)); break; case BLOBMSG_TYPE_INT64: printf("%"PRIu64"\n", blobmsg_get_u64(data)); break; case BLOBMSG_TYPE_TABLE: case BLOBMSG_TYPE_ARRAY: blobmsg_format_std_indent(data, next_indent); break; } }
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_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 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; }
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 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_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 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; }