Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c81df43

Browse files
committed
add write method for returning the num of bytes transferred
1 parent baa1370 commit c81df43

File tree

8 files changed

+140
-12
lines changed

8 files changed

+140
-12
lines changed

‎src/BLECharacteristic.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ int BLECharacteristic::readValue(int32_t& value)
236236
return readValue((uint8_t*)&value, sizeof(value));
237237
}
238238

239+
int BLECharacteristic::write(const char* value, bool withResponse)
240+
{
241+
if (_local) {
242+
return _local->write(value);
243+
}
244+
245+
if (_remote) {
246+
return _remote->write(value, withResponse);
247+
}
248+
249+
return 0;
250+
}
251+
252+
int BLECharacteristic::write(const uint8_t value[], int length, bool withResponse)
253+
{
254+
if (_local) {
255+
return _local->write(value, length);
256+
}
257+
258+
if (_remote) {
259+
return _remote->write(value, length, withResponse);
260+
}
261+
262+
return 0;
263+
}
264+
239265
int BLECharacteristic::writeValue(const uint8_t value[], int length, bool withResponse)
240266
{
241267
if (_local) {

‎src/BLECharacteristic.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class BLECharacteristic {
7777
int writeValue(int16_t value, bool withResponse = true);
7878
int writeValue(uint32_t value, bool withResponse = true);
7979
int writeValue(int32_t value, bool withResponse = true);
80+
int write(const uint8_t value[], int length, bool withResponse = true);
81+
int write(const char* value, bool withResponse = true);
82+
8083

8184
// deprecated, use writeValue(...)
8285
int setValue(const uint8_t value[], int length) { return writeValue(value, length); }

‎src/local/BLELocalCharacteristic.cpp‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length)
119119
}
120120

121121
if ((_properties & BLEIndicate) && (_cccdValue & 0x0002)) {
122-
return ATT.handleInd(valueHandle(), _value,_valueLength);
122+
return ATT.handleInd(valueHandle(), _value,&_valueLength);
123123
} else if ((_properties & BLENotify) && (_cccdValue & 0x0001)) {
124-
return ATT.handleNotify(valueHandle(), _value, _valueLength);
124+
return ATT.handleNotify(valueHandle(), _value, &_valueLength);
125125
}
126126

127127
if (_broadcast) {
@@ -142,6 +142,47 @@ int BLELocalCharacteristic::writeValue(const char* value)
142142
return writeValue((uint8_t*)value, strlen(value));
143143
}
144144

145+
int BLELocalCharacteristic::write(const uint8_t value[], int length)
146+
{
147+
_valueLength = min(length, _valueSize);
148+
memcpy(_value, value, _valueLength);
149+
150+
if (_fixedLength) {
151+
_valueLength = _valueSize;
152+
}
153+
154+
if ((_properties & BLEIndicate) && (_cccdValue & 0x0002)) {
155+
uint8_t res = ATT.handleInd(valueHandle(), _value, &_valueLength);
156+
if (res != 1){
157+
return res;
158+
}
159+
return _valueLength;
160+
} else if ((_properties & BLENotify) && (_cccdValue & 0x0001)) {
161+
uint8_t res = ATT.handleNotify(valueHandle(), _value, &_valueLength);
162+
if (res != 1){
163+
return res;
164+
}
165+
return _valueLength;
166+
}
167+
168+
if (_broadcast) {
169+
uint16_t serviceUuid = GATT.serviceUuidForCharacteristic(this);
170+
171+
BLE.setAdvertisedServiceData(serviceUuid, value, length);
172+
173+
if (!ATT.connected() && GAP.advertising()) {
174+
BLE.advertise();
175+
}
176+
}
177+
178+
return _valueLength;
179+
}
180+
181+
int BLELocalCharacteristic::write(const char* value)
182+
{
183+
return write((uint8_t*)value, strlen(value));
184+
}
185+
145186
int BLELocalCharacteristic::broadcast()
146187
{
147188
if (_properties & BLEBroadcast) {

‎src/local/BLELocalCharacteristic.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class BLELocalCharacteristic : public BLELocalAttribute {
4949

5050
int writeValue(const uint8_t value[], int length);
5151
int writeValue(const char* value);
52+
int write(const uint8_t value[], int length);
53+
int write(const char* value);
5254

5355
int broadcast();
5456

‎src/remote/BLERemoteCharacteristic.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,59 @@ uint8_t BLERemoteCharacteristic::operator[] (int offset) const
8686
return 0;
8787
}
8888

89+
int BLERemoteCharacteristic::write(const uint8_t value[], int length, bool withResponse)
90+
{
91+
if (!ATT.connected(_connectionHandle)) {
92+
return false;
93+
}
94+
95+
uint16_t maxLength = ATT.mtu(_connectionHandle) - 3;
96+
97+
if (length > (int)maxLength) {
98+
// cap to MTU max length
99+
length = maxLength;
100+
}
101+
102+
_value = (uint8_t*)realloc(_value, length);
103+
if (_value == NULL) {
104+
// realloc failed
105+
return 0;
106+
}
107+
108+
if ((_properties & BLEWrite) && withResponse) {
109+
uint8_t resp[4];
110+
int respLength = ATT.writeReq(_connectionHandle, _valueHandle, value, length, resp);
111+
112+
if (!respLength) {
113+
return 0;
114+
}
115+
116+
if (resp[0] == 0x01) {
117+
// error
118+
return 0;
119+
}
120+
121+
memcpy(_value, value, length);
122+
_valueLength = length;
123+
124+
return length;
125+
} else if (_properties & BLEWriteWithoutResponse) {
126+
ATT.writeCmd(_connectionHandle, _valueHandle, value, length);
127+
128+
memcpy(_value, value, length);
129+
_valueLength = length;
130+
131+
return length;
132+
}
133+
134+
return 0;
135+
}
136+
137+
int BLERemoteCharacteristic::write(const char* value, bool withResponse)
138+
{
139+
return write((uint8_t*)value, strlen(value), withResponse);
140+
}
141+
89142
int BLERemoteCharacteristic::writeValue(const uint8_t value[], int length, bool withResponse)
90143
{
91144
if (!ATT.connected(_connectionHandle)) {

‎src/remote/BLERemoteCharacteristic.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class BLERemoteCharacteristic : public BLERemoteAttribute {
4141

4242
int writeValue(const uint8_t value[], int length, bool withResponse = true);
4343
int writeValue(const char* value, bool withResponse = true);
44+
int write(const uint8_t value[], int length, bool withResponse = true);
45+
int write(const char* value, bool withResponse = true);
4446

4547
bool valueUpdated();
4648
bool updatedValueRead();

‎src/utility/ATT.cpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ BLEDevice ATTClass::central()
582582
return BLEDevice();
583583
}
584584

585-
bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, intlength)
585+
bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, uint16_t *length)
586586
{
587587
int numNotifications = 0;
588588

@@ -600,9 +600,9 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
600600
memcpy(&notification[1], &handle, sizeof(handle));
601601
notificationLength += sizeof(handle);
602602

603-
length = min((uint16_t)(_peers[i].mtu - notificationLength), (uint16_t)length);
604-
memcpy(&notification[notificationLength], value, length);
605-
notificationLength += length;
603+
*length = min((uint16_t)(_peers[i].mtu - notificationLength), *length);
604+
memcpy(&notification[notificationLength], value, *length);
605+
notificationLength += *length;
606606

607607
/// TODO: Set encryption requirement on notify.
608608
HCI.sendAclPkt(_peers[i].connectionHandle, ATT_CID, notificationLength, notification);
@@ -613,7 +613,7 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
613613
return (numNotifications > 0);
614614
}
615615

616-
bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, intlength)
616+
bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, uint16_t *length)
617617
{
618618
int numIndications = 0;
619619

@@ -631,9 +631,10 @@ bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length)
631631
memcpy(&indication[1], &handle, sizeof(handle));
632632
indicationLength += sizeof(handle);
633633

634-
length = min((uint16_t)(_peers[i].mtu - indicationLength), (uint16_t)length);
635-
memcpy(&indication[indicationLength], value, length);
636-
indicationLength += length;
634+
*length = min((uint16_t)(_peers[i].mtu - indicationLength), *length);
635+
636+
memcpy(&indication[indicationLength], value, *length);
637+
indicationLength += *length;
637638

638639
_cnf = false;
639640

‎src/utility/ATT.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class ATTClass {
8383

8484
virtual BLEDevice central();
8585

86-
virtual bool handleNotify(uint16_t handle, const uint8_t* value, intlength);
87-
virtual bool handleInd(uint16_t handle, const uint8_t* value, intlength);
86+
virtual bool handleNotify(uint16_t handle, const uint8_t* value, uint16_t *length);
87+
virtual bool handleInd(uint16_t handle, const uint8_t* value, uint16_t *length);
8888

8989
virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler);
9090

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /