├── .codespellrc ├── .github ├── dependabot.yml └── workflows │ ├── compile-examples.yml │ ├── report-size-deltas.yml │ ├── spell-check.yml │ ├── sync-labels.yml │ └── unit-tests.yml ├── .gitignore ├── CHANGELOG ├── LICENSE ├── README.md ├── docs ├── api.md ├── assets │ └── ble-bulletin-board-model.png └── readme.md ├── examples ├── Central │ ├── LedControl │ │ └── LedControl.ino │ ├── PeripheralExplorer │ │ └── PeripheralExplorer.ino │ ├── Scan │ │ └── Scan.ino │ ├── ScanCallback │ │ └── ScanCallback.ino │ └── SensorTagButton │ │ └── SensorTagButton.ino └── Peripheral │ ├── Advertising │ ├── EnhancedAdvertising │ │ └── EnhancedAdvertising.ino │ └── RawDataAdvertising │ │ └── RawDataAdvertising.ino │ ├── BatteryMonitor │ └── BatteryMonitor.ino │ ├── ButtonLED │ └── ButtonLED.ino │ ├── CallbackLED │ └── CallbackLED.ino │ ├── EncryptedBatteryMonitor │ └── EncryptedBatteryMonitor.ino │ └── LED │ └── LED.ino ├── extras ├── arduino-ble-parser.py └── test │ ├── .gitignore │ ├── CMakeLists.txt │ ├── include │ ├── Arduino.h │ ├── test_advertising_data │ │ └── FakeBLELocalDevice.h │ ├── test_discovered_device │ │ └── FakeGAP.h │ └── util │ │ ├── Common.h │ │ ├── HCIFakeTransport.h │ │ ├── Stream.h │ │ ├── String.h │ │ ├── TestUtil.h │ │ └── itoa.h │ └── src │ ├── Arduino.cpp │ ├── test_advertising_data │ ├── FakeBLELocalDevice.cpp │ ├── test_advertising_data.cpp │ ├── test_local_name.cpp │ ├── test_manufacturer.cpp │ └── test_service.cpp │ ├── test_characteristic │ ├── test_permissions.cpp │ └── test_writeValue.cpp │ ├── test_discovered_device │ ├── FakeGAP.cpp │ └── test_discovered_device.cpp │ ├── test_main.cpp │ ├── test_uuid │ └── test_uuid.cpp │ └── util │ ├── Common.cpp │ ├── HCIFakeTransport.cpp │ ├── String.cpp │ ├── TestUtil.cpp │ └── itoa.c ├── keywords.txt ├── library.properties └── src ├── ArduinoBLE.h ├── BLEAdvertisingData.cpp ├── BLEAdvertisingData.h ├── BLECharacteristic.cpp ├── BLECharacteristic.h ├── BLEDescriptor.cpp ├── BLEDescriptor.h ├── BLEDevice.cpp ├── BLEDevice.h ├── BLEProperty.h ├── BLEService.cpp ├── BLEService.h ├── BLEStringCharacteristic.cpp ├── BLEStringCharacteristic.h ├── BLETypedCharacteristic.h ├── BLETypedCharacteristics.cpp ├── BLETypedCharacteristics.h ├── local ├── BLELocalAttribute.cpp ├── BLELocalAttribute.h ├── BLELocalCharacteristic.cpp ├── BLELocalCharacteristic.h ├── BLELocalDescriptor.cpp ├── BLELocalDescriptor.h ├── BLELocalDevice.cpp ├── BLELocalDevice.h ├── BLELocalService.cpp └── BLELocalService.h ├── remote ├── BLERemoteAttribute.cpp ├── BLERemoteAttribute.h ├── BLERemoteCharacteristic.cpp ├── BLERemoteCharacteristic.h ├── BLERemoteDescriptor.cpp ├── BLERemoteDescriptor.h ├── BLERemoteDevice.cpp ├── BLERemoteDevice.h ├── BLERemoteService.cpp └── BLERemoteService.h └── utility ├── ATT.cpp ├── ATT.h ├── BLELinkedList.h ├── BLEUuid.cpp ├── BLEUuid.h ├── CordioHCICustomDriver.h ├── GAP.cpp ├── GAP.h ├── GATT.cpp ├── GATT.h ├── HCI.cpp ├── HCI.h ├── HCICordioTransport.cpp ├── HCICordioTransport.h ├── HCISilabsTransport.cpp ├── HCISilabsTransport.h ├── HCITransport.h ├── HCIUartTransport.cpp ├── HCIUartTransport.h ├── HCIVirtualTransport.cpp ├── HCIVirtualTransport.h ├── HCIVirtualTransportAT.cpp ├── HCIVirtualTransportAT.h ├── L2CAPSignaling.cpp ├── L2CAPSignaling.h ├── bitDescriptions.cpp ├── bitDescriptions.h ├── btct.cpp ├── btct.h ├── keyDistribution.cpp └── keyDistribution.h /.codespellrc: -------------------------------------------------------------------------------- 1 | # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/spell-check/.codespellrc 2 | # See: https://github.com/codespell-project/codespell#using-a-config-file 3 | [codespell] 4 | # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: 5 | ignore-words-list = , 6 | skip = ./.git,./.licenses,__pycache__,node_modules,./go.mod,./go.sum,./package-lock.json,./poetry.lock,./yarn.lock,./extras/test 7 | builtin = clear,informal,en-GB_to_en-US 8 | check-filenames = 9 | check-hidden = 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # See: https://docs.github.com/en/code-security/supply-chain-security/configuration-options-for-dependency-updates#about-the-dependabotyml-file 2 | version: 2 3 | 4 | updates: 5 | # Configure check for outdated GitHub Actions actions in workflows. 6 | # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/dependabot/README.md 7 | # See: https://docs.github.com/en/code-security/supply-chain-security/keeping-your-actions-up-to-date-with-dependabot 8 | - package-ecosystem: github-actions 9 | directory: / # Check the repository's workflows under /.github/workflows/ 10 | schedule: 11 | interval: daily 12 | labels: 13 | - "topic: infrastructure" 14 | -------------------------------------------------------------------------------- /.github/workflows/compile-examples.yml: -------------------------------------------------------------------------------- 1 | name: Compile Examples 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | paths: 7 | - ".github/workflows/compile-examples.yml" 8 | - "examples/**" 9 | - "src/**" 10 | pull_request: 11 | paths: 12 | - ".github/workflows/compile-examples.yml" 13 | - "examples/**" 14 | - "src/**" 15 | schedule: 16 | # Run every Tuesday at 8 AM UTC to catch breakage caused by changes to external resources (libraries, platforms). 17 | - cron: "0 8 * * TUE" 18 | workflow_dispatch: 19 | repository_dispatch: 20 | 21 | jobs: 22 | build: 23 | name: ${{ matrix.board.fqbn }} 24 | runs-on: ubuntu-latest 25 | 26 | env: 27 | SKETCHES_REPORTS_PATH: sketches-reports 28 | 29 | strategy: 30 | fail-fast: false 31 | 32 | matrix: 33 | board: 34 | - fqbn: arduino:samd:mkrwifi1010 35 | platforms: | 36 | - name: arduino:samd 37 | artifact-name-suffix: arduino-samd-mkrwifi1010 38 | - fqbn: arduino:samd:nano_33_iot 39 | platforms: | 40 | - name: arduino:samd 41 | artifact-name-suffix: arduino-samd-nano_33_iot 42 | - fqbn: arduino:megaavr:uno2018:mode=on 43 | platforms: | 44 | - name: arduino:megaavr 45 | artifact-name-suffix: arduino-megaavr-uno2018 46 | - fqbn: arduino:mbed_nano:nano33ble 47 | platforms: | 48 | - name: arduino:mbed_nano 49 | artifact-name-suffix: arduino-mbed_nano-nano33ble 50 | - fqbn: arduino:mbed_nano:nanorp2040connect 51 | platforms: | 52 | - name: arduino:mbed_nano 53 | artifact-name-suffix: arduino-mbed_nano-nanorp2040connect 54 | - fqbn: arduino:renesas_uno:unor4wifi 55 | platforms: | 56 | - name: arduino:renesas_uno 57 | artifact-name-suffix: arduino-renesas_uno-unor4wifi 58 | 59 | steps: 60 | - name: Checkout repository 61 | uses: actions/checkout@v4 62 | 63 | - name: Compile examples 64 | uses: arduino/compile-sketches@v1 65 | with: 66 | github-token: ${{ secrets.GITHUB_TOKEN }} 67 | fqbn: ${{ matrix.board.fqbn }} 68 | platforms: ${{ matrix.board.platforms }} 69 | libraries: | 70 | # Install the library from the local path. 71 | - source-path: ./ 72 | sketch-paths: | 73 | - examples 74 | enable-deltas-report: true 75 | sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} 76 | 77 | - name: Save sketches report as workflow artifact 78 | uses: actions/upload-artifact@v4 79 | with: 80 | if-no-files-found: error 81 | path: ${{ env.SKETCHES_REPORTS_PATH }} 82 | name: sketches-report-${{ matrix.board.artifact-name-suffix }} 83 | 84 | build-for-esp32: 85 | runs-on: ubuntu-latest 86 | 87 | strategy: 88 | matrix: 89 | fqbn: 90 | - esp32:esp32:esp32 91 | - esp32:esp32:esp32s3 92 | - esp32:esp32:esp32c3 93 | - esp32:esp32:esp32c6 94 | - esp32:esp32:esp32h2 95 | # Not supported out of the box by ESP32 Arduino core 96 | #- esp32:esp32:esp32c2 97 | 98 | steps: 99 | - uses: actions/checkout@v4 100 | - uses: arduino/compile-sketches@v1 101 | with: 102 | github-token: ${{ secrets.GITHUB_TOKEN }} 103 | fqbn: ${{ matrix.fqbn }} 104 | platforms: | 105 | - name: esp32:esp32 106 | source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 107 | sketch-paths: | 108 | - examples/Central/Scan 109 | - examples/Central/PeripheralExplorer 110 | - examples/Central/ScanCallback 111 | - examples/Central/SensorTagButton 112 | - examples/Peripheral/Advertising/EnhancedAdvertising 113 | - examples/Peripheral/Advertising/RawDataAdvertising 114 | cli-compile-flags: | 115 | - --warnings="none" 116 | -------------------------------------------------------------------------------- /.github/workflows/report-size-deltas.yml: -------------------------------------------------------------------------------- 1 | name: Report Size Deltas 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | paths: 7 | - ".github/workflows/report-size-deltas.yml" 8 | schedule: 9 | # Run at the minimum interval allowed by GitHub Actions. 10 | # Note: GitHub Actions periodically has outages which result in workflow failures. 11 | # In this event, the workflows will start passing again once the service recovers. 12 | - cron: "*/5 * * * *" 13 | workflow_dispatch: 14 | repository_dispatch: 15 | 16 | jobs: 17 | report: 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Comment size deltas reports to PRs 21 | uses: arduino/report-size-deltas@v1 22 | with: 23 | # Regex matching the names of the workflow artifacts created by the "Compile Examples" workflow 24 | sketches-reports-source: ^sketches-report-.+ 25 | -------------------------------------------------------------------------------- /.github/workflows/spell-check.yml: -------------------------------------------------------------------------------- 1 | # Source: https://github.com/per1234/.github/blob/main/workflow-templates/spell-check.md 2 | name: Spell Check 3 | 4 | # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows 5 | on: 6 | push: 7 | pull_request: 8 | schedule: 9 | # Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates. 10 | - cron: "0 8 * * TUE" 11 | workflow_dispatch: 12 | repository_dispatch: 13 | 14 | jobs: 15 | spellcheck: 16 | runs-on: ubuntu-latest 17 | permissions: 18 | contents: read 19 | 20 | steps: 21 | - name: Checkout repository 22 | uses: actions/checkout@v4 23 | 24 | - name: Spell check 25 | uses: codespell-project/actions-codespell@v2 26 | -------------------------------------------------------------------------------- /.github/workflows/sync-labels.yml: -------------------------------------------------------------------------------- 1 | # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md 2 | name: Sync Labels 3 | 4 | # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows 5 | on: 6 | push: 7 | paths: 8 | - ".github/workflows/sync-labels.ya?ml" 9 | - ".github/label-configuration-files/*.ya?ml" 10 | pull_request: 11 | paths: 12 | - ".github/workflows/sync-labels.ya?ml" 13 | - ".github/label-configuration-files/*.ya?ml" 14 | schedule: 15 | # Run daily at 8 AM UTC to sync with changes to shared label configurations. 16 | - cron: "0 8 * * *" 17 | workflow_dispatch: 18 | repository_dispatch: 19 | 20 | env: 21 | CONFIGURATIONS_FOLDER: .github/label-configuration-files 22 | CONFIGURATIONS_ARTIFACT: label-configuration-files 23 | 24 | jobs: 25 | check: 26 | runs-on: ubuntu-latest 27 | 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v4 31 | 32 | - name: Download JSON schema for labels configuration file 33 | id: download-schema 34 | uses: carlosperate/download-file-action@v2 35 | with: 36 | file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json 37 | location: ${{ runner.temp }}/label-configuration-schema 38 | 39 | - name: Install JSON schema validator 40 | run: | 41 | sudo npm install \ 42 | --global \ 43 | ajv-cli \ 44 | ajv-formats 45 | 46 | - name: Validate local labels configuration 47 | run: | 48 | # See: https://github.com/ajv-validator/ajv-cli#readme 49 | ajv validate \ 50 | --all-errors \ 51 | -c ajv-formats \ 52 | -s "${{ steps.download-schema.outputs.file-path }}" \ 53 | -d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}" 54 | 55 | download: 56 | needs: check 57 | runs-on: ubuntu-latest 58 | 59 | strategy: 60 | matrix: 61 | filename: 62 | # Filenames of the shared configurations to apply to the repository in addition to the local configuration. 63 | # https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/sync-labels 64 | - universal.yml 65 | 66 | steps: 67 | - name: Download 68 | uses: carlosperate/download-file-action@v2 69 | with: 70 | file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} 71 | 72 | - name: Pass configuration files to next job via workflow artifact 73 | uses: actions/upload-artifact@v4 74 | with: 75 | path: | 76 | *.yaml 77 | *.yml 78 | if-no-files-found: error 79 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 80 | 81 | sync: 82 | needs: download 83 | runs-on: ubuntu-latest 84 | 85 | steps: 86 | - name: Set environment variables 87 | run: | 88 | # See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable 89 | echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV" 90 | 91 | - name: Determine whether to dry run 92 | id: dry-run 93 | if: > 94 | github.event_name == 'pull_request' || 95 | ( 96 | ( 97 | github.event_name == 'push' || 98 | github.event_name == 'workflow_dispatch' 99 | ) && 100 | github.ref != format('refs/heads/{0}', github.event.repository.default_branch) 101 | ) 102 | run: | 103 | # Use of this flag in the github-label-sync command will cause it to only check the validity of the 104 | # configuration. 105 | echo "::set-output name=flag::--dry-run" 106 | 107 | - name: Checkout repository 108 | uses: actions/checkout@v4 109 | 110 | - name: Download configuration files artifact 111 | uses: actions/download-artifact@v4 112 | with: 113 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 114 | path: ${{ env.CONFIGURATIONS_FOLDER }} 115 | 116 | - name: Remove unneeded artifact 117 | uses: geekyeggo/delete-artifact@v5 118 | with: 119 | name: ${{ env.CONFIGURATIONS_ARTIFACT }} 120 | 121 | - name: Merge label configuration files 122 | run: | 123 | # Merge all configuration files 124 | shopt -s extglob 125 | cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}" 126 | 127 | - name: Install github-label-sync 128 | run: sudo npm install --global github-label-sync 129 | 130 | - name: Sync labels 131 | env: 132 | GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} 133 | run: | 134 | # See: https://github.com/Financial-Times/github-label-sync 135 | github-label-sync \ 136 | --labels "${{ env.MERGED_CONFIGURATION_PATH }}" \ 137 | ${{ steps.dry-run.outputs.flag }} \ 138 | ${{ github.repository }} 139 | -------------------------------------------------------------------------------- /.github/workflows/unit-tests.yml: -------------------------------------------------------------------------------- 1 | name: Unit Tests 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - ".github/workflows/unit-tests.yml" 7 | - 'extras/test/**' 8 | - 'src/**' 9 | 10 | push: 11 | paths: 12 | - ".github/workflows/unit-tests.yml" 13 | - 'extras/test/**' 14 | - 'src/**' 15 | 16 | jobs: 17 | test: 18 | name: Run unit tests 19 | runs-on: ubuntu-latest 20 | 21 | env: 22 | COVERAGE_DATA_PATH: extras/coverage-data/coverage.info 23 | 24 | steps: 25 | - name: Checkout 26 | uses: actions/checkout@v4 27 | 28 | - uses: arduino/cpp-test-action@main 29 | with: 30 | runtime-paths: | 31 | - extras/test/build/bin/TEST_TARGET_UUID 32 | - extras/test/build/bin/TEST_TARGET_DISC_DEVICE 33 | - extras/test/build/bin/TEST_TARGET_ADVERTISING_DATA 34 | coverage-exclude-paths: | 35 | - '*/extras/test/*' 36 | - '/usr/*' 37 | coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} 38 | 39 | # A token is used to avoid intermittent spurious job failures caused by rate limiting. 40 | - name: Set up Codecov upload token 41 | run: | 42 | if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoBLE" ]]; then 43 | # In order to avoid uploads of data from forks, only use the token for runs in the parent repo. 44 | # Token is intentionally exposed. 45 | # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 46 | CODECOV_TOKEN="8118de48-b2af-48b4-a66a-26026847bfc5" 47 | else 48 | # codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input. 49 | CODECOV_TOKEN="" 50 | fi 51 | echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV" 52 | 53 | - name: Upload coverage report to Codecov 54 | uses: codecov/codecov-action@v3 55 | with: 56 | file: "${{ env.COVERAGE_DATA_PATH }}" 57 | fail_ci_if_error: true 58 | token: ${{ env.CODECOV_TOKEN }} 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | ArduinoBLE ?.?.? - ????.??.?? 2 | 3 | ArduinoBLE 1.1.2 - 2019.11.12 4 | 5 | * cordio: switch to lower power when polling with timeout 6 | * Fixed issue with wrong types for disconnection event handling. Thanks @cparata 7 | 8 | ArduinoBLE 1.1.1 - 2019.09.05 9 | 10 | * [Cordio] Fixed crashing when BLE.begin() is not called 11 | * Fixed ACL RX packet fragmentation, affected devices that supported large MTUs 12 | * Fixed BLECharacteristic::readValue(...) triggering a read request after a notification/indication was received 13 | * Added support for case insensitive comparison of UUID or address string inputs 14 | * Added (optional) company ID argument to BLE.setManufacturerData(...). Thanks @konikoni428 15 | 16 | ArduinoBLE 1.1.0 - 2019.08.27 17 | 18 | * Added BLE Central support 19 | 20 | ArduinoBLE 1.0.0 - 2019.07.31 21 | 22 | * Added support for Arduino Nano 33 BLE boards 23 | 24 | ArduinoBLE 0.1.2 and older 25 | 26 | * Changes not recorded 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArduinoBLE 2 | 3 | [![Compile Examples Status](https://github.com/arduino-libraries/ArduinoBLE/workflows/Compile%20Examples/badge.svg)](https://github.com/arduino-libraries/ArduinoBLE/actions?workflow=Compile+Examples) [![Spell Check Status](https://github.com/arduino-libraries/ArduinoBLE/workflows/Spell%20Check/badge.svg)](https://github.com/arduino-libraries/ArduinoBLE/actions?workflow=Spell+Check) 4 | 5 | Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev2, Arduino Nano 33 IoT, Arduino Nano 33 BLE, Arduino Portenta H7, Arduino Giga R1 and Arduino UNO R4 WiFi. 6 | 7 | This library supports creating a Bluetooth® Low Energy peripheral & central mode. 8 | 9 | For the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev2, and Arduino Nano 33 IoT boards, it requires the NINA module to be running [Arduino NINA-W102 firmware](https://github.com/arduino/nina-fw) v1.2.0 or later. 10 | 11 | For the Arduino UNO R4 WiFi, it requires the ESP32-S3 module to be running [firmware](https://github.com/arduino/uno-r4-wifi-usb-bridge) v0.2.0 or later. 12 | 13 | 14 | For more information about this library please visit us at: 15 | https://www.arduino.cc/en/Reference/ArduinoBLE 16 | 17 | ## License 18 | 19 | ``` 20 | Copyright (c) 2019 Arduino SA. All rights reserved. 21 | 22 | This library is free software; you can redistribute it and/or 23 | modify it under the terms of the GNU Lesser General Public 24 | License as published by the Free Software Foundation; either 25 | version 2.1 of the License, or (at your option) any later version. 26 | 27 | This library is distributed in the hope that it will be useful, 28 | but WITHOUT ANY WARRANTY; without even the implied warranty of 29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 30 | Lesser General Public License for more details. 31 | 32 | You should have received a copy of the GNU Lesser General Public 33 | License along with this library; if not, write to the Free Software 34 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 35 | ``` 36 | -------------------------------------------------------------------------------- /docs/assets/ble-bulletin-board-model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arduino-libraries/ArduinoBLE/9263b3a058d5b00fe47b2ed244bbaa98853d3214/docs/assets/ble-bulletin-board-model.png -------------------------------------------------------------------------------- /examples/Central/LedControl/LedControl.ino: -------------------------------------------------------------------------------- 1 | /* 2 | LED Control 3 | 4 | This example scans for Bluetooth® Low Energy peripherals until one with the advertised service 5 | "19b10000-e8f2-537e-4f6c-d104768a1214" UUID is found. Once discovered and connected, 6 | it will remotely control the Bluetooth® Low Energy peripheral's LED, when the button is pressed or released. 7 | 8 | The circuit: 9 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 10 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 11 | - Button with pull-up resistor connected to pin 2. 12 | 13 | You can use it with another board that is compatible with this library and the 14 | Peripherals -> LED example. 15 | 16 | This example code is in the public domain. 17 | */ 18 | 19 | #include 20 | 21 | // variables for button 22 | const int buttonPin = 2; 23 | int oldButtonState = LOW; 24 | 25 | void setup() { 26 | Serial.begin(9600); 27 | while (!Serial); 28 | 29 | // configure the button pin as input 30 | pinMode(buttonPin, INPUT); 31 | 32 | // initialize the Bluetooth® Low Energy hardware 33 | BLE.begin(); 34 | 35 | Serial.println("Bluetooth® Low Energy Central - LED control"); 36 | 37 | // start scanning for peripherals 38 | BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); 39 | } 40 | 41 | void loop() { 42 | // check if a peripheral has been discovered 43 | BLEDevice peripheral = BLE.available(); 44 | 45 | if (peripheral) { 46 | // discovered a peripheral, print out address, local name, and advertised service 47 | Serial.print("Found "); 48 | Serial.print(peripheral.address()); 49 | Serial.print(" '"); 50 | Serial.print(peripheral.localName()); 51 | Serial.print("' "); 52 | Serial.print(peripheral.advertisedServiceUuid()); 53 | Serial.println(); 54 | 55 | if (peripheral.localName() != "LED") { 56 | return; 57 | } 58 | 59 | // stop scanning 60 | BLE.stopScan(); 61 | 62 | controlLed(peripheral); 63 | 64 | // peripheral disconnected, start scanning again 65 | BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); 66 | } 67 | } 68 | 69 | void controlLed(BLEDevice peripheral) { 70 | // connect to the peripheral 71 | Serial.println("Connecting ..."); 72 | 73 | if (peripheral.connect()) { 74 | Serial.println("Connected"); 75 | } else { 76 | Serial.println("Failed to connect!"); 77 | return; 78 | } 79 | 80 | // discover peripheral attributes 81 | Serial.println("Discovering attributes ..."); 82 | if (peripheral.discoverAttributes()) { 83 | Serial.println("Attributes discovered"); 84 | } else { 85 | Serial.println("Attribute discovery failed!"); 86 | peripheral.disconnect(); 87 | return; 88 | } 89 | 90 | // retrieve the LED characteristic 91 | BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214"); 92 | 93 | if (!ledCharacteristic) { 94 | Serial.println("Peripheral does not have LED characteristic!"); 95 | peripheral.disconnect(); 96 | return; 97 | } else if (!ledCharacteristic.canWrite()) { 98 | Serial.println("Peripheral does not have a writable LED characteristic!"); 99 | peripheral.disconnect(); 100 | return; 101 | } 102 | 103 | while (peripheral.connected()) { 104 | // while the peripheral is connected 105 | 106 | // read the button pin 107 | int buttonState = digitalRead(buttonPin); 108 | 109 | if (oldButtonState != buttonState) { 110 | // button changed 111 | oldButtonState = buttonState; 112 | 113 | if (buttonState) { 114 | Serial.println("button pressed"); 115 | 116 | // button is pressed, write 0x01 to turn the LED on 117 | ledCharacteristic.writeValue((byte)0x01); 118 | } else { 119 | Serial.println("button released"); 120 | 121 | // button is released, write 0x00 to turn the LED off 122 | ledCharacteristic.writeValue((byte)0x00); 123 | } 124 | } 125 | } 126 | 127 | Serial.println("Peripheral disconnected"); 128 | } 129 | -------------------------------------------------------------------------------- /examples/Central/Scan/Scan.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Scan 3 | 4 | This example scans for Bluetooth® Low Energy peripherals and prints out their advertising details: 5 | address, local name, advertised service UUID's. 6 | 7 | The circuit: 8 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 9 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 10 | 11 | This example code is in the public domain. 12 | */ 13 | 14 | #include 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | while (!Serial); 19 | 20 | // begin initialization 21 | if (!BLE.begin()) { 22 | Serial.println("starting Bluetooth® Low Energy module failed!"); 23 | 24 | while (1); 25 | } 26 | 27 | Serial.println("Bluetooth® Low Energy Central scan"); 28 | 29 | // start scanning for peripheral 30 | BLE.scan(); 31 | } 32 | 33 | void loop() { 34 | // check if a peripheral has been discovered 35 | BLEDevice peripheral = BLE.available(); 36 | 37 | if (peripheral) { 38 | // discovered a peripheral 39 | Serial.println("Discovered a peripheral"); 40 | Serial.println("-----------------------"); 41 | 42 | // print address 43 | Serial.print("Address: "); 44 | Serial.println(peripheral.address()); 45 | 46 | // print the local name, if present 47 | if (peripheral.hasLocalName()) { 48 | Serial.print("Local Name: "); 49 | Serial.println(peripheral.localName()); 50 | } 51 | 52 | // print the advertised service UUIDs, if present 53 | if (peripheral.hasAdvertisedServiceUuid()) { 54 | Serial.print("Service UUIDs: "); 55 | for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) { 56 | Serial.print(peripheral.advertisedServiceUuid(i)); 57 | Serial.print(" "); 58 | } 59 | Serial.println(); 60 | } 61 | 62 | // print the RSSI 63 | Serial.print("RSSI: "); 64 | Serial.println(peripheral.rssi()); 65 | 66 | Serial.println(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /examples/Central/ScanCallback/ScanCallback.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Scan Callback 3 | 4 | This example scans for Bluetooth® Low Energy peripherals and prints out their advertising details: 5 | address, local name, advertised service UUIDs. Unlike the Scan example, it uses 6 | the callback style APIs and disables filtering so the peripheral discovery is 7 | reported for every single advertisement it makes. 8 | 9 | The circuit: 10 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 11 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 12 | 13 | This example code is in the public domain. 14 | */ 15 | 16 | #include 17 | 18 | void setup() { 19 | Serial.begin(9600); 20 | while (!Serial); 21 | 22 | // begin initialization 23 | if (!BLE.begin()) { 24 | Serial.println("starting Bluetooth® Low Energy module failed!"); 25 | 26 | while (1); 27 | } 28 | 29 | Serial.println("Bluetooth® Low Energy Central scan callback"); 30 | 31 | // set the discovered event handle 32 | BLE.setEventHandler(BLEDiscovered, bleCentralDiscoverHandler); 33 | 34 | // start scanning for peripherals with duplicates 35 | BLE.scan(true); 36 | } 37 | 38 | void loop() { 39 | // poll the central for events 40 | BLE.poll(); 41 | } 42 | 43 | void bleCentralDiscoverHandler(BLEDevice peripheral) { 44 | // discovered a peripheral 45 | Serial.println("Discovered a peripheral"); 46 | Serial.println("-----------------------"); 47 | 48 | // print address 49 | Serial.print("Address: "); 50 | Serial.println(peripheral.address()); 51 | 52 | // print the local name, if present 53 | if (peripheral.hasLocalName()) { 54 | Serial.print("Local Name: "); 55 | Serial.println(peripheral.localName()); 56 | } 57 | 58 | // print the advertised service UUIDs, if present 59 | if (peripheral.hasAdvertisedServiceUuid()) { 60 | Serial.print("Service UUIDs: "); 61 | for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) { 62 | Serial.print(peripheral.advertisedServiceUuid(i)); 63 | Serial.print(" "); 64 | } 65 | Serial.println(); 66 | } 67 | 68 | // print the RSSI 69 | Serial.print("RSSI: "); 70 | Serial.println(peripheral.rssi()); 71 | 72 | Serial.println(); 73 | } 74 | -------------------------------------------------------------------------------- /examples/Central/SensorTagButton/SensorTagButton.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SensorTag Button 3 | 4 | This example scans for Bluetooth® Low Energy peripherals until a TI SensorTag is discovered. 5 | It then connects to it, discovers the attributes of the 0xffe0 service, 6 | subscribes to the Simple Key Characteristic (UUID 0xffe1). When a button is 7 | pressed on the SensorTag a notification is received and the button state is 8 | outputted to the Serial Monitor when one is pressed. 9 | 10 | The circuit: 11 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 12 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 13 | - TI SensorTag 14 | 15 | This example code is in the public domain. 16 | */ 17 | 18 | #include 19 | 20 | void setup() { 21 | Serial.begin(9600); 22 | while (!Serial); 23 | 24 | // begin initialization 25 | if (!BLE.begin()) { 26 | Serial.println("starting Bluetooth® Low Energy module failed!"); 27 | 28 | while (1); 29 | } 30 | 31 | Serial.println("Bluetooth® Low Energy Central - SensorTag button"); 32 | Serial.println("Make sure to turn on the device."); 33 | 34 | // start scanning for peripheral 35 | BLE.scan(); 36 | } 37 | 38 | void loop() { 39 | // check if a peripheral has been discovered 40 | BLEDevice peripheral = BLE.available(); 41 | 42 | if (peripheral) { 43 | // discovered a peripheral, print out address, local name, and advertised service 44 | Serial.print("Found "); 45 | Serial.print(peripheral.address()); 46 | Serial.print(" '"); 47 | Serial.print(peripheral.localName()); 48 | Serial.print("' "); 49 | Serial.print(peripheral.advertisedServiceUuid()); 50 | Serial.println(); 51 | 52 | // Check if the peripheral is a SensorTag, the local name will be: 53 | // "CC2650 SensorTag" 54 | if (peripheral.localName() == "CC2650 SensorTag") { 55 | // stop scanning 56 | BLE.stopScan(); 57 | 58 | monitorSensorTagButtons(peripheral); 59 | 60 | // peripheral disconnected, start scanning again 61 | BLE.scan(); 62 | } 63 | } 64 | } 65 | 66 | void monitorSensorTagButtons(BLEDevice peripheral) { 67 | // connect to the peripheral 68 | Serial.println("Connecting ..."); 69 | if (peripheral.connect()) { 70 | Serial.println("Connected"); 71 | } else { 72 | Serial.println("Failed to connect!"); 73 | return; 74 | } 75 | 76 | // discover peripheral attributes 77 | Serial.println("Discovering service 0xffe0 ..."); 78 | if (peripheral.discoverService("ffe0")) { 79 | Serial.println("Service discovered"); 80 | } else { 81 | Serial.println("Attribute discovery failed."); 82 | peripheral.disconnect(); 83 | 84 | while (1); 85 | return; 86 | } 87 | 88 | // retrieve the simple key characteristic 89 | BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("ffe1"); 90 | 91 | // subscribe to the simple key characteristic 92 | Serial.println("Subscribing to simple key characteristic ..."); 93 | if (!simpleKeyCharacteristic) { 94 | Serial.println("no simple key characteristic found!"); 95 | peripheral.disconnect(); 96 | return; 97 | } else if (!simpleKeyCharacteristic.canSubscribe()) { 98 | Serial.println("simple key characteristic is not subscribable!"); 99 | peripheral.disconnect(); 100 | return; 101 | } else if (!simpleKeyCharacteristic.subscribe()) { 102 | Serial.println("subscription failed!"); 103 | peripheral.disconnect(); 104 | return; 105 | } else { 106 | Serial.println("Subscribed"); 107 | Serial.println("Press the right and left buttons on your SensorTag."); 108 | } 109 | 110 | while (peripheral.connected()) { 111 | // while the peripheral is connected 112 | 113 | // check if the value of the simple key characteristic has been updated 114 | if (simpleKeyCharacteristic.valueUpdated()) { 115 | // yes, get the value, characteristic is 1 byte so use byte value 116 | byte value = 0; 117 | 118 | simpleKeyCharacteristic.readValue(value); 119 | 120 | if (value & 0x01) { 121 | // first bit corresponds to the right button 122 | Serial.println("Right button pressed"); 123 | } 124 | 125 | if (value & 0x02) { 126 | // second bit corresponds to the left button 127 | Serial.println("Left button pressed"); 128 | } 129 | } 130 | } 131 | 132 | Serial.println("SensorTag disconnected!"); 133 | } 134 | -------------------------------------------------------------------------------- /examples/Peripheral/Advertising/EnhancedAdvertising/EnhancedAdvertising.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | BLEService myService("fff0"); 4 | BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast); 5 | 6 | // Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop' 7 | const uint8_t manufactData[4] = {0x01, 0x02, 0x03, 0x04}; 8 | const uint8_t serviceData[3] = {0x00, 0x01, 0x02}; 9 | 10 | void setup() { 11 | Serial.begin(9600); 12 | while (!Serial); 13 | 14 | if (!BLE.begin()) { 15 | Serial.println("failed to initialize BLE!"); 16 | while (1); 17 | } 18 | 19 | myService.addCharacteristic(myCharacteristic); 20 | BLE.addService(myService); 21 | 22 | // Build scan response data packet 23 | BLEAdvertisingData scanData; 24 | // Set parameters for scan response packet 25 | scanData.setLocalName("Test enhanced advertising"); 26 | // Copy set parameters in the actual scan response packet 27 | BLE.setScanResponseData(scanData); 28 | 29 | // Build advertising data packet 30 | BLEAdvertisingData advData; 31 | // Set parameters for advertising packet 32 | advData.setManufacturerData(0x004C, manufactData, sizeof(manufactData)); 33 | advData.setAdvertisedService(myService); 34 | advData.setAdvertisedServiceData(0xfff0, serviceData, sizeof(serviceData)); 35 | // Copy set parameters in the actual advertising packet 36 | BLE.setAdvertisingData(advData); 37 | 38 | BLE.advertise(); 39 | Serial.println("advertising ..."); 40 | } 41 | 42 | void loop() { 43 | BLE.poll(); 44 | } 45 | -------------------------------------------------------------------------------- /examples/Peripheral/Advertising/RawDataAdvertising/RawDataAdvertising.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | BLEService myService("fff0"); 4 | BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast); 5 | 6 | // Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop' 7 | const uint8_t completeRawAdvertisingData[] = {0x02,0x01,0x06,0x09,0xff,0x01,0x01,0x00,0x01,0x02,0x03,0x04,0x05}; 8 | 9 | void setup() { 10 | Serial.begin(9600); 11 | while (!Serial); 12 | 13 | if (!BLE.begin()) { 14 | Serial.println("failed to initialize BLE!"); 15 | while (1); 16 | } 17 | 18 | myService.addCharacteristic(myCharacteristic); 19 | BLE.addService(myService); 20 | 21 | // Build advertising data packet 22 | BLEAdvertisingData advData; 23 | // If a packet has a raw data parameter, then all the other parameters of the packet will be ignored 24 | advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData)); 25 | // Copy set parameters in the actual advertising packet 26 | BLE.setAdvertisingData(advData); 27 | 28 | // Build scan response data packet 29 | BLEAdvertisingData scanData; 30 | scanData.setLocalName("Test advertising raw data"); 31 | // Copy set parameters in the actual scan response packet 32 | BLE.setScanResponseData(scanData); 33 | 34 | BLE.advertise(); 35 | 36 | Serial.println("advertising ..."); 37 | } 38 | 39 | void loop() { 40 | BLE.poll(); 41 | } 42 | -------------------------------------------------------------------------------- /examples/Peripheral/BatteryMonitor/BatteryMonitor.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Battery Monitor 3 | 4 | This example creates a Bluetooth® Low Energy peripheral with the standard battery service and 5 | level characteristic. The A0 pin is used to calculate the battery level. 6 | 7 | The circuit: 8 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 9 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 10 | 11 | You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or 12 | nRF Connect (Android), to interact with the services and characteristics 13 | created in this sketch. 14 | 15 | This example code is in the public domain. 16 | */ 17 | 18 | #include 19 | 20 | // Bluetooth® Low Energy Battery Service 21 | BLEService batteryService("180F"); 22 | 23 | // Bluetooth® Low Energy Battery Level Characteristic 24 | BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID 25 | BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes 26 | 27 | int oldBatteryLevel = 0; // last battery level reading from analog input 28 | long previousMillis = 0; // last time the battery level was checked, in ms 29 | 30 | void setup() { 31 | Serial.begin(9600); // initialize serial communication 32 | while (!Serial); 33 | 34 | pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected 35 | 36 | // begin initialization 37 | if (!BLE.begin()) { 38 | Serial.println("starting BLE failed!"); 39 | 40 | while (1); 41 | } 42 | 43 | /* Set a local name for the Bluetooth® Low Energy device 44 | This name will appear in advertising packets 45 | and can be used by remote devices to identify this Bluetooth® Low Energy device 46 | The name can be changed but maybe be truncated based on space left in advertisement packet 47 | */ 48 | BLE.setLocalName("BatteryMonitor"); 49 | BLE.setAdvertisedService(batteryService); // add the service UUID 50 | batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic 51 | BLE.addService(batteryService); // Add the battery service 52 | batteryLevelChar.writeValue(oldBatteryLevel); // set initial value for this characteristic 53 | 54 | /* Start advertising Bluetooth® Low Energy. It will start continuously transmitting Bluetooth® Low Energy 55 | advertising packets and will be visible to remote Bluetooth® Low Energy central devices 56 | until it receives a new connection */ 57 | 58 | // start advertising 59 | BLE.advertise(); 60 | 61 | Serial.println("Bluetooth® device active, waiting for connections..."); 62 | } 63 | 64 | void loop() { 65 | // wait for a Bluetooth® Low Energy central 66 | BLEDevice central = BLE.central(); 67 | 68 | // if a central is connected to the peripheral: 69 | if (central) { 70 | Serial.print("Connected to central: "); 71 | // print the central's BT address: 72 | Serial.println(central.address()); 73 | // turn on the LED to indicate the connection: 74 | digitalWrite(LED_BUILTIN, HIGH); 75 | 76 | // check the battery level every 200 ms 77 | // while the central is connected: 78 | while (central.connected()) { 79 | long currentMillis = millis(); 80 | // if 200 ms have passed, check the battery level: 81 | if (currentMillis - previousMillis >= 200) { 82 | previousMillis = currentMillis; 83 | updateBatteryLevel(); 84 | } 85 | } 86 | // when the central disconnects, turn off the LED: 87 | digitalWrite(LED_BUILTIN, LOW); 88 | Serial.print("Disconnected from central: "); 89 | Serial.println(central.address()); 90 | } 91 | } 92 | 93 | void updateBatteryLevel() { 94 | /* Read the current voltage level on the A0 analog input pin. 95 | This is used here to simulate the charge level of a battery. 96 | */ 97 | int battery = analogRead(A0); 98 | int batteryLevel = map(battery, 0, 1023, 0, 100); 99 | 100 | if (batteryLevel != oldBatteryLevel) { // if the battery level has changed 101 | Serial.print("Battery Level % is now: "); // print it 102 | Serial.println(batteryLevel); 103 | batteryLevelChar.writeValue(batteryLevel); // and update the battery level characteristic 104 | oldBatteryLevel = batteryLevel; // save the level for next comparison 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /examples/Peripheral/ButtonLED/ButtonLED.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Button LED 3 | 4 | This example creates a Bluetooth® Low Energy peripheral with service that contains a 5 | characteristic to control an LED and another characteristic that 6 | represents the state of the button. 7 | 8 | The circuit: 9 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 10 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 11 | - Button connected to pin 4 12 | 13 | You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or 14 | nRF Connect (Android), to interact with the services and characteristics 15 | created in this sketch. 16 | 17 | This example code is in the public domain. 18 | */ 19 | 20 | #include 21 | 22 | const int ledPin = LED_BUILTIN; // set ledPin to on-board LED 23 | const int buttonPin = 4; // set buttonPin to digital pin 4 24 | 25 | BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service 26 | 27 | // create switch characteristic and allow remote device to read and write 28 | BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 29 | // create button characteristic and allow remote device to get notifications 30 | BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); 31 | 32 | void setup() { 33 | Serial.begin(9600); 34 | while (!Serial); 35 | 36 | pinMode(ledPin, OUTPUT); // use the LED as an output 37 | pinMode(buttonPin, INPUT); // use button pin as an input 38 | 39 | // begin initialization 40 | if (!BLE.begin()) { 41 | Serial.println("starting Bluetooth® Low Energy module failed!"); 42 | 43 | while (1); 44 | } 45 | 46 | // set the local name peripheral advertises 47 | BLE.setLocalName("ButtonLED"); 48 | // set the UUID for the service this peripheral advertises: 49 | BLE.setAdvertisedService(ledService); 50 | 51 | // add the characteristics to the service 52 | ledService.addCharacteristic(ledCharacteristic); 53 | ledService.addCharacteristic(buttonCharacteristic); 54 | 55 | // add the service 56 | BLE.addService(ledService); 57 | 58 | ledCharacteristic.writeValue(0); 59 | buttonCharacteristic.writeValue(0); 60 | 61 | // start advertising 62 | BLE.advertise(); 63 | 64 | Serial.println("Bluetooth® device active, waiting for connections..."); 65 | } 66 | 67 | void loop() { 68 | // poll for Bluetooth® Low Energy events 69 | BLE.poll(); 70 | 71 | // read the current button pin state 72 | char buttonValue = digitalRead(buttonPin); 73 | 74 | // has the value changed since the last read 75 | bool buttonChanged = (buttonCharacteristic.value() != buttonValue); 76 | 77 | if (buttonChanged) { 78 | // button state changed, update characteristics 79 | ledCharacteristic.writeValue(buttonValue); 80 | buttonCharacteristic.writeValue(buttonValue); 81 | } 82 | 83 | if (ledCharacteristic.written() || buttonChanged) { 84 | // update LED, either central has written to characteristic or button state has changed 85 | if (ledCharacteristic.value()) { 86 | Serial.println("LED on"); 87 | digitalWrite(ledPin, HIGH); 88 | } else { 89 | Serial.println("LED off"); 90 | digitalWrite(ledPin, LOW); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /examples/Peripheral/CallbackLED/CallbackLED.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Callback LED 3 | 4 | This example creates a Bluetooth® Low Energy peripheral with service that contains a 5 | characteristic to control an LED. The callback features of the 6 | library are used. 7 | 8 | The circuit: 9 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 10 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 11 | 12 | You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or 13 | nRF Connect (Android), to interact with the services and characteristics 14 | created in this sketch. 15 | 16 | This example code is in the public domain. 17 | */ 18 | 19 | #include 20 | 21 | BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service 22 | 23 | // create switch characteristic and allow remote device to read and write 24 | BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 25 | 26 | const int ledPin = LED_BUILTIN; // pin to use for the LED 27 | 28 | void setup() { 29 | Serial.begin(9600); 30 | while (!Serial); 31 | 32 | pinMode(ledPin, OUTPUT); // use the LED pin as an output 33 | 34 | // begin initialization 35 | if (!BLE.begin()) { 36 | Serial.println("starting Bluetooth® Low Energy module failed!"); 37 | 38 | while (1); 39 | } 40 | 41 | // set the local name peripheral advertises 42 | BLE.setLocalName("LEDCallback"); 43 | // set the UUID for the service this peripheral advertises 44 | BLE.setAdvertisedService(ledService); 45 | 46 | // add the characteristic to the service 47 | ledService.addCharacteristic(switchCharacteristic); 48 | 49 | // add service 50 | BLE.addService(ledService); 51 | 52 | // assign event handlers for connected, disconnected to peripheral 53 | BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler); 54 | BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); 55 | 56 | // assign event handlers for characteristic 57 | switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); 58 | // set an initial value for the characteristic 59 | switchCharacteristic.setValue(0); 60 | 61 | // start advertising 62 | BLE.advertise(); 63 | 64 | Serial.println(("Bluetooth® device active, waiting for connections...")); 65 | } 66 | 67 | void loop() { 68 | // poll for Bluetooth® Low Energy events 69 | BLE.poll(); 70 | } 71 | 72 | void blePeripheralConnectHandler(BLEDevice central) { 73 | // central connected event handler 74 | Serial.print("Connected event, central: "); 75 | Serial.println(central.address()); 76 | } 77 | 78 | void blePeripheralDisconnectHandler(BLEDevice central) { 79 | // central disconnected event handler 80 | Serial.print("Disconnected event, central: "); 81 | Serial.println(central.address()); 82 | } 83 | 84 | void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) { 85 | // central wrote new value to characteristic, update LED 86 | Serial.print("Characteristic event, written: "); 87 | 88 | if (switchCharacteristic.value()) { 89 | Serial.println("LED on"); 90 | digitalWrite(ledPin, HIGH); 91 | } else { 92 | Serial.println("LED off"); 93 | digitalWrite(ledPin, LOW); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /examples/Peripheral/LED/LED.ino: -------------------------------------------------------------------------------- 1 | /* 2 | LED 3 | 4 | This example creates a Bluetooth® Low Energy peripheral with service that contains a 5 | characteristic to control an LED. 6 | 7 | The circuit: 8 | - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, 9 | Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. 10 | 11 | You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or 12 | nRF Connect (Android), to interact with the services and characteristics 13 | created in this sketch. 14 | 15 | This example code is in the public domain. 16 | */ 17 | 18 | #include 19 | 20 | BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service 21 | 22 | // Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central 23 | BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 24 | 25 | const int ledPin = LED_BUILTIN; // pin to use for the LED 26 | 27 | void setup() { 28 | Serial.begin(9600); 29 | while (!Serial); 30 | 31 | // set LED pin to output mode 32 | pinMode(ledPin, OUTPUT); 33 | 34 | // begin initialization 35 | if (!BLE.begin()) { 36 | Serial.println("starting Bluetooth® Low Energy module failed!"); 37 | 38 | while (1); 39 | } 40 | 41 | // set advertised local name and service UUID: 42 | BLE.setLocalName("LED"); 43 | BLE.setAdvertisedService(ledService); 44 | 45 | // add the characteristic to the service 46 | ledService.addCharacteristic(switchCharacteristic); 47 | 48 | // add service 49 | BLE.addService(ledService); 50 | 51 | // set the initial value for the characteristic: 52 | switchCharacteristic.writeValue(0); 53 | 54 | // start advertising 55 | BLE.advertise(); 56 | 57 | Serial.println("BLE LED Peripheral"); 58 | } 59 | 60 | void loop() { 61 | // listen for Bluetooth® Low Energy peripherals to connect: 62 | BLEDevice central = BLE.central(); 63 | 64 | // if a central is connected to peripheral: 65 | if (central) { 66 | Serial.print("Connected to central: "); 67 | // print the central's MAC address: 68 | Serial.println(central.address()); 69 | 70 | // while the central is still connected to peripheral: 71 | while (central.connected()) { 72 | // if the remote device wrote to the characteristic, 73 | // use the value to control the LED: 74 | if (switchCharacteristic.written()) { 75 | if (switchCharacteristic.value()) { // any value other than 0 76 | Serial.println("LED on"); 77 | digitalWrite(ledPin, HIGH); // will turn the LED on 78 | } else { // a 0 value 79 | Serial.println(F("LED off")); 80 | digitalWrite(ledPin, LOW); // will turn the LED off 81 | } 82 | } 83 | } 84 | 85 | // when the central disconnects, print it out: 86 | Serial.print(F("Disconnected from central: ")); 87 | Serial.println(central.address()); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /extras/arduino-ble-parser.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Convert ArduinoBLE debug files into Btsnoop files ready to be analyzed using wireshark or hcidump 3 | Btsnoop file format reference 4 | https://www.fte.com/WebHelpII/Sodera/Content/Technical_Information/BT_Snoop_File_Format.htm 5 | ''' 6 | 7 | import os 8 | import argparse 9 | 10 | DEBUG = False 11 | 12 | parser = argparse.ArgumentParser() 13 | parser.add_argument('-i', dest='inputPath', type=str, required=True, help='input file containing debug log') 14 | parser.add_argument('-o', dest='outputPath', type=str, required=True, help='result file that will contain the btsnoop encoded debug file') 15 | args = parser.parse_args() 16 | 17 | # Extract only hci debug messages 18 | def extractHCIDebugPrint(inputPath, outputPath): 19 | inputFile = open(inputPath, 'r') 20 | outputFile = open(outputPath, 'w') 21 | for inputLine in inputFile: 22 | lineItems = inputLine.split() 23 | if (len(lineItems) < 7) or (lineItems[1] != "->") or (lineItems[2] != "HCI"): 24 | if (len(lineItems) < 4) or (lineItems[0] != "HCI") or ((lineItems[3] != "<-") and (lineItems[3] != "->")): 25 | continue 26 | outputFile.write(inputLine) 27 | outputFile.close() 28 | 29 | # Return packet in btsnoop format 30 | def buildBinaryPacket(hciMessage, hciDirection, hciType): 31 | commandFlag = 1 if (hciType == "COMMAND" or hciType == "EVENT") else 0 32 | directionFlag = 0 if (hciDirection == "TX") else 1 33 | flagHex = ("0" * 7) + str((commandFlag * 2) + directionFlag) 34 | timestampHex = "0" * 16 35 | packetDropHex = "0" * 8 36 | dataLengthHex = format( (int(len(hciMessage) / 2)), 'x') 37 | packetLengthHex = ("0" * (8 - len(dataLengthHex))) + dataLengthHex 38 | binaryPacket = bytearray.fromhex(packetLengthHex + packetLengthHex + flagHex + packetDropHex + timestampHex + hciMessage) 39 | if DEBUG: 40 | print(len(hciMessage)) 41 | print(dataLengthHex) 42 | print(packetLengthHex) 43 | print(flagHex) 44 | print('\n') 45 | return binaryPacket 46 | 47 | def buildBinaryHeader(): 48 | defaultHeader = "6274736e6f6f700000000001000003ea" 49 | binaryHeader = bytearray.fromhex(defaultHeader) 50 | return binaryHeader 51 | 52 | def convertToBtsnoop(inputPath, outputPath): 53 | # Open output file and write the Btsnoop header 54 | outputFile = open(outputPath,'wb') 55 | header = buildBinaryHeader() 56 | outputFile.write(header) 57 | 58 | # Open input file containing HCI debug packets 59 | inputFile = open(inputPath, 'r') 60 | for inputLine in inputFile: 61 | lineItems = inputLine.split() 62 | # For a safer script, do not use indexes but look for symbols in the line 63 | baseIndex = lineItems.index("HCI") 64 | hciMessage = lineItems[baseIndex + 4] 65 | hciDirection = lineItems[baseIndex + 2] 66 | hciType = lineItems[baseIndex + 1] 67 | # Build and write the encoded line 68 | btsnoopPacket = buildBinaryPacket(hciMessage, hciDirection, hciType) 69 | outputFile.write(btsnoopPacket) 70 | if DEBUG: 71 | print(hciDirection) 72 | print(hciMessage) 73 | print(hciType) 74 | print('\n') 75 | outputFile.close() 76 | 77 | inputPath = args.inputPath 78 | outputPath = args.outputPath 79 | tempFile = "temp-debug-print.txt" 80 | # Run 81 | extractHCIDebugPrint(inputPath,tempFile) 82 | convertToBtsnoop(tempFile, outputPath) 83 | # Delete temp file 84 | os.remove(tempFile) 85 | 86 | -------------------------------------------------------------------------------- /extras/test/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | ### CMake ### 3 | CMakeLists.txt.user 4 | CMakeCache.txt 5 | CMakeFiles 6 | CMakeScripts 7 | Testing 8 | Makefile 9 | cmake_install.cmake 10 | install_manifest.txt 11 | compile_commands.json 12 | CTestTestfile.cmake 13 | _deps 14 | 15 | ### CMake Patch ### 16 | # External projects 17 | *-prefix/ -------------------------------------------------------------------------------- /extras/test/include/Arduino.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef TEST_ARDUINO_H_ 21 | #define TEST_ARDUINO_H_ 22 | 23 | /****************************************************************************** 24 | INCLUDE 25 | ******************************************************************************/ 26 | 27 | #include 28 | #include 29 | #include "String.h" 30 | #include "Stream.h" 31 | #include "itoa.h" 32 | #include "Common.h" 33 | 34 | /****************************************************************************** 35 | TYPEDEF 36 | ******************************************************************************/ 37 | 38 | typedef arduino::String String; 39 | typedef bool boolean; 40 | 41 | #if defined(__cplusplus) 42 | 43 | #undef F 44 | // C++11 F replacement declaration 45 | template 46 | auto F(T1&& A) 47 | -> const arduino::__FlashStringHelper* 48 | { 49 | return (const arduino::__FlashStringHelper*)A; 50 | } 51 | 52 | #endif 53 | 54 | /****************************************************************************** 55 | FUNCTION PROTOTYPES 56 | ******************************************************************************/ 57 | 58 | #endif /* TEST_ARDUINO_H_ */ 59 | -------------------------------------------------------------------------------- /extras/test/include/test_advertising_data/FakeBLELocalDevice.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _FAKE_BLELOCALDEVICE_H_ 21 | #define _FAKE_BLELOCALDEVICE_H_ 22 | 23 | #define private public 24 | #define protected public 25 | #include "BLELocalDevice.h" 26 | 27 | class FakeBLELocalDevice : public BLELocalDevice { 28 | public: 29 | FakeBLELocalDevice(); 30 | virtual ~FakeBLELocalDevice(); 31 | 32 | int advertise(); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /extras/test/include/test_discovered_device/FakeGAP.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _FAKE_GAP_H_ 21 | #define _FAKE_GAP_H_ 22 | 23 | #define private public 24 | #define protected public 25 | #include "GAP.h" 26 | 27 | class FakeGAPClass : public GAPClass { 28 | public: 29 | FakeGAPClass(); 30 | virtual ~FakeGAPClass(); 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /extras/test/include/util/Common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #ifdef __cplusplus 5 | extern "C"{ 6 | #endif 7 | 8 | void yield(void); 9 | 10 | typedef enum { 11 | LOW = 0, 12 | HIGH = 1, 13 | CHANGE = 2, 14 | FALLING = 3, 15 | RISING = 4, 16 | } PinStatus; 17 | 18 | typedef enum { 19 | INPUT = 0x0, 20 | OUTPUT = 0x1, 21 | INPUT_PULLUP = 0x2, 22 | INPUT_PULLDOWN = 0x3, 23 | } PinMode; 24 | 25 | typedef enum { 26 | LSBFIRST = 0, 27 | MSBFIRST = 1, 28 | } BitOrder; 29 | 30 | #define PI 3.1415926535897932384626433832795 31 | #define HALF_PI 1.5707963267948966192313216916398 32 | #define TWO_PI 6.283185307179586476925286766559 33 | #define DEG_TO_RAD 0.017453292519943295769236907684886 34 | #define RAD_TO_DEG 57.295779513082320876798154814105 35 | #define EULER 2.718281828459045235360287471352 36 | 37 | #define SERIAL 0x0 38 | #define DISPLAY 0x1 39 | 40 | #ifndef constrain 41 | #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) 42 | #endif 43 | 44 | #ifndef radians 45 | #define radians(deg) ((deg)*DEG_TO_RAD) 46 | #endif 47 | 48 | #ifndef degrees 49 | #define degrees(rad) ((rad)*RAD_TO_DEG) 50 | #endif 51 | 52 | #ifndef sq 53 | #define sq(x) ((x)*(x)) 54 | #endif 55 | 56 | typedef void (*voidFuncPtr)(void); 57 | typedef void (*voidFuncPtrParam)(void*); 58 | 59 | // interrupts() / noInterrupts() must be defined by the core 60 | 61 | #define lowByte(w) ((uint8_t) ((w) & 0xff)) 62 | #define highByte(w) ((uint8_t) ((w) >> 8)) 63 | 64 | #define bitRead(value, bit) (((value) >> (bit)) & 0x01) 65 | #define bitSet(value, bit) ((value) |= (1UL << (bit))) 66 | #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) 67 | #define bitToggle(value, bit) ((value) ^= (1UL << (bit))) 68 | #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) 69 | 70 | #ifndef bit 71 | #define bit(b) (1UL << (b)) 72 | #endif 73 | 74 | /* TODO: request for removal */ 75 | typedef bool boolean; 76 | typedef uint8_t byte; 77 | typedef uint16_t word; 78 | 79 | void init(void); 80 | void initVariant(void); 81 | 82 | int atexit(void (*func)()) __attribute__((weak)); 83 | int main() __attribute__((weak)); 84 | 85 | #ifdef EXTENDED_PIN_MODE 86 | // Platforms who wnat to declare more than 256 pins need to define EXTENDED_PIN_MODE globally 87 | typedef uint32_t pin_size_t; 88 | #else 89 | typedef uint8_t pin_size_t; 90 | #endif 91 | 92 | void pinMode(pin_size_t pinNumber, PinMode pinMode); 93 | void digitalWrite(pin_size_t pinNumber, PinStatus status); 94 | PinStatus digitalRead(pin_size_t pinNumber); 95 | int analogRead(pin_size_t pinNumber); 96 | void analogReference(uint8_t mode); 97 | void analogWrite(pin_size_t pinNumber, int value); 98 | 99 | unsigned long millis(void); 100 | unsigned long micros(void); 101 | void delay(unsigned long); 102 | void delayMicroseconds(unsigned int us); 103 | unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout); 104 | unsigned long pulseInLong(pin_size_t pin, uint8_t state, unsigned long timeout); 105 | 106 | void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val); 107 | pin_size_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder); 108 | 109 | void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode); 110 | void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param); 111 | void detachInterrupt(pin_size_t interruptNumber); 112 | 113 | void setup(void); 114 | void loop(void); 115 | 116 | #ifdef __cplusplus 117 | } // extern "C" 118 | #endif 119 | 120 | #ifdef __cplusplus 121 | template 122 | auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) 123 | { 124 | return (b < a) ? b : a; 125 | } 126 | 127 | template 128 | auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) 129 | { 130 | return (a < b) ? b : a; 131 | } 132 | #else 133 | #ifndef min 134 | #define min(a,b) \ 135 | ({ __typeof__ (a) _a = (a); \ 136 | __typeof__ (b) _b = (b); \ 137 | _a < _b ? _a : _b; }) 138 | #endif 139 | #ifndef max 140 | #define max(a,b) \ 141 | ({ __typeof__ (a) _a = (a); \ 142 | __typeof__ (b) _b = (b); \ 143 | _a > _b ? _a : _b; }) 144 | #endif 145 | #endif 146 | 147 | #ifdef __cplusplus 148 | 149 | /* C++ prototypes */ 150 | uint16_t makeWord(uint16_t w); 151 | uint16_t makeWord(byte h, byte l); 152 | 153 | #define word(...) makeWord(__VA_ARGS__) 154 | 155 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); 156 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); 157 | 158 | void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); 159 | void noTone(uint8_t _pin); 160 | 161 | // WMath prototypes 162 | long random(long); 163 | long random(long, long); 164 | void randomSeed(unsigned long); 165 | long map(long, long, long, long, long); 166 | 167 | #endif // __cplusplus 168 | -------------------------------------------------------------------------------- /extras/test/include/util/HCIFakeTransport.h: -------------------------------------------------------------------------------- 1 | //#include "Common.h" 2 | #pragma once 3 | 4 | #include "HCITransport.h" 5 | 6 | class HCIFakeTransportClass : public HCITransportInterface 7 | { 8 | public: 9 | HCIFakeTransportClass() {}; 10 | ~HCIFakeTransportClass() {}; 11 | 12 | int begin() {return 0;} 13 | void end() {return;} 14 | void wait(unsigned long timeout) {return;} 15 | int available() {return 0;} 16 | int peek() {return 0;} 17 | int read() {return 0;} 18 | size_t write(const uint8_t* data, size_t length) {return 0;} 19 | }; -------------------------------------------------------------------------------- /extras/test/include/util/Stream.h: -------------------------------------------------------------------------------- 1 | //#include "Common.h" 2 | #pragma once 3 | 4 | #define DEC 10 5 | #define HEX 16 6 | #define OCT 8 7 | #define BIN 2 8 | 9 | class Stream 10 | { 11 | public: 12 | Stream(const char *name = NULL); 13 | ~Stream(); 14 | 15 | void flush() {} 16 | 17 | size_t print(const char[]) { return 0; } 18 | size_t print(char) { return 0; } 19 | size_t print(unsigned char, int) { return 0; } 20 | size_t print(int, int) { return 0; } 21 | size_t print(unsigned int, int) { return 0; } 22 | size_t print(long, int) { return 0; } 23 | size_t print(unsigned long, int) { return 0; } 24 | size_t print(long long, int) { return 0; } 25 | size_t print(unsigned long long, int) { return 0; } 26 | size_t print(double, int) { return 0; } 27 | size_t print(void) { return 0; } 28 | 29 | size_t println(const char[]) { return 0; } 30 | size_t println(char) { return 0; } 31 | size_t println(unsigned char, int) { return 0; } 32 | size_t println(int, int) { return 0; } 33 | size_t println(unsigned int, int) { return 0; } 34 | size_t println(long, int) { return 0; } 35 | size_t println(unsigned long, int) { return 0; } 36 | size_t println(long long, int) { return 0; } 37 | size_t println(unsigned long long, int) { return 0; } 38 | size_t println(double, int) { return 0; } 39 | size_t println(void) { return 0; } 40 | }; 41 | -------------------------------------------------------------------------------- /extras/test/include/util/TestUtil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | */ 4 | -------------------------------------------------------------------------------- /extras/test/include/util/itoa.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | #pragma once 20 | 21 | // Standard C functions required in Arduino API 22 | // If these functions are not provided by the standard library, the 23 | // core should supply an implementation of them. 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | extern char* itoa(int value, char *string, int radix); 30 | extern char* ltoa(long value, char *string, int radix); 31 | extern char* utoa(unsigned value, char *string, int radix); 32 | extern char* ultoa(unsigned long value, char *string, int radix); 33 | 34 | #ifdef __cplusplus 35 | } // extern "C" 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /extras/test/src/Arduino.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | /****************************************************************************** 21 | INCLUDE 22 | ******************************************************************************/ 23 | 24 | #include 25 | 26 | /****************************************************************************** 27 | GLOBAL VARIABLES 28 | ******************************************************************************/ 29 | 30 | static unsigned long current_millis = 0; 31 | 32 | /****************************************************************************** 33 | PUBLIC FUNCTIONS 34 | ******************************************************************************/ 35 | 36 | void set_millis(unsigned long const millis) 37 | { 38 | current_millis = millis; 39 | } 40 | 41 | unsigned long millis() 42 | { 43 | return current_millis; 44 | } 45 | -------------------------------------------------------------------------------- /extras/test/src/test_advertising_data/FakeBLELocalDevice.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "FakeBLELocalDevice.h" 21 | 22 | FakeBLELocalDevice::FakeBLELocalDevice() 23 | { 24 | 25 | } 26 | 27 | FakeBLELocalDevice::~FakeBLELocalDevice() 28 | { 29 | 30 | } 31 | 32 | int FakeBLELocalDevice::advertise() 33 | { 34 | _advertisingData.updateData(); 35 | _scanResponseData.updateData(); 36 | return 1; 37 | } 38 | 39 | FakeBLELocalDevice FakeBLEObj; 40 | BLELocalDevice& BLE = FakeBLEObj; 41 | -------------------------------------------------------------------------------- /extras/test/src/test_advertising_data/test_local_name.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #define private public 23 | #define protected public 24 | 25 | #include "FakeBLELocalDevice.h" 26 | #include "BLEAdvertisingData.h" 27 | 28 | TEST_CASE("Test local name setting", "[ArduinoBLE::BLEAdvertisingData]") 29 | { 30 | BLEAdvertisingData advData; 31 | int oldRemainingLength = advData.remainingLength(); 32 | bool retVal; 33 | 34 | WHEN("Set correct local name") 35 | { 36 | const char* name = "test"; 37 | retVal = advData.setLocalName(name); 38 | THEN("Set local name should return true. The name parameter should be correctly set") 39 | { 40 | REQUIRE(retVal); 41 | } 42 | 43 | THEN("Check the exact number of bytes occupied by the name just set") 44 | { 45 | REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) ); 46 | } 47 | oldRemainingLength = advData.remainingLength(); 48 | 49 | advData.updateData(); 50 | REQUIRE(advData.dataLength() == (strlen(name) + 2)); 51 | } 52 | 53 | WHEN("Set too long local name") 54 | { 55 | const char* name = "way too long local name (len 32)"; 56 | retVal = advData.setLocalName(name); 57 | THEN("Set local name should return false. The name parameter should not be set") 58 | { 59 | REQUIRE(!retVal); 60 | REQUIRE( oldRemainingLength == advData.remainingLength() ); 61 | advData.updateData(); 62 | REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) ); 63 | } 64 | } 65 | 66 | WHEN("Overwrite local name with a name as long as max data length") 67 | { 68 | const char* name = "local name with full length "; 69 | retVal = advData.setLocalName(name); 70 | THEN("The name parameter should be correctly overwritten. The remaining length should be 0") 71 | { 72 | REQUIRE(retVal); 73 | REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) ); 74 | oldRemainingLength = advData.remainingLength(); 75 | 76 | advData.updateData(); 77 | REQUIRE(advData.dataLength() == (strlen(name) + 2)); 78 | // advData should be full now 79 | REQUIRE( 0 == advData.remainingLength() ); 80 | REQUIRE( 0 == advData.availableForWrite() ); 81 | } 82 | } 83 | 84 | WHEN("Check consistency when setting the external advertising data") 85 | { 86 | const auto goldenData = advData.data(); 87 | BLE.setAdvertisingData(advData); 88 | BLE.advertise(); 89 | REQUIRE( 0 == memcmp(goldenData, BLE.getAdvertisingData().data(), advData.dataLength()) ); 90 | } 91 | 92 | // Clear BLE advertising data 93 | advData.clear(); 94 | BLE.setAdvertisingData(advData); 95 | } 96 | -------------------------------------------------------------------------------- /extras/test/src/test_characteristic/test_permissions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #define private public 23 | #define protected public 24 | 25 | #include "FakeBLELocalDevice.h" 26 | #include "BLEAdvertisingData.h" 27 | #include "BLETypedCharacteristics.h" 28 | #include "BLELocalCharacteristic.h" 29 | #include "BLEStringCharacteristic.h" 30 | #include "BLEProperty.h" 31 | #include 32 | 33 | int property[] = { 34 | BLEBroadcast, 35 | BLERead, 36 | BLEWriteWithoutResponse, 37 | BLEWrite, 38 | BLENotify, 39 | BLEIndicate, 40 | BLEAuthSignedWrite, 41 | BLEExtProp, 42 | BLERead | BLEWrite | BLENotify 43 | }; 44 | 45 | int permission[] = { 46 | BLEEncryption, 47 | BLEAuthentication, 48 | BLEAuthorization, 49 | BLEEncryption | BLEAuthentication 50 | }; 51 | 52 | const char uuid[][31] = { 53 | "1 Bool", 54 | "2 Char", 55 | "3 UnsignedChar", 56 | "4 Byte", 57 | "5 Short", 58 | "6 UnsignedShort", 59 | "7 Word", 60 | "8 Int", 61 | "9 UnsignedInt", 62 | "A Long", 63 | "B UnsignedLong", 64 | "C Float", 65 | "D Double", 66 | "E String" 67 | }; 68 | 69 | std::unique_ptr createCharacteristic(const char* uuid, unsigned int properties) 70 | { 71 | switch(uuid[0]) 72 | { 73 | case '1': 74 | return std::unique_ptr(new BLEBoolCharacteristic(uuid, properties)); 75 | case '2': 76 | return std::unique_ptr(new BLECharCharacteristic(uuid, properties)); 77 | case '3': 78 | return std::unique_ptr(new BLEUnsignedCharCharacteristic(uuid, properties)); 79 | case '4': 80 | return std::unique_ptr(new BLEByteCharacteristic(uuid, properties)); 81 | case '5': 82 | return std::unique_ptr(new BLEShortCharacteristic(uuid, properties)); 83 | case '6': 84 | return std::unique_ptr(new BLEUnsignedShortCharacteristic(uuid, properties)); 85 | case '7': 86 | return std::unique_ptr(new BLEWordCharacteristic(uuid, properties)); 87 | case '8': 88 | return std::unique_ptr(new BLEIntCharacteristic(uuid, properties)); 89 | case '9': 90 | return std::unique_ptr(new BLEUnsignedIntCharacteristic(uuid, properties)); 91 | case 'A': 92 | return std::unique_ptr(new BLELongCharacteristic(uuid, properties)); 93 | case 'B': 94 | return std::unique_ptr(new BLEUnsignedLongCharacteristic(uuid, properties)); 95 | case 'C': 96 | return std::unique_ptr(new BLEFloatCharacteristic(uuid, properties)); 97 | case 'D': 98 | return std::unique_ptr(new BLEDoubleCharacteristic(uuid, properties)); 99 | case 'E': 100 | return std::unique_ptr(new BLEStringCharacteristic(uuid, properties, 2)); 101 | default: 102 | break; 103 | } 104 | return nullptr; 105 | } 106 | 107 | TEST_CASE("Test characteristic properties and permissions", "[ArduinoBLE::BLECharacteristic]") 108 | { 109 | WHEN("Create a characteristic") 110 | { 111 | for(int i = 0; i < sizeof(property)/sizeof(int); i++) 112 | { 113 | for(int j = 0; j < sizeof(permission)/sizeof(int); j++) 114 | { 115 | for(int k = 0; k < 14; k++) 116 | { 117 | std::unique_ptr ptr = createCharacteristic(uuid[k], property[i] | permission[j]); 118 | REQUIRE(ptr != nullptr); 119 | REQUIRE(ptr->properties() == (property[i])); 120 | BLELocalCharacteristic * local = ptr->local(); 121 | REQUIRE(local->permissions() == (permission[j] >> 8)); 122 | } 123 | } 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /extras/test/src/test_discovered_device/FakeGAP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "FakeGAP.h" 21 | 22 | FakeGAPClass::FakeGAPClass() 23 | { 24 | } 25 | 26 | FakeGAPClass::~FakeGAPClass() 27 | { 28 | } 29 | 30 | FakeGAPClass GAPFakeObj; 31 | GAPClass& GAP = GAPFakeObj; 32 | -------------------------------------------------------------------------------- /extras/test/src/test_discovered_device/test_discovered_device.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #define private public 23 | #define protected public 24 | #include "BLEDevice.h" 25 | 26 | TEST_CASE("BLE discovered device test", "[ArduinoBLE::BLEDevice]") 27 | { 28 | 29 | WHEN("Retrieve local name from advertisement packet") 30 | { 31 | // Mocking advertisement packet 32 | uint8_t advType = 0x03; 33 | uint8_t eirLength = 6; 34 | uint8_t eirData[] = {0x05, 0x09, 't', 'e', 's', 't'}; 35 | uint8_t rssi = 0; 36 | 37 | // Expected results 38 | String goldenName = "test"; 39 | 40 | // Simulate device discovery 41 | BLEDevice device = BLEDevice(); 42 | device.setAdvertisementData(0x03, eirLength, eirData, rssi); 43 | 44 | bool hasName = device.hasLocalName(); 45 | REQUIRE(hasName); 46 | 47 | String name = device.localName(); 48 | REQUIRE(goldenName == name); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /extras/test/src/test_main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #define CATCH_CONFIG_MAIN /* This tells Catch to provide a main() - only do this in one cpp file */ 21 | #include 22 | -------------------------------------------------------------------------------- /extras/test/src/test_uuid/test_uuid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #include "utility/BLEUuid.h" 23 | 24 | TEST_CASE("BLE uuid test", "[ArduinoBLE::BLEUuid]") 25 | { 26 | WHEN("Set and retrieve uuid") 27 | { 28 | bool verify; 29 | 30 | const char* goldenUuid = "19b10010-e8f2-537e-4f6c-d104768a1214"; 31 | // little-endian order 32 | const uint8_t goldenData[] = {0x14,0x12,0x8A,0x76,0x04,0xD1,0x6C,0x4F,0x7E,0x53,0xF2,0xE8,0x10,0x00,0xB1,0x19}; 33 | uint8_t goldenLength = 16; 34 | 35 | BLEUuid test(goldenUuid); 36 | 37 | uint8_t testLength = test.length(); 38 | verify = (goldenLength == testLength); 39 | REQUIRE(verify); 40 | 41 | const uint8_t *testData = test.data(); 42 | verify = ( 0 == (memcmp(goldenData, testData, sizeof(goldenData))) ); 43 | REQUIRE(verify); 44 | 45 | const char *testUuid = test.uuidToString(testData, testLength); 46 | verify = ( 0 == strcmp(testUuid, goldenUuid) ); 47 | REQUIRE(verify); 48 | 49 | // print the uuid 50 | //WARN("test: " << testUuid << ", golden: " << goldenUuid); 51 | } 52 | } -------------------------------------------------------------------------------- /extras/test/src/util/Common.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "Common.h" 21 | 22 | void delay(unsigned long) 23 | { 24 | 25 | } 26 | 27 | /* C++ prototypes */ 28 | long map(long x, long in_min, long in_max, long out_min, long out_max) 29 | { 30 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 31 | } 32 | 33 | uint16_t makeWord(uint16_t w) { return w; } 34 | uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; } -------------------------------------------------------------------------------- /extras/test/src/util/HCIFakeTransport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "HCIFakeTransport.h" 21 | 22 | HCIFakeTransportClass HCIFakeTransport; 23 | HCITransportInterface& HCITransport = HCIFakeTransport; -------------------------------------------------------------------------------- /extras/test/src/util/TestUtil.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | */ 4 | -------------------------------------------------------------------------------- /extras/test/src/util/itoa.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | extern char* itoa( int value, char *string, int radix ) 27 | { 28 | return ltoa( value, string, radix ) ; 29 | } 30 | 31 | extern char* ltoa( long value, char *string, int radix ) 32 | { 33 | char tmp[33]; 34 | char *tp = tmp; 35 | long i; 36 | unsigned long v; 37 | int sign; 38 | char *sp; 39 | 40 | if ( string == NULL ) 41 | { 42 | return 0 ; 43 | } 44 | 45 | if (radix > 36 || radix <= 1) 46 | { 47 | return 0 ; 48 | } 49 | 50 | sign = (radix == 10 && value < 0); 51 | if (sign) 52 | { 53 | v = -value; 54 | } 55 | else 56 | { 57 | v = (unsigned long)value; 58 | } 59 | 60 | while (v || tp == tmp) 61 | { 62 | i = v % radix; 63 | v = v / radix; 64 | if (i < 10) 65 | *tp++ = i+'0'; 66 | else 67 | *tp++ = i + 'a' - 10; 68 | } 69 | 70 | sp = string; 71 | 72 | if (sign) 73 | *sp++ = '-'; 74 | while (tp > tmp) 75 | *sp++ = *--tp; 76 | *sp = 0; 77 | 78 | return string; 79 | } 80 | 81 | extern char* utoa( unsigned int value, char *string, int radix ) 82 | { 83 | return ultoa( value, string, radix ) ; 84 | } 85 | 86 | extern char* ultoa( unsigned long value, char *string, int radix ) 87 | { 88 | char tmp[33]; 89 | char *tp = tmp; 90 | long i; 91 | unsigned long v = value; 92 | char *sp; 93 | 94 | if ( string == NULL ) 95 | { 96 | return 0; 97 | } 98 | 99 | if (radix > 36 || radix <= 1) 100 | { 101 | return 0; 102 | } 103 | 104 | while (v || tp == tmp) 105 | { 106 | i = v % radix; 107 | v = v / radix; 108 | if (i < 10) 109 | *tp++ = i+'0'; 110 | else 111 | *tp++ = i + 'a' - 10; 112 | } 113 | 114 | sp = string; 115 | 116 | 117 | while (tp > tmp) 118 | *sp++ = *--tp; 119 | *sp = 0; 120 | 121 | return string; 122 | } 123 | 124 | #ifdef __cplusplus 125 | } // extern "C" 126 | #endif 127 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For ArduinoBLE 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ArduinoBLE KEYWORD1 10 | BLE KEYWORD1 11 | 12 | BLEDevice KEYWORD1 13 | BLECharacteristic KEYWORD1 14 | BLEDescriptor KEYWORD1 15 | BLEService KEYWORD1 16 | 17 | BLEBoolCharacteristic KEYWORD1 18 | BLEBooleanCharacteristic KEYWORD1 19 | BLECharCharacteristic KEYWORD1 20 | BLEUnsignedCharCharacteristic KEYWORD1 21 | BLEByteCharacteristic KEYWORD1 22 | BLEShortCharacteristic KEYWORD1 23 | BLEUnsignedShortCharacteristic KEYWORD1 24 | BLEWordCharacteristic KEYWORD1 25 | BLEIntCharacteristic KEYWORD1 26 | BLEUnsignedIntCharacteristic KEYWORD1 27 | BLELongCharacteristic KEYWORD1 28 | BLEUnsignedLongCharacteristic KEYWORD1 29 | BLEFloatCharacteristic KEYWORD1 30 | BLEDoubleCharacteristic KEYWORD1 31 | BLEStringCharacteristic KEYWORD1 32 | 33 | ####################################### 34 | # Methods and Functions (KEYWORD2) 35 | ####################################### 36 | 37 | begin KEYWORD2 38 | poll KEYWORD2 39 | end KEYWORD2 40 | 41 | connected KEYWORD2 42 | disconnect KEYWORD2 43 | address KEYWORD2 44 | hasLocalName KEYWORD2 45 | hasAdvertisedServiceUuid KEYWORD2 46 | advertisedServiceUuidCount KEYWORD2 47 | localName KEYWORD2 48 | advertisedServiceUuid KEYWORD2 49 | rssi KEYWORD2 50 | connect KEYWORD2 51 | discoverAttributes KEYWORD2 52 | discoverService KEYWORD2 53 | deviceName KEYWORD2 54 | appearance KEYWORD2 55 | serviceCount KEYWORD2 56 | hasService KEYWORD2 57 | service KEYWORD2 58 | characteristicCount KEYWORD2 59 | characteristic KEYWORD2 60 | 61 | setAdvertisedServiceUuid KEYWORD2 62 | setManufacturerData KEYWORD2 63 | setLocalName KEYWORD2 64 | setDeviceName KEYWORD2 65 | setAppearance KEYWORD2 66 | addService KEYWORD2 67 | advertise KEYWORD2 68 | stopAdvertise KEYWORD2 69 | scan KEYWORD2 70 | scanForName KEYWORD2 71 | scanForUuid KEYWORD2 72 | scanForAddress KEYWORD2 73 | stopScan KEYWORD2 74 | central KEYWORD2 75 | available KEYWORD2 76 | setEventHandler KEYWORD2 77 | setAdvertisingInterval KEYWORD2 78 | setConnectionInterval KEYWORD2 79 | setConnectable KEYWORD2 80 | setPairable KEYWORD2 81 | setTimeout KEYWORD2 82 | debug KEYWORD2 83 | noDebug KEYWORD2 84 | pairable KEYWORD2 85 | paired KEYWORD2 86 | 87 | properties KEYWORD2 88 | valueSize KEYWORD2 89 | value KEYWORD2 90 | valueLength KEYWORD2 91 | readValue KEYWORD2 92 | writeValue KEYWORD2 93 | setValue KEYWORD2 94 | broadcast KEYWORD2 95 | written KEYWORD2 96 | subscribed KEYWORD2 97 | valueUpdated KEYWORD2 98 | addDescriptor KEYWORD2 99 | descriptorCount KEYWORD2 100 | hasDescriptor KEYWORD2 101 | descriptor KEYWORD2 102 | canRead KEYWORD2 103 | read KEYWORD2 104 | canWrite KEYWORD2 105 | canSubscribe KEYWORD2 106 | subscribe KEYWORD2 107 | canUnsubscribe KEYWORD2 108 | unsubscribe KEYWORD2 109 | writeValueLE KEYWORD2 110 | setValueLE KEYWORD2 111 | valueLE KEYWORD2 112 | writeValueBE KEYWORD2 113 | setValueBE KEYWORD2 114 | valueBE KEYWORD2 115 | 116 | uuid KEYWORD2 117 | addCharacteristic KEYWORD2 118 | 119 | ####################################### 120 | # Constants (LITERAL1) 121 | ####################################### 122 | 123 | BLEConnected LITERAL1 124 | BLEDisconnected LITERAL1 125 | BLEDiscovered LITERAL1 126 | 127 | BLEBroadcast LITERAL1 128 | BLERead LITERAL1 129 | BLEWriteWithoutResponse LITERAL1 130 | BLEWrite LITERAL1 131 | BLENotify LITERAL1 132 | BLEIndicate LITERAL1 133 | 134 | BLESubscribed LITERAL1 135 | BLEUnsubscribed LITERAL1 136 | BLEWritten LITERAL1 137 | BLEUpdated LITERAL1 138 | 139 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ArduinoBLE 2 | version=1.4.0 3 | author=Arduino 4 | maintainer=Arduino 5 | sentence=Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev2, Arduino Nano 33 IoT, Arduino Nano 33 BLE, Nicla Sense ME and UNO R4 WiFi. 6 | paragraph=This library supports creating a Bluetooth® Low Energy peripheral & central mode. 7 | category=Communication 8 | url=https://www.arduino.cc/en/Reference/ArduinoBLE 9 | architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32,mbed_giga,renesas,renesas_portenta,mbed_opta,renesas_uno,silabs 10 | includes=ArduinoBLE.h 11 | -------------------------------------------------------------------------------- /src/ArduinoBLE.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _ARDUINO_BLE_H_ 21 | #define _ARDUINO_BLE_H_ 22 | 23 | #include "local/BLELocalDevice.h" 24 | #include "BLEProperty.h" 25 | #include "BLEStringCharacteristic.h" 26 | #include "BLETypedCharacteristics.h" 27 | #include "utility/btct.h" 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/BLEAdvertisingData.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_ADVERTISING_DATA_H_ 21 | #define _BLE_ADVERTISING_DATA_H_ 22 | 23 | #include 24 | #include "utility/BLEUuid.h" 25 | #include "BLEService.h" 26 | 27 | #define MAX_AD_DATA_LENGTH (31) 28 | 29 | enum BLEFlags { 30 | BLEFlagsLimitedDiscoverable = 0x01, 31 | BLEFlagsGeneralDiscoverable = 0x02, 32 | BLEFlagsBREDRNotSupported = 0x04 33 | }; 34 | 35 | enum BLEAdField { 36 | BLEFieldFlags = 0x01, 37 | BLEFieldIncompleteAdvertisedService16 = 0x02, 38 | BLEFieldCompleteAdvertisedService16 = 0x03, 39 | BLEFieldIncompleteAdvertisedService128 = 0x06, 40 | BLEFieldCompleteAdvertisedService128 = 0x07, 41 | BLEFieldShortLocalName = 0x08, 42 | BLEFieldCompleteLocalName = 0x09, 43 | BLEFieldServiceData = 0x16, 44 | BLEFieldManufacturerData = 0xFF, 45 | 46 | BLEAdFieldLast 47 | }; 48 | 49 | struct BLEAdvertisingRawData { 50 | uint8_t data[MAX_AD_DATA_LENGTH]; 51 | int length; 52 | }; 53 | 54 | class BLEAdvertisingData { 55 | public: 56 | BLEAdvertisingData(); 57 | virtual ~BLEAdvertisingData(); 58 | 59 | int availableForWrite(); 60 | void clear(); 61 | void copy(const BLEAdvertisingData& adv); 62 | BLEAdvertisingData& operator=(const BLEAdvertisingData &other); 63 | 64 | bool setAdvertisedService(const BLEService& service); 65 | bool setAdvertisedServiceUuid(const char* advertisedServiceUuid); 66 | bool setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength); 67 | bool setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength); 68 | bool setLocalName(const char *localName); 69 | bool setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length); 70 | bool setRawData(const uint8_t* data, int length); 71 | bool setRawData(const BLEAdvertisingRawData& data); 72 | bool setFlags(uint8_t flags); 73 | 74 | protected: 75 | friend class BLELocalDevice; 76 | bool updateData(); 77 | uint8_t* data(); 78 | int dataLength() const; 79 | int remainingLength() const; 80 | bool hasFlags() const; 81 | 82 | private: 83 | bool updateRemainingLength(int oldFieldLength, int newFieldLength); 84 | 85 | bool addAdvertisedServiceUuid(const char* advertisedServiceUuid); 86 | bool addManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength); 87 | bool addManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength); 88 | bool addLocalName(const char *localName); 89 | bool addAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length); 90 | bool addRawData(const uint8_t* data, int length); 91 | bool addFlags(uint8_t flags); 92 | 93 | bool addField(BLEAdField field, const char* data); 94 | bool addField(BLEAdField field, const uint8_t* data, int length); 95 | 96 | uint8_t _data[MAX_AD_DATA_LENGTH]; 97 | int _dataLength; 98 | 99 | int _remainingLength; 100 | 101 | const uint8_t* _rawData; 102 | int _rawDataLength; 103 | 104 | uint8_t _flags; 105 | bool _hasFlags; 106 | const char* _localName; 107 | 108 | const uint8_t* _manufacturerData; 109 | int _manufacturerDataLength; 110 | uint16_t _manufacturerCompanyId; 111 | bool _hasManufacturerCompanyId; 112 | 113 | const char* _advertisedServiceUuid; 114 | int _advertisedServiceUuidLength; 115 | uint16_t _serviceDataUuid; 116 | const uint8_t* _serviceData; 117 | int _serviceDataLength; 118 | }; 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /src/BLECharacteristic.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_CHARACTERISTIC_H_ 21 | #define _BLE_CHARACTERISTIC_H_ 22 | 23 | #include 24 | 25 | #include "BLEDescriptor.h" 26 | 27 | enum BLECharacteristicEvent { 28 | BLESubscribed = 0, 29 | BLEUnsubscribed = 1, 30 | //BLERead = 2, // defined in BLEProperties.h 31 | BLEWritten = 3, 32 | BLEUpdated = BLEWritten, // alias 33 | 34 | BLECharacteristicEventLast 35 | }; 36 | 37 | class BLECharacteristic; 38 | class BLEDevice; 39 | 40 | typedef void (*BLECharacteristicEventHandler)(BLEDevice device, BLECharacteristic characteristic); 41 | 42 | class BLELocalCharacteristic; 43 | class BLERemoteCharacteristic; 44 | 45 | class BLECharacteristic { 46 | public: 47 | BLECharacteristic(); 48 | BLECharacteristic(const char* uuid, uint16_t permissions, int valueSize, bool fixedLength = false); 49 | BLECharacteristic(const char* uuid, uint16_t permissions, const char* value); 50 | BLECharacteristic(const BLECharacteristic& other); 51 | virtual ~BLECharacteristic(); 52 | 53 | const char* uuid() const; 54 | 55 | uint8_t properties() const; 56 | 57 | int valueSize() const; 58 | const uint8_t* value() const; 59 | int valueLength() const; 60 | uint8_t operator[] (int offset) const; 61 | 62 | int readValue(uint8_t value[], int length); 63 | int readValue(void* value, int length); 64 | int readValue(uint8_t& value); 65 | int readValue(int8_t& value); 66 | int readValue(uint16_t& value); 67 | int readValue(int16_t& value); 68 | int readValue(uint32_t& value); 69 | int readValue(int32_t& value); 70 | 71 | int writeValue(const uint8_t value[], int length, bool withResponse = true); 72 | int writeValue(const void* value, int length, bool withResponse = true); 73 | int writeValue(const char* value, bool withResponse = true); 74 | int writeValue(uint8_t value, bool withResponse = true); 75 | int writeValue(int8_t value, bool withResponse = true); 76 | int writeValue(uint16_t value, bool withResponse = true); 77 | int writeValue(int16_t value, bool withResponse = true); 78 | int writeValue(uint32_t value, bool withResponse = true); 79 | int writeValue(int32_t value, bool withResponse = true); 80 | 81 | // deprecated, use writeValue(...) 82 | int setValue(const uint8_t value[], int length) { return writeValue(value, length); } 83 | int setValue(const char* value) { return writeValue(value); } 84 | 85 | int broadcast(); 86 | 87 | bool written(); 88 | bool subscribed(); 89 | bool valueUpdated(); 90 | 91 | void addDescriptor(BLEDescriptor& descriptor); 92 | 93 | operator bool() const; 94 | 95 | void setEventHandler(int event, BLECharacteristicEventHandler eventHandler); 96 | 97 | int descriptorCount() const; 98 | bool hasDescriptor(const char* uuid) const; 99 | bool hasDescriptor(const char* uuid, int index) const; 100 | BLEDescriptor descriptor(int index) const; 101 | BLEDescriptor descriptor(const char * uuid) const; 102 | BLEDescriptor descriptor(const char * uuid, int index) const; 103 | 104 | bool canRead(); 105 | bool read(); 106 | bool canWrite(); 107 | bool canSubscribe(); 108 | bool subscribe(); 109 | bool canUnsubscribe(); 110 | bool unsubscribe(); 111 | 112 | protected: 113 | friend class BLELocalCharacteristic; 114 | friend class BLELocalService; 115 | 116 | BLECharacteristic(BLELocalCharacteristic* local); 117 | 118 | BLELocalCharacteristic* local(); 119 | 120 | protected: 121 | friend class BLEDevice; 122 | friend class BLEService; 123 | friend class BLERemoteCharacteristic; 124 | 125 | BLECharacteristic(BLERemoteCharacteristic* remote); 126 | 127 | private: 128 | BLELocalCharacteristic* _local; 129 | BLERemoteCharacteristic* _remote; 130 | }; 131 | 132 | #endif 133 | -------------------------------------------------------------------------------- /src/BLEDescriptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_DESCRIPTOR_H_ 21 | #define _BLE_DESCRIPTOR_H_ 22 | 23 | #include 24 | 25 | class BLELocalDescriptor; 26 | class BLERemoteDescriptor; 27 | 28 | class BLEDescriptor { 29 | public: 30 | BLEDescriptor(); 31 | BLEDescriptor(const BLEDescriptor& other); 32 | BLEDescriptor(const char* uuid, const uint8_t value[], int valueSize); 33 | BLEDescriptor(const char* uuid, const char* value); 34 | virtual ~BLEDescriptor(); 35 | 36 | const char* uuid() const; 37 | 38 | int valueSize() const; 39 | const uint8_t* value() const; 40 | int valueLength() const; 41 | uint8_t operator[] (int offset) const; 42 | 43 | int readValue(uint8_t value[], int length); 44 | int readValue(void* value, int length); 45 | int readValue(uint8_t& value); 46 | int readValue(int8_t& value); 47 | int readValue(uint16_t& value); 48 | int readValue(int16_t& value); 49 | int readValue(uint32_t& value); 50 | int readValue(int32_t& value); 51 | 52 | operator bool() const; 53 | 54 | bool read(); 55 | 56 | protected: 57 | friend class BLELocalCharacteristic; 58 | 59 | BLEDescriptor(BLELocalDescriptor* local); 60 | 61 | BLELocalDescriptor* local(); 62 | 63 | protected: 64 | friend class BLECharacteristic; 65 | 66 | BLEDescriptor(BLERemoteDescriptor* remote); 67 | 68 | private: 69 | BLELocalDescriptor* _local; 70 | BLERemoteDescriptor* _remote; 71 | }; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/BLEDevice.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_DEVICE_H_ 21 | #define _BLE_DEVICE_H_ 22 | 23 | #include 24 | 25 | #include "BLEService.h" 26 | 27 | enum BLEDeviceEvent { 28 | BLEConnected = 0, 29 | BLEDisconnected = 1, 30 | BLEDiscovered = 2, 31 | 32 | BLEDeviceLastEvent 33 | }; 34 | 35 | class BLEDevice; 36 | 37 | typedef void (*BLEDeviceEventHandler)(BLEDevice device); 38 | 39 | class BLEDevice { 40 | public: 41 | BLEDevice(); 42 | virtual ~BLEDevice(); 43 | 44 | virtual void poll(); 45 | virtual void poll(unsigned long timeout); 46 | 47 | virtual bool connected() const; 48 | virtual bool disconnect(); 49 | 50 | virtual String address() const; 51 | 52 | bool hasLocalName() const; 53 | 54 | bool hasAdvertisedServiceUuid() const; 55 | bool hasAdvertisedServiceUuid(int index) const; 56 | int advertisedServiceUuidCount() const; 57 | 58 | String localName() const; 59 | String advertisedServiceUuid() const; 60 | String advertisedServiceUuid(int index) const; 61 | 62 | bool hasAdvertisementData() const; 63 | int advertisementDataLength() const; 64 | int advertisementData(uint8_t value[], int length) const; 65 | 66 | bool hasManufacturerData() const; 67 | int manufacturerDataLength() const; 68 | int manufacturerData(uint8_t value[], int length) const; 69 | 70 | virtual int rssi(); 71 | 72 | bool connect(); 73 | bool discoverAttributes(); 74 | bool discoverService(const char* serviceUuid); 75 | 76 | virtual operator bool() const; 77 | virtual bool operator==(const BLEDevice& rhs) const; 78 | virtual bool operator!=(const BLEDevice& rhs) const; 79 | 80 | String deviceName(); 81 | int appearance(); 82 | 83 | int serviceCount() const; 84 | bool hasService(const char* uuid) const; 85 | bool hasService(const char* uuid, int index) const; 86 | BLEService service(int index) const; 87 | BLEService service(const char * uuid) const; 88 | BLEService service(const char * uuid, int index) const; 89 | int characteristicCount() const; 90 | bool hasCharacteristic(const char* uuid) const; 91 | bool hasCharacteristic(const char* uuid, int index) const; 92 | BLECharacteristic characteristic(int index) const; 93 | BLECharacteristic characteristic(const char * uuid) const; 94 | BLECharacteristic characteristic(const char * uuid, int index) const; 95 | 96 | protected: 97 | friend class ATTClass; 98 | friend class GAPClass; 99 | 100 | BLEDevice(uint8_t addressType, uint8_t address[6]); 101 | 102 | protected: 103 | friend class GAPClass; 104 | 105 | bool hasAddress(uint8_t addressType, uint8_t address[6]); 106 | 107 | void setAdvertisementData(uint8_t type, uint8_t eirDataLength, uint8_t eirData[], int8_t rssi); 108 | void setScanResponseData(uint8_t eirDataLength, uint8_t eirData[], int8_t rssi); 109 | 110 | bool discovered(); 111 | 112 | private: 113 | uint8_t _addressType; 114 | uint8_t _address[6]; 115 | uint8_t _advertisementTypeMask; 116 | uint8_t _eirDataLength; 117 | uint8_t _eirData[31 * 2]; 118 | int8_t _rssi; 119 | }; 120 | 121 | #endif 122 | -------------------------------------------------------------------------------- /src/BLEProperty.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | // #include 21 | 22 | #ifndef _BLE_PROPERTY_H_ 23 | #define _BLE_PROPERTY_H_ 24 | 25 | enum BLEProperty { 26 | BLEBroadcast = 0x01, 27 | BLERead = 0x02, 28 | BLEWriteWithoutResponse = 0x04, 29 | BLEWrite = 0x08, 30 | BLENotify = 0x10, 31 | BLEIndicate = 0x20, 32 | BLEAuthSignedWrite = 1 << 6, 33 | BLEExtProp = 1 << 7, 34 | }; 35 | 36 | enum BLEPermission { 37 | BLEEncryption = 1 << 9, 38 | BLEAuthentication = 1 << 10, 39 | BLEAuthorization = 1 << 11, 40 | // BLEWriteEncryption = 1 << 11, 41 | // BLEWriteAuthentication = 1 << 12, 42 | // BLEWriteAuthorization = 1 << 13, 43 | }; 44 | 45 | #define ESP_GATT_CHAR_PROP_BIT_BROADCAST (1 << 0) /* 0x01 */ /* relate to BTA_GATT_CHAR_PROP_BIT_BROADCAST in bta/bta_gatt_api.h */ 46 | #define ESP_GATT_CHAR_PROP_BIT_READ (1 << 1) /* 0x02 */ /* relate to BTA_GATT_CHAR_PROP_BIT_READ in bta/bta_gatt_api.h */ 47 | #define ESP_GATT_CHAR_PROP_BIT_WRITE_NR (1 << 2) /* 0x04 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE_NR in bta/bta_gatt_api.h */ 48 | #define ESP_GATT_CHAR_PROP_BIT_WRITE (1 << 3) /* 0x08 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE in bta/bta_gatt_api.h */ 49 | #define ESP_GATT_CHAR_PROP_BIT_NOTIFY (1 << 4) /* 0x10 */ /* relate to BTA_GATT_CHAR_PROP_BIT_NOTIFY in bta/bta_gatt_api.h */ 50 | #define ESP_GATT_CHAR_PROP_BIT_INDICATE (1 << 5) /* 0x20 */ /* relate to BTA_GATT_CHAR_PROP_BIT_INDICATE in bta/bta_gatt_api.h */ 51 | #define ESP_GATT_CHAR_PROP_BIT_AUTH (1 << 6) /* 0x40 */ /* relate to BTA_GATT_CHAR_PROP_BIT_AUTH in bta/bta_gatt_api.h */ 52 | #define ESP_GATT_CHAR_PROP_BIT_EXT_PROP (1 << 7) /* 0x80 */ /* relate to BTA_GATT_CHAR_PROP_BIT_EXT_PROP in bta/bta_gatt_api.h */ 53 | 54 | #define ESP_GATT_PERM_READ (1 << 0) /* bit 0 - 0x0001 */ /* relate to BTA_GATT_PERM_READ in bta/bta_gatt_api.h */ 55 | #define ESP_GATT_PERM_READ_ENCRYPTED (1 << 1) /* bit 1 - 0x0002 */ /* relate to BTA_GATT_PERM_READ_ENCRYPTED in bta/bta_gatt_api.h */ 56 | #define ESP_GATT_PERM_READ_ENC_MITM (1 << 2) /* bit 2 - 0x0004 */ /* relate to BTA_GATT_PERM_READ_ENC_MITM in bta/bta_gatt_api.h */ 57 | #define ESP_GATT_PERM_WRITE (1 << 4) /* bit 4 - 0x0010 */ /* relate to BTA_GATT_PERM_WRITE in bta/bta_gatt_api.h */ 58 | #define ESP_GATT_PERM_WRITE_ENCRYPTED (1 << 5) /* bit 5 - 0x0020 */ /* relate to BTA_GATT_PERM_WRITE_ENCRYPTED in bta/bta_gatt_api.h */ 59 | #define ESP_GATT_PERM_WRITE_ENC_MITM (1 << 6) /* bit 6 - 0x0040 */ /* relate to BTA_GATT_PERM_WRITE_ENC_MITM in bta/bta_gatt_api.h */ 60 | #define ESP_GATT_PERM_WRITE_SIGNED (1 << 7) /* bit 7 - 0x0080 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED in bta/bta_gatt_api.h */ 61 | #define ESP_GATT_PERM_WRITE_SIGNED_MITM (1 << 8) /* bit 8 - 0x0100 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED_MITM in bta/bta_gatt_api.h */ 62 | #define ESP_GATT_PERM_READ_AUTHORIZATION (1 << 9) /* bit 9 - 0x0200 */ 63 | #define ESP_GATT_PERM_WRITE_AUTHORIZATION (1 << 10) /* bit 10 - 0x0400 */ 64 | 65 | enum BLE_GATT_PERM_ { 66 | BLE_GATT_READ = 1 << 0, 67 | READ_ENCRYPTED = 1 << 1, 68 | READ_ENC_MITM = 1 << 2, 69 | BLE_GATT_WRITE = 1 << 4, 70 | WRITE_ENCRYPTED = 1 << 5, 71 | WRITE_ENC_MITM = 1 << 6, 72 | WRITE_SIGNED = 1 << 7, 73 | WRITE_SIGNED_MITM = 1 << 8, 74 | READ_AUTHORIZATION = 1 << 9, 75 | WRITE_AUTHORIZATION = 1 << 10, 76 | }; 77 | 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/BLEService.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "local/BLELocalService.h" 21 | #include "remote/BLERemoteService.h" 22 | 23 | #include "BLEService.h" 24 | 25 | extern "C" int strcasecmp(char const *a, char const *b); 26 | 27 | BLEService::BLEService() : 28 | BLEService((BLELocalService*)NULL) 29 | { 30 | } 31 | 32 | BLEService::BLEService(BLELocalService* local) : 33 | _local(local), 34 | _remote(NULL) 35 | { 36 | if (_local) { 37 | _local->retain(); 38 | } 39 | } 40 | 41 | BLEService::BLEService(BLERemoteService* remote) : 42 | _local(NULL), 43 | _remote(remote) 44 | { 45 | if (_remote) { 46 | _remote->retain(); 47 | } 48 | } 49 | 50 | BLEService::BLEService(const char* uuid) : 51 | BLEService(new BLELocalService(uuid)) 52 | { 53 | } 54 | 55 | BLEService::BLEService(const BLEService& other) 56 | { 57 | _local = other._local; 58 | if (_local) { 59 | _local->retain(); 60 | } 61 | 62 | _remote = other._remote; 63 | if (_remote) { 64 | _remote->retain(); 65 | } 66 | } 67 | 68 | void BLEService::clear() 69 | { 70 | if (_local) { 71 | _local->clear(); 72 | } 73 | } 74 | 75 | BLEService::~BLEService() 76 | { 77 | if (_local && _local->release() == 0) { 78 | delete _local; 79 | } 80 | 81 | if (_remote && _remote->release() == 0) { 82 | delete _remote; 83 | } 84 | } 85 | 86 | const char* BLEService::uuid() const 87 | { 88 | if (_local) { 89 | return _local->uuid(); 90 | } 91 | 92 | if (_remote) { 93 | return _remote->uuid(); 94 | } 95 | 96 | return ""; 97 | } 98 | 99 | void BLEService::addCharacteristic(BLECharacteristic& characteristic) 100 | { 101 | if (_local) { 102 | _local->addCharacteristic(characteristic); 103 | } 104 | } 105 | 106 | BLEService::operator bool() const 107 | { 108 | return (_local != NULL) || (_remote != NULL); 109 | } 110 | 111 | int BLEService::characteristicCount() const 112 | { 113 | if (_remote) { 114 | return _remote->characteristicCount(); 115 | } 116 | 117 | return 0; 118 | } 119 | 120 | bool BLEService::hasCharacteristic(const char* uuid) const 121 | { 122 | return hasCharacteristic(uuid, 0); 123 | } 124 | 125 | bool BLEService::hasCharacteristic(const char* uuid, int index) const 126 | { 127 | if (_remote) { 128 | int count = 0; 129 | int numCharacteristics = _remote->characteristicCount(); 130 | 131 | for (int i = 0; i < numCharacteristics; i++) { 132 | BLERemoteCharacteristic* c = _remote->characteristic(i); 133 | 134 | if (strcasecmp(uuid, c->uuid()) == 0) { 135 | if (count == index) { 136 | return true; 137 | } 138 | 139 | count++; 140 | } 141 | } 142 | } 143 | 144 | return false; 145 | } 146 | 147 | BLECharacteristic BLEService::characteristic(int index) const 148 | { 149 | if (_remote) { 150 | return BLECharacteristic(_remote->characteristic(index)); 151 | } 152 | 153 | return BLECharacteristic(); 154 | } 155 | 156 | BLECharacteristic BLEService::characteristic(const char * uuid) const 157 | { 158 | return characteristic(uuid, 0); 159 | } 160 | 161 | BLECharacteristic BLEService::characteristic(const char * uuid, int index) const 162 | { 163 | if (_remote) { 164 | int count = 0; 165 | int numCharacteristics = _remote->characteristicCount(); 166 | 167 | for (int i = 0; i < numCharacteristics; i++) { 168 | BLERemoteCharacteristic* c = _remote->characteristic(i); 169 | 170 | if (strcasecmp(uuid, c->uuid()) == 0) { 171 | if (count == index) { 172 | return BLECharacteristic(c); 173 | } 174 | 175 | count++; 176 | } 177 | } 178 | } 179 | 180 | return BLECharacteristic(); 181 | } 182 | 183 | BLELocalService* BLEService::local() 184 | { 185 | return _local; 186 | } 187 | -------------------------------------------------------------------------------- /src/BLEService.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_SERVICE_H_ 21 | #define _BLE_SERVICE_H_ 22 | 23 | #include "BLECharacteristic.h" 24 | 25 | class BLELocalService; 26 | class BLERemoteService; 27 | 28 | class BLEService { 29 | public: 30 | BLEService(); 31 | BLEService(const char* uuid); 32 | BLEService(const BLEService& other); 33 | virtual ~BLEService(); 34 | 35 | const char* uuid() const; 36 | void clear(); 37 | 38 | void addCharacteristic(BLECharacteristic& characteristic); 39 | 40 | operator bool() const; 41 | 42 | int characteristicCount() const; 43 | bool hasCharacteristic(const char* uuid) const; 44 | bool hasCharacteristic(const char* uuid, int index) const; 45 | BLECharacteristic characteristic(int index) const; 46 | BLECharacteristic characteristic(const char * uuid) const; 47 | BLECharacteristic characteristic(const char * uuid, int index) const; 48 | 49 | protected: 50 | friend class GATTClass; 51 | 52 | BLEService(BLELocalService* local); 53 | 54 | BLELocalService* local(); 55 | 56 | void addCharacteristic(BLELocalCharacteristic* characteristic); 57 | 58 | protected: 59 | friend class BLEDevice; 60 | 61 | BLEService(BLERemoteService* remote); 62 | 63 | private: 64 | BLELocalService* _local; 65 | BLERemoteService* _remote; 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /src/BLEStringCharacteristic.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "BLEStringCharacteristic.h" 21 | 22 | BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize) : 23 | BLECharacteristic(uuid, properties, valueSize) 24 | { 25 | } 26 | 27 | int BLEStringCharacteristic::writeValue(const String& value) 28 | { 29 | return BLECharacteristic::writeValue(value.c_str()); 30 | } 31 | 32 | String BLEStringCharacteristic::value(void) 33 | { 34 | String str; 35 | int length = BLECharacteristic::valueLength(); 36 | const uint8_t* val = BLECharacteristic::value(); 37 | 38 | str.reserve(length); 39 | 40 | for (int i = 0; i < length; i++) { 41 | str += (char)val[i]; 42 | } 43 | 44 | return str; 45 | } 46 | -------------------------------------------------------------------------------- /src/BLEStringCharacteristic.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_STRING_CHARACTERISTIC_H_ 21 | #define _BLE_STRING_CHARACTERISTIC_H_ 22 | 23 | #include 24 | 25 | #include "BLECharacteristic.h" 26 | 27 | class BLEStringCharacteristic : public BLECharacteristic 28 | { 29 | public: 30 | BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize); 31 | 32 | int writeValue(const String& value); 33 | int setValue(const String& value) { return writeValue(value); } 34 | String value(void); 35 | 36 | private: 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/BLETypedCharacteristic.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_TYPED_CHARACTERISTIC_H_ 21 | #define _BLE_TYPED_CHARACTERISTIC_H_ 22 | 23 | #include "BLECharacteristic.h" 24 | 25 | template class BLETypedCharacteristic : public BLECharacteristic 26 | { 27 | public: 28 | BLETypedCharacteristic(const char* uuid, unsigned int permissions); 29 | 30 | int writeValue(T value); 31 | int setValue(T value) { return writeValue(value); } 32 | T value(void); 33 | 34 | int writeValueLE(T value); 35 | int setValueLE(T value) { return writeValueLE(value); } 36 | T valueLE(void); 37 | 38 | int writeValueBE(T value); 39 | int setValueBE(T value) { return writeValueBE(value); } 40 | T valueBE(void); 41 | 42 | private: 43 | T byteSwap(T value); 44 | }; 45 | 46 | template BLETypedCharacteristic::BLETypedCharacteristic(const char* uuid, unsigned int permissions) : 47 | BLECharacteristic(uuid, permissions, sizeof(T), true) 48 | { 49 | T value; 50 | memset(&value, 0x00, sizeof(value)); 51 | 52 | writeValue(value); 53 | } 54 | 55 | template int BLETypedCharacteristic::writeValue(T value) 56 | { 57 | return BLECharacteristic::writeValue((uint8_t*)&value, sizeof(T)); 58 | } 59 | 60 | template T BLETypedCharacteristic::value() 61 | { 62 | T value; 63 | 64 | memcpy(&value, (unsigned char*)BLECharacteristic::value(), BLECharacteristic::valueSize()); 65 | 66 | return value; 67 | } 68 | 69 | template int BLETypedCharacteristic::writeValueLE(T value) 70 | { 71 | return writeValue(value); 72 | } 73 | 74 | template T BLETypedCharacteristic::valueLE() 75 | { 76 | return value(); 77 | } 78 | 79 | template int BLETypedCharacteristic::writeValueBE(T value) 80 | { 81 | return writeValue(byteSwap(value)); 82 | } 83 | 84 | template T BLETypedCharacteristic::valueBE() 85 | { 86 | return byteSwap(value()); 87 | } 88 | 89 | template T BLETypedCharacteristic::byteSwap(T value) 90 | { 91 | T result; 92 | unsigned char* src = (unsigned char*)&value; 93 | unsigned char* dst = (unsigned char*)&result; 94 | 95 | for (int i = 0; i < sizeof(T); i++) { 96 | dst[i] = src[sizeof(T) - i - 1]; 97 | } 98 | 99 | return result; 100 | } 101 | 102 | #endif 103 | -------------------------------------------------------------------------------- /src/BLETypedCharacteristics.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #include "BLETypedCharacteristics.h" 23 | 24 | BLEBoolCharacteristic::BLEBoolCharacteristic(const char* uuid, unsigned int properties) : 25 | BLETypedCharacteristic(uuid, properties) 26 | { 27 | } 28 | 29 | BLEBooleanCharacteristic::BLEBooleanCharacteristic(const char* uuid, unsigned int properties) : 30 | BLETypedCharacteristic(uuid, properties) 31 | { 32 | } 33 | 34 | BLECharCharacteristic::BLECharCharacteristic(const char* uuid, unsigned int properties) : 35 | BLETypedCharacteristic(uuid, properties) 36 | { 37 | } 38 | 39 | BLEUnsignedCharCharacteristic::BLEUnsignedCharCharacteristic(const char* uuid, unsigned int properties) : 40 | BLETypedCharacteristic(uuid, properties) 41 | { 42 | } 43 | 44 | BLEByteCharacteristic::BLEByteCharacteristic(const char* uuid, unsigned int properties) : 45 | BLETypedCharacteristic(uuid, properties) 46 | { 47 | } 48 | 49 | BLEShortCharacteristic::BLEShortCharacteristic(const char* uuid, unsigned int properties) : 50 | BLETypedCharacteristic(uuid, properties) 51 | { 52 | } 53 | 54 | BLEUnsignedShortCharacteristic::BLEUnsignedShortCharacteristic(const char* uuid, unsigned int properties) : 55 | BLETypedCharacteristic(uuid, properties) 56 | { 57 | } 58 | 59 | BLEWordCharacteristic::BLEWordCharacteristic(const char* uuid, unsigned int properties) : 60 | BLETypedCharacteristic(uuid, properties) 61 | { 62 | } 63 | 64 | BLEIntCharacteristic::BLEIntCharacteristic(const char* uuid, unsigned int properties) : 65 | BLETypedCharacteristic(uuid, properties) 66 | { 67 | } 68 | 69 | BLEUnsignedIntCharacteristic::BLEUnsignedIntCharacteristic(const char* uuid, unsigned int properties) : 70 | BLETypedCharacteristic(uuid, properties) 71 | { 72 | } 73 | 74 | BLELongCharacteristic::BLELongCharacteristic(const char* uuid, unsigned int properties) : 75 | BLETypedCharacteristic(uuid, properties) 76 | { 77 | } 78 | 79 | BLEUnsignedLongCharacteristic::BLEUnsignedLongCharacteristic(const char* uuid, unsigned int properties) : 80 | BLETypedCharacteristic(uuid, properties) 81 | { 82 | } 83 | 84 | BLEFloatCharacteristic::BLEFloatCharacteristic(const char* uuid, unsigned int properties) : 85 | BLETypedCharacteristic(uuid, properties) 86 | { 87 | } 88 | 89 | BLEDoubleCharacteristic::BLEDoubleCharacteristic(const char* uuid, unsigned int properties) : 90 | BLETypedCharacteristic(uuid, properties) 91 | { 92 | } 93 | -------------------------------------------------------------------------------- /src/BLETypedCharacteristics.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_TYPED_CHARACTERISTICS_H_ 21 | #define _BLE_TYPED_CHARACTERISTICS_H_ 22 | 23 | #include "BLETypedCharacteristic.h" 24 | 25 | class BLEBoolCharacteristic : public BLETypedCharacteristic { 26 | public: 27 | BLEBoolCharacteristic(const char* uuid, unsigned int permissions); 28 | }; 29 | 30 | class BLEBooleanCharacteristic : public BLETypedCharacteristic { 31 | public: 32 | BLEBooleanCharacteristic(const char* uuid, unsigned int permissions); 33 | }; 34 | 35 | class BLECharCharacteristic : public BLETypedCharacteristic { 36 | public: 37 | BLECharCharacteristic(const char* uuid, unsigned int permissions); 38 | }; 39 | 40 | class BLEUnsignedCharCharacteristic : public BLETypedCharacteristic { 41 | public: 42 | BLEUnsignedCharCharacteristic(const char* uuid, unsigned int permissions); 43 | }; 44 | 45 | class BLEByteCharacteristic : public BLETypedCharacteristic { 46 | public: 47 | BLEByteCharacteristic(const char* uuid, unsigned int permissions); 48 | }; 49 | 50 | class BLEShortCharacteristic : public BLETypedCharacteristic { 51 | public: 52 | BLEShortCharacteristic(const char* uuid, unsigned int permissions); 53 | }; 54 | 55 | class BLEUnsignedShortCharacteristic : public BLETypedCharacteristic { 56 | public: 57 | BLEUnsignedShortCharacteristic(const char* uuid, unsigned int permissions); 58 | }; 59 | 60 | class BLEWordCharacteristic : public BLETypedCharacteristic { 61 | public: 62 | BLEWordCharacteristic(const char* uuid, unsigned int permissions); 63 | }; 64 | 65 | class BLEIntCharacteristic : public BLETypedCharacteristic { 66 | public: 67 | BLEIntCharacteristic(const char* uuid, unsigned int permissions); 68 | }; 69 | 70 | class BLEUnsignedIntCharacteristic : public BLETypedCharacteristic { 71 | public: 72 | BLEUnsignedIntCharacteristic(const char* uuid, unsigned int permissions); 73 | }; 74 | 75 | class BLELongCharacteristic : public BLETypedCharacteristic { 76 | public: 77 | BLELongCharacteristic(const char* uuid, unsigned int permissions); 78 | }; 79 | 80 | class BLEUnsignedLongCharacteristic : public BLETypedCharacteristic { 81 | public: 82 | BLEUnsignedLongCharacteristic(const char* uuid, unsigned int permissions); 83 | }; 84 | 85 | class BLEFloatCharacteristic : public BLETypedCharacteristic { 86 | public: 87 | BLEFloatCharacteristic(const char* uuid, unsigned int permissions); 88 | }; 89 | 90 | class BLEDoubleCharacteristic : public BLETypedCharacteristic { 91 | public: 92 | BLEDoubleCharacteristic(const char* uuid, unsigned int permissions); 93 | }; 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/local/BLELocalAttribute.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "BLELocalAttribute.h" 21 | 22 | BLELocalAttribute::BLELocalAttribute(const char* uuid) : 23 | _uuid(uuid), 24 | _refCount(0) 25 | { 26 | } 27 | 28 | BLELocalAttribute::~BLELocalAttribute() 29 | { 30 | } 31 | 32 | const char* BLELocalAttribute::uuid() const 33 | { 34 | return _uuid.str(); 35 | } 36 | 37 | const uint8_t* BLELocalAttribute::uuidData() const 38 | { 39 | return _uuid.data(); 40 | } 41 | 42 | uint8_t BLELocalAttribute::uuidLength() const 43 | { 44 | return _uuid.length(); 45 | } 46 | 47 | enum BLEAttributeType BLELocalAttribute::type() const 48 | { 49 | return BLETypeUnknown; 50 | } 51 | 52 | int BLELocalAttribute::retain() 53 | { 54 | _refCount++; 55 | 56 | return _refCount; 57 | } 58 | 59 | bool BLELocalAttribute::active() 60 | { 61 | return _refCount > 0; 62 | } 63 | 64 | int BLELocalAttribute::release() 65 | { 66 | _refCount--; 67 | 68 | return _refCount; 69 | } 70 | -------------------------------------------------------------------------------- /src/local/BLELocalAttribute.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LOCAL_ATTRIBUTE_H_ 21 | #define _BLE_LOCAL_ATTRIBUTE_H_ 22 | 23 | #include "utility/BLEUuid.h" 24 | 25 | #define BLE_ATTRIBUTE_TYPE_SIZE 2 26 | 27 | enum BLEAttributeType { 28 | BLETypeUnknown = 0x0000, 29 | 30 | BLETypeService = 0x2800, 31 | BLETypeCharacteristic = 0x2803, 32 | BLETypeDescriptor = 0x2900 33 | }; 34 | 35 | class BLELocalAttribute 36 | { 37 | public: 38 | BLELocalAttribute(const char* uuid); 39 | virtual ~BLELocalAttribute(); 40 | 41 | const char* uuid() const; 42 | 43 | virtual enum BLEAttributeType type() const; 44 | 45 | int retain(); 46 | int release(); 47 | bool active(); 48 | 49 | protected: 50 | friend class ATTClass; 51 | friend class GATTClass; 52 | 53 | const uint8_t* uuidData() const; 54 | uint8_t uuidLength() const; 55 | 56 | private: 57 | BLEUuid _uuid; 58 | int _refCount; 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/local/BLELocalCharacteristic.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LOCAL_CHARACTERISTIC_H_ 21 | #define _BLE_LOCAL_CHARACTERISTIC_H_ 22 | 23 | #include 24 | 25 | #include "BLECharacteristic.h" 26 | #include "BLEDescriptor.h" 27 | 28 | #include "BLELocalAttribute.h" 29 | 30 | #include "utility/BLELinkedList.h" 31 | 32 | class BLELocalDescriptor; 33 | 34 | class BLELocalCharacteristic : public BLELocalAttribute { 35 | public: 36 | BLELocalCharacteristic(const char* uuid, uint16_t permissions, int valueSize, bool fixedLength = false); 37 | BLELocalCharacteristic(const char* uuid, uint16_t permissions, const char* value); 38 | virtual ~BLELocalCharacteristic(); 39 | 40 | virtual enum BLEAttributeType type() const; 41 | 42 | uint8_t properties() const; 43 | uint8_t permissions() const; 44 | 45 | int valueSize() const; 46 | const uint8_t* value() const; 47 | int valueLength() const; 48 | uint8_t operator[] (int offset) const; 49 | 50 | int writeValue(const uint8_t value[], int length); 51 | int writeValue(const char* value); 52 | 53 | int broadcast(); 54 | 55 | bool written(); 56 | bool subscribed(); 57 | 58 | void addDescriptor(BLEDescriptor& descriptor); 59 | 60 | void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler); 61 | 62 | protected: 63 | friend class ATTClass; 64 | friend class GATTClass; 65 | 66 | void setHandle(uint16_t handle); 67 | uint16_t handle() const; 68 | uint16_t valueHandle() const; 69 | 70 | unsigned int descriptorCount() const; 71 | BLELocalDescriptor* descriptor(unsigned int index) const; 72 | 73 | void readValue(BLEDevice device, uint16_t offset, uint8_t value[], int length); 74 | void writeValue(BLEDevice device, const uint8_t value[], int length); 75 | void writeCccdValue(BLEDevice device, uint16_t value); 76 | 77 | private: 78 | uint8_t _properties; 79 | uint8_t _permissions; 80 | int _valueSize; 81 | uint8_t* _value; 82 | uint16_t _valueLength; 83 | bool _fixedLength; 84 | 85 | uint16_t _handle; 86 | 87 | bool _broadcast; 88 | bool _written; 89 | 90 | uint16_t _cccdValue; 91 | BLELinkedList _descriptors; 92 | 93 | BLECharacteristicEventHandler _eventHandlers[BLECharacteristicEventLast]; 94 | }; 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/local/BLELocalDescriptor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | 22 | #include "BLELocalDescriptor.h" 23 | 24 | BLELocalDescriptor::BLELocalDescriptor(const char* uuid, const uint8_t value[], int valueSize) : 25 | BLELocalAttribute(uuid), 26 | _value(value), 27 | _valueSize(min(valueSize, 512)), 28 | _handle(0x0000) 29 | { 30 | } 31 | 32 | BLELocalDescriptor::BLELocalDescriptor(const char* uuid, const char* value) : 33 | BLELocalDescriptor(uuid, (const uint8_t*)value, strlen(value)) 34 | { 35 | } 36 | 37 | BLELocalDescriptor::~BLELocalDescriptor() 38 | { 39 | } 40 | 41 | enum BLEAttributeType BLELocalDescriptor::type() const 42 | { 43 | return BLETypeDescriptor; 44 | } 45 | 46 | int BLELocalDescriptor::valueSize() const 47 | { 48 | return _valueSize; 49 | } 50 | 51 | const uint8_t* BLELocalDescriptor::value() const 52 | { 53 | return _value; 54 | } 55 | 56 | uint8_t BLELocalDescriptor::operator[] (int offset) const 57 | { 58 | return _value[offset]; 59 | } 60 | 61 | void BLELocalDescriptor::setHandle(uint16_t handle) 62 | { 63 | _handle = handle; 64 | } 65 | 66 | uint16_t BLELocalDescriptor::handle() const 67 | { 68 | return _handle; 69 | } 70 | -------------------------------------------------------------------------------- /src/local/BLELocalDescriptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LOCAL_DESCRIPTOR_H_ 21 | #define _BLE_LOCAL_DESCRIPTOR_H_ 22 | 23 | #include 24 | 25 | #include "BLELocalAttribute.h" 26 | 27 | class BLELocalDescriptor : public BLELocalAttribute { 28 | public: 29 | BLELocalDescriptor(const char* uuid, const uint8_t value[], int valueSize); 30 | BLELocalDescriptor(const char* uuid, const char* value); 31 | virtual ~BLELocalDescriptor(); 32 | 33 | virtual enum BLEAttributeType type() const; 34 | 35 | int valueSize() const; 36 | const uint8_t* value() const; 37 | uint8_t operator[] (int offset) const; 38 | 39 | protected: 40 | friend class GATTClass; 41 | 42 | void setHandle(uint16_t handle); 43 | uint16_t handle() const; 44 | 45 | private: 46 | const uint8_t* _value; 47 | int _valueSize; 48 | 49 | uint16_t _handle; 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/local/BLELocalDevice.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LOCAL_DEVICE_H_ 21 | #define _BLE_LOCAL_DEVICE_H_ 22 | 23 | #include "BLEDevice.h" 24 | #include "BLEService.h" 25 | #include "BLEAdvertisingData.h" 26 | 27 | enum Pairable { 28 | NO = 0, 29 | YES = 1, 30 | ONCE = 2, 31 | }; 32 | 33 | class BLELocalDevice { 34 | public: 35 | BLELocalDevice(); 36 | virtual ~BLELocalDevice(); 37 | 38 | virtual int begin(); 39 | virtual void end(); 40 | 41 | virtual void poll(); 42 | virtual void poll(unsigned long timeout); 43 | 44 | virtual bool connected() const; 45 | virtual bool disconnect(); 46 | 47 | virtual String address() const; 48 | 49 | virtual int rssi(); 50 | 51 | virtual bool setAdvertisedServiceUuid(const char* advertisedServiceUuid); 52 | virtual bool setAdvertisedService(const BLEService& service); 53 | virtual bool setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length); 54 | virtual bool setManufacturerData(const uint8_t manufacturerData[], int manufacturerDataLength); 55 | virtual bool setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength); 56 | virtual bool setLocalName(const char *localName); 57 | 58 | virtual void setAdvertisingData(BLEAdvertisingData& advertisingData); 59 | virtual void setScanResponseData(BLEAdvertisingData& scanResponseData); 60 | 61 | virtual void setDeviceName(const char* deviceName); 62 | virtual void setAppearance(uint16_t appearance); 63 | 64 | virtual void addService(BLEService& service); 65 | 66 | virtual int advertise(); 67 | virtual void stopAdvertise(); 68 | 69 | virtual int scan(bool withDuplicates = false); 70 | virtual int scanForName(String name, bool withDuplicates = false); 71 | virtual int scanForUuid(String uuid, bool withDuplicates = false); 72 | virtual int scanForAddress(String address, bool withDuplicates = false); 73 | virtual void stopScan(); 74 | 75 | virtual BLEDevice central(); 76 | virtual BLEDevice available(); 77 | 78 | virtual void setAdvertisingInterval(uint16_t advertisingInterval); 79 | virtual void setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval); 80 | virtual void setSupervisionTimeout(uint16_t supervisionTimeout); 81 | virtual void setConnectable(bool connectable); 82 | 83 | virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); 84 | 85 | virtual void setTimeout(unsigned long timeout); 86 | 87 | virtual void debug(Stream& stream); 88 | virtual void noDebug(); 89 | 90 | virtual void setPairable(uint8_t pairable); 91 | virtual bool pairable(); 92 | virtual bool paired(); 93 | 94 | // address - The mac to store 95 | // IRK - The IRK to store with this mac 96 | virtual void setStoreIRK(int (*storeIRK)(uint8_t* address, uint8_t* IRK)); 97 | // nIRKs - the number of IRKs being provided. 98 | // BDAddrType - an array containing the type of each address (0 public, 1 static random) 99 | // BDAddrs - an array containing the list of addresses 100 | virtual void setGetIRKs(int (*getIRKs)(uint8_t* nIRKs, uint8_t** BDAddrType, uint8_t*** BDAddrs, uint8_t*** IRKs)); 101 | // address - the address to store [6 bytes] 102 | // LTK - the LTK to store with this mac [16 bytes] 103 | virtual void setStoreLTK(int (*storeLTK)(uint8_t* address, uint8_t* LTK)); 104 | // address - The mac address needing its LTK 105 | // LTK - 16 octet LTK for the mac address 106 | virtual void setGetLTK(int (*getLTK)(uint8_t* address, uint8_t* LTK)); 107 | 108 | virtual void setDisplayCode(void (*displayCode)(uint32_t confirmationCode)); 109 | virtual void setBinaryConfirmPairing(bool (*binaryConfirmPairing)()); 110 | uint8_t BDaddress[6]; 111 | 112 | protected: 113 | virtual BLEAdvertisingData& getAdvertisingData(); 114 | virtual BLEAdvertisingData& getScanResponseData(); 115 | 116 | private: 117 | BLEAdvertisingData _advertisingData; 118 | BLEAdvertisingData _scanResponseData; 119 | }; 120 | 121 | extern BLELocalDevice& BLE; 122 | 123 | #endif 124 | -------------------------------------------------------------------------------- /src/local/BLELocalService.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "BLELocalCharacteristic.h" 21 | 22 | #include "BLELocalService.h" 23 | 24 | BLELocalService::BLELocalService(const char* uuid) : 25 | BLELocalAttribute(uuid), 26 | _startHandle(0x0000), 27 | _endHandle(0x0000) 28 | { 29 | } 30 | 31 | void BLELocalService::clear() { 32 | _characteristics.clear(); 33 | _startHandle = 0; 34 | _endHandle = 0; 35 | } 36 | 37 | BLELocalService::~BLELocalService() 38 | { 39 | for (unsigned int i = 0; i < characteristicCount(); i++) { 40 | BLELocalCharacteristic* c = characteristic(i); 41 | 42 | if (c->release() == 0) { 43 | delete c; 44 | } 45 | } 46 | clear(); 47 | } 48 | 49 | enum BLEAttributeType BLELocalService::type() const 50 | { 51 | return BLETypeService; 52 | } 53 | 54 | void BLELocalService::addCharacteristic(BLECharacteristic& characteristic) 55 | { 56 | BLELocalCharacteristic* localCharacteristic = characteristic.local(); 57 | 58 | if (localCharacteristic) { 59 | addCharacteristic(localCharacteristic); 60 | } 61 | } 62 | 63 | void BLELocalService::setHandles(uint16_t start, uint16_t end) 64 | { 65 | _startHandle = start; 66 | _endHandle = end; 67 | } 68 | 69 | uint16_t BLELocalService::startHandle() const 70 | { 71 | return _startHandle; 72 | } 73 | 74 | uint16_t BLELocalService::endHandle() const 75 | { 76 | return _endHandle; 77 | } 78 | 79 | unsigned int BLELocalService::characteristicCount() const 80 | { 81 | return _characteristics.size(); 82 | } 83 | 84 | BLELocalCharacteristic* BLELocalService::characteristic(unsigned int index) const 85 | { 86 | return _characteristics.get(index); 87 | } 88 | 89 | void BLELocalService::addCharacteristic(BLELocalCharacteristic* characteristic) 90 | { 91 | characteristic->retain(); 92 | 93 | _characteristics.add(characteristic); 94 | } 95 | -------------------------------------------------------------------------------- /src/local/BLELocalService.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LOCAL_SERVICE_H_ 21 | #define _BLE_LOCAL_SERVICE_H_ 22 | 23 | #include "BLECharacteristic.h" 24 | 25 | #include "BLELocalAttribute.h" 26 | 27 | #include "utility/BLELinkedList.h" 28 | 29 | class BLELocalCharacteristic; 30 | 31 | class BLELocalService : public BLELocalAttribute { 32 | public: 33 | BLELocalService(const char* uuid); 34 | virtual ~BLELocalService(); 35 | 36 | virtual enum BLEAttributeType type() const; 37 | 38 | void addCharacteristic(BLECharacteristic& characteristic); 39 | void clear(); 40 | 41 | protected: 42 | friend class ATTClass; 43 | friend class GATTClass; 44 | 45 | void setHandles(uint16_t start, uint16_t end); 46 | uint16_t startHandle() const; 47 | uint16_t endHandle() const; 48 | 49 | unsigned int characteristicCount() const; 50 | BLELocalCharacteristic* characteristic(unsigned int index) const; 51 | 52 | void addCharacteristic(BLELocalCharacteristic* characteristic); 53 | 54 | private: 55 | uint16_t _startHandle; 56 | uint16_t _endHandle; 57 | 58 | BLELinkedList _characteristics; 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/remote/BLERemoteAttribute.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "utility/BLEUuid.h" 21 | 22 | #include "BLERemoteAttribute.h" 23 | 24 | BLERemoteAttribute::BLERemoteAttribute(const uint8_t uuid[], uint8_t uuidLen) : 25 | _uuid(BLEUuid::uuidToString(uuid, uuidLen)), 26 | _refCount(0) 27 | { 28 | } 29 | 30 | BLERemoteAttribute::~BLERemoteAttribute() 31 | { 32 | } 33 | 34 | const char* BLERemoteAttribute::uuid() const 35 | { 36 | return _uuid.c_str(); 37 | } 38 | 39 | int BLERemoteAttribute::retain() 40 | { 41 | _refCount++; 42 | 43 | return _refCount; 44 | } 45 | 46 | int BLERemoteAttribute::release() 47 | { 48 | _refCount--; 49 | 50 | return _refCount; 51 | } 52 | -------------------------------------------------------------------------------- /src/remote/BLERemoteAttribute.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_REMOTE_ATTRIBUTE_H_ 21 | #define _BLE_REMOTE_ATTRIBUTE_H_ 22 | 23 | #include 24 | 25 | class BLERemoteAttribute 26 | { 27 | public: 28 | BLERemoteAttribute(const uint8_t uuid[], uint8_t uuidLen); 29 | virtual ~BLERemoteAttribute(); 30 | 31 | const char* uuid() const; 32 | 33 | int retain(); 34 | int release(); 35 | 36 | private: 37 | String _uuid; 38 | int _refCount; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /src/remote/BLERemoteCharacteristic.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_REMOTE_CHARACTERISTIC_H_ 21 | #define _BLE_REMOTE_CHARACTERISTIC_H_ 22 | 23 | #include "BLECharacteristic.h" 24 | 25 | #include "BLERemoteAttribute.h" 26 | #include "BLERemoteDescriptor.h" 27 | 28 | #include "utility/BLELinkedList.h" 29 | 30 | class BLERemoteCharacteristic : public BLERemoteAttribute { 31 | public: 32 | BLERemoteCharacteristic(const uint8_t uuid[], uint8_t uuidLen, uint16_t connectionHandle, uint16_t startHandle, uint16_t permissions, uint16_t valueHandle); 33 | virtual ~BLERemoteCharacteristic(); 34 | 35 | uint8_t properties() const; 36 | uint8_t permissions() const; 37 | 38 | const uint8_t* value() const; 39 | int valueLength() const; 40 | uint8_t operator[] (int offset) const; 41 | 42 | int writeValue(const uint8_t value[], int length, bool withResponse = true); 43 | int writeValue(const char* value, bool withResponse = true); 44 | 45 | bool valueUpdated(); 46 | bool updatedValueRead(); 47 | 48 | bool read(); 49 | bool writeCccd(uint16_t value); 50 | 51 | unsigned int descriptorCount() const; 52 | BLERemoteDescriptor* descriptor(unsigned int index) const; 53 | 54 | void setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler); 55 | 56 | protected: 57 | friend class ATTClass; 58 | 59 | uint16_t startHandle() const; 60 | uint16_t valueHandle() const; 61 | 62 | void addDescriptor(BLERemoteDescriptor* descriptor); 63 | 64 | void writeValue(BLEDevice device, const uint8_t value[], int length); 65 | 66 | private: 67 | uint16_t _connectionHandle; 68 | uint16_t _startHandle; 69 | uint8_t _properties; 70 | uint8_t _permissions; 71 | uint16_t _valueHandle; 72 | 73 | uint8_t* _value; 74 | int _valueLength; 75 | 76 | bool _valueUpdated; 77 | bool _updatedValueRead; 78 | 79 | BLELinkedList _descriptors; 80 | 81 | BLECharacteristicEventHandler _valueUpdatedEventHandler; 82 | }; 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /src/remote/BLERemoteDescriptor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "utility/ATT.h" 21 | 22 | #include "BLERemoteDescriptor.h" 23 | 24 | BLERemoteDescriptor::BLERemoteDescriptor(const uint8_t uuid[], uint8_t uuidLen, uint16_t connectionHandle, uint16_t handle) : 25 | BLERemoteAttribute(uuid, uuidLen), 26 | _connectionHandle(connectionHandle), 27 | _handle(handle), 28 | _value(NULL), 29 | _valueLength(0) 30 | { 31 | } 32 | 33 | BLERemoteDescriptor::~BLERemoteDescriptor() 34 | { 35 | if (_value) { 36 | free(_value); 37 | _value = NULL; 38 | } 39 | } 40 | 41 | const uint8_t* BLERemoteDescriptor::value() const 42 | { 43 | return _value; 44 | } 45 | 46 | int BLERemoteDescriptor::valueLength() const 47 | { 48 | return _valueLength; 49 | } 50 | 51 | uint8_t BLERemoteDescriptor::operator[] (int offset) const 52 | { 53 | if (_value) { 54 | return _value[offset]; 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | int BLERemoteDescriptor::writeValue(const uint8_t value[], int length) 61 | { 62 | if (!ATT.connected(_connectionHandle)) { 63 | return false; 64 | } 65 | 66 | uint16_t maxLength = ATT.mtu(_connectionHandle) - 3; 67 | 68 | if (length > (int)maxLength) { 69 | // cap to MTU max length 70 | length = maxLength; 71 | } 72 | 73 | _value = (uint8_t*)realloc(_value, length); 74 | if (_value == NULL) { 75 | // realloc failed 76 | return 0; 77 | } 78 | 79 | uint8_t resp[4]; 80 | int respLength = ATT.writeReq(_connectionHandle, _handle, value, length, resp); 81 | 82 | if (!respLength) { 83 | return 0; 84 | } 85 | 86 | if (resp[0] == 0x01) { 87 | // error 88 | return 0; 89 | } 90 | 91 | memcpy(_value, value, length); 92 | _valueLength = length; 93 | 94 | return 1; 95 | } 96 | 97 | bool BLERemoteDescriptor::read() 98 | { 99 | if (!ATT.connected(_connectionHandle)) { 100 | return false; 101 | } 102 | 103 | uint8_t resp[256]; 104 | 105 | int respLength = ATT.readReq(_connectionHandle, _handle, resp); 106 | 107 | if (!respLength) { 108 | _valueLength = 0; 109 | return false; 110 | } 111 | 112 | if (resp[0] == 0x01) { 113 | // error 114 | _valueLength = 0; 115 | return false; 116 | } 117 | 118 | _valueLength = respLength - 1; 119 | _value = (uint8_t*)realloc(_value, _valueLength); 120 | 121 | if (_value == NULL) { 122 | _valueLength = 0; 123 | return false; 124 | } 125 | 126 | memcpy(_value, &resp[1], _valueLength); 127 | 128 | return true; 129 | } 130 | 131 | uint16_t BLERemoteDescriptor::handle() const 132 | { 133 | return _handle; 134 | } 135 | -------------------------------------------------------------------------------- /src/remote/BLERemoteDescriptor.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_REMOTE_DESCRIPTOR_H_ 21 | #define _BLE_REMOTE_DESCRIPTOR_H_ 22 | 23 | #include "BLERemoteAttribute.h" 24 | 25 | class BLERemoteDescriptor : public BLERemoteAttribute { 26 | public: 27 | BLERemoteDescriptor(const uint8_t uuid[], uint8_t uuidLen, uint16_t connectionHandle, uint16_t handle); 28 | virtual ~BLERemoteDescriptor(); 29 | 30 | const uint8_t* value() const; 31 | int valueLength() const; 32 | uint8_t operator[] (int offset) const; 33 | 34 | int writeValue(const uint8_t value[], int length); 35 | 36 | bool read(); 37 | 38 | protected: 39 | friend class ATTClass; 40 | uint16_t handle() const; 41 | 42 | private: 43 | uint16_t _connectionHandle; 44 | uint16_t _handle; 45 | 46 | uint8_t* _value; 47 | int _valueLength; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /src/remote/BLERemoteDevice.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "BLERemoteDevice.h" 21 | 22 | BLERemoteDevice::BLERemoteDevice() 23 | { 24 | } 25 | 26 | BLERemoteDevice::~BLERemoteDevice() 27 | { 28 | clearServices(); 29 | } 30 | 31 | void BLERemoteDevice::addService(BLERemoteService* service) 32 | { 33 | service->retain(); 34 | 35 | _services.add(service); 36 | } 37 | 38 | unsigned int BLERemoteDevice::serviceCount() const 39 | { 40 | return _services.size(); 41 | } 42 | 43 | BLERemoteService* BLERemoteDevice::service(unsigned int index) const 44 | { 45 | return _services.get(index); 46 | } 47 | 48 | void BLERemoteDevice::clearServices() 49 | { 50 | for (unsigned int i = 0; i < serviceCount(); i++) { 51 | BLERemoteService* s = service(i); 52 | 53 | if (s->release() == 0) { 54 | delete s; 55 | } 56 | } 57 | 58 | _services.clear(); 59 | } 60 | -------------------------------------------------------------------------------- /src/remote/BLERemoteDevice.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_REMOTE_DEVICE_H_ 21 | #define _BLE_REMOTE_DEVICE_H_ 22 | 23 | #include "utility/BLELinkedList.h" 24 | 25 | #include "BLERemoteService.h" 26 | 27 | class BLERemoteDevice /*: public BLEDevice*/ { 28 | public: 29 | BLERemoteDevice(); 30 | virtual ~BLERemoteDevice(); 31 | 32 | void addService(BLERemoteService* service); 33 | 34 | unsigned int serviceCount() const; 35 | BLERemoteService* service(unsigned int index) const; 36 | 37 | void clearServices(); 38 | 39 | private: 40 | BLELinkedList _services; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/remote/BLERemoteService.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "BLERemoteService.h" 21 | 22 | BLERemoteService::BLERemoteService(const uint8_t uuid[], uint8_t uuidLen, uint16_t startHandle, uint16_t endHandle) : 23 | BLERemoteAttribute(uuid, uuidLen), 24 | _startHandle(startHandle), 25 | _endHandle(endHandle) 26 | { 27 | } 28 | 29 | BLERemoteService::~BLERemoteService() 30 | { 31 | for (unsigned int i = 0; i < characteristicCount(); i++) { 32 | BLERemoteCharacteristic* c = characteristic(i); 33 | 34 | if (c->release() == 0) { 35 | delete c; 36 | } 37 | } 38 | 39 | _characteristics.clear(); 40 | } 41 | 42 | uint16_t BLERemoteService::startHandle() const 43 | { 44 | return _startHandle; 45 | } 46 | 47 | uint16_t BLERemoteService::endHandle() const 48 | { 49 | return _endHandle; 50 | } 51 | 52 | unsigned int BLERemoteService::characteristicCount() const 53 | { 54 | return _characteristics.size(); 55 | } 56 | 57 | BLERemoteCharacteristic* BLERemoteService::characteristic(unsigned int index) const 58 | { 59 | return _characteristics.get(index); 60 | } 61 | 62 | void BLERemoteService::addCharacteristic(BLERemoteCharacteristic* characteristic) 63 | { 64 | characteristic-> retain(); 65 | 66 | _characteristics.add(characteristic); 67 | } 68 | -------------------------------------------------------------------------------- /src/remote/BLERemoteService.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_REMOTE_SERVICE_H_ 21 | #define _BLE_REMOTE_SERVICE_H_ 22 | 23 | #include "BLERemoteAttribute.h" 24 | #include "BLERemoteCharacteristic.h" 25 | 26 | #include "utility/BLELinkedList.h" 27 | 28 | class BLERemoteService : public BLERemoteAttribute { 29 | public: 30 | BLERemoteService(const uint8_t uuid[], uint8_t uuidLen, uint16_t startHandle, uint16_t endHandle); 31 | virtual ~BLERemoteService(); 32 | 33 | unsigned int characteristicCount() const; 34 | BLERemoteCharacteristic* characteristic(unsigned int index) const; 35 | 36 | protected: 37 | friend class ATTClass; 38 | 39 | uint16_t startHandle() const; 40 | uint16_t endHandle() const; 41 | 42 | void addCharacteristic(BLERemoteCharacteristic* characteristic); 43 | 44 | private: 45 | uint16_t _startHandle; 46 | uint16_t _endHandle; 47 | 48 | String _uuid; 49 | 50 | BLELinkedList _characteristics; 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/utility/BLELinkedList.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_LINKED_LIST_ 21 | #define _BLE_LINKED_LIST_ 22 | 23 | #include 24 | 25 | template struct BLELinkedListNode 26 | { 27 | T data; 28 | BLELinkedListNode* next; 29 | }; 30 | 31 | template class BLELinkedList { 32 | public: 33 | BLELinkedList(); 34 | ~BLELinkedList(); 35 | 36 | void add(T); 37 | T get(unsigned int index) const; 38 | void clear(); 39 | T remove(unsigned int index); 40 | 41 | unsigned int size() const; 42 | 43 | private: 44 | unsigned int _size; 45 | BLELinkedListNode* _root; 46 | BLELinkedListNode* _last; 47 | }; 48 | 49 | template BLELinkedList::BLELinkedList() : 50 | _size(0), 51 | _root(NULL), 52 | _last(NULL) 53 | { 54 | } 55 | 56 | template BLELinkedList::~BLELinkedList() 57 | { 58 | clear(); 59 | } 60 | 61 | template void BLELinkedList::add(T item) 62 | { 63 | BLELinkedListNode* itemNode = new BLELinkedListNode(); 64 | 65 | itemNode->data = item; 66 | itemNode->next = NULL; 67 | 68 | if (_root == NULL) { 69 | _root = itemNode; 70 | } else { 71 | _last->next = itemNode; 72 | } 73 | _last = itemNode; 74 | 75 | _size++; 76 | } 77 | 78 | template T BLELinkedList::get(unsigned int index) const 79 | { 80 | if (index >= _size) { 81 | return T(); 82 | } 83 | 84 | BLELinkedListNode* itemNode = _root; 85 | 86 | for (unsigned int i = 0; i < index; i++) { 87 | itemNode = itemNode->next; 88 | } 89 | 90 | return itemNode->data; 91 | } 92 | 93 | template void BLELinkedList::clear() 94 | { 95 | BLELinkedListNode* itemNode = _root; 96 | 97 | for (unsigned int i = 0; i < _size; i++) { 98 | BLELinkedListNode* n = itemNode; 99 | 100 | itemNode = itemNode->next; 101 | 102 | delete n; 103 | } 104 | 105 | _size = 0; 106 | _root = NULL; 107 | _last = NULL; 108 | } 109 | 110 | template unsigned int BLELinkedList::size() const 111 | { 112 | return _size; 113 | } 114 | 115 | template T BLELinkedList::remove(unsigned int index) 116 | { 117 | if (index >= _size) { 118 | return T(); 119 | } 120 | 121 | BLELinkedListNode* previousItemNode = NULL; 122 | BLELinkedListNode* itemNode = _root; 123 | 124 | for (unsigned int i = 0; i < index; i++) { 125 | previousItemNode = itemNode; 126 | itemNode = itemNode->next; 127 | } 128 | 129 | T result = itemNode->data; 130 | 131 | if (previousItemNode == NULL) { 132 | _root = itemNode->next; 133 | } else { 134 | previousItemNode->next = itemNode->next; 135 | } 136 | 137 | if (_last == itemNode) { 138 | _last = previousItemNode; 139 | } 140 | 141 | delete itemNode; 142 | _size--; 143 | 144 | return result; 145 | } 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /src/utility/BLEUuid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "BLEUuid.h" 24 | 25 | BLEUuid::BLEUuid(const char * str) : 26 | _str(str) 27 | { 28 | char temp[] = {0, 0, 0}; 29 | 30 | memset(_data, 0x00, sizeof(_data)); 31 | 32 | _length = 0; 33 | 34 | if (str == NULL) { 35 | return; 36 | } 37 | 38 | for (int i = strlen(str) - 1; i >= 0 && _length < BLE_UUID_MAX_LENGTH; i -= 2) { 39 | if (str[i] == '-') { 40 | i++; 41 | continue; 42 | } 43 | 44 | temp[0] = str[i - 1]; 45 | temp[1] = str[i]; 46 | 47 | _data[_length] = strtoul(temp, NULL, 16); 48 | 49 | _length++; 50 | } 51 | 52 | if (_length <= 2) { 53 | _length = 2; 54 | } else { 55 | _length = 16; 56 | } 57 | } 58 | 59 | const char* BLEUuid::str() const 60 | { 61 | return _str; 62 | } 63 | 64 | const uint8_t* BLEUuid::data() const 65 | { 66 | return _data; 67 | } 68 | 69 | uint8_t BLEUuid::length() const 70 | { 71 | return _length; 72 | } 73 | 74 | const char* BLEUuid::uuidToString(const uint8_t* data, uint8_t length) 75 | { 76 | static char uuid[36 + 1]; 77 | char* c = uuid; 78 | 79 | for (int i = length - 1; i >= 0; i--) { 80 | uint8_t b = data[i]; 81 | 82 | utoa(b >> 4, c++, 16); 83 | utoa(b & 0x0f, c++, 16); 84 | 85 | if (i == 6 || i == 8 || i == 10 || i == 12) { 86 | *c++ = '-'; 87 | } 88 | } 89 | 90 | *c = '\0'; 91 | 92 | return uuid; 93 | } 94 | -------------------------------------------------------------------------------- /src/utility/BLEUuid.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _BLE_UUID_H_ 21 | #define _BLE_UUID_H_ 22 | 23 | #include 24 | 25 | #define BLE_UUID_MAX_LENGTH 16 26 | 27 | class BLEUuid 28 | { 29 | public: 30 | BLEUuid(const char * str); 31 | 32 | const char* str() const; 33 | const uint8_t * data() const; 34 | uint8_t length() const; 35 | 36 | static const char* uuidToString(const uint8_t* data, uint8_t length); 37 | 38 | private: 39 | const char* _str; 40 | uint8_t _data[BLE_UUID_MAX_LENGTH]; 41 | uint8_t _length; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/utility/CordioHCICustomDriver.h: -------------------------------------------------------------------------------- 1 | #if defined(CORE_CM4) 2 | 3 | #include "CyH4TransportDriver.h" 4 | 5 | ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_h4_transport_driver() 6 | { 7 | static ble::vendor::cypress_ble::CyH4TransportDriver s_transport_driver( 8 | /* TX */ CYBSP_BT_UART_TX, /* RX */ CYBSP_BT_UART_RX, 9 | /* cts */ CYBSP_BT_UART_CTS, /* rts */ CYBSP_BT_UART_RTS, NC, DEF_BT_BAUD_RATE, 10 | CYBSP_BT_HOST_WAKE, CYBSP_BT_DEVICE_WAKE 11 | ); 12 | return s_transport_driver; 13 | } 14 | 15 | #define CUSTOM_HCI_DRIVER 16 | 17 | #endif -------------------------------------------------------------------------------- /src/utility/GAP.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _GAP_H_ 21 | #define _GAP_H_ 22 | 23 | #include "utility/BLELinkedList.h" 24 | 25 | #include "BLEDevice.h" 26 | 27 | class GAPClass { 28 | public: 29 | GAPClass(); 30 | virtual ~GAPClass(); 31 | 32 | virtual bool advertising(); 33 | virtual int advertise(uint8_t* advData, uint8_t advDataLength, uint8_t* scanData, uint8_t scanDataLength); 34 | virtual void stopAdvertise(); 35 | 36 | virtual int scan(bool withDuplicates); 37 | virtual int scanForName(String name, bool withDuplicates); 38 | virtual int scanForUuid(String uuid, bool withDuplicates); 39 | virtual int scanForAddress(String address, bool withDuplicates); 40 | virtual void stopScan(); 41 | virtual BLEDevice available(); 42 | 43 | virtual void setAdvertisingInterval(uint16_t advertisingInterval); 44 | virtual void setConnectable(bool connectable); 45 | 46 | virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); 47 | 48 | protected: 49 | friend class HCIClass; 50 | 51 | virtual void handleLeAdvertisingReport(uint8_t type, uint8_t addressType, uint8_t address[6], 52 | uint8_t eirLength, uint8_t eirData[], int8_t rssi); 53 | 54 | private: 55 | virtual bool matchesScanFilter(const BLEDevice& device); 56 | 57 | private: 58 | bool _advertising; 59 | bool _scanning; 60 | 61 | uint16_t _advertisingInterval; 62 | bool _connectable; 63 | 64 | BLEDeviceEventHandler _discoverEventHandler; 65 | BLELinkedList _discoveredDevices; 66 | 67 | String _scanNameFilter; 68 | String _scanUuidFilter; 69 | String _scanAddressFilter; 70 | }; 71 | 72 | extern GAPClass& GAP; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/utility/GATT.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _GATT_H_ 21 | #define _GATT_H_ 22 | 23 | #include "utility/BLELinkedList.h" 24 | 25 | #include "local/BLELocalAttribute.h" 26 | #include "local/BLELocalCharacteristic.h" 27 | #include "local/BLELocalService.h" 28 | 29 | #include "BLEService.h" 30 | 31 | class GATTClass { 32 | public: 33 | GATTClass(); 34 | virtual ~GATTClass(); 35 | 36 | virtual void begin(); 37 | virtual void end(); 38 | 39 | virtual void setDeviceName(const char* deviceName); 40 | virtual void setAppearance(uint16_t appearance); 41 | 42 | virtual void addService(BLEService& service); 43 | 44 | protected: 45 | friend class ATTClass; 46 | 47 | virtual unsigned int attributeCount() const; 48 | virtual BLELocalAttribute* attribute(unsigned int index) const; 49 | 50 | protected: 51 | friend class BLELocalCharacteristic; 52 | 53 | virtual uint16_t serviceUuidForCharacteristic(BLELocalCharacteristic* characteristic) const; 54 | 55 | private: 56 | virtual void addService(BLELocalService* service); 57 | 58 | virtual void clearAttributes(); 59 | 60 | private: 61 | BLELinkedList _attributes; 62 | BLELinkedList _services; 63 | 64 | BLELocalService* _genericAccessService; 65 | BLELocalCharacteristic* _deviceNameCharacteristic; 66 | BLELocalCharacteristic* _appearanceCharacteristic; 67 | BLELocalService* _genericAttributeService; 68 | BLELocalCharacteristic* _servicesChangedCharacteristic; 69 | }; 70 | 71 | extern GATTClass& GATT; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/utility/HCICordioTransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2019 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _HCI_CORDIO_TRANSPORT_H_ 21 | #define _HCI_CORDIO_TRANSPORT_H_ 22 | 23 | #include 24 | 25 | #include "api/RingBuffer.h" 26 | 27 | #include "HCITransport.h" 28 | 29 | class HCICordioTransportClass : public HCITransportInterface { 30 | public: 31 | HCICordioTransportClass(); 32 | virtual ~HCICordioTransportClass(); 33 | 34 | virtual int begin(); 35 | virtual void end(); 36 | 37 | virtual void wait(unsigned long timeout); 38 | 39 | virtual int available(); 40 | virtual int peek(); 41 | virtual int read(); 42 | 43 | virtual size_t write(const uint8_t* data, size_t length); 44 | 45 | private: 46 | static void onDataReceived(uint8_t* data, uint8_t len); 47 | void handleRxData(uint8_t* data, uint8_t len); 48 | 49 | private: 50 | bool _begun; 51 | RingBufferN<256> _rxBuf; 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/utility/HCISilabsTransport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2024 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if defined(ARDUINO_SILABS) 21 | 22 | #include "HCISilabsTransport.h" 23 | #include "sl_string.h" 24 | 25 | extern "C" { 26 | #include "sl_btctrl_linklayer.h" 27 | #include "sl_hci_common_transport.h" 28 | } 29 | 30 | extern "C" int strcasecmp(char const *a, char const *b) { 31 | return sl_strcasecmp(a, b); 32 | } 33 | 34 | static RingBufferN<512> buf; 35 | 36 | HCISilabsTransportClass::HCISilabsTransportClass() 37 | { 38 | } 39 | 40 | HCISilabsTransportClass::~HCISilabsTransportClass() 41 | { 42 | } 43 | 44 | int HCISilabsTransportClass::begin() 45 | { 46 | if(!sl_btctrl_is_initialized()) { 47 | sl_bt_controller_init(); 48 | } 49 | 50 | /* Initialize adv & scan components */ 51 | sl_btctrl_init_adv(); 52 | sl_btctrl_init_scan(); 53 | sl_btctrl_init_conn(); 54 | sl_btctrl_init_adv_ext(); 55 | sl_btctrl_init_scan_ext(); 56 | 57 | /* Initialize HCI controller */ 58 | sl_bthci_init_upper(); 59 | sl_btctrl_hci_parser_init_default(); 60 | sl_btctrl_hci_parser_init_conn(); 61 | sl_btctrl_hci_parser_init_adv(); 62 | sl_btctrl_hci_parser_init_phy(); 63 | 64 | return 1; 65 | } 66 | 67 | void HCISilabsTransportClass::end() 68 | { 69 | sl_bt_controller_deinit(); 70 | } 71 | 72 | void HCISilabsTransportClass::wait(unsigned long timeout) 73 | { 74 | for (unsigned long start = millis(); (millis() - start) < timeout;) { 75 | if (available()) { 76 | break; 77 | } 78 | } 79 | } 80 | 81 | int HCISilabsTransportClass::available() 82 | { 83 | return buf.available(); 84 | } 85 | 86 | int HCISilabsTransportClass::peek() 87 | { 88 | return buf.peek(); 89 | } 90 | 91 | int HCISilabsTransportClass::read() 92 | { 93 | return buf.read_char(); 94 | } 95 | 96 | size_t HCISilabsTransportClass::write(const uint8_t* data, size_t len) 97 | { 98 | int ret = 0; 99 | ret = hci_common_transport_receive((uint8_t *)data, len, true); 100 | 101 | if (ret == 0) return len; 102 | 103 | return 0; 104 | } 105 | 106 | extern "C" { 107 | /** 108 | * @brief Transmit HCI message using the currently used transport layer. 109 | * The HCI calls this function to transmit a full HCI message. 110 | * @param[in] data Packet type followed by HCI packet data. 111 | * @param[in] len Length of the `data` parameter 112 | * @return 0 - on success, or non-zero on failure. 113 | */ 114 | uint32_t hci_common_transport_transmit(uint8_t *data, int16_t len) 115 | { 116 | for (int i = 0; i < len; i++) { 117 | buf.store_char(data[i]); 118 | if (buf.isFull()) return SL_STATUS_FAIL; 119 | } 120 | 121 | sl_btctrl_hci_transmit_complete(0); 122 | return 0; 123 | } 124 | } 125 | 126 | HCISilabsTransportClass HCISilabsTransport; 127 | 128 | HCITransportInterface& HCITransport = HCISilabsTransport; 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /src/utility/HCISilabsTransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _HCI_SILABS_TRANSPORT_H_ 21 | #define _HCI_SILABS_TRANSPORT_H_ 22 | 23 | #include "HCITransport.h" 24 | 25 | class HCISilabsTransportClass : public HCITransportInterface { 26 | public: 27 | HCISilabsTransportClass(); 28 | virtual ~HCISilabsTransportClass(); 29 | 30 | virtual int begin(); 31 | virtual void end(); 32 | 33 | virtual void wait(unsigned long timeout); 34 | 35 | virtual int available(); 36 | virtual int peek(); 37 | virtual int read(); 38 | 39 | virtual size_t write(const uint8_t* data, size_t length); 40 | }; 41 | 42 | #endif -------------------------------------------------------------------------------- /src/utility/HCITransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _HCI_TRANSPORT_INTERFACE_H_ 21 | #define _HCI_TRANSPORT_INTERFACE_H_ 22 | 23 | #include 24 | 25 | class HCITransportInterface { 26 | public: 27 | virtual int begin() = 0; 28 | virtual void end() = 0; 29 | 30 | virtual void wait(unsigned long timeout) = 0; 31 | 32 | virtual int available() = 0; 33 | virtual int peek() = 0; 34 | virtual int read() = 0; 35 | 36 | virtual size_t write(const uint8_t* data, size_t length) = 0; 37 | }; 38 | 39 | extern HCITransportInterface& HCITransport; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /src/utility/HCIUartTransport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4) 21 | 22 | #include "HCIUartTransport.h" 23 | 24 | #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) 25 | #define SerialHCI Serial2 26 | #elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT) 27 | // SerialHCI is already defined in the variant 28 | #elif defined(ARDUINO_PORTENTA_H7_M4) 29 | // SerialHCI is already defined in the variant 30 | #elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) 31 | #define SerialHCI Serial2 32 | #elif defined(ARDUINO_OPTA) 33 | #define SerialHCI Serial3 34 | #elif defined(ARDUINO_PORTENTA_C33) 35 | #define SerialHCI Serial5 36 | #elif defined(ARDUINO_GIGA) 37 | arduino::UART SerialHCI(CYBSP_BT_UART_TX, CYBSP_BT_UART_RX, CYBSP_BT_UART_RTS, CYBSP_BT_UART_CTS); 38 | #else 39 | #error "Unsupported board selected!" 40 | #endif 41 | 42 | HCIUartTransportClass::HCIUartTransportClass(HardwareSerial& uart, unsigned long baudrate) : 43 | _uart(&uart), 44 | _baudrate(baudrate) 45 | { 46 | } 47 | 48 | HCIUartTransportClass::~HCIUartTransportClass() 49 | { 50 | } 51 | 52 | int HCIUartTransportClass::begin() 53 | { 54 | _uart->begin(_baudrate); 55 | 56 | return 1; 57 | } 58 | 59 | void HCIUartTransportClass::end() 60 | { 61 | _uart->end(); 62 | } 63 | 64 | void HCIUartTransportClass::wait(unsigned long timeout) 65 | { 66 | for (unsigned long start = millis(); (millis() - start) < timeout;) { 67 | if (available()) { 68 | break; 69 | } 70 | } 71 | } 72 | 73 | int HCIUartTransportClass::available() 74 | { 75 | return _uart->available(); 76 | } 77 | 78 | int HCIUartTransportClass::peek() 79 | { 80 | return _uart->peek(); 81 | } 82 | 83 | int HCIUartTransportClass::read() 84 | { 85 | return _uart->read(); 86 | } 87 | 88 | size_t HCIUartTransportClass::write(const uint8_t* data, size_t length) 89 | { 90 | #ifdef ARDUINO_AVR_UNO_WIFI_REV2 91 | // wait while the CTS pin is low 92 | while (digitalRead(NINA_CTS) == HIGH); 93 | #endif 94 | 95 | size_t result = _uart->write(data, length); 96 | 97 | _uart->flush(); 98 | 99 | return result; 100 | } 101 | 102 | #if defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_NANO_RP2040_CONNECT) 103 | HCIUartTransportClass HCIUartTransport(SerialHCI, 119600); 104 | #else 105 | HCIUartTransportClass HCIUartTransport(SerialHCI, 912600); 106 | #endif 107 | HCITransportInterface& HCITransport = HCIUartTransport; 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /src/utility/HCIUartTransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _HCI_UART_TRANSPORT_H_ 21 | #define _HCI_UART_TRANSPORT_H_ 22 | 23 | #include "HCITransport.h" 24 | 25 | class HCIUartTransportClass : public HCITransportInterface { 26 | public: 27 | HCIUartTransportClass(HardwareSerial& uart, unsigned long baudrate); 28 | virtual ~HCIUartTransportClass(); 29 | 30 | virtual int begin(); 31 | virtual void end(); 32 | 33 | virtual void wait(unsigned long timeout); 34 | 35 | virtual int available(); 36 | virtual int peek(); 37 | virtual int read(); 38 | 39 | virtual size_t write(const uint8_t* data, size_t length); 40 | 41 | private: 42 | HardwareSerial* _uart; 43 | unsigned long _baudrate; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/utility/HCIVirtualTransport.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if defined(ESP32) 21 | 22 | #include "HCIVirtualTransport.h" 23 | 24 | StreamBufferHandle_t rec_buffer; 25 | StreamBufferHandle_t send_buffer; 26 | TaskHandle_t bleHandle; 27 | 28 | 29 | static void notify_host_send_available(void) 30 | { 31 | } 32 | 33 | static int notify_host_recv(uint8_t *data, uint16_t length) 34 | { 35 | xStreamBufferSend(rec_buffer,data,length,portMAX_DELAY); // !!!potentially waiting forever 36 | return 0; 37 | } 38 | 39 | static esp_vhci_host_callback_t vhci_host_cb = { 40 | notify_host_send_available, 41 | notify_host_recv 42 | }; 43 | 44 | void bleTask(void *pvParameters) 45 | { 46 | esp_vhci_host_register_callback(&vhci_host_cb); 47 | size_t length; 48 | uint8_t mybuf[258]; 49 | 50 | while(true){ 51 | length = xStreamBufferReceive(send_buffer,mybuf,258,portMAX_DELAY); 52 | while (!esp_vhci_host_check_send_available()) {} 53 | esp_vhci_host_send_packet(mybuf, length); 54 | } 55 | } 56 | 57 | 58 | HCIVirtualTransportClass::HCIVirtualTransportClass() 59 | { 60 | } 61 | 62 | HCIVirtualTransportClass::~HCIVirtualTransportClass() 63 | { 64 | } 65 | 66 | int HCIVirtualTransportClass::begin() 67 | { 68 | btStarted(); // this somehow stops the arduino ide from initializing bluedroid 69 | 70 | rec_buffer = xStreamBufferCreate(258, 1); 71 | send_buffer = xStreamBufferCreate(258, 1); 72 | 73 | esp_err_t ret = nvs_flash_init(); 74 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { 75 | ESP_ERROR_CHECK(nvs_flash_erase()); 76 | ret = nvs_flash_init(); 77 | } 78 | esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); 79 | 80 | #if CONFIG_IDF_TARGET_ESP32 81 | bt_cfg.mode = ESP_BT_MODE_BLE; //original esp32 chip 82 | #else 83 | #if !(CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2) 84 | bt_cfg.bluetooth_mode = ESP_BT_MODE_BLE; //different api for newer models 85 | #endif 86 | #endif 87 | 88 | esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); 89 | esp_bt_controller_init(&bt_cfg); 90 | esp_bt_controller_enable(ESP_BT_MODE_BLE); 91 | xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, &bleHandle, 0); 92 | return 1; 93 | } 94 | 95 | void HCIVirtualTransportClass::end() 96 | { 97 | vStreamBufferDelete(rec_buffer); 98 | vStreamBufferDelete(send_buffer); 99 | esp_bt_controller_disable(); 100 | esp_bt_controller_deinit(); 101 | vTaskDelete(bleHandle); 102 | } 103 | 104 | void HCIVirtualTransportClass::wait(unsigned long timeout) 105 | { 106 | for (unsigned long start = (esp_timer_get_time() / 1000ULL); ((esp_timer_get_time() / 1000ULL) - start) < timeout;) { 107 | if (available()) { 108 | break; 109 | } 110 | } 111 | } 112 | 113 | int HCIVirtualTransportClass::available() 114 | { 115 | size_t bytes = xStreamBufferBytesAvailable(rec_buffer); 116 | return bytes; 117 | } 118 | 119 | // never called 120 | int HCIVirtualTransportClass::peek() 121 | { 122 | return -1; 123 | } 124 | 125 | int HCIVirtualTransportClass::read() 126 | { 127 | uint8_t c; 128 | if(xStreamBufferReceive(rec_buffer, &c, 1, portMAX_DELAY)) { 129 | return c; 130 | } 131 | return -1; 132 | } 133 | 134 | size_t HCIVirtualTransportClass::write(const uint8_t* data, size_t length) 135 | { 136 | size_t result = xStreamBufferSend(send_buffer,data,length,portMAX_DELAY); 137 | return result; 138 | } 139 | 140 | HCIVirtualTransportClass HCIVirtualTransport; 141 | 142 | HCITransportInterface& HCITransport = HCIVirtualTransport; 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /src/utility/HCIVirtualTransport.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "HCITransport.h" 21 | #include 22 | #include 23 | #include 24 | 25 | #include "freertos/FreeRTOS.h" 26 | #include "freertos/task.h" 27 | #include "freertos/stream_buffer.h" 28 | 29 | #include "esp_bt.h" 30 | #include "nvs_flash.h" 31 | 32 | #include "esp32-hal-bt.h" // this is needed to disable bluedroid 33 | 34 | 35 | class HCIVirtualTransportClass : public HCITransportInterface { 36 | public: 37 | HCIVirtualTransportClass(); 38 | virtual ~HCIVirtualTransportClass(); 39 | 40 | virtual int begin(); 41 | virtual void end(); 42 | 43 | virtual void wait(unsigned long timeout); 44 | 45 | virtual int available(); 46 | virtual int peek(); 47 | virtual int read(); 48 | 49 | virtual size_t write(const uint8_t* data, size_t length); 50 | }; -------------------------------------------------------------------------------- /src/utility/HCIVirtualTransportAT.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if defined(ARDUINO_UNOR4_WIFI) 21 | 22 | #include "HCIVirtualTransportAT.h" 23 | 24 | extern ModemClass modem; 25 | 26 | HCIVirtualTransportATClass::HCIVirtualTransportATClass() 27 | { 28 | } 29 | 30 | HCIVirtualTransportATClass::~HCIVirtualTransportATClass() 31 | { 32 | } 33 | 34 | static RingBufferN<258> buf; 35 | 36 | int HCIVirtualTransportATClass::begin() 37 | { 38 | // TODO: add this helper 39 | //modem.debug(Serial); 40 | buf.clear(); 41 | //modem.debug(true); 42 | std::string res = ""; 43 | modem.begin(); 44 | if (modem.write(std::string(PROMPT(_HCI_BEGIN)), res, CMD(_HCI_BEGIN))) { 45 | return 1; 46 | } 47 | return 0; 48 | } 49 | 50 | void HCIVirtualTransportATClass::end() 51 | { 52 | } 53 | 54 | void HCIVirtualTransportATClass::wait(unsigned long timeout) 55 | { 56 | std::string res = ""; 57 | modem.write(std::string(PROMPT(_HCI_WAIT)), res, "%d\n\r", CMD_WRITE(_HCI_WAIT), timeout); 58 | } 59 | 60 | int HCIVirtualTransportATClass::available() 61 | { 62 | std::string res = ""; 63 | if (buf.available()) { 64 | return buf.available(); 65 | } 66 | if (modem.write(std::string(PROMPT(_HCI_AVAILABLE)), res, CMD_READ(_HCI_AVAILABLE))) { 67 | return atoi(res.c_str()); 68 | } 69 | 70 | return 0; 71 | } 72 | 73 | // never called 74 | int HCIVirtualTransportATClass::peek() 75 | { 76 | return -1; 77 | } 78 | 79 | int HCIVirtualTransportATClass::read() 80 | { 81 | uint8_t c; 82 | std::string res = ""; 83 | if (buf.available()) { 84 | return buf.read_char(); 85 | } 86 | modem.avoid_trim_results(); 87 | modem.read_using_size(); 88 | if (modem.write(std::string(PROMPT(_HCI_READ)), res, CMD(_HCI_READ))) { 89 | for(int i = 0; i < res.size(); i++) { 90 | buf.store_char((uint8_t)res[i]); 91 | } 92 | return buf.read_char(); 93 | } 94 | 95 | return -1; 96 | } 97 | 98 | size_t HCIVirtualTransportATClass::write(const uint8_t* data, size_t length) 99 | { 100 | std::string res = ""; 101 | modem.write_nowait(std::string(PROMPT(_HCI_WRITE)), res, "%s%d\r\n" , CMD_WRITE(_HCI_WRITE), length); 102 | if(modem.passthrough(data, length)) { 103 | return length; 104 | } 105 | return 0; 106 | } 107 | 108 | HCIVirtualTransportATClass HCIVirtualTransportAT; 109 | 110 | HCITransportInterface& HCITransport = HCIVirtualTransportAT; 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /src/utility/HCIVirtualTransportAT.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "HCITransport.h" 21 | #include 22 | #include 23 | #include 24 | 25 | #include "WiFiS3.h" 26 | 27 | class HCIVirtualTransportATClass : public HCITransportInterface { 28 | public: 29 | HCIVirtualTransportATClass(); 30 | virtual ~HCIVirtualTransportATClass(); 31 | 32 | virtual int begin(); 33 | virtual void end(); 34 | 35 | virtual void wait(unsigned long timeout); 36 | 37 | virtual int available(); 38 | virtual int peek(); 39 | virtual int read(); 40 | 41 | virtual size_t write(const uint8_t* data, size_t length); 42 | }; -------------------------------------------------------------------------------- /src/utility/L2CAPSignaling.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoBLE library. 3 | Copyright (c) 2018 Arduino SA. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef _L2CAP_SIGNALING_H_ 21 | #define _L2CAP_SIGNALING_H_ 22 | 23 | #include 24 | 25 | #define SIGNALING_CID 0x0005 26 | #define SECURITY_CID 0x0006 27 | 28 | 29 | #define CONNECTION_PAIRING_REQUEST 0x01 30 | #define CONNECTION_PAIRING_RESPONSE 0x02 31 | #define CONNECTION_PAIRING_CONFIRM 0x03 32 | #define CONNECTION_PAIRING_RANDOM 0x04 33 | #define CONNECTION_PAIRING_FAILED 0x05 34 | #define CONNECTION_ENCRYPTION_INFORMATION 0x06 35 | #define CONNECTION_MASTER_IDENTIFICATION 0x07 36 | #define CONNECTION_IDENTITY_INFORMATION 0x08 37 | #define CONNECTION_IDENTITY_ADDRESS 0x09 38 | #define CONNECTION_SIGNING_INFORMATION 0x0A 39 | #define CONNECTION_SECURITY_REQUEST 0x0B 40 | #define CONNECTION_PAIRING_PUBLIC_KEY 0x0C 41 | #define CONNECTION_PAIRING_DHKEY_CHECK 0x0D 42 | #define CONNECTION_PAIRING_KEYPRESS 0x0E 43 | 44 | #define IOCAP_DISPLAY_ONLY 0x00 45 | #define IOCAP_DISPLAY_YES_NO 0x01 46 | #define IOCAP_KEYBOARD_ONLY 0x02 47 | #define IOCAP_NO_INPUT_NO_OUTPUT 0x03 48 | #define IOCAP_KEYBOARD_DISPLAY 0x04 49 | 50 | 51 | #define LOCAL_AUTHREQ 0b00101101 52 | // #define LOCAL_IOCAP IOCAP_DISPLAY_ONLY // will use JustWorks pairing 53 | 54 | class L2CAPSignalingClass { 55 | public: 56 | L2CAPSignalingClass(); 57 | virtual ~L2CAPSignalingClass(); 58 | 59 | virtual void addConnection(uint16_t handle, uint8_t role, uint8_t peerBdaddrType, 60 | uint8_t peerBdaddr[6], uint16_t interval, 61 | uint16_t latency, uint16_t supervisionTimeout, 62 | uint8_t masterClockAccuracy); 63 | 64 | virtual void handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]); 65 | 66 | virtual void handleSecurityData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]); 67 | 68 | virtual void removeConnection(uint8_t handle, uint16_t reason); 69 | 70 | virtual void setConnectionInterval(uint16_t minInterval, uint16_t maxInterval); 71 | 72 | virtual void setSupervisionTimeout(uint16_t supervisionTimeout); 73 | 74 | virtual void setPairingEnabled(uint8_t enabled); 75 | virtual bool isPairingEnabled(); 76 | 77 | 78 | 79 | virtual void smCalculateLTKandConfirm(uint16_t handle, uint8_t expectedEa[]); 80 | 81 | 82 | private: 83 | virtual void connectionParameterUpdateRequest(uint16_t handle, uint8_t identifier, uint8_t dlen, uint8_t data[]); 84 | virtual void connectionParameterUpdateResponse(uint16_t handle, uint8_t identifier, uint8_t dlen, uint8_t data[]); 85 | 86 | 87 | private: 88 | uint16_t _minInterval; 89 | uint16_t _maxInterval; 90 | uint16_t _supervisionTimeout; 91 | uint8_t _pairing_enabled; 92 | }; 93 | 94 | extern L2CAPSignalingClass& L2CAPSignaling; 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/utility/bitDescriptions.cpp: -------------------------------------------------------------------------------- 1 | #include "bitDescriptions.h" 2 | 3 | 4 | #define BONDING_BIT 0b00000001 5 | #define MITM_BIT 0b00000100 6 | #define SC_BIT 0b00001000 7 | #define KEYPRESS_BIT 0b00010000 8 | #define CT2_BIT 0b00100000 9 | 10 | 11 | AuthReq::AuthReq(){} 12 | AuthReq::AuthReq(uint8_t octet):_octet(octet){} 13 | bool AuthReq::Bonding(){ return (_octet & BONDING_BIT)>0;} 14 | bool AuthReq::MITM(){ return (_octet & MITM_BIT)>0;} 15 | bool AuthReq::SC(){ return (_octet & SC_BIT)>0;} 16 | bool AuthReq::KeyPress(){ return (_octet & KEYPRESS_BIT)>0;} 17 | bool AuthReq::CT2(){ return (_octet & CT2_BIT)>0;} 18 | 19 | 20 | void AuthReq::setBonding(bool state) { _octet= state? _octet|BONDING_BIT : _octet&~BONDING_BIT;} 21 | void AuthReq::setMITM(bool state) { _octet= state? _octet|MITM_BIT : _octet&~MITM_BIT;} 22 | void AuthReq::setSC(bool state){ _octet= state? _octet|SC_BIT : _octet&~SC_BIT;} 23 | void AuthReq::setKeyPress(bool state){ _octet= state? _octet|KEYPRESS_BIT : _octet&~KEYPRESS_BIT;} 24 | void AuthReq::setCT2(bool state){ _octet= state? _octet|CT2_BIT : _octet&~CT2_BIT;} 25 | 26 | uint8_t _octet; 27 | 28 | 29 | void AuthReq::setOctet( uint8_t octet){_octet = octet;} 30 | uint8_t AuthReq::getOctet() {return _octet;} 31 | -------------------------------------------------------------------------------- /src/utility/bitDescriptions.h: -------------------------------------------------------------------------------- 1 | #ifndef _BIT_DESCRIPTIONS_H_ 2 | #define _BIT_DESCRIPTIONS_H_ 3 | #include 4 | 5 | class AuthReq{ 6 | public: 7 | AuthReq(); 8 | AuthReq(uint8_t octet); 9 | void setOctet( uint8_t octet); 10 | uint8_t getOctet(); 11 | 12 | 13 | // The Bonding_Flags field is a 2-bit field that indicates the type of bonding being requested by the initiating device 14 | bool Bonding(); 15 | // The MITM field is a 1-bit flag that is set to one if the device is requesting MITM protection 16 | bool MITM(); 17 | // The SC field is a 1 bit flag. If LE Secure Connections pairing is supported by the device, then the SC field shall be set to 1, otherwise it shall be set to 0. 18 | bool SC(); 19 | // The keypress field is a 1-bit flag that is used only in the Passkey Entry protocol and shall be ignored in other protocols. 20 | bool KeyPress(); 21 | // The CT2 field is a 1-bit flag that shall be set to 1 upon transmission to indicate support for the h7 function. 22 | bool CT2(); 23 | 24 | void setBonding(bool state); 25 | void setMITM(bool state); 26 | void setSC(bool state); 27 | void setKeyPress(bool state); 28 | void setCT2(bool state); 29 | private: 30 | uint8_t _octet; 31 | }; 32 | 33 | enum IOCap { 34 | DisplayOnly, 35 | DisplayYesNo, 36 | KeyboardOnly, 37 | NoInputNoOutput, 38 | KeyboardDisplay 39 | }; 40 | 41 | #endif -------------------------------------------------------------------------------- /src/utility/btct.h: -------------------------------------------------------------------------------- 1 | #ifndef _BTCT_H_ 2 | #define _BTCT_H_ 3 | #include 4 | 5 | // Implementation of functions defined in BTLE standard 6 | class BluetoothCryptoToolbox{ 7 | public: 8 | BluetoothCryptoToolbox(); 9 | void printBytes(uint8_t bytes[], uint8_t length); 10 | void generateSubkey(uint8_t* K, uint8_t* K1, uint8_t* K2); 11 | void AES_CMAC ( unsigned char *key, unsigned char *input, int length, 12 | unsigned char *mac ); 13 | int f5(uint8_t DHKey[],uint8_t N_master[], uint8_t N_slave[], 14 | uint8_t BD_ADDR_master[], uint8_t BD_ADDR_slave[], uint8_t MacKey[], uint8_t LTK[]); 15 | int f6(uint8_t W[], uint8_t N1[],uint8_t N2[],uint8_t R[], uint8_t IOCap[], uint8_t A1[], uint8_t A2[], uint8_t Ex[]); 16 | int g2(uint8_t U[], uint8_t V[], uint8_t X[], uint8_t Y[], uint8_t out[4]); 17 | int ah(uint8_t k[16], uint8_t r[3], uint8_t result[3]); 18 | void test(); 19 | void testF5(); 20 | void testF6(); 21 | void testAh(); 22 | void testg2(); 23 | private: 24 | int AES_128(uint8_t key[], uint8_t data_in[], uint8_t data_out[]); 25 | void leftshift_onebit(unsigned char *input,unsigned char *output); 26 | void xor_128(unsigned char *a, unsigned char *b, unsigned char *out); 27 | void padding ( unsigned char *lastb, unsigned char *pad, int length ); 28 | }; 29 | extern BluetoothCryptoToolbox btct; 30 | #endif -------------------------------------------------------------------------------- /src/utility/keyDistribution.cpp: -------------------------------------------------------------------------------- 1 | #include "keyDistribution.h" 2 | 3 | KeyDistribution::KeyDistribution():_octet(0){} 4 | KeyDistribution::KeyDistribution(uint8_t octet):_octet(octet){} 5 | 6 | #define ENCKEY 0b00000001 7 | #define IDKEY 0b00000010 8 | #define SIGNKEY 0b00000100 9 | #define LINKKEY 0b00001000 10 | void KeyDistribution::setOctet( uint8_t octet){_octet = octet;} 11 | uint8_t KeyDistribution::getOctet() {return _octet;} 12 | // Ignored when SMP is on LE transport 13 | bool KeyDistribution::EncKey(){ return (_octet & ENCKEY)>0;} 14 | // Device shall distribute IRK using Identity information command followed by its address using Identity address information 15 | bool KeyDistribution::IdKey(){ return (_octet & IDKEY)>0;} 16 | // Device shall distribute CSRK using signing information command 17 | bool KeyDistribution::SignKey(){ return (_octet & SIGNKEY)>0;} 18 | // Device would like to derive BR/EDR from LTK 19 | bool KeyDistribution::LinkKey(){ return (_octet & LINKKEY)>0;} 20 | 21 | void KeyDistribution::setEncKey(bool state) { _octet= state? _octet|ENCKEY : _octet&~ENCKEY;} 22 | void KeyDistribution::setIdKey(bool state) { _octet= state? _octet|IDKEY : _octet&~IDKEY;} 23 | void KeyDistribution::setSignKey(bool state){ _octet= state? _octet|SIGNKEY : _octet&~SIGNKEY;} 24 | void KeyDistribution::setLinkKey(bool state){ _octet= state? _octet|LINKKEY : _octet&~LINKKEY;} -------------------------------------------------------------------------------- /src/utility/keyDistribution.h: -------------------------------------------------------------------------------- 1 | #ifndef _KEY_DISTRIBUTION_H_ 2 | #define _KEY_DISTRIBUTION_H_ 3 | #include 4 | 5 | class KeyDistribution{ 6 | public: 7 | KeyDistribution(); 8 | KeyDistribution(uint8_t octet); 9 | void setOctet( uint8_t octet); 10 | uint8_t getOctet(); 11 | // Ignored when SMP is on LE transport 12 | bool EncKey(); 13 | // Device shall distribute IRK using Identity information command followed by its address using Identity address information 14 | bool IdKey(); 15 | // Device shall distribute CSRK using signing information command 16 | bool SignKey(); 17 | // Device would like to derive BR/EDR from LTK 18 | bool LinkKey(); 19 | 20 | void setEncKey(bool state); 21 | void setIdKey(bool state); 22 | void setSignKey(bool state); 23 | void setLinkKey(bool state); 24 | private: 25 | uint8_t _octet; 26 | // 1. IRK by the slave2. BD ADDR by the slave3. CSRK by the slave4. IRK by the master5. BD_ADDR by the master6. CSRK by the master 27 | }; 28 | 29 | #endif --------------------------------------------------------------------------------