Implementation of the CellularInterface for u-blox C030 boards with N2xx modems. Note: requires the N211 module firmware to be at least 06.57 A01.02.

Dependents:   example-ublox-cellular-interface HelloMQTT example-ublox-cellular-interface_r410M example-ublox-mbed-client ... more

Revision:
4:2bf3875a13f1
Parent:
1:8ea78dce6b36
Child:
6:658419981430
--- a/UbloxATCellularInterfaceN2xx.cpp	Fri Jul 07 13:15:59 2017 +0100
+++ b/UbloxATCellularInterfaceN2xx.cpp	Tue Aug 22 16:37:30 2017 +0100
@@ -27,15 +27,12 @@
 #define tr_error(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
 #endif
 
+// When calling the SendTo function, the large hex string for the bytes to send is chopped into chunks 
+#define SENDTO_CHUNK_SIZE 50
+
 /**********************************************************************
  * PRIVATE METHODS
  **********************************************************************/
-
- bool UbloxATCellularInterfaceN2xx::SendAT(const char *cmd)
- {
-     tr_debug("SendAT helper()");
-     return _at->send(cmd) && _at->recv("OK");     
- } 
  
 // Event thread for asynchronous received data handling.
 void UbloxATCellularInterfaceN2xx::handle_event(){
@@ -317,10 +314,10 @@
     LOCK();
         
     // AT+NSOSTF= socket, remote_addr, remote_port, length, data
-    tr_debug("Going to send this:-");
-    tr_debug("AT+NSOSTF=%d,%s,%d,0x0,%d,%s", socket->modem_handle, address.get_ip_address(), address.get_port(), size, str);
-    tr_debug("Writing AT command...");
-    if (_at->send("AT+NSOSTF=%d,%s,%d,0x0,%d,%s", socket->modem_handle, address.get_ip_address(), address.get_port(), size, str)) {
+    tr_debug("Writing AT+NSOSTF=<sktid>,<ipaddr>,<port>,<flags>,<size>,<hex string> command...");
+    char cmd[50];
+    int cmdsize = sprintf(cmd, "AT+NSOSTF=%d,%s,%d,0x0,%d,", socket->modem_handle, address.get_ip_address(), address.get_port(), size);    
+    if (_at->write(cmd, cmdsize+1) && sendATChopped(str)) {
         tr_debug("Reading back the Sent Size...");
         if (_at->recv("%d,%d\n", &id, &sent) && _at->recv("OK")) {            
             tr_debug("Received %d bytes on socket %d", sent, id);
@@ -337,6 +334,40 @@
     return sent;
 }
 
+bool UbloxATCellularInterfaceN2xx::sendATChopped(const char *cmd)
+{
+    char buff[SENDTO_CHUNK_SIZE];
+    while (*cmd != '\0')
+    {
+        int i=0;
+        
+        tr_debug("Copying up to 50 chars... ");
+        for (i=0; i<SENDTO_CHUNK_SIZE; i++) {
+            buff[i] = *cmd;
+            
+            // if we have copied the NULL terminator, we can exit 
+            if (*cmd == '\0')  
+                break;
+            
+            // still more characters to copy, so move along
+            cmd++;
+        }
+        tr_debug("Copied %d chars. ", i);
+        
+        if (*cmd == '\0') {
+            tr_debug("send()\n");
+            if (!_at->send(buff))
+                return false;
+        } else {
+            tr_debug("write()\n");
+            if (!_at->write(buff, 50))
+                return false;
+        }
+     }
+     
+     return true;
+}
+
 void UbloxATCellularInterfaceN2xx::bin_to_hex(const char *buff, unsigned int length, char *output)
 {
     char binHex[] = "0123456789ABCDEF";
@@ -691,6 +722,11 @@
     return nsapi_error;
 }
 
+bool UbloxATCellularInterfaceN2xx::initialise()
+{
+    return init();
+}
+
 // Make a cellular connection using the IP stack on board the cellular modem
 nsapi_error_t UbloxATCellularInterfaceN2xx::connect()
 {
@@ -698,7 +734,7 @@
     bool registered = false;
 
     // Set up modem and then register with the network
-    if (init()) {
+    if (initialise()) {
         
         tr_debug("Trying to register...");
         nsapi_error = NSAPI_ERROR_NO_CONNECTION;