diff options
| author | kutayboz <kutayboz@gmail.com> | 2023-08-01 04:59:34 +0300 |
|---|---|---|
| committer | kutayboz <kutayboz@gmail.com> | 2023-08-01 04:59:34 +0300 |
| commit | ac5fcc1e3a873e61dba138e5b632479141433614 (patch) | |
| tree | 7dde6438be4ad95ba924d14b82c63b874221f82c | |
| parent | a29985e3dc53f832511ef7eadb0bcb64afd8520d (diff) | |
| download | celnetmon-ac5fcc1e3a873e61dba138e5b632479141433614.tar.gz | |
Primitive config file implementation
| -rw-r--r-- | CMakeLists.txt | 13 | ||||
| -rw-r--r-- | include/bg96.h | 2 | ||||
| -rw-r--r-- | include/gpio.h | 8 | ||||
| -rw-r--r-- | include/utils.h | 16 | ||||
| -rw-r--r-- | src/bg96.c | 44 | ||||
| -rw-r--r-- | src/gpio.c | 79 | ||||
| -rw-r--r-- | src/main.c | 66 | ||||
| -rw-r--r-- | src/utils.c | 199 |
8 files changed, 310 insertions, 117 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 43f5ea5..22f619f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.0.0) set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD_REQUIRED True) +#LIBGPIOD requires extensions over C90 +#set(CMAKE_C_EXTENSIONS OFF) project(network_monitor VERSION 0.1.0) set(CMAKE_C_FLAGS "-Wall -Wextra -Werror -pedantic-errors") @@ -8,16 +10,17 @@ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O2") add_executable(network_monitor ./src/main.c) -add_library(gpio STATIC ./src/gpio.c ./include/gpio.h) -target_link_libraries(gpio gpiod) - add_library(serial STATIC ./src/serial.c ./include/serial.h) add_library(bg96 STATIC ./src/bg96.c ./include/bg96.h) -target_link_libraries(bg96 serial gpio m) +target_link_libraries(bg96 serial gpiod m utils) target_include_directories(bg96 PUBLIC ./include) +add_library(utils STATIC ./src/utils.c ./include/utils.h) +target_link_libraries(utils gpiod) +target_include_directories(utils PUBLIC ./include) + target_include_directories(network_monitor PUBLIC ./include) -target_link_libraries(network_monitor gpio serial bg96) +target_link_libraries(network_monitor serial bg96 utils) install(TARGETS network_monitor DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/bin/) diff --git a/include/bg96.h b/include/bg96.h index 820bbc8..3e8c350 100644 --- a/include/bg96.h +++ b/include/bg96.h @@ -10,6 +10,8 @@ typedef struct networkDataTag { struct tm netTime; } networkData; +int modemPowerToggle(char *pathToChip, int pinNum); + int initNetworkData(networkData *nD); int freeNetworkData(networkData *nD); diff --git a/include/gpio.h b/include/gpio.h deleted file mode 100644 index 4faf2b1..0000000 --- a/include/gpio.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __INCLUDE_GPIO__ -#define __INCLUDE_GPIO__ - -int modemPowerToggle(char *pathToChip, int pinNum); - -int checkPinVal(char *pathToChip, int pinNum); - -#endif diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..a337ba3 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,16 @@ +#ifndef __INCLUDE_UTILS__ +#define __INCLUDE_UTILS__ + +struct structCfg { + char *pathSerialPort; + char *pathGpioChip; + int pinPwrKey, pinStatus; +}; + +int checkPinVal(char *pathToChip, int pinNum); + +int readCfg(struct structCfg *dataCfg, const char *pathExe); + +int freeCfgData(struct structCfg *dataCfg); + +#endif @@ -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; -} @@ -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; +} |
