commit 1a31190c36c5b7acfaf811a2c4477a03fd4dbd7d
parent ded762d0b490ffaf396eea52400f4158985bedda
Author: Friedel Schön <[email protected]>
Date: Wed, 8 Jun 2022 19:41:30 +0200
command-wrapper for SARA
Diffstat:
4 files changed, 132 insertions(+), 64 deletions(-)
diff --git a/.vscode/arduino.json b/.vscode/arduino.json
@@ -1,6 +1,5 @@
{
+ "sketch": "5g-client/5g-board.ino",
"board": "SODAQ:samd:sodaq_sara",
- "programmer": "SODAQ:atmel_ice",
- "port": "/dev/tty.usbmodem142301",
- "sketch": "5g-board/5g-board.ino"
+ "port": "/dev/tty.usbmodem14201"
}
\ No newline at end of file
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
@@ -3,7 +3,7 @@
"configurations": [
{
"name": "Arduino",
- "compilerPath": "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++",
+ "compilerPath": "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++",
"compilerArgs": [
"-mcpu=cortex-m0plus",
"-mthumb",
@@ -19,19 +19,19 @@
],
"intelliSenseMode": "gcc-x64",
"includePath": [
- "/home/friedel/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/",
- "/home/friedel/.arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/",
- "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino",
- "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/variants/sodaq_sara",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/arm-none-eabi",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/backward",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include-fixed",
- "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include"
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/",
+ "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino",
+ "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/variants/sodaq_sara",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/arm-none-eabi",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/backward",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include-fixed",
+ "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include"
],
"forcedInclude": [
- "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino/Arduino.h"
+ "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino/Arduino.h"
],
"cStandard": "c11",
"cppStandard": "c++11",
diff --git a/.vscode/settings.json b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+ "C_Cpp.intelliSenseEngine": "Default",
+}
+\ No newline at end of file
diff --git a/5g-client/5g-board.ino b/5g-client/5g-board.ino
@@ -1,67 +1,121 @@
+// -*- utilities -*-
#define usbSerial SerialUSB
#define modemSerial Serial1
#define powerPin SARA_ENABLE
#define enablePin SARA_TX_ENABLE
#define voltagePin SARA_R4XX_TOGGLE
-#define baud 115200
-#define COMMAND_OK 0
-#define COMMAND_ERROR 1
-#define LINE_BUFFER 256
+// -*- settings -*-
+#define baud 115200 // baut-rate of modem-/usb-serial
+#define lineBuffer 256 // buffer-size (bytes) to use to store lines
+#define commandTimeout 5.0 // seconds to cancel a command
+#define commandDelay 0.1 // delay after every command
+#define commandDebug true // send debug information about command requests
+#define eventHandling false // handle '+'-events different
+#define eventDebug true // print '+'-events
+#define lineDebug true // print each line to debug
+
+
+// -*- enums -*-
+enum command_status {
+ COMMAND_OK,
+ COMMAND_ERROR,
+ COMMAND_TIMEOUT
+};
#define SIM_PIN "0000"
//#define APN_DOMAIN "live.vodafone.com"
#define APN_DOMAIN "nb.inetd.gdsp"
-
-size_t modemReadline(char* dest) {
- char buf;
- size_t index = 0;
-
- for (;;) {
- while (!modemSerial.available())
- ;
- buf = modemSerial.read();
- if (buf == '\r')
- continue;
- if (buf == '\n')
- break;
- dest[index++] = buf;
- }
- dest[index] = '\0';
- return index;
-}
-
-// sendCommand returns COMMAND_OK or COMMAND_ERROR
-int sendCommand(const char* request, char* response) {
- char line[LINE_BUFFER];
+// -*- helper functions -*-
+
+/** command_status sendCommand(const char* requst, char* response)
+ * sends `request` to 5G and stores its response in `response` (may be NULL'ed)
+ *
+ * if the command succeed, COMMAND_OK will be returned
+ * if the command failed, COMMAND_ERROR will be returned
+ * if the command timed out, COMMAND_TIMEOUT will be returned (took longer than `commandTimeout`)
+ */
+command_status sendCommand(const char* request, char* response, bool silent = false) {
+ char line[lineBuffer];
size_t lineLen;
+ char buf;
if (response)
response[0] = '\0';
+ unsigned long start = millis(),
+ now;
+
modemSerial.write(request);
modemSerial.write("\r\n");
modemSerial.flush();
for (;;) {
- lineLen = modemReadline(line);
+ lineLen = 0;
+ for (;;) {
+ while (!modemSerial.available()) {
+ now = millis();
+ if (now - start > commandTimeout * 1000) {
+ if (commandDebug && !silent) {
+ usbSerial.print("[WARN] command '");
+ usbSerial.print(request);
+ usbSerial.println("' timed out");
+ }
+ return COMMAND_TIMEOUT;
+ }
+ }
+ buf = modemSerial.read();
+ if (buf == '\r')
+ continue;
+ if (buf == '\n')
+ break;
+ line[lineLen++] = buf;
+ }
+ line[lineLen] = '\0';
+
if (String(line) == "OK") {
+ if (commandDebug && !silent) {
+ usbSerial.print("[DBUG] command '");
+ usbSerial.print(request);
+ usbSerial.println("' succeed");
+ }
return COMMAND_OK;
- } else if (String(line) == "ERROR") {
+ } else if (strstr(line, "ERROR")) {
+ if (commandDebug && !silent) {
+ usbSerial.print("[WARN] command '");
+ usbSerial.print(request);
+ usbSerial.println("' failed");
+ }
return COMMAND_ERROR;
- } else if (response && line[0] != '\0') {
- if (response[0] != '\0') // check if not empty string
- strcat(response, "\n");
- strcat(response, line);
+ } else if (eventHandling && line[0] == '+') { // additional info
+ if (eventDebug) {
+ usbSerial.print("[EVNT] event '");
+ usbSerial.print(line);
+ usbSerial.println(" caused'");
+ }
+ } else if (line[0] != '\0') {
+ if (lineDebug) {
+ usbSerial.print("[LINE] ");
+ usbSerial.print(request);
+ usbSerial.print(" -> '");
+ usbSerial.print(line);
+ usbSerial.println("'");
+ }
+
+ if (response) {
+ if (response[0] != '\0') // check if not empty string
+ strcat(response, "\n");
+ strcat(response, line);
+ }
}
}
- delay(100); // wait 1/10
+ delay(commandDelay * 1000); // wait 0.1 sec
}
-int sendCommand(const char* request) {
- return sendCommand(request, NULL);
+int sendCommand(const char* request, bool silent = false) {
+ return sendCommand(request, NULL, silent);
}
@@ -83,11 +137,11 @@ void setup() {
;
- usbSerial.print("[INFO] Waiting for module to response");
+ usbSerial.print("[INFO] waiting for module to start up");
for (;;) {
usbSerial.print('.');
modemSerial.write("AT\r\n");
- delay(100);
+ delay(1000);
if (modemSerial.available())
break;
}
@@ -97,15 +151,22 @@ void setup() {
sendCommand("ATE0"); // disable command-echo
- usbSerial.println("[INFO] Disabled command-echo");
+ char info[256];
+ sendCommand("ATI", info);
- if (sendCommand("AT+CPIN=\"" SIM_PIN "\"") == COMMAND_ERROR) {
- usbSerial.println("[ERROR] SIM can't be unlocked, wrong PIN");
- return;
- }
+ usbSerial.println("[INFO] module information:");
+ usbSerial.println(info);
+
+ // if (sendCommand("AT+CPIN=\"" SIM_PIN "\"") == COMMAND_ERROR) {
+ // usbSerial.println("[EROR] sim can't be unlocked, wrong PIN");
+ // return;
+ // }
+ usbSerial.println("[INFO] SIM unlocked");
sendCommand("AT+CPSMS=0"); // Disable Power Saving Mode
sendCommand("AT+CEDRXS=0"); // Disable eDRX
+ usbSerial.println("[INFO] disable power safe");
+
sendCommand("AT+CFUN=15"); // Reset the module
sendCommand("AT+UMNOPROF=1"); // Set MNO profile (1=automatic,100=standard europe)
@@ -114,15 +175,19 @@ void setup() {
sendCommand("AT+CGDCONT=1,\"IP\",\"" APN_DOMAIN "\""); // Set the APN
sendCommand("AT+COPS=0,2"); // Autoselect the operator
- usbSerial.println("Waiting...");
+ // usbSerial.println("[INFO] waiting for connection...");
+
+ /* char response[100];
+ while (sendCommand("AT+CSQ", response, true) == COMMAND_OK && strcmp(response, "+CSQ: 99,99"))
+ ;
+ // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal)
+ while (sendCommand("AT+CGATT?", response, true) == COMMAND_OK && !strcmp(response, "+CGATT: 1"))
+ ;
+ // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal)
- // char response[100];
- // while (sendCommand("AT+CSQ", response) == COMMAND_OK && strcmp(response, "+CSQ: 99,99"))
- // ; // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal)
- // while (sendCommand("AT+CGATT?", response) == COMMAND_OK && !strcmp(response, "+CGATT: 1"))
- // ; // Check Siganl strenght, repeat till you have a valid CSQ (99,99 means no signal)
+ usbSerial.println("Connected!");*/
- usbSerial.println("Connected!");
+ usbSerial.println("[INFO] initiation completed, starting passthrough:");
}
void loop() {