mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-07-28 12:17:14 +00:00
ble: avoid aliases for UUID constants
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2021 The Pybricks Authors
|
||||
// Copyright (c) 2021-2022 The Pybricks Authors
|
||||
//
|
||||
// Pybricks uses the standard Device Info service.
|
||||
// Refer to Device Information Service (DIS) at https://www.bluetooth.com/specifications/specs/
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from '../ble-lwp3-service/protocol';
|
||||
|
||||
/** Device Information service UUID. */
|
||||
export const serviceUUID = 0x180a;
|
||||
export const deviceInformationServiceUUID = 0x180a;
|
||||
|
||||
/** Firmware Revision String characteristic UUID. */
|
||||
export const firmwareRevisionStringUUID = 0x2a26;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
// Copyright (c) 2020-2022 The Pybricks Authors
|
||||
//
|
||||
// Definitions related to the nRF UART Bluetooth low energy GATT service.
|
||||
//
|
||||
@@ -8,16 +8,16 @@
|
||||
// https://infocenter.nordicsemi.com/topic/sdk_nrf5_v16.0.0/ble_sdk_app_nus_eval.html
|
||||
|
||||
/** nRF UART Service UUID. */
|
||||
export const ServiceUUID = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
export const nordicUartServiceUUID = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
|
||||
/** nRF UART RX Characteristic UUID. Supports Write or Write without response. */
|
||||
export const RxCharUUID = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
export const nordicUartRxCharUUID = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
|
||||
/** nRF UART TX Characteristic UUID. Supports Notifications. */
|
||||
export const TxCharUUID = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
export const nordicUartTxCharUUID = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
|
||||
|
||||
/**
|
||||
* This is the largest data size for the TX characteristic that is safe to use
|
||||
* when the negotiated MTU is unknown.
|
||||
*/
|
||||
export const SafeTxCharLength = 20;
|
||||
export const nordicUartSafeTxCharLength = 20;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020-2021 The Pybricks Authors
|
||||
// Copyright (c) 2020-2022 The Pybricks Authors
|
||||
//
|
||||
// Definitions related to the Pybricks Bluetooth low energy GATT service.
|
||||
|
||||
import { assert } from '../utils';
|
||||
|
||||
/** Pybricks service UUID. */
|
||||
export const ServiceUUID = 'c5f50001-8280-46da-89f4-6d8051e4aeef';
|
||||
export const pybricksServiceUUID = 'c5f50001-8280-46da-89f4-6d8051e4aeef';
|
||||
/** Pybricks control characteristic UUID. */
|
||||
export const ControlCharacteristicUUID = 'c5f50002-8280-46da-89f4-6d8051e4aeef';
|
||||
export const pybricksControlCharacteristicUUID = 'c5f50002-8280-46da-89f4-6d8051e4aeef';
|
||||
|
||||
/** Commands are instructions sent to the hub. */
|
||||
export enum CommandType {
|
||||
|
||||
+18
-16
@@ -11,20 +11,20 @@ import {
|
||||
bleDIServiceDidReceiveSoftwareRevision,
|
||||
} from '../ble-device-info-service/actions';
|
||||
import {
|
||||
serviceUUID as deviceInfoServiceUUID,
|
||||
deviceInformationServiceUUID,
|
||||
firmwareRevisionStringUUID,
|
||||
pnpIdUUID,
|
||||
softwareRevisionStringUUID,
|
||||
} from '../ble-device-info-service/protocol';
|
||||
import { encodeInfo } from '../ble-device-info-service/protocol.test';
|
||||
import {
|
||||
RxCharUUID as uartRxCharUUID,
|
||||
ServiceUUID as uartServiceUUID,
|
||||
TxCharUUID as uartTxCharUUID,
|
||||
nordicUartRxCharUUID,
|
||||
nordicUartServiceUUID,
|
||||
nordicUartTxCharUUID,
|
||||
} from '../ble-nordic-uart-service/protocol';
|
||||
import {
|
||||
ControlCharacteristicUUID as pybricksCommandCharacteristicUUID,
|
||||
ServiceUUID as pybricksServiceUUID,
|
||||
pybricksControlCharacteristicUUID,
|
||||
pybricksServiceUUID,
|
||||
} from '../ble-pybricks-service/protocol';
|
||||
import {
|
||||
BleDeviceFailToConnectReasonType,
|
||||
@@ -106,7 +106,7 @@ function createMocks(): Mocks {
|
||||
|
||||
const pybricksService = mock<BluetoothRemoteGATTService>();
|
||||
pybricksService.getCharacteristic
|
||||
.calledWith(pybricksCommandCharacteristicUUID)
|
||||
.calledWith(pybricksControlCharacteristicUUID)
|
||||
.mockResolvedValue(pybricksChar);
|
||||
|
||||
const uartRxChar = mock<BluetoothRemoteGATTCharacteristic>();
|
||||
@@ -122,10 +122,10 @@ function createMocks(): Mocks {
|
||||
|
||||
const uartService = mock<BluetoothRemoteGATTService>();
|
||||
uartService.getCharacteristic
|
||||
.calledWith(uartRxCharUUID)
|
||||
.calledWith(nordicUartRxCharUUID)
|
||||
.mockResolvedValue(uartRxChar);
|
||||
uartService.getCharacteristic
|
||||
.calledWith(uartTxCharUUID)
|
||||
.calledWith(nordicUartTxCharUUID)
|
||||
.mockResolvedValue(uartTxChar);
|
||||
|
||||
const gatt = mock<BluetoothRemoteGATTServer>();
|
||||
@@ -136,12 +136,14 @@ function createMocks(): Mocks {
|
||||
}, 10);
|
||||
});
|
||||
gatt.getPrimaryService
|
||||
.calledWith(deviceInfoServiceUUID)
|
||||
.calledWith(deviceInformationServiceUUID)
|
||||
.mockResolvedValue(deviceInfoService);
|
||||
gatt.getPrimaryService
|
||||
.calledWith(pybricksServiceUUID)
|
||||
.mockResolvedValue(pybricksService);
|
||||
gatt.getPrimaryService.calledWith(uartServiceUUID).mockResolvedValue(uartService);
|
||||
gatt.getPrimaryService
|
||||
.calledWith(nordicUartServiceUUID)
|
||||
.mockResolvedValue(uartService);
|
||||
|
||||
const deviceEvents = new EventTarget();
|
||||
const device = mock<BluetoothDevice>({
|
||||
@@ -339,7 +341,7 @@ describe('connect action is dispatched', () => {
|
||||
it('should fail if device does not have device info service', async () => {
|
||||
const testError = new DOMException('test error', 'NotFoundError');
|
||||
mocks.gatt.getPrimaryService
|
||||
.calledWith(deviceInfoServiceUUID)
|
||||
.calledWith(deviceInformationServiceUUID)
|
||||
.mockRejectedValueOnce(testError);
|
||||
|
||||
await runConnectUntil(saga, ConnectRunPoint.Connect);
|
||||
@@ -485,7 +487,7 @@ describe('connect action is dispatched', () => {
|
||||
it('should fail if getting pybricks characteristic fails', async () => {
|
||||
const testError = new Error('test error');
|
||||
mocks.pybricksService.getCharacteristic
|
||||
.calledWith(pybricksCommandCharacteristicUUID)
|
||||
.calledWith(pybricksControlCharacteristicUUID)
|
||||
.mockRejectedValue(testError);
|
||||
|
||||
await runConnectUntil(saga, ConnectRunPoint.DidReceivePnpId);
|
||||
@@ -544,7 +546,7 @@ describe('connect action is dispatched', () => {
|
||||
it('should fail if device does not have nordic uart service', async () => {
|
||||
const testError = new DOMException('test error', 'NotFoundError');
|
||||
mocks.gatt.getPrimaryService
|
||||
.calledWith(uartServiceUUID)
|
||||
.calledWith(nordicUartServiceUUID)
|
||||
.mockRejectedValueOnce(testError);
|
||||
|
||||
await runConnectUntil(saga, ConnectRunPoint.DidReceivePnpId);
|
||||
@@ -562,7 +564,7 @@ describe('connect action is dispatched', () => {
|
||||
it('should fail if getting nordic uart rx characteristic fails', async () => {
|
||||
const testError = new Error('test error');
|
||||
mocks.uartService.getCharacteristic
|
||||
.calledWith(uartRxCharUUID)
|
||||
.calledWith(nordicUartRxCharUUID)
|
||||
.mockRejectedValue(testError);
|
||||
|
||||
await runConnectUntil(saga, ConnectRunPoint.DidReceivePnpId);
|
||||
@@ -583,7 +585,7 @@ describe('connect action is dispatched', () => {
|
||||
it('should fail if getting nordic uart tx characteristic fails', async () => {
|
||||
const testError = new Error('test error');
|
||||
mocks.uartService.getCharacteristic
|
||||
.calledWith(uartTxCharUUID)
|
||||
.calledWith(nordicUartTxCharUUID)
|
||||
.mockRejectedValue(testError);
|
||||
|
||||
await runConnectUntil(saga, ConnectRunPoint.DidReceivePnpId);
|
||||
|
||||
+19
-13
@@ -23,7 +23,7 @@ import {
|
||||
} from '../ble-device-info-service/actions';
|
||||
import {
|
||||
decodePnpId,
|
||||
serviceUUID as deviceInfoServiceUUID,
|
||||
deviceInformationServiceUUID,
|
||||
firmwareRevisionStringUUID,
|
||||
pnpIdUUID,
|
||||
softwareRevisionStringUUID,
|
||||
@@ -35,9 +35,9 @@ import {
|
||||
write as writeUart,
|
||||
} from '../ble-nordic-uart-service/actions';
|
||||
import {
|
||||
RxCharUUID as uartRxCharUUID,
|
||||
ServiceUUID as uartServiceUUID,
|
||||
TxCharUUID as uartTxCharUUID,
|
||||
nordicUartRxCharUUID,
|
||||
nordicUartServiceUUID,
|
||||
nordicUartTxCharUUID,
|
||||
} from '../ble-nordic-uart-service/protocol';
|
||||
import {
|
||||
didFailToWriteCommand,
|
||||
@@ -46,8 +46,8 @@ import {
|
||||
writeCommand,
|
||||
} from '../ble-pybricks-service/actions';
|
||||
import {
|
||||
ControlCharacteristicUUID as pybricksCommandCharacteristicUUID,
|
||||
ServiceUUID as pybricksServiceUUID,
|
||||
pybricksControlCharacteristicUUID,
|
||||
pybricksServiceUUID,
|
||||
} from '../ble-pybricks-service/protocol';
|
||||
import { RootState } from '../reducers';
|
||||
import { ensureError } from '../utils';
|
||||
@@ -121,8 +121,8 @@ function* handleBleConnectPybricks(): Generator {
|
||||
filters: [{ services: [pybricksServiceUUID] }],
|
||||
optionalServices: [
|
||||
pybricksServiceUUID,
|
||||
deviceInfoServiceUUID,
|
||||
uartServiceUUID,
|
||||
deviceInformationServiceUUID,
|
||||
nordicUartServiceUUID,
|
||||
],
|
||||
}),
|
||||
);
|
||||
@@ -183,7 +183,7 @@ function* handleBleConnectPybricks(): Generator {
|
||||
try {
|
||||
deviceInfoService = yield* call(
|
||||
[server, 'getPrimaryService'],
|
||||
deviceInfoServiceUUID,
|
||||
deviceInformationServiceUUID,
|
||||
);
|
||||
} catch (err) {
|
||||
server.disconnect();
|
||||
@@ -360,7 +360,7 @@ function* handleBleConnectPybricks(): Generator {
|
||||
try {
|
||||
pybricksControlChar = yield* call(
|
||||
[pybricksService, 'getCharacteristic'],
|
||||
pybricksCommandCharacteristicUUID,
|
||||
pybricksControlCharacteristicUUID,
|
||||
);
|
||||
} catch (err) {
|
||||
server.disconnect();
|
||||
@@ -432,7 +432,7 @@ function* handleBleConnectPybricks(): Generator {
|
||||
|
||||
let uartService: BluetoothRemoteGATTService;
|
||||
try {
|
||||
uartService = yield* call([server, 'getPrimaryService'], uartServiceUUID);
|
||||
uartService = yield* call([server, 'getPrimaryService'], nordicUartServiceUUID);
|
||||
} catch (err) {
|
||||
yield* cancel(tasks);
|
||||
pybricksControlChannel.close();
|
||||
@@ -460,7 +460,10 @@ function* handleBleConnectPybricks(): Generator {
|
||||
|
||||
let uartRxChar: BluetoothRemoteGATTCharacteristic;
|
||||
try {
|
||||
uartRxChar = yield* call([uartService, 'getCharacteristic'], uartRxCharUUID);
|
||||
uartRxChar = yield* call(
|
||||
[uartService, 'getCharacteristic'],
|
||||
nordicUartRxCharUUID,
|
||||
);
|
||||
} catch (err) {
|
||||
yield* cancel(tasks);
|
||||
pybricksControlChannel.close();
|
||||
@@ -482,7 +485,10 @@ function* handleBleConnectPybricks(): Generator {
|
||||
|
||||
let uartTxChar: BluetoothRemoteGATTCharacteristic;
|
||||
try {
|
||||
uartTxChar = yield* call([uartService, 'getCharacteristic'], uartTxCharUUID);
|
||||
uartTxChar = yield* call(
|
||||
[uartService, 'getCharacteristic'],
|
||||
nordicUartTxCharUUID,
|
||||
);
|
||||
} catch (err) {
|
||||
yield* cancel(tasks);
|
||||
pybricksControlChannel.close();
|
||||
|
||||
+3
-3
@@ -12,7 +12,7 @@ import {
|
||||
takeEvery,
|
||||
} from 'typed-redux-saga/macro';
|
||||
import { didFailToWrite, didWrite, write } from '../ble-nordic-uart-service/actions';
|
||||
import { SafeTxCharLength } from '../ble-nordic-uart-service/protocol';
|
||||
import { nordicUartSafeTxCharLength } from '../ble-nordic-uart-service/protocol';
|
||||
import {
|
||||
didFailToSendCommand,
|
||||
didSendCommand,
|
||||
@@ -121,10 +121,10 @@ function* handleDownloadAndRun(action: ReturnType<typeof downloadAndRun>): Gener
|
||||
const chunk = mpy.data.slice(i, i + downloadChunkSize);
|
||||
|
||||
// we can actually only write 20 bytes at a time
|
||||
for (let j = 0; j < chunk.length; j += SafeTxCharLength) {
|
||||
for (let j = 0; j < chunk.length; j += nordicUartSafeTxCharLength) {
|
||||
yield* put(didProgressDownload((i + j) / mpy.data.byteLength));
|
||||
const writeAction = yield* put(
|
||||
write(nextMessageId(), chunk.slice(j, j + SafeTxCharLength)),
|
||||
write(nextMessageId(), chunk.slice(j, j + nordicUartSafeTxCharLength)),
|
||||
);
|
||||
const { didFailToWrite } = yield* waitForWrite(writeAction.id);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020-2021 The Pybricks Authors
|
||||
// Copyright (c) 2020-2022 The Pybricks Authors
|
||||
|
||||
// Ref: https://lego.github.io/lego-ble-wireless-protocol-docs/index.html#lego-hub-boot-loader-service
|
||||
|
||||
@@ -8,12 +8,12 @@ import { assert, hex } from '../utils';
|
||||
/**
|
||||
* LEGO Powered Up Bootloader Service UUID.
|
||||
*/
|
||||
export const ServiceUUID = '00001625-1212-efde-1623-785feabcd123';
|
||||
export const lwp3BootloaderServiceUUID = '00001625-1212-efde-1623-785feabcd123';
|
||||
|
||||
/**
|
||||
* LEGO Powered Up Bootloader Characteristic UUID.
|
||||
*/
|
||||
export const CharacteristicUUID = '00001626-1212-efde-1623-785feabcd123';
|
||||
export const lwp3BootloaderCharacteristicUUID = '00001626-1212-efde-1623-785feabcd123';
|
||||
|
||||
/**
|
||||
* The maximum message size that can be sent or received.
|
||||
|
||||
@@ -19,7 +19,10 @@ import {
|
||||
disconnect,
|
||||
send,
|
||||
} from './actions';
|
||||
import { CharacteristicUUID, ServiceUUID } from './protocol';
|
||||
import {
|
||||
lwp3BootloaderCharacteristicUUID,
|
||||
lwp3BootloaderServiceUUID,
|
||||
} from './protocol';
|
||||
|
||||
function* handleNotify(data: DataView): Generator {
|
||||
yield* put(didReceive(data));
|
||||
@@ -59,8 +62,8 @@ function* handleConnect(): Generator {
|
||||
try {
|
||||
device = yield* call(() =>
|
||||
navigator.bluetooth.requestDevice({
|
||||
filters: [{ services: [ServiceUUID] }],
|
||||
optionalServices: [ServiceUUID],
|
||||
filters: [{ services: [lwp3BootloaderServiceUUID] }],
|
||||
optionalServices: [lwp3BootloaderServiceUUID],
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
@@ -98,7 +101,7 @@ function* handleConnect(): Generator {
|
||||
|
||||
let service: BluetoothRemoteGATTService;
|
||||
try {
|
||||
service = yield* call([server, 'getPrimaryService'], ServiceUUID);
|
||||
service = yield* call([server, 'getPrimaryService'], lwp3BootloaderServiceUUID);
|
||||
} catch (err) {
|
||||
server.disconnect();
|
||||
yield* takeMaybe(disconnectChannel);
|
||||
@@ -116,7 +119,7 @@ function* handleConnect(): Generator {
|
||||
try {
|
||||
characteristic = yield* call(
|
||||
[service, 'getCharacteristic'],
|
||||
CharacteristicUUID,
|
||||
lwp3BootloaderCharacteristicUUID,
|
||||
);
|
||||
} catch (err) {
|
||||
server.disconnect();
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
didWrite,
|
||||
write,
|
||||
} from '../ble-nordic-uart-service/actions';
|
||||
import { SafeTxCharLength } from '../ble-nordic-uart-service/protocol';
|
||||
import { nordicUartSafeTxCharLength } from '../ble-nordic-uart-service/protocol';
|
||||
import { checksum } from '../hub/actions';
|
||||
import { HubRuntimeState } from '../hub/reducers';
|
||||
import { RootState } from '../reducers';
|
||||
@@ -56,7 +56,7 @@ function* receiveTerminalData(): Generator {
|
||||
let value = action.value;
|
||||
|
||||
// Try to collect more data so that we aren't sending just one byte at time
|
||||
while (value.length < SafeTxCharLength) {
|
||||
while (value.length < nordicUartSafeTxCharLength) {
|
||||
const { action, timeout } = yield* race({
|
||||
action: take(channel),
|
||||
timeout: delay(20),
|
||||
@@ -72,9 +72,9 @@ function* receiveTerminalData(): Generator {
|
||||
|
||||
// stdin gets piped to BLE connection
|
||||
const data = encoder.encode(value);
|
||||
for (let i = 0; i < data.length; i += SafeTxCharLength) {
|
||||
for (let i = 0; i < data.length; i += nordicUartSafeTxCharLength) {
|
||||
const { id } = yield* put(
|
||||
write(nextMessageId(), data.slice(i, i + SafeTxCharLength)),
|
||||
write(nextMessageId(), data.slice(i, i + nordicUartSafeTxCharLength)),
|
||||
);
|
||||
|
||||
yield* take(
|
||||
|
||||
Reference in New Issue
Block a user