/* on call, propagate it to the dbus service */ static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb) { size_t size; int rc; char *method = strndupa(verb, lenverb); struct dbus_memo *memo; /* create the recording data */ memo = api_dbus_client_make_memo(req, context); if (memo == NULL) { afb_req_fail(req, "error", "out of memory"); return; } /* makes the call */ rc = sd_bus_call_method_async(api->sdbus, NULL, api->name, api->path, api->name, method, api_dbus_client_on_reply, memo, "ssu", afb_req_raw(req, &size), ctxClientGetUuid(context->session), (uint32_t)context->flags); /* if there was an error report it directly */ if (rc < 0) { errno = -rc; afb_req_fail(req, "error", "dbus error"); api_dbus_client_free_memo(memo); } }
static void seek (struct afb_req request) { /* AFB_SESSION_CHECK */ mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; /* check that context is initialized */ if (ctx == NULL) { afb_req_fail (request, "failed", "uninitialized"); return; } /* no "?value=" parameter : return error */ if (!value) { afb_req_fail (request, "failed", "you must provide a time"); return; } if (!_rygel_do (ctx, SEEK, (char *)value)) { afb_req_fail (request, "failed", "could not seek chosen media"); return; } jresp = json_object_new_object(); json_object_object_add (jresp, "seek", json_object_new_string ("success")); afb_req_success (request, jresp, "Media - Sought"); }
static void embed_call_void(struct afb_req request, const char *method) { struct memo *memo = make_memo(request, method); if (memo == NULL) afb_req_fail(request, "failed", "out of memory"); else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) { afb_req_fail(request, "failed", "dbus failure"); free(memo); } }
static void selecting (struct afb_req request) { /* AFB_SESSION_CHECK */ mediaCtxHandleT *ctx = afb_req_context_get(request); const char *value = afb_req_value (request, "value"); json_object *jresp; unsigned int index; char index_str[5]; /* check that context is initialized */ if (ctx == NULL) { afb_req_fail (request, "failed", "uninitialized"); return; } /* no "?value=" parameter : return current index */ if (!value) { snprintf (index_str, sizeof(index_str), "%d", ctx->index); jresp = json_object_new_object(); json_object_object_add (jresp, "index", json_object_new_string (index_str)); } /* "?value=" parameter is negative */ else if (atoi(value) < 0) { afb_req_fail (request, "failed", "chosen index cannot be negative"); return; } /* "?value=" parameter is positive */ else if (atoi(value) >= 0) { index = (unsigned int) atoi(value); if (!_rygel_select (ctx, index)) { afb_req_fail (request, "failed", "chosen index superior to current media count"); return; } ctx->index = index; jresp = json_object_new_object(); json_object_object_add (jresp, "index", json_object_new_string (value)); } else jresp = NULL; afb_req_success (request, jresp, "Media - Listed"); }
static void start(struct afb_req request) { struct json_object *obj; const char *id, *mode; char *query; int rc; /* get the id */ id = afb_req_value(request, _id_); if (id == NULL) { afb_req_fail(request, "bad-request", "missing 'id'"); return; } /* get the mode */ mode = afb_req_value(request, _mode_); if (mode == NULL || !strcmp(mode, _auto_)) { mode = afb_interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_; } /* create the query */ rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode); if (rc < 0) { afb_req_fail(request, "server-error", "out of memory"); return; } /* calls the service */ obj = jbus_call_sj_sync(jbus, _start_, query); if (afb_interface->verbosity) fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL"); free(query); /* check status */ obj = json_object_get(obj); if (obj == NULL) { afb_req_fail(request, "failed", "framework daemon failure"); return; } /* embed if needed */ if (json_object_get_type(obj) == json_type_int) obj = embed(_runid_, obj); afb_req_success(request, obj, NULL); }
static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo) { if (afb_interface->verbosity) fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method, obj ? json_object_to_json_string(obj) : "NULL"); if (obj == NULL) { afb_req_fail(memo->request, "failed", "framework daemon failure"); } else { obj = json_object_get(obj); obj = embed(memo->method, obj); if (obj == NULL) { afb_req_fail(memo->request, "failed", "framework daemon failure"); } else { afb_req_success(memo->request, obj, NULL); } } afb_req_unref(memo->request); free(memo); }
static void call_runid(struct afb_req request, const char *method) { struct json_object *obj; const char *id = afb_req_value(request, _runid_); if (id == NULL) { afb_req_fail(request, "bad-request", "missing 'runid'"); return; } obj = jbus_call_sj_sync(jbus, method, id); if (afb_interface->verbosity) fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id, obj ? json_object_to_json_string(obj) : "NULL"); if (obj == NULL) { afb_req_fail(request, "failed", "framework daemon failure"); return; } obj = json_object_get(obj); afb_req_success(request, obj, NULL); }
static void list (struct afb_req request) { /* AFB_SESSION_CHECK */ mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; /* check that context is initialized */ if (ctx == NULL) { afb_req_fail (request, "failed", "uninitialized"); return; } jresp = _rygel_list (ctx); if (!jresp) { afb_req_fail (request, "failed", "no content found in media server"); return; } afb_req_success (request, jresp, "Media - Listed"); }
static void pausing (struct afb_req request) { /* AFB_SESSION_CHECK */ mediaCtxHandleT *ctx = afb_req_context_get(request); json_object *jresp; /* check that context is initialized */ if (ctx == NULL) { afb_req_fail (request, "failed", "uninitialized"); return; } if (!_rygel_do (ctx, PAUSE, NULL)) { afb_req_fail (request, "failed", "could not pause chosen media"); return; } jresp = json_object_new_object(); json_object_object_add (jresp, "pause", json_object_new_string ("success")); afb_req_success (request, jresp, "Media - Paused"); }
static void call_appid(struct afb_req request, const char *method) { struct memo *memo; char *sid; const char *id = afb_req_value(request, _id_); if (id == NULL) { afb_req_fail(request, "bad-request", "missing 'id'"); return; } memo = make_memo(request, method); if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) { afb_req_fail(request, "server-error", "out of memory"); free(memo); return; } if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) { afb_req_fail(request, "failed", "dbus failure"); free(memo); } free(sid); }
static void install(struct afb_req request) { struct json_object *obj, *added; char *query; const char *filename; struct afb_arg arg; /* get the argument */ arg = afb_req_get(request, "widget"); filename = arg.path; if (filename == NULL) { afb_req_fail(request, "bad-request", "missing 'widget' file"); return; } /* makes the query */ if (0 >= asprintf(&query, "\"%s\"", filename)) { afb_req_fail(request, "server-error", "out of memory"); return; } obj = jbus_call_sj_sync(jbus, _install_, query); if (afb_interface->verbosity) fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL"); free(query); /* check status */ if (obj == NULL) { afb_req_fail(request, "failed", "framework daemon failure"); return; } /* embed if needed */ if (json_object_object_get_ex(obj, _added_, &added)) obj = added; obj = json_object_get(obj); obj = embed(_id_, obj); afb_req_success(request, obj, NULL); }
static void on_uploaded(struct afb_req *prequest, int status) { struct afb_req request = afb_req_unstore(prequest); struct afb_arg argfile = afb_req_get(request, "file-upload"); char *file = renamed_filename(argfile); if (file != NULL) unlink(file); free(file); if (status) afb_req_fail (request, "failed", "expected file not received"); else afb_req_success_f (request, NULL, "uploaded file %s", argfile.value); afb_req_unref(request); }
/* callback when received answer */ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { int rc; struct dbus_memo *memo; const char *first, *second; uint8_t type; uint32_t flags; /* retrieve the recorded data */ memo = userdata; /* get the answer */ rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags); if (rc < 0) { /* failing to have the answer */ afb_req_fail(memo->req, "error", "dbus error"); } else { /* report the answer */ memo->context->flags = (unsigned)flags; switch(type) { case RETOK: afb_req_success(memo->req, json_tokener_parse(first), second); break; case RETERR: afb_req_fail(memo->req, first, second); break; case RETRAW: afb_req_send(memo->req, first, strlen(first)); break; default: afb_req_fail(memo->req, "error", "dbus link broken"); break; } } api_dbus_client_free_memo(memo); return 1; }
static void upload (struct afb_req request) { /* AFB_SESSION_CHECK */ mediaCtxHandleT *ctx = afb_req_context_get(request); struct afb_req *prequest; struct afb_arg argfile; char *path; /* check that context is initialized */ if (ctx == NULL) { afb_req_fail (request, "failed", "uninitialized"); return; } /* get the file */ argfile = afb_req_get(request, "file-upload"); if (!argfile.value || !argfile.path) { afb_req_fail (request, "failed", "expected file not received"); return; } /* rename the file */ path = renamed_filename(argfile); if (path == NULL) { afb_req_fail (request, "failed", "out of memory"); return; } if (rename(argfile.path, path) != 0) { free(path); afb_req_fail (request, "failed", "system error"); return; } /* for asynchronous processing */ prequest = afb_req_store(request); if (path == NULL) { unlink(path); afb_req_fail (request, "failed", "out of memory"); } else if (!_rygel_upload (ctx, path, (void*)on_uploaded, prequest)) { afb_req_unref(afb_req_unstore(prequest)); unlink(path); afb_req_fail (request, "failed", "Error when uploading file to media server... could not complete"); } free(path); }
static void pingFail (struct afb_req request) { afb_req_fail(request, "failed", "Ping Binder Daemon fails"); }