|
| 1 | +/* |
| 2 | + This file is part of the Arduino_RouterBridge library. |
| 3 | +
|
| 4 | + Copyright (c) 2025 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +
|
| 10 | +*/ |
| 11 | + |
| 12 | +#include <Arduino_RouterBridge.h> |
| 13 | + |
| 14 | +void setup() { |
| 15 | + Serial.begin(115200); |
| 16 | + while (!Serial) { |
| 17 | + delay(10); |
| 18 | + } |
| 19 | + |
| 20 | + Serial.println("Arduino HCI Example - Read Local Version"); |
| 21 | + |
| 22 | + if (!Bridge.begin()) { |
| 23 | + Serial.println("Failed to setup Bridge"); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (!HCI.begin("hci0")) { |
| 28 | + Serial.println("Failed to open HCI device"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + Serial.println("HCI device opened successfully"); |
| 33 | + |
| 34 | + delay(1000); |
| 35 | + readLocalVersion(); |
| 36 | +} |
| 37 | + |
| 38 | +void loop() { |
| 39 | + // Nothing to do in loop |
| 40 | + delay(1000); |
| 41 | +} |
| 42 | + |
| 43 | +void readLocalVersion() { |
| 44 | + uint8_t cmd[4]; |
| 45 | + cmd[0] = 0x01; // HCI Command Packet Type |
| 46 | + cmd[1] = 0x01; // OCF = 0x0001 (lower byte) |
| 47 | + cmd[2] = 0x10; // OGF = 0x04 (0x04 << 2 = 0x10 in upper 6 bits) |
| 48 | + cmd[3] = 0x00; // Parameter length = 0 |
| 49 | + |
| 50 | + Serial.println("Sending HCI Read Local Version command..."); |
| 51 | + |
| 52 | + int sent = HCI.send(cmd, sizeof(cmd)); |
| 53 | + if (sent < 0) { |
| 54 | + Serial.println("Failed to send HCI command"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + Serial.print("Sent "); |
| 59 | + Serial.print(sent); |
| 60 | + Serial.println(" bytes"); |
| 61 | + |
| 62 | + // Wait for response with timeout |
| 63 | + Serial.println("Waiting for response..."); |
| 64 | + int avail = 0; |
| 65 | + unsigned long startTime = millis(); |
| 66 | + while (avail == 0 && (millis() - startTime) < 1000) { // 1 second timeout |
| 67 | + avail = HCI.available(); |
| 68 | + if (avail == 0) { |
| 69 | + delay(10); // Small delay between polls |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Serial.print("Available bytes: "); |
| 74 | + Serial.println(avail); |
| 75 | + |
| 76 | + if (avail == 0) { |
| 77 | + Serial.println("Timeout: No response received"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Read response |
| 82 | + uint8_t response[255]; |
| 83 | + int received = HCI.recv(response, sizeof(response)); |
| 84 | + |
| 85 | + if (received > 0) { |
| 86 | + Serial.print("Received "); |
| 87 | + Serial.print(received); |
| 88 | + Serial.println(" bytes:"); |
| 89 | + |
| 90 | + // Print response in hex |
| 91 | + for (int i = 0; i < received; i++) { |
| 92 | + if (response[i] < 0x10) Serial.print("0"); |
| 93 | + Serial.print(response[i], HEX); |
| 94 | + Serial.print(" "); |
| 95 | + } |
| 96 | + Serial.println(); |
| 97 | + |
| 98 | + // Parse Command Complete Event |
| 99 | + // Event format: Packet Type, Event Code, Param Length, Num_HCI_Command_Packets, Opcode, Status, ... |
| 100 | + if (received >= 6 && response[0] == 0x04 && response[1] == 0x0E) { |
| 101 | + Serial.println("Command Complete Event received"); |
| 102 | + Serial.print("Status: 0x"); |
| 103 | + Serial.println(response[6], HEX); |
| 104 | + } |
| 105 | + } else { |
| 106 | + Serial.println("No response received"); |
| 107 | + } |
| 108 | +} |
0 commit comments