Skip to content

Commit 762d58f

Browse files
committed
pbio/platform/virtual_hub: Move socket code to platform.
We want to be able to use this in a few other places.
1 parent cda7489 commit 762d58f

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

lib/pbio/drv/bluetooth/bluetooth_simulation.c

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <unistd.h>
13-
#include <arpa/inet.h>
1413

1514
#include "bluetooth.h"
1615
#include <pbdrv/bluetooth.h>
@@ -51,10 +50,6 @@ bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection) {
5150
return false;
5251
}
5352

54-
// Socket used to send data to Python animation.
55-
static int data_socket = -1;
56-
static struct sockaddr_in serv_addr;
57-
5853
pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *state, const uint8_t *data, uint16_t size) {
5954
PBIO_OS_ASYNC_BEGIN(state);
6055

@@ -64,18 +59,8 @@ pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *s
6459
(void)ret;
6560
}
6661

67-
// No output configured, so done.
68-
if (data_socket < 0) {
69-
return PBIO_SUCCESS;
70-
}
71-
72-
// Send the value notification via socket.
73-
ssize_t sent = sendto(data_socket, data, size, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
74-
if (sent < 0) {
75-
printf("send() failed");
76-
close(data_socket);
77-
data_socket = -1;
78-
}
62+
extern void virtual_hub_socket_send(const uint8_t *data, uint32_t size);
63+
virtual_hub_socket_send(data, size);
7964

8065
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
8166
}
@@ -195,24 +180,6 @@ void pbdrv_bluetooth_init_hci(void) {
195180
bluetooth_thread_err = PBIO_ERROR_AGAIN;
196181
bluetooth_thread_state = 0;
197182
pbio_os_process_start(&pbdrv_bluetooth_simulation_process, pbdrv_bluetooth_simulation_process_thread, NULL);
198-
199-
// Configure socket if specified.
200-
if (getenv("PBIO_TEST_CONNECT_SOCKET")) {
201-
data_socket = socket(AF_INET, SOCK_DGRAM, 0);
202-
if (data_socket < 0) {
203-
printf("socket() failed");
204-
return;
205-
}
206-
serv_addr.sin_family = AF_INET;
207-
serv_addr.sin_port = htons(5002);
208-
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
209-
printf("inet_pton() failed");
210-
close(data_socket);
211-
data_socket = -1;
212-
return;
213-
}
214-
}
215-
216183
}
217184

218185
#endif // PBDRV_CONFIG_BLUETOOTH_SIMULATION

lib/pbio/platform/virtual_hub/platform.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdlib.h>
99
#include <termios.h>
1010
#include <unistd.h>
11+
#include <arpa/inet.h>
1112

1213
#include "../../drv/motor_driver/motor_driver_virtual_simulation.h"
1314

@@ -141,11 +142,49 @@ const pbdrv_motor_driver_virtual_simulation_platform_data_t
141142
},
142143
};
143144

145+
// Socket used to send data to Python animation.
146+
static int data_socket = -1;
147+
static struct sockaddr_in serv_addr;
148+
149+
void virtual_hub_socket_send(const uint8_t *data, uint32_t size) {
150+
if (data_socket < 0) {
151+
return;
152+
}
153+
ssize_t sent = sendto(data_socket, data, size, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
154+
if (sent < 0) {
155+
printf("send() failed");
156+
close(data_socket);
157+
data_socket = -1;
158+
}
159+
}
160+
161+
static void virtual_hub_socket_init(void) {
162+
if (!getenv("PBIO_TEST_CONNECT_SOCKET")) {
163+
return;
164+
}
165+
data_socket = socket(AF_INET, SOCK_DGRAM, 0);
166+
if (data_socket < 0) {
167+
printf("socket() failed");
168+
return;
169+
}
170+
serv_addr.sin_family = AF_INET;
171+
serv_addr.sin_port = htons(5002);
172+
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
173+
printf("inet_pton() failed");
174+
close(data_socket);
175+
data_socket = -1;
176+
return;
177+
}
178+
}
179+
144180
// The 'embedded' main.
145181
extern void _main(void);
146182

147183
int main(int argc, char **argv) {
148184

185+
// Optional output via animated hub.
186+
virtual_hub_socket_init();
187+
149188
// Separate heap for large allocations - defined in linker script.
150189
static uint8_t umm_heap[1024 * 1024 * 2];
151190
umm_init_heap(umm_heap, sizeof(umm_heap));

0 commit comments

Comments
 (0)