791f4c196a34bd1a421db7072d95a423a7f23123
[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
64 typedef int BOOL;
65 #ifndef FALSE
66   #define FALSE 0
67 #endif
68 #ifndef TRUE
69   #define TRUE 1
70 #endif
71
72 #define PUBLIC
73 #define STATIC    static
74 #define FAILED    -1
75
76 extern int verbose;  // this is the only global variable
77
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_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 MAX_POST_SIZE  4096   // maximum size for POST data
91 #define CTX_NBCLIENTS   10   // allow a default of 10 authenticated clients
92
93
94 typedef json_object* (*AFB_apiCB)();
95
96 // Error code are requested through function to manage json usage count
97 typedef struct {
98   int   level;
99   char* label;
100   json_object *json;
101 } AFB_errorT;
102
103 // Post handler
104 typedef struct {
105   char* data;
106   int   len;
107   int   uid;
108 } AFB_HttpPost;
109
110 typedef struct {
111   char  path[512];
112   int   fd;
113 } AFB_staticfile;
114
115 typedef struct {
116   char  *url;
117   char  *path;
118   size_t len;
119 } AFB_aliasdir;
120
121 // Command line structure hold cli --command + help text
122 typedef struct {
123   int  val;        // command number within application
124   int  has_arg;    // command number within application
125   char *name;      // command as used in --xxxx cli
126   char *help;      // help text
127 } AFB_options;
128
129 // main config structure
130 typedef struct {
131   char *logname;           // logfile path for info & error log
132   char *console;           // console device name (can be a file or a tty)
133   int  localhostOnly;
134   int   httpdPort;
135   char *smack;             // smack label
136   char *plugins;           // list of requested plugins
137   char *rootdir;           // base dir for httpd file download
138   char *rootbase;          // Angular HTML5 base URL
139   char *rootapi;           // Base URL for REST APIs
140   char *pidfile;           // where to store pid when running background
141   char *sessiondir;        // where to store mixer session files
142   char *configfile;        // where to store configuration on gateway exit
143   char *setuid;            // downgrade uid to username
144   char *token;             // initial authentication token [default NULL no session]
145   int  cacheTimeout;
146   int  apiTimeout;
147   int  cntxTimeout;        // Client Session Context timeout
148   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
149 } AFB_config;
150
151
152
153 typedef struct {
154   int  len;        // command number within application
155   json_object *jtype;
156 } AFB_privateApi;
157
158
159 typedef struct {
160      char    *msg;
161      size_t  len;
162 } AFB_redirect_msg;
163
164 // API definition
165 typedef struct {
166   char *name;
167   AFB_apiCB callback;
168   char *info;
169   AFB_privateApi *private;
170 } AFB_restapi;
171
172
173 // User Client Session Context
174 typedef struct {
175   int  cid;         // index 0 if global
176   char uuid[37];    // long term authentication of remote client
177   char token[37];   // short term authentication of remote client
178   time_t timeStamp; // last time token was refresh
179   int   restfull;   // client does not use cookie
180   void *handle;     // application specific context
181   AFB_apiCB freeHandleCB;  // callback to free application handle [null for standard free]
182 } AFB_clientCtx;
183
184
185 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
186 typedef struct {
187   const char *url;
188   char *plugin;
189   char *api;
190   char *post;
191   int  loa;
192   json_object *jresp;
193   AFB_clientCtx *client;      // needed because libmicrohttp cannot create an empty response
194   int   restfull;             // request is resfull [uuid token provided]
195   int   errcode;              // http error code
196   sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
197   AFB_config *config;         // plugin may need access to config
198   struct MHD_Connection *connection;
199 } AFB_request;
200
201
202 // Plugin definition
203 typedef struct {
204   AFB_pluginT type;  
205   char *info;
206   char *prefix;
207   size_t prefixlen;
208   json_object *jtype;
209   AFB_restapi *apis;
210   void *handle;
211   int  ctxCount;
212 } AFB_plugin;
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"