Commit 607853e29a49b0fb89d0247f9e3c07c23ff25197
Committed by
Moritz Wirger
1 parent
74194aad
Add connect and disconnect functions
Showing
1 changed file
with
116 additions
and
31 deletions
src/EntertainmentMode.cpp
| @@ -103,49 +103,134 @@ EntertainmentMode::EntertainmentMode(Bridge& bridge, Group& group):bridge(bridge | @@ -103,49 +103,134 @@ EntertainmentMode::EntertainmentMode(Bridge& bridge, Group& group):bridge(bridge | ||
| 103 | | Parse certificate | | 103 | | Parse certificate | |
| 104 | \*-------------------------------------------------*/ | 104 | \*-------------------------------------------------*/ |
| 105 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char*)mbedtls_test_cas_pem, mbedtls_test_cas_pem_len); | 105 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char*)mbedtls_test_cas_pem, mbedtls_test_cas_pem_len); |
| 106 | +} | ||
| 106 | 107 | ||
| 108 | +bool EntertainmentMode::Connect() | ||
| 109 | +{ | ||
| 107 | /*-------------------------------------------------*\ | 110 | /*-------------------------------------------------*\ |
| 108 | - | Connect to the Hue bridge UDP server | | 111 | + | Signal the bridge to start streaming | |
| 112 | + | If successful, connect to the UDP port | | ||
| 109 | \*-------------------------------------------------*/ | 113 | \*-------------------------------------------------*/ |
| 110 | - ret = mbedtls_net_connect(&server_fd, bridge.getBridgeIP().c_str(), "2100", MBEDTLS_NET_PROTO_UDP); | 114 | + if(bridge.StartStreaming(std::to_string(group.getId()))) |
| 115 | + { | ||
| 116 | + /*-------------------------------------------------*\ | ||
| 117 | + | Connect to the Hue bridge UDP server | | ||
| 118 | + \*-------------------------------------------------*/ | ||
| 119 | + int ret = mbedtls_net_connect(&server_fd, bridge.getBridgeIP().c_str(), "2100", MBEDTLS_NET_PROTO_UDP); | ||
| 120 | + | ||
| 121 | + /*-------------------------------------------------*\ | ||
| 122 | + | If connecting failed, close and return false | | ||
| 123 | + \*-------------------------------------------------*/ | ||
| 124 | + if(ret != 0) | ||
| 125 | + { | ||
| 126 | + mbedtls_ssl_close_notify(&ssl); | ||
| 127 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 128 | + return false; | ||
| 129 | + } | ||
| 111 | 130 | ||
| 112 | - /*-------------------------------------------------*\ | ||
| 113 | - | Configure defaults | | ||
| 114 | - \*-------------------------------------------------*/ | ||
| 115 | - ret = mbedtls_ssl_config_defaults( | ||
| 116 | - &conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT); | 131 | + /*-------------------------------------------------*\ |
| 132 | + | Configure defaults | | ||
| 133 | + \*-------------------------------------------------*/ | ||
| 134 | + ret = mbedtls_ssl_config_defaults( | ||
| 135 | + &conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT); | ||
| 117 | 136 | ||
| 118 | - mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); | ||
| 119 | - mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); | ||
| 120 | - mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); | 137 | + /*-------------------------------------------------*\ |
| 138 | + | If configuring failed, close and return false | | ||
| 139 | + \*-------------------------------------------------*/ | ||
| 140 | + if(ret != 0) | ||
| 141 | + { | ||
| 142 | + mbedtls_ssl_close_notify(&ssl); | ||
| 143 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 144 | + return false; | ||
| 145 | + } | ||
| 121 | 146 | ||
| 122 | - /*-------------------------------------------------*\ | ||
| 123 | - | Convert client key to binary array | | ||
| 124 | - \*-------------------------------------------------*/ | ||
| 125 | - std::vector<char> psk_binary = HexToBytes(bridge.getClientKey()); | 147 | + mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); |
| 148 | + mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); | ||
| 149 | + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); | ||
| 150 | + | ||
| 151 | + /*-------------------------------------------------*\ | ||
| 152 | + | Convert client key to binary array | | ||
| 153 | + \*-------------------------------------------------*/ | ||
| 154 | + std::vector<char> psk_binary = HexToBytes(bridge.getClientKey()); | ||
| 155 | + | ||
| 156 | + /*-------------------------------------------------*\ | ||
| 157 | + | Configure SSL pre-shared key and identity | | ||
| 158 | + | PSK - binary array from client key | | ||
| 159 | + | Identity - username (ASCII) | | ||
| 160 | + \*-------------------------------------------------*/ | ||
| 161 | + ret = mbedtls_ssl_conf_psk(&conf, (const unsigned char*)&psk_binary[0], psk_binary.size(), | ||
| 162 | + (const unsigned char*)bridge.getUsername().c_str(), bridge.getUsername().length()); | ||
| 163 | + | ||
| 164 | + /*-------------------------------------------------*\ | ||
| 165 | + | If configuring failed, close and return false | | ||
| 166 | + \*-------------------------------------------------*/ | ||
| 167 | + if(ret != 0) | ||
| 168 | + { | ||
| 169 | + mbedtls_ssl_close_notify(&ssl); | ||
| 170 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 171 | + return false; | ||
| 172 | + } | ||
| 126 | 173 | ||
| 127 | - /*-------------------------------------------------*\ | ||
| 128 | - | Configure SSL pre-shared key and identity | | ||
| 129 | - | PSK - binary array from client key | | ||
| 130 | - | Identity - username (ASCII) | | ||
| 131 | - \*-------------------------------------------------*/ | ||
| 132 | - ret = mbedtls_ssl_conf_psk(&conf, (const unsigned char*)&psk_binary[0], psk_binary.size(), | ||
| 133 | - (const unsigned char*)bridge.getUsername().c_str(), bridge.getUsername().length()); | 174 | + /*-------------------------------------------------*\ |
| 175 | + | Set up the SSL | | ||
| 176 | + \*-------------------------------------------------*/ | ||
| 177 | + ret = mbedtls_ssl_setup(&ssl, &conf); | ||
| 134 | 178 | ||
| 135 | - /*-------------------------------------------------*\ | ||
| 136 | - | Set up the SSL | | ||
| 137 | - \*-------------------------------------------------*/ | ||
| 138 | - ret = mbedtls_ssl_setup(&ssl, &conf); | 179 | + /*-------------------------------------------------*\ |
| 180 | + | If setup failed, close and return false | | ||
| 181 | + \*-------------------------------------------------*/ | ||
| 182 | + if(ret != 0) | ||
| 183 | + { | ||
| 184 | + mbedtls_ssl_close_notify(&ssl); | ||
| 185 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 186 | + return false; | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + ret = mbedtls_ssl_set_hostname(&ssl, "localhost"); | ||
| 139 | 190 | ||
| 140 | - ret = mbedtls_ssl_set_hostname(&ssl, "localhost"); | 191 | + /*-------------------------------------------------*\ |
| 192 | + | If set hostname failed, close and return false | | ||
| 193 | + \*-------------------------------------------------*/ | ||
| 194 | + if(ret != 0) | ||
| 195 | + { | ||
| 196 | + mbedtls_ssl_close_notify(&ssl); | ||
| 197 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 198 | + return false; | ||
| 199 | + } | ||
| 141 | 200 | ||
| 142 | - mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout); | ||
| 143 | - mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay); | 201 | + mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout); |
| 202 | + mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay); | ||
| 203 | + | ||
| 204 | + /*-------------------------------------------------*\ | ||
| 205 | + | Handshake | | ||
| 206 | + \*-------------------------------------------------*/ | ||
| 207 | + do | ||
| 208 | + { | ||
| 209 | + ret = mbedtls_ssl_handshake(&ssl); | ||
| 210 | + } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE); | ||
| 211 | + | ||
| 212 | + /*-------------------------------------------------*\ | ||
| 213 | + | If set hostname failed, close and return false | | ||
| 214 | + \*-------------------------------------------------*/ | ||
| 215 | + if(ret != 0) | ||
| 216 | + { | ||
| 217 | + mbedtls_ssl_close_notify(&ssl); | ||
| 218 | + bridge.StopStreaming(std::to_string(group.getId())); | ||
| 219 | + return false; | ||
| 220 | + } | ||
| 144 | 221 | ||
| 145 | - do | 222 | + return true; |
| 223 | + } | ||
| 224 | + else | ||
| 146 | { | 225 | { |
| 147 | - ret = mbedtls_ssl_handshake(&ssl); | ||
| 148 | - } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE); | 226 | + return false; |
| 227 | + } | ||
| 228 | +} | ||
| 229 | + | ||
| 230 | +bool EntertainmentMode::Disconnect() | ||
| 231 | +{ | ||
| 232 | + mbedtls_ssl_close_notify(&ssl); | ||
| 233 | + return bridge.StopStreaming(std::to_string(group.getId())); | ||
| 149 | } | 234 | } |
| 150 | 235 | ||
| 151 | bool EntertainmentMode::SetColorRGB(uint8_t light_index, uint8_t red, uint8_t green, uint8_t blue) | 236 | bool EntertainmentMode::SetColorRGB(uint8_t light_index, uint8_t red, uint8_t green, uint8_t blue) |