summaryrefslogtreecommitdiff
path: root/src/utils.c
blob: ae7ae02c6e36ea2815f1076d11f6e8c7b505951b (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"

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, "pin 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;
}

int readCfg(struct structCfg *dataCfg, const char *pathExe) {
  char strBuffer[BUFSIZ];
  char *pathCfg, *pathDirEnd, *tmpPtr = NULL;
  const char *filenameCfg = "network_monitor.cfg";
  int sizePathDir;
  unsigned long numCharRead;
  FILE *fileCfg;

  /* Form config filepath string. */
  pathDirEnd = strrchr(pathExe, '/');
  sizePathDir = pathDirEnd - pathExe;
  if (pathDirEnd == NULL) {
    printf("Cannot obtain the executable directory.\n");
    return 1;
  }
  pathCfg = malloc(sizeof(char) * (sizePathDir + strlen(filenameCfg) + 2));
  strncpy(pathCfg, pathExe, sizePathDir + 1);
  strcat(pathCfg, filenameCfg);

  /* Open the config file. */
  fileCfg = fopen(pathCfg, "r");
  if (fileCfg == NULL) {
    printf("Cannot open config file \"%s\".\n", pathCfg);
    goto readCfgFail1;
  }

  /* Check that it is not empty. */
  if (fgets(strBuffer, BUFSIZ, fileCfg) == NULL) {
    printf("Config file is empty.\n");
    goto readCfgFail2;
  }

  /* Read the file and look for the "path_to_serial_port". */
  rewind(fileCfg);
  do {
    fgets(strBuffer, BUFSIZ, fileCfg);
    tmpPtr = strtok(strBuffer, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;
    if (strcmp("path_to_serial_port", tmpPtr) != 0)
      continue;

    tmpPtr = strtok(NULL, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;

    dataCfg->pathSerialPort =
        realloc(dataCfg->pathSerialPort, sizeof(char) * (strlen(tmpPtr) + 1));
    strcpy(dataCfg->pathSerialPort, tmpPtr);
    break;
  } while (feof(fileCfg) == 0);
  if (dataCfg->pathSerialPort == NULL) {
    printf("Could not find the \"path_to_serial_port\" variable.\n");
    goto readCfgFail2;
  }

  /* Read the file and look for the "path_to_gpio_chip". */
  rewind(fileCfg);
  do {
    fgets(strBuffer, BUFSIZ, fileCfg);
    tmpPtr = strtok(strBuffer, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;
    if (strcmp("path_to_gpio_chip", tmpPtr) != 0)
      continue;

    tmpPtr = strtok(NULL, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;

    dataCfg->pathGpioChip =
        realloc(dataCfg->pathGpioChip, sizeof(char) * (strlen(tmpPtr) + 1));
    strcpy(dataCfg->pathGpioChip, tmpPtr);
    break;
  } while (feof(fileCfg) == 0);
  if (dataCfg->pathGpioChip == NULL) {
    printf("Could not find the \"path_to_gpio_chip\" variable.\n");
    goto readCfgFail3;
  }

  /* Read the file and look for the "pin_pwr_key". */
  rewind(fileCfg);
  do {
    fgets(strBuffer, BUFSIZ, fileCfg);
    tmpPtr = strtok(strBuffer, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;
    if (strcmp("pin_pwr_key", tmpPtr) != 0)
      continue;

    tmpPtr = strtok(NULL, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;

    sscanf(tmpPtr, "%d%ln", &dataCfg->pinPwrKey, &numCharRead);
    if ((numCharRead != strlen(tmpPtr)) || (strtok(NULL, " =\n\t\r\"") != NULL)) {
      printf("Format error for \"pin_pwr_key\".\n");
      goto readCfgFail4;
    }
    break;
  } while (feof(fileCfg) == 0);
  if (dataCfg->pinPwrKey == -1) {
    printf("Error parsing the config file.\nCould not find the "
           "\"pin_pwr_key\" variable.\n");
    goto readCfgFail4;
  }

  /* Read the file and look for the "pin_status". */
  rewind(fileCfg);
  do {
    fgets(strBuffer, BUFSIZ, fileCfg);
    tmpPtr = strtok(strBuffer, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;
    if (strcmp("pin_status", tmpPtr) != 0)
      continue;

    tmpPtr = strtok(NULL, " =\n\t\r\"");
    if (tmpPtr == NULL)
      continue;

    sscanf(tmpPtr, "%d%ln", &dataCfg->pinStatus, &numCharRead);
    if ((numCharRead != strlen(tmpPtr)) || (strtok(NULL, " =\n\t\r\"") != NULL)) {
      printf("Format error for \"pin_status\".\n");
      goto readCfgFail4;
    }
    break;
  } while (feof(fileCfg) == 0);
  if (dataCfg->pinStatus == -1) {
    printf("Error parsing the config file.\nCould not find the "
           "\"pin_status\" variable.\n");
    goto readCfgFail4;
  }

  free(pathCfg);
  fclose(fileCfg);
  return 0;

readCfgFail4:
  free(dataCfg->pathGpioChip);
readCfgFail3:
  free(dataCfg->pathSerialPort);
readCfgFail2:
  fclose(fileCfg);
readCfgFail1:
  free(pathCfg);
  return 1;
}

/* Do not call this before initializing dataCfg with readCfg(). */
int freeCfgData(struct structCfg *dataCfg) {
  dataCfg->pinPwrKey = -1;
  dataCfg->pinStatus = -1;
  free(dataCfg->pathGpioChip);
  free(dataCfg->pathSerialPort);
  return 0;
}