cc y / mbed

Fork of mbed by mbed official

Committer:
kaoshen
Date:
Tue Jan 17 23:27:32 2017 +0000
Revision:
135:fce8a9387ed1
Parent:
123:b0220dba8be7
333 ADS1115 ADC1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 123:b0220dba8be7 1 /*
Kojto 123:b0220dba8be7 2 * Copyright (c) 2015 Nordic Semiconductor ASA
Kojto 123:b0220dba8be7 3 * All rights reserved.
Kojto 123:b0220dba8be7 4 *
Kojto 123:b0220dba8be7 5 * Redistribution and use in source and binary forms, with or without modification,
Kojto 123:b0220dba8be7 6 * are permitted provided that the following conditions are met:
Kojto 123:b0220dba8be7 7 *
Kojto 123:b0220dba8be7 8 * 1. Redistributions of source code must retain the above copyright notice, this list
Kojto 123:b0220dba8be7 9 * of conditions and the following disclaimer.
Kojto 123:b0220dba8be7 10 *
Kojto 123:b0220dba8be7 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
Kojto 123:b0220dba8be7 12 * integrated circuit in a product or a software update for such product, must reproduce
Kojto 123:b0220dba8be7 13 * the above copyright notice, this list of conditions and the following disclaimer in
Kojto 123:b0220dba8be7 14 * the documentation and/or other materials provided with the distribution.
Kojto 123:b0220dba8be7 15 *
Kojto 123:b0220dba8be7 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
Kojto 123:b0220dba8be7 17 * used to endorse or promote products derived from this software without specific prior
Kojto 123:b0220dba8be7 18 * written permission.
Kojto 123:b0220dba8be7 19 *
Kojto 123:b0220dba8be7 20 * 4. This software, with or without modification, must only be used with a
Kojto 123:b0220dba8be7 21 * Nordic Semiconductor ASA integrated circuit.
Kojto 123:b0220dba8be7 22 *
Kojto 123:b0220dba8be7 23 * 5. Any software provided in binary or object form under this license must not be reverse
Kojto 123:b0220dba8be7 24 * engineered, decompiled, modified and/or disassembled.
Kojto 123:b0220dba8be7 25 *
Kojto 123:b0220dba8be7 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Kojto 123:b0220dba8be7 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Kojto 123:b0220dba8be7 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 123:b0220dba8be7 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Kojto 123:b0220dba8be7 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Kojto 123:b0220dba8be7 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Kojto 123:b0220dba8be7 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Kojto 123:b0220dba8be7 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 123:b0220dba8be7 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Kojto 123:b0220dba8be7 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 123:b0220dba8be7 36 *
Kojto 123:b0220dba8be7 37 */
Kojto 123:b0220dba8be7 38
Kojto 123:b0220dba8be7 39
Kojto 123:b0220dba8be7 40
Kojto 123:b0220dba8be7 41 /**
Kojto 123:b0220dba8be7 42 * @file peer_manager_types.h
Kojto 123:b0220dba8be7 43 *
Kojto 123:b0220dba8be7 44 * @addtogroup peer_manager
Kojto 123:b0220dba8be7 45 * @{
Kojto 123:b0220dba8be7 46 */
Kojto 123:b0220dba8be7 47
Kojto 123:b0220dba8be7 48 #ifndef PEER_MANAGER_TYPES_H__
Kojto 123:b0220dba8be7 49 #define PEER_MANAGER_TYPES_H__
Kojto 123:b0220dba8be7 50
Kojto 123:b0220dba8be7 51 #include <stdint.h>
Kojto 123:b0220dba8be7 52 #include <stdbool.h>
Kojto 123:b0220dba8be7 53 #include <stddef.h>
Kojto 123:b0220dba8be7 54 #include "nrf.h"
Kojto 123:b0220dba8be7 55 #include "nrf_ble_gap.h"
Kojto 123:b0220dba8be7 56 #include "nrf_ble_hci.h"
Kojto 123:b0220dba8be7 57 #include "app_util.h"
Kojto 123:b0220dba8be7 58 #include "app_util_platform.h"
Kojto 123:b0220dba8be7 59 #include "ble_gatt_db.h"
Kojto 123:b0220dba8be7 60
Kojto 123:b0220dba8be7 61
Kojto 123:b0220dba8be7 62 /**@brief Handle to uniquely identify a peer for which we have persistently stored data.
Kojto 123:b0220dba8be7 63 */
Kojto 123:b0220dba8be7 64 typedef uint16_t pm_peer_id_t;
Kojto 123:b0220dba8be7 65
Kojto 123:b0220dba8be7 66 /**@brief Type that is used for write prepares (used to reserve space in flash).
Kojto 123:b0220dba8be7 67 */
Kojto 123:b0220dba8be7 68 typedef uint32_t pm_prepare_token_t;
Kojto 123:b0220dba8be7 69
Kojto 123:b0220dba8be7 70 /**@brief Type that is used to hold a reference to a stored item in flash.
Kojto 123:b0220dba8be7 71 */
Kojto 123:b0220dba8be7 72 typedef uint32_t pm_store_token_t;
Kojto 123:b0220dba8be7 73
Kojto 123:b0220dba8be7 74 /**@brief Errors from security procedures in Peer Manager.
Kojto 123:b0220dba8be7 75 *
Kojto 123:b0220dba8be7 76 * @details Possible values are defined in @ref PM_SEC_ERRORS and @ref BLE_GAP_SEC_STATUS.
Kojto 123:b0220dba8be7 77 */
Kojto 123:b0220dba8be7 78 typedef uint16_t pm_sec_error_code_t;
Kojto 123:b0220dba8be7 79
Kojto 123:b0220dba8be7 80
Kojto 123:b0220dba8be7 81 //lint -emacro(516,PM_LOCAL_DB_LEN_OVERHEAD_BYTES)
Kojto 123:b0220dba8be7 82
Kojto 123:b0220dba8be7 83 #define PM_PEER_ID_INVALID 0xFFFF /**< @brief Invalid value for @ref pm_peer_id_t. */
Kojto 123:b0220dba8be7 84 #define PM_STORE_TOKEN_INVALID 0 /**< @brief Invalid value for store token. */
Kojto 123:b0220dba8be7 85 #define PM_PEER_ID_N_AVAILABLE_IDS 256 /**< @brief The number of available peer IDs. */
Kojto 123:b0220dba8be7 86 #define PM_LOCAL_DB_LEN_OVERHEAD_BYTES offsetof(pm_peer_data_local_gatt_db_t, data) /**< @brief The static-length part of the local GATT data struct. */
Kojto 123:b0220dba8be7 87
Kojto 123:b0220dba8be7 88
Kojto 123:b0220dba8be7 89 #define PM_CONN_SEC_ERROR_BASE 0x1000 /**< @brief The base for Peer Manager defined errors. See @ref PM_SEC_ERRORS and @ref pm_sec_error_code_t. */
Kojto 123:b0220dba8be7 90
Kojto 123:b0220dba8be7 91
Kojto 123:b0220dba8be7 92 /**@defgroup PM_SEC_ERRORS Peer Manager defined security errors
Kojto 123:b0220dba8be7 93 *
Kojto 123:b0220dba8be7 94 * @details The first 256 numbers in this range correspond to the status codes in
Kojto 123:b0220dba8be7 95 * @ref BLE_HCI_STATUS_CODES.
Kojto 123:b0220dba8be7 96 * @{ */
Kojto 123:b0220dba8be7 97 #define PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING (PM_CONN_SEC_ERROR_BASE + 0x06) /**< @brief Encryption failed because the peripheral has lost the LTK for this bond. See also @ref BLE_HCI_STATUS_CODE_PIN_OR_KEY_MISSING and Table 3.7 ("Pairing Failed Reason Codes") in the Bluetooth Core Specification 4.2, section 3.H.3.5.5 (@linkBLEcore). */
Kojto 123:b0220dba8be7 98 #define PM_CONN_SEC_ERROR_MIC_FAILURE (PM_CONN_SEC_ERROR_BASE + 0x3D) /**< @brief Encryption ended with disconnection because of mismatching keys or a stray packet during a procedure. See the SoftDevice GAP Message Sequence Charts on encryption (@linkBLEMSCgap), the Bluetooth Core Specification 4.2, sections 6.B.5.1.3.1 and 3.H.3.5.5 (@linkBLEcore), and @ref BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE. */
Kojto 123:b0220dba8be7 99 #define PM_CONN_SEC_ERROR_DISCONNECT (PM_CONN_SEC_ERROR_BASE + 0x100) /**< @brief Pairing or encryption did not finish before the link disconnected for an unrelated reason. */
Kojto 123:b0220dba8be7 100 #define PM_CONN_SEC_ERROR_SMP_TIMEOUT (PM_CONN_SEC_ERROR_BASE + 0x101) /**< @brief Pairing/bonding could not start because an SMP time-out has already happened on this link. This means that no more pairing or bonding can happen on this link. To be able to pair or bond, the link must be disconnected and then reconnected. See Bluetooth Core Specification 4.2 section 3.H.3.4 (@linkBLEcore). */
Kojto 123:b0220dba8be7 101 /** @} */
Kojto 123:b0220dba8be7 102
Kojto 123:b0220dba8be7 103
Kojto 123:b0220dba8be7 104
Kojto 123:b0220dba8be7 105 /**@defgroup PM_PEER_ID_VERSIONS All versions of Peer IDs.
Kojto 123:b0220dba8be7 106 * @brief The data ID for each iteration of the data formats in flash.
Kojto 123:b0220dba8be7 107 * @details Each time the format (in flash) of a piece of peer data changes, the data ID will also
Kojto 123:b0220dba8be7 108 * be updated. This list of defines is a record of each data ID that has ever existed, and
Kojto 123:b0220dba8be7 109 * code that caters to legacy formats can find the relevant IDs here.
Kojto 123:b0220dba8be7 110 * @{ */
Kojto 123:b0220dba8be7 111 #define PM_PEER_DATA_ID_FIRST_VX 0 /**< @brief The smallest data ID. */
Kojto 123:b0220dba8be7 112 #define PM_PEER_DATA_ID_BONDING_V1 0 /**< @brief The data ID of the first version of bonding data. */
Kojto 123:b0220dba8be7 113 #define PM_PEER_DATA_ID_BONDING_V2 7 /**< @brief The data ID of the second version of bonding data. */
Kojto 123:b0220dba8be7 114 #define PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING_V1 1 /**< @brief The data ID of the first version of the service changed pending flag. */
Kojto 123:b0220dba8be7 115 #define PM_PEER_DATA_ID_GATT_LOCAL_V1 2 /**< @brief The data ID of the first version of local GATT data. */
Kojto 123:b0220dba8be7 116 #define PM_PEER_DATA_ID_GATT_LOCAL_V2 8 /**< @brief The data ID of the second version of local GATT data. */
Kojto 123:b0220dba8be7 117 #define PM_PEER_DATA_ID_GATT_REMOTE_V1 3 /**< @brief The data ID of the first version of remote GATT data. */
Kojto 123:b0220dba8be7 118 #define PM_PEER_DATA_ID_APPLICATION_V1 4 /**< @brief The data ID of the first version of application data. */
Kojto 123:b0220dba8be7 119 #define PM_PEER_DATA_ID_GATT_REMOTE_V2 5 /**< @brief The data ID of the second version of remote GATT data. */
Kojto 123:b0220dba8be7 120 #define PM_PEER_DATA_ID_PEER_RANK_V1 6 /**< @brief The data ID of the first version of the rank. */
Kojto 123:b0220dba8be7 121 #define PM_PEER_DATA_ID_LAST_VX 9 /**< @brief The data ID after the last valid one. */
Kojto 123:b0220dba8be7 122 #define PM_PEER_DATA_ID_INVALID_VX 0xFF /**< @brief A data ID guaranteed to be invalid. */
Kojto 123:b0220dba8be7 123 /**@}*/
Kojto 123:b0220dba8be7 124
Kojto 123:b0220dba8be7 125
Kojto 123:b0220dba8be7 126 /**@brief The different types of data associated with a peer.
Kojto 123:b0220dba8be7 127 */
Kojto 123:b0220dba8be7 128 typedef enum
Kojto 123:b0220dba8be7 129 {
Kojto 123:b0220dba8be7 130 PM_PEER_DATA_ID_FIRST = PM_PEER_DATA_ID_FIRST_VX, /**< @brief The smallest data ID. */
Kojto 123:b0220dba8be7 131 PM_PEER_DATA_ID_BONDING = PM_PEER_DATA_ID_BONDING_V2, /**< @brief The data ID for bonding data. See @ref pm_peer_data_bonding_t. */
Kojto 123:b0220dba8be7 132 PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING_V1, /**< @brief The data ID for service changed state. */
Kojto 123:b0220dba8be7 133 PM_PEER_DATA_ID_GATT_LOCAL = PM_PEER_DATA_ID_GATT_LOCAL_V2, /**< @brief The data ID for local GATT data (sys attributes). See @ref pm_peer_data_local_gatt_db_t. */
Kojto 123:b0220dba8be7 134 PM_PEER_DATA_ID_GATT_REMOTE = PM_PEER_DATA_ID_GATT_REMOTE_V2, /**< @brief The data ID for remote GATT data. */
Kojto 123:b0220dba8be7 135 PM_PEER_DATA_ID_PEER_RANK = PM_PEER_DATA_ID_PEER_RANK_V1, /**< @brief The data ID for peer rank. See @ref pm_peer_rank_highest. */
Kojto 123:b0220dba8be7 136 PM_PEER_DATA_ID_APPLICATION = PM_PEER_DATA_ID_APPLICATION_V1, /**< @brief The data ID for application data. */
Kojto 123:b0220dba8be7 137 PM_PEER_DATA_ID_LAST = PM_PEER_DATA_ID_LAST_VX, /**< @brief One more than the highest data ID. */
Kojto 123:b0220dba8be7 138 PM_PEER_DATA_ID_INVALID = PM_PEER_DATA_ID_INVALID_VX, /**< @brief A data ID guaranteed to be invalid. */
Kojto 123:b0220dba8be7 139 } pm_peer_data_id_t;
Kojto 123:b0220dba8be7 140
Kojto 123:b0220dba8be7 141
Kojto 123:b0220dba8be7 142 /**@brief Different procedures that can lead to an encrypted link.
Kojto 123:b0220dba8be7 143 */
Kojto 123:b0220dba8be7 144 typedef enum
Kojto 123:b0220dba8be7 145 {
Kojto 123:b0220dba8be7 146 PM_LINK_SECURED_PROCEDURE_ENCRYPTION, /**< @brief Using an LTK that was shared during a previous bonding procedure to encrypt the link. */
Kojto 123:b0220dba8be7 147 PM_LINK_SECURED_PROCEDURE_BONDING, /**< @brief A pairing procedure, followed by a bonding procedure. */
Kojto 123:b0220dba8be7 148 PM_LINK_SECURED_PROCEDURE_PAIRING, /**< @brief A pairing procedure with no bonding. */
Kojto 123:b0220dba8be7 149 } pm_conn_sec_procedure_t;
Kojto 123:b0220dba8be7 150
Kojto 123:b0220dba8be7 151
Kojto 123:b0220dba8be7 152 /**@brief Configuration of a security procedure.
Kojto 123:b0220dba8be7 153 */
Kojto 123:b0220dba8be7 154 typedef struct
Kojto 123:b0220dba8be7 155 {
Kojto 123:b0220dba8be7 156 bool allow_repairing; /** @brief Whether to allow the peer to pair if it wants to, but is already bonded. If this is false, the procedure is rejected, and no more events are sent. Default: false. */
Kojto 123:b0220dba8be7 157 } pm_conn_sec_config_t;
Kojto 123:b0220dba8be7 158
Kojto 123:b0220dba8be7 159
Kojto 123:b0220dba8be7 160 /**@brief Data associated with a bond to a peer.
Kojto 123:b0220dba8be7 161 */
Kojto 123:b0220dba8be7 162 typedef struct
Kojto 123:b0220dba8be7 163 {
Kojto 123:b0220dba8be7 164 uint8_t own_role; /**< @brief The role of the local device during bonding. */
Kojto 123:b0220dba8be7 165 ble_gap_id_key_t peer_id; /**< @brief The peer's peer address and identity resolution key. */
Kojto 123:b0220dba8be7 166 ble_gap_enc_key_t peer_ltk; /**< @brief The peer's long-term encryption key. */
Kojto 123:b0220dba8be7 167 ble_gap_enc_key_t own_ltk; /**< @brief Locally generated long-term encryption key, distributed to the peer. */
Kojto 123:b0220dba8be7 168 } pm_peer_data_bonding_t;
Kojto 123:b0220dba8be7 169
Kojto 123:b0220dba8be7 170
Kojto 123:b0220dba8be7 171
Kojto 123:b0220dba8be7 172 /**@brief Data on a local GATT database.
Kojto 123:b0220dba8be7 173 */
Kojto 123:b0220dba8be7 174 typedef struct
Kojto 123:b0220dba8be7 175 {
Kojto 123:b0220dba8be7 176 uint32_t flags; /**< @brief Flags that describe the database attributes. */
Kojto 123:b0220dba8be7 177 uint16_t len; /**< @brief Size of the attribute array. */
Kojto 123:b0220dba8be7 178
Kojto 123:b0220dba8be7 179 #ifdef __ICCARM__ //IAR dosen't support "flexible array member" in c++ compilation
Kojto 123:b0220dba8be7 180 uint8_t data[1]; /**< @brief Array to hold the database attributes. */
Kojto 123:b0220dba8be7 181 #else
Kojto 123:b0220dba8be7 182 uint8_t data[]; /**< @brief Array to hold the database attributes. */
Kojto 123:b0220dba8be7 183 #endif
Kojto 123:b0220dba8be7 184 } pm_peer_data_local_gatt_db_t;
Kojto 123:b0220dba8be7 185
Kojto 123:b0220dba8be7 186
Kojto 123:b0220dba8be7 187 /**@brief Macro to check whether a data type is valid, thus one of the valid enum values.
Kojto 123:b0220dba8be7 188 *
Kojto 123:b0220dba8be7 189 * @param[in] data_id The data type to check.
Kojto 123:b0220dba8be7 190 */
Kojto 123:b0220dba8be7 191 #define PM_PEER_DATA_ID_IS_VALID(data_id) \
Kojto 123:b0220dba8be7 192 ( ((data_id) == PM_PEER_DATA_ID_BONDING) \
Kojto 123:b0220dba8be7 193 || ((data_id) == PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING)\
Kojto 123:b0220dba8be7 194 || ((data_id) == PM_PEER_DATA_ID_GATT_LOCAL) \
Kojto 123:b0220dba8be7 195 || ((data_id) == PM_PEER_DATA_ID_GATT_REMOTE) \
Kojto 123:b0220dba8be7 196 || ((data_id) == PM_PEER_DATA_ID_PEER_RANK) \
Kojto 123:b0220dba8be7 197 || ((data_id) == PM_PEER_DATA_ID_APPLICATION))
Kojto 123:b0220dba8be7 198
Kojto 123:b0220dba8be7 199
Kojto 123:b0220dba8be7 200 /** @} */
Kojto 123:b0220dba8be7 201
Kojto 123:b0220dba8be7 202 #endif /* PEER_MANAGER_TYPES_H__ */