EntertainmentMode.cpp
10.6 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
\file EntertainmentMode.cpp
Copyright Notice\n
Copyright (C) 2020 Adam Honse - developer\n
This file is part of hueplusplus.
hueplusplus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hueplusplus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
**/
#include "hueplusplus/EntertainmentMode.h"
std::vector<char> HexToBytes(const std::string& hex)
{
std::vector<char> bytes;
for (unsigned int i = 0; i < hex.length(); i += 2)
{
std::string byteString = hex.substr(i, 2);
char byte = (char) strtol(byteString.c_str(), NULL, 16);
bytes.push_back(byte);
}
return bytes;
}
namespace hueplusplus
{
EntertainmentMode::EntertainmentMode(Bridge& bridge, Group& group):bridge(bridge),group(group)
{
/*-------------------------------------------------*\
| Signal the bridge to start streaming |
\*-------------------------------------------------*/
bridge.StartStreaming(std::to_string(group.getId()));
/*-------------------------------------------------*\
| Get the number of lights from the group |
\*-------------------------------------------------*/
entertainment_num_lights = group.getLightIds().size();
/*-------------------------------------------------*\
| Resize Entertainment Mode message buffer |
\*-------------------------------------------------*/
entertainment_msg.resize(HUE_ENTERTAINMENT_HEADER_SIZE + (entertainment_num_lights * HUE_ENTERTAINMENT_LIGHT_SIZE));
/*-------------------------------------------------*\
| Fill in Entertainment Mode message header |
\*-------------------------------------------------*/
memcpy(&entertainment_msg[0], "HueStream", 9);
entertainment_msg[9] = 0x01; // Version Major (1)
entertainment_msg[10] = 0x00; // Version Minor (0)
entertainment_msg[11] = 0x00; // Sequence ID
entertainment_msg[12] = 0x00; // Reserved
entertainment_msg[13] = 0x00; // Reserved
entertainment_msg[14] = 0x00; // Color Space (RGB)
entertainment_msg[15] = 0x00; // Reserved
/*-------------------------------------------------*\
| Fill in Entertainment Mode light data |
\*-------------------------------------------------*/
for (unsigned int light_idx = 0; light_idx < entertainment_num_lights; light_idx++)
{
unsigned int msg_idx = HUE_ENTERTAINMENT_HEADER_SIZE + (light_idx * HUE_ENTERTAINMENT_LIGHT_SIZE);
entertainment_msg[msg_idx + 0] = 0x00; // Type (Light)
entertainment_msg[msg_idx + 1] = group.getLightIds()[light_idx] >> 8; // ID MSB
entertainment_msg[msg_idx + 2] = group.getLightIds()[light_idx] & 0xFF; // ID LSB
entertainment_msg[msg_idx + 3] = 0x00; // Red MSB
entertainment_msg[msg_idx + 4] = 0x00; // Red LSB;
entertainment_msg[msg_idx + 5] = 0x00; // Green MSB;
entertainment_msg[msg_idx + 6] = 0x00; // Green LSB;
entertainment_msg[msg_idx + 7] = 0x00; // Blue MSB;
entertainment_msg[msg_idx + 8] = 0x00; // Blue LSB;
}
/*-------------------------------------------------*\
| Initialize mbedtls contexts |
\*-------------------------------------------------*/
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
/*-------------------------------------------------*\
| Seed the Deterministic Random Bit Generator (RNG) |
\*-------------------------------------------------*/
int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
/*-------------------------------------------------*\
| Parse certificate |
\*-------------------------------------------------*/
ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char*)mbedtls_test_cas_pem, mbedtls_test_cas_pem_len);
}
bool EntertainmentMode::Connect()
{
/*-------------------------------------------------*\
| Signal the bridge to start streaming |
| If successful, connect to the UDP port |
\*-------------------------------------------------*/
if(bridge.StartStreaming(std::to_string(group.getId())))
{
/*-------------------------------------------------*\
| Connect to the Hue bridge UDP server |
\*-------------------------------------------------*/
int ret = mbedtls_net_connect(&server_fd, bridge.getBridgeIP().c_str(), "2100", MBEDTLS_NET_PROTO_UDP);
/*-------------------------------------------------*\
| If connecting failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
/*-------------------------------------------------*\
| Configure defaults |
\*-------------------------------------------------*/
ret = mbedtls_ssl_config_defaults(
&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT);
/*-------------------------------------------------*\
| If configuring failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
/*-------------------------------------------------*\
| Convert client key to binary array |
\*-------------------------------------------------*/
std::vector<char> psk_binary = HexToBytes(bridge.getClientKey());
/*-------------------------------------------------*\
| Configure SSL pre-shared key and identity |
| PSK - binary array from client key |
| Identity - username (ASCII) |
\*-------------------------------------------------*/
ret = mbedtls_ssl_conf_psk(&conf, (const unsigned char*)&psk_binary[0], psk_binary.size(),
(const unsigned char*)bridge.getUsername().c_str(), bridge.getUsername().length());
/*-------------------------------------------------*\
| If configuring failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
/*-------------------------------------------------*\
| Set up the SSL |
\*-------------------------------------------------*/
ret = mbedtls_ssl_setup(&ssl, &conf);
/*-------------------------------------------------*\
| If setup failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
ret = mbedtls_ssl_set_hostname(&ssl, "localhost");
/*-------------------------------------------------*\
| If set hostname failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
/*-------------------------------------------------*\
| Handshake |
\*-------------------------------------------------*/
do
{
ret = mbedtls_ssl_handshake(&ssl);
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
/*-------------------------------------------------*\
| If set hostname failed, close and return false |
\*-------------------------------------------------*/
if(ret != 0)
{
mbedtls_ssl_close_notify(&ssl);
bridge.StopStreaming(std::to_string(group.getId()));
return false;
}
return true;
}
else
{
return false;
}
}
bool EntertainmentMode::Disconnect()
{
mbedtls_ssl_close_notify(&ssl);
return bridge.StopStreaming(std::to_string(group.getId()));
}
bool EntertainmentMode::SetColorRGB(uint8_t light_index, uint8_t red, uint8_t green, uint8_t blue)
{
if(light_index < entertainment_num_lights)
{
unsigned int msg_idx = HUE_ENTERTAINMENT_HEADER_SIZE + (light_index * HUE_ENTERTAINMENT_LIGHT_SIZE);
entertainment_msg[msg_idx + 3] = red; // Red MSB
entertainment_msg[msg_idx + 4] = red; // Red LSB;
entertainment_msg[msg_idx + 5] = green; // Green MSB;
entertainment_msg[msg_idx + 6] = green; // Green LSB;
entertainment_msg[msg_idx + 7] = blue; // Blue MSB;
entertainment_msg[msg_idx + 8] = blue; // Blue LSB;
return true;
}
else
{
return false;
}
}
void EntertainmentMode::Update()
{
int ret;
unsigned int total = 0;
while(total < entertainment_msg.size())
{
ret = mbedtls_ssl_write(&ssl, (const unsigned char*)&entertainment_msg[total], entertainment_msg.size());
if(ret < 0)
{
// Return if mbedtls_ssl_write errors
return;
}
else
{
total += ret;
}
}
}
} // namespace hueplusplus