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 
00015  **************************************************************************************/
00016 #ifndef SENSORNETWORKX_H_
00017 #define SENSORNETWORKX_H_
00018 
00019 #include "MQTTSNGWDefines.h"
00020 #include "MQTTSNGWProcess.h"
00021 #include <string>
00022 #include <termios.h>
00023 
00024 using namespace std;
00025 
00026 namespace MQTTSNGW
00027 {
00028 //#define DEBUG_NWSTACK
00029 
00030 #ifdef  DEBUG_NWSTACK
00031   #define D_NWSTACK(...) printf(__VA_ARGS__)
00032 #else
00033   #define D_NWSTACK(...)
00034 #endif
00035 
00036 #define API_XMITREQUEST          0x10
00037 #define API_RESPONSE             0x90
00038 #define API_MODEMSTATUS          0x8A
00039 #define API_XMITSTATUS           0x8B
00040 
00041 #define XMIT_STATUS_TIME_OVER    5000
00042 
00043 #define START_BYTE               0x7e
00044 #define ESCAPE                   0x7d
00045 #define XON                      0x11
00046 #define XOFF                     0x13
00047 
00048 /*===========================================
00049   Class  SerialPort
00050  ============================================*/
00051 class SerialPort{
00052 public:
00053     SerialPort();
00054     ~SerialPort();
00055     int open(char* devName, unsigned int baudrate,  bool parity, unsigned int stopbit, unsigned int flg);
00056     bool send(unsigned char b);
00057     bool recv(unsigned char* b);
00058     void flush();
00059 
00060 private:
00061     int _fd;  // file descriptor
00062     struct termios _tio;
00063 };
00064 
00065 /*===========================================
00066  Class  SensorNetAddreess
00067  ============================================*/
00068 class SensorNetAddress
00069 {
00070     friend class XBee;
00071 public:
00072     SensorNetAddress();
00073     ~SensorNetAddress();
00074     void setAddress(uint8_t* address64, uint8_t* address16);
00075     int  setAddress(string* data);
00076     void setBroadcastAddress(void);
00077     bool isMatch(SensorNetAddress* addr);
00078     SensorNetAddress& operator =(SensorNetAddress& addr);
00079     char* sprint(char*);
00080 private:
00081     uint8_t _address16[2];
00082     uint8_t _address64[8];
00083 };
00084 
00085 /*========================================
00086  Class XBee
00087  =======================================*/
00088 class XBee
00089 {
00090 public:
00091     XBee();
00092     ~XBee();
00093 
00094     int open(char* device, int boudrate);
00095     void close(void);
00096     int unicast(const uint8_t* buf, uint16_t length, SensorNetAddress* sendToAddr);
00097     int broadcast(const uint8_t* buf, uint16_t length);
00098     int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr);
00099     void setApiMode(uint8_t mode);
00100 
00101 private:
00102     int readApiFrame(uint8_t* recvData);
00103     int recv(uint8_t* buf);
00104     int send(const uint8_t* payload, uint8_t pLen, SensorNetAddress* addr);
00105     void send(uint8_t b);
00106 
00107     Semaphore _sem;
00108     Mutex _meutex;
00109     SerialPort* _serialPort;
00110     uint8_t _frameId;
00111     uint8_t _respCd;
00112     uint8_t _respId;
00113     uint8_t _dataLen;
00114     uint8_t _apiMode;
00115 };
00116 
00117 /*===========================================
00118  Class  SensorNetwork
00119  ============================================*/
00120 class SensorNetwork: public XBee
00121 {
00122 public:
00123     SensorNetwork();
00124     ~SensorNetwork();
00125 
00126     int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
00127     int broadcast(const uint8_t* payload, uint16_t payloadLength);
00128     int read(uint8_t* buf, uint16_t bufLen);
00129     int initialize(void);
00130     const char* getDescription(void);
00131     SensorNetAddress* getSenderAddress(void);
00132 
00133 private:
00134     SensorNetAddress _clientAddr;   // Sender's address. not gateway's one.
00135     string _description;
00136 };
00137 
00138 }
00139 
00140 #endif /* SENSORNETWORKX_H_ */