Skip to content

BLE privacy, signing, persistent security database #6932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 314 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
314 commits
Select commit Hold shift + click to select a range
5ae9cc3
set peer csrk on pal
paul-szczepanek-arm Mar 28, 2018
8d966db
invalid mic event
paul-szczepanek-arm Mar 29, 2018
511135f
signing only for the slave, unless keys requested to be sent by master
paul-szczepanek-arm Mar 29, 2018
780d8a4
let the stack know whether csrk is authenticated
paul-szczepanek-arm Mar 29, 2018
9283413
count failures, trigger reparing when verification fails
paul-szczepanek-arm Mar 29, 2018
f5fee68
fixed naming for sig failure count
paul-szczepanek-arm Mar 29, 2018
e276478
only bother reacting to verification failures if we want to use signing
paul-szczepanek-arm Mar 29, 2018
dfdfcfb
style fix
paul-szczepanek-arm Mar 29, 2018
15c06ac
style fix
paul-szczepanek-arm Mar 29, 2018
c1e2e07
missing line from commit
paul-szczepanek-arm Mar 29, 2018
cbf80e9
typos, style and other review fixes
paul-szczepanek-arm Apr 3, 2018
08c9ac5
Merge branch 'signing' of https://github.com/paul-szczepanek-arm/mbed…
paul-szczepanek-arm Apr 3, 2018
0833827
Merge pull request #26 from paul-szczepanek-arm/signing
paul-szczepanek-arm Apr 3, 2018
7bf0eb0
BLE: use ArrayView to pass and get parameters in Crypto API
pan- Apr 4, 2018
bf41bb3
BLE: Export byte_array_t size exploitable at compile time.
pan- Apr 4, 2018
d8f3d9c
BLE: provide non const overload of byte_array_t::data member function.
pan- Apr 4, 2018
9e1f0b3
BLE: Fix byte_array_t subscript operator
pan- Apr 4, 2018
5761caf
BLE: Extend ArrayView to encode size in type.
pan- Apr 4, 2018
cd39406
BLE: Add conversion function from byte_array_t to ArrayView.
pan- Apr 4, 2018
59a301a
BLE: CryptoToolbox enhancement
pan- Apr 4, 2018
04ab07f
BLE: Add documentation to CryptoToolbox.
pan- Apr 4, 2018
17b3e26
BLE: remove trace in Nordic PAL security manager
pan- Apr 4, 2018
539a11e
sign counter added
paul-szczepanek-arm Apr 4, 2018
2e1c5fc
completely redundant
paul-szczepanek-arm Apr 4, 2018
acfc5b2
set cordio csrk and counter
paul-szczepanek-arm Apr 4, 2018
dcff810
BLE: replace byte_array_t::buffer with byte_array_t::data
pan- Apr 4, 2018
1d74dfa
redundant funcs removed
paul-szczepanek-arm Apr 4, 2018
57149b6
monitor for signing events and set local counter
paul-szczepanek-arm Apr 4, 2018
f53a0e4
Merge branch 'security-manager-dev' of https://github.com/paul-szczep…
pan- Apr 9, 2018
fc27369
Merge pull request #27 from pan-/sc-nordic
paul-szczepanek-arm Apr 9, 2018
be912de
normalised the getters const insanity
paul-szczepanek-arm Apr 9, 2018
55d3423
typedef uint32_t
paul-szczepanek-arm Apr 9, 2018
637dcb9
missing *
paul-szczepanek-arm Apr 9, 2018
deeb0ea
redundant store for sign counter
paul-szczepanek-arm Apr 9, 2018
4fff205
sign counter kept track by the class using it
paul-szczepanek-arm Apr 9, 2018
a2484b6
Signing event monitor proxy to bind both server and client
paul-szczepanek-arm Apr 10, 2018
7006710
class in header
paul-szczepanek-arm Apr 10, 2018
1ba920a
missing init
paul-szczepanek-arm Apr 10, 2018
6c90064
Merge pull request #28 from paul-szczepanek-arm/signing-counter
paul-szczepanek-arm Apr 10, 2018
3aaedf6
fixed missed function rename
paul-szczepanek-arm Apr 11, 2018
1e6455d
check encryption before signing
paul-szczepanek-arm Apr 11, 2018
1f02913
BLE: Add Gap privacy interfaces.
pan- Apr 11, 2018
8643fd5
BLE: Add stub for signing API in Nordic pal security manager.
pan- Apr 11, 2018
1f81d20
Merge pull request #31 from pan-/signing-nordic-fix
paul-szczepanek-arm Apr 11, 2018
9be8837
Merge pull request #29 from paul-szczepanek-arm/no-sign-when-encrypted
paul-szczepanek-arm Apr 11, 2018
2811e00
BLE: Fix Gap privacy related documentation
pan- Apr 12, 2018
90c8595
BLE: Fix GAP privacy related signatures.
pan- Apr 12, 2018
d8f5100
Merge pull request #30 from pan-/sm-privacy
paul-szczepanek-arm Apr 12, 2018
6b67a6d
missing deref after signature change
paul-szczepanek-arm Apr 16, 2018
12899b1
fix const induced recursive call
paul-szczepanek-arm Apr 16, 2018
9a91676
moved init code into non-const for clarity
paul-szczepanek-arm Apr 16, 2018
a7f2384
BLE: Add default privacy configuration
pan- Apr 16, 2018
365f3d2
BLE: Add ah in Nordic CryptoToolbox.
pan- Apr 16, 2018
fb680db
BLE: Implement resolving list in Nordic PAL security manager
pan- Apr 16, 2018
a18283d
BLE: change nRF5xSecurityManager::get_resolving_list return
pan- Apr 16, 2018
2a9d80c
BLE: Change nRF5xSecurityManager::resolve_address signature
pan- Apr 16, 2018
b3c871c
BLE: Implement privacy on Nordic targets.
pan- Apr 16, 2018
51e1c76
BLE: Set default mac address to all 00.
pan- Apr 17, 2018
dfbf383
BLE: Fix const correctness of ::Gap::getRandomAddressType
pan- Apr 17, 2018
9643b57
BLE: Remove flag SecurityDistributionFlags_t::local_address_is_public
pan- Apr 17, 2018
ab6821a
BLE: Add a flag indicating if irk is stored in the distribution flags.
pan- Apr 17, 2018
e804151
BLE: Add a flag that indicate if the identity address is public or not.
pan- Apr 17, 2018
15fa3bf
BLE: Add a function to retrieve a device identity.
pan- Apr 17, 2018
6fc74fb
BLE: Fix MemorySecurityDB constructor warning.
pan- Apr 17, 2018
06e0aa3
BLE: flag that irk has been stored in the security entry.
pan- Apr 17, 2018
a3bb18d
BLE: store identity address type in DB.
pan- Apr 17, 2018
f905d2a
BLE: Improve DB entry lookup.
pan- Apr 17, 2018
ae8d5b4
BLE: Implement identity retrieval in secure DB.
pan- Apr 17, 2018
77b1903
BLE: Add bonded device to resolving list at the end of bonding.
pan- Apr 17, 2018
ede3d43
BLE: Add function to querry the list of identity addresses present in…
pan- Apr 17, 2018
951a6be
BLE: retrieve and fill resolving list at GenericSecurityManager startup.
pan- Apr 17, 2018
f56f57b
reorder enum to add new values at the end
paul-szczepanek-arm Apr 17, 2018
169e579
BLE: Handle new addresses type in GenericGap.
pan- Apr 17, 2018
a99a4b8
Merge pull request #33 from pan-/fix-cordio-addresses-type
paul-szczepanek-arm Apr 18, 2018
d1f3e4f
don't require master sends keys for signing key
paul-szczepanek-arm Apr 20, 2018
0b92397
BLE: Fix flag IRK stored in Memory security DB.
pan- Apr 20, 2018
cbb33ea
BLE: Add comments to GenericSecurityManager::on_identity_list_retrieved.
pan- Apr 20, 2018
6a90232
BLE: Update cordio stack binaries.
pan- Apr 20, 2018
b0d5ba3
BLE: return an error when application tries to turn down encryption.
pan- Apr 20, 2018
27d36ba
BLE: Add missing relational operator to SafeEnum.
pan- Apr 26, 2018
7afd01d
BLE: Add type describing ATT security requirements.
pan- Apr 26, 2018
9258b9d
BLE: Add security requement support into GattAttribute.
pan- Apr 26, 2018
1c18351
BLE: Remove unwanted documentation block of GattCharacteristic.
pan- Apr 26, 2018
486a3e0
BLE: Add fine grained security to GattCharacteristic.
pan- Apr 26, 2018
db565d3
BLE: remove useless flags in GattCharacteristic.
pan- Apr 26, 2018
80941af
BLE: Add : LESC authenticated encryption to the list of possible link…
pan- May 1, 2018
c4b78ad
BLE: Set default mac address to 00:00:00:00:00:00
pan- May 1, 2018
1e277ba
BLE: Lookup for non identity addresses when a secure entry is opened.
pan- May 1, 2018
74bc214
BLE - Security Manager PAL: Add a function to remove the peer csrk.
pan- May 1, 2018
2924bb4
Cordio PAL ATT Client: initialize the local sign counter to 0.
pan- May 1, 2018
e4813f4
Cordio PAL Security Manager: Copy locally own IRK.
pan- May 1, 2018
2da6fa3
Cordio PAL security manager: Copy locally own CSRK
pan- May 1, 2018
e39bb4b
BLE - GenericGattClient: Exploit ENCRYPTED_WITH_SC_AND_MITM encryption.
pan- May 1, 2018
2e3c7e8
Generic Security Manager: Set LinkKey to false unconditionally.
pan- May 1, 2018
9880db7
Generic Security Manager: Improve formating
pan- May 1, 2018
1ac95e1
ble - Generic Security Manager: set signing unconditionnal to role re…
pan- May 1, 2018
f90eacf
Generic Security Manager: remove peer csrk at disconnection
pan- May 1, 2018
41a3442
Generic Security Manager: Set ltk to stored when the peer ltk has bee…
pan- May 1, 2018
55eb703
Generic Security Manager: Set csrk to stored when the peer csrk has b…
pan- May 1, 2018
0f64b1c
CordioGattServer: Global refactoring
pan- May 1, 2018
d0c4d7a
Cordio: Forward server related events to CordioGattServer
pan- May 1, 2018
549a513
Cordio: Enable client and server signing
pan- May 1, 2018
01e3a00
Cordio: register server authorization callback.
pan- May 1, 2018
f79eeb0
Cordio: Update stack and pal to support LE security mode 2 level 2.
pan- May 2, 2018
ba0f18c
Fix casing in MemorySecurityDb.h
May 2, 2018
a2f206f
Merge pull request #37 from donatieng/fix_sec_db_casing
pan- May 2, 2018
4e5639f
BLE: Support encryption with secure connection key.
pan- May 2, 2018
7e043ea
Nordic BLE: Add stub implementation of remove_peer_csrk in security m…
pan- May 2, 2018
4f1e574
Cordio GattServer: Fix uses of designated initializer.
pan- May 2, 2018
0a59e00
Nordic BLE: Update GATT server security management
pan- May 2, 2018
b5e8d4e
Merge branch 'security-manager-dev' of https://github.com/paul-szczep…
pan- May 3, 2018
1a35f3e
fixed case in name
paul-szczepanek-arm May 8, 2018
214656a
fix case in #include
paul-szczepanek-arm May 8, 2018
9a0a086
Merge branch 'security-manager-dev' into sm-privacy-nordic
paul-szczepanek-arm May 8, 2018
ebb50a6
Merge pull request #32 from pan-/sm-privacy-nordic
paul-szczepanek-arm May 8, 2018
d36bed3
Merge pull request #39 from ARMmbed/master
paul-szczepanek-arm May 9, 2018
b8fe37a
disable ECDH if the platform doesn't support it
paul-szczepanek-arm May 9, 2018
2b02148
Merge branch 'master' into security-manager-dev
paul-szczepanek-arm May 9, 2018
c02b318
fixed case in filename
paul-szczepanek-arm May 9, 2018
6a26a8a
Merge branch 'security-manager-dev' of https://github.com/paul-szczep…
paul-szczepanek-arm May 9, 2018
b98ffa4
fix uppercase X in nrf5x
paul-szczepanek-arm May 9, 2018
79b3bc4
fix case
paul-szczepanek-arm May 9, 2018
367fe34
fix case
paul-szczepanek-arm May 9, 2018
37b11d8
fix case
paul-szczepanek-arm May 9, 2018
b4d7bb6
fix case
paul-szczepanek-arm May 9, 2018
fee9867
fix case
paul-szczepanek-arm May 9, 2018
b95da8d
remove crypto when missing ECDH
paul-szczepanek-arm May 9, 2018
473482d
move securitydb into generic
paul-szczepanek-arm May 10, 2018
40a403e
Merge branch 'security-manager-dev' of https://github.com/paul-szczep…
pan- May 10, 2018
afa4bdc
pull common logic into secure db
paul-szczepanek-arm May 11, 2018
678d494
init _db pointer
paul-szczepanek-arm May 11, 2018
3ca3c2d
Initial implementation of the Security Manager's Privacy feature for …
May 11, 2018
bcfbeb7
Some doc adjustments
May 11, 2018
71bd30d
More inline doc
May 11, 2018
40b6813
Fix wrong use of DmLlPrivEnabled()
May 11, 2018
a63f38e
moved logic to security db and left storage in memory and file dbs
paul-szczepanek-arm May 11, 2018
3af4d0b
get identity list function argument needs to have its own memory allo…
paul-szczepanek-arm May 11, 2018
33c46f5
Remove LL resolving 'shorcut' when adding a device to resolving list …
May 13, 2018
e9ad148
Added privacy-related methods overrides in GenericGap
May 13, 2018
7b4a813
Added stubs in GenericGap for privacy configuration methods
May 13, 2018
5734fca
Added set_address_resolution method in GAP Pal with Cordio implementa…
May 13, 2018
cf03d40
Doc fix in PalGap.h
May 13, 2018
4c5e2a8
Added method to update resolution settings in GenericGap
May 13, 2018
1a623e6
Added method to enable/disable privacy in GAP Pal with Cordio impl
May 13, 2018
2509a88
Revert set_privacy() in Cordio GAP PAL impl
May 13, 2018
620ebc3
Some fixes in GenericGap
May 13, 2018
6a2ffae
Update own address type generation in GenericGap
May 13, 2018
926efa4
Filter out advertising reports for unresolved addresses if required
May 13, 2018
21471bb
and make sure it compiles and that policy is only applied if privacy …
May 13, 2018
ab11773
write back counter, sync entry by hand;e
paul-szczepanek-arm May 13, 2018
69e35c4
Handle resolution policy for peripheral in GenericGap
May 13, 2018
23c6a69
key dist flags only in db now and not in control block of sec manager
paul-szczepanek-arm May 13, 2018
8f90875
reset entry now remove old keys
paul-szczepanek-arm May 13, 2018
d3524e2
doxygen
paul-szczepanek-arm May 13, 2018
a7f8d54
more doxygen
paul-szczepanek-arm May 13, 2018
f4f3a3c
store ltk and csrk but not irk sent status
paul-szczepanek-arm May 14, 2018
579cb5e
avoid setting flags twice
paul-szczepanek-arm May 14, 2018
a3d9d6c
Business logic for handling non-resolvable private addresses
May 14, 2018
472d3de
Give GenericGap access to the Security Manager's PAL
May 14, 2018
3523cdb
Missing initializer in GenericGap
May 14, 2018
67ec632
Added update_random_address() implementation in GenericGap
May 14, 2018
d8b63fc
Reordered initializers in GenericGap
May 14, 2018
6c6af1b
Added missing masks and fixed bit ordering in is_random_xx_address() …
May 14, 2018
f8244a3
review comments, init partly moved to restore, restore setting enable…
paul-szczepanek-arm May 15, 2018
49db7e2
restoring db file blanks file if set to not restore, allow reusing th…
paul-szczepanek-arm May 15, 2018
2860365
BLE: Improve readibility of condition.
pan- May 15, 2018
2c7ed99
BLE: Fix security requirements in GattCharacteristic.
pan- May 15, 2018
ee720f3
BLE: Comment security requirement elevation for signed writes.
pan- May 15, 2018
4acb393
Cordio BLE: Fix potential memory leak in GattServer.
pan- May 15, 2018
f1c3fdb
Merge pull request #35 from pan-/fix-encryption-rejection
pan- May 15, 2018
f7f1272
Nordic: Backport security manager pal for NRF5X targets.
pan- May 11, 2018
ed20b17
Nordic BLE: Fix link count
pan- May 15, 2018
f5ac8d6
Nordic: Adjust RAM requirements.
pan- May 15, 2018
02e3c13
Nordic BLE: Protect event signaled flag.
pan- May 15, 2018
dc54da0
Nordic BLE: Force signing and link requirements to false.
pan- May 15, 2018
2af5508
Nordic BLE: Do not conditionnaly assign sm key pointers.
pan- May 15, 2018
4a7c9a9
Nordic BLE: expose publically btle_handler
pan- May 15, 2018
68b3288
Nordic BLE: Fix signature of nRF5xGattServer::hwCallback
pan- May 15, 2018
8fbecf2
Nordic BLE: Fix incorrect assignement
pan- May 15, 2018
6810c02
Nordic BLE: Fix processing of events.
pan- May 15, 2018
3939695
Nordic BLE: simplify whitelist management.
pan- May 15, 2018
31f8cd1
Nordic BLE: Backport Gap from nRF5
pan- May 15, 2018
167a660
Nordic BLE: use pal security manager.
pan- May 15, 2018
6f24078
Nordic BLE: route event handling correctly.
pan- May 15, 2018
dd6a535
Nordic BLE: Remove peer manager handling.
pan- May 15, 2018
a22b4e5
Nordic BLE: Remove nordic gap init.
pan- May 15, 2018
ca5a9f3
Nordic BLE: remove unecessary nordic ble libraries.
pan- May 15, 2018
2cb6e65
Nordic BLE: Backport security fixes from nRF5 to nRF5x
pan- May 15, 2018
5c59868
templates for reading and writing to avoid repetition
paul-szczepanek-arm May 15, 2018
687ecc1
Merge pull request #45 from pan-/notdic-sdk-update
pan- May 15, 2018
288c395
Address Paul's comments
May 15, 2018
afcbdfc
Merge branch 'security-manager-dev' of https://github.com/paul-szczep…
pan- May 16, 2018
c2bbc94
handle init of an already initialised security db
paul-szczepanek-arm May 16, 2018
bcca759
reseting the security db
paul-szczepanek-arm May 16, 2018
1ae13bc
don't reset db on security manager reset as the docs require
paul-szczepanek-arm May 16, 2018
b43f27a
Merge pull request #43 from paul-szczepanek-arm/filedb
pan- May 16, 2018
1fdb57e
Removed set_privacy() API and added is_privacy_supported() check to P…
May 16, 2018
7ef7ef5
Fix is_random_xxx_address() functions in GenericGap that I had broken :)
May 16, 2018
3f7a7a4
Merge
May 16, 2018
ad09ba0
Fix handling of security escalation in on_connection_complete()
May 16, 2018
e1f9d06
Merge pull request #44 from donatieng/cordio-privacy
pan- May 16, 2018
6c44a78
Merge branch 'master' of https://github.com/ARMmbed/mbed-os into secu…
pan- May 16, 2018
37c036c
Nordic: Port privacy to softdevice v4.
pan- May 16, 2018
0c3d9e2
Merge pull request #47 from pan-/privacy-sd-4
pan- May 16, 2018
57a02d6
fixed signature
paul-szczepanek-arm May 16, 2018
b845a9d
fixed doxygen
paul-szczepanek-arm May 16, 2018
e5d9193
BLE: Revert address type changes.
pan- May 17, 2018
7f05fc0
BLE: Define peer_address_type_t
pan- May 17, 2018
0eb680a
GenericGap: Revert changes introduced to accomodate privacy.
pan- May 17, 2018
a052afd
BLE: Update APIs to take advantage of ble::peer_address_type_t
pan- May 17, 2018
7e69444
security manager doxygen overview updated
paul-szczepanek-arm May 17, 2018
a8ac925
privacy doxygen overview
paul-szczepanek-arm May 17, 2018
d361960
BLE: Use peer_address_type_t instead of the legacy address in securit…
pan- May 18, 2018
c3bcd10
BLE NRF52: Implement features related to peer_address_t
pan- May 18, 2018
608ad33
return error when not initialised
paul-szczepanek-arm May 18, 2018
439d002
new API call to change db at runtime
paul-szczepanek-arm May 18, 2018
9da64e5
refactor into separate functions for readability and correctness of p…
paul-szczepanek-arm May 18, 2018
ace491d
remove duplicate call
paul-szczepanek-arm May 18, 2018
cd9f12a
return error codes
paul-szczepanek-arm May 18, 2018
28766bd
BLE: Do not pass peer resolvable address in connection event handler
pan- May 21, 2018
d962fc0
BLE: factor code in GAP.
pan- May 21, 2018
4c1a309
Nordic BLE: Set own resolvable address to NULL.
pan- May 21, 2018
14a1095
Nordic BLE: Disable secure connection support on NRF51.
pan- May 21, 2018
17e8ed9
forward resolving list init
paul-szczepanek-arm May 21, 2018
4e3a9ff
Merge pull request #48 from paul-szczepanek-arm/sm-init-error
pan- May 21, 2018
62ba220
doxygen fix
paul-szczepanek-arm May 21, 2018
57ffa14
Nordic BLE: Backport privacy backward compatibility to NRF51
pan- May 21, 2018
cbba6dd
BLE PAL: Update connection event to support enhanced connection events.
pan- May 21, 2018
cc286fe
BLE: Cordio pal: Handle enhanced connection events.
pan- May 21, 2018
bdad5d6
BLE Generic: Handle peer address type backward compatibility.
pan- May 21, 2018
b973a8a
BLE: Add default constructor to peer_address_type_t
pan- May 21, 2018
fb9e0dc
BLE: Fix resolvable private address identification.
pan- May 21, 2018
7d916a9
fix not writing version number into the file
paul-szczepanek-arm May 22, 2018
1cd0c20
fix opening the file in the wrong mode
paul-szczepanek-arm May 22, 2018
84ff831
measure file size with SEEK_END
paul-szczepanek-arm May 22, 2018
08cb7ae
fix not writing flags in the file
paul-szczepanek-arm May 22, 2018
69a0c10
removing erroneous (and redundant) check
paul-szczepanek-arm May 22, 2018
d427fcf
Nordic BLE: Remove dependency to mbedtls for CryptoToolbox::ah.
pan- May 22, 2018
755cf5f
BLE NRF51: Defer connection to a common function.
pan- May 22, 2018
ba61c09
BLE NRF51: Resolve private address on connection in peripheral role
pan- May 22, 2018
cfe325c
BLE NRF51: Do not filter advertising packet in peripheral if the devi…
pan- May 22, 2018
80137be
Merge https://github.com/ARMmbed/mbed-os into security-manager-dev
paul-szczepanek-arm May 23, 2018
f4b7f32
Merge pull request #49 from pan-/fix-privacy-backward-compatibility
paul-szczepanek-arm May 23, 2018
de41355
Fix not processing all advertising reports if one is filtered out
paul-szczepanek-arm May 23, 2018
1ba5a11
BLE: Fix wrong macro name for IAR
pan- May 23, 2018
30dac7c
BLE NRF52: Fix identity address flag in connection request.
pan- May 23, 2018
ef20891
BLE NRF52: Report correct own address type for connection
pan- May 23, 2018
db4d14c
BLE NRF52: Fix advertising filtering when no bonds are present.
pan- May 23, 2018
fa9ab03
Merge pull request #51 from pan-/fix-iar-compilation
May 23, 2018
f08ac2e
Merge pull request #52 from pan-/fix-privacy-nrf52
May 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ tags

# Visual Studio Code
.vscode/

features/FEATURE_BLE/targets/TARGET_CORDIO/stack_backup/
235 changes: 203 additions & 32 deletions features/FEATURE_BLE/ble/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
#ifndef BLE_ARRAY_VIEW_H_
#define BLE_ARRAY_VIEW_H_

#include <algorithm>

#include <stddef.h>
#include <stdint.h>
#include "platform/mbed_assert.h"

/**
* @addtogroup ble
Expand All @@ -33,11 +36,18 @@

namespace ble {

/**
* Special value for the Size parameter of ArrayView.
* If the type use this value then the size of the array is stored in the object
* at runtime.
*/
#define ARRAY_VIEW_DYNAMIC_SIZE -1

/**
* Immutable view to an array.
*
* Array views encapsulate the pointer to an array and its size into a single
* object; however, it does not manage the lifetime of the array viewed.
* object or type; however, it does not manage the lifetime of the array viewed.
* You can use instances of ArrayView to replace the traditional pair of pointer
* and size arguments in function calls.
*
Expand All @@ -48,20 +58,28 @@ namespace ble {
* @note You can create ArrayView instances with the help of the function
* template make_ArrayView() and make_const_ArrayView().
*
* @note ArrayView<T, Size> objects can be implicitly converted to ArrayView<T>
* objects where required.
*
* @tparam T type of objects held by the array.
* @tparam Size The size of the array viewed. The default value
* ARRAY_VIEW_DYNAMIC_SIZE is special as it allows construction of ArrayView
* objects of any size (set at runtime).
*/
template<typename T>
template<typename T, ptrdiff_t Size = ARRAY_VIEW_DYNAMIC_SIZE>
struct ArrayView {

MBED_STATIC_ASSERT(Size >= 0, "Invalid size for an ArrayView");

/**
* Construct a view to an empty array.
*
* @post a call to size() will return 0, and data() will return NULL.
*/
ArrayView() : _array(0), _size(0) { }
ArrayView() : _array(NULL) { }

/**
* Construct an array view from a pointer to a buffer and its size.
* Construct an array view from a pointer to a buffer.
*
* @param array_ptr Pointer to the array data
* @param array_size Number of elements of T present in the array.
Expand All @@ -70,7 +88,9 @@ struct ArrayView {
* array_tpr.
*/
ArrayView(T* array_ptr, size_t array_size) :
_array(array_ptr), _size(array_size) { }
_array(array_ptr) {
MBED_ASSERT(array_size >= (size_t) Size);
}

/**
* Construct an array view from the reference to an array.
Expand All @@ -82,9 +102,8 @@ struct ArrayView {
* @post a call to size() will return Size, and data() will return
* a pointer to elements.
*/
template<size_t Size>
ArrayView(T (&elements)[Size]):
_array(elements), _size(Size) { }
_array(elements) { }

/**
* Return the size of the array viewed.
Expand All @@ -93,7 +112,7 @@ struct ArrayView {
*/
size_t size() const
{
return _size;
return _array ? Size : 0;
}

/**
Expand Down Expand Up @@ -144,40 +163,115 @@ struct ArrayView {
return _array;
}

private:
T* const _array;
};

/**
* ArrayView specialisation that handle dynamic array size.
*/
template<typename T>
struct ArrayView<T, ARRAY_VIEW_DYNAMIC_SIZE> {

/**
* Construct a view to an empty array.
*
* @post a call to size() will return 0, and data() will return NULL.
*/
ArrayView() : _array(0), _size(0) { }

/**
* Construct an array view from a pointer to a buffer and its size.
*
* @param array_ptr Pointer to the array data
* @param array_size Number of elements of T present in the array.
*
* @post a call to size() will return array_size and data() will return
* array_tpr.
*/
ArrayView(T* array_ptr, size_t array_size) :
_array(array_ptr), _size(array_size) { }

/**
* Equality operator.
* Construct an array view from the reference to an array.
*
* @param lhs Left hand side of the binary operation.
* @param rhs Right hand side of the binary operation.
* @param elements Reference to the array viewed.
*
* @tparam Size Number of elements of T presents in the array.
*
* @post a call to size() will return Size, and data() will return
* a pointer to elements.
*/
template<size_t Size>
ArrayView(T (&elements)[Size]):
_array(elements), _size(Size) { }


/**
* Construct a ArrayView object with a dynamic size from an ArrayView object
* with a static size.
* @param other The ArrayView object used to construct this.
*/
template<size_t Size>
ArrayView(ArrayView<T, Size> other):
_array(other.data()), _size(other.size()) { }

/**
* Return the size of the array viewed.
*
* @return True if arrays in input have the same size and the same content
* and false otherwise.
* @return The number of elements present in the array viewed.
*/
friend bool operator==(const ArrayView& lhs, const ArrayView& rhs)
size_t size() const
{
if (lhs.size() != rhs.size()) {
return false;
}
return _size;
}

if (lhs.data() == rhs.data()) {
return true;
}
/**
* Access to a mutable element of the array.
*
* @param index Element index to access.
*
* @return A reference to the element at the index specified in input.
*
* @pre index shall be less than size().
*/
T& operator[](size_t index)
{
return _array[index];
}

return memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
/**
* Access to an immutable element of the array.
*
* @param index Element index to access.
*
* @return A const reference to the element at the index specified in input.
*
* @pre index shall be less than size().
*/
const T& operator[](size_t index) const
{
return _array[index];
}

/**
* Not equal operator
* Get the raw pointer to the array.
*
* @param lhs Left hand side of the binary operation.
* @param rhs Right hand side of the binary operation.
* @return The raw pointer to the array.
*/
T* data()
{
return _array;
}

/**
* Get the raw const pointer to the array.
*
* @return True if arrays in input do not have the same size or the same
* content and false otherwise.
* @return The raw pointer to the array.
*/
friend bool operator!=(const ArrayView& lhs, const ArrayView& rhs)
const T* data() const
{
return !(lhs == rhs);
return _array;
}

private:
Expand All @@ -186,6 +280,45 @@ struct ArrayView {
};


/**
* Equality operator.
*
* @param lhs Left hand side of the binary operation.
* @param rhs Right hand side of the binary operation.
*
* @return True if arrays in input have the same size and the same content
* and false otherwise.
*/
template<typename T, ptrdiff_t LhsSize, ptrdiff_t RhsSize>
bool operator==(const ArrayView<T, LhsSize>& lhs, const ArrayView<T, LhsSize>& rhs)
{
if (lhs.size() != rhs.size()) {
return false;
}

if (lhs.data() == rhs.data()) {
return true;
}

return std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
}

/**
* Not equal operator
*
* @param lhs Left hand side of the binary operation.
* @param rhs Right hand side of the binary operation.
*
* @return True if arrays in input do not have the same size or the same
* content and false otherwise.
*/
template<typename T, ptrdiff_t LhsSize, ptrdiff_t RhsSize>
bool operator!=(const ArrayView<T, LhsSize>& lhs, const ArrayView<T, LhsSize>& rhs)
{
return !(lhs == rhs);
}


/**
* Generate an array view from a reference to a C/C++ array.
*
Expand All @@ -200,9 +333,28 @@ struct ArrayView {
* created 'inline'.
*/
template<typename T, size_t Size>
ArrayView<T> make_ArrayView(T (&elements)[Size])
ArrayView<T, Size> make_ArrayView(T (&elements)[Size])
{
return ArrayView<T>(elements);
return ArrayView<T, Size>(elements);
}

/**
* Generate an array view from a pointer to a C/C++ array.
*
* @tparam Size Number of items held in elements.
* @tparam T Type of elements held in elements.
*
* @param elements The reference to the array viewed.
*
* @return The ArrayView to elements.
*
* @note This helper avoids the typing of template parameter when ArrayView is
* created 'inline'.
*/
template<size_t Size, typename T>
ArrayView<T, Size> make_ArrayView(T* elements)
{
return ArrayView<T, Size>(elements, Size);
}

/**
Expand Down Expand Up @@ -237,9 +389,28 @@ ArrayView<T> make_ArrayView(T* array_ptr, size_t array_size)
* created 'inline'.
*/
template<typename T, size_t Size>
ArrayView<const T> make_const_ArrayView(T (&elements)[Size])
ArrayView<const T, Size> make_const_ArrayView(T (&elements)[Size])
{
return ArrayView<const T, Size>(elements);
}

/**
* Generate a const array view from a pointer to a C/C++ array.
*
* @tparam Size Number of items held in elements.
* @tparam T Type of elements held in elements.
*
* @param elements The reference to the array viewed.
*
* @return The ArrayView to elements.
*
* @note This helper avoids the typing of template parameter when ArrayView is
* created 'inline'.
*/
template<size_t Size, typename T>
ArrayView<const T, Size> make_const_ArrayView(const T* elements)
{
return ArrayView<const T>(elements);
return ArrayView<const T, Size>(elements, Size);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions features/FEATURE_BLE/ble/BLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,7 @@ class BLE
ble_error_t setAddress(
BLEProtocol::AddressType_t type,
const BLEProtocol::AddressBytes_t address
) {
return gap().setAddress(type, address);
}
);

/**
* Fetch the Bluetooth Low Energy MAC address and type.
Expand Down Expand Up @@ -1009,9 +1007,7 @@ class BLE
ble_error_t connect(const BLEProtocol::AddressBytes_t peerAddr,
BLEProtocol::AddressType_t peerAddrType = BLEProtocol::AddressType::RANDOM_STATIC,
const Gap::ConnectionParams_t *connectionParams = NULL,
const GapScanningParams *scanParams = NULL) {
return gap().connect(peerAddr, peerAddrType, connectionParams, scanParams);
}
const GapScanningParams *scanParams = NULL);

/**
* This call initiates the disconnection procedure, and its completion is
Expand Down Expand Up @@ -1045,9 +1041,7 @@ class BLE
* connection.
*/
MBED_DEPRECATED("Use ble.gap().disconnect(...)")
ble_error_t disconnect(Gap::DisconnectionReason_t reason) {
return gap().disconnect(reason);
}
ble_error_t disconnect(Gap::DisconnectionReason_t reason);

/**
* Returns the current Gap state of the device using a bitmask that
Expand Down
Loading