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.
33 #include <sys/ioctl.h>
34 #include <sys/signal.h>
35 #include <sys/types.h>
38 #include <microhttpd.h>
42 #include <uuid/uuid.h>
47 #define AJQ_VERSION "0.1"
49 /* other definitions --------------------------------------------------- */
51 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
52 #define MAGIC_DB "/usr/share/misc/magic.mgc"
53 #define OPA_INDEX "index.html"
54 #define MAX_ALIAS 10 // max number of aliases
55 #define COOKIE_NAME "AJB_session"
58 #define DEFLT_CNTX_TIMEOUT 3600 // default Client Connection Timeout
59 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout [0=NoLimit for Debug Only]
60 #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout
61 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
75 extern int verbose; // this is the only global variable
77 // prebuild json error are constructed in config.c
78 typedef enum { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE} AFB_error;
80 extern char *ERROR_LABEL[];
81 #define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"}
83 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
84 #define JSON_CONTENT "application/json"
85 #define MAX_POST_SIZE 4096 // maximum size for POST data
86 #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated clients
88 // use to check anonymous data when using dynamic loadable lib
89 typedef enum {AFB_PLUGIN=1234, AFB_REQUEST=5678} AFB_type;
90 typedef json_object* (*AFB_apiCB)();
92 // Error code are requested through function to manage json usage count
117 // Command line structure hold cli --command + help text
119 int val; // command number within application
120 int has_arg; // command number within application
121 char *name; // command as used in --xxxx cli
122 char *help; // help text
125 // main config structure
127 char *logname; // logfile path for info & error log
128 char *console; // console device name (can be a file or a tty)
131 char *smack; // smack label
132 char *plugins; // list of requested plugins
133 char *rootdir; // base dir for httpd file download
134 char *rootbase; // Angular HTML5 base URL
135 char *rootapi; // Base URL for REST APIs
136 char *pidfile; // where to store pid when running background
137 char *sessiondir; // where to store mixer session files
138 char *configfile; // where to store configuration on gateway exit
142 int cntxTimeout; // Client Session Context timeout
143 AFB_aliasdir *aliasdir; // alias mapping for icons,apps,...
149 int len; // command number within application
164 AFB_privateApi *private;
168 // User Client Session Context
170 int cid; // index 0 if global
171 char uuid[37]; // long term authentication of remote client
172 char token[37]; // short term authentication of remote client
173 time_t timeStamp; // last time token was refresh
174 int restfull; // client does not use cookie
175 void *handle; // application specific context
176 AFB_apiCB freeHandleCB; // callback to free application handle [null for standard free]
180 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
188 AFB_clientCtx *client; // needed because libmicrohttp cannot create an empty response
189 int restfull; // request is resfull [uuid token provided]
190 int errcode; // http error code
191 sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
192 AFB_config *config; // plugin may need access to config
193 struct MHD_Connection *connection;
207 AFB_clientCtx *ctxGlobal;
212 AFB_config *config; // pointer to current config
213 // List of commands to execute
215 int background; // run in backround mode
216 int foreground; // run in forground mode
217 int checkAlsa; // Display active Alsa Board
218 int configsave; // Save config on disk on start
219 char *cacheTimeout; // http require timeout to be a string
220 void *httpd; // anonymous structure for httpd handler
221 int fakemod; // respond to GET/POST request without interacting with sndboard
222 int forceexit; // when autoconfig from script force exit before starting server
223 AFB_plugin **plugins; // pointer to REST/API plugins
224 magic_t magic; // Mime type file magic lib
225 sigjmp_buf restartCkpt; // context save for restart set/longjmp
230 #include "proto-def.h"