Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust protobuf API #525

Merged
merged 6 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .ci/ci
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ make prepare-tidy
make -j8 unit-test
make -j8 run-unit-tests
make -j8 run-rust-unit-tests

# Rust linter
make -j8 run-rust-clippy

# Check that coverage report building is working.
make coverage

Expand Down
7 changes: 7 additions & 0 deletions messages/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ add_custom_command(
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}
'--nanopb_out=-I${CMAKE_CURRENT_SOURCE_DIR}:${CMAKE_CURRENT_BINARY_DIR}'
${PROTO_FILES_ABSOLUTE}
# We build the Rust protobuf files here and put them straight into the crate.
# This way, the crate can be compiled and tested without relying on cmake environment vars.
# Using prost-build the normal way as part of build.rs does not work due to a cargo bug:
# https://github.com/danburkert/prost/issues/344#issuecomment-650721245
COMMAND
${CMAKE_COMMAND} -E env
cargo run --manifest-path=${CMAKE_SOURCE_DIR}/tools/prost-build/Cargo.toml -- --messages-dir=${CMAKE_CURRENT_SOURCE_DIR} --out-dir=${CMAKE_SOURCE_DIR}/src/rust/bitbox02-rust/src/hww/api/
)

add_custom_target(
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ add_custom_target(rust-bindgen
--whitelist-type commander_error_t
--rustified-enum commander_error_t
--whitelist-function commander
--whitelist-function commander_states_can_call
--whitelist-function commander_states_clear_force_next
--whitelist-type BitBoxBaseRequest
--whitelist-var ".*_tag"
--whitelist-var MAX_LABEL_SIZE
Expand Down
36 changes: 2 additions & 34 deletions src/commander/commander.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ static commander_error_t _api_get_info(DeviceInfoResponse* device_info)
return COMMANDER_OK;
}

static commander_error_t _api_set_device_name(const SetDeviceNameRequest* request)
{
const confirm_params_t params = {
.title = "Name",
.body = request->name,
.scrollable = true,
};
if (!workflow_confirm_blocking(&params)) {
return COMMANDER_ERR_USER_ABORT;
}
if (!memory_set_device_name(request->name)) {
return COMMANDER_ERR_MEMORY;
}
return COMMANDER_OK;
}

static commander_error_t _api_set_password(const SetPasswordRequest* request)
{
if (!workflow_create_seed(request->entropy)) {
Expand Down Expand Up @@ -282,9 +266,6 @@ static commander_error_t _api_process(const Request* request, Response* response
case Request_device_info_tag:
response->which_response = Response_device_info_tag;
return _api_get_info(&(response->response.device_info));
case Request_device_name_tag:
response->which_response = Response_success_tag;
return _api_set_device_name(&(request->request.device_name));
case Request_set_password_tag:
response->which_response = Response_success_tag;
return _api_set_password(&(request->request.set_password));
Expand Down Expand Up @@ -381,17 +362,8 @@ void commander(const in_buffer_t* in_buf, buffer_t* out_buf)
commander_error_t err =
protobuf_decode(in_buf, &request) ? COMMANDER_OK : COMMANDER_ERR_INVALID_INPUT;
if (err == COMMANDER_OK) {
if (!commander_states_can_call(request.which_request)) {
err = COMMANDER_ERR_INVALID_STATE;
} else {
// Since we will process the call now, so can clear the 'force next' info.
// We do this before processing as the api call can potentially define the next api call
// to be forced.
commander_states_clear_force_next();

err = _api_process(&request, &response);
util_zero(&request, sizeof(request));
}
err = _api_process(&request, &response);
util_zero(&request, sizeof(request));
}
if (err != COMMANDER_OK) {
_report_error(&response, err);
Expand All @@ -401,10 +373,6 @@ void commander(const in_buffer_t* in_buf, buffer_t* out_buf)
}

#ifdef TESTING
commander_error_t commander_api_set_device_name(const SetDeviceNameRequest* request)
{
return _api_set_device_name(request);
}
commander_error_t commander_api_set_mnemonic_passphrase_enabled(
const SetMnemonicPassphraseEnabledRequest* request)
{
Expand Down
51 changes: 51 additions & 0 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ lto = true

[profile.dev]
opt-level = 'z'

[patch.crates-io]
bytes = { git = "https://github.com/digitalbitbox/bytes.git", branch = "bitbox02-firmware-20200702" }
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust-c/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl CStrMut {
/// then are available.
pub fn write<F>(&mut self, req: usize, f: F)
where
F: FnOnce(&mut [u8]) -> (),
F: FnOnce(&mut [u8]),
{
// Must be room for requested amount of bytes and null terminator.
if self.cap - self.len < req + 1 {
Expand Down
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust-c/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub unsafe extern "C" fn rust_workflow_confirm_blocking(
) -> bool {
let title = crate::util::rust_util_cstr(params.title);
let body = crate::util::rust_util_cstr(params.body);
if params.font != core::ptr::null() {
if !params.font.is_null() {
panic!("Only default font supported");
}
let params = confirm::Params {
Expand Down
7 changes: 7 additions & 0 deletions src/rust/bitbox02-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ bitbox02-noise = {path = "../bitbox02-noise"}
version = "0.5.1"
# Disable the "std" feature
default-features = false

[dependencies.prost]
git = "https://github.com/danburkert/prost.git"
# keep rev in sync with tools/prost-build/Cargo.toml
rev = "6113789f70b69709820becba4242824b4fb3ffec"
default-features = false
features = ["prost-derive"]
Loading