summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEServer.h
blob: d39d8bfee1a3163b9a0d36153e27dbaf50495ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * BLEServer.h
 *
 *  Created on: Apr 16, 2017
 *      Author: kolban
 */
 
#ifndef COMPONENTS_CPP_UTILS_BLESERVER_H_
#define COMPONENTS_CPP_UTILS_BLESERVER_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <esp_gatts_api.h>
 
#include <string>
#include <string.h>
// #include "BLEDevice.h" 
 
#include "BLEUUID.h"
#include "BLEAdvertising.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
#include "BLESecurity.h"
#include "FreeRTOS.h"
#include "BLEAddress.h"
 
class BLEServerCallbacks;
/* TODO possibly refactor this struct */ 
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 BLEServiceMap {
public:
BLEService* getByHandle(uint16_t handle);
BLEService* getByUUID(const char* uuid);
BLEService* getByUUID(BLEUUID uuid, uint8_t inst_id = 0);
void        handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param);
void        setByHandle(uint16_t handle, BLEService* service);
void        setByUUID(const char* uuid, BLEService* service);
void        setByUUID(BLEUUID uuid, BLEService* service);
std::string toString();
BLEService* getFirst();
BLEService* getNext();
void  removeService(BLEService *service);
int  getRegisteredServiceCount();
 
private:
std::map<uint16_t, BLEService*>    m_handleMap;
std::map<BLEService*, std::string> m_uuidMap;
std::map<BLEService*, std::string>::iterator m_iterator;
};
 
 
/**
 * @brief The model of a %BLE server.
 */
class BLEServer {
public:
uint32_t        getConnectedCount();
BLEService*     createService(const char* uuid);
BLEService*     createService(BLEUUID uuid, uint32_t numHandles=15uint8_t inst_id=0);
BLEAdvertising* getAdvertising();
void            setCallbacks(BLEServerCallbacks* pCallbacks);
void            startAdvertising();
void  removeService(BLEService* service);
BLEService*  getServiceByUUID(const char* uuid);
BLEService*  getServiceByUUID(BLEUUID uuid);
bool  connect(BLEAddress address);
uint16_t m_appId;
void updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
 
/* multi connection support */
std::map<uint16_tconn_status_t> getPeerDevices(bool client);
void addPeerDevice(void* peer, bool is_client, uint16_t conn_id);
void removePeerDevice(uint16_t conn_id, bool client);
BLEServer* getServerByConnId(uint16_t conn_id);
void updatePeerMTU(uint16_t connId, uint16_t mtu);
uint16_t getPeerMTU(uint16_t conn_id);
uint16_t        getConnId();
 
 
private:
BLEServer();
friend class BLEService;
friend class BLECharacteristic;
friend class BLEDevice;
esp_ble_adv_data_t  m_adv_data;
// BLEAdvertising      m_bleAdvertising; 
uint16_t m_connId;
uint32_t            m_connectedCount;
uint16_t            m_gatts_if;
   std::map<uint16_tconn_status_t> m_connectedServersMap;
 
FreeRTOS::Semaphore m_semaphoreRegisterAppEvt  = FreeRTOS::Semaphore("RegisterAppEvt");
FreeRTOS::Semaphore m_semaphoreCreateEvt  = FreeRTOS::Semaphore("CreateEvt");
FreeRTOS::Semaphore m_semaphoreOpenEvt    = FreeRTOS::Semaphore("OpenEvt");
BLEServiceMap       m_serviceMap;
BLEServerCallbacks* m_pServerCallbacks = nullptr;
 
void            createApp(uint16_t appId);
uint16_t        getGattsIf();
void            handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
void            registerApp(uint16_t);
}; // BLEServer 
 
 
/**
 * @brief Callbacks associated with the operation of a %BLE server.
 */
class BLEServerCallbacks {
public:
virtual ~BLEServerCallbacks() {};
/**
 * @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(BLEServer* pServer);
virtual void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param);
/**
 * @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(BLEServer* pServer);
}// BLEServerCallbacks 
 
 
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_CPP_UTILS_BLESERVER_H_ */