Nano RGB LED mesh

Dependencies:   nrf51-sdk

Dependents:   BLENano_Mesh

Fork of nRF51822 by Nordic Semiconductor

Committer:
Jackson_lv
Date:
Thu Apr 28 08:52:31 2016 +0000
Revision:
639:afb5817801fd
Parent:
613:cbc548e9df98
Nano RGB LED mesh

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 558:c4b56f9d6f3b 1 /* mbed Microcontroller Library
vcoubard 558:c4b56f9d6f3b 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 558:c4b56f9d6f3b 3 *
vcoubard 558:c4b56f9d6f3b 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 558:c4b56f9d6f3b 5 * you may not use this file except in compliance with the License.
vcoubard 558:c4b56f9d6f3b 6 * You may obtain a copy of the License at
vcoubard 558:c4b56f9d6f3b 7 *
vcoubard 558:c4b56f9d6f3b 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 558:c4b56f9d6f3b 9 *
vcoubard 558:c4b56f9d6f3b 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 558:c4b56f9d6f3b 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 558:c4b56f9d6f3b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 558:c4b56f9d6f3b 13 * See the License for the specific language governing permissions and
vcoubard 558:c4b56f9d6f3b 14 * limitations under the License.
vcoubard 558:c4b56f9d6f3b 15 */
vcoubard 558:c4b56f9d6f3b 16
vcoubard 558:c4b56f9d6f3b 17 #ifndef __NRF51822_SECURITY_MANAGER_H__
vcoubard 558:c4b56f9d6f3b 18 #define __NRF51822_SECURITY_MANAGER_H__
vcoubard 558:c4b56f9d6f3b 19
vcoubard 558:c4b56f9d6f3b 20 #include <stddef.h>
vcoubard 558:c4b56f9d6f3b 21
vcoubard 598:814c1ce92947 22 #include "nRF5xGap.h"
vcoubard 558:c4b56f9d6f3b 23 #include "ble/SecurityManager.h"
vcoubard 558:c4b56f9d6f3b 24 #include "btle_security.h"
vcoubard 558:c4b56f9d6f3b 25
vcoubard 558:c4b56f9d6f3b 26 class nRF5xSecurityManager : public SecurityManager
vcoubard 558:c4b56f9d6f3b 27 {
vcoubard 558:c4b56f9d6f3b 28 public:
vcoubard 558:c4b56f9d6f3b 29 /* Functions that must be implemented from SecurityManager */
vcoubard 558:c4b56f9d6f3b 30 virtual ble_error_t init(bool enableBonding,
vcoubard 558:c4b56f9d6f3b 31 bool requireMITM,
vcoubard 558:c4b56f9d6f3b 32 SecurityIOCapabilities_t iocaps,
vcoubard 558:c4b56f9d6f3b 33 const Passkey_t passkey) {
vcoubard 558:c4b56f9d6f3b 34 return btle_initializeSecurity(enableBonding, requireMITM, iocaps, passkey);
vcoubard 558:c4b56f9d6f3b 35 }
vcoubard 558:c4b56f9d6f3b 36
vcoubard 558:c4b56f9d6f3b 37 virtual ble_error_t getLinkSecurity(Gap::Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) {
vcoubard 558:c4b56f9d6f3b 38 return btle_getLinkSecurity(connectionHandle, securityStatusP);
vcoubard 558:c4b56f9d6f3b 39 }
vcoubard 558:c4b56f9d6f3b 40
vcoubard 558:c4b56f9d6f3b 41 virtual ble_error_t setLinkSecurity(Gap::Handle_t connectionHandle, SecurityMode_t securityMode) {
vcoubard 558:c4b56f9d6f3b 42 return btle_setLinkSecurity(connectionHandle, securityMode);
vcoubard 558:c4b56f9d6f3b 43 }
vcoubard 558:c4b56f9d6f3b 44
vcoubard 558:c4b56f9d6f3b 45 virtual ble_error_t purgeAllBondingState(void) {
vcoubard 558:c4b56f9d6f3b 46 return btle_purgeAllBondingState();
vcoubard 558:c4b56f9d6f3b 47 }
vcoubard 558:c4b56f9d6f3b 48
vcoubard 575:7023a8204a1b 49 /**
vcoubard 613:cbc548e9df98 50 * @brief Returns a list of addresses from peers in the stacks bond table.
vcoubard 613:cbc548e9df98 51 *
vcoubard 613:cbc548e9df98 52 * @param[in/out] addresses
vcoubard 613:cbc548e9df98 53 * (on input) @ref Gap::Whitelist_t structure where at
vcoubard 613:cbc548e9df98 54 * most addresses.capacity addresses from bonded peers will
vcoubard 613:cbc548e9df98 55 * be stored.
vcoubard 613:cbc548e9df98 56 * (on output) A copy of the addresses from bonded peers.
vcoubard 613:cbc548e9df98 57 *
vcoubard 613:cbc548e9df98 58 * @return
vcoubard 613:cbc548e9df98 59 * BLE_ERROR_NONE if successful.
vcoubard 613:cbc548e9df98 60 */
vcoubard 613:cbc548e9df98 61 virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const {
vcoubard 613:cbc548e9df98 62 uint8_t i;
vcoubard 613:cbc548e9df98 63
vcoubard 613:cbc548e9df98 64 ble_gap_whitelist_t whitelistFromBondTable;
vcoubard 613:cbc548e9df98 65 ble_gap_addr_t *addressPtr[YOTTA_CFG_WHITELIST_MAX_SIZE];
vcoubard 613:cbc548e9df98 66 ble_gap_irk_t *irkPtr[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
vcoubard 613:cbc548e9df98 67
vcoubard 613:cbc548e9df98 68 /* Initialize the structure so that we get as many addreses as the whitelist can hold */
vcoubard 613:cbc548e9df98 69 whitelistFromBondTable.addr_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
vcoubard 613:cbc548e9df98 70 whitelistFromBondTable.pp_addrs = addressPtr;
vcoubard 613:cbc548e9df98 71 whitelistFromBondTable.irk_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
vcoubard 613:cbc548e9df98 72 whitelistFromBondTable.pp_irks = irkPtr;
vcoubard 613:cbc548e9df98 73
vcoubard 613:cbc548e9df98 74 ble_error_t error = createWhitelistFromBondTable(whitelistFromBondTable);
vcoubard 613:cbc548e9df98 75 if (error != BLE_ERROR_NONE) {
vcoubard 613:cbc548e9df98 76 addresses.size = 0;
vcoubard 613:cbc548e9df98 77 return error;
vcoubard 613:cbc548e9df98 78 }
vcoubard 613:cbc548e9df98 79
vcoubard 613:cbc548e9df98 80 /* Put all the addresses in the structure */
vcoubard 613:cbc548e9df98 81 for (i = 0; i < whitelistFromBondTable.addr_count; ++i) {
vcoubard 613:cbc548e9df98 82 if (i >= addresses.capacity) {
vcoubard 613:cbc548e9df98 83 /* Ran out of space in the output Gap::Whitelist_t */
vcoubard 613:cbc548e9df98 84 addresses.size = i;
vcoubard 613:cbc548e9df98 85 return BLE_ERROR_NONE;
vcoubard 613:cbc548e9df98 86 }
vcoubard 613:cbc548e9df98 87 memcpy(&addresses.addresses[i], whitelistFromBondTable.pp_addrs[i], sizeof(BLEProtocol::Address_t));
vcoubard 613:cbc548e9df98 88 }
vcoubard 613:cbc548e9df98 89
vcoubard 613:cbc548e9df98 90 /* Update the current address count */
vcoubard 613:cbc548e9df98 91 addresses.size = i;
vcoubard 613:cbc548e9df98 92
vcoubard 613:cbc548e9df98 93 /* The assumption here is that the underlying implementation of
vcoubard 613:cbc548e9df98 94 * createWhitelistFromBondTable() will not return the private resolvable
vcoubard 613:cbc548e9df98 95 * addresses (which is the case in the SoftDevice). Rather it returns the
vcoubard 613:cbc548e9df98 96 * IRKs, so we need to generate the private resolvable address by ourselves.
vcoubard 613:cbc548e9df98 97 */
vcoubard 613:cbc548e9df98 98 for (i = 0; i < whitelistFromBondTable.irk_count; ++i) {
vcoubard 613:cbc548e9df98 99 if (i + addresses.size >= addresses.capacity) {
vcoubard 613:cbc548e9df98 100 /* Ran out of space in the output Gap::Whitelist_t */
vcoubard 613:cbc548e9df98 101 addresses.size += i;
vcoubard 613:cbc548e9df98 102 return BLE_ERROR_NONE;
vcoubard 613:cbc548e9df98 103 }
vcoubard 613:cbc548e9df98 104 btle_generateResolvableAddress(
vcoubard 613:cbc548e9df98 105 *whitelistFromBondTable.pp_irks[i],
vcoubard 613:cbc548e9df98 106 (ble_gap_addr_t &) addresses.addresses[i + addresses.size]
vcoubard 613:cbc548e9df98 107 );
vcoubard 613:cbc548e9df98 108 }
vcoubard 613:cbc548e9df98 109
vcoubard 613:cbc548e9df98 110 /* Update the current address count */
vcoubard 613:cbc548e9df98 111 addresses.size += i;
vcoubard 613:cbc548e9df98 112
vcoubard 613:cbc548e9df98 113 return BLE_ERROR_NONE;
vcoubard 613:cbc548e9df98 114 }
vcoubard 613:cbc548e9df98 115
vcoubard 613:cbc548e9df98 116 /**
vcoubard 575:7023a8204a1b 117 * @brief Clear nRF5xSecurityManager's state.
vcoubard 575:7023a8204a1b 118 *
vcoubard 575:7023a8204a1b 119 * @return
vcoubard 575:7023a8204a1b 120 * BLE_ERROR_NONE if successful.
vcoubard 575:7023a8204a1b 121 */
vcoubard 575:7023a8204a1b 122 virtual ble_error_t reset(void)
vcoubard 575:7023a8204a1b 123 {
vcoubard 575:7023a8204a1b 124 if (SecurityManager::reset() != BLE_ERROR_NONE) {
vcoubard 575:7023a8204a1b 125 return BLE_ERROR_INVALID_STATE;
vcoubard 575:7023a8204a1b 126 }
vcoubard 575:7023a8204a1b 127
vcoubard 575:7023a8204a1b 128 return BLE_ERROR_NONE;
vcoubard 575:7023a8204a1b 129 }
vcoubard 575:7023a8204a1b 130
vcoubard 598:814c1ce92947 131 bool hasInitialized(void) const {
vcoubard 598:814c1ce92947 132 return btle_hasInitializedSecurity();
vcoubard 598:814c1ce92947 133 }
vcoubard 598:814c1ce92947 134
vcoubard 558:c4b56f9d6f3b 135 public:
vcoubard 575:7023a8204a1b 136 /*
vcoubard 575:7023a8204a1b 137 * Allow instantiation from nRF5xn when required.
vcoubard 575:7023a8204a1b 138 */
vcoubard 575:7023a8204a1b 139 friend class nRF5xn;
vcoubard 575:7023a8204a1b 140
vcoubard 558:c4b56f9d6f3b 141 nRF5xSecurityManager() {
vcoubard 558:c4b56f9d6f3b 142 /* empty */
vcoubard 558:c4b56f9d6f3b 143 }
vcoubard 558:c4b56f9d6f3b 144
vcoubard 558:c4b56f9d6f3b 145 private:
vcoubard 558:c4b56f9d6f3b 146 nRF5xSecurityManager(const nRF5xSecurityManager &);
vcoubard 558:c4b56f9d6f3b 147 const nRF5xSecurityManager& operator=(const nRF5xSecurityManager &);
vcoubard 598:814c1ce92947 148
vcoubard 599:3e66e1eb264d 149 /*
vcoubard 599:3e66e1eb264d 150 * Expose an interface that allows us to query the SoftDevice bond table
vcoubard 599:3e66e1eb264d 151 * and extract a whitelist.
vcoubard 599:3e66e1eb264d 152 */
vcoubard 598:814c1ce92947 153 ble_error_t createWhitelistFromBondTable(ble_gap_whitelist_t &whitelistFromBondTable) const {
vcoubard 598:814c1ce92947 154 return btle_createWhitelistFromBondTable(&whitelistFromBondTable);
vcoubard 598:814c1ce92947 155 }
vcoubard 598:814c1ce92947 156
vcoubard 599:3e66e1eb264d 157 /*
vcoubard 599:3e66e1eb264d 158 * Given a BLE address and a IRK this function check whether the address
vcoubard 599:3e66e1eb264d 159 * can be generated from the IRK. To do so, this function uses the hash
vcoubard 599:3e66e1eb264d 160 * function and algorithm described in the Bluetooth low Energy
vcoubard 599:3e66e1eb264d 161 * Specification. Internally, Nordic SDK functions are used.
vcoubard 599:3e66e1eb264d 162 */
vcoubard 598:814c1ce92947 163 bool matchAddressAndIrk(ble_gap_addr_t *address, ble_gap_irk_t *irk) const {
vcoubard 598:814c1ce92947 164 return btle_matchAddressAndIrk(address, irk);
vcoubard 598:814c1ce92947 165 }
vcoubard 599:3e66e1eb264d 166
vcoubard 599:3e66e1eb264d 167 /*
vcoubard 599:3e66e1eb264d 168 * Give nRF5xGap access to createWhitelistFromBondTable() and
vcoubard 599:3e66e1eb264d 169 * matchAddressAndIrk()
vcoubard 599:3e66e1eb264d 170 */
vcoubard 598:814c1ce92947 171 friend class nRF5xGap;
vcoubard 558:c4b56f9d6f3b 172 };
vcoubard 558:c4b56f9d6f3b 173
rgrover1 388:db85a09c27ef 174 #endif // ifndef __NRF51822_SECURITY_MANAGER_H__