2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEServer.h
|
|
|
|
*
|
|
|
|
* Created: on March 2, 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEServer.h
|
|
|
|
*
|
|
|
|
* Created on: Apr 16, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MAIN_NIMBLESERVER_H_
|
|
|
|
#define MAIN_NIMBLESERVER_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_PERIPHERAL)
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAddress.h"
|
|
|
|
#include "NimBLEUUID.h"
|
|
|
|
#include "NimBLEAdvertising.h"
|
|
|
|
#include "NimBLEService.h"
|
|
|
|
#include "NimBLESecurity.h"
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
class NimBLEService;
|
|
|
|
class NimBLECharacteristic;
|
|
|
|
class NimBLEServerCallbacks;
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
/* TODO possibly refactor this struct */
|
2020-03-30 01:44:20 +02:00
|
|
|
typedef struct {
|
|
|
|
void *peer_device; // peer device BLEClient or BLEServer - maybe its better to have 2 structures or union here
|
|
|
|
bool connected; // do we need it?
|
|
|
|
uint16_t mtu; // every peer device negotiate own mtu
|
|
|
|
} conn_status_t;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A data structure that manages the %BLE servers owned by a BLE server.
|
|
|
|
*/
|
|
|
|
class NimBLEServiceMap {
|
|
|
|
public:
|
|
|
|
// NimBLEService* getByHandle(uint16_t handle);
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEService* getByUUID(const char* uuid);
|
2020-05-10 15:21:46 +02:00
|
|
|
NimBLEService* getByUUID(const NimBLEUUID &uuid, uint8_t inst_id = 0);
|
2020-03-30 01:44:20 +02:00
|
|
|
// void setByHandle(uint16_t handle, NimBLEService* service);
|
|
|
|
void setByUUID(const char* uuid, NimBLEService* service);
|
2020-05-10 15:21:46 +02:00
|
|
|
void setByUUID(const NimBLEUUID &uuid, NimBLEService* service);
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string toString();
|
|
|
|
NimBLEService* getFirst();
|
|
|
|
NimBLEService* getNext();
|
|
|
|
void removeService(NimBLEService *service);
|
|
|
|
int getRegisteredServiceCount();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// std::map<uint16_t, NimBLEService*> m_handleMap;
|
|
|
|
std::map<NimBLEService*, std::string> m_uuidMap;
|
|
|
|
std::map<NimBLEService*, std::string>::iterator m_iterator;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The model of a %BLE server.
|
|
|
|
*/
|
|
|
|
class NimBLEServer {
|
|
|
|
public:
|
|
|
|
uint32_t getConnectedCount();
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEService* createService(const char* uuid);
|
2020-05-10 15:21:46 +02:00
|
|
|
NimBLEService* createService(const NimBLEUUID &uuid, uint32_t numHandles=15, uint8_t inst_id=0);
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEAdvertising* getAdvertising();
|
|
|
|
void setCallbacks(NimBLEServerCallbacks* pCallbacks);
|
|
|
|
void startAdvertising();
|
|
|
|
void stopAdvertising();
|
|
|
|
void start();
|
|
|
|
// void removeService(BLEService* service);
|
|
|
|
NimBLEService* getServiceByUUID(const char* uuid);
|
2020-05-10 15:21:46 +02:00
|
|
|
NimBLEService* getServiceByUUID(const NimBLEUUID &uuid);
|
2020-03-30 01:44:20 +02:00
|
|
|
int disconnect(uint16_t connID, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
|
|
|
|
// bool connect(BLEAddress address);
|
2020-05-14 06:03:56 +02:00
|
|
|
void updateConnParams(uint16_t conn_handle,
|
|
|
|
uint16_t minInterval, uint16_t maxInterval,
|
2020-03-30 01:44:20 +02:00
|
|
|
uint16_t latency, uint16_t timeout,
|
|
|
|
uint16_t minConnTime=0, uint16_t maxConnTime=0);
|
|
|
|
|
|
|
|
/* multi connection support */
|
|
|
|
std::map<uint16_t, conn_status_t> getPeerDevices();
|
|
|
|
void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
|
|
|
|
void removePeerDevice(uint16_t conn_id, bool client);
|
|
|
|
NimBLEServer* getServerByConnId(uint16_t conn_id);
|
|
|
|
void updatePeerMTU(uint16_t connId, uint16_t mtu);
|
|
|
|
uint16_t getPeerMTU(uint16_t conn_id);
|
|
|
|
uint16_t getConnId();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
NimBLEServer();
|
|
|
|
//friend class BLEService;
|
|
|
|
friend class NimBLECharacteristic;
|
|
|
|
friend class NimBLEDevice;
|
|
|
|
friend class NimBLEAdvertising;
|
|
|
|
// void onHostReset();
|
|
|
|
// BLEAdvertising m_bleAdvertising;
|
|
|
|
uint16_t m_connId;
|
|
|
|
uint16_t m_svcChgChrHdl;
|
2020-05-14 06:03:56 +02:00
|
|
|
bool m_gattsStarted;
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
std::map<uint16_t, conn_status_t> m_connectedServersMap;
|
|
|
|
std::map<uint16_t, NimBLECharacteristic*> m_notifyChrMap;
|
|
|
|
|
|
|
|
NimBLEServiceMap m_serviceMap;
|
|
|
|
NimBLEServerCallbacks* m_pServerCallbacks;
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
2020-03-30 01:44:20 +02:00
|
|
|
}; // NimBLEServer
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callbacks associated with the operation of a %BLE server.
|
|
|
|
*/
|
|
|
|
class NimBLEServerCallbacks {
|
|
|
|
public:
|
|
|
|
virtual ~NimBLEServerCallbacks() {};
|
|
|
|
/**
|
|
|
|
* @brief Handle a new client connection.
|
|
|
|
*
|
|
|
|
* When a new client connects, we are invoked.
|
|
|
|
*
|
|
|
|
* @param [in] pServer A reference to the %BLE server that received the client connection.
|
|
|
|
*/
|
|
|
|
virtual void onConnect(NimBLEServer* pServer);
|
|
|
|
virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
|
|
|
|
/**
|
|
|
|
* @brief Handle an existing client disconnection.
|
|
|
|
*
|
|
|
|
* When an existing client disconnects, we are invoked.
|
|
|
|
*
|
|
|
|
* @param [in] pServer A reference to the %BLE server that received the existing client disconnection.
|
|
|
|
*/
|
|
|
|
virtual void onDisconnect(NimBLEServer* pServer);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
virtual uint32_t onPassKeyRequest(); //{return 0;}
|
|
|
|
virtual void onPassKeyNotify(uint32_t pass_key); //{}
|
|
|
|
virtual bool onSecurityRequest(); //{return true;}
|
|
|
|
virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);//{};
|
|
|
|
virtual bool onConfirmPIN(uint32_t pin);//{return true;}
|
|
|
|
}; // BLEServerCallbacks
|
|
|
|
|
|
|
|
|
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_NIMBLESERVER_H_ */
|