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) 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 SENSORNETWORK_H_
00018 #define SENSORNETWORK_H_
00019 
00020 #include "MQTTSNGWDefines.h"
00021 #include <string>
00022 
00023 using namespace std;
00024 
00025 namespace MQTTSNGW
00026 {
00027 
00028 #ifdef  DEBUG_NWSTACK
00029   #define D_NWSTACK(...) printf(__VA_ARGS__)
00030 #else
00031   #define D_NWSTACK(...)
00032 #endif
00033 
00034 /*===========================================
00035  Class  SensorNetAddreess
00036  ============================================*/
00037 class SensorNetAddress
00038 {
00039 public:
00040     SensorNetAddress();
00041     ~SensorNetAddress();
00042     void setAddress(uint32_t IpAddr, uint16_t port);
00043     int  setAddress(string* data);
00044     uint16_t getPortNo(void);
00045     uint32_t getIpAddress(void);
00046     bool isMatch(SensorNetAddress* addr);
00047     SensorNetAddress& operator =(SensorNetAddress& addr);
00048     char* sprint(char* buf);
00049 private:
00050     uint16_t _portNo;
00051     uint32_t _IpAddr;
00052 };
00053 
00054 /*========================================
00055  Class UpdPort
00056  =======================================*/
00057 class UDPPort
00058 {
00059 public:
00060     UDPPort();
00061     virtual ~UDPPort();
00062 
00063     int open(const char* ipAddress, uint16_t multiPortNo,   uint16_t uniPortNo);
00064     void close(void);
00065     int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
00066     int broadcast(const uint8_t* buf, uint32_t length);
00067     int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr);
00068 
00069 private:
00070     void setNonBlocking(const bool);
00071     int recvfrom(int sockfd, uint8_t* buf, uint16_t len, uint8_t flags, SensorNetAddress* addr);
00072 
00073     int _sockfdUnicast;
00074     int _sockfdMulticast;
00075 
00076     SensorNetAddress _grpAddr;
00077     SensorNetAddress _clientAddr;
00078     bool _disconReq;
00079 
00080 };
00081 
00082 /*===========================================
00083  Class  SensorNetwork
00084  ============================================*/
00085 class SensorNetwork: public UDPPort
00086 {
00087 public:
00088     SensorNetwork();
00089     ~SensorNetwork();
00090 
00091     int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
00092     int broadcast(const uint8_t* payload, uint16_t payloadLength);
00093     int read(uint8_t* buf, uint16_t bufLen);
00094     int initialize(void);
00095     const char* getDescription(void);
00096     SensorNetAddress* getSenderAddress(void);
00097 
00098 private:
00099     SensorNetAddress _clientAddr;   // Sender's address. not gateway's one.
00100     string _description;
00101 };
00102 
00103 }
00104 #endif /* SENSORNETWORK_H_ */