Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LNetworkUdp.h Source File

LNetworkUdp.h

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 #ifndef NETWORKUDP_H_
00018 #define NETWORKUDP_H_
00019 
00020 #include <sys/time.h>
00021 #include <iostream>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <netdb.h>
00026 #include <unistd.h>
00027 #include <string>
00028 #include <arpa/inet.h>
00029 
00030 #include "LMqttsnClientApp.h"
00031 
00032 #define SOCKET_MAXHOSTNAME  200
00033 #define SOCKET_MAXCONNECTIONS  5
00034 #define SOCKET_MAXRECV  500
00035 #define SOCKET_MAXBUFFER_LENGTH 500 // buffer size
00036 
00037 #define STAT_UNICAST   1
00038 #define STAT_MULTICAST 2
00039 
00040 using namespace std;
00041 
00042 namespace linuxAsyncClient {
00043 /*========================================
00044        Class LUpdPort
00045  =======================================*/
00046 class LUdpPort{
00047     friend class LNetwork;
00048 public:
00049     LUdpPort();
00050     virtual ~LUdpPort();
00051 
00052     bool open(LUdpConfig config);
00053 
00054     int unicast(const uint8_t* buf, uint32_t length, uint32_t ipaddress, uint16_t port  );
00055     int multicast( const uint8_t* buf, uint32_t length );
00056     int recv(uint8_t* buf, uint16_t len, bool nonblock, uint32_t* ipaddress, uint16_t* port );
00057     int recv(uint8_t* buf, int flags);
00058     bool checkRecvBuf();
00059     bool isUnicast();
00060 
00061 private:
00062     void close();
00063     int recvfrom ( uint8_t* buf, uint16_t len, int flags, uint32_t* ipaddress, uint16_t* port );
00064 
00065     int      _sockfdUcast;
00066     int      _sockfdMcast;
00067     uint16_t _gPortNo;
00068     uint16_t _uPortNo;
00069     uint32_t _gIpAddr;
00070     uint8_t  _castStat;
00071     bool   _disconReq;
00072 
00073 };
00074 
00075 #define NO_ERROR    0
00076 #define PACKET_EXCEEDS_LENGTH  1
00077 /*===========================================
00078                Class  Network
00079  ============================================*/
00080 class LNetwork : public LUdpPort {
00081 public:
00082     LNetwork();
00083     ~LNetwork();
00084 
00085     int  broadcast(const uint8_t* payload, uint16_t payloadLen);
00086     int  unicast(const uint8_t* payload, uint16_t payloadLen);
00087     void setGwAddress(void);
00088     void resetGwAddress(void);
00089     void setFixedGwAddress(void);
00090     bool initialize(LUdpConfig  config);
00091     uint8_t*  getMessage(int* len);
00092 private:
00093     void setSleep();
00094     int  readApiFrame(void);
00095 
00096     uint32_t _gwIpAddress;
00097     uint16_t _gwPortNo;
00098     uint32_t _ipAddress;
00099     uint16_t _portNo;
00100     int     _returnCode;
00101     bool _sleepflg;
00102     uint8_t _rxDataBuf[MQTTSN_MAX_PACKET_SIZE + 1];  // defined in MqttsnClientApp.h
00103 
00104 };
00105 
00106 
00107 }    /* end of namespace */
00108 #endif /* NETWORKUDP_H_ */