Post En court Ne fonctionne pas
[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 // Post handler
105 typedef struct {
106   char* data;
107   int   len;
108   int   uid;
109 } AFB_HttpPost;
110
111 typedef struct {
112   char  path[512];
113   int   fd;
114 } AFB_staticfile;
115
116 typedef struct {
117   char  *url;
118   char  *path;
119   size_t len;
120 } AFB_aliasdir;
121
122 // Command line structure hold cli --command + help text
123 typedef struct {
124   int  val;        // command number within application
125   int  has_arg;    // command number within application
126   char *name;      // command as used in --xxxx cli
127   char *help;      // help text
128 } AFB_options;
129
130 // main config structure
131 typedef struct {
132   char *logname;           // logfile path for info & error log
133   char *console;           // console device name (can be a file or a tty)
134   int  localhostOnly;
135   int   httpdPort;
136   char *smack;             // smack label
137   char *plugins;           // list of requested plugins
138   char *rootdir;           // base dir for httpd file download
139   char *rootbase;          // Angular HTML5 base URL
140   char *rootapi;           // Base URL for REST APIs
141   char *pidfile;           // where to store pid when running background
142   char *sessiondir;        // where to store mixer session files
143   char *configfile;        // where to store configuration on gateway exit
144   char *setuid;            // downgrade uid to username
145   char *token;             // initial authentication token [default NULL no session]
146   int  cacheTimeout;
147   int  apiTimeout;
148   int  cntxTimeout;        // Client Session Context timeout
149   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
150 } AFB_config;
151
152
153
154 typedef struct {
155   int  len;        // command number within application
156   json_object *jtype;
157 } AFB_privateApi;
158
159
160 typedef struct {
161      char    *msg;
162      size_t  len;
163 } AFB_redirect_msg;
164
165 // API definition
166 typedef struct {
167   char *name;
168   AFB_apiCB callback;
169   char *info;
170   AFB_privateApi *private;
171 } AFB_restapi;
172
173 // Plugin definition
174 typedef struct {
175   AFB_pluginT type;  
176   char *info;
177   char *prefix;
178   size_t prefixlen;
179   json_object *jtype;
180   AFB_restapi *apis;
181   void *handle;
182   int  ctxCount;
183   AFB_apiCB freeCtxCB;  // callback to free application context [null for standard free]
184 } AFB_plugin;
185
186
187 // User Client Session Context
188 typedef struct {
189   int  cid;             // index 0 if global
190   char uuid[37];        // long term authentication of remote client
191   char token[37];       // short term authentication of remote client
192   time_t timeStamp;     // last time token was refresh
193   int   restfull;       // client does not use cookie
194   void *ctx;            // application specific context
195   AFB_plugin *plugin;   // provide callback and easy access to plugin
196 } AFB_clientCtx;
197
198 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
199 typedef struct {
200   const char *url;
201   char *plugin;
202   char *api;
203   char *post; // post data in raw format
204   int  len;   // post data len
205   json_object *jresp;
206   AFB_clientCtx *client;      // needed because libmicrohttp cannot create an empty response
207   int   restfull;             // request is resfull [uuid token provided]
208   int   errcode;              // http error code
209   sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
210   AFB_config *config;         // plugin may need access to config
211   struct MHD_Connection *connection;
212 } AFB_request;
213
214
215 typedef struct {
216   AFB_config  *config;   // pointer to current config
217   // List of commands to execute
218   int  killPrevious;
219   int  background;        // run in backround mode
220   int  foreground;        // run in forground mode
221   int  checkAlsa;         // Display active Alsa Board
222   int  configsave;        // Save config on disk on start
223   char *cacheTimeout;     // http require timeout to be a string
224   void *httpd;            // anonymous structure for httpd handler
225   int  fakemod;           // respond to GET/POST request without interacting with sndboard
226   int  forceexit;         // when autoconfig from script force exit before starting server
227   AFB_plugin **plugins;   // pointer to REST/API plugins 
228   magic_t  magic;         // Mime type file magic lib
229   sigjmp_buf restartCkpt; // context save for restart set/longjmp
230 } AFB_session;
231
232
233
234 #include "proto-def.h"