diff options
Diffstat (limited to 'lib/ubus.c')
-rw-r--r-- | lib/ubus.c | 127 |
1 files changed, 67 insertions, 60 deletions
@@ -24,7 +24,7 @@ #define err_return(err) do { last_error = err; return NULL; } while(0) static enum ubus_msg_status last_error = 0; -static uc_ressource_type *conn_type; +static uc_ressource_type_t *conn_type; typedef struct { int timeout; @@ -32,28 +32,28 @@ typedef struct { struct ubus_context *ctx; } ubus_connection; -static json_object * +static uc_value_t * uc_ubus_error(uc_vm *vm, size_t nargs) { - json_object *errmsg; + uc_value_t *errmsg; if (last_error == 0) return NULL; - errmsg = json_object_new_string(ubus_strerror(last_error)); + errmsg = ucv_string_new(ubus_strerror(last_error)); last_error = 0; return errmsg; } -static json_object * -uc_blob_to_json(struct blob_attr *attr, bool table, const char **name); +static uc_value_t * +uc_blob_to_json(uc_vm *vm, struct blob_attr *attr, bool table, const char **name); -static json_object * -uc_blob_array_to_json(struct blob_attr *attr, size_t len, bool table) +static uc_value_t * +uc_blob_array_to_json(uc_vm *vm, struct blob_attr *attr, size_t len, bool table) { - json_object *o = table ? json_object_new_object() : json_object_new_array(); - json_object *v; + uc_value_t *o = table ? ucv_object_new(vm) : ucv_array_new(vm); + uc_value_t *v; struct blob_attr *pos; size_t rem = len; const char *name; @@ -63,21 +63,21 @@ uc_blob_array_to_json(struct blob_attr *attr, size_t len, bool table) __blob_for_each_attr(pos, attr, rem) { name = NULL; - v = uc_blob_to_json(pos, table, &name); + v = uc_blob_to_json(vm, pos, table, &name); if (table && name) - json_object_object_add(o, name, v); + ucv_object_add(o, name, v); else if (!table) - json_object_array_add(o, v); + ucv_array_push(o, v); else - json_object_put(v); + ucv_put(v); } return o; } -static json_object * -uc_blob_to_json(struct blob_attr *attr, bool table, const char **name) +static uc_value_t * +uc_blob_to_json(uc_vm *vm, struct blob_attr *attr, bool table, const char **name) { void *data; int len; @@ -93,16 +93,16 @@ uc_blob_to_json(struct blob_attr *attr, bool table, const char **name) switch (blob_id(attr)) { case BLOBMSG_TYPE_BOOL: - return json_object_new_boolean(*(uint8_t *)data); + return ucv_boolean_new(*(uint8_t *)data); case BLOBMSG_TYPE_INT16: - return json_object_new_int64((int64_t)be16_to_cpu(*(uint16_t *)data)); + return ucv_int64_new((int64_t)be16_to_cpu(*(uint16_t *)data)); case BLOBMSG_TYPE_INT32: - return json_object_new_int64((int64_t)be32_to_cpu(*(uint32_t *)data)); + return ucv_int64_new((int64_t)be32_to_cpu(*(uint32_t *)data)); case BLOBMSG_TYPE_INT64: - return json_object_new_uint64(be64_to_cpu(*(uint64_t *)data)); + return ucv_uint64_new(be64_to_cpu(*(uint64_t *)data)); case BLOBMSG_TYPE_DOUBLE: ; @@ -113,16 +113,16 @@ uc_blob_to_json(struct blob_attr *attr, bool table, const char **name) v.u64 = be64_to_cpu(*(uint64_t *)data); - return json_object_new_double(v.d); + return ucv_double_new(v.d); case BLOBMSG_TYPE_STRING: - return json_object_new_string(data); + return ucv_string_new(data); case BLOBMSG_TYPE_ARRAY: - return uc_blob_array_to_json(data, len, false); + return uc_blob_array_to_json(vm, data, len, false); case BLOBMSG_TYPE_TABLE: - return uc_blob_array_to_json(data, len, true); + return uc_blob_array_to_json(vm, data, len, true); default: return NULL; @@ -130,16 +130,16 @@ uc_blob_to_json(struct blob_attr *attr, bool table, const char **name) } -static json_object * +static uc_value_t * uc_ubus_connect(uc_vm *vm, size_t nargs) { - json_object *socket = uc_get_arg(0); - json_object *timeout = uc_get_arg(1); - json_object *co; + uc_value_t *socket = uc_get_arg(0); + uc_value_t *timeout = uc_get_arg(1); + uc_value_t *co; ubus_connection *c; - if ((socket && !json_object_is_type(socket, json_type_string)) || - (timeout && !json_object_is_type(timeout, json_type_int))) + if ((socket && ucv_type(socket) != UC_STRING) || + (timeout && ucv_type(timeout) != UC_INTEGER)) err_return(UBUS_STATUS_INVALID_ARGUMENT); c = calloc(1, sizeof(*c)); @@ -147,8 +147,8 @@ uc_ubus_connect(uc_vm *vm, size_t nargs) if (!c) err_return(UBUS_STATUS_UNKNOWN_ERROR); - c->ctx = ubus_connect(socket ? json_object_get_string(socket) : NULL); - c->timeout = timeout ? json_object_get_int(timeout) : 30; + c->ctx = ubus_connect(socket ? ucv_string_get(socket) : NULL); + c->timeout = timeout ? ucv_int64_get(timeout) : 30; if (!c->ctx) { free(c); @@ -158,7 +158,7 @@ uc_ubus_connect(uc_vm *vm, size_t nargs) if (c->timeout < 0) c->timeout = 30; - co = json_object_new_object(); + co = ucv_object_new(vm); if (!co) { ubus_free(c->ctx); @@ -174,16 +174,16 @@ uc_ubus_connect(uc_vm *vm, size_t nargs) static void uc_ubus_signatures_cb(struct ubus_context *c, struct ubus_object_data *o, void *p) { - json_object *arr = p; - json_object *sig; + uc_value_t *arr = p; + uc_value_t *sig; if (!o->signature) return; - sig = uc_blob_array_to_json(blob_data(o->signature), blob_len(o->signature), true); + sig = uc_blob_array_to_json(NULL, blob_data(o->signature), blob_len(o->signature), true); if (sig) - json_object_array_add(arr, sig); + ucv_array_push(arr, sig); } static void @@ -198,27 +198,27 @@ uc_ubus_objects_cb(struct ubus_context *c, struct ubus_object_data *o, void *p) json_object_array_add(arr, obj); } -static json_object * +static uc_value_t * uc_ubus_list(uc_vm *vm, size_t nargs) { ubus_connection **c = uc_get_self("ubus.connection"); - json_object *objname = uc_get_arg(0); - json_object *res = NULL; + uc_value_t *objname = uc_get_arg(0); + uc_value_t *res = NULL; enum ubus_msg_status rv; if (!c || !*c || !(*c)->ctx) err_return(UBUS_STATUS_CONNECTION_FAILED); - if (objname && !json_object_is_type(objname, json_type_string)) + if (objname && ucv_type(objname) != UC_STRING) err_return(UBUS_STATUS_INVALID_ARGUMENT); - res = json_object_new_array(); + res = ucv_array_new(vm); if (!res) err_return(UBUS_STATUS_UNKNOWN_ERROR); rv = ubus_lookup((*c)->ctx, - objname ? json_object_get_string(objname) : NULL, + objname ? ucv_string_get(objname) : NULL, objname ? uc_ubus_signatures_cb : uc_ubus_objects_cb, res); @@ -231,41 +231,48 @@ uc_ubus_list(uc_vm *vm, size_t nargs) static void uc_ubus_call_cb(struct ubus_request *req, int type, struct blob_attr *msg) { - json_object **res = (json_object **)req->priv; + uc_value_t **res = (uc_value_t **)req->priv; - *res = msg ? uc_blob_array_to_json(blob_data(msg), blob_len(msg), true) : NULL; + *res = msg ? uc_blob_array_to_json(NULL, blob_data(msg), blob_len(msg), true) : NULL; } -static json_object * +static uc_value_t * uc_ubus_call(uc_vm *vm, size_t nargs) { ubus_connection **c = uc_get_self("ubus.connection"); - json_object *objname = uc_get_arg(0); - json_object *funname = uc_get_arg(1); - json_object *funargs = uc_get_arg(2); - json_object *res = NULL; + uc_value_t *objname = uc_get_arg(0); + uc_value_t *funname = uc_get_arg(1); + uc_value_t *funargs = uc_get_arg(2); + uc_value_t *res = NULL; + json_object *o; enum ubus_msg_status rv; uint32_t id; if (!c || !*c || !(*c)->ctx) err_return(UBUS_STATUS_CONNECTION_FAILED); - if (!json_object_is_type(objname, json_type_string) || - !json_object_is_type(funname, json_type_string) || - (funargs && !json_object_is_type(funargs, json_type_object))) + if (ucv_type(objname) != UC_STRING || + ucv_type(funname) != UC_STRING || + (funargs && ucv_type(funargs) != UC_OBJECT)) err_return(UBUS_STATUS_INVALID_ARGUMENT); blob_buf_init(&(*c)->buf, 0); - if (funargs && !blobmsg_add_object(&(*c)->buf, funargs)) - err_return(UBUS_STATUS_UNKNOWN_ERROR); + if (funargs) { + o = ucv_to_json(funargs); + rv = blobmsg_add_object(&(*c)->buf, o); + json_object_put(o); + + if (!rv) + err_return(UBUS_STATUS_UNKNOWN_ERROR); + } - rv = ubus_lookup_id((*c)->ctx, json_object_get_string(objname), &id); + rv = ubus_lookup_id((*c)->ctx, ucv_string_get(objname), &id); if (rv != UBUS_STATUS_OK) err_return(rv); - rv = ubus_invoke((*c)->ctx, id, json_object_get_string(funname), (*c)->buf.head, + rv = ubus_invoke((*c)->ctx, id, ucv_string_get(funname), (*c)->buf.head, uc_ubus_call_cb, &res, (*c)->timeout * 1000); if (rv != UBUS_STATUS_OK) @@ -274,7 +281,7 @@ uc_ubus_call(uc_vm *vm, size_t nargs) return res; } -static json_object * +static uc_value_t * uc_ubus_disconnect(uc_vm *vm, size_t nargs) { ubus_connection **c = uc_get_self("ubus.connection"); @@ -285,7 +292,7 @@ uc_ubus_disconnect(uc_vm *vm, size_t nargs) ubus_free((*c)->ctx); (*c)->ctx = NULL; - return json_object_new_boolean(true); + return ucv_boolean_new(true); } @@ -313,7 +320,7 @@ static void close_connection(void *ud) { free(conn); } -void uc_module_init(uc_prototype *scope) +void uc_module_init(uc_value_t *scope) { uc_add_proto_functions(scope, global_fns); |