Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /* Copyright (C) 2012 mbed.org, MIT License
sam_grove 5:3f93dd1d4cb3 2 *
sam_grove 5:3f93dd1d4cb3 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sam_grove 5:3f93dd1d4cb3 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
sam_grove 5:3f93dd1d4cb3 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
sam_grove 5:3f93dd1d4cb3 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
sam_grove 5:3f93dd1d4cb3 7 * furnished to do so, subject to the following conditions:
sam_grove 5:3f93dd1d4cb3 8 *
sam_grove 5:3f93dd1d4cb3 9 * The above copyright notice and this permission notice shall be included in all copies or
sam_grove 5:3f93dd1d4cb3 10 * substantial portions of the Software.
sam_grove 5:3f93dd1d4cb3 11 *
sam_grove 5:3f93dd1d4cb3 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sam_grove 5:3f93dd1d4cb3 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sam_grove 5:3f93dd1d4cb3 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sam_grove 5:3f93dd1d4cb3 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sam_grove 5:3f93dd1d4cb3 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sam_grove 5:3f93dd1d4cb3 17 */
sam_grove 5:3f93dd1d4cb3 18 #include "TCPSocketConnection.h"
sam_grove 5:3f93dd1d4cb3 19 #include <cstring>
sam_grove 5:3f93dd1d4cb3 20
sam_grove 5:3f93dd1d4cb3 21 using std::memset;
sam_grove 5:3f93dd1d4cb3 22 using std::memcpy;
sam_grove 5:3f93dd1d4cb3 23
sam_grove 5:3f93dd1d4cb3 24 TCPSocketConnection::TCPSocketConnection() :
sam_grove 5:3f93dd1d4cb3 25 _is_connected(false) {
sam_grove 5:3f93dd1d4cb3 26 }
sam_grove 5:3f93dd1d4cb3 27
sam_grove 5:3f93dd1d4cb3 28 int TCPSocketConnection::connect(const char* host, const int port) {
sam_grove 5:3f93dd1d4cb3 29 if (init_socket(SOCK_STREAM) < 0)
sam_grove 5:3f93dd1d4cb3 30 return -1;
sam_grove 5:3f93dd1d4cb3 31
sam_grove 5:3f93dd1d4cb3 32 if (set_address(host, port) != 0)
sam_grove 5:3f93dd1d4cb3 33 return -1;
sam_grove 5:3f93dd1d4cb3 34
sam_grove 5:3f93dd1d4cb3 35 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
sam_grove 5:3f93dd1d4cb3 36 close();
sam_grove 5:3f93dd1d4cb3 37 return -1;
sam_grove 5:3f93dd1d4cb3 38 }
sam_grove 5:3f93dd1d4cb3 39 _is_connected = true;
sam_grove 5:3f93dd1d4cb3 40
sam_grove 5:3f93dd1d4cb3 41 return 0;
sam_grove 5:3f93dd1d4cb3 42 }
sam_grove 5:3f93dd1d4cb3 43
sam_grove 5:3f93dd1d4cb3 44 bool TCPSocketConnection::is_connected(void) {
sam_grove 5:3f93dd1d4cb3 45 return _is_connected;
sam_grove 5:3f93dd1d4cb3 46 }
sam_grove 5:3f93dd1d4cb3 47
sam_grove 5:3f93dd1d4cb3 48 int TCPSocketConnection::send(char* data, int length) {
sam_grove 5:3f93dd1d4cb3 49 if ((_sock_fd < 0) || !_is_connected)
sam_grove 5:3f93dd1d4cb3 50 return -1;
sam_grove 5:3f93dd1d4cb3 51
sam_grove 5:3f93dd1d4cb3 52 if (!_blocking) {
sam_grove 5:3f93dd1d4cb3 53 TimeInterval timeout(_timeout);
sam_grove 5:3f93dd1d4cb3 54 if (wait_writable(timeout) != 0)
sam_grove 5:3f93dd1d4cb3 55 return -1;
sam_grove 5:3f93dd1d4cb3 56 }
sam_grove 5:3f93dd1d4cb3 57
sam_grove 5:3f93dd1d4cb3 58 int n = lwip_send(_sock_fd, data, length, 0);
sam_grove 5:3f93dd1d4cb3 59 _is_connected = (n != 0);
sam_grove 5:3f93dd1d4cb3 60
sam_grove 5:3f93dd1d4cb3 61 return n;
sam_grove 5:3f93dd1d4cb3 62 }
sam_grove 5:3f93dd1d4cb3 63
sam_grove 5:3f93dd1d4cb3 64 // -1 if unsuccessful, else number of bytes written
sam_grove 5:3f93dd1d4cb3 65 int TCPSocketConnection::send_all(char* data, int length) {
sam_grove 5:3f93dd1d4cb3 66 if ((_sock_fd < 0) || !_is_connected)
sam_grove 5:3f93dd1d4cb3 67 return -1;
sam_grove 5:3f93dd1d4cb3 68
sam_grove 5:3f93dd1d4cb3 69 size_t writtenLen = 0;
sam_grove 5:3f93dd1d4cb3 70 TimeInterval timeout(_timeout);
sam_grove 5:3f93dd1d4cb3 71 while (writtenLen < length) {
sam_grove 5:3f93dd1d4cb3 72 if (!_blocking) {
sam_grove 5:3f93dd1d4cb3 73 // Wait for socket to be writeable
sam_grove 5:3f93dd1d4cb3 74 if (wait_writable(timeout) != 0)
sam_grove 5:3f93dd1d4cb3 75 return writtenLen;
sam_grove 5:3f93dd1d4cb3 76 }
sam_grove 5:3f93dd1d4cb3 77
sam_grove 5:3f93dd1d4cb3 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
sam_grove 5:3f93dd1d4cb3 79 if (ret > 0) {
sam_grove 5:3f93dd1d4cb3 80 writtenLen += ret;
sam_grove 5:3f93dd1d4cb3 81 continue;
sam_grove 5:3f93dd1d4cb3 82 } else if (ret == 0) {
sam_grove 5:3f93dd1d4cb3 83 _is_connected = false;
sam_grove 5:3f93dd1d4cb3 84 return writtenLen;
sam_grove 5:3f93dd1d4cb3 85 } else {
sam_grove 5:3f93dd1d4cb3 86 return -1; //Connnection error
sam_grove 5:3f93dd1d4cb3 87 }
sam_grove 5:3f93dd1d4cb3 88 }
sam_grove 5:3f93dd1d4cb3 89 return writtenLen;
sam_grove 5:3f93dd1d4cb3 90 }
sam_grove 5:3f93dd1d4cb3 91
sam_grove 5:3f93dd1d4cb3 92 int TCPSocketConnection::receive(char* data, int length) {
sam_grove 5:3f93dd1d4cb3 93 if ((_sock_fd < 0) || !_is_connected)
sam_grove 5:3f93dd1d4cb3 94 return -1;
sam_grove 5:3f93dd1d4cb3 95
sam_grove 5:3f93dd1d4cb3 96 if (!_blocking) {
sam_grove 5:3f93dd1d4cb3 97 TimeInterval timeout(_timeout);
sam_grove 5:3f93dd1d4cb3 98 if (wait_readable(timeout) != 0)
sam_grove 5:3f93dd1d4cb3 99 return -1;
sam_grove 5:3f93dd1d4cb3 100 }
sam_grove 5:3f93dd1d4cb3 101
sam_grove 5:3f93dd1d4cb3 102 int n = lwip_recv(_sock_fd, data, length, 0);
sam_grove 5:3f93dd1d4cb3 103 _is_connected = (n != 0);
sam_grove 5:3f93dd1d4cb3 104
sam_grove 5:3f93dd1d4cb3 105 return n;
sam_grove 5:3f93dd1d4cb3 106 }
sam_grove 5:3f93dd1d4cb3 107
sam_grove 5:3f93dd1d4cb3 108 // -1 if unsuccessful, else number of bytes received
sam_grove 5:3f93dd1d4cb3 109 int TCPSocketConnection::receive_all(char* data, int length) {
sam_grove 5:3f93dd1d4cb3 110 if ((_sock_fd < 0) || !_is_connected)
sam_grove 5:3f93dd1d4cb3 111 return -1;
sam_grove 5:3f93dd1d4cb3 112
sam_grove 5:3f93dd1d4cb3 113 size_t readLen = 0;
sam_grove 5:3f93dd1d4cb3 114 TimeInterval timeout(_timeout);
sam_grove 5:3f93dd1d4cb3 115 while (readLen < length) {
sam_grove 5:3f93dd1d4cb3 116 if (!_blocking) {
sam_grove 5:3f93dd1d4cb3 117 //Wait for socket to be readable
sam_grove 5:3f93dd1d4cb3 118 if (wait_readable(timeout) != 0)
sam_grove 5:3f93dd1d4cb3 119 return readLen;
sam_grove 5:3f93dd1d4cb3 120 }
sam_grove 5:3f93dd1d4cb3 121
sam_grove 5:3f93dd1d4cb3 122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
sam_grove 5:3f93dd1d4cb3 123 if (ret > 0) {
sam_grove 5:3f93dd1d4cb3 124 readLen += ret;
sam_grove 5:3f93dd1d4cb3 125 } else if (ret == 0) {
sam_grove 5:3f93dd1d4cb3 126 _is_connected = false;
sam_grove 5:3f93dd1d4cb3 127 return readLen;
sam_grove 5:3f93dd1d4cb3 128 } else {
sam_grove 5:3f93dd1d4cb3 129 return -1; //Connnection error
sam_grove 5:3f93dd1d4cb3 130 }
sam_grove 5:3f93dd1d4cb3 131 }
sam_grove 5:3f93dd1d4cb3 132 return readLen;
sam_grove 5:3f93dd1d4cb3 133 }