Skip to content

Commit fd4e43a

Browse files
committed
Update code based on code review
1. Implement BLEAttributeWithValue class 2. Change the code structure 3. Change the return value
1 parent bf78754 commit fd4e43a

25 files changed

+206
-25
lines changed

libraries/BLE/src/ArduinoBLE.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BLEDescriptorImp;
3232
#include "BLECommon.h"
3333

3434
#include "BLEDevice.h"
35-
35+
#include "BLEAttributeWithValue.h"
3636
#include "BLECharacteristic.h"
3737
#include "BLEDescriptor.h"
3838
#include "BLEService.h"

libraries/BLE/src/BLEAttribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "ArduinoBLE.h"
2121
#include "BLEAttribute.h"
2222

23-
#include "BLEUtils.h"
23+
#include "./internal/BLEUtils.h"
2424

2525

2626
BLEAttribute::BLEAttribute(const char* uuid, BLEAttributeType type) :
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
BLE Attribute with value API
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
#include "Arduino.h"
20+
#include "BLEAttributeWithValue.h"
21+
22+
BLEAttributeWithValue::BLEAttributeWithValue()
23+
{
24+
25+
}
26+
27+
// intepret the value of the attribute with the specified type
28+
String BLEAttributeWithValue::stringValue() const
29+
{
30+
const char *retTemp = (const char *)this->value();
31+
return retTemp;
32+
}
33+
34+
char BLEAttributeWithValue::charValue() const
35+
{
36+
char ret = this->operator[](0);
37+
return ret;
38+
}
39+
40+
unsigned char BLEAttributeWithValue::unsignedCharValue() const
41+
{
42+
unsigned char ret = this->operator[](0);
43+
return ret;
44+
}
45+
46+
byte BLEAttributeWithValue::byteValue() const
47+
{
48+
return this->operator[](0);
49+
}
50+
51+
short BLEAttributeWithValue::shortValue() const
52+
{
53+
short retTmp = 0;
54+
memcpy(&retTmp, this->value(), sizeof(retTmp));
55+
return retTmp;
56+
}
57+
58+
unsigned short BLEAttributeWithValue::unsignedShortValue() const
59+
{
60+
unsigned short retTmp = 0;
61+
memcpy(&retTmp, this->value(), sizeof(retTmp));
62+
return retTmp;
63+
}
64+
65+
int BLEAttributeWithValue::intValue() const
66+
{
67+
int retTmp = 0;
68+
memcpy(&retTmp, this->value(), sizeof(retTmp));
69+
return retTmp;
70+
}
71+
72+
unsigned int BLEAttributeWithValue::unsignedIntValue() const
73+
{
74+
unsigned int retTmp = 0;
75+
memcpy(&retTmp, this->value(), sizeof(retTmp));
76+
return retTmp;
77+
}
78+
79+
long BLEAttributeWithValue::longValue() const
80+
{
81+
long retTmp = 0;
82+
memcpy(&retTmp, this->value(), sizeof(retTmp));
83+
return retTmp;
84+
}
85+
86+
unsigned long BLEAttributeWithValue::unsignedLongValue() const
87+
{
88+
unsigned long retTmp = 0;
89+
memcpy(&retTmp, this->value(), sizeof(retTmp));
90+
return retTmp;
91+
}
92+
93+
float BLEAttributeWithValue::floatValue() const
94+
{
95+
float retTmp = 0;
96+
memcpy(&retTmp, this->value(), sizeof(retTmp));
97+
return retTmp;
98+
}
99+
100+
double BLEAttributeWithValue::doubleValue() const
101+
{
102+
double retTmp = 0;
103+
memcpy(&retTmp, this->value(), sizeof(retTmp));
104+
return retTmp;
105+
}
106+
107+
// write the value of the attribute with the specified type
108+
bool BLEAttributeWithValue::writeString(const String& s)
109+
{
110+
if (s.length() > (unsigned int)this->valueSize())
111+
{
112+
return false;
113+
}
114+
return this->writeValue((const byte*)s.c_str(), s.length());
115+
}
116+
117+
bool BLEAttributeWithValue::writeString(const char* s)
118+
{
119+
if (strlen(s) > (unsigned int)this->valueSize())
120+
{
121+
return false;
122+
}
123+
return this->writeValue((const byte*)s, strlen(s));
124+
}
125+
126+
bool BLEAttributeWithValue::writeChar(char c)
127+
{
128+
return this->writeValue((const byte*)&c, sizeof(c));
129+
}
130+
131+
bool BLEAttributeWithValue::writeUnsignedChar(unsigned char c)
132+
{
133+
return this->writeValue((const byte*)&c, sizeof(c));
134+
}
135+
136+
bool BLEAttributeWithValue::writeByte(byte b)
137+
{
138+
return this->writeValue((const byte*)&b, sizeof(b));
139+
}
140+
141+
bool BLEAttributeWithValue::writeShort(short s)
142+
{
143+
return this->writeValue((const byte*)&s, sizeof(s));
144+
}
145+
146+
bool BLEAttributeWithValue::writeUnsignedShort(unsigned short s)
147+
{
148+
return this->writeValue((const byte*)&s, sizeof(s));
149+
}
150+
151+
bool BLEAttributeWithValue::writeInt(int i)
152+
{
153+
return this->writeValue((const byte*)&i, sizeof(i));
154+
}
155+
156+
bool BLEAttributeWithValue::writeUnsignedInt(unsigned int i)
157+
{
158+
return this->writeValue((const byte*)&i, sizeof(i));
159+
}
160+
161+
bool BLEAttributeWithValue::writeLong(long l)
162+
{
163+
return this->writeValue((const byte*)&l, sizeof(l));
164+
}
165+
166+
bool BLEAttributeWithValue::writeUnsignedLong(unsigned int l)
167+
{
168+
return this->writeValue((const byte*)&l, sizeof(l));
169+
}
170+
171+
bool BLEAttributeWithValue::writeFloat(float f)
172+
{
173+
return this->writeValue((const byte*)&f, sizeof(f));
174+
}
175+
176+
bool BLEAttributeWithValue::writeDouble(double d)
177+
{
178+
return this->writeValue((const byte*)&d, sizeof(d));
179+
}
180+
181+

libraries/BLE/src/BLEAttributeWithValue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class BLEAttributeWithValue
2525
public:
2626
BLEAttributeWithValue();
2727

28-
virtual int valueLength() const; // returns the length of the attribute value
29-
virtual const byte* value() const; // returns the value of the attribute array
30-
virtual byte operator[] (int offset) const; // access an attribute value at the specified offset
31-
virtual bool writeValue(const byte value[], int length);
28+
virtual int valueSize() const = 0; // returns the length of the attribute value
29+
virtual const byte* value() const = 0; // returns the value of the attribute array
30+
virtual byte operator[] (int offset) const = 0; // access an attribute value at the specified offset
31+
virtual bool writeValue(const byte value[], int length) = 0;
3232

3333
// intepret the value of the attribute with the specified type
3434
String stringValue() const;

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
#include "ArduinoBLE.h"
33

4-
#include "BLEUtils.h"
4+
#include "./internal/BLEUtils.h"
55

66
#include "BLECharacteristic.h"
7-
#include "BLEProfileManager.h"
7+
#include "./internal/BLEProfileManager.h"
88

9-
#include "BLECharacteristicImp.h"
9+
#include "./internal/BLECharacteristicImp.h"
1010

1111
BLECharacteristic::BLECharacteristic():
1212
_bledev(), _internal(NULL), _properties(0),

libraries/BLE/src/BLECharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef void (*BLECharacteristicEventHandler)(BLEDevice& bledev, BLECharacterist
4545

4646
//#include "BLECharacteristicImp.h"
4747

48-
class BLECharacteristic //: public BLEAttributeWithValue
48+
class BLECharacteristic: public BLEAttributeWithValue
4949
{
5050
public:
5151
BLECharacteristic();

libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
#include "BLEAttribute.h"
2020
#include "BLEDescriptor.h"
21-
#include "BLEUtils.h"
22-
#include "BLEDescriptorImp.h"
21+
#include "./internal/BLEUtils.h"
22+
#include "./internal/BLEDescriptorImp.h"
2323

2424
BLEDescriptor::BLEDescriptor():
2525
_properties(0),

libraries/BLE/src/BLEDevice.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#include "ArduinoBLE.h"
2020
#include "BLEDevice.h"
2121

22-
#include "BLEUtils.h"
22+
#include "./internal/BLEUtils.h"
2323

24-
#include "BLEProfileManager.h"
25-
#include "BLEDeviceManager.h"
26-
#include "BLECharacteristicImp.h"
24+
#include "./internal/BLEProfileManager.h"
25+
#include "./internal/BLEDeviceManager.h"
26+
#include "./internal/BLECharacteristicImp.h"
2727

2828
BLEDevice::BLEDevice()
2929
{
@@ -159,12 +159,12 @@ void BLEDevice::setAppearance(unsigned short appearance)
159159
BLEDeviceManager::instance()->setAppearance(appearance);
160160
}
161161

162-
BLE_STATUS_T BLEDevice::addService(BLEService& attribute)
162+
int BLEDevice::addService(BLEService& attribute)
163163
{
164164
return BLEProfileManager::instance()->addService(*this, attribute);
165165
}
166166

167-
BLE_STATUS_T BLEDevice::startAdvertising()
167+
int BLEDevice::startAdvertising()
168168
{
169169
preCheckProfile();
170170
return BLEDeviceManager::instance()->startAdvertising();

libraries/BLE/src/BLEDevice.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,22 @@ class BLEDevice
298298
*
299299
* @param[in] attribute The service that will add to Peripheral
300300
*
301-
* @return BLE_STATUS_T Indicating success or error type
301+
* @return int Indicating success or error type @enum BLE_STATUS_T
302302
*
303303
* @note This method must be called before the begin method
304304
*/
305-
BLE_STATUS_T addService(BLEService& attribute);
305+
int addService(BLEService& attribute);
306306

307307
/**
308308
* @brief Construct the ADV data and start send advertisement
309309
*
310310
* @param none
311311
*
312-
* @return BLE_STATUS_T 0 - Success. Others - error code
312+
* @return int 0 - Success. Others - error code @enum BLE_STATUS_T
313313
*
314314
* @note none
315315
*/
316-
BLE_STATUS_T startAdvertising();
316+
int startAdvertising();
317317

318318
/**
319319
* @brief Stop send advertisement

libraries/BLE/src/BLEService.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
#include "BLEService.h"
2020

21-
#include "BLEProfileManager.h"
22-
#include "BLECharacteristicImp.h"
21+
#include "./internal/BLEProfileManager.h"
22+
#include "./internal/BLECharacteristicImp.h"
2323

24-
#include "BLEUtils.h"
24+
#include "./internal/BLEUtils.h"
2525

2626
BLEService::BLEService():_bledevice(),_service_imp(NULL)
2727
{

0 commit comments

Comments
 (0)