|
17 | 17 | #include <pbio/error.h> |
18 | 18 | #include <pbio/os.h> |
19 | 19 |
|
| 20 | +#include <lwrb/lwrb.h> |
| 21 | + |
| 22 | +/** |
| 23 | + * Host is subscribed to our outgoing event messages. |
| 24 | + */ |
| 25 | +static bool pbdrv_usb_events_subscribed; |
| 26 | + |
| 27 | +bool pbdrv_usb_connection_is_active(void) { |
| 28 | + return pbdrv_usb_events_subscribed; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Pybricks system command handler. |
| 33 | + */ |
| 34 | +static pbdrv_usb_receive_handler_t pbdrv_usb_receive_handler; |
| 35 | + |
20 | 36 | void pbdrv_usb_set_receive_handler(pbdrv_usb_receive_handler_t handler) { |
| 37 | + pbdrv_usb_receive_handler = handler; |
21 | 38 | } |
22 | 39 |
|
| 40 | +/** |
| 41 | + * Buffer scheduled status. |
| 42 | + */ |
| 43 | +static uint8_t status_data[PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE]; |
| 44 | +static bool status_data_pending; |
| 45 | + |
23 | 46 | void pbdrv_usb_schedule_status_update(const uint8_t *status_msg) { |
| 47 | + // Ignore if message identical to last. |
| 48 | + if (!memcmp(status_data, status_msg, sizeof(status_data))) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // Schedule to send whenever the Bluetooth process gets round to it. |
| 53 | + memcpy(status_data, status_msg, sizeof(status_data)); |
| 54 | + status_data_pending = true; |
| 55 | + pbio_os_request_poll(); |
24 | 56 | } |
25 | 57 |
|
| 58 | +/** |
| 59 | + * Buffer for scheduled stdout. |
| 60 | + */ |
| 61 | +static lwrb_t stdout_ring_buf; |
| 62 | + |
26 | 63 | pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) { |
| 64 | + |
| 65 | + if (!pbdrv_usb_connection_is_active()) { |
| 66 | + return PBIO_ERROR_INVALID_OP; |
| 67 | + } |
| 68 | + |
| 69 | + // Buffer data to send it more efficiently even if the caller is only |
| 70 | + // writing one byte at a time. |
| 71 | + if ((*size = lwrb_write(&stdout_ring_buf, data, *size)) == 0) { |
| 72 | + return PBIO_ERROR_AGAIN; |
| 73 | + } |
| 74 | + |
| 75 | + // Poke the process to start tx soon-ish. This way, we can accumulate |
| 76 | + // data bytes before actually transmitting. |
| 77 | + pbio_os_request_poll(); |
| 78 | + |
27 | 79 | return PBIO_SUCCESS; |
28 | 80 | } |
29 | 81 |
|
30 | 82 | uint32_t pbdrv_usb_stdout_tx_available(void) { |
31 | | - return UINT32_MAX; |
| 83 | + if (!pbdrv_usb_connection_is_active()) { |
| 84 | + return UINT32_MAX; |
| 85 | + } |
| 86 | + return lwrb_get_free(&stdout_ring_buf); |
32 | 87 | } |
33 | 88 |
|
34 | 89 | bool pbdrv_usb_stdout_tx_is_idle(void) { |
35 | | - return true; |
| 90 | + if (!pbdrv_usb_connection_is_active()) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + return lwrb_get_full(&stdout_ring_buf) == 0; |
36 | 94 | } |
37 | 95 |
|
38 | | -bool pbdrv_usb_connection_is_active(void) { |
39 | | - return false; |
| 96 | +static bool respond_soon; |
| 97 | +static pbio_pybricks_error_t respond_result; |
| 98 | + |
| 99 | +/** |
| 100 | + * Non-blocking poll handler to process pending incoming messages. |
| 101 | + */ |
| 102 | +static void pbdrv_usb_handle_data_in(void) { |
| 103 | + |
| 104 | + // Ignore incoming data if we haven't sent our previous response yet. |
| 105 | + if (respond_soon) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + static uint8_t data_in[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE]; |
| 110 | + uint32_t size = pbdrv_usb_get_data_in(data_in); |
| 111 | + |
| 112 | + // Expecting at least EP_MSG and payload. |
| 113 | + if (size < 2) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + switch (data_in[0]) { |
| 118 | + case PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE: |
| 119 | + pbdrv_usb_events_subscribed = data_in[1]; |
| 120 | + respond_result = PBIO_PYBRICKS_ERROR_OK; |
| 121 | + respond_soon = true; |
| 122 | + |
| 123 | + // Schedule sending current status immediately after subscribing. |
| 124 | + status_data_pending = true; |
| 125 | + break; |
| 126 | + case PBIO_PYBRICKS_OUT_EP_MSG_COMMAND: |
| 127 | + if (pbdrv_usb_receive_handler) { |
| 128 | + respond_result = pbdrv_usb_receive_handler(data_in + 1, size - 1); |
| 129 | + respond_soon = true; |
| 130 | + } |
| 131 | + break; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +static void pbdrv_usb_reset_state(void) { |
| 136 | + pbdrv_usb_events_subscribed = false; |
| 137 | + respond_soon = false; |
| 138 | + status_data_pending = false; |
| 139 | + lwrb_reset(&stdout_ring_buf); |
| 140 | +} |
| 141 | + |
| 142 | +static pbio_os_process_t pbdrv_usb_process; |
| 143 | + |
| 144 | +static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *context) { |
| 145 | + |
| 146 | + static pbio_os_state_t sub; |
| 147 | + |
| 148 | + static uint8_t out_data[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE]; |
| 149 | + static uint32_t out_size; |
| 150 | + |
| 151 | + pbio_error_t err; |
| 152 | + |
| 153 | + // Runs every time. If there is no connection, there just won't be data. |
| 154 | + pbdrv_usb_handle_data_in(); |
| 155 | + |
| 156 | + PBIO_OS_ASYNC_BEGIN(state); |
| 157 | + |
| 158 | + for (;;) { |
| 159 | + |
| 160 | + // Run charger detection: wait for USB to become physically plugged in. |
| 161 | + PBIO_OS_AWAIT(state, &sub, err = pbdrv_usb_wait_for_charger(&sub)); |
| 162 | + |
| 163 | + while (pbdrv_usb_process.request != PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL && pbdrv_usb_is_ready()) { |
| 164 | + |
| 165 | + // Find out what we should send, if anything, priotizing response, then |
| 166 | + // status, then stdout, then other events. |
| 167 | + if (respond_soon) { |
| 168 | + // Pack the response to the most recent message. |
| 169 | + out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_RESPONSE; |
| 170 | + pbio_set_uint32_le(&out_data[1], respond_result); |
| 171 | + out_size = sizeof(uint32_t) + 1; |
| 172 | + respond_soon = false; |
| 173 | + } else if (pbdrv_usb_connection_is_active() && status_data_pending) { |
| 174 | + // Send out status if pending (already includes event code). |
| 175 | + out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT; |
| 176 | + memcpy(&out_data[1], status_data, PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE); |
| 177 | + out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE(PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE); |
| 178 | + status_data_pending = false; |
| 179 | + } else if (pbdrv_usb_connection_is_active() && lwrb_get_full(&stdout_ring_buf) != 0) { |
| 180 | + // Send out stdout if anything is buffered. |
| 181 | + out_data[0] = PBIO_PYBRICKS_IN_EP_MSG_EVENT; |
| 182 | + out_data[1] = PBIO_PYBRICKS_EVENT_WRITE_STDOUT; |
| 183 | + out_size = lwrb_read(&stdout_ring_buf, &out_data[2], PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE - 2) + 2; |
| 184 | + } |
| 185 | + |
| 186 | + // If there was anything to send, send it. |
| 187 | + if (out_size) { |
| 188 | + PBIO_OS_AWAIT(state, &sub, err = pbdrv_usb_tx(&sub, out_data, out_size)); |
| 189 | + out_size = 0; |
| 190 | + if (err != PBIO_SUCCESS) { |
| 191 | + pbdrv_usb_reset_state(); |
| 192 | + PBIO_OS_AWAIT(state, &sub, pbdrv_usb_tx_reset(&sub)); |
| 193 | + } |
| 194 | + } else { |
| 195 | + // Otherwise yield once before going and check again. |
| 196 | + PBIO_OS_AWAIT_ONCE(state); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + PBIO_OS_AWAIT_WHILE(state, pbdrv_usb_is_ready()); |
| 201 | + pbdrv_usb_reset_state(); |
| 202 | + pbdrv_usb_tx_reset(&sub); |
| 203 | + } |
| 204 | + |
| 205 | + // Unreachable. On cancellation, the charger detection step in the above |
| 206 | + // loop keeps running. It will just skip the data handler. |
| 207 | + PBIO_OS_ASYNC_END(PBIO_ERROR_FAILED); |
40 | 208 | } |
41 | 209 |
|
42 | 210 | void pbdrv_usb_init(void) { |
43 | 211 | pbdrv_usb_init_device(); |
| 212 | + |
| 213 | + static uint8_t stdout_buf[PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE * 2]; |
| 214 | + lwrb_init(&stdout_ring_buf, stdout_buf, PBIO_ARRAY_SIZE(stdout_buf)); |
| 215 | + |
| 216 | + pbio_os_process_start(&pbdrv_usb_process, pbdrv_usb_process_thread, NULL); |
44 | 217 | } |
45 | 218 |
|
46 | 219 | void pbdrv_usb_deinit(void) { |
| 220 | + pbdrv_usb_deinit_device(); |
| 221 | + pbio_os_process_make_request(&pbdrv_usb_process, PBIO_OS_PROCESS_REQUEST_TYPE_CANCEL); |
47 | 222 | } |
48 | 223 |
|
49 | 224 | #endif // PBDRV_CONFIG_USB_SIMULATION |
0 commit comments