forked from arduino-libraries/ArduinoBLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEService.cpp
177 lines (142 loc) · 3.52 KB
/
BLEService.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "local/BLELocalService.h"
#include "remote/BLERemoteService.h"
#include "BLEService.h"
BLEService::BLEService() :
BLEService((BLELocalService*)NULL)
{
}
BLEService::BLEService(BLELocalService* local) :
_local(local),
_remote(NULL)
{
if (_local) {
_local->retain();
}
}
BLEService::BLEService(BLERemoteService* remote) :
_local(NULL),
_remote(remote)
{
if (_remote) {
_remote->retain();
}
}
BLEService::BLEService(const char* uuid) :
BLEService(new BLELocalService(uuid))
{
}
BLEService::BLEService(const BLEService& other)
{
_local = other._local;
if (_local) {
_local->retain();
}
_remote = other._remote;
if (_remote) {
_remote->retain();
}
}
BLEService::~BLEService()
{
if (_local && _local->release() <= 0) {
delete _local;
}
if (_remote && _remote->release() <= 0) {
delete _remote;
}
}
const char* BLEService::uuid() const
{
if (_local) {
return _local->uuid();
}
if (_remote) {
return _remote->uuid();
}
return "";
}
void BLEService::addCharacteristic(BLECharacteristic& characteristic)
{
if (_local) {
_local->addCharacteristic(characteristic);
}
}
BLEService::operator bool() const
{
return (_local != NULL) || (_remote != NULL);
}
int BLEService::characteristicCount() const
{
if (_remote) {
return _remote->characteristicCount();
}
return 0;
}
bool BLEService::hasCharacteristic(const char* uuid) const
{
return hasCharacteristic(uuid, 0);
}
bool BLEService::hasCharacteristic(const char* uuid, int index) const
{
if (_remote) {
int count = 0;
int numCharacteristics = _remote->characteristicCount();
for (int i = 0; i < numCharacteristics; i++) {
BLERemoteCharacteristic* c = _remote->characteristic(i);
if (strcasecmp(uuid, c->uuid()) == 0) {
if (count == index) {
return true;
}
count++;
}
}
}
return false;
}
BLECharacteristic BLEService::characteristic(int index) const
{
if (_remote) {
return BLECharacteristic(_remote->characteristic(index));
}
return BLECharacteristic();
}
BLECharacteristic BLEService::characteristic(const char * uuid) const
{
return characteristic(uuid, 0);
}
BLECharacteristic BLEService::characteristic(const char * uuid, int index) const
{
if (_remote) {
int count = 0;
int numCharacteristics = _remote->characteristicCount();
for (int i = 0; i < numCharacteristics; i++) {
BLERemoteCharacteristic* c = _remote->characteristic(i);
if (strcasecmp(uuid, c->uuid()) == 0) {
if (count == index) {
return BLECharacteristic(c);
}
count++;
}
}
}
return BLECharacteristic();
}
BLELocalService* BLEService::local()
{
return _local;
}