Skip to content

Commit 46f0276

Browse files
pan-adbridge
authored andcommitted
Nordic BLE: Simplification and clarification of pal client implementation.
1 parent 9686fcc commit 46f0276

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5/source/nRF5XPalGattClient.cpp

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ static attribute_handle_range_t to_ble_handle_range(const ble_gattc_handle_range
7878
return result;
7979
}
8080

81+
/**
82+
* Convert an error from the softdevice into a ble_error_t
83+
*/
84+
static ble_error_t convert_sd_error(uint32_t err)
85+
{
86+
switch (err) {
87+
case NRF_SUCCESS:
88+
return BLE_ERROR_NONE;
89+
case NRF_ERROR_INVALID_PARAM:
90+
case BLE_ERROR_INVALID_CONN_HANDLE:
91+
case NRF_ERROR_INVALID_ADDR:
92+
return BLE_ERROR_INVALID_PARAM;
93+
case NRF_ERROR_INVALID_STATE:
94+
return BLE_ERROR_INVALID_STATE;
95+
case NRF_ERROR_DATA_SIZE:
96+
return BLE_ERROR_PARAM_OUT_OF_RANGE;
97+
case BLE_ERROR_NO_TX_PACKETS:
98+
return BLE_ERROR_NO_MEM;
99+
case NRF_ERROR_BUSY:
100+
return BLE_STACK_BUSY;
101+
case NRF_ERROR_NO_MEM:
102+
return BLE_ERROR_NO_MEM;
103+
default:
104+
return BLE_ERROR_UNSPECIFIED;
105+
}
106+
}
107+
81108
/**
82109
* Convert a UUID into a ble_uuid_t .
83110
* If the UUID is a 128 bit one then it is registered into the softdevice.
@@ -101,54 +128,19 @@ static ble_error_t to_nordic_uuid(const UUID &uuid, ble_uuid_t &nordic_uuid)
101128
memcpy(uuid128.uuid128, uuid.getBaseUUID(), sizeof(uuid128.uuid128));
102129

103130
// UUID not found, try to register it
104-
err = sd_ble_uuid_vs_add(
105-
&uuid128,
106-
&nordic_uuid.type
107-
);
108-
109-
switch (err) {
110-
case NRF_SUCCESS:
111-
nordic_uuid.uuid = bytes_to_u16(uuid.getBaseUUID() + 12);
112-
return BLE_ERROR_NONE;
113-
case NRF_ERROR_INVALID_ADDR:
114-
return BLE_ERROR_INVALID_PARAM;
115-
case NRF_ERROR_NO_MEM:
116-
return BLE_ERROR_NO_MEM;
117-
default:
118-
return BLE_ERROR_UNSPECIFIED;
131+
err = sd_ble_uuid_vs_add(&uuid128, &nordic_uuid.type);
132+
if (err == NRF_SUCCESS) {
133+
nordic_uuid.uuid = bytes_to_u16(uuid.getBaseUUID() + 12);
119134
}
135+
136+
return convert_sd_error(err);
120137
} else {
121138
nordic_uuid.type = BLE_UUID_TYPE_BLE;
122139
nordic_uuid.uuid = uuid.getShortUUID();
123140
return BLE_ERROR_NONE;
124141
}
125142
}
126143

127-
/**
128-
* Convert an error from the softdevice into a ble_error_t
129-
*/
130-
static ble_error_t convert_sd_error(uint32_t err)
131-
{
132-
switch (err) {
133-
case NRF_SUCCESS:
134-
return BLE_ERROR_NONE;
135-
case NRF_ERROR_INVALID_PARAM:
136-
case BLE_ERROR_INVALID_CONN_HANDLE:
137-
case NRF_ERROR_INVALID_ADDR:
138-
return BLE_ERROR_INVALID_PARAM;
139-
case NRF_ERROR_INVALID_STATE:
140-
return BLE_ERROR_INVALID_STATE;
141-
case NRF_ERROR_DATA_SIZE:
142-
return BLE_ERROR_PARAM_OUT_OF_RANGE;
143-
case BLE_ERROR_NO_TX_PACKETS:
144-
return BLE_ERROR_NO_MEM;
145-
case NRF_ERROR_BUSY:
146-
return BLE_STACK_BUSY;
147-
default:
148-
return BLE_ERROR_UNSPECIFIED;
149-
}
150-
}
151-
152144
/**
153145
* Convert an attribute error code from the softdevice into a valid ATT error.
154146
*/
@@ -186,6 +178,7 @@ ble_error_t nRF5XGattClient::initialize()
186178
ble_error_t nRF5XGattClient::exchange_mtu(connection_handle_t connection)
187179
{
188180
// Note: Not supported by the current softdevice
181+
// FIXME: implement when SD 140 5.x.x is present
189182
return BLE_ERROR_NOT_IMPLEMENTED;
190183
}
191184

@@ -361,7 +354,7 @@ ble_error_t nRF5XGattClient::execute_write_queue(
361354
* the peer. It must be implemented by Procedure derived classes.
362355
* - handle: Event handler that process ble events comming from the softdevice.
363356
* This function drive the procedure flow and must be implemented by
364-
* Procedure childs.
357+
* Procedure derived classes.
365358
* - terminate: end the procedure and forward the result to the GattClient
366359
* event handle.
367360
*
@@ -422,7 +415,7 @@ struct nRF5XGattClient::GattProcedure {
422415
* It initiate a single request to the peer and excepts a single response. This
423416
* kind of procedure doesn't requires extra processing step.
424417
*
425-
* Given that such procedure expect a single event type from the soft device,
418+
* Given that such procedure expects a single event type from the soft device,
426419
* error handling can be generalized.
427420
*/
428421
struct nRF5XGattClient::RegularGattProcedure : GattProcedure {
@@ -675,6 +668,9 @@ struct nRF5XGattClient::DiscoverPrimaryServiceProcedure : GattProcedure {
675668
}
676669
}
677670

671+
// Hold read by group type response of services with 128 bit UUID.
672+
// The response is composed of the service attribute handle (2 bytes), the
673+
// end group handle (2 bytes) and the service UUID (16 bytes).
678674
typedef uint8_t packed_discovery_response_t[20];
679675

680676
packed_discovery_response_t* response;

0 commit comments

Comments
 (0)