Skip to content

Commit 8b81dd6

Browse files
committed
Add unimplement APIs and fix UART bugs
1. Fix UART can't send out all data. 2. Add the descriptor's read feature.
1 parent da9b932 commit 8b81dd6

File tree

15 files changed

+367
-132
lines changed

15 files changed

+367
-132
lines changed

cores/arduino/UARTClass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ size_t UARTClass::write( const uint8_t uc_data )
179179
return(0);
180180

181181
// Is the hardware currently busy?
182-
if (_tx_buffer->_iTail != _tx_buffer->_iHead || !uart_tx_ready(CONFIG_UART_CONSOLE_INDEX))
182+
if (_tx_buffer->_iTail != _tx_buffer->_iHead)
183183
{
184184
// If busy we buffer
185185
int l = (_tx_buffer->_iHead + 1) % UART_BUFFER_SIZE;
@@ -215,13 +215,13 @@ void UARTClass::IrqHandler( void )
215215
}
216216

217217
// if irq is Transmitter Holding Register
218-
else if(uart_irq_tx_ready(CONFIG_UART_CONSOLE_INDEX))
218+
if(uart_irq_tx_ready(CONFIG_UART_CONSOLE_INDEX))
219219
{
220220
if(_tx_buffer->_iTail != _tx_buffer->_iHead)
221221
{
222-
int end = (_tx_buffer->_iTail < _tx_buffer->_iHead) ? _tx_buffer->_iHead:UART_BUFFER_SIZE;
222+
int end = (_tx_buffer->_iTail < _tx_buffer->_iHead) ? _tx_buffer->_iHead : UART_BUFFER_SIZE;
223223
int l = min(end - _tx_buffer->_iTail, UART_FIFO_SIZE);
224-
uart_fifo_fill(CONFIG_UART_CONSOLE_INDEX, _tx_buffer->_aucBuffer+_tx_buffer->_iTail, l);
224+
l = uart_fifo_fill(CONFIG_UART_CONSOLE_INDEX, _tx_buffer->_aucBuffer+_tx_buffer->_iTail, l);
225225
_tx_buffer->_iTail = (_tx_buffer->_iTail+l)%UART_BUFFER_SIZE;
226226
}
227227
else

libraries/CurieBLE/examples/central/peripheral_explorer/peripheral_explorer.ino

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,30 @@ void exploreCharacteristic(BLECharacteristic characteristic) {
132132
Serial.print(", value 0x");
133133
printData(characteristic.value(), characteristic.valueLength());
134134
}
135-
}
136135

137136
Serial.println();
138137

138+
// loop the descriptors of the characteristic and explore each
139+
for (int i = 0; i < characteristic.descriptorCount(); i++) {
140+
BLEDescriptor descriptor = characteristic.descriptor(i);
141+
142+
exploreDescriptor(descriptor);
143+
}
144+
}
145+
146+
void exploreDescriptor(BLEDescriptor descriptor) {
147+
// print the UUID of the descriptor
148+
Serial.print("\t\tDescriptor ");
149+
Serial.print(descriptor.uuid());
150+
151+
// read the descriptor value
152+
descriptor.read();
153+
154+
// print out the value of the descriptor
155+
Serial.print(", value 0x");
156+
printData(descriptor.value(), descriptor.valueLength());
157+
158+
Serial.println();
139159
}
140160

141161
void printData(const unsigned char data[], int length) {

libraries/CurieBLE/src/BLEDescriptor.cpp

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,19 @@ BLEDescriptor::BLEDescriptor(BLEDescriptorImp* descriptorImp,
3535
const BLEDevice *bleDev):
3636
_bledev(bleDev),
3737
_value_size(0),
38-
_value(NULL)
38+
_value(NULL),
39+
_internal(descriptorImp)
3940
{
4041
_properties = descriptorImp->properties();
4142
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
4243
BLEUtils::uuidBT2String(descriptorImp->bt_uuid(), _uuid_cstr);
43-
44-
_value_size = descriptorImp->valueSize();
45-
_value = (unsigned char*)malloc(_value_size);
46-
if (NULL == _value)
47-
{
48-
memcpy(_value, descriptorImp->value(), _value_size);
49-
}
50-
else
51-
{
52-
errno = ENOMEM;
53-
}
5444
}
5545

5646
BLEDescriptor::BLEDescriptor(const char* uuid,
5747
const unsigned char value[],
5848
unsigned short valueLength):
59-
_bledev()
49+
_bledev(),
50+
_internal(NULL)
6051
{
6152
bt_uuid_128_t uuid_tmp;
6253
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
@@ -65,15 +56,16 @@ BLEDescriptor::BLEDescriptor(const char* uuid,
6556

6657
_bledev.setAddress(*BLEUtils::bleGetLoalAddress());
6758

68-
_value_size = valueLength > BLE_MAX_ATTR_LONGDATA_LEN ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
59+
_value_size = (valueLength > BLE_MAX_ATTR_LONGDATA_LEN) ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
6960
_value = (unsigned char*)malloc(_value_size);
70-
if (NULL == _value)
61+
if (NULL != _value)
7162
{
7263
memcpy(_value, value, _value_size);
7364
}
7465
else
7566
{
7667
errno = ENOMEM;
68+
_value_size = 0;
7769
}
7870
}
7971

@@ -82,22 +74,27 @@ BLEDescriptor::BLEDescriptor(const char* uuid,
8274
BLEDescriptor(uuid, (const unsigned char*)value, strlen(value))
8375
{}
8476

85-
BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs)
77+
BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs):
78+
_bledev(&rhs._bledev),
79+
_properties(rhs._properties),
80+
_value_size(0),
81+
_value(NULL),
82+
_internal(rhs._internal)
8683
{
87-
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
88-
if (_value)
89-
{
90-
memcpy(_value, rhs._value, rhs._value_size);
91-
_value_size = rhs._value_size;
92-
}
93-
else
84+
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
85+
if (NULL == _internal && rhs._value_size > 0)
9486
{
95-
_value_size = 0;
96-
errno = ENOMEM;
87+
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
88+
if (_value)
89+
{
90+
memcpy(_value, rhs._value, rhs._value_size);
91+
_value_size = rhs._value_size;
92+
}
93+
else
94+
{
95+
errno = ENOMEM;
96+
}
9797
}
98-
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
99-
_properties = rhs._properties;
100-
_bledev = BLEDevice(&rhs._bledev);
10198
}
10299

103100
BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
@@ -107,23 +104,27 @@ BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
107104
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
108105
_properties = rhs._properties;
109106
_bledev = BLEDevice(&rhs._bledev);
110-
if (_value_size < rhs._value_size)
107+
_internal = rhs._internal;
108+
if (NULL == _internal && rhs._value_size > 0)
111109
{
112-
_value_size = rhs._value_size;
110+
if (_value_size < rhs._value_size)
111+
{
112+
_value_size = rhs._value_size;
113+
114+
if (NULL != _value)
115+
free(_value);
116+
_value = (unsigned char*)malloc(_value_size);
117+
}
113118

114119
if (NULL != _value)
115-
free(_value);
116-
_value = (unsigned char*)malloc(_value_size);
117-
}
118-
119-
if (NULL != _value)
120-
{
121-
memcpy(_value, rhs._value, rhs._value_size);
122-
}
123-
else
124-
{
125-
_value_size = 0;
126-
errno = ENOMEM;
120+
{
121+
memcpy(_value, rhs._value, rhs._value_size);
122+
}
123+
else
124+
{
125+
_value_size = 0;
126+
errno = ENOMEM;
127+
}
127128
}
128129
}
129130
return *this;
@@ -145,12 +146,22 @@ const char* BLEDescriptor::uuid() const
145146

146147
const byte* BLEDescriptor::value() const
147148
{
148-
return _value;
149+
const byte* ret = _value;
150+
if (NULL != _internal)
151+
{
152+
ret = _internal->value();
153+
}
154+
return ret;
149155
}
150156

151157
int BLEDescriptor::valueLength() const
152158
{
153-
return _value_size;
159+
int ret = _value_size;
160+
if (NULL != _internal)
161+
{
162+
ret = _internal->valueLength();
163+
}
164+
return ret;
154165
}
155166

156167
BLEDescriptor::operator bool() const
@@ -166,6 +177,22 @@ unsigned char BLEDescriptor::properties() const
166177

167178
int BLEDescriptor::valueSize() const
168179
{
169-
return _value_size;
180+
int ret = _value_size;
181+
if (NULL != _internal)
182+
{
183+
ret = _internal->valueSize();
184+
}
185+
return ret;
186+
}
187+
188+
bool BLEDescriptor::read()
189+
{
190+
bool retVar = false;
191+
192+
if (NULL != _internal)
193+
{
194+
retVar = _internal->read();
195+
}
196+
return retVar;
170197
}
171198

libraries/CurieBLE/src/BLEDescriptor.h

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class BLEDescriptor
4343
virtual int valueLength() const; // returns the current length of the value
4444

4545
virtual operator bool() const; // is the descriptor valid (discovered from peripheral)
46+
47+
bool read();
4648

4749
unsigned char properties() const;
4850
int valueSize() const;
@@ -52,58 +54,9 @@ class BLEDescriptor
5254

5355
unsigned char _properties; // The characteristic property
5456

55-
unsigned short _value_size; // The value size
57+
unsigned short _value_size; // The value size
5658
unsigned char* _value; // The value. Will delete after create the _internal
57-
58-
59-
// The API reserved for feature release
60-
// move here for temp
61-
62-
/**
63-
* @brief Write the value of the descriptor
64-
*
65-
* @param value The value buffer that want to write to descriptor
66-
*
67-
* @param length The value buffer's length
68-
*
69-
* @return bool true - Success, false - Failed
70-
*
71-
* @note none
72-
*/
73-
//virtual bool writeValue(const byte value[], int length);
74-
75-
/**
76-
* @brief Write the value of the descriptor
77-
*
78-
* @param value The value buffer that want to write to descriptor
79-
*
80-
* @param length The value buffer's length
81-
*
82-
* @param offset The offset in the descriptor's data
83-
*
84-
* @return bool true - Success, false - Failed
85-
*
86-
* @note none
87-
*/
88-
//bool writeValue(const byte value[], int length, int offset);
89-
90-
/**
91-
* @brief Write the value of the descriptor
92-
*
93-
* @param value The value string that want to write to descriptor
94-
*
95-
* @return bool true - Success, false - Failed
96-
*
97-
* @note none
98-
*/
99-
//bool writeValue(const char* value);
100-
//virtual byte operator[] (int offset) const; // returns a byte of the value at the specified offset
101-
102-
// GATT client Write the value of the descriptor
103-
//virtual bool write(const byte value[], int length);
104-
//bool write(const byte value[], int length, int offset);
105-
//bool write(const char* value);
106-
//bool read();
59+
BLEDescriptorImp *_internal; // The real implementation of Descriptor
10760
};
10861

10962
#endif

libraries/CurieBLE/src/BLEDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ class BLEDevice
633633
void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); // set an event handler (callback)
634634

635635
protected:
636+
friend class BLEDescriptorImp;
636637
friend class BLECharacteristicImp;
637638
friend class BLEServiceImp;
638639
friend class BLEDeviceManager;
@@ -648,6 +649,11 @@ class BLEDevice
648649
bt_gatt_read_params_t *params,
649650
const void *data,
650651
uint16_t length);
652+
friend uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
653+
int err,
654+
bt_gatt_read_params_t *params,
655+
const void *data,
656+
uint16_t length);
651657
const bt_addr_le_t* bt_le_address() const;
652658
const bt_le_conn_param* bt_conn_param() const;
653659
void setAddress(const bt_addr_le_t& addr);

libraries/CurieBLE/src/internal/BLECallbacks.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ uint8_t profile_read_rsp_process(bt_conn_t *conn,
161161
return BT_GATT_ITER_STOP;
162162
}
163163

164+
uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
165+
int err,
166+
bt_gatt_read_params_t *params,
167+
const void *data,
168+
uint16_t length)
169+
{
170+
if (NULL == data)
171+
{
172+
return BT_GATT_ITER_STOP;
173+
}
174+
BLEDescriptorImp *descriptor = NULL;
175+
BLEDevice bleDevice(bt_conn_get_dst(conn));
176+
177+
// Get characteristic by handle params->single.handle
178+
descriptor = BLEProfileManager::instance()->descriptor(bleDevice, params->single.handle);
179+
180+
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
181+
if (descriptor)
182+
{
183+
descriptor->writeValue((const unsigned char *)data, length, params->single.offset);
184+
}
185+
//pr_debug(LOG_MODULE_BLE, "%s-%d: desc len-%d", __FUNCTION__, __LINE__, descriptor->valueLength());
186+
return BT_GATT_ITER_STOP;
187+
}
188+
164189
uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
165190
int err,
166191
bt_gatt_read_params_t *params,

libraries/CurieBLE/src/internal/BLECallbacks.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ uint8_t profile_read_rsp_process(bt_conn_t *conn, int err,
2828
bt_gatt_read_params_t *params,
2929
const void *data,
3030
uint16_t length);
31+
32+
uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
33+
int err,
34+
bt_gatt_read_params_t *params,
35+
const void *data,
36+
uint16_t length);
37+
3138
int profile_longflush_process(struct bt_conn *conn,
3239
const struct bt_gatt_attr *attr,
3340
uint8_t flags);

0 commit comments

Comments
 (0)