5
5
#include < stdint.h>
6
6
#include < string.h>
7
7
8
+ #include " broadcast_server.h"
8
9
#include " custom_cast.h"
9
10
#include " json.hpp"
10
11
#include " network_interfaces.h"
@@ -20,7 +21,7 @@ Emotibit::Emotibit (struct BrainFlowInputParams params)
20
21
{
21
22
data_socket = NULL ;
22
23
control_socket = NULL ;
23
- advertise_socket_server = NULL ;
24
+ adv_socket = NULL ;
24
25
keep_alive = false ;
25
26
initialized = false ;
26
27
control_port = -1 ;
@@ -70,10 +71,10 @@ int Emotibit::prepare_session ()
70
71
71
72
if (res != (int )BrainFlowExitCodes::STATUS_OK)
72
73
{
73
- if (advertise_socket_server != NULL )
74
+ if (adv_socket != NULL )
74
75
{
75
- delete advertise_socket_server ;
76
- advertise_socket_server = NULL ;
76
+ delete adv_socket ;
77
+ adv_socket = NULL ;
77
78
}
78
79
if (data_socket != NULL )
79
80
{
@@ -165,11 +166,11 @@ int Emotibit::release_session ()
165
166
delete control_socket;
166
167
control_socket = NULL ;
167
168
}
168
- if (advertise_socket_server )
169
+ if (adv_socket )
169
170
{
170
- advertise_socket_server ->close ();
171
- delete advertise_socket_server ;
172
- advertise_socket_server = NULL ;
171
+ adv_socket ->close ();
172
+ delete adv_socket ;
173
+ adv_socket = NULL ;
173
174
}
174
175
control_port = -1 ;
175
176
data_port = -1 ;
@@ -493,6 +494,13 @@ std::vector<std::string> Emotibit::split_string (const std::string &package, cha
493
494
494
495
bool Emotibit::get_header (
495
496
const std::string &package_string, int *package_num, int *data_len, std::string &type_tag)
497
+ {
498
+ std::string serial_number = " " ;
499
+ return get_header (package_string, package_num, data_len, type_tag, serial_number);
500
+ }
501
+
502
+ bool Emotibit::get_header (const std::string &package_string, int *package_num, int *data_len,
503
+ std::string &type_tag, std::string &serial_number)
496
504
{
497
505
std::vector<std::string> package = split_string (package_string, PAYLOAD_DELIMITER);
498
506
if (package.size () >= HEADER_LENGTH)
@@ -518,6 +526,13 @@ bool Emotibit::get_header (
518
526
if (package.at (3 ) != " " )
519
527
{
520
528
type_tag = package.at (3 );
529
+ if (type_tag == HELLO_HOST)
530
+ {
531
+ if (package.size () > 9 )
532
+ {
533
+ serial_number = package.at (9 );
534
+ }
535
+ }
521
536
}
522
537
else
523
538
{
@@ -582,14 +597,14 @@ int Emotibit::create_adv_connection ()
582
597
583
598
for (std::string broadcast_address : broadcast_addresses)
584
599
{
600
+ BroadCastServer *advertise_socket =
601
+ new BroadCastServer (broadcast_address.c_str (), WIFI_ADVERTISING_PORT);
585
602
res = (int )BrainFlowExitCodes::STATUS_OK;
586
603
safe_logger (
587
604
spdlog::level::info, " trying broadcast address: {}" , broadcast_address.c_str ());
588
605
if (res == (int )BrainFlowExitCodes::STATUS_OK)
589
606
{
590
- advertise_socket_server =
591
- new BroadCastServer (broadcast_address.c_str (), WIFI_ADVERTISING_PORT);
592
- int init_res = advertise_socket_server->init ();
607
+ int init_res = advertise_socket->init ();
593
608
if (init_res != (int )BroadCastServerReturnCodes::STATUS_OK)
594
609
{
595
610
safe_logger (spdlog::level::err, " failed to init broadcast server socket: {}" , res);
@@ -600,7 +615,7 @@ int Emotibit::create_adv_connection ()
600
615
{
601
616
std::string package = create_package (HELLO_EMOTIBIT, 0 , " " , 0 );
602
617
safe_logger (spdlog::level::info, " sending package: {}" , package.c_str ());
603
- int bytes_send = advertise_socket_server ->send (package.c_str (), (int )package.size ());
618
+ int bytes_send = advertise_socket ->send (package.c_str (), (int )package.size ());
604
619
if (bytes_send != (int )package.size ())
605
620
{
606
621
safe_logger (
@@ -618,8 +633,8 @@ int Emotibit::create_adv_connection ()
618
633
double start_time = get_timestamp ();
619
634
for (int i = 0 ; (i < 100 ) && (!found); i++)
620
635
{
621
- int bytes_recv = advertise_socket_server-> recv (
622
- recv_data, max_size, emotibit_ip, max_ip_addr_size);
636
+ int bytes_recv =
637
+ advertise_socket-> recv ( recv_data, max_size, emotibit_ip, max_ip_addr_size);
623
638
if (bytes_recv > 0 )
624
639
{
625
640
std::vector<std::string> splitted_packages =
@@ -630,13 +645,21 @@ int Emotibit::create_adv_connection ()
630
645
int package_num = 0 ;
631
646
int data_len = 0 ;
632
647
std::string type_tag = " " ;
633
- if (get_header (recv_package, &package_num, &data_len, type_tag))
648
+ std::string serial_number = " " ;
649
+ if (get_header (
650
+ recv_package, &package_num, &data_len, type_tag, serial_number))
634
651
{
635
652
safe_logger (spdlog::level::info, " received {} package" , type_tag);
636
- if (( type_tag == HELLO_HOST) || (type_tag == PONG) )
653
+ if (type_tag == HELLO_HOST)
637
654
{
638
- found = true ;
639
- ip_address = emotibit_ip;
655
+ safe_logger (
656
+ spdlog::level::info, " Found emotibit: {}" , serial_number);
657
+ if (params.serial_number .empty () ||
658
+ (params.serial_number == serial_number))
659
+ {
660
+ found = true ;
661
+ ip_address = emotibit_ip;
662
+ }
640
663
}
641
664
}
642
665
else
@@ -657,19 +680,40 @@ int Emotibit::create_adv_connection ()
657
680
res = (int )BrainFlowExitCodes::BOARD_NOT_READY_ERROR;
658
681
}
659
682
}
660
- if (res != ( int )BrainFlowExitCodes::STATUS_OK )
683
+ if (advertise_socket != NULL )
661
684
{
662
- if (advertise_socket_server != NULL )
663
- {
664
- delete advertise_socket_server;
665
- advertise_socket_server = NULL ;
666
- }
685
+ delete advertise_socket;
686
+ advertise_socket = NULL ;
667
687
}
668
- else
688
+ if (res == ( int )BrainFlowExitCodes::STATUS_OK)
669
689
{
670
690
break ;
671
691
}
672
692
}
693
+ // we dont need broadcast anymore, replace it with normal socket
694
+ if (res == (int )BrainFlowExitCodes::STATUS_OK)
695
+ {
696
+ // ugly but makes it safer
697
+ #ifdef _WIN32
698
+ Sleep (1000 );
699
+ #else
700
+ usleep (1000000 );
701
+ #endif
702
+ adv_socket = new SocketClientUDP (ip_address.c_str (), WIFI_ADVERTISING_PORT);
703
+ if (adv_socket->connect () != ((int )SocketClientUDPReturnCodes::STATUS_OK))
704
+ {
705
+ safe_logger (spdlog::level::err, " Failed to bind adv_socket" );
706
+ res = (int )BrainFlowExitCodes::SET_PORT_ERROR;
707
+ adv_socket->close ();
708
+ delete adv_socket;
709
+ adv_socket = NULL ;
710
+ }
711
+ else
712
+ {
713
+ safe_logger (spdlog::level::debug, " adv_connection established, ip: {}, port: {}" ,
714
+ ip_address, WIFI_ADVERTISING_PORT);
715
+ }
716
+ }
673
717
return res;
674
718
}
675
719
@@ -749,7 +793,7 @@ int Emotibit::send_connect_msg ()
749
793
safe_logger (spdlog::level::info, " sending connect package: {}" , package.c_str ());
750
794
751
795
int res = (int )BrainFlowExitCodes::STATUS_OK;
752
- int bytes_send = advertise_socket_server ->send (package.c_str (), (int )package.size ());
796
+ int bytes_send = adv_socket ->send (package.c_str (), (int )package.size ());
753
797
if (bytes_send != (int )package.size ())
754
798
{
755
799
safe_logger (spdlog::level::err, " failed to send connect package, res is {}" , bytes_send);
@@ -795,7 +839,7 @@ int Emotibit::wait_for_connection ()
795
839
}
796
840
else
797
841
{
798
- int max_attempts = 15 ;
842
+ int max_attempts = 20 ;
799
843
for (int i = 0 ; i < max_attempts; i++)
800
844
{
801
845
safe_logger (spdlog::level::trace, " waiting for accept {}/{}" , i, max_attempts);
@@ -838,7 +882,7 @@ void Emotibit::ping_thread ()
838
882
payload.push_back (std::to_string (data_port));
839
883
std::string package = create_package (PING, package_num++, payload);
840
884
// safe_logger (spdlog::level::trace, "sending package: {}", package.c_str ());
841
- int bytes_send = advertise_socket_server ->send (package.c_str (), (int )package.size ());
885
+ int bytes_send = adv_socket ->send (package.c_str (), (int )package.size ());
842
886
if (bytes_send != (int )package.size ())
843
887
{
844
888
safe_logger (spdlog::level::err, " failed to send adv package, res is {}" , bytes_send);
0 commit comments