summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bg96.c44
-rw-r--r--src/gpio.c79
-rw-r--r--src/main.c66
-rw-r--r--src/utils.c199
4 files changed, 284 insertions, 104 deletions
diff --git a/src/bg96.c b/src/bg96.c
index 620c960..21cd848 100644
--- a/src/bg96.c
+++ b/src/bg96.c
@@ -1,6 +1,7 @@
#include "bg96.h"
-#include "gpio.h"
#include "serial.h"
+#include "utils.h"
+#include <gpiod.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
@@ -9,6 +10,47 @@
#include <time.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 initNetworkData(networkData *nD) {
nD->operatorName = calloc(1, sizeof(char));
nD->networkName = calloc(1, sizeof(char));
diff --git a/src/gpio.c b/src/gpio.c
deleted file mode 100644
index fd46fc7..0000000
--- a/src/gpio.c
+++ /dev/null
@@ -1,79 +0,0 @@
-#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;
-}
diff --git a/src/main.c b/src/main.c
index dbe85d4..13b0b3f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,100 +5,115 @@
#include <unistd.h>
#include "bg96.h"
+#include "utils.h"
-#define PATH_TO_SERIAL_PORT "/dev/serial0"
-#define PATH_TO_GPIO_CHIP "/dev/gpiochip0"
-#define PIN_NUM_PWR_KEY 17
-#define PIN_NUM_STATUS 27
+struct structCfg dataCfg = {NULL, NULL, -1, -1};
int main(int argc, char **argv) {
if (argc == 1) {
- printf("No arguments provided\n");
+ printf("No arguments provided.\n");
return 1;
}
- else if (0 == strcmp(argv[1], "--initpublic")) {
+ if (readCfg(&dataCfg, argv[0]) != 0) {
+ printf("Error reading the config file.\n");
+ return 1;
+ }
+
+ if (0 == strcmp(argv[1], "--initpublic")) {
printf("Powering on\n");
- if (0 != powerOn(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, PIN_NUM_STATUS,
- PIN_NUM_PWR_KEY)) {
+ if (0 != powerOn(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
+ dataCfg.pinStatus, dataCfg.pinPwrKey)) {
printf("Error powering the module on\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Initiating GNSS\n");
- if (0 != initGNSS(PATH_TO_SERIAL_PORT)) {
+ if (0 != initGNSS(dataCfg.pathSerialPort)) {
printf("Error initiating GNSS\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Configuring the network settings for DNA public network\n");
- if (0 != cfgPublicNetwork(PATH_TO_SERIAL_PORT)) {
+ if (0 != cfgPublicNetwork(dataCfg.pathSerialPort)) {
printf("Error setting public network configuration\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Waiting for establishing network connection\n");
- if (0 != waitForNetwork(PATH_TO_SERIAL_PORT, 120)) {
+ if (0 != waitForNetwork(dataCfg.pathSerialPort, 120)) {
printf("Error while waiting for network\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Initialization complete\n");
+ freeCfgData(&dataCfg);
return 0;
}
else if (0 == strcmp(argv[1], "--init6GTN")) {
printf("Powering on\n");
- if (0 != powerOn(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, PIN_NUM_STATUS,
- PIN_NUM_PWR_KEY)) {
+ if (0 != powerOn(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
+ dataCfg.pinStatus, dataCfg.pinPwrKey)) {
printf("Error powering the module on\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Initiating GNSS\n");
- if (0 != initGNSS(PATH_TO_SERIAL_PORT)) {
+ if (0 != initGNSS(dataCfg.pathSerialPort)) {
printf("Error initiating GNSS\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Configuring the network settings for 6GTN\n");
- if (0 != cfg6GTN(PATH_TO_SERIAL_PORT)) {
+ if (0 != cfg6GTN(dataCfg.pathSerialPort)) {
printf("Error setting 6GTN network configuration\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Waiting for establishing network connection\n");
- if (0 != connect6GTN(PATH_TO_SERIAL_PORT, 120)) {
+ if (0 != connect6GTN(dataCfg.pathSerialPort, 120)) {
printf("Error while connecting to 6GTN\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Initialization complete\n");
+ freeCfgData(&dataCfg);
return 0;
}
else if (0 == strcmp(argv[1], "--stop")) {
printf("Stopping GNSS\n");
- if (0 != stopGNSS(PATH_TO_SERIAL_PORT)) {
+ if (0 != stopGNSS(dataCfg.pathSerialPort)) {
printf("Error stopping GNSS\n");
}
printf("Disconnecting MQTT\n");
- if (0 != mqttDisc(PATH_TO_SERIAL_PORT)) {
+ if (0 != mqttDisc(dataCfg.pathSerialPort)) {
printf("Could not disconnect MQTT\n");
}
printf("Powering off\n");
- if (0 != powerOff(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, PIN_NUM_STATUS,
- PIN_NUM_PWR_KEY)) {
+ if (0 != powerOff(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
+ dataCfg.pinStatus, dataCfg.pinPwrKey)) {
printf("Error powering the module off\n");
+ freeCfgData(&dataCfg);
return 1;
}
printf("Power off complete\n");
+ freeCfgData(&dataCfg);
return 0;
}
@@ -107,26 +122,26 @@ int main(int argc, char **argv) {
printf("Obtaining network info\n");
initNetworkData(nD);
- if (gatherData(nD, PATH_TO_SERIAL_PORT) != 0) {
+ if (gatherData(nD, dataCfg.pathSerialPort) != 0) {
printf("Failed to gather network info\n");
goto netinfopubFail;
}
printf("Connecting to the MQTT broker\n");
- if (0 != mqttConn(PATH_TO_SERIAL_PORT, "io.adafruit.com", 8883, "kboz",
+ if (0 != mqttConn(dataCfg.pathSerialPort, "io.adafruit.com", 8883, "kboz",
"aio_lvhz87FJ0rIlidH8g30V5mQ7FK7s")) {
printf("Error connecting MQTT\n");
goto netinfopubFail;
}
printf("Publishing network information\n");
- if (0 != mqttPubNetData(PATH_TO_SERIAL_PORT, *nD)) {
+ if (0 != mqttPubNetData(dataCfg.pathSerialPort, *nD)) {
printf("Error publishing to MQTT server\n");
goto netinfopubFail;
}
printf("Disconnecting from the MQTT broker\n");
- if (0 != mqttDisc(PATH_TO_SERIAL_PORT)) {
+ if (0 != mqttDisc(dataCfg.pathSerialPort)) {
printf("Could not disconnect MQTT\n");
goto netinfopubFail;
}
@@ -134,16 +149,19 @@ int main(int argc, char **argv) {
printf("Network info publish complete\n");
freeNetworkData(nD);
free(nD);
+ freeCfgData(&dataCfg);
return 0;
netinfopubFail:
freeNetworkData(nD);
free(nD);
+ freeCfgData(&dataCfg);
return 1;
}
else {
printf("Unknown arguments\n");
+ freeCfgData(&dataCfg);
return 1;
}
}
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..ae7ae02
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,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;
+}