28
28
bt_uuid_16_t BLECharacteristicImp::_gatt_chrc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CHRC_VAL};
29
29
bt_uuid_16_t BLECharacteristicImp::_gatt_ccc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CCC_VAL};
30
30
volatile bool BLECharacteristicImp::_gattc_writing = false ;
31
+ volatile bool BLECharacteristicImp::_gattc_write_result = false ;
31
32
32
33
BLECharacteristicImp::BLECharacteristicImp (const bt_uuid_t * uuid,
33
34
unsigned char properties,
34
35
uint16_t handle,
35
36
const BLEDevice& bledevice):
36
37
BLEAttribute(uuid, BLETypeCharacteristic),
38
+ _value_size(0 ),
37
39
_value_length(0 ),
40
+ _value(NULL ),
38
41
_value_buffer(NULL ),
39
42
_value_updated(false ),
40
43
_value_handle(handle),
@@ -45,23 +48,12 @@ BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,
45
48
_reading(false ),
46
49
_ble_device()
47
50
{
48
- _value_size = BLE_MAX_ATTR_DATA_LEN;// Set as MAX value. TODO: long read/write need to twist
49
- _value = (unsigned char *)malloc (_value_size);
50
51
51
52
// TODO: Enable when max value is not set.
52
53
// if (_value_size > BLE_MAX_ATTR_DATA_LEN)
53
54
// {
54
55
// _value_buffer = (unsigned char*)malloc(_value_size);
55
56
// }
56
-
57
- if (_value)
58
- {
59
- memset (_value, 0 , _value_size);
60
- }
61
- else
62
- {
63
- errno = ENOMEM;
64
- }
65
57
66
58
memset (&_ccc_cfg, 0 , sizeof (_ccc_cfg));
67
59
memset (&_ccc_value, 0 , sizeof (_ccc_value));
@@ -124,11 +116,25 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
124
116
_value = (unsigned char *)malloc (_value_size);
125
117
if (_value == NULL )
126
118
{
119
+ _value_size = 0 ;
127
120
errno = ENOMEM;
128
121
}
122
+ else
123
+ {
124
+ memset (_value, 0 , _value_size);
125
+ }
129
126
if (_value_size > BLE_MAX_ATTR_DATA_LEN)
130
127
{
131
128
_value_buffer = (unsigned char *)malloc (_value_size);
129
+ if (_value_buffer == NULL )
130
+ {
131
+ pr_info (LOG_MODULE_BLE, " %s-%d" , __FUNCTION__, __LINE__);
132
+ errno = ENOMEM;
133
+ }
134
+ else
135
+ {
136
+ memset (_value_buffer, 0 , _value_size);
137
+ }
132
138
}
133
139
134
140
memset (&_ccc_cfg, 0 , sizeof (_ccc_cfg));
@@ -168,7 +174,7 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
168
174
169
175
_sub_params.notify = profile_notify_process;
170
176
171
- if (NULL != characteristic._value )
177
+ if (NULL != characteristic._value && NULL != _value )
172
178
{
173
179
memcpy (_value, characteristic._value , _value_size);
174
180
_value_length = _value_size;
@@ -260,23 +266,34 @@ bool BLECharacteristicImp::writeValue(const byte value[], int length, int offset
260
266
bool
261
267
BLECharacteristicImp::setValue (const unsigned char value[], uint16_t length)
262
268
{
263
- _setValue (value, length, 0 );
264
- _value_updated = true ;
269
+ bool read_process_result = true ;
270
+ if (NULL != value && length != 0 )
271
+ {
272
+ _setValue (value, length, 0 );
273
+ _value_updated = true ;
274
+ }
275
+ else
276
+ {
277
+ read_process_result = false ;
278
+ }
265
279
if (BLEUtils::isLocalBLE (_ble_device) == true )
266
280
{
267
281
// GATT server
268
282
// Write request for GATT server
269
- if (_event_handlers[BLEWritten])
270
- {
271
- BLECharacteristic chrcTmp (this , &_ble_device);
272
- _event_handlers[BLEWritten](_ble_device, chrcTmp);
273
- }
274
-
275
- if (_oldevent_handlers[BLEWritten])
283
+ if (_value_updated)
276
284
{
277
- BLECharacteristic chrcTmp (this , &_ble_device);
278
- BLECentral central (_ble_device);
279
- _oldevent_handlers[BLEWritten](central, chrcTmp);
285
+ if (_event_handlers[BLEWritten])
286
+ {
287
+ BLECharacteristic chrcTmp (this , &_ble_device);
288
+ _event_handlers[BLEWritten](_ble_device, chrcTmp);
289
+ }
290
+
291
+ if (_oldevent_handlers[BLEWritten])
292
+ {
293
+ BLECharacteristic chrcTmp (this , &_ble_device);
294
+ BLECentral central (_ble_device);
295
+ _oldevent_handlers[BLEWritten](central, chrcTmp);
296
+ }
280
297
}
281
298
}
282
299
else
@@ -288,19 +305,23 @@ BLECharacteristicImp::setValue(const unsigned char value[], uint16_t length)
288
305
{
289
306
// Read response received. Not block the other reading.
290
307
_reading = false ;
308
+ _gattc_read_result = read_process_result;
291
309
}
292
310
293
- if (_event_handlers[BLEValueUpdated])
294
- {
295
- BLECharacteristic chrcTmp (this , &_ble_device);
296
- _event_handlers[BLEValueUpdated](_ble_device, chrcTmp);
297
- }
298
-
299
- if (_oldevent_handlers[BLEValueUpdated])
311
+ if (_value_updated)
300
312
{
301
- BLECharacteristic chrcTmp (this , &_ble_device);
302
- BLECentral central (_ble_device);
303
- _oldevent_handlers[BLEValueUpdated](central, chrcTmp);
313
+ if (_event_handlers[BLEValueUpdated])
314
+ {
315
+ BLECharacteristic chrcTmp (this , &_ble_device);
316
+ _event_handlers[BLEValueUpdated](_ble_device, chrcTmp);
317
+ }
318
+
319
+ if (_oldevent_handlers[BLEValueUpdated])
320
+ {
321
+ BLECharacteristic chrcTmp (this , &_ble_device);
322
+ BLECentral central (_ble_device);
323
+ _oldevent_handlers[BLEValueUpdated](central, chrcTmp);
324
+ }
304
325
}
305
326
}
306
327
@@ -561,6 +582,22 @@ BLEDescriptorImp* BLECharacteristicImp::descriptor(uint16_t handle)
561
582
void
562
583
BLECharacteristicImp::_setValue (const uint8_t value[], uint16_t length, uint16_t offset)
563
584
{
585
+ if (NULL == _value)
586
+ {
587
+ _value_size = length + offset;
588
+ // The discover didn't create the buffer
589
+ _value = (unsigned char *)malloc (_value_size);
590
+ if (_value)
591
+ {
592
+ memset (_value, 0 , _value_size);
593
+ }
594
+ else
595
+ {
596
+ _value_size = 0 ;
597
+ errno = ENOMEM;
598
+ }
599
+ }
600
+
564
601
if (length + offset > _value_size)
565
602
{
566
603
if (_value_size > offset)
@@ -650,12 +687,13 @@ bool BLECharacteristicImp::read(bool blocked)
650
687
return false ;
651
688
}
652
689
690
+ _reading = true ;
653
691
// Send read request
654
692
retval = bt_gatt_read (conn, &_read_params);
655
693
if (0 == retval)
656
694
{
657
- _reading = true ;
658
695
ret_bool = true ;
696
+ _gattc_read_result = false ;
659
697
660
698
// Block the call
661
699
if (blocked == true )
@@ -665,8 +703,17 @@ bool BLECharacteristicImp::read(bool blocked)
665
703
delay (5 );
666
704
ret_bool = _ble_device.connected ();
667
705
}
706
+ if (ret_bool)
707
+ {
708
+ ret_bool = _gattc_read_result;
709
+ }
668
710
}
669
711
}
712
+ else
713
+ {
714
+ // Read request failed
715
+ _reading = false ;
716
+ }
670
717
bt_conn_unref (conn);
671
718
return ret_bool;
672
719
}
@@ -676,12 +723,14 @@ void BLECharacteristicImp::writeResponseReceived(struct bt_conn *conn,
676
723
const void *data)
677
724
{
678
725
_gattc_writing = false ;
726
+ _gattc_write_result = (err == 0 );
679
727
}
680
728
681
729
bool BLECharacteristicImp::write (const unsigned char value[],
682
730
uint16_t length)
683
731
{
684
732
int retval = 0 ;
733
+ bool write_process_result = true ;
685
734
bt_conn_t * conn = NULL ;
686
735
687
736
if (true == BLEUtils::isLocalBLE (_ble_device) || true == _gattc_writing)
@@ -700,15 +749,20 @@ bool BLECharacteristicImp::write(const unsigned char value[],
700
749
if (_gatt_chrc.properties & BT_GATT_CHRC_WRITE)
701
750
{
702
751
_gattc_writing = true ;
752
+ _gattc_write_result = false ;
703
753
retval = bt_gatt_write (conn,
704
754
_value_handle,
705
755
0 ,
706
756
value,
707
757
length,
708
758
ble_on_write_no_rsp_complete);
709
- while (_gattc_writing )
759
+ if ( 0 == retval )
710
760
{
711
- delay (2 );
761
+ while (_gattc_writing)
762
+ {
763
+ delay (2 );
764
+ }
765
+ write_process_result = _gattc_write_result;
712
766
}
713
767
} else if (_gatt_chrc.properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP)
714
768
{
@@ -719,7 +773,7 @@ bool BLECharacteristicImp::write(const unsigned char value[],
719
773
false );
720
774
}
721
775
bt_conn_unref (conn);
722
- return (0 == retval);
776
+ return (0 == retval) && write_process_result ;
723
777
}
724
778
725
779
void BLECharacteristicImp::setBuffer (const uint8_t value[],
0 commit comments