summaryrefslogtreecommitdiff
path: root/src/main.c
blob: df33ffbe5fbf1b6ffd43d2e0f073ba4d57e3a54a (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <gpio.h>
#include <serial.h>

int main() {

  char *commandList[] = {"AT", "ATI", "AT+COPS?", "AT+COPS=?"},
       serialFilePath[] = "/dev/serial0", *output;

  int serialPort, moduleStatus;
  unsigned long idx;
  time_t pwrKeyTime;

  moduleStatus = checkPinVal(27);
  if (0 > moduleStatus) {
    printf("Error checkPinVal()\n");
    return 1;
  } else if (moduleStatus == 0) {
    printf("Module is off\nPowering on\n");
    if (0 != modemPowerToggle(17)) {
      printf("Error toggling modem power\n");
      return 1;
    }
    pwrKeyTime = time(NULL);
    while (!checkPinVal(27)) {
      sleep(1);
      if (time(NULL) - pwrKeyTime > 10) {
        printf("Modem power on takes longer than 10s\nAborting\n");
        return 1;
      }
    }
  }

  serialPort = openSerialPort(serialFilePath);
  if (serialPort < 0) {
    printf("Error openSerialPort()\n");
    return 1;
  }

  output = malloc(1);
  for (idx = 0; idx < sizeof(commandList) / sizeof(*commandList); idx++) {
    if (0 != querySerialPort(&output, serialPort, commandList[idx])) {
      printf("Error querySerialPort()\n");
      close(serialPort);
      free(output);
      return 1;
    }
    printf("%s\n", output);
  }

  close(serialPort);

  free(output);
  return 0;
}