-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsig_receiver.c
More file actions
65 lines (55 loc) · 1.42 KB
/
sig_receiver.c
File metadata and controls
65 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<signal.h>
#include<time.h>
#include<sys/types.h>
#define MYSIGNAL SIGUSR1 // 10
#define MYEXITSIGNAL SIGINT // 2
void errExit(char *msg){
perror(msg);
exit(1);
}
int led_toggle = 1,int_exit = 0,int_sig_received = 0;
static void handler(int sig,siginfo_t *si,void *uc){
if(sig == MYSIGNAL){
int_sig_received = 1;
led_toggle = !led_toggle; // if on turn off and if off turn on
}
else if(sig == MYEXITSIGNAL){
int_sig_received = 1;
int_exit = 1;
}
else{
printf("Unknown Signal\n");
}
} // instead of if-else we can also use switch case for different cases
int main(){
struct sigaction sa_my,sa_int;
printf("\nSignal Receiver using gpioset\n");
printf("My Process ID = %d\n",getpid()); // getpid() - returns process id
// Register SIGUSR1
sa_my.sa_flags = SA_SIGINFO;
sa_my.sa_sigaction = handler;
sigemptyset(&sa_my.sa_mask);
if(sigaction(MYSIGNAL,&sa_my,NULL) == -1){
errExit("sigaction");
}
while(int_exit == 0){
pause();
if(int_sig_received == 1){
if(led_toggle == 0){
system("gpioset gpiochip1 10=0");
printf("LED OFF\n");
}
else {
system("gpioset gpiochip1 10=1");
printf("LED ON\n");
}
int_sig_received = 0;
} //if
}// while
printf("\nSignal Receiver : Terminated\n");
return 0;
}