Skip to content

Commit 9e1f2fd

Browse files
committed
clean up various parts of bluetooth service
1 parent 9c48d43 commit 9e1f2fd

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

.github/workflows/build-develop.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ jobs:
3434
with:
3535
runVcpkgInstall: true
3636
vcpkgJsonGlob: 'vcpkg.json'
37-
appendedCacheKey: vcpkginstall
3837
vcpkgGitCommitId: '76a382b7b8ce7ed30bbf976aa9aecae868594d5e'
3938

4039
- name: Prints output of run-vcpkg's action.

server/src/communication/services/service_bluetooth_win.cpp

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace og;
1717

1818
static 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+
4162
BluetoothCommunicationService::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

5278
bool 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

183228
std::string BluetoothCommunicationService::GetIdentifier() {

server/src/communication/services/service_bluetooth_win.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BluetoothCommunicationService : public ICommunicationService {
3535
private:
3636
bool Connect();
3737

38-
void LogError(const std::string&, bool with_win_error) const;
38+
void LogError(const std::string&, bool wsa_error, bool with_win_error) const;
3939

4040
og::DeviceBluetoothCommunicationConfiguration configuration_;
4141

server/src/device/lucidgloves/discovery/lucidgloves_fw_discovery.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121

2222
static og::Logger& logger = og::Logger::GetInstance();
2323

24-
static const std::vector<SerialProberIdentifier> lucidgloves_serial_ids = {
25-
{"10C4", "EA60"}, // cp2102
26-
{"7523", "7524"} // ch340
27-
};
28-
static const std::vector<std::string> lucidgloves_bt_ids = {"lucidgloves", "lucidgloves-left", "lucidgloves-right"};
29-
3024
LucidglovesDeviceDiscoverer::LucidglovesDeviceDiscoverer(
3125
og::CommunicationConfiguration communication_configuration, std::vector<og::DeviceConfiguration> device_configurations)
3226
: device_configurations_(std::move(device_configurations)), communication_configuration_(communication_configuration) {}
@@ -44,6 +38,7 @@ void LucidglovesDeviceDiscoverer::StartDiscovery(std::function<void(std::unique_
4438
logger.Log(og::kLoggerLevel_Info, "Setting up bluetooth probers...");
4539

4640
for (const auto& device_configuration : device_configurations_) {
41+
logger.Log(og::kLoggerLevel_Info, "Setting up bluetooth prober for: %s", device_configuration.communication.bluetooth.name.c_str());
4742
const og::DeviceBluetoothCommunicationConfiguration& configuration = device_configuration.communication.bluetooth;
4843
BluetoothPortProberConfiguration prober_configuration{configuration.name};
4944

@@ -62,6 +57,7 @@ void LucidglovesDeviceDiscoverer::StartDiscovery(std::function<void(std::unique_
6257
logger.Log(og::kLoggerLevel_Info, "Setting up serial probers...");
6358

6459
for (const auto& device_configuration : device_configurations_) {
60+
logger.Log(og::kLoggerLevel_Info, "Setting up serial prober for: %s", device_configuration.communication.bluetooth.name.c_str());
6561
const og::DeviceSerialCommunicationConfiguration& configuration = device_configuration.communication.serial;
6662

6763
SerialPortProberConfiguration prober_configuration{configuration.port_name};

0 commit comments

Comments
 (0)