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

pybricks / pybricks-micropython / 10115957565

26 Jul 2024 06:29PM UTC coverage: 56.033% (+0.008%) from 56.025%
10115957565

Pull #254

github

laurensvalk
bricks/common/modules: Reduce port view message size.

The City/Technic Hub currently do not support messages longer than 19 bytes.
Pull Request #254: Builtin programs

0 of 5 new or added lines in 2 files covered. (0.0%)

24 existing lines in 4 files now uncovered.

3766 of 6721 relevant lines covered (56.03%)

20059108.33 hits per line

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

32.14
/lib/pbio/sys/command.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2022-2023 The Pybricks Authors
3

4
#include <assert.h>
5
#include <stdint.h>
6

7
#include <pbdrv/reset.h>
8
#include <pbio/protocol.h>
9
#include <pbsys/command.h>
10
#include <pbsys/config.h>
11
#include <pbsys/storage.h>
12

13
#include "./bluetooth.h"
14
#include "./storage.h"
15
#include "./program_stop.h"
16

17
static pbsys_command_write_app_data_callback_t write_app_data_callback = NULL;
18

19
/**
20
 * Sets callback for the write program data buffer command.
21
 *
22
 * @param [in]  callback  The callback to set or @c NULL to unset.
23
 */
24
void pbsys_command_set_write_app_data_callback(pbsys_command_write_app_data_callback_t callback) {
×
25
    write_app_data_callback = callback;
×
26
}
×
27

28
/**
29
 * Parses binary data for command and dispatches handler for command.
30
 * @param [in]  data    The raw command data.
31
 * @param [in]  size    The size of @p data in bytes.
32
 */
33
pbio_pybricks_error_t pbsys_command(const uint8_t *data, uint32_t size) {
1✔
34
    assert(data);
1✔
35
    assert(size);
1✔
36

37
    pbio_pybricks_command_t cmd = data[0];
1✔
38

39
    switch (cmd) {
1✔
40
        case PBIO_PYBRICKS_COMMAND_STOP_USER_PROGRAM:
41
            pbsys_program_stop(false);
42
            return PBIO_PYBRICKS_ERROR_OK;
43
        case PBIO_PYBRICKS_COMMAND_START_USER_PROGRAM: {
NEW
44
            uint32_t id = 0;
×
NEW
45
            if (size == (1 + sizeof(uint32_t))) {
×
46
                id = pbio_get_uint32_le(&data[1]);
47
            }
NEW
48
            return pbio_pybricks_error_from_pbio_error(
×
49
                pbsys_main_program_request_start(PBSYS_MAIN_PROGRAM_TYPE_USER, id));
50
        }
51
        #if PBSYS_CONFIG_APP_BUILTIN_PROGRAMS
52
        case PBIO_PYBRICKS_COMMAND_START_BUILTIN_PROGRAM: {
53
            uint32_t id = 0;
54
            if (size == (1 + sizeof(uint32_t))) {
55
                id = pbio_get_uint32_le(&data[1]);
56
            }
57
            return pbio_pybricks_error_from_pbio_error(
58
                pbsys_main_program_request_start(PBSYS_MAIN_PROGRAM_TYPE_BUILTIN, id));
59
        }
60
        #endif // PBIO_PYBRICKS_FEATURE_TEST(PBIO_PYBRICKS_FEATURE_BUILTIN_PROGRAMS)
UNCOV
61
        case PBIO_PYBRICKS_COMMAND_WRITE_USER_PROGRAM_META:
×
62
            return pbio_pybricks_error_from_pbio_error(pbsys_storage_set_program_size(
×
63
                pbio_get_uint32_le(&data[1])));
64
        case PBIO_PYBRICKS_COMMAND_WRITE_USER_RAM:
×
UNCOV
65
            return pbio_pybricks_error_from_pbio_error(pbsys_storage_set_program_data(
×
66
                pbio_get_uint32_le(&data[1]), &data[5], size - 5));
67
        case PBIO_PYBRICKS_COMMAND_REBOOT_TO_UPDATE_MODE:
68
            pbdrv_reset(PBDRV_RESET_ACTION_RESET_IN_UPDATE_MODE);
69
            return PBIO_PYBRICKS_ERROR_OK;
70
        case PBIO_PYBRICKS_COMMAND_WRITE_STDIN:
1✔
71
            #if PBSYS_CONFIG_BLUETOOTH
72
            if (pbsys_bluetooth_rx_get_free() < size - 1) {
1✔
73
                return PBIO_PYBRICKS_ERROR_BUSY;
74
            }
75
            pbsys_bluetooth_rx_write(&data[1], size - 1);
1✔
76
            #endif
77
            // If no consumers are configured, goes to "/dev/null" without error
78
            return PBIO_PYBRICKS_ERROR_OK;
1✔
UNCOV
79
        case PBIO_PYBRICKS_COMMAND_WRITE_APP_DATA: {
×
80
            if (!write_app_data_callback) {
×
81
                // No errors when no consumer is configured. This avoids errors
82
                // when data is sent after the program ends.
83
                return PBIO_PYBRICKS_ERROR_OK;
84
            }
85

86
            // Requires at least the message type and data offset.
UNCOV
87
            if (size <= 3) {
×
88
                return PBIO_PYBRICKS_ERROR_VALUE_NOT_ALLOWED;
89
            }
90

UNCOV
91
            uint16_t offset = pbio_get_uint16_le(&data[1]);
×
UNCOV
92
            uint16_t data_size = size - 3;
×
93
            const uint8_t *data_to_write = &data[3];
×
94
            return pbio_pybricks_error_from_pbio_error(write_app_data_callback(offset, data_size, data_to_write));
×
95
        }
96
        default:
×
UNCOV
97
            return PBIO_PYBRICKS_ERROR_INVALID_COMMAND;
×
98
    }
99
}
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