summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/bg96.h4
-rw-r--r--include/utils.h44
-rw-r--r--src/bg96.c19
-rw-r--r--src/main.c84
-rw-r--r--src/utils.c214
5 files changed, 209 insertions, 156 deletions
diff --git a/include/bg96.h b/include/bg96.h
index 3e8c350..3a8b762 100644
--- a/include/bg96.h
+++ b/include/bg96.h
@@ -20,11 +20,11 @@ int powerOn(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin);
int powerOff(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin);
-int cfgPublicNetwork(char *pathToPort);
+int configureNetwork(char *pathToPort);
int cfg6GTN(char *pathToPort);
-int connect6GTN(char *pathToPort, int waitTimeSeconds);
+int defineNetworkDetails(char *pathToPort/* , int waitTimeSeconds */);
int waitForNetwork(char *pathToPort, int waitTimeSeconds);
diff --git a/include/utils.h b/include/utils.h
index a337ba3..06c48e6 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -1,16 +1,48 @@
#ifndef __INCLUDE_UTILS__
#define __INCLUDE_UTILS__
-struct structCfg {
- char *pathSerialPort;
- char *pathGpioChip;
- int pinPwrKey, pinStatus;
+enum {
+ /* Mandatory */
+ PATH_TO_SERIAL_PORT_POS,
+ PATH_TO_GPIO_CHIP_POS,
+ /* Optional */
+ NETWORK_USERNAME_POS,
+ NETWORK_PASSWORD_POS,
+ NETWORK_AUTH_POS,
+ STR_LIST_LENGTH
};
+enum { PIN_PWR_KEY_POS, PIN_STATUS_POS, INT_LIST_LENGTH };
+
+typedef struct {
+ char *name;
+ char *value;
+} strData;
+
+typedef struct {
+ char *name;
+ int value;
+} intData;
+
+typedef struct {
+ strData *strDataList;
+ intData *intDataList;
+} listCfgData;
+
int checkPinVal(char *pathToChip, int pinNum);
-int readCfg(struct structCfg *dataCfg, const char *pathExe);
+/* Example config file:
+path_to_serial_port = "/dev/serial0"
+path_to_gpio_chip = "/dev/gpiochip0"
+pin_pwr_key = "17"
+pin_status = "27"
+Below are optional:
+network_username = "something something"
+network_password = "something something"
+network_auth = "something something"
+*/
+int readCfg(listCfgData dataCfg, const char *pathExe);
-int freeCfgData(struct structCfg *dataCfg);
+int freeCfgData(listCfgData cfgData);
#endif
diff --git a/src/bg96.c b/src/bg96.c
index 21cd848..8b8fc84 100644
--- a/src/bg96.c
+++ b/src/bg96.c
@@ -189,7 +189,7 @@ int powerOff(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) {
return 0;
}
-int cfgPublicNetwork(char *pathToPort) {
+int configureNetwork(char *pathToPort) {
unsigned long idx;
int serialPort;
char *response = malloc(sizeof(char)),
@@ -301,23 +301,24 @@ int cfg6GTN(char *pathToPort) {
return 1;
}
- if (0 != querySerialPort(&response, serialPort,
+ /* Test the function without below commented block. */
+ /* if (0 != querySerialPort(&response, serialPort,
"AT+CGDCONT=1,\"IP\",\"5gtnouluiot\"", 2, NULL)) {
printf("Error querySerialPort()\n");
close(serialPort);
free(response);
return 1;
- }
+ } */
close(serialPort);
free(response);
return 0;
}
-int connect6GTN(char *pathToPort, int waitTimeSeconds) {
- int serialPort, refreshPeriodSeconds = 1;
- char *response = malloc(sizeof(char)), *responseCpy, *strPtr;
- time_t startTime;
+int defineNetworkDetails(char *pathToPort/* , int waitTimeSeconds */) {
+ int serialPort/* , refreshPeriodSeconds = 1 */;
+ char *response = malloc(sizeof(char))/* , *responseCpy, *strPtr */;
+ /* time_t startTime; */
serialPort = openSerialPort(pathToPort);
if (0 > serialPort) {
@@ -341,7 +342,7 @@ int connect6GTN(char *pathToPort, int waitTimeSeconds) {
return 1;
}
- startTime = time(NULL);
+ /* startTime = time(NULL);
do {
if (0 != querySerialPort(&response, serialPort, "AT+COPS?", 180, NULL)) {
printf("Error querySerialPort()\n");
@@ -378,7 +379,7 @@ int connect6GTN(char *pathToPort, int waitTimeSeconds) {
break;
sleep(1);
}
- free(responseCpy);
+ free(responseCpy);*/
close(serialPort);
free(response);
diff --git a/src/main.c b/src/main.c
index 13b0b3f..aecc820 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,7 +7,7 @@
#include "bg96.h"
#include "utils.h"
-struct structCfg dataCfg = {NULL, NULL, -1, -1};
+listCfgData cfgData = {NULL, NULL};
int main(int argc, char **argv) {
@@ -16,12 +16,12 @@ int main(int argc, char **argv) {
return 1;
}
- if (readCfg(&dataCfg, argv[0]) != 0) {
+ if (readCfg(cfgData, argv[0]) != 0) {
printf("Error reading the config file.\n");
return 1;
}
- if (0 == strcmp(argv[1], "--initpublic")) {
+ /* if (0 == strcmp(argv[1], "--initpublic")) {
printf("Powering on\n");
if (0 != powerOn(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
@@ -39,7 +39,7 @@ int main(int argc, char **argv) {
}
printf("Configuring the network settings for DNA public network\n");
- if (0 != cfgPublicNetwork(dataCfg.pathSerialPort)) {
+ if (0 != configureNetwork(dataCfg.pathSerialPort)) {
printf("Error setting public network configuration\n");
freeCfgData(&dataCfg);
return 1;
@@ -55,65 +55,82 @@ int main(int argc, char **argv) {
printf("Initialization complete\n");
freeCfgData(&dataCfg);
return 0;
- }
+ } */
- else if (0 == strcmp(argv[1], "--init6GTN")) {
+ if (0 == strcmp(argv[1], "--initConnection")) {
printf("Powering on\n");
- if (0 != powerOn(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
- dataCfg.pinStatus, dataCfg.pinPwrKey)) {
+ if (0 != powerOn(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value,
+ cfgData.strDataList[PATH_TO_GPIO_CHIP_POS].value,
+ cfgData.intDataList[PIN_STATUS_POS].value,
+ cfgData.intDataList[PIN_PWR_KEY_POS].value)) {
printf("Error powering the module on\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 1;
}
printf("Initiating GNSS\n");
- if (0 != initGNSS(dataCfg.pathSerialPort)) {
+ if (0 != initGNSS(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) {
printf("Error initiating GNSS\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 1;
}
- printf("Configuring the network settings for 6GTN\n");
- if (0 != cfg6GTN(dataCfg.pathSerialPort)) {
- printf("Error setting 6GTN network configuration\n");
- freeCfgData(&dataCfg);
+ printf("Configuring the network settings\n");
+ if (0 !=
+ configureNetwork(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) {
+ printf("Error setting network configuration\n");
+ freeCfgData(cfgData);
return 1;
}
+ if (1 /*DETAILS*/) {
+ printf("Defining operator and PDP context\n");
+ if (0 !=
+ defineNetworkDetails(
+ cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value /* , 120 */)) {
+ printf("Error defining operator and PDP context\n");
+ freeCfgData(cfgData);
+ return 1;
+ }
+ }
+
printf("Waiting for establishing network connection\n");
- if (0 != connect6GTN(dataCfg.pathSerialPort, 120)) {
- printf("Error while connecting to 6GTN\n");
- freeCfgData(&dataCfg);
+ if (0 != waitForNetwork(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value,
+ 120)) {
+ printf("Error while waiting for network connection\n");
+ freeCfgData(cfgData);
return 1;
}
printf("Initialization complete\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 0;
}
else if (0 == strcmp(argv[1], "--stop")) {
printf("Stopping GNSS\n");
- if (0 != stopGNSS(dataCfg.pathSerialPort)) {
+ if (0 != stopGNSS(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) {
printf("Error stopping GNSS\n");
}
printf("Disconnecting MQTT\n");
- if (0 != mqttDisc(dataCfg.pathSerialPort)) {
+ if (0 != mqttDisc(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) {
printf("Could not disconnect MQTT\n");
}
printf("Powering off\n");
- if (0 != powerOff(dataCfg.pathSerialPort, dataCfg.pathGpioChip,
- dataCfg.pinStatus, dataCfg.pinPwrKey)) {
+ if (0 != powerOff(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value,
+ cfgData.strDataList[PATH_TO_GPIO_CHIP_POS].value,
+ cfgData.intDataList[PIN_STATUS_POS].value,
+ cfgData.intDataList[PIN_PWR_KEY_POS].value)) {
printf("Error powering the module off\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 1;
}
printf("Power off complete\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 0;
}
@@ -122,26 +139,29 @@ int main(int argc, char **argv) {
printf("Obtaining network info\n");
initNetworkData(nD);
- if (gatherData(nD, dataCfg.pathSerialPort) != 0) {
+ if (gatherData(nD, cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value) !=
+ 0) {
printf("Failed to gather network info\n");
goto netinfopubFail;
}
printf("Connecting to the MQTT broker\n");
- if (0 != mqttConn(dataCfg.pathSerialPort, "io.adafruit.com", 8883, "kboz",
+ if (0 != mqttConn(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value,
+ "io.adafruit.com", 8883, "kboz",
"aio_lvhz87FJ0rIlidH8g30V5mQ7FK7s")) {
printf("Error connecting MQTT\n");
goto netinfopubFail;
}
printf("Publishing network information\n");
- if (0 != mqttPubNetData(dataCfg.pathSerialPort, *nD)) {
+ if (0 != mqttPubNetData(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value,
+ *nD)) {
printf("Error publishing to MQTT server\n");
goto netinfopubFail;
}
printf("Disconnecting from the MQTT broker\n");
- if (0 != mqttDisc(dataCfg.pathSerialPort)) {
+ if (0 != mqttDisc(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) {
printf("Could not disconnect MQTT\n");
goto netinfopubFail;
}
@@ -149,19 +169,19 @@ int main(int argc, char **argv) {
printf("Network info publish complete\n");
freeNetworkData(nD);
free(nD);
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 0;
netinfopubFail:
freeNetworkData(nD);
free(nD);
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 1;
}
else {
printf("Unknown arguments\n");
- freeCfgData(&dataCfg);
+ freeCfgData(cfgData);
return 1;
}
}
diff --git a/src/utils.c b/src/utils.c
index e526115..b4ddee1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -5,6 +5,11 @@
#include "utils.h"
+char *listStrDataNames[] = {"path_to_serial_port", "path_to_gpio_chip",
+ "network_username", "network_password",
+ "network_auth"};
+char *listIntDataNames[] = {"pin_pwr_key", "pin_status"};
+
int checkPinVal(char *pathToChip, int pinNum) {
int pinVal = -1;
struct gpiod_chip *chip;
@@ -40,11 +45,11 @@ int checkPinVal(char *pathToChip, int pinNum) {
return pinVal;
}
-int readCfg(struct structCfg *dataCfg, const char *pathExe) {
+int readCfg(listCfgData cfgData, const char *pathExe) {
char strBuffer[BUFSIZ];
char *pathCfg, *pathDirEnd, *tmpPtr = NULL;
const char *filenameCfg = "network_monitor.cfg";
- int sizePathDir;
+ int sizePathDir, idx, idx2;
unsigned long numCharRead;
FILE *fileCfg;
@@ -63,137 +68,132 @@ int readCfg(struct structCfg *dataCfg, const char *pathExe) {
fileCfg = fopen(pathCfg, "r");
if (fileCfg == NULL) {
printf("Cannot open config file \"%s\".\n", pathCfg);
- goto readCfgFail1;
+ free(pathCfg);
+ return 1;
}
/* Check that it is not empty. */
if (fgets(strBuffer, BUFSIZ, fileCfg) == NULL) {
printf("Config file is empty.\n");
- goto readCfgFail2;
+ fclose(fileCfg);
+ free(pathCfg);
+ return 1;
}
- /* 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;
+ /* Allocate memory for the config data list. */
+ cfgData.strDataList = malloc(STR_LIST_LENGTH * sizeof(strData));
+ cfgData.intDataList = malloc(INT_LIST_LENGTH * sizeof(intData));
- tmpPtr = strtok(NULL, " =\n\t\r\"");
- if (tmpPtr == NULL)
- continue;
+ /* Read the file and populate the config string data. */
+ for (idx = 0; idx < STR_LIST_LENGTH; idx++) {
+ cfgData.strDataList[idx].name =
+ malloc(sizeof(char) * (strlen(listStrDataNames[idx]) + 1));
+ strcpy(cfgData.strDataList[idx].name, listStrDataNames[idx]);
+ cfgData.strDataList[idx].value = NULL;
- 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;
- }
+ rewind(fileCfg);
+ do {
+ fgets(strBuffer, BUFSIZ, fileCfg);
+ tmpPtr = strtok(strBuffer, " =\n\t\r");
+ if (tmpPtr == NULL)
+ continue;
+ if (strcmp(cfgData.strDataList[idx].name, tmpPtr) != 0)
+ continue;
- /* 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 || *tmpPtr != '\"')
+ continue;
- tmpPtr = strtok(NULL, " =\n\t\r\"");
- if (tmpPtr == NULL)
- continue;
+ tmpPtr = strtok(tmpPtr, "\"");
+ 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;
+ cfgData.strDataList[idx].value =
+ malloc(sizeof(char) * (strlen(tmpPtr) + 1));
+ strcpy(cfgData.strDataList[idx].value, tmpPtr);
+ break;
+ } while (feof(fileCfg) == 0);
+ if (cfgData.strDataList[idx].value == NULL && idx < NETWORK_USERNAME_POS) {
+ printf("Could not find the \"%s\" variable.\n",
+ cfgData.strDataList[idx].name);
+ for (idx2 = 0; idx2 <= idx; idx2++) {
+ free(cfgData.strDataList[idx2].name);
+ free(cfgData.strDataList[idx2].value);
+ }
+ fclose(fileCfg);
+ free(pathCfg);
+ return 1;
+ }
}
- /* 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;
+ /* Populate the config integer data names. */
+ for (idx = 0; idx < INT_LIST_LENGTH; idx++) {
+ cfgData.intDataList[idx].name =
+ malloc(sizeof(char) * (strlen(listIntDataNames[idx]) + 1));
+ strcpy(cfgData.intDataList[idx].name, listIntDataNames[idx]);
+ cfgData.intDataList[idx].value = -1;
- 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;
- }
+ rewind(fileCfg);
+ do {
+ fgets(strBuffer, BUFSIZ, fileCfg);
+ tmpPtr = strtok(strBuffer, " =\n\t\r");
+ if (tmpPtr == NULL)
+ continue;
+ if (strcmp(cfgData.intDataList[idx].name, tmpPtr) != 0)
+ continue;
- /* 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 || *tmpPtr != '\"')
+ continue;
- tmpPtr = strtok(NULL, " =\n\t\r\"");
- if (tmpPtr == NULL)
- continue;
+ tmpPtr = strtok(tmpPtr, "\"");
+ 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;
+ sscanf(tmpPtr, "%d%ln", &cfgData.intDataList[idx].value, &numCharRead);
+ if ((numCharRead != strlen(tmpPtr)) ||
+ (strtok(NULL, " =\n\t\r\"") != NULL)) {
+ printf("Format error for \"%s\".\n", cfgData.intDataList[idx].name);
+ for (idx2 = 0; idx2 <= idx; idx2++) {
+ free(cfgData.intDataList[idx2].name);
+ }
+ fclose(fileCfg);
+ free(pathCfg);
+ return 1;
+ }
+ break;
+ } while (feof(fileCfg) == 0);
+ if (cfgData.intDataList[idx].value == -1) {
+ printf("Error parsing the config file.\nCould not find the "
+ "\"%s\" variable.\n",
+ cfgData.intDataList[idx].name);
+ for (idx2 = 0; idx2 <= idx; idx2++) {
+ free(cfgData.intDataList[idx2].name);
+ }
+ fclose(fileCfg);
+ free(pathCfg);
+ return 1;
}
- 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);
+int freeCfgData(listCfgData cfgData) {
+ int idx;
+ for (idx = 0; idx < STR_LIST_LENGTH; idx++) {
+ free(cfgData.strDataList[idx].name);
+ free(cfgData.strDataList[idx].value);
+ }
+ free(cfgData.strDataList);
+
+ for (idx = 0; idx < INT_LIST_LENGTH; idx++) {
+ free(cfgData.intDataList[idx].name);
+ cfgData.intDataList[idx].value = -1;
+ }
+ free(cfgData.intDataList);
return 0;
}