This library controls the WNC. There is a derived class for usage from the K64F board.
Fork of WncControllerLibrary by
Diff: WncController.cpp
- Revision:
- 10:2ec59906a24e
- Parent:
- 9:76c6dd1434e1
- Child:
- 11:3ff6c1cb5b03
--- a/WncController.cpp Fri Sep 09 17:47:55 2016 +0000 +++ b/WncController.cpp Fri Sep 09 23:07:36 2016 +0000 @@ -36,10 +36,9 @@ ///////////////////////////////////////////////////// // Static initializers ///////////////////////////////////////////////////// -WncController::WncSocketInfo_s WncController::m_sSock[MAX_NUM_WNC_SOCKETS] = { - { false, "192.168.0.1", 80, 0, 25, true, 30 } // , -// { false, "192.168.0.1", 80, 0, 25, true, 30 } -}; +WncController::WncSocketInfo_s WncController::m_sSock[MAX_NUM_WNC_SOCKETS]; +const WncController::WncSocketInfo_s WncController::defaultSockStruct = { 0, false, "192.168.0.1", 80, 0, 25, true, 30 }; + WncController::WncState_e WncController::m_sState = WNC_OFF; uint16_t WncController::m_sCmdTimeoutMs = WNC_CMD_TIMEOUT_MS; string WncController::m_sApnStr = "NULL"; @@ -118,6 +117,8 @@ */ WncController::WncController(void) { + for(unsigned i; i<MAX_NUM_WNC_SOCKETS; i++) + m_sSock[i] = defaultSockStruct; } void WncController::enableDebug(bool on, bool moreDebugOn) @@ -348,20 +349,27 @@ // Already open ? Must close if want to re-open with new settings. if (m_sSock[numSock].open == true) { dbgPuts("Socket already open, close then re-open!"); - at_sockclose_wnc(numSock); + at_sockclose_wnc(m_sSock[numSock].numWncSock); m_sSock[numSock].open = false; } m_sSock[numSock].myPort = port; m_sSock[numSock].isTcp = tcp; m_sSock[numSock].timeOutSec = timeOutSec; - m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), port, numSock, tcp, timeOutSec); + + int numWncSock = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), port, numSock, tcp, timeOutSec); + m_sSock[numSock].numWncSock = numWncSock; + if (numWncSock > 0 && numWncSock <= MAX_NUM_WNC_SOCKETS) + m_sSock[numSock].open = true; + else + m_sSock[numSock].open = false; + if (m_sSock[numSock].open == false) { dbgPuts("Socket open fail!!!!"); // Work-around. If the sock open fails it needs to be told // to close. If 6 sock opens happen with a fail, it further // crashes the WNC. Not sure why the sock won't open. - at_sockclose_wnc(numSock); + at_sockclose_wnc(m_sSock[numSock].numWncSock); } } else { @@ -386,9 +394,9 @@ bool WncController::sockWrite(const char * const s, uint32_t n, uint16_t numSock, bool isTcp) { - bool result = true;; + bool result = true; - AtCmdErr_e cmdRes = at_sockwrite_wnc(s, n, numSock, isTcp); + AtCmdErr_e cmdRes = at_sockwrite_wnc(s, n, m_sSock[numSock].numWncSock, isTcp); if (cmdRes != WNC_AT_CMD_OK) { if ((cmdRes == WNC_AT_CMD_ERREXT) || (cmdRes == WNC_AT_CMD_TIMEOUT) || (cmdRes == WNC_AT_CMD_ERRCME)) { @@ -468,7 +476,7 @@ bool foundData = false; do { AtCmdErr_e cmdRes; - cmdRes = at_sockread_wnc(&readStr, numSock, m_sSock[numSock].isTcp); + cmdRes = at_sockread_wnc(&readStr, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); if (WNC_AT_CMD_OK == cmdRes) { // This will let this loop read until the socket data is // empty. If no data, then wait the retry amount of time. @@ -524,12 +532,10 @@ size_t numRead; do { AtCmdErr_e cmdRes; - if (maxReadBufLen < MAX_WNC_READ_BYTES) { - cmdRes = at_sockread_wnc(readBuf, &numRead, maxReadBufLen, numSock, m_sSock[numSock].isTcp); - dbgPutsNoTime("Warning: read back buffer to small?"); - } + if (maxReadBufLen < MAX_WNC_READ_BYTES) + cmdRes = at_sockread_wnc(readBuf, &numRead, maxReadBufLen, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); else - cmdRes = at_sockread_wnc(readBuf, &numRead, MAX_WNC_READ_BYTES, numSock, m_sSock[numSock].isTcp); + cmdRes = at_sockread_wnc(readBuf, &numRead, MAX_WNC_READ_BYTES, m_sSock[numSock].numWncSock, m_sSock[numSock].isTcp); if (WNC_AT_CMD_OK == cmdRes) { // This will let this loop read until the socket data is @@ -537,7 +543,7 @@ if (numRead > 0) { foundData = true; i = 1; - if (numRead < maxReadBufLen) { + if (numRead <= maxReadBufLen) { maxReadBufLen -= numRead; numCopied += numRead; readBuf += numRead; @@ -566,7 +572,7 @@ else waitMs(to); } - } while (i-- > 0); + } while ((i-- > 0) && (maxReadBufLen > 0)); } else { dbgPuts("Socket is closed for read"); @@ -628,7 +634,7 @@ { if (numSock < MAX_NUM_WNC_SOCKETS) { - if (false == at_sockclose_wnc(numSock)) + if (false == at_sockclose_wnc(m_sSock[numSock].numWncSock)) dbgPuts("Sock close may not have closed!"); // Even with an error the socket could have closed, @@ -818,8 +824,15 @@ // Try to open and close the socket do { dbgPuts("Try to close and re-open socket"); - at_sockclose_wnc(numSock); - m_sSock[numSock].open = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), m_sSock[numSock].myPort, numSock, m_sSock[numSock].isTcp, m_sSock[numSock].timeOutSec); + at_sockclose_wnc(m_sSock[numSock].numWncSock); + + int numWncSock = at_sockopen_wnc(m_sSock[numSock].myIpAddressStr.c_str(), m_sSock[numSock].myPort, numSock, m_sSock[numSock].isTcp, m_sSock[numSock].timeOutSec); + m_sSock[numSock].numWncSock = numWncSock; + if (numWncSock > 0 && numWncSock <= MAX_NUM_WNC_SOCKETS) + m_sSock[numSock].open = true; + else + m_sSock[numSock].open = false; + if (m_sSock[numSock].open == false) dbgPuts("Failed to re-open socket!"); } while (m_sSock[numSock].open == false); @@ -1303,7 +1316,7 @@ } -bool WncController::at_sockopen_wnc(const char * const ip, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec) +int16_t WncController::at_sockopen_wnc(const char * const ip, uint16_t port, uint16_t numSock, bool tcp, uint16_t timeOutSec) { string * pRespStr; string cmd_str("AT@SOCKCREAT="); @@ -1316,27 +1329,37 @@ res = sendWncCmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs); if (res == WNC_AT_CMD_OK) { - cmd_str = "AT@SOCKCONN="; - cmd_str += _to_string(numSock + 1); - cmd_str += ",\""; - cmd_str += ip; - cmd_str += "\","; - cmd_str += _to_string(port); - cmd_str += ","; - if (timeOutSec < 30) - timeOutSec = 30; - else if (timeOutSec > 360) - timeOutSec = 360; - cmd_str += _to_string(timeOutSec); - res = sendWncCmd(cmd_str.c_str(), &pRespStr, 1000 * timeOutSec + 1000); - if (m_sMoreDebugEnabled) { - at_send_wnc_cmd("AT@SOCKCREAT?", &pRespStr, m_sCmdTimeoutMs); - at_send_wnc_cmd("AT@SOCKCONN?", &pRespStr, m_sCmdTimeoutMs); + size_t pos1 = pRespStr->find("T:"); + size_t pos2 = pRespStr->rfind("OK"); + if ((pos1 != string::npos) && (pos2 != string::npos)) { + size_t numLen = pos2 - (pos1 + 2); + string sockStr = pRespStr->substr(pos1 + 2, numLen); + cmd_str = "AT@SOCKCONN="; + cmd_str += sockStr; + cmd_str += ",\""; + cmd_str += ip; + cmd_str += "\","; + cmd_str += _to_string(port); + cmd_str += ","; + if (timeOutSec < 30) + timeOutSec = 30; + else if (timeOutSec > 360) + timeOutSec = 360; + cmd_str += _to_string(timeOutSec); + res = sendWncCmd(cmd_str.c_str(), &pRespStr, 1000 * timeOutSec + 1000); + if (m_sMoreDebugEnabled) { + at_send_wnc_cmd("AT@SOCKCREAT?", &pRespStr, m_sCmdTimeoutMs); + at_send_wnc_cmd("AT@SOCKCONN?", &pRespStr, m_sCmdTimeoutMs); + } + return (strtol(sockStr.c_str(), NULL, 10)); } - return (res == WNC_AT_CMD_OK); + else { + dbgPuts("Invalid sockcreat response!"); + return (0); + } } else - return (false); + return (0); } bool WncController::at_sockclose_wnc(uint16_t numSock) @@ -1344,7 +1367,7 @@ string * pRespStr; string cmd_str("AT@SOCKCLOSE="); - cmd_str += _to_string(numSock + 1); + cmd_str += _to_string(numSock); // Don't check the cell status to close the socket return (WNC_AT_CMD_OK == at_send_wnc_cmd(cmd_str.c_str(), &pRespStr, m_sCmdTimeoutMs)); } @@ -1413,7 +1436,7 @@ else cmd_str="AT@SOCKWRITE="; // "AT@SOCKSEND="; - cmd_str += _to_string(numSock + 1); + cmd_str += _to_string(numSock); cmd_str += ","; cmd_str += _to_string(n); cmd_str += ",\""; @@ -1452,7 +1475,7 @@ else cmd_str="AT@SOCKREAD="; // "AT@SOCKRECV="; - cmd_str += _to_string(numSock + 1); + cmd_str += _to_string(numSock); cmd_str += ","; cmd_str += _to_string(MAX_WNC_READ_BYTES); @@ -1507,7 +1530,7 @@ else cmd_str="AT@SOCKREAD="; // "AT@SOCKRECV="; - cmd_str += _to_string(numSock + 1); + cmd_str += _to_string(numSock); cmd_str += ","; cmd_str += _to_string(n); @@ -1522,7 +1545,7 @@ i = (pos_end - pos_start + 1); // Num hex chars, 2 per byte else i = 0; - + if (i < 0) dbgPuts("Invalid READ string!"); else if (i > 2*n) {