summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEServer.h
blob: 95c55d5370b61cedc45435d127561a63ffd3cde0 (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
/*
 * 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 "BLEUUID.h"
#include "BLEAdvertising.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
#include "BLESecurity.h"
#include "FreeRTOS.h"
 
class BLEServerCallbacks;
 
 
/**
 * @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);
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);
 
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);
 
 
private:
BLEServer();
friend class BLEService;
friend class BLECharacteristic;
friend class BLEDevice;
esp_ble_adv_data_t  m_adv_data;
uint16_t            m_appId;
BLEAdvertising      m_bleAdvertising;
  uint16_t m_connId;
  uint32_t            m_connectedCount;
  uint16_t            m_gatts_if;
FreeRTOS::Semaphore m_semaphoreRegisterAppEvt = FreeRTOS::Semaphore("RegisterAppEvt");
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
BLEServiceMap       m_serviceMap;
BLEServerCallbacks* m_pServerCallbacks;
 
void            createApp(uint16_t appId);
uint16_t        getConnId();
uint16_t        getGattsIf();
void            handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
void            handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
void            registerApp();
}; // 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);
 
/**
 * @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_ */