Skip to content

Commit

Permalink
simulator: make simulator the server side in socket connection
Browse files Browse the repository at this point in the history
In the first implementation of socket connection in simulator, it was acting
as a client in the communication, trying to connect to a specific network host
and port. It was done like this because `send_message.py` running in host
machine was having problems connecting to a simulator

But actually, now that we do not need to connect BitBox02 device physically
while running the simulator, we also do not need to run `send_message.py` in
the host machine. Also ideally simulator should be a server hosting the
firmware to the clients.

Signed-off-by: asi345 <[email protected]>
  • Loading branch information
asi345 committed Feb 8, 2024
1 parent 1c5491d commit 835a8a1
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 110 deletions.
16 changes: 6 additions & 10 deletions py/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,28 +1573,24 @@ class Simulator(PhysicalLayer):
def __init__(self) -> None:
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 15423
self.client_socket.bind(("", port))
self.client_socket.listen(50)
print(f"Waiting for connection on port {port}")
self.connection, addr = self.client_socket.accept()
print(f"Connected to {addr}")
self.client_socket.connect(("127.0.0.1", port))
if debug:
print("Connected to the simulator")

def write(self, data: bytes) -> None:
self.connection.send(data[1:])
self.client_socket.send(data[1:])
if debug:
print(f"Written to the simulator:\n{data.hex()[2:]}")

def read(self, size: int, timeout_ms: int) -> bytes:
res = self.connection.recv(64)
res = self.client_socket.recv(64)
if debug:
print(f"Read from the simulator:\n{res.hex()}")
return res

def __del__(self) -> None:
print("Simulator quit")
if self.connection:
self.connection.shutdown(socket.SHUT_RDWR)
self.connection.close()
self.client_socket.close()

simulator = Simulator()

Expand Down
8 changes: 1 addition & 7 deletions test/simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Shift Cryptosecurity AG
# Copyright 2024 Shift Cryptosecurity AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,6 @@ set(IGNORE_SOURCES
"src/memory/smarteeprom.c"
"src/memory.mpu.c"
)
#f9b3ceaa91e

# Exclude some files which depends on the hardware.
foreach(SOURCEFILE ${IGNORE_SOURCES})
Expand Down Expand Up @@ -169,11 +168,6 @@ if(SANTIZE_UNDEFINED)
target_compile_options(bitbox_objects-simulator PUBLIC "-fsanitize=undefined")
target_compile_options(bitbox-simulator PUBLIC "-fsanitize=undefined")
endif()
if(COVERAGE)
target_link_libraries(bitbox-simulator PUBLIC "--coverage")
target_compile_options(bitbox_objects-simulator PUBLIC "--coverage")
target_compile_options(bitbox-simulator PUBLIC "--coverage")
endif()

#-----------------------------------------------------------------------------
# Simulator
Expand Down
15 changes: 3 additions & 12 deletions test/simulator/framework/eh_personality.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Needed to link the C unit test executables in /test/unit-test, which link to bitbox_merged.
// `rust_eh_personality` is provided by Rust when building the firmware or running Rust unit tests
//
// See
// https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib.
//
// One could get rid of this and also considerably shrink the binary size by compiling core instead
// of using pre-built binaries. See a proof of concept implementation here:
// https://github.com/digitalbitbox/bitbox02-firmware/tree/build-std-PoC. We decided against doing
// this for now as the feature seems immature and because of the warnings against using it in
// production:
// https://github.com/rust-lang/wg-cargo-std-aware/tree/81765f0eb744b9c47840c16f43a32c9f61fd7f0c#mvp-implementation
// Needed to link the simulator executable in /test/simulator, which link to
// bitbox_merged-simulator. `rust_eh_personality` is provided by Rust when building the firmware or
// running Rust unit tests.
void rust_eh_personality(void);
void rust_eh_personality(void) {}
6 changes: 0 additions & 6 deletions test/simulator/framework/includes/mock_qtouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

#include <stdint.h>

void __wrap_qtouch_process(void);

uint8_t __wrap_qtouch_get_scroller_is_active(uint16_t sensor_node);

uint16_t __wrap_qtouch_get_scroller_position(uint16_t sensor_node);

void qtouch_process(void);

uint8_t qtouch_is_scroller_active(uint16_t sensor_node);
Expand Down
25 changes: 0 additions & 25 deletions test/simulator/framework/includes/test_random.h

This file was deleted.

35 changes: 1 addition & 34 deletions test/simulator/framework/mock_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,4 @@ void memory_read_chunk_mock(uint32_t chunk_num, uint8_t* chunk_out)
void memory_read_shared_bootdata_mock(uint8_t* chunk_out)
{
memcpy(chunk_out, _memory_shared_data, FLASH_SHARED_DATA_LEN);
}

static uint8_t _encrypted_seed_and_hmac[96];
static uint8_t _encrypted_seed_and_hmac_len;

bool __wrap_memory_set_encrypted_seed_and_hmac(uint8_t* encrypted_seed_and_hmac, uint8_t len)
{
memcpy(_encrypted_seed_and_hmac, encrypted_seed_and_hmac, len);
_encrypted_seed_and_hmac_len = len;
return true;
}

bool __wrap_memory_get_encrypted_seed_and_hmac(
uint8_t* encrypted_seed_and_hmac_out,
uint8_t* len_out)
{
*len_out = _encrypted_seed_and_hmac_len;
memcpy(encrypted_seed_and_hmac_out, _encrypted_seed_and_hmac, *len_out);
return true;
}

static uint8_t _salt_root[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
};
void mock_memory_set_salt_root(const uint8_t* salt_root)
{
memcpy(_salt_root, salt_root, 32);
}
bool __wrap_memory_get_salt_root(uint8_t* salt_root_out)
{
memcpy(salt_root_out, _salt_root, 32);
return true;
}
}
34 changes: 19 additions & 15 deletions test/simulator/simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
#define BUFFER_SIZE 1024

int data_len;
int sockfd;
int commfd;

int get_usb_message_socket(uint8_t* input)
{
return read(sockfd, input, USB_HID_REPORT_OUT_SIZE);
return read(commfd, input, USB_HID_REPORT_OUT_SIZE);
}

void send_usb_message_socket(void)
Expand All @@ -48,7 +48,7 @@ void send_usb_message_socket(void)
const uint8_t* data = queue_pull(q);
if (data != NULL) {
data_len = 256 * (int)data[5] + (int)data[6];
if (!write(sockfd, data, USB_HID_REPORT_OUT_SIZE)) {
if (!write(commfd, data, USB_HID_REPORT_OUT_SIZE)) {
perror("ERROR, could not write to socket");
exit(1);
}
Expand All @@ -67,27 +67,31 @@ int main(void)
{
// Establish socket connection with client
int portno = 15423;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
return 1;
}
struct hostent* server = gethostbyname("host.docker.internal");
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
return 1;
}
struct sockaddr_in serv_addr;
memset((char*)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr_list[0], server->h_length);
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR, could not connect to client");
int serv_addr_len = sizeof(serv_addr);
if (bind(sockfd, (struct sockaddr*)&serv_addr, serv_addr_len) < 0) {
perror("ERROR binding socket");
return 1;
}
if (listen(sockfd, 50) < 0) {
perror("ERROR listening on socket");
return 1;
}
if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) < 0) {
perror("accept");
return 1;
}
printf("Socket connection setup success\n");

// BitBox02 simulation initializaition
// BitBox02 simulation initialization
usb_processing_init();
usb_processing_set_send(usb_processing_hww(), send_usb_message_socket);
printf("USB setup success\n");
Expand Down Expand Up @@ -137,6 +141,6 @@ int main(void)
temp_len -= (USB_HID_REPORT_OUT_SIZE - 5);
}
}
close(sockfd);
close(commfd);
return 0;
}
1 change: 0 additions & 1 deletion test/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ set(IGNORE_SOURCES
"src/memory/smarteeprom.c"
"src/memory.mpu.c"
)
#f9b3ceaa91e

# Exclude some files which depends on the hardware.
foreach(SOURCEFILE ${IGNORE_SOURCES})
Expand Down

0 comments on commit 835a8a1

Please sign in to comment.