11// Local-Hyperion includes
22#include " LedDevicePhilipsHue.h"
33
4+ #include < array>
45#include < chrono>
56#include < QStringLiteral>
67#include < utils/QStringUtils.h>
@@ -149,50 +150,57 @@ namespace
149150
150151 const int STREAM_CONNECTION_RETRYS = 20 ;
151152 const int STREAM_SSL_HANDSHAKE_ATTEMPTS = 5 ;
152- const int SSL_CIPHERSUITES[ 2 ] = {MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 0 };
153+ const std::array< int , 2 > SSL_CIPHERSUITES = {{ MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 0 } };
153154
154155 const int DEV_FIRMWAREVERSION_APIV2 = 1948086000 ;
155156
156157 // Enable rewrites that Hue-Bridge does not close the connection ("After 10 seconds of no activity the connection is closed automatically, and status is set back to inactive.")
157158 constexpr std::chrono::milliseconds STREAM_REWRITE_TIME{5000 };
158159
159160 // Streaming message header and payload definition
160- const uint8_t HEADER[] =
161- {
161+ const std::array< uint8_t , 16 > HEADER =
162+ {{
162163 ' H' , ' u' , ' e' , ' S' , ' t' , ' r' , ' e' , ' a' , ' m' , // protocol
163164 0x01 , 0x00 , // version 1.0
164165 0x01 , // sequence number 1
165166 0x00 , 0x00 , // Reserved write 0’s
166167 0x00 , // 0x00 = RGB; 0x01 = XY Brightness
167168 0x00 , // Reserved, write 0’s
168- };
169+ }} ;
169170
170- const uint8_t PAYLOAD_PER_LIGHT[] =
171- {
171+ const std::array< uint8_t , 9 > PAYLOAD_PER_LIGHT =
172+ {{
172173 0x01 , 0x00 , 0x06 , // light ID
173174 // color: 16 bpc
174175 0xff , 0xff , // Red
175176 0xff , 0xff , // Green
176177 0xff , 0xff , // Blue
177- };
178+ }} ;
178179
179180 // API v2 - Streaming message header and payload definition
180- const uint8_t HEADER_V2[] =
181- {
181+ const std::array< uint8_t , 16 > HEADER_V2 =
182+ {{
182183 ' H' , ' u' , ' e' , ' S' , ' t' , ' r' , ' e' , ' a' , ' m' , // protocol
183184 0x02 , 0x00 , // version 2.0
184185 0x01 , // sequence number 1
185186 0x00 , 0x00 , // Reserved write 0’s
186187 0x00 , // 0x00 = RGB; 0x01 = XY Brightness
187188 0x00 , // Reserved
188- };
189+ }} ;
189190
190- const char *ENTERTAINMENT_ID[ 36 ];
191- const uint8_t PAYLOAD_PER_CHANNEL_V2[] =
192- {
191+ const int ENTERTAINMENT_ID_SIZE = 36 ; // Expected length of Entertainment Configuration UUID (without null terminator)
192+ const std::array< uint8_t , 7 > PAYLOAD_PER_CHANNEL_V2 =
193+ {{
193194 0xff , // channel id
194195 0xff , 0xff , 0xff , 0xff , 0xff , 0xff // color
195- };
196+ }};
197+
198+ // Compile-time sanity checks for protocol element sizes
199+ static_assert (HEADER.size() == 16 , " Hue v1 header must be 16 bytes" );
200+ static_assert (HEADER_V2.size() == 16 , " Hue v2 header must be 16 bytes" );
201+ static_assert (PAYLOAD_PER_LIGHT.size() == 9 , " Payload per light must be 9 bytes (3 id + 6 color)" );
202+ static_assert (PAYLOAD_PER_CHANNEL_V2.size() == 7 , " Payload per channel v2 must be 7 bytes (1 id + 6 color)" );
203+ static_assert (ENTERTAINMENT_ID_SIZE == 36 , " Entertainment ID size expected to be 36 characters" );
196204
197205} // End of constants
198206
@@ -620,7 +628,7 @@ bool LedDevicePhilipsHueBridge::configureSsl()
620628
621629const int *LedDevicePhilipsHueBridge::getCiphersuites () const
622630{
623- return SSL_CIPHERSUITES;
631+ return SSL_CIPHERSUITES. data () ;
624632}
625633
626634void LedDevicePhilipsHueBridge::log (const QString& msg, const QVariant& value) const
@@ -1775,7 +1783,7 @@ void PhilipsHueLight::saveOriginalState(const QJsonObject &values)
17751783 state[API_XY_COORDINATES].toArray ()[1 ].toDouble (),
17761784 state[API_BRIGHTNESS].toDouble () / 254.0 };
17771785 _originalColor = _color;
1778- c = QString (" { \ " %1\ " : [%2, %3], \ " %4\ " : %5 }" ).arg (API_XY_COORDINATES).arg (_originalColor.x , 0 , ' d' , 4 ).arg (_originalColor.y , 0 , ' d' , 4 ).arg (API_BRIGHTNESS).arg ((_originalColor.bri * 254.0 ), 0 , ' d' , 4 );
1786+ c = QString (R"( { "%1": [%2, %3], "%4": %5 }) " ).arg (API_XY_COORDINATES).arg (_originalColor.x , 0 , ' d' , 4 ).arg (_originalColor.y , 0 , ' d' , 4 ).arg (API_BRIGHTNESS).arg ((_originalColor.bri * 254.0 ), 0 , ' d' , 4 );
17791787 Debug (_log, " Philips original state stored: %s" , QSTRING_CSTR (c));
17801788 _transitionTime = values[API_STATE].toObject ()[API_TRANSITIONTIME].toInt ();
17811789 }
@@ -2381,9 +2389,11 @@ int LedDevicePhilipsHue::writeStreamData(const std::vector<ColorRgb> &ledValues,
23812389 // 0xff, 0xff, 0xff, 0xff, 0xff, 0xff //white
23822390 // //etc for channel ids 4-7
23832391
2384- msg.reserve (static_cast <int >(sizeof (HEADER_V2) + sizeof (ENTERTAINMENT_ID) + sizeof (PAYLOAD_PER_CHANNEL_V2) * _lights.size ()));
2385- msg.append (reinterpret_cast <const char *>(HEADER_V2), sizeof (HEADER_V2));
2386- msg.append (_groupId.toLocal8Bit ());
2392+ msg.reserve (static_cast <int >(HEADER_V2.size () + ENTERTAINMENT_ID_SIZE + PAYLOAD_PER_CHANNEL_V2.size () * _lights.size ()));
2393+ msg.append (reinterpret_cast <const char *>(HEADER_V2.data ()), static_cast <int >(HEADER_V2.size ()));
2394+
2395+ QByteArray entertainmentID (_groupId.toLocal8Bit (),ENTERTAINMENT_ID_SIZE);
2396+ msg.append (entertainmentID);
23872397
23882398 auto maxChannels = static_cast <uint8_t >(ledValues.size ());
23892399
@@ -2400,8 +2410,8 @@ int LedDevicePhilipsHue::writeStreamData(const std::vector<ColorRgb> &ledValues,
24002410 auto B = static_cast <quint16>(color.blue << 8 );
24012411
24022412 msg.append (static_cast <char >(channel));
2403- const uint16_t payload[] = {qToBigEndian<quint16>(R), qToBigEndian<quint16>(G), qToBigEndian<quint16>(B)};
2404- msg.append (reinterpret_cast <const char *>(payload), sizeof (payload));
2413+ std::array<quint16, 3 > const payload = {qToBigEndian<quint16>(R), qToBigEndian<quint16>(G), qToBigEndian<quint16>(B)};
2414+ msg.append (reinterpret_cast <const char *>(payload. data ()), static_cast < int >( sizeof (payload) ));
24052415 }
24062416 }
24072417 }
@@ -2418,8 +2428,8 @@ int LedDevicePhilipsHue::writeStreamData(const std::vector<ColorRgb> &ledValues,
24182428 // 0x00, 0x00, 0x04, //light ID 4
24192429 // 0x00, 0x00, 0x00, 0x00, 0xff, 0xff //blue
24202430
2421- msg.reserve (static_cast <int >(sizeof ( HEADER) + sizeof ( PAYLOAD_PER_LIGHT) * _lights.size ()));
2422- msg.append (reinterpret_cast <const char *>(HEADER), sizeof (HEADER));
2431+ msg.reserve (static_cast <int >(HEADER. size ( ) + PAYLOAD_PER_LIGHT. size ( ) * _lights.size ()));
2432+ msg.append (reinterpret_cast <const char *>(HEADER. data ()), static_cast < int > (HEADER. size () ));
24232433
24242434 ColorRgb color;
24252435
@@ -2437,9 +2447,8 @@ int LedDevicePhilipsHue::writeStreamData(const std::vector<ColorRgb> &ledValues,
24372447
24382448 msg.append (2 , 0x00 );
24392449 msg.append (static_cast <char >(id));
2440- const uint16_t payload[] = {
2441- qToBigEndian<quint16>(R), qToBigEndian<quint16>(G), qToBigEndian<quint16>(B)};
2442- msg.append (reinterpret_cast <const char *>(payload), sizeof (payload));
2450+ std::array<quint16, 3 > const payload = {qToBigEndian<quint16>(R), qToBigEndian<quint16>(G), qToBigEndian<quint16>(B)};
2451+ msg.append (reinterpret_cast <const char *>(payload.data ()), static_cast <int >(sizeof (payload)));
24432452 }
24442453 ++i;
24452454 }
0 commit comments