summaryrefslogtreecommitdiff
path: root/src/gpio.c
blob: fd46fc7bdcfba0a1de8d27c955ad7a2896a086e3 (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
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int modemPowerToggle(char *pathToChip, int pinNum) {
  struct gpiod_chip *chip;
  struct gpiod_line *line;

  chip = gpiod_chip_open(pathToChip);
  if (!chip) {
    perror("Error opening GPIO chip");
    return 1;
  }

  line = gpiod_chip_get_line(chip, pinNum);
  if (!line) {
    perror("Error getting GPIO line");
    gpiod_chip_close(chip);
    return 1;
  }

  if (gpiod_line_request_output(line, "cellular modem power toggle", 0) < 0) {
    perror("Error setting GPIO line as output");
    gpiod_chip_close(chip);
    return 1;
  }

  if (gpiod_line_set_value(line, 1) < 0) {
    perror("Error setting GPIO line high");
    gpiod_chip_close(chip);
    return 1;
  }

  sleep(1);

  if (gpiod_line_set_value(line, 0) < 0) {
    perror("Error setting GPIO line low");
    gpiod_chip_close(chip);
    return 1;
  }

  gpiod_chip_close(chip);
  return 0;
}

int checkPinVal(char *pathToChip, int pinNum) {
  int pinVal = -1;
  struct gpiod_chip *chip;
  struct gpiod_line *line;

  chip = gpiod_chip_open(pathToChip);
  if (!chip) {
    perror("Error opening GPIO chip");
    return -1;
  }

  line = gpiod_chip_get_line(chip, pinNum);
  if (!line) {
    perror("Error getting GPIO line");
    gpiod_chip_close(chip);
    return -1;
  }

  if (gpiod_line_request_input(line, "cellular modem power status") < 0) {
    perror("Error setting GPIO line as input");
    gpiod_chip_close(chip);
    return -1;
  }

  pinVal = gpiod_line_get_value(line);
  if (pinVal < 0) {
    perror("Error getting GPIO line value");
    gpiod_chip_close(chip);
    return -1;
  }

  gpiod_chip_close(chip);
  return pinVal;
}