• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pybricks / pybricks-micropython / 13093213950

01 Feb 2025 10:55PM UTC coverage: 56.234% (-0.09%) from 56.324%
13093213950

push

github

dlech
pbdrv/usb: Add WebUSB platform descriptor.

Add a WebUSB platform descriptor to the BOS descriptor and a landing
page pointing to https://code.pybricks.com.

Signed-off-by: Nate Karstens <nate.karstens@gmail.com>

3852 of 6850 relevant lines covered (56.23%)

20302297.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.67
/lib/pbio/src/protocol/pybricks.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2021-2022 The Pybricks Authors
3

4
// Pybricks communication protocol
5

6
#include <stdint.h>
7

8
#include <pbio/error.h>
9
#include <pbio/protocol.h>
10
#include <pbio/util.h>
11

12
_Static_assert(NUM_PBIO_PYBRICKS_STATUS <= sizeof(uint32_t) * 8,
13
    "oh no, we added too many status flags");
14

15
/**
16
 * Writes Pybricks status report command to @p buf.
17
 *
18
 * The buffer must be at least ::PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE bytes.
19
 *
20
 * @param [in]  buf         The buffer to hold the binary data.
21
 * @param [in]  flags       The status flags.
22
 * @param [in]  program_id  Program identifier.
23
 * @return                  The number of bytes written to @p buf.
24
 */
25
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id) {
2✔
26
    buf[0] = PBIO_PYBRICKS_EVENT_STATUS_REPORT;
2✔
27
    pbio_set_uint32_le(&buf[1], flags);
2✔
28
    buf[5] = program_id;
2✔
29
    return PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE;
2✔
30
}
31

32
/**
33
 * Encodes the value of the Pybricks hub capabilities characteristic.
34
 *
35
 * @param [in]  buf                 A buffer where the result will be written.
36
 *                                  Must be at least ::PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE bytes.
37
 * @param [in]  max_char_size       The maximum characteristic value size (negotiated MTU - 3).
38
 * @param [in]  feature_flags       The feature flags.
39
 * @param [in]  max_user_prog_size  The maximum allowable size for the user program.
40
 */
41
void pbio_pybricks_hub_capabilities(uint8_t *buf,
×
42
    uint16_t max_char_size,
43
    pbio_pybricks_feature_flags_t feature_flags,
44
    uint32_t max_user_prog_size) {
45

46
    pbio_set_uint16_le(&buf[0], max_char_size);
×
47
    pbio_set_uint32_le(&buf[2], feature_flags);
×
48
    pbio_set_uint32_le(&buf[6], max_user_prog_size);
×
49
}
×
50

51
/**
52
 * Pybricks Service UUID.
53
 *
54
 * C5F50001-8280-46DA-89F4-6D8051E4AEEF
55
 *
56
 * @since Protocol v1.0.0
57
 */
58
const uint8_t pbio_pybricks_service_uuid[] = {
59
    0xC5, 0xF5, 0x00, 0x01, 0x82, 0x80, 0x46, 0xDA,
60
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
61
};
62

63
/**
64
 * Pybricks Command/Event Characteristic UUID.
65
 *
66
 * C5F50002-8280-46DA-89F4-6D8051E4AEEF
67
 *
68
 * @since Protocol v1.0.0
69
 */
70
const uint8_t pbio_pybricks_command_event_char_uuid[] = {
71
    0xC5, 0xF5, 0x00, 0x02, 0x82, 0x80, 0x46, 0xDA,
72
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
73
};
74

75
/**
76
 * Pybricks Hub Capabilities Characteristic UUID.
77
 *
78
 * C5F50003-8280-46DA-89F4-6D8051E4AEEF
79
 *
80
 * @since Protocol v1.2.0
81
 */
82
const uint8_t pbio_pybricks_hub_capabilities_char_uuid[] = {
83
    0xC5, 0xF5, 0x00, 0x03, 0x82, 0x80, 0x46, 0xDA,
84
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
85
};
86

87
// Standard BLE UUIDs used as part of Pybricks "protocol".
88

89
/** Bluetooth Device Information Service UUID. */
90
const uint16_t pbio_gatt_device_info_service_uuid = 0x180A;
91

92
/** Bluetooth Device Name Characteristic UUID. */
93
const uint16_t pbio_gatt_device_name_char_uuid = 0x2A00;
94

95
/** Bluetooth Firmware Version Characteristic UUID. */
96
const uint16_t pbio_gatt_firmware_version_char_uuid = 0x2A26;
97

98
/** Bluetooth Software Version Characteristic UUID (Pybricks protocol version). */
99
const uint16_t pbio_gatt_software_version_char_uuid = 0x2A28;
100

101
/** Bluetooth PnP ID Characteristic UUID. */
102
const uint16_t pbio_gatt_pnp_id_char_uuid = 0x2A50;
103

104
/**
105
 * Converts a ::pbio_error_t to a ::pbio_pybricks_error_t for commands.
106
 *
107
 * @param [in]  error   The ::pbio_error_t.
108
 * @returns             The equivelent ::pbio_pybricks_error_t.
109
 */
110
pbio_pybricks_error_t pbio_pybricks_error_from_pbio_error(pbio_error_t error) {
×
111
    switch (error) {
×
112
        case PBIO_SUCCESS:
113
            return PBIO_PYBRICKS_ERROR_OK;
114
        case PBIO_ERROR_INVALID_ARG:
115
            return PBIO_PYBRICKS_ERROR_VALUE_NOT_ALLOWED;
116
        case PBIO_ERROR_BUSY:
117
            return PBIO_PYBRICKS_ERROR_BUSY;
118
        case PBIO_ERROR_NOT_SUPPORTED:
119
            return PBIO_PYBRICKS_ERROR_INVALID_COMMAND;
120
        default:
121
            // to keep code size down, only know used values are included
122
            // in the map and this is a fallback in case we missed something
123
            return PBIO_PYBRICKS_ERROR_UNLIKELY_ERROR;
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc