forked from arduino/ArduinoCore-arc32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEPeripheral.cpp
162 lines (132 loc) · 3.64 KB
/
BLEPeripheral.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
BLE Peripheral API (deprecated)
Copyright (c) 2016 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CurieBLE.h"
#include "BLEPeripheral.h"
static BLEPeripheralEventHandler m_eventHandlers[BLEDeviceLastEvent];
void bleBackCompatiblePeripheralConnectHandler(BLEDevice central)
{
if (m_eventHandlers[BLEConnected])
{
BLECentral temp(central);
m_eventHandlers[BLEConnected](temp);
}
}
void bleBackCompatiblePeripheralDisconnectHandler(BLEDevice central)
{
if (m_eventHandlers[BLEDisconnected])
{
BLECentral temp(central);
m_eventHandlers[BLEDisconnected](temp);
}
}
BLEPeripheral::BLEPeripheral(void) :
_initCalled(false),
_lastService(NULL),
_lastCharacteristic(NULL)
{
}
BLEPeripheral::~BLEPeripheral(void)
{
}
void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
{
BLE.setAdvertisedServiceUuid(advertisedServiceUuid);
}
void BLEPeripheral::setLocalName(const char* localName)
{
BLE.setLocalName(localName);
}
void BLEPeripheral::setDeviceName(const char *deviceName)
{
BLE.setDeviceName(deviceName);
}
void BLEPeripheral::setAppearance(const unsigned short appearance)
{
BLE.setAppearance(appearance);
}
void BLEPeripheral::setConnectionInterval(const unsigned short minConnInterval, const unsigned short maxConnInterval)
{
BLE.setConnectionInterval(minConnInterval, maxConnInterval);
}
void BLEPeripheral::addAttribute(BLEService& service)
{
BLE.addService(service);
_lastService = &service;
}
void BLEPeripheral::addAttribute(BLECharacteristic& characteristic)
{
if (_lastService)
{
_lastService->addCharacteristic(characteristic);
_lastCharacteristic = &characteristic;
}
}
void BLEPeripheral::addAttribute(BLEDescriptor& descriptor)
{
if (_lastCharacteristic)
{
_lastCharacteristic->addDescriptor(descriptor);
}
}
void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler callback)
{
if (BLEConnected == event || BLEDisconnected == event)
{
m_eventHandlers[event] = callback;
}
}
bool BLEPeripheral::begin(void)
{
if (!_initCalled)
{
init();
}
BLE.setEventHandler(BLEDisconnected, bleBackCompatiblePeripheralDisconnectHandler);
BLE.setEventHandler(BLEConnected, bleBackCompatiblePeripheralConnectHandler);
BLE.advertise();
return true;
}
void BLEPeripheral::poll(void)
{
BLE.poll();
}
void BLEPeripheral::end(void)
{
BLE.end();
}
bool BLEPeripheral::disconnect(void)
{
return BLE.disconnect();
}
BLECentral BLEPeripheral::central(void)
{
BLEDevice centralBle = BLE.central();
return BLECentral(centralBle);
}
bool BLEPeripheral::connected(void)
{
BLEDevice centralBle = BLE.central();
return centralBle.connected();
}
void BLEPeripheral::init()
{
if (!_initCalled)
{
BLE.begin();
memset(m_eventHandlers, 0, sizeof(m_eventHandlers));
_initCalled = true;
}
}