2 alsajson-gw -- provide a REST/HTTP interface to ALSA-Mixer
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>
49 #define AJQ_VERSION "0.1"
51 /* other definitions --------------------------------------------------- */
53 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
54 #define MAGIC_DB "/usr/share/misc/magic.mgc"
55 #define OPA_INDEX "index.html"
56 #define MAX_ALIAS 10 // max number of aliases
57 #define COOKIE_NAME "AJB_session"
60 #define DEFLT_CNTX_TIMEOUT 3600 // default Client Connection Timeout
61 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout [0=NoLimit for Debug Only]
62 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout
63 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
64 #define DEFLT_AUTH_TOKEN NULL // expect for debug should == NULL
65 #define DEFLT_HTTP_TIMEOUT 15 // Max MibMicroHttp timeout
66 #define AFB_MAX_PLUGINS 20 // Max number of plugins for a given binder
80 #define AUDIO_BUFFER "/tmp/buf"
82 extern int verbose; // this is the only global variable
85 typedef enum {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321, AFB_PLUGIN_RAW=987123546} AFB_pluginE;
87 // prebuild json error are constructed in config.c
88 typedef enum { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
90 extern char *ERROR_LABEL[];
91 #define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"}
93 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
94 #define JSON_CONTENT "application/json"
95 #define FORM_CONTENT "multipart/form-data"
96 #define MAX_POST_SIZE 4096 // maximum size for POST data
97 #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated clients
100 typedef json_object* (*AFB_apiCB)();
101 typedef void (*AFB_freeCtxCB)(void*, void*, char*);
103 // Error code are requested through function to manage json usage count
110 typedef enum {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
112 // Post Upload File Handle
121 int len; // post element size
122 char *data; // post data in raw format
123 AFB_PostType type; // Json type
128 void* ctx; // Application context
129 int len; // current len for post
130 int uid; // post uid for debug
131 AFB_PostType type; // JSON or FORM
132 AFB_apiCB completeCB; // callback when post is completed
133 char *private; // use internally to keep track or partial buffer
134 struct MHD_PostProcessor *pp; // iterator handle
138 enum MHD_ValueKind kind; // kind type of the value
139 const char *key; // key 0-terminated key for the value
140 const char *filename; // filename of the uploaded file, NULL if not known
141 const char *mimetype; // content_type mime-type of the data, NULL if not known
142 const char *encoding; // transfer_encoding encoding of the data, NULL if not known
143 const char *data; // data pointer to size bytes of data at the specified offset
144 uint64_t offset; // offset of data in the overall value
145 size_t len; // number of bytes in data available
159 // Command line structure hold cli --command + help text
161 int val; // command number within application
162 int has_arg; // command number within application
163 char *name; // command as used in --xxxx cli
164 char *help; // help text
167 // main config structure
169 char *logname; // logfile path for info & error log
170 char *console; // console device name (can be a file or a tty)
173 char *ldpaths; // list of plugins directories
174 char *rootdir; // base dir for httpd file download
175 char *rootbase; // Angular HTML5 base URL
176 char *rootapi; // Base URL for REST APIs
177 char *pidfile; // where to store pid when running background
178 char *sessiondir; // where to store mixer session files
179 char *configfile; // where to store configuration on gateway exit
180 char *setuid; // downgrade uid to username
181 char *token; // initial authentication token [default NULL no session]
184 int cntxTimeout; // Client Session Context timeout
185 int pluginCount; // loaded plugins count
186 AFB_aliasdir *aliasdir; // alias mapping for icons,apps,...
190 int len; // command number within application
200 // Enum for Session/Token/Authentication middleware
201 typedef enum {AFB_SESSION_NONE, AFB_SESSION_CREATE, AFB_SESSION_CLOSE, AFB_SESSION_RENEW, AFB_SESSION_CHECK} AFB_sessionE;
206 AFB_sessionE session;
209 AFB_privateApi *private;
222 AFB_freeCtxCB freeCtxCB; // callback to free application context [null for standard free]
226 // User Client Session Context
228 char uuid[37]; // long term authentication of remote client
229 char token[37]; // short term authentication of remote client
230 time_t timeStamp; // last time token was refresh
231 int restfull; // client does not use cookie
232 void **contexts; // application specific context [one per plugin]]
233 AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
236 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
240 char *prefix; // plugin convivial name
242 AFB_PostRequest *post;
244 void *context; // Hold Client Context when using session
245 void *handle; // provide callback and easy access to plugin
246 int restfull; // request is resfull [uuid token provided]
247 int errcode; // http error code
248 sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
249 AFB_config *config; // plugin may need access to config
250 struct MHD_Connection *connection;
251 AFB_plugin **plugins;
256 AFB_config *config; // pointer to current config
257 // List of commands to execute
259 int background; // run in backround mode
260 int foreground; // run in forground mode
261 int configsave; // Save config on disk on start
262 char *cacheTimeout; // http require timeout to be a string
263 void *httpd; // anonymous structure for httpd handler
264 int fakemod; // respond to GET/POST request without interacting with sndboard
265 int forceexit; // when autoconfig from script force exit before starting server
266 AFB_plugin **plugins; // pointer to REST/API plugins
267 magic_t magic; // Mime type file magic lib
268 sigjmp_buf restartCkpt; // context save for restart set/longjmp
273 #include "proto-def.h"
275 #endif /* LOCAL_DEF_H */