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

pybricks / pybricks-micropython / 10601837686

28 Aug 2024 05:51PM UTC coverage: 55.942% (-0.02%) from 55.961%
10601837686

Pull #264

github

web-flow
Merge efe75cbe2 into 261663efc
Pull Request #264: Allow for multiple builtin program including Port View, take 2.

8 of 17 new or added lines in 4 files covered. (47.06%)

17 existing lines in 3 files now uncovered.

3761 of 6723 relevant lines covered (55.94%)

20643224.7 hits per line

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

31.03
/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

41
        case PBIO_PYBRICKS_COMMAND_STOP_USER_PROGRAM:
42
            pbsys_program_stop(false);
43
            return PBIO_PYBRICKS_ERROR_OK;
44

45
        case PBIO_PYBRICKS_COMMAND_START_USER_PROGRAM:
46
            // Deprecated. For backwards compatibility with Pybricks
47
            // Profile < v1.4.0, assume we should start user program 0.
NEW
48
            return pbio_pybricks_error_from_pbio_error(
×
49
                pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_FIRST_SLOT));
50

51
        #if PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_REPL
52
        case PBIO_PYBRICKS_COMMAND_START_REPL:
53
            // Deprecated. For backwards compatibility with Pybricks
54
            // Profile < v1.4.0, make it work anyway.
55
            return pbio_pybricks_error_from_pbio_error(
56
                pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_REPL));
57
        #endif // PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_REPL
58

59
        case PBIO_PYBRICKS_COMMAND_WRITE_USER_PROGRAM_META:
×
60
            return pbio_pybricks_error_from_pbio_error(pbsys_storage_set_program_size(
×
61
                pbio_get_uint32_le(&data[1])));
62

63
        case PBIO_PYBRICKS_COMMAND_WRITE_USER_RAM:
×
64
            return pbio_pybricks_error_from_pbio_error(pbsys_storage_set_program_data(
×
65
                pbio_get_uint32_le(&data[1]), &data[5], size - 5));
66

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

71
        case PBIO_PYBRICKS_COMMAND_WRITE_STDIN:
1✔
72
            #if PBSYS_CONFIG_BLUETOOTH
73
            if (pbsys_bluetooth_rx_get_free() < size - 1) {
1✔
74
                return PBIO_PYBRICKS_ERROR_BUSY;
75
            }
76
            pbsys_bluetooth_rx_write(&data[1], size - 1);
1✔
77
            #endif
78
            // If no consumers are configured, goes to "/dev/null" without error
79
            return PBIO_PYBRICKS_ERROR_OK;
1✔
80

UNCOV
81
        case PBIO_PYBRICKS_COMMAND_WRITE_APP_DATA: {
×
UNCOV
82
            if (!write_app_data_callback) {
×
83
                // No errors when no consumer is configured. This avoids errors
84
                // when data is sent after the program ends.
85
                return PBIO_PYBRICKS_ERROR_OK;
86
            }
87

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

UNCOV
93
            uint16_t offset = pbio_get_uint16_le(&data[1]);
×
94
            uint16_t data_size = size - 3;
×
UNCOV
95
            const uint8_t *data_to_write = &data[3];
×
UNCOV
96
            return pbio_pybricks_error_from_pbio_error(write_app_data_callback(offset, data_size, data_to_write));
×
97
        }
98

NEW
99
        case PBIO_PYBRICKS_COMMAND_START_USER_OR_BUILTIN_PROGRAM:
×
100
            // Identifier payload required
NEW
101
            if (size != 2) {
×
102
                return PBIO_PYBRICKS_ERROR_VALUE_NOT_ALLOWED;
103
            }
NEW
104
            return pbio_pybricks_error_from_pbio_error(
×
105
                pbsys_main_program_request_start(data[1]));
106

UNCOV
107
        default:
×
UNCOV
108
            return PBIO_PYBRICKS_ERROR_INVALID_COMMAND;
×
109
    }
110
}
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