2 local-def.h -- provide a REST/HTTP interface
4 Copyright (C) 2015, Fulup Ar Foll
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <sys/ioctl.h>
36 #include <sys/signal.h>
37 #include <sys/types.h>
40 #include <microhttpd.h>
44 #include <uuid/uuid.h>
48 /* other definitions --------------------------------------------------- */
50 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
51 #define OPA_INDEX "index.html"
52 #define MAX_ALIAS 10 // max number of aliases
53 #define COOKIE_NAME "afb-session"
55 #define DEFLT_CNTX_TIMEOUT 3600 // default Client Connection Timeout
56 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout [0=NoLimit for Debug Only]
57 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout
58 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
59 #define DEFLT_AUTH_TOKEN NULL // expect for debug should == NULL
60 #define DEFLT_HTTP_TIMEOUT 15 // Max MibMicroHttp timeout
61 #define AFB_MAX_PLUGINS 20 // Max number of plugins for a given binder
74 #define AUDIO_BUFFER "/tmp/buf"
76 extern int verbose; // this is the only global variable
79 typedef enum {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321, AFB_PLUGIN_RAW=987123546} AFB_pluginE;
81 // prebuild json error are constructed in helper-api.c
82 typedef enum { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
84 #define MAX_POST_SIZE 4096 // maximum size for POST data
85 #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated clients
88 typedef json_object* (*AFB_apiCB)();
89 typedef void (*AFB_freeCtxCB)(void*, void*, char*);
91 typedef enum {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
92 typedef enum {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
94 // Post Upload File Handle
103 int len; // post element size
104 char *data; // post data in raw format
105 AFB_PostType type; // Json type
110 void* ctx; // Application context
111 int len; // current len for post
112 int uid; // post uid for debug
113 AFB_PostType type; // JSON or FORM
114 AFB_apiCB completeCB; // callback when post is completed
115 char *privatebuf; // use internally to keep track or partial buffer
116 struct MHD_PostProcessor *pp; // iterator handle
120 enum MHD_ValueKind kind; // kind type of the value
121 const char *key; // key 0-terminated key for the value
122 const char *filename; // filename of the uploaded file, NULL if not known
123 const char *mimetype; // content_type mime-type of the data, NULL if not known
124 const char *encoding; // transfer_encoding encoding of the data, NULL if not known
125 const char *data; // data pointer to size bytes of data at the specified offset
126 uint64_t offset; // offset of data in the overall value
127 size_t len; // number of bytes in data available
146 // Enum for Session/Token/Authentication middleware
147 typedef enum {AFB_SESSION_NONE, AFB_SESSION_CREATE, AFB_SESSION_CLOSE, AFB_SESSION_RENEW, AFB_SESSION_CHECK} AFB_sessionE;
152 AFB_sessionE session;
167 AFB_freeCtxCB freeCtxCB; // callback to free application context [null for standard free]
171 // User Client Session Context
173 char uuid[37]; // long term authentication of remote client
174 char token[37]; // short term authentication of remote client
175 time_t timeStamp; // last time token was refresh
176 int restfull; // client does not use cookie
177 void **contexts; // application specific context [one per plugin]]
178 AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
181 // main config structure
183 char *console; // console device name (can be a file or a tty)
185 char *ldpaths; // list of plugins directories
186 char *rootdir; // base dir for httpd file download
187 char *rootbase; // Angular HTML5 base URL
188 char *rootapi; // Base URL for REST APIs
189 char *sessiondir; // where to store mixer session files
190 char *token; // initial authentication token [default NULL no session]
193 int cntxTimeout; // Client Session Context timeout
194 int pluginCount; // loaded plugins count
195 AFB_Mode mode; // mode of listening
196 AFB_aliasdir *aliasdir; // alias mapping for icons,apps,...
199 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
203 char *prefix; // plugin convivial name
205 AFB_PostRequest *post;
207 void *context; // Hold Client Context when using session
208 void *handle; // provide callback and easy access to plugin
209 int restfull; // request is resfull [uuid token provided]
210 int errcode; // http error code
211 sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
212 AFB_config *config; // plugin may need access to config
213 struct MHD_Connection *connection;
214 AFB_plugin **plugins;
217 struct afb_hsrv_handler;
221 AFB_config *config; // pointer to current config
222 // List of commands to execute
223 int background; // run in backround mode
224 int foreground; // run in forground mode
225 char *cacheTimeout; // http require timeout to be a string
226 struct MHD_Daemon *httpd; // structure for httpd handler
227 int fakemod; // respond to GET/POST request without interacting with sndboard
228 int readyfd; // a #fd to signal when ready to serve
229 AFB_plugin **plugins; // pointer to REST/API plugins
230 magic_t magic; // Mime type file magic lib
231 struct afb_hsrv_handler *handlers;
236 #include "proto-def.h"
238 #endif /* LOCAL_DEF_H */