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
Diff: internal/wiconnect/Wiconnect.cpp
- Revision:
- 0:ea85c4bb5e1f
- Child:
- 1:6ec9998427ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/internal/wiconnect/Wiconnect.cpp Mon Aug 11 09:58:24 2014 +0000 @@ -0,0 +1,297 @@ +/* + * Copyright 2014, ACKme Networks + * All Rights Reserved. + * + * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks; + * the contents of this file may not be disclosed to third parties, copied + * or duplicated in any form, in whole or in part, without the prior + * written permission of ACKme Networks. + */ + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <assert.h> + +#include "WiconnectInterface.h" +#include "internal/common.h" + + + + + +using namespace wiconnect; + + + + + +#ifdef WICONNECT_ENABLE_MALLOC +#define MALLOC_ARGS , void* (*mallocPtr)(size_t), void (*freePtr)(void*) +#define MALLOC_CONSTRUCTORS , _malloc(mallocPtr), _free(freePtr) +#else +#define MALLOC_ARGS +#define MALLOC_CONSTRUCTORS +#endif + + +static Wiconnect* instance = NULL; + + + + +/*************************************************************************************************/ +void Wiconnect::prepare(void *internalBuffer_, int internalBufferSize_, bool nonBlocking_) +{ + instance = this; + internalBufferAlloc = false; +#ifdef WICONNECT_ENABLE_MALLOC + if(internalBufferSize_ > 0 && internalBuffer_ == NULL) + { + assert(_malloc != NULL); + internalBuffer = (char*)_malloc(internalBufferSize_); + internalBufferAlloc = true; + } + else +#endif + { + internalBuffer = (char*)internalBuffer_; + } + + internalProcessingState = 0; + currentCommandId = NULL; + internalBufferSize = internalBufferSize_; + nonBlocking = nonBlocking_; + commandExecuting = false; + initialized = false; + pinToGpioMapper = NULL; + defaultTimeoutMs = WICONNECT_DEFAULT_TIMEOUT; + + memset(commandContext, 0, sizeof(commandContext)); + +#ifdef WICONNECT_ASYNC_TIMER_ENABLED + commandProcessingPeriod = WICONNECT_DEFAULT_COMMAND_PROCESSING_PERIOD; + currentQueuedCommand = NULL; +#endif +} + + +/*************************************************************************************************/ +Wiconnect::Wiconnect(const SerialConfig &serialConfig, void *internalBuffer, int internalBufferSize, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) : + NetworkInterface(this), SocketInterface(this), FileInterface(this), serial(serialConfig, this), resetGpio(reset), wakeGpio(wake) MALLOC_CONSTRUCTORS +{ + prepare(internalBuffer, internalBufferSize, nonBlocking); +} + +/*************************************************************************************************/ +Wiconnect::Wiconnect(const SerialConfig &serialConfig, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) : + NetworkInterface(this), SocketInterface(this), FileInterface(this), serial(serialConfig, this), resetGpio(reset), wakeGpio(wake) MALLOC_CONSTRUCTORS +{ + prepare(NULL, 0, nonBlocking); +} + +/*************************************************************************************************/ +Wiconnect::~Wiconnect() +{ +#ifdef WICONNECT_ENABLE_MALLOC + if(internalBufferAlloc) + { + _free(internalBuffer); + } +#endif +} + +/*************************************************************************************************/ +WiconnectResult Wiconnect::init(bool bringNetworkUp) +{ + WiconnectResult result; + int retries; + bool savedNonBlocking = nonBlocking; + + if(WICONNECT_FAILED(result, reset())) + { + return result; + } + + delayMs(1000); + + initialized = true; + nonBlocking = false; + + for(retries = 3; retries > 0; --retries) + { + result = sendCommand(CMD_SET_SYSTEM_COMMAND_MODE, "machine"); + if(result != WICONNECT_SUCCESS) + { + delayMs(1000); + } + else + { + break; + } + } + + if(result == WICONNECT_SUCCESS && bringNetworkUp) + { + sendCommand(15000, "ping -g"); + } + + nonBlocking = savedNonBlocking; + if(result != WICONNECT_SUCCESS) + { + initialized = false; + } + + + return result; +} + +/*************************************************************************************************/ +void Wiconnect::deinit(void) +{ + initialized = false; +} + +/*************************************************************************************************/ +Wiconnect* Wiconnect::getInstance() +{ + return instance; +} + +/*************************************************************************************************/ +bool Wiconnect::isInitialized() +{ + return initialized; +} + +/*************************************************************************************************/ +WiconnectResult Wiconnect::reset() +{ + resetGpio = 0; + delayMs(10); + resetGpio = 1; + delayMs(1000); + return WICONNECT_SUCCESS; +} + +/*************************************************************************************************/ +WiconnectResult Wiconnect::wakeup() +{ + wakeGpio = 1; + delayMs(1); + wakeGpio = 0; + return WICONNECT_SUCCESS; +} + +/*************************************************************************************************/ +void Wiconnect::flush(int delayMs) +{ + if(delayMs != 0) + { + serial.write("\r\n\r\n", 4, 0); + } + delayMs(delayMs); + serial.flush(); +} + +/*************************************************************************************************/ +void Wiconnect::setPinToGpioMapper(PinToGpioMapper mapper) +{ + pinToGpioMapper = mapper; +} + +/*************************************************************************************************/ +WiconnectResult Wiconnect::getVersion(char *versionBuffer, int versionBufferSize, const Callback &completeCallback) +{ + if(versionBuffer != NULL && versionBufferSize == 0) + { + return WICONNECT_BAD_ARG; + } + return sendCommand(completeCallback, versionBuffer, versionBufferSize, CMD_GET_VERSION); +} + +/*************************************************************************************************/ +const char* Wiconnect::getWiconnectResultStr(WiconnectResult wiconnectResult) +{ + static const char* const wiconnectSuccessStrTable[] = { + "Success", // WICONNECT_SUCCESS + "Processing command", // WICONNECT_PROCESSING + "Idle", // WICONNECT_IDLE + "Aborted", // WICONNECT_ABORTED + }; + static const char* const wiconnectErrorStrTable[] = { + "", + "General error", // WICONNECT_ERROR + "WiConnect command code error", // WICONNECT_CMD_RESPONSE_ERROR + "Null buffer", // WICONNECT_NULL_BUFFER + "Not initialized", // WICONNECT_NOT_INITIALIZED + "Overflow", // WICONNECT_OVERFLOW + "Timeout", // WICONNECT_TIMEOUT + "Response handler null", // WICONNECT_RESPONSE_HANDLER_NULL + "Response parse error", // WICONNECT_RESPONSE_PARSE_ERROR + "Another command is executing", // WICONNECT_ANOTHER_CMD_EXECUTING + "Bad argument(s)", // WICONNECT_BAD_ARG + "Unsupported", // WICONNECT_UNSUPPORTED + "Pin name to GPIO mapper null", // WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL + "Duplicate", // WICONNECT_DUPLICATE + "Not found", // WICONNECT_NOT_FOUND + "No mapping for pinname to GPIO", // WICONNECT_PINNAME_TO_GPIO_NO_MAPPING + "Not connected", // WICONNECT_NOT_CONNECTED + "Underflow", // WICONNECT_UNDERFLOW + }; + + if((int)wiconnectResult >= (int)WICONNECT_SUCCESS) + { + return wiconnectSuccessStrTable[wiconnectResult]; + } + else + { + wiconnectResult = (WiconnectResult)(-((int)wiconnectResult)); + return wiconnectErrorStrTable[wiconnectResult]; + } +} + + +/*************************************************************************************************/ +void Wiconnect::setDebugLogger(LogFunc logFunc) +{ + debugLogger = logFunc; +} + +/*************************************************************************************************/ +void Wiconnect::debugLog(const char *msg, ...) +{ + if(!debugLogger.isValid()) + { + return; + } + + char buffer[96]; + va_list args; + va_start(args, msg); + int len = vsnprintf(buffer, sizeof(buffer), msg, args); + va_end(args); + + if(len > (int)(sizeof(buffer)-6)) + { + char *p = &buffer[sizeof(buffer)-6]; + *p++ = '.'; + *p++ = '.'; + *p++ = '.'; + *p++ = '\r'; + *p++ = '\n'; + *p = 0; + } + else + { + if(buffer[len-2] != '\r') + { + char *p = &buffer[len]; + *p++ = '\r'; + *p++ = '\n'; + *p = 0; + } + } + debugLogger.call(buffer); +} +