Integrating the ublox LISA C200 modem
Fork of SprintUSBModemHTTPClientTest by
SprintUSBModem/Socket/Socket.cpp@13:c920eb6774de, 2013-10-01 (annotated)
- Committer:
- sam_grove
- Date:
- Tue Oct 01 21:39:27 2013 +0000
- Revision:
- 13:c920eb6774de
- Parent:
- 5:3f93dd1d4cb3
Tested a few different URL's
Who changed what in which revision?
User | Revision | Line number | New 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 "Socket/Socket.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 | |
sam_grove | 5:3f93dd1d4cb3 | 23 | Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) { |
sam_grove | 5:3f93dd1d4cb3 | 24 | |
sam_grove | 5:3f93dd1d4cb3 | 25 | } |
sam_grove | 5:3f93dd1d4cb3 | 26 | |
sam_grove | 5:3f93dd1d4cb3 | 27 | void Socket::set_blocking(bool blocking, unsigned int timeout) { |
sam_grove | 5:3f93dd1d4cb3 | 28 | _blocking = blocking; |
sam_grove | 5:3f93dd1d4cb3 | 29 | _timeout = timeout; |
sam_grove | 5:3f93dd1d4cb3 | 30 | } |
sam_grove | 5:3f93dd1d4cb3 | 31 | |
sam_grove | 5:3f93dd1d4cb3 | 32 | int Socket::init_socket(int type) { |
sam_grove | 5:3f93dd1d4cb3 | 33 | if (_sock_fd != -1) |
sam_grove | 5:3f93dd1d4cb3 | 34 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 35 | |
sam_grove | 5:3f93dd1d4cb3 | 36 | int fd = lwip_socket(AF_INET, type, 0); |
sam_grove | 5:3f93dd1d4cb3 | 37 | if (fd < 0) |
sam_grove | 5:3f93dd1d4cb3 | 38 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 39 | |
sam_grove | 5:3f93dd1d4cb3 | 40 | _sock_fd = fd; |
sam_grove | 5:3f93dd1d4cb3 | 41 | return 0; |
sam_grove | 5:3f93dd1d4cb3 | 42 | } |
sam_grove | 5:3f93dd1d4cb3 | 43 | |
sam_grove | 5:3f93dd1d4cb3 | 44 | int Socket::select(struct timeval *timeout, bool read, bool write) { |
sam_grove | 5:3f93dd1d4cb3 | 45 | fd_set fdSet; |
sam_grove | 5:3f93dd1d4cb3 | 46 | FD_ZERO(&fdSet); |
sam_grove | 5:3f93dd1d4cb3 | 47 | FD_SET(_sock_fd, &fdSet); |
sam_grove | 5:3f93dd1d4cb3 | 48 | |
sam_grove | 5:3f93dd1d4cb3 | 49 | fd_set* readset = (read ) ? (&fdSet) : (NULL); |
sam_grove | 5:3f93dd1d4cb3 | 50 | fd_set* writeset = (write) ? (&fdSet) : (NULL); |
sam_grove | 5:3f93dd1d4cb3 | 51 | |
sam_grove | 5:3f93dd1d4cb3 | 52 | int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout); |
sam_grove | 5:3f93dd1d4cb3 | 53 | return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); |
sam_grove | 5:3f93dd1d4cb3 | 54 | } |
sam_grove | 5:3f93dd1d4cb3 | 55 | |
sam_grove | 5:3f93dd1d4cb3 | 56 | int Socket::wait_readable(TimeInterval& timeout) { |
sam_grove | 5:3f93dd1d4cb3 | 57 | return select(&timeout._time, true, false); |
sam_grove | 5:3f93dd1d4cb3 | 58 | } |
sam_grove | 5:3f93dd1d4cb3 | 59 | |
sam_grove | 5:3f93dd1d4cb3 | 60 | int Socket::wait_writable(TimeInterval& timeout) { |
sam_grove | 5:3f93dd1d4cb3 | 61 | return select(&timeout._time, false, true); |
sam_grove | 5:3f93dd1d4cb3 | 62 | } |
sam_grove | 5:3f93dd1d4cb3 | 63 | |
sam_grove | 5:3f93dd1d4cb3 | 64 | int Socket::close() { |
sam_grove | 5:3f93dd1d4cb3 | 65 | if (_sock_fd < 0) |
sam_grove | 5:3f93dd1d4cb3 | 66 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 67 | |
sam_grove | 5:3f93dd1d4cb3 | 68 | lwip_close(_sock_fd); |
sam_grove | 5:3f93dd1d4cb3 | 69 | _sock_fd = -1; |
sam_grove | 5:3f93dd1d4cb3 | 70 | |
sam_grove | 5:3f93dd1d4cb3 | 71 | return 0; |
sam_grove | 5:3f93dd1d4cb3 | 72 | } |
sam_grove | 5:3f93dd1d4cb3 | 73 | |
sam_grove | 5:3f93dd1d4cb3 | 74 | Socket::~Socket() { |
sam_grove | 5:3f93dd1d4cb3 | 75 | close(); //Don't want to leak |
sam_grove | 5:3f93dd1d4cb3 | 76 | } |
sam_grove | 5:3f93dd1d4cb3 | 77 | |
sam_grove | 5:3f93dd1d4cb3 | 78 | TimeInterval::TimeInterval(unsigned int ms) { |
sam_grove | 5:3f93dd1d4cb3 | 79 | _time.tv_sec = ms / 1000; |
sam_grove | 5:3f93dd1d4cb3 | 80 | _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; |
sam_grove | 5:3f93dd1d4cb3 | 81 | } |