Skip to content

Commit fb5241c

Browse files
Merge pull request #5060 from pan-/cordio_port
BLE: Cordio port
2 parents c24fed1 + 75c9dfc commit fb5241c

File tree

128 files changed

+18795
-107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+18795
-107
lines changed

features/FEATURE_BLE/ble/ArrayView.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef BLE_ARRAY_VIEW_H_
18+
#define BLE_ARRAY_VIEW_H_
19+
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
namespace ble {
24+
25+
/**
26+
* Immutable view to an array.
27+
*/
28+
template<typename T>
29+
struct ArrayView {
30+
31+
/**
32+
* construct an array view to an empty array
33+
*/
34+
ArrayView() : _array(0), _size(0) { }
35+
36+
/**
37+
* construct an array view from a pointer.
38+
* and its size.
39+
*/
40+
ArrayView(T* array_ptr, size_t array_size) :
41+
_array(array_ptr), _size(array_size) { }
42+
43+
/**
44+
* Construct an array view from the reference to an array.
45+
*/
46+
template<size_t Size>
47+
ArrayView(T (&elements)[Size]):
48+
_array(elements), _size(Size) { }
49+
50+
/**
51+
* Return the size of the array viewed.
52+
*/
53+
size_t size() const {
54+
return _size;
55+
}
56+
57+
/**
58+
* Access to a mutable element of the array.
59+
*/
60+
T& operator[](size_t index) {
61+
return _array[index];
62+
}
63+
64+
/**
65+
* Access to an immutable element of the array.
66+
*/
67+
const T& operator[](size_t index) const {
68+
return _array[index];
69+
}
70+
71+
/**
72+
* Get the pointer to the array
73+
*/
74+
T* data() {
75+
return _array;
76+
}
77+
78+
/**
79+
* Get the pointer to the const array
80+
*/
81+
const T* data() const {
82+
return _array;
83+
}
84+
85+
/**
86+
* Equality operator
87+
*/
88+
friend bool operator==(const ArrayView& lhs, const ArrayView& rhs) {
89+
if (lhs.size() != rhs.size()) {
90+
return false;
91+
}
92+
93+
if (lhs.data() == rhs.data()) {
94+
return true;
95+
}
96+
97+
return memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
98+
}
99+
100+
/**
101+
* Not equal operator
102+
*/
103+
friend bool operator!=(const ArrayView& lhs, const ArrayView& rhs) {
104+
return !(lhs == rhs);
105+
}
106+
107+
private:
108+
T* const _array;
109+
const size_t _size;
110+
};
111+
112+
113+
/**
114+
* Generate an array view from a C/C++ array.
115+
* This helper avoid the typing of template parameter when ArrayView are
116+
* created 'inline'.
117+
* @param elements The array viewed.
118+
* @return The array_view to elements.
119+
*/
120+
template<typename T, size_t Size>
121+
ArrayView<T> make_ArrayView(T (&elements)[Size]) {
122+
return ArrayView<T>(elements);
123+
}
124+
125+
/**
126+
* Generate an array view from a C/C++ pointer and the size of the array.
127+
* This helper avoid the typing of template parameter when ArrayView are
128+
* created 'inline'.
129+
* @param array_ptr The pointer to the array to view.
130+
* @param array_size The size of the array.
131+
* @return The array_view to array_ptr with a size of array_size.
132+
*/
133+
template<typename T>
134+
ArrayView<T> make_ArrayView(T* array_ptr, size_t array_size) {
135+
return ArrayView<T>(array_ptr, array_size);
136+
}
137+
138+
/**
139+
* Generate a const array view from a C/C++ array.
140+
* This helper avoid the typing of template parameter when ArrayView are
141+
* created 'inline'.
142+
* @param elements The array viewed.
143+
* @return The ArrayView to elements.
144+
*/
145+
template<typename T, size_t Size>
146+
ArrayView<const T> make_const_ArrayView(T (&elements)[Size]) {
147+
return ArrayView<const T>(elements);
148+
}
149+
150+
/**
151+
* Generate a const array view from a C/C++ pointer and the size of the array.
152+
* This helper avoid the typing of template parameter when ArrayView are
153+
* created 'inline'.
154+
* @param array_ptr The pointer to the array to view.
155+
* @param array_size The size of the array.
156+
* @return The ArrayView to array_ptr with a size of array_size.
157+
*/
158+
template<typename T>
159+
ArrayView<const T> make_const_ArrayView(T* array_ptr, size_t array_size) {
160+
return ArrayView<const T>(array_ptr, array_size);
161+
}
162+
163+
} // namespace ble
164+
165+
#endif /* BLE_ARRAY_VIEW_H_ */

features/FEATURE_BLE/ble/BLETypes.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef BLE_TYPES_H_
18+
#define BLE_TYPES_H_
19+
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
namespace ble {
24+
25+
/**
26+
* A connection handle is an unsigned integer capable of holding a pointer.
27+
* The real type (either a pointer to an object or an integer) is opaque and
28+
* platform dependent.
29+
*/
30+
typedef uintptr_t connection_handle_t;
31+
32+
/**
33+
* Model an attribute handle in a GATT database.
34+
*/
35+
typedef uint16_t attribute_handle_t;
36+
37+
38+
/**
39+
* Model an inclusive range of GATT attributes handles.
40+
*/
41+
struct attribute_handle_range_t {
42+
attribute_handle_t begin;
43+
attribute_handle_t end;
44+
45+
friend bool operator==(
46+
const attribute_handle_range_t& lhs, const attribute_handle_range_t& rhs
47+
) {
48+
return (lhs.begin == rhs.begin) && (lhs.end == rhs.end);
49+
}
50+
51+
friend bool operator!=(
52+
const attribute_handle_range_t& lhs, const attribute_handle_range_t& rhs
53+
) {
54+
return !(lhs == rhs);
55+
}
56+
};
57+
58+
59+
/**
60+
* Construct an attribute_handle_range_t from its start and end handle.
61+
* @note This function is defined instead of a constructor to keep "POD-ness"
62+
* of attribute_handle_range_t.
63+
*/
64+
static inline attribute_handle_range_t attribute_handle_range(
65+
attribute_handle_t begin,
66+
attribute_handle_t end
67+
) {
68+
attribute_handle_range_t result = {
69+
begin,
70+
end
71+
};
72+
return result;
73+
}
74+
75+
} // namespace ble
76+
77+
#endif /* BLE_TYPES_H_ */

features/FEATURE_BLE/ble/CharacteristicDescriptorDiscovery.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class CharacteristicDescriptorDiscovery {
7070
* status of the discovery operation
7171
*/
7272
ble_error_t status;
73+
74+
/**
75+
* error code associated with the status if any.
76+
*/
77+
uint8_t error_code;
7378
};
7479

7580
/**

features/FEATURE_BLE/ble/DiscoveredCharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "GattAttribute.h"
2323
#include "GattClient.h"
2424
#include "CharacteristicDescriptorDiscovery.h"
25-
#include "ble/DiscoveredCharacteristicDescriptor.h"
25+
#include "DiscoveredCharacteristicDescriptor.h"
2626

2727
/**
2828
* @brief Representation of a characteristic discovered during a GattClient

features/FEATURE_BLE/ble/Gap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef __GAP_H__
1818
#define __GAP_H__
1919

20+
#include "BLETypes.h"
2021
#include "ble/BLEProtocol.h"
2122
#include "GapAdvertisingData.h"
2223
#include "GapAdvertisingParams.h"
@@ -171,7 +172,7 @@ class Gap {
171172
/**
172173
* Type for connection handle.
173174
*/
174-
typedef uint16_t Handle_t;
175+
typedef ble::connection_handle_t Handle_t;
175176

176177
/**
177178
* Structure containing GAP connection parameters. When in peripheral role

features/FEATURE_BLE/ble/GattAttribute.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define __GATT_ATTRIBUTE_H__
1919

2020
#include "UUID.h"
21+
#include "BLETypes.h"
2122

2223
/**
2324
* Instances of this class encapsulate the data that belongs to a Bluetooth Low
@@ -29,7 +30,7 @@ class GattAttribute {
2930
* Type for the handle or ID of the attribute in the ATT table. These are
3031
* unique and are usually generated by the underlying BLE stack.
3132
*/
32-
typedef uint16_t Handle_t;
33+
typedef ble::attribute_handle_t Handle_t;
3334
/**
3435
* Define the value of an invalid attribute handle.
3536
*/

features/FEATURE_BLE/ble/GattCallbackParamTypes.h

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
#ifndef __GATT_CALLBACK_PARAM_TYPES_H__
1818
#define __GATT_CALLBACK_PARAM_TYPES_H__
1919

20+
/**
21+
* Parameter of the callback invoked on a write operation.
22+
* This parameter is used whether a GattServer as received a write operation or
23+
* if the GattClient has completed a write operation.
24+
*
25+
* @important The fields connHandle, handle and writeOp are used in both
26+
* callbacks while:
27+
* - offset, len and data are reserved for GattServer write callbacks.
28+
* - status and error_code are reserved for GattClient write callbacks.
29+
*/
2030
struct GattWriteCallbackParams {
2131
/**
2232
* Enumeration for write operations.
@@ -34,29 +44,56 @@ struct GattWriteCallbackParams {
3444
Gap::Handle_t connHandle; /**< The handle of the connection that triggered the event. */
3545
GattAttribute::Handle_t handle; /**< Attribute Handle to which the write operation applies. */
3646
WriteOp_t writeOp; /**< Type of write operation. */
37-
uint16_t offset; /**< Offset for the write operation. */
38-
uint16_t len; /**< Length (in bytes) of the data to write. */
47+
48+
// Note: offset is used in GattServer while status is used in GattClient
49+
union {
50+
uint16_t offset; /**< Offset for the GattServer write operation. */
51+
ble_error_t status; /**< Status of the GattClient Write operation */
52+
};
53+
54+
// Note: len is used in GattServer while error_code is used in GattClient
55+
union {
56+
uint16_t len; /**< Length (in bytes) of the data to write (GattServer). */
57+
uint8_t error_code; /**< Error code of the GattClient Write operation */
58+
};
59+
3960
/**
4061
* Pointer to the data to write.
4162
*
4263
* @note Data might not persist beyond the callback; make a local copy if
4364
* needed.
65+
* @note This field is not used by callbacks invoked by the GattClient module.
4466
*/
4567
const uint8_t *data;
4668
};
4769

70+
/**
71+
* Parameter of the callback invoked on a read operation.
72+
* This parameter is used whether a GattServer as received a read operation or
73+
* if the GattClient has completed a read operation.
74+
*
75+
* @important The fields connHandle, handle and offset are used in both
76+
* callbacks while:
77+
* - len and data are reserved for GattServer read callbacks.
78+
* - status and error_code are reserved for GattClient read callbacks.
79+
*/
4880
struct GattReadCallbackParams {
4981
Gap::Handle_t connHandle; /**< The handle of the connection that triggered the event. */
5082
GattAttribute::Handle_t handle; /**< Attribute Handle to which the read operation applies. */
5183
uint16_t offset; /**< Offset for the read operation. */
52-
uint16_t len; /**< Length (in bytes) of the data to read. */
84+
union {
85+
uint16_t len; /**< Length (in bytes) of the data to read. */
86+
uint8_t error_code; /**< Error code if any (GattClient) */
87+
};
88+
5389
/**
5490
* Pointer to the data read.
5591
*
5692
* @note Data might not persist beyond the callback; make a local copy if
5793
* needed.
5894
*/
5995
const uint8_t *data;
96+
ble_error_t status; /**< Status of the operation BLE_ERROR_NONE in case of success or the error in case of error */
6097
};
6198

6299
enum GattAuthCallbackReply_t {

features/FEATURE_BLE/ble/GattClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class GattClient {
8080
* The following functions are meant to be overridden in the platform-specific sub-class.
8181
*/
8282
public:
83+
84+
virtual ~GattClient() { }
85+
8386
/**
8487
* Launch service discovery. Once launched, application callbacks will be
8588
* invoked for matching services or characteristics. isServiceDiscoveryActive()

0 commit comments

Comments
 (0)