refactoring (in progress, tbf)
[src/app-framework-binder.git] / plugins / radio / radio-api.c
index 09fbcb5..c6e4b90 100644 (file)
@@ -17,7 +17,6 @@
  */
 
 #include "radio-api.h"
-#include "radio-rtlsdr.h"
 
 /* ********************************************************
 
@@ -27,6 +26,8 @@
 
 /* ------ LOCAL HELPER FUNCTIONS --------- */
 
+static pluginHandleT *the_radio = NULL;
+
 /* detect new radio devices */
 STATIC void updateRadioDevList(pluginHandleT *handle) {
 
@@ -117,21 +118,22 @@ STATIC AFB_error releaseRadio (pluginHandleT *handle, radioCtxHandleT *ctx) {
 }
 
 /* called when client session dies [e.g. client quits for more than 15mns] */
-STATIC void freeRadio (void *context, void *handle) {
+STATIC void freeRadio (void *context) {
 
-    releaseRadio (handle, context);
+    releaseRadio (the_radio, context);
     free (context);
 }
 
 
 /* ------ PUBLIC PLUGIN FUNCTIONS --------- */
 
-STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
+STATIC json_object* init (AFB_request *request) {        /* AFB_SESSION_CHECK */
 
     json_object *jresp;
 
     /* create a private client context */
-    request->context = initRadioCtx();
+    if (!request->context)
+        request->context = initRadioCtx();
 
     jresp = json_object_new_object();
     json_object_object_add(jresp, "info", json_object_new_string ("Radio initialized"));
@@ -140,7 +142,7 @@ STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
 
 STATIC json_object* power (AFB_request *request) {       /* AFB_SESSION_CHECK */
 
-    pluginHandleT *handle = (pluginHandleT*)request->plugin;
+    pluginHandleT *handle = the_radio;
     radioCtxHandleT *ctx = (radioCtxHandleT*)request->context;
     const char *value = getQueryValue (request, "value");
     json_object *jresp;
@@ -176,6 +178,8 @@ STATIC json_object* power (AFB_request *request) {       /* AFB_SESSION_CHECK */
         jresp = json_object_new_object();
         json_object_object_add (jresp, "power", json_object_new_string ("off"));
     }
+    else
+        jresp = NULL;
 
     return jresp;
 }
@@ -306,7 +310,7 @@ STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
 
 
 STATIC AFB_restapi pluginApis[]= {
-  {"init"   , AFB_SESSION_CREATE, (AFB_apiCB)init       , "Radio API - init"},
+  {"init"   , AFB_SESSION_CHECK,  (AFB_apiCB)init       , "Radio API - init"},
   {"power"  , AFB_SESSION_CHECK,  (AFB_apiCB)power      , "Radio API - power"},
   {"mode"   , AFB_SESSION_CHECK,  (AFB_apiCB)mode       , "Radio API - mode"},
   {"freq"   , AFB_SESSION_CHECK,  (AFB_apiCB)freq       , "Radio API - freq"},
@@ -323,8 +327,8 @@ PUBLIC AFB_plugin* pluginRegister () {
     plugin->prefix  = "radio";
     plugin->apis  = pluginApis;
 
-    plugin->handle = initRadioPlugin();
     plugin->freeCtxCB = (AFB_freeCtxCB)freeRadio;
 
-    return (plugin);
+    the_radio = initRadioPlugin();
+    return plugin;
 };