Esempio n. 1
0
resctl_t *resctl_init(void)
{
    resctl_t              *ctl;
    mrp_resource_client_t *client;
    mrp_resource_set_t    *set;
    const char            *zone = "driver";
    const char            *play = "audio_playback";
    const char            *rec  = "audio_recording";
    const char            *cls  = "phone";
    bool                   ar   = false;
    uint32_t               prio = 1;               /* what is this ? */

    ctl    = NULL;
    client = NULL;
    set    = NULL;

    ctl = mrp_allocz(sizeof(*ctl));

    if (ctl == NULL) {
        mrp_log_error("Failed to initialize telephony resource control.");
        goto fail;
    }

    client = mrp_resource_client_create("telephony", ctl);

    if (client == NULL) {
        mrp_log_error("Failed to create telephony resource client.");
        goto fail;
    }

    set = mrp_resource_set_create(client, ar, prio, event_cb, ctl);

    if (set == NULL) {
        mrp_log_error("Failed to create telephony resource set.");
        goto fail;
    }

    if (mrp_resource_set_add_resource(set, play, false, NULL, true) < 0 ||
            mrp_resource_set_add_resource(set, rec , false, NULL, true) < 0) {
        mrp_log_error("Failed to initialize telephony resource set.");
        goto fail;
    }

    if (mrp_application_class_add_resource_set(cls, zone, set, 0) != 0) {
        mrp_log_error("Failed to assign telephony resource set with class.");
        goto fail;
    }

    ctl->client = client;
    ctl->set    = set;
    ctl->seqno  = 1;

    return ctl;

fail:
    if (set != NULL)
        mrp_resource_set_destroy(set);

    if (client != NULL)
        mrp_resource_client_destroy(client);

    mrp_free(ctl);

    return NULL;
}
Esempio n. 2
0
static int resource_set_add_resource(lua_State *L)
{
    int narg;
    resource_set_lua_t *rset;
    resource_lua_t *resource;
    const char *resource_name;
    bool shared = FALSE;
    bool mandatory = TRUE;
    mrp_attr_t attribute_list[MAX_ATTRS], *attrs;

    mrp_debug("> add_resource");

    narg = lua_gettop(L);

    if (narg != 2)
        return luaL_error(L, "expecting one argument");

    rset = resource_set_lua_check(L, 1);

    if (!rset)
        goto error;

    /* the argument should be a table with at least "resource_name" index */

    if (!lua_istable(L, -1))
        return luaL_error(L, "argument error -- not a table");

    lua_pushstring(L, "resource_name");
    lua_gettable(L, -2);

    if (!lua_isstring(L, -1))
        return luaL_error(L, "'resource_name' is a mandatory field");

    resource_name = lua_tostring(L, -1);
    lua_pop(L, 1);

    lua_pushstring(L, "mandatory");
    lua_gettable(L, -2);

    if (lua_isboolean(L, -1)) {
        mandatory = lua_toboolean(L, -1);
    }
    lua_pop(L, 1);

    lua_pushstring(L, "shared");
    lua_gettable(L, -2);

    if (lua_isboolean(L, -1)) {
        shared = lua_toboolean(L, -1);
    }
    lua_pop(L, 1);

    /* create resource object and add it to the resource table in the resource
     * set object */

    resource = (resource_lua_t *) mrp_lua_create_object(L, RESOURCE_LUA_CLASS,
            NULL, 0);

    if (!resource)
        goto error;

    /* mrp_lua_object_ref_value(resource, L, 0); */

    resource->mandatory = mandatory;
    resource->shared = shared;
    resource->acquired = FALSE;
    resource->available = FALSE;
    resource->resource_name = mrp_strdup(resource_name);

    if (!resource->resource_name)
        goto error;

    resource->parent = rset;
    resource->L = L;

    resource->real_attributes = (attribute_lua_t *) mrp_lua_create_object(L,
            ATTRIBUTE_LUA_CLASS, NULL, 0);

    if (!resource->real_attributes)
        goto error;

    /* mrp_lua_object_ref_value(resource->real_attributes, L, 0); */

    resource->real_attributes->L = L;
    resource->real_attributes->parent = resource;
    resource->real_attributes->resource_set = rset;
    resource->real_attributes->initialized = TRUE;

    attrs = mrp_resource_set_read_all_attributes(rset->resource_set,
            resource->resource_name, MAX_ATTRS-1, attribute_list);

    if (mrp_resource_set_add_resource(rset->resource_set,
            resource->resource_name, shared, attrs, mandatory) < 0)
        goto error;

    /* add to resource map */

    mrp_debug("inserted resource %s to %p", resource->resource_name, rset);
    mrp_htbl_insert(rset->resources, resource->resource_name, resource);

    return 0;

error:
    /* TODO: clean up the already allocated objects */

    return luaL_error(L, "internal resource library error");
}