mirror of
https://github.com/miky-kr5/Robotd.git
synced 2023-01-29 18:47:06 +00:00
Fixed init script. Better stopping code.
This commit is contained in:
16
init.sh
Normal file → Executable file
16
init.sh
Normal file → Executable file
@@ -1,4 +1,4 @@
|
|||||||
#! /bin/bash
|
#! /bin/sh
|
||||||
### BEGIN INIT INFO
|
### BEGIN INIT INFO
|
||||||
# Provides: robotd
|
# Provides: robotd
|
||||||
# Required-Start:
|
# Required-Start:
|
||||||
@@ -9,8 +9,7 @@
|
|||||||
### END INIT INFO
|
### END INIT INFO
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
PID=`pidof robotd`
|
if pidof robotd > /dev/null
|
||||||
if [ $? -eq 0 ]
|
|
||||||
then
|
then
|
||||||
echo "robotd already started."
|
echo "robotd already started."
|
||||||
return
|
return
|
||||||
@@ -22,8 +21,7 @@ start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
PID=`pidof weblabsd`
|
if pidof robotd > /dev/null
|
||||||
if [ $? -eq 0 ]
|
|
||||||
then
|
then
|
||||||
echo "Stopping robotd"
|
echo "Stopping robotd"
|
||||||
kill -s INT `pidof robotd`
|
kill -s INT `pidof robotd`
|
||||||
@@ -34,15 +32,14 @@ stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start)
|
start|restart|force-reload)
|
||||||
start
|
start
|
||||||
;;
|
;;
|
||||||
stop)
|
stop)
|
||||||
stop
|
stop
|
||||||
;;
|
;;
|
||||||
status)
|
status)
|
||||||
PID=`pidof robotd`
|
if pidof robotd > /dev/null
|
||||||
if [ $? -eq 0 ]
|
|
||||||
then
|
then
|
||||||
echo "robotd is running."
|
echo "robotd is running."
|
||||||
else
|
else
|
||||||
@@ -54,8 +51,7 @@ case "$1" in
|
|||||||
start
|
start
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Usage: {start|stop|restart|status}"
|
echo "Usage: {start|stop|restart|force-reload|status}"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
exit $?
|
|
||||||
|
61
robotd.c
61
robotd.c
@@ -2,21 +2,37 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <mraa.h>
|
#include <mraa.h>
|
||||||
|
|
||||||
#define GPIO_PIN_4 4
|
#define GPIO_PIN_4 4
|
||||||
#define GPIO_PIN_13 13
|
#define GPIO_PIN_13 13
|
||||||
|
|
||||||
|
mraa_result_t mr;
|
||||||
|
mraa_gpio_context pin13, pin4;
|
||||||
|
|
||||||
|
void clean_up() {
|
||||||
|
syslog(LOG_DAEMON | LOG_WARNING, "Cleaning up.");
|
||||||
|
mr = mraa_gpio_write(pin4, 0);
|
||||||
|
// TODO: MRAA error handling
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sig_handler(int signal) {
|
||||||
|
// Turn off the red LED
|
||||||
|
syslog(LOG_DAEMON | LOG_WARNING, "Robot daemon interrupted.");
|
||||||
|
clean_up();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char ** argv) {
|
int main(int argc, char ** argv) {
|
||||||
int rv;
|
int rv;
|
||||||
bool done = false;
|
bool done = false;
|
||||||
mraa_gpio_context pin13, pin4;
|
sighandler_t sh;
|
||||||
mraa_result_t mr;
|
|
||||||
|
|
||||||
// Turn the process into a daemon.
|
// Turn the process into a daemon.
|
||||||
rv = daemon(0, 1);
|
rv = daemon(0, 1);
|
||||||
@@ -26,6 +42,14 @@ int main(int argc, char ** argv) {
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set SIGINT signal handler
|
||||||
|
sh = signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
|
if (sh == SIG_ERR) {
|
||||||
|
perror("sh = signal(SIGINT, sig_handler)");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
// Open a syslog connection
|
// Open a syslog connection
|
||||||
openlog(argv[0], LOG_NDELAY | LOG_PID, LOG_DAEMON);
|
openlog(argv[0], LOG_NDELAY | LOG_PID, LOG_DAEMON);
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Started robot daemon.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Started robot daemon.");
|
||||||
@@ -36,22 +60,22 @@ int main(int argc, char ** argv) {
|
|||||||
mr = mraa_gpio_mode(pin13, MRAA_GPIO_PULLUP);
|
mr = mraa_gpio_mode(pin13, MRAA_GPIO_PULLUP);
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Push button ready.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Push button ready.");
|
||||||
|
|
||||||
// TODO: MRAA error handling.
|
// TODO: MRAA error handling
|
||||||
|
|
||||||
// Open PIN 4 as an output pin for the red indicator LED
|
// Open PIN 4 as an output pin for the red indicator LED
|
||||||
pin4 = mraa_gpio_init(GPIO_PIN_4);
|
pin4 = mraa_gpio_init(GPIO_PIN_4);
|
||||||
mr = mraa_gpio_dir(pin4, MRAA_GPIO_OUT);
|
mr = mraa_gpio_dir(pin4, MRAA_GPIO_OUT);
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Red LED ready.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Red LED ready.");
|
||||||
|
|
||||||
// TODO: MRAA error handling.
|
// TODO: MRAA error handling
|
||||||
|
|
||||||
// Star pin processing loop
|
// Start pin processing loop
|
||||||
while (!done) {
|
while (!done) {
|
||||||
// Keep the red LED on and read from the push button pin
|
// Keep the red LED on and read from the push button pin
|
||||||
mr = mraa_gpio_write(pin4, 1);
|
mr = mraa_gpio_write(pin4, 1);
|
||||||
rv = mraa_gpio_read(pin13);
|
rv = mraa_gpio_read(pin13);
|
||||||
|
|
||||||
// TODO: MRAA error handling.
|
// TODO: MRAA error handling
|
||||||
|
|
||||||
if (rv == 0) {
|
if (rv == 0) {
|
||||||
// If the button is pressed then stop reading and go on with the program
|
// If the button is pressed then stop reading and go on with the program
|
||||||
@@ -63,14 +87,26 @@ int main(int argc, char ** argv) {
|
|||||||
// Turn off the red LED
|
// Turn off the red LED
|
||||||
mr = mraa_gpio_write(pin4, 0);
|
mr = mraa_gpio_write(pin4, 0);
|
||||||
|
|
||||||
// TODO: MRAA error handling.
|
// TODO: MRAA error handling
|
||||||
|
|
||||||
// Open the source code file for the Python script
|
// Open the source code file for the Python script
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Opening Python source code.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Opening Python source code.");
|
||||||
FILE * code = fopen("/home/root/robot.py", "r");
|
FILE * code = fopen("/home/root/Robotd/robot.py", "r");
|
||||||
|
|
||||||
if (code == NULL) {
|
if (code == NULL) {
|
||||||
|
perror("FILE * code = fopen(\"/home/root/Robotd/robot.py\", \"r\")");
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Failed to load Python program.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Failed to load Python program.");
|
||||||
|
clean_up();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return SIGINT handler to it's default state
|
||||||
|
sh = signal(SIGINT, SIG_DFL);
|
||||||
|
|
||||||
|
if (sh == SIG_ERR) {
|
||||||
|
perror("sh = signal(SIGINT, SIG_DLF)");
|
||||||
|
syslog(LOG_DAEMON | LOG_NOTICE, "Failed to disable signal handler.");
|
||||||
|
clean_up();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +114,13 @@ int main(int argc, char ** argv) {
|
|||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Performing Python call.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Performing Python call.");
|
||||||
Py_SetProgramName(argv[0]);
|
Py_SetProgramName(argv[0]);
|
||||||
Py_Initialize();
|
Py_Initialize();
|
||||||
PyRun_SimpleFile(code, "/home/root/robot.py");
|
PyRun_SimpleFile(code, "/home/root/Robotd/robot.py");
|
||||||
Py_Finalize();
|
Py_Finalize();
|
||||||
|
|
||||||
// TODO: Python error handling.
|
// TODO: Python error handling
|
||||||
|
|
||||||
|
// Close the source code file
|
||||||
|
fclose(code);
|
||||||
|
|
||||||
// Clean up and leave
|
// Clean up and leave
|
||||||
syslog(LOG_DAEMON | LOG_NOTICE, "Finishing.");
|
syslog(LOG_DAEMON | LOG_NOTICE, "Finishing.");
|
||||||
|
Reference in New Issue
Block a user