abbbd61f02f3d1786a3761abd298da8cb785978f
[src/app-framework-binder.git] / include / local-def.h
1 /*
2    alsajson-gw -- provide a REST/HTTP interface to ALSA-Mixer
3
4    Copyright (C) 2015, Fulup Ar Foll
5
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.
10
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.
15
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.
19
20 */
21 #ifndef LOCAL_DEF_H
22 #define LOCAL_DEF_H
23
24 #ifndef _GNU_SOURCE
25   #define _GNU_SOURCE
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <termios.h>
35 #include <sys/ioctl.h>
36 #include <sys/signal.h>
37 #include <sys/types.h>
38 #include <time.h>
39 #include <json.h>
40 #include <microhttpd.h>
41 #include <magic.h>
42 #include <setjmp.h>
43 #include <signal.h>
44 #include <uuid/uuid.h>
45
46
47
48
49 #define AJQ_VERSION "0.1"
50
51 /* other definitions --------------------------------------------------- */
52
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"
58
59
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
67 typedef int BOOL;
68 #ifndef FALSE
69   #define FALSE 0
70 #endif
71 #ifndef TRUE
72   #define TRUE 1
73 #endif
74
75 #define PUBLIC
76 #define STATIC    static
77 #define FAILED    -1
78
79 #define AUDIO_BUFFER "/tmp/buf"
80
81 extern int verbose;  // this is the only global variable
82
83 // Plugin Type
84 typedef enum  {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321,  AFB_PLUGIN_RAW=987123546} AFB_pluginE;
85
86 // prebuild json error are constructed in config.c
87 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
88
89 extern char *ERROR_LABEL[];
90 #define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"}
91
92 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
93 #define JSON_CONTENT  "application/json"
94 #define FORM_CONTENT "multipart/form-data"
95 #define MAX_POST_SIZE  4096   // maximum size for POST data
96 #define CTX_NBCLIENTS   10   // allow a default of 10 authenticated clients
97
98
99 typedef json_object* (*AFB_apiCB)();
100
101 // Error code are requested through function to manage json usage count
102 typedef struct {
103   int   level;
104   char* label;
105   json_object *json;
106 } AFB_errorT;
107
108 typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM} AFB_PostType;
109
110 // Post Upload File Handle
111 typedef struct {
112    int   fd; 
113    char *path; 
114    int  errcode;
115    json_object* jresp;
116 } AFB_PostCtx;
117
118 typedef  struct {
119     int  len;   // post element size
120     char *data; // post data in raw format
121     AFB_PostType type; // Json type
122 } AFB_PostRequest;
123   
124 // Post handler
125 typedef struct {
126   void*  ctx;               // Application context
127   int    len;               // current len for post
128   int    uid;               // post uid for debug
129   AFB_PostType type;        // JSON or FORM
130   AFB_apiCB  completeCB;    // callback when post is completed
131   char   *private;          // use internally to keep track or partial buffer
132   struct MHD_PostProcessor *pp; // iterator handle
133 } AFB_PostHandle;
134
135 typedef struct {
136     enum MHD_ValueKind kind; // kind type of the value
137     const char *key;         // key 0-terminated key for the value
138     const char *filename;    // filename of the uploaded file, NULL if not known
139     const char *mimetype;    // content_type mime-type of the data, NULL if not known
140     const char *encoding;    // transfer_encoding encoding of the data, NULL if not known
141     const char *data;        // data pointer to size bytes of data at the specified offset
142     uint64_t   offset;       // offset of data in the overall value
143     size_t     len;          // number of bytes in data available
144 } AFB_PostItem;
145
146 typedef struct {
147   char  path[512];
148   int   fd;
149 } AFB_staticfile;
150
151 typedef struct {
152   char  *url;
153   char  *path;
154   size_t len;
155 } AFB_aliasdir;
156
157 // Command line structure hold cli --command + help text
158 typedef struct {
159   int  val;        // command number within application
160   int  has_arg;    // command number within application
161   char *name;      // command as used in --xxxx cli
162   char *help;      // help text
163 } AFB_options;
164
165 // main config structure
166 typedef struct {
167   char *logname;           // logfile path for info & error log
168   char *console;           // console device name (can be a file or a tty)
169   int  localhostOnly;
170   int   httpdPort;
171   char *smack;             // smack label
172   char *plugins;           // list of requested plugins
173   char *rootdir;           // base dir for httpd file download
174   char *rootbase;          // Angular HTML5 base URL
175   char *rootapi;           // Base URL for REST APIs
176   char *pidfile;           // where to store pid when running background
177   char *sessiondir;        // where to store mixer session files
178   char *configfile;        // where to store configuration on gateway exit
179   char *setuid;            // downgrade uid to username
180   char *token;             // initial authentication token [default NULL no session]
181   int  cacheTimeout;
182   int  apiTimeout;
183   int  cntxTimeout;        // Client Session Context timeout
184   int  pluginCount;        // loaded plugins count
185   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
186 } AFB_config;
187
188 typedef struct {
189   int  len;        // command number within application
190   json_object *jtype;
191 } AFB_privateApi;
192
193
194 typedef struct {
195      char    *msg;
196      size_t  len;
197 } AFB_redirect_msg;
198
199 // Enum for Session/Token/Authentication middleware
200 typedef enum  {AFB_SESSION_NONE, AFB_SESSION_CREATE, AFB_SESSION_CLOSE, AFB_SESSION_RENEW, AFB_SESSION_CHECK} AFB_sessionE;
201
202 // API definition
203 typedef struct {
204   char *name;
205   AFB_sessionE session;
206   AFB_apiCB callback;
207   char *info;
208   AFB_privateApi *private;
209 } AFB_restapi;
210
211 // Plugin definition
212 typedef struct {
213   AFB_pluginE type;  
214   char *info;
215   char *prefix;
216   size_t prefixlen;
217   json_object *jtype;
218   AFB_restapi *apis;
219   void *handle;
220   int  ctxCount;
221   AFB_apiCB freeCtxCB;  // callback to free application context [null for standard free]
222 } AFB_plugin;
223
224
225 // User Client Session Context
226 typedef struct {
227   int  cid;             // index 0 if global
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 **ctx;           // application specific context [one per plugin]]
233   AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
234 } AFB_clientCtx;
235
236 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
237 typedef struct {
238   const char *url;
239   char *plugin;
240   char *api;
241   AFB_PostRequest *post;
242   json_object *jresp;
243   void *context;             // Hold Client Context when using session
244   int  restfull;             // request is resfull [uuid token provided]
245   int  errcode;              // http error code
246   sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
247   AFB_config *config;         // plugin may need access to config
248   struct MHD_Connection *connection;
249   AFB_plugin **plugins;
250 } AFB_request;
251
252
253 typedef struct {
254   AFB_config  *config;   // pointer to current config
255   // List of commands to execute
256   int  killPrevious;
257   int  background;        // run in backround mode
258   int  foreground;        // run in forground mode
259   int  configsave;        // Save config on disk on start
260   char *cacheTimeout;     // http require timeout to be a string
261   void *httpd;            // anonymous structure for httpd handler
262   int  fakemod;           // respond to GET/POST request without interacting with sndboard
263   int  forceexit;         // when autoconfig from script force exit before starting server
264   AFB_plugin **plugins;   // pointer to REST/API plugins 
265   magic_t  magic;         // Mime type file magic lib
266   sigjmp_buf restartCkpt; // context save for restart set/longjmp
267 } AFB_session;
268
269
270
271 #include "proto-def.h"
272
273 #endif /* LOCAL_DEF_H */