asprintf (&alsa_info[i]->synonyms, "%s:%s", alsa_info[i]->synonyms, alsa_name);
new_info = 0;
break;
- }
+ }
}
/* it was not ; create it */
if (new_info) {
/*
* Get the string of 'key' from 'obj'
* Returns NULL if 'key' isn't in 'obj'
- */
+ */
static const char *strval(struct json_object *obj, const char *key)
{
struct json_object *keyval;
}
if (rc < 0)
break;
- return 1 + len;
+ return 1 + len;
case SD_BUS_TYPE_DICT_ENTRY_BEGIN:
if (!allows_not_basic || !allows_dict)
json_object_object_add (json_i, "title", json_title);
json_object_array_add (json_a, json_i);
- free (id); free (title);
+ free (id); free (title);
i++;
}
return;
if (number_returned == 1) {
- found = strstr (result, "id=\"");
+ found = strstr (result, "id=\"");
found += 4;
strncpy (subid, found, 32); subid[32] = '\0';
/* detect new radio devices */
void updateRadioDevList(pluginHandleT *handle) {
- int idx;
+ int idx;
// loop on existing radio if any
for (idx = 0; idx < _radio_dev_count(); idx++) {
json_object_object_add (jresp, "mute", json_object_new_string ("off"));
}
- afb_req_success (request, jresp, "Radio - Mute set");
+ afb_req_success (request, jresp, "Radio - Mute set");
}
static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
afb_req_fail (request, "failed", "you must call 'init' first");
return;
}
-
+
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
ctx->is_playing ?
/* Radio initialization should be done only when user start the radio and not at plugin initialization
Making this call too early would impose to restart the binder to detect a radio */
unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
-
+
if (num >= _radio_dev_count())
return 0;
-
+
if (init_dev_count < _radio_dev_count()) {
init_dev_count = _radio_dev_count();
- dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T*));
+ dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T*));
}
dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
_radio_dev_free (dev_ctx[num]);
free (dev_ctx[num]);
}
-
+
/* free(dev_ctx); */
}
// Dummy sample of Client Application Context
typedef struct {
- int something;
+ int something;
void *whateveryouwant;
} MyClientApplicationHandle;
// do something intelligent to check if we should or not update level of assurance from 0(anonymous) to 1(logged)
afb_req_session_set_LOA(request, 1);
-
+
// Send response to UI
- jresp = json_object_new_object();
+ jresp = json_object_new_object();
json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
afb_req_success(request, jresp, NULL);
-
+
}
// Before entering here token will be check and renew
static void clientContextRefresh (struct afb_req request) {
json_object *jresp;
-
+
jresp = json_object_new_object();
- json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
-
+ json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
+
afb_req_success(request, jresp, NULL);
}
// Session token will we verified before entering here
static void clientContextCheck (struct afb_req request) {
-
- json_object *jresp = json_object_new_object();
- json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
-
+
+ json_object *jresp = json_object_new_object();
+ json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
+
afb_req_success(request, jresp, NULL);
}
// Close and Free context
static void clientContextLogout (struct afb_req request) {
json_object *jresp;
-
+
/* after this call token will be reset
- * - no further access to API will be possible
+ * - no further access to API will be possible
* - every context from any used plugin will be freed
*/
-
+
jresp = json_object_new_object();
json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
-
- // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
+
+ // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
afb_req_success(request, jresp, NULL);
-
+
afb_req_session_set_LOA(request, 0);
}
// Close and Free context
jresp = json_object_new_object();
json_object_object_add(jresp, "count", json_object_new_int (count ++));
-
+
afb_req_success(request, jresp, NULL);
}
#include <afb/afb-plugin.h>
typedef struct {
- /*
+ /*
* client context is attached a session but private to a each plugin.
* Context is passed to each API under request->context
- *
+ *
* Note:
* -client context is free when a session is closed. Developer should not
* forget that even if context is private to each plugin, session is unique
* -when an API use AFB_SESSION_RESET this close the session and each plugin
* will be notified to free ressources.
*/
-
+
int count;
char *abcd;
-
+
} MyClientContextT;
-// This function is call at session open time. Any client trying to
+// This function is call at session open time. Any client trying to
// call it with an already open session will be denied.
// Ex: http://localhost:1234/api/context/create?token=123456789
static void myCreate (struct afb_req request)
// store something in our plugin private client context
ctx->count = 0;
- ctx->abcd = "SomeThingUseful";
+ ctx->abcd = "SomeThingUseful";
afb_req_context_set(request, ctx, free);
afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", ctx->abcd);
static void myAction (struct afb_req request)
{
MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request);
-
+
// store something in our plugin private client context
ctx->count++;
afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", ctx->abcd, ctx->count);
static void myClose (struct afb_req request)
{
MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request);
-
+
// store something in our plugin private client context
ctx->count++;
afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", ctx->abcd, ctx->count);
}
// PostForm callback is called multiple times (one or each key within form, or once per file buffer)
-// When file has been fully uploaded call is call with item==NULL
+// When file has been fully uploaded call is call with item==NULL
static void UploadImage (struct afb_req request)
{
Uploads(request, "images");
// For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
static void pingJson (struct afb_req request) {
- json_object *jresp, *embed;
-
+ json_object *jresp, *embed;
+
jresp = json_object_new_object();
json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
json_object_object_add(jresp, "myInt", json_object_new_int (1234));
-
+
embed = json_object_new_object();
json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
-
+
json_object_object_add(jresp,"eobj", embed);
ping(request, jresp, "pingJson");
* It records the functions to be called for the request.
* Don't use this structure directly.
* Use the helper functions documented below.
- */
+ */
struct afb_event_itf {
/* CAUTION: respect the order, add at the end */
* It records the functions to be called for the request.
* Don't use this structure directly.
* Use the helper functions documented below.
- */
+ */
struct afb_req_itf {
/* CAUTION: respect the order, add at the end */
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
afb_context_close(context);
afb_req_fail(req, "failed", "invalid token's identity");
return;
- }
+ }
}
if ((stag & AFB_SESSION_CREATE) != 0) {
afb_context_change_loa(context, 1);
afb_context_refresh(context);
}
-
+
if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
afb_context_refresh(context);
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Fulup Ar Foll"
* Author José Bollo <jose.bollo@iot.bzh>
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Fulup Ar Foll"
* Author José Bollo <jose.bollo@iot.bzh>
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Fulup Ar Foll"
* Author José Bollo <jose.bollo@iot.bzh>
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Fulup Ar Foll"
* Author José Bollo <jose.bollo@iot.bzh>
/*
* Sends on 'wsj1' a call to the method of 'api'/'verb' with arguments
- * given by 'object'. If not NULL, 'object' should be a valid JSON string.
+ * given by 'object'. If not NULL, 'object' should be a valid JSON string.
* On receiving the reply, the function 'on_reply' is called with 'closure'
* as its first argument and the message of the reply.
* Return 0 in case of success. Otherwise, returns -1 and set errno.
-/*
+/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Fulup Ar Foll"
* Author José Bollo <jose.bollo@iot.bzh>
{SET_ROOT_BASE ,1,"rootbase" , "Angular Base Root URL [default /opa]"},
{SET_ROOT_API ,1,"rootapi" , "HTML Root API URL [default /api]"},
{SET_ALIAS ,1,"alias" , "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
-
+
{SET_APITIMEOUT ,1,"apitimeout" , "Binding API timeout in seconds [default 10]"},
{SET_CNTXTIMEOUT ,1,"cntxtimeout" , "Client Session Context Timeout [default 900]"},
{SET_CACHE_TIMEOUT,1,"cache-eol" , "Client cache end of live [default 3600]"},
-
+
{SET_SESSION_DIR ,1,"sessiondir" , "Sessions file path [default rootdir/sessions]"},
{SET_LDPATH ,1,"ldpaths" , "Load bindingss from dir1:dir2:... [default = "BINDING_INSTALL_DIR"]"},
{SET_AUTH_TOKEN ,1,"token" , "Initial Secret [default=no-session, --token="" for session without authentication]"},
-
+
{DISPLAY_VERSION ,0,"version" , "Display version and copyright"},
{DISPLAY_HELP ,0,"help" , "Display this help"},
// default HTTP port
if (config->httpdPort == 0)
config->httpdPort = 1234;
-
+
// default binding API timeout
if (config->apiTimeout == 0)
config->apiTimeout = DEFLT_API_TIMEOUT;
-
+
// default AUTH_TOKEN
if (config->token == NULL)
config->token = DEFLT_AUTH_TOKEN;
// if directory does not exist createit
mkdir (config->rootdir, O_RDWR | S_IRWXU | S_IRGRP);
}
-
+
// if no Angular/HTML5 rootbase let's try '/' as default
if (config->rootbase == NULL)
config->rootbase = "/opa";
-
+
if (config->rootapi == NULL)
config->rootapi = "/api";
if (optarg == 0) goto needValueForOption;
if (!sscanf (optarg, "%d", &config->httpdPort)) goto notAnInteger;
break;
-
+
case SET_APITIMEOUT:
if (optarg == 0) goto needValueForOption;
if (!sscanf (optarg, "%d", &config->apiTimeout)) goto notAnInteger;
if (optarg == 0) goto needValueForOption;
config->rootdir = optarg;
INFO("Forcing Rootdir=%s",config->rootdir);
- break;
-
+ break;
+
case SET_ROOT_BASE:
if (optarg == 0) goto needValueForOption;
config->rootbase = optarg;
config->rootapi = optarg;
INFO("Forcing Rootapi=%s",config->rootapi);
break;
-
+
case SET_ALIAS:
if (optarg == 0) goto needValueForOption;
if ((unsigned)config->aliascount < sizeof (config->aliasdir) / sizeof (config->aliasdir[0])) {
}
} else {
ERROR("Too many aliases [max:%d] %s ignored", MAX_ALIAS, optarg);
- }
+ }
break;
-
+
case SET_AUTH_TOKEN:
if (optarg == 0) goto needValueForOption;
config->token = optarg;
}
}
free(gnuOptions);
-
+
config_set_default (config);
return;