Skip to content

Commit 64f6f55

Browse files
committed
Merge pull request #8 from hmeyers/master
PiGatewaySerial as propper daemon
2 parents 6d77e46 + f305440 commit 64f6f55

File tree

10 files changed

+603
-26
lines changed

10 files changed

+603
-26
lines changed

Makefile

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
##########################################################################
2+
# Configurable options #
3+
##########################################################################
4+
# Install Base location
5+
PREFIX=/usr/local
6+
# Bin Dir
7+
BINDIR=$(PREFIX)/sbin
8+
# Set the name of predictable tty
9+
TTY_NAME := /dev/ttyMySensorsGateway
10+
# Set the group name for the raw tty
11+
TTY_GROUPNAME := tty
12+
##########################################################################
13+
# Please do not change anything below this line #
14+
##########################################################################
115
CC=g++
216
# get PI Revision from cpuinfo
317
PIREV := $(shell cat /proc/cpuinfo | grep Revision | cut -f 2 -d ":" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$$//')
4-
CCFLAGS=-Wall -Ofast -mfpu=vfp -lpthread -g -D__Raspberry_Pi -mfloat-abi=hard -mtune=arm1176jzf-s
18+
CCFLAGS=-Wall -Ofast -mfpu=vfp -lpthread -g -D__Raspberry_Pi -mfloat-abi=hard -mtune=arm1176jzf-s -D_TTY_NAME=\"${TTY_NAME}\" -D_TTY_GROUPNAME=\"${TTY_GROUPNAME}\"
519

620
ifeq (${PIREV},$(filter ${PIREV},a01041 a21041))
721
# a01041 and a21041 are PI 2 Model B and armv7
@@ -51,3 +65,42 @@ ${GATEWAY_SERIAL}: ${OBJS} ${GATEWAY_SERIAL_OBJS}
5165
clean:
5266
rm -rf $(PROGRAMS) $(GATEWAY) $(GATEWAY_SERIAL) ${OBJS} $(GATEWAY_OBJS) $(GATEWAY_SERIAL_OBJS)
5367

68+
install: all install-gatewayserial install-gateway install-initscripts
69+
70+
install-gatewayserial:
71+
@echo "Installing ${GATEWAY_SERIAL} to ${BINDIR}"
72+
@install -m 0755 ${GATEWAY_SERIAL} ${BINDIR}
73+
74+
install-gateway:
75+
@echo "Installing ${GATEWAY} to ${BINDIR}"
76+
@install -m 0755 ${GATEWAY} ${BINDIR}
77+
78+
install-initscripts:
79+
@echo "Installing initscripts to /etc/init.d"
80+
@install -m 0755 initscripts/PiGatewaySerial /etc/init.d
81+
@install -m 0755 initscripts/PiGateway /etc/init.d
82+
@echo "Installing syslog config to /etc/rsyslog.d"
83+
@install -m 0755 initscripts/30-PiGatewaySerial.conf /etc/rsyslog.d
84+
@install -m 0755 initscripts/30-PiGateway.conf /etc/rsyslog.d
85+
@service rsyslog restart
86+
87+
enable-gw: install
88+
@update-rc.d PiGateway defaults
89+
90+
enable-gwserial: install
91+
@update-rc.d PiGatewaySerial defaults
92+
93+
remove-gw:
94+
@update-rc.d -f PiGateway remove
95+
96+
remove-gwserial:
97+
@update-rc.d -f PiGatewaySerial remove
98+
99+
uninstall: remove-gw remove-gwserial
100+
@echo "Stopping daemon PiGatewaySerial (ignore errors)"
101+
-@service PiGatewaySerial stop
102+
@echo "Stopping daemon PiGateway (ignore errors)"
103+
-@service PiGateway stop
104+
@echo "removing files"
105+
rm ${BINDIR}/PiGatewaySerial ${BINDIR}/PiGateway /etc/init.d/PiGatewaySerial /etc/init.d/PiGateway /etc/rsyslog.d/30-PiGatewaySerial.conf /etc/rsyslog.d/30-PiGateway.conf
106+

MySensor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ boolean MySensor::process() {
279279

280280
if (!available || pipe>6)
281281
return false;
282-
282+
memset(&msg,0,sizeof(MyMessage));
283283
uint8_t len = RF24::getDynamicPayloadSize();
284284
RF24::read(&msg, len);
285285
RF24::writeAckPayload(pipe,&pipe, 1 );

MySensor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
#include <string>
4545
#include <getopt.h>
4646
#include <iostream>
47+
#include <syslog.h>
4748
#endif
4849

4950
#ifdef DEBUG
50-
#define debug(x,...) debugPrint(x, ##__VA_ARGS__)
51+
extern void log(int priority, const char *format, ...);
52+
#define debug(x,...) log(LOG_DEBUG, x, ##__VA_ARGS__)
5153
#else
5254
#define debug(x,...)
5355
#endif

PiGateway.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,40 @@
1515

1616
MyGateway *gw;
1717

18+
int daemonizeFlag = 0;
19+
20+
void openSyslog()
21+
{
22+
setlogmask(LOG_UPTO (LOG_INFO));
23+
openlog(NULL, 0, LOG_USER);
24+
}
25+
26+
void closeSyslog()
27+
{
28+
closelog();
29+
}
30+
31+
void log(int priority, const char *format, ...)
32+
{
33+
va_list argptr;
34+
va_start(argptr, format);
35+
if (daemonizeFlag == 1) {
36+
vsyslog(priority, format, argptr);
37+
} else {
38+
vprintf(format, argptr);
39+
}
40+
va_end(argptr);
41+
}
42+
1843
void msgCallback(char *msg){
1944
printf("[CALLBACK]%s", msg);
2045

2146
}
2247

2348
void setup(void)
2449
{
25-
printf("Starting Gateway...\n");
26-
gw = new MyGateway(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ, 30000);
50+
printf("Starting Gateway...\n");
51+
gw = new MyGateway(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ, 60);
2752

2853
if (gw == NULL)
2954
{
@@ -39,9 +64,10 @@ void loop(void)
3964

4065
int main(int argc, char** argv)
4166
{
67+
openSyslog();
4268
setup();
4369
while(1)
4470
loop();
45-
71+
closeSyslog();
4672
return 0;
4773
}

0 commit comments

Comments
 (0)