Skip to content

Commit 98b3883

Browse files
committed
simulator: add --port flag
We might want to use different ports or run multiple simulators in parallel for testing, in which case one must be able to specify the port.
1 parent 23eca2c commit 98b3883

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

py/send_message.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ def run(self) -> int:
15721572
return 0
15731573

15741574

1575-
def connect_to_simulator_bitbox(debug: bool) -> int:
1575+
def connect_to_simulator_bitbox(debug: bool, port: int) -> int:
15761576
"""
15771577
Connects and runs the main menu on host computer,
15781578
simulating a BitBox02 connected over USB.
@@ -1586,7 +1586,6 @@ class Simulator(PhysicalLayer):
15861586

15871587
def __init__(self) -> None:
15881588
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1589-
port = 15423
15901589
self.client_socket.connect(("127.0.0.1", port))
15911590
if debug:
15921591
print("Connected to the simulator")
@@ -1713,6 +1712,12 @@ def main() -> int:
17131712
action="store_true",
17141713
help="Connect to the BitBox02 simulator instead of a real BitBox02",
17151714
)
1715+
parser.add_argument(
1716+
"--simulator-port",
1717+
default=15423,
1718+
type=int,
1719+
help="Simulator port",
1720+
)
17161721
parser.add_argument(
17171722
"--no-cache", action="store_true", help="Don't use cached or store noise keys"
17181723
)
@@ -1734,7 +1739,7 @@ def main() -> int:
17341739
return 1
17351740

17361741
if args.simulator:
1737-
return connect_to_simulator_bitbox(args.debug)
1742+
return connect_to_simulator_bitbox(args.debug, args.simulator_port)
17381743

17391744
return connect_to_usb_bitbox(args.debug, not args.no_cache)
17401745

test/simulator/simulator.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdio.h>
2929
#include <unistd.h>
3030

31+
#include <getopt.h>
3132
#include <netdb.h>
3233
#include <netinet/in.h>
3334
#include <sys/socket.h>
@@ -63,8 +64,25 @@ void simulate_firmware_execution(const uint8_t* input)
6364
usb_processing_process(usb_processing_hww());
6465
}
6566

66-
int main(void)
67+
int main(int argc, char* argv[])
6768
{
69+
// Default port number
70+
int portno = 15423;
71+
72+
struct option long_options[] = {{"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
73+
74+
int opt;
75+
while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
76+
switch (opt) {
77+
case 'p':
78+
portno = atoi(optarg);
79+
break;
80+
default:
81+
fprintf(stderr, "Usage: %s --port <port number>\n", argv[0]);
82+
return 1;
83+
}
84+
}
85+
6886
// BitBox02 simulation initialization
6987
usb_processing_init();
7088
usb_processing_set_send(usb_processing_hww(), send_usb_message_socket);
@@ -96,7 +114,6 @@ int main(void)
96114
idle_workflow_blocking();
97115

98116
// Establish socket connection with client
99-
int portno = 15423;
100117
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
101118
if (sockfd < 0) {
102119
perror("ERROR opening socket");
@@ -115,6 +132,9 @@ int main(void)
115132
perror("ERROR listening on socket");
116133
return 1;
117134
}
135+
136+
printf("Listening on port %d\n", portno);
137+
118138
while (1) {
119139
if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) <
120140
0) {
@@ -148,4 +168,4 @@ int main(void)
148168
printf("Waiting for new clients, CTRL+C to shut down the simulator\n");
149169
}
150170
return 0;
151-
}
171+
}

0 commit comments

Comments
 (0)