Sun, 18 May 2014 22:56:43 +0200
Initial server code added
7 | 1 | #ifndef _MBSELIB_H |
2 | #define _MBSELIB_H | |
3 | ||
4 | #include "../config.h" | |
5 | ||
6 | #include <stdlib.h> | |
7 | #include <stdio.h> | |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
8 | #include <stdint.h> |
43 | 9 | #include <stdarg.h> |
7 | 10 | #include <string.h> |
11 | #include <ctype.h> | |
12 | #include <sys/types.h> | |
12 | 13 | #include <sys/stat.h> |
32
3bac8fd4173d
Added user log function for program data
Michiel Broek <mbroek@mbse.eu>
parents:
31
diff
changeset
|
14 | #include <sys/time.h> |
3bac8fd4173d
Added user log function for program data
Michiel Broek <mbroek@mbse.eu>
parents:
31
diff
changeset
|
15 | #include <time.h> |
12 | 16 | #include <fcntl.h> |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
17 | #include <syslog.h> |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
18 | #include <unistd.h> |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
19 | #include <errno.h> |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
20 | #include <signal.h> |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
21 | #include <getopt.h> |
34 | 22 | #include <limits.h> |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
23 | #include <sys/socket.h> |
43 | 24 | #include <arpa/inet.h> |
25 | #include <netdb.h> | |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
26 | #include <poll.h> |
34 | 27 | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
28 | |
16
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
29 | /* mosquitto */ |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
30 | #include <mosquitto.h> |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
31 | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
32 | #ifdef HAVE_WIRINGPI_H |
16
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
33 | /* wiringPi */ |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
34 | #include <wiringPi.h> |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
35 | #include <pcf8574.h> |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
36 | #include <lcd.h> |
7 | 37 | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
38 | #endif |
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
39 | |
7 | 40 | #define TRUE 1 |
41 | #define FALSE 0 | |
42 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
43 | #define MBSE_SS(x) (x)?(x):"(null)" |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
44 | |
7 | 45 | /* rdconfig.c */ |
46 | typedef struct _key_list { | |
47 | char *key; | |
48 | int (*prc)(char **); | |
49 | char **dest; | |
50 | } key_list; | |
51 | ||
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
52 | typedef struct _w1_therm { |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
53 | struct _w1_therm *next; |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
54 | char *master; /* Master for this device */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
55 | int bus; /* Reserved for ds2482-800 */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
56 | char *name; /* Name of this device */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
57 | char *alias; /* Friendly name */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
58 | int present; /* 1=present, 0=absent */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
59 | int lastval; /* Last valid value */ |
26
9322c619c525
Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
25
diff
changeset
|
60 | int update; /* Value updated */ |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
61 | } w1_therm; |
7 | 62 | |
24
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
63 | typedef struct _rc_switch { |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
64 | struct _rc_switch *next; |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
65 | char *address; /* Address code */ |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
66 | char *alias; /* Friendly name */ |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
67 | } rc_switch; |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
68 | |
7 | 69 | typedef struct _sys_config { |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
70 | char *name; /* Configuration name */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
71 | char *mosq_host; /* mosquitto server hostname */ |
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
72 | int mosq_port; /* mosquitto server port */ |
43 | 73 | int my_port; /* my client/server port */ |
9
91218bc77abc
Config now reads sensors configuration
Michiel Broek <mbroek@mbse.eu>
parents:
8
diff
changeset
|
74 | w1_therm *w1therms; /* 1-wire temp sensors */ |
17
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
75 | #ifdef HAVE_WIRINGPI_H |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
76 | int lcd_cols; /* LCD display columns */ |
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
77 | int lcd_rows; /* LCD display rows */ |
24
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
78 | int rx433; /* 433 MHz receiver pin */ |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
79 | int tx433; /* 433 MHz transmitter pin */ |
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
80 | rc_switch *rcswitch; /* 433 MHz RC Power switches */ |
17
b802305046dc
Tests for wiringPi are now not fatal
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
81 | #endif |
7 | 82 | } sys_config; |
83 | ||
84 | ||
85 | void killconfig(void); | |
24
873786a20a61
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
86 | int rdconfig(char *); |
7 | 87 | |
88 | ||
34 | 89 | /* lock.c */ |
90 | int lockprog(char *); | |
91 | void ulockprog(char *); | |
92 | ||
93 | ||
7 | 94 | /* xutil.c */ |
95 | char *xmalloc(size_t); | |
96 | char *xstrcpy(char *); | |
97 | char *xstrcat(char *, char *); | |
98 | ||
16
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
99 | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
100 | #ifdef HAVE_WIRINGPI_H |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
101 | |
16
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
102 | /* lcd-pcf8574.c */ |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
103 | // Defines for the pcf8574 Pi LCD interface board |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
104 | #define AF_BASE 100 |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
105 | |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
106 | #define AF_RS (AF_BASE + 0) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
107 | #define AF_RW (AF_BASE + 1) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
108 | #define AF_E (AF_BASE + 2) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
109 | #define AF_BACKLIGHT (AF_BASE + 3) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
110 | #define AF_DB4 (AF_BASE + 4) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
111 | #define AF_DB5 (AF_BASE + 5) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
112 | #define AF_DB6 (AF_BASE + 6) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
113 | #define AF_DB7 (AF_BASE + 7) |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
114 | |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
115 | void setBacklight (int); |
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
116 | int initLCD (int, int); |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
117 | void mb_lcdPutchar(const int, unsigned char); |
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
118 | void mb_lcdPuts(const int, const char *); |
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
119 | void mb_lcdClear(const int); |
16
f4cbe008da72
Version 0.0.2. Added LCD display using wiringPi library. The display is connected via a LCM1602 IIC board.
Michiel Broek <mbroek@mbse.eu>
parents:
12
diff
changeset
|
120 | |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
121 | |
32
3bac8fd4173d
Added user log function for program data
Michiel Broek <mbroek@mbse.eu>
parents:
31
diff
changeset
|
122 | /* logger.c */ |
3bac8fd4173d
Added user log function for program data
Michiel Broek <mbroek@mbse.eu>
parents:
31
diff
changeset
|
123 | void logger(char *, char *, char *); |
3bac8fd4173d
Added user log function for program data
Michiel Broek <mbroek@mbse.eu>
parents:
31
diff
changeset
|
124 | |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
125 | |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
126 | /* rc-switch.c */ |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
127 | int toggleSwitch(char *); |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
128 | int toggleTypeA(char *, char *, bool); |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
129 | int toggleTypeB(int, int, bool); |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
130 | int toggleTypeC(char, int, int, bool); |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
26
diff
changeset
|
131 | int toggleTypeE(char, int, bool); |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
132 | |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
133 | void enableReceiveIRQ(int interrupt); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
134 | void enableReceive(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
135 | void disableReceive(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
136 | bool available(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
137 | void resetAvailable(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
138 | |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
139 | unsigned long getReceivedValue(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
140 | unsigned int getReceivedBitlength(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
141 | unsigned int getReceivedDelay(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
142 | unsigned int getReceivedProtocol(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
143 | unsigned int *getReceivedRawdata(void); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
144 | |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
145 | void enableTransmit(int); |
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
146 | void disableTransmit(void); |
29
ac763b87ee25
Improved rc-switch library to be more flexible.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
147 | |
ac763b87ee25
Improved rc-switch library to be more flexible.
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
148 | char *dec2binWzerofill(unsigned long, unsigned int); |
20
f7f9463cdefd
Added 433 MHz transmitter and receiver library and demo programs
Michiel Broek <mbroek@mbse.eu>
parents:
18
diff
changeset
|
149 | |
31
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
150 | |
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
151 | /* dht11.c */ |
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
152 | void dht11Read(void); |
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
153 | void dht11Init(int, int, int); |
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
154 | |
89dd2b691701
Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
29
diff
changeset
|
155 | |
7 | 156 | #endif |
157 | ||
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
158 | |
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
159 | #endif |
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
17
diff
changeset
|
160 |