summaryrefslogtreecommitdiff
path: root/src/x
blob: 9f7de3150bc14ab671a20426779535eab925f164 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * mts-wait-for-cell-reset
 *
 * Block signal
 * If not in reset, exit
 * Register with mts-io for reset
 * If not in reset, exit
 * Suspend process for signal
 * If signal occurs, exit
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

const char *name = "mts-wait-for-cell-reset";
const int RST_SIG=SIGUSR2;  // Wait for this signal
const char *MONITOR="/sys/devices/platform/mts-io/radio-reset-monitor";
const char *RADIO_DISCOVERY="/sys/devices/platform/mts-io/radio-udev-discovery";

/* Return 0 if Cellular modem is being reset
 * Return 1 if Cellular modem is not being reset
 */
int
not_reset(const char *attr_name)
{
    int not_in_reset, result;
    char buf[256];
    int discovery;

    discovery = open(attr_name,O_RDONLY);
    if (discovery == -1) {
        fprintf(stderr,"FAILED: %s cannot open attribute %s: %s\n",name,attr_name,strerror(errno));
        exit(1);
    }
    result = read(discovery,buf,sizeof buf);
    if (result > 0)
        not_in_reset = atoi(buf);
    else if (result == 0) {
        fprintf(stderr,"FAILED: %s attribute %s has no state\n",name,attr_name);
        exit(1);
    } else {
        fprintf(stderr,"FAILED: %s attribute %s error: %s\n",name,attr_name,strerror(errno));
        exit(1);
    }
    close(discovery);
    return not_in_reset;
}

void
handler(int sig)
{
    /* Normal way out of this program
     * exit not safe from signal handler so
     * use _exit()
     */
    _exit(0);
}

int
main(int argc, char *argv[])
{
    sigset_t blockedset,nullset;
    int monitor;
    int result;
    char buf[128];
    struct sigaction action;

    memset(&action,0,sizeof action);
    action.sa_handler = handler;
    memset(&blockedset,0,sizeof blockedset);
    sigaddset(&blockedset,RST_SIG);
    memset(&nullset,0,sizeof nullset);

    sigprocmask(SIG_BLOCK,&blockedset,NULL);  // Blocked RST_SIG

    sigaction(RST_SIG,&action,NULL);

    if (not_reset(RADIO_DISCOVERY))
        exit(0);  // Not in reset
    sprintf(buf,"%d %d",getpid(),RST_SIG);

    monitor = open(MONITOR,O_WRONLY);
    if (monitor == -1) {
        fprintf(stderr,"FAILED: %s cannot open attribute %s: %s\n",name,MONITOR,strerror(errno));
        exit(2);
    }
    result = write(monitor,buf,strlen(buf));
    if(result != strlen(buf)) {
        if (result == -1) {
            fprintf(stderr,"FAILED: %s attribute %s write error: %s\n",name,MONITOR,strerror(errno));
            exit(3);
        }
        fprintf(stderr,"FAILED: %s attribute %s incomplete write error: %d/%d\n",name,MONITOR,result,(int)strlen(buf));
        exit(4);
    }
    close(monitor);
    if (not_reset(RADIO_DISCOVERY))
        exit(0);

    sigsuspend(&nullset); // Allow all signals
    /* NOTREACHED */
}