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

pybricks / pybricks-micropython / 19016791193

02 Nov 2025 06:40PM UTC coverage: 57.167% (-2.6%) from 59.744%
19016791193

Pull #406

github

laurensvalk
bricks/virtualhub: Replace with embedded simulation.

Instead of using the newly introduced simhub alongside the virtualhub, we'll just replace the old one entirely now that it has reached feature parity. We can keep calling it the virtualhub.
Pull Request #406: New virtual hub for more effective debugging

41 of 48 new or added lines in 7 files covered. (85.42%)

414 existing lines in 53 files now uncovered.

4479 of 7835 relevant lines covered (57.17%)

17178392.75 hits per line

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

25.0
/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
 * @since Pybricks Profile v1.0.0
21
 *
22
 * Program ID parameter was added in Pybricks Profile v1.4.0.
23
 * Slot parameter was added in Pybricks Profile v1.5.0.
24
 *
25
 * @param [in]  buf         The buffer to hold the binary data.
26
 * @param [in]  flags       The status flags.
27
 * @param [in]  program_id  Program identifier of currently running program.
28
 * @param [in]  slot        The currently selected program slot.
29
 * @return                  The number of bytes written to @p buf.
30
 */
31
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id, uint8_t slot) {
87✔
32
    buf[0] = PBIO_PYBRICKS_EVENT_STATUS_REPORT;
87✔
33
    pbio_set_uint32_le(&buf[1], flags);
87✔
34
    buf[5] = program_id;
87✔
35
    buf[6] = slot;
87✔
36
    return PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE;
87✔
37
}
38

39
/**
40
 * Encodes the value of the Pybricks hub capabilities characteristic.
41
 *
42
 * @param [in]  buf                 A buffer where the result will be written.
43
 *                                  Must be at least ::PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE bytes.
44
 * @param [in]  max_char_size       The maximum characteristic value size (negotiated MTU - 3).
45
 * @param [in]  feature_flags       The feature flags.
46
 * @param [in]  max_user_prog_size  The maximum allowable size for the user program.
47
 * @param [in]  num_slots           The number of program slots available on the hub.
48
 *
49
 * @since Pybricks Profile v1.2.0
50
 *
51
 * num_slots was added in Pybricks Profile v1.5.0.
52
 */
53
void pbio_pybricks_hub_capabilities(uint8_t *buf,
×
54
    uint16_t max_char_size,
55
    pbio_pybricks_feature_flags_t feature_flags,
56
    uint32_t max_user_prog_size,
57
    uint8_t num_slots) {
58

59
    pbio_set_uint16_le(&buf[0], max_char_size);
×
60
    pbio_set_uint32_le(&buf[2], feature_flags);
×
61
    pbio_set_uint32_le(&buf[6], max_user_prog_size);
×
62
    buf[10] = num_slots;
×
63
}
×
64

65
/**
66
 * Pybricks Service UUID.
67
 *
68
 * C5F50001-8280-46DA-89F4-6D8051E4AEEF
69
 *
70
 * @since Protocol v1.0.0
71
 */
72
const uint8_t pbio_pybricks_service_uuid[] = {
73
    0xC5, 0xF5, 0x00, 0x01, 0x82, 0x80, 0x46, 0xDA,
74
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
75
};
76

77
/**
78
 * Pybricks Command/Event Characteristic UUID.
79
 *
80
 * C5F50002-8280-46DA-89F4-6D8051E4AEEF
81
 *
82
 * @since Protocol v1.0.0
83
 */
84
const uint8_t pbio_pybricks_command_event_char_uuid[] = {
85
    0xC5, 0xF5, 0x00, 0x02, 0x82, 0x80, 0x46, 0xDA,
86
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
87
};
88

89
/**
90
 * Pybricks Hub Capabilities Characteristic UUID.
91
 *
92
 * C5F50003-8280-46DA-89F4-6D8051E4AEEF
93
 *
94
 * @since Protocol v1.2.0
95
 */
96
const uint8_t pbio_pybricks_hub_capabilities_char_uuid[] = {
97
    0xC5, 0xF5, 0x00, 0x03, 0x82, 0x80, 0x46, 0xDA,
98
    0x89, 0xF4, 0x6D, 0x80, 0x51, 0xE4, 0xAE, 0xEF,
99
};
100

101
// Standard BLE UUIDs used as part of Pybricks "protocol".
102

103
/** Bluetooth Device Information Service UUID. */
104
const uint16_t pbio_gatt_device_info_service_uuid = 0x180A;
105

106
/** Bluetooth Device Name Characteristic UUID. */
107
const uint16_t pbio_gatt_device_name_char_uuid = 0x2A00;
108

109
/** Bluetooth Firmware Version Characteristic UUID. */
110
const uint16_t pbio_gatt_firmware_version_char_uuid = 0x2A26;
111

112
/** Bluetooth Software Version Characteristic UUID (Pybricks protocol version). */
113
const uint16_t pbio_gatt_software_version_char_uuid = 0x2A28;
114

115
/** Bluetooth PnP ID Characteristic UUID. */
116
const uint16_t pbio_gatt_pnp_id_char_uuid = 0x2A50;
117

118
/**
119
 * Converts a ::pbio_error_t to a ::pbio_pybricks_error_t for commands.
120
 *
121
 * @param [in]  error   The ::pbio_error_t.
122
 * @returns             The equivelent ::pbio_pybricks_error_t.
123
 */
124
pbio_pybricks_error_t pbio_pybricks_error_from_pbio_error(pbio_error_t error) {
×
125
    switch (error) {
×
UNCOV
126
        case PBIO_SUCCESS:
×
UNCOV
127
            return PBIO_PYBRICKS_ERROR_OK;
×
UNCOV
128
        case PBIO_ERROR_INVALID_ARG:
×
UNCOV
129
            return PBIO_PYBRICKS_ERROR_VALUE_NOT_ALLOWED;
×
UNCOV
130
        case PBIO_ERROR_BUSY:
×
UNCOV
131
            return PBIO_PYBRICKS_ERROR_BUSY;
×
UNCOV
132
        case PBIO_ERROR_NOT_SUPPORTED:
×
UNCOV
133
            return PBIO_PYBRICKS_ERROR_INVALID_COMMAND;
×
UNCOV
134
        default:
×
135
            // to keep code size down, only know used values are included
136
            // in the map and this is a fallback in case we missed something
UNCOV
137
            return PBIO_PYBRICKS_ERROR_UNLIKELY_ERROR;
×
138
    }
139
}
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