2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "local-def.h"
23 // Dummy sample of Client Application Context
26 void *whateveryouwant;
27 } MyClientApplicationHandle;
30 // Request Creation of new context if it does not exist
31 STATIC json_object* clientContextCreate (AFB_request *request) {
34 // add an application specific client context to session
35 request->context = malloc (sizeof (MyClientApplicationHandle));
37 // Send response to UI
38 jresp = json_object_new_object();
39 json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
44 // Before entering here token will be check and renew
45 STATIC json_object* clientContextRefresh (AFB_request *request) {
49 jresp = json_object_new_object();
50 json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
56 // Session token will we verified before entering here
57 STATIC json_object* clientContextCheck (AFB_request *request) {
59 json_object *jresp = json_object_new_object();
60 json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
66 // Close and Free context
67 STATIC json_object* clientContextReset (AFB_request *request) {
70 /* after this call token will be reset
71 * - no further access to API will be possible
72 * - every context from any used plugin will be freed
75 jresp = json_object_new_object();
76 json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
78 // WARNING: if you free context resource manually here do not forget to set request->context=NULL;
81 // Close and Free context
82 STATIC json_object* clientGetPing (AFB_request *request) {
86 jresp = json_object_new_object();
87 json_object_object_add(jresp, "count", json_object_new_int (count ++));
93 // This function is call when Client Session Context is removed
94 // Note: when freeCtxCB==NULL standard free/malloc is called
95 STATIC void clientContextFree(void *context) {
96 fprintf (stderr,"Plugin[token] Closing Session\n");
100 STATIC AFB_restapi pluginApis[]= {
101 {"ping" , AFB_SESSION_NONE , (AFB_apiCB)clientGetPing ,"Ping Rest Test Service"},
102 {"create" , AFB_SESSION_CREATE, (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"},
103 {"refresh" , AFB_SESSION_RENEW , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
104 {"check" , AFB_SESSION_CHECK , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"},
105 {"reset" , AFB_SESSION_CLOSE , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"},
109 PUBLIC AFB_plugin *pluginRegister () {
110 AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
111 plugin->type = AFB_PLUGIN_JSON;
112 plugin->info = "Application Framework Binder Service";
113 plugin->prefix= "token"; // url base
114 plugin->apis = pluginApis;
115 plugin->freeCtxCB= (void*) clientContextFree;