本项止转自github官方arduino-esp32 传在这里仅为阅读源码方便
projectuser
2019-07-05 50148ccffe21ff54262064ec9f2245900eaf18aa
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
/*
 * BLERemoteCharacteristic.h
 *
 *  Created on: Jul 8, 2017
 *      Author: kolban
 */
 
#ifndef COMPONENTS_CPP_UTILS_BLEREMOTECHARACTERISTIC_H_
#define COMPONENTS_CPP_UTILS_BLEREMOTECHARACTERISTIC_H_
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
 
#include <string>
 
#include <esp_gattc_api.h>
 
#include "BLERemoteService.h"
#include "BLERemoteDescriptor.h"
#include "BLEUUID.h"
#include "FreeRTOS.h"
 
class BLERemoteService;
class BLERemoteDescriptor;
typedef void (*notify_callback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
 
/**
 * @brief A model of a remote %BLE characteristic.
 */
class BLERemoteCharacteristic {
public:
    ~BLERemoteCharacteristic();
 
    // Public member functions
    bool        canBroadcast();
    bool        canIndicate();
    bool        canNotify();
    bool        canRead();
    bool        canWrite();
    bool        canWriteNoResponse();
    BLERemoteDescriptor* getDescriptor(BLEUUID uuid);
    std::map<std::string, BLERemoteDescriptor*>* getDescriptors();
    uint16_t    getHandle();
    BLEUUID     getUUID();
    std::string readValue();
    uint8_t     readUInt8();
    uint16_t    readUInt16();
    uint32_t    readUInt32();
    void        registerForNotify(notify_callback _callback, bool notifications = true);
    void        writeValue(uint8_t* data, size_t length, bool response = false);
    void        writeValue(std::string newValue, bool response = false);
    void        writeValue(uint8_t newValue, bool response = false);
    std::string toString();
    uint8_t*    readRawData();
 
private:
    BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
    friend class BLEClient;
    friend class BLERemoteService;
    friend class BLERemoteDescriptor;
 
    // Private member functions
    void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
 
    BLERemoteService* getRemoteService();
    void              removeDescriptors();
    void              retrieveDescriptors();
 
    // Private properties
    BLEUUID              m_uuid;
    esp_gatt_char_prop_t m_charProp;
    uint16_t             m_handle;
    BLERemoteService*    m_pRemoteService;
    FreeRTOS::Semaphore  m_semaphoreReadCharEvt      = FreeRTOS::Semaphore("ReadCharEvt");
    FreeRTOS::Semaphore  m_semaphoreRegForNotifyEvt  = FreeRTOS::Semaphore("RegForNotifyEvt");
    FreeRTOS::Semaphore  m_semaphoreWriteCharEvt     = FreeRTOS::Semaphore("WriteCharEvt");
    std::string          m_value;
    uint8_t              *m_rawData;
    notify_callback         m_notifyCallback;
 
    // We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
    std::map<std::string, BLERemoteDescriptor*> m_descriptorMap;
}; // BLERemoteCharacteristic
#endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_CPP_UTILS_BLEREMOTECHARACTERISTIC_H_ */