@@ -17,7 +17,7 @@ using namespace og;
1717
1818static Logger& logger = Logger::GetInstance();
1919
20- static std::string GetLastErrorAsString () {
20+ static std::string GetLastWSAErrorAsString () {
2121 const DWORD errorMessageId = ::WSAGetLastError ();
2222 if (errorMessageId == 0 ) return std::string ();
2323
@@ -38,15 +38,41 @@ static std::string GetLastErrorAsString() {
3838 return message;
3939}
4040
41+ static std::string GetLastErrorAsString () {
42+ const DWORD errorMessageId = GetLastError ();
43+ if (errorMessageId == 0 ) return " " ;
44+
45+ LPSTR messageBuffer = nullptr ;
46+ const size_t size = FormatMessageA (
47+ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
48+ nullptr ,
49+ errorMessageId,
50+ MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
51+ reinterpret_cast <LPSTR>(&messageBuffer),
52+ 0 ,
53+ nullptr );
54+
55+ std::string message (messageBuffer, size);
56+
57+ LocalFree (messageBuffer);
58+
59+ return message;
60+ }
61+
4162BluetoothCommunicationService::BluetoothCommunicationService (og::DeviceBluetoothCommunicationConfiguration configuration)
4263 : configuration_(std::move(configuration)) {
4364 Connect ();
4465}
4566
46- void BluetoothCommunicationService::LogError (const std::string& message, bool with_win_error = true ) const {
67+ void BluetoothCommunicationService::LogError (const std::string& message, bool wsa_error, bool with_win_error = true ) const {
4768 std::ostringstream oss;
4869
49- logger.Log (kLoggerLevel_Error , " %s, %s: %s" , configuration_.name .c_str (), message.c_str (), with_win_error ? GetLastErrorAsString ().c_str () : " " );
70+ std::string err_msg = " " ;
71+ if (with_win_error) {
72+ err_msg = wsa_error ? GetLastWSAErrorAsString () : GetLastErrorAsString ();
73+ }
74+
75+ logger.Log (kLoggerLevel_Error , " %s, %s: %s" , configuration_.name .c_str (), message.c_str (), err_msg.c_str ());
5076}
5177
5278bool BluetoothCommunicationService::IsConnected () {
@@ -58,7 +84,7 @@ bool BluetoothCommunicationService::Connect() {
5884 WSAData data{};
5985
6086 if (WSAStartup (MAKEWORD (2 , 2 ), &data) != 0 ) {
61- LogError (" WSA failed to startup" );
87+ LogError (" WSA failed to startup" , true );
6288
6389 return false ;
6490 }
@@ -81,7 +107,7 @@ bool BluetoothCommunicationService::Connect() {
81107
82108 BTH_ADDR device_address = 0 ;
83109 if (btDevice == nullptr ) {
84- logger. Log (og:: kLoggerLevel_Warning , " Could not find any bluetooth devices" );
110+ LogError ( " Could not find any bluetooth devices" , false );
85111 device_address = NULL ;
86112 return false ;
87113 }
@@ -103,18 +129,26 @@ bool BluetoothCommunicationService::Connect() {
103129 }
104130 } while (BluetoothFindNextDevice (btDevice, &btDeviceInfo)); // loop through remaining BT devices connected to this machine
105131
132+ if (!BluetoothFindDeviceClose (btDevice)) {
133+ LogError (" Failed to close bluetooth device" , false );
134+ }
135+
106136 if (!found_device) return false ;
107137
108138 sock_ = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
139+ if (sock_ == INVALID_SOCKET) {
140+ LogError (" unable to create socket" , true );
141+ return false ;
142+ }
109143
110144 SOCKADDR_BTH sock_address{};
111145 sock_address.addressFamily = AF_BTH;
112- sock_address.serviceClassId = RFCOMM_PROTOCOL_UUID ;
146+ sock_address.serviceClassId = SerialPortServiceClass_UUID ;
113147 sock_address.port = 0 ;
114148 sock_address.btAddr = device_address;
115149
116150 if (connect (sock_, reinterpret_cast <SOCKADDR*>(&sock_address), sizeof sock_address) != 0 ) {
117- LogError (" Failed to connect to bluetooth device" );
151+ LogError (" Failed to connect to bluetooth device" , true );
118152
119153 return false ;
120154 }
@@ -137,8 +171,12 @@ bool BluetoothCommunicationService::ReceiveNextPacket(std::string& buff) {
137171 std::scoped_lock lock (io_mutex_);
138172 const int err = recv (sock_, &next_char, 1 , 0 );
139173
174+ if (err == 0 ) {
175+ is_connected_ = false ;
176+ return false ;
177+ }
140178 if (err == SOCKET_ERROR) {
141- LogError (" Received socket error reading next byte from bluetooth" );
179+ LogError (" Received socket error reading next byte from bluetooth" , true );
142180
143181 return false ;
144182 }
@@ -156,7 +194,7 @@ bool BluetoothCommunicationService::RawWrite(const std::string& buff) {
156194
157195 std::scoped_lock lock (io_mutex_);
158196 if (send (sock_, cbuff, strlen (cbuff), 0 ) < 0 ) {
159- LogError (" Failed to send data to bluetooth device" );
197+ LogError (" Failed to send data to bluetooth device" , true );
160198
161199 return false ;
162200 }
@@ -173,11 +211,18 @@ BluetoothCommunicationService::~BluetoothCommunicationService() {
173211 std::scoped_lock lock (io_mutex_);
174212 if (is_connected_.exchange (false )) {
175213 if (shutdown (sock_, SD_BOTH) == SOCKET_ERROR) {
176- LogError (" Failed to disconnect from bluetooth socket device" );
214+ LogError (" Failed to disconnect from bluetooth socket device" , true );
177215 }
178216
217+ closesocket (sock_);
218+ sock_ = INVALID_SOCKET;
219+
179220 is_connected_ = false ;
180221 }
222+
223+ if (WSACleanup ()) {
224+ LogError (" WSA failed to cleanup" , true );
225+ }
181226}
182227
183228std::string BluetoothCommunicationService::GetIdentifier () {
0 commit comments