C027_SupportTest_xively_locationで使用しているC027用ライブラリ

Fork of C027_Support by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.h Source File

UDPSocket.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #ifndef UDPSOCKET_H
00020 #define UDPSOCKET_H
00021 
00022 #include "Socket/Socket.h"
00023 #include "Socket/Endpoint.h"
00024 
00025 /**
00026 UDP Socket
00027 */
00028 class UDPSocket : public Socket {
00029 
00030 public:
00031     int init(void) 
00032     {
00033         if (_mdm == NULL)
00034             _mdm = MDMParser::getInstance();
00035         if (_mdm == NULL)
00036             return -1;
00037         return 0;
00038     }
00039     
00040     int bind(int port) {
00041         if (_socket < 0) {
00042             _socket = _mdm->socketSocket(MDMParser::IPPROTO_UDP, port);
00043             if (_socket < 0) {
00044                 return -1;
00045             }
00046         }
00047         _mdm->socketSetBlocking(_socket, _timeout_ms); 
00048         return 0;
00049     }
00050     
00051     int join_multicast_group(const char* address)   { return -1; }
00052     
00053     int set_broadcasting(bool broadcast=true)       { return -1; }
00054     
00055     int sendTo(Endpoint &remote, char *packet, int length)
00056     {
00057         char* str = remote.get_address();
00058         int port = remote.get_port();
00059         MDMParser::IP ip = _mdm->gethostbyname(str);
00060         if (ip == NOIP)
00061             return -1;
00062         return _mdm->socketSendTo(_socket, ip, port, packet, length); 
00063     }
00064     
00065     int receiveFrom(Endpoint &remote, char *buffer, int length)
00066     { 
00067         MDMParser::IP ip;
00068         int port;
00069         int ret = _mdm->socketRecvFrom(_socket, &ip, &port, buffer, length); 
00070         if (ret >= 0) {
00071             char str[17];
00072             sprintf(str, IPSTR, IPNUM(ip));
00073             remote.set_address(str, port);
00074         }
00075         return ret;
00076     }
00077 };
00078 
00079 #endif