Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Files at this revision

API Documentation at this revision

Comitter:
dan_ackme
Date:
Mon Aug 11 13:55:07 2014 -0700
Parent:
5:8d91a87ebba2
Child:
7:41d456a65f14
Commit message:
added support for assertion handling

Changed in this revision

WiconnectInterface.h Show annotated file Show diff for this revision Revisions of this file
internal/common.h Show annotated file Show diff for this revision Revisions of this file
internal/network/NetworkInterface.cpp Show annotated file Show diff for this revision Revisions of this file
internal/network/NetworkJoin.cpp Show annotated file Show diff for this revision Revisions of this file
internal/network/NetworkWebSetup.cpp Show annotated file Show diff for this revision Revisions of this file
internal/types/File.cpp Show annotated file Show diff for this revision Revisions of this file
internal/types/QueuedCommand.cpp Show annotated file Show diff for this revision Revisions of this file
internal/types/ScanResult.cpp Show annotated file Show diff for this revision Revisions of this file
internal/types/ScanResultList.cpp Show annotated file Show diff for this revision Revisions of this file
internal/types/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
internal/wiconnect/Wiconnect.cpp Show annotated file Show diff for this revision Revisions of this file
sdk/mbed/WiconnectSerial.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/WiconnectInterface.h	Mon Aug 11 04:00:39 2014 -0700
+++ b/WiconnectInterface.h	Mon Aug 11 13:55:07 2014 -0700
@@ -90,6 +90,7 @@
 
     static const char* getWiconnectResultStr(WiconnectResult wiconnectResult);
     void setDebugLogger(LogFunc logFunc);
+    void setAssertLogger(LogFunc assertLogFunc);
 
 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
     WiconnectResult enqueueCommand(QueuedCommand *command, const Callback &commandCompleteHandler = Callback());
@@ -104,6 +105,7 @@
     friend class QueuedCommand;
     friend class WiconnectSerial;
     friend class ScanResult;
+    friend class ScanResultList;
     friend class Socket;
     friend class File;
 #endif
@@ -136,7 +138,8 @@
     WiconnectResult inline receivePacket();
     void issueCommandCallback(WiconnectResult result);
 
-    wiconnect::LogFunc debugLogger;
+    LogFunc debugLogger;
+    LogFunc assertLogger;
     void debugLog(const char *msg, ...);
 
 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
--- a/internal/common.h	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/common.h	Mon Aug 11 13:55:07 2014 -0700
@@ -11,7 +11,6 @@
 #pragma once
 
 
-
 #include "WiconnectCommands.h"
 
 /* Note we need the 2 concats below because arguments to ##
@@ -21,6 +20,9 @@
 #define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
 #define ct_assert(e) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
 
+#define wiconnect_assert(_wiconnect, msg, expr) if(!(expr)){_wiconnect->assertLogger.call(msg); for(;;); }
+
+
 
 #ifndef WICONNECT_ASYNC_TIMER_ENABLED
 #define CHECK_CALLBACK_AVAILABLE(cb) if(cb.isValid()) return WICONNECT_UNSUPPORTED
--- a/internal/network/NetworkInterface.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/network/NetworkInterface.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -182,6 +182,9 @@
             if(!NetworkInterface::strToIp(wiconnect->internalBuffer, ip))
             {
                 result = WICONNECT_RESPONSE_PARSE_ERROR;
+            }
+            else
+            {
                 wiconnect->internalProcessingState = FS_GET_NETMASK;
             }
         }
@@ -194,6 +197,9 @@
             if(!NetworkInterface::strToIp(wiconnect->internalBuffer, netmask))
             {
                 result = WICONNECT_RESPONSE_PARSE_ERROR;
+            }
+            else
+            {
                 wiconnect->internalProcessingState = FS_GET_GATEWAY;
             }
         }
--- a/internal/network/NetworkJoin.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/network/NetworkJoin.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -118,14 +118,15 @@
 // this is called every 1s by the monitorTimer
 void NetworkInterface::joinStatusMonitor()
 {
+    static char responseBuffer[4];
+    static uint8_t cmdBuffer[sizeof(QueuedCommand)];
+    QueuedCommand *cmd = (QueuedCommand*)cmdBuffer;
+
     monitorTimer.stop();
-    QueuedCommand *cmd = new QueuedCommand(32, NULL, "get network.status");
-    if(cmd == NULL)
-        return;
-    if(wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::joinStatusCheckCallback)) != WICONNECT_SUCCESS)
-    {
-        delete cmd;
-    }
+
+   *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "get network.status");
+
+    wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::joinStatusCheckCallback));
 }
 
 /*************************************************************************************************/
@@ -149,8 +150,6 @@
         }
     }
 
-    delete cmd;
-
     if(isComplete)
     {
         completeHandler.call(result, NULL, NULL);
--- a/internal/network/NetworkWebSetup.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/network/NetworkWebSetup.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -117,14 +117,15 @@
 /*************************************************************************************************/
 void NetworkInterface::webSetupStatusMonitor()
 {
+    static char responseBuffer[4];
+    static uint8_t cmdBuffer[sizeof(QueuedCommand)];
+    QueuedCommand *cmd = (QueuedCommand*)cmdBuffer;
+
     monitorTimer.stop();
-    QueuedCommand *cmd = new QueuedCommand(32, NULL, "setup status");
-    if(cmd == NULL)
-        return;
-    if(wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::webSetupStatusCheckCallback)) != WICONNECT_SUCCESS)
-    {
-        delete cmd;
-    }
+
+    *cmd = QueuedCommand(sizeof(responseBuffer), responseBuffer, "setup status");
+
+    wiconnect->enqueueCommand(cmd, Callback(this, &NetworkInterface::webSetupStatusCheckCallback));
 }
 
 /*************************************************************************************************/
@@ -147,8 +148,6 @@
         }
     }
 
-    delete cmd;
-
     if(isComplete)
     {
         completeHandler.call(result, NULL, NULL);
--- a/internal/types/File.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/types/File.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -9,7 +9,6 @@
  */
 
 
-#include <assert.h>
 #include "Wiconnect.h"
 #include "internal/common.h"
 
@@ -33,12 +32,12 @@
         if(rxBuffer_ == NULL)
         {
 #ifdef WICONNECT_ENABLE_MALLOC
-            assert(wiconnect->_malloc != NULL);
+            wiconnect_assert(wiconnect, "File(), malloc not defined", wiconnect->_malloc != NULL);
             rxBuffer.buffer = (uint8_t*)wiconnect->_malloc(rxBufferLen);
-            assert(rxBuffer.buffer != NULL);
+            wiconnect_assert(wiconnect, "File(), failed to malloc buffer", rxBuffer.buffer != NULL);
             rxBuffer.allocated = true;
 #else
-            assert(0);
+            wiconnect_assert(0);
 #endif
         }
     }
@@ -63,7 +62,7 @@
 #ifdef WICONNECT_ENABLE_MALLOC
     if(rxBuffer.allocated && rxBuffer.size > 0)
     {
-        assert(wiconnect->_free != NULL);
+        wiconnect_assert(wiconnect, "~File(), free not defined", wiconnect->_free != NULL);
         wiconnect->_free(rxBuffer.buffer);
     }
 #endif
@@ -115,14 +114,16 @@
 /*************************************************************************************************/
 void* File::operator new(size_t size)
 {
-    assert(Wiconnect::getInstance()->_malloc != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "File:new, malloc not defined", wiconnect->_malloc != NULL);
     return Wiconnect::getInstance()->_malloc(size);
 }
 
 /*************************************************************************************************/
 void File::operator delete(void* ptr)
 {
-    assert(Wiconnect::getInstance()->_free != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "File:delete, free not defined", wiconnect->_free != NULL);
     Wiconnect::getInstance()->_free(ptr);
 }
 
--- a/internal/types/QueuedCommand.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/types/QueuedCommand.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -8,10 +8,9 @@
  * written permission of ACKme Networks.
  */
 
-#include <assert.h>
 
 #include "Wiconnect.h"
-
+#include "internal/common.h"
 
 /*************************************************************************************************/
 QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList)
@@ -123,14 +122,16 @@
 /*************************************************************************************************/
 void* QueuedCommand::operator new(size_t size)
 {
-    assert(Wiconnect::getInstance()->_malloc != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "QueuedCommand:new malloc not defined", wiconnect->_malloc != NULL);
     return Wiconnect::getInstance()->_malloc(size);
 }
 
 /*************************************************************************************************/
 void QueuedCommand::operator delete(void* ptr)
 {
-    assert(Wiconnect::getInstance()->_free != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "QueuedCommand:delete free not defined", wiconnect->_free != NULL);
     Wiconnect::getInstance()->_free(ptr);
 }
 
@@ -140,17 +141,19 @@
     if(responseBufferLen_ > 0)
     {
 #ifdef WICONNECT_ENABLE_MALLOC
+        Wiconnect *wiconnect = Wiconnect::getInstance();
         allocatedBuffer = false;
         if(responseBuffer_ == NULL)
         {
-            assert(Wiconnect::getInstance()->_malloc != NULL);
-            responseBuffer = (char*)Wiconnect::getInstance()->_malloc(responseBufferLen_);
+            wiconnect_assert(wiconnect, "QueuedCommand() malloc not defined", wiconnect->_malloc != NULL);
+            responseBuffer = (char*)wiconnect->_malloc(responseBufferLen_);
+            wiconnect_assert(wiconnect, "QueuedCommand() responseBuffer malloc failed", responseBuffer != NULL);
             allocatedBuffer = true;
         }
         else
 #endif
         {
-            assert(responseBuffer_ != NULL);
+            wiconnect_assert(wiconnect, "QueuedCommand(), null buffer", responseBuffer_ != NULL);
             responseBuffer = responseBuffer_;
         }
     }
--- a/internal/types/ScanResult.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/types/ScanResult.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -8,8 +8,6 @@
  * written permission of ACKme Networks.
  */
 
-#include <assert.h>
-
 #include "Wiconnect.h"
 #include "types/ScanResult.h"
 #include "NetworkInterface.h"
@@ -70,14 +68,16 @@
 /*************************************************************************************************/
 void* ScanResult::operator new(size_t size)
 {
-    assert(Wiconnect::getInstance()->_malloc != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "ScanResult:new, malloc not defined", wiconnect->_malloc != NULL);
     return Wiconnect::getInstance()->_malloc(size);
 }
 
 /*************************************************************************************************/
 void ScanResult::operator delete(void* ptr)
 {
-    assert(Wiconnect::getInstance()->_free != NULL);
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "ScanResult:delete, free not defined", wiconnect->_free != NULL);
     Wiconnect::getInstance()->_free(ptr);
 }
 
--- a/internal/types/ScanResultList.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/types/ScanResultList.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -8,16 +8,16 @@
  * written permission of ACKme Networks.
  */
 
-#include <assert.h>
 #include "Wiconnect.h"
 #include "types/ScanResult.h"
-
+#include "internal/common.h"
 
 
 /*************************************************************************************************/
 ScanResultList::ScanResultList(int bufferLen_, void *buffer_)
 {
-    assert((bufferLen_ == 0 && buffer_ == NULL) || (bufferLen_ != 0 && buffer_ != NULL));
+    Wiconnect *wiconnect = Wiconnect::getInstance();
+    wiconnect_assert(wiconnect, "ScanResultList(), bad buffer", (bufferLen_ == 0 && buffer_ == NULL) || (bufferLen_ != 0 && buffer_ != NULL));
     count = 0;
     listHead = listTail = NULL;
     buffer = (uint8_t*)buffer_;
--- a/internal/types/Socket.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/types/Socket.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -8,7 +8,6 @@
  * written permission of ACKme Networks.
  */
 
-#include <assert.h>
 #include <stdarg.h>
 #include "Wiconnect.h"
 #include "internal/common.h"
@@ -37,12 +36,12 @@
         if(txBuffer_ == NULL)
         {
 #ifdef WICONNECT_ENABLE_MALLOC
-            assert(wiconnect->_malloc != NULL);
+            wiconnect_assert(wiconnect, "Socket(), malloc not defined", wiconnect->_malloc != NULL);
             txBuffer.buffer = (uint8_t*)wiconnect->_malloc(txBufferLen_);
-            assert(txBuffer.buffer != NULL);
+            wiconnect_assert(wiconnect, "Socket(), txBuffer malloc failed", txBuffer.buffer != NULL);
             txBuffer.allocated = true;
 #else
-            assert(0);
+            wiconnect_assert(0);
 #endif
         }
     }
@@ -52,12 +51,12 @@
         if(rxBuffer_ == NULL)
         {
 #ifdef WICONNECT_ENABLE_MALLOC
-            assert(wiconnect->_malloc != NULL);
+            wiconnect_assert(wiconnect, "Socket(), malloc not defined", wiconnect->_malloc != NULL);
             rxBuffer.buffer = (uint8_t*)wiconnect->_malloc(rxBufferLen_);
-            assert(rxBuffer.buffer != NULL);
+            wiconnect_assert(wiconnect, "Socket(), rxBuffer malloc failed", rxBuffer.buffer != NULL);
             rxBuffer.allocated = true;
 #else
-            assert(0);
+            wiconnect_assert(0);
 #endif
         }
     }
@@ -93,12 +92,12 @@
 #ifdef WICONNECT_ENABLE_MALLOC
     if(txBuffer.allocated && txBuffer.size > 0)
     {
-        assert(wiconnect->_free != NULL);
+        wiconnect_assert(wiconnect, "~Socket(), free not defined", wiconnect->_free != NULL);
         wiconnect->_free(txBuffer.buffer);
     }
     if(rxBuffer.allocated && rxBuffer.size > 0)
     {
-        assert(wiconnect->_free != NULL);
+        wiconnect_assert(wiconnect, "~Socket(), free not defined", wiconnect->_free != NULL);
         wiconnect->_free(rxBuffer.buffer);
     }
 #endif
--- a/internal/wiconnect/Wiconnect.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/internal/wiconnect/Wiconnect.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -11,7 +11,6 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
-#include <assert.h>
 
 #include "WiconnectInterface.h"
 #include "internal/common.h"
@@ -48,7 +47,7 @@
 #ifdef WICONNECT_ENABLE_MALLOC
     if(internalBufferSize_ > 0 && internalBuffer_ == NULL)
     {
-        assert(_malloc != NULL);
+        wiconnect_assert(this, "Wiconnect(), malloc not defined", _malloc != NULL);
         internalBuffer = (char*)_malloc(internalBufferSize_);
         internalBufferAlloc = true;
     }
@@ -80,6 +79,9 @@
 Wiconnect::Wiconnect(const SerialConfig &serialConfig, void *internalBuffer, int internalBufferSize, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) :
         NetworkInterface(this), SocketInterface(this), FileInterface(this), MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake)
 {
+#ifdef WICONNECT_ENABLE_MALLOC
+    wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL));
+#endif
     prepare(internalBuffer, internalBufferSize, nonBlocking);
 }
 
@@ -87,6 +89,9 @@
 Wiconnect::Wiconnect(const SerialConfig &serialConfig, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) :
     NetworkInterface(this), SocketInterface(this), FileInterface(this), MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake)
 {
+#ifdef WICONNECT_ENABLE_MALLOC
+    wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL));
+#endif
     prepare(NULL, 0, nonBlocking);
 }
 
@@ -259,6 +264,12 @@
 }
 
 /*************************************************************************************************/
+void Wiconnect::setAssertLogger(LogFunc assertLogFunc)
+{
+    assertLogger = assertLogFunc;
+}
+
+/*************************************************************************************************/
 void Wiconnect::debugLog(const char *msg, ...)
 {
     if(!debugLogger.isValid())
--- a/sdk/mbed/WiconnectSerial.cpp	Mon Aug 11 04:00:39 2014 -0700
+++ b/sdk/mbed/WiconnectSerial.cpp	Mon Aug 11 13:55:07 2014 -0700
@@ -8,7 +8,6 @@
  * written permission of ACKme Networks.
  */
 
-#include <assert.h>
 
 #include "Wiconnect.h"
 #include "internal/common.h"
@@ -23,10 +22,10 @@
 typedef struct
 {
     uint16_t size;
+    volatile uint16_t count;
     uint8_t *start, *end;
     volatile uint8_t *head;
     volatile uint8_t *tail;
-    volatile uint16_t count;
 } SerialRxBuffer;
 
 
@@ -44,13 +43,14 @@
     {
         if(config.serialRxBuffer != NULL)
         {
-            rxBuffer->head = (uint8_t*)config.serialRxBuffer;
+            rxBuffer->start = (uint8_t*)config.serialRxBuffer;
         }
 #ifdef WICONNECT_ENABLE_MALLOC
         else
         {
-            assert(wiconnect->_malloc != NULL);
+            wiconnect_assert(wiconnect, "WiconnectSerial(), malloc not defined", wiconnect->_malloc != NULL);
             rxBuffer->start = (uint8_t*)wiconnect->_malloc(config.serialRxBufferSize);
+            wiconnect_assert(wiconnect, "WiconnectSerial(), malloc failed", rxBuffer->start != NULL);
             bufferAlloc = true;
         }
 #endif