summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorkutayboz <kutayboz@gmail.com>2023-06-09 02:02:07 +0300
committerkutayboz <kutayboz@gmail.com>2023-06-09 02:02:07 +0300
commit386074fd3ffd04ae21cb914dc84969c9fd981c5e (patch)
tree4e48a10607a75c09fb253b275b3048908a686fa9 /src/main.c
parent05a602d2abd9716af6707d5b31c0e8beee707634 (diff)
downloadcelnetmon-386074fd3ffd04ae21cb914dc84969c9fd981c5e.tar.gz
Overhaul. Created bg96 lib.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c131
1 files changed, 33 insertions, 98 deletions
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 <time.h>
#include <unistd.h>
-#include <gpio.h>
-#include <serial.h>
+#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;
}