cc3000 hostdriver with the mbed socket interface
Fork of cc3000_hostdriver_mbedsocket by
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 5:245ac5b73132
- Parent:
- 3:ad95e296bfbf
- Child:
- 11:5e3771b29385
--- a/Socket/TCPSocketConnection.cpp Sat Sep 21 15:01:05 2013 +0000 +++ b/Socket/TCPSocketConnection.cpp Thu Sep 26 19:50:37 2013 +0000 @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 mbed.org, MIT License +/* Copyright (C) 2013 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, @@ -19,38 +19,133 @@ #include "TCPSocketConnection.h" #include <algorithm> -TCPSocketConnection::TCPSocketConnection() {} - -int TCPSocketConnection::connect(const char* host, const int port) -{ - +TCPSocketConnection::TCPSocketConnection() : _is_connected(false) { + _cc3000_module = cc3000::get_instance(); + if (_cc3000_module == NULL) { + error("Endpoint constructor error: no cc3000 instance available!\r\n"); + } } -bool TCPSocketConnection::is_connected(void) -{ +int TCPSocketConnection::connect(const char *host, const int port) { + if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: Failed to create tcp socket.\n"); +#endif + return -1; + } + if (set_address(host, port) != 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: Failed to set address (tcp).\n"); +#endif + return -1; + } + + if (_cc3000_module->_socket.connect(_sock_fd, (const sockaddr *)&_remote_host, sizeof(_remote_host)) < 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: Failed to connect (tcp).\n"); +#endif + close(); + return -1; + } + + _is_connected = true; + + return 0; } -int TCPSocketConnection::send(char* data, int length) -{ +bool TCPSocketConnection::is_connected(void) { + return _is_connected; +} + +int TCPSocketConnection::send(char* data, int length) { + if ((_sock_fd < 0) || !_is_connected) { + return -1; + } + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_writable(timeout) != 0) { + return -1; + } + } + + int n = _cc3000_module->_socket.send(_sock_fd, data, length, 0); + _is_connected = (n != 0); + + return n; } -// -1 if unsuccessful, else number of bytes written -int TCPSocketConnection::send_all(char* data, int length) -{ +int TCPSocketConnection::send_all(char *data, int length) { + if ((_sock_fd < 0) || !_is_connected) { + return -1; + } + int writtenLen = 0; + TimeInterval timeout(_timeout); + while (writtenLen < length) { + if (!_blocking) { + // Wait for socket to be writeable + if (wait_writable(timeout) != 0) { + return writtenLen; + } + } + + int ret = _cc3000_module->_socket.send(_sock_fd, data + writtenLen, length - writtenLen, 0); + if (ret > 0) { + writtenLen += ret; + continue; + } else if (ret == 0) { + _is_connected = false; + return writtenLen; + } else { + return -1; //Connnection error + } + } + + return writtenLen; } -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive(char* data, int length) -{ +int TCPSocketConnection::receive(char *data, int length) { + if ((_sock_fd < 0) || !_is_connected) { + return -1; + } + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_readable(timeout) != 0) + return -1; + } + + int n = _cc3000_module->_socket.recv(_sock_fd, data, length, 0); + _is_connected = (n != 0); + + return n; } +int TCPSocketConnection::receive_all(char *data, int length) { + if ((_sock_fd < 0) || !_is_connected) { + return -1; + } -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length) -{ + int readLen = 0; + TimeInterval timeout(_timeout); + while (readLen < length) { + if (!_blocking) { + //Wait for socket to be readable + if (wait_readable(timeout) != 0) + return readLen; + } + int ret = _cc3000_module->_socket.recv(_sock_fd, data + readLen, length - readLen, 0); + if (ret > 0) { + readLen += ret; + } else if (ret == 0) { + _is_connected = false; + return readLen; + } else { + return -1; //Connnection error + } + } + return readLen; }