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