-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDevice.cpp
More file actions
201 lines (176 loc) · 5.03 KB
/
Copy pathDevice.cpp
File metadata and controls
201 lines (176 loc) · 5.03 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* File: Device.cpp
* Author: tomko
*
* Created on 26 marzec 2012, 23:24
*/
#include "Device.h"
#include <iostream>
//Temporary
#undef BDADDR_ALL
#undef BDADDR_ANY
#undef BDADDR_LOCAL
#define BDADDR_ANY_INITIALIZER {{0, 0, 0, 0, 0, 0}}
#define BDADDR_ALL_INITIALIZER {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}
#define BDADDR_LOCAL_INITIALIZER {{0, 0, 0, 0xff, 0xff, 0xff}}
#define BDADDR_ANY (&(bdaddr_t) BDADDR_ANY_INITIALIZER)
#define BDADDR_ALL (&(bdaddr_t) BDADDR_ALL_INITIALIZER)
#define BDADDR_LOCAL (&(bdaddr_t) BDADDR_LOCAL_INITIALIZER)
//Temporary
#include <unistd.h>
#include <sys/socket.h>
Device::Device()
{
my_socket = 0;
}
Device::Device(const Device& c)
{
//kopiowanie tylko danych urządzenia
MAC = c.MAC;
name = c.name;
port = c.port;
my_socket = 0;
}
Device::~Device()
{
stopConnection();
}
void Device::sendInt(int to_send) throw (Device::ConnectionError)
{
//createSockAddrSend();
//int socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); //alokacja socket
//int status = connect(my_socket, (struct sockaddr *) &sock_addr_send, sizeof (sock_addr_send)); //nawiązanie połączenie
/*if (status < 0)
{
std::cout << "Status: " << status << "\n";
close(my_socket);
throw Device::ConnectionError(name, MAC, "connecting");
}*/
int status = write(my_socket, &to_send, sizeof (int));
if (status < 0)
{
// std::cout << "Status: " << status << "\n";
throw Device::ConnectionError(name, MAC, "sending");
}
}
void Device::sendChar(char to_send) throw (Device::ConnectionError)
{
int status = write(my_socket, &to_send, sizeof (char));
//close(socket);
if (status < 0)
{
std::cout << "Status: " << status << "\n";
throw Device::ConnectionError(name, MAC, "sending");
}
}
void Device::createSockAddrSend()
{
createSockAddrSend(port);
}
void Device::createSockAddrSend(uint8_t port)
{
sock_addr_send.rc_family = AF_BLUETOOTH; // ustawienie na bluetooth
sock_addr_send.rc_channel = port; //TODO: ustalić port
str2ba(MAC.c_str(), &sock_addr_send.rc_bdaddr); //ustawienie adresu MAC urządzenia docelowego
}
void Device::createSockAddrRec()
{
sock_addr_rec.rc_family = AF_BLUETOOTH; // ustawienie na bluetooth
bdaddr_t bt_any = BDADDR_ANY_INITIALIZER;
sock_addr_rec.rc_bdaddr = bt_any; //dowolne źródło
sock_addr_rec.rc_channel = (uint8_t) 1; // port nasłuchiwania
}
char Device::receiveUChar() throw (Device::ConnectionError)
{
u_char buff;
int read_len = read(my_socket, &buff, sizeof (buff)); //czytanie
if (read_len < 0) //bład
{
throw (Device::ConnectionError(name, MAC, "Error while reading from socket"));
}
else if (!read_len) //jeśli nie odebrano danych
{
return receiveUChar(); //próbujemy jeszcze raz odebrać
}
return buff;
}
//TODO: sprawdzić wydajnosć
uint32_t Device::receiveUInt4() throw (Device::ConnectionError)
{
unsigned int buff; //bufor
//pobranie pierwszego bajtu
buff = receiveUChar(); //domyślna konwersja char do uint
for (int i = 0; i < 3; i++) //trzykrotnie przesunięcie bitowe i pobranie po kolejnym
{
buff << 8; //zrobienie miejsca na kolejne bajty
buff += receiveUChar(); //pobranie kolejnego bajtu
}
return buff;
//stare
/*int read_len = read(my_socket, &buff, sizeof (buff)); //czytanie
if (read_len < 0)
{
throw (Device::ConnectionError(name, MAC, ""));
}
while (read_len != sizeof (buff))
{
}
return buff;**/
//bardzo stare
/*
struct sockaddr remote_addr;
createSockAddrRec();
//int socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); //alokacja socket
//bind(socket, (struct sockaddr *) &sock_addr_rec, sizeof (sock_addr_rec));
//listen(socket, 1); // nasłuchiwanie (chyba dozwolone jedno połączenie)
socklen_t remote_addr_len = sizeof (remote_addr);
std::cout << "Czytanie1\n";
int client = accept(my_socket, (struct sockaddr*) &remote_addr, &remote_addr_len); //akceptacja połączenia
int buff; //bufor
std::cout << "Czytanie2\n";
int read_len = read(client, &buff, sizeof (buff)); //czytanie
std::cout << "Przeczytane\n";
close(client);
close(my_socket);
if (read_len < 0)
{
throw (Device::ConnectionError(name, MAC, "no_data_received"));
}
return buff;*/
}
void Device::stopConnection()
{
if (my_socket)
{
close(my_socket);
}
my_socket = 0;
}
void Device::startConnection()
{
createSockAddrSend();
this->my_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); //alokacja socket
int status = connect(my_socket, (struct sockaddr *) &sock_addr_send, sizeof (sock_addr_send)); //nawiązanie połączenie
if (status < 0)
{
//std::cout << "Status: " << status << "\n";
close(my_socket);
throw Device::ConnectionError(name, MAC, "Error while connecting");
}
}
std::ostream & operator <<(std::ostream& o, Device& d)
{
o << d.getMAC() << " " << d.getName() << " " << d.getPort() << "\n";
return o;
}
std::istream & operator >>(std::istream& i, Device& d)
{
std::string MAC, name;
i >> MAC;
i >> name;
uint8_t port;
i >> port;
d.setMAC(MAC);
d.setName(name);
d.setPort(port);
}