Server - Fixed leaked file handles in both itemdb and modbus
[tpg/opendispense2.git] / src / server / common.h
1 /*
2  * OpenDispense2
3  *
4  * This code is published under the terms of the Acess licence.
5  * See the file COPYING for details.
6  *
7  * common.h - Core Header
8  */
9 #ifndef _COMMON_H_
10 #define _COMMON_H_
11
12 #include <regex.h>
13 #include "../cokebank.h"
14
15 // === CONSTANTS ===
16 #define DEFAULT_CONFIG_FILE     "/etc/opendispense/main.cfg"
17 #define DEFAULT_ITEM_FILE       "/etc/opendispense/items.cfg"
18
19 // === HELPER MACROS ===
20 #define _EXPSTR(x)      #x
21 #define EXPSTR(x)       _EXPSTR(x)
22
23 #define UNUSED(var)     unused__##var __attribute__((__unused__))
24
25 #define ASSERT(cnd) do{if(!(cnd)){fprintf(stderr, "ASSERT failed at "__FILE__":"EXPSTR(__LINE__)" - "EXPSTR(cnd)"\n");exit(-1);}}while(0)
26
27 // === STRUCTURES ===
28 typedef struct sItem    tItem;
29 typedef struct sUser    tUser;
30 typedef struct sConfigItem      tConfigItem;
31 typedef struct sHandler tHandler;
32
33 struct sItem
34 {
35         char    *Name;  //!< Display Name
36          int    Price;  //!< Price
37          int    bHidden;        //!< Hidden item?
38          int    bDisabledi;     //!< Disabled Item
39         
40         tHandler        *Handler;       //!< Handler for the item
41         short   ID;     //!< Item ID
42 };
43
44 struct sUser
45 {
46          int    ID;             //!< User ID (LDAP ID)
47          int    Balance;        //!< Balance in cents
48          int    Bytes;  //!< Traffic Usage
49         char    Name[]; //!< Username
50 };
51
52 struct sConfigItem
53 {
54         char    *Name;
55         char    *Value;
56 };
57
58 struct sHandler
59 {
60         char    *Name;
61          int    (*Init)(int NConfig, tConfigItem *Config);
62         /**
63          * \brief Check if an item can be dispensed
64          * \return Boolean Failure
65          */
66          int    (*CanDispense)(int User, int ID);
67          int    (*DoDispense)(int User, int ID);
68 };
69
70 // === GLOBALS ===
71 extern tItem    *gaItems;
72 extern int      giNumItems;
73 extern tHandler *gaHandlers[];
74 extern int      giNumHandlers;
75 extern int      giDebugLevel;
76 extern int      gbNoCostMode;
77
78 // === FUNCTIONS ===
79 extern void     Items_UpdateFile(void);
80
81 // --- Helpers --
82 extern void     StartPeriodicThread(void);
83 extern void     AddPeriodicFunction(void (*Fcn)(void));
84 extern void     CompileRegex(regex_t *Regex, const char *Pattern, int Flags);
85 extern int      RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage);
86 extern int      InitSerial(const char *Path, int BaudRate);
87 extern char     *mkstr(const char *Format, ...);
88
89 // --- Dispense ---
90 extern int      DispenseItem(int ActualUser, int User, tItem *Item);
91 extern int      DispenseRefund(int ActualUser, int DestUser, tItem *Item, int OverridePrice);
92 extern int      DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const char *ReasonGiven);
93 extern int      DispenseAdd(int ActualUser, int User, int Ammount, const char *ReasonGiven);
94 extern int      DispenseSet(int ActualUser, int User, int Balance, const char *ReasonGiven);
95 extern int      DispenseDonate(int ActualUser, int User, int Ammount, const char *ReasonGiven);
96 extern int      DispenseUpdateItem(int User, tItem *Item, const char *NewName, int NewPrice);
97
98 // --- Logging ---
99 // to syslog
100 extern void     Log_Error(const char *Format, ...);
101 extern void     Log_Info(const char *Format, ...);
102 // To stdout
103 #define Debug_Notice(msg, v...) fprintf(stderr, "%08llun: "msg"\n", (unsigned long long)time(NULL) ,##v)
104 #define Debug_Debug(msg, v...)  fprintf(stderr, "%08llud: "msg"\n", (unsigned long long)time(NULL) ,##v)
105
106 // --- Config Database ---
107 extern void     Config_ParseFile(const char *Filename);
108 extern void     Config_AddValue(const char *Key, const char *Value);
109 extern int      Config_GetValueCount(const char *KeyName);
110 extern const char       *Config_GetValue(const char *KeyName, int Index);
111 extern int      Config_GetValue_Bool(const char *KeyName, int Index);
112 extern int      Config_GetValue_Int(const char *KeyName, int Index);
113
114 #endif

UCC git Repository :: git.ucc.asn.au