Integrating the ublox LISA C200 modem
Fork of SprintUSBModemHTTPClientTest by
SprintUSBModem/Socket/UDPSocket.cpp@5:3f93dd1d4cb3, 2013-09-26 (annotated)
- 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?
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 | |
sam_grove | 5:3f93dd1d4cb3 | 19 | #include "Socket/UDPSocket.h" |
sam_grove | 5:3f93dd1d4cb3 | 20 | |
sam_grove | 5:3f93dd1d4cb3 | 21 | #include <cstring> |
sam_grove | 5:3f93dd1d4cb3 | 22 | |
sam_grove | 5:3f93dd1d4cb3 | 23 | using std::memset; |
sam_grove | 5:3f93dd1d4cb3 | 24 | |
sam_grove | 5:3f93dd1d4cb3 | 25 | UDPSocket::UDPSocket() { |
sam_grove | 5:3f93dd1d4cb3 | 26 | } |
sam_grove | 5:3f93dd1d4cb3 | 27 | |
sam_grove | 5:3f93dd1d4cb3 | 28 | int UDPSocket::init(void) { |
sam_grove | 5:3f93dd1d4cb3 | 29 | return init_socket(SOCK_DGRAM); |
sam_grove | 5:3f93dd1d4cb3 | 30 | } |
sam_grove | 5:3f93dd1d4cb3 | 31 | |
sam_grove | 5:3f93dd1d4cb3 | 32 | // Server initialization |
sam_grove | 5:3f93dd1d4cb3 | 33 | int UDPSocket::bind(int port) { |
sam_grove | 5:3f93dd1d4cb3 | 34 | if (init_socket(SOCK_DGRAM) < 0) |
sam_grove | 5:3f93dd1d4cb3 | 35 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 36 | |
sam_grove | 5:3f93dd1d4cb3 | 37 | struct sockaddr_in localHost; |
sam_grove | 5:3f93dd1d4cb3 | 38 | std::memset(&localHost, 0, sizeof(localHost)); |
sam_grove | 5:3f93dd1d4cb3 | 39 | |
sam_grove | 5:3f93dd1d4cb3 | 40 | localHost.sin_family = AF_INET; |
sam_grove | 5:3f93dd1d4cb3 | 41 | localHost.sin_port = htons(port); |
sam_grove | 5:3f93dd1d4cb3 | 42 | localHost.sin_addr.s_addr = INADDR_ANY; |
sam_grove | 5:3f93dd1d4cb3 | 43 | |
sam_grove | 5:3f93dd1d4cb3 | 44 | if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) { |
sam_grove | 5:3f93dd1d4cb3 | 45 | close(); |
sam_grove | 5:3f93dd1d4cb3 | 46 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 47 | } |
sam_grove | 5:3f93dd1d4cb3 | 48 | |
sam_grove | 5:3f93dd1d4cb3 | 49 | return 0; |
sam_grove | 5:3f93dd1d4cb3 | 50 | } |
sam_grove | 5:3f93dd1d4cb3 | 51 | |
sam_grove | 5:3f93dd1d4cb3 | 52 | // -1 if unsuccessful, else number of bytes written |
sam_grove | 5:3f93dd1d4cb3 | 53 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) { |
sam_grove | 5:3f93dd1d4cb3 | 54 | if (_sock_fd < 0) |
sam_grove | 5:3f93dd1d4cb3 | 55 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 56 | |
sam_grove | 5:3f93dd1d4cb3 | 57 | if (!_blocking) { |
sam_grove | 5:3f93dd1d4cb3 | 58 | TimeInterval timeout(_timeout); |
sam_grove | 5:3f93dd1d4cb3 | 59 | if (wait_writable(timeout) != 0) |
sam_grove | 5:3f93dd1d4cb3 | 60 | return 0; |
sam_grove | 5:3f93dd1d4cb3 | 61 | } |
sam_grove | 5:3f93dd1d4cb3 | 62 | |
sam_grove | 5:3f93dd1d4cb3 | 63 | return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost)); |
sam_grove | 5:3f93dd1d4cb3 | 64 | } |
sam_grove | 5:3f93dd1d4cb3 | 65 | |
sam_grove | 5:3f93dd1d4cb3 | 66 | // -1 if unsuccessful, else number of bytes received |
sam_grove | 5:3f93dd1d4cb3 | 67 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) { |
sam_grove | 5:3f93dd1d4cb3 | 68 | if (_sock_fd < 0) |
sam_grove | 5:3f93dd1d4cb3 | 69 | return -1; |
sam_grove | 5:3f93dd1d4cb3 | 70 | |
sam_grove | 5:3f93dd1d4cb3 | 71 | if (!_blocking) { |
sam_grove | 5:3f93dd1d4cb3 | 72 | TimeInterval timeout(_timeout); |
sam_grove | 5:3f93dd1d4cb3 | 73 | if (wait_readable(timeout) != 0) |
sam_grove | 5:3f93dd1d4cb3 | 74 | return 0; |
sam_grove | 5:3f93dd1d4cb3 | 75 | } |
sam_grove | 5:3f93dd1d4cb3 | 76 | remote.reset_address(); |
sam_grove | 5:3f93dd1d4cb3 | 77 | socklen_t remoteHostLen = sizeof(remote._remoteHost); |
sam_grove | 5:3f93dd1d4cb3 | 78 | return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen); |
sam_grove | 5:3f93dd1d4cb3 | 79 | } |