I read about Daemon Programming and i think I’ll be needing this to detect my devices if online or not eg(RS232, usb, ethernet). then fetch in a webservice PHP.
The code from this site. http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
I added some parts to test if i can detect devices.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int devfd;
int main(int argc, char *argv[]) {
int c;
while((c=getopt(argc,argv,"s")) != -1)
switch(c){
case 's': devfd = open("/dev/ttyUSB0", O_RDWR);
if(devfd==-1){
printf("offline\n");
}
else{
printf("online\n");
}
break;
default: printf("Wrong command\n");
}
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Daemon-specific initialization goes here */
/* The Big Loop */
while (1) {
}
exit(EXIT_SUCCESS);
}
I added this code..
while((c=getopt(argc,argv,"s")) != -1)
switch(c){
case 's': devfd = open("/dev/ttyUSB0", O_RDWR);
if(devfd==-1){
printf("offline\n");
}
else{
printf("online\n");
}
break;
default: printf("Wrong command\n");
}
so doing it like this in the terminal.
./daemon -s //prints offline because the device USB0 is not connected.
Is there another way for me to detect my devices?
Thanks,
If it has udev, you can use libudev to detect and monitor your devices. Checkout nice tutorial from signal11.