Esempio n. 1
0
mrp_attr_t *mrp_resource_set_get_attribute_by_name(
        mrp_resource_set_t *resource_set, const char *resource_name,
        const char *attribute_name)
{
    mrp_attr_t *attr = NULL, *attrs;
    uint32_t res_id;
    mrp_attr_t attr_buf[128];
    uint32_t attr_idx = 0;

    memset(attr_buf, 0, sizeof(attr_buf));

    res_id = mrp_resource_definition_get_resource_id_by_name(resource_name);
    attrs = mrp_resource_definition_read_all_attributes(res_id, 128, attr_buf);

    if (!attrs)
        return NULL;

    while (attrs->name != NULL) {
        if (strcmp(attrs->name, attribute_name) == 0) {

            mrp_attr_t *buf = mrp_allocz(sizeof(mrp_attr_t));
            mrp_resource_set_read_attribute(resource_set, resource_name,
                    attr_idx, buf);

            attr = buf;

            break;
        }
        attr_idx++;
        attrs++;
    }

    return attr;
}
static void print_resources_cb(mrp_console_t *c, void *user_data,
                               int argc, char **argv)
{
    const char **names;
    mrp_attr_t  *attrs, *a;
    mrp_attr_t   buf[ATTRIBUTE_MAX];
    uint32_t     resid;

    MRP_UNUSED(c);
    MRP_UNUSED(user_data);
    MRP_UNUSED(argc);
    MRP_UNUSED(argv);

    if (!(names = mrp_resource_definition_get_all_names(0, NULL))) {
        printf("Failed to read resource definitions.\n");
        return;
    }

    printf("Resource definitions:\n");
    for (resid = 0; names[resid]; resid++) {
        attrs = mrp_resource_definition_read_all_attributes(resid,
                                                            ATTRIBUTE_MAX, buf);
        printf("    Resource '%s'\n", names[resid]);
        for (a = attrs; a->name; a++) {
            printf("        attribute %s: ", a->name);
            switch (a->type) {
            case mqi_string:
                printf("'%s'\n", a->value.string);
                break;
            case mqi_integer:
                printf("%d\n", a->value.integer);
                break;
            case mqi_unsignd:
                printf("%u\n", a->value.unsignd);
                break;
            case mqi_floating:
                printf("%f\n", a->value.floating);
                break;
            default:
                printf("<unsupported type>\n");
                break;
            }
        }
    }

    mrp_free(names);
}
Esempio n. 3
0
static void query_resources(wrt_client_t *c, mrp_json_t *req)
{
    const char  *type = RESWRT_QUERY_RESOURCES;
    mrp_json_t  *reply, *rarr, *r, *ao;
    const char **resources;
    int          seq, cnt;
    mrp_attr_t  *attrs, *a;
    mrp_attr_t   buf[ATTRIBUTE_MAX];
    uint32_t     id;

    if (!mrp_json_get_integer(req, "seq", &seq)) {
        ignore_invalid_request(c, req, "missing 'seq' field");
        return;
    }

    rarr = r = ao = NULL;
    resources = mrp_resource_definition_get_all_names(0, NULL);

    if (resources == NULL) {
        error_reply(c, type, seq, ENOMEM, "failed to query class names");
        return;
    }

    reply = alloc_reply(type, seq);

    if (reply == NULL)
        return;

    rarr = mrp_json_create(MRP_JSON_ARRAY);

    if (rarr == NULL)
        goto fail;

    for (id = 0; resources[id]; id++) {
        r = mrp_json_create(MRP_JSON_OBJECT);

        if (r == NULL)
            goto fail;

        if (!mrp_json_add_string (r, "name", resources[id]))
            goto fail;

        attrs = mrp_resource_definition_read_all_attributes(id,
                                                            ATTRIBUTE_MAX, buf);

        ao = mrp_json_create(MRP_JSON_OBJECT);

        if (ao == NULL)
            goto fail;

        for (a = attrs, cnt = 0; a->name; a++, cnt++) {
            switch (a->type) {
            case mqi_string:
                if (!mrp_json_add_string(ao, a->name, a->value.string))
                    goto fail;

                break;
            case mqi_integer:
            case mqi_unsignd:
                if (!mrp_json_add_integer(ao, a->name, a->value.integer))
                    goto fail;
                break;
            case mqi_floating:
                if (!mrp_json_add_double(ao, a->name, a->value.floating))
                    goto fail;
                break;
            default:
                mrp_log_error("attribute '%s' of resource '%s' "
                              "has unknown type %d", a->name, resources[id],
                              a->type);
                break;
            }
        }

        if (cnt > 0)
            mrp_json_add(r, "attributes", ao);
        else
            mrp_json_unref(ao);

        ao = NULL;

        if (!mrp_json_array_append(rarr, r))
            goto fail;
        else
            r = NULL;
    }

    if (mrp_json_add_integer(reply, "status"   , 0)) {
        mrp_json_add        (reply, "resources", rarr);
        send_message(c, reply);
    }

    mrp_json_unref(reply);
    mrp_free(resources);
    return;

 fail:
    mrp_json_unref(reply);
    mrp_json_unref(rarr);
    mrp_json_unref(r);
    mrp_json_unref(ao);
    mrp_free(resources);
}