2828bt_uuid_16_t BLECharacteristicImp::_gatt_chrc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CHRC_VAL};
2929bt_uuid_16_t BLECharacteristicImp::_gatt_ccc_uuid = {BT_UUID_TYPE_16, BT_UUID_GATT_CCC_VAL};
3030volatile bool BLECharacteristicImp::_gattc_writing = false ;
31+ volatile bool BLECharacteristicImp::_gattc_write_result = false ;
3132
3233BLECharacteristicImp::BLECharacteristicImp (const bt_uuid_t * uuid,
3334 unsigned char properties,
3435 uint16_t handle,
3536 const BLEDevice& bledevice):
3637 BLEAttribute(uuid, BLETypeCharacteristic),
38+ _value_size(0 ),
3739 _value_length(0 ),
40+ _value(NULL ),
3841 _value_buffer(NULL ),
3942 _value_updated(false ),
4043 _value_handle(handle),
@@ -45,23 +48,12 @@ BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,
4548 _reading(false ),
4649 _ble_device()
4750{
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);
5051
5152 // TODO: Enable when max value is not set.
5253 // if (_value_size > BLE_MAX_ATTR_DATA_LEN)
5354 // {
5455 // _value_buffer = (unsigned char*)malloc(_value_size);
5556 // }
56-
57- if (_value)
58- {
59- memset (_value, 0 , _value_size);
60- }
61- else
62- {
63- errno = ENOMEM;
64- }
6557
6658 memset (&_ccc_cfg, 0 , sizeof (_ccc_cfg));
6759 memset (&_ccc_value, 0 , sizeof (_ccc_value));
@@ -124,11 +116,25 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
124116 _value = (unsigned char *)malloc (_value_size);
125117 if (_value == NULL )
126118 {
119+ _value_size = 0 ;
127120 errno = ENOMEM;
128121 }
122+ else
123+ {
124+ memset (_value, 0 , _value_size);
125+ }
129126 if (_value_size > BLE_MAX_ATTR_DATA_LEN)
130127 {
131128 _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+ }
132138 }
133139
134140 memset (&_ccc_cfg, 0 , sizeof (_ccc_cfg));
@@ -168,7 +174,7 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
168174
169175 _sub_params.notify = profile_notify_process;
170176
171- if (NULL != characteristic._value )
177+ if (NULL != characteristic._value && NULL != _value )
172178 {
173179 memcpy (_value, characteristic._value , _value_size);
174180 _value_length = _value_size;
@@ -260,23 +266,34 @@ bool BLECharacteristicImp::writeValue(const byte value[], int length, int offset
260266bool
261267BLECharacteristicImp::setValue (const unsigned char value[], uint16_t length)
262268{
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+ }
265279 if (BLEUtils::isLocalBLE (_ble_device) == true )
266280 {
267281 // GATT server
268282 // 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)
276284 {
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+ }
280297 }
281298 }
282299 else
@@ -288,19 +305,23 @@ BLECharacteristicImp::setValue(const unsigned char value[], uint16_t length)
288305 {
289306 // Read response received. Not block the other reading.
290307 _reading = false ;
308+ _gattc_read_result = read_process_result;
291309 }
292310
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)
300312 {
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+ }
304325 }
305326 }
306327
@@ -561,6 +582,22 @@ BLEDescriptorImp* BLECharacteristicImp::descriptor(uint16_t handle)
561582void
562583BLECharacteristicImp::_setValue (const uint8_t value[], uint16_t length, uint16_t offset)
563584{
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+
564601 if (length + offset > _value_size)
565602 {
566603 if (_value_size > offset)
@@ -650,12 +687,13 @@ bool BLECharacteristicImp::read(bool blocked)
650687 return false ;
651688 }
652689
690+ _reading = true ;
653691 // Send read request
654692 retval = bt_gatt_read (conn, &_read_params);
655693 if (0 == retval)
656694 {
657- _reading = true ;
658695 ret_bool = true ;
696+ _gattc_read_result = false ;
659697
660698 // Block the call
661699 if (blocked == true )
@@ -665,8 +703,17 @@ bool BLECharacteristicImp::read(bool blocked)
665703 delay (5 );
666704 ret_bool = _ble_device.connected ();
667705 }
706+ if (ret_bool)
707+ {
708+ ret_bool = _gattc_read_result;
709+ }
668710 }
669711 }
712+ else
713+ {
714+ // Read request failed
715+ _reading = false ;
716+ }
670717 bt_conn_unref (conn);
671718 return ret_bool;
672719}
@@ -676,12 +723,14 @@ void BLECharacteristicImp::writeResponseReceived(struct bt_conn *conn,
676723 const void *data)
677724{
678725 _gattc_writing = false ;
726+ _gattc_write_result = (err == 0 );
679727}
680728
681729bool BLECharacteristicImp::write (const unsigned char value[],
682730 uint16_t length)
683731{
684732 int retval = 0 ;
733+ bool write_process_result = true ;
685734 bt_conn_t * conn = NULL ;
686735
687736 if (true == BLEUtils::isLocalBLE (_ble_device) || true == _gattc_writing)
@@ -700,15 +749,20 @@ bool BLECharacteristicImp::write(const unsigned char value[],
700749 if (_gatt_chrc.properties & BT_GATT_CHRC_WRITE)
701750 {
702751 _gattc_writing = true ;
752+ _gattc_write_result = false ;
703753 retval = bt_gatt_write (conn,
704754 _value_handle,
705755 0 ,
706756 value,
707757 length,
708758 ble_on_write_no_rsp_complete);
709- while (_gattc_writing )
759+ if ( 0 == retval )
710760 {
711- delay (2 );
761+ while (_gattc_writing)
762+ {
763+ delay (2 );
764+ }
765+ write_process_result = _gattc_write_result;
712766 }
713767 } else if (_gatt_chrc.properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP)
714768 {
@@ -719,7 +773,7 @@ bool BLECharacteristicImp::write(const unsigned char value[],
719773 false );
720774 }
721775 bt_conn_unref (conn);
722- return (0 == retval);
776+ return (0 == retval) && write_process_result ;
723777}
724778
725779void BLECharacteristicImp::setBuffer (const uint8_t value[],
0 commit comments