/*
- * Copyright (C) 2015 "IoT.bzh"
+ * Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Manuel Bachmann"
*
* Licensed under the Apache License, Version 2.0 (the "License");
#define _GNU_SOURCE
#include <strings.h>
+#include <json-c/json.h>
#include "radio-api.h"
#include "radio-rtlsdr.h"
-#include "afb-plugin.h"
-#include "afb-req-itf.h"
+#include <afb/afb-plugin.h>
+#include <afb/afb-req-itf.h>
/* ********************************************************
static pluginHandleT *the_radio = NULL;
/* detect new radio devices */
-STATIC void updateRadioDevList(pluginHandleT *handle) {
+void updateRadioDevList(pluginHandleT *handle) {
int idx;
// loop on existing radio if any
for (idx = 0; idx < _radio_dev_count(); idx++) {
if (idx == MAX_RADIO) break;
- handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set used to FALSE */
- handle->radios[idx]->name = (char *) _radio_dev_name(idx);
+ handle->radios[idx] = calloc(1, sizeof(radioDevT)); /* use calloc to set "used" to FALSE */
+ handle->radios[idx]->name = (char *) _radio_dev_name(idx);
}
handle->devCount = _radio_dev_count();
}
/* global plugin context creation ; at loading time [radio devices might not be visible] */
-STATIC pluginHandleT* initRadioPlugin() {
+static void initRadioPlugin() {
pluginHandleT *handle;
handle = calloc (1, sizeof(pluginHandleT));
updateRadioDevList (handle);
-
- return handle;
+ the_radio = handle;
}
/* private client context creation ; default values */
-STATIC radioCtxHandleT* initRadioCtx () {
+static radioCtxHandleT* initRadioCtx () {
radioCtxHandleT *ctx;
ctx = malloc (sizeof(radioCtxHandleT));
ctx->radio = NULL;
- //ctx->idx = -1;
+ ctx->idx = -1;
ctx->mode = FM;
ctx->freq = 100.0;
ctx->mute = 0;
}
/* reserve a radio device for requesting client, power it on */
-STATIC AFB_error reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+unsigned char reserveRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+
unsigned int idx;
/* loop on all devices, find an unused one */
if (idx == MAX_RADIO) break;
if (handle->radios[idx]->used == FALSE) goto found_radio; /* found one */
}
- return AFB_FAIL;
+ return 0;
- found_radio:
+ found_radio:
/* try to power it on, passing client context info such as frequency... */
+
_radio_on (idx, ctx);
/* TODO : try to re-iterate from the next ones if it failed ! */
ctx->radio = handle->radios[idx];
ctx->idx = idx;
- return AFB_SUCCESS;
+ return 1;
}
/* free a radio device from requesting client, power it off */
-STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
+unsigned char releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
/* stop playing if it was doing this (blocks otherwise) */
if (ctx->is_playing) {
/* clean client context */
ctx->radio = NULL;
- //ctx->idx = -1;
+ ctx->idx = -1;
- return AFB_SUCCESS;
+ return 1;
}
/* called when client session dies [e.g. client quits for more than 15mns] */
-STATIC void freeRadio (void *context) {
+static void freeRadio (void *context) {
releaseRadio (the_radio, context);
free (context);
/* ------ PUBLIC PLUGIN FUNCTIONS --------- */
-STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void init (struct afb_req request) { /* AFB_SESSION_CHECK */
+ radioCtxHandleT *ctx = afb_req_context_get (request);
json_object *jresp;
+ /* create a global plugin handle */
+ if (!the_radio)
+ initRadioPlugin();
+
/* create a private client context */
- if (!request.context)
- request.context = initRadioCtx();
+ if (!ctx) {
+ ctx = initRadioCtx();
+ afb_req_context_set (request, ctx, free);
+ }
jresp = json_object_new_object();
- json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
- afb_req_success (request, jresp, "Radio - Initialized");
+ json_object_object_add(jresp, "init", json_object_new_string ("success"));
+ afb_req_success (request, jresp, "Radio initialized");
}
-STATIC void power (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void power (struct afb_req request) { /* AFB_SESSION_CHECK */
pluginHandleT *handle = the_radio;
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp;
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
+
/* no "?value=" parameter : return current state */
if (!value) {
- jresp = json_object_new_object();
ctx->radio ?
json_object_object_add (jresp, "power", json_object_new_string ("on"))
: json_object_object_add (jresp, "power", json_object_new_string ("off"));
+ afb_req_success (request, jresp, "Radio - Power status obtained");
+ return;
}
/* "?value=" parameter is "1" or "true" */
else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
if (!ctx->radio) {
- if (reserveRadio (handle, ctx) == AFB_FAIL) {
- //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
- afb_req_fail (request, "failed", "No more radio devices available");
- return;
+ if (!reserveRadio (handle, ctx)) {
+ afb_req_fail (request, "failed", "no more radio devices available");
+ return;
}
}
- jresp = json_object_new_object();
json_object_object_add (jresp, "power", json_object_new_string ("on"));
}
/* "?value=" parameter is "0" or "false" */
else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
if (ctx->radio) {
- if (releaseRadio (handle, ctx) == AFB_FAIL) {
- //request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
+ if (!releaseRadio (handle, ctx)) {
afb_req_fail (request, "failed", "Unable to release radio device");
- return;
+ return;
}
}
- jresp = json_object_new_object();
json_object_object_add (jresp, "power", json_object_new_string ("off"));
}
else
afb_req_success (request, jresp, "Radio - Power set");
}
-STATIC void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void mode (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
- json_object *jresp = json_object_new_object();
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
+ json_object *jresp;
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
afb_req_success (request, jresp, "Radio - Mode set");
}
-STATIC void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void freq (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
- json_object *jresp = json_object_new_object();
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
+ json_object *jresp;
double freq;
char freq_str[256];
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
+ jresp = json_object_new_object();
+
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
snprintf (freq_str, sizeof(freq_str), "%f", ctx->freq);
afb_req_success (request, jresp, "Radio - Frequency Set");
}
-STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp = json_object_new_object();
- //char *mute_str;
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
afb_req_success (request, jresp, "Radio - Mute set");
}
-STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
+static void play (struct afb_req request) { /* AFB_SESSION_CHECK */
- radioCtxHandleT *ctx = (radioCtxHandleT*)request.context;
- const char *value = afb_req_argument (request, "value");
+ radioCtxHandleT *ctx = afb_req_context_get (request);
+ const char *value = afb_req_value (request, "value");
json_object *jresp = json_object_new_object();
+
+ if (!ctx) {
+ afb_req_fail (request, "failed", "you must call 'init' first");
+ return;
+ }
/* no "?value=" parameter : return current state */
if (!value || !ctx->radio) {
afb_req_success (request, jresp, "Radio - Play succeeded");
}
-STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
+static void ping (struct afb_req request) { /* AFB_SESSION_NONE */
afb_req_success (request, NULL, "Radio - Ping succeeded");
}
-STATIC const struct AFB_restapi pluginApis[]= {
+static const struct AFB_verb_desc_v1 verbs[] = {
{"init" , AFB_SESSION_CHECK, init , "Radio API - init"},
{"power" , AFB_SESSION_CHECK, power , "Radio API - power"},
{"mode" , AFB_SESSION_CHECK, mode , "Radio API - mode"},
{NULL}
};
-STATIC const struct AFB_plugin plug_desc = {
- .type = AFB_PLUGIN_JSON,
- .info = "Application Framework Binder - Radio plugin",
- .prefix = "radio",
- .apis = pluginApis,
- //plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
- //the_radio = initRadioPlugin();
+static const struct AFB_plugin pluginDesc = {
+ .type = AFB_PLUGIN_VERSION_1,
+ .v1 = {
+ .info = "Application Framework Binder - Radio plugin",
+ .prefix = "radio",
+ .verbs = verbs
+ }
};
+
+const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
+{
+ return &pluginDesc;
+}