-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinysp.hh
115 lines (93 loc) · 2.02 KB
/
tinysp.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef TINYSP_HH
#define TINYSP_HH
#include <string>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <deque>
#include <exception>
#include <poll.h>
#include "serialport.hh"
#define MAX_BUF 64
struct PckException :
public std::exception
{
};
struct PckDecodeError :
public PckException
{
};
struct PckWrongData :
public PckException
{
};
struct PckNoPacketsReady :
public PckException
{
};
class PckIOError :
public PckException
{
private:
std::string _s;
public:
PckIOError(const std::string &);
virtual ~PckIOError() throw ();
std::string str() const;
};
struct Packet
{
unsigned char id;
size_t len;
unsigned char buf[MAX_BUF];
Packet(unsigned char *, int);
Packet(unsigned char, const std::deque<unsigned char> &);
Packet(unsigned char);
};
enum COMM
{
SERIAL = 0,
BLUETOOTH = 1
};
class TinySP
{
public:
struct Stats
{
unsigned rxpkt;
unsigned rxbytes;
unsigned txpkt;
unsigned txbytes;
unsigned rxerr;
Stats() : rxpkt(0), rxbytes(0), txpkt(0), txbytes(0), rxerr(0) { }
};
private:
COMM _comm;
bool _connected;
unsigned char _buf[MAX_BUF];
unsigned char _read_byte();
int _decode_byte();
int _state;
int _ctr;
Stats _commStat;
struct pollfd _fds[1];
// Serial port
SerialPort _serialport;
// Bluetooth
std::string _mac;
int _socket;
struct sockaddr_rc _loc_addr;
public:
TinySP(std::string);
TinySP(std::string, int);
~TinySP();
std::string get_mac(void);
std::string get_serial_port(void);
void start_connection(void);
void close_connection(void);
bool get_connection_status(void);
Packet read_packet();
void send_packet(const Packet &);
TinySP::Stats get_stats() const;
};
#endif