Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorNetwork.h Source File

SensorNetwork.h

00001 /**************************************************************************************
00002  * Copyright (c) 2017, Benjamin Aigner
00003  * based on UDP implementation (Copyright (c) 2016 Tomoaki Yamaguchi)
00004  *
00005  * All rights reserved. This program and the accompanying materials
00006  * are made available under the terms of the Eclipse Public License v1.0
00007  * and Eclipse Distribution License v1.0 which accompany this distribution.
00008  *
00009  * The Eclipse Public License is available at
00010  *    http://www.eclipse.org/legal/epl-v10.html
00011  * and the Eclipse Distribution License is available at
00012  *   http://www.eclipse.org/org/documents/edl-v10.php.
00013  *
00014  * Contributors:
00015  *    Benjamin Aigner - port to UDPv6, used by RFC7668 (6lowpan over Bluetooth LE)
00016  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00017  **************************************************************************************/
00018 
00019 #ifndef SENSORNETWORK_H_
00020 #define SENSORNETWORK_H_
00021 
00022 #include "MQTTSNGWDefines.h"
00023 #include <arpa/inet.h>
00024 #include <string>
00025 
00026 using namespace std;
00027 
00028 namespace MQTTSNGW
00029 {
00030 
00031 #ifdef  DEBUG_NWSTACK
00032   #define D_NWSTACK(...) printf(__VA_ARGS__)
00033 #else
00034   #define D_NWSTACK(...)
00035 #endif
00036 
00037 /*===========================================
00038  Class  SensorNetAddreess
00039  ============================================*/
00040 class SensorNetAddress
00041 {
00042 public:
00043     SensorNetAddress();
00044     ~SensorNetAddress();
00045     void setAddress(struct sockaddr_in6 *IpAddr, uint16_t port);
00046     int  setAddress(string* data);
00047     int  setAddress(const char* data);
00048     uint16_t getPortNo(void);
00049     struct sockaddr_in6 *getIpAddress(void);
00050     char* getAddress(void);
00051     bool isMatch(SensorNetAddress* addr);
00052     SensorNetAddress& operator =(SensorNetAddress& addr);
00053     char* sprint(char* buf);
00054 private:
00055     uint16_t _portNo;
00056     char _addrString[INET6_ADDRSTRLEN+1];
00057     struct sockaddr_in6 _IpAddr;
00058 };
00059 
00060 /*========================================
00061  Class UpdPort
00062  =======================================*/
00063 class UDPPort6
00064 {
00065 public:
00066     UDPPort6();
00067     virtual ~UDPPort6();
00068 
00069     int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName);
00070     void close(void);
00071     int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
00072     int broadcast(const uint8_t* buf, uint32_t length);
00073     int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr);
00074 
00075 private:
00076     void setNonBlocking(const bool);
00077     int recvfrom(int sockfd, uint8_t* buf, uint16_t len, uint8_t flags, SensorNetAddress* addr);
00078 
00079     int _sockfdUnicast;
00080     int _sockfdMulticast;
00081     char _interfaceName[10];
00082 
00083     SensorNetAddress _grpAddr;
00084     SensorNetAddress _clientAddr;
00085     uint16_t _uniPortNo;
00086     bool _disconReq;
00087 
00088 };
00089 
00090 /*===========================================
00091  Class  SensorNetwork
00092  ============================================*/
00093 class SensorNetwork: public UDPPort6
00094 {
00095 public:
00096     SensorNetwork();
00097     ~SensorNetwork();
00098 
00099     int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
00100     int broadcast(const uint8_t* payload, uint16_t payloadLength);
00101     int read(uint8_t* buf, uint16_t bufLen);
00102     int initialize(void);
00103     const char* getDescription(void);
00104     SensorNetAddress* getSenderAddress(void);
00105 
00106 private:
00107     SensorNetAddress _clientAddr;   // Sender's address. not gateway's one.
00108     string _description;
00109 };
00110 
00111 }
00112 #endif /* SENSORNETWORK6_H_ */