Axeda Corp
/
AxedaGo-ubloxC027
Axeda demo software for u-blox C027 (GSM)
C027_Support/Socket/UDPSocket.h@0:a725e8eab383, 2014-08-11 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Aug 11 19:02:42 2014 +0000
- Revision:
- 0:a725e8eab383
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:a725e8eab383 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
AxedaCorp | 0:a725e8eab383 | 2 | * |
AxedaCorp | 0:a725e8eab383 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
AxedaCorp | 0:a725e8eab383 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
AxedaCorp | 0:a725e8eab383 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
AxedaCorp | 0:a725e8eab383 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
AxedaCorp | 0:a725e8eab383 | 7 | * furnished to do so, subject to the following conditions: |
AxedaCorp | 0:a725e8eab383 | 8 | * |
AxedaCorp | 0:a725e8eab383 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
AxedaCorp | 0:a725e8eab383 | 10 | * substantial portions of the Software. |
AxedaCorp | 0:a725e8eab383 | 11 | * |
AxedaCorp | 0:a725e8eab383 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
AxedaCorp | 0:a725e8eab383 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
AxedaCorp | 0:a725e8eab383 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
AxedaCorp | 0:a725e8eab383 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AxedaCorp | 0:a725e8eab383 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
AxedaCorp | 0:a725e8eab383 | 17 | */ |
AxedaCorp | 0:a725e8eab383 | 18 | |
AxedaCorp | 0:a725e8eab383 | 19 | #ifndef UDPSOCKET_H |
AxedaCorp | 0:a725e8eab383 | 20 | #define UDPSOCKET_H |
AxedaCorp | 0:a725e8eab383 | 21 | |
AxedaCorp | 0:a725e8eab383 | 22 | #include "Socket/Socket.h" |
AxedaCorp | 0:a725e8eab383 | 23 | #include "Socket/Endpoint.h" |
AxedaCorp | 0:a725e8eab383 | 24 | |
AxedaCorp | 0:a725e8eab383 | 25 | /** |
AxedaCorp | 0:a725e8eab383 | 26 | UDP Socket |
AxedaCorp | 0:a725e8eab383 | 27 | */ |
AxedaCorp | 0:a725e8eab383 | 28 | class UDPSocket : public Socket { |
AxedaCorp | 0:a725e8eab383 | 29 | |
AxedaCorp | 0:a725e8eab383 | 30 | public: |
AxedaCorp | 0:a725e8eab383 | 31 | int init(void) |
AxedaCorp | 0:a725e8eab383 | 32 | { |
AxedaCorp | 0:a725e8eab383 | 33 | if (_mdm == NULL) |
AxedaCorp | 0:a725e8eab383 | 34 | _mdm = MDMParser::getInstance(); |
AxedaCorp | 0:a725e8eab383 | 35 | if (_mdm == NULL) |
AxedaCorp | 0:a725e8eab383 | 36 | return -1; |
AxedaCorp | 0:a725e8eab383 | 37 | return 0; |
AxedaCorp | 0:a725e8eab383 | 38 | } |
AxedaCorp | 0:a725e8eab383 | 39 | |
AxedaCorp | 0:a725e8eab383 | 40 | int bind(int port) { |
AxedaCorp | 0:a725e8eab383 | 41 | if (_socket < 0) { |
AxedaCorp | 0:a725e8eab383 | 42 | _socket = _mdm->socketSocket(MDMParser::IPPROTO_UDP, port); |
AxedaCorp | 0:a725e8eab383 | 43 | if (_socket < 0) { |
AxedaCorp | 0:a725e8eab383 | 44 | return -1; |
AxedaCorp | 0:a725e8eab383 | 45 | } |
AxedaCorp | 0:a725e8eab383 | 46 | } |
AxedaCorp | 0:a725e8eab383 | 47 | _mdm->socketSetBlocking(_socket, _timeout_ms); |
AxedaCorp | 0:a725e8eab383 | 48 | return 0; |
AxedaCorp | 0:a725e8eab383 | 49 | } |
AxedaCorp | 0:a725e8eab383 | 50 | |
AxedaCorp | 0:a725e8eab383 | 51 | int join_multicast_group(const char* address) { return -1; } |
AxedaCorp | 0:a725e8eab383 | 52 | |
AxedaCorp | 0:a725e8eab383 | 53 | int set_broadcasting(bool broadcast=true) { return -1; } |
AxedaCorp | 0:a725e8eab383 | 54 | |
AxedaCorp | 0:a725e8eab383 | 55 | int sendTo(Endpoint &remote, char *packet, int length) |
AxedaCorp | 0:a725e8eab383 | 56 | { |
AxedaCorp | 0:a725e8eab383 | 57 | char* str = remote.get_address(); |
AxedaCorp | 0:a725e8eab383 | 58 | int port = remote.get_port(); |
AxedaCorp | 0:a725e8eab383 | 59 | MDMParser::IP ip = _mdm->gethostbyname(str); |
AxedaCorp | 0:a725e8eab383 | 60 | if (ip == NOIP) |
AxedaCorp | 0:a725e8eab383 | 61 | return -1; |
AxedaCorp | 0:a725e8eab383 | 62 | return _mdm->socketSendTo(_socket, ip, port, packet, length); |
AxedaCorp | 0:a725e8eab383 | 63 | } |
AxedaCorp | 0:a725e8eab383 | 64 | |
AxedaCorp | 0:a725e8eab383 | 65 | int receiveFrom(Endpoint &remote, char *buffer, int length) |
AxedaCorp | 0:a725e8eab383 | 66 | { |
AxedaCorp | 0:a725e8eab383 | 67 | MDMParser::IP ip; |
AxedaCorp | 0:a725e8eab383 | 68 | int port; |
AxedaCorp | 0:a725e8eab383 | 69 | int ret = _mdm->socketRecvFrom(_socket, &ip, &port, buffer, length); |
AxedaCorp | 0:a725e8eab383 | 70 | if (ret >= 0) { |
AxedaCorp | 0:a725e8eab383 | 71 | char str[17]; |
AxedaCorp | 0:a725e8eab383 | 72 | sprintf(str, IPSTR, IPNUM(ip)); |
AxedaCorp | 0:a725e8eab383 | 73 | remote.set_address(str, port); |
AxedaCorp | 0:a725e8eab383 | 74 | } |
AxedaCorp | 0:a725e8eab383 | 75 | return ret; |
AxedaCorp | 0:a725e8eab383 | 76 | } |
AxedaCorp | 0:a725e8eab383 | 77 | }; |
AxedaCorp | 0:a725e8eab383 | 78 | |
AxedaCorp | 0:a725e8eab383 | 79 | #endif |