A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Diff: cellular/Cellular.cpp
- Revision:
- 32:629e6b1c8e22
- Parent:
- 27:8e6188cbcfd4
- Child:
- 33:e04aa7c013c9
--- a/cellular/Cellular.cpp Tue Dec 17 23:58:50 2013 +0000 +++ b/cellular/Cellular.cpp Wed Dec 18 22:45:53 2013 +0000 @@ -235,15 +235,15 @@ return -1; } int bytesRead = 0; - + if(timeout >= 0) { Timer tmr; tmr.start(); while (tmr.read_ms() <= timeout && bytesRead < max) { - if (io->readable()) { - if(io->read(data[bytesRead]) == 1) { - bytesRead++; - } + int available = io->readable(); + if (available > 0) { + int size = MIN(available, max - bytesRead); + bytesRead += io->read(&data[bytesRead], size); } else { wait(0.05); } @@ -252,6 +252,49 @@ bytesRead = io->read(data, max); } + if(bytesRead > 0 && socketCloseable) { + //Remove escape characters + int index = 0; + bool escapeFlag = false; + for(int i = 0; i < bytesRead; i++) { + if(data[i] == DLE || data[i] == ETX) { + if(escapeFlag == true) { + //This character has been escaped + escapeFlag = false; + } else if(data[bytesRead] == DLE) { + //Found escape character + escapeFlag = true; + continue; + } else { + //ETX sent without escape -> Socket closed + printf("[INFO] Read ETX character without DLE escape. Socket closed\r\n"); + socketOpened = false; + continue; + } + } + + if(index != i) { + data[index] = data[i]; + } + index++; + } + bytesRead = index; + } + + printf("[DEBUG] Scanning received data for socket closed message\r\n"); + //Scan for socket closed message + for(size_t i = 0; i < bytesRead; i++) { + if(data[i] == 'O') { + if(strstr(&data[i], "Ok_Info_SocketClosed")) { + printf("[INFO] Found socket closed message. Socket closed\r\n"); + //Close socket and Cut Off End of Message + socketOpened = false; + data[i] = '\0'; + bytesRead = i; + break; + } + } + } return bytesRead; }