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_
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "host/ble_gap.h"
|
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"
|
|
|
|
|
|
|
|
#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 )
|
|
|
|
/* ************************* */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
void setFlags(uint8_t);
|
|
|
|
void setManufacturerData(const std::string &data);
|
|
|
|
void setName(const std::string &name);
|
|
|
|
void setPartialServices(const NimBLEUUID &uuid);
|
|
|
|
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);
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string getPayload(); // Retrieve the current advert payload.
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
2020-05-14 06:03:56 +02:00
|
|
|
friend class NimBLEAdvertising;
|
|
|
|
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);
|
2021-01-14 03:48:38 +01:00
|
|
|
bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
|
2020-05-14 06:03:56 +02:00
|
|
|
void stop();
|
|
|
|
void setAppearance(uint16_t appearance);
|
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);
|
2020-09-14 04:36:59 +02:00
|
|
|
void advCompleteCB();
|
|
|
|
bool isAdvertising();
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class NimBLEDevice;
|
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;
|
2020-12-27 23:25:38 +01:00
|
|
|
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
|
|
|
|
uint8_t m_slaveItvl[4];
|
2021-01-13 04:42:19 +01:00
|
|
|
uint32_t m_duration;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED */
|
2020-05-10 15:21:46 +02:00
|
|
|
#endif /* MAIN_BLEADVERTISING_H_ */
|