refactoring (in progress, tbf)
[src/app-framework-binder.git] / include / local-def.h
1 /*
2    local-def.h -- provide a REST/HTTP interface
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 /* other definitions --------------------------------------------------- */
49
50 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
51 #define OPA_INDEX "index.html"
52 #define MAX_ALIAS 10           // max number of aliases
53 #define COOKIE_NAME   "afb-session"
54
55 #define DEFLT_CNTX_TIMEOUT  3600   // default Client Connection Timeout
56 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout [0=NoLimit for Debug Only]
57 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout
58 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
59 #define DEFLT_AUTH_TOKEN    NULL   // expect for debug should == NULL
60 #define DEFLT_HTTP_TIMEOUT  15     // Max MibMicroHttp timeout
61 #define AFB_MAX_PLUGINS     20     // Max number of plugins for a given binder
62
63 #ifndef FALSE
64   #define FALSE 0
65 #endif
66 #ifndef TRUE
67   #define TRUE 1
68 #endif
69
70 #define PUBLIC
71 #define STATIC    static
72 #define FAILED    -1
73
74 #define AUDIO_BUFFER "/tmp/buf"
75
76 extern int verbose;  // this is the only global variable
77
78 // prebuild json error are constructed in helper-api.c
79 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
80
81 #define MAX_POST_SIZE  4096   // maximum size for POST data
82 #define CTX_NBCLIENTS   10   // allow a default of 10 authenticated clients
83
84
85
86
87
88
89
90
91 // Plugin Type
92 enum  AFB_pluginE
93 {
94         AFB_PLUGIN_JSON = 123456789,
95         AFB_PLUGIN_JSCRIPT = 987654321,
96         AFB_PLUGIN_RAW = 987123546
97 };
98
99 // Enum for Session/Token/Authentication middleware
100 enum AFB_sessionE
101 {
102         AFB_SESSION_NONE,
103         AFB_SESSION_CREATE,
104         AFB_SESSION_CLOSE,
105         AFB_SESSION_RENEW,
106         AFB_SESSION_CHECK
107 };
108
109 // API definition
110 struct AFB_restapi
111 {
112         const char *name;
113         enum AFB_sessionE session;
114         json_object* (*callback)();
115         const char *info;
116 };
117
118 // Plugin definition
119 struct AFB_plugin
120 {
121         enum AFB_pluginE type;  
122         const char *info;
123         const char *prefix;
124         const struct AFB_restapi *apis;
125         void (*freeCtxCB)(void*);  // callback to free application context [null for standard free]
126 };
127
128 typedef enum AFB_pluginE AFB_pluginE;
129 typedef enum AFB_sessionE AFB_sessionE;
130 typedef json_object* (*AFB_apiCB)();
131 typedef void (*AFB_freeCtxCB)(void*);
132 typedef struct AFB_restapi AFB_restapi;
133 typedef struct AFB_plugin AFB_plugin;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
149 typedef enum  {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
150
151
152
153 // Post Upload File Handle
154 typedef struct {
155    int   fd; 
156    char *path; 
157    int  errcode;
158    json_object* jresp;
159 } AFB_PostCtx;
160
161 typedef  struct {
162     int  len;   // post element size
163     char *data; // post data in raw format
164     AFB_PostType type; // Json type
165 } AFB_PostRequest;
166   
167 // Post handler
168 typedef struct {
169   void*  ctx;               // Application context
170   int    len;               // current len for post
171   int    uid;               // post uid for debug
172   AFB_PostType type;        // JSON or FORM
173   AFB_apiCB  completeCB;    // callback when post is completed
174   char   *privatebuf;       // use internally to keep track or partial buffer
175   struct MHD_PostProcessor *pp; // iterator handle
176 } AFB_PostHandle;
177
178 typedef struct {
179     enum MHD_ValueKind kind; // kind type of the value
180     const char *key;         // key 0-terminated key for the value
181     const char *filename;    // filename of the uploaded file, NULL if not known
182     const char *mimetype;    // content_type mime-type of the data, NULL if not known
183     const char *encoding;    // transfer_encoding encoding of the data, NULL if not known
184     const char *data;        // data pointer to size bytes of data at the specified offset
185     uint64_t   offset;       // offset of data in the overall value
186     size_t     len;          // number of bytes in data available
187 } AFB_PostItem;
188
189 typedef struct {
190   char  path[512];
191   int   fd;
192 } AFB_staticfile;
193
194 typedef struct {
195   char  *url;
196   char  *path;
197   size_t len;
198 } AFB_aliasdir;
199
200 typedef struct {
201      char    *msg;
202      size_t  len;
203 } AFB_redirect_msg;
204
205 // main config structure
206 struct AFB_config
207 {
208   char *console;           // console device name (can be a file or a tty)
209   int   httpdPort;
210   char *ldpaths;           // list of plugins directories
211   char *rootdir;           // base dir for httpd file download
212   char *rootbase;          // Angular HTML5 base URL
213   char *rootapi;           // Base URL for REST APIs
214   char *sessiondir;        // where to store mixer session files
215   char *token;             // initial authentication token [default NULL no session]
216   int  cacheTimeout;
217   int  apiTimeout;
218   int  cntxTimeout;        // Client Session Context timeout
219   AFB_Mode mode;           // mode of listening
220   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
221 };
222
223 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
224 typedef struct {
225   const char *uuid;
226   const char *url;
227   const char *prefix;              // plugin convivial name
228   const char *method;
229 /*
230   AFB_PostRequest *post;
231 */
232   json_object *jresp;
233   void *context;             // Hold Client Context when using session
234   int  restfull;             // request is resfull [uuid token provided]
235   int  errcode;              // http error code
236   struct AFB_config *config;         // plugin may need access to config
237   struct afb_req *areq;
238 } AFB_request;
239
240 struct afb_hsrv_handler;
241 struct MHD_Daemon;
242
243 struct AFB_session {
244   struct AFB_config  *config;   // pointer to current config
245   // List of commands to execute
246   int  background;        // run in backround mode
247   int  foreground;        // run in forground mode
248   char *cacheTimeout;     // http require timeout to be a string
249   struct MHD_Daemon *httpd;            // structure for httpd handler
250   int  fakemod;           // respond to GET/POST request without interacting with sndboard
251   int  readyfd;           // a #fd to signal when ready to serve
252   magic_t  magic;         // Mime type file magic lib
253   struct afb_hsrv_handler *handlers;
254 };
255
256
257 typedef struct AFB_config AFB_config;
258 typedef struct AFB_session AFB_session;
259
260 #include "proto-def.h"
261
262 #endif /* LOCAL_DEF_H */