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/>.
23 #include "afb-plugin.h"
24 #include "afb-req-itf.h"
26 // Dummy sample of Client Application Context
29 void *whateveryouwant;
30 } MyClientApplicationHandle;
33 // Request Creation of new context if it does not exist
34 static void clientContextCreate (struct afb_req request)
38 // add an application specific client context to session
39 request.context = malloc (sizeof (MyClientApplicationHandle));
41 // Send response to UI
42 jresp = json_object_new_object();
43 json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
45 afb_req_success(request, jresp, NULL);
48 // Before entering here token will be check and renew
49 static void clientContextRefresh (struct afb_req request) {
53 jresp = json_object_new_object();
54 json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
56 afb_req_success(request, jresp, NULL);
60 // Session token will we verified before entering here
61 static void clientContextCheck (struct afb_req request) {
63 json_object *jresp = json_object_new_object();
64 json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
66 afb_req_success(request, jresp, NULL);
70 // Close and Free context
71 static void clientContextReset (struct afb_req request) {
74 /* after this call token will be reset
75 * - no further access to API will be possible
76 * - every context from any used plugin will be freed
79 jresp = json_object_new_object();
80 json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
82 // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
83 afb_req_success(request, jresp, NULL);
85 // Close and Free context
86 static void clientGetPing (struct afb_req request) {
90 jresp = json_object_new_object();
91 json_object_object_add(jresp, "count", json_object_new_int (count ++));
93 afb_req_success(request, jresp, NULL);
97 // This function is call when Client Session Context is removed
98 // Note: when freeCtxCB==NULL standard free/malloc is called
99 static void clientContextFree(void *context) {
100 fprintf (stderr,"Plugin[token] Closing Session\n");
104 static const struct AFB_restapi pluginApis[]= {
105 {"ping" , AFB_SESSION_NONE , clientGetPing ,"Ping Rest Test Service"},
106 {"create" , AFB_SESSION_CREATE, clientContextCreate ,"Request Client Context Creation"},
107 {"refresh" , AFB_SESSION_RENEW , clientContextRefresh,"Refresh Client Context Token"},
108 {"check" , AFB_SESSION_CHECK , clientContextCheck ,"Check Client Context Token"},
109 {"reset" , AFB_SESSION_CLOSE , clientContextReset ,"Close Client Context and Free resources"},
113 static const struct AFB_plugin plugin_desc = {
114 .type = AFB_PLUGIN_JSON,
115 .info = "Application Framework Binder Service",
118 .freeCtxCB = clientContextFree
121 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)