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