Added POST, Plugin API signal protection, refactor HTML5 rewrite
[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 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <termios.h>
31 #include <sys/ioctl.h>
32 #include <sys/signal.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <json.h>
36 #include <microhttpd.h>
37 #include <magic.h>
38
39 #define AJQ_VERSION "0.1"
40
41 /* other definitions --------------------------------------------------- */
42
43 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
44 #define MAGIC_DB "/usr/share/misc/magic.mgc"
45 #define OPA_INDEX "index.html"
46
47 typedef int BOOL;
48 #ifndef FALSE
49   #define FALSE 0
50 #endif
51 #ifndef TRUE
52   #define TRUE 1
53 #endif
54
55 #define PUBLIC
56 #define STATIC    static
57 #define FAILED    -1
58
59 // prebuild json error are constructed in config.c
60 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS} AFB_error;
61
62 extern char *ERROR_LABEL[];
63 #define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"}
64
65 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
66 #define JSON_CONTENT  "application/json"
67 #define MAX_POST_SIZE  4096   // maximum size for POST data
68
69 // use to check anonymous data when using dynamic loadable lib
70 typedef enum  {AFB_PLUGIN=1234, AFB_REQUEST=5678} AFB_type;
71
72 // Error code are requested through function to manage json usage count
73 typedef struct {
74   int   level;
75   char* label;
76   json_object *json;
77 } AFB_errorT;
78
79 // Post handler
80 typedef struct {
81   char* data;
82   int   len;
83   int   uid;
84 } AFB_HttpPost;
85
86 typedef struct {
87   char  path[512];
88   int   fd;
89 } AFB_staticfile;
90
91
92 // some usefull static object initialized when entering listen loop.
93 extern int verbose;
94 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
95 typedef struct {
96   const char *url;
97   char *plugin;
98   char *api;
99   char *post;
100   struct MHD_Connection *connection;
101 } AFB_request;
102
103 typedef struct {
104      char    *msg;
105      size_t  len;
106 } AFB_redirect_msg;
107
108 // main config structure
109 typedef struct {
110   char *logname;           // logfile path for info & error log
111   char *console;           // console device name (can be a file or a tty)
112   int  localhostOnly;
113   int   httpdPort;
114   char *smack;             // smack label
115   char *plugins;           // list of requested plugins
116   char *rootdir;           // base dir for httpd file download
117   char *rootbase;          // Angular HTML5 base URL
118   char *rootapi;           // Base URL for REST APIs
119   char *pidfile;           // where to store pid when running background
120   char *sessiondir;        // where to store mixer session files
121   char *configfile;        // where to store configuration on gateway exit
122   uid_t setuid;
123   int  cacheTimeout;
124   int  apiTimeout;
125 } AFB_config;
126
127 // Command line structure hold cli --command + help text
128 typedef struct {
129   int  val;        // command number within application
130   int  has_arg;    // command number within application
131   char *name;      // command as used in --xxxx cli
132   char *help;      // help text
133 } AFB_options;
134
135 typedef json_object* (*AFB_apiCB)();
136
137 // API definition
138 typedef struct {
139   char *name;
140   AFB_apiCB callback;
141   char *info;
142   void * handle;
143 } AFB_restapi;
144
145 // Plugin definition
146 typedef struct {
147   AFB_type type;  
148   char *info;
149   char *prefix;
150   size_t prefixlen;
151   json_object *jtype;
152   AFB_restapi *apis;
153 } AFB_plugin;
154
155 typedef struct {
156   AFB_config  *config;   // pointer to current config
157   // List of commands to execute
158   int  killPrevious;
159   int  background;        // run in backround mode
160   int  foreground;        // run in forground mode
161   int  checkAlsa;         // Display active Alsa Board
162   int  configsave;        // Save config on disk on start
163   char *cacheTimeout;     // http require timeout to be a string
164   void *httpd;            // anonymous structure for httpd handler
165   int  fakemod;           // respond to GET/POST request without interacting with sndboard
166   int  forceexit;         // when autoconfig from script force exit before starting server
167   AFB_plugin **plugins;   // pointer to REST/API plugins 
168   magic_t  magic;         // Mime type file magic lib
169 } AFB_session;
170
171
172 #include "proto-def.h"