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"
27 * In case your plugin is implemented on multiple files or used share routines
28 * with other plugins, it might not be possible to use global static variable.
29 * In this case you can attach a static handle to your plugin. This handle
30 * is passed within each API call under request->handle
33 void *anythingYouWant;
40 * client context is attached a session but private to a each plugin.
41 * Context is passed to each API under request->context
44 * -client context is free when a session is closed. Developer should not
45 * forget that even if context is private to each plugin, session is unique
46 * to a client. When session close, every plugin are notified to free there
48 * -by default standard "free" function from libc is used to free context.
49 * Developer may define it own under plugin->freeCB. This call received
50 * FreeCtxCb(void *ClientCtx, void*PluginHandle, char*SessionUUID) if
51 * FreeCtxCb=(void*)-1 then context wont be free by session manager.
52 * -when an API use AFB_SESSION_RESET this close the session and each plugin
53 * will be notified to free ressources.
62 // Plugin handle should not be in stack (malloc or static)
63 static MyPluginHandleT global_handle;
65 // This function is call at session open time. Any client trying to
66 // call it with an already open session will be denied.
67 // Ex: http://localhost:1234/api/context/create?token=123456789
68 static void myCreate (struct afb_req request)
70 MyClientContextT *ctx = malloc (sizeof (MyClientContextT));
71 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
73 // store something in our plugin private client context
75 ctx->abcd = "SomeThingUseful";
77 *request.context = ctx;
78 afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", handle->anythingYouWant);
81 // This function can only be called with a valid token. Token should be renew before
82 // session timeout a standard renew api is avaliable at /api/token/renew this API
83 // can be called automatically with <token-renew> HTML5 widget.
84 // ex: http://localhost:1234/api/context/action?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
85 static void myAction (struct afb_req request)
87 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
88 MyClientContextT *ctx = (MyClientContextT*) *request.context;
90 // store something in our plugin private client context
92 afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", handle->anythingYouWant, ctx->count);
95 // After execution of this function, client session will be close and if they
96 // created a context [request->context != NULL] every plugins will be notified
97 // that they should free context resources.
98 // ex: http://localhost:1234/api/context/close?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
99 static void myClose (struct afb_req request)
101 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
102 MyClientContextT *ctx = (MyClientContextT*) *request.context;
104 // store something in our plugin private client context
106 afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", handle->anythingYouWant, ctx->count);
109 static void freeCtxCB (MyClientContextT *ctx) {
110 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
111 fprintf (stderr, "FreeCtxCB Plugin=[%s] count=[%d]", (char*)handle->anythingYouWant, ctx->count);
114 // Note: handle should be free it is a static resource attached to plugin and not to session
117 // NOTE: this sample does not use session to keep test a basic as possible
118 // in real application most APIs should be protected with AFB_SESSION_CHECK
119 static const struct AFB_restapi pluginApis[]= {
120 {"create", AFB_SESSION_CREATE, myCreate , "Create a new session"},
121 {"action", AFB_SESSION_CHECK , myAction , "Use Session Context"},
122 {"close" , AFB_SESSION_CLOSE , myClose , "Free Context"},
126 static const struct AFB_plugin plugin_desc = {
127 .type = AFB_PLUGIN_JSON,
128 .info = "Sample of Client Context Usage",
131 .freeCtxCB = (void*)freeCtxCB
134 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
136 global_handle.anythingYouWant = "anythingYouWant";