-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGattServerSt.cpp
137 lines (112 loc) · 5.06 KB
/
GattServerSt.cpp
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
#include "hal_st/middlewares/ble_middleware/GattServerSt.hpp"
extern "C"
{
#include "ble/core/ble_core.h"
}
namespace
{
uint8_t UuidToType(const services::AttAttribute::Uuid& uuid)
{
if (uuid.Is<services::AttAttribute::Uuid16>())
return 0x01;
else if (uuid.Is<services::AttAttribute::Uuid128>())
return 0x02;
std::abort(); // Unsupported uuid type
}
template<class UuidType>
const auto* ConvertUuid(const services::AttAttribute::Uuid& uuid)
{
if (uuid.Is<services::AttAttribute::Uuid16>())
return reinterpret_cast<const UuidType*>(&uuid.Get<services::AttAttribute::Uuid16>());
else if (uuid.Is<services::AttAttribute::Uuid128>())
return reinterpret_cast<const UuidType*>(&uuid.Get<services::AttAttribute::Uuid128>());
std::abort(); // Unsupported uuid type
}
uint8_t ConvertProperties(services::GattCharacteristic::PropertyFlags properties)
{
return infra::enum_cast(properties);
}
uint8_t ConvertPermissions(services::GattServerCharacteristic::PermissionFlags permissions)
{
return infra::enum_cast(permissions);
}
}
namespace hal
{
GattServerSt::GattServerSt(hal::HciEventSource& hciEventSource)
: hal::HciEventSink(hciEventSource)
{}
void GattServerSt::AddService(services::GattServerService& service)
{
constexpr uint8_t gattPrimaryService = 0x01;
auto result = aci_gatt_add_service(UuidToType(service.Type()), ConvertUuid<Service_UUID_t>(service.Type()),
gattPrimaryService, service.GetAttributeCount(), &service.Handle());
if (result != BLE_STATUS_SUCCESS)
ReportError(result);
for (auto& characteristic : service.Characteristics())
AddCharacteristic(characteristic);
services.push_front(service);
}
services::GattServerCharacteristicOperations::UpdateStatus GattServerSt::Update(const services::GattServerCharacteristicOperationsObserver& characteristic, infra::ConstByteRange data) const
{
constexpr uint8_t valueOffset = 0;
auto result = aci_gatt_update_char_value(characteristic.ServiceHandle(),
characteristic.CharacteristicHandle(),
valueOffset,
data.size(),
data.begin());
if (result != BLE_STATUS_SUCCESS)
ReportError(result);
if (result == BLE_STATUS_SUCCESS)
return UpdateStatus::success;
else if (result == BLE_STATUS_INSUFFICIENT_RESOURCES)
return UpdateStatus::retry;
else
return UpdateStatus::error;
}
void GattServerSt::HciEvent(hci_event_pckt& event)
{
if (event.evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE)
{
assert(event.data != nullptr);
auto& coreEvent = *reinterpret_cast<evt_blecore_aci*>(event.data);
if (coreEvent.ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE)
{
assert(coreEvent.data != nullptr);
auto& attributeModifiedEvent = *reinterpret_cast<aci_gatt_attribute_modified_event_rp0*>(coreEvent.data);
HandleGattAttributeModified(attributeModifiedEvent);
}
}
}
void GattServerSt::AddCharacteristic(services::GattServerCharacteristic& characteristic)
{
constexpr uint8_t encryptionKeySize = 0x10;
constexpr uint8_t notifyAttributeWrite = 0x01;
constexpr uint8_t variableLength = 0x01;
auto result = aci_gatt_add_char(characteristic.ServiceHandle(),
UuidToType(characteristic.Type()),
ConvertUuid<Char_UUID_t>(characteristic.Type()),
characteristic.ValueLength(),
ConvertProperties(characteristic.Properties()),
ConvertPermissions(characteristic.Permissions()),
notifyAttributeWrite,
encryptionKeySize,
variableLength,
&characteristic.Handle());
if (result == BLE_STATUS_SUCCESS)
characteristic.Attach(*this);
else
ReportError(result);
}
void GattServerSt::HandleGattAttributeModified(aci_gatt_attribute_modified_event_rp0& event)
{
constexpr uint16_t valueAttributeOffset = 1;
infra::ByteRange data{event.Attr_Data, event.Attr_Data + event.Attr_Data_Length};
for (auto& service : services)
for (auto& characteristic : service.Characteristics())
if (event.Attr_Handle == characteristic.Handle() + valueAttributeOffset)
characteristic.NotifyObservers([data](auto& observer) { observer.DataReceived(data); });
}
void GattServerSt::ReportError(tBleStatus status) const
{}
}