diff options
| author | kutayboz <kutayboz@gmail.com> | 2023-06-13 02:13:08 +0300 |
|---|---|---|
| committer | kutayboz <kutayboz@gmail.com> | 2023-06-13 02:13:08 +0300 |
| commit | a29985e3dc53f832511ef7eadb0bcb64afd8520d (patch) | |
| tree | 203e0fbe523e01afffce575914f2deeaa0e884de /src | |
| parent | fbd07ec5acaed65b892e0a1c144eb779d6d95ddc (diff) | |
| download | celnetmon-a29985e3dc53f832511ef7eadb0bcb64afd8520d.tar.gz | |
6GTN functionality added
Diffstat (limited to 'src')
| -rw-r--r-- | src/bg96.c | 138 | ||||
| -rw-r--r-- | src/main.c | 58 |
2 files changed, 171 insertions, 25 deletions
@@ -205,6 +205,144 @@ int cfgPublicNetwork(char *pathToPort) { return 0; } +int cfg6GTN(char *pathToPort) { + unsigned long idx; + int serialPort; + char *response = malloc(sizeof(char)), + *cmdList[] = {"AT+QCFG=\"nwscanmode\",3,1", + "AT+QCFG=\"iotopmode\",1,1", + "AT+QCFG=\"band\",0,8000000,8000000,1", + "AT+QCFG=\"servicedomain\",1,1", + "AT+COPS=0,0", + "AT+CEREG=2"}; + + serialPort = openSerialPort(pathToPort); + if (0 > serialPort) { + printf("Error opening serial port %s\n", pathToPort); + free(response); + return 1; + } + + for (idx = 0; idx < sizeof(cmdList) / sizeof(*cmdList); idx++) { + if (0 != querySerialPort(&response, serialPort, cmdList[idx], 20, NULL)) { + printf("Error querySerialPort()\n"); + close(serialPort); + free(response); + return 1; + } + if (NULL == strstr(response, "\r\nOK\r\n")) { + printf("Module responded not OK. Response:\n%s\n", response); + close(serialPort); + free(response); + return 1; + } + } + + if (0 != querySerialPort(&response, serialPort, "AT+CPIN?", 2, NULL)) { + printf("Error querySerialPort()\n"); + close(serialPort); + free(response); + return 1; + } else if (NULL == strstr(response, "+CPIN: READY")) { + printf("(U)SIM not ready\nResponse:\n%s\n", response); + close(serialPort); + free(response); + return 1; + } + + if (0 != querySerialPort(&response, serialPort, + "AT+QICSGP=1,1,\"5gtnouluiot\",\"\",\"\",0", 5, + NULL)) { + printf("Error querySerialPort()\n"); + close(serialPort); + free(response); + return 1; + } + + 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; + + serialPort = openSerialPort(pathToPort); + if (0 > serialPort) { + printf("Error opening serial port %s\n", pathToPort); + free(response); + return 1; + } + + if (0 != querySerialPort(&response, serialPort, "AT+COPS=1,2,\"24427\",9", + 180, NULL)) { + printf("Error querying AT+COPS=1\n"); + close(serialPort); + free(response); + return 1; + } + + if (0 != querySerialPort(&response, serialPort, "AT+CGATT=1", 140, NULL)) { + printf("Error querying AT+CGATT=1\n"); + close(serialPort); + free(response); + return 1; + } + + startTime = time(NULL); + do { + if (0 != querySerialPort(&response, serialPort, "AT+COPS?", 180, NULL)) { + printf("Error querySerialPort()\n"); + close(serialPort); + free(response); + return 1; + } + strPtr = strstr(response, "+COPS: "); + if (waitTimeSeconds - (time(NULL) - startTime) < refreshPeriodSeconds) { + printf("Connection takes longer than %d seconds to initiate\n", + waitTimeSeconds); + close(serialPort); + free(response); + return 1; + } + sleep(refreshPeriodSeconds); + } while (!strchr(strPtr, ',')); + + responseCpy = malloc(sizeof(char)); + startTime = time(NULL); + while (1) { + if (0 != querySerialPort(&response, serialPort, "AT+CEREG?", 2, NULL)) { + printf("Error querySerialPort()\n"); + close(serialPort); + free(responseCpy); + free(response); + return 1; + } + responseCpy = realloc(responseCpy, sizeof(char) * (strlen(response) + 1)); + strncpy(responseCpy, response, strlen(response) + 1); + strPtr = strtok(response, ","); + strPtr = strtok(NULL, ","); + if (NULL != strpbrk(strPtr, "15") || time(NULL) - startTime > 60) + break; + sleep(1); + } + free(responseCpy); + + close(serialPort); + free(response); + return 0; +} + int waitForNetwork(char *pathToPort, int waitTimeSeconds) { int serialPort, refreshPeriodSeconds = 1; char *response = malloc(sizeof(char)), *responseCpy, *strPtr; @@ -12,29 +12,13 @@ #define PIN_NUM_STATUS 27 int main(int argc, char **argv) { - /* char *cmdList5GTNCfg[] = {"AT+QCFG=\"servicedomain\",1", - "AT+QCFG=\"iotopmode\",1", - "AT+QCFG=\"nwscanmode\",3", - "AT+QCFG=\"band\",0,8000000,8000000,1", - "AT+QICSGP=1,1,\"5gtnouluiot\",\"\",\"\",0", - "AT+CGDCONT=1,\"IP\",\"5gtnouluiot\""}, - *cmdList5GTNConnectAndPing[] = { - "AT+COPS=1,2,\"24427\",9", - "AT+CGATT=1", - "AT+QHTTPCFG=\"contextid\",1", - "AT+QHTTPCFG=\"responseheader\",1", - "AT+QICSGP=1,1,\"5gtnouluiot\",\"\",\"\",0", - "AT+QIACT=1", - "AT+QPING=1,\"https://www.google.fi\"", - "AT+QPING=1,\"www.google.fi\"", - "AT+QPING=1,\"8.8.8.8\""}; */ if (argc == 1) { printf("No arguments provided\n"); return 1; } - else if (0 == strcmp(argv[1], "--init")) { + else if (0 == strcmp(argv[1], "--initpublic")) { printf("Powering on\n"); if (0 != powerOn(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, PIN_NUM_STATUS, @@ -65,6 +49,37 @@ int main(int argc, char **argv) { 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)) { + printf("Error powering the module on\n"); + return 1; + } + + printf("Initiating GNSS\n"); + if (0 != initGNSS(PATH_TO_SERIAL_PORT)) { + printf("Error initiating GNSS\n"); + return 1; + } + + printf("Configuring the network settings for 6GTN\n"); + if (0 != cfg6GTN(PATH_TO_SERIAL_PORT)) { + printf("Error setting 6GTN network configuration\n"); + return 1; + } + + printf("Waiting for establishing network connection\n"); + if (0 != connect6GTN(PATH_TO_SERIAL_PORT, 120)) { + printf("Error while connecting to 6GTN\n"); + return 1; + } + + printf("Initialization complete\n"); + return 0; + } + else if (0 == strcmp(argv[1], "--stop")) { printf("Stopping GNSS\n"); @@ -104,13 +119,6 @@ int main(int argc, char **argv) { goto netinfopubFail; } - /* if (0 != mqttConn(PATH_TO_SERIAL_PORT, - "29038f72a9d84f18940e0d44daffa687.s2.eu.hivemq.cloud", - 8883, "rpi5gtn", "Raspi132.")) { - printf("Error connecting MQTT\n"); - goto netinfopubFail; - } */ - printf("Publishing network information\n"); if (0 != mqttPubNetData(PATH_TO_SERIAL_PORT, *nD)) { printf("Error publishing to MQTT server\n"); @@ -123,7 +131,7 @@ int main(int argc, char **argv) { goto netinfopubFail; } - printf("Network info published\n"); + printf("Network info publish complete\n"); freeNetworkData(nD); free(nD); return 0; |
