From 386074fd3ffd04ae21cb914dc84969c9fd981c5e Mon Sep 17 00:00:00 2001 From: kutayboz Date: Fri, 9 Jun 2023 02:02:07 +0300 Subject: Overhaul. Created bg96 lib. --- src/main.c | 131 ++++++++++++++++--------------------------------------------- 1 file changed, 33 insertions(+), 98 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 4f68637..08363b6 100644 --- a/src/main.c +++ b/src/main.c @@ -4,12 +4,13 @@ #include #include -#include -#include +#include "bg96.h" -int main() { +#define PATH_TO_SERIAL_PORT "/dev/serial0" +#define PATH_TO_GPIO_CHIP "/dev/gpiochip0" - char /* *commandList[] = {"AT", +int main() { + /* char *commandList[] = {"AT", "ATI", "AT+QCFG=\"nwscanmode\"", "AT+QCFG=\"iotopmode\"", @@ -20,15 +21,15 @@ int main() { "AT+QNWINFO", "AT+QCSQ", "AT+QSPN", - "AT+CCLK?"}, */ - /* *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+CCLK?"}, + *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", @@ -36,111 +37,45 @@ int main() { "AT+QIACT=1", "AT+QPING=1,\"https://www.google.fi\"", "AT+QPING=1,\"www.google.fi\"", - "AT+QPING=1,\"8.8.8.8\""}, - serialFilePath[] = "/dev/serial0", *output; - - int serialPort, moduleStatus; - unsigned long idx; - time_t pwrKeyTime; + "AT+QPING=1,\"8.8.8.8\""}; */ - moduleStatus = checkPinVal(27); - if (0 > moduleStatus) { - printf("Error checkPinVal()\n"); + if (0 != powerOn(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, 27, 17)) { + printf("Error powering the module on\n"); return 1; - } else if (moduleStatus == 0) { - printf("Module is off\nPowering on\n"); - if (0 != modemPowerToggle(17)) { - printf("Error toggling modem power\n"); - return 1; - } - pwrKeyTime = time(NULL); - while (!checkPinVal(27)) { - sleep(1); - if (time(NULL) - pwrKeyTime > 10) { - printf("Modem power on takes longer than 10s\nAborting\n"); - return 1; - } - } - sleep(5); /* Wait for APP RDY*/ } - serialPort = openSerialPort(serialFilePath); - if (serialPort < 0) { - printf("Error openSerialPort()\n"); + if (0 != setupPublicConnection(PATH_TO_SERIAL_PORT)) { + printf("Error setting public network configuration\n"); return 1; } - output = malloc(1); - /* for (idx = 0; idx < sizeof(commandList) / sizeof(*commandList); idx++) { - if (0 != querySerialPort(&output, serialPort, commandList[idx])) { - printf("Error querySerialPort()\n"); - close(serialPort); - free(output); - return 1; - } - printf("%s\n", output); - } */ - - /* for (idx = 0; idx < sizeof(cmdList5GTNCfg) / sizeof(*cmdList5GTNCfg); - idx++) { if (0 != querySerialPort(&output, serialPort, cmdList5GTNCfg[idx])) { - printf("Error querySerialPort()\n"); - close(serialPort); - free(output); - return 1; - } - printf("%s\n", output); - } - - printf("Rebooting\n"); - if (0 != modemPowerToggle(17)) { - printf("Error toggling modem power\n"); - return 1; - } - sleep(5); - if (0 != modemPowerToggle(17)) { - printf("Error toggling modem power\n"); + if (0 != waitForConnection(PATH_TO_SERIAL_PORT, 120)) { + printf("Error while waiting for connection\n"); return 1; } - pwrKeyTime = time(NULL); - while (!checkPinVal(27)) { - sleep(1); - if (time(NULL) - pwrKeyTime > 10) { - printf("Modem power on takes longer than 10s\nAborting\n"); + + if (0 != tcpipInit(PATH_TO_SERIAL_PORT)) { + printf("Error initiating TCP/IP context\nPowering off\n"); + if (0 != powerOff(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, 27, 17)) { + printf("Error powering the module off\n"); return 1; } + return 1; } - sleep(5);*/ - for (idx = 0; idx < sizeof(cmdList5GTNConnectAndPing) / - sizeof(*cmdList5GTNConnectAndPing); - idx++) { - if (0 != - querySerialPort(&output, serialPort, cmdList5GTNConnectAndPing[idx])) { - printf("Error querySerialPort()\n"); - close(serialPort); - free(output); + if (0 != tcpipClose(PATH_TO_SERIAL_PORT)) { + printf("Error closing TCP/IP context\nPowering off\n"); + if (0 != powerOff(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, 27, 17)) { + printf("Error powering the module off\n"); return 1; } - printf("%s\n", output); - } - - if (0 != querySerialPort(&output, serialPort, "AT+QIACT=0")) { - printf("Error querySerialPort()\n"); - close(serialPort); - free(output); return 1; } - printf("%s\n", output); - if (0 != querySerialPort(&output, serialPort, "AT+CGATT=0")) { - printf("Error querySerialPort()\n"); - close(serialPort); - free(output); + if (0 != powerOff(PATH_TO_SERIAL_PORT, PATH_TO_GPIO_CHIP, 27, 17)) { + printf("Error powering the module off\n"); return 1; } - printf("%s\n", output); - close(serialPort); - free(output); return 0; } -- cgit v1.3