From 5d48f1280612b2f525c9a2d60f96434f1a217156 Mon Sep 17 00:00:00 2001 From: kutayboz Date: Thu, 3 Aug 2023 17:44:37 +0300 Subject: Finished cfg file implementation. Preliminary readme. v1.0.0 --- src/bg96.c | 260 +++++++++++++++++++++++++++++++++++++----------------------- src/main.c | 73 +++++++---------- src/utils.c | 135 +++++++++++++++++-------------- 3 files changed, 263 insertions(+), 205 deletions(-) (limited to 'src') diff --git a/src/bg96.c b/src/bg96.c index 8b8fc84..00417c4 100644 --- a/src/bg96.c +++ b/src/bg96.c @@ -10,47 +10,6 @@ #include #include -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)); @@ -93,9 +52,55 @@ int freeNetworkData(networkData *nD) { return 0; } -int powerOn(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { - int moduleStatus, serialPort; - char *response; +int modemPowerToggle(listCfgData cfgData, int pinNum) { + struct gpiod_chip *chip; + struct gpiod_line *line; + char *pathToChip = cfgData.strDataList[PATH_TO_GPIO_CHIP_POS].value; + + 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 powerOn(listCfgData cfgData) { + int moduleStatus, serialPort, + statusPin = cfgData.intDataList[PIN_STATUS_POS].value, + pwrKeyPin = cfgData.intDataList[PIN_PWR_KEY_POS].value; + char *response, + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *pathToChip = cfgData.strDataList[PATH_TO_GPIO_CHIP_POS].value; time_t pwrKeyTime; moduleStatus = checkPinVal(pathToChip, statusPin); @@ -105,7 +110,7 @@ int powerOn(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { } else if (moduleStatus == 1) { printf("Module is already on\n"); return 1; - } else if (0 != modemPowerToggle(pathToChip, pwrKeyPin)) { + } else if (0 != modemPowerToggle(cfgData, pwrKeyPin)) { printf("Error toggling modem power\n"); return 1; } @@ -142,9 +147,13 @@ int powerOn(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { return 0; } -int powerOff(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { - int serialPort, moduleStatus; - char *response; +int powerOff(listCfgData cfgData) { + int serialPort, moduleStatus, + statusPin = cfgData.intDataList[PIN_STATUS_POS].value, + pwrKeyPin = cfgData.intDataList[PIN_PWR_KEY_POS].value; + char *response, + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *pathToChip = cfgData.strDataList[PATH_TO_GPIO_CHIP_POS].value; time_t timePowdCmd, timePwrKey; moduleStatus = checkPinVal(pathToChip, statusPin); @@ -178,7 +187,7 @@ int powerOff(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { } else if (time(NULL) - timePwrKey > 12) { printf("Error: Modem status pin did not switch off after 12s\nUsing " "power key\n"); - if (0 != modemPowerToggle(pathToChip, pwrKeyPin)) { + if (0 != modemPowerToggle(cfgData, pwrKeyPin)) { printf("Error toggling modem power\n"); return 1; } @@ -189,16 +198,21 @@ int powerOff(char *pathToPort, char *pathToChip, int statusPin, int pwrKeyPin) { return 0; } -int configureNetwork(char *pathToPort) { +int configureNetwork(listCfgData cfgData) { unsigned long idx; - int serialPort; - char *response = malloc(sizeof(char)), + int serialPort, netAuthInt; + char *response = malloc(sizeof(char)), *cmdBuf, *cmdList[] = {"AT+QCFG=\"nwscanmode\",3,1", "AT+QCFG=\"iotopmode\",1,1", "AT+QCFG=\"band\",F,400A0E189F,A0E189F,1", "AT+QCFG=\"servicedomain\",1,1", "AT+COPS=0,0", - "AT+CEREG=2"}; + "AT+CEREG=2"}, + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *netAPN = cfgData.strDataList[NETWORK_APN_POS].value, + *netUsername = cfgData.strDataList[NETWORK_USERNAME_POS].value, + *netPasword = cfgData.strDataList[NETWORK_PASSWORD_POS].value, + *netAuthStr = cfgData.strDataList[NETWORK_AUTH_POS].value; serialPort = openSerialPort(pathToPort); if (0 > serialPort) { @@ -234,8 +248,29 @@ int configureNetwork(char *pathToPort) { return 1; } - if (0 != querySerialPort(&response, serialPort, - "AT+QICSGP=1,1,\"internet\",\"\",\"\",1", 5, NULL)) { + if (strcmp(netAuthStr, "NONE") == 0 || strcmp(netAuthStr, "") == 0 || + (netAuthStr == NULL)) { + netAuthInt = 0; + } else if (strcmp(netAuthStr, "PAP") == 0) { + netAuthInt = 1; + } else if (strcmp(netAuthStr, "CHAP") == 0) { + netAuthInt = 2; + } else if (strcmp(netAuthStr, "PAP/CHAP") == 0) { + netAuthInt = 3; + } else { + printf("Network authenication \"%s\" is unknown.\n", netAuthStr); + close(serialPort); + free(response); + return 1; + } + + cmdBuf = + malloc(sizeof(char) * (strlen("AT+QICSGP=1,1,\"\",\"\",\"\",") + + strlen(netUsername) + strlen(netPasword) + 3)); + sprintf(cmdBuf, "AT+QICSGP=1,1,\"%s\",\"%s\",\"%s\",%d", netAPN, netUsername, + netPasword, netAuthInt); + + if (0 != querySerialPort(&response, serialPort, cmdBuf, 5, NULL)) { printf("Error querySerialPort()\n"); close(serialPort); free(response); @@ -247,7 +282,7 @@ int configureNetwork(char *pathToPort) { return 0; } -int cfg6GTN(char *pathToPort) { +int cfg6GTN(listCfgData cfgData) { unsigned long idx; int serialPort; char *response = malloc(sizeof(char)), @@ -255,8 +290,9 @@ int cfg6GTN(char *pathToPort) { "AT+QCFG=\"iotopmode\",1,1", "AT+QCFG=\"band\",0,8000000,8000000,1", "AT+QCFG=\"servicedomain\",1,1", - "AT+COPS=0,0", - "AT+CEREG=2"}; + "AT+COPS=0,0,\"\",9", + "AT+CEREG=2"}, + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; serialPort = openSerialPort(pathToPort); if (0 > serialPort) { @@ -315,10 +351,14 @@ int cfg6GTN(char *pathToPort) { return 0; } -int defineNetworkDetails(char *pathToPort/* , int waitTimeSeconds */) { - int serialPort/* , refreshPeriodSeconds = 1 */; - char *response = malloc(sizeof(char))/* , *responseCpy, *strPtr */; - /* time_t startTime; */ +int defineNetworkDetails(listCfgData cfgData) { + int serialPort; + char *response = malloc(sizeof(char)), + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *cmdBuf = malloc(sizeof(char) * (strlen("AT+COPS=1,2,\"\",9") + 6)); + + sprintf(cmdBuf, "AT+COPS=1,2,\"%d\",9", + cfgData.intDataList[NETWORK_OPERATOR_POS].value); serialPort = openSerialPort(pathToPort); if (0 > serialPort) { @@ -327,8 +367,7 @@ int defineNetworkDetails(char *pathToPort/* , int waitTimeSeconds */) { return 1; } - if (0 != querySerialPort(&response, serialPort, "AT+COPS=1,2,\"24427\",9", - 180, NULL)) { + if (0 != querySerialPort(&response, serialPort, cmdBuf, 180, NULL)) { printf("Error querying AT+COPS=1\n"); close(serialPort); free(response); @@ -386,9 +425,11 @@ int defineNetworkDetails(char *pathToPort/* , int waitTimeSeconds */) { return 0; } -int waitForNetwork(char *pathToPort, int waitTimeSeconds) { +int waitForNetwork(listCfgData cfgData, int waitTimeSeconds) { int serialPort, refreshPeriodSeconds = 1; - char *response = malloc(sizeof(char)), *responseCpy, *strPtr; + char *response = malloc(sizeof(char)), + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *responseCpy, *strPtr; time_t startTime; serialPort = openSerialPort(pathToPort); @@ -442,9 +483,10 @@ int waitForNetwork(char *pathToPort, int waitTimeSeconds) { return 0; } -int mqttDisc(char *pathToPort) { +int mqttDisc(listCfgData cfgData) { int serialPort; - char *response = malloc(sizeof(char)); + char *response = malloc(sizeof(char)), + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; serialPort = openSerialPort(pathToPort); if (0 > serialPort) { @@ -490,11 +532,15 @@ int mqttDisc(char *pathToPort) { return 0; } -int mqttConn(char *pathToPort, char *hostURL, int hostPort, char *username, - char *password) { - int serialPort, cmdStrLen; +int mqttConn(listCfgData cfgData) { + int serialPort, cmdStrLen, + hostPort = cfgData.intDataList[MQTT_PORT_POS].value; unsigned long idx; - char *response = malloc(sizeof(char)), *command, clientID[11]; + char *response = malloc(sizeof(char)), *command, clientID[11], + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, + *hostURL = cfgData.strDataList[MQTT_URL_POS].value, + *username = cfgData.strDataList[MQTT_USERNAME_POS].value, + *password = cfgData.strDataList[MQTT_PASSWORD_POS].value; srand(time(NULL)); for (idx = 0; idx < sizeof(clientID) - 1; idx++) { @@ -589,7 +635,7 @@ mqttConnError3: return 1; } -int mqttPubNetData(char *pathToPort, networkData nD) { +int mqttPubNetData(listCfgData cfgData, networkData nD) { int serialPort, cmdStrLen, msgID, msgSize; char *response = malloc(sizeof(char)), *command = malloc(sizeof(char)), *msg = malloc(sizeof(char)), @@ -607,7 +653,8 @@ int mqttPubNetData(char *pathToPort, networkData nD) { "\t\"lat\": %s,\n" "\t\"lon\": %s,\n" "\t\"ele\": %s\n" - "}"; + "}", + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; msgSize = sizeof(char) * (strlen(textDataTemplate) + strlen(nD.operatorName) + strlen(nD.networkName) + strlen(nD.networkRegStat) + @@ -628,10 +675,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/textinfo") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/textinfo") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/textinfo", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/textinfo", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -646,10 +694,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { strncpy(msg, nD.rssi, msgSize / sizeof(char)); msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/rssi") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/rssi") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/rssi", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/rssi", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -664,10 +713,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { strncpy(msg, nD.rsrp, msgSize / sizeof(char)); msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/rsrp") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/rsrp") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/rsrp", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/rsrp", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -682,10 +732,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { strncpy(msg, nD.sinr, msgSize / sizeof(char)); msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/sinr") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/sinr") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/sinr", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/sinr", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -700,10 +751,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { strncpy(msg, nD.rsrq, msgSize / sizeof(char)); msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/rsrq") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/rsrq") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/rsrq", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/rsrq", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -718,10 +770,11 @@ int mqttPubNetData(char *pathToPort, networkData nD) { strncpy(msg, nD.ber, msgSize / sizeof(char)); msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/ber") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/ber") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/ber", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/ber", msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -741,10 +794,12 @@ int mqttPubNetData(char *pathToPort, networkData nD) { msgID = rand() % 65536; cmdStrLen = strlen("AT+QMTPUB=0") + (int)ceil(log10(msgID)) + 1 + 1 + - strlen("kboz/feeds/gpsdata/json") + (int)ceil(log10(msgSize)) + 8; + strlen(cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value) + + strlen("/gpsdata/json") + (int)ceil(log10(msgSize)) + 8; command = realloc(command, sizeof(char) * (cmdStrLen + 1)); - snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s\",%d", msgID, - "kboz/feeds/gpsdata/json", msgSize); + snprintf(command, cmdStrLen + 1, "AT+QMTPUB=0,%d,1,0,\"%s%s\",%d", msgID, + cfgData.strDataList[MQTT_ROOT_TOPIC_POS].value, "/gpsdata/json", + msgSize); if (0 != querySerialPort(&response, serialPort, command, 15, (char *)-1)) { printf("Error querying publish command: %s\n", command); goto mqttPubError1; @@ -769,9 +824,10 @@ mqttPubError2: return 1; } -int gatherData(networkData *output, char *pathToPort) { +int gatherData(networkData *output, listCfgData cfgData) { int serialPort, strLen, tmpNum; - char *response = malloc(sizeof(char)), *varPtr, *qcsqSystemMode; + char *response = malloc(sizeof(char)), *varPtr, *qcsqSystemMode, + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; serialPort = openSerialPort(pathToPort); if (0 > serialPort) { @@ -1004,8 +1060,9 @@ gatherDataError2: return 1; } -int initGNSS(char *pathToPort) { - char *response = malloc(sizeof(char)); +int initGNSS(listCfgData cfgData) { + char *response = malloc(sizeof(char)), + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; int serialPort; serialPort = openSerialPort(pathToPort); @@ -1043,8 +1100,9 @@ initGNSSError2: return 1; } -int stopGNSS(char *pathToPort) { - char *response = malloc(sizeof(char)); +int stopGNSS(listCfgData cfgData) { + char *response = malloc(sizeof(char)), + *pathToPort = cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value; int serialPort; serialPort = openSerialPort(pathToPort); diff --git a/src/main.c b/src/main.c index aecc820..0b65ff8 100644 --- a/src/main.c +++ b/src/main.c @@ -16,7 +16,7 @@ int main(int argc, char **argv) { return 1; } - if (readCfg(cfgData, argv[0]) != 0) { + if (readCfg(&cfgData, argv[0]) != 0) { printf("Error reading the config file.\n"); return 1; } @@ -57,111 +57,98 @@ int main(int argc, char **argv) { return 0; } */ - if (0 == strcmp(argv[1], "--initConnection")) { + if (0 == strcmp(argv[1], "--init")) { printf("Powering on\n"); - 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)) { + if (0 != powerOn(cfgData)) { printf("Error powering the module on\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } printf("Initiating GNSS\n"); - if (0 != initGNSS(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) { + if (0 != initGNSS(cfgData)) { printf("Error initiating GNSS\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } printf("Configuring the network settings\n"); - if (0 != - configureNetwork(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) { + if (0 != configureNetwork(cfgData)) { printf("Error setting network configuration\n"); - freeCfgData(cfgData); + 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); + if (cfgData.intDataList[NETWORK_OPERATOR_POS].value != -1) { + printf("Defining operator \"%d\"\n", + cfgData.intDataList[NETWORK_OPERATOR_POS].value); + if (0 != defineNetworkDetails(cfgData)) { + printf("Error defining the specified operator\n"); + freeCfgData(&cfgData); return 1; } } printf("Waiting for establishing network connection\n"); - if (0 != waitForNetwork(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, - 120)) { + if (0 != waitForNetwork(cfgData, 120)) { printf("Error while waiting for network connection\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } printf("Initialization complete\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 0; } else if (0 == strcmp(argv[1], "--stop")) { printf("Stopping GNSS\n"); - if (0 != stopGNSS(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) { + if (0 != stopGNSS(cfgData)) { printf("Error stopping GNSS\n"); } printf("Disconnecting MQTT\n"); - if (0 != mqttDisc(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) { + if (0 != mqttDisc(cfgData)) { printf("Could not disconnect MQTT\n"); } printf("Powering off\n"); - 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)) { + if (0 != powerOff(cfgData)) { printf("Error powering the module off\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } printf("Power off complete\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 0; } - else if (0 == strcmp(argv[1], "--netinfo-pub")) { + else if (0 == strcmp(argv[1], "--publish")) { networkData *nD = malloc(sizeof(networkData)); printf("Obtaining network info\n"); initNetworkData(nD); - if (gatherData(nD, cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value) != - 0) { + if (gatherData(nD, cfgData) != 0) { printf("Failed to gather network info\n"); goto netinfopubFail; } printf("Connecting to the MQTT broker\n"); - if (0 != mqttConn(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, - "io.adafruit.com", 8883, "kboz", - "aio_lvhz87FJ0rIlidH8g30V5mQ7FK7s")) { + if (0 != mqttConn(cfgData)) { printf("Error connecting MQTT\n"); goto netinfopubFail; } printf("Publishing network information\n"); - if (0 != mqttPubNetData(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value, - *nD)) { + if (0 != mqttPubNetData(cfgData, *nD)) { printf("Error publishing to MQTT server\n"); goto netinfopubFail; } printf("Disconnecting from the MQTT broker\n"); - if (0 != mqttDisc(cfgData.strDataList[PATH_TO_SERIAL_PORT_POS].value)) { + if (0 != mqttDisc(cfgData)) { printf("Could not disconnect MQTT\n"); goto netinfopubFail; } @@ -169,19 +156,19 @@ int main(int argc, char **argv) { printf("Network info publish complete\n"); freeNetworkData(nD); free(nD); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 0; netinfopubFail: freeNetworkData(nD); free(nD); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } else { printf("Unknown arguments\n"); - freeCfgData(cfgData); + freeCfgData(&cfgData); return 1; } } diff --git a/src/utils.c b/src/utils.c index b4ddee1..1167001 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,10 +5,15 @@ #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"}; +static char configFilename[] = "celnetmon.cfg"; + +char *listStrDataNames[] = { + "path_to_serial_port", "path_to_gpio_chip", "mqtt_url", + "network_apn", "mqtt_username", "mqtt_password", + "mqtt_root_topic", "network_username", "network_password", + "network_auth"}; +char *listIntDataNames[] = {"pin_pwr_key", "pin_status", "mqtt_port", + "network_operator"}; int checkPinVal(char *pathToChip, int pinNum) { int pinVal = -1; @@ -45,10 +50,9 @@ int checkPinVal(char *pathToChip, int pinNum) { return pinVal; } -int readCfg(listCfgData cfgData, const char *pathExe) { - char strBuffer[BUFSIZ]; +int readCfg(listCfgData *cfgData, const char *pathExe) { + char lineBuffer[BUFSIZ]; char *pathCfg, *pathDirEnd, *tmpPtr = NULL; - const char *filenameCfg = "network_monitor.cfg"; int sizePathDir, idx, idx2; unsigned long numCharRead; FILE *fileCfg; @@ -60,9 +64,9 @@ int readCfg(listCfgData cfgData, const char *pathExe) { printf("Cannot obtain the executable directory.\n"); return 1; } - pathCfg = malloc(sizeof(char) * (sizePathDir + strlen(filenameCfg) + 2)); + pathCfg = malloc(sizeof(char) * (sizePathDir + strlen(configFilename) + 2)); strncpy(pathCfg, pathExe, sizePathDir + 1); - strcat(pathCfg, filenameCfg); + strcat(pathCfg, configFilename); /* Open the config file. */ fileCfg = fopen(pathCfg, "r"); @@ -73,7 +77,7 @@ int readCfg(listCfgData cfgData, const char *pathExe) { } /* Check that it is not empty. */ - if (fgets(strBuffer, BUFSIZ, fileCfg) == NULL) { + if (fgets(lineBuffer, BUFSIZ, fileCfg) == NULL) { printf("Config file is empty.\n"); fclose(fileCfg); free(pathCfg); @@ -81,81 +85,88 @@ int readCfg(listCfgData cfgData, const char *pathExe) { } /* Allocate memory for the config data list. */ - cfgData.strDataList = malloc(STR_LIST_LENGTH * sizeof(strData)); - cfgData.intDataList = malloc(INT_LIST_LENGTH * sizeof(intData)); + cfgData->strDataList = malloc(STR_LIST_LENGTH * sizeof(strData)); + cfgData->intDataList = malloc(INT_LIST_LENGTH * sizeof(intData)); /* Read the file and populate the config string data. */ for (idx = 0; idx < STR_LIST_LENGTH; idx++) { - cfgData.strDataList[idx].name = + cfgData->strDataList[idx].name = malloc(sizeof(char) * (strlen(listStrDataNames[idx]) + 1)); - strcpy(cfgData.strDataList[idx].name, listStrDataNames[idx]); - cfgData.strDataList[idx].value = NULL; + strcpy(cfgData->strDataList[idx].name, listStrDataNames[idx]); + cfgData->strDataList[idx].value = NULL; rewind(fileCfg); do { - fgets(strBuffer, BUFSIZ, fileCfg); - tmpPtr = strtok(strBuffer, " =\n\t\r"); + fgets(lineBuffer, BUFSIZ, fileCfg); + tmpPtr = strtok(lineBuffer, "="); if (tmpPtr == NULL) continue; - if (strcmp(cfgData.strDataList[idx].name, tmpPtr) != 0) + if (tmpPtr[0] == '/' && tmpPtr[1] == '/') continue; - - tmpPtr = strtok(NULL, " =\n\t\r"); - if (tmpPtr == NULL || *tmpPtr != '\"') + if (strstr(tmpPtr, cfgData->strDataList[idx].name) == NULL) continue; - tmpPtr = strtok(tmpPtr, "\""); + do { + tmpPtr = strtok(NULL, "\""); + } while (tmpPtr != NULL && *tmpPtr == ' '); if (tmpPtr == NULL) continue; - cfgData.strDataList[idx].value = + cfgData->strDataList[idx].value = malloc(sizeof(char) * (strlen(tmpPtr) + 1)); - strcpy(cfgData.strDataList[idx].value, tmpPtr); + 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); + if (cfgData->strDataList[idx].value == NULL) { + if (idx < NETWORK_USERNAME_POS) { + printf("Error parsing the config file.\nCould 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); + } + free(cfgData->strDataList); + fclose(fileCfg); + free(pathCfg); + return 1; + } else { + cfgData->strDataList[idx].value = + realloc(cfgData->strDataList[idx].value, sizeof(char)); + strcpy(cfgData->strDataList[idx].value, ""); } - fclose(fileCfg); - free(pathCfg); - return 1; } } /* Populate the config integer data names. */ for (idx = 0; idx < INT_LIST_LENGTH; idx++) { - cfgData.intDataList[idx].name = + cfgData->intDataList[idx].name = malloc(sizeof(char) * (strlen(listIntDataNames[idx]) + 1)); - strcpy(cfgData.intDataList[idx].name, listIntDataNames[idx]); - cfgData.intDataList[idx].value = -1; + strcpy(cfgData->intDataList[idx].name, listIntDataNames[idx]); + cfgData->intDataList[idx].value = -1; rewind(fileCfg); do { - fgets(strBuffer, BUFSIZ, fileCfg); - tmpPtr = strtok(strBuffer, " =\n\t\r"); + fgets(lineBuffer, BUFSIZ, fileCfg); + tmpPtr = strtok(lineBuffer, "="); if (tmpPtr == NULL) continue; - if (strcmp(cfgData.intDataList[idx].name, tmpPtr) != 0) + if (tmpPtr[0] == '/' && tmpPtr[1] == '/') continue; - - tmpPtr = strtok(NULL, " =\n\t\r"); - if (tmpPtr == NULL || *tmpPtr != '\"') + if (strstr(tmpPtr, cfgData->intDataList[idx].name) == NULL) continue; - tmpPtr = strtok(tmpPtr, "\""); + do { + tmpPtr = strtok(NULL, "\""); + } while (tmpPtr != NULL && *tmpPtr == ' '); if (tmpPtr == NULL) continue; - 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); + sscanf(tmpPtr, "%d%ln", &cfgData->intDataList[idx].value, &numCharRead); + if (numCharRead != strlen(tmpPtr)) { + printf("Format error for \"%s\".\n", cfgData->intDataList[idx].name); for (idx2 = 0; idx2 <= idx; idx2++) { - free(cfgData.intDataList[idx2].name); + free(cfgData->intDataList[idx2].name); } fclose(fileCfg); free(pathCfg); @@ -163,13 +174,16 @@ int readCfg(listCfgData cfgData, const char *pathExe) { } 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); + if ((cfgData->intDataList[idx].value == -1) && + (idx < NETWORK_OPERATOR_POS)) { + 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); + free(cfgData->intDataList[idx2].name); } + free(cfgData->strDataList); + free(cfgData->intDataList); fclose(fileCfg); free(pathCfg); return 1; @@ -181,19 +195,18 @@ int readCfg(listCfgData cfgData, const char *pathExe) { return 0; } -/* Do not call this before initializing dataCfg with readCfg(). */ -int freeCfgData(listCfgData cfgData) { +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[idx].name); + free(cfgData->strDataList[idx].value); } - free(cfgData.strDataList); + free(cfgData->strDataList); for (idx = 0; idx < INT_LIST_LENGTH; idx++) { - free(cfgData.intDataList[idx].name); - cfgData.intDataList[idx].value = -1; + free(cfgData->intDataList[idx].name); + cfgData->intDataList[idx].value = -1; } - free(cfgData.intDataList); + free(cfgData->intDataList); return 0; } -- cgit v1.3