Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemonize #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ base64.o: base64.c
$(CC) $(CFLAGS) base64.c

clean:
rm *.o single_chan_pkt_fwd
rm *.o single_chan_pkt_fwd

install : SCLoRaGWd
cp SCLoRaGWd /usr/local/bin
cp SCLoRaGWdService /etc/init.d/SCLoRaGWd
chmod +x /etc/init.d/SCLoRaGWd
update-rc.d SCLoRaGWd defaults
service SCLoRaGWd start

uninstall:
service SCLoRaGWd stop
update-rc.d -f SCLoRaGWd remove
rm /etc/init.d/SCLoRaGWd
rm /usr/local/bin/SCLoRaGWd

SCLoRaGWd : main.cpp base64.c
$(CC) -Wall main.cpp base64.c -o SCLoRaGWd $(LIBS) -DDAEMON
52 changes: 52 additions & 0 deletions SCLoRaGWdService
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh
# /etc/init.d/SCLoRaGWd

### BEGIN INIT INFO
# Provides: SCLoRaGWd
# Required-Start: $all
# Required-Stop: $all
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Start-Before:
# X-Start-After: openhab
# Short-Description: Single Channel LoRa Packet Forwarder daemon
# Description: Get data from LoRa module and push it to TheThingsNetwork
### END INIT INFO

# Adapted from noip2 (http://www.togaware.com/linux/survivor/No_IP_Manual.html)

# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions
DAEMON=/usr/local/bin/SCLoRaGWd
NAME=SCLoRaGWd
PIDFILE=/var/run/SCLoRaGWd.pid

test -x $DAEMON || exit 0

case "$1" in
start)
echo -n "Starting LoRa Gateway: "
start-stop-daemon --start --exec $DAEMON --pidfile $PIDFILE --name $NAME
echo $NAME
;;
stop)
echo -n "Stopping Lora Gateway daemon: "
start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON --name $NAME
echo $NAME
;;

restart)
echo -n "Restarting Lora Gateway daemon: "
start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON
start-stop-daemon --start --exec $DAEMON
echo $NAME
;;
*)
echo "Usage: $0 start|stop|restart"
exit 1
;;
esac

exit 0
90 changes: 74 additions & 16 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
#include <sys/ioctl.h>
#include <net/if.h>

#include <errno.h>

#ifdef DAEMON
// Daemon support
#include <sys/stat.h>
#include <unistd.h>
#include <syslog.h>

#define LOG(...) do { syslog(LOG_INFO, __VA_ARGS__); } while (0)
#define LOG_E(...) do { syslog(LOG_ERR, __VA_ARGS__); } while (0)
#else
#define LOG(...) do { printf(__VA_ARGS__); } while (0)
#define LOG_E(...) do { printf(__VA_ARGS__); } while (0)
#endif

using namespace std;

#include "base64.h"
Expand Down Expand Up @@ -164,7 +179,9 @@ static char description[64] = ""; /* used for free form d

void die(const char *s)
{
perror(s);
LOG(__FUNCTION__);
LOG_E(s);
perror(s);
exit(1);
}

Expand Down Expand Up @@ -217,7 +234,7 @@ boolean receivePkt(char *payload)
// payload crc: 0x20
if((irqflags & 0x20) == 0x20)
{
printf("CRC error\n");
LOG("CRC error\n");
writeRegister(REG_IRQ_FLAGS, 0x20);
return false;
} else {
Expand Down Expand Up @@ -250,7 +267,7 @@ void SetupLoRa()

if (version == 0x22) {
// sx1272
printf("SX1272 detected, starting.\n");
LOG("SX1272 detected, starting.\n");
sx1272 = true;
} else {
// sx1276?
Expand All @@ -261,10 +278,10 @@ void SetupLoRa()
version = readRegister(REG_VERSION);
if (version == 0x12) {
// sx1276
printf("SX1276 detected, starting.\n");
LOG("SX1276 detected, starting.\n");
sx1272 = false;
} else {
printf("Unrecognized transceiver.\n");
LOG("Unrecognized transceiver.\n");
//printf("Version: 0x%x\n",version);
exit(1);
}
Expand Down Expand Up @@ -320,7 +337,8 @@ void sendudp(char *msg, int length) {
inet_aton(SERVER1 , &si_other.sin_addr);
if (sendto(s, (char *)msg, length, 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
LOG_E("Error sending UDP %d", errno);
// die("sendto()");
}
#endif

Expand Down Expand Up @@ -369,7 +387,7 @@ void sendstat() {
stat_index += j;
status_report[stat_index] = 0; /* add string terminator, for safety */

printf("stat update: %s\n", (char *)(status_report+12)); /* DEBUG: display JSON stat */
LOG("stat update: %s\n", (char *)(status_report+12)); /* DEBUG: display JSON stat */

//send the update
sendudp(status_report, stat_index);
Expand Down Expand Up @@ -403,11 +421,11 @@ void receivepacket() {
rssicorr = 157;
}

printf("Packet RSSI: %d, ",readRegister(0x1A)-rssicorr);
printf("RSSI: %d, ",readRegister(0x1B)-rssicorr);
printf("SNR: %li, ",SNR);
printf("Length: %i",(int)receivedbytes);
printf("\n");
LOG("Packet RSSI: %d, ",readRegister(0x1A)-rssicorr);
LOG("RSSI: %d, ",readRegister(0x1B)-rssicorr);
LOG("SNR: %li, ",SNR);
LOG("Length: %i",(int)receivedbytes);
LOG("\n");

int j;
j = bin_to_b64((uint8_t *)message, receivedbytes, (char *)(b64), 341);
Expand Down Expand Up @@ -519,7 +537,7 @@ void receivepacket() {
++buff_index;
buff_up[buff_index] = 0; /* add string terminator, for safety */

printf("rxpk update: %s\n", (char *)(buff_up + 12)); /* DEBUG: display JSON payload */
LOG("rxpk update: %s\n", (char *)(buff_up + 12)); /* DEBUG: display JSON payload */

//send the messages
sendudp(buff_up, buff_index);
Expand All @@ -533,6 +551,46 @@ void receivepacket() {

int main () {

#ifdef DAEMON
//Adapted from http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
pid_t pid, sid;

openlog("SingleChanPktFwd", LOG_PID, LOG_USER);

pid = fork();
if (pid < 0) {
LOG("fork failed");
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
LOG("Child spawned, pid %d\n", pid);
exit(EXIT_SUCCESS);
}

/* Change the file mode mask */
umask(0);

/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
LOG("setsid failed");
exit(EXIT_FAILURE);
}

/* Change the current working directory */
if ((chdir("/")) < 0) {
LOG("chdir failed");
exit(EXIT_FAILURE);
}

/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
#endif //DAEMON

struct timeval nowtime;
uint32_t lasttime;

Expand Down Expand Up @@ -560,16 +618,16 @@ int main () {
ioctl(s, SIOCGIFHWADDR, &ifr);

/* display result */
printf("Gateway ID: %.2x:%.2x:%.2x:ff:ff:%.2x:%.2x:%.2x\n",
LOG("Gateway ID: %.2x:%.2x:%.2x:ff:ff:%.2x:%.2x:%.2x\n",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);

printf("Listening at SF%i on %.6lf Mhz.\n", sf,(double)freq/1000000);
printf("------------------\n");
LOG("Listening at SF%i on %.6lf Mhz.\n", sf,(double)freq/1000000);
LOG("------------------\n");

while(1) {

Expand Down