Skip to content

Commit 20547b2

Browse files
committed
simulator : multiple sequential client support
An issue that was faced while implementing HWI integration was multiple client support. In HWI, one client could access to simulator for initialization and then disconnect, while newer clients could connect and directly test the functionalities of wallet API, assuming that the device is already initialized. Parallel handling of clients are still not supported, also not expected from HWI. Signed-off-by: asi345 <[email protected]>
1 parent 4e91e75 commit 20547b2

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

test/simulator/simulator.c

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,6 @@ void simulate_firmware_execution(const uint8_t* input)
6565

6666
int main(void)
6767
{
68-
// Establish socket connection with client
69-
int portno = 15423;
70-
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
71-
if (sockfd < 0) {
72-
perror("ERROR opening socket");
73-
return 1;
74-
}
75-
struct sockaddr_in serv_addr;
76-
serv_addr.sin_family = AF_INET;
77-
serv_addr.sin_addr.s_addr = INADDR_ANY;
78-
serv_addr.sin_port = htons(portno);
79-
int serv_addr_len = sizeof(serv_addr);
80-
if (bind(sockfd, (struct sockaddr*)&serv_addr, serv_addr_len) < 0) {
81-
perror("ERROR binding socket");
82-
return 1;
83-
}
84-
if (listen(sockfd, 50) < 0) {
85-
perror("ERROR listening on socket");
86-
return 1;
87-
}
88-
if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) < 0) {
89-
perror("accept");
90-
return 1;
91-
}
92-
printf("Socket connection setup success\n");
93-
9468
// BitBox02 simulation initialization
9569
usb_processing_init();
9670
usb_processing_set_send(usb_processing_hww(), send_usb_message_socket);
@@ -121,26 +95,57 @@ int main(void)
12195
bitbox02_smarteeprom_init();
12296
idle_workflow_blocking();
12397

124-
// BitBox02 firmware loop
125-
uint8_t input[BUFFER_SIZE];
126-
int temp_len;
98+
// Establish socket connection with client
99+
int portno = 15423;
100+
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
101+
if (sockfd < 0) {
102+
perror("ERROR opening socket");
103+
return 1;
104+
}
105+
struct sockaddr_in serv_addr;
106+
serv_addr.sin_family = AF_INET;
107+
serv_addr.sin_addr.s_addr = INADDR_ANY;
108+
serv_addr.sin_port = htons(portno);
109+
int serv_addr_len = sizeof(serv_addr);
110+
if (bind(sockfd, (struct sockaddr*)&serv_addr, serv_addr_len) < 0) {
111+
perror("ERROR binding socket");
112+
return 1;
113+
}
114+
if (listen(sockfd, 50) < 0) {
115+
perror("ERROR listening on socket");
116+
return 1;
117+
}
127118
while (1) {
128-
// Simulator polls for USB messages from client and then processes them
129-
if (!get_usb_message_socket(input)) break;
130-
simulate_firmware_execution(input);
119+
if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) <
120+
0) {
121+
perror("accept");
122+
return 1;
123+
}
124+
printf("Socket connection setup success\n");
125+
126+
// BitBox02 firmware loop
127+
uint8_t input[BUFFER_SIZE];
128+
int temp_len;
129+
while (1) {
130+
// Simulator polls for USB messages from client and then processes them
131+
if (!get_usb_message_socket(input)) break;
132+
simulate_firmware_execution(input);
131133

132-
// If the USB message to be sent from firmware is bigger than one packet,
133-
// then the simulator sends the message in multiple packets. Packets use
134-
// HID format, just like the real USB messages.
135-
temp_len = data_len - (USB_HID_REPORT_OUT_SIZE - 7);
136-
while (temp_len > 0) {
137-
// When USB message processing function is called without a new
138-
// input, then it does not consume any packets but it still calls
139-
// the send function to send further USB messages
140-
usb_processing_process(usb_processing_hww());
141-
temp_len -= (USB_HID_REPORT_OUT_SIZE - 5);
134+
// If the USB message to be sent from firmware is bigger than one packet,
135+
// then the simulator sends the message in multiple packets. Packets use
136+
// HID format, just like the real USB messages.
137+
temp_len = data_len - (USB_HID_REPORT_OUT_SIZE - 7);
138+
while (temp_len > 0) {
139+
// When USB message processing function is called without a new
140+
// input, then it does not consume any packets but it still calls
141+
// the send function to send further USB messages
142+
usb_processing_process(usb_processing_hww());
143+
temp_len -= (USB_HID_REPORT_OUT_SIZE - 5);
144+
}
142145
}
146+
close(commfd);
147+
printf("Socket connection closed\n");
148+
printf("Waiting for new clients, CTRL+C to shut down the simulator\n");
143149
}
144-
close(commfd);
145150
return 0;
146151
}

0 commit comments

Comments
 (0)