Skip to content

Commit 20c4232

Browse files
Driver version support (#59)
* Update build instructions and fix protobuf java package * Fix message size not accounting for negative numbers * Add version support via protobuf * Update tests for version message * Apply suggestions from code review --------- Co-authored-by: Butterscotch! <[email protected]>
1 parent dae39a3 commit 20c4232

File tree

9 files changed

+49
-8
lines changed

9 files changed

+49
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ vcpkg_installed/
2929
# Project-specific
3030
build/*
3131
out/
32+
/.vscode

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,18 @@ Run the bootstrap script to build vcpkg binary `.\vcpkg\bootstrap-vcpkg.bat` or
4646

4747
After installing vcpkg if you're on Windows, you need to run `vcpkg integrate install` command from the vcpkg folder to integrate it for VSCode.
4848

49-
For other systems and IDEs instructions are not available as of now, contributions are welcome.
49+
Then you will need to run `cmake -B build` once to setup CMake.
50+
51+
Finally, everytime you want to build binaries, just run `cmake --build build --config Release`. The newly built binary will be in the build/release directory.
52+
53+
For other systems and IDEs, instructions are not available as of now, but contributions are welcome.
5054

5155
### Updating vcpkg packages
5256

53-
To update vcpkg packages set the vcpkg registry submodule to a newer commit and rerun the bootstrap script.
57+
To update vcpkg packages set the vcpkg registry submodule to a newer commit and rerun the bootstrap script.
58+
59+
### Updating protobuf messages
60+
61+
You can modify the protobuf messages for communication between the server and this driver in [src\bridge\ProtobufMessages.proto](src\bridge\ProtobufMessages.proto). When making breaking changes, please update PROTOCOL_VERSION in [src\bridge\BridgeClient.hpp](src\bridge\BridgeClient.hpp).
62+
63+
To update the protobuf messages on the server, you will need to install [protoc](https://protobuf.dev/installation/) (we use version 4.31.1), then make sure that your SlimeVR-Server and SlimeVR-OpenVR-Driver repositories are in the same parent directory and run `protobuf_update.bat` located at `SlimeVR-Server\server\desktop`.

src/bridge/BridgeClient.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void BridgeClient::CreateConnection() {
4040
logger_->Log("[{}] connected", path);
4141
connected_ = true;
4242
last_error_ = std::nullopt;
43+
SendVersion();
4344
});
4445
connection_handle_->on<uvw::end_event>([this, path](const uvw::end_event&, uvw::pipe_handle&) {
4546
logger_->Log("[{}] disconnected", path);
@@ -79,3 +80,12 @@ void BridgeClient::CloseConnectionHandles() {
7980
if (reconnect_timeout_) reconnect_timeout_->close();
8081
connected_ = false;
8182
}
83+
84+
void BridgeClient::SendVersion() {
85+
messages::ProtobufMessage* message = google::protobuf::Arena::CreateMessage<messages::ProtobufMessage>(&arena_);
86+
messages::Version* version = google::protobuf::Arena::CreateMessage<messages::Version>(&arena_);
87+
message->set_allocated_version(version);
88+
version->set_protocol_version(PROTOCOL_VERSION);
89+
SendBridgeMessage(*message);
90+
arena_.Reset();
91+
}

src/bridge/BridgeClient.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "BridgeTransport.hpp"
3030

31+
#define PROTOCOL_VERSION 1
32+
3133
/**
3234
* @brief Client implementation for communication with SlimeVR Server using pipes or unix sockets.
3335
*
@@ -49,8 +51,11 @@ class BridgeClient: public BridgeTransport {
4951
void ResetConnection() override;
5052
void CloseConnectionHandles() override;
5153
void Reconnect();
54+
void SendVersion();
5255

5356
std::optional<std::string> last_error_;
5457
std::optional<std::string> last_path_;
5558
std::shared_ptr<uvw::timer_handle> reconnect_timeout_;
59+
60+
google::protobuf::Arena arena_;
5661
};

src/bridge/BridgeTransport.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ void BridgeTransport::OnRecv(const uvw::data_event& event) {
8181
char len_buf[4];
8282
recv_buf_.Peek(len_buf, 4);
8383
uint32_t size = 0;
84-
size |= static_cast<uint32_t>(static_cast<uint8_t>(len_buf[0])) << 0;
85-
size |= static_cast<uint32_t>(static_cast<uint8_t>(len_buf[1])) << 8;
86-
size |= static_cast<uint32_t>(static_cast<uint8_t>(len_buf[2])) << 16;
87-
size |= static_cast<uint32_t>(static_cast<uint8_t>(len_buf[3])) << 24;
84+
size = static_cast<uint32_t>(static_cast<uint8_t>(len_buf[0])) |
85+
(static_cast<uint32_t>(static_cast<uint8_t>(len_buf[1])) << 8) |
86+
(static_cast<uint32_t>(static_cast<uint8_t>(len_buf[2])) << 16) |
87+
(static_cast<uint32_t>(static_cast<uint8_t>(len_buf[3])) << 24);
8888

8989
if (size > VRBRIDGE_MAX_MESSAGE_SIZE) {
9090
logger_->Log(

src/bridge/ProtobufMessages.proto

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ package messages;
4747
* recieve messages this frame.
4848
*/
4949

50-
option java_package = "dev.slimevr.bridge";
50+
option java_package = "dev.slimevr.desktop.platform";
5151
option java_outer_classname = "ProtobufMessages";
5252
option optimize_for = LITE_RUNTIME;
5353

5454
message PingPong {
5555
}
5656

57+
message Version {
58+
int32 protocol_version = 1;
59+
}
60+
5761
message Position {
5862
int32 tracker_id = 1;
5963
optional float x = 2;
@@ -117,5 +121,6 @@ message ProtobufMessage {
117121
TrackerAdded tracker_added = 3;
118122
TrackerStatus tracker_status = 4;
119123
Battery battery = 5;
124+
Version version = 6;
120125
}
121126
}

test/TestBridgeClientMock.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ TEST_CASE("IO with a mock server", "[Bridge]") {
7474
tracker_position->set_qw(0);
7575
server_mock->SendBridgeMessage(*server_message);
7676
}
77-
} else {
77+
} else if(message.has_version()) {
78+
TestLogVersion(logger, message);
79+
}
80+
else {
7881
invalid_messages++;
7982
}
8083

test/common/TestBridgeClient.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ void TestLogTrackerStatus(std::shared_ptr<Logger> logger, const messages::Protob
2525
}
2626
}
2727

28+
void TestLogVersion(std::shared_ptr<Logger> logger, const messages::ProtobufMessage& message) {
29+
if (!message.has_version()) return;
30+
messages::Version version = message.version();
31+
logger->Log("protocol version {}", version.protocol_version());
32+
}
33+
2834
void TestBridgeClient() {
2935
using namespace std::chrono;
3036

test/common/TestBridgeClient.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
void TestLogTrackerAdded(std::shared_ptr<Logger> logger, const messages::ProtobufMessage& message);
1010
void TestLogTrackerStatus(std::shared_ptr<Logger> logger, const messages::ProtobufMessage& message);
11+
void TestLogVersion(std::shared_ptr<Logger> logger, const messages::ProtobufMessage& message);
1112
void TestBridgeClient();

0 commit comments

Comments
 (0)