/* This file is part of the ArduinoBLE library. Copyright (c) 2018 Arduino SA. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "utility/ATT.h" #include "utility/HCI.h" #include "utility/GAP.h" #include "utility/GATT.h" #include "utility/L2CAPSignaling.h" #include "BLELocalDevice.h" #if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) #ifndef BT_REG_ON #define BT_REG_ON PJ_12 #endif #elif defined(ARDUINO_NICLA_VISION) #ifndef BT_REG_ON #define BT_REG_ON PF_14 #endif #elif defined(ARDUINO_GIGA) #ifndef BT_REG_ON #define BT_REG_ON PA_10 #endif #endif BLELocalDevice::BLELocalDevice() { _advertisingData.setFlags(BLEFlagsGeneralDiscoverable | BLEFlagsBREDRNotSupported); } BLELocalDevice::~BLELocalDevice() { } int BLELocalDevice::begin() { #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT) // reset the NINA in BLE mode pinMode(SPIWIFI_SS, OUTPUT); pinMode(NINA_RESETN, OUTPUT); digitalWrite(SPIWIFI_SS, LOW); #endif #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) digitalWrite(NINA_RESETN, HIGH); delay(100); digitalWrite(NINA_RESETN, LOW); delay(750); #elif defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT) // inverted reset digitalWrite(NINA_RESETN, LOW); delay(100); digitalWrite(NINA_RESETN, HIGH); delay(750); #elif defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA) // BT_REG_ON -> HIGH pinMode(BT_REG_ON, OUTPUT); digitalWrite(BT_REG_ON, HIGH); #endif #ifdef ARDUINO_AVR_UNO_WIFI_REV2 // set SS HIGH digitalWrite(SPIWIFI_SS, HIGH); // set RTS HIGH pinMode(NINA_RTS, OUTPUT); digitalWrite(NINA_RTS, HIGH); // set CTS as input pinMode(NINA_CTS, INPUT); #endif if (!HCI.begin()) { end(); return 0; } delay(100); if (HCI.reset() != 0) { end(); return 0; } uint8_t hciVer; uint16_t hciRev; uint8_t lmpVer; uint16_t manufacturer; uint16_t lmpSubVer; if (HCI.readLocalVersion(hciVer, hciRev, lmpVer, manufacturer, lmpSubVer) != 0) { end(); return 0; } if (HCI.setEventMask(0x3FFFFFFFFFFFFFFF) != 0) { end(); return 0; } if (HCI.setLeEventMask(0x00000000000003FF) != 0) { end(); return 0; } uint16_t pktLen; uint8_t maxPkt; if (HCI.readLeBufferSize(pktLen, maxPkt) != 0) { end(); return 0; } /// The HCI should allow automatic address resolution. // // If we have callbacks to remember bonded devices: // if(HCI._getIRKs!=0){ // uint8_t nIRKs = 0; // uint8_t** BADDR_Type = new uint8_t*; // uint8_t*** BADDRs = new uint8_t**; // uint8_t*** IRKs = new uint8_t**; // uint8_t* memcheck; // if(!HCI._getIRKs(&nIRKs, BADDR_Type, BADDRs, IRKs)){ // Serial.println("error"); // } // for(int i=0; i