@@ -206,7 +206,7 @@ static bool verify_csms_cn(const std::string& hostname, bool preverified, const
206
206
207
207
WebsocketTlsTPM::WebsocketTlsTPM (const WebsocketConnectionOptions& connection_options,
208
208
std::shared_ptr<EvseSecurity> evse_security) :
209
- WebsocketBase (), evse_security(evse_security) {
209
+ WebsocketBase (), evse_security(evse_security), stopped( false ) {
210
210
211
211
set_connection_options (connection_options);
212
212
@@ -232,17 +232,18 @@ void WebsocketTlsTPM::set_connection_options(const WebsocketConnectionOptions& c
232
232
switch (connection_options.security_profile ) { // `switch` used to lint on missing enum-values
233
233
case security::SecurityProfile::OCPP_1_6_ONLY_UNSECURED_TRANSPORT_WITHOUT_BASIC_AUTHENTICATION:
234
234
case security::SecurityProfile::UNSECURED_TRANSPORT_WITH_BASIC_AUTHENTICATION:
235
+ set_connection_options_base (connection_options);
236
+ this ->connection_options .csms_uri .set_secure (false );
237
+ break ;
235
238
case security::SecurityProfile::TLS_WITH_BASIC_AUTHENTICATION:
236
239
case security::SecurityProfile::TLS_WITH_CLIENT_SIDE_CERTIFICATES:
240
+ set_connection_options_base (connection_options);
241
+ this ->connection_options .csms_uri .set_secure (true );
237
242
break ;
238
243
default :
239
244
throw std::invalid_argument (" unknown `security_profile`, value = " +
240
245
std::to_string (connection_options.security_profile ));
241
246
}
242
-
243
- set_connection_options_base (connection_options);
244
-
245
- this ->connection_options .csms_uri .set_secure (true );
246
247
}
247
248
248
249
static int callback_minimal (struct lws * wsi, enum lws_callback_reasons reason, void * user, void * in, size_t len) {
@@ -362,6 +363,10 @@ void WebsocketTlsTPM::tls_init(SSL_CTX* ctx, const std::string& path_chain, cons
362
363
}
363
364
364
365
void WebsocketTlsTPM::recv_loop () {
366
+ if (stopped) {
367
+ return ;
368
+ }
369
+
365
370
std::shared_ptr<ConnectionData> local_data = conn_data;
366
371
367
372
if (local_data == nullptr ) {
@@ -402,6 +407,10 @@ void WebsocketTlsTPM::recv_loop() {
402
407
}
403
408
404
409
void WebsocketTlsTPM::client_loop () {
410
+ if (stopped) {
411
+ return ;
412
+ }
413
+
405
414
std::shared_ptr<ConnectionData> local_data = conn_data;
406
415
407
416
if (local_data == nullptr ) {
@@ -428,6 +437,15 @@ void WebsocketTlsTPM::client_loop() {
428
437
429
438
info.fd_limit_per_thread = 1 + 1 + 1 ;
430
439
440
+ #ifdef LWS_WITH_NETWORK
441
+ if (connection_options.iface_or_ip .has_value ()) {
442
+ info.iface = connection_options.iface_or_ip .value ().c_str ();
443
+ // TODO make bind_iface work.
444
+ // See ticket https://github.com/EVerest/libocpp/issues/542
445
+ // info.bind_iface = 1;
446
+ }
447
+ #endif
448
+
431
449
if (this ->connection_options .security_profile == 2 || this ->connection_options .security_profile == 3 ) {
432
450
// Setup context - need to know the key type first
433
451
std::string path_key;
@@ -598,12 +616,6 @@ bool WebsocketTlsTPM::connect() {
598
616
this ->recv_message_thread ->join ();
599
617
}
600
618
601
- // Stop any pending reconnect timer
602
- {
603
- std::lock_guard<std::mutex> lk (this ->reconnect_mutex );
604
- this ->reconnect_timer_tpm .stop ();
605
- }
606
-
607
619
// Clear any pending messages on a new connection
608
620
{
609
621
std::lock_guard<std::mutex> lock (queue_mutex);
@@ -617,20 +629,6 @@ bool WebsocketTlsTPM::connect() {
617
629
empty.swap (recv_message_queue);
618
630
}
619
631
620
- // Bind reconnect callback
621
- this ->reconnect_callback = [this ](const websocketpp::lib::error_code& ec) {
622
- EVLOG_info << " Reconnecting to TLS websocket at uri: " << this ->connection_options .csms_uri .string ()
623
- << " with security profile: " << this ->connection_options .security_profile ;
624
-
625
- // close connection before reconnecting
626
- if (this ->m_is_connected ) {
627
- EVLOG_info << " Closing websocket connection before reconnecting" ;
628
- this ->close (websocketpp::close ::status::abnormal_close, " Reconnect" );
629
- }
630
-
631
- this ->connect ();
632
- };
633
-
634
632
std::unique_lock<std::mutex> lock (connection_mutex);
635
633
636
634
// Release other threads
@@ -666,37 +664,20 @@ bool WebsocketTlsTPM::connect() {
666
664
return (connected);
667
665
}
668
666
669
- void WebsocketTlsTPM::reconnect (std::error_code reason, long delay) {
670
- EVLOG_info << " Attempting TLS TPM reconnect with reason: " << reason << " and delay: " << delay;
667
+ // void WebsocketTlsTPM::reconnect(std::error_code reason, long delay) {
668
+ void WebsocketTlsTPM::reconnect () {
671
669
672
670
if (this ->shutting_down ) {
673
671
EVLOG_info << " Not reconnecting because the websocket is being shutdown." ;
674
672
return ;
675
673
}
676
674
677
- if (this ->m_is_connected ) {
678
- EVLOG_info << " Closing websocket connection before reconnecting" ;
679
- this ->close (websocketpp::close ::status::abnormal_close, " Reconnect" );
675
+ this ->connect ();
680
676
}
681
677
682
- EVLOG_info << " Reconnecting in: " << delay << " ms"
683
- << " , attempt: " << this ->connection_attempts ;
684
-
685
- {
686
- std::lock_guard<std::mutex> lk (this ->reconnect_mutex );
687
- this ->reconnect_timer_tpm .timeout ([this ]() { this ->reconnect_callback (websocketpp::lib::error_code ()); },
688
- std::chrono::milliseconds (delay));
689
- }
690
- }
691
-
692
- void WebsocketTlsTPM::close (websocketpp::close::status::value code, const std::string& reason) {
678
+ void WebsocketTlsTPM::close (WebsocketCloseReason code, const std::string& reason, const bool /* stop_perpetual*/ ) {
693
679
EVLOG_info << " Closing TLS TPM websocket with reason: " << reason;
694
680
695
- {
696
- std::lock_guard<std::mutex> lk (this ->reconnect_mutex );
697
- this ->reconnect_timer_tpm .stop ();
698
- }
699
-
700
681
std::shared_ptr<ConnectionData> local_data = conn_data;
701
682
if (local_data != nullptr ) {
702
683
// Set the trigger from us
@@ -707,16 +688,15 @@ void WebsocketTlsTPM::close(websocketpp::close::status::value code, const std::s
707
688
conn_data.reset ();
708
689
709
690
this ->m_is_connected = false ;
710
- std::thread closing ([this ]() { this ->closed_callback (websocketpp:: close ::status:: normal ); });
691
+ std::thread closing ([this ]() { this ->closed_callback (WebsocketCloseReason::Normal ); });
711
692
closing.detach ();
693
+ stopped = true ;
712
694
}
713
695
714
696
void WebsocketTlsTPM::on_conn_connected () {
715
697
EVLOG_info << " OCPP client successfully connected to TLS websocket server" ;
716
698
717
- this ->connection_attempts = 1 ; // reset connection attempts
718
699
this ->m_is_connected = true ;
719
- this ->reconnecting = false ;
720
700
721
701
std::thread connected ([this ]() { this ->connected_callback (this ->connection_options .security_profile ); });
722
702
connected.detach ();
@@ -727,35 +707,20 @@ void WebsocketTlsTPM::on_conn_close() {
727
707
728
708
std::lock_guard<std::mutex> lk (this ->connection_mutex );
729
709
this ->m_is_connected = false ;
730
- this ->disconnected_callback ();
731
- this ->cancel_reconnect_timer ();
732
710
733
- std::thread closing ([this ]() { this ->closed_callback (websocketpp:: close ::status:: normal ); });
711
+ std::thread closing ([this ]() { this ->closed_callback (WebsocketCloseReason::Normal ); });
734
712
closing.detach ();
735
713
}
736
714
737
715
void WebsocketTlsTPM::on_conn_fail () {
738
716
EVLOG_error << " OCPP client connection failed to TLS websocket server" ;
739
717
740
718
std::lock_guard<std::mutex> lk (this ->connection_mutex );
741
- if (this ->m_is_connected ) {
742
- std::thread disconnect ([this ]() { this ->disconnected_callback (); });
743
- disconnect.detach ();
744
- }
745
-
746
- this ->m_is_connected = false ;
747
719
748
- // -1 indicates to always attempt to reconnect
749
- if (this ->connection_options .max_connection_attempts == -1 or
750
- this ->connection_attempts <= this ->connection_options .max_connection_attempts ) {
751
- this ->reconnect (std::error_code (), this ->get_reconnect_interval ());
720
+ std::thread disconnect ([this ]() { this ->failed_callback (WebsocketCloseReason::Normal); });
721
+ disconnect.detach ();
752
722
753
- // Increment reconn attempts
754
- this ->connection_attempts += 1 ;
755
- } else {
756
- EVLOG_info << " Closed TLS websocket, reconnect attempts exhausted" ;
757
- this ->close (websocketpp::close ::status::normal , " Connection failed" );
758
- }
723
+ this ->m_is_connected = false ;
759
724
}
760
725
761
726
void WebsocketTlsTPM::on_message (void * msg, size_t len) {
@@ -1217,4 +1182,4 @@ int WebsocketTlsTPM::process_callback(void* wsi_ptr, int callback_reason, void*
1217
1182
return 0 ;
1218
1183
}
1219
1184
1220
- } // namespace ocpp
1185
+ } // namespace ocpp
0 commit comments