diff --git a/.eslintrc.json b/.eslintrc.json index 76534c4a..5b815328 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -29,14 +29,14 @@ "strict": "off", "no-console": "off", "no-restricted-syntax": "off", + "no-undef": 1, + "no-unused-vars": 1, "valid-jsdoc": [2, { "prefer": { "return": "returns" } }] }, - "no-undef": 1, - "no-unused-vars": 1, "plugins": [ "react" ], @@ -44,4 +44,4 @@ "es6": true, "jest": true } -} \ No newline at end of file +} diff --git a/.gitignore b/.gitignore index daab1335..7761ac90 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ package-lock.json build jest.json docs +/pc-ble-driver diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 911a5043..00000000 --- a/.gitmodules +++ /dev/null @@ -1,4 +0,0 @@ -[submodule "pc-ble-driver"] - path = pc-ble-driver - url = https://github.com/NordicSemiconductor/pc-ble-driver.git - branch = release/v2.3.2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dcd8b28..b1fff515 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,9 @@ -cmake_minimum_required(VERSION 3.3) +cmake_minimum_required(VERSION 3.13) + +if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) + set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "") +endif() # Name of the project (will be the name of the plugin) project (pc-ble-driver-js) @@ -7,19 +12,18 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -# All projects depending on pc-ble-driver need to include this first -include(pc-ble-driver/cmake/pc-ble-driver.cmake) -add_subdirectory(pc-ble-driver) +find_package(nrf-ble-driver 4.1.0 REQUIRED) -set(UECC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/uECC) -# Essential include files to build a node addon, -# you should add this line in every CMake.js based project. +if (NOT DEFINED CMAKE_JS_INC OR NOT DEFINED CMAKE_JS_LIB) + message ( + FATAL_ERROR + "-DCMAKE_JS_INC and -DCMAKE_JS_LIB must be provided. " + "Run `npm install` to build this project instead of " + "running cmake directly." + ) +endif () -include_directories(${CMAKE_JS_INC} ${PC_BLE_DRIVER_INCLUDE_DIR} ${UECC_INCLUDE_DIR}) -include_directories(${PC_BLE_DRIVER_INCLUDE_DIR}/common) -include_directories(${PC_BLE_DRIVER_INCLUDE_DIR}/common/sdk_compat) -include_directories(${PC_BLE_DRIVER_INCLUDE_DIR}/common/internal) -include_directories(${PC_BLE_DRIVER_INCLUDE_DIR}/common/internal/transport) +set(UECC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/uECC) file (GLOB SOURCE_FILES "src/adapter.cpp" @@ -41,8 +45,6 @@ if(WIN32) ) endif() - - file (GLOB UECC_SOURCE_FILES "src/uECC/*.c" ) @@ -69,11 +71,18 @@ else() include(${CMAKE_CURRENT_LIST_DIR}/cmake/gcc.cmake) endif() -foreach(SD_API_VER ${SD_API_VERS}) - string(TOLOWER ${SD_API_VER} SD_API_VER_L) - set(CURRENT_TARGET pc-ble-driver-js-${SD_API_VER_L}) +# There are several nrf-ble-driver libraies corresponding to SoftDevices. They +# expose a common API with some difference in features available. For now, we +# statially link to each one we need be creating several libraries, and select +# which resulting functions to use in JS code. We need more than one because +# nRF51 devices are not supported by SoftDevices v3 and above, but we need later +# SoftDevices for features for nRF52. +# +# Compile a node library with SD API v2 for nRF51, and one with v3 for nRF52. +foreach(SD_API_VER "2" "3") + set(CURRENT_TARGET pc-ble-driver-js-sd_api_v${SD_API_VER}) - add_library(${CURRENT_TARGET} SHARED ${SOURCE_FILES} ${UECC_SOURCE_FILES} ${LIB_PLATFORM_SRC_FILES} ${PLATFORM_SOURCE_FILES}) + add_library(${CURRENT_TARGET} SHARED ${SOURCE_FILES} ${UECC_SOURCE_FILES} ${PLATFORM_SOURCE_FILES}) # This line will give our library file a .node extension without any "lib" prefix set_target_properties(${CURRENT_TARGET} @@ -82,10 +91,9 @@ foreach(SD_API_VER ${SD_API_VERS}) PREFIX "" SUFFIX ".node") - string(REGEX MATCH "[0-9]+$" _SD_API_VER_NUM "${SD_API_VER}") - set_target_properties(${CURRENT_TARGET} PROPERTIES COMPILE_OPTIONS -DNRF_SD_BLE_API_VERSION=${_SD_API_VER_NUM}) + set_target_properties(${CURRENT_TARGET} PROPERTIES COMPILE_OPTIONS -DNRF_SD_BLE_API_VERSION=${SD_API_VER}) - target_include_directories(${CURRENT_TARGET} PRIVATE ${PC_BLE_DRIVER_${SD_API_VER}_PUBLIC_INCLUDE_DIRS}) + target_include_directories(${CURRENT_TARGET} PRIVATE ${CMAKE_JS_INC} ${UECC_INCLUDE_DIR}) if(WIN32) # suppress C4251 v8/msvc related warning, for more info: https://github.com/nodejs/node/pull/15570 @@ -95,14 +103,30 @@ foreach(SD_API_VER ${SD_API_VERS}) set_target_properties(${CURRENT_TARGET} PROPERTIES COMPILE_DEFINITIONS "_CRT_SECURE_NO_WARNINGS") set_target_properties(${CURRENT_TARGET} PROPERTIES LINK_FLAGS "/DELAYLOAD:node.exe" ) elseif(APPLE) - target_link_libraries(${CURRENT_TARGET} "-framework CoreFoundation") - target_link_libraries(${CURRENT_TARGET} "-framework IOKit") + target_link_libraries(${CURRENT_TARGET} PRIVATE "-framework CoreFoundation") + target_link_libraries(${CURRENT_TARGET} PRIVATE "-framework IOKit") set_property(TARGET ${CURRENT_TARGET} PROPERTY MACOSX_RPATH ON) else() # Assume Linux - target_link_libraries(${CURRENT_TARGET} "udev") + target_link_libraries(${CURRENT_TARGET} PRIVATE "udev") endif() - # actual shared and static libraries built from the same object files - target_link_libraries(${CURRENT_TARGET} ${CMAKE_JS_LIB} ${PC_BLE_DRIVER_${SD_API_VER}_STATIC_LIB}) + target_link_libraries(${CURRENT_TARGET} PRIVATE ${CMAKE_JS_LIB} nrf::nrf_ble_driver_sd_api_v${SD_API_VER}_static) + + get_target_property(ble_driver_if_dir nrf::nrf_ble_driver_sd_api_v${SD_API_VER}_static INTERFACE_INCLUDE_DIRECTORIES) + set(CONNECTIVITY_SD_API_V${SD_API_VER}_PATH "${ble_driver_if_dir}/../../share/nrf-ble-driver/hex/sd_api_v${SD_API_VER}/*.hex" CACHE FILEPATH "Path with wildcards to connectivity firmware files") + file(GLOB_RECURSE connectivity_firmware ${CONNECTIVITY_SD_API_V${SD_API_VER}_PATH}) + + set_property( + TARGET ${CURRENT_TARGET} + PROPERTY RESOURCE ${connectivity_firmware} + ) + + set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}) + install( + TARGETS ${CURRENT_TARGET} + RUNTIME DESTINATION "build/Release/" + LIBRARY DESTINATION "build/Release/" + RESOURCE DESTINATION "pc-ble-driver/hex/" + ) endforeach(SD_API_VER) diff --git a/Installation.md b/Installation.md deleted file mode 100644 index 6ccd0123..00000000 --- a/Installation.md +++ /dev/null @@ -1,72 +0,0 @@ -# Installation - -## Installation from npm - -To install pc-ble-driver-js for use in a Node.js project, simply: - - $ npm install pc-ble-driver-js - -This will work as long as precompiled binaries exist for your platform/runtime environment, ref. the files attached to the [Releases](https://github.com/NordicSemiconductor/pc-ble-driver-js/releases). (See [Node releases](https://nodejs.org/en/download/releases/) for an overview of the relation between Node release versions and Node module versions (ABI).) If your runtime is not supported, either change to a supported runtime, or follow the steps below to install from source. - -## Installation from source - -If precompiled binaries do not exist in your case, or you are going to do development on this project, you will need to install from source. - -### Dependencies - -The following Node/npm versions are required: - -* Node.js (>=6.5.7) -* npm (>=3.7.0) - - -### Submodule - -This repository refers to the [pc-ble-driver](https://github.com/NordicSemiconductor/pc-ble-driver) library as a submodule. To ensure that the submodule is downloaded when cloning pc-ble-driver-js, use: - - $ git clone --recursive - -Or if you have already cloned this repository: - - $ git submodule update --init --recursive - -### pc-ble-driver - -As building pc-ble-driver-js also involves building pc-ble-driver, you first need to follow the [pc-ble-driver installation instructions](https://github.com/NordicSemiconductor/pc-ble-driver/blob/master/Installation.md). Important note: When building the Boost libraries, make sure to build it for the architecture (32 or 64-bit) required by your Node.js installation. Once you have been able to successfully compile pc-ble-driver, you are ready to proceed with the steps below. - -### Platform-specific - -Some extra npm config is required for cmake-js. - -#### Windows - -Configure cmake-js: - - $ npm config set cmake_CMAKE_GENERATOR:INTERNAL="Visual Studio 14 2015" - $ npm config set cmake_BOOST_ROOT=c:\path\to\boost_x_xx_x - -#### Ubuntu Linux and macOS - -Configure cmake-js: - - $ npm config set cmake_BOOST_ROOT=/path/to/boost_x_xx_x - -### Building for Electron runtime - -If the pc-ble-driver-js module is going to be run from a different Node runtime, e.g. Electron, it is necessary provide that information to npm. To configure a different node runtime, add a .npmrc file to the root folder of the repo. Example .npmrc file content: - - runtime = Electron - target = 1.16.6 - disturl = https://atom.io/download/atom-shell - -### Installation - -Now you are ready to install pc-ble-driver-js: - - $ npm install - -### Unit tests - -Run unit tests to verify a successful installation: - - $ npm test diff --git a/README.md b/README.md index 7630f212..a7ea66bd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # pc-ble-driver-js -pc-ble-driver-js provides a Node.js interface to the [pc-ble-driver](https://github.com/NordicSemiconductor/pc-ble-driver) C/C++ library. +High-level Node.js API for Bluetooth Low Energy (BLE) using nRF51 and nRF52 hardware. + +## Usage +See the [examples](examples) folder. ## Overview @@ -8,11 +11,34 @@ The pc-ble-driver-js library allows an nRF5 connectivity chip running Nordic Sem ## Installation -To install pc-ble-driver-js for use in a Node.js project, simply: - $ npm install pc-ble-driver-js -This will work as long as precompiled binaries exist for your platform/runtime environment, ref. the files attached to the [Releases](https://github.com/NordicSemiconductor/pc-ble-driver-js/releases). If not, then it will attempt to build the binaries from source, which requires some additional setup as described in [Installation.md](./Installation.md). +The install script will try to download precompiled binaries for your platform/runtime environment from our [Releases](https://github.com/NordicSemiconductor/pc-ble-driver-js/releases). If not available, it will attempt to build the binaries from source, which requires a C++ compiler and the pc-ble-driver library. See [Building](#building). + +### Installing in Electron apps + +If you want to use pc-ble-driver-js with the electron runtime, you must set the runtime to Electron in .npmrc in the root of your project. Then install pc-ble-driver-js again. Example .npmrc file content: + + runtime = Electron + target = 1.16.6 + disturl = https://atom.io/download/atom-shell + +## Building + +If there are no precompiled binaries for your platform, the install script will try to build them. You will need a working C++ compiler and the pc-ble-driver library available by cmake find_package. + +The recommended way to get pc-ble-driver is using vcpkg. Clone the vcpkg repo, install nrf-ble-driver for the architecture of your node executable. Then set the environment variable `VCPKG_ROOT` to the full path of the vcpkg repository and it will get automatically picked up when doing `npm install`. + +A full example of preparing building on Windows for 64-bit Node: + + $ git clone https://github.com/NordicPlayground/vcpkg.git + $ ./vcpkg/bootstrap.bat + $ ./vcpkg/vcpkg install nrf-ble-driver:x64-windows + +And then when installing: + + $ set VCPKG_ROOT=/absolute/path/to/vcpkg/dir + $ npm install pc-ble-driver-js ## Hardware setup @@ -23,9 +49,9 @@ A connectivity firmware needs to be flashed on the nRF5 IC before using pc-ble-d The [examples](./examples) and [integration tests](./test) may be used as a starting point for development with pc-ble-driver-js. Examples include a [heart rate monitor](./examples/heart_rate_monitor.js) (BLE peripheral) and [heart rate collector](./examples/heart_rate_collector.js) (BLE master) and show the basic structure of an application built on pc-ble-driver-js. To run the heart rate monitor example, verify your nRF5 connectivity chip is set-up and connected to your PC and run: $ node examples/heart_rate_monitor.js - + To get more information about the command options you can run the command without any arguments. - + ## Architecture All functionality of pc-ble-driver-js is exposed through its [api](./api/). Other directories in `pc-ble-driver-js/` are for building, binding to C/C++, and testing, and a developer building an application on top of pc-ble-driver-js need not concern themselves with these details. diff --git a/api/__tests__/__snapshots__/firmwareRegistry-test.js.snap b/api/__tests__/__snapshots__/firmwareRegistry-test.js.snap index 9176a7cb..c1ec7cf0 100644 --- a/api/__tests__/__snapshots__/firmwareRegistry-test.js.snap +++ b/api/__tests__/__snapshots__/firmwareRegistry-test.js.snap @@ -2,68 +2,68 @@ exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf51 and darwin 1`] = ` Object { - "baudRate": 115200, - "file": "/pc-ble-driver/hex/sd_api_v2/connectivity_1.2.3_115k2_with_s130_2.0.1.hex", + "baudRate": 1000000, + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s130_2.0.1.hex", "sdBleApiVersion": 2, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf51 and linux 1`] = ` Object { "baudRate": 1000000, - "file": "/pc-ble-driver/hex/sd_api_v2/connectivity_1.2.3_1m_with_s130_2.0.1.hex", + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s130_2.0.1.hex", "sdBleApiVersion": 2, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf51 and win32 1`] = ` Object { "baudRate": 1000000, - "file": "/pc-ble-driver/hex/sd_api_v2/connectivity_1.2.3_1m_with_s130_2.0.1.hex", + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s130_2.0.1.hex", "sdBleApiVersion": 2, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf52 and darwin 1`] = ` Object { - "baudRate": 115200, - "file": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_115k2_with_s132_3.1.hex", + "baudRate": 1000000, + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s132_3.1.0.hex", "sdBleApiVersion": 3, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf52 and linux 1`] = ` Object { "baudRate": 1000000, - "file": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_1m_with_s132_3.1.hex", + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s132_3.1.0.hex", "sdBleApiVersion": 3, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getJlinkConnectivityFirmware returns expected jlink firmware info for nrf52 and win32 1`] = ` Object { "baudRate": 1000000, - "file": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_1m_with_s132_3.1.hex", + "file": "/pc-ble-driver/hex/connectivity_4.1.0_1m_with_s132_3.1.0.hex", "sdBleApiVersion": 3, - "version": "1.2.3", + "version": "4.1.0", } `; exports[`FirmwareRegistry.getNordicUSBConnectivityFirmware returns expected nordicUsb firmware info for darwin 1`] = ` Object { - "baudRate": 115200, + "baudRate": 1000000, "files": Object { - "application": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_usb_for_s132_3.hex", - "softdevice": "/pc-ble-driver/hex/sd_api_v3/s132_nrf52_3.1.0_softdevice.hex", + "application": "/pc-ble-driver/hex/connectivity_4.1.0_usb_for_s132_3.1.0.hex", + "softdevice": "/pc-ble-driver/hex/s132_nrf52_3.1.0_softdevice.hex", }, "sdBleApiVersion": 3, "sdId": 145, - "version": "ble-connectivity 0.1.0+Aug-14-2018-15-12-51", + "version": "ble-connectivity 4.1.0+Mar 21 2019 07:43:44", } `; @@ -71,12 +71,12 @@ exports[`FirmwareRegistry.getNordicUSBConnectivityFirmware returns expected nord Object { "baudRate": 1000000, "files": Object { - "application": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_usb_for_s132_3.hex", - "softdevice": "/pc-ble-driver/hex/sd_api_v3/s132_nrf52_3.1.0_softdevice.hex", + "application": "/pc-ble-driver/hex/connectivity_4.1.0_usb_for_s132_3.1.0.hex", + "softdevice": "/pc-ble-driver/hex/s132_nrf52_3.1.0_softdevice.hex", }, "sdBleApiVersion": 3, "sdId": 145, - "version": "ble-connectivity 0.1.0+Aug-14-2018-15-12-51", + "version": "ble-connectivity 4.1.0+Mar 21 2019 07:43:44", } `; @@ -84,11 +84,11 @@ exports[`FirmwareRegistry.getNordicUSBConnectivityFirmware returns expected nord Object { "baudRate": 1000000, "files": Object { - "application": "/pc-ble-driver/hex/sd_api_v3/connectivity_1.2.3_usb_for_s132_3.hex", - "softdevice": "/pc-ble-driver/hex/sd_api_v3/s132_nrf52_3.1.0_softdevice.hex", + "application": "/pc-ble-driver/hex/connectivity_4.1.0_usb_for_s132_3.1.0.hex", + "softdevice": "/pc-ble-driver/hex/s132_nrf52_3.1.0_softdevice.hex", }, "sdBleApiVersion": 3, "sdId": 145, - "version": "ble-connectivity 0.1.0+Aug-14-2018-15-12-51", + "version": "ble-connectivity 4.1.0+Mar 21 2019 07:43:44", } `; diff --git a/api/firmwareRegistry.js b/api/firmwareRegistry.js index 070ecbd0..45612c97 100644 --- a/api/firmwareRegistry.js +++ b/api/firmwareRegistry.js @@ -42,13 +42,15 @@ const arrayToInt = require('./util/intArrayConv').arrayToInt; const currentDir = require.resolve('./firmwareRegistry'); const hexDir = path.join(currentDir, '..', '..', 'pc-ble-driver', 'hex'); -const sdV2Dir = path.join(hexDir, 'sd_api_v2'); -const sdV3Dir = path.join(hexDir, 'sd_api_v3'); const VERSION_INFO_MAGIC = 0x46D8A517; const VERSION_INFO_START = 0x20000; const VERSION_INFO_LENGTH = 24; +const connectivityVersion = '4.1.0'; +const connectivityApplicationVersionString = 'ble-connectivity 4.1.0+Mar-21-2019-07-43-44'; +const connectivityBaudRate = 1000000; + /* * Holds connectivity firmware information for all supported devices. * @@ -56,39 +58,31 @@ const VERSION_INFO_LENGTH = 24; * have only one firmware hex file. Devices that use the Nordic USB stack * are programmed using serial DFU. In this case, we need two hex files: One * for the softdevice and one for the connectivity application. - * - * MacOS does not support opening serial ports using baud rates higher than - * 115200, while Windows and Linux supports 1m. This requires separate - * connectivity firmwares and baud rate settings for the different OS'es. */ function getFirmwareMap(platform) { return { jlink: { nrf51: { - file: platform === 'darwin' ? - path.join(sdV2Dir, 'connectivity_1.2.3_115k2_with_s130_2.0.1.hex') : - path.join(sdV2Dir, 'connectivity_1.2.3_1m_with_s130_2.0.1.hex'), - version: '1.2.3', - baudRate: platform === 'darwin' ? 115200 : 1000000, + file: path.join(hexDir, `connectivity_${connectivityVersion}_1m_with_s130_2.0.1.hex`), + version: connectivityVersion, + baudRate: connectivityBaudRate, sdBleApiVersion: 2, }, nrf52: { - file: platform === 'darwin' ? - path.join(sdV3Dir, 'connectivity_1.2.3_115k2_with_s132_3.1.hex') : - path.join(sdV3Dir, 'connectivity_1.2.3_1m_with_s132_3.1.hex'), - version: '1.2.3', - baudRate: platform === 'darwin' ? 115200 : 1000000, + file: path.join(hexDir, `connectivity_${connectivityVersion}_1m_with_s132_3.1.0.hex`), + version: connectivityVersion, + baudRate: connectivityBaudRate, sdBleApiVersion: 3, }, }, nordicUsb: { pca10059: { files: { - application: path.join(sdV3Dir, 'connectivity_1.2.3_usb_for_s132_3.hex'), - softdevice: path.join(sdV3Dir, 's132_nrf52_3.1.0_softdevice.hex'), + application: path.join(hexDir, `connectivity_${connectivityVersion}_usb_for_s132_3.1.0.hex`), + softdevice: path.join(hexDir, 's132_nrf52_3.1.0_softdevice.hex'), }, - version: 'ble-connectivity 0.1.0+Aug-14-2018-15-12-51', - baudRate: platform === 'darwin' ? 115200 : 1000000, + version: connectivityApplicationVersionString, + baudRate: connectivityBaudRate, sdBleApiVersion: 3, sdId: 0x91, // SoftDevice FWID, s132_nrf52_3.1.0 === 0x91 }, diff --git a/build.js b/build.js index 67f66668..1cafd9a8 100644 --- a/build.js +++ b/build.js @@ -51,6 +51,7 @@ function getBuildSystem(debug) { arch: process.env.npm_config_arch || undefined, generator: 'Ninja', debug, + target: 'install', }; if (process.platform === 'win32') { @@ -83,17 +84,6 @@ function getBuildSystem(debug) { let times = 0; function begin(args) { - // Sanity check for the platform-specific binary driver files - fs.readdir('./pc-ble-driver', (err, files) => { - if (err) { - console.error('ERROR: Could not read the \'pc-ble-driver\' subrepo, please check manually.'); - process.exit(2); - } else if (!files.length) { - console.error('ERROR: The \'pc-ble-driver\' subrepo is empty, please run \'git submodule update --init --recursive\' and try again.'); - process.exit(1); - } - }); - let debug = false; let build = 'rebuild'; diff --git a/package.json b/package.json index 371f22ee..9ce51109 100644 --- a/package.json +++ b/package.json @@ -78,5 +78,16 @@ "sinon": "^1.17.1", "yargs": "^3.29.0" }, + "files": [ + "api/", + "build.js", + "cmake/", + "CMakeLists.txt", + "index.js", + "pc-ble-driver/", + "scripts/", + "src/", + "typings/" + ], "typings": "typings/index.d.ts" } diff --git a/pc-ble-driver b/pc-ble-driver deleted file mode 160000 index 2d97262f..00000000 --- a/pc-ble-driver +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2d97262fa9940cb815778f01af9d05093558a72c diff --git a/src/serialadapter.cpp b/src/serialadapter.cpp index 40d9fc30..e5ed7a93 100644 --- a/src/serialadapter.cpp +++ b/src/serialadapter.cpp @@ -28,7 +28,15 @@ */ #include "serialadapter.h" -#include "serial_port_enum.h" + +#include +#include + +#include +#include + +// Maximum of adapters allowed on a system before GetAdapterList fails +constexpr size_t max_adapter_count = 64; NAN_METHOD(GetAdapterList) { @@ -40,16 +48,20 @@ NAN_METHOD(GetAdapterList) v8::Local callback = info[0].As(); auto baton = new AdapterListBaton(callback); - strcpy(baton->errorString, ""); uv_queue_work(uv_default_loop(), baton->req, GetAdapterList, reinterpret_cast(AfterGetAdapterList)); } void GetAdapterList(uv_work_t *req) { - auto baton = static_cast(req->data); + auto adapters = std::vector(max_adapter_count); + auto adapter_count = adapters.size(); + auto status = sd_rpc_serial_port_enum(adapters.data(), (std::uint32_t*) &adapter_count); + adapters.resize(adapter_count); - EnumSerialPorts(baton->results); + auto baton = static_cast(req->data); + baton->result = status; + baton->results = adapters; } void AfterGetAdapterList(uv_work_t* req) @@ -59,12 +71,7 @@ void AfterGetAdapterList(uv_work_t* req) v8::Local argv[2]; - if(baton->errorString[0]) - { - argv[0] = v8::Exception::Error(Nan::New(baton->errorString).ToLocalChecked()); - argv[1] = Nan::Undefined(); - } - else + if(baton->result == NRF_SUCCESS) { v8::Local results = Nan::New(); auto i = 0; @@ -72,27 +79,25 @@ void AfterGetAdapterList(uv_work_t* req) for(auto adapterItem : baton->results) { v8::Local item = Nan::New(); - Utility::Set(item, "comName", adapterItem->comName); - Utility::Set(item, "manufacturer", adapterItem->manufacturer); - Utility::Set(item, "serialNumber", adapterItem->serialNumber); - Utility::Set(item, "pnpId", adapterItem->pnpId); - Utility::Set(item, "locationId", adapterItem->locationId); - Utility::Set(item, "vendorId", adapterItem->vendorId); - Utility::Set(item, "productId", adapterItem->productId); + Utility::Set(item, "comName", adapterItem.port); + Utility::Set(item, "manufacturer", adapterItem.manufacturer); + Utility::Set(item, "serialNumber", adapterItem.serialNumber); + Utility::Set(item, "pnpId", adapterItem.pnpId); + Utility::Set(item, "locationId", adapterItem.locationId); + Utility::Set(item, "vendorId", adapterItem.vendorId); + Utility::Set(item, "productId", adapterItem.productId); results->Set(i++, item); } argv[0] = Nan::Undefined(); argv[1] = results; + } else { + argv[0] = ErrorMessage::getErrorMessage(baton->result, "getting adapter list"); + argv[1] = Nan::Undefined(); } Nan::AsyncResource resource("pc-ble-driver-js:callback"); baton->callback->Call(2, argv, &resource); - for(auto it = baton->results.begin(); it != baton->results.end(); ++it) - { - delete *it; - } - delete baton; } diff --git a/src/serialadapter.h b/src/serialadapter.h index 6b1d7c00..5a0eb870 100644 --- a/src/serialadapter.h +++ b/src/serialadapter.h @@ -32,10 +32,9 @@ #include "common.h" -#include -#include +#include -#include "serial_port_enum.h" +#include #define ERROR_STRING_SIZE 1024 @@ -45,8 +44,7 @@ struct AdapterListBaton : Baton { public: BATON_CONSTRUCTOR(AdapterListBaton) - std::list results; - char errorString[ERROR_STRING_SIZE]; // TODO: change this to std::string + std::vector results; }; -#endif // ADAPTER_H \ No newline at end of file +#endif // ADAPTER_H diff --git a/test/mtu.test.js b/test/mtu.test.js index 0688b047..dbe27e81 100644 --- a/test/mtu.test.js +++ b/test/mtu.test.js @@ -264,7 +264,8 @@ describe('the API', async () => { let peripheralAdapter; beforeAll(async () => { - [centralAdapter, peripheralAdapter] = await Promise.all([grabAdapter(), grabAdapter()]); + centralAdapter = await grabAdapter(); + peripheralAdapter = await grabAdapter(); await Promise.all([ setupAdapter(centralAdapter, '#CENTRAL', 'central', CENTRAL_DEVICE_ADDRESS, CENTRAL_DEVICE_ADDRESS_TYPE), diff --git a/test/openClose.test.js b/test/openClose.test.js index 12e27af9..437c8ae9 100644 --- a/test/openClose.test.js +++ b/test/openClose.test.js @@ -49,10 +49,11 @@ const SCAN_DURATION = 2000; const SCAN_DURATION_WAIT_TIME = 3000; const EXPECTED_NUMBER_OF_SCAN_REPORTS_PR_ITERATION = 2; +const DEVICE_SETUP_WAIT_TIME = 30000 const GENERIC_WAIT_PR_ITERATION = 15000; // Adjust the jest timeout based on the number of iterations required -const JEST_TIMEOUT = NUMBER_OF_ITERATIONS * +const JEST_TIMEOUT = DEVICE_SETUP_WAIT_TIME + NUMBER_OF_ITERATIONS * (NRF51_NRF52_WAIT_TIME + SCAN_DURATION_WAIT_TIME + GENERIC_WAIT_PR_ITERATION); debug(`jest timeout is set to ${JEST_TIMEOUT} ms.`); diff --git a/test/setup.js b/test/setup.js index 8f447009..74a5c1a4 100644 --- a/test/setup.js +++ b/test/setup.js @@ -40,7 +40,8 @@ const api = require('../index'); // Milliseconds wait before terminating test. // In worst case programming of two devices needs to be done + the tests shall run. -const JEST_TIMEOUT_FOR_SETUP_OF_DEVICE = 40000; +// For example, the connection test with nRF52 dongles takes about 43 seconds. +const JEST_TIMEOUT_FOR_SETUP_OF_DEVICE = 60000; jest.setTimeout(JEST_TIMEOUT_FOR_SETUP_OF_DEVICE); const adapterFactory = api.AdapterFactory.getInstance(undefined, { enablePolling: false });