mrp_res_resource_set_t *acquire_resource_set_response(mrp_msg_t *msg, mrp_res_context_t *cx, void **pcursor) { int status; uint32_t rset_id; mrp_res_resource_set_t *rset = NULL; if (!fetch_resource_set_id(msg, pcursor, &rset_id) || !fetch_status(msg, pcursor, &status)) { mrp_log_error("ignoring malformed response to resource set"); goto error; } if (status) { mrp_log_error("acquiring of resource set failed. error code %u",status); goto error; } /* we need the previous resource set because the new one doesn't * tell us the resource set class */ rset = mrp_htbl_lookup(cx->priv->rset_mapping, u_to_p(rset_id)); if (!rset) { mrp_log_error("no rset found!"); goto error; } return rset; error: return NULL; }
static void server_setup(context_t *c) { c->dbus = mrp_dbus_connect(c->ml, c->busaddr, NULL); if (c->dbus == NULL) { mrp_log_error("Failed to create D-BUS connection to '%s' bus.", c->busaddr); exit(1); } c->name = mrp_dbus_get_unique_name(c->dbus); mrp_log_info("Our address is %s on the bus...", c->name ? c->name : "unknown"); if (!mrp_dbus_acquire_name(c->dbus, SERVER_NAME, NULL)) { mrp_log_error("Failed to acquire D-BUS name '%s' on bus '%s'.", SERVER_NAME, c->busaddr); exit(1); } if (!mrp_dbus_export_method(c->dbus, SERVER_PATH, SERVER_INTERFACE, PING, ping_handler, c)) { mrp_log_error("Failed to export D-BUS method '%s'.", PING); exit(1); } }
static int attribute_lua_getfield(lua_State *L) { attribute_lua_t *attribute = attribute_lua_check(L, 1); resource_lua_t *res = attribute->parent; resource_set_lua_t *rset = res->parent; mrp_attr_t attribute_list[MAX_ATTRS], *attrs; const char *key; mrp_debug("> attribute_lua_getfield"); /* attributes are indexed by string */ if (lua_type(L, 2) != LUA_TSTRING) return luaL_error(L, "invalid attribute index type (needs to be string)"); key = lua_tostring(L, 2); attrs = mrp_resource_set_read_all_attributes(rset->resource_set, res->resource_name, MAX_ATTRS-1, attribute_list); if (!attrs) return luaL_error(L, "internal resource library error"); while (attrs->name != NULL) { if (strcmp(attrs->name, key) == 0) { switch (attrs->type) { case mqi_string: lua_pushstring(L, attrs->value.string); return 1; case mqi_integer: lua_pushinteger(L, attrs->value.integer); return 1; case mqi_unsignd: if (attrs->value.unsignd > INT_MAX) { /* too big! */ mrp_log_error("Sorry, we don't support big unsigned values right now"); return luaL_error(L, "too big value in attribute"); } else { lua_pushinteger(L, attrs->value.unsignd); } return 1; case mqi_floating: lua_pushnumber(L, attrs->value.floating); return 1; default: mrp_log_error("Unhandled attribute type"); return 1; } } attrs++; } return luaL_error(L, "trying to get a non-existing attribute"); }
static mrp_transport_t *create_transport(pdp_t *pdp, const char *address) { static mrp_transport_evt_t msg_evt, wrt_evt; mrp_transport_evt_t *e; mrp_transport_t *t; mrp_sockaddr_t addr; socklen_t alen; int flags; const char *type; t = NULL; alen = mrp_transport_resolve(NULL, address, &addr, sizeof(addr), &type); if (alen <= 0) { mrp_log_error("Failed to resolve transport address '%s'.", address); return NULL; } flags = MRP_TRANSPORT_REUSEADDR; if (strncmp(address, "wsck", 4) != 0) { e = &msg_evt; e->connection = msg_connect_cb; e->closed = msg_closed_cb; e->recvmsg = msg_recv_cb; e->recvmsgfrom = NULL; } else { e = &wrt_evt; e->connection = wrt_connect_cb; e->closed = wrt_closed_cb; e->recvcustom = wrt_recv_cb; e->recvcustomfrom = NULL; flags |= MRP_TRANSPORT_MODE_CUSTOM; } t = mrp_transport_create(pdp->ctx->ml, type, e, pdp, flags); if (t != NULL) { if (mrp_transport_bind(t, &addr, alen) && mrp_transport_listen(t, 4)) return t; else { mrp_log_error("Failed to bind to transport address '%s'.", address); mrp_transport_destroy(t); } } else mrp_log_error("Failed to create transport '%s'.", address); return NULL; }
static int resource_get_attributes(void *data, lua_State *L, int member, mrp_lua_value_t *v) { resource_lua_t *res = (resource_lua_t *) data; resource_set_lua_t *rset = res->parent; mrp_attr_t attribute_list[MAX_ATTRS], *attrs; mrp_debug("> resource_get_attributes"); lua_newtable(L); attrs = mrp_resource_set_read_all_attributes(rset->resource_set, res->resource_name, MAX_ATTRS-1, attribute_list); while (attrs->name != NULL) { switch (attrs->type) { case mqi_string: lua_pushstring(L, attrs->name); lua_pushstring(L, attrs->value.string); lua_settable(L, -3); break; case mqi_integer: lua_pushstring(L, attrs->name); lua_pushinteger(L, attrs->value.integer); lua_settable(L, -3); break; case mqi_unsignd: if (attrs->value.unsignd > INT_MAX) { /* too big! */ mrp_log_error("Sorry, we don't support big unsigned values right now"); } else { lua_pushstring(L, attrs->name); lua_pushinteger(L, attrs->value.unsignd); lua_settable(L, -3); } break; case mqi_floating: lua_pushstring(L, attrs->name); lua_pushnumber(L, attrs->value.floating); lua_settable(L, -3); break; default: mrp_log_error("Unhandled attribute type"); break; } attrs++; } return 1; }
static int load_config(lua_State *L, const char *path) { if (!luaL_loadfile(L, path) && !lua_pcall(L, 0, 0, 0)) return TRUE; else { mrp_log_error("plugin-lua: failed to load config file %s.", path); mrp_log_error("%s", lua_tostring(L, -1)); lua_settop(L, 0); return FALSE; } }
static int command_cb(srs_client_t *c, int idx, int ntoken, char **tokens, uint32_t *start, uint32_t *end, srs_audiobuf_t *audio) { static const char *events[] = { [CMD_PLAY] = "play", [CMD_STOP] = "stop", [CMD_PAUSE] = "pause", [CMD_NEXT] = "next", [CMD_PREV] = "previous", }; static int nevent = MRP_ARRAY_SIZE(events); wrtc_t *wrtc = (wrtc_t *)c->user_data; const char *event; GVariantBuilder *vb; GVariant *args; MRP_UNUSED(start); MRP_UNUSED(end); MRP_UNUSED(audio); MRP_UNUSED(tokens); MRP_UNUSED(ntoken); if (!wrtc->name) { mrp_log_error("WRT media client: can't relay, got no D-Bus name."); return TRUE; } if (idx < 0 || idx >= nevent) { mrp_log_error("WRT media client: got invalid command #%d.", idx); return TRUE; } else event = events[idx]; mrp_log_info("WRT media client: relaying command %s.", event); vb = g_variant_builder_new(G_VARIANT_TYPE("as")); g_variant_builder_add(vb, "s", event); args = g_variant_new("(as)", vb); g_variant_builder_unref(vb); g_dbus_connection_emit_signal(wrtc->gdbus, NULL, "/srs", "org.tizen.srs", "Result", args, NULL); return TRUE; }
static void prepare_ruleset(mrp_context_t *ctx) { if (ctx->r != NULL) { if (mrp_resolver_prepare(ctx->r)) mrp_log_info("Ruleset prepared for resolution."); else { mrp_log_error("Failed to prepare ruleset for execution."); exit(1); } if (!mrp_resolver_enable_autoupdate(ctx->r, "autoupdate")) { mrp_log_error("Failed to enable resolver autoupdate."); exit(1); } } }
static int config_wrtc(srs_plugin_t *plugin, srs_cfg_t *settings) { wrtc_t *wrtc = (wrtc_t *)plugin->plugin_data; struct { const char **addr; const char *key; const char *def; } config[] = { { &wrtc->config.bus , CONFIG_BUS , DEFAULT_BUS }, { &wrtc->config.play , CONFIG_PLAY , DEFAULT_PLAY }, { &wrtc->config.stop , CONFIG_STOP , DEFAULT_STOP }, { &wrtc->config.pause, CONFIG_PAUSE, DEFAULT_PAUSE }, { &wrtc->config.next , CONFIG_NEXT , DEFAULT_NEXT }, { &wrtc->config.prev , CONFIG_PREV , DEFAULT_PREV }, { NULL, NULL, NULL } }, *cfg; mrp_debug("configure WRT media client plugin"); if (plugin->srs->gl == NULL) { mrp_log_error("The WRT media client plugin requires GMainLoop."); mrp_log_error("Please set the 'gmainloop' config variable true."); if (plugin->srs->config_file != NULL) mrp_log_error("You can do this either in the configuration file" "'%s',\n" "or by passing -s gmainloop=true on the command line", plugin->srs->config_file); else mrp_log_error("You can do this by passing the -s gmainloop=true\n" "command line option."); return FALSE; } for (cfg = config; cfg->key; cfg++) *cfg->addr = srs_get_string_config(settings, cfg->key, cfg->def); mrp_log_info("WRT media client configuration:"); mrp_log_info(" D-Bus: '%s'", wrtc->config.bus); mrp_log_info(" play command: '%s'", wrtc->config.play); mrp_log_info(" stop command: '%s'", wrtc->config.stop); mrp_log_info(" pause command: '%s'", wrtc->config.pause); mrp_log_info(" next command: '%s'", wrtc->config.next); mrp_log_info(" prev command: '%s'", wrtc->config.prev); return TRUE; }
void event_cb(uint32_t request_id, mrp_resource_set_t *resource_set, void *user_data) { resource_set_lua_t *rset = (resource_set_lua_t *) user_data; mrp_resource_mask_t grant, advice; int top; MRP_UNUSED(request_id); MRP_UNUSED(resource_set); mrp_debug("> event_cb"); top = lua_gettop(rset->L); grant = mrp_get_resource_set_grant(rset->resource_set); advice = mrp_get_resource_set_advice(rset->resource_set); /* update resource set */ rset->acquired = !!grant; rset->available = !!advice; if (mrp_lua_object_deref_value(rset, rset->L, rset->callback, false)) { mrp_lua_push_object(rset->L, rset); if (lua_pcall(rset->L, 1, 0, 0) != 0) mrp_log_error("failed to invoke Lua resource set callback: %s", lua_tostring(rset->L, -1)); } lua_settop(rset->L, top); }
static void player_free(void *key, void *object) { player_t *player = (player_t *)object; context_t *ctx; srs_plugin_t *pl; srs_context_t *srs; pa_mainloop_api *api; if (strcmp(key, player->name)) mrp_log_error("mpris2-client: corrupt hashtable (key '%s')", (char *)key); else { if (player->timer && (ctx = player->ctx) && (pl = ctx->plugin) && (srs = pl->srs) && (api = srs->pa)) { api->time_free(player->timer); } mrp_free((void *)player->name); mrp_free((void *)player->service); mrp_free((void *)player->object); mrp_free((void *)player); playlist_free(player->nlist, player->lists); } }
static const char *load_libs(loader_t *l) { char path[PATH_MAX], *err; void *h; int i, j; for (i = 0; i < l->nlib; i++) { for (j = 0; j < l->ndir; j++) { mrp_log_info("Looking for %s in %s...", l->libs[i], l->dirs[j]); if (find_matching(path, sizeof(path), l->dirs[j], l->libs[i])) { h = dlopen(path, RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND); if (h != NULL) { mrp_log_info("Preloaded %s.", path); l->handles[i] = h; break; } else { err = dlerror(); mrp_log_warning("Failed to load %s (error: %s).", l->libs[i], err ? err : "unknown"); } } } if (l->handles[i] == NULL) { mrp_log_error("Failed to preload %s.", l->libs[i]); return l->libs[i]; } } return NULL; }
static void wrt_recv_cb(mrp_transport_t *t, void *data, void *user_data) { pep_proxy_t *proxy = (pep_proxy_t *)user_data; char *name = proxy && proxy->name ? proxy->name : "<unknown>"; msg_t *msg; int seqno; MRP_UNUSED(t); /* mrp_log_info("Message from WRT client %p:", proxy); */ msg = json_decode_message(data); if (msg != NULL) { process_message(proxy, msg); msg_free_message(msg); } else { if (!mrp_json_get_integer(data, "seq", &seqno)) seqno = 0; mrp_log_error("Failed to decode message from %s.", name); msg_send_nak(proxy, seqno, 1, "failed to decode message"); } }
void clients_player_disappeared(context_t *ctx, const char *name) { clients_t *clients; player_t *player, *removed; if (ctx && (clients = ctx->clients) && clients->player.name) { if ((player = mrp_htbl_lookup(clients->player.name, (void *)name))) { removed = mrp_htbl_remove(clients->player.addr, (void *)player->address, FALSE); if (player != removed) { mrp_log_error("mpris2 client: confused with data structures " "when removing '%s'", player->address); } else { mrp_free((void *)player->address); player->address = NULL; player->state = UNKNOWN; player->ready = false; playlist_free(player->nlist, player->lists); player->nlist = 0; player->lists = NULL; player->active_list = NULL; mrp_log_info("mrpis2 client '%s' disappeared", name); } } } }
static void ignore_invalid_request(wrt_client_t *c, mrp_json_t *req, ...) { MRP_UNUSED(c); MRP_UNUSED(req); mrp_log_error("Ignoring invalid WRT resource request"); }
void mainloop_cleanup(test_config_t *cfg) { switch (cfg->mainloop_type) { case MAINLOOP_NATIVE: break; case MAINLOOP_PULSE: pulse_mainloop_cleanup(cfg); break; case MAINLOOP_ECORE: ecore_mainloop_cleanup(cfg); break; case MAINLOOP_GLIB: glib_mainloop_cleanup(cfg); break; #ifdef QT_ENABLED case MAINLOOP_QT: qt_mainloop_cleanup(cfg->ml); cfg->ml = NULL; break; #endif default: mrp_log_error("Unknown mainloop type (0x%x).", cfg->mainloop_type); exit(1); } }
static void wrt_connect_cb(mrp_transport_t *t, void *user_data) { static proxy_ops_t ops = { .send_msg = wrt_op_send_msg, .unref = wrt_op_unref_msg, .create_notify = wrt_op_create_notify, .update_notify = wrt_op_update_notify, .send_notify = wrt_op_send_notify, .free_notify = wrt_op_free_notify, }; pdp_t *pdp = (pdp_t *)user_data; pep_proxy_t *proxy; int flags; proxy = create_proxy(pdp); if (proxy != NULL) { flags = MRP_TRANSPORT_REUSEADDR | MRP_TRANSPORT_NONBLOCK; proxy->t = mrp_transport_accept(t, proxy, flags); if (proxy->t != NULL) { proxy->ops = &ops; mrp_log_info("Accepted new client connection."); } else { mrp_log_error("Failed to accept new client connection."); destroy_proxy(proxy); } } }
int glib_mainloop_cleanup(test_config_t *cfg) { MRP_UNUSED(cfg); mrp_log_error("glib mainloop support is not available."); exit(1); }
static void mainloop_quit(test_config_t *cfg) { switch (cfg->mainloop_type) { case MAINLOOP_NATIVE: mrp_mainloop_quit(cfg->ml, 0); break; case MAINLOOP_PULSE: pulse_mainloop_quit(cfg); break; case MAINLOOP_ECORE: ecore_mainloop_quit(cfg); break; case MAINLOOP_GLIB: glib_mainloop_quit(cfg); break; #ifdef QT_ENABLED case MAINLOOP_QT: qt_mainloop_quit(); break; #endif default: mrp_log_error("Invalid mainloop type 0x%x.", cfg->mainloop_type); exit(1); } }
static void server_status_cb(mrp_dbus_t *dbus, const char *name, int up, const char *owner, void *user_data) { context_t *c = (context_t *)user_data; MRP_UNUSED(dbus); MRP_UNUSED(name); if (up) { mrp_log_info("%s came up (as %s)", name, owner); if (c->timer == NULL) { c->timer = mrp_add_timer(c->ml, 1000, send_cb, c); if (c->timer == NULL) { mrp_log_error("Failed to create D-BUS sending timer."); exit(1); } } } else { mrp_log_info("%s went down", name); if (c->timer != NULL) { mrp_del_timer(c->timer); c->timer = NULL; } } }
int mrp_attribute_copy_definitions(mrp_attr_def_t *from, mrp_attr_def_t *to) { mrp_attr_def_t *s, *d; MRP_ASSERT(to,"invalid argument"); if (from) { for (s = from, d = to; s->name; s++, d++) { if (!(d->name = mrp_strdup(s->name))) goto no_memory; d->access = s->access; if ((d->type = s->type) != mqi_string) d->value = s->value; else { if (!(d->value.string = mrp_strdup(s->value.string))) { mrp_free((void *)d->name); memset(d, 0, sizeof(*d)); goto no_memory; } } } } return 0; no_memory: mrp_log_error("Memory alloc failure. Can't copy attribute definition"); return -1; }
static void ignore_unknown_request(wrt_client_t *c, mrp_json_t *req, const char *type) { MRP_UNUSED(c); MRP_UNUSED(req); mrp_log_error("Ignoring unknown WRT resource request '%s'", type); }
static void client_setup(context_t *c) { const char *dest; c->dbus = mrp_dbus_connect(c->ml, c->busaddr, NULL); if (c->dbus == NULL) { mrp_log_error("Failed to create D-BUS connection to '%s' bus.", c->busaddr); exit(1); } c->name = mrp_dbus_get_unique_name(c->dbus); mrp_log_info("Our address is %s on the bus...", c->name ? c->name : "unknown"); mrp_dbus_follow_name(c->dbus, SERVER_NAME, server_status_cb, c); if (c->all_pongs) { mrp_log_info("Subscribing for all pong signals..."); dest = NULL; } else { mrp_log_info("Subscribing only for pong signals to us..."); dest = c->name; } if (!mrp_dbus_subscribe_signal(c->dbus, pong_handler, c, dest, SERVER_PATH, SERVER_INTERFACE, PONG, NULL)) { mrp_log_error("Failed to subscribe for signal '%s/%s.%s'.", SERVER_PATH, SERVER_INTERFACE, PONG); exit(1); } c->timer = mrp_add_timer(c->ml, 1000, send_cb, c); if (c->timer == NULL) { mrp_log_error("Failed to create D-BUS sending timer."); exit(1); } }
static void setup_logging(mrp_context_t *ctx) { const char *target; target = mrp_log_parse_target(ctx->log_target); if (!target) mrp_log_error("invalid log target '%s'", ctx->log_target); else mrp_log_set_target(target); }
static int sdlogger_init(mrp_plugin_t *plugin) { MRP_UNUSED(plugin); if (mrp_log_register_target("systemd", sdlogger, NULL)) mrp_log_info("systemd: registered logging target."); else mrp_log_error("systemd: failed to register logging target."); return TRUE; }
static void daemonize(mrp_context_t *ctx) { if (!ctx->foreground) { mrp_log_info("Switching to daemon mode."); if (!mrp_daemonize("/", "/dev/null", "/dev/null")) { mrp_log_error("Failed to daemonize."); exit(1); } } }
static int resource_set_get_resources(void *data, lua_State *L, int member, mrp_lua_value_t *v) { resource_set_lua_t *rset; void *iter = NULL; mrp_resource_t *resource; mrp_resource_mask_t grant, advice; MRP_UNUSED(member); MRP_UNUSED(v); mrp_debug("> resource_set_get_resources"); rset = (resource_set_lua_t *) data; if (!rset) return luaL_error(L, "internal error"); grant = mrp_get_resource_set_grant(rset->resource_set); advice = mrp_get_resource_set_advice(rset->resource_set); lua_newtable(L); /* push all resource objects to a table and return it */ while ((resource = mrp_resource_set_iterate_resources(rset->resource_set, &iter))) { const char *name = mrp_resource_get_name(resource); mrp_resource_mask_t mask = mrp_resource_get_mask(resource); /* fetch and update the resource object */ resource_lua_t *res = (resource_lua_t *) mrp_htbl_lookup(rset->resources, (void *) name); if (!res) { mrp_log_error("resources out of sync: %s not found", name); continue; } /* mrp_lua_object_ref_value(res, L, 0); */ res->acquired = !!(mask & grant); res->available = !!(mask & advice); /* TODO: update attributes */ /* push the resource to the table */ lua_pushstring(L, res->resource_name); mrp_lua_push_object(L, res); lua_settable(L, -3); } return 1; }
mrp_res_string_array_t *class_query_response(mrp_msg_t *msg, void **pcursor) { int status; mrp_res_string_array_t *arr = NULL; if (!fetch_status(msg, pcursor, &status) || (status == 0 && !fetch_mrp_str_array(msg, pcursor, RESPROTO_CLASS_NAME, &arr))) { mrp_log_error("ignoring malformed response to class query"); return NULL; } if (status) { mrp_log_error("class query failed with error code %u", status); mrp_res_free_string_array(arr); return NULL; } return arr; }
static int create_loader(srs_plugin_t *plugin) { const char *failed; MRP_UNUSED(plugin); mrp_clear(&loader); if (check_config(&loader) != 0) { mrp_log_error("Failed to get configuration (%d: %s).", errno, strerror(errno)); return FALSE; } if ((failed = load_libs(&loader)) != NULL) { mrp_log_error("Failed to preload library '%s'.", failed); return FALSE; } else return TRUE; }
static void connection_evt(mrp_transport_t *lt, void *user_data) { server_t *s = (server_t *)user_data; client_t *c; c = create_client(s, lt); if (c != NULL) mrp_log_info("Accepted new native client connection."); else mrp_log_error("Failed to accept new native client connection."); }