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