2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEAdvertising.h
|
|
|
|
*
|
|
|
|
* Created: on March 3, 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEAdvertising.h
|
|
|
|
*
|
|
|
|
* Created on: Jun 21, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MAIN_BLEADVERTISING_H_
|
|
|
|
#define MAIN_BLEADVERTISING_H_
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2022-04-10 18:21:45 +02:00
|
|
|
#if (defined(CONFIG_BT_ENABLED) && \
|
|
|
|
defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && \
|
|
|
|
!CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "host/ble_gap.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#else
|
|
|
|
#include "nimble/nimble/host/include/host/ble_gap.h"
|
|
|
|
#endif
|
|
|
|
|
2020-04-01 04:16:27 +02:00
|
|
|
/**** FIX COMPILATION ****/
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
/**************************/
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
#include "NimBLEUUID.h"
|
2023-04-06 20:04:40 +02:00
|
|
|
#include "NimBLEAddress.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-06-04 15:12:20 +02:00
|
|
|
#include <functional>
|
2020-03-30 01:44:20 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/* COMPATIBILITY - DO NOT USE */
|
|
|
|
#define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
|
|
|
|
#define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
|
|
|
|
#define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
|
|
|
|
#define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
|
|
|
|
#define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
|
|
|
|
#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
|
|
|
|
/* ************************* */
|
|
|
|
|
2024-06-04 15:12:20 +02:00
|
|
|
class NimBLEAdvertising;
|
|
|
|
|
|
|
|
typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Advertisement data set by the programmer to be published by the %BLE server.
|
|
|
|
*/
|
|
|
|
class NimBLEAdvertisementData {
|
2020-05-14 06:03:56 +02:00
|
|
|
// Only a subset of the possible BLE architected advertisement fields are currently exposed. Others will
|
|
|
|
// be exposed on demand/request or as time permits.
|
|
|
|
//
|
2020-03-30 01:44:20 +02:00
|
|
|
public:
|
2020-05-14 06:03:56 +02:00
|
|
|
void setAppearance(uint16_t appearance);
|
|
|
|
void setCompleteServices(const NimBLEUUID &uuid);
|
2021-02-06 21:27:12 +01:00
|
|
|
void setCompleteServices16(const std::vector<NimBLEUUID> &v_uuid);
|
|
|
|
void setCompleteServices32(const std::vector<NimBLEUUID> &v_uuid);
|
2020-05-14 06:03:56 +02:00
|
|
|
void setFlags(uint8_t);
|
|
|
|
void setManufacturerData(const std::string &data);
|
2023-05-29 00:05:31 +02:00
|
|
|
void setManufacturerData(const std::vector<uint8_t> &data);
|
2021-02-08 04:33:57 +01:00
|
|
|
void setURI(const std::string &uri);
|
2020-05-14 06:03:56 +02:00
|
|
|
void setName(const std::string &name);
|
|
|
|
void setPartialServices(const NimBLEUUID &uuid);
|
2021-02-06 21:27:12 +01:00
|
|
|
void setPartialServices16(const std::vector<NimBLEUUID> &v_uuid);
|
|
|
|
void setPartialServices32(const std::vector<NimBLEUUID> &v_uuid);
|
2020-05-14 06:03:56 +02:00
|
|
|
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
|
|
|
void setShortName(const std::string &name);
|
|
|
|
void addData(const std::string &data); // Add data to the payload.
|
2020-07-28 05:38:22 +02:00
|
|
|
void addData(char * data, size_t length);
|
2021-02-08 04:33:57 +01:00
|
|
|
void addTxPower();
|
|
|
|
void setPreferredParams(uint16_t min, uint16_t max);
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string getPayload(); // Retrieve the current advert payload.
|
2024-06-13 23:02:32 +02:00
|
|
|
void clearData(); // Clear the advertisement data.
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
2020-05-14 06:03:56 +02:00
|
|
|
friend class NimBLEAdvertising;
|
2021-02-06 21:27:12 +01:00
|
|
|
void setServices(const bool complete, const uint8_t size,
|
|
|
|
const std::vector<NimBLEUUID> &v_uuid);
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string m_payload; // The payload of the advertisement.
|
2020-03-30 01:44:20 +02:00
|
|
|
}; // NimBLEAdvertisementData
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform and manage %BLE advertising.
|
|
|
|
*
|
|
|
|
* A %BLE server will want to perform advertising in order to make itself known to %BLE clients.
|
|
|
|
*/
|
|
|
|
class NimBLEAdvertising {
|
|
|
|
public:
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEAdvertising();
|
|
|
|
void addServiceUUID(const NimBLEUUID &serviceUUID);
|
|
|
|
void addServiceUUID(const char* serviceUUID);
|
2020-07-14 05:24:07 +02:00
|
|
|
void removeServiceUUID(const NimBLEUUID &serviceUUID);
|
2024-06-04 15:12:20 +02:00
|
|
|
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
|
2024-06-04 02:27:25 +02:00
|
|
|
void removeServices();
|
2022-04-10 18:21:45 +02:00
|
|
|
bool stop();
|
2020-05-14 06:03:56 +02:00
|
|
|
void setAppearance(uint16_t appearance);
|
2021-02-08 04:33:57 +01:00
|
|
|
void setName(const std::string &name);
|
|
|
|
void setManufacturerData(const std::string &data);
|
2023-05-29 00:05:31 +02:00
|
|
|
void setManufacturerData(const std::vector<uint8_t> &data);
|
2021-02-08 04:33:57 +01:00
|
|
|
void setURI(const std::string &uri);
|
|
|
|
void setServiceData(const NimBLEUUID &uuid, const std::string &data);
|
2020-03-30 01:44:20 +02:00
|
|
|
void setAdvertisementType(uint8_t adv_type);
|
2020-05-14 06:03:56 +02:00
|
|
|
void setMaxInterval(uint16_t maxinterval);
|
|
|
|
void setMinInterval(uint16_t mininterval);
|
|
|
|
void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
|
2020-09-14 04:02:15 +02:00
|
|
|
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
2020-05-14 06:03:56 +02:00
|
|
|
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
|
|
|
|
void setScanResponse(bool);
|
2020-12-27 23:25:38 +01:00
|
|
|
void setMinPreferred(uint16_t);
|
|
|
|
void setMaxPreferred(uint16_t);
|
2021-02-08 04:33:57 +01:00
|
|
|
void addTxPower();
|
|
|
|
void reset();
|
2020-09-14 04:36:59 +02:00
|
|
|
void advCompleteCB();
|
|
|
|
bool isAdvertising();
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class NimBLEDevice;
|
2022-04-10 18:21:45 +02:00
|
|
|
friend class NimBLEServer;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-13 04:42:19 +01:00
|
|
|
void onHostSync();
|
2020-09-14 04:36:59 +02:00
|
|
|
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-09-14 04:36:59 +02:00
|
|
|
ble_hs_adv_fields m_advData;
|
|
|
|
ble_hs_adv_fields m_scanData;
|
|
|
|
ble_gap_adv_params m_advParams;
|
2020-05-14 06:03:56 +02:00
|
|
|
std::vector<NimBLEUUID> m_serviceUUIDs;
|
2020-09-14 04:36:59 +02:00
|
|
|
bool m_customAdvData;
|
|
|
|
bool m_customScanResponseData;
|
|
|
|
bool m_scanResp;
|
|
|
|
bool m_advDataSet;
|
2024-06-04 15:12:20 +02:00
|
|
|
advCompleteCB_t m_advCompCB{nullptr};
|
2020-12-27 23:25:38 +01:00
|
|
|
uint8_t m_slaveItvl[4];
|
2021-01-13 04:42:19 +01:00
|
|
|
uint32_t m_duration;
|
2021-02-08 04:33:57 +01:00
|
|
|
std::vector<uint8_t> m_svcData16;
|
|
|
|
std::vector<uint8_t> m_svcData32;
|
|
|
|
std::vector<uint8_t> m_svcData128;
|
|
|
|
std::vector<uint8_t> m_name;
|
|
|
|
std::vector<uint8_t> m_mfgData;
|
|
|
|
std::vector<uint8_t> m_uri;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
|
2020-05-10 15:21:46 +02:00
|
|
|
#endif /* MAIN_BLEADVERTISING_H_ */
|