2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include "afb-plugin.h"
23 #include "afb-req-itf.h"
25 // Dummy sample of Client Application Context
28 void *whateveryouwant;
29 } MyClientApplicationHandle;
32 // Request Creation of new context if it does not exist
33 static void clientContextCreate (struct afb_req request)
37 // add an application specific client context to session
38 request.context = malloc (sizeof (MyClientApplicationHandle));
40 // Send response to UI
41 jresp = json_object_new_object();
42 json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));
44 afb_req_success(request, jresp, NULL);
47 // Before entering here token will be check and renew
48 static void clientContextRefresh (struct afb_req request) {
52 jresp = json_object_new_object();
53 json_object_object_add(jresp, "token", json_object_new_string ("Token was refreshed"));
55 afb_req_success(request, jresp, NULL);
59 // Session token will we verified before entering here
60 static void clientContextCheck (struct afb_req request) {
62 json_object *jresp = json_object_new_object();
63 json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
65 afb_req_success(request, jresp, NULL);
69 // Close and Free context
70 static void clientContextReset (struct afb_req request) {
73 /* after this call token will be reset
74 * - no further access to API will be possible
75 * - every context from any used plugin will be freed
78 jresp = json_object_new_object();
79 json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
81 // WARNING: if you free context resource manually here do not forget to set *request.context=NULL;
82 afb_req_success(request, jresp, NULL);
84 // Close and Free context
85 static void clientGetPing (struct afb_req request) {
89 jresp = json_object_new_object();
90 json_object_object_add(jresp, "count", json_object_new_int (count ++));
92 afb_req_success(request, jresp, NULL);
96 // This function is call when Client Session Context is removed
97 // Note: when freeCtxCB==NULL standard free/malloc is called
98 static void clientContextFree(void *context) {
99 fprintf (stderr,"Plugin[token] Closing Session\n");
103 static const struct AFB_restapi pluginApis[]= {
104 {"ping" , AFB_SESSION_NONE , clientGetPing ,"Ping Rest Test Service"},
105 {"create" , AFB_SESSION_CREATE, clientContextCreate ,"Request Client Context Creation"},
106 {"refresh" , AFB_SESSION_RENEW , clientContextRefresh,"Refresh Client Context Token"},
107 {"check" , AFB_SESSION_CHECK , clientContextCheck ,"Check Client Context Token"},
108 {"reset" , AFB_SESSION_CLOSE , clientContextReset ,"Close Client Context and Free resources"},
112 static const struct AFB_plugin plugin_desc = {
113 .type = AFB_PLUGIN_JSON,
114 .info = "Application Framework Binder Service",
117 .freeCtxCB = clientContextFree
120 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)