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
Revision 12:3dfec824715a, committed 2014-08-12
- Comitter:
- dan_ackme
- Date:
- Tue Aug 12 02:44:34 2014 -0700
- Parent:
- 11:ea484e1b7fc4
- Child:
- 13:2b51f5267c92
- Commit message:
- fixed build error
Changed in this revision
internal/network/socket/SocketInterface.cpp | Show diff for this revision Revisions of this file |
--- a/internal/network/socket/SocketInterface.cpp Tue Aug 12 02:34:46 2014 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,261 +0,0 @@ -/* - * 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 "Wiconnect.h" -#include "internal/common.h" -#include "StringUtil.h" - -#include "types/SocketIrqHandlerMap.h" - - - - -/*************************************************************************************************/ -SocketInterface::SocketInterface(Wiconnect *wiconnect_) -{ - wiconnect = wiconnect_; -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::connect(Socket &socket, SocketType type, const char *host, uint16_t remortPort, uint16_t localPort, const void *args, Pin irqPin) -{ - WiconnectResult result; - int32_t handle; - char cmdBuffer[WICONNECT_MAX_CMD_SIZE]; - - if(WICONNECT_IS_IDLE()) - { - char gpioOption[8] = ""; - - if(irqPin != NC) - { - PinToGpioMapper mapper = wiconnect->pinToGpioMapper; - if(mapper == NULL) - { - return WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL; - } - int8_t gpio = mapper(irqPin); - if(gpio == -1) - { - return WICONNECT_PINNAME_TO_GPIO_NO_MAPPING; - } - else if(!irqHandlers.pinIsRegistered(irqPin)) - { - return WICONNECT_NOT_FOUND; - } - - sprintf(gpioOption, "-g %d ", gpio); - } - - - switch(type) - { - case SOCKET_TYPE_TCP: - sprintf(cmdBuffer, "tcpc %s%s %d", gpioOption, host, remortPort); - break; - - case SOCKET_TYPE_UDP: { - char tmp[16]; - sprintf(cmdBuffer, "udpc %s%s %d %s", gpioOption, host, remortPort, - (localPort != SOCKET_ANY_PORT) ? StringUtil::uint32ToStr(tmp, localPort) : ""); - } break; - - case SOCKET_TYPE_TLS: - sprintf(cmdBuffer, "tlsc %s%s %d %s", gpioOption, host, remortPort, - (args != NULL) ? (char*)args : ""); - break; - - case SOCKET_TYPE_HTTP: { - const HttpSocketArgs *httpArgs = (const HttpSocketArgs*)args; - switch(httpArgs->type) - { - case SOCKET_HTTP_GET: - sprintf(cmdBuffer, "http_get %s%s %s", httpArgs->openOnly ? "-o " : "", - host, - (httpArgs->certName != NULL) ? httpArgs->certName : ""); - break; - - case SOCKET_HTTP_HEAD: - sprintf(cmdBuffer, "http_head %s%s %s", httpArgs->openOnly ? "-o " : "", - host, - (httpArgs->certName != NULL) ? httpArgs->certName : ""); - break; - - case SOCKET_HTTP_POST: - sprintf(cmdBuffer, "http_post %s%s %s %s", httpArgs->openOnly ? "-o " : "", - host, - httpArgs->contextType, - (httpArgs->certName != NULL) ? httpArgs->certName : ""); - break; - - default: - return WICONNECT_BAD_ARG; - } - - } break; - default: - return WICONNECT_BAD_ARG; - } - } - - CHECK_OTHER_COMMAND_EXECUTING(); - - if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(cmdBuffer))) - { - if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle))) - { - socket.init(handle, type, host, remortPort, localPort); - } - } - - CHECK_CLEANUP_COMMAND(); - - return result; -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::tcpConnect(Socket &socket, const char *host, uint16_t remortPort, Pin irqPin) -{ - return connect(socket, SOCKET_TYPE_TCP, host, remortPort, SOCKET_ANY_PORT, NULL, irqPin); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::tlsConnect(Socket &socket, const char *host, uint16_t remortPort, const char *certFilename, Pin irqPin) -{ - return connect(socket, SOCKET_TYPE_TLS, host, remortPort, SOCKET_ANY_PORT, certFilename, irqPin); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::udpConnect(Socket &socket, const char *host, uint16_t remortPort, uint16_t localPort, Pin irqPin) -{ - return connect(socket, SOCKET_TYPE_UDP, host, remortPort, localPort, NULL, irqPin); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpConnect(Socket &socket, const char *url, const HttpSocketArgs *args) -{ - return connect(socket, SOCKET_TYPE_HTTP, url, SOCKET_ANY_PORT, SOCKET_ANY_PORT, args, NC); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpGet(Socket &socket, const char *url, bool openOnly, const char *certFilename) -{ - const HttpSocketArgs args = - { - NULL, - certFilename, - openOnly, - SOCKET_HTTP_GET - }; - return httpConnect(socket, url, &args); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpPost(Socket &socket, const char *url, const char *contextType, bool openOnly, const char *certFilename) -{ - const HttpSocketArgs args = - { - contextType, - certFilename, - openOnly, - SOCKET_HTTP_POST - }; - return httpConnect(socket, url, &args); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpHead(Socket &socket, const char *url, const char *certFilename) -{ - const HttpSocketArgs args = - { - NULL, - certFilename, - false, - SOCKET_HTTP_HEAD - }; - return httpConnect(socket, url, &args); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpAddHeader(Socket &socket, const char *key, const char *value) -{ - WiconnectResult result; - char cmdBuffer[WICONNECT_MAX_CMD_SIZE]; - - if(WICONNECT_IS_IDLE()) - { - sprintf(cmdBuffer, "http_add_header %d %s %s", socket.getHandle(), key, value); - } - - CHECK_OTHER_COMMAND_EXECUTING(); - - result = wiconnect->sendCommand(cmdBuffer); - - CHECK_CLEANUP_COMMAND(); - - return result; -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::httpGetStatus(Socket &socket, uint32_t *statusCodePtr) -{ - WiconnectResult result; - - CHECK_OTHER_COMMAND_EXECUTING(); - - result = wiconnect->sendCommand("http_read_status %d", socket.getHandle()); - - CHECK_CLEANUP_COMMAND(); - - return result; -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::closeAllSockets() -{ - WiconnectResult result; - - CHECK_OTHER_COMMAND_EXECUTING(); - - result = wiconnect->sendCommand("close all"); - - CHECK_CLEANUP_COMMAND(); - - return result; -} - - -/*************************************************************************************************/ -WiconnectResult SocketInterface::registerSocketIrqHandler(Pin irqPin, const Callback &handler) -{ - PinToGpioMapper mapper = wiconnect->pinToGpioMapper; - if(irqHandlers.pinIsRegistered(irqPin)) - { - return WICONNECT_DUPLICATE; - } - else if(mapper == NULL) - { - return WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL; - } - int8_t gpio = mapper(irqPin); - if(gpio == -1) - { - return WICONNECT_PINNAME_TO_GPIO_NO_MAPPING; - } - - return irqHandlers.registerHandler(irqPin, handler); -} - -/*************************************************************************************************/ -WiconnectResult SocketInterface::unregisterSocketIrqHandler(Pin irqPin) -{ - return irqHandlers.unregisterHandler(irqPin); -}