Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:6ba9b94b8997, committed 2017-02-09
- Comitter:
- jinu
- Date:
- Thu Feb 09 06:08:17 2017 +0000
- Commit message:
- NRF51 serialization libraries for mDot
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/inc/app_error.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,100 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup app_error Common application error handler + * @{ + * @ingroup app_common + * + * @brief Common application error handler and macros for utilizing a common error handler. + */ + +#ifndef APP_ERROR_H__ +#define APP_ERROR_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "nrf_error.h" + +/**@brief Function for error handling, which is called when an error has occurred. + * + * @param[in] error_code Error code supplied to the handler. + * @param[in] line_num Line number where the handler is called. + * @param[in] p_file_name Pointer to the file name. + */ +void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name); + +/**@brief Macro for calling error handler function. + * + * @param[in] ERR_CODE Error code supplied to the error handler. + */ +#define APP_ERROR_HANDLER(ERR_CODE) \ + // do \ + // { \ + // app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__); \ + // } while (0) + +/**@brief Macro for calling error handler function if supplied error code any other than NRF_SUCCESS. + * + * @param[in] ERR_CODE Error code supplied to the error handler. + */ +#define APP_ERROR_CHECK(ERR_CODE) \ + do \ + { \ + const uint32_t LOCAL_ERR_CODE = (ERR_CODE); \ + if (LOCAL_ERR_CODE != NRF_SUCCESS) \ + { \ + APP_ERROR_HANDLER(LOCAL_ERR_CODE); \ + } \ + } while (0) + +/**@brief Macro for calling error handler function if supplied boolean value is false. + * + * @param[in] BOOLEAN_VALUE Boolean value to be evaluated. + */ +#define APP_ERROR_CHECK_BOOL(BOOLEAN_VALUE) \ + do \ + { \ + const bool LOCAL_BOOLEAN_VALUE = (BOOLEAN_VALUE); \ + if (!LOCAL_BOOLEAN_VALUE) \ + { \ + APP_ERROR_HANDLER(0); \ + } \ + } while (0) + +#endif // APP_ERROR_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/inc/app_uart_stream.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,170 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// @todo: @file scope comment block missing. + +#ifndef APP_UART_STREAM_H__ +#define APP_UART_STREAM_H__ + +#include <stdbool.h> +#include <stdint.h> + +/**@brief Enumeration of supported baud rates. */ +typedef enum +{ + UART_BAUD_RATE_1200, /**< Baud rate 1200. */ + UART_BAUD_RATE_2400, /**< Baud rate 2400. */ + UART_BAUD_RATE_4800, /**< Baud rate 4800. */ + UART_BAUD_RATE_9600, /**< Baud rate 9600. */ + UART_BAUD_RATE_14400, /**< Baud rate 14400. */ + UART_BAUD_RATE_19200, /**< Baud rate 19200. */ + UART_BAUD_RATE_28800, /**< Baud rate 28800. */ + UART_BAUD_RATE_38400, /**< Baud rate 38400. */ + UART_BAUD_RATE_57600, /**< Baud rate 57600. */ + UART_BAUD_RATE_76800, /**< Baud rate 76800. */ + UART_BAUD_RATE_115200, /**< Baud rate 115200. */ + UART_BAUD_RATE_230400, /**< Baud rate 230400. */ + UART_BAUD_RATE_250000, /**< Baud rate 250000. */ + UART_BAUD_RATE_460800, /**< Baud rate 460800. */ + UART_BAUD_RATE_921600, /**< Baud rate 921600. */ + UART_BAUD_RATE_1000000, /**< Baud rate 1000000. */ + UART_BAUD_RATE_MAX /**< Enumeration upper bound. */ +} app_uart_stream_baud_rate_t; + +/**@brief UART communication structure holding configuration settings for the peripheral. + */ +typedef struct +{ + uint8_t rx_pin_no; /**< RX pin number. */ + uint8_t tx_pin_no; /**< TX pin number. */ + uint8_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */ + uint8_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */ + bool use_parity; /**< Even parity if TRUE, no parity if FALSE. */ + app_uart_stream_baud_rate_t baud_rate; /**< Baud rate configuration. */ +} app_uart_stream_comm_params_t; + +/**@brief Event callback function events. */ +typedef enum +{ + APP_UART_STREAM_TX_DONE, /**< An event indicating that TX stream has been transmitted. */ + APP_UART_STREAM_RX_RDY, /**< An event indicating that RX stream has been received. */ + APP_UART_STREAM_RX_OVERFLOW, /**< An event indicating that RX data has been discarded due to lack of free RX memory. Either lack of RX buffer or RX buffer overflow would occur */ + APP_UART_STREAM_ERROR, /**< An error in the app_uart_stream module has occured. */ + APP_UART_STREAM_EVT_TYPE_MAX /**< Enumeration upper bound. */ +} app_uart_stream_evt_type_t; + +/**@brief Struct containing events from the UART module. + */ +typedef struct +{ + app_uart_stream_evt_type_t event; /**< Type of event. */ + + uint32_t param1; /**< Event specific parameter. @todo: this is begin of RX-buffer with APP_UART_STREAM_RX_RDY */ + uint32_t param2; /**< Event specific parameter. @todo: this is length of data in RX-buffer with APP_UART_STREAM_RX_RDY */ +} app_uart_stream_evt_t; + +typedef void (*app_uart_stream_event_handler_t)(app_uart_stream_evt_t event); + +/**@brief Function for opening and initializing the UART module. + * + * @warning Must not be called if has been allready opened. + * + * @param[in] p_comm_params Pin and communication parameters or NULL to use driver defined default values. + * @param[in] event_handler Function to be called in case of an event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_INVALID_STATE Operation failure. Module in invalid state. + * @retval NRF_ERROR_NO_MEM Operation failure. No memory available. + * @retval NRF_ERROR_INVALID_PARAM Operation failure. If the given parameters are not supported by the uart driver. + */ +uint32_t app_uart_stream_open(const app_uart_stream_comm_params_t * p_comm_params); + +/**@brief Function for registering a generic event handler. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called by the XXX layer upon an event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t app_uart_stream_evt_handler_reg(app_uart_stream_event_handler_t event_handler); + +/**@brief Function for closing the UART module. + * + * @note Can be called multiple times and also for not opened module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t app_uart_stream_close(void); + +/**@brief Write a bytestream to the UART. + * + * @param[in] p_buffer Pointer to the buffer to transmit. + * @param[in] length Buffer length in bytes. + * + * @retval NRF_SUCCESS Operation success. Stream was added to the transmission queue + * and event will be send upon transmission completion. + * @retval NRF_ERROR_INVALID_STATE Operation failure. Module in invalid state. + * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and stream was not + * added to the transmission queue. User should wait for a + * appropriate event prior issuing this operation again. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t app_uart_stream_write(const uint8_t * p_buffer, uint16_t length); + +/**@brief Set RX-stream buffer where the RX data is appended and enable the PHY flow and reception of data. + * + * @param[in] p_buffer Pointer to the RX buffer where to receive. + * @param[in] num_of_bytes Number of bytes to append to the RX buffer prior sending the + * @ref APP_UART_STREAM_RX_RDY event + * @param[in] header Boolean specifying if the data to be received is considered part of a + * header frame. If set to false, the uart stream driver should enter low + * power mode when complete packet has been received. + * + * @note When @ref num_of_bytes has been received an event @ref APP_UART_STREAM_RX_READY will be send, + * PHY flow will be turned OFF and no more data is appended to the RX buffer. + * + * @note The actual length of the RX buffer is atleast the size of @ref num_of_bytes + * + * @note Can be called multiple times, and also for not opened module, possible existing buffer is replaced with the new one. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_BUSY Operation failure. RX buffer append in progress. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t app_uart_stream_rx_buffer_set(uint8_t * p_buffer, uint16_t num_of_bytes, bool header); + +#endif // APP_UART_STREAM_H__ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/inc/ble_timer.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,110 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXXX + * @{ + * @ingroup YYYY + * + * @brief ZZZZZ. + */ + +#ifndef BLE_TIMER_H__ +#define BLE_TIMER_H__ + +#include <stdint.h> + +/**@brief Timer id type. */ +typedef uint32_t ble_timer_id_t; + +/**@brief BLE timeout handler type. */ +typedef void (*ble_timer_timeout_handler_t)(void * p_context); + +/**@brief Timer modes. */ +typedef enum +{ + BLE_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */ + BLE_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */ +} ble_timer_mode_t; + +/**@brief Function for creating a timer instance. + * + * @param[out] p_timer_id Id of the newly created timer. + * @param[in] mode Timer mode. + * @param[in] timeout_handler Function to be executed when the timer expires. + * + * @retval NRF_SUCCESS Timer was successfully created. + * @retval NRF_ERROR_INVALID_PARAM Invalid parameter. + * @retval NRF_ERROR_INVALID_STATE Timer module has not been initialized. + * @retval NRF_ERROR_NO_MEM Maximum number of timers has already been reached. + */ +uint32_t ble_timer_create(ble_timer_id_t * p_timer_id, + ble_timer_mode_t mode, + ble_timer_timeout_handler_t timeout_handler); + +/**@brief Function for starting a timer. + * + * @param[in] timer_id Id of timer to start. + * @param[in] timeout_ms Number of millseconds to timeout event + * @param[in] p_context General purpose pointer. Will be passed to the timeout handler when + * the timer expires. + * + * @retval NRF_SUCCESS Timer was successfully started. + * @retval NRF_ERROR_INVALID_PARAM Invalid parameter. + * @retval NRF_ERROR_INVALID_STATE Timer module has not been initialized, or timer has not + * been created. + * @retval NRF_ERROR_NO_MEM Timer operations queue was full. + * + * @note When calling this method on a timer which is already running, the second start operation + * will be ignored. + */ +uint32_t ble_timer_start(ble_timer_id_t timer_id, uint32_t timeout_ms, void * p_context); + +/**@brief Function for stopping the specified timer. + * + * @param[in] timer_id Id of timer to stop. + * + * @retval NRF_SUCCESS Timer was successfully stopped. + * @retval NRF_ERROR_INVALID_PARAM Invalid parameter. + * @retval NRF_ERROR_INVALID_STATE Timer module has not been initialized, or timer has not + * been created. + * @retval NRF_ERROR_NO_MEM Timer operations queue was full. + */ +uint32_t ble_timer_stop(ble_timer_id_t timer_id); + +#endif // BLE_TIMER_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/inc/blocking.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,64 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXXX + * @{ + * @ingroup YYYY + * + * @brief ZZZZZ. + */ + +#ifndef BLOCKING_H__ +#define BLOCKING_H__ + +#include <stdint.h> + +/**@brief Function for initializing the module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t blocking_init(void); + +/**@brief Function for doing a blocking wait for BLE command response. + * + * @return BLE command return code. + */ +uint32_t blocking_resp_wait(void); + +#endif // BLOCKING_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/inc/pstorage.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,405 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup persistent_storage Persistent Storage Interface + * @{ + * @ingroup app_common + * @brief Abstracted flash interface. + * + * @details In order to ensure that the SDK and application be moved to alternate persistent storage + * options other than the default provided with NRF solution, an abstracted interface is provided + * by the module to ensure SDK modules and application can be ported to alternate option with ease. + */ + +#ifndef PSTORAGE_H__ +#define PSTORAGE_H__ + +#include <stdint.h> +/**@defgroup ps_opcode Persistent Storage Access Operation Codes + * @{ + * @brief Persistent Storage Access Operation Codes. These are used to report any error during + * a persistent storage access operation or any general error that may occur in the + * interface. + * + * @details Persistent Storage Access Operation Codes used in error notification callback + * registered with the interface to report any error during an persistent storage access + * operation or any general error that may occur in the interface. + */ +#define PSTORAGE_ERROR_OP_CODE 0x01 /**< General Error Code */ +#define PSTORAGE_STORE_OP_CODE 0x02 /**< Error when Store Operation was requested */ +#define PSTORAGE_LOAD_OP_CODE 0x03 /**< Error when Load Operation was requested */ +#define PSTORAGE_CLEAR_OP_CODE 0x04 /**< Error when Clear Operation was requested */ +#define PSTORAGE_UPDATE_OP_CODE 0x05 /**< Update an already touched storage block */ + +/** Abstracts persistently memory block identifier. */ +typedef uint32_t pstorage_block_t; + +typedef struct +{ + uint32_t module_id; /**< Module ID.*/ + pstorage_block_t block_id; /**< Block ID.*/ +} pstorage_handle_t; + +typedef uint16_t pstorage_size_t; /** Size of length and offset fields. */ + +/**@} */ + +/**@defgroup pstorage_data_types Persistent Memory Interface Data Types + * @{ + * @brief Data Types needed for interfacing with persistent memory. + * + * @details Data Types needed for interfacing with persistent memory. + */ + +/**@brief Persistent Storage Error Reporting Callback + * + * @details Persistent Storage Error Reporting Callback that is used by the interface to report + * success or failure of a flash operation. Therefore, for store operation or clear + * operations, that take time, application can know when the procedure was complete. + * For store operation, since no data copy is made, receiving a success or failure + * notification, indicated by the reason parameter of callback is an indication that + * the resident memory could now be reused or freed, as the case may be. + * This callback is not received for load operation. + * + * @param[in] handle Identifies module and block for which callback is received. + * @param[in] op_code Identifies the operation for which the event is notified. + * @param[in] result Identifies the result of flash access operation. + * NRF_SUCCESS implies, operation succeeded. + * @param[in] p_data Identifies the application data pointer. In case of store operation, this + * points to the resident source of application memory that application can now + * free or reuse. In case of clear, this is NULL as no application pointer is + * needed for this operation. + * @param[in] data_len Length data application had provided for the operation. + * + */ +typedef void (*pstorage_ntf_cb_t)(pstorage_handle_t * p_handle, + uint8_t op_code, + uint32_t result, + uint8_t * p_data, + uint32_t data_len); + + +typedef struct +{ + pstorage_ntf_cb_t cb; /**< Callback registered with the module to be notified of any error occurring in persistent memory management */ + pstorage_size_t block_size; /**< Desired block size for persistent memory storage, for example, if a module has a table with 10 entries, each entry is size 64 bytes, + * it can request 10 blocks with block size 64 bytes. On the other hand, the module can also request one block of size 640 based on + * how it would like to access or alter memory in persistent memory. + * First option is preferred when single entries that need to be updated often when having no impact on the other entries. + * While second option is preferred when entries of table are not changed on individually but have common point of loading and storing + * data. */ + pstorage_size_t block_count; /** Number of blocks requested by the module, minimum values is 1. */ +} pstorage_module_param_t; + +/**@} */ + +/**@defgroup pstorage_routines Persistent Storage Access Routines + * @{ + * @brief Functions/Interface SDK modules use to persistently store data. + * + * @details Interface for Application & SDK module to load/store information persistently. + * Note: that while implementation of each of the persistent storage access function + * depends on the system and can specific to system/solution, the signature of the + * interface routines should not be altered. + */ + +/**@brief Module Initialization Routine. + * + * @details Initializes module. To be called once before any other APIs of the module are used. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + */ +uint32_t pstorage_init(void); + + +/**@brief Register with persistent storage interface. + * + * @param[in] p_module_param Module registration param. + * @param[out] p_block_id Block identifier to identify persistent memory blocks in case + * registration succeeds. Application is expected to use the block ids + * for subsequent operations on requested persistent memory. Maximum + * registrations permitted is determined by configuration parameter + * PSTORAGE_MAX_APPLICATIONS. + * In case more than one memory blocks are requested, the identifier provided here is + * the base identifier for the first block and to identify subsequent block, + * application shall use \@ref pstorage_block_identifier_get with this base identifier + * and block number. Therefore if 10 blocks of size 64 are requested and application + * wishes to store memory in 6th block, it shall use + * \@ref pstorage_block_identifier_get with based id and provide a block number of 5. + * This way application is only expected to remember the base block identifier. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_NO_MEM in case no more registrations can be supported. + */ +uint32_t pstorage_register(pstorage_module_param_t * p_module_param, + pstorage_handle_t * p_block_id); + + +/** + * @brief Function to get block id with reference to base block identifier provided at time of + * registration. + * + * @details Function to get block id with reference to base block identifier provided at time of + * registration. + * In case more than one memory blocks were requested when registering, the identifier + * provided here is the base identifier for the first block and to identify subsequent + * block, application shall use this routine to get block identifier providing input as + * base identifier and block number. Therefore if 10 blocks of size 64 are requested and + * application wishes to store memory in 6th block, it shall use + * \@ref pstorage_block_identifier_get with based id and provide a block number of 5. + * This way application is only expected to remember the base block identifier. + * + * @param[in] p_base_id Base block id received at the time of registration. + * @param[in] block_num Block Number, with first block numbered zero. + * @param[out] p_block_id Block identifier for the block number requested in case the API succeeds. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + */ +uint32_t pstorage_block_identifier_get(pstorage_handle_t * p_base_id, + pstorage_size_t block_num, + pstorage_handle_t * p_block_id); + + +/**@brief Routine to persistently store data of length 'size' contained in 'p_src' address + * in storage module at 'p_dest' address; Equivalent to Storage Write. + * + * @param[in] p_dest Destination address where data is to be stored persistently. + * @param[in] p_src Source address containing data to be stored. API assumes this to be resident + * memory and no intermediate copy of data is made by the API. + * @param[in] size Size of data to be stored expressed in bytes. Should be word aligned. + * @param[in] offset Offset in bytes to be applied when writing to the block. + * For example, if within a block of 100 bytes, application wishes to + * write 20 bytes at offset of 12, then this field should be set to 12. + * Should be word aligned. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_INVALID_ADDR in case data address 'p_src' is not aligned. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + * + * @warning No copy of the data is made, and hence memory provided for data source to be written + * to flash cannot be freed or reused by the application until this procedure + * is complete. End of this procedure is notified to the application using the + * notification callback registered by the application. + */ +uint32_t pstorage_store(pstorage_handle_t * p_dest, + uint8_t * p_src, + pstorage_size_t size, + pstorage_size_t offset); + +/**@brief Routine to update persistently stored data of length 'size' contained in 'p_src' address + * in storage module at 'p_dest' address. + * + * @param[in] p_dest Destination address where data is to be updated. + * @param[in] p_src Source address containing data to be stored. API assumes this to be resident + * memory and no intermediate copy of data is made by the API. + * @param[in] size Size of data to be stored expressed in bytes. Should be word aligned. + * @param[in] offset Offset in bytes to be applied when writing to the block. + * For example, if within a block of 100 bytes, application wishes to + * write 20 bytes at offset of 12, then this field should be set to 12. + * Should be word aligned. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_INVALID_ADDR in case data address 'p_src' is not aligned. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + * + * @warning No copy of the data is made, and hence memory provided for data source to be written + * to flash cannot be freed or reused by the application until this procedure + * is complete. End of this procedure is notified to the application using the + * notification callback registered by the application. + */ +uint32_t pstorage_update(pstorage_handle_t * p_dest, + uint8_t * p_src, + pstorage_size_t size, + pstorage_size_t offset); + +/**@brief Routine to load persistently stored data of length 'size' from 'p_src' address + * to 'p_dest' address; Equivalent to Storage Read. + * + * @param[in] p_dest Destination address where persistently stored data is to be loaded. + * @param[in] p_src Source from where data is to be loaded from persistent memory. + * @param[in] size Size of data to be loaded from persistent memory expressed in bytes. + * Should be word aligned. + * @param[in] offset Offset in bytes to be applied when loading from the block. + * For example, if within a block of 100 bytes, application wishes to + * load 20 bytes from offset of 12, then this field should be set to 12. + * Should be word aligned. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_INVALID_ADDR in case data address 'p_dst' is not aligned. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + */ +uint32_t pstorage_load(uint8_t * p_dest, + pstorage_handle_t * p_src, + pstorage_size_t size, + pstorage_size_t offset); + +/**@brief Routine to clear data in persistent memory. + * + * @param[in] p_base_id Base block identifier in persistent memory that needs to cleared; + * Equivalent to an Erase Operation. + * + * @param[in] size Size of data to be cleared from persistent memory expressed in bytes. + * This parameter is to provision for clearing of certain blocks + * of memory, or all memory blocks in a registered module. If the total size + * of the application module is used (blocks * block size) in combination with + * the identifier for the first block in the module, all blocks in the + * module will be erased. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_INVALID_ADDR in case data address 'p_dst' is not aligned. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + * + * @note Clear operations may take time. This API however, does not block until the clear + * procedure is complete. Application is notified of procedure completion using + * notification callback registered by the application. 'result' parameter of the + * callback suggests if the procedure was successful or not. + */ +uint32_t pstorage_clear(pstorage_handle_t * p_base_id, pstorage_size_t size); + +/** + * @brief API to get status of number of pending operations with the module. + * + * @param[out] p_count Number of storage operations pending with the module, if 0, + * there are no outstanding requests. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + */ +uint32_t pstorage_access_status_get(uint32_t * p_count); + +#ifdef PSTORAGE_RAW_MODE_ENABLE + +/**@brief Function for registering with persistent storage interface. + * + * @param[in] p_module_param Module registration param. + * @param[out] p_block_id Block identifier to identify persistent memory blocks in case + * registration succeeds. Application is expected to use the block ids + * for subsequent operations on requested persistent memory. + * In case more than one memory blocks are requested, the identifier provided here is + * the base identifier for the first block and to identify subsequent block, + * application shall use \@ref pstorage_block_identifier_get with this base identifier + * and block number. Therefore if 10 blocks of size 64 are requested and application + * wishes to store memory in 6th block, it shall use + * \@ref pstorage_block_identifier_get with based id and provide a block number of 5. + * This way application is only expected to remember the base block identifier. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_NO_MEM in case no more registrations can be supported. + */ +uint32_t pstorage_raw_register(pstorage_module_param_t * p_module_param, + pstorage_handle_t * p_block_id); + +/**@brief Raw mode function for persistently storing data of length 'size' contained in 'p_src' + * address in storage module at 'p_dest' address; Equivalent to Storage Write. + * + * @param[in] p_dest Destination address where data is to be stored persistently. + * @param[in] p_src Source address containing data to be stored. API assumes this to be resident + * memory and no intermediate copy of data is made by the API. + * @param[in] size Size of data to be stored expressed in bytes. Should be word aligned. + * @param[in] offset Offset in bytes to be applied when writing to the block. + * For example, if within a block of 100 bytes, application wishes to + * write 20 bytes at offset of 12, then this field should be set to 12. + * Should be word aligned. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_INVALID_ADDR in case data address 'p_src' is not aligned. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + * + * @warning No copy of the data is made, and hence memory provided for data source to be written + * to flash cannot be freed or reused by the application until this procedure + * is complete. End of this procedure is notified to the application using the + * notification callback registered by the application. + */ +uint32_t pstorage_raw_store(pstorage_handle_t * p_dest, + uint8_t * p_src, + pstorage_size_t size, + pstorage_size_t offset); + +/**@brief Function for clearing data in persistent memory in raw mode. + * + * @param[in] p_dest Base block identifier in persistent memory that needs to cleared; + * Equivalent to an Erase Operation. + * @param[in] size Size of data to be cleared from persistent memory expressed in bytes. + * This is currently unused. And a clear would mean clearing all blocks, + * however, this parameter is to provision for clearing of certain blocks + * of memory only and not all if need be. + * + * @retval NRF_SUCCESS on success, else an error code indicating reason for failure. + * @retval NRF_ERROR_INVALID_STATE is returned is API is called without module initialization. + * @retval NRF_ERROR_NULL if NULL parameter has been passed. + * @retval NRF_ERROR_INVALID_PARAM if invalid parameters are passed to the API. + * @retval NRF_ERROR_NO_MEM in case request cannot be processed. + * + * @note Clear operations may take time. This API however, does not block until the clear + * procedure is complete. Application is notified of procedure completion using + * notification callback registered by the application. 'result' parameter of the + * callback suggests if the procedure was successful or not. + */ +uint32_t pstorage_raw_clear(pstorage_handle_t * p_dest, pstorage_size_t size); + +#endif // PSTORAGE_RAW_MODE_ENABLE + +/**@} */ +/**@} */ + +#endif // PSTORAGE_H__ + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/src/ble_nvm.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,117 @@ +/** + ****************************************************************************** + * @file ble_nvm.c + * @author MCD Application Team + * @version V1.0 + * @date 14-April-2014 + * @brief Wrapper between the BLE NVM interface and the NVM driver + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> + * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + + +/** + * @note This file contains the mapping of the BLE module NVM interface to the NVM driver + */ + +/* Includes ------------------------------------------------------------------*/ +#include "pstorage.h" +#include "app_error.h" +//#include "hal_nvm.h" + +/* Private defines -----------------------------------------------------------*/ + +/** + * Base address to store the BLE parameters + * Bank2 is selected as the code is running in bank1 + * This allows RWW (Read While Write) feature to improve performance of the system + */ +#define BLE_NVM_BASE_ADDRESS 0x08082000 + +/* Private variables ---------------------------------------------------------*/ +static uint16_t aBlockSizeList[3]; /**< Only 3 BLE modules Id supported */ +static uint8_t ModuleId; +static uint8_t *pCurrentPointerToNVMLocation; + +/* Public functions ----------------------------------------------------------*/ + +uint32_t pstorage_init(void) +{ + ModuleId = 0; + pCurrentPointerToNVMLocation = (uint8_t *)BLE_NVM_BASE_ADDRESS; + //HAL_NVM_Init(); + + return NRF_SUCCESS; +} + +uint32_t pstorage_register(pstorage_module_param_t * p_module_param, + pstorage_handle_t * p_block_id) +{ + p_block_id->block_id = (uint32_t)pCurrentPointerToNVMLocation; + (p_block_id->module_id) = ModuleId; + /* + * store the size of the allocated block to be used in the API pstorage_block_identifier_get() + */ + aBlockSizeList[ModuleId] = p_module_param->block_size; + + pCurrentPointerToNVMLocation = pCurrentPointerToNVMLocation + (p_module_param->block_size)*(p_module_param->block_count); + ModuleId++; /**< increment the module Id for the next registration */ + + return NRF_SUCCESS; +} + +uint32_t pstorage_block_identifier_get(pstorage_handle_t * p_base_id, + pstorage_size_t block_num, + pstorage_handle_t * p_block_id) +{ + + p_block_id->block_id = (p_base_id->block_id) + (block_num * aBlockSizeList[p_base_id->module_id]); + + return NRF_SUCCESS; +} + +uint32_t pstorage_store(pstorage_handle_t * p_dest, + uint8_t * p_src, + pstorage_size_t size, + pstorage_size_t offset) +{ +// HAL_NVM_Operation(eNVM_Write, p_src, (uint8_t *)(p_dest->block_id) + offset, size); + + return NRF_SUCCESS; +} + +uint32_t pstorage_load(uint8_t * p_dest, + pstorage_handle_t * p_src, + pstorage_size_t size, + pstorage_size_t offset) +{ +// HAL_NVM_Read(p_dest, (uint8_t *)(p_src->block_id) + offset, size); + + return NRF_SUCCESS; +} + +uint32_t pstorage_clear(pstorage_handle_t * p_dest, pstorage_size_t size) +{ +// HAL_NVM_Operation(eNVM_Clear, 0, (uint8_t *)(p_dest->block_id), aBlockSizeList[p_dest->module_id] * size); + + return NRF_SUCCESS; +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/src/ble_timer.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,61 @@ +/** + ****************************************************************************** + * @file ble_timer.c + * @author MCD Application Team + * @version V1.0 + * @date 14-April-2014 + * @brief Wrapper between the BLE timer interface and the Timer server + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> + * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + + +/** + * @note This file contains the mapping of the BLE module TIMER interface to the TIMER server + */ + +/* Includes ------------------------------------------------------------------*/ +#include "ble_timer.h" +//#include "hal_timer.h" +#include "nrf_error.h" + +/* Public functions ----------------------------------------------------------*/ + +uint32_t ble_timer_create(ble_timer_id_t * p_timer_id, + ble_timer_mode_t mode, + ble_timer_timeout_handler_t timeout_handler) +{ + // HAL_TIMER_Create( eTimerModuleID_BLE, (uint8_t *)p_timer_id, (eHAL_TIMER_TimerMode_t)mode, (pf_HAL_TIMER_TimerCallBack_t)timeout_handler); + return NRF_SUCCESS; +} + +uint32_t ble_timer_start(ble_timer_id_t timer_id, uint32_t timeout_ms, void * p_context) +{ +// HAL_TIMER_Start(timer_id, timeout_ms); + return NRF_SUCCESS; +} + +uint32_t ble_timer_stop(ble_timer_id_t timer_id) +{ + // HAL_TIMER_Delete(timer_id); + return NRF_SUCCESS; +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/src/ble_uart.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,82 @@ +/** + ****************************************************************************** + * @file ble_uart.c + * @author MCD Application Team + * @version V1.0 + * @date 14-April-2014 + * @brief Wrapper between BLE UART interface and low power potocol UART driver + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> + * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + + +/** + * @note This file contains the mapping of the BLE module UART interface to the low power protocol UART driver + */ + +/* Includes ------------------------------------------------------------------*/ +//#include "stm32l1xx.h" +#include "nrf_error.h" +#include "app_uart_stream.h" +//#include "hal_uart_interfaces.h" +//#include "hal_timer.h" + + +/* External variables --------------------------------------------------------*/ +/* Private typedef -----------------------------------------------------------*/ +/* Private defines -----------------------------------------------------------*/ +/* Private macros ------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +/* Public functions ----------------------------------------------------------*/ + +uint32_t app_uart_stream_open(const app_uart_stream_comm_params_t * p_comm_params) + { + return NRF_SUCCESS; + } + +uint32_t app_uart_stream_evt_handler_reg(app_uart_stream_event_handler_t event_handler) +{ +// HAL_UART_uart_open((pf_HAL_UART_PhyDriverEvent_Handler_t)event_handler); + + return NRF_SUCCESS; +} + + uint32_t app_uart_stream_close(void) + { + return NRF_SUCCESS; + } + + uint32_t app_uart_stream_write(const uint8_t * p_buffer, uint16_t length) + { +// HAL_UART_send_data((uint8_t*)p_buffer, length); + + return NRF_SUCCESS; + } + + uint32_t app_uart_stream_rx_buffer_set(uint8_t * p_buffer, uint16_t num_of_bytes, bool header) + { +// HAL_UART_receive_data(p_buffer); + + return NRF_SUCCESS; + } + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/interface/src/blocking.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,121 @@ +/** + ****************************************************************************** + * @file blocking.c + * @author MCD Application Team + * @version V1.0 + * @date 14-April-2014 + * @brief Pausing BLE between Command and Response + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> + * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "blocking.h" + +#include <stdint.h> +#include <stdbool.h> + +#include "nrf_error.h" +#include "ble.h" +#include "ble_encode_access.h" +#include "app_error.h" +//#include "stm32l1xx_conf.h" +//#include "main.h" + +#if (APP_HRS == 1) +#include "ble_app_main.h" +#endif + +/* Private variables ---------------------------------------------------------*/ +static volatile bool m_cmd_rsp_event_rcvd = false; +static uint32_t m_cmd_result_code; + +/* Private function prototypes -----------------------------------------------*/ +static void internal_response_handler(uint32_t result_code); + +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief Handler to notifying a response packet has been received + * + * @note This API is called when a response packet has been received + * + * @param result_code: Error code while decoding the response packet + * + * @retval None + */ +static void internal_response_handler(uint32_t result_code) +{ + m_cmd_result_code = result_code; + m_cmd_rsp_event_rcvd = true; + + /** + * This is added to solve race condition when this event occurs between the check of the variable m_cmd_rsp_event_rcvd + * and the time we enter low power mode + * This will prevent entering low power mode and will force the re-evaluation of the variable m_cmd_rsp_event_rcvd + */ +// TaskExecutionRequest(eMAIN_Main_SD_Command_Resp); + + return; +} + +/* Public functions ----------------------------------------------------------*/ + +/** + * @brief Initialization of the blocking mechanism to prevent sending two SD command back to back + * + * @note + * + * @param None + * + * @retval None + */ +uint32_t blocking_init(void) +{ + uint32_t err_code; + err_code = ble_encode_cmd_resp_handler_reg(internal_response_handler); + // APP_ERROR_CHECK(err_code); + + return NRF_SUCCESS; +} + +/** + * @brief Interface to pause the BLE module until a response packet is received + * + * @note This API is called after each SD command sent to the nRF device. + * + * @param None + * + * @retval None + */ +uint32_t blocking_resp_wait(void) +{ + while (!m_cmd_rsp_event_rcvd) + { +// Background(SD_COMMAND_NOT_ALLOWED); + } + + m_cmd_rsp_event_rcvd = false; +// TaskExecuted(eMAIN_Main_SD_Command_Resp); + + return m_cmd_result_code; +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/app_util.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,250 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup app_util Utility Functions and Definitions + * @{ + * @ingroup app_common + * + * @brief Various types and definitions available to all applications. + */ + +#ifndef APP_UTIL_H__ +#define APP_UTIL_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "compiler_abstraction.h" + +enum +{ + UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */ + UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */ + UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */ +}; + +/**@brief Macro for doing static (i.e. compile time) assertion. + * + * @note If the assertion fails when compiling using Keil, the compiler will report error message + * "error: #94: the size of an array must be greater than zero" (while gcc will list the + * symbol static_assert_failed, making the error message more readable). + * If the supplied expression can not be evaluated at compile time, Keil will report + * "error: #28: expression must have a constant value". + * + * @note The macro is intentionally implemented not using do while(0), allowing it to be used + * outside function blocks (e.g. close to global type- and variable declarations). + * If used in a code block, it must be used before any executable code in this block. + * + * @param[in] EXPR Constant expression to be verified. + */ + +#define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1] + +/**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */ +typedef uint8_t uint16_le_t[2]; + +/**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */ +typedef uint8_t uint32_le_t[4]; + +/**@brief Byte array type. */ +typedef struct +{ + uint16_t size; /**< Number of array entries. */ + uint8_t * p_data; /**< Pointer to array entries. */ +} uint8_array_t; + +/**@brief Perform rounded integer division (as opposed to truncating the result). + * + * @param[in] A Numerator. + * @param[in] B Denominator. + * + * @return Rounded (integer) result of dividing A by B. + */ +#define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B)) + +/**@brief Check if the integer provided is a power of two. + * + * @param[in] A Number to be tested. + * + * @return true if value is power of two. + * @return false if value not power of two. + */ +#define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) ) + +/**@brief To convert ticks to millisecond + * @param[in] time Number of millseconds that needs to be converted. + * @param[in] resolution Units to be converted. + */ +#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) + + +/**@brief Perform integer division, making sure the result is rounded up. + * + * @details One typical use for this is to compute the number of objects with size B is needed to + * hold A number of bytes. + * + * @param[in] A Numerator. + * @param[in] B Denominator. + * + * @return Integer result of dividing A by B, rounded up. + */ +#define CEIL_DIV(A, B) \ + /*lint -save -e573 */ \ + ((((A) - 1) / (B)) + 1) \ + /*lint -restore */ + +/**@brief Function for encoding a uint16 value. + * + * @param[in] value Value to be encoded. + * @param[out] p_encoded_data Buffer where the encoded data is to be written. + * + * @return Number of bytes written. + */ +static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data) +{ + p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0); + p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8); + return sizeof(uint16_t); +} + +/**@brief Function for encoding a uint32 value. + * + * @param[in] value Value to be encoded. + * @param[out] p_encoded_data Buffer where the encoded data is to be written. + * + * @return Number of bytes written. + */ +static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data) +{ + p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0); + p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8); + p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16); + p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24); + return sizeof(uint32_t); +} + +/**@brief Function for decoding a uint16 value. + * + * @param[in] p_encoded_data Buffer where the encoded data is stored. + * + * @return Decoded value. + */ +static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data) +{ + return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) | + (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 )); +} + +/**@brief Function for decoding a uint32 value. + * + * @param[in] p_encoded_data Buffer where the encoded data is stored. + * + * @return Decoded value. + */ +static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data) +{ + return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) | + (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) | + (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) | + (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 )); +} + +/** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. + * + * @details The calculation is based on a linearized version of the battery's discharge + * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and + * is considered to be the lower boundary. + * + * The discharge curve for CR2032 is non-linear. In this model it is split into + * 4 linear sections: + * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV) + * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV) + * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV) + * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV) + * + * These numbers are by no means accurate. Temperature and + * load in the actual application is not accounted for! + * + * @param[in] mvolts The voltage in mV + * + * @return Battery level in percent. +*/ +static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts) +{ + uint8_t battery_level; + + if (mvolts >= 3000) + { + battery_level = 100; + } + else if (mvolts > 2900) + { + battery_level = 100 - ((3000 - mvolts) * 58) / 100; + } + else if (mvolts > 2740) + { + battery_level = 42 - ((2900 - mvolts) * 24) / 160; + } + else if (mvolts > 2440) + { + battery_level = 18 - ((2740 - mvolts) * 12) / 300; + } + else if (mvolts > 2100) + { + battery_level = 6 - ((2440 - mvolts) * 6) / 340; + } + else + { + battery_level = 0; + } + + return battery_level; +} + +/**@brief Function for checking if a pointer value is aligned to a 4 byte boundary. + * + * @param[in] p Pointer value to be checked. + * + * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise. + */ +static __INLINE bool is_word_aligned(void * p) +{ + return (((uint32_t)p & 0x00000003) == 0); +} + +#endif // APP_UTIL_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,412 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_COMMON BLE SoftDevice Common + @{ + @defgroup ble_api Events, type definitions and API calls + @{ + + @brief Module independent events, type definitions and API calls for the S110 SoftDevice. + + */ + +#ifndef BLE_H__ +#define BLE_H__ + +#include "ble_ranges.h" +#include "ble_types.h" +#include "ble_gap.h" +#include "ble_l2cap.h" +#include "ble_gatt.h" +#include "ble_gattc.h" +#include "ble_gatts.h" + +/** @addtogroup BLE_COMMON_ENUMERATIONS Enumerations + * @{ */ + +/** + * @brief Common API SVC numbers. + */ +enum BLE_COMMON_SVCS +{ + SD_BLE_ENABLE = BLE_SVC_BASE, /**< Enable and initialize the BLE stack */ + SD_BLE_EVT_GET, /**< Get an event from the pending events queue. */ + SD_BLE_TX_BUFFER_COUNT_GET, /**< Get the total number of available application transmission buffers from the stack. */ + SD_BLE_UUID_VS_ADD, /**< Add a Vendor Specific UUID. */ + SD_BLE_UUID_DECODE, /**< Decode UUID bytes. */ + SD_BLE_UUID_ENCODE, /**< Encode UUID bytes. */ + SD_BLE_VERSION_GET, /**< Get the local version information (company id, Link Layer Version, Link Layer Subversion). */ + SD_BLE_USER_MEM_REPLY, /**< User Memory Reply. */ + SD_BLE_OPT_SET, /**< Set a BLE option. */ + SD_BLE_OPT_GET, /**< Get a BLE option. */ +}; + +/** @} */ + +/** @addtogroup BLE_COMMON_DEFINES Defines + * @{ */ + +/** @brief Required pointer alignment for BLE Events. +*/ +#define BLE_EVTS_PTR_ALIGNMENT 4 + +/** @defgroup BLE_USER_MEM_TYPES User Memory Types + * @{ */ +#define BLE_USER_MEM_TYPE_INVALID 0x00 /**< Invalid User Memory Types. */ +#define BLE_USER_MEM_TYPE_GATTS_QUEUED_WRITES 0x01 /**< User Memory for GATTS queued writes. */ +/** @} */ + +/** @brief Maximum number of Vendor Specific UUIDs. +*/ +#define BLE_UUID_VS_MAX_COUNT 10 + +/** @} */ + +/** @addtogroup BLE_COMMON_STRUCTURES Structures + * @{ */ + +/** + * @brief BLE Module Independent Event IDs. + */ +enum BLE_COMMON_EVTS +{ + BLE_EVT_TX_COMPLETE = BLE_EVT_BASE, /**< Transmission Complete. */ + BLE_EVT_USER_MEM_REQUEST, /**< User Memory request. */ + BLE_EVT_USER_MEM_RELEASE /**< User Memory release. */ +}; + +/**@brief User Memory Block. */ +typedef struct +{ + uint8_t* p_mem; /**< Pointer to the start of the user memory block. */ + uint16_t len; /**< Length in bytes of the user memory block. */ +} ble_user_mem_block_t; + +/** + * @brief TX complete event. + */ +typedef struct +{ + uint8_t count; /**< Number of packets transmitted. */ +} ble_evt_tx_complete_t; + +/**@brief Event structure for BLE_EVT_USER_MEM_REQUEST. */ +typedef struct +{ + uint8_t type; /**< User memory type, see @ref BLE_USER_MEM_TYPES. */ +} ble_evt_user_mem_request_t; + +/**@brief Event structure for BLE_EVT_USER_MEM_RELEASE. */ +typedef struct +{ + uint8_t type; /**< User memory type, see @ref BLE_USER_MEM_TYPES. */ + ble_user_mem_block_t mem_block; /**< User memory block */ +} ble_evt_user_mem_release_t; + + +/**@brief Event structure for events not associated with a specific function module. */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle on which this event occured. */ + union + { + ble_evt_tx_complete_t tx_complete; /**< Transmission Complete. */ + ble_evt_user_mem_request_t user_mem_request; /**< User Memory Request Event Parameters. */ + ble_evt_user_mem_release_t user_mem_release; /**< User Memory Release Event Parameters. */ + } params; +} ble_common_evt_t; + +/**@brief BLE Event header. */ +typedef struct +{ + uint16_t evt_id; /**< Value from a BLE_<module>_EVT series. */ + uint16_t evt_len; /**< Length in octets excluding this header. */ +} ble_evt_hdr_t; + +/**@brief Common BLE Event type, wrapping the module specific event reports. */ +typedef struct +{ + ble_evt_hdr_t header; /**< Event header. */ + union + { + ble_common_evt_t common_evt; /**< Common Event, evt_id in BLE_EVT_* series. */ + ble_gap_evt_t gap_evt; /**< GAP originated event, evt_id in BLE_GAP_EVT_* series. */ + ble_l2cap_evt_t l2cap_evt; /**< L2CAP originated event, evt_id in BLE_L2CAP_EVT* series. */ + ble_gattc_evt_t gattc_evt; /**< GATT client originated event, evt_id in BLE_GATTC_EVT* series. */ + ble_gatts_evt_t gatts_evt; /**< GATT server originated event, evt_id in BLE_GATTS_EVT* series. */ + } evt; +} ble_evt_t; + + +/** + * @brief Version Information. + */ +typedef struct +{ + uint8_t version_number; /**< Link Layer Version number for BT 4.1 spec is 7 (https://www.bluetooth.org/en-us/specification/assigned-numbers/link-layer). */ + uint16_t company_id; /**< Company ID, Nordic Semiconductor's company ID is 89 (0x0059) (https://www.bluetooth.org/apps/content/Default.aspx?doc_id=49708). */ + uint16_t subversion_number; /**< Link Layer Sub Version number, corresponds to the SoftDevice Config ID or Firmware ID (FWID). */ +} ble_version_t; + +/**@brief Common BLE Option type, wrapping the module specific options. */ +typedef union +{ + ble_gap_opt_t gap; /**< GAP option, opt_id in BLE_GAP_OPT_* series. */ +} ble_opt_t; + +/** + * @brief BLE GATTS init options + */ +typedef struct +{ + ble_gatts_enable_params_t gatts_enable_params; /**< GATTS init options @ref ble_gatts_enable_params_t. */ +} ble_enable_params_t; + +/** @} */ + +/** @addtogroup BLE_COMMON_FUNCTIONS Functions + * @{ */ + +/**@brief Enable the bluetooth stack + * + * @param[in] p_ble_enable_params Pointer to ble_enable_params_t + * + * @details This call initializes the bluetooth stack, no other BLE related call can be called before this one has been executed. + * + * @return @ref NRF_SUCCESS BLE stack has been initialized successfully + * @return @ref NRF_ERROR_INVALID_ADDR Invalid or not sufficiently aligned pointer supplied. + */ +SVCALL(SD_BLE_ENABLE, uint32_t, sd_ble_enable(ble_enable_params_t * p_ble_enable_params)); + +/**@brief Get an event from the pending events queue. + * + * @param[in] p_dest Pointer to buffer to be filled in with an event, or NULL to retrieve the event length. This buffer <b>must be 4-byte aligned in memory</b>. + * @param[in, out] p_len Pointer the length of the buffer, on return it is filled with the event length. + * + * @details This call allows the application to pull a BLE event from the BLE stack. The application is signalled that an event is + * available from the BLE Stack by the triggering of the SD_EVT_IRQn interrupt (mapped to IRQ 22). + * The application is free to choose whether to call this function from thread mode (main context) or directly from the Interrupt Service Routine + * that maps to SD_EVT_IRQn. In any case however, and because the BLE stack runs at a higher priority than the application, this function should be called + * in a loop (until @ref NRF_ERROR_NOT_FOUND is returned) every time SD_EVT_IRQn is raised to ensure that all available events are pulled from the stack. + * Failure to do so could potentially leave events in the internal queue without the application being aware of this fact. + * Sizing the p_dest buffer is equally important, since the application needs to provide all the memory necessary for the event to be copied into + * application memory. If the buffer provided is not large enough to fit the entire contents of the event, @ref NRF_ERROR_DATA_SIZE will be returned + * and the application can then call again with a larger buffer size. + * Please note that because of the variable length nature of some events, sizeof(ble_evt_t) will not always be large enough to fit certain events, + * and so it is the application's responsability to provide an amount of memory large enough so that the relevant event is copied in full. + * The application may "peek" the event length by providing p_dest as a NULL pointer and inspecting the value of *p_len upon return. + * + * @note The pointer supplied must be aligned to the extend defined by @ref BLE_EVTS_PTR_ALIGNMENT + * + * @return @ref NRF_SUCCESS Event pulled and stored into the supplied buffer. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid or not sufficiently aligned pointer supplied. + * @return @ref NRF_ERROR_NOT_FOUND No events ready to be pulled. + * @return @ref NRF_ERROR_DATA_SIZE Event ready but could not fit into the supplied buffer. + */ +SVCALL(SD_BLE_EVT_GET, uint32_t, sd_ble_evt_get(uint8_t* p_dest, uint16_t *p_len)); + + +/**@brief Get the total number of available application transmission buffers in the BLE stack. + * + * @details This call allows the application to obtain the total number of + * transmission buffers available for application data. Please note that + * this does not give the number of free buffers, but rather the total amount of them. + * The application has two options to handle its own application transmission buffers: + * - Use a simple arithmetic calculation: at boot time the application should use this function + * to find out the total amount of buffers available to it and store it in a variable. + * Every time a packet that consumes an application buffer is sent using any of the + * exposed functions in this BLE API, the application should decrement that variable. + * Conversely, whenever a @ref BLE_EVT_TX_COMPLETE event is received by the application + * it should retrieve the count field in such event and add that number to the same + * variable storing the number of available packets. + * This mechanism allows the application to be aware at any time of the number of + * application packets available in the BLE stack's internal buffers, and therefore + * it can know with certainty whether it is possible to send more data or it has to + * wait for a @ref BLE_EVT_TX_COMPLETE event before it proceeds. + * - Choose to simply not keep track of available buffers at all, and instead handle the + * @ref BLE_ERROR_NO_TX_BUFFERS error by queueing the packet to be transmitted and + * try again as soon as a @ref BLE_EVT_TX_COMPLETE event arrives. + * + * The API functions that <b>may</b> consume an application buffer depending on + * the parameters supplied to them can be found below: + * + * - @ref sd_ble_gattc_write (write witout response only) + * - @ref sd_ble_gatts_hvx (notifications only) + * - @ref sd_ble_l2cap_tx (all packets) + * + * @param[out] p_count Pointer to a uint8_t which will contain the number of application transmission buffers upon + * successful return. + * + * @return @ref NRF_SUCCESS Number of application transmission buffers retrieved successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + */ +SVCALL(SD_BLE_TX_BUFFER_COUNT_GET, uint32_t, sd_ble_tx_buffer_count_get(uint8_t* p_count)); + + +/**@brief Add a Vendor Specific UUID. + * + * @details This call enables the application to add a vendor specific UUID to the BLE stack's table, + * for later use all other modules and APIs. This then allows the application to use the shorter, + * 24-bit @ref ble_uuid_t format when dealing with both 16-bit and 128-bit UUIDs without having to + * check for lengths and having split code paths. The way that this is accomplished is by extending the + * grouping mechanism that the Bluetooth SIG standard base UUID uses for all other 128-bit UUIDs. The + * type field in the @ref ble_uuid_t structure is an index (relative to @ref BLE_UUID_TYPE_VENDOR_BEGIN) + * to the table populated by multiple calls to this function, and the uuid field in the same structure + * contains the 2 bytes at indices 12 and 13. The number of possible 128-bit UUIDs available to the + * application is therefore the number of Vendor Specific UUIDs added with the help of this function times 65536, + * although restricted to modifying bytes 12 and 13 for each of the entries in the supplied array. + * + * @note Bytes 12 and 13 of the provided UUID will not be used internally, since those are always replaced by + * the 16-bit uuid field in @ref ble_uuid_t. + * + * + * @param[in] p_vs_uuid Pointer to a 16-octet (128-bit) little endian Vendor Specific UUID disregarding + * bytes 12 and 13. + * @param[out] p_uuid_type Pointer where the type field in @ref ble_uuid_t corresponding to this UUID will be stored. + * + * @return @ref NRF_SUCCESS Successfully added the Vendor Specific UUID. + * @return @ref NRF_ERROR_INVALID_ADDR If p_vs_uuid or p_uuid_type is NULL or invalid. + * @return @ref NRF_ERROR_NO_MEM If there are no more free slots for VS UUIDs. + * @return @ref NRF_ERROR_FORBIDDEN If p_vs_uuid has already been added to the VS UUID table. + */ +SVCALL(SD_BLE_UUID_VS_ADD, uint32_t, sd_ble_uuid_vs_add(ble_uuid128_t const * const p_vs_uuid, uint8_t * const p_uuid_type)); + + +/** @brief Decode little endian raw UUID bytes (16-bit or 128-bit) into a 24 bit @ref ble_uuid_t structure. + * + * @details The raw UUID bytes excluding bytes 12 and 13 (i.e. bytes 0-11 and 14-15) of p_uuid_le are compared + * to the corresponding ones in each entry of the table of vendor specific UUIDs pouplated with @ref sd_ble_uuid_vs_add + * to look for a match. If there is such a match, bytes 12 and 13 are returned as p_uuid->uuid and the index + * relative to @ref BLE_UUID_TYPE_VENDOR_BEGIN as p_uuid->type. + * + * @note If the UUID length supplied is 2, then the type set by this call will always be @ref BLE_UUID_TYPE_BLE. + * + * @param[in] uuid_le_len Length in bytes of the buffer pointed to by p_uuid_le (must be 2 or 16 bytes). + * @param[in] p_uuid_le Pointer pointing to little endian raw UUID bytes. + * @param[in,out] p_uuid Pointer to a @ref ble_uuid_t structure to be filled in. + * + * @return @ref NRF_SUCCESS Successfully decoded into the @ref ble_uuid_t structure. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_LENGTH Invalid UUID length. + * @return @ref NRF_ERROR_NOT_FOUND For a 128-bit UUID, no match in the populated table of UUIDs. + */ +SVCALL(SD_BLE_UUID_DECODE, uint32_t, sd_ble_uuid_decode(uint8_t uuid_le_len, uint8_t const * const p_uuid_le, ble_uuid_t * const p_uuid)); + + +/** @brief Encode a @ref ble_uuid_t structure into little endian raw UUID bytes (16-bit or 128-bit). + * + * @note The pointer to the destination buffer p_uuid_le may be NULL, in which case only the validitiy and size of p_uuid is computed. + * + * @param[in] p_uuid Pointer to a @ref ble_uuid_t structure that will be encoded into bytes. + * @param[out] p_uuid_le_len Pointer to a uint8_t that will be filled with the encoded length (2 or 16 bytes). + * @param[out] p_uuid_le Pointer to a buffer where the little endian raw UUID bytes (2 or 16) will be stored. + * + * @return @ref NRF_SUCCESS Successfully encoded into the buffer. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid UUID type. + */ +SVCALL(SD_BLE_UUID_ENCODE, uint32_t, sd_ble_uuid_encode(ble_uuid_t const * const p_uuid, uint8_t * const p_uuid_le_len, uint8_t * const p_uuid_le)); + + +/**@brief Get Version Information. + * + * @details This call allows the application to get the BLE stack version information. + * + * @param[in] p_version Pointer to ble_version_t structure to be filled in. + * + * @return @ref NRF_SUCCESS Version information stored successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY The stack is busy (typically doing a locally-initiated disconnection procedure). + */ +SVCALL(SD_BLE_VERSION_GET, uint32_t, sd_ble_version_get(ble_version_t * p_version)); + + +/**@brief Provide a user memory block. + * + * @note This call can only be used as a response to a @ref BLE_EVT_USER_MEM_REQUEST event issued to the application. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_block Pointer to a user memory block structure. + * + * @return @ref NRF_SUCCESS Successfully queued a response to the peer. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_STATE No execute write request pending. + */ +SVCALL(SD_BLE_USER_MEM_REPLY, uint32_t, sd_ble_user_mem_reply(uint16_t conn_handle, ble_user_mem_block_t *p_block)); + + +/**@brief Set a BLE option. + * + * @details This call allows the application to set the value of an option. + * + * @param[in] opt_id Option ID. + * @param[in] p_opt Pointer to a ble_opt_t structure containing the option value. + * + * @retval ::NRF_SUCCESS Option set successfully. + * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints. + * @retval ::NRF_ERROR_INVALID_STATE Unable to set the parameter at this time. + * @retval ::NRF_ERROR_BUSY The stack is busy or the previous procedure has not completed. + */ +SVCALL(SD_BLE_OPT_SET, uint32_t, sd_ble_opt_set(uint32_t opt_id, ble_opt_t const *p_opt)); + + +/**@brief Get a BLE option. + * + * @details This call allows the application to retrieve the value of an option. + * + * @param[in] opt_id Option ID. + * @param[out] p_opt Pointer to a ble_opt_t structure to be filled in. + * + * @retval ::NRF_SUCCESS Option retrieved successfully. + * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints. + * @retval ::NRF_ERROR_INVALID_STATE Unable to retrieve the parameter at this time. + * @retval ::NRF_ERROR_BUSY The stack is busy or the previous procedure has not completed. + * @retval ::NRF_ERROR_NOT_SUPPORTED This option is not supported. + * + */ +SVCALL(SD_BLE_OPT_GET, uint32_t, sd_ble_opt_get(uint32_t opt_id, ble_opt_t *p_opt)); + +/** @} */ + +#endif /* BLE_H__ */ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_advdata.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,136 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_lib_advdata Advertising Data Encoder + * @{ + * @ingroup ble_sdk_lib + * @brief Function for encoding the advertising data and/or scan response data, and passing them to + * the stack. + */ + +#ifndef BLE_ADVDATA_H__ +#define BLE_ADVDATA_H__ + +#include <stdint.h> +#include <stdbool.h> +#include <string.h> +#include "ble.h" +#include "app_util.h" + +/**@brief Advertising data name type. This contains the options available for the device name inside + * the advertising data. */ +typedef enum +{ + BLE_ADVDATA_NO_NAME, /**< Include no device name in advertising data. */ + BLE_ADVDATA_SHORT_NAME, /**< Include short device name in advertising data. */ + BLE_ADVDATA_FULL_NAME /**< Include full device name in advertising data. */ +} ble_advdata_name_type_t; + +/**@brief UUID list type. */ +typedef struct +{ + uint16_t uuid_cnt; /**< Number of UUID entries. */ + ble_uuid_t * p_uuids; /**< Pointer to UUID array entries. */ +} ble_advdata_uuid_list_t; + +/**@brief Connection interval range structure. */ +typedef struct +{ + uint16_t min_conn_interval; /**< Minimum Connection Interval, in units of 1.25ms, range 6 to 3200 (i.e. 7.5ms to 4s). */ + uint16_t max_conn_interval; /**< Maximum Connection Interval, in units of 1.25ms, range 6 to 3200 (i.e. 7.5ms to 4s). Value of 0xFFFF indicates no specific maximum. */ +} ble_advdata_conn_int_t; + +/**@brief Manufacturer specific data structure. */ +typedef struct +{ + uint16_t company_identifier; /**< Company Identifier Code. */ + uint8_array_t data; /**< Additional manufacturer specific data. */ +} ble_advdata_manuf_data_t; + +/**@brief Service data structure. */ +typedef struct +{ + uint16_t service_uuid; /**< Service UUID. */ + uint8_array_t data; /**< Additional service data. */ +} ble_advdata_service_data_t; + +/**@brief Advertising data structure. This contains all options and data needed for encoding and + * setting the advertising data. */ +typedef struct +{ + ble_advdata_name_type_t name_type; /**< Type of device name. */ + uint8_t short_name_len; /**< Length of short device name (if short type is specified). */ + bool include_appearance; /**< Determines if Appearance shall be included. */ + uint8_array_t flags; /**< Advertising data Flags field. */ + int8_t * p_tx_power_level; /**< TX Power Level field. */ + ble_advdata_uuid_list_t uuids_more_available; /**< List of UUIDs in the 'More Available' list. */ + ble_advdata_uuid_list_t uuids_complete; /**< List of UUIDs in the 'Complete' list. */ + ble_advdata_uuid_list_t uuids_solicited; /**< List of solcited UUIDs. */ + ble_advdata_conn_int_t * p_slave_conn_int; /**< Slave Connection Interval Range. */ + ble_advdata_manuf_data_t * p_manuf_specific_data; /**< Manufacturer specific data. */ + ble_advdata_service_data_t * p_service_data_array; /**< Array of Service data structures. */ + uint8_t service_data_count; /**< Number of Service data structures. */ +} ble_advdata_t; + +/**@brief Function for encoding and setting the advertising data and/or scan response data. + * + * @details This function encodes advertising data and/or scan response data based on the selections + * in the supplied structures, and passes the encoded data to the stack. + * + * @param[in] p_advdata Structure for specifying the content of the advertising data. + * Set to NULL if advertising data is not to be set. + * @param[in] p_srdata Structure for specifying the content of the scan response data. + * Set to NULL if scan response data is not to be set. + * + * @return NRF_SUCCESS on success, NRF_ERROR_DATA_SIZE if not all the requested data could fit + * into the advertising packet. The maximum size of the advertisement packet is @ref + * BLE_GAP_ADV_MAX_SIZE. + * + * @warning This API may override application's request to use the long name and use a short name + * instead. This truncation will occur in case the long name does not fit advertisement data size. + * Application is permitted to specify a preferred short name length in case truncation is required. + * For example, if the complete device name is ABCD_HRMonitor, application can specify short name + * length to 8 such that short device name appears as ABCD_HRM instead of ABCD_HRMo or ABCD_HRMoni + * etc if available size for short name is 9 or 12 respectively to have more apporpriate short name. + * However, it should be noted that this is just a preference that application can specify and + * if the preference too large to fit in Advertisement Data, this can be further truncated. + */ +uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata); + +#endif // BLE_ADVDATA_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_bondmngr.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,361 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_lib_bond_manager Bonds and Persistent Data Manager + * @{ + * @ingroup ble_sdk_lib + * @brief This module handles multiple bonds and persistent attributes. + * + * @details When using <i>Bluetooth</i> low energy, a central device and a peripheral device can + * exchange identification information which they are capable of storing in memory (for + * example, in non-volatile memory). This information can be used for identity verification + * between the devices when they reconnect in the future. This relationship is known as a + * 'bond'. + * + * <b>Bonding Information:</b> + * + * The S110 SoftDevice handles the BLE on-air transactions necessary to establish a new + * bond or to use a previous bond when it reconnects to a bonded central. It is however up + * to the application layer to memorize the <i>identification information</i> between + * connections and to provide them back to the S110 stack when it detects a re-connection + * from a previously bonded central. This identification information is referred to as + * Bonding Information in code and the SDK. + * + * <b>System Attributes:</b> + * + * If the application is a GATT server, it stores a set of persistent attributes related + * to a connection when bonding with a central. On reconnection with the known bonded + * central, the application restores the related persistent attributes in the last known + * state back to the S110 SoftDevice. These persistent attributes mainly include the Client + * Characteristic Configuration Descriptor (or CCCD, see the <i><b>Bluetooth</b> + * Core Specification</i> for more information) states and could include other attributes + * in the future. Persistent attributes are referred to as System Attributes in code and + * in the SDK documentation. + * + * An application can use the Bonds and Persistent Data Manager module (referred to as the + * bond manager) included in the nRF51 SDK to handle most of the operations related to + * the Bonding Information and System Attributes. The bond manager notifies the + * application when it's connected to a new bonded central or to a previously bonded central. + * The application can use the Bond Manager API to store or load (or restore) the + * Bonding Information and System Attributes. The bond manager identifies all the centrals + * the application is bonded to and restores their respective Bonding Information and + * System Attributes to the S110 stack. + * + * In addition, you can use the bond manager to set up your application to advertise: + * + * - To a given bonded central using directed advertisement. + * - To a list of bonded centrals - i.e. using whitelist. + * + * The bond manager automatically writes the Bonding Information to the flash memory when + * the bonding procedure to a new central is finished. Upon disconnection, the application + * should use the Bond Manager API @ref ble_bondmngr_bonded_centrals_store to write the + * latest Bonding Information and System Attributes to flash. + * + * The bond manager provides the API @ref ble_bondmngr_sys_attr_store to allow the + * application to write the System Attributes to flash while in a connection. Your + * application should call this API when it considers that all CCCDs and other persistent + * attributes are in a stable state. This API call will safely fail if System Attributes + * data for the current connected central is already present in the flash. The API does so + * because a flash write in such a situation would require an erase of flash, which takes a + * long time (21 milliseconds) to complete. This may disrupt the radio. + * + * Applications using the Bond Manager must have a configuration file named + * ble_bondmngr_cfg.h (see below for details). + * + * Refer to @ref ble_bond_mgr_msc to see the flow of events when connecting to a central + * using the Bond Manager + * + * + * @section ble_sdk_lib_bond_manager_cfg Configuration File + * Applications using the Bond Manager must have a configuration file named ble_bondmngr_cfg.h. + * Here is an example of this file: + * + * @code + * #ifndef BLE_BONDMNGR_CFG_H__ + * #define BLE_BONDMNGR_CFG_H__ + * + * #define BLE_BONDMNGR_CCCD_COUNT 1 + * #define BLE_BONDMNGR_MAX_BONDED_CENTRALS 4 + * + * #endif // BLE_BONDMNGR_CFG_H__ + * @endcode + * + * BLE_BONDMNGR_CCCD_COUNT is the number of CCCDs used in the application, and + * BLE_BONDMNGR_MAX_BONDED_CENTRALS is the maximum number of bonded centrals to be supported by the + * application. + */ + +#ifndef BLE_BONDMNGR_H__ +#define BLE_BONDMNGR_H__ + +#include <stdint.h> +#include "ble.h" +#include "ble_srv_common.h" + + +/** @defgroup ble_bond_mgr_msc Message Sequence Charts + * @{ + * @brief Bond Manager interaction with S110 Stack + * @{ + * @defgroup UNBONDED_CENTRAL Connecting to an unbonded central + * @image html bond_manager_unbonded_master.jpg Connecting to an unbonded central + * @defgroup BONDED_CENTRAL Connecting to a bonded central + * @image html bond_manager_bonded_master.jpg Connecting to a bonded central + * @} + * @} + */ + +/** @defgroup DEFINES Defines + * @brief Macros defined by this module. + * @{ */ + +#define INVALID_CENTRAL_HANDLE (-1) /**< Invalid handle, used to indicate that the central is not a known bonded central. */ + +/** @} */ + +/** @defgroup ENUMS Enumerations + * @brief Enumerations defined by this module. + * @{ */ + +/**@brief Bond Manager Module event type. */ +typedef enum +{ + BLE_BONDMNGR_EVT_NEW_BOND, /**< New bond has been created. */ + BLE_BONDMNGR_EVT_CONN_TO_BONDED_CENTRAL, /**< Connected to a previously bonded central. */ + BLE_BONDMNGR_EVT_ENCRYPTED, /**< Current link is encrypted. */ + BLE_BONDMNGR_EVT_AUTH_STATUS_UPDATED, /**< Authentication status updated for current central. */ + BLE_BONDMNGR_EVT_BOND_FLASH_FULL /**< Flash block for storing Bonding Information is full. */ +} ble_bondmngr_evt_type_t; + +/** @} */ + + +/** @defgroup DATA_STRUCTURES Data Structures + * @brief Data Structures defined by this module. + * @{ */ + +/**@brief Bond Manager Module event. */ +typedef struct +{ + ble_bondmngr_evt_type_t evt_type; /**< Type of event. */ + int8_t central_handle; /**< Handle to the current central. This is an index to the central list maintained internally by the bond manager. */ + uint16_t central_id; /**< Identifier to the central. This will be the same as Encryption diversifier of the central (see @ref ble_gap_evt_sec_info_request_t). This value is constant for the duration of the bond. */ +} ble_bondmngr_evt_t; + +/** @} */ + +/** @defgroup TYPEDEFS Typedefs + * @brief Typedefs defined by this module. + * @{ */ + +/**@brief Bond Manager Module event handler type. */ +typedef void (*ble_bondmngr_evt_handler_t) (ble_bondmngr_evt_t * p_evt); + +/** @} */ + +/** @addtogroup DATA_STRUCTURES + * @{ */ + +/**@brief Bond Manager Module init structure. This contains all options and data needed for + * initialization of the Bond Manager module. */ +typedef struct +{ + uint8_t flash_page_num_bond; /**< Deprecated: Flash page number to use for storing Bonding Information. */ + uint8_t flash_page_num_sys_attr; /**< Deprecated: Flash page number to use for storing System Attributes. */ + bool bonds_delete; /**< TRUE if bonding and System Attributes for all centrals is to be deleted from flash during initialization, FALSE otherwise. */ + ble_bondmngr_evt_handler_t evt_handler; /**< Event handler to be called for handling events in bond manager. */ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ +} ble_bondmngr_init_t; + +/** @} */ + + +/** @defgroup FUNCTIONS Functions + * @brief Functions/APIs implemented and exposed by this module. + * @{ */ + +/**@brief Function for initializing the Bond Manager. + * + * @param[in] p_init This contains information needed to initialize this module. + * + * @return NRF_SUCCESS on successful initialization, otherwise an error code. + */ +uint32_t ble_bondmngr_init(ble_bondmngr_init_t * p_init); + +/**@brief Function for handling all events from the BLE stack that relate to this module. + * + * @param[in] p_ble_evt The event received from the BLE stack. + * + * @return NRF_SUCCESS if all operations went successfully, + * NRF_ERROR_NO_MEM if the maximum number of bonded centrals has been reached. + * Other error codes in other situations. + */ +void ble_bondmngr_on_ble_evt(ble_evt_t * p_ble_evt); + +/**@brief Function for storing the bonded centrals data including bonding info and System Attributes into + * flash memory. + * + * @details If the data to be written is different from the existing data, this function erases the + * flash pages before writing to flash. + * + * @warning This function could prevent the radio from running. Therefore it MUST be called ONLY + * when the application knows that the <i>Bluetooth</i> radio is not active. An example of + * such a state is when the application has received the Disconnected event and has not yet + * started advertising. <b>If it is called in any other state, or if it is not called at + * all, the behavior is undefined.</b> + * + * @return NRF_SUCCESS on success, an error_code otherwise. + */ +uint32_t ble_bondmngr_bonded_centrals_store(void); + +/**@brief Function for deleting the bonded central database from flash. + * + * @details After calling this function you should call ble_bondmngr_init() to re-initialize the + * RAM database. + * + * @return NRF_SUCCESS on success, an error_code otherwise. + */ +uint32_t ble_bondmngr_bonded_centrals_delete(void); + +/**@brief Function for getting the whitelist containing all currently bonded centrals. + * + * @details This function populates the whitelist with either the IRKs or the public adresses + * of all bonded centrals. + * + * @param[out] p_whitelist Whitelist structure with all bonded centrals. + * + * @return NRF_SUCCESS on success, an error_code otherwise. + */ +uint32_t ble_bondmngr_whitelist_get(ble_gap_whitelist_t * p_whitelist); + +/**@brief Function for getting the central's address corresponding to a given central_handle. + * + * @note This function returns NRF_ERROR_INVALID_PARAM if the given central has a private + * address. + * + * @param[in] central_handle Central's handle. + * @param[out] p_central_addr Pointer to the central's address which can be used for + * directed advertising. + */ +uint32_t ble_bondmngr_central_addr_get(int8_t central_handle, ble_gap_addr_t * p_central_addr); + +/**@brief Function for storing the System Attributes of a newly connected central. + * + * @details This function fetches the System Attributes of the current central from the stack, adds + * it to the database in memory, and also stores it in the flash (without erasing any + * flash page). + * This function is intended to facilitate the storage of System Attributes when connected + * to new central (whose System Attributes are NOT yet stored in flash) even in connected + * state without affecting radio link. This function can, for example, be called after the + * CCCD is written by a central. The function will succeed if the central is a new central. + * See @ref ble_sdk_app_hids_keyboard or @ref ble_sdk_app_hids_mouse for sample usage. + * + * @return NRF_SUCCESS on success, otherwise an error code. + * NRF_ERROR_INVALID_STATE is returned if the System Attributes of the current central is + * already present in the flash because it is a previously known central. + */ +uint32_t ble_bondmngr_sys_attr_store(void); + +/**@brief Function for fetching the identifiers of known centrals. + * + * @details This function fetches the identifiers of the centrals that are currently in the + * database, or in other words, known to the bond manager. + * + * @param[out] p_central_ids Pointer to the array of central identifiers. It is recommended + * that the length of this array be equal to + * MAX_NUMBER_OF_BONDED_CENTRALS * 2 bytes. If value of this pointer + * is NULL, only the number of centrals in the database will be + * filled in p_length. This can be used to find out the + * required size of the array pointed to by p_central_ids in + * a subsequent call. + * @param[in, out] p_length Pointer to the length of p_central_ids array provided as + * input. On return, this function will write the number of central + * identifiers found to p_length + * + * @return NRF_SUCCESS on success. + * NRF_ERROR_NULL if the input parameter p_length is NULL. + * NRF_ERROR_INVALID_STATE is returned if the bond manager was not initialized. + * NRF_ERROR_DATA_SIZE is returned if the length of the input parameter + * p_central_ids provided is not enough to fit in all the central identifiers in the + * database. + */ +uint32_t ble_bondmngr_central_ids_get(uint16_t * p_central_ids, uint16_t * p_length); + +/**@brief Function for deleting a single central from the database. + * @details This function deletes the Bonding Information and System Attributes of a single + * central from the flash. + * The application can use the @ref ble_bondmngr_central_ids_get function to fetch the + * identifiers of centrals present in the database and then call this function. + * + * @warning This function could prevent the radio from running. Therefore it MUST be called ONLY + * when the application knows that the <i>Bluetooth</i> radio is not active. An example + * of such a state could be when the application is not in a connected state AND is + * also not advertising. <b>If it is called in any other state, the behavior is + * undefined.</b> + * + * @param[in] central_id Identifier of the central to be deleted. + * + * @return NRF_SUCCESS on success. + * NRF_ERROR_INVALID_STATE is returned if the bond manager was not initialized. + * NRF_ERROR_NOT_FOUND if the central with the given identifier is not found in the + * database. + */ +uint32_t ble_bondmngr_bonded_central_delete(uint16_t central_id); + +/**@brief Function to verify encryption status link with bonded central is encrypted or not. + * @details This function provides status of encrption of the link with a bonded central. + * Its is recommended that the application can use the + * @ref ble_bondmngr_central_ids_get function to verify if the central is in the + * database and then call this function. + * + * @warning Currently the central id paramater is unused and is added only for future extension. + * As of today, only one link is permitted for the peripheral device, status of current + * link is provided. In future, with more possibilities in the topology, central_id + * will be needed to identify the central. + * + * @param[out] status Status of encryption, true implies link encrypted. + * + * @return NRF_SUCCESS on success. + * NRF_ERROR_INVALID_STATE is returned if the bond manager was not initialized. + */ +uint32_t ble_bondmngr_is_link_encrypted(bool * status); + +/** @} */ + +#endif // BLE_BONDMNGR_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_bondmngr_cfg.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,61 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + /** @cond To make doxygen skip this file */ + +/** @file + * + * @defgroup ble_sdk_app_proximity_bondmngr_cfg Proximity Bond Manager Configuration + * @{ + * @ingroup ble_sdk_app_proximity + * @brief Definition of bond manager configurable parameters + */ + +#ifndef BLE_BONDMNGR_CFG_H__ +#define BLE_BONDMNGR_CFG_H__ + +/**@brief Number of CCCDs used in the proximity application. */ +#define BLE_BONDMNGR_CCCD_COUNT 2 + +/**@brief Maximum number of bonded centrals. */ +#define BLE_BONDMNGR_MAX_BONDED_CENTRALS 7 + +/**< Bit mask that defines an empty address in flash. */ +#define PSTORAGE_FLASH_EMPTY_MASK 0x00000000 + +#endif // BLE_BONDMNGR_CFG_H__ + +/** @} */ +/** @endcond */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_conn_params.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,134 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_lib_conn_params Connection Parameters Negotiation + * @{ + * @ingroup ble_sdk_lib + * @brief Module for initiating and executing a connection parameters negotiation procedure. + */ + +#ifndef BLE_CONN_PARAMS_H__ +#define BLE_CONN_PARAMS_H__ + +#include <stdint.h> +#include "ble.h" +#include "ble_srv_common.h" + +/**@brief Connection Parameters Module event type. */ +typedef enum +{ + BLE_CONN_PARAMS_EVT_FAILED , /**< Negotiation procedure failed. */ + BLE_CONN_PARAMS_EVT_SUCCEEDED /**< Negotiation procedure succeeded. */ +} ble_conn_params_evt_type_t; + +/**@brief Connection Parameters Module event. */ +typedef struct +{ + ble_conn_params_evt_type_t evt_type; /**< Type of event. */ +} ble_conn_params_evt_t; + +/**@brief Connection Parameters Module event handler type. */ +typedef void (*ble_conn_params_evt_handler_t) (ble_conn_params_evt_t * p_evt); + +/**@brief Connection Parameters Module init structure. This contains all options and data needed for + * initialization of the connection parameters negotiation module. */ +typedef struct +{ + ble_gap_conn_params_t * p_conn_params; /**< Pointer to the connection parameters desired by the application. When calling ble_conn_params_init, if this parameter is set to NULL, the connection parameters will be fetched from host. */ + uint32_t first_conn_params_update_delay; /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (in number of timer ticks). */ + uint32_t next_conn_params_update_delay; /**< Time between each call to sd_ble_gap_conn_param_update after the first (in number of timer ticks). Recommended value 30 seconds as per BLUETOOTH SPECIFICATION Version 4.0. */ + uint8_t max_conn_params_update_count; /**< Number of attempts before giving up the negotiation. */ + uint16_t start_on_notify_cccd_handle; /**< If procedure is to be started when notification is started, set this to the handle of the corresponding CCCD. Set to BLE_GATT_HANDLE_INVALID if procedure is to be started on connect event. */ + bool disconnect_on_fail; /**< Set to TRUE if a failed connection parameters update shall cause an automatic disconnection, set to FALSE otherwise. */ + ble_conn_params_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Connection Parameters. */ + ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ +} ble_conn_params_init_t; + + +/**@brief Function for initializing the Connection Parameters module. + * + * @note If the negotiation procedure should be triggered when notification/indication of + * any characteristic is enabled by the peer, then this function must be called after + * having initialized the services. + * + * @param[in] p_init This contains information needed to initialize this module. + * + * @return NRF_SUCCESS on successful initialization, otherwise an error code. + */ +uint32_t ble_conn_params_init(const ble_conn_params_init_t * p_init); + +/**@brief Function for stopping the Connection Parameters module. + * + * @details This function is intended to be used by the application to clean up the connection + * parameters update module. This will stop the connection parameters update timer if + * running, thereby preventing any impending connection parameters update procedure. This + * function must be called by the application when it needs to clean itself up (for + * example, before disabling the bluetooth SoftDevice) so that an unwanted timer expiry + * event can be avoided. + * + * @return NRF_SUCCESS on successful initialization, otherwise an error code. + */ +uint32_t ble_conn_params_stop(void); + +/**@brief Function for changing the current connection parameters to a new set. + * + * @details Use this function to change the connection parameters to a new set of parameter + * (ie different from the ones given at init of the module). + * This function is usefull for scenario where most of the time the application + * needs a relatively big connection interval, and just sometimes, for a temporary + * period requires shorter connection interval, for example to transfer a higher + * amount of data. + * If the given parameters does not match the current connection's parameters + * this function initiates a new negotiation. + * + * @param[in] new_params This contains the new connections parameters to setup. + * + * @return NRF_SUCCESS on successful initialization, otherwise an error code. + */ +uint32_t ble_conn_params_change_conn_params(ble_gap_conn_params_t *new_params); + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack that are of interest to this module. + * + * @param[in] p_ble_evt The event received from the BLE stack. + */ +void ble_conn_params_on_ble_evt(ble_evt_t * p_ble_evt); + +#endif // BLE_CONN_PARAMS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_debug_assert_handler.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,71 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_debug_assert_handler Assert Handler for debug purposes. + * @{ + * @ingroup ble_sdk_lib + * @brief Module for handling of assert during application development when debugging. + * + * @details This module may be used during development of an application to facilitate debugging. + * It contains a function to write file name, line number and the Stack Memory to flash. + * This module is ONLY for debugging purposes and must never be used in final product. + * + */ + +#ifndef BLE_DEBUG_ASSERT_HANDLER_H__ +#define BLE_DEBUG_ASSERT_HANDLER_H__ + +#include <stdint.h> + +/**@brief Function for handling the Debug assert, which can be called from an error handler. + * To be used only for debugging purposes. + * + *@details This code will copy the filename and line number into local variables for them to always + * be accessible in Keil debugger. The function will also write the ARM Cortex-M0 stack + * memory into flash where it can be retrieved and manually un-winded in order to + * back-trace the location where the error ocured.<br> + * @warning <b>ALL INTERRUPTS WILL BE DISABLED.</b> + * + * @note This function will never return but loop forever for debug purposes. + * + * @param[in] error_code Error code supplied to the handler. + * @param[in] line_num Line number where the original handler is called. + * @param[in] p_file_name Pointer to the file name. + */ +void ble_debug_assert_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name); + +#endif /* BLE_DEBUG_ASSERT_HANDLER_H__ */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_dtm_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,52 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BLE_DTM_APP_H__ +#define BLE_DTM_APP_H__ + +#include "app_uart_stream.h" + +uint32_t ble_dtm_init_req_enc(app_uart_stream_comm_params_t const * const p_uart_comm_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +uint32_t ble_dtm_init_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +uint32_t ble_dtm_init(app_uart_stream_comm_params_t * uart_comm_params); + + +#endif // BLE_DTM_APP_H__ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_encode_access.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,127 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXX + * @{ + * @ingroup XXX + * + * @brief XXX + */ + +#ifndef BLE_ENCODE_ACCESS_H__ +#define BLE_ENCODE_ACCESS_H__ + +#include <stdint.h> +#include "ble.h" + +/**@brief Command result callback function type. + * + * @param[in] result_code Command result code. + */ +typedef void (*ble_encode_cmd_resp_handler_t)(uint32_t result_code); + +/**@brief Generic event callback function events. */ +typedef enum +{ + BLE_ENCODE_EVT_RDY /**< An event indicating that SoftDevice event is available for read. */ +} ble_encode_evt_type_t; + +/**@brief Event callback function type. + * + * @param[in] event The event occurred. + */ +typedef void (*ble_encode_event_handler_t)(ble_encode_evt_type_t event); + +/**@brief Function for opening the module. + * + * @warning Must not be called for a module which has been allready opened. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_INTERNAL Operation failure. Internal error ocurred. + */ +uint32_t ble_encode_open(void); + +/**@brief Function for closing the module and resetting its internal state. + * + * @note Can be called multiple times and also for not opened module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t ble_encode_close(void); + +/**@brief Function for registering a BLE command response callback handler. + * + * @param[in] command_resp_handler BLE command response handler. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_BUSY Operation failure. Command allready in progress. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t ble_encode_cmd_resp_handler_reg(ble_encode_cmd_resp_handler_t command_resp_handler); + +/**@brief Function for registering a generic event handler. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called upon an event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t ble_encode_evt_handler_register(ble_encode_event_handler_t event_handler); + +/**@brief Function for extracting an received BLE event. + * + * If @ref p_event is NULL, the required length of @ref p_event is returned in @ref p_event_len. + * + * @param[out] p_event Pointer to memory where event specific data is copied. If + * NULL, required length will be returned in @ref p_event_len. + * @param[in,out] p_event_len in: Size (in bytes) of @ref p_event buffer. + * out: Length of decoded contents of @ref p_event. + * + * @retval NRF_SUCCESS Operation success. Event was copied to @ref p_event or in case @ref + * p_event is NULL @ref p_event_len is updated. + * @retval NRF_ERROR_NO_MEM Operation failure. No event available to extract. + * @retval NRF_ERROR_DATA_SIZE Operation failure. Length of @ref p_event is too small to hold + * decoded event. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t ble_encode_event_pop(ble_evt_t * p_event, uint32_t * p_event_len); + +#endif // BLE_ENCODE_ACCESS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_encode_transport.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,109 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXX + * @{ + * @ingroup XXX + * + * @brief XXX + */ + +#include <stdint.h> + +#ifndef BLE_ENCODE_TRANSPORT_H__ +#define BLE_ENCODE_TRANSPORT_H__ + +/**@brief @ref ble_encode_transport_cmd_write API command mode definitions. */ +typedef enum +{ + BLE_ENCODE_WRITE_MODE_RESP, /**< Command written includes a corresponding command response. */ + BLE_ENCODE_WRITE_MODE_NO_RESP, /**< Command written does not include a corresponding command response. */ + BLE_ENCODE_WRITE_MODE_MAX /**< Enumeration upper bound. */ +} ble_encode_cmd_write_mode_t; + +/**@brief Command response callback function type. + * + * Callback function for decoding the possible command response output parameteres and copying them + * to application and decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +typedef uint32_t (*ble_command_resp_decode_callback_t)(const uint8_t * p_buffer, uint32_t length); + +/**@brief Function for allocating TX command memory. + * + * @note If memory can't be acquired error check is executed as implies an error as not allowed by + * the system design and must be fixed at compile time. + * + * @return Pointer to the begin of the buffer allocated. + */ +uint8_t * ble_encode_transport_tx_alloc(void); + +/**@brief Function for freeing TX command memory. + * + * @note Memory management works in FIFO principle meaning that free order must match the alloc + * order. + * @note Does nothing if no TX command memory to be freed exists. + */ +void ble_encode_transport_tx_free(void); + +/**@brief Function for writing a command. + * + * @note Attempt to have multiple commands to be in progress at same time will lead to error check + * as not allowed by the system design. + * @note Error check is executed for validating @ref p_buffer against NULL. + * @note Error check is executed for validating @ref length against 0. + * @note Error check is executed for validating @ref cmd_write_mode. + * @note Error check is executed for validating @ref cmd_resp_decode_callback against NULL. + * + * @param[in] p_buffer Pointer to begin of command buffer. + * @param[in] length Length of data in bytes. + * @param[in] cmd_write_mode Command write mode see @ref + * ble_command_resp_decode_callback_t for details. + * @param[in] cmd_resp_decode_callback Command response callback. + */ +void ble_encode_transport_cmd_write(const uint8_t * p_buffer, + uint32_t length, + ble_encode_cmd_write_mode_t cmd_write_mode, + ble_command_resp_decode_callback_t cmd_resp_decode_callback); + +#endif // BLE_ENCODE_TRANSPORT_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_err.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /** + @addtogroup BLE_COMMON + @{ + @addtogroup nrf_error + @{ + @ingroup BLE_COMMON + @} + + @defgroup ble_err General error codes + @{ + + @brief General error code definitions for the BLE API. + + @ingroup BLE_COMMON +*/ +#ifndef NRF_BLE_ERR_H__ +#define NRF_BLE_ERR_H__ + +#include "nrf_error.h" + +/* @defgroup BLE_ERRORS Error Codes + * @{ */ +#define BLE_ERROR_NOT_ENABLED (NRF_ERROR_STK_BASE_NUM+0x001) /**< @ref sd_ble_enable has not been called. */ +#define BLE_ERROR_INVALID_CONN_HANDLE (NRF_ERROR_STK_BASE_NUM+0x002) /**< Invalid connection handle. */ +#define BLE_ERROR_INVALID_ATTR_HANDLE (NRF_ERROR_STK_BASE_NUM+0x003) /**< Invalid attribute handle. */ +#define BLE_ERROR_NO_TX_BUFFERS (NRF_ERROR_STK_BASE_NUM+0x004) /**< Buffer capacity exceeded. */ +/** @} */ + + +/** @defgroup BLE_ERROR_SUBRANGES Module specific error code subranges + * @brief Assignment of subranges for module specific error codes. + * @note For specific error codes, see ble_<module>.h or ble_error_<module>.h. + * @{ */ +#define NRF_L2CAP_ERR_BASE (NRF_ERROR_STK_BASE_NUM+0x100) /**< L2CAP specific errors. */ +#define NRF_GAP_ERR_BASE (NRF_ERROR_STK_BASE_NUM+0x200) /**< GAP specific errors. */ +#define NRF_GATTC_ERR_BASE (NRF_ERROR_STK_BASE_NUM+0x300) /**< GATT client specific errors. */ +#define NRF_GATTS_ERR_BASE (NRF_ERROR_STK_BASE_NUM+0x400) /**< GATT server specific errors. */ +/** @} */ + +#endif + + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_gap.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,1059 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_GAP Generic Access Profile (GAP) + @{ + @brief Definitions and prototypes for the GAP interface. + */ + +#ifndef BLE_GAP_H__ +#define BLE_GAP_H__ + +#include "ble_types.h" +#include "ble_ranges.h" +#include "nrf_svc.h" + + +/**@addtogroup BLE_GAP_ENUMERATIONS Enumerations + * @{ */ + +/**@brief GAP API SVC numbers. + */ +enum BLE_GAP_SVCS +{ + SD_BLE_GAP_ADDRESS_SET = BLE_GAP_SVC_BASE, /**< Set own Bluetooth Address. */ + SD_BLE_GAP_ADDRESS_GET, /**< Get own Bluetooth Address. */ + SD_BLE_GAP_ADV_DATA_SET, /**< Set Advertisement Data. */ + SD_BLE_GAP_ADV_START, /**< Start Advertising. */ + SD_BLE_GAP_ADV_STOP, /**< Stop Advertising. */ + SD_BLE_GAP_CONN_PARAM_UPDATE, /**< Connection Parameter Update. */ + SD_BLE_GAP_DISCONNECT, /**< Disconnect. */ + SD_BLE_GAP_TX_POWER_SET, /**< Set TX Power. */ + SD_BLE_GAP_APPEARANCE_SET, /**< Set Appearance. */ + SD_BLE_GAP_APPEARANCE_GET, /**< Get Appearance. */ + SD_BLE_GAP_PPCP_SET, /**< Set PPCP. */ + SD_BLE_GAP_PPCP_GET, /**< Get PPCP. */ + SD_BLE_GAP_DEVICE_NAME_SET, /**< Set Device Name. */ + SD_BLE_GAP_DEVICE_NAME_GET, /**< Get Device Name. */ + SD_BLE_GAP_AUTHENTICATE, /**< Initiate Pairing/Bonding. */ + SD_BLE_GAP_SEC_PARAMS_REPLY, /**< Reply with Security Parameters. */ + SD_BLE_GAP_AUTH_KEY_REPLY, /**< Reply with an authentication key. */ + SD_BLE_GAP_SEC_INFO_REPLY, /**< Reply with Security Information. */ + SD_BLE_GAP_CONN_SEC_GET, /**< Obtain connection security level. */ + SD_BLE_GAP_RSSI_START, /**< Start reporting of changes in RSSI. */ + SD_BLE_GAP_RSSI_STOP, /**< Stop reporting of changes in RSSI. */ +}; +/**@} */ + +/**@addtogroup BLE_GAP_DEFINES Defines + * @{ */ + +/**@defgroup BLE_ERRORS_GAP SVC return values specific to GAP + * @{ */ +#define BLE_ERROR_GAP_UUID_LIST_MISMATCH (NRF_GAP_ERR_BASE + 0x000) /**< UUID list does not contain an integral number of UUIDs. */ +#define BLE_ERROR_GAP_DISCOVERABLE_WITH_WHITELIST (NRF_GAP_ERR_BASE + 0x001) /**< Use of Whitelist not permitted with discoverable advertising. */ +#define BLE_ERROR_GAP_INVALID_BLE_ADDR (NRF_GAP_ERR_BASE + 0x002) /**< The upper two bits of the address do not correspond to the specified address type. */ +/**@} */ + + +/**@defgroup BLE_GAP_ROLES GAP Roles + * @note Not explicitly used in peripheral API, but will be relevant for central API. + * @{ */ +#define BLE_GAP_ROLE_INVALID 0x0 /**< Invalid Role. */ +#define BLE_GAP_ROLE_PERIPH 0x1 /**< Peripheral Role. */ +#define BLE_GAP_ROLE_CENTRAL 0x2 /**< Central Role. */ +/**@} */ + + +/**@defgroup BLE_GAP_TIMEOUT_SOURCES GAP Timeout sources + * @{ */ +#define BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT 0x00 /**< Advertisement timeout. */ +#define BLE_GAP_TIMEOUT_SRC_SECURITY_REQUEST 0x01 /**< Security request timeout. */ +/**@} */ + + +/**@defgroup BLE_GAP_ADDR_TYPES GAP Address types + * @{ */ +#define BLE_GAP_ADDR_TYPE_PUBLIC 0x00 /**< Public address. */ +#define BLE_GAP_ADDR_TYPE_RANDOM_STATIC 0x01 /**< Random Static address. */ +#define BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE 0x02 /**< Private Resolvable address. */ +#define BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE 0x03 /**< Private Non-Resolvable address. */ +/**@} */ + +/**@defgroup BLE_GAP_ADDR_CYCLE_MODES GAP Address cycle modes + * @{ */ +#define BLE_GAP_ADDR_CYCLE_MODE_NONE 0x00 /**< Set addresses directly, no automatic address cycling. */ +#define BLE_GAP_ADDR_CYCLE_MODE_AUTO 0x01 /**< Automatically generate and update private addresses. */ +/** @} */ + +/**@brief The default interval in seconds at which a private address is refreshed when address cycle mode is @ref BLE_GAP_ADDR_CYCLE_MODE_AUTO. */ +#define BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S (60 * 15) + +/** @brief BLE address length. */ +#define BLE_GAP_ADDR_LEN 6 + + +/**@defgroup BLE_GAP_AD_TYPE_DEFINITIONS GAP Advertising and Scan Response Data format + * @note Found at https://www.bluetooth.org/Technical/AssignedNumbers/generic_access_profile.htm + * @{ */ +#define BLE_GAP_AD_TYPE_FLAGS 0x01 /**< Flags for discoverability. */ +#define BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE 0x02 /**< Partial list of 16 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE 0x03 /**< Complete list of 16 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE 0x04 /**< Partial list of 32 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_COMPLETE 0x05 /**< Complete list of 32 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE 0x06 /**< Partial list of 128 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE 0x07 /**< Complete list of 128 bit service UUIDs. */ +#define BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME 0x08 /**< Short local device name. */ +#define BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME 0x09 /**< Complete local device name. */ +#define BLE_GAP_AD_TYPE_TX_POWER_LEVEL 0x0A /**< Transmit power level. */ +#define BLE_GAP_AD_TYPE_CLASS_OF_DEVICE 0x0D /**< Class of device. */ +#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_HASH_C 0x0E /**< Simple Pairing Hash C. */ +#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_RANDOMIZER_R 0x0F /**< Simple Pairing Randomizer R. */ +#define BLE_GAP_AD_TYPE_SECURITY_MANAGER_TK_VALUE 0x10 /**< Security Manager TK Value. */ +#define BLE_GAP_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS 0x11 /**< Security Manager Out Of Band Flags. */ +#define BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE 0x12 /**< Slave Connection Interval Range. */ +#define BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT 0x14 /**< List of 16-bit Service Solicitation UUIDs. */ +#define BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT 0x15 /**< List of 128-bit Service Solicitation UUIDs. */ +#define BLE_GAP_AD_TYPE_SERVICE_DATA 0x16 /**< Service Data - 16-bit UUID. */ +#define BLE_GAP_AD_TYPE_PUBLIC_TARGET_ADDRESS 0x17 /**< Public Target Address. */ +#define BLE_GAP_AD_TYPE_RANDOM_TARGET_ADDRESS 0x18 /**< Random Target Address. */ +#define BLE_GAP_AD_TYPE_APPEARANCE 0x19 /**< Appearance. */ +#define BLE_GAP_AD_TYPE_ADVERTISING_INTERVAL 0x1A /**< Advertising Interval. */ +#define BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS 0x1B /**< LE Bluetooth Device Address. */ +#define BLE_GAP_AD_TYPE_LE_ROLE 0x1C /**< LE Role. */ +#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_HASH_C256 0x1D /**< Simple Pairing Hash C-256. */ +#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_RANDOMIZER_R256 0x1E /**< Simple Pairing Randomizer R-256. */ +#define BLE_GAP_AD_TYPE_SERVICE_DATA_32BIT_UUID 0x20 /**< Service Data - 32-bit UUID. */ +#define BLE_GAP_AD_TYPE_SERVICE_DATA_128BIT_UUID 0x21 /**< Service Data - 128-bit UUID. */ +#define BLE_GAP_AD_TYPE_3D_INFORMATION_DATA 0x3D /**< 3D Information Data. */ +#define BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA 0xFF /**< Manufacturer Specific Data. */ +/**@} */ + + +/**@defgroup BLE_GAP_ADV_FLAGS GAP Advertisement Flags + * @{ */ +#define BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE (0x01) /**< LE Limited Discoverable Mode. */ +#define BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE (0x02) /**< LE General Discoverable Mode. */ +#define BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED (0x04) /**< BR/EDR not supported. */ +#define BLE_GAP_ADV_FLAG_LE_BR_EDR_CONTROLLER (0x08) /**< Simultaneous LE and BR/EDR, Controller. */ +#define BLE_GAP_ADV_FLAG_LE_BR_EDR_HOST (0x10) /**< Simultaneous LE and BR/EDR, Host. */ +#define BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE (BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) /**< LE Limited Discoverable Mode, BR/EDR not supported. */ +#define BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE (BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) /**< LE General Discoverable Mode, BR/EDR not supported. */ +/**@} */ + + +/**@defgroup BLE_GAP_ADV_INTERVALS GAP Advertising interval max and min + * @{ */ +#define BLE_GAP_ADV_INTERVAL_MIN 0x0020 /**< Minimum Advertising interval in 625 us units, i.e. 20 ms. */ +#define BLE_GAP_ADV_NONCON_INTERVAL_MIN 0x00A0 /**< Minimum Advertising interval in 625 us units for non connectable mode, i.e. 100 ms. */ +#define BLE_GAP_ADV_INTERVAL_MAX 0x4000 /**< Maximum Advertising interval in 625 us units, i.e. 10.24 s. */ + /**@} */ + + +/**@brief Maximum size of advertising data in octets. */ +#define BLE_GAP_ADV_MAX_SIZE 31 + + +/**@defgroup BLE_GAP_ADV_TYPES GAP Advertising types + * @{ */ +#define BLE_GAP_ADV_TYPE_ADV_IND 0x00 /**< Connectable undirected. */ +#define BLE_GAP_ADV_TYPE_ADV_DIRECT_IND 0x01 /**< Connectable directed. */ +#define BLE_GAP_ADV_TYPE_ADV_SCAN_IND 0x02 /**< Scannable undirected. */ +#define BLE_GAP_ADV_TYPE_ADV_NONCONN_IND 0x03 /**< Non connectable undirected. */ +/**@} */ + + +/**@defgroup BLE_GAP_ADV_FILTER_POLICIES GAP Advertising filter policies + * @{ */ +#define BLE_GAP_ADV_FP_ANY 0x00 /**< Allow scan requests and connect requests from any device. */ +#define BLE_GAP_ADV_FP_FILTER_SCANREQ 0x01 /**< Filter scan requests with whitelist. */ +#define BLE_GAP_ADV_FP_FILTER_CONNREQ 0x02 /**< Filter connect requests with whitelist. */ +#define BLE_GAP_ADV_FP_FILTER_BOTH 0x03 /**< Filter both scan and connect requests with whitelist. */ +/**@} */ + + +/**@defgroup BLE_GAP_ADV_TIMEOUT_VALUES GAP Advertising timeout values + * @{ */ +#define BLE_GAP_ADV_TIMEOUT_LIMITED_MAX 180 /**< Maximum advertising time in limited discoverable mode (TGAP(lim_adv_timeout) = 180s in spec (Addendum 2)). */ +#define BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED 0 /**< Unlimited advertising in general discoverable mode. */ +/**@} */ + + +/**@defgroup BLE_GAP_DISC_MODES GAP Discovery modes + * @{ */ +#define BLE_GAP_DISC_MODE_NOT_DISCOVERABLE 0x00 /**< Not discoverable discovery Mode. */ +#define BLE_GAP_DISC_MODE_LIMITED 0x01 /**< Limited Discovery Mode. */ +#define BLE_GAP_DISC_MODE_GENERAL 0x02 /**< General Discovery Mode. */ +/**@} */ + +/**@defgroup BLE_GAP_IO_CAPS GAP IO Capabilities + * @{ */ +#define BLE_GAP_IO_CAPS_DISPLAY_ONLY 0x00 /**< Display Only. */ +#define BLE_GAP_IO_CAPS_DISPLAY_YESNO 0x01 /**< Display and Yes/No entry. */ +#define BLE_GAP_IO_CAPS_KEYBOARD_ONLY 0x02 /**< Keyboard Only. */ +#define BLE_GAP_IO_CAPS_NONE 0x03 /**< No I/O capabilities. */ +#define BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY 0x04 /**< Keyboard and Display. */ +/**@} */ + + +/**@defgroup BLE_GAP_AUTH_KEY_TYPES GAP Authentication Key Types + * @{ */ +#define BLE_GAP_AUTH_KEY_TYPE_NONE 0x00 /**< No key (may be used to reject). */ +#define BLE_GAP_AUTH_KEY_TYPE_PASSKEY 0x01 /**< 6-digit Passkey. */ +#define BLE_GAP_AUTH_KEY_TYPE_OOB 0x02 /**< Out Of Band data. */ +/**@} */ + +/**@defgroup BLE_GAP_SEC_STATUS GAP Security status + * @{ */ +#define BLE_GAP_SEC_STATUS_SUCCESS 0x00 /**< Successful parameters. */ +#define BLE_GAP_SEC_STATUS_TIMEOUT 0x01 /**< Procedure timed out. */ +#define BLE_GAP_SEC_STATUS_PDU_INVALID 0x02 /**< Invalid PDU received. */ +#define BLE_GAP_SEC_STATUS_PASSKEY_ENTRY_FAILED 0x81 /**< Passkey entry failed (user cancelled or other). */ +#define BLE_GAP_SEC_STATUS_OOB_NOT_AVAILABLE 0x82 /**< Out of Band Key not available. */ +#define BLE_GAP_SEC_STATUS_AUTH_REQ 0x83 /**< Authentication requirements not met. */ +#define BLE_GAP_SEC_STATUS_CONFIRM_VALUE 0x84 /**< Confirm value failed. */ +#define BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP 0x85 /**< Pairing not supported. */ +#define BLE_GAP_SEC_STATUS_ENC_KEY_SIZE 0x86 /**< Encryption key size. */ +#define BLE_GAP_SEC_STATUS_SMP_CMD_UNSUPPORTED 0x87 /**< Unsupported SMP command. */ +#define BLE_GAP_SEC_STATUS_UNSPECIFIED 0x88 /**< Unspecified reason. */ +#define BLE_GAP_SEC_STATUS_REPEATED_ATTEMPTS 0x89 /**< Too little time elapsed since last attempt. */ +#define BLE_GAP_SEC_STATUS_INVALID_PARAMS 0x8A /**< Invalid parameters. */ +/**@} */ + +/**@defgroup BLE_GAP_SEC_STATUS_SOURCES GAP Security status sources + * @{ */ +#define BLE_GAP_SEC_STATUS_SOURCE_LOCAL 0x00 /**< Local failure. */ +#define BLE_GAP_SEC_STATUS_SOURCE_REMOTE 0x01 /**< Remote failure. */ +/**@} */ + +/**@defgroup BLE_GAP_CP_LIMITS GAP Connection Parameters Limits + * @{ */ +#define BLE_GAP_CP_MIN_CONN_INTVL_NONE 0xFFFF /**< No new minimum connction interval specified in connect parameters. */ +#define BLE_GAP_CP_MIN_CONN_INTVL_MIN 0x0006 /**< Lowest mimimum connection interval permitted, in units of 1.25 ms, i.e. 7.5 ms. */ +#define BLE_GAP_CP_MIN_CONN_INTVL_MAX 0x0C80 /**< Highest minimum connection interval permitted, in units of 1.25 ms, i.e. 4 s. */ +#define BLE_GAP_CP_MAX_CONN_INTVL_NONE 0xFFFF /**< No new maximum connction interval specified in connect parameters. */ +#define BLE_GAP_CP_MAX_CONN_INTVL_MIN 0x0006 /**< Lowest maximum connection interval permitted, in units of 1.25 ms, i.e. 7.5 ms. */ +#define BLE_GAP_CP_MAX_CONN_INTVL_MAX 0x0C80 /**< Highest maximum connection interval permitted, in units of 1.25 ms, i.e. 4 s. */ +#define BLE_GAP_CP_SLAVE_LATENCY_MAX 0x03E8 /**< Highest slave latency permitted, in connection events. */ +#define BLE_GAP_CP_CONN_SUP_TIMEOUT_NONE 0xFFFF /**< No new supervision timeout specified in connect parameters. */ +#define BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN 0x000A /**< Lowest supervision timeout permitted, in units of 10 ms, i.e. 100 ms. */ +#define BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX 0x0C80 /**< Highest supervision timeout permitted, in units of 10 ms, i.e. 32 s. */ +/**@} */ + + +/**@brief GAP device name maximum length. */ +#define BLE_GAP_DEVNAME_MAX_LEN 31 + + +/**@defgroup BLE_GAP_CONN_SEC_MODE_SET_MACROS GAP attribute security requirement setters + * + * See @ref ble_gap_conn_sec_mode_t. + * @{ */ +/**@brief Set sec_mode pointed to by ptr to have no access rights.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(ptr) do {(ptr)->sm = 0; (ptr)->lv = 0;} while(0) +/**@brief Set sec_mode pointed to by ptr to require no protection, open link.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_OPEN(ptr) do {(ptr)->sm = 1; (ptr)->lv = 1;} while(0) +/**@brief Set sec_mode pointed to by ptr to require encryption, but no MITM protection.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(ptr) do {(ptr)->sm = 1; (ptr)->lv = 2;} while(0) +/**@brief Set sec_mode pointed to by ptr to require encryption and MITM protection.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(ptr) do {(ptr)->sm = 1; (ptr)->lv = 3;} while(0) +/**@brief Set sec_mode pointed to by ptr to require signing or encryption, no MITM protection needed.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(ptr) do {(ptr)->sm = 2; (ptr)->lv = 1;} while(0) +/**@brief Set sec_mode pointed to by ptr to require signing or encryption with MITM protection.*/ +#define BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(ptr) do {(ptr)->sm = 2; (ptr)->lv = 2;} while(0) +/**@} */ + + +/**@brief GAP Security Key Length. */ +#define BLE_GAP_SEC_KEY_LEN 16 + +/**@brief GAP Passkey Length. */ +#define BLE_GAP_PASSKEY_LEN 6 + +/**@brief Maximum amount of addresses in a whitelist. */ +#define BLE_GAP_WHITELIST_ADDR_MAX_COUNT (8) + +/**@brief Maximum amount of IRKs in a whitelist. + * @note The number of IRKs is limited to 8, even if the hardware supports more. + */ +#define BLE_GAP_WHITELIST_IRK_MAX_COUNT (8) + +/**@defgroup GAP_SEC_MODES GAP Security Modes + * @{ */ +#define BLE_GAP_SEC_MODE 0x00 /**< No key (may be used to reject). */ +/**@} */ + +/**@} */ + +/**@addtogroup BLE_GAP_STRUCTURES Structures + * @{ */ + +/**@brief Bluetooth Low Energy address. */ +typedef struct +{ + uint8_t addr_type; /**< See @ref BLE_GAP_ADDR_TYPES. */ + uint8_t addr[BLE_GAP_ADDR_LEN]; /**< 48-bit address, LSB format. */ +} ble_gap_addr_t; + + +/**@brief GAP connection parameters. + * + * @note When ble_conn_params_t is received in an event, both min_conn_interval and + * max_conn_interval will be equal to the connection interval set by the central. + */ +typedef struct +{ + uint16_t min_conn_interval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ + uint16_t max_conn_interval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ + uint16_t slave_latency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ + uint16_t conn_sup_timeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ +} ble_gap_conn_params_t; + + +/**@brief GAP link requirements. + * + * See Bluetooth Core specification, Volume 3 Part C 10.2 for details. + * + * Security Mode 0 Level 0: No access permissions at all (this level is not defined by the Bluetooth Core specification).\n + * Security Mode 1 Level 1: No security is needed (aka open link).\n + * Security Mode 1 Level 2: Encrypted link required, MITM protection not necessary.\n + * Security Mode 1 Level 3: MITM protected encrypted link required.\n + * Security Mode 2 Level 1: Signing or encryption required, MITM protection not necessary.\n + * Security Mode 2 Level 2: MITM protected signing required, unless link is MITM protected encrypted.\n + */ +typedef struct +{ + uint8_t sm : 4; /**< Security Mode (1 or 2), 0 for no permissions at all. */ + uint8_t lv : 4; /**< Level (1, 2 or 3), 0 for no permissions at all. */ + +} ble_gap_conn_sec_mode_t; + + +/**@brief GAP connection security status.*/ +typedef struct +{ + ble_gap_conn_sec_mode_t sec_mode; /**< Currently active security mode for this connection.*/ + uint8_t encr_key_size; /**< Length of currently active encryption key, 7 to 16 octets (only applicable for bonding procedures). */ +} ble_gap_conn_sec_t; + + +/**@brief Identity Resolving Key. */ +typedef struct +{ + uint8_t irk[BLE_GAP_SEC_KEY_LEN]; /**< Array containing IRK. */ +} ble_gap_irk_t; + + +/**@brief Whitelist structure. */ +typedef struct +{ + ble_gap_addr_t ** pp_addrs; /**< Pointer to array of device address pointers, pointing to addresses to be used in whitelist. NULL if none are given. */ + uint8_t addr_count; /**< Count of device addresses in array, up to @ref BLE_GAP_WHITELIST_ADDR_MAX_COUNT. */ + ble_gap_irk_t ** pp_irks; /**< Pointer to array of Identity Resolving Key (IRK) pointers, each pointing to an IRK in the whitelist. NULL if none are given. */ + uint8_t irk_count; /**< Count of IRKs in array, up to @ref BLE_GAP_WHITELIST_IRK_MAX_COUNT. */ +} ble_gap_whitelist_t; + + +/**@brief GAP advertising parameters.*/ +typedef struct +{ + uint8_t type; /**< See @ref BLE_GAP_ADV_TYPES. */ + ble_gap_addr_t* p_peer_addr; /**< For BLE_GAP_CONN_MODE_DIRECTED mode only, known peer address. */ + uint8_t fp; /**< Filter Policy, see @ref BLE_GAP_ADV_FILTER_POLICIES. */ + ble_gap_whitelist_t * p_whitelist; /**< Pointer to whitelist, NULL if none is given. */ + uint16_t interval; /**< Advertising interval between 0x0020 and 0x4000 in 0.625 ms units (20ms to 10.24s), see @ref BLE_GAP_ADV_INTERVALS. This parameter must be set to 0 if type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND. */ + uint16_t timeout; /**< Advertising timeout between 0x0001 and 0x3FFF in seconds, 0x0000 disables timeout. See also @ref BLE_GAP_ADV_TIMEOUT_VALUES. This parameter must be set to 0 if type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND. */ +} ble_gap_adv_params_t; + + +/**@brief GAP scanning parameters. */ +typedef struct +{ + uint8_t filter; /**< Filter based on discovery mode, see @ref BLE_GAP_DISC_MODES. */ + uint8_t active : 1; /**< If 1, perform active scanning (scan requests). */ + uint8_t selective : 1; /**< If 1, ignore unknown devices (non whitelisted). */ + uint16_t interval; /**< Scan interval between 0x0020 and 0x4000 in 0.625ms units (20ms to 10.24s). */ + uint16_t window; /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ + uint16_t timeout; /**< Scan timeout between 0x0001 and 0x3FFF in seconds, 0x0000 disables timeout. */ +} ble_gap_scan_params_t; + + +/**@brief GAP security parameters. */ +typedef struct +{ + uint16_t timeout; /**< Timeout for SMP transactions or Security Request in seconds, see @ref sd_ble_gap_authenticate and @ref sd_ble_gap_sec_params_reply for more information. */ + uint8_t bond : 1; /**< Perform bonding. */ + uint8_t mitm : 1; /**< Man In The Middle protection required. */ + uint8_t io_caps : 3; /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */ + uint8_t oob : 1; /**< Out Of Band data available. */ + uint8_t min_key_size; /**< Minimum encryption key size in octets between 7 and 16. */ + uint8_t max_key_size; /**< Maximum encryption key size in octets between min_key_size and 16. */ +} ble_gap_sec_params_t; + + +/**@brief GAP Encryption Information. */ +typedef struct +{ + uint16_t div; /**< Encryption Diversifier. */ + uint8_t ltk[BLE_GAP_SEC_KEY_LEN]; /**< Long Term Key. */ + uint8_t auth : 1; /**< Authenticated Key. */ + uint8_t ltk_len : 7; /**< LTK length in octets. */ +} ble_gap_enc_info_t; + + +/**@brief GAP Master Identification. */ +typedef struct +{ + uint16_t ediv; /**< Encrypted Diversifier. */ + uint8_t rand[8]; /**< Random Number. */ +} ble_gap_master_id_t; + + +/**@brief GAP Identity Information. */ +typedef struct +{ + ble_gap_addr_t addr; /**< Bluetooth address to which this key applies. */ + uint8_t irk[BLE_GAP_SEC_KEY_LEN]; /**< Identity Resolution Key. */ +} ble_gap_id_info_t; + + +/**@brief GAP Signing Information. */ +typedef struct +{ + uint8_t csrk[BLE_GAP_SEC_KEY_LEN]; /* Connection Signature Resolving Key. */ +} ble_gap_sign_info_t; + + +/**@brief GAP Event IDs. + * Those IDs uniquely identify an event coming from the stack to the application. + */ +enum BLE_GAP_EVTS +{ + BLE_GAP_EVT_CONNECTED = BLE_GAP_EVT_BASE, /**< Connection established. */ + BLE_GAP_EVT_DISCONNECTED, /**< Disconnected from peer. */ + BLE_GAP_EVT_CONN_PARAM_UPDATE, /**< Connection Parameters updated. */ + BLE_GAP_EVT_SEC_PARAMS_REQUEST, /**< Request to provide security parameters. */ + BLE_GAP_EVT_SEC_INFO_REQUEST, /**< Request to provide security information. */ + BLE_GAP_EVT_PASSKEY_DISPLAY, /**< Request to display a passkey to the user. */ + BLE_GAP_EVT_AUTH_KEY_REQUEST, /**< Request to provide an authentication key. */ + BLE_GAP_EVT_AUTH_STATUS, /**< Authentication procedure completed with status. */ + BLE_GAP_EVT_CONN_SEC_UPDATE, /**< Connection security updated. */ + BLE_GAP_EVT_TIMEOUT, /**< Timeout expired. */ + BLE_GAP_EVT_RSSI_CHANGED, /**< Signal strength measurement report. */ +}; + + +/** + * @brief GAP Option IDs. + * IDs that uniquely identify a GAP option. + */ +enum BLE_GAP_OPTS +{ + BLE_GAP_OPT_LOCAL_CONN_LATENCY = BLE_GAP_OPT_BASE, /**< Local connection latency. */ + BLE_GAP_OPT_PASSKEY, /**< Set passkey to be used during pairing. This option can be used to make the SoftDevice use an application provided passkey instead of generating a random passkey.*/ + BLE_GAP_OPT_PRIVACY, /**< Set or get custom IRK or custom private address cycle interval. */ +}; +/**@} */ + + +/**@brief Event data for connected event. */ +typedef struct +{ + ble_gap_addr_t peer_addr; /**< Bluetooth address of the peer device. */ + uint8_t irk_match :1; /**< If 1, peer device's address resolved using an IRK. */ + uint8_t irk_match_idx :7; /**< Index in IRK list where the address was matched. */ + ble_gap_conn_params_t conn_params; /**< GAP Connection Parameters. */ +} ble_gap_evt_connected_t; + + +/**@brief Event data for disconnected event. */ +typedef struct +{ + uint8_t reason; /**< HCI error code. */ +} ble_gap_evt_disconnected_t; + + +/**@brief Event data for connection parameter update event. */ +typedef struct +{ + ble_gap_conn_params_t conn_params; /**< GAP Connection Parameters. */ +} ble_gap_evt_conn_param_update_t; + + +/**@brief Event data for security parameters request event. */ +typedef struct +{ + ble_gap_sec_params_t peer_params; /**< Initiator Security Parameters. */ +} ble_gap_evt_sec_params_request_t; + + +/**@brief Event data for security info request event. */ +typedef struct +{ + ble_gap_addr_t peer_addr; /**< Bluetooth address of the peer device. */ + uint16_t div; /**< Encryption diversifier for LTK lookup. */ + uint8_t enc_info : 1; /**< If 1, Encryption Information required. */ + uint8_t id_info : 1; /**< If 1, Identity Information required. */ + uint8_t sign_info : 1; /**< If 1, Signing Information required. */ +} ble_gap_evt_sec_info_request_t; + + +/**@brief Event data for passkey display event. */ +typedef struct +{ + uint8_t passkey[BLE_GAP_PASSKEY_LEN]; /**< 6-digit passkey in ASCII ('0'-'9' digits only). */ +} ble_gap_evt_passkey_display_t; + + +/**@brief Event data for authentication key request event. */ +typedef struct +{ + uint8_t key_type; /**< See @ref BLE_GAP_AUTH_KEY_TYPES. */ +} ble_gap_evt_auth_key_request_t; + + +/**@brief Security levels supported. + * @note See Bluetooth Specification Version 4.1 Volume 3, Part C, Chapter 10. +*/ +typedef struct +{ + uint8_t lv1 : 1; /**< If 1: Level 1 is supported. */ + uint8_t lv2 : 1; /**< If 1: Level 2 is supported. */ + uint8_t lv3 : 1; /**< If 1: Level 3 is supported. */ +} ble_gap_sec_levels_t; + + +/**@brief Keys that have been exchanged. */ +typedef struct +{ + uint8_t ltk : 1; /**< Long Term Key. */ + uint8_t ediv_rand : 1; /**< Encrypted Diversifier and Random value. */ + uint8_t irk : 1; /**< Identity Resolving Key. */ + uint8_t address : 1; /**< Public or static random address. */ + uint8_t csrk : 1; /**< Connection Signature Resolving Key. */ +} ble_gap_sec_keys_t; + + +/**@brief Event data for authentication status event. */ +typedef struct +{ + uint8_t auth_status; /**< Authentication status, see @ref BLE_GAP_SEC_STATUS. */ + uint8_t error_src; /**< On error, source that caused the failure, see @ref BLE_GAP_SEC_STATUS_SOURCES. */ + ble_gap_sec_levels_t sm1_levels; /**< Levels supported in Security Mode 1. */ + ble_gap_sec_levels_t sm2_levels; /**< Levels supported in Security Mode 2. */ + ble_gap_sec_keys_t periph_kex; /**< Bitmap stating which keys were exchanged (distributed) by the peripheral. */ + ble_gap_sec_keys_t central_kex; /**< Bitmap stating which keys were exchanged (distributed) by the central. */ + struct periph_keys_t + { + ble_gap_enc_info_t enc_info; /**< Peripheral's Encryption information. */ + } periph_keys; /**< Actual keys distributed from the Peripheral to the Central. */ + struct central_keys_t + { + ble_gap_irk_t irk; /**< Central's IRK. */ + ble_gap_addr_t id_info; /**< Central's Identity Info. */ + } central_keys; /**< Actual keys distributed from the Central to the Peripheral. */ +} ble_gap_evt_auth_status_t; + + +/**@brief Event data for connection security update event. */ +typedef struct +{ + ble_gap_conn_sec_t conn_sec; /**< Connection security level. */ +} ble_gap_evt_conn_sec_update_t; + + +/**@brief Event data for timeout event. */ +typedef struct +{ + uint8_t src; /**< Source of timeout event, see @ref BLE_GAP_TIMEOUT_SOURCES. */ +} ble_gap_evt_timeout_t; + + +/**@brief Event data for advertisement report event. */ +typedef struct +{ + int8_t rssi; /**< Received Signal Strength Indication in dBm. */ +} ble_gap_evt_rssi_changed_t; + + +/**@brief GAP event callback event structure. */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle on which event occured. */ + union /**< union alternative identified by evt_id in enclosing struct. */ + { + ble_gap_evt_connected_t connected; /**< Connected Event Parameters. */ + ble_gap_evt_disconnected_t disconnected; /**< Disconnected Event Parameters. */ + ble_gap_evt_conn_param_update_t conn_param_update; /**< Connection Parameter Update Parameters. */ + ble_gap_evt_sec_params_request_t sec_params_request; /**< Security Parameters Request Event Parameters. */ + ble_gap_evt_sec_info_request_t sec_info_request; /**< Security Information Request Event Parameters. */ + ble_gap_evt_passkey_display_t passkey_display; /**< Passkey Display Event Parameters. */ + ble_gap_evt_auth_key_request_t auth_key_request; /**< Authentication Key Request Event Parameters. */ + ble_gap_evt_auth_status_t auth_status; /**< Authentication Status Event Parameters. */ + ble_gap_evt_conn_sec_update_t conn_sec_update; /**< Connection Security Update Event Parameters. */ + ble_gap_evt_timeout_t timeout; /**< Timeout Event Parameters. */ + ble_gap_evt_rssi_changed_t rssi_changed; /**< RSSI Event parameters. */ + } params; + +} ble_gap_evt_t; + + +/**@brief Local connection latency option. + * + * Local connection latency is a feature which enables the slave to improve + * current consumption by ignoring the slave latency set by the peer. The + * local connection latency can only be set to a multiple of the slave latency, + * and cannot be longer than half of the supervision timeout. + * + * Used with @ref sd_ble_opt_set to set the local connection latency. The + * @ref sd_ble_opt_get is not supported for this option, but the actual + * local connection latency (unless set to NULL) is set as a return parameter + * when setting the option. + * + * @note The latency set will be truncated down to the closest slave latency event + * multiple, or the nearest multiple before half of the supervision timeout. + * + * @note The local connection latency is default off, and needs to be set for new + * connections and whenever the connection is updated. + * + * @retval ::NRF_SUCCESS Set successfully. + * @retval ::NRF_ERROR_NOT_SUPPORTED Get is not supported. + * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle parameter. + */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle */ + uint16_t requested_latency; /**< Requested local connection latency. */ + uint16_t * p_actual_latency; /**< Pointer to storage for the actual local connection latency (can be set to NULL to skip return value). */ +} ble_gap_opt_local_conn_latency_t; + + +/**@brief Passkey Option. + * + * Structure containing the passkey to be used during pairing. This can be used with @ref + * sd_ble_opt_set to make the SoftDevice use a pre-programmed passkey for authentication + * instead of generating a random one. + * + * @note @ref sd_ble_opt_get is not supported for this option. + * + */ +typedef struct +{ + uint8_t * p_passkey; /**< Pointer to 6-digit ASCII string (digit 0..9 only, no NULL termination) passkey to be used during pairing. If this is NULL, the SoftDevice will generate a random passkey if required.*/ +} ble_gap_opt_passkey_t; + + +/**@brief Custom Privacy Options. + * + * @note The specified address cycle interval is used when the address cycle mode is + * @ref BLE_GAP_ADDR_CYCLE_MODE_AUTO. If 0 is given, the address will not be refreshed at any + * interval, and not at start of advertising. A new address can be generated manually by calling + * @ref sd_ble_gap_address_set with the same type again. The default interval is + * @ref BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S. + * + * @note If cycle mode is @ref BLE_GAP_ADDR_CYCLE_MODE_AUTO, the address will immediately be + * refreshed when this option is set. + */ +typedef struct +{ + ble_gap_irk_t * p_irk; /**< When input: Pointer to custom IRK, or NULL to use/reset to the device's default IRK. When output: Pointer to where the current IRK is to be stored, or NULL to not read out the IRK. */ + uint16_t interval_s; /**< When input: Custom private address cycle interval in seconds. When output: The current private address cycle interval. */ +} ble_gap_opt_privacy_t; + + +/**@brief Option structure for GAP options. */ +typedef union +{ + ble_gap_opt_local_conn_latency_t local_conn_latency; /**< Local connection latency. */ + ble_gap_opt_passkey_t passkey; /**< Passkey to be used for pairing.*/ + ble_gap_opt_privacy_t privacy; /**< Custom privacy options. */ +} ble_gap_opt_t; +/**@} */ + + +/**@addtogroup BLE_GAP_FUNCTIONS Functions + * @{ */ + +/**@brief Set local Bluetooth address. + * + * If the address cycle mode is @ref BLE_GAP_ADDR_CYCLE_MODE_AUTO, the address type is required to + * be @ref BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE or + * @ref BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE. The given address is ignored and the + * SoftDevice will generate a new private address automatically every time advertising is + * (re)started, and every @ref BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S seconds. If this API + * call is used again with the same parameters while advertising, the SoftDevice will immediately + * generate a new private address to replace the current address. + * + * If the application wishes to use a @ref BLE_GAP_ADDR_TYPE_PUBLIC or + * @ref BLE_GAP_ADDR_TYPE_RANDOM_STATIC address, the cycle mode must be + * @ref BLE_GAP_ADDR_CYCLE_MODE_NONE. + * + * If this API function is called while advertising, the softdevice will immediately update the + * advertising address without the need to stop advertising in the following cases: + * - If the previously set address is of type @ref BLE_GAP_ADDR_TYPE_PUBLIC and the new address + * is also of type @ref BLE_GAP_ADDR_TYPE_PUBLIC + * - If the previously set address is not @ref BLE_GAP_ADDR_TYPE_PUBLIC and the new address is + * also not @ref BLE_GAP_ADDR_TYPE_PUBLIC. + * + * If the address is changed from a @ref BLE_GAP_ADDR_TYPE_PUBLIC address to another type or from + * another type to a @ref BLE_GAP_ADDR_TYPE_PUBLIC address, the change will take effect the next + * time advertising is started. + * + * @note If the address cycle mode is @ref BLE_GAP_ADDR_CYCLE_MODE_NONE and the application is + * using privacy, the application must take care to generate and set new private addresses + * periodically to comply with the Privacy specification in Bluetooth Core Spec. + * + * @param[in] addr_cycle_mode Address cycle mode, see @ref BLE_GAP_ADDR_CYCLE_MODES. + * @param[in] p_addr Pointer to address structure. + * + * @return @ref NRF_SUCCESS Address successfully set. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameters. + * @return @ref BLE_ERROR_GAP_INVALID_BLE_ADDR Invalid address. + * @return @ref NRF_ERROR_BUSY The stack is busy, process pending events and retry. + */ +SVCALL(SD_BLE_GAP_ADDRESS_SET, uint32_t, sd_ble_gap_address_set(uint8_t addr_cycle_mode, ble_gap_addr_t const * const p_addr)); + + +/**@brief Get local Bluetooth address. + * + * @param[out] p_addr Pointer to address structure. + * + * @return @ref NRF_SUCCESS Address successfully retrieved. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + */ +SVCALL(SD_BLE_GAP_ADDRESS_GET, uint32_t, sd_ble_gap_address_get(ble_gap_addr_t * const p_addr)); + + +/**@brief Set, clear or update advertisement and scan response data. + * + * @note The format of the advertisement data will be checked by this call to ensure interoperability. + * Limitations imposed by this API call to the data provided include having a flags data type in the scan response data and + * duplicating the local name in the advertisement data and scan response data. + * + * @note: To clear the advertisement data and set it to a 0-length packet, simply provide a valid pointer (p_data/p_sr_data) with its corresponding + * length (dlen/srdlen) set to 0. + * + * @note: The call will fail if p_data and p_sr_data are both NULL since this would have no effect. + * + * @param[in] p_data Raw data to be placed in advertisement packet. If NULL, no changes are made to the current advertisement packet data. + * @param[in] dlen Data length for p_data. Max size: @ref BLE_GAP_ADV_MAX_SIZE octets. Should be 0 if p_data is NULL, can be 0 if p_data is not NULL. + * @param[in] p_sr_data Raw data to be placed in scan response packet. If NULL, no changes are made to the current scan response packet data. + * @param[in] srdlen Data length for p_sr_data. Max size: @ref BLE_GAP_ADV_MAX_SIZE octets. Should be 0 if p_sr_data is NULL, can be 0 if p_data is not NULL. + * + * @return @ref NRF_SUCCESS Advertisement data successfully updated or cleared. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_FLAGS Invalid combination of advertising flags supplied. + * @return @ref NRF_ERROR_INVALID_DATA Invalid data type(s) supplied, check the advertising data format specification. + * @return @ref NRF_ERROR_INVALID_LENGTH Invalid data length(s) supplied. + * @return @ref BLE_ERROR_GAP_UUID_LIST_MISMATCH Invalid UUID list supplied. + * @return @ref NRF_ERROR_BUSY The stack is busy, process pending events and retry. + */ +SVCALL(SD_BLE_GAP_ADV_DATA_SET, uint32_t, sd_ble_gap_adv_data_set(uint8_t const * const p_data, uint8_t dlen, uint8_t const * const p_sr_data, uint8_t srdlen)); + + +/**@brief Start advertising (GAP Discoverable, Connectable modes, Broadcast Procedure). + * + * @param[in] p_adv_params Pointer to advertising parameters structure. + * + * @return @ref NRF_SUCCESS The BLE stack has started advertising. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check the accepted ranges and limits. + * @return @ref BLE_ERROR_GAP_INVALID_BLE_ADDR Invalid Bluetooth address supplied. + * @return @ref BLE_ERROR_GAP_DISCOVERABLE_WITH_WHITELIST Discoverable mode and whitelist incompatible. + */ +SVCALL(SD_BLE_GAP_ADV_START, uint32_t, sd_ble_gap_adv_start(ble_gap_adv_params_t const * const p_adv_params)); + + +/**@brief Stop advertising (GAP Discoverable, Connectable modes, Broadcast Procedure). + * + * @return @ref NRF_SUCCESS The BLE stack has stopped advertising. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation (most probably not in advertising state). + */ +SVCALL(SD_BLE_GAP_ADV_STOP, uint32_t, sd_ble_gap_adv_stop(void)); + + +/**@brief Update connection parameters. + * + * @details In the central role this will initiate a Link Layer connection parameter update procedure, + * otherwise in the peripheral role, this will send the corresponding L2CAP request and wait for + * the central to perform the procedure. In both cases, and regardless of success or failure, the application + * will be informed of the result with a @ref BLE_GAP_EVT_CONN_PARAM_UPDATE event. + * + * @note If both a connection supervision timeout and a maximum connection interval are specified, then the following constraint + * applies: (conn_sup_timeout * 8) >= (max_conn_interval * (slave_latency + 1)) + * + * @param[in] conn_handle Connection handle. + * @param[in] p_conn_params Pointer to desired connection parameters. If NULL is provided on a peripheral role, + * the parameters in the PPCP characteristic of the GAP service will be used instead. + * + * @return @ref NRF_SUCCESS The Connection Update procedure has been started successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints. + * @return @ref NRF_ERROR_BUSY Procedure already in progress or not allowed at this time, process pending events and retry. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + */ +SVCALL(SD_BLE_GAP_CONN_PARAM_UPDATE, uint32_t, sd_ble_gap_conn_param_update(uint16_t conn_handle, ble_gap_conn_params_t const * const p_conn_params)); + + +/**@brief Disconnect (GAP Link Termination). + * + * @details This call initiates the disconnection procedure, and its completion will be communicated to the application + * with a BLE_GAP_EVT_DISCONNECTED event. + * + * @param[in] conn_handle Connection handle. + * @param[in] hci_status_code HCI status code, see @ref BLE_HCI_STATUS_CODES (accepted values are BTLE_REMOTE_USER_TERMINATED_CONNECTION and BTLE_CONN_INTERVAL_UNACCEPTABLE). + * + * @return @ref NRF_SUCCESS The disconnection procedure has been started successfully. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation (disconnection is already in progress or not connected at all). + */ +SVCALL(SD_BLE_GAP_DISCONNECT, uint32_t, sd_ble_gap_disconnect(uint16_t conn_handle, uint8_t hci_status_code)); + + +/**@brief Set the radio's transmit power. + * + * @param[in] tx_power Radio transmit power in dBm (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). + * + * @note -40 dBm will not actually give -40 dBm, but will instead be remapped to -30 dBm. + * + * @return @ref NRF_SUCCESS Successfully changed the transmit power. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_BUSY The stack is busy, process pending events and retry. + */ +SVCALL(SD_BLE_GAP_TX_POWER_SET, uint32_t, sd_ble_gap_tx_power_set(int8_t tx_power)); + + +/**@brief Set GAP Appearance value. + * + * @param[in] appearance Appearance (16-bit), see @ref BLE_APPEARANCES. + * + * @return @ref NRF_SUCCESS Appearance value set successfully. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + */ +SVCALL(SD_BLE_GAP_APPEARANCE_SET, uint32_t, sd_ble_gap_appearance_set(uint16_t appearance)); + + +/**@brief Get GAP Appearance value. + * + * @param[out] p_appearance Appearance (16-bit), see @ref BLE_APPEARANCES. + * + * @return @ref NRF_SUCCESS Appearance value retrieved successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + */ +SVCALL(SD_BLE_GAP_APPEARANCE_GET, uint32_t, sd_ble_gap_appearance_get(uint16_t * const p_appearance)); + + +/**@brief Set GAP Peripheral Preferred Connection Parameters. + * + * @param[in] p_conn_params Pointer to a @ref ble_gap_conn_params_t structure with the desired parameters. + * + * @return @ref NRF_SUCCESS Peripheral Preferred Connection Parameters set successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + */ +SVCALL(SD_BLE_GAP_PPCP_SET, uint32_t, sd_ble_gap_ppcp_set(ble_gap_conn_params_t const * const p_conn_params)); + + +/**@brief Get GAP Peripheral Preferred Connection Parameters. + * + * @param[out] p_conn_params Pointer to a @ref ble_gap_conn_params_t structure where the parameters will be stored. + * + * @return @ref NRF_SUCCESS Peripheral Preferred Connection Parameters retrieved successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + */ +SVCALL(SD_BLE_GAP_PPCP_GET, uint32_t, sd_ble_gap_ppcp_get(ble_gap_conn_params_t * const p_conn_params)); + + +/**@brief Set GAP device name. + * + * @param[in] p_write_perm Write permissions for the Device Name characteristic see @ref ble_gap_conn_sec_mode_t. + * @param[in] p_dev_name Pointer to a UTF-8 encoded, <b>non NULL-terminated</b> string. + * @param[in] len Length of the UTF-8, <b>non NULL-terminated</b> string pointed to by p_dev_name in octets (must be smaller or equal than @ref BLE_GAP_DEVNAME_MAX_LEN). + * + * @return @ref NRF_SUCCESS GAP device name and permissions set successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied. + */ +SVCALL(SD_BLE_GAP_DEVICE_NAME_SET, uint32_t, sd_ble_gap_device_name_set(ble_gap_conn_sec_mode_t const * const p_write_perm, uint8_t const * const p_dev_name, uint16_t len)); + + +/**@brief Get GAP device name. + * + * @param[in] p_dev_name Pointer to an empty buffer where the UTF-8 <b>non NULL-terminated</b> string will be placed. Set to NULL to obtain the complete device name length. + * @param[in,out] p_len Length of the buffer pointed by p_dev_name, complete device name length on output. + * + * @note If the device name is longer than the size of the supplied buffer, + * p_len will return the complete device name length, + * and not the number of bytes actually returned in p_dev_name. + * The application may use this information to allocate a suitable buffer size. + * + * @return @ref NRF_SUCCESS GAP device name retrieved successfully. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied. + */ +SVCALL(SD_BLE_GAP_DEVICE_NAME_GET, uint32_t, sd_ble_gap_device_name_get(uint8_t * const p_dev_name, uint16_t * const p_len)); + + +/**@brief Initiate GAP Authentication procedure. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_sec_params Pointer to the @ref ble_gap_sec_params_t structure with the security parameters to be used during the pairing procedure. + * + * @details In the central role, this function will send an SMP Pairing Request, otherwise in the peripheral role, an SMP Security Request will be sent. + * In the peripheral role, only the timeout, bond and mitm fields of @ref ble_gap_sec_params_t are used. + * + * @note The GAP Authentication procedure may be triggered by the central without calling this function when accessing a secure service. + * @note Calling this function may result in the following events depending on the outcome and parameters: @ref BLE_GAP_EVT_SEC_PARAMS_REQUEST, + * @ref BLE_GAP_EVT_SEC_INFO_REQUEST, @ref BLE_GAP_EVT_AUTH_KEY_REQUEST, @ref BLE_GAP_EVT_AUTH_STATUS. + * @note The timeout parameter in @ref ble_gap_sec_params_t is interpreted here as the Security Request timeout + * + * + * @return @ref NRF_SUCCESS Successfully initiated authentication procedure. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_AUTHENTICATE, uint32_t, sd_ble_gap_authenticate(uint16_t conn_handle, ble_gap_sec_params_t const * const p_sec_params)); + + +/**@brief Reply with GAP security parameters. + * + * @param[in] conn_handle Connection handle. + * @param[in] sec_status Security status, see @ref BLE_GAP_SEC_STATUS. + * @param[in] p_sec_params Pointer to a @ref ble_gap_sec_params_t security parameters structure. + * + * @details This function is only used to reply to a @ref BLE_GAP_EVT_SEC_PARAMS_REQUEST, calling it at other times will result in an NRF_ERROR_INVALID_STATE. + * @note If the call returns an error code, the request is still pending, and the reply call may be repeated with corrected parameters. + * @note The timeout parameter in @ref ble_gap_sec_params_t is interpreted here as the SMP procedure timeout, and must be 30 seconds. The function will fail + * if the application supplies a different value. + * + * @return @ref NRF_SUCCESS Successfully accepted security parameter from the application. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_SEC_PARAMS_REPLY, uint32_t, sd_ble_gap_sec_params_reply(uint16_t conn_handle, uint8_t sec_status, ble_gap_sec_params_t const * const p_sec_params)); + + +/**@brief Reply with an authentication key. + * + * @param[in] conn_handle Connection handle. + * @param[in] key_type See @ref BLE_GAP_AUTH_KEY_TYPES. + * @param[in] key If key type is BLE_GAP_AUTH_KEY_TYPE_NONE, then NULL. + * If key type is BLE_GAP_AUTH_KEY_TYPE_PASSKEY, then a 6-byte ASCII string (digit 0..9 only, no NULL termination). + * If key type is BLE_GAP_AUTH_KEY_TYPE_OOB, then a 16-byte OOB key value in Little Endian format. + * + * @details This function is only used to reply to a @ref BLE_GAP_EVT_AUTH_KEY_REQUEST, calling it at other times will result in an NRF_ERROR_INVALID_STATE. + * @note If the call returns an error code, the request is still pending, and the reply call may be repeated with corrected parameters. + * + * @return @ref NRF_SUCCESS Authentication key successfully set. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_AUTH_KEY_REPLY, uint32_t, sd_ble_gap_auth_key_reply(uint16_t conn_handle, uint8_t key_type, uint8_t const * const key)); + + +/**@brief Reply with GAP security information. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_enc_info Pointer to a @ref ble_gap_enc_info_t encryption information structure. May be NULL to signal none is available. + * @param[in] p_sign_info Pointer to a @ref ble_gap_sign_info_t signing information structure. May be NULL to signal none is available. + * + * @details This function is only used to reply to a @ref BLE_GAP_EVT_SEC_INFO_REQUEST, calling it at other times will result in NRF_ERROR_INVALID_STATE. + * @note If the call returns an error code, the request is still pending, and the reply call may be repeated with corrected parameters. + * @note Data signing is not implemented yet. p_sign_info must therefore be NULL. + * + * @return @ref NRF_SUCCESS Successfully accepted security information. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + * @return @ref NRF_ERROR_BUSY The stack is busy, process pending events and retry. + */ +SVCALL(SD_BLE_GAP_SEC_INFO_REPLY, uint32_t, sd_ble_gap_sec_info_reply(uint16_t conn_handle, ble_gap_enc_info_t const * const p_enc_info, ble_gap_sign_info_t const * const p_sign_info)); + + +/**@brief Get the current connection security. + * + * @param[in] conn_handle Connection handle. + * @param[out] p_conn_sec Pointer to a @ref ble_gap_conn_sec_t structure to be filled in. + * + * @return @ref NRF_SUCCESS Current connection security successfully retrieved. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_CONN_SEC_GET, uint32_t, sd_ble_gap_conn_sec_get(uint16_t conn_handle, ble_gap_conn_sec_t * const p_conn_sec)); + + +/**@brief Start reporting the received signal strength to the application. + * + * A new event is reported whenever the RSSI value changes, until @ref sd_ble_gap_rssi_stop is called. + * + * @param[in] conn_handle Connection handle. + * + * @return @ref NRF_SUCCESS Successfully activated RSSI reporting. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_RSSI_START, uint32_t, sd_ble_gap_rssi_start(uint16_t conn_handle)); + + +/**@brief Stop reporting the received singnal strength. + * + * An RSSI change detected before the call but not yet received by the application + * may be reported after @ref sd_ble_gap_rssi_stop has been called. + * + * @param[in] conn_handle Connection handle. + * + * @return @ref NRF_SUCCESS Successfully deactivated RSSI reporting. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied. + */ +SVCALL(SD_BLE_GAP_RSSI_STOP, uint32_t, sd_ble_gap_rssi_stop(uint16_t conn_handle)); +/**@} */ + +#endif // BLE_GAP_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_gatt.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,193 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /** + @addtogroup BLE_GATT Generic Attribute Profile (GATT) Common + @{ + @brief Common definitions and prototypes for the GATT interfaces. + */ + +#ifndef BLE_GATT_H__ +#define BLE_GATT_H__ + +#include "ble_types.h" +#include "ble_ranges.h" + + +/** @addtogroup BLE_GATT_DEFINES Defines + * @{ */ + +/** @brief Default MTU size. */ +#define GATT_MTU_SIZE_DEFAULT 23 + +/** @brief Only the default MTU size of 23 is currently supported. */ +#define GATT_RX_MTU 23 + + +/**@brief Invalid Attribute Handle. */ +#define BLE_GATT_HANDLE_INVALID 0x0000 + +/** @defgroup BLE_GATT_TIMEOUT_SOURCES GATT Timeout sources + * @{ */ +#define BLE_GATT_TIMEOUT_SRC_PROTOCOL 0x00 /**< ATT Protocol timeout. */ +/** @} */ + +/** @defgroup BLE_GATT_WRITE_OPS GATT Write operations + * @{ */ +#define BLE_GATT_OP_INVALID 0x00 /**< Invalid Operation. */ +#define BLE_GATT_OP_WRITE_REQ 0x01 /**< Write Request. */ +#define BLE_GATT_OP_WRITE_CMD 0x02 /**< Write Command. */ +#define BLE_GATT_OP_SIGN_WRITE_CMD 0x03 /**< Signed Write Command. */ +#define BLE_GATT_OP_PREP_WRITE_REQ 0x04 /**< Prepare Write Request. */ +#define BLE_GATT_OP_EXEC_WRITE_REQ 0x05 /**< Execute Write Request. */ +/** @} */ + +/** @defgroup BLE_GATT_EXEC_WRITE_FLAGS GATT Execute Write flags + * @{ */ +#define BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL 0x00 +#define BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE 0x01 +/** @} */ + +/** @defgroup BLE_GATT_HVX_TYPES GATT Handle Value operations + * @{ */ +#define BLE_GATT_HVX_INVALID 0x00 /**< Invalid Operation. */ +#define BLE_GATT_HVX_NOTIFICATION 0x01 /**< Handle Value Notification. */ +#define BLE_GATT_HVX_INDICATION 0x02 /**< Handle Value Indication. */ +/** @} */ + +/** @defgroup BLE_GATT_STATUS_CODES GATT Status Codes + * @{ */ +#define BLE_GATT_STATUS_SUCCESS 0x0000 /**< Success. */ +#define BLE_GATT_STATUS_UNKNOWN 0x0001 /**< Unknown or not applicable status. */ +#define BLE_GATT_STATUS_ATTERR_INVALID 0x0100 /**< ATT Error: Invalid Error Code. */ +#define BLE_GATT_STATUS_ATTERR_INVALID_HANDLE 0x0101 /**< ATT Error: Invalid Attribute Handle. */ +#define BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED 0x0102 /**< ATT Error: Read not permitted. */ +#define BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED 0x0103 /**< ATT Error: Write not permitted. */ +#define BLE_GATT_STATUS_ATTERR_INVALID_PDU 0x0104 /**< ATT Error: Used in ATT as Invalid PDU. */ +#define BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION 0x0105 /**< ATT Error: Authenticated link required. */ +#define BLE_GATT_STATUS_ATTERR_REQUEST_NOT_SUPPORTED 0x0106 /**< ATT Error: Used in ATT as Request Not Supported. */ +#define BLE_GATT_STATUS_ATTERR_INVALID_OFFSET 0x0107 /**< ATT Error: Offset specified was past the end of the attribute. */ +#define BLE_GATT_STATUS_ATTERR_INSUF_AUTHORIZATION 0x0108 /**< ATT Error: Used in ATT as Insufficient Authorisation. */ +#define BLE_GATT_STATUS_ATTERR_PREPARE_QUEUE_FULL 0x0109 /**< ATT Error: Used in ATT as Prepare Queue Full. */ +#define BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND 0x010A /**< ATT Error: Used in ATT as Attribute not found. */ +#define BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_LONG 0x010B /**< ATT Error: Attribute cannot be read or written using read/write blob requests. */ +#define BLE_GATT_STATUS_ATTERR_INSUF_ENC_KEY_SIZE 0x010C /**< ATT Error: Encryption key size used is insufficient. */ +#define BLE_GATT_STATUS_ATTERR_INVALID_ATT_VAL_LENGTH 0x010D /**< ATT Error: Invalid value size. */ +#define BLE_GATT_STATUS_ATTERR_UNLIKELY_ERROR 0x010E /**< ATT Error: Very unlikely error. */ +#define BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION 0x010F /**< ATT Error: Encrypted link required. */ +#define BLE_GATT_STATUS_ATTERR_UNSUPPORTED_GROUP_TYPE 0x0110 /**< ATT Error: Attribute type is not a supported grouping attribute. */ +#define BLE_GATT_STATUS_ATTERR_INSUF_RESOURCES 0x0111 /**< ATT Error: Encrypted link required. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE1_BEGIN 0x0112 /**< ATT Error: Reserved for Future Use range #1 begin. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE1_END 0x017F /**< ATT Error: Reserved for Future Use range #1 end. */ +#define BLE_GATT_STATUS_ATTERR_APP_BEGIN 0x0180 /**< ATT Error: Application range begin. */ +#define BLE_GATT_STATUS_ATTERR_APP_END 0x019F /**< ATT Error: Application range end. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE2_BEGIN 0x01A0 /**< ATT Error: Reserved for Future Use range #2 begin. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE2_END 0x01DF /**< ATT Error: Reserved for Future Use range #2 end. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE3_BEGIN 0x01E0 /**< ATT Error: Reserved for Future Use range #3 begin. */ +#define BLE_GATT_STATUS_ATTERR_RFU_RANGE3_END 0x01FC /**< ATT Error: Reserved for Future Use range #3 end. */ +#define BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR 0x01FD /**< ATT Common Profile and Service Error: Client Characteristic Configuration Descriptor improperly configured. */ +#define BLE_GATT_STATUS_ATTERR_CPS_PROC_ALR_IN_PROG 0x01FE /**< ATT Common Profile and Service Error: Procedure Already in Progress. */ +#define BLE_GATT_STATUS_ATTERR_CPS_OUT_OF_RANGE 0x01FF /**< ATT Common Profile and Service Error: Out Of Range. */ +/** @} */ + + +/** @defgroup BLE_GATT_CPF_FORMATS Characteristic Presentation Formats + * @note Found at http://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml + * @{ */ +#define BLE_GATT_CPF_FORMAT_RFU 0x00 /**< Reserved For Future Use. */ +#define BLE_GATT_CPF_FORMAT_BOOLEAN 0x01 /**< Boolean. */ +#define BLE_GATT_CPF_FORMAT_2BIT 0x02 /**< Unsigned 2-bit integer. */ +#define BLE_GATT_CPF_FORMAT_NIBBLE 0x03 /**< Unsigned 4-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT8 0x04 /**< Unsigned 8-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT12 0x05 /**< Unsigned 12-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT16 0x06 /**< Unsigned 16-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT24 0x07 /**< Unsigned 24-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT32 0x08 /**< Unsigned 32-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT48 0x09 /**< Unsigned 48-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT64 0x0A /**< Unsigned 64-bit integer. */ +#define BLE_GATT_CPF_FORMAT_UINT128 0x0B /**< Unsigned 128-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT8 0x0C /**< Signed 2-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT12 0x0D /**< Signed 12-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT16 0x0E /**< Signed 16-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT24 0x0F /**< Signed 24-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT32 0x10 /**< Signed 32-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT48 0x11 /**< Signed 48-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT64 0x12 /**< Signed 64-bit integer. */ +#define BLE_GATT_CPF_FORMAT_SINT128 0x13 /**< Signed 128-bit integer. */ +#define BLE_GATT_CPF_FORMAT_FLOAT32 0x14 /**< IEEE-754 32-bit floating point. */ +#define BLE_GATT_CPF_FORMAT_FLOAT64 0x15 /**< IEEE-754 64-bit floating point. */ +#define BLE_GATT_CPF_FORMAT_SFLOAT 0x16 /**< IEEE-11073 16-bit SFLOAT. */ +#define BLE_GATT_CPF_FORMAT_FLOAT 0x17 /**< IEEE-11073 32-bit FLOAT. */ +#define BLE_GATT_CPF_FORMAT_DUINT16 0x18 /**< IEEE-20601 format. */ +#define BLE_GATT_CPF_FORMAT_UTF8S 0x19 /**< UTF-8 string. */ +#define BLE_GATT_CPF_FORMAT_UTF16S 0x1A /**< UTF-16 string. */ +#define BLE_GATT_CPF_FORMAT_STRUCT 0x1B /**< Opaque Structure. */ +/** @} */ + +/** @defgroup BLE_GATT_CPF_NAMESPACES GATT Bluetooth Namespaces + * @{ + */ +#define BLE_GATT_CPF_NAMESPACE_BTSIG 0x01 +#define BLE_GATT_CPF_NAMESPACE_DESCRIPTION_UNKNOWN 0x0000 +/** @} */ + +/** @} */ + +/**@brief GATT Characteristic Properties. */ +typedef struct +{ + /* Standard properties */ + uint8_t broadcast :1; /**< Broadcasting of value permitted. */ + uint8_t read :1; /**< Reading value permitted. */ + uint8_t write_wo_resp :1; /**< Writing value with Write Command permitted. */ + uint8_t write :1; /**< Writing value with Write Request permitted. */ + uint8_t notify :1; /**< Notications of value permitted. */ + uint8_t indicate :1; /**< Indications of value permitted. */ + uint8_t auth_signed_wr :1; /**< Writing value with Signed Write Command permitted. */ +} ble_gatt_char_props_t; + +/**@brief GATT Characteristic Extended Properties. */ +typedef struct +{ + /* Extended properties */ + uint8_t reliable_wr :1; /**< Writing value with Queued Write Request permitted. */ + uint8_t wr_aux :1; /**< Writing the Characteristic User Description permitted. */ +} ble_gatt_char_ext_props_t; + +#endif // BLE_GATT_H__ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_gattc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,422 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_GATTC Generic Attribute Profile (GATT) Client + @{ + @brief Definitions and prototypes for the GATT Client interface. + */ + +#ifndef BLE_GATTC_H__ +#define BLE_GATTC_H__ + +#include "ble_gatt.h" +#include "ble_types.h" +#include "ble_ranges.h" +#include "nrf_svc.h" + + +/**@brief GATTC API SVC numbers. */ +enum BLE_GATTC_SVCS +{ + SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER = BLE_GATTC_SVC_BASE, /**< Primary Service Discovery. */ + SD_BLE_GATTC_RELATIONSHIPS_DISCOVER, /**< Relationship Discovery. */ + SD_BLE_GATTC_CHARACTERISTICS_DISCOVER, /**< Characteristic Discovery. */ + SD_BLE_GATTC_DESCRIPTORS_DISCOVER, /**< Characteristic Descriptor Discovery. */ + SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ, /**< Read Characteristic Value by UUID. */ + SD_BLE_GATTC_READ, /**< Generic read. */ + SD_BLE_GATTC_CHAR_VALUES_READ, /**< Read multiple Characteristic Values. */ + SD_BLE_GATTC_WRITE, /**< Generic write. */ + SD_BLE_GATTC_HV_CONFIRM /**< Handle Value Confirmation. */ +}; + +/** @addtogroup BLE_GATTC_DEFINES Defines + * @{ */ + +/** @defgroup BLE_ERRORS_GATTC SVC return values specific to GATTC + * @{ */ +#define BLE_ERROR_GATTC_PROC_NOT_PERMITTED (NRF_GATTC_ERR_BASE + 0x000) +/** @} */ + +/**@brief Last Attribute Handle. */ +#define BLE_GATTC_HANDLE_END 0xFFFF + +/** @} */ + +/**@brief Operation Handle Range. */ +typedef struct +{ + uint16_t start_handle; /**< Start Handle. */ + uint16_t end_handle; /**< End Handle. */ +} ble_gattc_handle_range_t; + + +/**@brief GATT service. */ +typedef struct +{ + ble_uuid_t uuid; /**< Service UUID. */ + ble_gattc_handle_range_t handle_range; /**< Service Handle Range. */ +} ble_gattc_service_t; + + +/**@brief GATT include. */ +typedef struct +{ + uint16_t handle; /**< Include Handle. */ + ble_gattc_service_t included_srvc; /**< Handle of the included service. */ +} ble_gattc_include_t; + + +/**@brief GATT characteristic. */ +typedef struct +{ + ble_uuid_t uuid; /**< Characteristic UUID. */ + ble_gatt_char_props_t char_props; /**< Characteristic Properties. */ + uint8_t char_ext_props : 1; /**< Extended properties present. */ + uint16_t handle_decl; /**< Handle of the Characteristic Declaration. */ + uint16_t handle_value; /**< Handle of the Characteristic Value. */ +} ble_gattc_char_t; + + +/**@brief GATT descriptor. */ +typedef struct +{ + uint16_t handle; /**< Descriptor Handle. */ + ble_uuid_t uuid; /**< Descriptor UUID. */ +} ble_gattc_desc_t; + + +/**@brief Write Parameters. */ +typedef struct +{ + uint8_t write_op; /**< Write Operation to be performed, see BLE_GATT_WRITE_OPS. */ + uint16_t handle; /**< Handle to the attribute to be written. */ + uint16_t offset; /**< Offset in bytes. */ + uint16_t len; /**< Length of data in bytes. */ + uint8_t* p_value; /**< Pointer to the value data. */ + uint8_t flags; /**< Flags, see @ref BLE_GATT_EXEC_WRITE_FLAGS. */ +} ble_gattc_write_params_t; + + +/** + * @brief GATT Client Event IDs. + */ +enum BLE_GATTC_EVTS +{ + BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP = BLE_GATTC_EVT_BASE, /**< Primary Service Discovery Response event. */ + BLE_GATTC_EVT_REL_DISC_RSP, /**< Relationship Discovery Response event. */ + BLE_GATTC_EVT_CHAR_DISC_RSP, /**< Characteristic Discovery Response event. */ + BLE_GATTC_EVT_DESC_DISC_RSP, /**< Descriptor Discovery Response event. */ + BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP, /**< Read By UUID Response event. */ + BLE_GATTC_EVT_READ_RSP, /**< Read Response event. */ + BLE_GATTC_EVT_CHAR_VALS_READ_RSP, /**< Read multiple Response event. */ + BLE_GATTC_EVT_WRITE_RSP, /**< Write Response event. */ + BLE_GATTC_EVT_HVX, /**< Handle Value Notification or Indication event. */ + BLE_GATTC_EVT_TIMEOUT /**< Timeout event. */ +}; + +/**@brief Event structure for BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP. */ +typedef struct +{ + uint16_t count; /**< Service count. */ + ble_gattc_service_t services[1]; /**< Service data, variable length. */ +} ble_gattc_evt_prim_srvc_disc_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_REL_DISC_RSP. */ +typedef struct +{ + uint16_t count; /**< Include count. */ + ble_gattc_include_t includes[1]; /**< Include data, variable length. */ +} ble_gattc_evt_rel_disc_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_CHAR_DISC_RSP. */ +typedef struct +{ + uint16_t count; /**< Characteristic count. */ + ble_gattc_char_t chars[1]; /**< Characteristic data, variable length. */ +} ble_gattc_evt_char_disc_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_DESC_DISC_RSP. */ +typedef struct +{ + uint16_t count; /**< Descriptor count. */ + ble_gattc_desc_t descs[1]; /**< Descriptor data, variable length. */ +} ble_gattc_evt_desc_disc_rsp_t; + +/**@brief GATT read by UUID handle value pair. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ + uint8_t *p_value; /**< Pointer to value, variable length (length available as value_len in ble_gattc_evt_read_by_uuid_rsp_t). + Please note that this pointer is absolute to the memory provided by the user when retrieving the event, + so it will effectively point to a location inside the handle_value array. */ +} ble_gattc_handle_value_t; + +/**@brief Event structure for BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP. */ +typedef struct +{ + uint16_t count; /**< Handle-Value Pair Count. */ + uint16_t value_len; /**< Length of the value in Handle-Value(s) list. */ + ble_gattc_handle_value_t handle_value[1]; /**< Handle-Value(s) list, variable length. */ +} ble_gattc_evt_char_val_by_uuid_read_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_READ_RSP. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ + uint16_t offset; /**< Offset of the attribute data. */ + uint16_t len; /**< Attribute data length. */ + uint8_t data[1]; /**< Attribute data, variable length. */ +} ble_gattc_evt_read_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_CHAR_VALS_READ_RSP. */ +typedef struct +{ + uint16_t len; /**< Concatenated Attribute values length. */ + uint8_t values[1]; /**< Attribute values, variable length. */ +} ble_gattc_evt_char_vals_read_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_WRITE_RSP. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ + uint8_t write_op; /**< Type of write operation, see @ref BLE_GATT_WRITE_OPS. */ + uint16_t offset; /**< Data Offset. */ + uint16_t len; /**< Data length. */ + uint8_t data[1]; /**< Data, variable length. */ +} ble_gattc_evt_write_rsp_t; + +/**@brief Event structure for BLE_GATTC_EVT_HVX. */ +typedef struct +{ + uint16_t handle; /**< Handle to which the HVx operation applies. */ + uint8_t type; /**< Indication or Notification, see @ref BLE_GATT_HVX_TYPES. */ + uint16_t len; /**< Attribute data length. */ + uint8_t data[1]; /**< Attribute data, variable length. */ +} ble_gattc_evt_hvx_t; + +/**@brief Event structure for BLE_GATTC_EVT_TIMEOUT. */ +typedef struct +{ + uint8_t src; /**< Timeout source, see @ref BLE_GATT_TIMEOUT_SOURCES. */ +} ble_gattc_evt_timeout_t; + +/**@brief GATTC event type. */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle on which event occured. */ + uint16_t gatt_status; /**< GATT status code for the operation, see @ref BLE_GATT_STATUS_CODES. */ + uint16_t error_handle; /**< In case of error: The handle causing the error. In all other cases BLE_GATT_HANDLE_INVALID. */ + union + { + ble_gattc_evt_prim_srvc_disc_rsp_t prim_srvc_disc_rsp; /**< Primary Service Discovery Response Event Parameters. */ + ble_gattc_evt_rel_disc_rsp_t rel_disc_rsp; /**< Relationship Discovery Response Event Parameters. */ + ble_gattc_evt_char_disc_rsp_t char_disc_rsp; /**< Characteristic Discovery Response Event Parameters. */ + ble_gattc_evt_desc_disc_rsp_t desc_disc_rsp; /**< Descriptor Discovery Response Event Parameters. */ + ble_gattc_evt_char_val_by_uuid_read_rsp_t char_val_by_uuid_read_rsp; /**< Characteristic Value Read by UUID Response Event Parameters. */ + ble_gattc_evt_read_rsp_t read_rsp; /**< Read Response Event Parameters. */ + ble_gattc_evt_char_vals_read_rsp_t char_vals_read_rsp; /**< Characteristic Values Read Response Event Parameters. */ + ble_gattc_evt_write_rsp_t write_rsp; /**< Write Response Event Parameters. */ + ble_gattc_evt_hvx_t hvx; /**< Handle Value Notification/Indication Event Parameters. */ + ble_gattc_evt_timeout_t timeout; /**< Timeout Event Parameters. */ + } params; /**< Event Parameters. @note Only valid if @ref gatt_status == BLE_GATT_STATUS_SUCCESS. */ +} ble_gattc_evt_t; + + +/**@brief Initiate or continue a GATT Primary Service Discovery procedure. + * + * @details This function initiates a Primary Service discovery, starting from the supplied handle. + * If the last service has not been reached, this must be called again with an updated start handle value to continue the search. + * + * @note If any of the discovered services have 128-bit UUIDs which are not present in the table provided to ble_vs_uuids_assign, a UUID structure with + * type BLE_UUID_TYPE_UNKNOWN will be received in the corresponding event. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] start_handle Handle to start searching from. + * @param[in] p_srvc_uuid Pointer to the service UUID to be found. If it is NULL, all primary services will be returned. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Primary Service Discovery procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER, uint32_t, sd_ble_gattc_primary_services_discover(uint16_t conn_handle, uint16_t start_handle, ble_uuid_t const * const p_srvc_uuid)); + + +/**@brief Initiate or continue a GATT Relationship Discovery procedure. + * + * @details This function initiates the Find Included Services sub-procedure. If the last included service has not been reached, + * this must be called again with an updated handle range to continue the search. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_handle_range A pointer to the range of handles of the Service to perform this procedure on. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Relationship Discovery procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_RELATIONSHIPS_DISCOVER, uint32_t, sd_ble_gattc_relationships_discover(uint16_t conn_handle, ble_gattc_handle_range_t const * const p_handle_range)); + + +/**@brief Initiate or continue a GATT Characteristic Discovery procedure. + * + * @details This function initiates a Characteristic discovery procedure. If the last Characteristic has not been reached, + * this must be called again with an updated handle range to continue the discovery. + * + * @note If any of the discovered characteristics have 128-bit UUIDs which are not present in the table provided to ble_vs_uuids_assign, a UUID structure with + * type BLE_UUID_TYPE_UNKNOWN will be received in the corresponding event. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_handle_range A pointer to the range of handles of the Service to perform this procedure on. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Characteristic Discovery procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_CHARACTERISTICS_DISCOVER, uint32_t, sd_ble_gattc_characteristics_discover(uint16_t conn_handle, ble_gattc_handle_range_t const * const p_handle_range)); + + +/**@brief Initiate or continue a GATT Characteristic Descriptor Discovery procedure. + * + * @details This function initiates the Characteristic Descriptor discovery procedure. If the last Descriptor has not been reached, + * this must be called again with an updated handle range to continue the discovery. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_handle_range A pointer to the range of handles of the Characteristic to perform this procedure on. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Descriptor Discovery procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_DESCRIPTORS_DISCOVER, uint32_t, sd_ble_gattc_descriptors_discover(uint16_t conn_handle, ble_gattc_handle_range_t const * const p_handle_range)); + + +/**@brief Initiate or continue a GATT Read using Characteristic UUID procedure. + * + * @details This function initiates the Read using Characteristic UUID procedure. If the last Characteristic has not been reached, + * this must be called again with an updated handle range to continue the discovery. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_uuid Pointer to a Characteristic value UUID to read. + * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Read using Characteristic UUID procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ, uint32_t, sd_ble_gattc_char_value_by_uuid_read(uint16_t conn_handle, ble_uuid_t const * const p_uuid, ble_gattc_handle_range_t const * const p_handle_range)); + + +/**@brief Initiate or continue a GATT Read (Long) Characteristic or Descriptor procedure. + * + * @details This function initiates a GATT Read (Long) Characteristic or Descriptor procedure. If the Characteristic or Descriptor + * to be read is longer than GATT_MTU - 1, this function must be called multiple times with appropriate offset to read the + * complete value. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] handle The handle of the attribute to be read. + * @param[in] offset Offset into the attribute value to be read. + * + * @return @ref NRF_SUCCESS Successfully started or resumed the Read (Long) procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_READ, uint32_t, sd_ble_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset)); + + +/**@brief Initiate a GATT Read Multiple Characteristic Values procedure. + * + * @details This function initiates a GATT Read Multiple Characteristic Values procedure. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_handles A pointer to the handle(s) of the attribute(s) to be read. + * @param[in] handle_count The number of handles in p_handles. + * + * @return @ref NRF_SUCCESS Successfully started the Read Multiple Characteristic Values procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_BUSY Client procedure already in progress. + */ +SVCALL(SD_BLE_GATTC_CHAR_VALUES_READ, uint32_t, sd_ble_gattc_char_values_read(uint16_t conn_handle, uint16_t const * const p_handles, uint16_t handle_count)); + + +/**@brief Perform a Write (Characteristic Value or Descriptor, with or without response, signed or not, long or reliable) procedure. + * + * @details This function can perform all write procedures described in GATT. + * + * @note It is important to note that a write without response will <b>consume an application buffer</b>, and will therefore + * generate a @ref BLE_EVT_TX_COMPLETE event when the packet has been transmitted. A write on the other hand will use the + * standard client internal buffer and thus will only generate a @ref BLE_GATTC_EVT_WRITE_RSP event as soon as the write response + * has been received from the peer. Please see the documentation of @ref sd_ble_tx_buffer_count_get for more details. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] p_write_params A pointer to a write parameters structure. + * + * @return @ref NRF_SUCCESS Successfully started the Write procedure. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied. + * @return @ref NRF_ERROR_BUSY Procedure already in progress. + * @return @ref BLE_ERROR_NO_TX_BUFFERS There are no available buffers left. + */ +SVCALL(SD_BLE_GATTC_WRITE, uint32_t, sd_ble_gattc_write(uint16_t conn_handle, ble_gattc_write_params_t const * const p_write_params)); + + +/**@brief Send a Handle Value Confirmation to the GATT Server. + * + * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on. + * @param[in] handle The handle of the attribute in the indication. + * + * @return @ref NRF_SUCCESS Successfully queued the Handle Value Confirmation for transmission. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_STATE No Indication pending to be confirmed. + * @return @ref BLE_ERROR_INVALID_ATTR_HANDLE Invalid attribute handle. + * @return @ref BLE_ERROR_NO_TX_BUFFERS There are no available buffers left. + */ +SVCALL(SD_BLE_GATTC_HV_CONFIRM, uint32_t, sd_ble_gattc_hv_confirm(uint16_t conn_handle, uint16_t handle)); + + +#endif /* BLE_GATTC_H__ */ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_gatts.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,593 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_GATTS Generic Attribute Profile (GATT) Server + @{ + @brief Definitions and prototypes for the GATTS interface. + */ + +#ifndef BLE_GATTS_H__ +#define BLE_GATTS_H__ + +#include "ble_types.h" +#include "ble_ranges.h" +#include "ble_l2cap.h" +#include "ble_gap.h" +#include "ble_gatt.h" +#include "nrf_svc.h" + +/** @addtogroup BLE_GATTS_ENUMERATIONS Enumerations + * @{ */ + +/** + * @brief GATTS API SVC numbers. + */ +enum BLE_GATTS_SVCS +{ + SD_BLE_GATTS_SERVICE_ADD = BLE_GATTS_SVC_BASE, /**< Add a service. */ + SD_BLE_GATTS_INCLUDE_ADD, /**< Add an included service. */ + SD_BLE_GATTS_CHARACTERISTIC_ADD, /**< Add a characteristic. */ + SD_BLE_GATTS_DESCRIPTOR_ADD, /**< Add a generic attribute. */ + SD_BLE_GATTS_VALUE_SET, /**< Set an attribute value. */ + SD_BLE_GATTS_VALUE_GET, /**< Get an attribute value. */ + SD_BLE_GATTS_HVX, /**< Handle Value Notification or Indication. */ + SD_BLE_GATTS_SERVICE_CHANGED, /**< Perform a Service Changed Indication to one or more peers. */ + SD_BLE_GATTS_RW_AUTHORIZE_REPLY, /**< Reply to an authorization request for a read or write operation on one or more attributes. */ + SD_BLE_GATTS_SYS_ATTR_SET, /**< Set the persistent system attributes for a connection. */ + SD_BLE_GATTS_SYS_ATTR_GET, /**< Get updated persistent system attributes after terminating a connection. */ +}; + +/** @} */ + +/** @addtogroup BLE_GATTS_DEFINES Defines + * @{ */ + +/** @brief Only the default MTU size of 23 is currently supported. */ +#define GATT_RX_MTU 23 + +/** @defgroup BLE_ERRORS_GATTS SVC return values specific to GATTS + * @{ */ +#define BLE_ERROR_GATTS_INVALID_ATTR_TYPE (NRF_GATTS_ERR_BASE + 0x000) /**< Invalid attribute type. */ +#define BLE_ERROR_GATTS_SYS_ATTR_MISSING (NRF_GATTS_ERR_BASE + 0x001) /**< System Attributes missing. */ +/** @} */ + +/** @defgroup BLE_GATTS_ATTR_LENS_MAX Maximum attribute lengths + * @{ */ +#define BLE_GATTS_FIX_ATTR_LEN_MAX (510) /**< Maximum length for fixed length Attribute Values. */ +#define BLE_GATTS_VAR_ATTR_LEN_MAX (512) /**< Maximum length for variable length Attribute Values. */ +/** @} */ + +/** @defgroup BLE_GATTS_SRVC_TYPES GATT Server Service Types + * @{ */ +#define BLE_GATTS_SRVC_TYPE_INVALID 0x00 /**< Invalid Service Type. */ +#define BLE_GATTS_SRVC_TYPE_PRIMARY 0x01 /**< Primary Service. */ +#define BLE_GATTS_SRVC_TYPE_SECONDARY 0x02 /**< Secondary Type. */ +/** @} */ + + +/** @defgroup BLE_GATTS_ATTR_TYPES GATT Server Attribute Types + * @{ */ +#define BLE_GATTS_ATTR_TYPE_INVALID 0x00 /**< Invalid Attribute Type. */ +#define BLE_GATTS_ATTR_TYPE_PRIM_SRVC_DECL 0x01 /**< Primary Service Declaration. */ +#define BLE_GATTS_ATTR_TYPE_SEC_SRVC_DECL 0x02 /**< Secondary Service Declaration. */ +#define BLE_GATTS_ATTR_TYPE_INC_DECL 0x03 /**< Include Declaration. */ +#define BLE_GATTS_ATTR_TYPE_CHAR_DECL 0x04 /**< Characteristic Declaration. */ +#define BLE_GATTS_ATTR_TYPE_CHAR_VAL 0x05 /**< Characteristic Value. */ +#define BLE_GATTS_ATTR_TYPE_DESC 0x06 /**< Descriptor. */ +#define BLE_GATTS_ATTR_TYPE_OTHER 0x07 /**< Other, non-GATT specific type. */ +/** @} */ + + +/** @defgroup BLE_GATTS_OPS GATT Server Operations + * @{ */ +#define BLE_GATTS_OP_INVALID 0x00 /**< Invalid Operation. */ +#define BLE_GATTS_OP_WRITE_REQ 0x01 /**< Write Request. */ +#define BLE_GATTS_OP_WRITE_CMD 0x02 /**< Write Command. */ +#define BLE_GATTS_OP_SIGN_WRITE_CMD 0x03 /**< Signed Write Command. */ +#define BLE_GATTS_OP_PREP_WRITE_REQ 0x04 /**< Prepare Write Request. */ +#define BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL 0x05 /**< Execute Write Request: Cancel all prepared writes. */ +#define BLE_GATTS_OP_EXEC_WRITE_REQ_NOW 0x06 /**< Execute Write Request: Immediately execute all prepared writes. */ +/** @} */ + +/** @defgroup BLE_GATTS_VLOCS GATT Value Locations + * @{ */ +#define BLE_GATTS_VLOC_INVALID 0x00 /**< Invalid Location. */ +#define BLE_GATTS_VLOC_STACK 0x01 /**< Attribute Value is located in stack memory, no user memory is required. */ +#define BLE_GATTS_VLOC_USER 0x02 /**< Attribute Value is located in user memory. This requires the user to maintain a valid buffer through the lifetime of the attribute, since the stack + will read and write directly to the memory using the pointer provided in the APIs. There are no alignment requirements for the buffer. */ +/** @} */ + +/** @defgroup BLE_GATTS_AUTHORIZE_TYPES GATT Server Authorization Types + * @{ */ +#define BLE_GATTS_AUTHORIZE_TYPE_INVALID 0x00 /**< Invalid Type. */ +#define BLE_GATTS_AUTHORIZE_TYPE_READ 0x01 /**< Authorize a Read Operation. */ +#define BLE_GATTS_AUTHORIZE_TYPE_WRITE 0x02 /**< Authorize a Write Request Operation. */ +/** @} */ + + +/** @} */ + +/** @addtogroup BLE_GATTS_STRUCTURES Structures + * @{ */ + +/** + * @brief BLE GATTS init options + */ +typedef struct +{ + uint8_t service_changed:1; /**< Include the Service Changed characteristic in the local attributes. */ +} ble_gatts_enable_params_t; + +/**@brief Attribute metadata. */ +typedef struct +{ + ble_gap_conn_sec_mode_t read_perm; /**< Read permissions. */ + ble_gap_conn_sec_mode_t write_perm; /**< Write permissions. */ + uint8_t vlen :1; /**< Variable length attribute. */ + uint8_t vloc :2; /**< Value location, see @ref BLE_GATTS_VLOCS.*/ + uint8_t rd_auth :1; /**< Read Authorization and value will be requested from the application on every read operation. */ + uint8_t wr_auth :1; /**< Write Authorization will be requested from the application on every Write Request operation (but not Write Command). */ +} ble_gatts_attr_md_t; + + +/**@brief GATT Attribute. */ +typedef struct +{ + ble_uuid_t* p_uuid; /**< Pointer to the attribute UUID. */ + ble_gatts_attr_md_t* p_attr_md; /**< Pointer to the attribute metadata structure. */ + uint16_t init_len; /**< Initial attribute value length in bytes. */ + uint16_t init_offs; /**< Initial attribute value offset in bytes. If different from zero, the first init_offs bytes of the attribute value will be left uninitialized. */ + uint16_t max_len; /**< Maximum attribute value length in bytes, see @ref BLE_GATTS_ATTR_LENS_MAX for maximum values. */ + uint8_t* p_value; /**< Pointer to the attribute data. Please note that if the @ref BLE_GATTS_VLOC_USER value location is selected in the attribute metadata, this will have to point to a buffer + that remains valid through the lifetime of the attribute. This excludes usage of automatic variables that may go out of scope or any other temporary location. + The stack may access that memory directly without the application's knowledge. */ +} ble_gatts_attr_t; + + +/**@brief GATT Attribute Context. */ +typedef struct +{ + ble_uuid_t srvc_uuid; /**< Service UUID. */ + ble_uuid_t char_uuid; /**< Characteristic UUID if applicable (BLE_UUID_TYPE_UNKNOWN if N/A). */ + ble_uuid_t desc_uuid; /**< Descriptor UUID if applicable (BLE_UUID_TYPE_UNKNOWN if N/A). */ + uint16_t srvc_handle; /**< Service Handle. */ + uint16_t value_handle; /**< Characteristic Handle if applicable (BLE_GATT_HANDLE_INVALID if N/A). */ + uint8_t type; /**< Attribute Type, see @ref BLE_GATTS_ATTR_TYPES. */ +} ble_gatts_attr_context_t; + + +/**@brief GATT Characteristic Presentation Format. */ +typedef struct +{ + uint8_t format; /**< Format of the value, see @ref BLE_GATT_CPF_FORMATS. */ + int8_t exponent; /**< Exponent for integer data types. */ + uint16_t unit; /**< UUID from Bluetooth Assigned Numbers. */ + uint8_t name_space; /**< Namespace from Bluetooth Assigned Numbers, see @ref BLE_GATT_CPF_NAMESPACES. */ + uint16_t desc; /**< Namespace description from Bluetooth Assigned Numbers, see @ref BLE_GATT_CPF_NAMESPACES. */ +} ble_gatts_char_pf_t; + + +/**@brief GATT Characteristic metadata. */ +typedef struct +{ + ble_gatt_char_props_t char_props; /**< Characteristic Properties. */ + ble_gatt_char_ext_props_t char_ext_props; /**< Characteristic Extended Properties. */ + uint8_t* p_char_user_desc; /**< Pointer to a UTF-8, NULL if the descriptor is not required. */ + uint16_t char_user_desc_max_size; /**< The maximum size in bytes of the user description descriptor. */ + uint16_t char_user_desc_size; /**< The size of the user description, must be smaller or equal to char_user_desc_max_size. */ + ble_gatts_char_pf_t* p_char_pf; /**< Pointer to a presentation format structure or NULL if the descriptor is not required. */ + ble_gatts_attr_md_t* p_user_desc_md; /**< Attribute metadata for the User Description descriptor, or NULL for default values. */ + ble_gatts_attr_md_t* p_cccd_md; /**< Attribute metadata for the Client Characteristic Configuration Descriptor, or NULL for default values. */ + ble_gatts_attr_md_t* p_sccd_md; /**< Attribute metadata for the Server Characteristic Configuration Descriptor, or NULL for default values. */ +} ble_gatts_char_md_t; + + +/**@brief GATT Characteristic Definition Handles. */ +typedef struct +{ + uint16_t value_handle; /**< Handle to the characteristic value. */ + uint16_t user_desc_handle; /**< Handle to the User Description descriptor, or BLE_GATT_HANDLE_INVALID if not present. */ + uint16_t cccd_handle; /**< Handle to the Client Characteristic Configuration Descriptor, or BLE_GATT_HANDLE_INVALID if not present. */ + uint16_t sccd_handle; /**< Handle to the Server Characteristic Configuration Descriptor, or BLE_GATT_HANDLE_INVALID if not present. */ +} ble_gatts_char_handles_t; + + +/**@brief GATT HVx parameters. */ +typedef struct +{ + uint16_t handle; /**< Characteristic Value Handle. */ + uint8_t type; /**< Indication or Notification, see @ref BLE_GATT_HVX_TYPES. */ + uint16_t offset; /**< Offset within the attribute value. */ + uint16_t* p_len; /**< Length in bytes to be written, length in bytes written after successful return. */ + uint8_t* p_data; /**< Actual data content, use NULL to use the current attribute value. */ +} ble_gatts_hvx_params_t; + +/**@brief GATT Read Authorization parameters. */ +typedef struct +{ + uint16_t gatt_status; /**< GATT status code for the operation, see @ref BLE_GATT_STATUS_CODES. */ + uint8_t update : 1; /**< If set, data supplied in p_data will be used in the ATT response. */ + uint16_t offset; /**< Offset of the attribute value being updated. */ + uint16_t len; /**< Length in bytes of the value in p_data pointer, see @ref BLE_GATTS_ATTR_LENS_MAX. */ + uint8_t* p_data; /**< Pointer to new value used to update the attribute value. */ +} ble_gatts_read_authorize_params_t; + +/**@brief GATT Write Authorisation parameters. */ +typedef struct +{ + uint16_t gatt_status; /**< GATT status code for the operation, see @ref BLE_GATT_STATUS_CODES. */ +} ble_gatts_write_authorize_params_t; + +/**@brief GATT Read or Write Authorize Reply parameters. */ +typedef struct +{ + uint8_t type; /**< Type of authorize operation, see @ref BLE_GATTS_AUTHORIZE_TYPES. */ + union { + ble_gatts_read_authorize_params_t read; /**< Read authorization parameters. */ + ble_gatts_write_authorize_params_t write; /**< Write authorization parameters. */ + } params; +} ble_gatts_rw_authorize_reply_params_t; + + +/** + * @brief GATT Server Event IDs. + */ +enum BLE_GATTS_EVTS +{ + BLE_GATTS_EVT_WRITE = BLE_GATTS_EVT_BASE, /**< Write operation performed. */ + BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST, /**< Read/Write Authorization request. */ + BLE_GATTS_EVT_SYS_ATTR_MISSING, /**< A persistent system attribute access is pending, awaiting a sd_ble_gatts_sys_attr_set(). */ + BLE_GATTS_EVT_HVC, /**< Handle Value Confirmation. */ + BLE_GATTS_EVT_SC_CONFIRM, /**< Service Changed Confirmation. */ + BLE_GATTS_EVT_TIMEOUT /**< Timeout. */ +}; + + +/**@brief Event structure for BLE_GATTS_EVT_WRITE. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ + uint8_t op; /**< Type of write operation, see @ref BLE_GATTS_OPS. */ + ble_gatts_attr_context_t context; /**< Attribute Context. */ + uint16_t offset; /**< Offset for the write operation. */ + uint16_t len; /**< Length of the incoming data. */ + uint8_t data[1]; /**< Incoming data, variable length. */ +} ble_gatts_evt_write_t; + +/**@brief Event structure for authorize read request. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ + ble_gatts_attr_context_t context; /**< Attribute Context. */ + uint16_t offset; /**< Offset for the read operation. */ +} ble_gatts_evt_read_t; + +/**@brief Event structure for BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST. */ +typedef struct +{ + uint8_t type; /**< Type of authorize operation, see @ref BLE_GATTS_AUTHORIZE_TYPES. */ + union { + ble_gatts_evt_read_t read; /**< Attribute Read Parameters. */ + ble_gatts_evt_write_t write; /**< Attribute Write Parameters. */ + } request; +} ble_gatts_evt_rw_authorize_request_t; + +/**@brief Event structure for BLE_GATTS_EVT_SYS_ATTR_MISSING. */ +typedef struct +{ + uint8_t hint; +} ble_gatts_evt_sys_attr_missing_t; + + +/**@brief Event structure for BLE_GATTS_EVT_HVC. */ +typedef struct +{ + uint16_t handle; /**< Attribute Handle. */ +} ble_gatts_evt_hvc_t; + +/**@brief Event structure for BLE_GATTS_EVT_TIMEOUT. */ +typedef struct +{ + uint8_t src; /**< Timeout source, see @ref BLE_GATT_TIMEOUT_SOURCES. */ +} ble_gatts_evt_timeout_t; + + +/**@brief GATT Server event callback event structure. */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle on which event occurred. */ + union + { + ble_gatts_evt_write_t write; /**< Write Event Parameters. */ + ble_gatts_evt_rw_authorize_request_t authorize_request; /**< Read or Write Authorize Request Parameters. */ + ble_gatts_evt_sys_attr_missing_t sys_attr_missing; /**< System attributes missing. */ + ble_gatts_evt_hvc_t hvc; /**< Handle Value Confirmation Event Parameters. */ + ble_gatts_evt_timeout_t timeout; /**< Timeout Event. */ + } params; +} ble_gatts_evt_t; + +/** @} */ + +/** @addtogroup BLE_GATTS_FUNCTIONS Functions + * @{ */ + +/**@brief Add a service declaration to the local server ATT table. + * + * @param[in] type Toggles between primary and secondary services, see @ref BLE_GATTS_SRVC_TYPES. + * @param[in] p_uuid Pointer to service UUID. + * @param[out] p_handle Pointer to a 16-bit word where the assigned handle will be stored. + * + * @note Secondary Services are only relevant in the context of the entity that references them, it is therefore forbidden to + * add a secondary service declaration that is not referenced by another service later in the ATT table. + * + * @return @ref NRF_SUCCESS Successfully added a service declaration. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, Vendor Specific UUIDs need to be present in the table. + * @return @ref NRF_ERROR_FORBIDDEN Forbidden value supplied, certain UUIDs are reserved for the stack. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + */ +SVCALL(SD_BLE_GATTS_SERVICE_ADD, uint32_t, sd_ble_gatts_service_add(uint8_t type, ble_uuid_t const*const p_uuid, uint16_t *const p_handle)); + + +/**@brief Add an include declaration to the local server ATT table. + * + * @note It is currently only possible to add an include declaration to the last added service (i.e. only sequential addition is supported at this time). + * + * @note The included service must already be present in the ATT table prior to this call. + * + * @param[in] service_handle Handle of the service where the included service is to be placed, if BLE_GATT_HANDLE_INVALID is used, it will be placed sequentially. + * @param[in] inc_srvc_handle Handle of the included service. + * @param[out] p_include_handle Pointer to a 16-bit word where the assigned handle will be stored. + * + * @return @ref NRF_SUCCESS Successfully added an include declaration. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, handle values need to match previously added services. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation. + * @return @ref NRF_ERROR_FORBIDDEN Forbidden value supplied, self inclusions are not allowed. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + * @return @ref NRF_ERROR_NOT_FOUND Attribute not found. + */ +SVCALL(SD_BLE_GATTS_INCLUDE_ADD, uint32_t, sd_ble_gatts_include_add(uint16_t service_handle, uint16_t inc_srvc_handle, uint16_t *const p_include_handle)); + + +/**@brief Add a characteristic declaration, a characteristic value declaration and optional characteristic descriptor declarations to the local server ATT table. + * + * @note It is currently only possible to add a characteristic to the last added service (i.e. only sequential addition is supported at this time). + * + * @note Several restrictions apply to the parameters, such as matching permissions between the user description descriptor and the writeable auxiliaries bits, + * readable (no security) and writeable (selectable) CCCDs and SCCDs and valid presentation format values. + * + * @note If no metadata is provided for the optional descriptors, their permissions will be derived from the characteristic permissions. + * + * @param[in] service_handle Handle of the service where the characteristic is to be placed, if BLE_GATT_HANDLE_INVALID is used, it will be placed sequentially. + * @param[in] p_char_md Characteristic metadata. + * @param[in] p_attr_char_value Pointer to the attribute structure corresponding to the characteristic value. + * @param[out] p_handles Pointer to the structure where the assigned handles will be stored. + * + * @return @ref NRF_SUCCESS Successfully added a characteristic. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, service handle, Vendor Specific UUIDs, lengths, and permissions need to adhere to the constraints. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation, a service context is required. + * @return @ref NRF_ERROR_FORBIDDEN Forbidden value supplied, certain UUIDs are reserved for the stack. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, attribute lengths are restricted by @ref BLE_GATTS_ATTR_LENS_MAX. + */ +SVCALL(SD_BLE_GATTS_CHARACTERISTIC_ADD, uint32_t, sd_ble_gatts_characteristic_add(uint16_t service_handle, ble_gatts_char_md_t const*const p_char_md, ble_gatts_attr_t const*const p_attr_char_value, ble_gatts_char_handles_t *const p_handles)); + + +/**@brief Add a descriptor to the local server ATT table. + * + * @note It is currently only possible to add a descriptor to the last added characteristic (i.e. only sequential addition is supported at this time). + * + * @param[in] char_handle Handle of the characteristic where the descriptor is to be placed, if BLE_GATT_HANDLE_INVALID is used, it will be placed sequentially. + * @param[in] p_attr Pointer to the attribute structure. + * @param[out] p_handle Pointer to a 16-bit word where the assigned handle will be stored. + * + * @return @ref NRF_SUCCESS Successfully added a descriptor. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, characteristic handle, Vendor Specific UUIDs, lengths, and permissions need to adhere to the constraints. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation, a characteristic context is required. + * @return @ref NRF_ERROR_FORBIDDEN Forbidden value supplied, certain UUIDs are reserved for the stack. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, attribute lengths are restricted by @ref BLE_GATTS_ATTR_LENS_MAX. + */ +SVCALL(SD_BLE_GATTS_DESCRIPTOR_ADD, uint32_t, sd_ble_gatts_descriptor_add(uint16_t char_handle, ble_gatts_attr_t const * const p_attr, uint16_t* const p_handle)); + +/**@brief Set the value of a given attribute. + * + * @param[in] handle Attribute handle. + * @param[in] offset Offset in bytes to write from. + * @param[in,out] p_len Length in bytes to be written, length in bytes written after successful return. + * @param[in] p_value Pointer to a buffer (at least len bytes long) containing the desired attribute value. If value is stored in user memory, only the attribute length is updated when p_value == NULL. + * + * @return @ref NRF_SUCCESS Successfully set the value of the attribute. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_NOT_FOUND Attribute not found. + * @return @ref NRF_ERROR_FORBIDDEN Forbidden handle supplied, certain attributes are not modifiable by the application. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, attribute lengths are restricted by @ref BLE_GATTS_ATTR_LENS_MAX. + */ +SVCALL(SD_BLE_GATTS_VALUE_SET, uint32_t, sd_ble_gatts_value_set(uint16_t handle, uint16_t offset, uint16_t* const p_len, uint8_t const * const p_value)); + +/**@brief Get the value of a given attribute. + * + * @param[in] handle Attribute handle. + * @param[in] offset Offset in bytes to read from. + * @param[in,out] p_len Length in bytes to be read, total length of attribute value (in bytes, starting from offset) after successful return. + * @param[in,out] p_data Pointer to a buffer (at least len bytes long) where to store the attribute value. Set to NULL to obtain the complete length of attribute value. + * + * @note If the attribute value is longer than the size of the supplied buffer, + * p_len will return the total attribute value length (excluding offset), + * and not the number of bytes actually returned in p_data. + * The application may use this information to allocate a suitable buffer size. + * + * @return @ref NRF_SUCCESS Successfully retrieved the value of the attribute. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_NOT_FOUND Attribute not found. + */ +SVCALL(SD_BLE_GATTS_VALUE_GET, uint32_t, sd_ble_gatts_value_get(uint16_t handle, uint16_t offset, uint16_t *const p_len, uint8_t* const p_data)); + +/**@brief Notify or Indicate an attribute value. + * + * @details This function checks for the relevant Client Characteristic Configuration descriptor value to verify that the relevant operation + * (notification or indication) has been enabled by the client. It is also able to update the attribute value before issuing the PDU, so that + * the application can atomically perform a value update and a server initiated transaction with a single API call. + * If the application chooses to indicate an attribute value, a @ref BLE_GATTS_EVT_HVC will be sent up as soon as the confirmation arrives from + * the peer. + * + * @note The local attribute value may be updated even if an outgoing packet is not sent to the peer due to an error during execution. + * When receiveing the error codes @ref NRF_ERROR_INVALID_STATE, @ref NRF_ERROR_BUSY, @ref BLE_ERROR_GATTS_SYS_ATTR_MISSING and + * @ref BLE_ERROR_NO_TX_BUFFERS the ATT table has been updated. + * The caller can check whether the value has been updated by looking at the contents of *(p_hvx_params->p_len). + * + * @note It is important to note that a notification will <b>consume an application buffer</b>, and will therefore + * generate a @ref BLE_EVT_TX_COMPLETE event when the packet has been transmitted. An indication on the other hand will use the + * standard server internal buffer and thus will only generate a @ref BLE_GATTS_EVT_HVC event as soon as the confirmation + * has been received from the peer. Please see the documentation of @ref sd_ble_tx_buffer_count_get for more details. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_hvx_params Pointer to an HVx parameters structure. If the p_data member contains a non-NULL pointer the attribute value will be updated with + * the contents pointed by it before sending the notification or indication. + * + * @return @ref NRF_SUCCESS Successfully queued a notification or indication for transmission, and optionally updated the attribute value. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref BLE_ERROR_INVALID_ATTR_HANDLE Invalid attribute handle(s) supplied. Only attributes added directly by the application are available to notify and indicate. + * @return @ref BLE_ERROR_GATTS_INVALID_ATTR_TYPE Invalid attribute type(s) supplied, only characteristic values may be notified and indicated. + * @return @ref NRF_ERROR_NOT_FOUND Attribute not found. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation, notifications or indications must be enabled in the CCCD. + * @return @ref NRF_ERROR_BUSY Procedure already in progress. + * @return @ref BLE_ERROR_GATTS_SYS_ATTR_MISSING System attributes missing, use @ref sd_ble_gatts_sys_attr_set to set them to a known value. + * @return @ref BLE_ERROR_NO_TX_BUFFERS There are no available buffers to send the data, applies only to notifications. + */ +SVCALL(SD_BLE_GATTS_HVX, uint32_t, sd_ble_gatts_hvx(uint16_t conn_handle, ble_gatts_hvx_params_t const*const p_hvx_params)); + +/**@brief Indicate the Service Changed attribute value. + * + * @details This call will send a Handle Value Indication to one or more peers connected to inform them that the attribute + * table layout has changed. As soon as the peer has confirmed the indication, a @ref BLE_GATTS_EVT_SC_CONFIRM event will + * be issued. + * + * @note Some of the restrictions and limitations that apply to @ref sd_ble_gatts_hvx also apply here. + * + * @param[in] conn_handle Connection handle. + * @param[in] start_handle Start of affected attribute handle range. + * @param[in] end_handle End of affected attribute handle range. + * + * @return @ref NRF_SUCCESS Successfully queued the Service Changed indication for transmission. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref BLE_ERROR_INVALID_ATTR_HANDLE Invalid attribute handle(s) supplied, handles must be in the range populated by the application. + * @return @ref NRF_ERROR_INVALID_STATE Invalid state to perform operation, notifications or indications must be enabled in the CCCD. + * @return @ref NRF_ERROR_BUSY Procedure already in progress. + * @return @ref BLE_ERROR_GATTS_SYS_ATTR_MISSING System attributes missing, use @ref sd_ble_gatts_sys_attr_set to set them to a known value. + */ +SVCALL(SD_BLE_GATTS_SERVICE_CHANGED, uint32_t, sd_ble_gatts_service_changed(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle)); + +/**@brief Respond to a Read/Write authorization request. + * + * @note This call should only be used as a response to a @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST event issued to the application. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_rw_authorize_reply_params Pointer to a structure with the attribute provided by the application. + * + * @return @ref NRF_SUCCESS Successfully queued a response to the peer, and in the case of a write operation, ATT table updated. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_STATE No authorization request pending. + * @return @ref NRF_ERROR_INVALID_PARAM Authorization op invalid, + * or for Read Authorization reply: requested handles not replied with, + * or for Write Authorization reply: handle supplied does not match requested handle. + */ +SVCALL(SD_BLE_GATTS_RW_AUTHORIZE_REPLY, uint32_t, sd_ble_gatts_rw_authorize_reply(uint16_t conn_handle, ble_gatts_rw_authorize_reply_params_t const*const p_rw_authorize_reply_params)); + + +/**@brief Update persistent system attribute information. + * + * @details Supply to the stack information about persistent system attributes. + * This call is legal in the connected state only, and is usually + * made immediately after a connection is established and the bond identified. + * usually as a response to a BLE_GATTS_EVT_SYS_ATTR_MISSING. + * + * p_sysattrs may point directly to the application's stored copy of the struct. + * If the pointer is NULL, the system attribute info is initialized, assuming that + * the application does not have any previously saved data for this bond. + * + * @note The state of persistent system attributes is reset upon connection and then remembered for its duration. + * + * @note If this call returns with an error code different from @ref NRF_SUCCESS, the storage of persistent system attributes may have been completed only partially. + * This means that the state of the attribute table is undefined, and the application should either provide a new set of attributes using this same call or + * reset the SoftDevice to return to a known state. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_sys_attr_data Pointer to a saved copy of system attributes supplied to the stack, or NULL. + * @param[in] len Size of data pointed by p_sys_attr_data, in octets. + * + * @return @ref NRF_SUCCESS Successfully set the system attribute information. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_DATA Invalid data supplied, the data should be exactly the same as retrieved with @ref sd_ble_gatts_sys_attr_get. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + */ +SVCALL(SD_BLE_GATTS_SYS_ATTR_SET, uint32_t, sd_ble_gatts_sys_attr_set(uint16_t conn_handle, uint8_t const*const p_sys_attr_data, uint16_t len)); + + +/**@brief Retrieve persistent system attribute information from the stack. + * + * @details This call is used to retrieve information about values to be stored perisistently by the application + * after a connection has been terminated. When a new connection is made to the same bond, the values + * should be restored using @ref sd_ble_gatts_sys_attr_set. + * The data should be read before any new advertising is started, or any new connection established. The connection handle for + * the previous now defunct connection will remain valid until a new one is created to allow this API call to refer to it. + * + * @param[in] conn_handle Connection handle of the recently terminated connection. + * @param[in] p_sys_attr_data Pointer to a buffer where updated information about system attributes will be filled in. NULL can be provided to + * obtain the length of the data + * @param[in,out] p_len Size of application buffer if p_sys_attr_data is not NULL. Unconditially updated to actual length of system attribute data. + * + * @return @ref NRF_SUCCESS Successfully retrieved the system attribute information. + * @return @ref BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_DATA_SIZE The system attribute information did not fit into the provided buffer. + */ +SVCALL(SD_BLE_GATTS_SYS_ATTR_GET, uint32_t, sd_ble_gatts_sys_attr_get(uint16_t conn_handle, uint8_t * const p_sys_attr_data, uint16_t* const p_len)); + +/** @} */ + +#endif // BLE_GATTS_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_hci.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,123 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_COMMON + @{ +*/ + + +#ifndef BLE_HCI_H__ +#define BLE_HCI_H__ + +/** @defgroup BLE_HCI_STATUS_CODES Bluetooth status codes + * @{ */ + +#define BLE_HCI_STATUS_CODE_SUCCESS 0x00 +#define BLE_HCI_STATUS_CODE_UNKNOWN_BTLE_COMMAND 0x01 +#define BLE_HCI_STATUS_CODE_UNKNOWN_CONNECTION_IDENTIFIER 0x02 +/*0x03 Hardware Failure +0x04 Page Timeout +*/ +#define BLE_HCI_AUTHENTICATION_FAILURE 0x05 +#define BLE_HCI_STATUS_CODE_PIN_OR_KEY_MISSING 0x06 +#define BLE_HCI_MEMORY_CAPACITY_EXCEEDED 0x07 +#define BLE_HCI_CONNECTION_TIMEOUT 0x08 +/*0x09 Connection Limit Exceeded +0x0A Synchronous Connection Limit To A Device Exceeded +0x0B ACL Connection Already Exists*/ +#define BLE_HCI_STATUS_CODE_COMMAND_DISALLOWED 0x0C +/*0x0D Connection Rejected due to Limited Resources +0x0E Connection Rejected Due To Security Reasons +0x0F Connection Rejected due to Unacceptable BD_ADDR +0x10 Connection Accept Timeout Exceeded +0x11 Unsupported Feature or Parameter Value*/ +#define BLE_HCI_STATUS_CODE_INVALID_BTLE_COMMAND_PARAMETERS 0x12 +#define BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION 0x13 +#define BLE_HCI_REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES 0x14 +#define BLE_HCI_REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF 0x15 +#define BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION 0x16 +/* +0x17 Repeated Attempts +0x18 Pairing Not Allowed +0x19 Unknown LMP PDU +*/ +#define BLE_HCI_UNSUPPORTED_REMOTE_FEATURE 0x1A +/* +0x1B SCO Offset Rejected +0x1C SCO Interval Rejected +0x1D SCO Air Mode Rejected*/ +#define BLE_HCI_STATUS_CODE_INVALID_LMP_PARAMETERS 0x1E +#define BLE_HCI_STATUS_CODE_UNSPECIFIED_ERROR 0x1F +/*0x20 Unsupported LMP Parameter Value +0x21 Role Change Not Allowed +*/ +#define BLE_HCI_STATUS_CODE_LMP_RESPONSE_TIMEOUT 0x22 +/*0x23 LMP Error Transaction Collision*/ +#define BLE_HCI_STATUS_CODE_LMP_PDU_NOT_ALLOWED 0x24 +/*0x25 Encryption Mode Not Acceptable +0x26 Link Key Can Not be Changed +0x27 Requested QoS Not Supported +*/ +#define BLE_HCI_INSTANT_PASSED 0x28 +#define BLE_HCI_PAIRING_WITH_UNIT_KEY_UNSUPPORTED 0x29 +#define BLE_HCI_DIFFERENT_TRANSACTION_COLLISION 0x2A +/* +0x2B Reserved +0x2C QoS Unacceptable Parameter +0x2D QoS Rejected +0x2E Channel Classification Not Supported +0x2F Insufficient Security +0x30 Parameter Out Of Mandatory Range +0x31 Reserved +0x32 Role Switch Pending +0x33 Reserved +0x34 Reserved Slot Violation +0x35 Role Switch Failed +0x36 Extended Inquiry Response Too Large +0x37 Secure Simple Pairing Not Supported By Host. +0x38 Host Busy - Pairing +0x39 Connection Rejected due to No Suitable Channel Found*/ +#define BLE_HCI_CONTROLLER_BUSY 0x3A +#define BLE_HCI_CONN_INTERVAL_UNACCEPTABLE 0x3B +#define BLE_HCI_DIRECTED_ADVERTISER_TIMEOUT 0x3C +#define BLE_HCI_CONN_TERMINATED_DUE_TO_MIC_FAILURE 0x3D +#define BLE_HCI_CONN_FAILED_TO_BE_ESTABLISHED 0x3E + +/** @} */ + + +#endif // BLE_HCI_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_hrs_main.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,70 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXXX + * @{ + * @ingroup YYYY + * + * @brief ZZZZZ. + */ + +#ifndef BLE_HRS_MAIN_H__ +#define BLE_HRS_MAIN_H__ + +#include <stdint.h> + +/**@brief Function for initializing the module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t ble_hrs_main_init(void); + +/**@brief Function for starting the execution of the module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t ble_hrs_main_start(void); + +/**@brief Function for starting the processing of received connectivity device BLE event. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t ble_hrs_evt_process(void); + +#endif // BLE_HRS_MAIN_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_l2cap.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,162 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_L2CAP Logical Link Control and Adaptation Protocol (L2CAP) + @{ + @brief Definitions and prototypes for the L2CAP interface. + */ + +#ifndef BLE_L2CAP_H__ +#define BLE_L2CAP_H__ + +#include "ble_types.h" +#include "ble_ranges.h" +#include "ble_err.h" +#include "nrf_svc.h" + +/**@brief L2CAP API SVC numbers. */ +enum BLE_L2CAP_SVCS +{ + SD_BLE_L2CAP_CID_REGISTER = BLE_L2CAP_SVC_BASE, /**< Register a CID. */ + SD_BLE_L2CAP_CID_UNREGISTER, /**< Unregister a CID. */ + SD_BLE_L2CAP_TX /**< Transmit a packet. */ +}; + +/**@addtogroup BLE_L2CAP_DEFINES Defines + * @{ */ + +/**@defgroup BLE_ERRORS_L2CAP SVC return values specific to L2CAP + * @{ */ +#define BLE_ERROR_L2CAP_CID_IN_USE (NRF_L2CAP_ERR_BASE + 0x000) /**< CID already in use. */ +/** @} */ + +/**@brief Default L2CAP MTU. */ +#define BLE_L2CAP_MTU_DEF (23) + +/**@brief Invalid Channel Identifier. */ +#define BLE_L2CAP_CID_INVALID (0x0000) + +/**@brief Dynamic Channel Identifier base. */ +#define BLE_L2CAP_CID_DYN_BASE (0x0040) + +/**@brief Maximum amount of dynamic CIDs. */ +#define BLE_L2CAP_CID_DYN_MAX (8) + +/** @} */ + +/**@brief Packet header format for L2CAP transmission. */ +typedef struct +{ + uint16_t len; /**< Length of valid info in data member. */ + uint16_t cid; /**< Channel ID on which packet is transmitted. */ +} ble_l2cap_header_t; + +/**@brief L2CAP Event IDs. */ +enum BLE_L2CAP_EVTS +{ + BLE_L2CAP_EVT_RX = BLE_L2CAP_EVT_BASE /**< L2CAP packet received. */ +}; + + +/**@brief L2CAP Received packet event report. */ +typedef struct +{ + ble_l2cap_header_t header; /** L2CAP packet header. */ + uint8_t data[1]; /**< Packet data, variable length. */ +} ble_l2cap_evt_rx_t; + + +/**@brief L2CAP event callback event structure. */ +typedef struct +{ + uint16_t conn_handle; /**< Connection Handle on which event occured. */ + union + { + ble_l2cap_evt_rx_t rx; /**< RX Event parameters. */ + } params; +} ble_l2cap_evt_t; + + +/**@brief Register a CID with L2CAP. + * + * @details This registers a higher protocol layer with the L2CAP multiplexer, and is requried prior to all operations on the CID. + * + * @param[in] cid L2CAP CID. + * + * @return @ref NRF_SUCCESS Successfully registered a CID with the L2CAP layer. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, CID must be above @ref BLE_L2CAP_CID_DYN_BASE. + * @return @ref BLE_ERROR_L2CAP_CID_IN_USE L2CAP CID already in use. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + */ +SVCALL(SD_BLE_L2CAP_CID_REGISTER, uint32_t, sd_ble_l2cap_cid_register(uint16_t cid)); + +/**@brief Unregister a CID with L2CAP. + * + * @details This unregisters a previously registerd higher protocol layer with the L2CAP multiplexer. + * + * @param[in] cid L2CAP CID. + * + * @return @ref NRF_SUCCESS Successfully unregistered the CID. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied. + * @return @ref NRF_ERROR_NOT_FOUND CID not previously registered. + */ +SVCALL(SD_BLE_L2CAP_CID_UNREGISTER, uint32_t, sd_ble_l2cap_cid_unregister(uint16_t cid)); + +/**@brief Transmit an L2CAP packet. + * + * @note It is important to note that a call to this function will <b>consume an application buffer</b>, and will therefore + * generate a @ref BLE_EVT_TX_COMPLETE event when the packet has been transmitted. + * Please see the documentation of @ref sd_ble_tx_buffer_count_get for more details. + * + * @param[in] conn_handle Connection Handle. + * @param[in] p_header Pointer to a packet header containing length and CID. + * @param[in] p_data Pointer to the data to be transmitted. + * + * @return @ref NRF_SUCCESS Successfully queued an L2CAP packet for transmission. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, CIDs must be registered beforehand with @ref sd_ble_l2cap_cid_register. + * @return @ref NRF_ERROR_NOT_FOUND CID not found. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + * @return @ref BLE_ERROR_NO_TX_BUFFERS Not enough application buffers available. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, see @ref BLE_L2CAP_MTU_DEF. + */ +SVCALL(SD_BLE_L2CAP_TX, uint32_t, sd_ble_l2cap_tx(uint16_t conn_handle, ble_l2cap_header_t const * const p_header, uint8_t const * const p_data)); + + +#endif // BLE_L2CAP_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_nrf_soc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,58 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXX + * @{ + * @ingroup YYY + * + * @brief ZZZ. + */ + +#ifndef BLE_NRF_SOC_H__ +#define BLE_NRF_SOC_H__ + +#include <stdint.h> + +/**@brief Function for putting the chip in System OFF mode. + * + * @retval NRF_SUCCESS + */ +uint32_t sd_power_system_off(void); + +#endif // BLE_NRF_SOC_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_ranges.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,116 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_COMMON + @{ + @defgroup ble_ranges Module specific SVC and event number subranges + @{ + + @brief Definition of SVC and event number subranges for each API module. + + @note + SVCs and event numbers are split into subranges for each API module. + Each module receives its entire allocated range of SVC calls, whether implemented or not, + but return BLE_ERROR_NOT_SUPPORTED for unimplemented or undefined calls in its range. + + Note that the symbols BLE_<module>_SVC_LAST is the end of the allocated SVC range, + rather than the last SVC function call actually defined and implemented. + + Specific SVC and event values are defined in each module's ble_<module>.h file, + which defines names of each individual SVC code based on the range start value. +*/ + +#ifndef BLE_RANGES_H__ +#define BLE_RANGES_H__ + +#define BLE_SVC_BASE 0x60 +#define BLE_SVC_LAST 0x6B /* Total: 12. */ + +#define BLE_RESERVED_SVC_BASE 0x6C +#define BLE_RESERVED_SVC_LAST 0x6F /* Total: 4. */ + +#define BLE_GAP_SVC_BASE 0x70 +#define BLE_GAP_SVC_LAST 0x8F /* Total: 32. */ + +#define BLE_GATTC_SVC_BASE 0x90 +#define BLE_GATTC_SVC_LAST 0x9F /* Total: 16. */ + +#define BLE_GATTS_SVC_BASE 0xA0 +#define BLE_GATTS_SVC_LAST 0xAF /* Total: 16. */ + +#define BLE_L2CAP_SVC_BASE 0xB0 +#define BLE_L2CAP_SVC_LAST 0xBF /* Total: 16. */ + + +#define BLE_EVT_INVALID 0x00 + +#define BLE_EVT_BASE 0x01 +#define BLE_EVT_LAST 0x0F /* Total: 15. */ + +#define BLE_GAP_EVT_BASE 0x10 +#define BLE_GAP_EVT_LAST 0x2F /* Total: 32. */ + +#define BLE_GATTC_EVT_BASE 0x30 +#define BLE_GATTC_EVT_LAST 0x4F /* Total: 32. */ + +#define BLE_GATTS_EVT_BASE 0x50 +#define BLE_GATTS_EVT_LAST 0x6F /* Total: 32. */ + +#define BLE_L2CAP_EVT_BASE 0x70 +#define BLE_L2CAP_EVT_LAST 0x8F /* Total: 32. */ + +#define BLE_OPT_INVALID 0x00 /**< Invalid BLE Option. */ + +#define BLE_OPT_BASE 0x01 /**< Common BLE Option base. */ +#define BLE_OPT_LAST 0x1F /**< Total: 31. */ + +#define BLE_GAP_OPT_BASE 0x20 /**< GAP BLE Option base. */ +#define BLE_GAP_OPT_LAST 0x3F /**< Total: 32. */ + +#define BLE_GATTC_OPT_BASE 0x40 /**< GATTC BLE Option base. */ +#define BLE_GATTC_OPT_LAST 0x5F /**< Total: 32. */ + +#define BLE_GATTS_OPT_BASE 0x60 /**< GATTS BLE Option base. */ +#define BLE_GATTS_OPT_LAST 0x7F /**< Total: 32. */ + +#define BLE_L2CAP_OPT_BASE 0x80 /**< L2CAP BLE Option base. */ +#define BLE_L2CAP_OPT_LAST 0x9F /**< Total: 32. */ + +#endif /* BLE_RANGES_H__ */ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_sensorsim.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,88 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_lib_sensorsim Sensor Data Simulator + * @{ + * @ingroup ble_sdk_lib + * @brief Functions for simulating sensor data. + * + * @details Currently only a triangular waveform simulator is implemented. + */ + +#ifndef BLE_SENSORSIM_H__ +#define BLE_SENSORSIM_H__ + +#include <stdint.h> +#include <stdbool.h> + +/**@brief Triangular waveform sensor simulator configuration. */ +typedef struct +{ + uint32_t min; /**< Minimum simulated value. */ + uint32_t max; /**< Maximum simulated value. */ + uint32_t incr; /**< Increment between each measurement. */ + bool start_at_max; /**< TRUE is measurement is to start at the maximum value, FALSE if it is to start at the minimum. */ +} ble_sensorsim_cfg_t; + +/**@brief Triangular waveform sensor simulator state. */ +typedef struct +{ + uint32_t current_val; /**< Current sensor value. */ + bool is_increasing; /**< TRUE if the simulator is in increasing state, FALSE otherwise. */ +} ble_sensorsim_state_t; + +/**@brief Function for initializing a triangular waveform sensor simulator. + * + * @param[out] p_state Current state of simulator. + * @param[in] p_cfg Simulator configuration. + */ +void ble_sensorsim_init(ble_sensorsim_state_t * p_state, + const ble_sensorsim_cfg_t * p_cfg); + +/**@brief Function for generating a simulated sensor measurement using a triangular waveform generator. + * + * @param[in,out] p_state Current state of simulator. + * @param[in] p_cfg Simulator configuration. + * + * @return Simulator output. + */ +uint32_t ble_sensorsim_measure(ble_sensorsim_state_t * p_state, + const ble_sensorsim_cfg_t * p_cfg); + +#endif // BLE_SENSORSIM_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_types.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,196 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_COMMON + @{ + @defgroup ble_types Common types and macro definitions + @{ + + @brief Common types and macro definitions for the S110 SoftDevice. + */ + +#ifndef BLE_TYPES_H__ +#define BLE_TYPES_H__ + +#include <stdint.h> + +/** @addtogroup BLE_COMMON_DEFINES Defines + * @{ */ + +/** @defgroup BLE_CONN_HANDLES BLE Connection Handles + * @{ */ +#define BLE_CONN_HANDLE_INVALID 0xFFFF /**< Invalid Connection Handle. */ +#define BLE_CONN_HANDLE_ALL 0xFFFE /**< Applies to all Connection Handles. */ +/** @} */ + + +/** @defgroup BLE_UUID_VALUES Assigned Values for BLE UUIDs + * @{ */ +/* Generic UUIDs, applicable to all services */ +#define BLE_UUID_UNKNOWN 0x0000 /**< Reserved UUID. */ +#define BLE_UUID_SERVICE_PRIMARY 0x2800 /**< Primary Service. */ +#define BLE_UUID_SERVICE_SECONDARY 0x2801 /**< Secondary Service. */ +#define BLE_UUID_SERVICE_INCLUDE 0x2802 /**< Include. */ +#define BLE_UUID_CHARACTERISTIC 0x2803 /**< Characteristic. */ +#define BLE_UUID_DESCRIPTOR_CHAR_EXT_PROP 0x2900 /**< Characteristic Extended Properties Descriptor. */ +#define BLE_UUID_DESCRIPTOR_CHAR_USER_DESC 0x2901 /**< Characteristic User Description Descriptor. */ +#define BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG 0x2902 /**< Client Characteristic Configuration Descriptor. */ +#define BLE_UUID_DESCRIPTOR_SERVER_CHAR_CONFIG 0x2903 /**< Server Characteristic Configuration Descriptor. */ +#define BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT 0x2904 /**< Characteristic Presentation Format Descriptor. */ +#define BLE_UUID_DESCRIPTOR_CHAR_AGGREGATE_FORMAT 0x2905 /**< Characteristic Aggregate Format Descriptor. */ +/* GATT specific UUIDs */ +#define BLE_UUID_GATT 0x1801 /**< Generic Attribute Profile. */ +#define BLE_UUID_GATT_CHARACTERISTIC_SERVICE_CHANGED 0x2A05 /**< Service Changed Characteristic. */ +/* GAP specific UUIDs */ +#define BLE_UUID_GAP 0x1800 /**< Generic Access Profile. */ +#define BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME 0x2A00 /**< Device Name Characteristic. */ +#define BLE_UUID_GAP_CHARACTERISTIC_APPEARANCE 0x2A01 /**< Appearance Characteristic. */ +#define BLE_UUID_GAP_CHARACTERISTIC_PPF 0x2A02 /**< Peripheral Privacy Flag Characteristic. */ +#define BLE_UUID_GAP_CHARACTERISTIC_RECONN_ADDR 0x2A03 /**< Reconnection Address Characteristic. */ +#define BLE_UUID_GAP_CHARACTERISTIC_PPCP 0x2A04 /**< Peripheral Preferred Connection Parameters Characteristic. */ +/** @} */ + + +/** @defgroup BLE_UUID_TYPES Types of UUID + * @{ */ +#define BLE_UUID_TYPE_UNKNOWN 0x00 /**< Invalid UUID type. */ +#define BLE_UUID_TYPE_BLE 0x01 /**< Bluetooth SIG UUID (16-bit). */ +#define BLE_UUID_TYPE_VENDOR_BEGIN 0x02 /**< Vendor UUID types start at this index (128-bit). */ +/** @} */ + + +/** @defgroup BLE_APPEARANCES Bluetooth Appearance values + * @note Retrieved from http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml + * @{ */ +#define BLE_APPEARANCE_UNKNOWN 0 /**< Unknown. */ +#define BLE_APPEARANCE_GENERIC_PHONE 64 /**< Generic Phone. */ +#define BLE_APPEARANCE_GENERIC_COMPUTER 128 /**< Generic Computer. */ +#define BLE_APPEARANCE_GENERIC_WATCH 192 /**< Generic Watch. */ +#define BLE_APPEARANCE_WATCH_SPORTS_WATCH 193 /**< Watch: Sports Watch. */ +#define BLE_APPEARANCE_GENERIC_CLOCK 256 /**< Generic Clock. */ +#define BLE_APPEARANCE_GENERIC_DISPLAY 320 /**< Generic Display. */ +#define BLE_APPEARANCE_GENERIC_REMOTE_CONTROL 384 /**< Generic Remote Control. */ +#define BLE_APPEARANCE_GENERIC_EYE_GLASSES 448 /**< Generic Eye-glasses. */ +#define BLE_APPEARANCE_GENERIC_TAG 512 /**< Generic Tag. */ +#define BLE_APPEARANCE_GENERIC_KEYRING 576 /**< Generic Keyring. */ +#define BLE_APPEARANCE_GENERIC_MEDIA_PLAYER 640 /**< Generic Media Player. */ +#define BLE_APPEARANCE_GENERIC_BARCODE_SCANNER 704 /**< Generic Barcode Scanner. */ +#define BLE_APPEARANCE_GENERIC_THERMOMETER 768 /**< Generic Thermometer. */ +#define BLE_APPEARANCE_THERMOMETER_EAR 769 /**< Thermometer: Ear. */ +#define BLE_APPEARANCE_GENERIC_HEART_RATE_SENSOR 832 /**< Generic Heart rate Sensor. */ +#define BLE_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT 833 /**< Heart Rate Sensor: Heart Rate Belt. */ +#define BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE 896 /**< Generic Blood Pressure. */ +#define BLE_APPEARANCE_BLOOD_PRESSURE_ARM 897 /**< Blood Pressure: Arm. */ +#define BLE_APPEARANCE_BLOOD_PRESSURE_WRIST 898 /**< Blood Pressure: Wrist. */ +#define BLE_APPEARANCE_GENERIC_HID 960 /**< Human Interface Device (HID). */ +#define BLE_APPEARANCE_HID_KEYBOARD 961 /**< Keyboard (HID Subtype). */ +#define BLE_APPEARANCE_HID_MOUSE 962 /**< Mouse (HID Subtype). */ +#define BLE_APPEARANCE_HID_JOYSTICK 963 /**< Joystiq (HID Subtype). */ +#define BLE_APPEARANCE_HID_GAMEPAD 964 /**< Gamepad (HID Subtype). */ +#define BLE_APPEARANCE_HID_DIGITIZERSUBTYPE 965 /**< Digitizer Tablet (HID Subtype). */ +#define BLE_APPEARANCE_HID_CARD_READER 966 /**< Card Reader (HID Subtype). */ +#define BLE_APPEARANCE_HID_DIGITAL_PEN 967 /**< Digital Pen (HID Subtype). */ +#define BLE_APPEARANCE_HID_BARCODE 968 /**< Barcode Scanner (HID Subtype). */ +#define BLE_APPEARANCE_GENERIC_GLUCOSE_METER 1024 /**< Generic Glucose Meter. */ +#define BLE_APPEARANCE_GENERIC_RUNNING_WALKING_SENSOR 1088 /**< Generic Running Walking Sensor. */ +#define BLE_APPEARANCE_RUNNING_WALKING_SENSOR_IN_SHOE 1089 /**< Running Walking Sensor: In-Shoe. */ +#define BLE_APPEARANCE_RUNNING_WALKING_SENSOR_ON_SHOE 1090 /**< Running Walking Sensor: On-Shoe. */ +#define BLE_APPEARANCE_RUNNING_WALKING_SENSOR_ON_HIP 1091 /**< Running Walking Sensor: On-Hip. */ +#define BLE_APPEARANCE_GENERIC_CYCLING 1152 /**< Generic Cycling. */ +#define BLE_APPEARANCE_CYCLING_CYCLING_COMPUTER 1153 /**< Cycling: Cycling Computer. */ +#define BLE_APPEARANCE_CYCLING_SPEED_SENSOR 1154 /**< Cycling: Speed Sensor. */ +#define BLE_APPEARANCE_CYCLING_CADENCE_SENSOR 1155 /**< Cycling: Cadence Sensor. */ +#define BLE_APPEARANCE_CYCLING_POWER_SENSOR 1156 /**< Cycling: Power Sensor. */ +#define BLE_APPEARANCE_CYCLING_SPEED_CADENCE_SENSOR 1157 /**< Cycling: Speed and Cadence Sensor. */ +#define BLE_APPEARANCE_GENERIC_PULSE_OXIMETER 3136 /**< Generic Pulse Oximeter. */ +#define BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP 3137 /**< Fingertip (Pulse Oximeter subtype). */ +#define BLE_APPEARANCE_PULSE_OXIMETER_WRIST_WORN 3138 /**< Wrist Worn(Pulse Oximeter subtype). */ +#define BLE_APPEARANCE_GENERIC_WEIGHT_SCALE 3200 /**< Generic Weight Scale. */ +#define BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS_ACT 5184 /**< Generic Outdoor Sports Activity. */ +#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_DISP 5185 /**< Location Display Device (Outdoor Sports Activity subtype). */ +#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_AND_NAV_DISP 5186 /**< Location and Navigation Display Device (Outdoor Sports Activity subtype). */ +#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_POD 5187 /**< Location Pod (Outdoor Sports Activity subtype). */ +#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_AND_NAV_POD 5188 /**< Location and Navigation Pod (Outdoor Sports Activity subtype). */ +/** @} */ + +/** @brief Set .type and .uuid fields of ble_uuid_struct to specified uuid value. */ +#define BLE_UUID_BLE_ASSIGN(instance, value) do {\ + instance.type = BLE_UUID_TYPE_BLE; \ + instance.uuid = value;} while(0) + +/** @brief Copy type and uuid members from src to dst ble_uuid_t pointer. Both pointers must be valid/non-null. */ +#define BLE_UUID_COPY_PTR(dst, src) do {\ + (dst)->type = (src)->type; \ + (dst)->uuid = (src)->uuid;} while(0) + +/** @brief Copy type and uuid members from src to dst ble_uuid_t struct. */ +#define BLE_UUID_COPY_INST(dst, src) do {\ + (dst).type = (src).type; \ + (dst).uuid = (src).uuid;} while(0) + +/** @brief Compare for equality both type and uuid members of two (valid, non-null) ble_uuid_t pointers. */ +#define BLE_UUID_EQ(p_uuid1, p_uuid2) \ + (((p_uuid1)->type == (p_uuid2)->type) && ((p_uuid1)->uuid == (p_uuid2)->uuid)) + +/** @brief Compare for difference both type and uuid members of two (valid, non-null) ble_uuid_t pointers. */ +#define BLE_UUID_NEQ(p_uuid1, p_uuid2) \ + (((p_uuid1)->type != (p_uuid2)->type) || ((p_uuid1)->uuid != (p_uuid2)->uuid)) + +/** @} */ + +/** @addtogroup BLE_TYPES_STRUCTURES Structures + * @{ */ + +/** @brief 128 bit UUID values. */ +typedef struct +{ + unsigned char uuid128[16]; +} ble_uuid128_t; + +/** @brief Bluetooth Low Energy UUID type, encapsulates both 16-bit and 128-bit UUIDs. */ +typedef struct +{ + uint16_t uuid; /**< 16-bit UUID value or octets 12-13 of 128-bit UUID. */ + uint8_t type; /**< UUID type, see @ref BLE_UUID_TYPES. If type is BLE_UUID_TYPE_UNKNOWN, the value of uuid is undefined. */ +} ble_uuid_t; + +/** @} */ + +#endif /* BLE_TYPES_H__ */ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ble_util.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,56 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXX + * @{ + * @ingroup YYY + * + * @brief ZZZ. + */ + +#ifndef BLE_UTIL_H__ +#define BLE_UTIL_H__ + +#define UNUSED_VARIABLE(X) ((void)(X)) /**< Used for removing lint warning. */ +#define UNUSED_PARAMETER(X) UNUSED_VARIABLE(X) /**< Used for removing lint warning. */ + +/*lint -emacro(506, MIN) */ /* Suppress "Constant value Boolean */ +#define MIN(a, b) ((a) < (b) ? (a) : (b)) /**< Leaves the minimum of the two arguments. */ + +#endif // BLE_UTIL_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/compiler_abstraction.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,98 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _COMPILER_ABSTRACTION_H +#define _COMPILER_ABSTRACTION_H + +/*lint ++flb "Enter library region" */ + +#if defined ( __CC_ARM ) + + #ifndef __ASM + #define __ASM __asm /*!< asm keyword for ARM Compiler */ + #endif + + #ifndef __INLINE + #define __INLINE __inline /*!< inline keyword for ARM Compiler */ + #endif + + #define GET_SP() __current_sp() /*!> read current SP function for ARM Compiler */ + +#elif defined ( __ICCARM__ ) + + #ifndef __ASM + #define __ASM __asm /*!< asm keyword for IAR Compiler */ + #endif + + #ifndef __INLINE + #define __INLINE inline /*!< inline keyword for IAR Compiler. Only available in High optimization mode! */ + #endif + + #define GET_SP() __get_SP() /*!> read current SP function for IAR Compiler */ + +#elif defined ( __GNUC__ ) + + #ifndef __ASM + #define __ASM __asm /*!< asm keyword for GNU Compiler */ + #endif + + #ifndef __INLINE + #define __INLINE inline /*!< inline keyword for GNU Compiler */ + #endif + + #define GET_SP() gcc_current_sp() /*!> read current SP function for GNU Compiler */ + + static inline unsigned int gcc_current_sp(void) + { + register unsigned sp asm("sp"); + return sp; + } + +#elif defined ( __TASKING__ ) + + #ifndef __ASM + #define __ASM __asm /*!< asm keyword for TASKING Compiler */ + #endif + + #ifndef __INLINE + #define __INLINE inline /*!< inline keyword for TASKING Compiler */ + #endif + + #define GET_SP() __get_MSP() /*!> read current SP function for TASKING Compiler */ + +#endif + +/*lint --flb "Leave library region" */ + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/crc16.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,66 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup crc_compute CRC compute + * @{ + * @ingroup hci_transport + * + * @brief This module implements the CRC-16 calculation in the blocks. + */ + +#ifndef CRC16_H__ +#define CRC16_H__ + +#include <stdint.h> + +/**@brief Function for calculating CRC-16 in blocks. + * + * Feed each consecutive data block into this function, along with the current value of p_crc as + * returned by the previous call of this function. The first call of this function should pass NULL + * as the initial value of the crc in p_crc. + * + * @param[in] p_data The input data block for computation. + * @param[in] size The size of the input data block in bytes. + * @param[in] p_crc The previous calculated CRC-16 value or NULL if first call. + * + * @return The updated CRC-16 value, based on the input supplied. + */ +uint16_t crc16_compute(const uint8_t * p_data, uint32_t size, const uint16_t * p_crc); + +#endif // CRC16_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/dtm_rpc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,43 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +//#defineDTM_INIT 1 + + +typedef enum +{ + DTM_OP_CODE_INIT, + DTM_OP_CODE_MAX +} rpc_dtm_op_codes_t; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/hal_transport.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,250 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup hci_transport HCI Transport + * @{ + * @ingroup app_common + * + * @brief HCI transport module implementation. + * + * This module implements certain specific features from the three-wire UART transport layer, + * defined by the Bluetooth specification version 4.0 [Vol 4] part D. + * + * \par Features supported + * - Transmission and reception of Vendor Specific HCI packet type application packets. + * - Transmission and reception of reliable packets: defined by chapter 6 of the specification. + * + * \par Features not supported + * - Link establishment procedure: defined by chapter 8 of the specification. + * - Low power: defined by chapter 9 of the specification. + * + * \par Implementation specific behaviour + * - As Link establishment procedure is not supported following static link configuration parameters + * are used: + * + TX window size is 1. + * + 16 bit CCITT-CRC must be used. + * + Out of frame software flow control not supported. + * + Parameters specific for resending reliable packets are compile time configurable (clarifed + * later in this document). + * + Acknowledgement packet transmissions are not timeout driven , meaning they are delivered for + * transmission within same context which the corresponding application packet was received. + * + * \par Implementation specific limitations + * Current implementation has the following limitations which will have impact to system wide + * behaviour: + * - Delayed acknowledgement scheduling not implemented: + * There exists a possibility that acknowledgement TX packet and application TX packet will collide + * in the TX pipeline having the end result that acknowledgement packet will be excluded from the TX + * pipeline which will trigger the retransmission algorithm within the peer protocol entity. + * - Delayed retransmission scheduling not implemented: + * There exists a possibility that retransmitted application TX packet and acknowledgement TX packet + * will collide in the TX pipeline having the end result that retransmitted application TX packet + * will be excluded from the TX pipeline. + * - Processing of the acknowledgement number from RX application packets: + * Acknowledgement number is not processed from the RX application packets having the end result + * that unnecessary application packet retransmissions can occur. + * + * The application TX packet processing flow is illustrated by the statemachine below. + * + * @image html hci_transport_tx_sm.png "TX - application packet statemachine" + * + * \par Component specific configuration options + * + * The following compile time configuration options are available, and used to configure the + * application TX packet retransmission interval, in order to suite various application specific + * implementations: + * - MAC_PACKET_SIZE_IN_BITS Maximum size of a single application packet in bits. + * - USED_BAUD_RATE Used uart baudrate. + * + * The following compile time configuration option is available to configure module specific + * behaviour: + * - MAX_RETRY_COUNT Max retransmission retry count for applicaton packets. + */ + +#ifndef HCI_TRANSPORT_H__ +#define HCI_TRANSPORT_H__ + +#include <stdint.h> + +#define HCI_TRANSPORT_PKT_HEADER_SIZE (2) /**< Size of transport packet header */ + +/**@brief Generic event callback function events. */ +typedef enum +{ + HCI_TRANSPORT_RX_RDY, /**< An event indicating that RX packet is ready for read. */ + HCI_TRANSPORT_RX_STARTED, /**< An event indicating that RX packet was started. */ + HCI_TRANSPORT_EVT_TYPE_MAX /**< Enumeration upper bound. */ +} hci_transport_evt_type_t; + +/**@brief Struct containing events from the Transport layer. + */ +typedef struct +{ + hci_transport_evt_type_t evt_type; /**< Type of event. */ +} hci_transport_evt_t; + +/**@brief Transport layer generic event callback function type. + * + * @param[in] event Transport layer event. + */ +typedef void (*hci_transport_event_handler_t)(hci_transport_evt_t event); + +/**@brief TX done event callback function result codes. */ +typedef enum +{ + HCI_TRANSPORT_TX_DONE_SUCCESS, /**< Transmission success, peer transport entity has acknowledged the transmission. */ + HCI_TRANSPORT_TX_DONE_FAILURE /**< Transmission failure. */ +} hci_transport_tx_done_result_t; + +/**@brief Transport layer TX done event callback function type. + * + * @param[in] result TX done event result code. + */ +typedef void (*hci_transport_tx_done_handler_t)(hci_transport_tx_done_result_t result); + +/**@brief Function for registering a generic event handler. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called by the transport layer upon an event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler); + +/**@brief Function for registering a handler for TX done event. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called by the transport layer upon TX done + * event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler); + +/**@brief Function for opening the transport channel and initializing the transport layer. + * + * @warning Must not be called for a channel which has been allready opened. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_INTERNAL Operation failure. Internal error ocurred. + */ +uint32_t hci_transport_open(void); + +/**@brief Function for closing the transport channel. + * + * @note Can be called multiple times and also for not opened channel. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_transport_close(void); + +/**@brief Function for allocating tx packet memory. + * + * @param[out] pp_memory Pointer to the packet data. + * + * @retval NRF_SUCCESS Operation success. Memory was allocated. + * @retval NRF_ERROR_NO_MEM Operation failure. No memory available. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory); + +/**@brief Function for freeing tx packet memory. + * + * @note Memory management works in FIFO principle meaning that free order must match the alloc + * order. + * + * @retval NRF_SUCCESS Operation success. Memory was freed. + */ +uint32_t hci_transport_tx_free(void); + +/**@brief Function for writing a packet. + * + * @note Completion of this method does not guarantee that actual peripheral transmission would + * have completed. + * + * @note In case of 0 byte packet length write request, message will consist of only transport + * module specific headers. + * + * @note The buffer provided to this function must be allocated through @ref hci_transport_tx_alloc + * function. + * + * @retval NRF_SUCCESS Operation success. Packet was added to the transmission queue + * and an event will be send upon transmission completion. + * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and packet was not + * added to the transmission queue. User should wait for + * a appropriate event prior issuing this operation again. + * @retval NRF_ERROR_DATA_SIZE Operation failure. Packet size exceeds limit. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_STATE Operation failure. Channel is not open. + * @retval NRF_ERROR_INVALID_ADDR Operation failure. Buffer provided is not allocated through + * hci_transport_tx_alloc function. + */ +uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length); + +/**@brief Function for extracting received packet. + * + * @note Extracted memory can't be reused by the underlying transport layer untill freed by call to + * hci_transport_rx_pkt_consume(). + * + * @param[out] pp_buffer Pointer to the packet data. + * @param[out] p_length Length of packet data in bytes. + * + * @retval NRF_SUCCESS Operation success. Packet was extracted. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length); + +/**@brief Function for consuming extracted packet described by p_buffer. + * + * RX memory pointed to by p_buffer is freed and can be reused by the underlying transport layer. + * + * @param[in] p_buffer Pointer to the buffer that has been consumed. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to consume. + * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer. + */ +uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer); + +#endif // HCI_TRANSPORT_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/hal_transport_config.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,55 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup XXX HCI Transport Layer Configuration + * @{ + * @ingroup YYY + * @brief Definition of HCI Transport Layer configurable parameters. + */ + +#ifndef HCI_TRANSPORT_CFG_H__ +#define HCI_TRANSPORT_CFG_H__ + +#include "hal_transport.h" + +#define HCI_TRANSPORT_PKT_DATA_SIZE (510) +#define HCI_TRANSPORT_PKT_BUFFER_SIZE (HCI_TRANSPORT_PKT_DATA_SIZE + \ + HCI_TRANSPORT_PKT_HEADER_SIZE) /**< Maximum size of a single application packet in bytes. */ + +#endif // HCI_TRANSPORT_CFG_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nordic_common.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,96 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * @brief Common defines and macros for firmware developed by Nordic Semiconductor. + */ + +#ifndef NORDIC_COMMON_H__ +#define NORDIC_COMMON_H__ + +/** Swaps the upper byte with the lower byte in a 16 bit variable */ +//lint -emacro((572),SWAP) // Suppress warning 572 "Excessive shift value" +#define SWAP(x) ((((x)&0xFF)<<8)|(((x)>>8)&0xFF)) + +/** The upper 8 bits of a 16 bit value */ +//lint -emacro(572,MSB) // Suppress warning 572 "Excessive shift value" +#define MSB(a) (((a) & 0xFF00) >> 8) +/** The lower 8 bits (of a 16 bit value) */ +#define LSB(a) ((a) & 0xFF) + +/** Leaves the minimum of the two arguments */ +/*lint -emacro(506, MIN) */ /* Suppress "Constant value Boolean */ +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +/** Leaves the maximum of the two arguments */ +/*lint -emacro(506, MAX) */ /* Suppress "Constant value Boolean */ +#define MAX(a, b) ((a) < (b) ? (b) : (a)) + +#define BIT_0 0x01 /**< The value of bit 0 */ +#define BIT_1 0x02 /**< The value of bit 1 */ +#define BIT_2 0x04 /**< The value of bit 2 */ +#define BIT_3 0x08 /**< The value of bit 3 */ +#define BIT_4 0x10 /**< The value of bit 4 */ +#define BIT_5 0x20 /**< The value of bit 5 */ +#define BIT_6 0x40 /**< The value of bit 6 */ +#define BIT_7 0x80 /**< The value of bit 7 */ +#define BIT_8 0x0100 /**< The value of bit 8 */ +#define BIT_9 0x0200 /**< The value of bit 9 */ +#define BIT_10 0x0400 /**< The value of bit 10 */ +#define BIT_11 0x0800 /**< The value of bit 11 */ +#define BIT_12 0x1000 /**< The value of bit 12 */ +#define BIT_13 0x2000 /**< The value of bit 13 */ +#define BIT_14 0x4000 /**< The value of bit 14 */ +#define BIT_15 0x8000 /**< The value of bit 15 */ +#define BIT_16 0x00010000 /**< The value of bit 16 */ +#define BIT_17 0x00020000 /**< The value of bit 17 */ +#define BIT_18 0x00040000 /**< The value of bit 18 */ +#define BIT_19 0x00080000 /**< The value of bit 19 */ +#define BIT_20 0x00100000 /**< The value of bit 20 */ +#define BIT_21 0x00200000 /**< The value of bit 21 */ +#define BIT_22 0x00400000 /**< The value of bit 22 */ +#define BIT_23 0x00800000 /**< The value of bit 23 */ +#define BIT_24 0x01000000 /**< The value of bit 24 */ +#define BIT_25 0x02000000 /**< The value of bit 25 */ +#define BIT_26 0x04000000 /**< The value of bit 26 */ +#define BIT_27 0x08000000 /**< The value of bit 27 */ +#define BIT_28 0x10000000 /**< The value of bit 28 */ +#define BIT_29 0x20000000 /**< The value of bit 29 */ +#define BIT_30 0x40000000 /**< The value of bit 30 */ +#define BIT_31 0x80000000 /**< The value of bit 31 */ + +#define UNUSED_VARIABLE(X) ((void)(X)) +#define UNUSED_PARAMETER(X) UNUSED_VARIABLE(X) + +#endif // NORDIC_COMMON_H__ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_error.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /** + @defgroup nrf_error SoftDevice Global Error Codes + @{ + + @brief Global Error definitions +*/ + +/* Header guard */ +#ifndef NRF_ERROR_H__ +#define NRF_ERROR_H__ + +/** @defgroup NRF_ERRORS_BASE Error Codes Base number definitions + * @{ */ +#define NRF_ERROR_BASE_NUM (0x0) ///< Global error base +#define NRF_ERROR_SDM_BASE_NUM (0x1000) ///< SDM error base +#define NRF_ERROR_SOC_BASE_NUM (0x2000) ///< SoC error base +#define NRF_ERROR_STK_BASE_NUM (0x3000) ///< STK error base +/** @} */ + +#define NRF_SUCCESS (NRF_ERROR_BASE_NUM + 0) ///< Successful command +#define NRF_ERROR_SVC_HANDLER_MISSING (NRF_ERROR_BASE_NUM + 1) ///< SVC handler is missing +#define NRF_ERROR_SOFTDEVICE_NOT_ENABLED (NRF_ERROR_BASE_NUM + 2) ///< SoftDevice has not been enabled +#define NRF_ERROR_INTERNAL (NRF_ERROR_BASE_NUM + 3) ///< Internal Error +#define NRF_ERROR_NO_MEM (NRF_ERROR_BASE_NUM + 4) ///< No Memory for operation +#define NRF_ERROR_NOT_FOUND (NRF_ERROR_BASE_NUM + 5) ///< Not found +#define NRF_ERROR_NOT_SUPPORTED (NRF_ERROR_BASE_NUM + 6) ///< Not supported +#define NRF_ERROR_INVALID_PARAM (NRF_ERROR_BASE_NUM + 7) ///< Invalid Parameter +#define NRF_ERROR_INVALID_STATE (NRF_ERROR_BASE_NUM + 8) ///< Invalid state, operation disallowed in this state +#define NRF_ERROR_INVALID_LENGTH (NRF_ERROR_BASE_NUM + 9) ///< Invalid Length +#define NRF_ERROR_INVALID_FLAGS (NRF_ERROR_BASE_NUM + 10) ///< Invalid Flags +#define NRF_ERROR_INVALID_DATA (NRF_ERROR_BASE_NUM + 11) ///< Invalid Data +#define NRF_ERROR_DATA_SIZE (NRF_ERROR_BASE_NUM + 12) ///< Data size exceeds limit +#define NRF_ERROR_TIMEOUT (NRF_ERROR_BASE_NUM + 13) ///< Operation timed out +#define NRF_ERROR_NULL (NRF_ERROR_BASE_NUM + 14) ///< Null Pointer +#define NRF_ERROR_FORBIDDEN (NRF_ERROR_BASE_NUM + 15) ///< Forbidden Operation +#define NRF_ERROR_INVALID_ADDR (NRF_ERROR_BASE_NUM + 16) ///< Bad Memory Address +#define NRF_ERROR_BUSY (NRF_ERROR_BASE_NUM + 17) ///< Busy + +#endif // NRF_ERROR_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_error_sdm.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,59 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /** + @addtogroup nrf_sdm_api + @{ + @defgroup nrf_sdm_error SoftDevice Manager Error Codes + @{ + + @brief Error definitions for the SDM API +*/ + +/* Header guard */ +#ifndef NRF_ERROR_SDM_H__ +#define NRF_ERROR_SDM_H__ + +#include "nrf_error.h" + +#define NRF_ERROR_SDM_LFCLK_SOURCE_UNKNOWN (NRF_ERROR_SDM_BASE_NUM + 0) ///< Unknown lfclk source +#define NRF_ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION (NRF_ERROR_SDM_BASE_NUM + 1) ///< Incorrect interrupt configuration (can be caused by using illegal priority levels, or having enabled SoftDevice interrupts) +#define NRF_ERROR_SDM_INCORRECT_CLENR0 (NRF_ERROR_SDM_BASE_NUM + 2) ///< Incorrect CLENR0 (can be caused by erronous SoftDevice flashing) + +#endif // NRF_ERROR_SDM_H__ + +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_error_soc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,75 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /** + @addtogroup nrf_soc_api + @{ + @defgroup nrf_soc_error SoC Library Error Codes + @{ + + @brief Error definitions for the SoC library + +*/ + +/* Header guard */ +#ifndef NRF_ERROR_SOC_H__ +#define NRF_ERROR_SOC_H__ + +#include "nrf_error.h" + +/* Mutex Errors */ +#define NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN (NRF_ERROR_SOC_BASE_NUM + 0) ///< Mutex already taken + +/* NVIC errors */ +#define NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE (NRF_ERROR_SOC_BASE_NUM + 1) ///< NVIC interrupt not available +#define NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED (NRF_ERROR_SOC_BASE_NUM + 2) ///< NVIC interrupt priority not allowed +#define NRF_ERROR_SOC_NVIC_SHOULD_NOT_RETURN (NRF_ERROR_SOC_BASE_NUM + 3) ///< NVIC should not return + +/* Power errors */ +#define NRF_ERROR_SOC_POWER_MODE_UNKNOWN (NRF_ERROR_SOC_BASE_NUM + 4) ///< Power mode unknown +#define NRF_ERROR_SOC_POWER_POF_THRESHOLD_UNKNOWN (NRF_ERROR_SOC_BASE_NUM + 5) ///< Power POF threshold unknown +#define NRF_ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN (NRF_ERROR_SOC_BASE_NUM + 6) ///< Power off should not return + +/* Rand errors */ +#define NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES (NRF_ERROR_SOC_BASE_NUM + 7) ///< RAND not enough values + +/* PPI errors */ +#define NRF_ERROR_SOC_PPI_INVALID_CHANNEL (NRF_ERROR_SOC_BASE_NUM + 8) ///< Invalid PPI Channel +#define NRF_ERROR_SOC_PPI_INVALID_GROUP (NRF_ERROR_SOC_BASE_NUM + 9) ///< Invalid PPI Group + +#endif // NRF_ERROR_SOC_H__ +/** + @} + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_mbr.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,181 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @defgroup nrf_mbr_api Master Boot Record API + @{ + + @brief APIs for updating SoftDevice and BootLoader + +*/ + +/* Header guard */ +#ifndef NRF_MBR_H__ +#define NRF_MBR_H__ + +#include "nrf_svc.h" +#include <stdint.h> + + +/** @addtogroup NRF_MBR_DEFINES Defines + * @{ */ + +/**@brief MBR SVC Base number. */ +#define MBR_SVC_BASE 0x18 +/** @} */ + +/** @addtogroup NRF_MBR_ENUMS Enumerations + * @{ */ + +/**@brief nRF Master Boot Record API SVC numbers. */ +enum NRF_MBR_SVCS +{ + SD_MBR_COMMAND = MBR_SVC_BASE, /**< ::sd_mbr_command */ +}; + +/**@brief Possible values for ::sd_mbr_command_t.command */ +enum NRF_MBR_COMMANDS +{ + SD_MBR_COMMAND_COPY_BL, /**< Copy a new a new BootLoader. @see sd_mbr_command_copy_bl_t */ + SD_MBR_COMMAND_COPY_SD, /**< Copy a new SoftDevice. @see ::sd_mbr_command_copy_sd_t*/ + SD_MBR_COMMAND_INIT_SD, /**< Init forwarding interrupts to SD, and run reset function in SD*/ + SD_MBR_COMMAND_COMPARE, /**< This command works like memcmp. @see ::sd_mbr_command_compare_t*/ + SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET, /**< Start forwarding all exception to this address @see ::sd_mbr_command_vector_table_base_set_t*/ +}; + +/** @} */ + +/** @addtogroup NRF_MBR_TYPES Types + * @{ */ + +/**@brief This command copies part of a new SoftDevice + * The destination area is erased before copying. + * If dst is in the middle of a flash page, that whole flash page will be erased. + * If (dst+len) is in the middle of a flash page, that whole flash page will be erased. + * + * The user of this function is responsible for setting the PROTENSET registers. + * + * @retval ::NRF_SUCCESS indicates that the contents of the memory blocks where copied correctly. + * @retval ::NRF_ERROR_INTERNAL indicates that the contents of the memory blocks where not verified correctly after copying. + */ +typedef struct +{ + uint32_t *src; /**< Pointer to the source of data to be copied.*/ + uint32_t *dst; /**< Pointer to the destination where the content is to be copied.*/ + uint32_t len; /**< Number of 32 bit words to copy. Must be a multiple of 256 words*/ +}sd_mbr_command_copy_sd_t; + + +/**@brief This command works like memcmp, but takes the length in words. + * + * @retval ::NRF_SUCCESS indicates that the contents of both memory blocks are equal. + * @retval ::NRF_ERROR_NULL indicates that the contents of the memory blocks are not equal. + */ +typedef struct +{ + uint32_t *ptr1; /**< Pointer to block of memory */ + uint32_t *ptr2; /**< Pointer to block of memory */ + uint32_t len; /**< Number of 32 bit words to compare*/ +}sd_mbr_command_compare_t; + + +/**@brief This command copies a new BootLoader. + * With this command, destination of BootLoader is always the address written in NRF_UICR->BOOTADDR. + * + * Destination is erased by this function. + * If (destination+bl_len) is in the middle of a flash page, that whole flash page will be erased. + * + * This function will use PROTENSET to protect the flash that is not intended to be written. + * + * On Success, this function will not return. It will start the new BootLoader from reset-vector as normal. + * + * @retval ::NRF_ERROR_INVALID_STATE indicates that something was wrong. + * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen. + * @retval ::NRF_ERROR_FORBIDDEN if NRF_UICR->BOOTADDR is not set + * @retval ::NRF_ERROR_INVALID_LENGTH is invalid. + */ +typedef struct +{ + uint32_t *bl_src; /**< Pointer to the source of the Bootloader to be be copied.*/ + uint32_t bl_len; /**< Number of 32 bit words to copy for BootLoader */ +}sd_mbr_command_copy_bl_t; + +/**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the MBR + * + * Once this function has been called, this address is where the MBR will start to forward interrupts to after a reset. + * + * To restore default forwarding thiss function should be called with @param address set to 0. + * The MBR will then start forwarding to interrupts to the adress in NFR_UICR->BOOTADDR or to the SoftDevice if the BOOTADDR is not set. + * + * @retval ::NRF_SUCCESS + */ +typedef struct +{ + uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/ +}sd_mbr_command_vector_table_base_set_t; + +typedef struct +{ + uint32_t command; /**< type of command to be issued see @ref NRF_MBR_COMMANDS. */ + union + { + sd_mbr_command_copy_sd_t copy_sd; /**< Parameters for copy*/ + sd_mbr_command_copy_bl_t copy_bl; /**< Parameters for copy SoftDevice and BootLoader*/ + sd_mbr_command_compare_t compare; /**< Parameters for verify*/ + sd_mbr_command_vector_table_base_set_t base_set; /**< Parameters for vector table base set.*/ + } params; +}sd_mbr_command_t; + +/** @} */ + +/** @addtogroup NRF_MBR_FUNCTIONS Functions + * @{ */ + +/**@brief Issue Master Boot Record commands + * + * Commands used when updating a SoftDevice and bootloader + * + * @param[in] param Pointer to a struct describing the command + * + *@note for retvals see ::sd_mbr_command_copy_sd_t ::sd_mbr_command_copy_bl_t ::sd_mbr_command_compare_t + +*/ +SVCALL(SD_MBR_COMMAND, uint32_t, sd_mbr_command(sd_mbr_command_t* param)); + +/** @} */ +#endif // NRF_MBR_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_sdm.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,193 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @defgroup nrf_sdm_api SoftDevice Manager API + @{ + + @brief APIs for SoftDevice management. + +*/ + +/* Header guard */ +#ifndef NRF_SDM_H__ +#define NRF_SDM_H__ + +#include "nrf_svc.h" +#include "nrf51.h" +#include "nrf_soc.h" +#include "nrf_error_sdm.h" + +/** @addtogroup NRF_SDM_DEFINES Defines + * @{ */ + +/**@brief SoftDevice Manager SVC Base number. */ +#define SDM_SVC_BASE (0x10) + +/** @} */ + +/** @addtogroup NRF_SDM_ENUMS Enumerations + * @{ */ + +/**@brief nRF SoftDevice Manager API SVC numbers. */ +enum NRF_SD_SVCS +{ + SD_SOFTDEVICE_ENABLE = SDM_SVC_BASE, /**< ::sd_softdevice_enable */ + SD_SOFTDEVICE_DISABLE, /**< ::sd_softdevice_disable */ + SD_SOFTDEVICE_IS_ENABLED, /**< ::sd_softdevice_is_enabled */ + SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, /**< ::sd_softdevice_vector_table_base_set */ + SVC_SDM_LAST /**< Placeholder for last SDM SVC */ +}; + +/**@brief Possible lfclk oscillator sources. */ +enum NRF_CLOCK_LFCLKSRCS +{ + NRF_CLOCK_LFCLKSRC_SYNTH_250_PPM, /**< LFCLK Synthesized from HFCLK. */ + NRF_CLOCK_LFCLKSRC_XTAL_500_PPM, /**< LFCLK crystal oscillator 500 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_250_PPM, /**< LFCLK crystal oscillator 250 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_150_PPM, /**< LFCLK crystal oscillator 150 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_100_PPM, /**< LFCLK crystal oscillator 100 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_75_PPM, /**< LFCLK crystal oscillator 75 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_50_PPM, /**< LFCLK crystal oscillator 50 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_30_PPM, /**< LFCLK crystal oscillator 30 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, /**< LFCLK crystal oscillator 20 PPM accuracy. */ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_250MS_CALIBRATION, /**< LFCLK RC oscillator, 250ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_500MS_CALIBRATION, /**< LFCLK RC oscillator, 500ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_1000MS_CALIBRATION, /**< LFCLK RC oscillator, 1000ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_2000MS_CALIBRATION, /**< LFCLK RC oscillator, 2000ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, /**< LFCLK RC oscillator, 4000ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_8000MS_CALIBRATION, /**< LFCLK RC oscillator, 8000ms calibration interval.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_TEMP_1000MS_CALIBRATION, /**< LFCLK RC oscillator. Temperature checked every 1000ms, if changed above a threshold, a calibration is done.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_TEMP_2000MS_CALIBRATION, /**< LFCLK RC oscillator. Temperature checked every 2000ms, if changed above a threshold, a calibration is done.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_TEMP_4000MS_CALIBRATION, /**< LFCLK RC oscillator. Temperature checked every 4000ms, if changed above a threshold, a calibration is done.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_TEMP_8000MS_CALIBRATION, /**< LFCLK RC oscillator. Temperature checked every 8000ms, if changed above a threshold, a calibration is done.*/ + NRF_CLOCK_LFCLKSRC_RC_250_PPM_TEMP_16000MS_CALIBRATION, /**< LFCLK RC oscillator. Temperature checked every 16000ms, if changed above a threshold, a calibration is done.*/ +}; + +/** @} */ + +/** @addtogroup NRF_SDM_TYPES Types + * @{ */ + +/**@brief Type representing lfclk oscillator source. */ +typedef uint32_t nrf_clock_lfclksrc_t; + + +/**@brief SoftDevice Assertion Handler type. + * + * When an unexpected error occurs within the SoftDevice it will call the SoftDevice assertion handler callback. + * The protocol stack will be in an undefined state when this happens and the only way to recover will be to + * perform a reset, using e.g. CMSIS NVIC_SystemReset(). + * + * @note This callback is executed in HardFault context, thus SVC functions cannot be called from the SoftDevice assert callback. + * + * @param[in] pc The program counter of the failed assert. + * @param[in] line_number Line number where the assert failed. + * @param[in] file_name File name where the assert failed. + */ +typedef void (*softdevice_assertion_handler_t)(uint32_t pc, uint16_t line_number, const uint8_t * p_file_name); + +/** @} */ + +/** @addtogroup NRF_SDM_FUNCTIONS Functions + * @{ */ + +/**@brief Enables the SoftDevice and by extension the protocol stack. + * + * Idempotent function to enable the SoftDevice. + * + * @note Some care must be taken if a low frequency clock source is already running when calling this function: + * If the LF clock has a different source then the one currently running, it will be stopped. Then, the new + * clock source will be started. + * + * @note This function has no effect when returning with an error. + * + * @post If return code is ::NRF_SUCCESS + * - SoC library and protocol stack APIs are made available + * - A portion of RAM will be unavailable (see relevant SDS documentation) + * - Some peripherals will be unavailable or available only through the SoC API (see relevant SDS documentation) + * - Interrupts will not arrive from protected peripherals or interrupts + * - nrf_nvic_ functions must be used instead of CMSIS NVIC_ functions for reliable usage of the softdevice. + * - Interrupt latency may be affected by the SoftDevice (see relevant SDS documentation) + * - Chosen low frequency clock source will be running + * + * @param clock_source Low frequency clock source and accuracy. (Note: In the case of XTAL source, the PPM accuracy of the chosen clock source must be greater than or equal to the actual characteristics of your XTAL clock). + * @param assertion_handler Callback for SoftDevice assertions. + * + * @retval ::NRF_SUCCESS + * @retval ::NRF_ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION SoftDeviceinterrupt is already enabled, or an enabled interrupt has an illegal priority level + * @retval ::NRF_ERROR_SDM_LFCLK_SOURCE_UNKNOWN Unknown low frequency clock source selected + */ +SVCALL(SD_SOFTDEVICE_ENABLE, uint32_t, sd_softdevice_enable(nrf_clock_lfclksrc_t clock_source, softdevice_assertion_handler_t assertion_handler)); + +/**@brief Disables the SoftDevice and by extension the protocol stack. + * + * Idempotent function to disable the SoftDevice. + * + * @post SoC library and protocol stack APIs are made unavailable. + * @post All interrupts that was protected by the SoftDevice will be disabled and initialized to priority 0 (highest). + * @post All peripherals used by the SoftDevice will be reset to default values. + * @post All of RAM become available. + * @post All interrupts are forwarded to the application. + * @post LFCLK source chosen in ::sd_softdevice_enable will be left running. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_SOFTDEVICE_DISABLE, uint32_t, sd_softdevice_disable(void)); + +/**@brief Check if the SoftDevice is enabled. + * + * @param[out] p_softdevice_enabled If the SoftDevice is enabled: 1 else 0. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_SOFTDEVICE_IS_ENABLED, uint32_t, sd_softdevice_is_enabled(uint8_t * p_softdevice_enabled)); + +/**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the SoftDevice + * + * This function is only intended to be called when a bootloader is enabled. + * + * @param[in] address The base address of the interrupt vector table for forwarded interrupts. + + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, uint32_t, sd_softdevice_vector_table_base_set(uint32_t address)); + +/** @} */ + +#endif // NRF_SDM_H__ + +/** + @} +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_soc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,985 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @defgroup nrf_soc_api SoC Library API + * @{ + * + * @brief APIs for the SoC library. + * +*/ + +#ifndef NRF_SOC_H__ +#define NRF_SOC_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "nrf_svc.h" +#include "nrf51.h" +#include "nrf51_bitfields.h" +#include "nrf_error_soc.h" + +/** @addtogroup NRF_SOC_DEFINES Defines + * @{ */ + +/**@brief The number of the lowest SVC number reserved for the SoC library. */ +#define SOC_SVC_BASE (0x20) +#define SOC_SVC_BASE_NOT_AVAILABLE (0x23) + +/**@brief Guranteed time for application to process radio inactive notification. */ +#define NRF_RADIO_NOTIFICATION_INACTIVE_GUARANTEED_TIME_US (62) + +/**@brief The minimum allowed timeslot extension time. */ +#define NRF_RADIO_MINIMUM_TIMESLOT_LENGTH_EXTENSION_TIME_US (200) + +#define SOC_ECB_KEY_LENGTH (16) /**< ECB key length. */ +#define SOC_ECB_CLEARTEXT_LENGTH (16) /**< ECB cleartext length. */ +#define SOC_ECB_CIPHERTEXT_LENGTH (SOC_ECB_CLEARTEXT_LENGTH) /**< ECB ciphertext length. */ + +#define SD_EVT_IRQn (SWI2_IRQn) /**< SoftDevice Event IRQ number. Used for both protocol events and SoC events. */ +#define SD_EVT_IRQHandler (SWI2_IRQHandler) /**< SoftDevice Event IRQ handler. Used for both protocol events and SoC events. */ +#define RADIO_NOTIFICATION_IRQn (SWI1_IRQn) /**< The radio notification IRQ number. */ +#define RADIO_NOTIFICATION_IRQHandler (SWI1_IRQHandler) /**< The radio notification IRQ handler. */ + +#define NRF_RADIO_LENGTH_MIN_US (100) /**< The shortest allowed radio timeslot, in microseconds. */ +#define NRF_RADIO_LENGTH_MAX_US (100000) /**< The longest allowed radio timeslot, in microseconds. */ + +#define NRF_RADIO_DISTANCE_MAX_US (128000000UL - 1UL) /**< The longest timeslot distance, in microseconds, allowed for the distance parameter (see @ref nrf_radio_request_normal_t) in the request. */ + +#define NRF_RADIO_EARLIEST_TIMEOUT_MAX_US (128000000UL - 1UL) /**< The longest timeout, in microseconds, allowed when requesting the earliest possible timeslot. */ + +#define NRF_RADIO_START_JITTER_US (2) /**< The maximum jitter in NRF_RADIO_CALLBACK_SIGNAL_TYPE_START relative to the requested start time. */ + +/** @} */ + +/** @addtogroup NRF_SOC_TYPES Types + * @{ */ + +/**@brief The SVC numbers used by the SVC functions in the SoC library. */ +enum NRF_SOC_SVCS +{ + SD_FLASH_PAGE_ERASE = SOC_SVC_BASE, + SD_FLASH_WRITE, + SD_FLASH_PROTECT, + SD_MUTEX_NEW = SOC_SVC_BASE_NOT_AVAILABLE, + SD_MUTEX_ACQUIRE, + SD_MUTEX_RELEASE, + SD_NVIC_ENABLEIRQ, + SD_NVIC_DISABLEIRQ, + SD_NVIC_GETPENDINGIRQ, + SD_NVIC_SETPENDINGIRQ, + SD_NVIC_CLEARPENDINGIRQ, + SD_NVIC_SETPRIORITY, + SD_NVIC_GETPRIORITY, + SD_NVIC_SYSTEMRESET, + SD_NVIC_CRITICAL_REGION_ENTER, + SD_NVIC_CRITICAL_REGION_EXIT, + SD_RAND_APPLICATION_POOL_CAPACITY, + SD_RAND_APPLICATION_BYTES_AVAILABLE, + SD_RAND_APPLICATION_GET_VECTOR, + SD_POWER_MODE_SET, + SD_POWER_SYSTEM_OFF, + SD_POWER_RESET_REASON_GET, + SD_POWER_RESET_REASON_CLR, + SD_POWER_POF_ENABLE, + SD_POWER_POF_THRESHOLD_SET, + SD_POWER_RAMON_SET, + SD_POWER_RAMON_CLR, + SD_POWER_RAMON_GET, + SD_POWER_GPREGRET_SET, + SD_POWER_GPREGRET_CLR, + SD_POWER_GPREGRET_GET, + SD_POWER_DCDC_MODE_SET, + SD_APP_EVT_WAIT, + SD_CLOCK_HFCLK_REQUEST, + SD_CLOCK_HFCLK_RELEASE, + SD_CLOCK_HFCLK_IS_RUNNING, + SD_PPI_CHANNEL_ENABLE_GET, + SD_PPI_CHANNEL_ENABLE_SET, + SD_PPI_CHANNEL_ENABLE_CLR, + SD_PPI_CHANNEL_ASSIGN, + SD_PPI_GROUP_TASK_ENABLE, + SD_PPI_GROUP_TASK_DISABLE, + SD_PPI_GROUP_ASSIGN, + SD_PPI_GROUP_GET, + SD_RADIO_NOTIFICATION_CFG_SET, + SD_ECB_BLOCK_ENCRYPT, + SD_RADIO_SESSION_OPEN, + SD_RADIO_SESSION_CLOSE, + SD_RADIO_REQUEST, + SD_EVT_GET, + SD_TEMP_GET, + SVC_SOC_LAST +}; + +/**@brief Possible values of a ::nrf_mutex_t. */ +enum NRF_MUTEX_VALUES +{ + NRF_MUTEX_FREE, + NRF_MUTEX_TAKEN +}; + +/**@brief Possible values of ::nrf_app_irq_priority_t. */ +enum NRF_APP_PRIORITIES +{ + NRF_APP_PRIORITY_HIGH = 1, + NRF_APP_PRIORITY_LOW = 3 +}; + +/**@brief Possible values of ::nrf_power_mode_t. */ +enum NRF_POWER_MODES +{ + NRF_POWER_MODE_CONSTLAT, /**< Constant latency mode. See power management in the reference manual. */ + NRF_POWER_MODE_LOWPWR /**< Low power mode. See power management in the reference manual. */ +}; + + +/**@brief Possible values of ::nrf_power_failure_threshold_t */ +enum NRF_POWER_THRESHOLDS +{ + NRF_POWER_THRESHOLD_V21, /**< 2.1 Volts power failure threshold. */ + NRF_POWER_THRESHOLD_V23, /**< 2.3 Volts power failure threshold. */ + NRF_POWER_THRESHOLD_V25, /**< 2.5 Volts power failure threshold. */ + NRF_POWER_THRESHOLD_V27 /**< 2.7 Volts power failure threshold. */ +}; + + +/**@brief Possible values of ::nrf_power_dcdc_mode_t. */ +enum NRF_POWER_DCDC_MODES +{ + NRF_POWER_DCDC_MODE_OFF, /**< The DCDC is always off. */ + NRF_POWER_DCDC_MODE_ON, /**< The DCDC is always on. */ + NRF_POWER_DCDC_MODE_AUTOMATIC /**< The DCDC is automatically managed. */ +}; + +/**@brief Possible values of ::nrf_radio_notification_distance_t. */ +enum NRF_RADIO_NOTIFICATION_DISTANCES +{ + NRF_RADIO_NOTIFICATION_DISTANCE_NONE = 0, /**< The event does not have a notification. */ + NRF_RADIO_NOTIFICATION_DISTANCE_800US, /**< The distance from the active notification to start of radio activity. */ + NRF_RADIO_NOTIFICATION_DISTANCE_1740US, /**< The distance from the active notification to start of radio activity. */ + NRF_RADIO_NOTIFICATION_DISTANCE_2680US, /**< The distance from the active notification to start of radio activity. */ + NRF_RADIO_NOTIFICATION_DISTANCE_3620US, /**< The distance from the active notification to start of radio activity. */ + NRF_RADIO_NOTIFICATION_DISTANCE_4560US, /**< The distance from the active notification to start of radio activity. */ + NRF_RADIO_NOTIFICATION_DISTANCE_5500US /**< The distance from the active notification to start of radio activity. */ +}; + + +/**@brief Possible values of ::nrf_radio_notification_type_t. */ +enum NRF_RADIO_NOTIFICATION_TYPES +{ + NRF_RADIO_NOTIFICATION_TYPE_NONE = 0, /**< The event does not have a radio notification signal. */ + NRF_RADIO_NOTIFICATION_TYPE_INT_ON_ACTIVE, /**< Using interrupt for notification when the radio will be enabled. */ + NRF_RADIO_NOTIFICATION_TYPE_INT_ON_INACTIVE, /**< Using interrupt for notification when the radio has been disabled. */ + NRF_RADIO_NOTIFICATION_TYPE_INT_ON_BOTH, /**< Using interrupt for notification both when the radio will be enabled and disabled. */ +}; + +/**@brief SoC Events. */ +enum NRF_SOC_EVTS +{ + NRF_EVT_HFCLKSTARTED, /**< Event indicating that the HFCLK has started. */ + NRF_EVT_POWER_FAILURE_WARNING, /**< Event indicating that a power failure warning has occurred. */ + NRF_EVT_FLASH_OPERATION_SUCCESS, /**< Event indicating that the ongoing flash operation has completed successfully. */ + NRF_EVT_FLASH_OPERATION_ERROR, /**< Event indicating that the ongoing flash operation has timed out with an error. */ + NRF_EVT_RADIO_BLOCKED, /**< Event indicating that a radio timeslot was blocked. */ + NRF_EVT_RADIO_CANCELED, /**< Event indicating that a radio timeslot was canceled by SoftDevice. */ + NRF_EVT_RADIO_SIGNAL_CALLBACK_INVALID_RETURN, /**< Event indicating that a radio signal callback handler return was invalid. */ + NRF_EVT_RADIO_SESSION_IDLE, /**< Event indicating that a radio session is idle. */ + NRF_EVT_RADIO_SESSION_CLOSED, /**< Event indicating that a radio session is closed. */ + NRF_EVT_NUMBER_OF_EVTS +}; + +/** @} */ + +/** @addtogroup NRF_SOC_TYPES Types + * @{ */ + +/**@brief Represents a mutex for use with the nrf_mutex functions. + * @note Accessing the value directly is not safe, use the mutex functions! + */ +typedef volatile uint8_t nrf_mutex_t; + +/**@brief The interrupt priorities available to the application while the softdevice is active. */ +typedef uint8_t nrf_app_irq_priority_t; + +/**@brief Represents a power mode, used in power mode functions */ +typedef uint8_t nrf_power_mode_t; + +/**@brief Represents a power failure threshold value. */ +typedef uint8_t nrf_power_failure_threshold_t; + +/**@brief Represents a DCDC mode value. */ +typedef uint32_t nrf_power_dcdc_mode_t; + +/**@brief Radio notification distances. */ +typedef uint8_t nrf_radio_notification_distance_t; + +/**@brief Radio notification types. */ +typedef uint8_t nrf_radio_notification_type_t; + +/** @brief The Radio signal callback types. */ +enum NRF_RADIO_CALLBACK_SIGNAL_TYPE +{ + NRF_RADIO_CALLBACK_SIGNAL_TYPE_START, /**< This signal indicates the start of the radio timeslot. */ + NRF_RADIO_CALLBACK_SIGNAL_TYPE_TIMER0, /**< This signal indicates the NRF_TIMER0 interrupt. */ + NRF_RADIO_CALLBACK_SIGNAL_TYPE_RADIO, /**< This signal indicates the NRF_RADIO interrupt. */ + NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_FAILED, /**< This signal indicates extend action failed. */ + NRF_RADIO_CALLBACK_SIGNAL_TYPE_EXTEND_SUCCEEDED /**< This signal indicates extend action succeeded. */ +}; + +/** @brief The actions requested by the signal callback. + * + * This code gives the SOC instructions about what action to take when the signal callback has + * returned. + */ +enum NRF_RADIO_SIGNAL_CALLBACK_ACTION +{ + NRF_RADIO_SIGNAL_CALLBACK_ACTION_NONE, /**< Return without action. */ + NRF_RADIO_SIGNAL_CALLBACK_ACTION_EXTEND, /**< Request an extension of the current timeslot (maximum execution time for this action is when the extension succeeded). */ + NRF_RADIO_SIGNAL_CALLBACK_ACTION_END, /**< End the current radio timeslot. */ + NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END /**< Request a new radio timeslot and end the current timeslot. */ +}; + +/**@brief Radio timeslot high frequency clock source configuration. */ +enum NRF_RADIO_HFCLK_CFG +{ + NRF_RADIO_HFCLK_CFG_DEFAULT, /**< Use the currently selected oscillator as HF clock source during the timeslot (i.e. the source is not specified). */ + NRF_RADIO_HFCLK_CFG_FORCE_XTAL, /**< Force external crystal to be used as HF clock source during whole the timeslot. */ +}; + +/** @brief Radio timeslot priorities. */ +enum NRF_RADIO_PRIORITY +{ + NRF_RADIO_PRIORITY_HIGH, /**< High (equal priority as the normal connection priority of the SoftDevice stack(s)). */ + NRF_RADIO_PRIORITY_NORMAL, /**< Normal (equal priority as the priority of secondary activites of the SoftDevice stack(s)). */ +}; + +/** @brief Radio timeslot request type. */ +enum NRF_RADIO_REQUEST_TYPE +{ + NRF_RADIO_REQ_TYPE_EARLIEST, /**< Request timeslot as early as possible. This should always be used for the first request in a session. */ + NRF_RADIO_REQ_TYPE_NORMAL /**< Normal timeslot request. */ +}; + +/** @brief Parameters for a request for a timeslot as early as possible. */ +typedef struct +{ + uint8_t hfclk; /**< High frequency clock source, see @ref NRF_RADIO_HFCLK_CFG. */ + uint8_t priority; /**< The radio timeslot priority, see @ref NRF_RADIO_PRIORITY. */ + uint32_t length_us; /**< The radio timeslot length (in the range 100 to 100,000] microseconds). */ + uint32_t timeout_us; /**< Longest acceptable delay until the start of the requested timeslot (up to @ref NRF_RADIO_EARLIEST_TIMEOUT_MAX_US microseconds). */ +} nrf_radio_request_earliest_t; + +/** @brief Parameters for a normal radio request. */ +typedef struct +{ + uint8_t hfclk; /**< High frequency clock source, see @ref NRF_RADIO_HFCLK_CFG. */ + uint8_t priority; /**< The radio timeslot priority, see @ref NRF_RADIO_PRIORITY. */ + uint32_t distance_us; /**< Distance from the start of the previous radio timeslot (up to @ref NRF_RADIO_DISTANCE_MAX_US microseconds). */ + uint32_t length_us; /**< The radio timeslot length (in the range [100..100,000] microseconds). */ +} nrf_radio_request_normal_t; + +/** @brief Radio request parameters. */ +typedef struct +{ + uint8_t request_type; /**< Type of request, see @ref NRF_RADIO_REQUEST_TYPE. */ + union + { + nrf_radio_request_earliest_t earliest; /**< Parameters for a request for a timeslot as early as possible. */ + nrf_radio_request_normal_t normal; /**< Parameters for a normal radio request. */ + } params; +} nrf_radio_request_t; + +/**@brief Return parameters of the radio timeslot signal callback. */ +typedef struct +{ + uint8_t callback_action; /**< The action requested by the application when returning from the signal callback, see @ref NRF_RADIO_SIGNAL_CALLBACK_ACTION. */ + union + { + struct + { + nrf_radio_request_t * p_next; /**< The request parameters for the next radio timeslot. */ + } request; /**< Additional parameters for return_code @ref NRF_RADIO_SIGNAL_CALLBACK_ACTION_REQUEST_AND_END. */ + struct + { + uint32_t length_us; /**< Requested extension of the timeslot duration (microseconds) (for minimum time see @ref NRF_RADIO_MINIMUM_TIMESLOT_LENGTH_EXTENSION_TIME_US). */ + } extend; /**< Additional parameters for return_code @ref NRF_RADIO_SIGNAL_CALLBACK_ACTION_EXTEND. */ + } params; +} nrf_radio_signal_callback_return_param_t; + +/**@brief The radio signal callback type. + * + * @note In case of invalid return parameters, the radio timeslot will automatically end + * immediately after returning from the signal callback and the + * @ref NRF_EVT_RADIO_SIGNAL_CALLBACK_INVALID_RETURN event will be sent. + * @note The returned struct pointer must remain valid after the signal callback + * function returns. For instance, this means that it must not point to a stack variable. + * + * @param[in] signal_type Type of signal, see @ref NRF_RADIO_CALLBACK_SIGNAL_TYPE. + * + * @return Pointer to structure containing action requested by the application. + */ +typedef nrf_radio_signal_callback_return_param_t * (*nrf_radio_signal_callback_t) (uint8_t signal_type); + +/**@brief AES ECB data structure */ +typedef struct +{ + uint8_t key[SOC_ECB_KEY_LENGTH]; /**< Encryption key. */ + uint8_t cleartext[SOC_ECB_CLEARTEXT_LENGTH]; /**< Clear Text data. */ + uint8_t ciphertext[SOC_ECB_CIPHERTEXT_LENGTH]; /**< Cipher Text data. */ +} nrf_ecb_hal_data_t; + +/** @} */ + +/** @addtogroup NRF_SOC_FUNCTIONS Functions + * @{ */ + +/**@brief Initialize a mutex. + * + * @param[in] p_mutex Pointer to the mutex to initialize. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_MUTEX_NEW, uint32_t, sd_mutex_new(nrf_mutex_t * p_mutex)); + +/**@brief Attempt to acquire a mutex. + * + * @param[in] p_mutex Pointer to the mutex to acquire. + * + * @retval ::NRF_SUCCESS The mutex was successfully acquired. + * @retval ::NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN The mutex could not be acquired. + */ +SVCALL(SD_MUTEX_ACQUIRE, uint32_t, sd_mutex_acquire(nrf_mutex_t * p_mutex)); + +/**@brief Release a mutex. + * + * @param[in] p_mutex Pointer to the mutex to release. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_MUTEX_RELEASE, uint32_t, sd_mutex_release(nrf_mutex_t * p_mutex)); + +/**@brief Enable External Interrupt. + * @note Corresponds to NVIC_EnableIRQ in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_EnableIRQ documentation in CMSIS. + * + * @retval ::NRF_SUCCESS The interrupt was enabled. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE The interrupt is not available for the application. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED The interrupt has a priority not available for the application. + */ +SVCALL(SD_NVIC_ENABLEIRQ, uint32_t, sd_nvic_EnableIRQ(IRQn_Type IRQn)); + +/**@brief Disable External Interrupt. + * @note Corresponds to NVIC_DisableIRQ in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_DisableIRQ documentation in CMSIS + * + * @retval ::NRF_SUCCESS The interrupt was disabled. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE The interrupt is not available for the application. + */ +SVCALL(SD_NVIC_DISABLEIRQ, uint32_t, sd_nvic_DisableIRQ(IRQn_Type IRQn)); + +/**@brief Get Pending Interrupt. + * @note Corresponds to NVIC_GetPendingIRQ in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_GetPendingIRQ documentation in CMSIS. + * @param[out] p_pending_irq Return value from NVIC_GetPendingIRQ. + * + * @retval ::NRF_SUCCESS The interrupt is available for the application. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application. + */ +SVCALL(SD_NVIC_GETPENDINGIRQ, uint32_t, sd_nvic_GetPendingIRQ(IRQn_Type IRQn, uint32_t * p_pending_irq)); + +/**@brief Set Pending Interrupt. + * @note Corresponds to NVIC_SetPendingIRQ in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_SetPendingIRQ documentation in CMSIS. + * + * @retval ::NRF_SUCCESS The interrupt is set pending. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application. + */ +SVCALL(SD_NVIC_SETPENDINGIRQ, uint32_t, sd_nvic_SetPendingIRQ(IRQn_Type IRQn)); + +/**@brief Clear Pending Interrupt. + * @note Corresponds to NVIC_ClearPendingIRQ in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_ClearPendingIRQ documentation in CMSIS. + * + * @retval ::NRF_SUCCESS The interrupt pending flag is cleared. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application. + */ +SVCALL(SD_NVIC_CLEARPENDINGIRQ, uint32_t, sd_nvic_ClearPendingIRQ(IRQn_Type IRQn)); + +/**@brief Set Interrupt Priority. + * @note Corresponds to NVIC_SetPriority in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * @pre{priority is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_SetPriority documentation in CMSIS. + * @param[in] priority A valid IRQ priority for use by the application. + * + * @retval ::NRF_SUCCESS The interrupt and priority level is available for the application. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE IRQn is not available for the application. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_PRIORITY_NOT_ALLOWED The interrupt priority is not available for the application. + */ +SVCALL(SD_NVIC_SETPRIORITY, uint32_t, sd_nvic_SetPriority(IRQn_Type IRQn, nrf_app_irq_priority_t priority)); + +/**@brief Get Interrupt Priority. + * @note Corresponds to NVIC_GetPriority in CMSIS. + * + * @pre{IRQn is valid and not reserved by the stack} + * + * @param[in] IRQn See the NVIC_GetPriority documentation in CMSIS. + * @param[out] p_priority Return value from NVIC_GetPriority. + * + * @retval ::NRF_SUCCESS The interrupt priority is returned in p_priority. + * @retval ::NRF_ERROR_SOC_NVIC_INTERRUPT_NOT_AVAILABLE - IRQn is not available for the application. + */ +SVCALL(SD_NVIC_GETPRIORITY, uint32_t, sd_nvic_GetPriority(IRQn_Type IRQn, nrf_app_irq_priority_t * p_priority)); + +/**@brief System Reset. + * @note Corresponds to NVIC_SystemReset in CMSIS. + * + * @retval ::NRF_ERROR_SOC_NVIC_SHOULD_NOT_RETURN + */ +SVCALL(SD_NVIC_SYSTEMRESET, uint32_t, sd_nvic_SystemReset(void)); + +/**@brief Enters critical region. + * + * @post Application interrupts will be disabled. + * @sa sd_nvic_critical_region_exit + * + * @param[out] p_is_nested_critical_region 1: If in a nested critical region. + * 0: Otherwise. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_NVIC_CRITICAL_REGION_ENTER, uint32_t, sd_nvic_critical_region_enter(uint8_t * p_is_nested_critical_region)); + +/**@brief Exit critical region. + * + * @pre Application has entered a critical region using ::sd_nvic_critical_region_enter. + * @post If not in a nested critical region, the application interrupts will restored to the state before ::sd_nvic_critical_region_enter was called. + * + * @param[in] is_nested_critical_region If this is set to 1, the critical region won't be exited. @sa sd_nvic_critical_region_enter. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_NVIC_CRITICAL_REGION_EXIT, uint32_t, sd_nvic_critical_region_exit(uint8_t is_nested_critical_region)); + +/**@brief Query the capacity of the application random pool. + * + * @param[out] p_pool_capacity The capacity of the pool. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_RAND_APPLICATION_POOL_CAPACITY, uint32_t, sd_rand_application_pool_capacity_get(uint8_t * p_pool_capacity)); + +/**@brief Get number of random bytes available to the application. + * + * @param[out] p_bytes_available The number of bytes currently available in the pool. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_RAND_APPLICATION_BYTES_AVAILABLE, uint32_t, sd_rand_application_bytes_available_get(uint8_t * p_bytes_available)); + +/**@brief Get random bytes from the application pool. + * + * @param[out] p_buff Pointer to unit8_t buffer for storing the bytes. + * @param[in] length Number of bytes to take from pool and place in p_buff. + * + * @retval ::NRF_SUCCESS The requested bytes were written to p_buff. + * @retval ::NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES No bytes were written to the buffer, because there were not enough bytes available. +*/ +SVCALL(SD_RAND_APPLICATION_GET_VECTOR, uint32_t, sd_rand_application_vector_get(uint8_t * p_buff, uint8_t length)); + +/**@brief Gets the reset reason register. + * + * @param[out] p_reset_reason Contents of the NRF_POWER->RESETREAS register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_RESET_REASON_GET, uint32_t, sd_power_reset_reason_get(uint32_t * p_reset_reason)); + +/**@brief Clears the bits of the reset reason register. + * + * @param[in] reset_reason_clr_msk Contains the bits to clear from the reset reason register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_RESET_REASON_CLR, uint32_t, sd_power_reset_reason_clr(uint32_t reset_reason_clr_msk)); + +/**@brief Sets the power mode when in CPU sleep. + * + * @param[in] power_mode The power mode to use when in CPU sleep. @sa sd_app_evt_wait + * + * @retval ::NRF_SUCCESS The power mode was set. + * @retval ::NRF_ERROR_SOC_POWER_MODE_UNKNOWN The power mode was unknown. + */ +SVCALL(SD_POWER_MODE_SET, uint32_t, sd_power_mode_set(nrf_power_mode_t power_mode)); + +/**@brief Puts the chip in System OFF mode. + * + * @retval ::NRF_ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN + */ +SVCALL(SD_POWER_SYSTEM_OFF, uint32_t, sd_power_system_off(void)); + +/**@brief Enables or disables the power-fail comparator. + * + * Enabling this will give a softdevice event (NRF_EVT_POWER_FAILURE_WARNING) when the power failure warning occurs. + * The event can be retrieved with sd_evt_get(); + * + * @param[in] pof_enable True if the power-fail comparator should be enabled, false if it should be disabled. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_POF_ENABLE, uint32_t, sd_power_pof_enable(uint8_t pof_enable)); + +/**@brief Sets the power-fail threshold value. + * + * @param[in] threshold The power-fail threshold value to use. + * + * @retval ::NRF_SUCCESS The power failure threshold was set. + * @retval ::NRF_ERROR_SOC_POWER_POF_THRESHOLD_UNKNOWN The power failure threshold is unknown. + */ +SVCALL(SD_POWER_POF_THRESHOLD_SET, uint32_t, sd_power_pof_threshold_set(nrf_power_failure_threshold_t threshold)); + +/**@brief Sets bits in the NRF_POWER->RAMON register. + * + * @param[in] ramon Contains the bits needed to be set in the NRF_POWER->RAMON register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_RAMON_SET, uint32_t, sd_power_ramon_set(uint32_t ramon)); + +/** @brief Clears bits in the NRF_POWER->RAMON register. + * + * @param ramon Contains the bits needed to be cleared in the NRF_POWER->RAMON register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_RAMON_CLR, uint32_t, sd_power_ramon_clr(uint32_t ramon)); + +/**@brief Get contents of NRF_POWER->RAMON register, indicates power status of ram blocks. + * + * @param[out] p_ramon Content of NRF_POWER->RAMON register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_RAMON_GET, uint32_t, sd_power_ramon_get(uint32_t * p_ramon)); + +/**@brief Set bits in the NRF_POWER->GPREGRET register. + * + * @param[in] gpregret_msk Bits to be set in the GPREGRET register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_GPREGRET_SET, uint32_t, sd_power_gpregret_set(uint32_t gpregret_msk)); + +/**@brief Clear bits in the NRF_POWER->GPREGRET register. + * + * @param[in] gpregret_msk Bits to be clear in the GPREGRET register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_GPREGRET_CLR, uint32_t, sd_power_gpregret_clr(uint32_t gpregret_msk)); + +/**@brief Get contents of the NRF_POWER->GPREGRET register. + * + * @param[out] p_gpregret Contents of the GPREGRET register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_POWER_GPREGRET_GET, uint32_t, sd_power_gpregret_get(uint32_t *p_gpregret)); + +/**@brief Sets the DCDC mode. + * + * Depending on the internal state of the SoftDevice, the mode change may not happen immediately. + * The DCDC mode switch will be blocked when occurring in close proximity to radio transmissions. When + * the radio transmission is done, the last mode will be used. + * + * @param[in] dcdc_mode The mode of the DCDC. + * + * @retval ::NRF_SUCCESS + * @retval ::NRF_ERROR_INVALID_PARAM The DCDC mode is invalid. + */ +SVCALL(SD_POWER_DCDC_MODE_SET, uint32_t, sd_power_dcdc_mode_set(nrf_power_dcdc_mode_t dcdc_mode)); + +/**@brief Request the high frequency crystal oscillator. + * + * Will start the high frequency crystal oscillator, the startup time of the crystal varies + * and the ::sd_clock_hfclk_is_running function can be polled to check if it has started. + * + * @see sd_clock_hfclk_is_running + * @see sd_clock_hfclk_release + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_CLOCK_HFCLK_REQUEST, uint32_t, sd_clock_hfclk_request(void)); + +/**@brief Releases the high frequency crystal oscillator. + * + * Will stop the high frequency crystal oscillator, this happens immediately. + * + * @see sd_clock_hfclk_is_running + * @see sd_clock_hfclk_request + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_CLOCK_HFCLK_RELEASE, uint32_t, sd_clock_hfclk_release(void)); + +/**@brief Checks if the high frequency crystal oscillator is running. + * + * @see sd_clock_hfclk_request + * @see sd_clock_hfclk_release + * + * @param[out] p_is_running 1 if the external crystal oscillator is running, 0 if not. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_CLOCK_HFCLK_IS_RUNNING, uint32_t, sd_clock_hfclk_is_running(uint32_t * p_is_running)); + +/**@brief Waits for an application event. + * + * An application event is either an application interrupt or a pended interrupt when the + * interrupt is disabled. When the interrupt is enabled it will be taken immediately since + * this function will wait in thread mode, then the execution will return in the application's + * main thread. When an interrupt is disabled and gets pended it will return to the application's + * thread main. The application must ensure that the pended flag is cleared using + * ::sd_nvic_ClearPendingIRQ in order to sleep using this function. This is only necessary for + * disabled interrupts, as the interrupt handler will clear the pending flag automatically for + * enabled interrupts. + * + * In order to wake up from disabled interrupts, the SEVONPEND flag has to be set in the Cortex-M0 + * System Control Register (SCR). @sa CMSIS_SCB + * + * @note If an application interrupt has happened since the last time sd_app_evt_wait was + * called this function will return immediately and not go to sleep. This is to avoid race + * conditions that can occur when a flag is updated in the interrupt handler and processed + * in the main loop. + * + * @post An application interrupt has happened or a interrupt pending flag is set. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_APP_EVT_WAIT, uint32_t, sd_app_evt_wait(void)); + +/**@brief Get PPI channel enable register contents. + * + * @param[out] p_channel_enable The contents of the PPI CHEN register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_CHANNEL_ENABLE_GET, uint32_t, sd_ppi_channel_enable_get(uint32_t * p_channel_enable)); + +/**@brief Set PPI channel enable register. + * + * @param[in] channel_enable_set_msk Mask containing the bits to set in the PPI CHEN register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_CHANNEL_ENABLE_SET, uint32_t, sd_ppi_channel_enable_set(uint32_t channel_enable_set_msk)); + +/**@brief Clear PPI channel enable register. + * + * @param[in] channel_enable_clr_msk Mask containing the bits to clear in the PPI CHEN register. + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_CHANNEL_ENABLE_CLR, uint32_t, sd_ppi_channel_enable_clr(uint32_t channel_enable_clr_msk)); + +/**@brief Assign endpoints to a PPI channel. + * + * @param[in] channel_num Number of the PPI channel to assign. + * @param[in] evt_endpoint Event endpoint of the PPI channel. + * @param[in] task_endpoint Task endpoint of the PPI channel. + * + * @retval ::NRF_ERROR_SOC_PPI_INVALID_CHANNEL The channel number is invalid. + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_CHANNEL_ASSIGN, uint32_t, sd_ppi_channel_assign(uint8_t channel_num, const volatile void * evt_endpoint, const volatile void * task_endpoint)); + +/**@brief Task to enable a channel group. + * + * @param[in] group_num Number of the channel group. + * + * @retval ::NRF_ERROR_SOC_PPI_INVALID_GROUP The group number is invalid + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_GROUP_TASK_ENABLE, uint32_t, sd_ppi_group_task_enable(uint8_t group_num)); + +/**@brief Task to disable a channel group. + * + * @param[in] group_num Number of the PPI group. + * + * @retval ::NRF_ERROR_SOC_PPI_INVALID_GROUP The group number is invalid. + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_GROUP_TASK_DISABLE, uint32_t, sd_ppi_group_task_disable(uint8_t group_num)); + +/**@brief Assign PPI channels to a channel group. + * + * @param[in] group_num Number of the channel group. + * @param[in] channel_msk Mask of the channels to assign to the group. + * + * @retval ::NRF_ERROR_SOC_PPI_INVALID_GROUP The group number is invalid. + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_GROUP_ASSIGN, uint32_t, sd_ppi_group_assign(uint8_t group_num, uint32_t channel_msk)); + +/**@brief Gets the PPI channels of a channel group. + * + * @param[in] group_num Number of the channel group. + * @param[out] p_channel_msk Mask of the channels assigned to the group. + * + * @retval ::NRF_ERROR_SOC_PPI_INVALID_GROUP The group number is invalid. + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_PPI_GROUP_GET, uint32_t, sd_ppi_group_get(uint8_t group_num, uint32_t * p_channel_msk)); + +/**@brief Configures the Radio Notification signal. + * + * @note + * - The notification signal latency depends on the interrupt priority settings of SWI used + * for notification signal. + * - In the period between the ACTIVE signal and the start of the Radio Event, the SoftDevice + * will interrupt the application to do Radio Event preparation. + * - Using the Radio Notification feature may limit the bandwidth, as the SoftDevice may have + * to shorten the connection events to have time for the Radio Notification signals. + * + * @param[in] type Type of notification signal. + * @ref NRF_RADIO_NOTIFICATION_TYPE_NONE shall be used to turn off radio + * notification. Using @ref NRF_RADIO_NOTIFICATION_DISTANCE_NONE is + * recommended (but not required) to be used with + * @ref NRF_RADIO_NOTIFICATION_TYPE_NONE. + * + * @param[in] distance Distance between the notification signal and start of radio activity. + * This parameter is ignored when @ref NRF_RADIO_NOTIFICATION_TYPE_NONE or + * @ref NRF_RADIO_NOTIFICATION_TYPE_INT_ON_INACTIVE is used. + * + * @retval ::NRF_ERROR_INVALID_PARAM The group number is invalid. + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_RADIO_NOTIFICATION_CFG_SET, uint32_t, sd_radio_notification_cfg_set(nrf_radio_notification_type_t type, nrf_radio_notification_distance_t distance)); + +/**@brief Encrypts a block according to the specified parameters. + * + * 128-bit AES encryption. + * + * @param[in, out] p_ecb_data Pointer to the ECB parameters' struct (two input + * parameters and one output parameter). + * + * @retval ::NRF_SUCCESS + */ +SVCALL(SD_ECB_BLOCK_ENCRYPT, uint32_t, sd_ecb_block_encrypt(nrf_ecb_hal_data_t * p_ecb_data)); + +/**@brief Gets any pending events generated by the SoC API. + * + * The application should keep calling this function to get events, until ::NRF_ERROR_NOT_FOUND is returned. + * + * @param[out] p_evt_id Set to one of the values in @ref NRF_SOC_EVTS, if any events are pending. + * + * @retval ::NRF_SUCCESS An event was pending. The event id is written in the p_evt_id parameter. + * @retval ::NRF_ERROR_NOT_FOUND No pending events. + */ +SVCALL(SD_EVT_GET, uint32_t, sd_evt_get(uint32_t * p_evt_id)); + +/**@brief Get the temperature measured on the chip + * + * This function will block until the temperature measurement is done. + * It takes around 50us from call to return. + * + * @note Pan #28 in PAN-028 v 1.6 "Negative measured values are not represented correctly" is corrected by this function. + * + * @param[out] p_temp Result of temperature measurement. Die temperature in 0.25 degrees celsius. + * + * @retval ::NRF_SUCCESS A temperature measurement was done, and the temperature was written to temp + */ +SVCALL(SD_TEMP_GET, uint32_t, sd_temp_get(int32_t * p_temp)); + +/**@brief Flash Write + * + * Commands to write a buffer to flash + * + * This call initiates the flash access command, and its completion will be communicated to the + * application with exactly one of the following events: + * - NRF_EVT_FLASH_OPERATION_SUCCESS - The command was successfully completed. + * - NRF_EVT_FLASH_OPERATION_ERROR - The command could not be started. + * + * @note + * - This call takes control over the radio and the CPU during flash erase and write to make sure that + * they will not interfere with the flash access. This means that all interrupts will be blocked + * for a predictable time (depending on the NVMC specification in nRF51 Series Reference Manual + * and the command parameters). + * + * + * @param[in] p_dst Pointer to start of flash location to be written. + * @param[in] p_src Pointer to buffer with data to be written + * @param[in] size Number of 32-bit words to write. Maximum size is 256 32bit words. + * + * @retval ::NRF_ERROR_INVALID_ADDR Tried to write to a non existing flash address, or p_dst or p_src was unaligned. + * @retval ::NRF_ERROR_BUSY The previous command has not yet completed. + * @retval ::NRF_ERROR_INVALID_LENGTH Size was 0, or more than 256 words. + * @retval ::NRF_ERROR_FORBIDDEN Tried to write to or read from protected location. + * @retval ::NRF_SUCCESS The command was accepted. + */ +SVCALL(SD_FLASH_WRITE, uint32_t, sd_flash_write(uint32_t * const p_dst, uint32_t const * const p_src, uint32_t size)); + + +/**@brief Flash Erase page + * + * Commands to erase a flash page + * + * This call initiates the flash access command, and its completion will be communicated to the + * application with exactly one of the following events: + * - NRF_EVT_FLASH_OPERATION_SUCCESS - The command was successfully completed. + * - NRF_EVT_FLASH_OPERATION_ERROR - The command could not be started. + * + * @note + * - This call takes control over the radio and the CPU during flash erase and write to make sure that + * they will not interfere with the flash access. This means that all interrupts will be blocked + * for a predictable time (depending on the NVMC specification in nRF51 Series Reference Manual + * and the command parameters). + * + * + * @param[in] page_number Pagenumber of the page to erase + * @retval ::NRF_ERROR_INTERNAL If a new session could not be opened due to an internal error. + * @retval ::NRF_ERROR_INVALID_ADDR Tried to erase to a non existing flash page. + * @retval ::NRF_ERROR_BUSY The previous command has not yet completed. + * @retval ::NRF_ERROR_FORBIDDEN Tried to erase a protected page. + * @retval ::NRF_SUCCESS The command was accepted. + */ +SVCALL(SD_FLASH_PAGE_ERASE, uint32_t, sd_flash_page_erase(uint32_t page_number)); + + +/**@brief Flash Protection set + * + * Commands to set the flash protection registers PROTENSETx + * + * @note To read the values in PROTENSETx you can read them directly. They are only write-protected. + * + * @param[in] protenset0 Value to be written to PROTENSET0 + * @param[in] protenset1 Value to be written to PROTENSET1 + * + * @retval ::NRF_ERROR_FORBIDDEN Tried to protect the SoftDevice + * @retval ::NRF_SUCCESS Values successfully written to PROTENSETx + */ +SVCALL(SD_FLASH_PROTECT, uint32_t, sd_flash_protect(uint32_t protenset0, uint32_t protenset1)); + +/**@brief Opens a session for radio requests. + * + * @note Only one session can be open at a time. + * @note p_radio_signal_callback(NRF_RADIO_CALLBACK_SIGNAL_TYPE_START) will be called when the radio timeslot + * starts. From this point the NRF_RADIO and NRF_TIMER0 peripherals can be freely accessed + * by the application. + * @note p_radio_signal_callback(NRF_RADIO_CALLBACK_SIGNAL_TYPE_TIMER0) is called whenever the NRF_TIMER0 + * interrupt occurs. + * @note p_radio_signal_callback(NRF_RADIO_CALLBACK_SIGNAL_TYPE_RADIO) is called whenever the NRF_RADIO + * interrupt occurs. + * @note p_radio_signal_callback() will be called at ARM interrupt priority level 0. This + * implies that none of the sd_* API calls can be used from p_radio_signal_callback(). + * + * @param[in] p_radio_signal_callback The signal callback. + * + * @retval ::NRF_ERROR_INVALID_ADDR p_radio_signal_callback is an invalid function pointer. + * @retval ::NRF_ERROR_BUSY If session cannot be opened. + * @retval ::NRF_ERROR_INTERNAL If a new session could not be opened due to an internal error. + * @retval ::NRF_SUCCESS Otherwise. + */ + SVCALL(SD_RADIO_SESSION_OPEN, uint32_t, sd_radio_session_open(nrf_radio_signal_callback_t p_radio_signal_callback)); + +/**@brief Closes a session for radio requests. + * + * @note Any current radio timeslot will be finished before the session is closed. + * @note If a radio timeslot is scheduled when the session is closed, it will be canceled. + * @note The application cannot consider the session closed until the NRF_EVT_RADIO_SESSION_CLOSED + * event is received. + * + * @retval ::NRF_ERROR_FORBIDDEN If session not opened. + * @retval ::NRF_ERROR_BUSY If session is currently being closed. + * @retval ::NRF_SUCCESS Otherwise. + */ + SVCALL(SD_RADIO_SESSION_CLOSE, uint32_t, sd_radio_session_close(void)); + + /**@brief Requests a radio timeslot. + * + * @note The timing of the radio timeslot is specified by p_request->distance_us. For the first + * request in a session, p_request->distance_us is required to be 0 by convention, and + * the timeslot is scheduled at the first possible opportunity. All following radio timeslots are + * requested with a distance of p_request->distance_us measured from the start of the + * previous radio timeslot. + * @note A too small p_request->distance_us will lead to a NRF_EVT_RADIO_BLOCKED event. + * @note Timeslots scheduled too close will lead to a NRF_EVT_RADIO_BLOCKED event. + * @note See the SoftDevice Specification for more on radio timeslot scheduling, distances and lengths. + * @note If an opportunity for the first radio timeslot is not found before 100ms after the call to this + * function, it is not scheduled, and instead a NRF_EVT_RADIO_BLOCKED event is sent. + * The application may then try to schedule the first radio timeslot again. + * @note Successful requests will result in nrf_radio_signal_callback_t(NRF_RADIO_CALLBACK_SIGNAL_TYPE_START). + * Unsuccessful requests will result in a NRF_EVT_RADIO_BLOCKED event, see @ref NRF_SOC_EVTS. + * @note The jitter in the start time of the radio timeslots is +/- NRF_RADIO_START_JITTER_US us. + * @note The nrf_radio_signal_callback_t(NRF_RADIO_CALLBACK_SIGNAL_TYPE_START) call has a latency relative to the + * specified radio timeslot start, but this does not affect the actual start time of the timeslot. + * @note NRF_TIMER0 is reset at the start of the radio timeslot, and is clocked at 1MHz from the high frequency + * (16 MHz) clock source. If p_request->hfclk_force_xtal is true, the high frequency clock is + * guaranteed to be clocked from the external crystal. + * @note The SoftDevice will neither access the NRF_RADIO peripheral nor the NRF_TIMER0 peripheral + * during the radio timeslot. + * + * @param[in] p_request Pointer to the request parameters. + * + * @retval ::NRF_ERROR_FORBIDDEN If session not opened or the session is not IDLE. + * @retval ::NRF_ERROR_INVALID_ADDR If the p_request pointer is invalid. + * @retval ::NRF_ERROR_INVALID_PARAM If the parameters of p_request are not valid. + * @retval ::NRF_SUCCESS Otherwise. + */ + SVCALL(SD_RADIO_REQUEST, uint32_t, sd_radio_request(nrf_radio_request_t * p_request )); + +/** @} */ + +#endif // NRF_SOC_H__ + +/**@} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/nrf_svc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NRF_SVC__ +#define NRF_SVC__ + +#define SVCALL(number, return_type, signature) return_type signature +#ifdef SVCALL_AS_NORMAL_FUNCTION + +#else + +#ifndef SVCALL +#if defined (__CC_ARM) +#define SVCALL(number, return_type, signature) return_type __svc(number) signature +#elif defined (__GNUC__) +#define SVCALL(number, return_type, signature) \ + _Pragma("GCC diagnostic ignored \"-Wreturn-type\"") \ + _Pragma("GCC diagnostic ignored \"-Wunused-function\"") \ + __attribute__((naked)) static return_type signature \ + { \ + __asm( \ + "svc %0\n" \ + "bx r14" : : "I" (number) : "r0" \ + ); \ + } +#elif defined (__ICCARM__) +#define PRAGMA(x) _Pragma(#x) +#define SVCALL(number, return_type, signature) \ +PRAGMA(swi_number = number) \ + __swi return_type signature; +#else +#define SVCALL(number, return_type, signature) return_type signature +#endif +#endif // SVCALL + +#endif // SVCALL_AS_NORMAL_FUNCTION +#endif // NRF_SVC__ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/ser_nrf_soc.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,145 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + @defgroup nrf_soc_api SoC Library API + @{ + + @brief APIs for the SoC library. + +*/ + +#ifndef NRF_SOC_H__ +#define NRF_SOC_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "nrf_svc.h" +/** @addtogroup NRF_SOC_DEFINES Defines + * @{ */ + +/**@brief The number of the lowest SVC number reserved for the SoC library. */ +#define SOC_SVC_BASE 0x20 + + +/** @} */ + +/** @addtogroup NRF_SOC_TYPES Types + * @{ */ + +/**@brief The SVC numbers used by the SVC functions in the SoC library. */ +enum NRF_SOC_SVCS +{ + SD_MUTEX_NEW = SOC_SVC_BASE, + SD_MUTEX_ACQUIRE, + SD_MUTEX_RELEASE, + SD_NVIC_ENABLEIRQ, + SD_NVIC_DISABLEIRQ, + SD_NVIC_GETPENDINGIRQ, + SD_NVIC_SETPENDINGIRQ, + SD_NVIC_CLEARPENDINGIRQ, + SD_NVIC_SETPRIORITY, + SD_NVIC_GETPRIORITY, + SD_NVIC_SYSTEMRESET, + SD_NVIC_CRITICAL_REGION_ENTER, + SD_NVIC_CRITICAL_REGION_EXIT, + SD_RAND_APPLICATION_POOL_CAPACITY, + SD_RAND_APPLICATION_BYTES_AVAILABLE, + SD_RAND_APPLICATION_GET_VECTOR, + SD_POWER_MODE_SET, + SD_POWER_SYSTEM_OFF, + SD_POWER_RESET_REASON_GET, + SD_POWER_RESET_REASON_CLR, + SD_POWER_POF_ENABLE, + SD_POWER_POF_THRESHOLD_SET, + SD_POWER_RAMON_SET, + SD_POWER_RAMON_CLR, + SD_POWER_RAMON_GET, + SD_POWER_GPREGRET_SET, + SD_POWER_GPREGRET_CLR, + SD_POWER_GPREGRET_GET, + SD_POWER_DCDC_MODE_SET, + SD_APP_EVT_WAIT, + SD_CLOCK_HFCLK_REQUEST, + SD_CLOCK_HFCLK_RELEASE, + SD_CLOCK_HFCLK_IS_RUNNING, + SD_PPI_CHANNEL_ENABLE_GET, + SD_PPI_CHANNEL_ENABLE_SET, + SD_PPI_CHANNEL_ENABLE_CLR, + SD_PPI_CHANNEL_ASSIGN, + SD_PPI_GROUP_TASK_ENABLE, + SD_PPI_GROUP_TASK_DISABLE, + SD_PPI_GROUP_ASSIGN, + SD_PPI_GROUP_GET, + SD_RADIO_NOTIFICATION_CFG_SET, + SD_ECB_BLOCK_ENCRYPT, + SD_RESERVED1, + SD_RESERVED2, + SD_RESERVED3, + SD_EVT_GET, + SD_TEMP_GET, + SD_FLASH_ERASE_PAGE, + SD_FLASH_WRITE, + SD_FLASH_PROTECT, + SVC_SOC_LAST +}; + +/**@brief Puts the chip in System OFF mode. + * + * @retval ::NRF_ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN + */ +SVCALL(SD_POWER_SYSTEM_OFF, uint32_t, sd_power_system_off(void)); + +/**@brief Get the temperature measured on the chip + * + * This function will block until the temperature measurement is done. + * It takes around 50us from call to return. + * + * @note Pan #28 in PAN-028 v 1.6 "Negative measured values are not represented correctly" is corrected by this function. + * + * @param[out] p_temp Result of temperature measurement. Die temperature in 0.25 degrees celsius. + * + * @retval ::NRF_SUCCESS A temperature measurement was done, and the temperature was written to temp + */ +SVCALL(SD_TEMP_GET, uint32_t, sd_temp_get(int32_t * p_temp)); + + +/** @} */ + +#endif // NRF_SOC_H__ + +/** + @} + */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/inc/softdevice_assert.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,72 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @brief Utilities for verifying program logic + */ + +#ifndef SOFTDEVICE_ASSERT_H_ +#define SOFTDEVICE_ASSERT_H_ + +#include <stdint.h> + +/** @brief This function handles assertions. + * + * + * @note + * This function is called when an assertion has triggered. + * + * + * @param line_num The line number where the assertion is called + * @param file_name Pointer to the file name + */ +void assert_softdevice_callback(uint16_t line_num, const uint8_t *file_name); + + +/*lint -emacro(506, ASSERT) */ /* Suppress "Constant value Boolean */ +/*lint -emacro(774, ASSERT) */ /* Suppress "Boolean within 'if' always evaluates to True" */ \ +/** @brief Check intended for production code + * + * Check passes if "expr" evaluates to true. */ +#define ASSERT(expr) \ +if (expr) \ +{ \ +} \ +else \ +{ \ + assert_softdevice_callback((uint16_t)__LINE__, (uint8_t *)__FILE__); \ + /*lint -unreachable */ \ +} + +#endif /* SOFTDEVICE_ASSERT_H_ */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,479 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble.h" +#include "ble_rpc_defines.h" +#include <stdint.h> +#include "ble_app.h" +#include "ble_encode_transport.h" +#include "hal_transport_config.h" +#include "app_error.h" + +/**@brief Structure containing @ref sd_ble_uuid_encode output parameters. */ +typedef struct +{ + uint8_t * p_uuid_le_len; /**< @ref sd_ble_uuid_encode appearance p_uuid_le_len output parameter. */ + uint8_t * p_uuid_le; /**< @ref sd_ble_uuid_encode appearance p_uuid_le output parameter. */ +} ble_uuid_encode_out_params_t; + +/**@brief Structure containing @ref sd_ble_tx_buffer_count_get output parameters. */ +typedef struct +{ + uint8_t * p_count; /**< @ref sd_ble_tx_buffer_count_get p_count output parameter. */ +} ble_tx_buffer_count__get_out_params_t; + +/**@brief Union containing BLE command output parameters. */ +typedef union +{ + ble_uuid_encode_out_params_t ble_uuid_encode_out_params; /**< @ref sd_ble_uuid_encode output parameters. */ + ble_tx_buffer_count__get_out_params_t ble_tx_buffer_count_get_out_params; /**< @ref sd_ble_uuid_encode output parameters. */ +} ble_command_output_params_t; + +static ble_command_output_params_t m_output_params; /**< BLE command output parameters. */ + +static void * mp_out_params[3]; + +/**@brief Command response callback function for @ref sd_ble_uuid_encode BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t uuid_encode_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_uuid_encode_rsp_dec(p_buffer, + length, + m_output_params.ble_uuid_encode_out_params.p_uuid_le_len, + m_output_params.ble_uuid_encode_out_params.p_uuid_le, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_uuid_encode(ble_uuid_t const * const p_uuid, + uint8_t * const p_uuid_le_len, + uint8_t * const p_uuid_le) +{ + m_output_params.ble_uuid_encode_out_params.p_uuid_le_len = p_uuid_le_len; + m_output_params.ble_uuid_encode_out_params.p_uuid_le = p_uuid_le; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_uuid_encode_req_enc(p_uuid, + p_uuid_le_len, + p_uuid_le, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + uuid_encode_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_tx_buffer_count_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t tx_buffer_count_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_tx_buffer_count_get_rsp_dec(p_buffer, + length, + (uint8_t **)&mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_tx_buffer_count_get(uint8_t* p_count) +{ + mp_out_params[0] = p_count; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_tx_buffer_count_get_req_enc(p_count, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + tx_buffer_count_get_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_uuid_vs_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t uuid_vs_add_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_uuid_vs_add_rsp_dec(p_buffer, + length, + (uint8_t **)&mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_uuid_vs_add(ble_uuid128_t const * const p_vs_uuid, uint8_t * const p_uuid_type) +{ + mp_out_params[0] = p_uuid_type; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_uuid_vs_add_req_enc(p_vs_uuid,p_uuid_type, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + uuid_vs_add_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_uuid_decode BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t uuid_decode_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_uuid_decode_rsp_dec(p_buffer, + length, + (ble_uuid_t **)&mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_uuid_decode(uint8_t uuid_le_len, uint8_t const * const p_uuid_le, ble_uuid_t * const p_uuid) +{ + mp_out_params[0] = p_uuid; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_uuid_decode_req_enc(uuid_le_len, p_uuid_le, p_uuid, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + uuid_decode_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_version_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t version_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_version_get_rsp_dec(p_buffer, + length, + (ble_version_t *)mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_version_get(ble_version_t * p_version) +{ + mp_out_params[0] = p_version; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_version_get_req_enc(p_version, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + version_get_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_enable BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t enable_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_enable_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_enable(ble_enable_params_t * p_ble_enable_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_enable_req_enc(p_ble_enable_params, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + enable_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_opt_set BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t opt_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_opt_set_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_opt_set(uint32_t opt_id, ble_opt_t const *p_opt) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_opt_set_req_enc(opt_id, p_opt, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + opt_set_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_opt_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t opt_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + uint32_t opt_id; + + const uint32_t err_code = ble_opt_get_rsp_dec(p_buffer, + length, + &opt_id, + (ble_opt_t *)mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + (void)opt_id; + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_opt_get(uint32_t opt_id, ble_opt_t *p_opt) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + mp_out_params[0] = p_opt; + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_opt_get_req_enc(opt_id, p_opt, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + opt_get_rsp_dec); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_advdata.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,661 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_advdata.h" +#include "app_error.h" +#include "ble_gap.h" +#include "ble_srv_common.h" +#include "blocking.h" + +// Offset from where advertisement data other than flags information can start. +#define ADV_FLAG_OFFSET 2 + +// Offset for Advertising Data. +// Offset is 2 as each Advertising Data contain 1 octet of Adveritising Data Type and +// one octet Advertising Data Length. +#define ADV_DATA_OFFSET 2 + +// NOTE: For now, Security Manager TK Value and Security Manager Out of Band Flags (OOB) are omitted +// from the advertising data. + + +static uint32_t name_encode(const ble_advdata_t * p_advdata, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint32_t err_code; + uint16_t rem_adv_data_len; + uint16_t actual_length; + uint8_t adv_data_format; + uint8_t adv_offset; + + adv_offset = *p_len; + + + // Check for buffer overflow. + if ((adv_offset + ADV_DATA_OFFSET > BLE_GAP_ADV_MAX_SIZE) || + ((p_advdata->short_name_len + ADV_DATA_OFFSET) > BLE_GAP_ADV_MAX_SIZE)) + { + return NRF_ERROR_DATA_SIZE; + } + actual_length = rem_adv_data_len = (BLE_GAP_ADV_MAX_SIZE - adv_offset - ADV_FLAG_OFFSET); + + // Get GAP device name and length + err_code = sd_ble_gap_device_name_get(&p_encoded_data[adv_offset + ADV_DATA_OFFSET], + &actual_length); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Check if device internd to use short name and it can fit available data size. + if ((p_advdata->name_type == BLE_ADVDATA_FULL_NAME) && (actual_length <= rem_adv_data_len)) + { + // Complete device name can fit, setting Complete Name in Adv Data. + adv_data_format = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME; + rem_adv_data_len = actual_length; + } + else + { + // Else short name needs to be used. Or application has requested use of short name. + adv_data_format = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME; + + // If application has set a preference on the short name size, it needs to be considered, + // else fit what can be fit. + if ((p_advdata->short_name_len != 0) && (p_advdata->short_name_len <= rem_adv_data_len)) + { + // Short name fits available size. + rem_adv_data_len = p_advdata->short_name_len; + } + // Else whatever can fit the data buffer will be packed. + else + { + rem_adv_data_len = actual_length; + } + } + + // Complete name field in encoded data. + p_encoded_data[adv_offset++] = rem_adv_data_len + 1; + p_encoded_data[adv_offset++] = adv_data_format; + (*p_len) += (rem_adv_data_len + ADV_DATA_OFFSET); + + return NRF_SUCCESS; +} + + +static uint32_t appearance_encode(uint8_t * p_encoded_data, uint8_t * p_len) +{ + uint32_t err_code; + uint16_t appearance; + + // Check for buffer overflow. + if ((*p_len) + 4 > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + // Get GAP appearance field. + err_code = sd_ble_gap_appearance_get(&appearance); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Encode Length, AD Type and Appearance. + p_encoded_data[(*p_len)++] = 3; + p_encoded_data[(*p_len)++] = BLE_GAP_AD_TYPE_APPEARANCE; + + (*p_len) += uint16_encode(appearance, &p_encoded_data[*p_len]); + + return NRF_SUCCESS; +} + + +static uint32_t uint8_array_encode(const uint8_array_t * p_uint8_array, + uint8_t adv_type, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + // Check parameter consistency. + if (p_uint8_array->p_data == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + + // Check for buffer overflow. + if ((*p_len) + ADV_DATA_OFFSET + p_uint8_array->size > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + // Encode Length and AD Type. + p_encoded_data[(*p_len)++] = 1 + p_uint8_array->size; + p_encoded_data[(*p_len)++] = adv_type; + + // Encode array. + memcpy(&p_encoded_data[*p_len], p_uint8_array->p_data, p_uint8_array->size); + (*p_len) += p_uint8_array->size; + + return NRF_SUCCESS; +} + + +static uint32_t tx_power_level_encode(int8_t tx_power_level, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + // Check for buffer overflow. + if ((*p_len) + 3 > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + // Encode TX Power Level. + p_encoded_data[(*p_len)++] = 2; + p_encoded_data[(*p_len)++] = BLE_GAP_AD_TYPE_TX_POWER_LEVEL; + p_encoded_data[(*p_len)++] = (uint8_t)tx_power_level; + + return NRF_SUCCESS; +} + + +static uint32_t uuid_list_sized_encode(const ble_advdata_uuid_list_t * p_uuid_list, + uint8_t adv_type, + uint8_t uuid_size, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + int i; + bool is_heading_written = false; + uint8_t start_pos = *p_len; + + for (i = 0; i < p_uuid_list->uuid_cnt; i++) + { + uint32_t err_code; + uint8_t encoded_size; + ble_uuid_t uuid = p_uuid_list->p_uuids[i]; + + // Find encoded uuid size. + err_code = sd_ble_uuid_encode(&uuid, &encoded_size, NULL); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Check size. + if (encoded_size == uuid_size) + { + uint8_t heading_bytes = (is_heading_written) ? 0 : 2; + + // Check for buffer overflow + if (*p_len + encoded_size + heading_bytes > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + if (!is_heading_written) + { + // Write AD structure heading. + (*p_len)++; + p_encoded_data[(*p_len)++] = adv_type; + is_heading_written = true; + } + + // Write UUID. + err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &p_encoded_data[*p_len]); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + (*p_len) += encoded_size; + } + } + + if (is_heading_written) + { + // Write length. + p_encoded_data[start_pos] = (*p_len) - (start_pos + 1); + } + + return NRF_SUCCESS; +} + + +static uint32_t uuid_list_encode(const ble_advdata_uuid_list_t * p_uuid_list, + uint8_t adv_type_16, + uint8_t adv_type_128, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint32_t err_code; + + // Encode 16 bit UUIDs. + err_code = uuid_list_sized_encode(p_uuid_list, + adv_type_16, + sizeof(uint16_le_t), + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Encode 128 bit UUIDs. + err_code = uuid_list_sized_encode(p_uuid_list, + adv_type_128, + sizeof(ble_uuid128_t), + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + return NRF_SUCCESS; +} + + +static uint32_t conn_int_check(const ble_advdata_conn_int_t *p_conn_int) +{ + // Check Minimum Connection Interval. + if ((p_conn_int->min_conn_interval < 0x0006) || + ( + (p_conn_int->min_conn_interval > 0x0c80) && + (p_conn_int->min_conn_interval != 0xffff) + ) + ) + { + return NRF_ERROR_INVALID_PARAM; + } + + // Check Maximum Connection Interval. + if ((p_conn_int->max_conn_interval < 0x0006) || + ( + (p_conn_int->max_conn_interval > 0x0c80) && + (p_conn_int->max_conn_interval != 0xffff) + ) + ) + { + return NRF_ERROR_INVALID_PARAM; + } + + // Make sure Minimum Connection Interval is not bigger than Maximum Connection Interval. + if ((p_conn_int->min_conn_interval != 0xffff) && + (p_conn_int->max_conn_interval != 0xffff) && + (p_conn_int->min_conn_interval > p_conn_int->max_conn_interval) + ) + { + return NRF_ERROR_INVALID_PARAM; + } + + return NRF_SUCCESS; +} + + +static uint32_t conn_int_encode(const ble_advdata_conn_int_t * p_conn_int, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint32_t err_code; + + // Check for buffer overflow. + if ((*p_len) + ADV_DATA_OFFSET + 2 * sizeof(uint16_le_t) > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + // Check parameters. + err_code = conn_int_check(p_conn_int); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Encode Length and AD Type. + p_encoded_data[(*p_len)++] = 1 + 2 * sizeof(uint16_le_t); + p_encoded_data[(*p_len)++] = BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE; + + // Encode Minimum and Maximum Connection Intervals. + (*p_len) += uint16_encode(p_conn_int->min_conn_interval, &p_encoded_data[*p_len]); + (*p_len) += uint16_encode(p_conn_int->max_conn_interval, &p_encoded_data[*p_len]); + + return NRF_SUCCESS; +} + + +static uint32_t manuf_specific_data_encode(const ble_advdata_manuf_data_t * p_manuf_sp_data, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint8_t data_size = sizeof(uint16_le_t) + p_manuf_sp_data->data.size; + + // Check for buffer overflow. + if ((*p_len) + ADV_DATA_OFFSET + data_size > BLE_GAP_ADV_MAX_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + // Encode Length and AD Type. + p_encoded_data[(*p_len)++] = 1 + data_size; + p_encoded_data[(*p_len)++] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA; + + // Encode Company Identifier. + (*p_len) += uint16_encode(p_manuf_sp_data->company_identifier, &p_encoded_data[*p_len]); + + // Encode additional manufacturer specific data. + if (p_manuf_sp_data->data.size > 0) + { + if (p_manuf_sp_data->data.p_data == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + memcpy(&p_encoded_data[*p_len], p_manuf_sp_data->data.p_data, p_manuf_sp_data->data.size); + (*p_len) += p_manuf_sp_data->data.size; + } + + return NRF_SUCCESS; +} + + +static uint32_t service_data_encode(const ble_advdata_t * p_advdata, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint8_t i; + + // Check parameter consistency. + if (p_advdata->p_service_data_array == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + + for (i = 0; i < p_advdata->service_data_count; i++) + { + ble_advdata_service_data_t * p_service_data; + uint8_t data_size; + + p_service_data = &p_advdata->p_service_data_array[i]; + data_size = sizeof(uint16_le_t) + p_service_data->data.size; + + // Encode Length and AD Type. + p_encoded_data[(*p_len)++] = 1 + data_size; + p_encoded_data[(*p_len)++] = BLE_GAP_AD_TYPE_SERVICE_DATA; + + // Encode service UUID. + (*p_len) += uint16_encode(p_service_data->service_uuid, &p_encoded_data[*p_len]); + + // Encode additional service data. + if (p_service_data->data.size > 0) + { + if (p_service_data->data.p_data == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + memcpy(&p_encoded_data[*p_len], p_service_data->data.p_data, p_service_data->data.size); + (*p_len) += p_service_data->data.size; + } + } + + return NRF_SUCCESS; +} + + +static uint32_t adv_data_encode(const ble_advdata_t * p_advdata, + uint8_t * p_encoded_data, + uint8_t * p_len) +{ + uint32_t err_code = NRF_SUCCESS; + + *p_len = 0; + + // Encode name. + if (p_advdata->name_type != BLE_ADVDATA_NO_NAME) + { + err_code = name_encode(p_advdata, p_encoded_data, p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode appearance. + if (p_advdata->include_appearance) + { + err_code = appearance_encode(p_encoded_data, p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode flags. + if (p_advdata->flags.size > 0) + { + err_code = uint8_array_encode(&p_advdata->flags, + BLE_GAP_AD_TYPE_FLAGS, + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode TX power level. + if (p_advdata->p_tx_power_level != NULL) + { + err_code = tx_power_level_encode(*p_advdata->p_tx_power_level, p_encoded_data, p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode 'more available' uuid list. + if (p_advdata->uuids_more_available.uuid_cnt > 0) + { + err_code = uuid_list_encode(&p_advdata->uuids_more_available, + BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, + BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode 'complete' uuid list. + if (p_advdata->uuids_complete.uuid_cnt > 0) + { + err_code = uuid_list_encode(&p_advdata->uuids_complete, + BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE, + BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE, + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode 'solicited service' uuid list. + if (p_advdata->uuids_solicited.uuid_cnt > 0) + { + err_code = uuid_list_encode(&p_advdata->uuids_solicited, + BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT, + BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT, + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode Slave Connection Interval Range. + if (p_advdata->p_slave_conn_int != NULL) + { + err_code = conn_int_encode(p_advdata->p_slave_conn_int, p_encoded_data, p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode Manufacturer Specific Data. + if (p_advdata->p_manuf_specific_data != NULL) + { + err_code = manuf_specific_data_encode(p_advdata->p_manuf_specific_data, + p_encoded_data, + p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Encode Service Data. + if (p_advdata->service_data_count > 0) + { + err_code = service_data_encode(p_advdata, p_encoded_data, p_len); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return err_code; +} + + +static uint32_t advdata_check(const ble_advdata_t * p_advdata) +{ + // Flags must be included in advertising data, and the BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED flag must be set. + if ((p_advdata->flags.size == 0) || + (p_advdata->flags.p_data == NULL) || + ((p_advdata->flags.p_data[0] & BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) == 0) + ) + { + return NRF_ERROR_INVALID_PARAM; + } + + return NRF_SUCCESS; +} + + +static uint32_t srdata_check(const ble_advdata_t * p_srdata) +{ + // Flags shall not be included in the scan response data. + if (p_srdata->flags.size > 0) + { + return NRF_ERROR_INVALID_PARAM; + } + + return NRF_SUCCESS; +} + + +uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata) +{ + uint32_t err_code; + uint8_t len_advdata = 0; + uint8_t len_srdata = 0; + uint8_t encoded_advdata[BLE_GAP_ADV_MAX_SIZE]; + uint8_t encoded_srdata[BLE_GAP_ADV_MAX_SIZE]; + uint8_t * p_encoded_advdata; + uint8_t * p_encoded_srdata; + + // Encode advertising data (if supplied). + if (p_advdata != NULL) + { + err_code = advdata_check(p_advdata); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = adv_data_encode(p_advdata, encoded_advdata, &len_advdata); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + p_encoded_advdata = encoded_advdata; + } + else + { + p_encoded_advdata = NULL; + } + + // Encode scan response data (if supplied). + if (p_srdata != NULL) + { + err_code = srdata_check(p_srdata); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = adv_data_encode(p_srdata, encoded_srdata, &len_srdata); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + p_encoded_srdata = encoded_srdata; + } + else + { + p_encoded_srdata = NULL; + } + + // Pass encoded advertising data and/or scan response data to the stack. + err_code = sd_ble_gap_adv_data_set(p_encoded_advdata, len_advdata, p_encoded_srdata, len_srdata); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_bondmngr.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,1640 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_bondmngr.h" +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include "ble_util.h" +#include "app_error.h" +#include "ble_gap.h" +#include "ble_srv_common.h" +#include "crc16.h" +#include "pstorage.h" +#include "ble_bondmngr_cfg.h" +#include "blocking.h" + +#define CCCD_SIZE 6 /**< Number of bytes needed for storing the state of one CCCD. */ +#define CRC_SIZE 2 /**< Size of CRC in sys_attribute data. */ +#define SYS_ATTR_BUFFER_MAX_LEN (((BLE_BONDMNGR_CCCD_COUNT + 1) * CCCD_SIZE) + CRC_SIZE) /**< Size of sys_attribute data. */ +#define MAX_NUM_CENTRAL_WHITE_LIST MIN(BLE_BONDMNGR_MAX_BONDED_CENTRALS, 8) /**< Maximum number of whitelisted centrals supported.*/ +#define MAX_BONDS_IN_FLASH 10 /**< Maximum number of bonds that can be stored in flash. */ +#define BOND_MANAGER_DATA_SIGNATURE 0x53240000 + +/**@defgroup ble_bond_mngr_sec_access Bond Manager Security Status Access Macros + * @brief The following group of macros abstract access to Security Status with a peer. + * @{ + */ + +#define SEC_STATUS_INIT_VAL 0x00 /**< Initialization value for security status flags. */ +#define ENC_STATUS_SET_VAL 0x01 /**< Bitmask for encryption status. */ +#define BOND_IN_PROGRESS_SET_VAL 0x02 /**< Bitmask for 'bonding in progress'. */ + + +/**@brief Macro for setting the Encryption Status for current link. + * + * @details Macro for setting the Encryption Status for current link. + */ +#define ENCRYPTION_STATUS_SET() \ + do \ + { \ + m_sec_con_status |= ENC_STATUS_SET_VAL; \ + } while (0) + +/**@brief Macro for getting the Encryption Status for current link. + * + * @details Macro for getting the Encryption Status for current link. + */ +#define ENCRYPTION_STATUS_GET() \ + (((m_sec_con_status & ENC_STATUS_SET_VAL) == 0) ? false : true) + +/**@brief Macro for resetting the Encryption Status for current link. + * + * @details Macro for resetting the Encryption Status for current link. + */ +#define ENCRYPTION_STATUS_RESET() \ + do \ + { \ + m_sec_con_status &= (~ENC_STATUS_SET_VAL); \ + } while (0) + + +/**@brief Macro for resetting the Bonding In Progress status for current link. + * + * @details Macro for resetting the Bonding In Progress status for current link. + */ +#define BONDING_IN_PROGRESS_STATUS_SET() \ + do \ + { \ + m_sec_con_status |= BOND_IN_PROGRESS_SET_VAL; \ + } while (0) + +/**@brief Macro for setting the Bonding In Progress status for current link. + * + * @details Macro for setting the Bonding In Progress status for current link. + */ +#define BONDING_IN_PROGRESS_STATUS_GET() \ + (((m_sec_con_status & BOND_IN_PROGRESS_SET_VAL) == 0) ? false: true) + +/**@brief Macro for resetting the Bonding In Progress status for current link. + * + * @details Macro for resetting the Bonding In Progress status for current link. + */ +#define BONDING_IN_PROGRESS_STATUS_RESET() \ + do \ + { \ + m_sec_con_status &= (~BOND_IN_PROGRESS_SET_VAL); \ + } while (0) + +/**@brief Macro for resetting all security status flags for current link. + * + * @details Macro for resetting all security status flags for current link. + */ +#define SECURITY_STATUS_RESET() \ + do \ + { \ + m_sec_con_status = SEC_STATUS_INIT_VAL; \ + } while (0) + +/** @} */ + +/**@brief Verify module's initialization status. + * + * @details Verify module's initialization status. Returns NRF_INVALID_STATE in case a module API + * is called without initializing the module. + */ +#define VERIFY_MODULE_INITIALIZED() \ + do \ + { \ + if (!m_is_bondmngr_initialized) \ + { \ + return NRF_ERROR_INVALID_STATE; \ + } \ + } while(0) + + +/**@brief This structure contains the Bonding Information for one central. + */ +typedef struct +{ + int32_t central_handle; /**< Central's handle (NOTE: Size is 32 bits just to make struct size dividable by 4). */ + ble_gap_evt_auth_status_t auth_status; /**< Central authentication data. */ + ble_gap_evt_sec_info_request_t central_id_info; /**< Central identification info. */ + ble_gap_addr_t central_addr; /**< Central's address. */ +} central_bond_t; + +STATIC_ASSERT(sizeof(central_bond_t) % 4 == 0); + +/**@brief This structure contains the System Attributes information related to one central. + */ +typedef struct +{ + int32_t central_handle; /**< Central's handle (NOTE: Size is 32 bits just to make struct size dividable by 4). */ + uint8_t sys_attr[SYS_ATTR_BUFFER_MAX_LEN]; /**< Central sys_attribute data. */ + uint32_t sys_attr_size; /**< Central sys_attribute data's size (NOTE: Size is 32 bits just to make struct size dividable by 4). */ +} central_sys_attr_t; + +STATIC_ASSERT(sizeof(central_sys_attr_t) % 4 == 0); + +/**@brief This structure contains the Bonding Information and System Attributes related to one + * central. + */ +typedef struct +{ + central_bond_t bond; /**< Bonding information. */ + central_sys_attr_t sys_attr; /**< System attribute information. */ +} central_t; + +/**@brief This structure contains the whitelisted addresses. + */ +typedef struct +{ + int8_t central_handle; /**< Central's handle. */ + ble_gap_addr_t * p_addr; /**< Pointer to the central's address if BLE_GAP_ADDR_TYPE_PUBLIC. */ +} whitelist_addr_t; + +/**@brief This structure contains the whitelisted IRKs. + */ +typedef struct +{ + int8_t central_handle; /**< Central's handle. */ + ble_gap_irk_t * p_irk; /**< Pointer to the central's irk if available. */ +} whitelist_irk_t; + +static bool m_is_bondmngr_initialized = false; /**< Flag for checking if module has been initialized. */ +static ble_bondmngr_init_t m_bondmngr_config; /**< Configuration as specified by the application. */ +static uint16_t m_conn_handle; /**< Current connection handle. */ +static central_t m_central; /**< Current central data. */ +static central_t m_centrals_db[BLE_BONDMNGR_MAX_BONDED_CENTRALS]; /**< Pointer to start of bonded centrals database. */ +static uint8_t m_centrals_in_db_count; /**< Number of bonded centrals. */ +static whitelist_addr_t m_whitelist_addr[MAX_NUM_CENTRAL_WHITE_LIST]; /**< List of central's addresses for the whitelist. */ +static whitelist_irk_t m_whitelist_irk[MAX_NUM_CENTRAL_WHITE_LIST]; /**< List of central's IRKs for the whitelist. */ +static uint8_t m_addr_count; /**< Number of addresses in the whitelist. */ +static uint8_t m_irk_count; /**< Number of IRKs in the whitelist. */ +static uint16_t m_crc_bond_info; /**< Combined CRC for all Bonding Information currently stored in flash. */ +static uint16_t m_crc_sys_attr; /**< Combined CRC for all System Attributes currently stored in flash. */ +static pstorage_handle_t mp_flash_bond_info; /**< Pointer to flash location to write next Bonding Information. */ +static pstorage_handle_t mp_flash_sys_attr; /**< Pointer to flash location to write next System Attribute information. */ +static uint8_t m_bond_info_in_flash_count; /**< Number of Bonding Information currently stored in flash. */ +static uint8_t m_sys_attr_in_flash_count; /**< Number of System Attributes currently stored in flash. */ +static uint8_t m_sec_con_status; /**< Variable to denote security status.*/ +static bool m_bond_loaded; /**< Variable to indicate if the bonding information of the currently connected central is available in the RAM.*/ +static bool m_sys_attr_loaded; /**< Variable to indicate if the system attribute information of the currently connected central is loaded from the database and set in the S110 SoftDevice.*/ +static uint32_t m_bond_crc_array[BLE_BONDMNGR_MAX_BONDED_CENTRALS]; +static uint32_t m_sys_crc_array[BLE_BONDMNGR_MAX_BONDED_CENTRALS]; + +/**@brief Function for extracting the CRC from an encoded 32 bit number that typical resides in + * the flash memory + * + * @param[in] header Header containing CRC and magic number. + * @param[out] p_crc Extracted CRC. + * + * @retval NRF_SUCCESS CRC successfully extracted. + * @retval NRF_ERROR_NOT_FOUND Flash seems to be empty. + * @retval NRF_ERROR_INVALID_DATA Header does not contain the magic number. + */ +static uint32_t crc_extract(uint32_t header, uint16_t * p_crc) +{ + if ((header & 0xFFFF0000U) == BOND_MANAGER_DATA_SIGNATURE) + { + *p_crc = (uint16_t)(header & 0x0000FFFFU); + + return NRF_SUCCESS; + } + else if (header == PSTORAGE_FLASH_EMPTY_MASK) + { + return NRF_ERROR_NOT_FOUND; + } + else + { + return NRF_ERROR_INVALID_DATA; + } +} + + +/**@brief Function for storing the Bonding Information of the specified central to the flash. + * + * @param[in] p_bond Bonding information to be stored. + * + * @return NRF_SUCCESS on success, an error_code otherwise. + */ +static uint32_t bond_info_store(central_bond_t * p_bond) +{ + uint32_t err_code; + pstorage_handle_t dest_block; + + // Check if flash is full + if (m_bond_info_in_flash_count >= MAX_BONDS_IN_FLASH) + { + return NRF_ERROR_NO_MEM; + } + + // Check if this is the first bond to be stored + if (m_bond_info_in_flash_count == 0) + { + // Initialize CRC + m_crc_bond_info = crc16_compute(NULL, 0, NULL); + } + + // Get block pointer from base + err_code = pstorage_block_identifier_get(&mp_flash_bond_info,m_bond_info_in_flash_count,&dest_block); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Write Bonding Information + err_code = pstorage_store(&dest_block, + (uint8_t *)p_bond, + sizeof(central_bond_t), + sizeof(uint32_t)); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + m_crc_bond_info = crc16_compute((uint8_t *)p_bond, + sizeof(central_bond_t), + &m_crc_bond_info); + + // Write header + m_bond_crc_array[m_bond_info_in_flash_count] = (BOND_MANAGER_DATA_SIGNATURE | m_crc_bond_info); + + err_code = pstorage_store (&dest_block, (uint8_t *)&m_bond_crc_array[m_bond_info_in_flash_count],sizeof(uint32_t),0); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_bond_info_in_flash_count++; + return NRF_SUCCESS; +} + + +/**@brief Function for storing the System Attributes related to a specified central in flash. + * + * @param[in] p_sys_attr System Attributes to be stored. + * + * @return NRF_SUCCESS on success, an error_code otherwise. + */ +static uint32_t sys_attr_store(central_sys_attr_t * p_sys_attr) +{ + uint32_t err_code; + pstorage_handle_t dest_block; + + // Check if flash is full. + if (m_sys_attr_in_flash_count >= MAX_BONDS_IN_FLASH) + { + return NRF_ERROR_NO_MEM; + } + + // Check if this is the first time any System Attributes is stored. + if (m_sys_attr_in_flash_count == 0) + { + // Initialize CRC + m_crc_sys_attr = crc16_compute(NULL, 0, NULL); + } + + + // Get block pointer from base + err_code = pstorage_block_identifier_get(&mp_flash_sys_attr,m_sys_attr_in_flash_count,&dest_block); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Write System Attributes in flash. + err_code = pstorage_store(&dest_block, + (uint8_t *)p_sys_attr, + sizeof(central_sys_attr_t), + sizeof(uint32_t)); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + m_crc_sys_attr = crc16_compute((uint8_t *)p_sys_attr, + sizeof(central_sys_attr_t), + &m_crc_sys_attr); + + // Write header. + m_sys_crc_array[m_sys_attr_in_flash_count] = (BOND_MANAGER_DATA_SIGNATURE | m_crc_sys_attr); + + err_code = pstorage_store (&dest_block, + (uint8_t *)&m_sys_crc_array[m_sys_attr_in_flash_count], + sizeof(uint32_t), + 0); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_sys_attr_in_flash_count++; + + + return NRF_SUCCESS; +} + + +/**@brief Function for loading the Bonding Information of one central from flash. + * + * @param[out] p_bond Loaded Bonding Information. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t bonding_info_load_from_flash(central_bond_t * p_bond) +{ + pstorage_handle_t source_block; + uint32_t err_code; + uint32_t crc; + uint16_t crc_header; + + // Check if this is the first bond to be loaded, in which case the + // m_bond_info_in_flash_count variable would have the intial value 0. + if (m_bond_info_in_flash_count == 0) + { + // Initialize CRC. + m_crc_bond_info = crc16_compute(NULL, 0, NULL); + } + + // Get block pointer from base + err_code = pstorage_block_identifier_get(&mp_flash_bond_info, + m_bond_info_in_flash_count, + &source_block); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = pstorage_load((uint8_t *)&crc, &source_block, sizeof(uint32_t), 0); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Extract CRC from header. + err_code = crc_extract(crc, &crc_header); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Load central. + err_code = pstorage_load((uint8_t *)p_bond, + &source_block, + sizeof(central_bond_t), + sizeof(uint32_t)); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Check CRC. + m_crc_bond_info = crc16_compute((uint8_t *)p_bond, + sizeof(central_bond_t), + &m_crc_bond_info); + if (m_crc_bond_info == crc_header) + { + m_bond_info_in_flash_count++; + return NRF_SUCCESS; + } + else + { + return NRF_ERROR_INVALID_DATA; + } +} + + + +/**@brief Function for loading the System Attributes related to one central from flash. + * + * @param[out] p_sys_attr Loaded System Attributes. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t sys_attr_load_from_flash(central_sys_attr_t * p_sys_attr) +{ + pstorage_handle_t source_block; + uint32_t err_code; + uint32_t crc; + uint16_t crc_header; + + // Check if this is the first time System Attributes is loaded from flash, in which case the + // m_sys_attr_in_flash_count variable would have the initial value 0. + if (m_sys_attr_in_flash_count == 0) + { + // Initialize CRC. + m_crc_sys_attr = crc16_compute(NULL, 0, NULL); + } + + // Get block pointer from base + err_code = pstorage_block_identifier_get(&mp_flash_sys_attr, + m_sys_attr_in_flash_count, + &source_block); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = pstorage_load((uint8_t *)&crc, &source_block, sizeof(uint32_t), 0); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Extract CRC from header. + err_code = crc_extract(crc, &crc_header); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + err_code = pstorage_load((uint8_t *)p_sys_attr, + &source_block, + sizeof(central_sys_attr_t), + sizeof(uint32_t)); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Check CRC. + m_crc_sys_attr = crc16_compute((uint8_t *)p_sys_attr, + sizeof(central_sys_attr_t), + &m_crc_sys_attr); + + if (m_crc_sys_attr == crc_header) + { + m_sys_attr_in_flash_count++; + return NRF_SUCCESS; + } + else + { + return NRF_ERROR_INVALID_DATA; + } +} + + +/**@brief Function for erasing the flash pages that contain Bonding Information and System + * Attributes. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t flash_pages_erase(void) +{ + uint32_t err_code; + + err_code = pstorage_clear(&mp_flash_bond_info, MAX_BONDS_IN_FLASH); + + if (err_code == NRF_SUCCESS) + { + err_code = pstorage_clear(&mp_flash_sys_attr, MAX_BONDS_IN_FLASH); + } + + return err_code; +} + + +/**@brief Function for checking if Bonding Information in RAM is different from that in + * flash. + * + * @return TRUE if Bonding Information in flash and RAM are different, FALSE otherwise. + */ +static bool bond_info_changed(void) +{ + int i; + uint16_t crc = crc16_compute(NULL, 0, NULL); + + // Compute CRC for all bonds in database. + for (i = 0; i < m_centrals_in_db_count; i++) + { + crc = crc16_compute((uint8_t *)&m_centrals_db[i].bond, + sizeof(central_bond_t), + &crc); + } + + // Compare the computed CRS to CRC stored in flash. + return (crc != m_crc_bond_info); +} + + +/**@brief Function for checking if System Attributes in RAM is different from that in flash. + * + * @return TRUE if System Attributes in flash and RAM are different, FALSE otherwise. + */ +static bool sys_attr_changed(void) +{ + int i; + uint16_t crc = crc16_compute(NULL, 0, NULL); + + // Compute CRC for all System Attributes in database. + for (i = 0; i < m_centrals_in_db_count; i++) + { + crc = crc16_compute((uint8_t *)&m_centrals_db[i].sys_attr, + sizeof(central_sys_attr_t), + &crc); + } + + // Compare the CRC of System Attributes in flash with that of the System Attributes in memory. + return (crc != m_crc_sys_attr); +} + + +/**@brief Function for setting the System Attributes for specified central to the SoftDevice, or + * clearing the System Attributes if central is a previously unknown. + * + * @param[in] p_central Central for which the System Attributes is to be set. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t central_sys_attr_set(central_t * p_central) +{ + uint8_t * p_sys_attr; + + if (m_central.sys_attr.sys_attr_size != 0) + { + if (m_central.sys_attr.sys_attr_size > SYS_ATTR_BUFFER_MAX_LEN) + { + return NRF_ERROR_INTERNAL; + } + + p_sys_attr = m_central.sys_attr.sys_attr; + } + else + { + p_sys_attr = NULL; + } + + uint32_t err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, + p_sys_attr, + m_central.sys_attr.sys_attr_size); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + return err_code; +} + + +/**@brief Function for updating the whitelist data structures. + */ +static void update_whitelist(void) +{ + int i; + + for (i = 0, m_addr_count = 0, m_irk_count = 0; i < m_centrals_in_db_count; i++) + { + central_bond_t * p_bond = &m_centrals_db[i].bond; + + if (p_bond->auth_status.central_kex.irk) + { + m_whitelist_irk[m_irk_count].central_handle = p_bond->central_handle; + m_whitelist_irk[m_irk_count].p_irk = &(p_bond->auth_status.central_keys.irk); + + m_irk_count++; + } + + if (p_bond->central_addr.addr_type != BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE) + { + m_whitelist_addr[m_addr_count].central_handle = p_bond->central_handle; + m_whitelist_addr[m_addr_count].p_addr = &(p_bond->central_addr); + + m_addr_count++; + } + } +} + + +/**@brief Function for handling the authentication status event related to a new central. + * + * @details This function adds the new central to the database and stores the central's Bonding + * Information to flash. It also notifies the application when the new bond is created, + * and sets the System Attributes to prepare the stack for connection with the new + * central. + * + * @param[in] p_auth_status New authentication status. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t on_auth_status_from_new_central(ble_gap_evt_auth_status_t * p_auth_status) +{ + uint32_t err_code; + + if (m_centrals_in_db_count >= BLE_BONDMNGR_MAX_BONDED_CENTRALS) + { + return NRF_ERROR_NO_MEM; + } + + // Update central. + m_central.bond.auth_status = *p_auth_status; + m_central.bond.central_id_info.div = p_auth_status->periph_keys.enc_info.div; + m_central.sys_attr.sys_attr_size = 0; + + // Add new central to database. + m_central.bond.central_handle = m_centrals_in_db_count; + m_centrals_db[m_centrals_in_db_count++] = m_central; + + update_whitelist(); + + m_bond_loaded = true; + + // Clear System Attributes. + err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Write new central's Bonding Information to flash. + err_code = bond_info_store(&m_central.bond); + if ((err_code == NRF_ERROR_NO_MEM) && (m_bondmngr_config.evt_handler != NULL)) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_BOND_FLASH_FULL; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } + else if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Pass the event to application. + if (m_bondmngr_config.evt_handler != NULL) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_NEW_BOND; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } + + return NRF_SUCCESS; +} + + +/**@brief Function for updating the current central's entry in the database. + */ +static uint32_t central_update(void) +{ + uint32_t err_code; + int32_t central_handle = m_central.bond.central_handle; + + if ((central_handle >= 0) && (central_handle < m_centrals_in_db_count)) + { + // Update the database based on whether the bond and system attributes have + // been loaded or not to avoid overwriting information that was not used in the + // connection session. + if (m_bond_loaded) + { + m_centrals_db[central_handle].bond = m_central.bond; + } + + if (m_sys_attr_loaded) + { + m_centrals_db[central_handle].sys_attr = m_central.sys_attr; + } + + update_whitelist(); + + err_code = NRF_SUCCESS; + } + else + { + err_code = NRF_ERROR_INTERNAL; + } + + return err_code; +} + + +/**@brief Function for searching for the central in the database of known centrals. + * + * @details If the central is found, the variable m_central will be populated with all the + * information (Bonding Information and System Attributes) related to that central. + * + * @param[in] central_id Central Identifier. + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t central_find_in_db(uint16_t central_id) +{ + int i; + + for (i = 0; i < m_centrals_in_db_count; i++) + { + if (central_id == m_centrals_db[i].bond.central_id_info.div) + { + m_central = m_centrals_db[i]; + return NRF_SUCCESS; + } + } + + return NRF_ERROR_NOT_FOUND; +} + + +/**@brief Function for loading all Bonding Information and System Attributes from flash. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t load_all_from_flash(void) +{ + uint32_t err_code; + int i; + + m_centrals_in_db_count = 0; + + while (m_centrals_in_db_count < BLE_BONDMNGR_MAX_BONDED_CENTRALS) + { + central_bond_t central_bond_info; + int central_handle; + + // Load Bonding Information. + err_code = bonding_info_load_from_flash(¢ral_bond_info); + if (err_code == NRF_ERROR_NOT_FOUND) + { + // No more bonds in flash. + break; + } + else if (err_code != NRF_SUCCESS) + { + return err_code; + } + + central_handle = central_bond_info.central_handle; + if (central_handle > m_centrals_in_db_count) + { + // Central handle value(s) missing in flash. This should never happen. + return NRF_ERROR_INVALID_DATA; + } + else + { + // Add/update Bonding Information in central array. + m_centrals_db[central_handle].bond = central_bond_info; + if (central_handle == m_centrals_in_db_count) + { + // New central handle, clear System Attributes. + m_centrals_db[central_handle].sys_attr.sys_attr_size = 0; + m_centrals_db[central_handle].sys_attr.central_handle = INVALID_CENTRAL_HANDLE; + m_centrals_in_db_count++; + } + else + { + // Entry was updated, do nothing. + } + } + } + + // Load System Attributes for all previously known centrals. + for (i = 0; i < m_centrals_in_db_count; i++) + { + central_sys_attr_t central_sys_attr; + + // Load System Attributes. + err_code = sys_attr_load_from_flash(¢ral_sys_attr); + if (err_code == NRF_ERROR_NOT_FOUND) + { + // No more System Attributes in flash. + break; + } + else if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (central_sys_attr.central_handle > m_centrals_in_db_count) + { + // Central handle value(s) missing in flash. This should never happen. + return NRF_ERROR_INVALID_DATA; + } + else + { + // Add/update Bonding Information in central array. + m_centrals_db[central_sys_attr.central_handle].sys_attr = central_sys_attr; + } + } + + // Initialize the remaining empty bond entries in the memory. + for (i = m_centrals_in_db_count; i < BLE_BONDMNGR_MAX_BONDED_CENTRALS; i++) + { + m_centrals_db[i].bond.central_handle = INVALID_CENTRAL_HANDLE; + m_centrals_db[i].sys_attr.sys_attr_size = 0; + m_centrals_db[i].sys_attr.central_handle = INVALID_CENTRAL_HANDLE; + } + + // Update whitelist data structures. + update_whitelist(); + + return NRF_SUCCESS; +} + + +/**@brief Function for handling the connected event received from the BLE stack. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_evt_t * p_ble_evt) +{ + m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + + m_central.bond.central_handle = INVALID_CENTRAL_HANDLE; + m_central.bond.central_addr = p_ble_evt->evt.gap_evt.params.connected.peer_addr; + m_central.sys_attr.sys_attr_size = 0; + + if (p_ble_evt->evt.gap_evt.params.connected.irk_match) + { + uint8_t irk_idx = p_ble_evt->evt.gap_evt.params.connected.irk_match_idx; + + if ((irk_idx >= MAX_NUM_CENTRAL_WHITE_LIST) || + (m_whitelist_irk[irk_idx].central_handle >= BLE_BONDMNGR_MAX_BONDED_CENTRALS)) + { + m_bondmngr_config.error_handler(NRF_ERROR_INTERNAL); + } + else + { + m_central = m_centrals_db[m_whitelist_irk[irk_idx].central_handle]; + } + } + else + { + int i; + + for (i = 0; i < m_addr_count; i++) + { + ble_gap_addr_t * p_cur_addr = m_whitelist_addr[i].p_addr; + + if (memcmp(p_cur_addr->addr, m_central.bond.central_addr.addr, BLE_GAP_ADDR_LEN) == 0) + { + m_central = m_centrals_db[m_whitelist_addr[i].central_handle]; + break; + } + } + } + + if (m_central.bond.central_handle != INVALID_CENTRAL_HANDLE) + { + // Reset bond and system attributes loaded variables. + m_bond_loaded = false; + m_sys_attr_loaded = false; + + // Do not set the system attributes of the central on connection. + if (m_bondmngr_config.evt_handler != NULL) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_CONN_TO_BONDED_CENTRAL; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } + } +} + + +/**@brief Function for handling the 'System Attributes Missing' event received from the + * SoftDevice. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_sys_attr_missing(ble_evt_t * p_ble_evt) +{ + uint32_t err_code; + + if ( + (m_central.bond.central_handle == INVALID_CENTRAL_HANDLE) || + !ENCRYPTION_STATUS_GET() || + BONDING_IN_PROGRESS_STATUS_GET() + ) + { + err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + } + else + { + // Current central is valid, use its data. Set the corresponding sys_attr. + err_code = central_sys_attr_set(&m_central); + if (err_code == NRF_SUCCESS) + { + // Set System Attributes loaded status variable. + m_sys_attr_loaded = true; + } + } + + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } +} + + +/**@brief Function for handling the new authentication status event, received from the + * SoftDevice, related to an already bonded central. + * + * @details This function also writes the updated Bonding Information to flash and notifies the + * application. + * + * @param[in] p_auth_status Updated authentication status. + */ +static void auth_status_update(ble_gap_evt_auth_status_t * p_auth_status) +{ + uint32_t err_code; + + // Authentication status changed, update Bonding Information. + m_central.bond.auth_status = *p_auth_status; + m_central.bond.central_id_info.div = p_auth_status->periph_keys.enc_info.div; + + memset(&(m_centrals_db[m_central.bond.central_handle]), 0, sizeof(central_t)); + m_centrals_db[m_central.bond.central_handle] = m_central; + + // Write updated Bonding Information to flash. + err_code = bond_info_store(&m_central.bond); + if ((err_code == NRF_ERROR_NO_MEM) && (m_bondmngr_config.evt_handler != NULL)) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_BOND_FLASH_FULL; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } + else if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } + + // Pass the event to the application. + if (m_bondmngr_config.evt_handler != NULL) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_AUTH_STATUS_UPDATED; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } +} + + +/**@brief Function for handling the Authentication Status event received from the BLE stack. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_auth_status(ble_gap_evt_auth_status_t * p_auth_status) +{ + if (p_auth_status->auth_status != BLE_GAP_SEC_STATUS_SUCCESS) + { + return; + } + + // Verify if its pairing and not bonding + if (!ENCRYPTION_STATUS_GET()) + { + return; + } + + if (m_central.bond.central_handle == INVALID_CENTRAL_HANDLE) + { + uint32_t err_code = central_find_in_db(p_auth_status->periph_keys.enc_info.div); + + if (err_code == NRF_SUCCESS) + { + // Possible DIV Collision indicate error to application, + // not storing the new LTK + err_code = NRF_ERROR_FORBIDDEN; + } + else + { + // Add the new device to data base + err_code = on_auth_status_from_new_central(p_auth_status); + } + + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } + } + else + { + m_bond_loaded = true; + + // Receiving a auth status again when already in have existing information! + auth_status_update(p_auth_status); + } +} + + +/**@brief Function for handling the Security Info Request event received from the BLE stack. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_sec_info_request(ble_evt_t * p_ble_evt) +{ + uint32_t err_code; + + err_code = central_find_in_db(p_ble_evt->evt.gap_evt.params.sec_info_request.div); + if (err_code == NRF_SUCCESS) + { + // Bond information has been found and loaded for security procedures. Reflect this in the + // status variable + m_bond_loaded = true; + + // Central found in the list of bonded central. Use the encryption info for this central. + err_code = sd_ble_gap_sec_info_reply(m_conn_handle, + &m_central.bond.auth_status.periph_keys.enc_info, + NULL); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } + + // Do not set the sys_attr yet, should be set only when sec_update is successful. + } + else if (err_code == NRF_ERROR_NOT_FOUND) + { + m_central.bond.central_id_info = p_ble_evt->evt.gap_evt.params.sec_info_request; + + // New central. + err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } + + // Initialize the sys_attr. + err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + } + + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } +} + + +/**@brief Function for handling the Connection Security Update event received from the BLE + * stack. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_sec_update(ble_gap_evt_conn_sec_update_t * p_sec_update) +{ + uint8_t security_mode = p_sec_update->conn_sec.sec_mode.sm; + uint8_t security_level = p_sec_update->conn_sec.sec_mode.lv; + + if (((security_mode == 1) && (security_level > 1)) || + ((security_mode == 2) && (security_level != 0))) + { + ENCRYPTION_STATUS_SET(); + + uint32_t err_code = central_sys_attr_set(&m_central); + + if (err_code != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(err_code); + } + else + { + m_sys_attr_loaded = true; + } + + if (m_bondmngr_config.evt_handler != NULL) + { + ble_bondmngr_evt_t evt; + + evt.evt_type = BLE_BONDMNGR_EVT_ENCRYPTED; + evt.central_handle = m_central.bond.central_handle; + evt.central_id = m_central.bond.central_id_info.div; + + m_bondmngr_config.evt_handler(&evt); + } + } +} + + +/**@brief Function for handling the Connection Security Parameters Request event received from + * the BLE stack. + * + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_sec_param_request(ble_gap_evt_sec_params_request_t * p_sec_update) +{ + if (p_sec_update->peer_params.bond) + { + BONDING_IN_PROGRESS_STATUS_SET(); + + if (m_central.bond.central_handle != INVALID_CENTRAL_HANDLE) + { + // Bonding request received from a bonded central, make all system attributes null + m_central.sys_attr.sys_attr_size = 0; + memset(m_central.sys_attr.sys_attr, 0, SYS_ATTR_BUFFER_MAX_LEN); + } + } +} + + +void ble_bondmngr_on_ble_evt(ble_evt_t * p_ble_evt) +{ + if (!m_is_bondmngr_initialized) + { + m_bondmngr_config.error_handler(NRF_ERROR_INVALID_STATE); + } + + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_ble_evt); + break; + + // NOTE: All actions to be taken on the Disconnected event are performed in + // ble_bondmngr_bonded_centrals_store(). This function must be called from the + // Disconnected handler of the application before advertising is restarted (to make + // sure the flash blocks are cleared while the radio is inactive). + case BLE_GAP_EVT_DISCONNECTED: + SECURITY_STATUS_RESET(); + break; + + case BLE_GATTS_EVT_SYS_ATTR_MISSING: + on_sys_attr_missing(p_ble_evt); + break; + + case BLE_GAP_EVT_AUTH_STATUS: + on_auth_status(&p_ble_evt->evt.gap_evt.params.auth_status); + break; + + case BLE_GAP_EVT_SEC_INFO_REQUEST: + on_sec_info_request(p_ble_evt); + break; + + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + on_sec_param_request(&p_ble_evt->evt.gap_evt.params.sec_params_request); + break; + + case BLE_GAP_EVT_CONN_SEC_UPDATE: + on_sec_update(&p_ble_evt->evt.gap_evt.params.conn_sec_update); + break; + + default: + // No implementation needed. + break; + } +} + + +uint32_t ble_bondmngr_bonded_centrals_store(void) +{ + uint32_t err_code; + int i; + + VERIFY_MODULE_INITIALIZED(); + + if (m_central.bond.central_handle != INVALID_CENTRAL_HANDLE) + { + // Fetch System Attributes from stack. + uint16_t sys_attr_size = SYS_ATTR_BUFFER_MAX_LEN; + + err_code = sd_ble_gatts_sys_attr_get(m_conn_handle, + m_central.sys_attr.sys_attr, + &sys_attr_size); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_central.sys_attr.central_handle = m_central.bond.central_handle; + m_central.sys_attr.sys_attr_size = (uint16_t)sys_attr_size; + + // Update the current central. + err_code = central_update(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + // Save Bonding Information if changed. + if (bond_info_changed()) + { + // Erase flash page. + err_code = pstorage_clear(&mp_flash_bond_info,MAX_BONDS_IN_FLASH); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Store bond information for all centrals. + m_bond_info_in_flash_count = 0; + for (i = 0; i < m_centrals_in_db_count; i++) + { + err_code = bond_info_store(&m_centrals_db[i].bond); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + } + + // Save System Attributes, if changed. + if (sys_attr_changed()) + { + // Erase flash page. + err_code = pstorage_clear(&mp_flash_sys_attr, MAX_BONDS_IN_FLASH); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Store System Attributes for all centrals. + m_sys_attr_in_flash_count = 0; + for (i = 0; i < m_centrals_in_db_count; i++) + { + err_code = sys_attr_store(&m_centrals_db[i].sys_attr); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + } + + m_conn_handle = BLE_CONN_HANDLE_INVALID; + m_central.bond.central_handle = INVALID_CENTRAL_HANDLE; + m_central.sys_attr.central_handle = INVALID_CENTRAL_HANDLE; + m_central.sys_attr.sys_attr_size = 0; + m_bond_loaded = false; + m_sys_attr_loaded = false; + + return NRF_SUCCESS; +} + + +uint32_t ble_bondmngr_sys_attr_store(void) +{ + uint32_t err_code; + + if (m_central.sys_attr.sys_attr_size == 0) + { + // Connected to new central. So the flash block for System Attributes for this + // central is empty. Hence no erase is needed. + + uint16_t sys_attr_size = SYS_ATTR_BUFFER_MAX_LEN; + + // Fetch System Attributes from stack. + err_code = sd_ble_gatts_sys_attr_get(m_conn_handle, + m_central.sys_attr.sys_attr, + &sys_attr_size); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_central.sys_attr.central_handle = m_central.bond.central_handle; + m_central.sys_attr.sys_attr_size = (uint16_t)sys_attr_size; + + // Copy the System Attributes to database. + m_centrals_db[m_central.bond.central_handle].sys_attr = m_central.sys_attr; + + // Write new central's System Attributes to flash. + return (sys_attr_store(&m_central.sys_attr)); + } + else + { + // Will not write to flash because System Attributes of an old central would already be + // in flash and so this operation needs a flash erase operation. + return NRF_ERROR_INVALID_STATE; + } +} + + +uint32_t ble_bondmngr_bonded_centrals_delete(void) +{ + VERIFY_MODULE_INITIALIZED(); + + m_centrals_in_db_count = 0; + m_bond_info_in_flash_count = 0; + m_sys_attr_in_flash_count = 0; + + return flash_pages_erase(); +} + + +uint32_t ble_bondmngr_central_addr_get(int8_t central_handle, ble_gap_addr_t * p_central_addr) +{ + if ( + (central_handle == INVALID_CENTRAL_HANDLE) || + (central_handle >= m_centrals_in_db_count) || + (p_central_addr == NULL) || + ( + m_centrals_db[central_handle].bond.central_addr.addr_type + == + BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE + ) + ) + { + return NRF_ERROR_INVALID_PARAM; + } + + *p_central_addr = m_centrals_db[central_handle].bond.central_addr; + return NRF_SUCCESS; +} + + +uint32_t ble_bondmngr_whitelist_get(ble_gap_whitelist_t * p_whitelist) +{ + static ble_gap_addr_t * s_addr[MAX_NUM_CENTRAL_WHITE_LIST]; + static ble_gap_irk_t * s_irk[MAX_NUM_CENTRAL_WHITE_LIST]; + + int i; + + for (i = 0; i < m_irk_count; i++) + { + s_irk[i] = m_whitelist_irk[i].p_irk; + } + for (i = 0; i < m_addr_count; i++) + { + s_addr[i] = m_whitelist_addr[i].p_addr; + } + + p_whitelist->addr_count = m_addr_count; + p_whitelist->pp_addrs = (m_addr_count != 0) ? s_addr : NULL; + p_whitelist->irk_count = m_irk_count; + p_whitelist->pp_irks = (m_irk_count != 0) ? s_irk : NULL; + + return NRF_SUCCESS; +} + + +static void bm_pstorage_cb_handler(pstorage_handle_t * handle, + uint8_t op_code, + uint32_t result, + uint8_t * p_data, + uint32_t data_len) +{ + if (result != NRF_SUCCESS) + { + m_bondmngr_config.error_handler(result); + } +} + + +uint32_t ble_bondmngr_init(ble_bondmngr_init_t * p_init) +{ + pstorage_module_param_t param; + uint32_t err_code; + + if (p_init->error_handler == NULL) + { + return NRF_ERROR_INVALID_PARAM; + } + + if (BLE_BONDMNGR_MAX_BONDED_CENTRALS > MAX_BONDS_IN_FLASH) + { + return NRF_ERROR_DATA_SIZE; + } + + param.block_size = sizeof (central_bond_t) + sizeof (uint32_t); + param.block_count = MAX_BONDS_IN_FLASH; + param.cb = bm_pstorage_cb_handler; + + // Blocks are requested twice, once for bond information and once for system attributes. + // The number of blocks requested has to be the maximum number of bonded devices that + // need to be supported. However, the size of blocks can be different if the sizes of + // system attributes and bonds are not too close. + err_code = pstorage_register(¶m, &mp_flash_bond_info); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + param.block_size = sizeof(central_sys_attr_t) + sizeof(uint32_t); + + err_code = pstorage_register(¶m, &mp_flash_sys_attr); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_bondmngr_config = *p_init; + + memset(&m_central, 0, sizeof(central_t)); + + m_central.bond.central_handle = INVALID_CENTRAL_HANDLE; + m_conn_handle = BLE_CONN_HANDLE_INVALID; + m_centrals_in_db_count = 0; + m_bond_info_in_flash_count = 0; + m_sys_attr_in_flash_count = 0; + + SECURITY_STATUS_RESET(); + + // Erase all stored centrals if specified. + if (m_bondmngr_config.bonds_delete) + { + err_code = flash_pages_erase(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_centrals_in_db_count = 0; + + for (int i = m_centrals_in_db_count; i < BLE_BONDMNGR_MAX_BONDED_CENTRALS; i++) + { + m_centrals_db[i].bond.central_handle = INVALID_CENTRAL_HANDLE; + m_centrals_db[i].sys_attr.sys_attr_size = 0; + m_centrals_db[i].sys_attr.central_handle = INVALID_CENTRAL_HANDLE; + } + } + else + { + // Load bond manager data from flash. + err_code = load_all_from_flash(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + m_is_bondmngr_initialized = true; + + return NRF_SUCCESS; +} + + +uint32_t ble_bondmngr_central_ids_get(uint16_t * p_central_ids, uint16_t * p_length) +{ + VERIFY_MODULE_INITIALIZED(); + + if (p_length == NULL) + { + return NRF_ERROR_NULL; + } + + if (*p_length < m_centrals_in_db_count) + { + // Length of the input array is not enough to fit all known central identifiers. + return NRF_ERROR_DATA_SIZE; + } + + *p_length = m_centrals_in_db_count; + if (p_central_ids == NULL) + { + // Only the length field was required to be filled. + return NRF_SUCCESS; + } + + for (int i = 0; i < m_centrals_in_db_count; i++) + { + p_central_ids[i] = m_centrals_db[i].bond.central_id_info.div; + } + + return NRF_SUCCESS; +} + + +uint32_t ble_bondmngr_bonded_central_delete(uint16_t central_id) +{ + VERIFY_MODULE_INITIALIZED(); + + int8_t central_handle_to_be_deleted = INVALID_CENTRAL_HANDLE; + uint8_t i; + + // Search for the handle of the central. + for (i = 0; i < m_centrals_in_db_count; i++) + { + if (m_centrals_db[i].bond.central_id_info.div == central_id) + { + central_handle_to_be_deleted = i; + break; + } + } + + if (central_handle_to_be_deleted == INVALID_CENTRAL_HANDLE) + { + // Central ID not found. + return NRF_ERROR_NOT_FOUND; + } + + // Delete the central in RAM. + for (i = central_handle_to_be_deleted; i < (m_centrals_in_db_count - 1); i++) + { + // Overwrite the current central entry with the next one. + m_centrals_db[i] = m_centrals_db[i + 1]; + + // Decrement the value of handle. + m_centrals_db[i].bond.central_handle--; + if (INVALID_CENTRAL_HANDLE != m_centrals_db[i].sys_attr.central_handle) + { + m_centrals_db[i].sys_attr.central_handle--; + } + } + + // Clear the last database entry. + memset(&(m_centrals_db[m_centrals_in_db_count - 1]), 0, sizeof(central_t)); + + m_centrals_in_db_count--; + + uint32_t err_code; + + // Reinitialize the pointers to the memory where bonding info and System Attributes are stored + // in flash. + // Refresh the data in the flash memory (both Bonding Information and System Attributes). + // Erase and rewrite bonding info and System Attributes. + + err_code = flash_pages_erase(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + m_bond_info_in_flash_count = 0; + m_sys_attr_in_flash_count = 0; + + for (i = 0; i < m_centrals_in_db_count; i++) + { + err_code = bond_info_store(&(m_centrals_db[i].bond)); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + for (i = 0; i < m_centrals_in_db_count; i++) + { + err_code = sys_attr_store(&(m_centrals_db[i].sys_attr)); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + update_whitelist(); + + return NRF_SUCCESS; +} + + +uint32_t ble_bondmngr_is_link_encrypted (bool * status) +{ + VERIFY_MODULE_INITIALIZED(); + + (*status) = ENCRYPTION_STATUS_GET(); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_conn_params.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,365 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_conn_params.h" +#include <stdlib.h> +#include "ble_util.h" +#include "ble_hci.h" +#include "ble_timer.h" +#include "ble_srv_common.h" +#include "blocking.h" +#include "app_error.h" + +static ble_conn_params_init_t m_conn_params_config; /**< Configuration as specified by the application. */ +static ble_gap_conn_params_t m_preferred_conn_params; /**< Connection parameters preferred by the application. */ +static uint8_t m_update_count; /**< Number of Connection Parameter Update messages that has currently been sent. */ +static uint16_t m_conn_handle; /**< Current connection handle. */ +static ble_gap_conn_params_t m_current_conn_params; /**< Connection parameters received in the most recent Connect event. */ +static ble_timer_id_t m_conn_params_timer_id; /**< Connection parameters timer. */ + +static bool m_change_param = false; + + +static bool is_conn_params_ok(ble_gap_conn_params_t * p_conn_params) +{ + // Check if interval is within the acceptable range. + // NOTE: Using max_conn_interval in the received event data because this contains + // the client's connection interval. + if ( + (p_conn_params->max_conn_interval >= m_preferred_conn_params.min_conn_interval) + && + (p_conn_params->max_conn_interval <= m_preferred_conn_params.max_conn_interval) + ) + { + return true; + } + else + { + return false; + } +} + + +static void update_timeout_handler(void * p_context) +{ + if (m_conn_handle != BLE_CONN_HANDLE_INVALID) + { + // Check if we have reached the maximum number of attempts + m_update_count++; + if (m_update_count <= m_conn_params_config.max_conn_params_update_count) + { + uint32_t err_code; + + // Parameters are not ok, send connection parameters update request. + err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) + { + m_conn_params_config.error_handler(err_code); + } + } + else + { + m_update_count = 0; + + // Negotiation failed, disconnect automatically if this has been configured. + if (m_conn_params_config.disconnect_on_fail) + { + uint32_t err_code; + + err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) + { + m_conn_params_config.error_handler(err_code); + } + } + + // Notify the application that the procedure has failed + if (m_conn_params_config.evt_handler != NULL) + { + ble_conn_params_evt_t evt; + + evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED; + m_conn_params_config.evt_handler(&evt); + } + } + } + +} + +uint32_t ble_conn_params_init(const ble_conn_params_init_t * p_init) +{ + uint32_t err_code; + + m_conn_params_config = *p_init; + m_change_param = false; + if (p_init->p_conn_params != NULL) + { + m_preferred_conn_params = *p_init->p_conn_params; + + // Set the connection params in stack + err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + else + { + // Fetch the connection params from stack + err_code = sd_ble_gap_ppcp_get(&m_preferred_conn_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + m_conn_handle = BLE_CONN_HANDLE_INVALID; + m_update_count = 0; + + return ble_timer_create(&m_conn_params_timer_id, + BLE_TIMER_MODE_SINGLE_SHOT, + update_timeout_handler); +} + + +uint32_t ble_conn_params_stop(void) +{ + return ble_timer_stop(m_conn_params_timer_id); +} + + +static void conn_params_negotiation(void) +{ + // Start negotiation if the received connection parameters are not acceptable + if (!is_conn_params_ok(&m_current_conn_params)) + { + uint32_t err_code; + uint32_t timeout_ms; + + if (m_change_param) + { + // Notify the application that the procedure has failed + if (m_conn_params_config.evt_handler != NULL) + { + ble_conn_params_evt_t evt; + + evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED; + m_conn_params_config.evt_handler(&evt); + } + } + else + { + if (m_update_count == 0) + { + // First connection parameter update + timeout_ms = m_conn_params_config.first_conn_params_update_delay; + } + else + { + timeout_ms = m_conn_params_config.next_conn_params_update_delay; + } + + err_code = ble_timer_start(m_conn_params_timer_id, timeout_ms, NULL); + + if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) + { + m_conn_params_config.error_handler(err_code); + } + } + } + else + { + // Notify the application that the procedure has succeded + if (m_conn_params_config.evt_handler != NULL) + { + ble_conn_params_evt_t evt; + + evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED; + m_conn_params_config.evt_handler(&evt); + } + } + m_change_param = false; +} + + +static void on_connect(ble_evt_t * p_ble_evt) +{ + // Save connection parameters + m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; + m_current_conn_params = p_ble_evt->evt.gap_evt.params.connected.conn_params; + m_update_count = 0; // Connection parameter negotiation should re-start every connection + + // Check if we shall handle negotiation on connect + if (m_conn_params_config.start_on_notify_cccd_handle == BLE_GATT_HANDLE_INVALID) + { + conn_params_negotiation(); + } +} + + +static void on_disconnect(ble_evt_t * p_ble_evt) +{ + uint32_t err_code; + + m_conn_handle = BLE_CONN_HANDLE_INVALID; + + // Stop timer if running + m_update_count = 0; // Connection parameters updates should happen during every connection + + err_code = ble_timer_stop(m_conn_params_timer_id); + + if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) + { + m_conn_params_config.error_handler(err_code); + } +} + + +static void on_write(ble_evt_t * p_ble_evt) +{ + ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; + + // Check if this the correct CCCD + if ( + (p_evt_write->handle == m_conn_params_config.start_on_notify_cccd_handle) + && + (p_evt_write->len == 2) + ) + { + // Check if this is a 'start notification' + if (ble_srv_is_notification_enabled(p_evt_write->data)) + { + // Do connection parameter negotiation if necessary + conn_params_negotiation(); + } + else + { + uint32_t err_code; + + // Stop timer if running + err_code = ble_timer_stop(m_conn_params_timer_id); + + if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) + { + m_conn_params_config.error_handler(err_code); + } + } + } +} + + +static void on_conn_params_update(ble_evt_t * p_ble_evt) +{ + // Copy the parameters + m_current_conn_params = p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params; + + conn_params_negotiation(); +} + + +void ble_conn_params_on_ble_evt(ble_evt_t * p_ble_evt) +{ + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_ble_evt); + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_ble_evt); + break; + + case BLE_GAP_EVT_CONN_PARAM_UPDATE: + on_conn_params_update(p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + +uint32_t ble_conn_params_change_conn_params(ble_gap_conn_params_t *new_params) +{ + uint32_t err_code; + + m_preferred_conn_params = *new_params; + // Set the connection params in stack + err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code == NRF_SUCCESS) + { + if (!is_conn_params_ok(&m_current_conn_params)) + { + m_change_param = true; + err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + m_update_count = 1; + } + else + { + // Notify the application that the procedure has succeded + if (m_conn_params_config.evt_handler != NULL) + { + ble_conn_params_evt_t evt; + + evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED; + m_conn_params_config.evt_handler(&evt); + } + err_code = NRF_SUCCESS; + } + } + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_dtm_app.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,91 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "ble_dtm_app.h" +#include <stdint.h> +#include "ble_serialization.h" +#include "hal_transport_config.h" +#include "ble_encode_transport.h" +#include "app_error.h" +#include "nrf_error.h" +#include "app_uart_stream.h" + + +/**@brief Command response callback function for @ref sd_ble_gap_adv_start BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t dtm_init_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_dtm_init_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t ble_dtm_init(app_uart_stream_comm_params_t * p_uart_comm_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_DTM_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_dtm_init_req_enc(p_uart_comm_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + dtm_init_rsp_dec); + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_dtm_init.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,79 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "ble_dtm_app.h" +#include "dtm_rpc.h" +//#include "dtm_uart.h" +#include "nrf_error.h" +#include "ble_serialization.h" + + +#define MIN_BUFFER_LEN 5 +#define RESERVED_FIXED_VALUE 0x00 + +/** @todo Document + */ +uint32_t ble_dtm_init_req_enc(app_uart_stream_comm_params_t const * const p_uart_comm_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_uart_comm_params); + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(MIN_BUFFER_LEN, *p_buf_len); + + // p_buf[index++] = DTM_OP_CODE_INIT; + // p_buf[index++] = RESERVED_FIXED_VALUE; + p_buf[index++] = p_uart_comm_params->tx_pin_no; + p_buf[index++] = p_uart_comm_params->rx_pin_no; + p_buf[index++] = p_uart_comm_params->baud_rate; + + *p_buf_len = index; + return NRF_SUCCESS; +} + + + +uint32_t ble_dtm_init_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, DTM_OP_CODE_INIT, p_result_code); +} + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_encode_transport.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,359 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_encode_access.h" +#include "ble_encode_transport.h" +#include <stdbool.h> +#include <stdio.h> +#include "ble.h" +#include "ble_rpc_defines.h" +#include "ble_app.h" +#include "hal_transport.h" +#include "nrf_error.h" +#include "app_error.h" +#include "compiler_abstraction.h" + +static ble_command_resp_decode_callback_t m_cmd_resp_decode_callback; /**< BLE command response decode callback function. */ +static ble_encode_cmd_resp_handler_t m_ble_cmd_resp_handler; /**< BLE command response application callback function. */ +static ble_encode_event_handler_t m_ble_event_handler; /**< BLE event application callback function. */ +static const uint8_t * mp_evt_buffer; /**< Pointer to begin of received BLE event buffer. */ +static uint16_t m_evt_length; /**< Length of data in bytes in BLE event buffer. */ +static ble_encode_cmd_write_mode_t m_cmd_write_mode; /**< @ref ble_encode_transport_cmd_write API command mode. */ +static volatile uint32_t m_event_flags; /**< Variable for storing boolean event flags. */ + +#define FLAG_CMD_RESP_HANDLER_REGISTERED (1u << 0) /**< Flag for determining is command response handler registered or not. */ +#define FLAG_EVT_AVAILABLE_FOR_POP (1u << 1u) /**< Flag for determining is event available for application extraction. */ +#define FLAG_CMD_WRITE_INPROGRESS (1u << 2u) /**< Flag for determining is command write in progress. */ + + +/**@brief Function for processing BLE command response. + * + * @param[in] p_buffer Pointer to the begin of command response after packet type field. + * @param[in] length Length of data in bytes. + */ +static __INLINE void ble_command_response_process(const uint8_t * p_buffer, uint32_t length) +{ + // @note: System design does not allow any valid use case for the callback to be NULL + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK_BOOL(m_cmd_resp_decode_callback != NULL); + const uint32_t result_code = m_cmd_resp_decode_callback(p_buffer, length); + + // @note: Relevant flags must be cleared before notifying the application of command response + // reception due to the fact that application can call back within the same context. + m_event_flags &= ~(FLAG_CMD_WRITE_INPROGRESS | FLAG_CMD_RESP_HANDLER_REGISTERED); + + // @note: System design does not allow any valid use case for the callback to be NULL + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK_BOOL(m_ble_cmd_resp_handler != NULL); + m_ble_cmd_resp_handler(result_code); +} + + +/**@brief Function for processing BLE event. + * + * @param[in] p_buffer Pointer to the begin of event after packet type field. + * @param[in] length Length of data in bytes. + */ +static __INLINE void ble_event_process(const uint8_t * p_buffer, uint32_t length) +{ + // @note: System design does not allow any valid use case for the callback to be NULL + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK_BOOL(m_ble_event_handler != NULL); + + mp_evt_buffer = p_buffer; + m_evt_length = length; + + m_event_flags |= FLAG_EVT_AVAILABLE_FOR_POP; + m_ble_event_handler(BLE_ENCODE_EVT_RDY); +} + + +/**@brief Function for processing RX packet ready event from transport layer. + */ +static __INLINE void rx_packet_ready_event_process(void) +{ + uint8_t * p_buffer; + uint16_t length; + + // @note: This implementation is based on system design where max 1 RX buffer is available. + // On any other design this system will fail as multiple received events can override existing + // received event if application does not pop the event out within the current context. + if (!(m_event_flags & FLAG_EVT_AVAILABLE_FOR_POP)) + { + uint32_t err_code = hci_transport_rx_pkt_extract(&p_buffer, &length); + // @note: System design does not allow any valid use case for the RX packet extract failure + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK(err_code); + + const uint8_t packet_type = p_buffer[0]; // @todo: use #define for 0 + + switch (packet_type) + { + case BLE_RPC_PKT_RESP: + case BLE_RPC_PKT_DTM_RESP: + // Adjust buffer begin pointer and length values after bypassing the packet type + // field. + ble_command_response_process(&(p_buffer[1]), --length); // @todo: use #define for 1 + + // @todo: consider moving consume prior application completion event. + err_code = hci_transport_rx_pkt_consume(p_buffer); + // @note: System design does not allow any valid use case for the RX packet consume + // failure which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK(err_code); + break; + + case BLE_RPC_PKT_EVT: + // Adjust buffer begin pointer and length values after bypassing the packet type + // field. + ble_event_process(&(p_buffer[1]), --length); // @todo: use #define for 1 + break; + + default: + // @note: Should never happen. + APP_ERROR_HANDLER(packet_type); + break; + } + } + else + { + // @note: Should never happen. + APP_ERROR_HANDLER(m_event_flags); + } +} + + +/**@brief Function for processing events from transport layer. + * + * @param[in] event Transport layer event to process. + */ +static void transport_event_process(hci_transport_evt_t event) +{ + switch (event.evt_type) + { + case HCI_TRANSPORT_RX_RDY: + rx_packet_ready_event_process(); + break; + case HCI_TRANSPORT_RX_STARTED: + break; + default: + // @note: Should never happen. + APP_ERROR_HANDLER(event.evt_type); + break; + } +} + + +/**@brief Function for processing TX done event from transport layer. + * + * @param[in] result TX done event result code. + */ +static void transport_tx_done_process(hci_transport_tx_done_result_t result) +{ + APP_ERROR_CHECK_BOOL(result == HCI_TRANSPORT_TX_DONE_SUCCESS); + + // Actions are only executed if no command response is required for the transmitted command. + if (m_cmd_write_mode == BLE_ENCODE_WRITE_MODE_NO_RESP) + { + // @note: System design does not allow any valid use case for the callback to be NULL + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK_BOOL(m_cmd_resp_decode_callback != NULL); + const uint32_t result_code = m_cmd_resp_decode_callback(NULL, 0); + + // @note: Relevant flags must be cleared before notifying the application of command + // processing completion due to the fact that application can call back within the same + // context. + m_event_flags &= ~(FLAG_CMD_WRITE_INPROGRESS | FLAG_CMD_RESP_HANDLER_REGISTERED); + + // @note: System design does not allow any valid use case for the callback to be NULL + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK_BOOL(m_ble_cmd_resp_handler != NULL); + m_ble_cmd_resp_handler(result_code); + } +} + + +uint32_t ble_encode_open(void) +{ + uint32_t err_code = hci_transport_evt_handler_reg(transport_event_process); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + err_code = hci_transport_tx_done_register(transport_tx_done_process); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + return hci_transport_open(); +} + + +uint32_t ble_encode_close(void) +{ + m_event_flags = 0; + m_evt_length = 0; + m_ble_cmd_resp_handler = NULL; + m_cmd_resp_decode_callback = NULL; + m_ble_event_handler = NULL; + mp_evt_buffer = NULL; + m_cmd_write_mode = BLE_ENCODE_WRITE_MODE_MAX; + + return hci_transport_close(); +} + + +uint32_t ble_encode_cmd_resp_handler_reg(ble_encode_cmd_resp_handler_t ble_command_resp_handler) +{ + if (ble_command_resp_handler == NULL) + { + return NRF_ERROR_NULL; + } + + uint32_t err_code; + + if (!(m_event_flags & FLAG_CMD_RESP_HANDLER_REGISTERED)) + { + m_event_flags |= FLAG_CMD_RESP_HANDLER_REGISTERED; + m_ble_cmd_resp_handler = ble_command_resp_handler; + err_code = NRF_SUCCESS; + } + else + { + err_code = NRF_ERROR_BUSY; + } + + return err_code; +} + + +uint32_t ble_encode_evt_handler_register(ble_encode_event_handler_t ble_event_handler) +{ + if (ble_event_handler == NULL) + { + return NRF_ERROR_NULL; + } + + m_ble_event_handler = ble_event_handler; + + return NRF_SUCCESS; +} + + +uint8_t * ble_encode_transport_tx_alloc(void) +{ + uint8_t * p_buffer; + uint32_t err_code; + + // This should be called only from main application context and not from interrupt, otherwise it + // will block the system. + do + { + err_code = hci_transport_tx_alloc(&p_buffer); + + //__WFE(); + } + while (err_code == NRF_ERROR_NO_MEM); + + + return p_buffer; +} + + +void ble_encode_transport_tx_free(void) +{ + const uint32_t err_code = hci_transport_tx_free(); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); +} + + +void ble_encode_transport_cmd_write(const uint8_t * p_buffer, + uint32_t length, + ble_encode_cmd_write_mode_t cmd_write_mode, + ble_command_resp_decode_callback_t cmd_resp_decode_callback) +{ + APP_ERROR_CHECK_BOOL(!(m_event_flags & FLAG_CMD_WRITE_INPROGRESS)); + APP_ERROR_CHECK_BOOL(p_buffer != NULL); + APP_ERROR_CHECK_BOOL(length != 0); + APP_ERROR_CHECK_BOOL((cmd_write_mode == BLE_ENCODE_WRITE_MODE_RESP) || + (cmd_write_mode == BLE_ENCODE_WRITE_MODE_NO_RESP)); + APP_ERROR_CHECK_BOOL(cmd_resp_decode_callback != NULL); + + m_event_flags |= FLAG_CMD_WRITE_INPROGRESS; + m_cmd_write_mode = cmd_write_mode; + m_cmd_resp_decode_callback = cmd_resp_decode_callback; + + const uint32_t err_code = hci_transport_pkt_write(p_buffer, length); + // @note: Should never fail as system design allows only 1 command to be in progress at any + // time. + APP_ERROR_CHECK(err_code); +} + + +uint32_t ble_encode_event_pop(ble_evt_t * p_event, uint32_t * p_event_len) +{ + uint32_t err_code; + + if (p_event_len == NULL) + { + return NRF_ERROR_NULL; + } + + if (m_event_flags & FLAG_EVT_AVAILABLE_FOR_POP) + { + m_event_flags &= ~FLAG_EVT_AVAILABLE_FOR_POP; + + err_code = ble_event_dec(mp_evt_buffer, m_evt_length, p_event, p_event_len); + // @note: Should never happen. + APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_DATA_SIZE)); + + if ((err_code == NRF_SUCCESS) && (p_event != NULL)) + { + // @note: (p_event != NULL) check needs to be included in order to cover the p_event + // length query use case. + + // @note: Decrement buffer pointer to original received from + // @ref hci_transport_rx_pkt_extract. + --mp_evt_buffer; + err_code = hci_transport_rx_pkt_consume((uint8_t *)mp_evt_buffer); + // @note: System design does not allow any valid use case for the RX packet consume failure + // which will imply design error that must be fixed at compile time. + APP_ERROR_CHECK(err_code); + } + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_gap.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,1131 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap.h" +#include <stdint.h> +#include "ble_rpc_defines.h" +#include "ble_encode_transport.h" +#include "ble_gap_app.h" +#include "hal_transport_config.h" +#include "app_error.h" + +/**@brief Structure containing @ref sd_ble_gap_device_name_get output parameters. */ +typedef struct +{ + uint8_t * p_dev_name; /**< @ref sd_ble_gap_device_name_get p_dev_name output parameter. */ + uint16_t * p_len; /**< @ref sd_ble_gap_device_name_get p_len output parameter. */ +} gap_device_name_get_output_params_t; + +/**@brief Structure containing @ref sd_ble_gap_appearance_get output parameters. */ +typedef struct +{ + uint16_t * p_appearance; /**< @ref sd_ble_gap_appearance_get p_appearance output parameter. */ +} gap_appearance_get_output_params_t; + +/**@brief Structure containing @ref sd_ble_gap_ppcp_get output parameters. */ +typedef struct +{ + ble_gap_conn_params_t * p_conn_params; /**< @ref sd_ble_gap_ppcp_get p_conn_params output parameter. */ +} gap_ppcp_get_out_params_t; + +/**@brief Union containing BLE command output parameters. */ +typedef union +{ + gap_device_name_get_output_params_t gap_device_name_get_out_params; /**< @ref sd_ble_gap_device_name_get output parameters. */ + gap_appearance_get_output_params_t gap_appearance_get_out_params; /**< @ref sd_ble_gap_appearance_get output parameters. */ + gap_ppcp_get_out_params_t gap_ppcp_get_out_params; /**< @ref sd_ble_gap_ppcp_get output parameters. */ +} gap_command_output_params_t; + +static gap_command_output_params_t m_output_params; /**< BLE command output parameters. */ + +static void * mp_out_params[1]; + +/**@brief Command response callback function for @ref sd_ble_gap_adv_start BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_adv_start_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_adv_start_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_adv_start(ble_gap_adv_params_t const * const p_adv_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_adv_start_req_enc(p_adv_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_adv_start_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref ble_gap_device_name_get_req_enc BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_device_name_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_gap_device_name_get_rsp_dec(p_buffer, + length, + m_output_params.gap_device_name_get_out_params.p_dev_name, + m_output_params.gap_device_name_get_out_params.p_len, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_device_name_get(uint8_t * const p_dev_name, uint16_t * const p_len) +{ + m_output_params.gap_device_name_get_out_params.p_dev_name = p_dev_name; + m_output_params.gap_device_name_get_out_params.p_len = p_len; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_device_name_get_req_enc(p_dev_name, + p_len, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_device_name_get_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_appearance_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_appearance_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_gap_appearance_get_rsp_dec(p_buffer, + length, + m_output_params.gap_appearance_get_out_params.p_appearance, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_appearance_get(uint16_t * const p_appearance) +{ + m_output_params.gap_appearance_get_out_params.p_appearance = p_appearance; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_appearance_get_req_enc(p_appearance, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_appearance_get_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_device_name_set BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_device_name_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_device_name_set_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_device_name_set(ble_gap_conn_sec_mode_t const * const p_write_perm, + uint8_t const * const p_dev_name, + uint16_t len) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_device_name_set_req_enc(p_write_perm, + p_dev_name, + len, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_device_name_set_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_appearance_set BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_appearance_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_appearance_set_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_appearance_set(uint16_t appearance) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_appearance_set_req_enc(appearance, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_appearance_set_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_ppcp_set BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_ppcp_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_ppcp_set_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_ppcp_set(ble_gap_conn_params_t const * const p_conn_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_ppcp_set_req_enc(p_conn_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_ppcp_set_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_adv_data_set BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_adv_data_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_adv_data_set_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_adv_data_set(uint8_t const * const p_data, + uint8_t dlen, + uint8_t const * const p_sr_data, + uint8_t srdlen) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_adv_data_set_req_enc(p_data, + dlen, + p_sr_data, + srdlen, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_adv_data_set_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_conn_param_update BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_conn_param_update_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_conn_param_update_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_conn_param_update(uint16_t conn_handle, + ble_gap_conn_params_t const * const p_conn_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_conn_param_update_req_enc(conn_handle, + p_conn_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_conn_param_update_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_disconnect BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_disconnect_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_disconnect_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_disconnect(uint16_t conn_handle, uint8_t hci_status_code) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_disconnect_req_enc(conn_handle, + hci_status_code, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_disconnect_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_sec_info_reply BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_sec_info_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gap_sec_info_reply_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_sec_info_reply(uint16_t conn_handle, + ble_gap_enc_info_t const * const p_enc_info, + ble_gap_sign_info_t const * const p_sign_info) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_sec_info_reply_req_enc(conn_handle, + p_enc_info, + p_sign_info, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_sec_info_reply_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_sec_params_reply BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_sec_params_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_sec_params_reply_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_sec_params_reply(uint16_t conn_handle, + uint8_t sec_status, + ble_gap_sec_params_t const * const p_sec_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_sec_params_reply_req_enc(conn_handle, + sec_status, + p_sec_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_sec_params_reply_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gap_ppcp_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_ppcp_get_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_ppcp_get_rsp_dec(p_buffer, + length, + m_output_params.gap_ppcp_get_out_params.p_conn_params, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_ppcp_get(ble_gap_conn_params_t * const p_conn_params) +{ + m_output_params.gap_ppcp_get_out_params.p_conn_params = p_conn_params; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_ppcp_get_req_enc(p_conn_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_ppcp_get_reply_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_address_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_address_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_address_get_rsp_dec(p_buffer, + length, + (ble_gap_addr_t *)mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_address_get(ble_gap_addr_t * const p_addr) +{ + mp_out_params[0] = p_addr; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_address_get_req_enc(p_addr, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_address_get_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_address_set BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_address_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_address_set_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_address_set(uint8_t addr_cycle_mode, ble_gap_addr_t const * const p_addr) +{ + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_address_set_req_enc(addr_cycle_mode, + p_addr, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_address_set_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_adv_stop BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_adv_stop_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_adv_stop_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_adv_stop(void) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_adv_stop_req_enc(&(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_adv_stop_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_auth_key_reply BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_auth_key_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_auth_key_reply_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_auth_key_reply(uint16_t conn_handle, uint8_t key_type, uint8_t const * const key) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_auth_key_reply_req_enc(conn_handle, key_type, key, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_auth_key_reply_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_authenticate BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_authenticate_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_authenticate_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_authenticate(uint16_t conn_handle, ble_gap_sec_params_t const * const p_sec_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_authenticate_req_enc(conn_handle, p_sec_params, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_authenticate_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_conn_sec_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_conn_sec_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_conn_sec_get_rsp_dec(p_buffer, + length, + (ble_gap_conn_sec_t **)&mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_conn_sec_get(uint16_t conn_handle, ble_gap_conn_sec_t * const p_conn_sec) +{ + mp_out_params[0] = p_conn_sec; + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_conn_sec_get_req_enc(conn_handle, p_conn_sec, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_conn_sec_get_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_rssi_start BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_rssi_start_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_rssi_start_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_rssi_start(uint16_t conn_handle) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_rssi_start_req_enc(conn_handle, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_rssi_start_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_rssi_stop BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_rssi_stop_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_rssi_stop_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_rssi_stop(uint16_t conn_handle) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_rssi_stop_req_enc(conn_handle, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_rssi_stop_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gap_tx_power_set BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gap_tx_power_set_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = 0; + + const uint32_t err_code = ble_gap_tx_power_set_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gap_tx_power_set(int8_t tx_power) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gap_tx_power_set_req_enc(tx_power, + &(p_buffer[1]),&buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gap_tx_power_set_rsp_dec); + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_gattc.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,482 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdint.h> +#include "ble_gattc.h" +#include "ble_gattc_app.h" +#include "ble_rpc_defines.h" +#include "ble_encode_transport.h" +#include "hal_transport_config.h" +#include "app_error.h" + +/**@brief Command response callback function for @ref sd_ble_gattc_primary_services_discover BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_primary_services_discover_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_primary_services_discover_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_primary_services_discover(uint16_t conn_handle, + uint16_t start_handle, + ble_uuid_t const *const p_srvc_uuid) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_primary_services_discover_req_enc(conn_handle, + start_handle, + p_srvc_uuid, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_primary_services_discover_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_relationships_discover BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_relationships_discover_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_relationships_discover_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_relationships_discover(uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_relationships_discover_req_enc(conn_handle, + p_handle_range, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_relationships_discover_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_characteristics_discover BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_characteristics_discover_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_characteristics_discover_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_characteristics_discover(uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_characteristics_discover_req_enc(conn_handle, + p_handle_range, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_characteristics_discover_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_descriptors_discover BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_descriptors_discover_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_descriptors_discover_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_descriptors_discover(uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_descriptors_discover_req_enc(conn_handle, + p_handle_range, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_descriptors_discover_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_char_value_by_uuid_read BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_char_value_by_uuid_read_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_char_value_by_uuid_read_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_char_value_by_uuid_read(uint16_t conn_handle, + ble_uuid_t const *const p_uuid, + ble_gattc_handle_range_t const * const p_handle_range) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_char_value_by_uuid_read_req_enc(conn_handle, + p_uuid, + p_handle_range, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_char_value_by_uuid_read_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_read BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_read_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_read_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_read(uint16_t conn_handle, + uint16_t handle, + uint16_t offset) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_read_req_enc(conn_handle, + handle, + offset, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_read_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_char_values_read BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_char_values_read_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_char_values_read_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_char_values_read(uint16_t conn_handle, + uint16_t const * const p_handles, + uint16_t handle_count) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_char_values_read_req_enc(conn_handle, + p_handles, + handle_count, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_char_values_read_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_write BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_write_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_write_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_write(uint16_t conn_handle, + ble_gattc_write_params_t const *const p_write_params) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_write_req_enc(conn_handle, + p_write_params, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_write_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gattc_hv_confirm BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gattc_hv_confirm_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gattc_hv_confirm_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gattc_hv_confirm(uint16_t conn_handle, + uint16_t handle) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gattc_hv_confirm_req_enc(conn_handle, + handle, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gattc_hv_confirm_rsp_dec); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_gatts.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,702 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts.h" +#include <stdint.h> +#include <stddef.h> +#include "ble_rpc_defines.h" +#include "ble_encode_transport.h" +#include "ble_gatts_app.h" +#include "hal_transport_config.h" +#include "app_error.h" + +/**@brief Structure containing @ref sd_ble_gatts_service_add output parameters. */ +typedef struct +{ + uint16_t * p_handle; /**< @ref sd_ble_gatts_service_add p_handle output parameter. */ +} gatts_service_add_out_params_t; + +/**@brief Structure containing @ref sd_ble_gatts_characteristic_add output parameters. */ +typedef struct +{ + uint16_t * p_handles; /**< @ref sd_ble_gatts_characteristic_add p_handles output parameter. */ +} gatts_char_add_out_params_t; + +/**@brief Structure containing @ref sd_ble_gatts_sys_attr_get output parameters. */ +typedef struct +{ + uint8_t * p_sys_attr_data; /**< @ref sd_ble_gatts_sys_attr_get p_sys_attr_data output parameter. */ + uint16_t * p_len; /**< @ref sd_ble_gatts_sys_attr_get p_len output parameter. */ +} gatts_sys_attr_get_out_params_t; + +/**@brief Structure containing @ref sd_ble_gatts_value_set output parameters. */ +typedef struct +{ + uint16_t * p_len; /**< @ref sd_ble_gatts_value_set p_len output parameter. */ +} gatts_value_set_out_params_t; + +/**@brief Union containing BLE command output parameters. */ +typedef union +{ + gatts_service_add_out_params_t gatts_service_add_out_params; /**< @ref sd_ble_gatts_service_add output parameters. */ + gatts_char_add_out_params_t gatts_char_add_out_params; /**< @ref sd_ble_gatts_characteristic_add output parameters. */ + gatts_sys_attr_get_out_params_t gatts_sys_attr_get_out_params; /**< @ref sd_ble_gatts_sys_attr_get output parameters. */ + gatts_value_set_out_params_t gatts_value_set_out_params; /**< @ref sd_ble_gatts_value_set output parameters. */ + ble_gatts_hvx_params_t gatts_hvx_out_params; +} gatts_command_output_params_t; + +static gatts_command_output_params_t m_output_params; /**< BLE command output parameters. */ + +//Pointer for sd calls output params +static void * mp_out_params[3]; + +/**@brief Command response callback function for @ref sd_ble_gatts_sys_attr_set BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_sys_attrset_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gatts_sys_attr_set_rsp_dec(p_buffer, length, &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_sys_attr_set(uint16_t conn_handle, + uint8_t const * const p_sys_attr_data, + uint16_t len) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_sys_attr_set_req_enc(conn_handle, + p_sys_attr_data, + len, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_sys_attrset_reply_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gatts_hvx BLE command. + * + * Callback for decoding the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_hvx_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gatts_hvx_rsp_dec(p_buffer, length, &result_code, + (uint16_t * *)& m_output_params.gatts_hvx_out_params.p_len); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_hvx(uint16_t conn_handle, ble_gatts_hvx_params_t const * const p_hvx_params) +{ + m_output_params.gatts_hvx_out_params.p_len = (p_hvx_params) ? p_hvx_params->p_len : NULL; + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_hvx_req_enc(conn_handle, + p_hvx_params, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_hvx_reply_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gatts_service_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_service_add_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_gatts_service_add_rsp_dec(p_buffer, + length, + m_output_params.gatts_service_add_out_params.p_handle, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_service_add(uint8_t type, + ble_uuid_t const * const p_uuid, + uint16_t * const p_handle) +{ + m_output_params.gatts_service_add_out_params.p_handle = p_handle; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_service_add_req_enc(type, + p_uuid, + p_handle, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_service_add_reply_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_service_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_service_changed_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = NRF_SUCCESS; + + const uint32_t err_code = ble_gatts_service_changed_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gatts_service_changed(uint16_t conn_handle, + uint16_t start_handle, + uint16_t end_handle) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_service_changed_req_enc(conn_handle, + start_handle, + end_handle, + &(p_buffer[1]), + &buffer_length); + + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_service_changed_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_include_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_include_add_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = NRF_SUCCESS; + + const uint32_t err_code = + ble_gatts_include_add_rsp_dec(p_buffer, + length, + (uint16_t *) &mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gatts_include_add(uint16_t service_handle, + uint16_t inc_serv_handle, + uint16_t * const p_include_handle) +{ + mp_out_params[0] = p_include_handle; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gatts_include_add_req_enc(service_handle, + inc_serv_handle, + p_include_handle, + &(p_buffer[1]), + &buffer_length); + + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_include_add_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_characteristic_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_char_add_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gatts_characteristic_add_rsp_dec( + p_buffer, + length, + &m_output_params.gatts_char_add_out_params.p_handles, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_characteristic_add(uint16_t service_handle, + ble_gatts_char_md_t const * const p_char_md, + ble_gatts_attr_t const * const p_attr_char_value, + ble_gatts_char_handles_t * const p_handles) +{ + m_output_params.gatts_char_add_out_params.p_handles = (uint16_t*)p_handles; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_characteristic_add_req_enc(service_handle, + p_char_md, + p_attr_char_value, + p_handles, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_char_add_reply_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_descriptor_add BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_descriptor_add_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = NRF_SUCCESS; + + const uint32_t err_code = + ble_gatts_descriptor_add_rsp_dec(p_buffer, + length, + (uint16_t *) &mp_out_params[0], + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gatts_descriptor_add(uint16_t char_handle, + ble_gatts_attr_t const *const p_attr, + uint16_t * const p_handle) +{ + mp_out_params[0] = p_handle; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gatts_descriptor_add_req_enc(char_handle, + p_attr, + p_handle, + &(p_buffer[1]), + &buffer_length); + + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_descriptor_add_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_rw_authorize_reply BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_rw_authorize_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code = NRF_SUCCESS; + + const uint32_t err_code = ble_gatts_rw_authorize_reply_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gatts_rw_authorize_reply(uint16_t conn_handle, + ble_gatts_rw_authorize_reply_params_t const *const p_rw_authorize_reply_params) +{ + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gatts_rw_authorize_reply_req_enc(conn_handle, + p_rw_authorize_reply_params, + &(p_buffer[1]), + &buffer_length); + + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_rw_authorize_reply_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_value_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_value_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + // Order of arguments is different from ble_gatts_value_get_req_enc() + // p_data is argument number 3 while p_len is argument number 4 + const uint32_t err_code = ble_gatts_value_get_rsp_dec(p_buffer, + length, + (uint8_t **)&mp_out_params[1], + (uint16_t **)&mp_out_params[0], + &result_code); + + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_ble_gatts_value_get(uint16_t handle, + uint16_t offset, + uint16_t * const p_len, + uint8_t * const p_data) +{ + mp_out_params[0] = p_len; + mp_out_params[1] = p_data; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = ble_gatts_value_get_req_enc(handle, + offset, + p_len, + p_data, + &(p_buffer[1]), + &buffer_length); + + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_value_get_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref sd_ble_gatts_value_set BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_value_set_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gatts_value_set_rsp_dec( + p_buffer, + length, + m_output_params.gatts_value_set_out_params.p_len, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_value_set(uint16_t handle, + uint16_t offset, + uint16_t * const p_len, + uint8_t const * const p_value) +{ + m_output_params.gatts_value_set_out_params.p_len = p_len; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_value_set_req_enc(handle, + offset, + p_len, + p_value, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_value_set_reply_rsp_dec); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_ble_gatts_sys_attr_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t gatts_sysattr_get_reply_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = ble_gatts_sys_attr_get_rsp_dec( + p_buffer, + length, + m_output_params.gatts_sys_attr_get_out_params.p_sys_attr_data, + m_output_params.gatts_sys_attr_get_out_params.p_len, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_INVALID_DATA); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_gatts_sys_attr_get(uint16_t conn_handle, + uint8_t * const p_sys_attr_data, + uint16_t * const p_len) +{ + m_output_params.gatts_sys_attr_get_out_params.p_sys_attr_data = p_sys_attr_data; + m_output_params.gatts_sys_attr_get_out_params.p_len = p_len; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_gatts_sys_attr_get_req_enc(conn_handle, + p_sys_attr_data, + p_len, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + gatts_sysattr_get_reply_rsp_dec); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/ble_l2cap.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,197 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap.h" +#include <stdint.h> +#include "ble_rpc_defines.h" +#include "ble_encode_transport.h" +#include "ble_l2cap_app.h" +#include "hal_transport_config.h" +#include "app_error.h" + + +/**@brief Command response callback function for @ref ble_l2cap_cid_register_req_enc BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t l2cap_cid_register_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_l2cap_cid_register_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_l2cap_cid_register(uint16_t cid) +{ + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_l2cap_cid_register_req_enc(cid, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + l2cap_cid_register_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref ble_l2cap_cid_unregister_req_enc BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t l2cap_cid_unregister_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_l2cap_cid_unregister_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_l2cap_cid_unregister(uint16_t cid) +{ + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_l2cap_cid_unregister_req_enc(cid, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + l2cap_cid_unregister_rsp_dec); + + return NRF_SUCCESS; +} + +/**@brief Command response callback function for @ref ble_l2cap_tx_req_enc BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ +static uint32_t l2cap_tx_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = + ble_l2cap_tx_rsp_dec(p_buffer, + length, + &result_code); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + + +uint32_t sd_ble_l2cap_tx(uint16_t conn_handle, ble_l2cap_header_t const * const p_header, uint8_t const * const p_data) +{ + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = ble_l2cap_tx_req_enc(conn_handle, p_header, p_data, + &(p_buffer[1]), + &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + l2cap_tx_rsp_dec); + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/crc16.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,54 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "crc16.h" +#include <stdio.h> + +uint16_t crc16_compute(const uint8_t * p_data, uint32_t size, const uint16_t * p_crc) +{ + uint32_t i; + uint16_t crc = (p_crc == NULL) ? 0xffff : *p_crc; + + for (i = 0; i < size; i++) + { + crc = (unsigned char)(crc >> 8) | (crc << 8); + crc ^= p_data[i]; + crc ^= (unsigned char)(crc & 0xff) >> 4; + crc ^= (crc << 8) << 4; + crc ^= ((crc & 0xff) << 4) << 1; + } + + return crc; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/nrf_soc.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,139 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include <stdint.h> +#include <string.h> +#include "ble_encode_transport.h" +#include "nrf_soc_app.h" +#include "hal_transport_config.h" +#include "nrf_error_soc.h" +#include "app_error.h" +#include "ble_rpc_defines.h" + +static void * mp_out_param; + +/**@brief Command processing complete function for @ref sd_power_system_off BLE command. + * + * Callback for returning the command response return code. + * + * @param[in] p_buffer NULL as no command response for the command send. + * @param[in] length 0 as no command response for the command send. + * + * @retval NRF_ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN + */ +static uint32_t cmd_write_complete(const uint8_t * p_buffer, uint32_t length) +{ + APP_ERROR_CHECK_BOOL(p_buffer == NULL); + APP_ERROR_CHECK_BOOL(length == 0); + + ble_encode_transport_tx_free(); + + return NRF_ERROR_SOC_POWER_OFF_SHOULD_NOT_RETURN; +} + + +uint32_t sd_power_system_off(void) +{ + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + const uint32_t err_code = power_system_off_req_enc(&(p_buffer[1]), &buffer_length); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_NO_RESP, + cmd_write_complete); + + return NRF_SUCCESS; +} + + +/**@brief Command response callback function for @ref sd_temp_get BLE command. + * + * Callback for decoding the output parameters and the command response return code. + * + * @param[in] p_buffer Pointer to begin of command response buffer. + * @param[in] length Length of data in bytes. + * + * @return Decoded command response return code. + */ + +static uint32_t mw_temp_get_rsp_dec(const uint8_t * p_buffer, uint32_t length) +{ + uint32_t result_code; + + const uint32_t err_code = temp_get_rsp_dec(p_buffer, + length, + &result_code, + (int32_t *)mp_out_param); + // @note: Should never fail. + APP_ERROR_CHECK(err_code); + + ble_encode_transport_tx_free(); + + return result_code; +} + +uint32_t sd_temp_get(int32_t * p_temp) +{ + mp_out_param = p_temp; + + uint8_t * p_buffer = ble_encode_transport_tx_alloc(); + + // @note: No NULL check required as error checked by called module as defined in API + // documentation. + + p_buffer[0] = BLE_RPC_PKT_CMD; + uint32_t buffer_length = HCI_TRANSPORT_PKT_DATA_SIZE - 1u; + + const uint32_t err_code = temp_get_req_enc(p_temp, + &(p_buffer[1]), + &buffer_length); + APP_ERROR_CHECK(err_code); + + // @note: Increment buffer length as internally managed packet type field must be included. + ble_encode_transport_cmd_write(p_buffer, + (++buffer_length), + BLE_ENCODE_WRITE_MODE_RESP, + mw_temp_get_rsp_dec); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/ble/src/raw_transport.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,320 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "hal_transport.h" +#include <stdbool.h> +#include <string.h> +#include "hal_transport_config.h" +#include "app_uart_stream.h" +#include "app_error.h" +#include "app_util.h" + +typedef enum +{ + TRANSPORT_RECEIVE_PKT_HEADER, + TRANSPORT_RECEIVE_PKT_DATA +} transport_receive_pkt_state_t; + +static bool m_tx_buffer_in_use = false; /**< Indicates if transmission buffer is in use. */ +static bool m_rx_buffer_in_use = false; /**< Indicates if reception buffer is in use. */ +static uint8_t m_tx_buffer[HCI_TRANSPORT_PKT_BUFFER_SIZE]; /**< Transmission buffer. */ +static uint8_t m_rx_buffer[HCI_TRANSPORT_PKT_BUFFER_SIZE]; /**< Reception buffer. */ + +static transport_receive_pkt_state_t m_receive_state; /**< Receive state. */ +static hci_transport_event_handler_t m_event_handler = NULL; /**< Transport event handler callback function. */ +static hci_transport_tx_done_handler_t m_tx_done_handler = NULL; /**< Transport tx done handler callback function. */ + +static void uart_stream_event_handle(app_uart_stream_evt_t uart_stream_event) +{ + if (uart_stream_event.event == APP_UART_STREAM_RX_RDY) + { + if (m_receive_state == TRANSPORT_RECEIVE_PKT_HEADER) + { + uint8_t * data = (uint8_t *)uart_stream_event.param1; + + // @todo: Evaluate to use data_len to trigger state changes instead of depending + // on rx_pkt_consume function to reset state and setting buffer. + // uint16_t data_len = (uint16_t)uart_stream_event.param2; + + // Expect next packet to be data. + m_receive_state = TRANSPORT_RECEIVE_PKT_DATA; + + // Decode packet length data + uint16_t decoded_data_len = uint16_decode(data); + + if (decoded_data_len > (HCI_TRANSPORT_PKT_DATA_SIZE)) + { + APP_ERROR_CHECK_BOOL(false); + } + + // Copy the header to the first bytes of the rx buffer. + memcpy(&m_rx_buffer[0], data, HCI_TRANSPORT_PKT_HEADER_SIZE); + + // Set uart stream module to receive data packet. + // 4 bytes allocated for packet size info. + uint32_t err_code = app_uart_stream_rx_buffer_set( + &m_rx_buffer[HCI_TRANSPORT_PKT_HEADER_SIZE], + decoded_data_len, false); + + (void)err_code; + + if (m_event_handler != NULL) + { + // Generate a transport event indicating that we are finished + // receiving a packet. + hci_transport_evt_t event; + event.evt_type = HCI_TRANSPORT_RX_STARTED; + + // Call application event callback function. + m_event_handler(event); + } + + } + else if (m_receive_state == TRANSPORT_RECEIVE_PKT_DATA) + { + + // Mark rx buffer as in use. + // Only marked as in use when a whole packet is received, header+data. This is + // done to prevent consume to be able to free something that is not fully + // received. + m_rx_buffer_in_use = true; + + if (m_event_handler != NULL) + { + // Generate a transport event indicating that we are finished + // receiving a packet. + hci_transport_evt_t event; + event.evt_type = HCI_TRANSPORT_RX_RDY; + + // Call application event callback function. + m_event_handler(event); + } + } + } + else if (uart_stream_event.event == APP_UART_STREAM_TX_DONE) + { + // Call application tx done event callback function. + if (m_tx_done_handler != NULL) + { + // Generate a transport event indicating that we are finished + // transmitting a packet. + hci_transport_tx_done_result_t event = HCI_TRANSPORT_TX_DONE_SUCCESS; + + m_tx_done_handler(event); + } + } +} + + +uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler) +{ + // Check that handler is valid. + if (event_handler == NULL) + { + return NRF_ERROR_NULL; + } + + m_event_handler = event_handler; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler) +{ + // Check that handler is valid. + if (event_handler == NULL) + { + return NRF_ERROR_NULL; + } + + m_tx_done_handler = event_handler; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_open(void) +{ + + // Register callback function for uart_stream events. + uint32_t err_code = app_uart_stream_evt_handler_reg(uart_stream_event_handle); + if (err_code != NRF_SUCCESS) + { + return NRF_ERROR_INTERNAL; + } + + // Initilize uart_stream to start receving header. + m_receive_state = TRANSPORT_RECEIVE_PKT_HEADER; + + err_code = app_uart_stream_rx_buffer_set(&m_rx_buffer[0], HCI_TRANSPORT_PKT_HEADER_SIZE, true); + if (err_code != NRF_SUCCESS) + { + return NRF_ERROR_INTERNAL; + } + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_close(void) +{ + // Reset callback handlers. + m_event_handler = NULL; + m_tx_done_handler = NULL; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory) +{ + if (pp_memory == NULL) + { + return NRF_ERROR_NULL; + } + + if (m_tx_buffer_in_use) + { + return NRF_ERROR_NO_MEM; + } + + + + // Return address to first byte after the transport pkt header placeholder. + *pp_memory = m_tx_buffer + HCI_TRANSPORT_PKT_HEADER_SIZE; + m_tx_buffer_in_use = true; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_tx_free(void) +{ + m_tx_buffer_in_use = false; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length) +{ + if (p_buffer == NULL) + { + return NRF_ERROR_NULL; + } + + if (!m_tx_buffer_in_use) + { + return NRF_ERROR_INVALID_STATE; + } + + if (length > (HCI_TRANSPORT_PKT_BUFFER_SIZE - HCI_TRANSPORT_PKT_HEADER_SIZE)) + { + // length exceeds available memory provided by the transport module. + return NRF_ERROR_DATA_SIZE; + } + + // Calculate base pointer of the provided buffer. + uint8_t * p_base = (uint8_t *)(p_buffer - HCI_TRANSPORT_PKT_HEADER_SIZE); + + // Verify that the memory address is the allocated in m_tx_buffer. + if (p_base != m_tx_buffer) + { + return NRF_ERROR_INVALID_ADDR; + } + + // Add length info to the packet in front of the data. + (void)uint16_encode(length, p_base); + + return app_uart_stream_write(p_base, length + HCI_TRANSPORT_PKT_HEADER_SIZE); +} + + +uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length) +{ + if (pp_buffer == NULL) + { + return NRF_ERROR_NULL; + } + + if (p_length == NULL) + { + return NRF_ERROR_NULL; + } + + if (!m_rx_buffer_in_use) + { + return NRF_ERROR_NO_MEM; + } + + // Extract packet length from rx buffer. + *p_length = uint16_decode(&m_rx_buffer[0]); + *pp_buffer = &m_rx_buffer[HCI_TRANSPORT_PKT_HEADER_SIZE]; + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer) +{ + // If there is nothing to consume + if (!m_rx_buffer_in_use) + { + return NRF_ERROR_NO_MEM; + } + + // Calculate base pointer of the provided buffer. + uint8_t * p_base = (uint8_t *)(p_buffer - HCI_TRANSPORT_PKT_HEADER_SIZE); + + // Verify that the memory address is the allocated in m_tx_buffer. + if (p_base != m_rx_buffer) + { + return NRF_ERROR_INVALID_ADDR; + } + + // Release rx buffer for use. + m_rx_buffer_in_use = false; + + // Expect next packet to be header. + m_receive_state = TRANSPORT_RECEIVE_PKT_HEADER; + + uint32_t err_code = app_uart_stream_rx_buffer_set(p_base, HCI_TRANSPORT_PKT_HEADER_SIZE, true); + + (void)err_code; + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,421 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_APP_H__ +#define BLE_APP_H__ +/**@file + * + * @defgroup ble_app Application command request encoders and command response decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief Application command request encoders and command response decoders. + */ +#include "ble.h" +/** + * @brief Encodes @ref sd_ble_tx_buffer_count_get command request. + * + * @sa @ref nrf51_tx_buffer_count_get_encoding for packet format, + * @ref ble_tx_buffer_count_get_rsp_dec for command response decoder. + * + * @param[in] p_count Pointer to count value to be filled. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_count will not be updated by the command + * request encoder. Updated values are set by @ref ble_tx_buffer_count_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_tx_buffer_count_get_req_enc(uint8_t const * const p_count, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_tx_buffer_count_get command. + * + * @sa @ref nrf51_tx_buffer_count_get_encoding for packet format, + * @ref ble_tx_buffer_count_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] pp_count Pointer to pointer to count value. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_tx_buffer_count_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * * const pp_count, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_uuid_encode command request. + * + * @sa @ref nrf51_uuid_encode_encoding for packet format, + * @ref ble_uuid_encode_rsp_dec for command response decoder. + * + * @param[in] p_uuid Pointer to a @ref ble_uuid_t structure that will be encoded into bytes. + * @param[in] p_uuid_le_len Size of \p p_uuid_le if \p p_uuid_le is not NULL + * @param[in] p_uuid_le Pointer to a buffer where the little endian raw UUID bytes(2 or 16) + * will be stored. Can be NULL to calculate required size. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_uuid_le_len and \p p_uuid_le will not be updated by the command + * request encoder. Updated values are set by @ref ble_uuid_encode_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_uuid_encode_req_enc(ble_uuid_t const * const p_uuid, + uint8_t const * const p_uuid_le_len, + uint8_t const * const p_uuid_le, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_uuid_encode command. + * + * @sa @ref nrf51_uuid_encode_encoding for packet format, + * @ref ble_uuid_encode_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[in,out] p_uuid_le_len \c in: Size (in bytes) of \p p_uuid_le buffer. + * \c out: Length of decoded contents of \p p_uuid_le. + * @param[out] p_uuid_le Pointer to a buffer where the encoded UUID will be stored. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Length of \p p_uuid_le is too small to hold decoded + * value from response. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_uuid_encode_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_uuid_le_len, + uint8_t * const p_uuid_le, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_uuid_decode command request. + * + * @sa @ref nrf51_uuid_decode_encoding for packet format, + * @ref ble_uuid_decode_rsp_dec for command response decoder. + * + * @param[in] p_uuid_le_len Size of \p p_uuid_le if \p p_uuid_le is not NULL + * @param[in] p_uuid_le Pointer to a buffer where the little endian raw UUID bytes(2 or 16) + * is stored. + * @param[out] p_uuid Pointer to a @ref ble_uuid_t structure were raw UUID will be decoded. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_uuid will not be updated by the command request encoder. + * Updated values are set by @ref ble_uuid_decode_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_uuid_decode_req_enc(uint8_t uuid_le_len, + uint8_t const * const p_uuid_le, + ble_uuid_t * const p_uuid, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_uuid_decode command. + * + * @sa @ref nrf51_uuid_decode_encoding for packet format, + * @ref ble_uuid_decode_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_uuid Pointer to a buffer where the decoded UUID will be stored. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_uuid_decode_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_uuid_t * * const p_uuid, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_uuid_vs_add command request. + * + * @sa @ref nrf51_uuid_vs_add_encoding for packet format, + * @ref ble_uuid_vs_add_rsp_dec for command response decoder. + * + * @param[in] p_vs_uuid Pointer to a @ref ble_uuid128_t structure. + * @param[in] p_uuid_type Pointer to uint8_t where UUID type will be returned. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_uuid_type will not be updated by the command request encoder. + * Updated values are set by @ref ble_uuid_vs_add_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_uuid_vs_add_req_enc(ble_uuid128_t const * const p_vs_uuid, + uint8_t * const p_uuid_type, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_uuid_vs_add command. + * + * @sa @ref nrf51_uuid_vs_add_encoding for packet format, + * @ref ble_uuid_vs_add_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] pp_uuid_type Pointer to a pointer to uint8_t where the decoded UUID type will be stored. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_uuid_vs_add_rsp_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint8_t * * const pp_uuid_type, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_version_encode command request. + * + * @sa @ref nrf51_version_get_encoding for packet format, + * @ref ble_version_get_rsp_dec for command response decoder. + * + * @param[in] p_version Pointer to a @ref ble_version_t structure to be filled by the response. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_version_get_req_enc(ble_version_t const * const p_version, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_version_get command. + * + * @sa @ref nrf51_version_get_encoding for packet format, + * @ref ble_version_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_version Pointer to a @ref ble_version_t where decoded version will be stored. + * @param[out] p_result_code Command result code. + * + * @return NRF_SUCCESS Version information stored successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_version_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_version_t * p_version, + uint32_t * const p_result_code); + + +/**@brief Encodes @ref sd_ble_opt_set command request. + * + * @sa @ref nrf51_opt_set_encoding for packet format, + * @ref ble_opt_set_rsp_dec for command response decoder. + * + * @param[in] opt_id Identifies type of parameter in ble_opt_t union. + * @param[in] p_opt Pointer to ble_opt_t union. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_PARAM Invalid opt id. + */ +uint32_t ble_opt_set_req_enc(uint32_t const opt_id, + ble_opt_t const * const p_opt, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_opt_set command. + * + * @sa @ref nrf51_opt_set_encoding for packet format, + * @ref ble_opt_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @return NRF_SUCCESS Version information stored successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t ble_opt_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_enable command request. + * + * @sa @ref nrf51_enable_encoding for packet format, + * @ref ble_enable_rsp_dec for command response decoder. + * + * @param[in] p_enable Pointer to a @ref ble_enable_params_t structure. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_enable_req_enc(ble_enable_params_t * p_ble_enable_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_enable command. + * + * @sa @ref nrf51_enable_encoding for packet format, + * @ref ble_enable_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @return NRF_SUCCESS Success. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_enable_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + + +/**@brief Encodes @ref sd_ble_opt_get_encode command request. + * + * @sa @ref nrf51_ble_opt_get_encoding for packet format, + * @ref ble_opt_get_rsp_dec for command response decoder. + * + * @param[in] opt_id Identifies type of parameter in ble_opt_t union. + * @param[in] p_opt Pointer to a @ref ble_opt_t union to be filled by the response. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_PARAM Invalid opt id. + */ +uint32_t ble_opt_get_req_enc(uint32_t opt_id, + ble_opt_t const * const p_opt, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + + + +/**@brief Decodes response to @ref sd_ble_opt_get command. + * + * @sa @ref nrf51_ble_opt_get_encoding for packet format, + * @ref ble_opt_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_opt_id Pointer to a decoded opt_id + * @param[out] p_opt Pointer to a decoded @ref ble_opt_t union + * @param[out] p_result_code Command result code. + * + * @return NRF_SUCCESS Opt stored successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + * @retval NRF_ERROR_INVALID_PARAM Invalid opt id. + */ + +uint32_t ble_opt_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_opt_id, + ble_opt_t * const p_opt, + uint32_t * const p_result_code); + + +/**@brief Event decoding dispatcher. + * + * The event decoding dispatcher will route the event packet to the correct decoder which in turn + * decodes the contents of the event and updates the \p p_event struct. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to beginning of event packet. + * @param[in] packet_len Length (in bytes) of event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + * @retval NRF_ERROR_NOT_FOUND Decoding failure. No event decoder is available. + */ +uint32_t ble_event_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_evt_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,73 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_EVT_APP_H__ +#define BLE_EVT_APP_H__ +/**@file + * + * @defgroup ble_evt_app Application event decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief Application event decoders. + */ +#include "ble.h" + +/** + * @brief Decodes ble_evt_tx_complete event. + * + * @sa @ref nrf51_evt_tx_complete_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_evt_tx_complete_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gap_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,928 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GAP_APP_H__ +#define BLE_GAP_APP_H__ + +/**@file + * + * @defgroup ble_gap_app GAP Application command request encoders and command response decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GAP Application command request encoders and command response decoders. + */ +#include "ble.h" +#include "ble_gap.h" + +/** + * @brief Encodes @ref sd_ble_gap_address_get command request. + * + * @sa @ref nrf51_address_get_encoding for packet format, + * @ref ble_gap_address_get_rsp_dec for command response decoder. + * + * @param[in] p_address Pointer to address. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_address will not be updated by the command + * request encoder. Updated values are set by @ref ble_gap_address_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_address_get_req_enc(ble_gap_addr_t const * const p_address, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_address_get command. + * + * @sa @ref nrf51_address_get_encoding for packet format, + * @ref ble_gap_address_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_address Pointer to address. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_address_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_addr_t * const p_address, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_address_set command request. + * + * @sa @ref nrf51_gap_addr_encoding for packet format, + * @ref ble_gap_address_set_rsp_dec for command response decoder. + * + * @param[in] addr_cycle_mode Address cycle mode. + * @param[in] p_addr Pointer to address structure. + * @param[in,out] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_address_set_req_enc(uint8_t addr_cycle_mode, + ble_gap_addr_t const * const p_addr, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_address_set command. + * + * @sa @ref nrf51_gap_addr_encoding for packet format, + * @ref ble_gap_address_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_address_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_adv_data_set command request. + * + * @sa @ref nrf51_adv_set_encoding for packet format, + * @ref ble_gap_adv_data_set_rsp_dec for command response decoder. + * + * @param[in] p_data Raw data to be placed in advertisement packet. If NULL, no changes + * are made to the current advertisement packet data. + * @param[in] dlen Data length for p_data. Max size: @ref BLE_GAP_ADV_MAX_SIZE octets. + * Should be 0 if p_data is NULL, can be 0 if p_data is not NULL. + * @param[in] p_sr_data Raw data to be placed in scan response packet. If NULL, + * no changes are made to the current scan response packet data. + * @param[in] srdlen Data length for p_sr_data. Max size: @ref BLE_GAP_ADV_MAX_SIZE octets. + * Should be 0 if p_sr_data is NULL, can be 0 if p_data is not NULL. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_adv_data_set_req_enc(uint8_t const * const p_data, + uint8_t dlen, + uint8_t const * const p_sr_data, + uint8_t srdlen, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_adv_data_set command. + * + * @sa @ref nrf51_adv_set_encoding for packet format, + * @ref ble_gap_adv_data_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_adv_data_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_adv_start command request. + * + * @sa @ref nrf51_adv_start_encoding for packet format, + * @ref ble_gap_adv_start_rsp_dec for command response decoder. + * + * @param[in] p_adv_params Pointer to advertising parameters structure. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_adv_start_req_enc(ble_gap_adv_params_t const * const p_adv_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_adv_start command. + * + * @sa @ref nrf51_adv_start_encoding for packet format, + * @ref ble_gap_adv_start_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_adv_start_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_tx_power_set command request. + * + * @sa @ref nrf51_tx_power_set_encoding for packet format, + * @ref ble_gap_tx_power_set_rsp_dec for command response decoder. + * + * @param[in] tx_power Radio transmit power in dBm (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm). + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_tx_power_set_req_enc(int8_t tx_power, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_tx_power_set command. + * + * @sa @ref nrf51_tx_power_set_encoding for packet format, + * @ref ble_gap_adv_start_req_snc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_tx_power_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_appearance_get command request. + * + * @sa @ref nrf51_appearance_get_encoding for packet format, + * @ref ble_gap_appearance_get_rsp_dec for command response decoder. + * + * @param[in] p_appearance Appearance (16-bit), see @ref BLE_APPEARANCES. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_appearance will not be updated by the command + * request encoder. Updated values are set by @ref ble_gap_appearance_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_appearance_get_req_enc(uint16_t const * const p_appearance, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_appearance_get command. + * + * @sa @ref nrf51_appearance_get_encoding for packet format, + * @ref ble_gap_appearance_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_appearance Appearance (16-bit), see @ref BLE_APPEARANCES. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_appearance_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_appearance, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_appearance_set command request. + * + * @sa @ref nrf51_appearance_set_encoding for packet format, + * @ref ble_gap_appearance_set_rsp_dec for command response decoder. + * + * @param[in] p_appearance Appearance (16-bit), see @ref BLE_APPEARANCES. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_appearance_set_req_enc(uint16_t appearance, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_appearance_set command. + * + * @sa @ref nrf51_appearance_set_encoding for packet format, + * @ref ble_gap_adv_start_req_snc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_appearance_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_device_name_get command request. + * + * @sa @ref nrf51_device_name_get_encoding for packet format, + * @ref ble_gap_device_name_get_rsp_dec for command response decoder. + * + * @param[in] p_dev_name Pointer to an empty buffer where the UTF-8 <b>non NULL-terminated</b> + * string will be placed. Set to NULL to obtain the complete device + * name length. + * @param[in] p_len Length of the buffer pointed by p_dev_name. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_dev_name and \p p_len will not be updated by the command + * request encoder. Updated values are set by @ref ble_gap_device_name_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_device_name_get_req_enc(uint8_t const * const p_dev_name, + uint16_t const * const p_dev_name_len, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_device_name_get command. + * + * @sa @ref nrf51_device_name_get_encoding for packet format, + * @ref ble_gap_device_name_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_dev_name Pointer to an empty buffer where the UTF-8 + * <b>non NULL-terminated</b> string will be placed. + * @param[in,out] p_dev_namelen Length of the buffer pointed by p_dev_name, complete device name + * length on output. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_device_name_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_dev_name, + uint16_t * const p_dev_name_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_device_name_set command request. + * + * @sa @ref nrf51_device_name_set_encoding for packet format, + * @ref ble_gap_device_name_set_rsp_dec for command response decoder. + * + * @param[in] p_write_perm Write permissions for the Device Name characteristic see + * @ref ble_gap_conn_sec_mode_t. + * @param[in] p_dev_name Pointer to a UTF-8 encoded, <b>non NULL-terminated</b> string. + * @param[in] len Length of the UTF-8, <b>non NULL-terminated</b> string pointed + * to by p_dev_name in octets (must be smaller or equal + * than @ref BLE_GAP_DEVNAME_MAX_LEN). + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_device_name_set_req_enc(ble_gap_conn_sec_mode_t const * const p_write_perm, + uint8_t const * const p_dev_name, + uint16_t len, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_device_name_set command. + * + * @sa @ref nrf51_device_name_set_encoding for packet format, + * @ref ble_gap_device_name_get_req_snc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_device_name_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** + * @brief Encodes @ref sd_ble_gap_ppcp_set command request. + * + * @sa @ref nrf51_ppcp_set_encoding for packet format, + * @ref ble_gap_ppcp_set_rsp_dec for command response decoder. + * + * @param[in] p_conn_params Pointer to a @ref ble_gap_conn_params_t structure with the + * desired parameters. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_ppcp_set_req_enc(ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_gap_ppcp_set command. + * + * @sa @ref nrf51_ppcp_set_encoding for packet format, + * @ref ble_gap_ppcp_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gap_ppcp_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_conn_param_update command request. + * + * @sa @ref nrf51_gap_conn_param_update_encoding for packet format, + * @ref ble_gap_conn_param_update_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_conn_params Pointer to desired connection parameters.. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_conn_param_update_req_enc(uint16_t conn_handle, + ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_conn_param_update command. + * + * @sa @ref nrf51_gap_conn_param_update_encoding for packet format, + * @ref ble_gap_conn_param_update_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_conn_param_update_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_disconnect command request. + * + * @sa @ref nrf51_disconnect_encoding for packet format, + * @ref ble_gap_disconnect_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] hci_status_code HCI status code, see @ref BLE_HCI_STATUS_CODES. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_disconnect_req_enc(uint16_t conn_handle, + uint8_t hci_status_code, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_disconnect command. + * + * @sa @ref nrf51_disconnect_encoding for packet format, + * @ref ble_gap_disconnect_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_disconnect_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + + +/**@brief Encodes @ref sd_ble_gap_rssi_stop command request. + * + * @sa @ref nrf51_rssi_stop for packet format, + * @ref ble_gap_rssi_stop_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_rssi_stop_req_enc(uint16_t conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble__rssi_stop command. + * + * @sa @ref nrf51_rssi_stop for packet format, + * @ref ble_gap_rssi_stop_rsp_dec for command response decoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_rssi_stop_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + + + + +/**@brief Encodes @ref sd_ble_gap_ppcp_get command request. + * + * @sa @ref nrf51_gap_ppcp_get_encoding for packet format, + * @ref ble_gap_ppcp_get_rsp_dec for command response decoder. + * + * @param[in] p_conn_params Pointer to a @ref ble_gap_conn_params_t structure where the + * parameters will be stored. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_conn_params will not be updated by the command request encoder. Updated values are + * set by @ref ble_gap_ppcp_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_ppcp_get_req_enc(ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_ppcp_get command. + * + * @sa @ref nrf51_gap_ppcp_get_encoding for packet format, + * @ref ble_gap_ppcp_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_conn_params Pointer to a @ref ble_gap_conn_params_t structure where the parameters + * will be stored. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_ppcp_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_conn_params_t * const p_conn_params, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_auth_key_reply command request. + * + * @sa @ref nrf51_gap_auth_key_reply_encoding for packet format, + * @ref ble_gap_auth_key_reply_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] key_type Key type which defines length of key data as defined for + * @ref sd_ble_gap_auth_key_reply . + * @param[in] p_key Pointer to a buffer which contains key + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_PARAM Encoding failure. Incorrect param provided (key_type). + */ +uint32_t ble_gap_auth_key_reply_req_enc(uint16_t conn_handle, + uint8_t key_type, + uint8_t const * const p_key, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_auth_key_reply command. + * + * @sa @ref nrf51_gap_auth_key_reply_encoding for packet format, + * @ref ble_gap_sec_auth_key_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_auth_key_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_sec_info_reply command request. + * + * @sa @ref nrf51_gap_sec_info_reply_encoding for packet format, + * @ref ble_gap_sec_info_reply_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_enc_info Pointer to a @ref ble_gap_enc_info_t encryption information + * structure. + * @param[in] p_sign_info Pointer to a @ref ble_gap_sign_info_t signing information + * structure. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_sec_info_reply_req_enc(uint16_t conn_handle, + ble_gap_enc_info_t const * const p_enc_info, + ble_gap_sign_info_t const * const p_sign_info, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_sec_info_reply command. + * + * @sa @ref nrf51_gap_sec_info_reply_encoding for packet format, + * @ref ble_gap_sec_info_reply_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_sec_info_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_sec_params_reply command request. + * + * @sa @ref nrf51_sec_params_reply_encoding for packet format, + * @ref ble_gap_sec_params_reply_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] sec_status Security status, see @ref BLE_GAP_SEC_STATUS. + * @param[in] p_sec_params Pointer to a @ref ble_gap_sec_params_t security parameters + * structure. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_sec_params_reply_req_enc(uint16_t conn_handle, + uint8_t sec_status, + ble_gap_sec_params_t const * const p_sec_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_sec_params_reply command. + * + * @sa @ref nrf51_sec_params_reply_encoding for packet format, + * @ref ble_gap_sec_params_reply_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_sec_params_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_authenticate command request. + * + * @sa @ref nrf51_ble_gap_authenticate_encoding for packet format, + * @ref ble_gap_authenticate_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_sec_params Pointer to a @ref ble_gap_sec_params_t security parameters + * structure. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_authenticate_req_enc(uint16_t conn_handle, + ble_gap_sec_params_t const * const p_sec_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_authenticate command. + * + * @sa @ref nrf51_ble_gap_authenticate_encoding for packet format, + * @ref ble_gap_authenticate_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_authenticate_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_adv_stop command request. + * + * @sa @ref nrf51_sd_ble_gap_adv_stop for packet format, + * @ref ble_gap_adv_stop_rsp_dec for command response decoder. + * + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_adv_stop_req_enc(uint8_t * const p_buf, uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_adv_stop command. + * + * @sa @ref nrf51_sd_ble_gap_adv_stop for packet format, + * @ref ble_gap_adv_stop_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_adv_stop_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_conn_sec_get command request. + * + * @sa @ref nrf51_conn_sec_get_encoding for packet format, + * @ref ble_gap_conn_sec_get_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_conn_sec Pointer to \ref ble_gap_conn_sec_t which will be filled in + * response. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_conn_sec_get_req_enc(uint16_t conn_handle, + ble_gap_conn_sec_t const * const p_conn_sec, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_conn_sec_get command. + * + * @sa @ref nrf51_conn_sec_get_encoding for packet format, + * @ref ble_gap_conn_sec_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] pp_conn_sec Pointer to pointer to \ref ble_gap_conn_sec_t which will be filled by + * the decoded data (if present). + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_conn_sec_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_conn_sec_t * * const pp_conn_sec, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gap_rssi_start command request. + * + * @sa @ref nrf51_rssi_start_encoding for packet format, + * @ref ble_gap_rssi_start_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gap_rssi_start_req_enc(uint16_t conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gap_rssi_start command. + * + * @sa @ref nrf51_rssi_start_encoding for packet format, + * @ref ble_gap_rssi_start_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gap_rssi_start_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gap_evt_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,323 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GAP_EVT_APP_H__ +#define BLE_GAP_EVT_APP_H__ +/**@file + * + * @defgroup ble_gap_evt_app GAP Application event decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GAP Application event decoders. + */ +#include "ble.h" + +/** + * @brief Decodes ble_gap_evt_auth_key_request event. + * + * @sa @ref nrf51_gap_evt_auth_key_request_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_auth_key_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_auth_status event. + * + * @sa @ref nrf51_gap_evt_auth_status_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_auth_status_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_conn_param_update event. + * + * @sa @ref nrf51_gap_evt_conn_param_update_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_conn_param_update_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_conn_sec_update event. + * + * @sa @ref nrf51_gap_evt_conn_sec_update_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_conn_sec_update_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_connected event. + * + * @sa @ref nrf51_gap_evt_connected_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_connected_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_disconnected event. + * + * @sa @ref nrf51_gap_evt_disconnected_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_disconnected_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_passkey_display event. + * + * @sa @ref nrf51_gap_evt_passkey_display_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_passkey_display_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_rssi_changed event. + * + * @sa @ref nrf51_gap_evt_rssi_changed_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_rssi_changed_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_sec_info_request event. + * + * @sa @ref nrf51_gap_evt_sec_info_request_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_sec_info_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_sec_params_request event. + * + * @sa @ref nrf51_gap_evt_sec_params_request_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_sec_params_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gap_evt_timeout event. + * + * @sa @ref nrf51_gap_evt_timeout_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gap_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gap_struct_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,210 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap.h" + +uint32_t ble_gap_irk_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_irk_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_data); + +uint32_t ble_gap_addr_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_addr_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_addr); + +uint32_t ble_gap_sec_levels_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_sec_keys_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_enc_info_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_enc_info_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_enc_info); + +uint32_t ble_gap_sign_info_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_sign_info); + +uint32_t ble_gap_evt_auth_status_t_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_conn_sec_mode_enc(void const * const p_void_sec_mode, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_conn_sec_mode_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec_mode); + +uint32_t ble_gap_conn_sec_t_enc(void const * const p_void_sec, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_conn_sec_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec); + +uint32_t ble_gap_evt_conn_sec_update_t_enc(void const * const p_void_conn_sec_update, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_evt_conn_sec_update_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_conn_sec_update); + +uint32_t ble_gap_evt_sec_info_request_t_enc(void const * const p_void_sec_info_request, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_evt_sec_info_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec_info_request); +uint32_t ble_gap_evt_connected_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_sec_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_sec_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gap_evt_sec_params_request_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_evt_sec_params_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gap_conn_params_t_enc(void const * const p_void_conn_params, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_conn_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_conn_params); + +uint32_t ble_gap_evt_conn_param_update_t_enc(void const * const p_void_evt_conn_param_update, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_evt_conn_param_update_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_evt_conn_param_update); + +uint32_t ble_gap_evt_disconnected_t_enc(void const * const p_void_disconnected, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_evt_disconnected_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_disconnected); + +uint32_t ble_gap_opt_local_conn_latency_t_enc(void const * const p_void_local_conn_latency, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_opt_local_conn_latency_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_local_conn_latency); + +uint32_t ble_gap_opt_passkey_t_enc(void const * const p_void_passkey, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_opt_passkey_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_passkey); + +uint32_t ble_gap_opt_privacy_t_enc(void const * const p_void_privacy, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gap_opt_privacy_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_privacy); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gattc_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,418 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTC_APP_H__ +#define BLE_GATTC_APP_H__ +/**@file + * + * @defgroup ble_gattc_app GATTC Application command request encoders and command response decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GATTC Application command request encoders and command response decoders. + */ +#include "ble_gattc.h" +#include "ble.h" + +/**@brief Encodes @ref sd_ble_gattc_primary_services_discover command request. + * + * @sa @ref nrf51_sd_ble_gattc_primary_services_discover_encoding for packet format, + * @ref ble_gattc_primary_services_discover_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] start_handle Handle to start searching from. + * @param[in] p_srvc_uuid Pointer to a @ref ble_uuid_t which indicates the service UUID to + * be found. If it is NULL, all primary services will be returned. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_primary_services_discover_req_enc(uint16_t conn_handle, + uint16_t start_handle, + ble_uuid_t const * const p_srvc_uuid, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_primary_services_discover command. + * + * @sa @ref nrf51_sd_ble_gattc_primary_services_discover_encoding for packet format, + * @ref ble_gattc_primary_services_discover_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_primary_services_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_descriptors_discover command request. + * + * @sa @ref nrf51_sd_ble_gattc_descriptors_discover_encoding for packet format, + * @ref ble_gattc_primary_descriptors_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_handle_range A pointer to the range of handles of the Service to perform + * this procedure on. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_descriptors_discover_req_enc( + uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len); + + +/**@brief Decodes response to @ref sd_ble_gattc_descriptors_discover command. + * + * @sa @ref nrf51_sd_ble_gattc_descriptors_discover_encoding for packet format, + * @ref ble_gattc_primary_services_discover_rsp_dec for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_descriptors_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_relationships_discover command request. + * + * @sa @ref nrf51_sd_ble_gattc_relationships_discover_encoding for packet format, + * @ref ble_gattc_relationships_discover_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_handle_range A pointer to the range of handles of the Service to perform + * this procedure on. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_relationships_discover_req_enc( + uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_relationships_discover command. + * + * @sa @ref nrf51_sd_ble_gattc_relationships_discover_encoding for packet format, + * @ref ble_gattc_relationships_discover_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_relationships_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_characteristics_discover command request. + * + * @sa @ref nrf51_sd_ble_gattc_characteristics_discover_encoding for packet format, + * @ref ble_gattc_primary_characteristics_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_handle_range A pointer to the range of handles of the Service to perform + * this procedure on. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_characteristics_discover_req_enc + (uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_characteristics_discover command. + * + * @sa @ref nrf51_sd_ble_gattc_characteristics_discover_encoding for packet format, + * @ref ble_gattc_primary_services_discover_rsp_dec for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_characteristics_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_read command request. + * + * @sa @ref nrf51_sd_ble_gattc_read_encoding for packet format, + * @ref ble_gattc_read_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] Handle The handle of the attribute to be read. + * @param[in] Offset Offset into the attribute value to be read. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_read_req_enc(uint16_t conn_handle, + uint16_t handle, + uint16_t offset, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_read command. + * + * @sa @ref nrf51_sd_ble_gattc_read_encoding for packet format, + * @ref ble_gattc_read_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_char_values_read command request. + * + * @sa @ref nrf51_sd_ble_gattc_read_encoding for packet format, + * @ref ble_gattc_char_values_read_rsp_decc for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_handles A pointer to the handle(s) of the attribute(s) to be read. + * @param[in] handle_count The number of handles in p_handles. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_char_values_read_req_enc(uint16_t conn_handle, + uint16_t const * const p_handles, + uint16_t handle_count, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_read command. + * + * @sa @ref nrf51_sd_ble_gattc_read_encoding for packet format, + * @ref ble_gattc_char_values_read_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_char_values_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_write command request. + * + * @sa @ref nrf51_sd_ble_gattc_write_encoding for packet format, + * @ref ble_gattc_write_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_write_params Pointer to \ref GATTC Write params. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_write_req_enc(uint16_t conn_handle, + ble_gattc_write_params_t const * const p_write_params, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_write command. + * + * @sa @ref nrf51_sd_ble_gattc_write_encoding for packet format, + * @ref ble_gattc_write_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] result_code Command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_write_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_hv_confirm command request. + * + * @sa @ref nrf51_sd_ble_gattc_hv_confirm_encoding for packet format, + * @ref ble_gattc_hv_confirm_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] handle Handle of the attribute in the indication. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_hv_confirm_req_enc(uint16_t conn_handle, + uint16_t handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_hv_confirm command. + * + * @sa @ref nrf51_sd_ble_gattc_hv_confirm_encoding for packet format, + * @ref ble_gattc_hv_confirm_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Pointer to command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_hv_confirm_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gattc_char_value_by_uuid_read command request. + * + * @sa @ref nrf51_ssd_ble_gattc_char_value_by_uuid_read_encoding for packet format, + * @ref ble_gattc_char_value_by_uuid_read_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_uuid Pointer to a characteristic value UUID to read. + * @param[in] p_handle_range Pointer to the range of handles to perform this procedure on. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in, out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gattc_char_value_by_uuid_read_req_enc + (uint16_t conn_handle, + ble_uuid_t const * const p_uuid, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gattc_char_value_by_uuid_read command. + * + * @sa @ref nrf51_sd_ble_gattc_char_value_by_uuid_read_encoding for packet format, + * @ref ble_gattc_char_value_by_uuid_read_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Pointer to command response result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match expected + * operation code. + */ +uint32_t ble_gattc_char_value_by_uuid_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); +/** @} */ +#endif + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gattc_evt_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,298 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTC_EVT_APP_H__ +#define BLE_GATTC_EVT_APP_H__ +/**@file + * + * @defgroup ble_gattc_evt_app GATTC Application event decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GATTC Application event decoders. + */ +#include "ble.h" + +/** + * @brief Decodes ble_gattc_evt_char_disc_rsp event. + * + * @sa @ref nrf51_gattc_evt_char_disc_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_char_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_char_val_by_uuid_read_rsp event. + * + * @sa @ref nrf51_gattc_evt_char_val_by_uuid_read_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_char_vals_read_rsp event. + * + * @sa @ref nrf51_gattc_evt_char_vals_read_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_char_vals_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_desc_disc_rsp event. + * + * @sa @ref nrf51_gattc_evt_desc_disc_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_desc_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_hvx event. + * + * @sa @ref nrf51_gattc_evt_hvx_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_hvx_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_prim_srvc_disc_rsp event. + * + * @sa @ref nrf51_gattc_evt_prim_srvc_disc_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_prim_srvc_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_read_rsp event. + * + * @sa @ref nrf51_gattc_evt_read_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_rel_disc_rsp_dec event. + * + * @sa @ref nrf51_gattc_evt_rel_disc_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_rel_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_timeout event. + * + * @sa @ref nrf51_gattc_evt_timeout_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gattc_evt_write_rsp event. + * + * @sa @ref nrf51_gattc_evt_write_rsp_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gattc_evt_write_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gattc_struct_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,111 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTC_STRUCT_SERIALIZATION_H +#define BLE_GATTC_STRUCT_SERIALIZATION_H + +#include "ble_gattc.h" + +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_size, + void * const p_void_struct); + +uint32_t ble_gattc_evt_char_vals_read_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_evt_char_vals_read_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gattc_handle_range_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_handle_range_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gattc_service_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_service_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gattc_include_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_include_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gattc_evt_rel_disc_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_evt_rel_disc_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gattc_write_params_t_enc(void const * const p_void_write, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gattc_write_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_write); + +#endif /*BLE_GATTC_STRUCT_SERIALIZATION_H*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gatts_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,571 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTS_APP_H__ +#define BLE_GATTS_APP_H__ +/**@file + * + * @defgroup ble_gatts_app GATTS Application command request encoders and command response decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GATTS Application command request encoders and command response decoders. + */ +#include "ble_gatts.h" +#include "ble.h" + +/**@brief Encodes @ref sd_ble_gatts_value_get command request. + * + * @sa @ref nrf51_value_get_encoding for packet format, + * @ref ble_gatts_value_get_rsp_dec for command response decoder. + * + * @param[in] handle Attribute handle. + * @param[in] offset Offset in bytes to read from. + * @param[in] p_value Pointer to buffer where the requested value will be stored. + * Can be NULL to calculate required size. + * @param[in] p_value_len Size of \p p_value buffer if \p p_value is not NULL. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_data_len and \p p_data will not be updated by the command + * request encoder. Updated values are set by @ref ble_gatts_value_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_value_get_req_enc(uint16_t handle, + uint16_t offset, + uint16_t const * const p_value_len, + uint8_t const * const p_value, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_value_get command. + * + * @sa @ref nrf51_value_get_encoding for packet format, + * @ref ble_gatts_value_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] pp_value Pointer to pointera buffer where the requested value will be stored. + * @param[in,out] pp_value_len \c in: Size (in bytes) of \p p_value buffer. + * \c out: Length of decoded contents of \p p_value. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Length of \p p_value is too small to hold decoded + * value from response. + */ +uint32_t ble_gatts_value_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * * const pp_value, + uint16_t * * const pp_value_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_hvx command request. + * + * @sa @ref nrf51_gatts_hvx_encoding for packet format, + * @ref ble_gatts_hvx_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_hvx_params Pointer to an HVx parameters structure to be encoded. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_hvx_params will not be updated by the command + * request encoder. Updated values are set by @ref ble_gatts_hvx_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_hvx_req_enc(uint16_t conn_handle, + ble_gatts_hvx_params_t const * const p_hvx_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_hvx command. + * + * @sa @ref nrf51_gatts_hvx_encoding for packet format, + * @ref ble_gatts_hvx_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * @param[out] pp_bytes_written Pointer to pointer to location where number of bytes is written. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_hvx_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code, + uint16_t * * const pp_bytes_written); + +/**@brief Encodes @ref sd_ble_gatts_characteristic_add command request. + * + * @sa @ref nrf51_characteristics_add_encoding for packet format, + * @ref ble_gatts_characteristic_add_rsp_dec for command response decoder. + * + * @param[in] service_handle Handle of the service where the characteristic is to be placed. + * If @ref BLE_GATT_HANDLE_INVALID is used, it will be placed + * sequentially. + * @param[in] p_char_md Pointer to a @ref ble_gatts_char_md_t structure, characteristic + * metadata. + * @param[in] p_attr_char_value Pointer to a @ref ble_gatts_attr_t structure, corresponding to + * the characteristic value. + * @param[in] p_handles Pointer to a @ref ble_gatts_char_handles_t structure, where the + * assigned handles will be stored. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_handles will not be updated by the command + * request encoder. Updated values are set by @ref ble_gatts_characteristic_add_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_characteristic_add_req_enc + (uint16_t service_handle, + ble_gatts_char_md_t const * const p_char_md, + ble_gatts_attr_t const * const p_attr_char_value, + ble_gatts_char_handles_t const * const p_handles, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_characteristic_add command. + * + * @sa @ref nrf51_characteristics_add_encoding for packet format, + * @ref ble_gatts_characteristic_add_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] pp_handles Pointer to pointer to location where handles should be decoded. + * @param[out] p_result_code Pointer to command result code decode location. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_characteristic_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * * const pp_handles, + uint32_t * const p_result_code); + + +/**@brief Encodes @ref sd_ble_gatts_service_add command request. + * + * @sa @ref nrf51_gatts_service_add_encoding for packet format, + * @ref ble_gatts_service_add_rsp_dec for command response decoder. + * + * @param[in] type Toggles between primary and secondary services, + * see @ref BLE_GATTS_SRVC_TYPES. + * @param[in] p_uuid Pointer to service UUID. + * @param[in] p_conn_handle Pointer to a 16-bit word where the assigned handle will be stored. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_conn_handle will not be updated by the command + * request encoder. Updated values are set by @ref ble_gatts_service_add_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_service_add_req_enc(uint8_t type, + ble_uuid_t const * const p_uuid, + uint16_t const * const p_conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_service_add command. + * + * @sa @ref nrf51_gatts_service_add_encoding for packet format, + * @ref ble_gatts_service_add_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_conn_handle Connection handle. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_service_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_conn_handle, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_sys_attr_set command request. + * + * @sa @ref nrf51_gatts_sys_attr_set_encoding for packet format, + * @ref ble_gatts_sys_attr_set_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_sys_attr_data Pointer to a buffer (at least \p sys_attr_data_len bytes long) + * containing the attribute value to write. + * @param[in] sys_attr_data_len Length (in bytes) of \p p_sys_attr_data. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_sys_attr_set_req_enc(uint16_t conn_handle, + uint8_t const * const p_sys_attr_data, + uint16_t sys_attr_data_len, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_sys_attr_set command. + * + * @sa @ref nrf51_gatts_sys_attr_set_encoding for packet format, + * @ref ble_gatts_sys_attr_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_sys_attr_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_value_set command request. + * + * @sa @ref nrf51_gatts_value_set_encoding for packet format, + * @ref ble_gatts_value_set_rsp_dec for command response decoder. + * + * @param[in] handle Attribute handle. + * @param[in] offset Offset (in bytes) to write from. + * @param[in] p_value_len Pointer to length (in bytes) of \p p_value. + * @param[in] p_value Pointer to a buffer (at least \p value_len bytes long) containing the + * attribute value to write. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_value_set_req_enc(uint16_t handle, + uint16_t offset, + uint16_t * const p_value_len, + uint8_t const * const p_value, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_value_set command. + * + * @sa @ref nrf51_gatts_value_set_encoding for packet format, + * @ref ble_gatts_value_set_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_value_len Length (in bytes) written. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_value_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_value_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_sys_attr_get command request. + * + * @sa @ref nrf51_gatts_sys_attr_get_encoding for packet format, + * @ref ble_gatts_sys_attr_get_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle of the connection. + * @param[in] p_sys_attr_data Pointer to buffer where updated information about system + * attributes will be stored. Can be NULL to calculate required + * size. + * @param[in] p_sys_attr_data_len Size of p_sys_attr_data buffer if \p p_sys_attr_data is + * not NULL. + * @param[in,out] p_buf Pointer to buffer where encoded data command will + * be returned. + * @param[in,out] p_buf_len \c in: size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @note \p p_sys_attr_data and \p p_sys_attr_data_len will not be updated by the command + * request encoder. Updated values are set by @ref ble_gatts_sys_attr_get_rsp_dec. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_sys_attr_get_req_enc(uint16_t conn_handle, + uint8_t const * const p_sys_attr_data, + uint16_t const * const p_sys_attr_data_len, + uint8_t * const p_buf, + uint32_t * p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_sys_attr_get command. + * + * @sa @ref nrf51_gatts_sys_attr_get_encoding for packet format, + * @ref ble_gatts_sys_attr_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_sys_attr_data Pointer to a buffer where updated information about system + * attributes will be stored. + * @param[in,out] p_sys_attr_data_len \c in: Size (in bytes) of \p p_sys_attr_data buffer. + * \c out: Length of decoded contents of \p p_sys_attr_data. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Length of \p p_sys_attr_data is too small to hold decoded + * value from response. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_sys_attr_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_sys_attr_data, + uint16_t * const p_sys_attr_data_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_descriptor_add command request. + * + * @sa @ref nrf51_descriptor_add_encoding for packet format, + * @ref ble_gatts_descriptor_add_rsp_dec for command response decoder. + * + * @param[in] char_handle Handle of the characteristic where the description is to be placed. + * If @ref BLE_GATT_HANDLE_INVALID is used, it will be placed + * sequentially. + * @param[in] p_attr_md Pointer to a @ref ble_gatts_attr_t structure, characteristic + * metadata. + * @param[in] p_handle Pointer to a @ref ble_gatts_char_handles_t structure, where the + * assigned handles will be stored. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_descriptor_add_req_enc(uint16_t char_handle, + ble_gatts_attr_t const * const p_attr, + uint16_t * const p_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_descriptor_add command. + * + * @sa @ref nrf51_descriptor_add_encoding for packet format, + * @ref ble_gatts_descriptor_add_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_handle Pointer to bufer where descriptor handle will be + returned. + * @param[out] p_result_code Pointer to command result code decode location. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_descriptor_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_handle, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_include_add command request. + * + * @sa @ref nrf51_include_add_encoding for packet format, + * @ref ble_gatts_include_add_rsp_dec for command response decoder. + * + * @param[in] service_handle Handle of the service where the included service is to be placed. + * @param[in] inc_srvc_handle Handle of the included service + * @param[in] p_include_handle Pointer to Pointer to a 16-bit word where the assigned handle will be stored. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_gatts_include_add_req_enc(uint16_t service_handle, + uint16_t inc_srvc_handle, + uint16_t * const p_include_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_include_add command. + * + * @sa @ref nrf51_include_add_encoding for packet format, + * @ref ble_gatts_include_add_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_include_handle Pointer to a 16-bit word where the assigned handle will be stored. + * @param[out] p_result_code Pointer to command result code decode location. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_include_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_include_handle, + uint32_t * const p_result_code); + + +/**@brief Encodes @ref sd_ble_gatts_rw_authorize_reply command request. + * + * @sa @ref nrf51_gatts_rw_authorize_reply_encoding for packet format, + * @ref ble_gatts_rw_authorize_reply_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle. + * @param[in] p_reply_params Pointer to \ref ble_gatts_rw_authorize_reply_params_t + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_PARAM Encoding failure. Invalid param provided in p_reply_params. + */ +uint32_t ble_gatts_rw_authorize_reply_req_enc( + uint16_t conn_handle, + ble_gatts_rw_authorize_reply_params_t const * const + p_reply_params, + uint8_t * const + p_buf, + uint32_t * const + p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_rw_authorize_reply command. + * + * @sa @ref nrf51_gatts_rw_authorize_reply_encoding for packet format, + * @ref ble_gatts_rw_authorize_reply_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_rw_authorize_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Encodes @ref sd_ble_gatts_service_changed command request. + * + * @sa @ref nrf51_gatts_service_chhanged_encoding for packet format, + * @ref ble_gatts_service_changed_rsp_dec for command response decoder. + * + * @param[in] conn_handle Connection handle. + * @param[in] start_handle Start of affected attribute handle range. + * @param[in] end_handle End of affected attribute handle range. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_PARAM Encoding failure. Invalid param provided in p_reply_params. + */ +uint32_t ble_gatts_service_changed_req_enc(uint16_t conn_handle, + uint16_t start_handle, + uint16_t end_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_ble_gatts_service_changed command. + * + * @sa @ref nrf51_gatts_service_changed_encoding for packet format, + * @ref ble_gatts_service_changed_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_gatts_service_changed_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/** @} */ +#endif //BLE_GATTS_APP_H__ + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gatts_evt_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,198 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTS_EVT_APP_H__ +#define BLE_GATTS_EVT_APP_H__ +/**@file + * + * @defgroup ble_gatts_evt_app GATTS Application event decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief GATTS Application event decoders. + */ +#include "ble.h" + +/** + * @brief Decodes ble_gatts_evt_hvc event. + * + * @sa @ref nrf51_gatts_evt_hvc_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_hvc_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gatts_evt_rw_authorize_request event. + * + * @sa @ref nrf51_gatts_evt_rw_authorize_request_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_rw_authorize_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gatts_evt_sc_confirm event. + * + * @sa @ref nrf51_gatts_evt_sc_confirm_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_sc_confirm_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gatts_evt_sys_attr_missing event. + * + * @sa @ref nrf51_gatts_evt_sys_attr_missing_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_sys_attr_missing_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gatts_evt_timeout event. + * + * @sa @ref nrf51_gatts_evt_timeout_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** + * @brief Decodes ble_gatts_evt_write event. + * + * @sa @ref nrf51_gatts_evt_write_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_gatts_evt_write_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_gatts_struct_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,173 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_GATTS_STRUCT_SERIALIZATION_H +#define BLE_GATTS_STRUCT_SERIALIZATION_H + +#include "ble_gatts.h" + +uint32_t ser_ble_gatts_char_pf_enc(void const * const p_void_char_pf, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ser_ble_gatts_char_pf_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_pf); + +uint32_t ble_gatts_attr_md_enc(void const * const p_void_attr_md, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_attr_md_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_attr_md); + +uint32_t ble_gatts_char_md_enc(void const * const p_void_char_md, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_char_md_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_md); + +uint32_t ble_gatts_attr_enc(void const * const p_void_gatts_attr, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_attr_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_gatts_attr); + +uint32_t ble_gatts_char_handles_enc(void const * const p_void_char_handles, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_char_handles_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_handles); + +uint32_t ble_gatts_attr_context_t_enc(void const * const p_void_attr_context, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_attr_context_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_attr_context); + +uint32_t ble_gatts_evt_write_t_enc(void const * const p_void_write, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_evt_write_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_write); + +uint32_t ble_gatts_hvx_params_t_enc(void const * const p_void_hvx_params, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_hvx_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_hvx_params); + +uint32_t ble_gatts_evt_read_t_enc(void const * const p_void_read, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_evt_read_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_read); + +uint32_t ble_gatts_evt_rw_authorize_request_t_enc(void const * const p_void_authorize_request, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_evt_rw_authorize_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_size, + void * const p_void_authorize_request); + +uint32_t ble_gatts_read_authorize_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_read_authorize_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gatts_write_authorize_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_write_authorize_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +uint32_t ble_gatts_rw_authorize_reply_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_gatts_rw_authorize_reply_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct); + +#endif /* BLE_GATTS_STRUCT_SERIALIZATION_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_l2cap_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,176 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + @addtogroup BLE_L2CAP Logical Link Control and Adaptation Protocol (L2CAP) + @{ + @brief Definitions and prototypes for the L2CAP interface. + */ + +#ifndef BLE_L2CAP_APP_H__ +#define BLE_L2CAP_APP_H__ + +#include "ble.h" +#include "ble_types.h" +#include "ble_ranges.h" +#include "ble_err.h" +#include "ble_l2cap.h" + +/**@brief Register a CID with L2CAP. + * + * @details This registers a higher protocol layer with the L2CAP multiplexer, and is requried prior to all operations on the CID. + * + * @param[in] cid L2CAP CID. + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_l2cap_cid_register_req_enc(uint16_t cid, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_l2cap_cid_register command. + * + * @sa @ref nrf51_adv_start_encoding for packet format, + * @ref ble_l2cap_cid_register_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_l2cap_cid_register_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Unregister a CID with L2CAP. + * + * @details This unregisters a previously registered higher protocol layer with the L2CAP multiplexer. + * + * @param[in] cid L2CAP CID. + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t ble_l2cap_cid_unregister_req_enc(uint16_t cid, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_l2cap_cid_unregister command. + * + * @sa @ref nrf51_adv_start_encoding for packet format, + * @ref ble_l2cap_cid_unregister_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_l2cap_cid_unregister_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + +/**@brief Transmit an L2CAP packet. + * + * @note It is important to note that a call to this function will <b>consume an application buffer</b>, and will therefore + * generate a @ref BLE_EVT_TX_COMPLETE event when the packet has been transmitted. + * Please see the documentation of @ref sd_ble_tx_buffer_count_get for more details. + * + * @param[in] conn_handle Connection Handle. + * @param[in] p_header Pointer to a packet header containing length and CID. + * @param[in] p_data Pointer to the data to be transmitted. + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in,out] p_buf_len \c in: Size of \p p_buf buffer. + * \c out: Length of encoded command packet. + * + * @return @ref NRF_SUCCESS Successfully queued an L2CAP packet for transmission. + * @return @ref NRF_ERROR_INVALID_ADDR Invalid pointer supplied. + * @return @ref NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, CIDs must be registered beforehand with @ref sd_ble_l2cap_cid_register. + * @return @ref NRF_ERROR_NOT_FOUND CID not found. + * @return @ref NRF_ERROR_NO_MEM Not enough memory to complete operation. + * @return @ref BLE_ERROR_NO_TX_BUFFERS Not enough application buffers available. + * @return @ref NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, see @ref BLE_L2CAP_MTU_DEF. + */ +uint32_t ble_l2cap_tx_req_enc(uint16_t conn_handle, + ble_l2cap_header_t const * const p_l2cap_header, + uint8_t const * const p_data, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/** + * @brief Decodes response to @ref sd_ble_l2cap_tx command. + * + * @sa @ref nrf51_adv_start_encoding for packet format, + * @ref ble_l2cap_tx_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_INVALID_DATA Decoding failure. Decoded operation code does not match + * expected operation code. + */ +uint32_t ble_l2cap_tx_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code); + + +#endif //BLE_L2CAP_APP_H__ + +/** + @} + */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_l2cap_evt_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,73 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef BLE_L2CAP_EVT_APP_H__ +#define BLE_L2CAP_EVT_APP_H__ +/**@file + * + * @defgroup ble_l2cap_evt_app L2CAP Application event decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief L2CAP Application event decoders. + */ +#include "ble.h" + +/** + * @brief Decodes ble_l2cap_evt_rx event. + * + * @sa @ref nrf51_l2cap_evt_rx_encoding for packet format. + * + * If \p p_event is null, the required length of \p p_event is returned in \p p_event_len. + * + * @param[in] p_buf Pointer to the beginning of an event packet. + * @param[in] packet_len Length (in bytes) of the event packet. + * @param[in,out] p_event Pointer to a \ref ble_evt_t buffer where the decoded event will be + * stored. If NULL, required length will be returned in \p p_event_len. + * @param[in,out] p_event_len \c in: Size (in bytes) of \p p_event buffer. + * \c out: Length of decoded contents of \p p_event. + * + * @retval NRF_SUCCESS Decoding success. + * @retval NRF_ERROR_NULL Decoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t ble_l2cap_evt_rx_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len); + +/** @} */ +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_rpc_cmd_decoder.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,104 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_rpc_cmd_decoder Command Decoder + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief Decoder for serialized commands from Application Chip. + * + * @details This file contains declaration of common functions used for sending responses back to + * Application Chip after the command is processed, and function for processing commands + * received by the transport layer. + */ + +#ifndef BLE_RPC_CMD_DECODER_H__ +#define BLE_RPC_CMD_DECODER_H__ + +#include <stdint.h> + +#define RPC_DECODER_LENGTH_CHECK(LEN, INDEX, CMD) if ( INDEX > LEN) \ + return ble_rpc_cmd_resp_send(CMD, NRF_ERROR_INVALID_LENGTH); + +/**@brief Function for sending a Command Response packet to the Application Chip through the transport + * layer. + * + * @param[in] op_code The op code of the command for which the Command Response is sent. + * @param[in] status The status field to be encoded into the Command Response. + * + * @retval NRF_SUCCESS On successful write of Command Response, otherwise an error code. + * If the transport layer returns an error code while sending + * the Command Response, the same error code will be returned by this + * function (see @ref hci_transport_pkt_write for the list of + * error codes). + */ +uint32_t ble_rpc_cmd_resp_send(uint8_t op_code, uint32_t status); + +/**@brief Function for sending a command response with additional data to the Application Chip through + * the transport layer. + * + * @param[in] op_code The op code of the command for which the Command Response is sent. + * @param[in] status The status field to be encoded into the Command Response. + * @param[in] p_data The data to be sent along with the status. + * @param[in] data_len The length of the additional data. + * + * @retval NRF_SUCCESS On successful write of Command Response, otherwise an error code. + * If the transport layer returns an error code while sending + * the Command Response, the same error code will be returned by this + * function (see @ref hci_transport_pkt_write for the list of + * error codes). + */ +uint32_t ble_rpc_cmd_resp_data_send(uint8_t op_code, + uint8_t status, + const uint8_t * const p_data, + uint16_t data_len); + +/**@brief Function for scheduling an RPC command event to be processed in main-thread. + * + * @details The function will read the arrived packet from the transport layer + * which is passed for decoding by the rpc_cmd_decoder module. + * + * @param[in] p_event_data Event data. This will be NULL as rpc_evt_schedule + * does not set any data. + * @param[in] event_size Event data size. This will be 0 as rpc_evt_schedule + * does not set any data. + */ +void ble_rpc_cmd_handle(void * p_event_data, uint16_t event_size); + +#endif // BLE_RPC_CMD_DECODER_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_rpc_defines.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup rpc_cmd_defines Defines related to serialized BLE commands. + * @{ + * @ingroup ble_sdk_lib + * + * @brief Defines for serialized BLE commands. + * + */ + +#ifndef BLE_RPC_DEFINES_H__ +#define BLE_RPC_DEFINES_H__ + +#define RPC_PKT_TYPE_POS 0 /**< Position of Packet type in the serialized packet buffer.*/ +#define RPC_PKT_OP_CODE_POS 1 /**< Position of Op Code in the serialized packet buffer.*/ + +#define RPC_CMD_OP_CODE_POS 0 /**< Position of the Op Code in the command buffer.*/ +#define RPC_CMD_DATA_POS 1 /**< Position of the data in the command buffer.*/ + +#define RPC_CMD_RESP_PKT_TYPE_POS 0 /**< Position of Packet type in the command response buffer.*/ +#define RPC_CMD_RESP_OP_CODE_POS 1 /**< Position of the Op Code in the command response buffer.*/ +#define RPC_CMD_RESP_STATUS_POS 2 /**< Position of the status field in the command response buffer.*/ + +#define RPC_DTM_CMD_OP_CODE_POS 0 /**< Position of the Op Code in the DTM command buffer.*/ +#define RPC_DTM_DATA_POS 1 /**< Position of the data in the DTM command buffer.*/ + +#define RPC_DTM_RESP_OP_CODE_POS 1 /**< Position of the Op Code in the DTM command response buffer.*/ +#define RPC_DTM_RESP_STATUS_POS 2 /**< Position of the status field in the DTM command response buffer.*/ + +#define RPC_BLE_FIELD_LEN 1 /**< Optional field length size in bytes. */ +#define RPC_BLE_FIELD_PRESENT 0x01 /**< Value to indicate that an optional field is encoded in the serialized packet, e.g. white list. */ +#define RPC_BLE_FIELD_NOT_PRESENT 0x00 /**< Value to indicate that an optional field is not encoded in the serialized packet. */ + +#define RPC_ERR_CODE_SIZE 4 /**< BLE API err_code size in bytes. */ +#define BLE_PKT_TYPE_SIZE 1 /**< Packet type (@ref ble_rpc_pkt_type_t) field size in bytes. */ +#define BLE_OP_CODE_SIZE 1 /**< Operation code field size in bytes. */ + +#define RPC_BLE_CMD_RESP_PKT_MIN_SIZE 6 /**< Minimum length of a command response. */ +#define RPC_BLE_PKT_MAX_SIZE 596 /**< Maximum size for a BLE packet on the HCI Transport layer. This value is the hci_mem_pool buffer size minus the HCI Transport size. @note This value must be aligned with TX_BUF_SIZE in hci_mem_pool_internal.h. */ + +/**@brief The types of packets. */ +typedef enum +{ + BLE_RPC_PKT_CMD, /**< Command packet type. */ + BLE_RPC_PKT_RESP, /**< Command Response packet type. */ + BLE_RPC_PKT_EVT, /**< Event packet type. */ + BLE_RPC_PKT_DTM_CMD, /**< DTM Command packet type. */ + BLE_RPC_PKT_DTM_RESP, /**< DTM Response packet type. */ + BLE_RPC_PKT_TYPE_MAX /**< Upper bound. */ +} ble_rpc_pkt_type_t; + +#endif // BLE_RPC_DEFINES_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_rpc_event_encoder.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,61 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_rpc_event_encoder Events Encoder + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief Event encoder for S110 SoftDevice serialization. + * + * @details This module provides a function for serializing S110 SoftDevice events. + * + */ +#ifndef BLE_RPC_EVENT_ENCODER_H__ +#define BLE_RPC_EVENT_ENCODER_H__ + +#include "ble.h" + +/**@brief Function for encoding a @ref ble_evt_t. The function will pass the serialized byte stream to the + * transport layer after encoding. + * + * @param[in] p_ble_evt S110 SoftDevice event to serialize. + */ +void ble_rpc_event_handle(ble_evt_t * p_ble_evt); + +#endif // BLE_RPC_EVENT_ENCODER_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,513 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BLE_SERIALIZATION_H__ +#define BLE_SERIALIZATION_H__ + +#include "nordic_common.h" +#include "ble_rpc_defines.h" +#include "nrf_error.h" +#include <stdint.h> +#include <stddef.h> + +#define LOW16(a) ((uint16_t)((a & 0x0000FFFF) >> 0)) +#define HIGH16(a) ((uint16_t)((a & 0xFFFF0000) >> 16)) + +//lint -esym(516,__INTADDR__) Symbol '__INTADDR__()' has arg. type conflict +//lint -esym(628,__INTADDR__) no argument information provided for function '__INTADDR__()' + + +#define SER_CMD_REQ_HEADER_SIZE (BLE_OP_CODE_SIZE) /**< Size of command request header. */ +#define SER_CMD_RSP_HEADER_SIZE (BLE_OP_CODE_SIZE + RPC_ERR_CODE_SIZE) /**< Size of command response header. */ + +#define SER_EVT_ID_SIZE 2 /**< Size of event ID field. */ +#define SER_EVT_ID_POS 0 /**< Position of event ID field. */ +#define SER_EVT_HEADER_SIZE (SER_EVT_ID_SIZE) /**< Size of event header. */ +#define SER_EVT_CONN_HANDLE_SIZE 2 /**< Size of event connection handler. */ + +#define SER_POS_CMD_DATA (SER_CMD_REQ_HEADER_SIZE) /**< Position of command data. */ +#define SER_POS_RSP_STATUS_CODE (BLE_OP_CODE_SIZE) /**< Position of command response code. */ + +/**< Enable SER_ASSERT<*> assserts */ +#define SER_ASSERTS_ENABLED 1 + +/**< Returns with error code if expr is not true. It is used for checking error which should be + * checked even when SER_ASSERTS_ENABLED is not set. */ +#define SER_ERROR_CHECK(expr, error_code) do { if (!(expr)) return (error_code); } while (0) + +#ifdef SER_ASSERTS_ENABLED +/**< Returns with error code if expr is not true. */ +#define SER_ASSERT(expr, error_code) SER_ERROR_CHECK(expr, error_code) +/**< Returns with if expr is not true. */ +#define SER_ASSERT_VOID_RETURN(expr) do { if (!(expr)) return; } while (0) +/**< Returns with \ref NRF_ERROR_INVALID_LENGTH if len is not less or equal to maxlen. */ +#define SER_ASSERT_LENGTH_LEQ(len, maxlen) \ + SER_ASSERT((len) <= (maxlen), NRF_ERROR_INVALID_LENGTH) +/**< Returns with \ref NRF_ERROR_INVALID_LENGTH if actual_len is not equal to expected_len. */ +#define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) \ + SER_ASSERT((actual_len) == (expected_len), NRF_ERROR_INVALID_LENGTH) +/**< Returns with \ref NRF_ERROR_NULL if pointer is null. */ +#define SER_ASSERT_NOT_NULL(ptr) SER_ASSERT((ptr) != NULL, NRF_ERROR_NULL) +#else +#define SER_ASSERT(expr, error_code) +#define SER_ASSERT_VOID_RETURN(expr) +#define SER_ASSERT_LENGTH_LEQ(len, maxlen) UNUSED_VARIABLE(maxlen) +#define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) +#define SER_ASSERT_NOT_NULL(ptr) +#endif + +/**< Maximum length of p_value in \ref ble_gattc_write_params_t. See Bluetooth 4.0 spec: 3.4.5.1 and 3.4.5.3. */ +#define BLE_GATTC_WRITE_P_VALUE_LEN_MAX (GATT_MTU_SIZE_DEFAULT - 3) + +/* See Bluetooth 4.0 spec: 3.4.4.7. */ +#define BLE_GATTC_HANDLE_COUNT_LEN_MAX (GATT_MTU_SIZE_DEFAULT - 1) / 2 + +/**< Generic command response status code encoder. */ +uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code, + uint32_t command_status, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**< Generic command response resykt code decoder. */ +uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf, + uint32_t * const p_pos, + uint32_t packet_len, + uint8_t op_code, + uint32_t * const p_result_code); + +/**< Generic command response resykt code decoder. */ +uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t op_code, + uint32_t * const p_result_code); + +/**@brief Function for safe encoding an uint16 value. + * + * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_field Uint16 value to be encoded. + * @param[out] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint16 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t uint16_t_enc(const void * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding an uint16 value. + * + * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint16 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] p_field Pointer to the location where uint16 value will be decoded. + */ +uint32_t uint16_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field); + +/**@brief Function for safe decoding an uint16 value. + * + * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] index \c in: Index to start of uint16 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] value Decoded uint16 value. + */ +void uint16_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const index, + uint16_t * const value); + +/**@brief Function for safe encoding an uint18 value. + * + * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] p_field Pointer to uint8 value to be encoded. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t uint8_t_enc(const void * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding an uint8 value. + * + * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] p_field Pointer to the location for decoded uint8 value. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t uint8_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field); + +/**@brief Function for safe decoding an uint8 value. + * + * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] value Decoded uint8 value. + */ +void uint8_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const index, + uint8_t * const value); + +/**@brief Function for safe decoding an uint18 value. + * + * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] value Decoded uint8 value. + */ +void int8_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const index, + int8_t * const value); + +/**@brief Function for safe encoding variable length field encoded as length(8bit)+data. + * + * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[out] p_data Pointer to data to encode. + * @param[in] dlen Length of data to encode (0-255). + * @param[out] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t len8data_enc(uint8_t const * const p_data, + uint8_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding variable length field encoded as length(8bit)+data. + * + * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] pp_data Pointer to pointer to decoded data (p_data is set to NULL in + * case data is not present in the buffer). + * @param[out] p_len Decoded length (0-255). + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t len8data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint8_t * const p_len); + +/**@brief Function for safe encoding variable length field encoded as length(16bit)+data. + * + * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * It is possible that provided p_data is NULL in that case length is encoded and it's followed by + * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag preceeds data otherwise. + * + * @param[in] p_data Data to encode. + * @param[in] dlen Input data length (16bit). + * @param[in] p_buf Pointer to the beginning of the output buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the encoded data. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t len16data_enc(uint8_t const * const p_data, + uint16_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding variable length field encoded as length(16bit)+data. + * + * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * Encoded data consist of length field, presence flag and conditional data (present only is presence flag + * is set). p_data pointer is required to be not NULL only is presence flag is set. + * + * @param[in] p_buf Pointer to the beginning of the input buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded data. + * @param[in] pp_data Pointer to pointer to decoded data. + * @param[in] p_dlen data length (16bit). + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t len16data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t * const p_dlen); + + +/**@brief Function for safe encoding of uint16 table with a given element count. + * + * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * It is possible that provided p_data is NULL in that case length is encoded and it's followed by + * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag precedes data otherwise. + * + * @param[in] p_data Data table to encode. + * @param[in] count Table element count. + * @param[in] p_buf Pointer to the beginning of the output buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the encoded data. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ + +uint32_t count16_cond_data16_enc(uint16_t const * const p_data, + uint16_t const count, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding of uint16 table with a given element count. + * + * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * It is possible that provided p_data is NULL in that case length is encoded and it's followed by + * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag precedes data otherwise. + * + * @param[in] p_buf Pointer to the beginning of the output buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the encoded data. + * @param[in] pp_data Pointer to pointer to the table to encode. + * @param[in,out] p_count Pointer to table element count - initialised with max count + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Initial count is smaller than actual. + */ + +uint32_t count16_cond_data16_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint16_t * * const pp_data, + uint16_t * const p_count); + + +/**@brief Function for safe decoding of variable length field encoded as length(16bit)+data. + * + * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * Encoded data consist of presence flag, optional length field, second presence flag and optional data. + * + * + * @param[in] p_buf Pointer to the beginning of the input buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded data. + * @param[out] pp_data Pointer to decoded data. + * @param[out] pp_len data length (16bit). + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ + +uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t * * const pp_len); + + +/**@brief Command response encoder - replacement of - ser_ble_cmd_rsp_status_code_enc + * with layout aligned to the rest of encoder functions + * + * @param[in] op_code Operation code - see BLE_GAP_SVCS + * @param[in] return_code nRF Error Code. + * @param[in] p_buff pointer to the start of pointer to decoded data. + * @param[in,out] p_buff_len \c in: size of buffer + * \c out: used bytes in buffer + * @param[in,out] p_buff_len \c in: initial offset in buffer + * \c out: final offset in buffer + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_NULL Invalid pointer + */ +uint32_t op_status_enc(uint8_t op_code, + uint32_t return_code, + uint8_t * const p_buff, + uint32_t * const p_buff_len, + uint32_t * const p_index); + +/**@brief command response encoder with conditional 16bit field + * + * @param[in] op_code Operation code - see BLE_GAP_SVCS + * @param[in] return_code nRF Error Code. + * @param[in] value optional 16bit field encoded for return code == NRF_SUCCESS + * @param[in] p_buff pointer to the start of pointer to decoded data. + * @param[in,out] p_buff_len \c in: size of buffer + * \c out: used bytes in buffer + * @param[in,out] p_buff_len \c in: initial offset in buffer + * \c out: final offset in buffer + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_NULL Invalid pointer + */ + +uint32_t op_status_cond_uint16_enc(uint8_t op_code, + uint32_t return_code, + uint16_t value, + uint8_t * const p_buff, + uint32_t * const p_buff_len, + uint32_t * const p_index); + +/**@brief Function for safe encoding a buffer of known size. + * + * Safe encoding of a buffer. Encoder assumes that size is known to the decoder and it is not + * encoded here. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_data Data to encode. + * @param[in] dlen Input data length (16bit). + * @param[in] p_buf Pointer to the beginning of the output buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the encoded data. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t buf_enc(uint8_t const * const p_data, + uint16_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding a buffer of known size. + * + * Safe decoding of buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * Encoded data consist of presence flag and conditional data (present only is presence flag + * is set). p_data pointer is required to be not NULL only is presence flag is set. Length is provided + * as an input to the function. + * + * @param[in] p_buf Pointer to the beginning of the input buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded data. + * @param[in] pp_data Pointer to pointer to decoded data. + * @param[in] data_len Length of buffer for decoded data (16bit). + * @param[in] dlen Length of data to decode (16bit). + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t buf_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t data_len, + uint16_t dlen); + +/**@brief Function for safe encoding an uint32 value. + * + * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_field uint32 value to be encoded. + * @param[out] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint32 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t uint32_t_enc(void const * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +/**@brief Function for safe decoding an uint32 value. + * + * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set. + * + * @param[in] p_buf Buffer containing the value. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint32 value in buffer. + * \c out: Index in buffer to first byte after the decoded value. + * @param[out] value Decoded uint32 value. + */ +uint32_t uint32_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field); + +#endif + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/ble_struct_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,88 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_types.h" + + +uint32_t ble_uuid_t_enc(void const * const p_void_uuid, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_uuid_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_uuid); + +uint32_t ble_uuid128_t_enc(const void * const p_void_uuid, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_uuid128_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_uuid); + +uint32_t ble_l2cap_header_t_enc(void const * const p_void_header, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_l2cap_header_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_header); + +uint32_t ble_l2cap_evt_rx_t_enc(void const * const p_void_evt_rx, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_l2cap_evt_rx_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_event_len, + void * const p_void_evt_rx); + +uint32_t ble_enable_params_t_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +uint32_t ble_enable_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_data); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/cond_field_serialization.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,87 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "stdint.h" + +typedef uint32_t (*field_encoder_handler_t)(void const * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index); + +typedef uint32_t (*field_decoder_handler_t)(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field); + +/**@brief Function for safe encoding conditional field. + * + * Function sets 'presence flag' and checks if conditional field is provided and if it is not NULL + * it calls provided parser function which attempts to encode field content to the buffer stream. + * + * @param[in] p_field Pointer to input struct. + * @param[in] p_buf Pointer to the beginning of the output buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the encoded data. + * @param[in] fp_field_encoder Pointer to the function which implements fields encoding. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t cond_field_enc(void const * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + field_encoder_handler_t field_parser); + +/**@brief Function for safe decoding conditional field. + * + * Function checks if conditional field is present in the input buffer and if it is set it calls + * provided parser function which attempts to parse buffer content to the known field. + * + * @param[in] p_buf Pointer to the beginning of the input buffer. + * @param[in] buf_len Size of buffer. + * @param[in,out] p_index \c in: Index to start of uint8 value in buffer. + * \c out: Index in buffer to first byte after the decoded data. + * @param[in] pp_field Pointer to pointer to output location. + * @param[in] fp_field_decoder Pointer to the function which implements field decoding. + * + * @return NRF_SUCCESS Fields decoded successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + */ +uint32_t cond_field_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * * const pp_field, + field_decoder_handler_t field_parser); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/inc/nrf_soc_app.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,103 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/**@file + * + * @defgroup soc_app SOC Application command request encoders and command response decoders + * @{ + * @ingroup ble_sdk_lib_serialization + * + * @brief SOC Application command request encoders and command response decoders. + */ + +#ifndef NRF_SOC_APP_H__ +#define NRF_SOC_APP_H__ + +#include <stdint.h> + +/**@brief Encodes @ref sd_power_system_off command request. + * + * @sa @ref nrf51_sd_power_off for packet format. + * + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: size of p_buf buffer. \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t power_system_off_req_enc(uint8_t * const p_buf, uint32_t * const p_buf_len); + + +/**@brief Encodes @ref sd_temp_get command request. + * + * @sa @ref nrf51_sd_temp_get for packet format. + @ref temp_get_rsp_dec for command response decoder. + * + * @param[in] p_temp Pointer to result of temperature measurement. + * @param[in] p_buf Pointer to buffer where encoded data command will be returned. + * @param[in,out] p_buf_len \c in: size of p_buf buffer. \c out: Length of encoded command packet. + * + * @retval NRF_SUCCESS Encoding success. + * @retval NRF_ERROR_NULL Encoding failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_LENGTH Encoding failure. Incorrect buffer length. + */ +uint32_t temp_get_req_enc(int32_t const * const p_temp, + uint8_t * const p_buf, + uint32_t * const p_buf_len); + +/**@brief Decodes response to @ref sd_temp_get command. + * + * @sa @ref nrf51_temp_get_encoding for packet format, + * @ref temp_get_req_enc for command request encoder. + * + * @param[in] p_buf Pointer to beginning of command response packet. + * @param[in] packet_len Length (in bytes) of response packet. + * @param[out] p_result_code Command result code. + * @param[out] p_temp Pointer to result of temperature measurement. + * + * @return NRF_SUCCESS Version information stored successfully. + * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length. + * @retval NRF_ERROR_DATA_SIZE Decoding failure. Length of \p p_event is too small to + * hold decoded event. + */ +uint32_t temp_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code, + int32_t * const p_temp); + +/** @} */ + +#endif // NRF_SOC_APP_H__ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_enable.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,73 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <string.h> +#include "ble_app.h" +#include "ble_serialization.h" +#include "ble_struct_serialization.h" +#include "cond_field_serialization.h" +#include "app_util.h" + + +uint32_t ble_enable_req_enc(ble_enable_params_t * p_ble_enable_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + + p_buf[index++] = SD_BLE_ENABLE; + + err_code = cond_field_enc(p_ble_enable_params, p_buf, *p_buf_len, &index, ble_enable_params_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_enable_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_ENABLE, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_event.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,236 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include "ble_evt_app.h" +#include "ble_gap_evt_app.h" +#include "ble_gattc_evt_app.h" +#include "ble_gatts_evt_app.h" +#include "ble_l2cap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_event_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t err_code; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + SER_ASSERT_LENGTH_LEQ(SER_EVT_HEADER_SIZE, packet_len); + + + const uint16_t event_id = uint16_decode(&p_buf[SER_EVT_ID_POS]); + const uint8_t * p_sub_buffer = &p_buf[SER_EVT_HEADER_SIZE]; + const uint32_t sub_packet_len = packet_len - SER_EVT_HEADER_SIZE; + + if (p_event) + { + SER_ASSERT_LENGTH_LEQ(sizeof (ble_evt_hdr_t), *p_event_len); + *p_event_len -= sizeof (ble_evt_hdr_t); + } + + switch (event_id) + { + case BLE_EVT_TX_COMPLETE: + err_code = ble_evt_tx_complete_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GAP_EVT_PASSKEY_DISPLAY: + err_code = ble_gap_evt_passkey_display_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_AUTH_KEY_REQUEST: + err_code = ble_gap_evt_auth_key_request_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_CONN_PARAM_UPDATE: + err_code = ble_gap_evt_conn_param_update_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_CONN_SEC_UPDATE: + err_code = ble_gap_evt_conn_sec_update_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_CONNECTED: + err_code = ble_gap_evt_connected_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GAP_EVT_DISCONNECTED: + err_code = ble_gap_evt_disconnected_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GAP_EVT_TIMEOUT: + err_code = ble_gap_evt_timeout_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GAP_EVT_RSSI_CHANGED: + err_code = ble_gap_evt_rssi_changed_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GAP_EVT_SEC_INFO_REQUEST: + err_code = ble_gap_evt_sec_info_request_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + err_code = ble_gap_evt_sec_params_request_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GAP_EVT_AUTH_STATUS: + err_code = ble_gap_evt_auth_status_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_CHAR_DISC_RSP: + err_code = ble_gattc_evt_char_disc_rsp_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: + err_code = ble_gattc_evt_char_val_by_uuid_read_rsp_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_DESC_DISC_RSP: + err_code = ble_gattc_evt_desc_disc_rsp_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: + err_code = ble_gattc_evt_prim_srvc_disc_rsp_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_READ_RSP: + err_code = ble_gattc_evt_read_rsp_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_HVX: + err_code = ble_gattc_evt_hvx_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GATTC_EVT_TIMEOUT: + err_code = ble_gattc_evt_timeout_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GATTC_EVT_WRITE_RSP: + err_code = ble_gattc_evt_write_rsp_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_CHAR_VALS_READ_RSP: + err_code = ble_gattc_evt_char_vals_read_rsp_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTC_EVT_REL_DISC_RSP: + err_code = ble_gattc_evt_rel_disc_rsp_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTS_EVT_WRITE: + err_code = ble_gatts_evt_write_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GATTS_EVT_TIMEOUT: + err_code = ble_gatts_evt_timeout_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GATTS_EVT_SC_CONFIRM: + err_code = ble_gatts_evt_sc_confirm_dec(p_sub_buffer, + sub_packet_len, + p_event, + p_event_len); + break; + + case BLE_GATTS_EVT_HVC: + err_code = ble_gatts_evt_hvc_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + case BLE_GATTS_EVT_SYS_ATTR_MISSING: + err_code = ble_gatts_evt_sys_attr_missing_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + err_code = ble_gatts_evt_rw_authorize_request_dec(p_sub_buffer, sub_packet_len, p_event, + p_event_len); + break; + + case BLE_L2CAP_EVT_RX: + err_code = ble_l2cap_evt_rx_dec(p_sub_buffer, sub_packet_len, p_event, p_event_len); + break; + + default: + err_code = NRF_ERROR_NOT_FOUND; + break; + } + + if (p_event != NULL) + { + p_event->header.evt_id = (err_code == NRF_SUCCESS) ? event_id : 0; + p_event->header.evt_len = (err_code == NRF_SUCCESS) ? + (uint16_t)*p_event_len : 0; + } + *p_event_len += sizeof(ble_evt_hdr_t); + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_evt_tx_complete.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,72 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_evt_app.h" +#include "ble_serialization.h" + + +uint32_t ble_evt_tx_complete_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + uint32_t event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_evt_tx_complete_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_EVT_TX_COMPLETE; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.common_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.common_evt.params.tx_complete.count); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_address_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,99 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <stdlib.h> +#include <string.h> +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_address_get_req_enc(ble_gap_addr_t const * const p_address, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_ADDRESS_GET; + p_buf[index++] = (p_address == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_address_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_addr_t * const p_address, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_BLE_GAP_ADDRESS_GET, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + 1 + BLE_GAP_ADDR_LEN, packet_len); + SER_ASSERT_NOT_NULL(p_address); + + p_address->addr_type = p_buf[index++]; + memcpy(p_address->addr, &p_buf[index], BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_address_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <stdlib.h> +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_address_set_req_enc(uint8_t addr_cycle_mode, + ble_gap_addr_t const * const p_addr, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_ADDRESS_SET; + p_buf[index++] = addr_cycle_mode; + p_buf[index++] = (p_addr == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + if (p_addr != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + BLE_GAP_ADDR_LEN, *p_buf_len); + p_buf[index++] = p_addr->addr_type; + memcpy(&p_buf[index], p_addr->addr, BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_address_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_ADDRESS_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_adv_data_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,75 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "nrf_error.h" + + +uint32_t ble_gap_adv_data_set_req_enc(uint8_t const * const p_data, + uint8_t dlen, + uint8_t const * const p_sr_data, + uint8_t srdlen, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint8_t opcode = SD_BLE_GAP_ADV_DATA_SET; + uint32_t err_code; + err_code = uint8_t_enc(&opcode, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = len8data_enc(p_data, dlen, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = len8data_enc(p_sr_data, srdlen, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gap_adv_data_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_ADV_DATA_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_adv_start.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,135 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +#define WHITELIST_ENCODE_LEN(p_whitelist) (1 + ((p_whitelist)->addr_count * (1 + BLE_GAP_ADDR_LEN)) \ + + 1 + ((p_whitelist)->irk_count * BLE_GAP_SEC_KEY_LEN)) + + +static uint32_t whitelist_encode(uint8_t * p_packet, + ble_gap_whitelist_t const * const p_whitelist) +{ + uint32_t index = 0, i = 0; + + p_packet[index++] = p_whitelist->addr_count; + + for (i = 0; i < p_whitelist->addr_count; i++) + { + p_packet[index++] = p_whitelist->pp_addrs[i]->addr_type; + memcpy(&p_packet[index], &p_whitelist->pp_addrs[i]->addr[0], BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + } + + p_packet[index++] = p_whitelist->irk_count; + + for (i = 0; i < p_whitelist->irk_count; i++) + { + memcpy(&p_packet[index], &p_whitelist->pp_irks[i]->irk[0], BLE_GAP_SEC_KEY_LEN); + index += BLE_GAP_SEC_KEY_LEN; + } + + return index; +} + + +uint32_t ble_gap_adv_start_req_enc(ble_gap_adv_params_t const * const p_adv_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + p_buf[index++] = SD_BLE_GAP_ADV_START; + p_buf[index++] = (p_adv_params == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + if (p_adv_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + p_buf[index++] = p_adv_params->type; + p_buf[index++] = (p_adv_params->p_peer_addr != NULL) ? + RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_adv_params->p_peer_addr != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + BLE_GAP_ADDR_LEN, *p_buf_len); + p_buf[index++] = p_adv_params->p_peer_addr->addr_type; + memcpy(&p_buf[index], &p_adv_params->p_peer_addr->addr[0], BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + } + + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + p_buf[index++] = p_adv_params->fp; + p_buf[index++] = (p_adv_params->p_whitelist != NULL) ? + RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_adv_params->p_whitelist != NULL) + { + ble_gap_whitelist_t * p_whitelist = p_adv_params->p_whitelist; + + SER_ERROR_CHECK(p_whitelist->addr_count <= BLE_GAP_WHITELIST_ADDR_MAX_COUNT, + NRF_ERROR_INVALID_PARAM); + SER_ERROR_CHECK(p_whitelist->irk_count <= BLE_GAP_WHITELIST_IRK_MAX_COUNT, + NRF_ERROR_INVALID_PARAM); + SER_ASSERT_LENGTH_LEQ(index + WHITELIST_ENCODE_LEN(p_whitelist), *p_buf_len); + + index += whitelist_encode(&p_buf[index], p_whitelist); + } + + SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len); + index += uint16_encode(p_adv_params->interval, &p_buf[index]); + index += uint16_encode(p_adv_params->timeout, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_adv_start_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_ADV_START, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_adv_stop.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,63 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" + +uint32_t ble_gap_adv_stop_req_enc(uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint8_t op_code = SD_BLE_GAP_ADV_STOP; + uint32_t err_code = NRF_SUCCESS; + uint32_t buf_len = *p_buf_len; + uint32_t index = 0; + + err_code = uint8_t_enc(&op_code, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gap_adv_stop_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_ADV_STOP, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_appearance_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,94 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_appearance_get_req_enc(uint16_t const * const p_appearance, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_APPEARANCE_GET; + p_buf[index++] = (p_appearance == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_appearance_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_appearance, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GAP_APPEARANCE_GET, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + sizeof (uint16_t), packet_len); + + uint16_dec(p_buf, packet_len, &index, p_appearance); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_appearance_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_appearance_set_req_enc(uint16_t appearance, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_APPEARANCE_SET; + index += uint16_encode(appearance, &p_buf[index]); + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_appearance_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_APPEARANCE_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_auth_key_reply.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,100 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_auth_key_reply_req_enc(uint16_t conn_handle, + uint8_t key_type, + uint8_t const * const p_key, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint8_t opcode = SD_BLE_GAP_AUTH_KEY_REPLY; + uint32_t err_code = NRF_SUCCESS; + uint8_t key_len; + + err_code = uint8_t_enc(&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&key_type, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + switch (key_type) + { + case BLE_GAP_AUTH_KEY_TYPE_NONE: + key_len = 0; + break; + + case BLE_GAP_AUTH_KEY_TYPE_PASSKEY: + key_len = 6; + break; + + case BLE_GAP_AUTH_KEY_TYPE_OOB: + key_len = 16; + break; + + default: + return NRF_ERROR_INVALID_PARAM; + } + + err_code = buf_enc(p_key, key_len, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gap_auth_key_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_AUTH_KEY_REPLY, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_authenticate.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,83 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_authenticate_req_enc(uint16_t conn_handle, + ble_gap_sec_params_t const * const p_sec_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1 + 2 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_AUTHENTICATE; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_sec_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_sec_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + 1 + 1 + 1, *p_buf_len); + + index += uint16_encode(p_sec_params->timeout, &p_buf[index]); + p_buf[index++] = ((p_sec_params->oob << 5) | + (p_sec_params->io_caps << 2) | + (p_sec_params->mitm << 1) | + (p_sec_params->bond << 0)); + p_buf[index++] = p_sec_params->min_key_size; + p_buf[index++] = p_sec_params->max_key_size; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_authenticate_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_AUTHENTICATE, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_conn_param_update.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,79 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_conn_param_update_req_enc(uint16_t conn_handle, + ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_CONN_PARAM_UPDATE; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_conn_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_conn_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 * 4, *p_buf_len); + index += uint16_encode(p_conn_params->min_conn_interval, &p_buf[index]); + index += uint16_encode(p_conn_params->max_conn_interval, &p_buf[index]); + index += uint16_encode(p_conn_params->slave_latency, &p_buf[index]); + index += uint16_encode(p_conn_params->conn_sup_timeout, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_conn_param_update_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_CONN_PARAM_UPDATE, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_conn_sec_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,98 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" +#include "ble_gap_struct_serialization.h" + +uint32_t ble_gap_conn_sec_get_req_enc(uint16_t conn_handle, + ble_gap_conn_sec_t const * const p_conn_sec, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t total_len = *p_buf_len; + uint32_t err_code = NRF_SUCCESS; + uint8_t opcode = SD_BLE_GAP_CONN_SEC_GET; + + + err_code = uint8_t_enc(&opcode, p_buf, total_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, total_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_conn_sec, p_buf, total_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gap_conn_sec_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_conn_sec_t * * const pp_conn_sec, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + uint32_t index = 0; + uint32_t err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GAP_CONN_SEC_GET, + p_result_code); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, packet_len, &index, (void * *)pp_conn_sec, + ble_gap_conn_sec_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_device_name_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,107 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_gap_device_name_get_req_enc(uint8_t const * const p_dev_name, + uint16_t const * const p_len, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_DEVICE_NAME_GET; + p_buf[index++] = (p_len != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_len != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + index += uint16_encode(*p_len, &p_buf[index]); + } + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_dev_name != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_device_name_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_dev_name, + uint16_t * const p_dev_name_len, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + uint32_t index = 0; + uint32_t status_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GAP_DEVICE_NAME_GET, + p_result_code); + + if (status_code != NRF_SUCCESS) + { + return status_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_NOT_NULL(p_dev_name_len); + status_code = len16data_dec(p_buf, packet_len, &index, (uint8_t * *)&p_dev_name, p_dev_name_len); + + if (status_code != NRF_SUCCESS) + { + return status_code; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return status_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_device_name_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_device_name_set_req_enc(ble_gap_conn_sec_mode_t const * const p_write_perm, + uint8_t const * const p_dev_name, + uint16_t len, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(2, *p_buf_len); + p_buf[index++] = SD_BLE_GAP_DEVICE_NAME_SET; + + p_buf[index++] = (p_write_perm != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_write_perm != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (uint8_t) ((p_write_perm->sm) | (p_write_perm->lv << 4)); + } + + SER_ERROR_CHECK(len <= BLE_GAP_DEVNAME_MAX_LEN, NRF_ERROR_INVALID_PARAM); + + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + index += uint16_encode(len, &p_buf[index]); + + p_buf[index++] = (p_dev_name != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_dev_name != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + len, *p_buf_len); + memcpy(&p_buf[index], p_dev_name, len); + index += len; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_device_name_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_DEVICE_NAME_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_disconnect.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_disconnect_req_enc(uint16_t conn_handle, + uint8_t hci_status_code, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_DISCONNECT; + index += uint16_encode(conn_handle, &p_buf[index]); + p_buf[index++] = hci_status_code; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_disconnect_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_DISCONNECT, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_auth_key_request.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,76 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_auth_key_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(3, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + 1; + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_AUTH_KEY_REQUEST; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.params.auth_key_request.key_type); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_auth_status.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,119 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_auth_status_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + uint32_t event_len = sizeof (ble_gap_evt_auth_status_t) + + sizeof (p_event->evt.gap_evt.conn_handle); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + SER_ASSERT_LENGTH_LEQ(2 + 1 + 1 + 1 + 1 + 1 + 2, packet_len); + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + ble_gap_evt_auth_status_t * p_decoded_evt = &(p_event->evt.gap_evt.params.auth_status); + + p_decoded_evt->auth_status = p_buf[index++]; + p_decoded_evt->error_src = p_buf[index++]; + + p_decoded_evt->sm1_levels.lv3 = (p_buf[index] >> 5) & 0x01; + p_decoded_evt->sm1_levels.lv2 = (p_buf[index] >> 4) & 0x01; + p_decoded_evt->sm1_levels.lv1 = (p_buf[index] >> 3) & 0x01; + p_decoded_evt->sm2_levels.lv3 = (p_buf[index] >> 2) & 0x01; + p_decoded_evt->sm2_levels.lv2 = (p_buf[index] >> 1) & 0x01; + p_decoded_evt->sm2_levels.lv1 = (p_buf[index] >> 0) & 0x01; + index++; + + p_decoded_evt->periph_kex.csrk = (p_buf[index] >> 4) & 0x01; + p_decoded_evt->periph_kex.address = (p_buf[index] >> 3) & 0x01; + p_decoded_evt->periph_kex.irk = (p_buf[index] >> 2) & 0x01; + p_decoded_evt->periph_kex.ediv_rand = (p_buf[index] >> 1) & 0x01; + p_decoded_evt->periph_kex.ltk = (p_buf[index] >> 0) & 0x01; + index++; + + p_decoded_evt->central_kex.ltk = (p_buf[index] >> 4) & 0x01; + p_decoded_evt->central_kex.ediv_rand = (p_buf[index] >> 3) & 0x01; + p_decoded_evt->central_kex.irk = (p_buf[index] >> 2) & 0x01; + p_decoded_evt->central_kex.address = (p_buf[index] >> 1) & 0x01; + p_decoded_evt->central_kex.csrk = (p_buf[index] >> 0) & 0x01; + index++; + + uint16_dec(p_buf, packet_len, &index, &p_decoded_evt->periph_keys.enc_info.div); + + SER_ASSERT_LENGTH_LEQ(index + BLE_GAP_SEC_KEY_LEN + 1 + + BLE_GAP_SEC_KEY_LEN + 1 + BLE_GAP_ADDR_LEN, + packet_len); + memcpy(&p_decoded_evt->periph_keys.enc_info.ltk[0], &p_buf[index], BLE_GAP_SEC_KEY_LEN); + index += BLE_GAP_SEC_KEY_LEN; + + p_decoded_evt->periph_keys.enc_info.ltk_len = (p_buf[index] >> 1); + p_decoded_evt->periph_keys.enc_info.auth = (p_buf[index] >> 0) & 0x01; + index++; + + memcpy(&p_decoded_evt->central_keys.irk.irk[0], &p_buf[index], BLE_GAP_SEC_KEY_LEN); + index += BLE_GAP_SEC_KEY_LEN; + + p_decoded_evt->central_keys.id_info.addr_type = p_buf[index++]; + + memcpy(&p_decoded_evt->central_keys.id_info.addr[0], &p_buf[index], BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_conn_param_update.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,82 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_conn_param_update_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 2, packet_len); + + uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gap_evt.params.conn_param_update)) + + sizeof (ble_gap_evt_conn_param_update_t) - + sizeof (ble_evt_hdr_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_CONN_PARAM_UPDATE; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gap_evt.params.conn_param_update.conn_params.min_conn_interval); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gap_evt.params.conn_param_update.conn_params.max_conn_interval); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gap_evt.params.conn_param_update.conn_params.slave_latency); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gap_evt.params.conn_param_update.conn_params.conn_sup_timeout); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_conn_sec_update.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_conn_sec_update_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 2, packet_len); + + uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gap_evt.params.conn_sec_update)) + + sizeof (ble_gap_evt_conn_sec_update_t) - + sizeof (ble_evt_hdr_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_CONN_SEC_UPDATE; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + p_event->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.sm = p_buf[index] & 0x0F; + p_event->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv = (p_buf[index] >> 4) & 0x0F; + index++; + + uint8_dec(p_buf, packet_len, + &index, &p_event->evt.gap_evt.params.conn_sec_update.conn_sec.encr_key_size); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_connected.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,87 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_connected_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(18, packet_len); + + uint32_t event_len = sizeof (ble_gap_evt_connected_t) + + sizeof (p_event->evt.gap_evt.conn_handle); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + ble_gap_evt_connected_t * p_decoded_evt = &(p_event->evt.gap_evt.params.connected); + + p_decoded_evt->peer_addr.addr_type = p_buf[index++]; + memcpy(p_decoded_evt->peer_addr.addr, &p_buf[index], BLE_GAP_ADDR_LEN); + index += BLE_GAP_ADDR_LEN; + + p_decoded_evt->irk_match = (p_buf[index] & 0x01); + p_decoded_evt->irk_match_idx = (p_buf[index] & 0xFE) >> 1; + index++; + + uint16_dec(p_buf, packet_len, &index, &p_decoded_evt->conn_params.min_conn_interval); + uint16_dec(p_buf, packet_len, &index, &p_decoded_evt->conn_params.max_conn_interval); + uint16_dec(p_buf, packet_len, &index, &p_decoded_evt->conn_params.slave_latency); + uint16_dec(p_buf, packet_len, &index, &p_decoded_evt->conn_params.conn_sup_timeout); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_disconnected.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,71 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_disconnected_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(3, packet_len); + + uint32_t event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_disconnected_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_DISCONNECTED; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.params.disconnected.reason); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_passkey_display.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + +#define PASSKEY_LEN sizeof (p_event->evt.gap_evt.params.passkey_display.passkey) + + +uint32_t ble_gap_evt_passkey_display_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + PASSKEY_LEN, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_passkey_display_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_PASSKEY_DISPLAY; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + memcpy(p_event->evt.gap_evt.params.passkey_display.passkey, &p_buf[index], PASSKEY_LEN); + index += PASSKEY_LEN; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_rssi_changed.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,76 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_rssi_changed_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_rssi_changed_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_RSSI_CHANGED; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + int8_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.params.rssi_changed.rssi); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_sec_info_request.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,91 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_sec_info_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(2 + 1 + 6 + 2 + 1, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_sec_info_request_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_SEC_INFO_REQUEST; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + ble_gap_evt_sec_info_request_t * p_sec_info_request = + &(p_event->evt.gap_evt.params.sec_info_request); + + p_sec_info_request->peer_addr.addr_type = p_buf[index++]; + p_sec_info_request->peer_addr.addr[0] = p_buf[index++]; + p_sec_info_request->peer_addr.addr[1] = p_buf[index++]; + p_sec_info_request->peer_addr.addr[2] = p_buf[index++]; + p_sec_info_request->peer_addr.addr[3] = p_buf[index++]; + p_sec_info_request->peer_addr.addr[4] = p_buf[index++]; + p_sec_info_request->peer_addr.addr[5] = p_buf[index++]; + + uint16_dec(p_buf, packet_len, &index, &p_sec_info_request->div); + + p_sec_info_request->enc_info = (p_buf[index] >> 0) & 0x1; + p_sec_info_request->id_info = (p_buf[index] >> 1) & 0x1; + p_sec_info_request->sign_info = (p_buf[index] >> 2) & 0x1; + index++; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_sec_params_request.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,88 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_sec_params_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(7, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_sec_params_request_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_SEC_PARAMS_REQUEST; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + + ble_gap_evt_sec_params_request_t * p_sec_params_request = + &(p_event->evt.gap_evt.params.sec_params_request); + + uint16_dec(p_buf, packet_len, &index, &p_sec_params_request->peer_params.timeout); + + p_sec_params_request->peer_params.bond = (p_buf[index] >> 0) & 0x1; + p_sec_params_request->peer_params.mitm = (p_buf[index] >> 1) & 0x1; + p_sec_params_request->peer_params.io_caps = (p_buf[index] >> 2) & 0x7; + p_sec_params_request->peer_params.oob = (p_buf[index] >> 5) & 0x1; + index++; + + p_sec_params_request->peer_params.min_key_size = p_buf[index++]; + p_sec_params_request->peer_params.max_key_size = p_buf[index++]; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_evt_timeout.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,75 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_EQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_timeout_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GAP_EVT_TIMEOUT; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.params.timeout.src); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_ppcp_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,92 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" + + +uint32_t ble_gap_ppcp_get_req_enc(ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_PPCP_GET; + p_buf[index++] = (p_conn_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_ppcp_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_gap_conn_params_t * const p_conn_params, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GAP_PPCP_GET, p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + return NRF_SUCCESS; + } + + SER_ASSERT_NOT_NULL(p_conn_params); + + SER_ASSERT_LENGTH_LEQ(index + 8, packet_len); + uint16_dec(p_buf, packet_len, &index, &p_conn_params->min_conn_interval); + uint16_dec(p_buf, packet_len, &index, &p_conn_params->max_conn_interval); + uint16_dec(p_buf, packet_len, &index, &p_conn_params->slave_latency); + uint16_dec(p_buf, packet_len, &index, &p_conn_params->conn_sup_timeout); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return decode_result; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_ppcp_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <stddef.h> +#include "nrf_error.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gap_ppcp_set_req_enc(ble_gap_conn_params_t const * const p_conn_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_PPCP_SET; + p_buf[index++] = (p_conn_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_conn_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 8, *p_buf_len); + index += uint16_encode(p_conn_params->min_conn_interval, &p_buf[index]); + index += uint16_encode(p_conn_params->max_conn_interval, &p_buf[index]); + index += uint16_encode(p_conn_params->slave_latency, &p_buf[index]); + index += uint16_encode(p_conn_params->conn_sup_timeout, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_ppcp_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_PPCP_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_rssi_start.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_gap_rssi_start_req_enc(uint16_t conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint8_t op_code = SD_BLE_GAP_RSSI_START; + uint32_t err_code = NRF_SUCCESS; + uint32_t buf_len = *p_buf_len; + uint32_t index = 0; + + err_code = uint8_t_enc(&op_code, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gap_rssi_start_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_RSSI_START, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_rssi_stop.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_gap_rssi_stop_req_enc(uint16_t conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t err_code = NRF_SUCCESS; + uint32_t buf_len = *p_buf_len; + uint32_t index = 0; + + uint8_t op_code = SD_BLE_GAP_RSSI_STOP; + err_code = uint8_t_enc(&op_code, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gap_rssi_stop_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_RSSI_STOP, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_sec_info_reply.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,91 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_sec_info_reply_req_enc(uint16_t conn_handle, + ble_gap_enc_info_t const * const p_enc_info, + ble_gap_sign_info_t const * const p_sign_info, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_SEC_INFO_REPLY; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_enc_info != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_enc_info != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + BLE_GAP_SEC_KEY_LEN + 1, *p_buf_len); + index += uint16_encode(p_enc_info->div, &p_buf[index]); + memcpy(&p_buf[index], p_enc_info->ltk, BLE_GAP_SEC_KEY_LEN); + index += BLE_GAP_SEC_KEY_LEN; + p_buf[index++] = (p_enc_info->auth | (p_enc_info->ltk_len << 1)); + } + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_sign_info != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_sign_info != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + BLE_GAP_SEC_KEY_LEN, *p_buf_len); + memcpy(&p_buf[index], p_sign_info->csrk, BLE_GAP_SEC_KEY_LEN); + index += BLE_GAP_SEC_KEY_LEN; + } + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_sec_info_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_SEC_INFO_REPLY, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_sec_params_reply.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,85 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gap_sec_params_reply_req_enc(uint16_t conn_handle, + uint8_t sec_status, + ble_gap_sec_params_t const * const p_sec_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1 + 2 + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GAP_SEC_PARAMS_REPLY; + index += uint16_encode(conn_handle, &p_buf[index]); + p_buf[index++] = sec_status; + + p_buf[index++] = (p_sec_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_sec_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + 1 + 1 + 1, *p_buf_len); + + index += uint16_encode(p_sec_params->timeout, &p_buf[index]); + p_buf[index++] = ((p_sec_params->oob << 5) | + (p_sec_params->io_caps << 2) | + (p_sec_params->mitm << 1) | + (p_sec_params->bond << 0)); + p_buf[index++] = p_sec_params->min_key_size; + p_buf[index++] = p_sec_params->max_key_size; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gap_sec_params_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_SEC_PARAMS_REPLY, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_struct_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,668 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_struct_serialization.h" +#include "ble_serialization.h" +#include "cond_field_serialization.h" +#include "app_util.h" +#include "string.h" + +uint32_t ble_gap_irk_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_irk_t * p_gap_irk = (ble_gap_irk_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(BLE_GAP_SEC_KEY_LEN, buf_len - *p_index); + + memcpy(&p_buf[*p_index], p_gap_irk->irk, BLE_GAP_SEC_KEY_LEN); + + *p_index += BLE_GAP_SEC_KEY_LEN; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_irk_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_data) +{ + ble_gap_irk_t * p_gap_irk = (ble_gap_irk_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(BLE_GAP_SEC_KEY_LEN, buf_len - *p_index); + + memcpy(p_gap_irk->irk, &p_buf[*p_index], BLE_GAP_SEC_KEY_LEN); + + *p_index += BLE_GAP_SEC_KEY_LEN; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_addr_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_addr_t * p_addr = (ble_gap_addr_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(1 + BLE_GAP_ADDR_LEN, buf_len - *p_index); + + p_buf[*p_index] = p_addr->addr_type; + (*p_index)++; + memcpy(&p_buf[*p_index], p_addr->addr, BLE_GAP_ADDR_LEN); + *p_index += BLE_GAP_ADDR_LEN; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_addr_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_addr) +{ + ble_gap_addr_t * p_address = (ble_gap_addr_t *) p_addr; + + SER_ASSERT_LENGTH_LEQ(sizeof (ble_gap_addr_t), buf_len - *p_index); + memcpy(p_address, &p_buf[*p_index], sizeof (ble_gap_addr_t)); + *p_index += sizeof (ble_gap_addr_t); + + return NRF_SUCCESS; +} + +uint32_t ble_gap_sec_levels_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_sec_levels_t * p_sec_levels = (ble_gap_sec_levels_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + + p_buf[*p_index] = (p_sec_levels->lv1 << 0) | (p_sec_levels->lv2 << 1) | (p_sec_levels->lv3 << 2); + (*p_index)++; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_sec_keys_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_sec_keys_t * p_sec_keys = (ble_gap_sec_keys_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + + p_buf[*p_index] = (p_sec_keys->ltk << 0) | (p_sec_keys->ediv_rand << 1) | (p_sec_keys->irk << 2) + | (p_sec_keys->address << 3) | (p_sec_keys->csrk << 4); + (*p_index)++; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_enc_info_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code; + ble_gap_enc_info_t * p_enc_info = (ble_gap_enc_info_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(2 + BLE_GAP_SEC_KEY_LEN + 1, buf_len - *p_index); + + err_code = uint16_t_enc(&p_enc_info->div, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + memcpy(&p_buf[*p_index], p_enc_info->ltk, BLE_GAP_SEC_KEY_LEN); + *p_index += BLE_GAP_SEC_KEY_LEN; + + p_buf[*p_index] = p_enc_info->auth & 0x01; + p_buf[*p_index] |= (p_enc_info->ltk_len & 0x7F) << 1; + (*p_index)++; + + return err_code; +} + +uint32_t ble_gap_enc_info_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_enc_info) +{ + ble_gap_enc_info_t * p_enc_info2 = (ble_gap_enc_info_t *)p_enc_info; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_enc_info2->div); + + SER_ASSERT_LENGTH_LEQ(BLE_GAP_SEC_KEY_LEN, buf_len - *p_index); + memcpy(p_enc_info2->ltk, &p_buf[*p_index], BLE_GAP_SEC_KEY_LEN); + *p_index += BLE_GAP_SEC_KEY_LEN; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + p_enc_info2->auth = p_buf[*p_index] & 0x01; + p_enc_info2->ltk_len = (p_buf[*p_index] >> 1) & 0x7F; + *p_index += 1; + + return NRF_SUCCESS; +} + +uint32_t ble_gap_sign_info_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_sign_info) +{ + SER_ASSERT_LENGTH_LEQ(sizeof (ble_gap_sign_info_t), buf_len - *p_index); + memcpy(p_sign_info, &p_buf[*p_index], sizeof (ble_gap_sign_info_t)); + *p_index += sizeof (ble_gap_sign_info_t); + + return NRF_SUCCESS; +} + +uint32_t ble_gap_evt_auth_status_t_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + uint32_t tmp; + + ble_gap_evt_auth_status_t * auth_status = (ble_gap_evt_auth_status_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + + err_code = uint8_t_enc(&auth_status->auth_status, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&auth_status->error_src, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + tmp = *p_index; + err_code = ble_gap_sec_levels_enc(&auth_status->sm1_levels, p_buf, buf_len, &tmp); + + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + tmp = p_buf[*p_index]; //read encoded sm1_levels fields + err_code = ble_gap_sec_levels_enc(&auth_status->sm2_levels, + p_buf, + buf_len, + p_index); + p_buf[(*p_index) - 1] |= tmp << 3; //add previously encoded sm1_levels fields + + err_code = ble_gap_sec_keys_enc(&auth_status->periph_kex, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_sec_keys_enc(&auth_status->central_kex, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_enc_info_enc(&auth_status->periph_keys.enc_info, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_irk_enc(&auth_status->central_keys.irk, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_addr_enc(&auth_status->central_keys.id_info, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_conn_sec_mode_enc(void const * const p_void_sec_mode, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_conn_sec_mode_t * p_sec_mode = (ble_gap_conn_sec_mode_t *)p_void_sec_mode; + uint32_t err_code = NRF_SUCCESS; + uint8_t temp8 = p_sec_mode->sm | (p_sec_mode->lv << 4); + + err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_conn_sec_mode_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec_mode) +{ + ble_gap_conn_sec_mode_t * p_sec_mode = (ble_gap_conn_sec_mode_t *)p_void_sec_mode; + uint32_t err_code = NRF_SUCCESS; + uint8_t temp8; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &temp8); + + p_sec_mode->sm = temp8; + p_sec_mode->lv = temp8 >> 4; + + return err_code; +} + +uint32_t ble_gap_evt_conn_sec_update_t_enc(void const * const p_void_conn_sec_update, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + return ble_gap_conn_sec_t_enc(p_void_conn_sec_update, p_buf, buf_len, p_index); +} + +uint32_t ble_gap_evt_conn_sec_update_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_conn_sec_update) +{ + return ble_gap_conn_sec_t_dec(p_buf, buf_len, p_index, p_void_conn_sec_update); +} + +uint32_t ble_gap_conn_sec_t_enc(void const * const p_void_sec, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_conn_sec_t * p_conn_sec = (ble_gap_conn_sec_t *)p_void_sec; + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_gap_conn_sec_mode_enc(&p_conn_sec->sec_mode, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&p_conn_sec->encr_key_size, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_conn_sec_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec) +{ + ble_gap_conn_sec_t * p_conn_sec = (ble_gap_conn_sec_t *)p_void_sec; + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_gap_conn_sec_mode_dec(p_buf, buf_len, p_index, &p_conn_sec->sec_mode); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &p_conn_sec->encr_key_size); + + return err_code; +} + +uint32_t ble_gap_evt_sec_info_request_t_enc(void const * const p_void_sec_info_request, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_evt_sec_info_request_t * p_conn_sec = + (ble_gap_evt_sec_info_request_t *)p_void_sec_info_request; + + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_gap_addr_enc(&p_conn_sec->peer_addr, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_conn_sec->div, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint8_t temp8 = p_conn_sec->enc_info | + (p_conn_sec->id_info << 1) | + (p_conn_sec->sign_info << 2); + + err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_evt_sec_info_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_sec_info_request) +{ + //ble_gap_evt_sec_info_request_t * p_conn_sec = (ble_gap_evt_sec_info_request_t *)p_void_sec_info_request; + uint32_t err_code = NRF_SUCCESS; + + return err_code; +} + +uint32_t ble_gap_evt_connected_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_evt_connected_t * p_evt_conn = (ble_gap_evt_connected_t *)p_void_struct; + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_gap_addr_enc((void *)&p_evt_conn->peer_addr, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + p_buf[*p_index] = p_evt_conn->irk_match & 0x01; + p_buf[*p_index] |= (p_evt_conn->irk_match_idx & 0x7F) << 1; + (*p_index)++; + + err_code = ble_gap_conn_params_t_enc((void *)&p_evt_conn->conn_params, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_sec_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_sec_params_t * p_sec_params = (ble_gap_sec_params_t *)p_void_struct; + uint32_t err_code = NRF_SUCCESS; + uint8_t temp8; + + err_code = uint16_t_enc((void *)&p_sec_params->timeout, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + temp8 = p_sec_params->bond | + (p_sec_params->mitm << 1) | + (p_sec_params->io_caps << 2) | + (p_sec_params->oob << 5); + + err_code = uint8_t_enc((void *)&temp8, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc((void *)&p_sec_params->min_key_size, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc((void *)&p_sec_params->max_key_size, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_sec_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gap_sec_params_t * p_sec_params = (ble_gap_sec_params_t *)p_void_struct; + uint32_t err_code = NRF_SUCCESS; + uint8_t temp8; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, (void *)&p_sec_params->timeout); + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, (void *)&temp8); + + p_sec_params->bond = temp8; + p_sec_params->mitm = temp8 >> 1; + p_sec_params->io_caps = temp8 >> 2; + p_sec_params->oob = temp8 >> 5; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, (void *)&p_sec_params->min_key_size); + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, (void *)&p_sec_params->max_key_size); + + return err_code; +} + +uint32_t ble_gap_evt_sec_params_request_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + return ble_gap_sec_params_t_enc(p_void_struct, p_buf, buf_len, p_index); +} + +uint32_t ble_gap_evt_sec_params_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + return ble_gap_sec_params_t_dec(p_buf, buf_len, p_index, p_void_struct); +} + +uint32_t ble_gap_evt_conn_param_update_t_enc(void const * const p_void_evt_conn_param_update, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + return ble_gap_conn_params_t_enc(p_void_evt_conn_param_update, p_buf, buf_len, p_index); +} + +uint32_t ble_gap_evt_conn_param_update_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_evt_conn_param_update) +{ + return ble_gap_conn_params_t_dec(p_buf, buf_len, p_index, p_void_evt_conn_param_update); +} + +uint32_t ble_gap_conn_params_t_enc(void const * const p_void_conn_params, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_conn_params_t * p_conn_params = (ble_gap_conn_params_t *)p_void_conn_params; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&p_conn_params->min_conn_interval, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_conn_params->max_conn_interval, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_conn_params->slave_latency, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_conn_params->conn_sup_timeout, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_conn_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_conn_params) +{ + ble_gap_conn_params_t * p_conn_params = (ble_gap_conn_params_t *)p_void_conn_params; + + SER_ASSERT_LENGTH_LEQ(*p_index + 2, buf_len); + uint16_dec(p_buf, buf_len, p_index, &p_conn_params->min_conn_interval); + + SER_ASSERT_LENGTH_LEQ(*p_index + 2, buf_len); + uint16_dec(p_buf, buf_len, p_index, &p_conn_params->max_conn_interval); + + SER_ASSERT_LENGTH_LEQ(*p_index + 2, buf_len); + uint16_dec(p_buf, buf_len, p_index, &p_conn_params->slave_latency); + + SER_ASSERT_LENGTH_LEQ(*p_index + 2, buf_len); + uint16_dec(p_buf, buf_len, p_index, &p_conn_params->conn_sup_timeout); + + return NRF_SUCCESS; +} + +uint32_t ble_gap_evt_disconnected_t_enc(void const * const p_void_disconnected, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gap_evt_disconnected_t * p_disconnected = (ble_gap_evt_disconnected_t *)p_void_disconnected; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint8_t_enc(&p_disconnected->reason, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_evt_disconnected_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_disconnected) +{ + ble_gap_evt_disconnected_t * p_disconnected = (ble_gap_evt_disconnected_t *)p_void_disconnected; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &p_disconnected->reason); + + return err_code; +} + +uint32_t ble_gap_opt_local_conn_latency_t_enc(void const * const p_void_local_conn_latency, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_local_conn_latency_t * p_latency = + (ble_gap_opt_local_conn_latency_t *)p_void_local_conn_latency; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&(p_latency->conn_handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_latency->requested_latency), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_latency->p_actual_latency, p_buf, buf_len, p_index, uint16_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_opt_local_conn_latency_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_local_conn_latency) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_local_conn_latency_t * p_latency = + (ble_gap_opt_local_conn_latency_t *)p_void_local_conn_latency; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_latency->conn_handle)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_latency->requested_latency)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, buf_len, p_index, (void **) &(p_latency->p_actual_latency), + uint16_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_opt_passkey_t_enc(void const * const p_void_passkey, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_passkey_t * p_opt_passkey = (ble_gap_opt_passkey_t *)p_void_passkey; + uint32_t err_code = NRF_SUCCESS; + uint16_t passkey_len = BLE_GAP_PASSKEY_LEN; + + err_code = buf_enc(p_opt_passkey->p_passkey, passkey_len, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_opt_passkey_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_passkey) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_passkey_t * p_opt_passkey = (ble_gap_opt_passkey_t *)p_void_passkey; + uint32_t err_code = NRF_SUCCESS; + uint16_t passkey_len = BLE_GAP_PASSKEY_LEN; + + err_code = buf_dec(p_buf, buf_len, p_index, &p_opt_passkey->p_passkey, passkey_len, + passkey_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_opt_privacy_t_enc(void const * const p_void_privacy, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_privacy_t * p_privacy = (ble_gap_opt_privacy_t *)p_void_privacy; + uint32_t err_code = NRF_SUCCESS; + + err_code = cond_field_enc(p_privacy->p_irk, p_buf, buf_len, p_index, ble_gap_irk_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_privacy->interval_s), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gap_opt_privacy_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_privacy) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + ble_gap_opt_privacy_t * p_privacy = (ble_gap_opt_privacy_t *)p_void_privacy; + uint32_t err_code = NRF_SUCCESS; + + err_code = cond_field_dec(p_buf, buf_len, p_index, (void **) &(p_privacy->p_irk), ble_gap_irk_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_privacy->interval_s)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gap_tx_power_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,68 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_gap_tx_power_set_req_enc(int8_t tx_power, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint8_t op_code = SD_BLE_GAP_TX_POWER_SET; + uint32_t err_code = NRF_SUCCESS; + uint32_t buf_len = *p_buf_len; + uint32_t index = 0; + + err_code = uint8_t_enc(&op_code, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&tx_power, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gap_tx_power_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GAP_TX_POWER_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_char_value_by_uuid_read.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,84 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_gattc_struct_serialization.h" +#include "cond_field_serialization.h" +#include "ble_types.h" +#include <string.h> + +uint32_t ble_gattc_char_value_by_uuid_read_req_enc + (uint16_t conn_handle, + ble_uuid_t const * const p_uuid, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t err_code; + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint8_t opcode = SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ; + + err_code = uint8_t_enc(&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_uuid, p_buf, buf_len, &index, ble_uuid_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_handle_range, p_buf, buf_len, &index, ble_gattc_handle_range_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gattc_char_value_by_uuid_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, + packet_len, + SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ, + p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_char_values_read.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,72 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +uint32_t ble_gattc_char_values_read_req_enc(uint16_t conn_handle, + uint16_t const * const p_handles, + uint16_t handle_count, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint32_t err_code; + uint8_t opCode = SD_BLE_GATTC_CHAR_VALUES_READ; + + err_code = uint8_t_enc(&opCode, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + err_code = count16_cond_data16_enc(p_handles, handle_count, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + *p_buf_len = index; + return err_code; +} + + +uint32_t ble_gattc_char_values_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_CHAR_VALUES_READ, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_characteristics_discover.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,81 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gattc_characteristics_discover_req_enc( + uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_CHARACTERISTICS_DISCOVER; + index += uint16_encode(conn_handle, &p_buf[index]); + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_handle_range != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_handle_range != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + 2, *p_buf_len); + + index += uint16_encode(p_handle_range->start_handle, &p_buf[index]); + index += uint16_encode(p_handle_range->end_handle, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_characteristics_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_CHARACTERISTICS_DISCOVER, + p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_descriptors_discover.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gattc_descriptors_discover_req_enc( + uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_DESCRIPTORS_DISCOVER; + index += uint16_encode(conn_handle, &p_buf[index]); + p_buf[index++] = (p_handle_range == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + if (p_handle_range != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + 2, *p_buf_len); + index += uint16_encode(p_handle_range->start_handle, &p_buf[index]); + index += uint16_encode(p_handle_range->end_handle, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_descriptors_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_DESCRIPTORS_DISCOVER, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_char_disc_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,126 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" +#include "nordic_common.h" + + +uint32_t ble_gattc_evt_char_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(2 + 2 + 2 + 2, packet_len); + + uint16_t tmp_conn_handle; + uint16_t tmp_gatt_status; + uint16_t tmp_error_handle; + uint16_t tmp_service_count; + uint16_dec(p_buf, packet_len, &index, &tmp_conn_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_gatt_status); + uint16_dec(p_buf, packet_len, &index, &tmp_error_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_service_count); + + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.char_disc_rsp) + + sizeof (uint16_t) + tmp_service_count * sizeof (ble_gattc_char_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_CHAR_DISC_RSP; + p_event->header.evt_len = event_len; + p_event->evt.gattc_evt.conn_handle = tmp_conn_handle; + p_event->evt.gattc_evt.gatt_status = tmp_gatt_status; + p_event->evt.gattc_evt.error_handle = tmp_error_handle; + p_event->evt.gattc_evt.params.char_disc_rsp.count = tmp_service_count; + + SER_ASSERT_LENGTH_LEQ(index + (tmp_service_count * 9), packet_len); + + for (uint16_t i = 0; i < tmp_service_count; i++) + { + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].uuid.uuid); + uint8_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].uuid.type); + + uint8_t characteristic_props; + uint8_dec(p_buf, packet_len, &index, &characteristic_props); + + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.broadcast = + !!(characteristic_props & BIT_0); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.read = + !!(characteristic_props & BIT_1); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.write_wo_resp = + !!(characteristic_props & BIT_2); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.write = + !!(characteristic_props & BIT_3); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.notify = + !!(characteristic_props & BIT_4); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.indicate = + !!(characteristic_props & BIT_5); + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_props.auth_signed_wr = + !!(characteristic_props & BIT_6); + + uint8_t characteristic_ext_props; + uint8_dec(p_buf, packet_len, &index, &characteristic_ext_props); + + p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].char_ext_props = + characteristic_ext_props & BIT_0; + + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].handle_decl); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.char_disc_rsp.chars[i].handle_value); + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_char_val_by_uuid_read_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,102 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "ble.h" +#include "ble_serialization.h" +#include "ble_gattc_struct_serialization.h" +#include "ble_gattc_evt_app.h" + + +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + uint32_t index = 0; + uint32_t err_code; + uint16_t conn_handle; + uint16_t gatt_status; + uint16_t error_handle; + + SER_ASSERT_LENGTH_LEQ(6, packet_len - index); + + uint32_t in_event_len = *p_event_len; + + *p_event_len = (offsetof(ble_evt_t, evt.gattc_evt.params)) - sizeof (ble_evt_hdr_t); + + uint16_dec(p_buf, packet_len, &index, &conn_handle); + uint16_dec(p_buf, packet_len, &index, &gatt_status); + uint16_dec(p_buf, packet_len, &index, &error_handle); + + void * p_data = NULL; + + if (p_event) + { + SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len); + + p_event->header.evt_id = BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP; + p_event->evt.gattc_evt.conn_handle = conn_handle; + p_event->evt.gattc_evt.gatt_status = gatt_status; + p_event->evt.gattc_evt.error_handle = error_handle; + + p_data = &p_event->evt.gattc_evt.params.char_val_by_uuid_read_rsp; + } + else + { + p_data = NULL; + } + + //call struct decoder with remaining size of event struct + uint32_t temp_event_len = in_event_len - *p_event_len; + err_code = ble_gattc_evt_char_val_by_uuid_read_rsp_t_dec(p_buf, packet_len, &index, + &temp_event_len, p_data); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + //update event length with the amount processed by struct decoder + *p_event_len += temp_event_len; + + if (p_event) + { + p_event->header.evt_len = *p_event_len; + } + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_char_vals_read_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_gattc_struct_serialization.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + +#define BLE_GATTC_EVT_CHAR_VALS_READ_RSP_LEN_POSITION 6 + + +uint32_t ble_gattc_evt_char_vals_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len = 0; + + uint32_t error_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(10, packet_len); + + event_len = (uint16_t) (offsetof(ble_evt_t, evt.gattc_evt.params.char_vals_read_rsp.values)) - + sizeof (ble_evt_hdr_t) + + uint16_decode(&p_buf[BLE_GATTC_EVT_CHAR_VALS_READ_RSP_LEN_POSITION]); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + else + { + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + *p_event_len = event_len; + } + + p_event->header.evt_id = BLE_GATTC_EVT_CHAR_VALS_READ_RSP; + + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle)); + + //Event structure for BLE_GATTC_EVT_CHAR_VALS_READ_RSP + error_code = + ble_gattc_evt_char_vals_read_rsp_t_dec(p_buf, packet_len, &index, + &(p_event->evt.gattc_evt.params.char_vals_read_rsp)); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return error_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_desc_disc_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,96 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_desc_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 6, packet_len); + + uint16_t tmp_conn_handle; + uint16_t tmp_gatt_status; + uint16_t tmp_error_handle; + uint16_t tmp_service_count; + uint16_dec(p_buf, packet_len, &index, &tmp_conn_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_gatt_status); + uint16_dec(p_buf, packet_len, &index, &tmp_error_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_service_count); + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.desc_disc_rsp) + + sizeof (uint16_t) + tmp_service_count * sizeof (ble_gattc_desc_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_DESC_DISC_RSP; + p_event->header.evt_len = event_len; + p_event->evt.gattc_evt.conn_handle = tmp_conn_handle; + p_event->evt.gattc_evt.gatt_status = tmp_gatt_status; + p_event->evt.gattc_evt.error_handle = tmp_error_handle; + p_event->evt.gattc_evt.params.desc_disc_rsp.count = tmp_service_count; + + SER_ASSERT_LENGTH_LEQ(index + (tmp_service_count * 5), packet_len); + + for (uint16_t i = 0; i < tmp_service_count; i++) + { + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.desc_disc_rsp.descs[i].handle); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.desc_disc_rsp.descs[i].uuid.uuid); + uint8_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.desc_disc_rsp.descs[i].uuid.type); + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_hvx.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_hvx_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint16_t tmp_attr_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(11, packet_len); + + tmp_attr_len = uint16_decode(&(p_buf[9])); + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.hvx) + + sizeof (ble_gattc_evt_hvx_t) - 1 + tmp_attr_len; + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_HVX; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.hvx.handle)); + uint8_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.hvx.type)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.hvx.len)); + + SER_ASSERT_LENGTH_LEQ(index + tmp_attr_len, packet_len); + + if (tmp_attr_len > 0) + { + memcpy(&(p_event->evt.gattc_evt.params.hvx.data[0]), &(p_buf[index]), tmp_attr_len); + index += tmp_attr_len; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_prim_srvc_disc_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,101 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_prim_srvc_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 6, packet_len); + + uint16_t tmp_conn_handle; + uint16_t tmp_gatt_status; + uint16_t tmp_error_handle; + uint16_t tmp_service_count; + uint16_dec(p_buf, packet_len, &index, &tmp_conn_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_gatt_status); + uint16_dec(p_buf, packet_len, &index, &tmp_error_handle); + uint16_dec(p_buf, packet_len, &index, &tmp_service_count); + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.prim_srvc_disc_rsp) + + sizeof (uint16_t) + tmp_service_count * sizeof (ble_gattc_service_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP; + p_event->header.evt_len = event_len; + p_event->evt.gattc_evt.conn_handle = tmp_conn_handle; + p_event->evt.gattc_evt.gatt_status = tmp_gatt_status; + p_event->evt.gattc_evt.error_handle = tmp_error_handle; + p_event->evt.gattc_evt.params.prim_srvc_disc_rsp.count = tmp_service_count; + + SER_ASSERT_LENGTH_LEQ(index + (tmp_service_count * 7), packet_len); + + for (uint16_t i = 0; i < tmp_service_count; i++) + { + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.uuid); + uint8_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].uuid.type); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].handle_range. + start_handle); + uint16_dec(p_buf, packet_len, &index, + &p_event->evt.gattc_evt.params.prim_srvc_disc_rsp.services[i].handle_range. + end_handle); + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_read_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint16_t tmp_attr_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(12, packet_len); + + tmp_attr_len = uint16_decode(&(p_buf[10])); + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.read_rsp) + + sizeof (ble_gattc_evt_read_rsp_t) - 1 + tmp_attr_len; + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_READ_RSP; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.read_rsp.handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.read_rsp.offset)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.read_rsp.len)); + + SER_ASSERT_LENGTH_LEQ(index + tmp_attr_len, packet_len); + + if (tmp_attr_len > 0) + { + memcpy(&(p_event->evt.gattc_evt.params.read_rsp.data[0]), &(p_buf[index]), tmp_attr_len); + index += tmp_attr_len; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_rel_disc_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,88 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_gattc_struct_serialization.h" +#include "ble_serialization.h" +#include "app_util.h" + +#define BLE_GATTC_EVT_REL_DISC_RSP_COUNT_POSITION 6 + + +uint32_t ble_gattc_evt_rel_disc_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len = 0; + uint16_t include_count = 0; + + uint32_t error_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(8, packet_len); + + include_count = uint16_decode(&p_buf[BLE_GATTC_EVT_REL_DISC_RSP_COUNT_POSITION]); + event_len = (uint16_t) (offsetof(ble_evt_t, evt.gattc_evt.params.rel_disc_rsp.includes)) - + sizeof (ble_evt_hdr_t) + (include_count * sizeof (ble_gattc_include_t)); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_REL_DISC_RSP; + error_code = + uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = + ble_gattc_evt_rel_disc_rsp_t_dec(p_buf, packet_len, &index, + &(p_event->evt.gattc_evt.params.rel_disc_rsp)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return error_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_timeout.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,76 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + uint32_t event_len; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + event_len = offsetof(ble_evt_t, evt.gattc_evt.params.timeout) + + sizeof (ble_gattc_evt_timeout_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_TIMEOUT; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gattc_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gattc_evt.params.timeout.src); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_evt_write_rsp.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,89 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gattc_evt_write_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(13, packet_len); + + uint16_t tmp_attr_len = uint16_decode(&(p_buf[11])); + + uint32_t event_len = offsetof(ble_evt_t, evt.gattc_evt.params.write_rsp) + + sizeof (ble_gattc_evt_write_rsp_t) - 1 + tmp_attr_len; + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTC_EVT_WRITE_RSP; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.write_rsp.handle)); + uint8_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.write_rsp.write_op)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.write_rsp.offset)); + uint16_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.params.write_rsp.len)); + + SER_ASSERT_LENGTH_LEQ(index + tmp_attr_len, packet_len); + + if (tmp_attr_len > 0) + { + memcpy(&(p_event->evt.gattc_evt.params.write_rsp.data[0]), &(p_buf[index]), tmp_attr_len); + index += tmp_attr_len; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_hv_confirm.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,72 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_gattc_hv_confirm_req_enc(uint16_t conn_handle, + uint16_t handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint8_t op_code = SD_BLE_GATTC_HV_CONFIRM; + uint32_t err_code = NRF_SUCCESS; + uint32_t buf_len = *p_buf_len; + uint32_t index = 0; + + err_code = uint8_t_enc(&op_code, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return NRF_SUCCESS; +} + +uint32_t ble_gattc_hv_confirm_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_HV_CONFIRM, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_primary_services_discover.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,83 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gattc_primary_services_discover_req_enc(uint16_t conn_handle, + uint16_t start_handle, + ble_uuid_t const * const p_srvc_uuid, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 5, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER; + index += uint16_encode(conn_handle, &p_buf[index]); + index += uint16_encode(start_handle, &p_buf[index]); + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + + p_buf[index++] = (p_srvc_uuid != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_srvc_uuid != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + + index += uint16_encode(p_srvc_uuid->uuid, &p_buf[index]); + p_buf[index++] = p_srvc_uuid->type; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_primary_services_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER, + p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_read.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,71 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gattc_read_req_enc(uint16_t conn_handle, + uint16_t handle, + uint16_t offset, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 7, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_READ; + index += uint16_encode(conn_handle, &p_buf[index]); + index += uint16_encode(handle, &p_buf[index]); + index += uint16_encode(offset, &p_buf[index]); + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_read_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_READ, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_relationships_discover.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,81 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gattc_relationships_discover_req_enc( + uint16_t conn_handle, + ble_gattc_handle_range_t const * const p_handle_range, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_RELATIONSHIPS_DISCOVER; + index += uint16_encode(conn_handle, &p_buf[index]); + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_handle_range != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_handle_range != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len); + + index += uint16_encode(p_handle_range->start_handle, &p_buf[index]); + index += uint16_encode(p_handle_range->end_handle, &p_buf[index]); + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_relationships_discover_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_RELATIONSHIPS_DISCOVER, + p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_struct_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,386 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "ble_gattc_struct_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_serialization.h" +#include "app_util.h" +#include "ble_gattc.h" +#include "cond_field_serialization.h" +#include <string.h> + +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gattc_evt_char_val_by_uuid_read_rsp_t * p_read = + (ble_gattc_evt_char_val_by_uuid_read_rsp_t *) p_void_struct; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&(p_read->count), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_read->value_len), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint32_t i; + ble_gattc_handle_value_t * p_handle_value = &p_read->handle_value[0]; + + for (i = 0; i < p_read->count; i++) + { + err_code = uint16_t_enc(&(p_handle_value->handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(p_read->value_len, buf_len - *p_index); + memcpy(&p_buf[*p_index], p_handle_value->p_value, p_read->value_len); + *p_index += p_read->value_len; + + p_handle_value++; + } + + return err_code; +} + +uint32_t ble_gattc_evt_char_val_by_uuid_read_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_size, + void * const p_void_struct) +{ + ble_gattc_evt_char_val_by_uuid_read_rsp_t * p_read = + (ble_gattc_evt_char_val_by_uuid_read_rsp_t *) p_void_struct; + uint32_t err_code = NRF_SUCCESS; + uint16_t value_len; + uint16_t count; + uint32_t i; + + SER_ASSERT_LENGTH_LEQ(4, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &count); + uint16_dec(p_buf, buf_len, p_index, &value_len); + + uint32_t total_struct_size = *p_struct_size; + + //calculate the size of the struct + *p_struct_size = offsetof(ble_gattc_evt_char_val_by_uuid_read_rsp_t, handle_value[0]); + *p_struct_size += (uint8_t*)&(p_read->handle_value[count])-(uint8_t*)&(p_read->handle_value[0]); + *p_struct_size += value_len * count; + + if (p_read) + { + p_read->value_len = value_len; + p_read->count = count; + + ble_gattc_handle_value_t * p_handle_value; + uint8_t * p_value; + + SER_ASSERT_LENGTH_LEQ(*p_struct_size, total_struct_size); + + p_value = (uint8_t *)&p_read->handle_value[count]; + + for (i = 0; i < count; i++) + { + p_handle_value = (ble_gattc_handle_value_t *)&p_read->handle_value[i]; + p_handle_value->p_value = p_value; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &(p_handle_value->handle)); + + SER_ASSERT_LENGTH_LEQ(p_read->value_len, buf_len - *p_index); + memcpy(p_handle_value->p_value, &p_buf[*p_index], p_read->value_len); + *p_index += p_read->value_len; + + p_value += value_len; + } + } + else + { + *p_index += count * (value_len + 2); + } + + return err_code; +} + +uint32_t ble_gattc_evt_char_vals_read_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gattc_evt_char_vals_read_rsp_t * p_read = + (ble_gattc_evt_char_vals_read_rsp_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + //Encode len + error_code = uint16_t_enc(&(p_read->len), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + //Encode values + SER_ASSERT_LENGTH_LEQ(p_read->len, buf_len - *p_index); + memcpy(&p_buf[*p_index], p_read->values, p_read->len); + *p_index += p_read->len; + + return error_code; +} + +uint32_t ble_gattc_evt_char_vals_read_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gattc_evt_char_vals_read_rsp_t * p_read = + (ble_gattc_evt_char_vals_read_rsp_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + //Decode len + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &(p_read->len)); + + //Decode values + SER_ASSERT_LENGTH_LEQ(p_read->len, buf_len - *p_index); + memcpy(p_read->values, &p_buf[*p_index], p_read->len); + *p_index += p_read->len; + + return error_code; +} + +uint32_t ble_gattc_handle_range_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gattc_handle_range_t * p_range = (ble_gattc_handle_range_t *) p_void_struct; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&(p_range->start_handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_range->end_handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gattc_handle_range_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gattc_handle_range_t * p_range = (ble_gattc_handle_range_t *) p_void_struct; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(4, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &(p_range->start_handle)); + uint16_dec(p_buf, buf_len, p_index, &(p_range->end_handle)); + + return err_code; +} + + +uint32_t ble_gattc_service_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gattc_service_t * p_service = (ble_gattc_service_t *) p_void_struct; + + error_code = ble_uuid_t_enc(&(p_service->uuid), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_gattc_handle_range_t_enc(&(p_service->handle_range), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gattc_service_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gattc_service_t * p_service = (ble_gattc_service_t *) p_void_struct; + + error_code = ble_uuid_t_dec(p_buf, buf_len, p_index, &(p_service->uuid)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_gattc_handle_range_t_dec(p_buf, buf_len, p_index, &(p_service->handle_range)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gattc_include_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gattc_include_t * p_include = (ble_gattc_include_t *) p_void_struct; + + error_code = uint16_t_enc(&(p_include->handle), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_gattc_service_t_enc(&(p_include->included_srvc), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gattc_include_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gattc_include_t * p_include = (ble_gattc_include_t *) p_void_struct; + + error_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_include->handle)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_gattc_service_t_dec(p_buf, buf_len, p_index, &(p_include->included_srvc)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gattc_evt_rel_disc_rsp_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t error_code = NRF_SUCCESS; + uint32_t i; + ble_gattc_evt_rel_disc_rsp_t * p_rsp = (ble_gattc_evt_rel_disc_rsp_t *) p_void_struct; + + error_code = uint16_t_enc(&(p_rsp->count), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + ble_gattc_include_t * p_include = (ble_gattc_include_t *) p_rsp->includes; + + for (i = 0; i < p_rsp->count; i++) + { + error_code = ble_gattc_include_t_enc(p_include, p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + p_include++; + } + + return error_code; +} + +uint32_t ble_gattc_evt_rel_disc_rsp_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gattc_evt_rel_disc_rsp_t * p_rsp = (ble_gattc_evt_rel_disc_rsp_t *) p_void_struct; + uint16_t include_count; + uint32_t i; + + error_code = uint16_t_dec(p_buf, buf_len, p_index, &include_count); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + p_rsp->count = include_count; + + ble_gattc_include_t * p_include = (ble_gattc_include_t *) p_rsp->includes; + + for (i = 0; i < include_count; i++) + { + error_code = ble_gattc_include_t_dec(p_buf, buf_len, p_index, p_include); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + p_include++; + } + + return error_code; +} + +uint32_t ble_gattc_write_params_t_enc(void const * const p_void_write, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_void_write); + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + uint32_t err_code = NRF_SUCCESS; + + ble_gattc_write_params_t * p_write = (ble_gattc_write_params_t *)p_void_write; + + err_code = uint8_t_enc(&(p_write->write_op), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&(p_write->flags), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_write->handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_write->offset), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = len16data_enc(p_write->p_value, p_write->len, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gattc_write_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_write) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_write); + + uint32_t err_code = NRF_SUCCESS; + + ble_gattc_write_params_t * p_write = (ble_gattc_write_params_t *)p_void_write; + + err_code = uint8_t_dec(p_buf, buf_len, p_index, &(p_write->write_op)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_dec(p_buf, buf_len, p_index, &(p_write->flags)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_write->handle)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_write->offset)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = len16data_dec(p_buf, buf_len, p_index, &(p_write->p_value), &(p_write->len)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gattc_write.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,92 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gattc_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +uint32_t ble_gattc_write_req_enc(uint16_t conn_handle, + ble_gattc_write_params_t const * const p_write_params, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTC_WRITE; + index += uint16_encode(conn_handle, &p_buf[index]); + p_buf[index++] = (p_write_params == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + if (p_write_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 9, *p_buf_len); + p_buf[index++] = p_write_params->write_op; + p_buf[index++] = p_write_params->flags; + index += uint16_encode(p_write_params->handle, &p_buf[index]); + index += uint16_encode(p_write_params->offset, &p_buf[index]); + index += uint16_encode(p_write_params->len, &p_buf[index]); + + SER_ERROR_CHECK(p_write_params->len <= BLE_GATTC_WRITE_P_VALUE_LEN_MAX, + NRF_ERROR_INVALID_PARAM); + + p_buf[index++] = (p_write_params->p_value == NULL) ? + RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + if (p_write_params->p_value != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + p_write_params->len, *p_buf_len); + memcpy(&p_buf[index], p_write_params->p_value, p_write_params->len); + index += p_write_params->len; + } + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gattc_write_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTC_WRITE, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_characteristic_add.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,113 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gatts.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" +#include "ble_gatts_struct_serialization.h" + +uint32_t ble_gatts_characteristic_add_req_enc( + uint16_t service_handle, + ble_gatts_char_md_t const * const p_char_md, + ble_gatts_attr_t const * const p_attr_char_value, + ble_gatts_char_handles_t const * const p_handles, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + uint8_t opcode = SD_BLE_GATTS_CHARACTERISTIC_ADD; + uint32_t buf_len = *p_buf_len; + + err_code = uint8_t_enc(&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&service_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_char_md, p_buf, buf_len, &index, ble_gatts_char_md_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_attr_char_value, p_buf, buf_len, &index, ble_gatts_attr_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_handles, p_buf, buf_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gatts_characteristic_add_rsp_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint16_t * * const pp_handles, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + + uint32_t err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, buf_len, + SD_BLE_GATTS_CHARACTERISTIC_ADD, + p_result_code); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, buf_len); + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, + buf_len, + &index, + (void * *)pp_handles, + ble_gatts_char_handles_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, buf_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_descriptor_add.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,115 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "ble_gatts_app.h" +#include <stdlib.h> +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" +#include "ble_gatts_struct_serialization.h" + + +uint32_t ble_gatts_descriptor_add_req_enc(uint16_t char_handle, + ble_gatts_attr_t const * const p_attr, + uint16_t * const p_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1, *p_buf_len); + p_buf[index++] = SD_BLE_GATTS_DESCRIPTOR_ADD; + + uint32_t err_code = uint16_t_enc(&char_handle, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_attr, + p_buf, + *p_buf_len, + &index, + ble_gatts_attr_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_handle != NULL) ? RPC_BLE_FIELD_PRESENT : + RPC_BLE_FIELD_NOT_PRESENT; + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_gatts_descriptor_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_handle, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_BLE_GATTS_DESCRIPTOR_ADD, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + 2, packet_len); + SER_ASSERT_NOT_NULL(p_handle); + + uint16_dec(p_buf, packet_len, &index, p_handle); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_hvc.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,74 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_hvc_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + uint32_t event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gatts_evt_hvc_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTS_EVT_HVC; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.conn_handle); + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.params.hvc.handle); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_rw_authorize_request.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,85 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include "ble_serialization.h" +#include "ble_gatts_struct_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_rw_authorize_request_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + uint32_t in_event_len = *p_event_len; + + *p_event_len = offsetof(ble_evt_t, evt.gatts_evt.params) - sizeof (ble_evt_hdr_t); + + uint16_t conn_handle; + err_code = uint16_t_dec(p_buf, packet_len, &index, &conn_handle); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + void * p_void_authorize_request = NULL; + + if (p_event != NULL) + { + SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len); + + p_event->header.evt_id = BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST; + p_event->evt.gatts_evt.conn_handle = conn_handle; + + p_void_authorize_request = &(p_event->evt.gatts_evt.params.authorize_request); + } + uint32_t tmp_event_len = in_event_len - *p_event_len; + err_code = ble_gatts_evt_rw_authorize_request_t_dec(p_buf, + packet_len, + &index, + &tmp_event_len, + p_void_authorize_request); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_event_len += tmp_event_len; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_sc_confirm.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,72 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_sc_confirm_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE, packet_len); + + uint32_t event_len = SER_EVT_CONN_HANDLE_SIZE; + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTS_EVT_SC_CONFIRM; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.conn_handle); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_sys_attr_missing.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,74 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_sys_attr_missing_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(3, packet_len); + + uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gatts_evt.params.sys_attr_missing)) + + sizeof (ble_gatts_evt_sys_attr_missing_t) - + sizeof (ble_evt_hdr_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTS_EVT_SYS_ATTR_MISSING; + p_event->header.evt_len = event_len; + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.params.sys_attr_missing.hint); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_timeout.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,73 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_timeout_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 1, packet_len); + + uint32_t event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gatts_evt_timeout_t); + + if (p_event == NULL) + { + *p_event_len = event_len; + return NRF_SUCCESS; + } + + SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE); + + p_event->header.evt_id = BLE_GATTS_EVT_TIMEOUT; + p_event->header.evt_len = event_len; + + uint16_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.conn_handle); + uint8_dec(p_buf, packet_len, &index, &p_event->evt.gatts_evt.params.timeout.src); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + *p_event_len = event_len; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_evt_write.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,85 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_evt_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gatts_struct_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_evt_write_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event_len); + + uint32_t err_code = NRF_SUCCESS; + uint32_t index = 0; + + uint32_t in_event_len = *p_event_len; + + *p_event_len = offsetof(ble_evt_t, evt.gatts_evt.params) - sizeof (ble_evt_hdr_t); + + uint16_t conn_handle; + err_code = uint16_t_dec(p_buf, packet_len, &index, &conn_handle); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + void * p_void_write = NULL; + + if (p_event != NULL) + { + SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len); + p_event->evt.gatts_evt.conn_handle = conn_handle; + + p_void_write = &(p_event->evt.gatts_evt.params.write); + } + + uint32_t tmp_struct_len = in_event_len - *p_event_len; + err_code = ble_gatts_evt_write_t_dec(p_buf, + packet_len, + &index, + &tmp_struct_len, + p_void_write); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_event_len += tmp_struct_len; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_hvx.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,139 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "cond_field_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_hvx_req_enc(uint16_t conn_handle, + ble_gatts_hvx_params_t const * const p_hvx_params, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT(p_hvx_params == NULL || + !(p_hvx_params->p_len == NULL && p_hvx_params->p_data != NULL), NRF_ERROR_NULL); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTS_HVX; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_hvx_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_hvx_params != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2 + 1 + 2 + 2, *p_buf_len); + index += uint16_encode(p_hvx_params->handle, &p_buf[index]); + p_buf[index++] = p_hvx_params->type; + index += uint16_encode(p_hvx_params->offset, &p_buf[index]); + + if (p_hvx_params->p_len != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + + SER_ERROR_CHECK(*p_hvx_params->p_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, + NRF_ERROR_INVALID_PARAM); + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + index += uint16_encode(*(p_hvx_params->p_len), &p_buf[index]); + + if (p_hvx_params->p_data != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + *(p_hvx_params->p_len), *p_buf_len); + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + memcpy(&(p_buf[index]), p_hvx_params->p_data, *(p_hvx_params->p_len)); + index += *(p_hvx_params->p_len); + } + else + { + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + } + } + else + { + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + } + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_hvx_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code, + uint16_t * * const pp_bytes_written) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t err_code; + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_BLE_GATTS_HVX, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, packet_len, &index, (void * *)pp_bytes_written, uint16_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_include_add.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,98 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include "ble_serialization.h" +#include "ble_gatts.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_include_add_req_enc(uint16_t service_handle, + uint16_t inc_srvc_handle, + uint16_t * const p_include_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + SER_ASSERT_LENGTH_LEQ(6, *p_buf_len); + + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint32_t err_code; + uint8_t opCode = SD_BLE_GATTS_INCLUDE_ADD; + uint8_t presenceFlag; + + err_code = uint8_t_enc(&opCode, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + err_code = uint16_t_enc(&service_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + err_code = uint16_t_enc(&inc_srvc_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + presenceFlag = (p_include_handle != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + err_code = uint8_t_enc(&presenceFlag, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + *p_buf_len = index; + return err_code; + +} + + +uint32_t ble_gatts_include_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_include_handle, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + uint32_t err_code; + + err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GATTS_INCLUDE_ADD, + p_result_code); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + SER_ASSERT_NOT_NULL(p_include_handle); + SER_ASSERT_LENGTH_LEQ(index + 2, packet_len); + err_code = uint16_t_dec(p_buf, packet_len, &index, p_include_handle); + SER_ASSERT_LENGTH_EQ(index, packet_len); + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_rw_authorize_reply.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include "ble_serialization.h" +#include "app_util.h" +#include "cond_field_serialization.h" +#include "ble_gatts_struct_serialization.h" + +uint32_t ble_gatts_rw_authorize_reply_req_enc( + uint16_t conn_handle, + ble_gatts_rw_authorize_reply_params_t const * const + p_reply_params, + uint8_t * const + p_buf, + uint32_t * const + p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint8_t opcode = SD_BLE_GATTS_RW_AUTHORIZE_REPLY; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint8_t_enc(&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_reply_params, p_buf, buf_len, &index, + ble_gatts_rw_authorize_reply_params_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gatts_rw_authorize_reply_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTS_RW_AUTHORIZE_REPLY, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_service_add.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,107 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include "ble_serialization.h" +#include "ble_gatts.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_service_add_req_enc(uint8_t type, + ble_uuid_t const * const p_uuid, + uint16_t const * const p_conn_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 1 + 1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTS_SERVICE_ADD; + p_buf[index++] = type; + + p_buf[index++] = (p_uuid != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_uuid != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + index += uint16_encode(p_uuid->uuid, &p_buf[index]); + p_buf[index++] = p_uuid->type; + } + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_conn_handle != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_service_add_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_conn_handle, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_GATTS_SERVICE_ADD, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_NOT_NULL(p_conn_handle); + + SER_ASSERT_LENGTH_LEQ(index + 2, packet_len); + uint16_dec(p_buf, packet_len, &index, p_conn_handle); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return decode_result; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_service_changed.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,79 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include "ble_serialization.h" +#include "ble_gatts.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_service_changed_req_enc(uint16_t conn_handle, + uint16_t start_handle, + uint16_t end_handle, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code; + uint8_t opcode = SD_BLE_GATTS_SERVICE_CHANGED; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + err_code = uint8_t_enc(&opcode, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&conn_handle, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&start_handle, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&end_handle, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_gatts_service_changed_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTS_SERVICE_CHANGED, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_struct_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,909 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_struct_serialization.h" +#include "ble_gap_struct_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_serialization.h" +#include "app_util.h" +#include "ble_gatts.h" +#include "cond_field_serialization.h" +#include <string.h> + +uint32_t ser_ble_gatts_char_pf_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_pf) +{ + ble_gatts_char_pf_t * p_char_pf = (ble_gatts_char_pf_t *)p_void_char_pf; + + SER_ASSERT_LENGTH_LEQ(7, buf_len - *p_index); + + uint8_dec(p_buf, buf_len, p_index, &p_char_pf->format); + uint8_dec(p_buf, buf_len, p_index, (uint8_t *)&p_char_pf->exponent); + uint16_dec(p_buf, buf_len, p_index, &p_char_pf->unit); + uint8_dec(p_buf, buf_len, p_index, &p_char_pf->name_space); + uint16_dec(p_buf, buf_len, p_index, &p_char_pf->desc); + + return NRF_SUCCESS; +} + +uint32_t ser_ble_gatts_char_pf_enc(void const * const p_void_char_pf, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_char_pf_t * p_char_pf = (ble_gatts_char_pf_t *)p_void_char_pf; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint8_t_enc(&p_char_pf->format, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&p_char_pf->exponent, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_pf->unit, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&p_char_pf->name_space, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_pf->desc, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_attr_md_enc(void const * const p_void_attr_md, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_attr_md_t * p_attr_md = (ble_gatts_attr_md_t *)p_void_attr_md; + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_gap_conn_sec_mode_enc(&p_attr_md->read_perm, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_conn_sec_mode_enc(&p_attr_md->write_perm, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + /* serializer does not support attributes on stack */ + if (p_attr_md->vloc != BLE_GATTS_VLOC_STACK) + { + err_code = NRF_ERROR_INVALID_PARAM; + } + + uint8_t temp8; + temp8 = p_attr_md->vlen | + (p_attr_md->vloc << 1) | + (p_attr_md->rd_auth << 3) | + (p_attr_md->wr_auth << 4); + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + p_buf[*p_index] = temp8; + *p_index += 1; + + return err_code; +} + +uint32_t ble_gatts_attr_md_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_attr_md) +{ + ble_gatts_attr_md_t * p_attr_md = (ble_gatts_attr_md_t *)p_void_attr_md; + uint32_t err_code = NRF_SUCCESS; + uint8_t temp8; + + err_code = ble_gap_conn_sec_mode_dec(p_buf, buf_len, p_index, &p_attr_md->read_perm); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gap_conn_sec_mode_dec(p_buf, buf_len, p_index, &p_attr_md->write_perm); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &temp8); + + p_attr_md->vlen = temp8; + p_attr_md->vloc = temp8 >> 1; + p_attr_md->rd_auth = temp8 >> 3; + p_attr_md->wr_auth = temp8 >> 4; + + return err_code; +} + +uint32_t ble_gatts_char_md_enc(void const * const p_void_char_md, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + + ble_gatts_char_md_t * p_char_md = (ble_gatts_char_md_t *)p_void_char_md; + uint8_t temp8; + + temp8 = p_char_md->char_props.broadcast | + (p_char_md->char_props.read << 1) | + (p_char_md->char_props.write_wo_resp << 2) | + (p_char_md->char_props.write << 3) | + (p_char_md->char_props.notify << 4) | + (p_char_md->char_props.indicate << 5) | + (p_char_md->char_props.auth_signed_wr << 6); + + err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + temp8 = p_char_md->char_ext_props.reliable_wr | + (p_char_md->char_ext_props.wr_aux << 1); + + err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_md->char_user_desc_max_size, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ERROR_CHECK(p_char_md->char_user_desc_size <= BLE_GATTS_VAR_ATTR_LEN_MAX, + NRF_ERROR_INVALID_PARAM); + err_code = len16data_enc(p_char_md->p_char_user_desc, p_char_md->char_user_desc_size, p_buf, + buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_char_md->p_char_pf, + p_buf, + buf_len, + p_index, + ser_ble_gatts_char_pf_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_char_md->p_user_desc_md, + p_buf, + buf_len, + p_index, + ble_gatts_attr_md_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_char_md->p_cccd_md, p_buf, buf_len, p_index, ble_gatts_attr_md_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_char_md->p_sccd_md, p_buf, buf_len, p_index, ble_gatts_attr_md_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_char_md_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_md) +{ + uint32_t err_code = NRF_SUCCESS; + + ble_gatts_char_md_t * p_char_md = (ble_gatts_char_md_t *)p_void_char_md; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint8_t temp8 = p_buf[*p_index]; + + p_char_md->char_props.broadcast = temp8 >> 0; + p_char_md->char_props.read = temp8 >> 1; + p_char_md->char_props.write_wo_resp = temp8 >> 2; + p_char_md->char_props.write = temp8 >> 3; + p_char_md->char_props.notify = temp8 >> 4; + p_char_md->char_props.indicate = temp8 >> 5; + p_char_md->char_props.auth_signed_wr = temp8 >> 6; + + temp8 = p_buf[*p_index + 1]; + p_char_md->char_ext_props.reliable_wr = temp8 >> 0; + p_char_md->char_ext_props.wr_aux = temp8 >> 1; + + *p_index += 2; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_char_md->char_user_desc_max_size); + + err_code = len16data_dec(p_buf, + buf_len, + p_index, + &p_char_md->p_char_user_desc, + &p_char_md->char_user_desc_size); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_char_md->p_char_pf, + ser_ble_gatts_char_pf_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_char_md->p_user_desc_md, + ble_gatts_attr_md_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_char_md->p_cccd_md, + ble_gatts_attr_md_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_char_md->p_sccd_md, + ble_gatts_attr_md_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; + +} + +uint32_t ble_gatts_attr_enc(void const * const p_void_gatts_attr, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + ble_gatts_attr_t * p_gatts_attr = (ble_gatts_attr_t *)p_void_gatts_attr; + + err_code = cond_field_enc((void *)p_gatts_attr->p_uuid, p_buf, buf_len, p_index, ble_uuid_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc((void *)p_gatts_attr->p_attr_md, + p_buf, + buf_len, + p_index, + ble_gatts_attr_md_enc); + SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_gatts_attr->init_offs, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_gatts_attr->max_len, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ERROR_CHECK(p_gatts_attr->init_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM); + //init len move just before p_data to be able to use len16data decoder. + err_code = len16data_enc(p_gatts_attr->p_value, p_gatts_attr->init_len, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_attr_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_gatts_attr) +{ + uint32_t err_code = NRF_SUCCESS; + ble_gatts_attr_t * p_gatts_attr = (ble_gatts_attr_t *)p_void_gatts_attr; + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_gatts_attr->p_uuid, + ble_uuid_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_dec(p_buf, + buf_len, + p_index, + (void * *)&p_gatts_attr->p_attr_md, + ble_gatts_attr_md_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(4, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_gatts_attr->init_offs); + uint16_dec(p_buf, buf_len, p_index, &p_gatts_attr->max_len); + + //init len move just before p_data to be able to use len16data decoder. + err_code = len16data_dec(p_buf, + buf_len, + p_index, + &p_gatts_attr->p_value, + &p_gatts_attr->init_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_char_handles_enc(void const * const p_void_char_handles, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_char_handles_t * p_char_handles = (ble_gatts_char_handles_t *)p_void_char_handles; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&p_char_handles->value_handle, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_handles->user_desc_handle, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_handles->cccd_handle, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_char_handles->sccd_handle, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_char_handles_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_char_handles) +{ + ble_gatts_char_handles_t * p_char_handles = (ble_gatts_char_handles_t *)p_void_char_handles; + + SER_ASSERT_LENGTH_LEQ(8, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &(p_char_handles->value_handle)); + uint16_dec(p_buf, buf_len, p_index, &p_char_handles->user_desc_handle); + uint16_dec(p_buf, buf_len, p_index, &p_char_handles->cccd_handle); + uint16_dec(p_buf, buf_len, p_index, &p_char_handles->sccd_handle); + + return NRF_SUCCESS; +} + +uint32_t ble_gatts_hvx_params_t_enc(void const * const p_void_hvx_params, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_hvx_params_t * p_hvx_params = (ble_gatts_hvx_params_t *)p_void_hvx_params; + + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(2 + 1 + 2, buf_len - *p_index); + + err_code = uint16_t_enc(&p_hvx_params->handle, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&p_hvx_params->type, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&p_hvx_params->offset, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (p_hvx_params->p_len != NULL) + { + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + p_buf[(*p_index)++] = RPC_BLE_FIELD_PRESENT; + + err_code = uint16_t_enc(p_hvx_params->p_len, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + } + + if (p_hvx_params->p_data != NULL) + { + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + p_buf[(*p_index)++] = RPC_BLE_FIELD_PRESENT; + + SER_ASSERT_LENGTH_LEQ(*p_hvx_params->p_len, buf_len - *p_index); + memcpy(&p_buf[*p_index], p_hvx_params->p_data, *p_hvx_params->p_len); + *p_index += *p_hvx_params->p_len; + } + + return err_code; +} + +uint32_t ble_gatts_hvx_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_hvx_params) +{ + ble_gatts_hvx_params_t * p_hvx_params = (ble_gatts_hvx_params_t *)p_void_hvx_params; + + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(2 + 1 + 2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_hvx_params->handle); + uint8_dec(p_buf, buf_len, p_index, &p_hvx_params->type); + uint16_dec(p_buf, buf_len, p_index, &p_hvx_params->offset); + + SER_ASSERT_NOT_NULL(&p_hvx_params->p_len); + err_code = cond_len16_cond_data_dec(p_buf, + buf_len, + p_index, + &p_hvx_params->p_data, + &p_hvx_params->p_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_attr_context_t_enc(void const * const p_void_attr_context, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gatts_attr_context_t * p_context = (ble_gatts_attr_context_t *) p_void_attr_context; + + error_code = ble_uuid_t_enc(&(p_context->srvc_uuid), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_uuid_t_enc(&(p_context->char_uuid), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_uuid_t_enc(&(p_context->desc_uuid), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint16_t_enc(&(p_context->srvc_handle), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint16_t_enc(&(p_context->value_handle), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint8_t_enc(&(p_context->type), p_buf, buf_len, p_index); + + return error_code; +} + +uint32_t ble_gatts_attr_context_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_attr_context) +{ + uint32_t error_code = NRF_SUCCESS; + ble_gatts_attr_context_t * p_context = (ble_gatts_attr_context_t *) p_void_attr_context; + + error_code = ble_uuid_t_dec(p_buf, buf_len, p_index, &(p_context->srvc_uuid)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_uuid_t_dec(p_buf, buf_len, p_index, &(p_context->char_uuid)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_uuid_t_dec(p_buf, buf_len, p_index, &(p_context->desc_uuid)); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + SER_ASSERT_LENGTH_LEQ(5, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &(p_context->srvc_handle)); + uint16_dec(p_buf, buf_len, p_index, &(p_context->value_handle)); + uint8_dec(p_buf, buf_len, p_index, &(p_context->type)); + return error_code; +} + +uint32_t ble_gatts_evt_write_t_enc(void const * const p_void_write, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_evt_write_t * p_write = (ble_gatts_evt_write_t *) p_void_write; + uint32_t error_code = NRF_SUCCESS; + + error_code = uint16_t_enc(&(p_write->handle), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint8_t_enc(&(p_write->op), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = ble_gatts_attr_context_t_enc(&(p_write->context), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + error_code = uint16_t_enc(&(p_write->offset), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + uint16_t data_len = p_write->len; + error_code = uint16_t_enc(&data_len, p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + SER_ASSERT_LENGTH_LEQ(data_len, buf_len - *p_index); + memcpy(&p_buf[*p_index], p_write->data, data_len); + *p_index += data_len; + + return error_code; +} + +uint32_t ble_gatts_evt_write_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_write) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_struct_len); + + uint32_t err_code = NRF_SUCCESS; + uint32_t in_struct_len = *p_struct_len; + + *p_struct_len = offsetof(ble_gatts_evt_write_t, data); + + uint16_t handle; + err_code = uint16_t_dec(p_buf, buf_len, p_index, &handle); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint8_t op; + err_code = uint8_t_dec(p_buf, buf_len, p_index, &op); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + ble_gatts_attr_context_t context; + err_code = ble_gatts_attr_context_t_dec(p_buf, buf_len, p_index, &context); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint16_t offset; + err_code = uint16_t_dec(p_buf, buf_len, p_index, &offset); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint16_t len; + err_code = uint16_t_dec(p_buf, buf_len, p_index, &len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_struct_len += len; + + if (p_void_write != NULL) + { + ble_gatts_evt_write_t * p_write = (ble_gatts_evt_write_t *)p_void_write; + + SER_ASSERT_LENGTH_LEQ(*p_struct_len, in_struct_len); + + p_write->handle = handle; + p_write->op = op; + + memcpy(&(p_write->context), &context, sizeof (ble_gatts_attr_context_t)); + + p_write->offset = offset; + p_write->len = len; + + SER_ASSERT_LENGTH_LEQ(p_write->len, buf_len - *p_index); + memcpy(p_write->data, &p_buf[*p_index], p_write->len); + } + + *p_index += len; + + return err_code; +} + +uint32_t ble_gatts_evt_read_t_enc(void const * const p_void_read, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_read); + + ble_gatts_evt_read_t * p_read = (ble_gatts_evt_read_t *)p_void_read; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&(p_read->handle), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = ble_gatts_attr_context_t_enc(&(p_read->context), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_read->offset), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_gatts_evt_read_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_read) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + + uint32_t err_code = NRF_SUCCESS; + uint32_t in_struct_len = *p_struct_len; + + *p_struct_len = sizeof (ble_gatts_evt_read_t); + + uint16_t handle; + err_code = uint16_t_dec(p_buf, buf_len, p_index, &handle); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + ble_gatts_attr_context_t context; + err_code = ble_gatts_attr_context_t_dec(p_buf, buf_len, p_index, &context); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint16_t offset; + err_code = uint16_t_dec(p_buf, buf_len, p_index, &offset); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (p_void_read != NULL) + { + ble_gatts_evt_read_t * p_read = (ble_gatts_evt_read_t *)p_void_read; + + SER_ASSERT_LENGTH_LEQ(*p_struct_len, in_struct_len); + + p_read->handle = handle; + memcpy(&(p_read->context), &context, sizeof (context)); + p_read->offset = offset; + } + + return err_code; +} + +uint32_t ble_gatts_evt_rw_authorize_request_t_enc(void const * const p_void_authorize_request, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_authorize_request); + + ble_gatts_evt_rw_authorize_request_t * p_authorize_request = + (ble_gatts_evt_rw_authorize_request_t *)p_void_authorize_request; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint8_t_enc(&(p_authorize_request->type), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + switch (p_authorize_request->type) + { + case BLE_GATTS_AUTHORIZE_TYPE_READ: + err_code = ble_gatts_evt_read_t_enc(&(p_authorize_request->request.read), + p_buf, + buf_len, + p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + break; + + case BLE_GATTS_AUTHORIZE_TYPE_WRITE: + err_code = ble_gatts_evt_write_t_enc(&(p_authorize_request->request.write), + p_buf, + buf_len, + p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + break; + + default: + case BLE_GATTS_AUTHORIZE_TYPE_INVALID: + err_code = NRF_ERROR_INVALID_PARAM; + break; + } + + return err_code; +} + +uint32_t ble_gatts_evt_rw_authorize_request_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_authorize_request) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_struct_len); + SER_ASSERT_NOT_NULL(p_index); + + uint32_t err_code = NRF_SUCCESS; + + uint8_t type; + err_code = uint8_t_dec(p_buf, buf_len, p_index, &type); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint32_t in_struct_len = *p_struct_len; + + *p_struct_len = offsetof(ble_gatts_evt_rw_authorize_request_t, request); + + ble_gatts_evt_rw_authorize_request_t * p_authorize_request = + (ble_gatts_evt_rw_authorize_request_t *)p_void_authorize_request; + + void * p_void_request = NULL; + + if (p_void_authorize_request != NULL) + { + p_authorize_request->type = type; + + SER_ASSERT_LENGTH_LEQ(*p_struct_len, in_struct_len); + + switch (type) + { + case BLE_GATTS_AUTHORIZE_TYPE_READ: + p_void_request = &(p_authorize_request->request.read); + break; + + case BLE_GATTS_AUTHORIZE_TYPE_WRITE: + p_void_request = &(p_authorize_request->request.write); + break; + + default: + case BLE_GATTS_AUTHORIZE_TYPE_INVALID: + return NRF_ERROR_INVALID_DATA; + } + } + + switch (type) + { + case BLE_GATTS_AUTHORIZE_TYPE_READ: + err_code = ble_gatts_evt_read_t_dec(p_buf, + buf_len, + p_index, + &in_struct_len, + p_void_request); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + break; + + case BLE_GATTS_AUTHORIZE_TYPE_WRITE: + err_code = ble_gatts_evt_write_t_dec(p_buf, + buf_len, + p_index, + &in_struct_len, + p_void_request); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + break; + + default: + case BLE_GATTS_AUTHORIZE_TYPE_INVALID: + return NRF_ERROR_INVALID_DATA; + } + + *p_struct_len += in_struct_len; + + return err_code; +} + +uint32_t ble_gatts_read_authorize_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_read_authorize_params_t * p_params = + (ble_gatts_read_authorize_params_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + error_code = uint16_t_enc(&(p_params->gatt_status), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + uint8_t temp_val = p_params->update; + error_code = uint8_t_enc(&temp_val, p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + error_code = uint16_t_enc(&(p_params->offset), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + error_code = len16data_enc(p_params->p_data, p_params->len, p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gatts_read_authorize_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gatts_read_authorize_params_t * p_params = + (ble_gatts_read_authorize_params_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_params->gatt_status); + + uint8_t temp_val; + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &temp_val); + p_params->update = temp_val; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_params->offset); + + error_code = len16data_dec(p_buf, buf_len, p_index, &p_params->p_data, &p_params->len); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gatts_write_authorize_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_write_authorize_params_t * p_params = + (ble_gatts_write_authorize_params_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + error_code = uint16_t_enc(&(p_params->gatt_status), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + return error_code; +} + +uint32_t ble_gatts_write_authorize_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gatts_write_authorize_params_t * p_params = + (ble_gatts_write_authorize_params_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_params->gatt_status); + + return error_code; +} + +uint32_t ble_gatts_rw_authorize_reply_params_t_enc(void const * const p_void_struct, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_gatts_rw_authorize_reply_params_t const * const p_params = + (ble_gatts_rw_authorize_reply_params_t * ) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + error_code = uint8_t_enc(&(p_params->type), p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + + if (p_params->type == BLE_GATTS_AUTHORIZE_TYPE_READ) + { + error_code = ble_gatts_read_authorize_params_t_enc(&p_params->params.read, + p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + } + else if (p_params->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) + { + error_code = ble_gatts_write_authorize_params_t_enc(&p_params->params.write, + p_buf, buf_len, p_index); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + } + else + { + return NRF_ERROR_INVALID_PARAM; + } + + return error_code; +} + +uint32_t ble_gatts_rw_authorize_reply_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_struct) +{ + ble_gatts_rw_authorize_reply_params_t * p_params = + (ble_gatts_rw_authorize_reply_params_t *) p_void_struct; + uint32_t error_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &(p_params->type)); + + if (p_params->type == BLE_GATTS_AUTHORIZE_TYPE_READ) + { + error_code = ble_gatts_read_authorize_params_t_dec(p_buf, buf_len, p_index, + &p_params->params.read); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + } + else if (p_params->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) + { + error_code = ble_gatts_write_authorize_params_t_dec(p_buf, buf_len, p_index, + &p_params->params.write); + SER_ASSERT(error_code == NRF_SUCCESS, error_code); + } + else + { + return NRF_ERROR_INVALID_PARAM; + } + + return error_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_sys_attr_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,132 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <stdlib.h> +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_sys_attr_get_req_enc(uint16_t conn_handle, + uint8_t const * const p_sys_attr_data, + uint16_t const * const p_sys_attr_data_len, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + p_buf[index++] = SD_BLE_GATTS_SYS_ATTR_GET; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_sys_attr_data_len != NULL) ? RPC_BLE_FIELD_PRESENT : + RPC_BLE_FIELD_NOT_PRESENT; + + if (p_sys_attr_data_len != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + index += uint16_encode(*p_sys_attr_data_len, &p_buf[index]); + } + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + p_buf[index++] = (p_sys_attr_data != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_sys_attr_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_sys_attr_data, + uint16_t * const p_sys_attr_data_len, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_BLE_GATTS_SYS_ATTR_GET, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + 2 + 1, packet_len); + + uint16_t sys_attr_len; + uint16_dec(p_buf, packet_len, &index, &sys_attr_len); + + if (p_buf[index++] == RPC_BLE_FIELD_PRESENT) + { + SER_ASSERT_NOT_NULL(p_sys_attr_data); + SER_ASSERT_NOT_NULL(p_sys_attr_data_len); + SER_ASSERT(sys_attr_len <= *p_sys_attr_data_len, NRF_ERROR_DATA_SIZE); + + SER_ASSERT_LENGTH_LEQ(index + sys_attr_len, packet_len); + memcpy(p_sys_attr_data, &p_buf[index], sys_attr_len); + *p_sys_attr_data_len = sys_attr_len; + index += sys_attr_len; + } + else + { + if (p_sys_attr_data_len != NULL) + { + *p_sys_attr_data_len = sys_attr_len; + } + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_sys_attr_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,84 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gatts.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_gatts_sys_attr_set_req_enc(uint16_t conn_handle, + uint8_t const * const p_sys_attr_data, + uint16_t sys_attr_data_len, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTS_SYS_ATTR_SET; + index += uint16_encode(conn_handle, &p_buf[index]); + + p_buf[index++] = (p_sys_attr_data != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_sys_attr_data != NULL) + { + //lint -save -esym(670,memcpy) + SER_ERROR_CHECK(sys_attr_data_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM); + SER_ASSERT_LENGTH_LEQ(index + 2 + sys_attr_data_len, *p_buf_len); + index += uint16_encode(sys_attr_data_len, &p_buf[index]); + memcpy(&(p_buf[index]), p_sys_attr_data, sys_attr_data_len); + //lint -restore + index += sys_attr_data_len; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_sys_attr_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTS_SYS_ATTR_SET, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_value_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,121 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" + +uint32_t ble_gatts_value_get_req_enc(uint16_t handle, + uint16_t offset, + uint16_t const * const p_data_len, + uint8_t const * const p_data, + uint8_t * const p_buf, + uint32_t * p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + + SER_ASSERT_LENGTH_LEQ(1 + 2 + 2, *p_buf_len); + p_buf[index++] = SD_BLE_GATTS_VALUE_GET; + + index += uint16_encode(handle, &p_buf[index]); + index += uint16_encode(offset, &p_buf[index]); + + if (p_data_len != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + index += uint16_encode(*p_data_len, &p_buf[index]); + p_buf[index++] = (p_data == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + } + else + { + SER_ASSERT_LENGTH_LEQ(index + 1 + 1, *p_buf_len); + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_value_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * * const pp_value, + uint16_t * * const pp_value_len, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + SER_ASSERT_NOT_NULL(pp_value); + SER_ASSERT_NOT_NULL(pp_value_len); + + uint32_t err_code; + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, + packet_len, SD_BLE_GATTS_VALUE_GET, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + uint16_t value_buffer_size = *(*pp_value_len); + + err_code = cond_field_dec(p_buf, packet_len, &index, (void * *)pp_value_len, uint16_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + uint16_t value_len = (*pp_value_len) ? *(*pp_value_len) : 0; + err_code = buf_dec(p_buf, packet_len, &index, pp_value, value_buffer_size, value_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_gatts_value_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,126 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gatts_app.h" +#include <string.h> +#include "nrf_error.h" +#include "ble_serialization.h" +#include "app_util.h" + + +uint32_t ble_gatts_value_set_req_enc(uint16_t handle, + uint16_t offset, + uint16_t * const p_value_len, + uint8_t const * const p_value, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1 + 2 + 2, *p_buf_len); + + p_buf[index++] = SD_BLE_GATTS_VALUE_SET; + index += uint16_encode(handle, &p_buf[index]); + index += uint16_encode(offset, &p_buf[index]); + + if (p_value_len == NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + *p_buf_len = index; + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len); + + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + index += uint16_encode(*p_value_len, &p_buf[index]); + + if (p_value != NULL) + { + SER_ERROR_CHECK(*p_value_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM); + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + SER_ASSERT_LENGTH_LEQ(index + *p_value_len, *p_buf_len); + memcpy(&(p_buf[index]), p_value, *p_value_len); + index += *p_value_len; + } + else + { + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + } + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_gatts_value_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint16_t * const p_value_len, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_BLE_GATTS_VALUE_SET, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + sizeof (uint16_t), packet_len); + uint16_dec(p_buf, packet_len, &index, p_value_len); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_l2cap_cid_register.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,69 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +uint32_t ble_l2cap_cid_register_req_enc(uint16_t cid, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + + p_buf[index++] = SD_BLE_L2CAP_CID_REGISTER; + err_code = uint16_t_enc(&cid, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_l2cap_cid_register_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_CID_REGISTER, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_l2cap_cid_unregister.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,69 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_gap_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +uint32_t ble_l2cap_cid_unregister_req_enc(uint16_t cid, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + + p_buf[index++] = SD_BLE_L2CAP_CID_UNREGISTER; + err_code = uint16_t_enc(&cid, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_l2cap_cid_unregister_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_CID_UNREGISTER, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_l2cap_evt_rx.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,80 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "ble_l2cap_evt_app.h" + +uint32_t ble_l2cap_evt_rx_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_evt_t * const p_event, + uint32_t * const p_event_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_event); + SER_ASSERT_NOT_NULL(p_event_len); + + uint32_t index = 0; + uint32_t in_event_len = *p_event_len; + + *p_event_len = offsetof(ble_l2cap_evt_t, params); + + uint16_t evt_id; + + uint32_t err_code = uint16_t_dec(p_buf, packet_len, &index, &evt_id); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + void * p_rx = NULL; + + if (p_event) + { + err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.l2cap_evt.conn_handle)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + p_rx = &(p_event->evt.l2cap_evt.params.rx); + } + + uint32_t struct_len = in_event_len - *p_event_len; + err_code = ble_l2cap_evt_rx_t_dec(p_buf, packet_len, &index, &struct_len, p_rx); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_event_len += struct_len; + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_l2cap_tx.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,85 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> +#include "ble_l2cap_app.h" +#include "ble_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_gap.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" + +uint32_t ble_l2cap_tx_req_enc(uint16_t conn_handle, + ble_l2cap_header_t const * const p_l2cap_header, + uint8_t const * const p_data, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1, *p_buf_len); + p_buf[index++] = SD_BLE_L2CAP_TX; + + err_code = uint16_t_enc(&conn_handle, p_buf, *p_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_l2cap_header, p_buf, *p_buf_len, &index, ble_l2cap_header_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (p_l2cap_header != NULL) + { + err_code = buf_enc(p_data, p_l2cap_header->len, p_buf, *p_buf_len, &index); + } + else + { + err_code = buf_enc(NULL, 0, p_buf, *p_buf_len, &index); + } + + *p_buf_len = index; + + return err_code; +} + +uint32_t ble_l2cap_tx_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_TX, p_result_code); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_opt_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,128 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap_struct_serialization.h" +#include "app_util.h" + +uint32_t ble_opt_get_req_enc(uint32_t opt_id, + ble_opt_t const * const p_opt, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + SER_ASSERT_LENGTH_LEQ(1+4+1, *p_buf_len); // [OPCODE][OP_ID][PRESENT] + SER_ASSERT(((opt_id == BLE_GAP_OPT_LOCAL_CONN_LATENCY) || + (opt_id == BLE_GAP_OPT_PASSKEY) || + (opt_id == BLE_GAP_OPT_PRIVACY)), NRF_ERROR_INVALID_PARAM); + + p_buf[index++] = SD_BLE_OPT_GET; + + err_code = uint32_t_enc(&opt_id, p_buf, *p_buf_len, &index); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + p_buf[index++] = (p_opt == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + +uint32_t ble_opt_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_opt_id, + ble_opt_t * const p_opt, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_opt_id); + SER_ASSERT_NOT_NULL(p_opt); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_OPT_GET, + p_result_code); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + (void) uint32_t_dec(p_buf, packet_len, &index, p_opt_id); + SER_ASSERT(((*p_opt_id == BLE_GAP_OPT_LOCAL_CONN_LATENCY) || + (*p_opt_id == BLE_GAP_OPT_PASSKEY) || + (*p_opt_id == BLE_GAP_OPT_PRIVACY)), NRF_ERROR_INVALID_PARAM); + + switch (*p_opt_id) + { + case BLE_GAP_OPT_LOCAL_CONN_LATENCY: + err_code = ble_gap_opt_local_conn_latency_t_dec( p_buf, packet_len, &index, (void *)&(p_opt->gap.local_conn_latency)); + break; + case BLE_GAP_OPT_PASSKEY: + err_code = ble_gap_opt_passkey_t_dec( p_buf, packet_len, &index, (void *)&(p_opt->gap.passkey) ); + break; + case BLE_GAP_OPT_PRIVACY: + err_code = ble_gap_opt_privacy_t_dec( p_buf, packet_len, &index, (void *)&(p_opt->gap.privacy) ); + break; + } + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_opt_set.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,127 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_gap_struct_serialization.h" +#include "app_util.h" + + +uint32_t ble_opt_set_req_enc(uint32_t const opt_id, + ble_opt_t const * const p_opt, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t initial_buf_len = *p_buf_len; + + SER_ASSERT_LENGTH_LEQ(1 + 4 + 1, initial_buf_len); + + SER_ASSERT(((opt_id == BLE_GAP_OPT_LOCAL_CONN_LATENCY) || + (opt_id == BLE_GAP_OPT_PASSKEY) || + (opt_id == BLE_GAP_OPT_PRIVACY)), NRF_ERROR_INVALID_PARAM); + + p_buf[index++] = SD_BLE_OPT_SET; + + err_code = uint32_t_enc(&opt_id, p_buf, initial_buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (p_opt != NULL) + { + p_buf[index++] = RPC_BLE_FIELD_PRESENT; + + switch(opt_id) + { + case BLE_GAP_OPT_LOCAL_CONN_LATENCY: + err_code = ble_gap_opt_local_conn_latency_t_enc(&(p_opt->gap.local_conn_latency), + p_buf, initial_buf_len, &index); + break; + case BLE_GAP_OPT_PASSKEY: + err_code = ble_gap_opt_passkey_t_enc(&(p_opt->gap.passkey), p_buf, initial_buf_len, + &index); + break; + case BLE_GAP_OPT_PRIVACY: + err_code = ble_gap_opt_privacy_t_enc(&(p_opt->gap.privacy), p_buf, initial_buf_len, + &index); + break; + } + } + else + { + p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT; + } + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_opt_set_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + uint32_t error_code; + + error_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_OPT_SET, p_result_code); + + if (error_code != NRF_SUCCESS) + { + return error_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,511 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "ble_serialization.h" +#include "nrf_error.h" +#include "app_util.h" +#include <stddef.h> +#include <string.h> + +uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code, + uint32_t command_status, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + uint32_t index = 0; + + SER_ASSERT_LENGTH_LEQ(SER_CMD_RSP_HEADER_SIZE, *p_buf_len); + + //Encode Op Code. + p_buf[index++] = op_code; + + //Encode Status. + index += uint32_encode(command_status, &(p_buf[index])); + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf, + uint32_t * const p_pos, + uint32_t packet_len, + uint8_t op_code, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_pos); + SER_ASSERT_NOT_NULL(p_result_code); + + if (packet_len < SER_CMD_RSP_HEADER_SIZE) + { + return NRF_ERROR_DATA_SIZE; + } + + if (p_buf[(*p_pos)] != op_code) + { + return NRF_ERROR_INVALID_DATA; + } + + *p_result_code = uint32_decode(&(p_buf[(*p_pos) + SER_POS_RSP_STATUS_CODE])); + *p_pos += SER_CMD_RSP_HEADER_SIZE; + + return NRF_SUCCESS; +} + + +uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t op_code, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + uint32_t result_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, op_code, + p_result_code); + + if (result_code != NRF_SUCCESS) + { + return result_code; + } + + if (index != packet_len) + { + return NRF_ERROR_DATA_SIZE; + } + + return NRF_SUCCESS; +} + +uint32_t uint32_t_enc(void const * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_field); + SER_ASSERT_NOT_NULL(p_index); + + uint32_t * p_uint32 = (uint32_t *)p_field; + + SER_ASSERT_LENGTH_LEQ(4, buf_len - *p_index); + + *p_index += uint32_encode(*p_uint32, &p_buf[*p_index]); + + return NRF_SUCCESS; +} + +uint32_t uint32_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_field); + + uint32_t * p_uint32 = (uint32_t *)p_field; + + SER_ASSERT_LENGTH_LEQ(4, ((int32_t)buf_len - *p_index)); + + *p_uint32 = uint32_decode(&p_buf[*p_index]); + *p_index += 4; + + return NRF_SUCCESS; +} + +uint32_t uint16_t_enc(const void * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint16_t * p_u16 = (uint16_t *)p_field; + + SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index); + + *p_index += uint16_encode(*p_u16, &p_buf[*p_index]); + + return NRF_SUCCESS; +} + +uint32_t uint16_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field) +{ + uint16_t * p_u16 = (uint16_t *)p_field; + + SER_ASSERT_LENGTH_LEQ(2, ((int32_t)buf_len - *p_index)); + + *p_u16 = uint16_decode(&p_buf[*p_index]); + *p_index += 2; + + return NRF_SUCCESS; +} + +void uint16_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const index, + uint16_t * const value) +{ + SER_ASSERT_VOID_RETURN(*index + 2 <= buf_len); + *value = uint16_decode(&p_buf[*index]); + *index += 2; +} + +uint32_t uint8_t_enc(const void * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + + uint8_t * p_u8 = (uint8_t *)p_field; + p_buf[*p_index] = *p_u8; + *p_index += 1; + + return NRF_SUCCESS; +} + +uint32_t uint8_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * p_field) +{ + uint8_t * p_u8 = (uint8_t *)p_field; + + SER_ASSERT_LENGTH_LEQ(1, ((int32_t)buf_len - *p_index)); + *p_u8 = p_buf[*p_index]; + *p_index += 1; + + return NRF_SUCCESS; +} + +void uint8_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const index, + uint8_t * const value) +{ + SER_ASSERT_VOID_RETURN(*index + 1 <= buf_len); + *value = p_buf[*index]; + *index += 1; +} + + +void int8_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const index, + int8_t * const value) +{ + SER_ASSERT_VOID_RETURN(*index + 1 <= buf_len); + *value = p_buf[*index]; + *index += 1; +} + +uint32_t len8data_enc(uint8_t const * const p_data, + uint8_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + + err_code = uint8_t_enc(&dlen, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = buf_enc(p_data, dlen, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t len8data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint8_t * const p_len) +{ + uint32_t err_code = NRF_SUCCESS; + uint16_t out_buf_len = *p_len; + + err_code = uint8_t_dec(p_buf, buf_len, p_index, p_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = buf_dec(p_buf, buf_len, p_index, pp_data, out_buf_len, *p_len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t len16data_enc(uint8_t const * const p_data, + uint16_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&dlen, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = buf_enc(p_data, dlen, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t len16data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t * const p_dlen) +{ + uint32_t err_code = NRF_SUCCESS; + uint16_t out_buf_len = *p_dlen; + + err_code = uint16_t_dec(p_buf, buf_len, p_index, p_dlen); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = buf_dec(p_buf, buf_len, p_index, pp_data, out_buf_len, *p_dlen); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t count16_cond_data16_enc(uint16_t const * const p_data, + uint16_t const count, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t i = 0; + + SER_ASSERT_LENGTH_LEQ(3, ((int32_t)buf_len - *p_index)); + *p_index += uint16_encode(count, &p_buf[*p_index]); + + if (p_data) + { + SER_ASSERT_LENGTH_LEQ((2 * count + 1), ((int32_t)buf_len - *p_index)); + p_buf[*p_index] = RPC_BLE_FIELD_PRESENT; + *p_index += 1; + + //memcpy may fail in case of Endianness difference between application and connectivity processor + for (i = 0; i < count; i++) + { + *p_index += uint16_encode(p_data[i], &p_buf[*p_index]); + } + } + else + { + SER_ASSERT_LENGTH_LEQ((1), ((int32_t)buf_len - *p_index)); + p_buf[*p_index] = RPC_BLE_FIELD_NOT_PRESENT; + *p_index += 1; + } + + return NRF_SUCCESS; +} + +uint32_t count16_cond_data16_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint16_t * * const pp_data, + uint16_t * const p_count) + +{ + uint16_t count = 0; + uint8_t is_present = 0; + uint16_t i; + + SER_ASSERT_NOT_NULL(p_count); + SER_ASSERT_NOT_NULL(pp_data); + SER_ASSERT_NOT_NULL(*pp_data); + + SER_ASSERT_LENGTH_LEQ(3, ((int32_t)buf_len - (*p_index))); + + uint16_dec(p_buf, buf_len, p_index, &count); + + if (count > *p_count) + { + return NRF_ERROR_DATA_SIZE; + } + + SER_ASSERT_LENGTH_LEQ(count, *p_count); + + uint8_dec(p_buf, buf_len, p_index, &is_present); + + if (!is_present) + { + *pp_data = NULL; + return NRF_SUCCESS; + } + else + { + for (i = 0; i < count; i++ ) + { + uint16_dec(p_buf, buf_len, p_index, &((&(**pp_data))[i]) ); + } + *p_count = i; + } + return NRF_SUCCESS; +} + + + +uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t * * const pp_len) +{ + SER_ASSERT_NOT_NULL(pp_len); + SER_ASSERT_NOT_NULL(*pp_len); + SER_ASSERT_NOT_NULL(pp_data); + SER_ASSERT_NOT_NULL(*pp_data); + + SER_ASSERT_LENGTH_LEQ(2, ((int32_t)buf_len - (*p_index))); + uint8_t is_present = 0; + + uint8_dec(p_buf, buf_len, p_index, &is_present); + + if (!is_present) + { + *pp_len = NULL; //if length field is not present + (*p_index)++; //then data can not be present + *pp_data = NULL; + return NRF_SUCCESS; + } + else + { + return len16data_dec(p_buf, buf_len, p_index, pp_data, *pp_len); + } +} + +uint32_t op_status_enc(uint8_t op_code, + uint32_t return_code, + uint8_t * const p_buff, + uint32_t * const p_buff_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buff); + SER_ASSERT_NOT_NULL(p_buff_len); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_LENGTH_LEQ(SER_CMD_RSP_HEADER_SIZE, *p_buff_len - *p_index); + + //Encode Op Code. + p_buff[(*p_index)++] = op_code; + //Encode Status. + *p_index += uint32_encode(return_code, &(p_buff[*p_index])); + //update size of used buffer + *p_buff_len = *p_index; + + return NRF_SUCCESS; +} + +uint32_t op_status_cond_uint16_enc(uint8_t op_code, + uint32_t return_code, + uint16_t value, + uint8_t * const p_buff, + uint32_t * const p_buff_len, + uint32_t * const p_index) +{ + uint32_t status_code; + uint32_t init_buff_len = *p_buff_len; + + status_code = op_status_enc(op_code, return_code, p_buff, p_buff_len, p_index); + SER_ASSERT(status_code == NRF_SUCCESS, status_code); + + if (return_code == NRF_SUCCESS) //Add 16bit value when return_code is a success + { + *p_buff_len = init_buff_len; //restore original value - it has been modified by op_status_enc + status_code = uint16_t_enc(&value, p_buff, *p_buff_len, p_index); + *p_buff_len = *p_index; + SER_ASSERT(status_code == NRF_SUCCESS, status_code); + } + + return status_code; +} + +uint32_t buf_enc(uint8_t const * const p_data, + uint16_t const dlen, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + uint32_t err_code = NRF_SUCCESS; + uint8_t is_present = (p_data == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + err_code = uint8_t_enc(&is_present, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (p_data) + { + SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index)); + memcpy(&p_buf[*p_index], p_data, dlen); + *p_index += dlen; + } + + return err_code; +} + +uint32_t buf_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint8_t * * const pp_data, + uint16_t data_len, + uint16_t dlen) +{ + uint8_t is_present = 0; + + SER_ASSERT_LENGTH_LEQ(1, ((int32_t)buf_len - *p_index)); + uint8_dec(p_buf, buf_len, p_index, &is_present); + + if (is_present == RPC_BLE_FIELD_PRESENT) + { + SER_ASSERT_NOT_NULL(pp_data); + SER_ASSERT_NOT_NULL(*pp_data); + SER_ASSERT_LENGTH_LEQ(dlen, data_len); + SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index)); + memcpy(*pp_data, &p_buf[*p_index], dlen); + *p_index += dlen; + } + else + { + if (pp_data) + { + *pp_data = NULL; + } + } + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_struct_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,243 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_struct_serialization.h" +#include "ble_serialization.h" +#include "app_util.h" +#include "ble_types.h" +#include "ble_l2cap.h" +#include "ble.h" +#include "cond_field_serialization.h" +#include <string.h> + + +uint32_t ble_uuid_t_enc(void const * const p_void_uuid, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_uuid_t * p_uuid = (ble_uuid_t *)p_void_uuid; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&p_uuid->uuid, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint8_t_enc(&p_uuid->type, p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_uuid_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_uuid) +{ + ble_uuid_t * p_uuid = (ble_uuid_t *)p_void_uuid; + + SER_ASSERT_LENGTH_LEQ(3, buf_len - *p_index); + uint16_dec(p_buf, buf_len, p_index, &p_uuid->uuid); + uint8_dec(p_buf, buf_len, p_index, &p_uuid->type); + + return NRF_SUCCESS; +} + +uint32_t ble_uuid128_t_enc(void const * const p_void_uuid, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_uuid128_t * p_uuid = (ble_uuid128_t *)p_void_uuid; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(16, buf_len - *p_index); + + memcpy(&p_buf[*p_index], p_uuid->uuid128, sizeof (p_uuid->uuid128)); + + *p_index += sizeof (p_uuid->uuid128); + + return err_code; +} + +uint32_t ble_uuid128_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_uuid) +{ + ble_uuid128_t * p_uuid = (ble_uuid128_t *)p_void_uuid; + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(16, buf_len - *p_index); + + memcpy(p_uuid->uuid128, &p_buf[*p_index], sizeof (p_uuid->uuid128)); + + *p_index += sizeof (p_uuid->uuid128); + + return err_code; +} + +uint32_t ble_l2cap_header_t_enc(void const * const p_void_header, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_header); + + ble_l2cap_header_t * p_header = (ble_l2cap_header_t *)p_void_header; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_enc(&(p_header->len), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_enc(&(p_header->cid), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_l2cap_header_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_void_header) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_header); + + ble_l2cap_header_t * p_header = (ble_l2cap_header_t *)p_void_header; + uint32_t err_code = NRF_SUCCESS; + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_header->len)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_header->cid)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + return err_code; +} + +uint32_t ble_l2cap_evt_rx_t_enc(void const * const p_void_evt_rx, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_void_evt_rx); + + ble_l2cap_evt_rx_t * p_evt_rx = (ble_l2cap_evt_rx_t *)p_void_evt_rx; + uint32_t err_code = NRF_SUCCESS; + + err_code = ble_l2cap_header_t_enc(&(p_evt_rx->header), p_buf, buf_len, p_index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(p_evt_rx->header.len, buf_len - *p_index); + memcpy(&p_buf[*p_index], p_evt_rx->data, p_evt_rx->header.len); + *p_index += p_evt_rx->header.len; + + return err_code; +} + +uint32_t ble_l2cap_evt_rx_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + uint32_t * const p_struct_len, + void * const p_void_evt_rx) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_index); + SER_ASSERT_NOT_NULL(p_struct_len); + + ble_l2cap_evt_rx_t * p_evt_rx = (ble_l2cap_evt_rx_t *)p_void_evt_rx; + uint32_t err_code = NRF_SUCCESS; + + uint32_t total_struct_len = *p_struct_len; + + /* Get data length */ + uint32_t tmp_index = *p_index; + uint16_t len = 0; + + err_code = uint16_t_dec(p_buf, buf_len, &tmp_index, &len); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + /* Update struct length */ + *p_struct_len = offsetof(ble_l2cap_evt_rx_t, data[0]); + *p_struct_len += (uint8_t*)&(p_evt_rx->data[len]) - (uint8_t*)&(p_evt_rx->data[0]); + + /* Decode header and copy data */ + if (p_void_evt_rx != NULL) + { + SER_ASSERT_LENGTH_LEQ(*p_struct_len, total_struct_len); + + err_code = ble_l2cap_header_t_dec(p_buf, buf_len, p_index, &(p_evt_rx->header)); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_LEQ(p_evt_rx->header.len, buf_len - *p_index); + memcpy(p_evt_rx->data, &p_buf[*p_index], p_evt_rx->header.len); + *p_index += p_evt_rx->header.len; + } + + return err_code; +} + +uint32_t ble_enable_params_t_enc(void const * const p_data, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index) +{ + ble_enable_params_t * p_enable_params = (ble_enable_params_t *)p_data; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + + p_buf[*p_index] = p_enable_params->gatts_enable_params.service_changed; + (*p_index)++; + + return NRF_SUCCESS; +} + +uint32_t ble_enable_params_t_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * const p_data) +{ + ble_enable_params_t * p_enable_params = (ble_enable_params_t *) p_data; + + SER_ASSERT_LENGTH_LEQ(sizeof (ble_enable_params_t), buf_len - *p_index); + p_enable_params->gatts_enable_params.service_changed = p_buf[(*p_index)++]; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_tx_buffer_count_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,92 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" +#include "cond_field_serialization.h" + +uint32_t ble_tx_buffer_count_get_req_enc(uint8_t const * const p_count, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint8_t opcode = SD_BLE_TX_BUFFER_COUNT_GET; + uint32_t err_code; + uint32_t total_len = *p_buf_len; + + err_code = uint8_t_enc(&opcode, p_buf, total_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc(p_count, p_buf, total_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_tx_buffer_count_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * * const pp_count, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t index = 0; + uint32_t err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_TX_BUFFER_COUNT_GET, p_result_code); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, packet_len, &index, (void * *)pp_count, uint8_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_uuid_decode.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,108 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <stdlib.h> +#include <string.h> +#include "ble_serialization.h" +#include "cond_field_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + +uint32_t ble_uuid_decode_req_enc(uint8_t uuid_le_len, + uint8_t const * const p_uuid_le, + ble_uuid_t * const p_uuid, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t err_code; + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint8_t opcode = SD_BLE_UUID_DECODE; + + err_code = uint8_t_enc((void *)&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = len8data_enc(p_uuid_le, uuid_le_len, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc((void *)p_uuid, p_buf, buf_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_uuid_decode_rsp_dec(uint8_t const * const p_buf, + uint32_t buf_len, + ble_uuid_t * * const pp_uuid, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t err_code; + uint32_t index = 0; + + uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + buf_len, + SD_BLE_UUID_DECODE, + p_result_code); + + if (decode_result != NRF_SUCCESS) + { + return decode_result; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, buf_len); + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, buf_len, &index, (void * *)pp_uuid, ble_uuid_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, buf_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_uuid_encode.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,119 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_uuid_encode_req_enc(ble_uuid_t const * const p_uuid, + uint8_t const * const p_uuid_le_len, + uint8_t const * const p_uuid_le, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1 + 1, *p_buf_len); + + p_buf[index++] = SD_BLE_UUID_ENCODE; + + p_buf[index++] = (p_uuid != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT; + + if (p_uuid != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len); + index += uint16_encode(p_uuid->uuid, &p_buf[index]); + p_buf[index++] = p_uuid->type; + } + + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + p_buf[index++] = (p_uuid_le_len == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + p_buf[index++] = (p_uuid_le == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + + +uint32_t ble_uuid_encode_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint8_t * const p_uuid_le_len, + uint8_t * const p_uuid_le, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + uint32_t error_code; + + error_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_UUID_ENCODE, p_result_code); + + if (error_code != NRF_SUCCESS) + { + return error_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + + SER_ASSERT_LENGTH_LEQ(index + 1, packet_len); + uint8_t uuid_le_len = p_buf[index++]; + + if (p_uuid_le_len != NULL) + { + if (p_uuid_le != NULL) + { + SER_ASSERT_LENGTH_LEQ(index + uuid_le_len, packet_len); + SER_ASSERT(uuid_le_len <= *p_uuid_le_len, NRF_ERROR_DATA_SIZE); + memcpy(p_uuid_le, &(p_buf[index]), uuid_le_len); + index += uuid_le_len; + } + *p_uuid_le_len = uuid_le_len; + } + + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_uuid_vs_add.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,108 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "cond_field_serialization.h" +#include "ble_struct_serialization.h" +#include "ble_rpc_defines.h" +#include "app_util.h" + + +uint32_t ble_uuid_vs_add_req_enc(ble_uuid128_t const * const p_vs_uuid, + uint8_t * const p_uuid_type, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t err_code; + uint32_t index = 0; + uint32_t buf_len = *p_buf_len; + uint8_t opcode = SD_BLE_UUID_VS_ADD; + + err_code = uint8_t_enc(&opcode, p_buf, buf_len, &index); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc((void *)p_vs_uuid, p_buf, buf_len, &index, ble_uuid128_t_enc); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + err_code = cond_field_enc((void *)p_uuid_type, p_buf, buf_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + + +uint32_t ble_uuid_vs_add_rsp_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint8_t * * const pp_uuid_type, + uint32_t * const p_result_code) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + SER_ASSERT_NOT_NULL(pp_uuid_type); + + uint32_t err_code = NRF_SUCCESS; + uint32_t index = 0; + + err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + buf_len, + SD_BLE_UUID_VS_ADD, + p_result_code); + + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, buf_len); + return NRF_SUCCESS; + } + + err_code = cond_field_dec(p_buf, buf_len, &index, (void * *)pp_uuid_type, uint8_t_dec); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, buf_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/ble_version_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,96 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ble_app.h" +#include <string.h> +#include "ble_serialization.h" +#include "app_util.h" + +uint32_t ble_version_get_req_enc(ble_version_t const * const p_version, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(index + 2, *p_buf_len); + + p_buf[index++] = SD_BLE_VERSION_GET; + + SER_ASSERT_LENGTH_LEQ(index + 1, *p_buf_len); + + p_buf[index++] = (p_version == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + + *p_buf_len = index; + + return NRF_SUCCESS; +} + +uint32_t ble_version_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + ble_version_t * p_version, + uint32_t * const p_result_code) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + + uint32_t status_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, + SD_BLE_VERSION_GET, + p_result_code); + + if (status_code != NRF_SUCCESS) + { + return status_code; + } + + if (*p_result_code != NRF_SUCCESS) + { + SER_ASSERT_LENGTH_EQ(index, packet_len); + return NRF_SUCCESS; + } + uint8_dec(p_buf, packet_len, &index, &(p_version->version_number)); + uint16_dec(p_buf, packet_len, &index, &(p_version->company_id)); + uint16_dec(p_buf, packet_len, &index, &p_version->subversion_number); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return status_code; +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/cond_field_serialization.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,97 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "nrf_error.h" +#include "ble_rpc_defines.h" +#include "cond_field_serialization.h" +#include "ble_serialization.h" +#include <stddef.h> + +uint32_t cond_field_enc(void const * const p_field, + uint8_t * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + field_encoder_handler_t fp_field_encoder) +{ + uint32_t err_code = NRF_SUCCESS; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + p_buf[*p_index] = (p_field == NULL) ? RPC_BLE_FIELD_NOT_PRESENT : RPC_BLE_FIELD_PRESENT; + *p_index += 1; + + if (p_field && (fp_field_encoder != NULL)) + { + err_code = fp_field_encoder(p_field, p_buf, buf_len, p_index); + } + + return err_code; +} + + +uint32_t cond_field_dec(uint8_t const * const p_buf, + uint32_t buf_len, + uint32_t * const p_index, + void * * const pp_field, + field_decoder_handler_t fp_field_parser) +{ + uint32_t err_code = NRF_SUCCESS; + uint8_t is_present; + + SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index); + uint8_dec(p_buf, buf_len, p_index, &is_present); + + if (is_present == RPC_BLE_FIELD_PRESENT) + { + SER_ASSERT_NOT_NULL(pp_field); + SER_ASSERT_NOT_NULL(*pp_field); + + if (fp_field_parser != NULL) + { + err_code = fp_field_parser(p_buf, buf_len, p_index, *pp_field); + } + } + else if (is_present == RPC_BLE_FIELD_NOT_PRESENT) + { + if (pp_field != NULL) + { + *pp_field = NULL; + } + } + else + { + err_code = NRF_ERROR_INVALID_DATA; + } + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/power_system_off.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,55 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "nrf_soc_app.h" +#include "ble_serialization.h" +#include "ser_nrf_soc.h" + + +uint32_t power_system_off_req_enc(uint8_t * const p_buf, uint32_t * const p_buf_len) +{ + uint32_t index = 0; + + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + SER_ASSERT_LENGTH_LEQ(1, *p_buf_len); + + p_buf[index++] = SD_POWER_SYSTEM_OFF; + + *p_buf_len = index; + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/codecs/src/temp_get.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,94 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ser_nrf_soc.h" +#include "nrf_error.h" +#include "ble_serialization.h" +#include "cond_field_serialization.h" + +uint32_t temp_get_req_enc(int32_t const * const p_temp, + uint8_t * const p_buf, + uint32_t * const p_buf_len) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_buf_len); + + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + uint32_t total_len = *p_buf_len; + + SER_ASSERT_LENGTH_LEQ(1, total_len); + p_buf[index++] = SD_TEMP_GET; + + err_code = cond_field_enc(p_temp, p_buf, total_len, &index, NULL); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + *p_buf_len = index; + + return err_code; +} + +uint32_t temp_get_rsp_dec(uint8_t const * const p_buf, + uint32_t packet_len, + uint32_t * const p_result_code, + int32_t * const p_temp) +{ + SER_ASSERT_NOT_NULL(p_buf); + SER_ASSERT_NOT_NULL(p_result_code); + SER_ASSERT_NOT_NULL(p_temp); + + uint32_t index = 0; + uint32_t err_code = NRF_SUCCESS; + + err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, + &index, + packet_len, + SD_TEMP_GET, + p_result_code); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + if (*p_result_code != NRF_SUCCESS) + { + return NRF_SUCCESS; + } + + err_code = uint32_t_dec(p_buf, packet_len, &index, p_temp); + SER_ASSERT(err_code == NRF_SUCCESS, err_code); + + SER_ASSERT_LENGTH_EQ(index, packet_len); + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_bas.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,156 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_srv_bas Battery Service + * @{ + * @ingroup ble_sdk_srv + * @brief Battery Service module. + * + * @details This module implements the Battery Service with the Battery Level characteristic. + * During initialization it adds the Battery Service and Battery Level characteristic + * to the BLE stack database. Optionally it can also add a Report Reference descriptor + * to the Battery Level characteristic (used when including the Battery Service in + * the HID service). + * + * If specified, the module will support notification of the Battery Level characteristic + * through the ble_bas_battery_level_update() function. + * If an event handler is supplied by the application, the Battery Service will + * generate Battery Service events to the application. + * + * @note The application must propagate BLE stack events to the Battery Service module by calling + * ble_bas_on_ble_evt() from the from the @ref ble_stack_handler callback. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_BAS_H__ +#define BLE_BAS_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" + +/**@brief Battery Service event type. */ +typedef enum +{ + BLE_BAS_EVT_NOTIFICATION_ENABLED, /**< Battery value notification enabled event. */ + BLE_BAS_EVT_NOTIFICATION_DISABLED /**< Battery value notification disabled event. */ +} ble_bas_evt_type_t; + +/**@brief Battery Service event. */ +typedef struct +{ + ble_bas_evt_type_t evt_type; /**< Type of event. */ +} ble_bas_evt_t; + +// Forward declaration of the ble_bas_t type. +typedef struct ble_bas_s ble_bas_t; + +/**@brief Battery Service event handler type. */ +typedef void (*ble_bas_evt_handler_t) (ble_bas_t * p_bas, ble_bas_evt_t * p_evt); + +/**@brief Battery Service init structure. This contains all options and data needed for + * initialization of the service.*/ +typedef struct +{ + ble_bas_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Battery Service. */ + bool support_notification; /**< TRUE if notification of Battery Level measurement is supported. */ + ble_srv_report_ref_t * p_report_ref; /**< If not NULL, a Report Reference descriptor with the specified value will be added to the Battery Level characteristic */ + uint8_t initial_batt_level; /**< Initial battery level */ + ble_srv_cccd_security_mode_t battery_level_char_attr_md; /**< Initial security level for battery characteristics attribute */ + ble_gap_conn_sec_mode_t battery_level_report_read_perm; /**< Initial security level for battery report read attribute */ +} ble_bas_init_t; + +/**@brief Battery Service structure. This contains various status information for the service. */ +typedef struct ble_bas_s +{ + ble_bas_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Battery Service. */ + uint16_t service_handle; /**< Handle of Battery Service (as provided by the BLE stack). */ + ble_gatts_char_handles_t battery_level_handles; /**< Handles related to the Battery Level characteristic. */ + uint16_t report_ref_handle; /**< Handle of the Report Reference descriptor. */ + uint8_t battery_level_last; /**< Last Battery Level measurement passed to the Battery Service. */ + uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ + bool is_notification_supported; /**< TRUE if notification of Battery Level is supported. */ +} ble_bas_t; + +/**@brief Function for initializing the Battery Service. + * + * @param[out] p_bas Battery Service structure. This structure will have to be supplied by + * the application. It will be initialized by this function, and will later + * be used to identify this particular service instance. + * @param[in] p_bas_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_bas_init(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init); + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack of interest to the Battery Service. + * + * @note For the requirements in the BAS specification to be fulfilled, + * ble_bas_battery_level_update() must be called upon reconnection if the + * battery level has changed while the service has been disconnected from a bonded + * client. + * + * @param[in] p_bas Battery Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +void ble_bas_on_ble_evt(ble_bas_t * p_bas, ble_evt_t * p_ble_evt); + +/**@brief Function for updating the battery level. + * + * @details The application calls this function after having performed a battery measurement. If + * notification has been enabled, the battery level characteristic is sent to the client. + * + * @note For the requirements in the BAS specification to be fulfilled, + * this function must be called upon reconnection if the battery level has changed + * while the service has been disconnected from a bonded client. + * + * @param[in] p_bas Battery Service structure. + * @param[in] battery_level New battery measurement value (in percent of full capacity). + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_bas_battery_level_update(ble_bas_t * p_bas, uint8_t battery_level); + +#endif // BLE_BAS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_date_time.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) 2011 Nordic Semiconductor. All Rights Reserved. +* +* The information contained herein is property of Nordic Semiconductor ASA. +* Terms and conditions of usage are described in detail in NORDIC +* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. +* +* Licensees are granted free, non-transferable use of the information. NO +* WARRANTY of ANY KIND is provided. This heading must NOT be removed from +* the file. +*/ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +/** @file + * @brief Contains definition of ble_date_time structure. + */ + +/** @file + * + * @defgroup ble_sdk_srv_date_time BLE Date Time characteristic type + * @{ + * @ingroup ble_sdk_srv + * @brief Definition of ble_date_time_t type. + */ + +#ifndef BLE_DATE_TIME_H__ +#define BLE_DATE_TIME_H__ + +#include <stdint.h> + +/**@brief Date and Time structure. */ +typedef struct +{ + uint16_t year; + uint8_t month; + uint8_t day; + uint8_t hours; + uint8_t minutes; + uint8_t seconds; +} ble_date_time_t; + +static __INLINE uint8_t ble_date_time_encode(const ble_date_time_t * p_date_time, + uint8_t * p_encoded_data) +{ + uint8_t len = uint16_encode(p_date_time->year, p_encoded_data); + + p_encoded_data[len++] = p_date_time->month; + p_encoded_data[len++] = p_date_time->day; + p_encoded_data[len++] = p_date_time->hours; + p_encoded_data[len++] = p_date_time->minutes; + p_encoded_data[len++] = p_date_time->seconds; + + return len; +} + +static __INLINE uint8_t ble_date_time_decode(ble_date_time_t * p_date_time, + const uint8_t * p_encoded_data) +{ + uint8_t len = sizeof(uint16_t); + + p_date_time->year = uint16_decode(p_encoded_data); + p_date_time->month = p_encoded_data[len++]; + p_date_time->day = p_encoded_data[len++]; + p_date_time->hours = p_encoded_data[len++]; + p_date_time->minutes = p_encoded_data[len++]; + p_date_time->seconds = p_encoded_data[len++]; + + return len; +} + +#endif // BLE_DATE_TIME_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_dis.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,118 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_srv_dis Device Information Service + * @{ + * @ingroup ble_sdk_srv + * @brief Device Information Service module. + * + * @details This module implements the Device Information Service. + * During initialization it adds the Device Information Service to the BLE stack database. + * It then encodes the supplied information, and adds the curresponding characteristics. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_DIS_H__ +#define BLE_DIS_H__ + +#include <stdint.h> +#include "ble_srv_common.h" + +// Vendor ID Source values +#define BLE_DIS_VENDOR_ID_SRC_BLUETOOTH_SIG 1 /**< Vendor ID assigned by Bluetooth SIG. */ +#define BLE_DIS_VENDOR_ID_SRC_USB_IMPL_FORUM 2 /**< Vendor ID assigned by USB Implementer's Forum. */ + +/**@brief System ID parameters */ +typedef struct +{ + uint64_t manufacturer_id; /**< Manufacturer ID. Only 5 LSOs shall be used. */ + uint32_t organizationally_unique_id; /**< Organizationally unique ID. Only 3 LSOs shall be used. */ +} ble_dis_sys_id_t; + +/**@brief IEEE 11073-20601 Regulatory Certification Data List Structure */ +typedef struct +{ + uint8_t * p_list; /**< Pointer the byte array containing the encoded opaque structure based on IEEE 11073-20601 specification. */ + uint8_t list_len; /**< Length of the byte array. */ +} ble_dis_reg_cert_data_list_t; + +/**@brief PnP ID parameters */ +typedef struct +{ + uint8_t vendor_id_source; /**< Vendor ID Source. see @ref DIS_VENDOR_ID_SRC_VALUES. */ + uint16_t vendor_id; /**< Vendor ID. */ + uint16_t product_id; /**< Product ID. */ + uint16_t product_version; /**< Product Version. */ +} ble_dis_pnp_id_t; + +/**@brief Device Information Service init structure. This contains all possible characteristics + * needed for initialization of the service. + */ +typedef struct +{ + ble_srv_utf8_str_t manufact_name_str; /**< Manufacturer Name String. */ + ble_srv_utf8_str_t model_num_str; /**< Model Number String. */ + ble_srv_utf8_str_t serial_num_str; /**< Serial Number String. */ + ble_srv_utf8_str_t hw_rev_str; /**< Hardware Revision String. */ + ble_srv_utf8_str_t fw_rev_str; /**< Firmware Revision String. */ + ble_srv_utf8_str_t sw_rev_str; /**< Software Revision String. */ + ble_dis_sys_id_t * p_sys_id; /**< System ID. The helper function @ref dis_sys_id_encode can be used to encode the value of this characteristic. */ + ble_dis_reg_cert_data_list_t * p_reg_cert_data_list; /**< IEEE 11073-20601 Regulatory Certification Data List. */ + ble_dis_pnp_id_t * p_pnp_id; /**< PnP ID. The helper function @ref dis_pnp_id_encode can be used to encode the value of this characteristic. */ + ble_srv_security_mode_t dis_attr_md; /**< Initial Security Setting for Device Information Characteristics. */ +} ble_dis_init_t; + +/**@brief Function for initializing the Device Information Service. + * + * @details This call allows the application to initialize the device information service. + * It adds the DIS service and DIS characteristics to the database, using the initial + * values supplied through the p_dis_init parameter. Characteristics which are not to be + * added, shall be set to NULL in p_dis_init. + * + * @param[in] p_dis_init The structure containing the values of characteristics needed by the + * service. + * + * @return NRF_SUCCESS on successful initialization of service. + */ +uint32_t ble_dis_init(const ble_dis_init_t * p_dis_init); + +#endif // BLE_DIS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_hrs.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,217 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_srv_hrs Heart Rate Service + * @{ + * @ingroup ble_sdk_srv + * @brief Heart Rate Service module. + * + * @details This module implements the Heart Rate Service with the Heart Rate Measurement, + * Body Sensor Location and Heart Rate Control Point characteristics. + * During initialization it adds the Heart Rate Service and Heart Rate Measurement + * characteristic to the BLE stack database. Optionally it also adds the + * Body Sensor Location and Heart Rate Control Point characteristics. + * + * If enabled, notification of the Heart Rate Measurement characteristic is performed + * when the application calls ble_hrs_heart_rate_measurement_send(). + * + * The Heart Rate Service also provides a set of functions for manipulating the + * various fields in the Heart Rate Measurement characteristic, as well as setting + * the Body Sensor Location characteristic value. + * + * If an event handler is supplied by the application, the Heart Rate Service will + * generate Heart Rate Service events to the application. + * + * @note The application must propagate BLE stack events to the Heart Rate Service module by calling + * ble_hrs_on_ble_evt() from the from the @ref ble_stack_handler callback. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_HRS_H__ +#define BLE_HRS_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" + +// Body Sensor Location values +#define BLE_HRS_BODY_SENSOR_LOCATION_OTHER 0 +#define BLE_HRS_BODY_SENSOR_LOCATION_CHEST 1 +#define BLE_HRS_BODY_SENSOR_LOCATION_WRIST 2 +#define BLE_HRS_BODY_SENSOR_LOCATION_FINGER 3 +#define BLE_HRS_BODY_SENSOR_LOCATION_HAND 4 +#define BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE 5 +#define BLE_HRS_BODY_SENSOR_LOCATION_FOOT 6 + +#define BLE_HRS_MAX_BUFFERED_RR_INTERVALS 20 /**< Size of RR Interval buffer inside service. */ + +/**@brief Heart Rate Service event type. */ +typedef enum +{ + BLE_HRS_EVT_NOTIFICATION_ENABLED, /**< Heart Rate value notification enabled event. */ + BLE_HRS_EVT_NOTIFICATION_DISABLED /**< Heart Rate value notification disabled event. */ +} ble_hrs_evt_type_t; + +/**@brief Heart Rate Service event. */ +typedef struct +{ + ble_hrs_evt_type_t evt_type; /**< Type of event. */ +} ble_hrs_evt_t; + +// Forward declaration of the ble_hrs_t type. +typedef struct ble_hrs_s ble_hrs_t; + +/**@brief Heart Rate Service event handler type. */ +typedef void (*ble_hrs_evt_handler_t) (ble_hrs_t * p_hrs, ble_hrs_evt_t * p_evt); + +/**@brief Heart Rate Service init structure. This contains all options and data needed for + * initialization of the service. */ +typedef struct +{ + ble_hrs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Heart Rate Service. */ + bool is_sensor_contact_supported; /**< Determines if sensor contact detection is to be supported. */ + uint8_t * p_body_sensor_location; /**< If not NULL, initial value of the Body Sensor Location characteristic. */ + ble_srv_cccd_security_mode_t hrs_hrm_attr_md; /**< Initial security level for heart rate service measurement attribute */ + ble_srv_security_mode_t hrs_bsl_attr_md; /**< Initial security level for body sensor location attribute */ +} ble_hrs_init_t; + +/**@brief Heart Rate Service structure. This contains various status information for the service. */ +typedef struct ble_hrs_s +{ + ble_hrs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Heart Rate Service. */ + bool is_expended_energy_supported; /**< TRUE if Expended Energy measurement is supported. */ + bool is_sensor_contact_supported; /**< TRUE if sensor contact detection is supported. */ + uint16_t service_handle; /**< Handle of Heart Rate Service (as provided by the BLE stack). */ + ble_gatts_char_handles_t hrm_handles; /**< Handles related to the Heart Rate Measurement characteristic. */ + ble_gatts_char_handles_t bsl_handles; /**< Handles related to the Body Sensor Location characteristic. */ + ble_gatts_char_handles_t hrcp_handles; /**< Handles related to the Heart Rate Control Point characteristic. */ + uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ + bool is_sensor_contact_detected; /**< TRUE if sensor contact has been detected. */ + uint16_t rr_interval[BLE_HRS_MAX_BUFFERED_RR_INTERVALS]; /**< Set of RR Interval measurements since the last Heart Rate Measurement transmission. */ + uint16_t rr_interval_count; /**< Number of RR Interval measurements since the last Heart Rate Measurement transmission. */ +} ble_hrs_t; + +/**@brief Function for initializing the Heart Rate Service. + * + * @param[out] p_hrs Heart Rate Service structure. This structure will have to be supplied by + * the application. It will be initialized by this function, and will later + * be used to identify this particular service instance. + * @param[in] p_hrs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_hrs_init(ble_hrs_t * p_hrs, const ble_hrs_init_t * p_hrs_init); + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack of interest to the Heart Rate Service. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +void ble_hrs_on_ble_evt(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt); + +/**@brief Function for sending heart rate measurement if notification has been enabled. + * + * @details The application calls this function after having performed a heart rate measurement. + * If notification has been enabled, the heart rate measurement data is encoded and sent to + * the client. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] heart_rate New heart rate measurement. + * @param[in] include_expended_energy Determines if expended energy will be included in the + * heart rate measurement data. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate); + +/**@brief Function for adding a RR Interval measurement to the RR Interval buffer. + * + * @details All buffered RR Interval measurements will be included in the next heart rate + * measurement message, up to the maximum number of measurements that will fit into the + * message. If the buffer is full, the oldest measurement in the buffer will be deleted. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] rr_interval New RR Interval measurement (will be buffered until the next + * transmission of Heart Rate Measurement). + */ +void ble_hrs_rr_interval_add(ble_hrs_t * p_hrs, uint16_t rr_interval); + +/**@brief Function for checking if RR Interval buffer is full. + * + * @param[in] p_hrs Heart Rate Service structure. + * + * @return true if RR Interval buffer is full, false otherwise. + */ +bool ble_hrs_rr_interval_buffer_is_full(ble_hrs_t * p_hrs); + +/**@brief Function for setting the state of the Sensor Contact Supported bit. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] is_sensor_contact_supported New state of the Sensor Contact Supported bit. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_hrs_sensor_contact_supported_set(ble_hrs_t * p_hrs, bool is_sensor_contact_supported); + +/**@brief Function for setting the state of the Sensor Contact Detected bit. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] is_sensor_contact_detected TRUE if sensor contact is detected, FALSE otherwise. + */ +void ble_hrs_sensor_contact_detected_update(ble_hrs_t * p_hrs, bool is_sensor_contact_detected); + +/**@brief Function for setting the Body Sensor Location. + * + * @details Sets a new value of the Body Sensor Location characteristic. The new value will be sent + * to the client the next time the client reads the Body Sensor Location characteristic. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] body_sensor_location New Body Sensor Location. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_hrs_body_sensor_location_set(ble_hrs_t * p_hrs, uint8_t body_sensor_location); + +#endif // BLE_HRS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_hts.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,161 @@ +/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. + * + * The information contained herein is property of Nordic Semiconductor ASA. + * Terms and conditions of usage are described in detail in NORDIC + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. + * + * Licensees are granted free, non-transferable use of the information. NO + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from + * the file. + */ + +/** @file + * + * @defgroup ble_sdk_srv_hts Health Thermometer Service + * @{ + * @ingroup ble_sdk_srv + * @brief Health Thermometer Service module. + * + * @details This module implements the Health Thermometer Service. + * + * If an event handler is supplied by the application, the Health Thermometer + * Service will generate Health Thermometer Service events to the application. + * + * @note The application must propagate BLE stack events to the Health Thermometer Service + * module by calling ble_hts_on_ble_evt() from the from the @ref ble_stack_handler function. + * + * @note Attention! + * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile + * qualification listings, this section of source code must not be modified. + */ + +#ifndef BLE_HTS_H__ +#define BLE_HTS_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble.h" +#include "ble_srv_common.h" +#include "ble_date_time.h" + +// Temperature Type measurement locations +#define BLE_HTS_TEMP_TYPE_ARMPIT 1 +#define BLE_HTS_TEMP_TYPE_BODY 2 +#define BLE_HTS_TEMP_TYPE_EAR 3 +#define BLE_HTS_TEMP_TYPE_FINGER 4 +#define BLE_HTS_TEMP_TYPE_GI_TRACT 5 +#define BLE_HTS_TEMP_TYPE_MOUTH 6 +#define BLE_HTS_TEMP_TYPE_RECTUM 7 +#define BLE_HTS_TEMP_TYPE_TOE 8 +#define BLE_HTS_TEMP_TYPE_EAR_DRUM 9 + +/**@brief Health Thermometer Service event type. */ +typedef enum +{ + BLE_HTS_EVT_INDICATION_ENABLED, /**< Health Thermometer value indication enabled event. */ + BLE_HTS_EVT_INDICATION_DISABLED, /**< Health Thermometer value indication disabled event. */ + BLE_HTS_EVT_INDICATION_CONFIRMED /**< Confirmation of a temperature measurement indication has been received. */ +} ble_hts_evt_type_t; + +/**@brief Health Thermometer Service event. */ +typedef struct +{ + ble_hts_evt_type_t evt_type; /**< Type of event. */ +} ble_hts_evt_t; + +// Forward declaration of the ble_hts_t type. +typedef struct ble_hts_s ble_hts_t; + +/**@brief Health Thermometer Service event handler type. */ +typedef void (*ble_hts_evt_handler_t) (ble_hts_t * p_hts, ble_hts_evt_t * p_evt); + +/**@brief FLOAT format (IEEE-11073 32-bit FLOAT, defined as a 32-bit value with a 24-bit mantissa + * and an 8-bit exponent. */ +typedef struct +{ + int8_t exponent; /**< Base 10 exponent */ + int32_t mantissa; /**< Mantissa, should be using only 24 bits */ +} ieee_float32_t; + +/**@brief Health Thermometer Service init structure. This contains all options and data + * needed for initialization of the service. */ +typedef struct +{ + ble_hts_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Health Thermometer Service. */ + ble_srv_cccd_security_mode_t hts_meas_attr_md; /**< Initial security level for health thermometer measurement attribute */ + ble_srv_security_mode_t hts_temp_type_attr_md; /**< Initial security level for health thermometer tempearture type attribute */ + uint8_t temp_type_as_characteristic; /**< Set non-zero if temp type given as characteristic */ + uint8_t temp_type; /**< Temperature type if temperature characteristic is used */ +} ble_hts_init_t; + +/**@brief Health Thermometer Service structure. This contains various status information for + * the service. */ +typedef struct ble_hts_s +{ + ble_hts_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Health Thermometer Service. */ + uint16_t service_handle; /**< Handle of Health Thermometer Service (as provided by the BLE stack). */ + ble_gatts_char_handles_t meas_handles; /**< Handles related to the Health Thermometer Measurement characteristic. */ + ble_gatts_char_handles_t temp_type_handles; /**< Handles related to the Health Thermometer Temperature Type characteristic. */ + uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */ + uint8_t temp_type; /**< Temperature type indicates where the measurement was taken. */ +} ble_hts_t; + +/**@brief Health Thermometer Service measurement structure. This contains a Health Thermometer + * measurement. */ +typedef struct ble_hts_meas_s +{ + bool temp_in_fahr_units; /**< True if Temperature is in Fahrenheit units, Celcius otherwise. */ + bool time_stamp_present; /**< True if Time Stamp is present. */ + bool temp_type_present; /**< True if Temperature Type is present. */ + ieee_float32_t temp_in_celcius; /**< Temperature Measurement Value (Celcius). */ + ieee_float32_t temp_in_fahr; /**< Temperature Measurement Value (Fahrenheit). */ + ble_date_time_t time_stamp; /**< Time Stamp. */ + uint8_t temp_type; /**< Temperature Type. */ +} ble_hts_meas_t; + +/**@brief Function for initializing the Health Thermometer Service. + * + * @param[out] p_hts Health Thermometer Service structure. This structure will have to + * be supplied by the application. It will be initialized by this function, + * and will later be used to identify this particular service instance. + * @param[in] p_hts_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on successful initialization of service, otherwise an error code. + */ +uint32_t ble_hts_init(ble_hts_t * p_hts, const ble_hts_init_t * p_hts_init); + +/**@brief Function for handling the Application's BLE Stack events. + * + * @details Handles all events from the BLE stack of interest to the Health Thermometer Service. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +void ble_hts_on_ble_evt(ble_hts_t * p_hts, ble_evt_t * p_ble_evt); + +/**@brief Function for sending health thermometer measurement if indication has been enabled. + * + * @details The application calls this function after having performed a Health Thermometer + * measurement. If indication has been enabled, the measurement data is encoded and + * sent to the client. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_hts_meas Pointer to new health thermometer measurement. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_hts_measurement_send(ble_hts_t * p_hts, ble_hts_meas_t * p_hts_meas); + +/**@brief Function for checking if indication of Temperature Measurement is currently enabled. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[out] p_indication_enabled TRUE if indication is enabled, FALSE otherwise. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +uint32_t ble_hts_is_indication_enabled(ble_hts_t * p_hts, bool * p_indication_enabled); + +#endif // BLE_HTS_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/inc/ble_srv_common.h Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,257 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @file + * + * @defgroup ble_sdk_srv_common 'Common service definitions' + * @{ + * @ingroup ble_sdk_srv + * @brief Constants, type definitions and functions that are common to all services. + */ + +#ifndef BLE_SRV_COMMON_H__ +#define BLE_SRV_COMMON_H__ + +#include <stdint.h> +#include <stdbool.h> +#include "ble_types.h" +#include "app_util.h" +#include "ble_gap.h" +#include "ble_gatt.h" + +/** @defgroup UUID_SERVICES Service UUID definitions + * @{ */ +#define BLE_UUID_ALERT_NOTIFICATION_SERVICE 0x1811 /**< Alert Notification service UUID. */ +#define BLE_UUID_BATTERY_SERVICE 0x180F /**< Battery service UUID. */ +#define BLE_UUID_BLOOD_PRESSURE_SERVICE 0x1810 /**< Blood Pressure service UUID. */ +#define BLE_UUID_CURRENT_TIME_SERVICE 0x1805 /**< Current Time service UUID. */ +#define BLE_UUID_CYCLING_SPEED_AND_CADENCE 0x1816 /**< Cycling Speed and Cadence service UUID. */ +#define BLE_UUID_DEVICE_INFORMATION_SERVICE 0x180A /**< Device Information service UUID. */ +#define BLE_UUID_GLUCOSE_SERVICE 0x1808 /**< Glucose service UUID. */ +#define BLE_UUID_HEALTH_THERMOMETER_SERVICE 0x1809 /**< Health Thermometer service UUID. */ +#define BLE_UUID_HEART_RATE_SERVICE 0x180D /**< Heart Rate service UUID. */ +#define BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE 0x1812 /**< Human Interface Device service UUID. */ +#define BLE_UUID_IMMEDIATE_ALERT_SERVICE 0x1802 /**< Immediate Alert service UUID. */ +#define BLE_UUID_LINK_LOSS_SERVICE 0x1803 /**< Link Loss service UUID. */ +#define BLE_UUID_NEXT_DST_CHANGE_SERVICE 0x1807 /**< Next Dst Change service UUID. */ +#define BLE_UUID_PHONE_ALERT_STATUS_SERVICE 0x180E /**< Phone Alert Status service UUID. */ +#define BLE_UUID_REFERENCE_TIME_UPDATE_SERVICE 0x1806 /**< Reference Time Update service UUID. */ +#define BLE_UUID_RUNNING_SPEED_AND_CADENCE 0x1814 /**< Running Speed and Cadence service UUID. */ +#define BLE_UUID_SCAN_PARAMETERS_SERVICE 0x1813 /**< Scan Parameters service UUID. */ +#define BLE_UUID_TX_POWER_SERVICE 0x1804 /**< TX Power service UUID. */ +/** @} */ + +/** @defgroup UUID_CHARACTERISTICS Characteristic UUID definitions + * @{ */ +#define BLE_UUID_BATTERY_LEVEL_STATE_CHAR 0x2A1B /**< Battery Level State characteristic UUID. */ +#define BLE_UUID_BATTERY_POWER_STATE_CHAR 0x2A1A /**< Battery Power State characteristic UUID. */ +#define BLE_UUID_REMOVABLE_CHAR 0x2A3A /**< Removable characteristic UUID. */ +#define BLE_UUID_SERVICE_REQUIRED_CHAR 0x2A3B /**< Service Required characteristic UUID. */ +#define BLE_UUID_ALERT_CATEGORY_ID_CHAR 0x2A43 /**< Alert Category Id characteristic UUID. */ +#define BLE_UUID_ALERT_CATEGORY_ID_BIT_MASK_CHAR 0x2A42 /**< Alert Category Id Bit Mask characteristic UUID. */ +#define BLE_UUID_ALERT_LEVEL_CHAR 0x2A06 /**< Alert Level characteristic UUID. */ +#define BLE_UUID_ALERT_NOTIFICATION_CONTROL_POINT_CHAR 0x2A44 /**< Alert Notification Control Point characteristic UUID. */ +#define BLE_UUID_ALERT_STATUS_CHAR 0x2A3F /**< Alert Status characteristic UUID. */ +#define BLE_UUID_BATTERY_LEVEL_CHAR 0x2A19 /**< Battery Level characteristic UUID. */ +#define BLE_UUID_BLOOD_PRESSURE_FEATURE_CHAR 0x2A49 /**< Blood Pressure Feature characteristic UUID. */ +#define BLE_UUID_BLOOD_PRESSURE_MEASUREMENT_CHAR 0x2A35 /**< Blood Pressure Measurement characteristic UUID. */ +#define BLE_UUID_BODY_SENSOR_LOCATION_CHAR 0x2A38 /**< Body Sensor Location characteristic UUID. */ +#define BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR 0x2A22 /**< Boot Keyboard Input Report characteristic UUID. */ +#define BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR 0x2A32 /**< Boot Keyboard Output Report characteristic UUID. */ +#define BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR 0x2A33 /**< Boot Mouse Input Report characteristic UUID. */ +#define BLE_UUID_CURRENT_TIME_CHAR 0x2A2B /**< Current Time characteristic UUID. */ +#define BLE_UUID_DATE_TIME_CHAR 0x2A08 /**< Date Time characteristic UUID. */ +#define BLE_UUID_DAY_DATE_TIME_CHAR 0x2A0A /**< Day Date Time characteristic UUID. */ +#define BLE_UUID_DAY_OF_WEEK_CHAR 0x2A09 /**< Day Of Week characteristic UUID. */ +#define BLE_UUID_DST_OFFSET_CHAR 0x2A0D /**< Dst Offset characteristic UUID. */ +#define BLE_UUID_EXACT_TIME_256_CHAR 0x2A0C /**< Exact Time 256 characteristic UUID. */ +#define BLE_UUID_FIRMWARE_REVISION_STRING_CHAR 0x2A26 /**< Firmware Revision String characteristic UUID. */ +#define BLE_UUID_GLUCOSE_FEATURE_CHAR 0x2A51 /**< Glucose Feature characteristic UUID. */ +#define BLE_UUID_GLUCOSE_MEASUREMENT_CHAR 0x2A18 /**< Glucose Measurement characteristic UUID. */ +#define BLE_UUID_GLUCOSE_MEASUREMENT_CONTEXT_CHAR 0x2A34 /**< Glucose Measurement Context characteristic UUID. */ +#define BLE_UUID_HARDWARE_REVISION_STRING_CHAR 0x2A27 /**< Hardware Revision String characteristic UUID. */ +#define BLE_UUID_HEART_RATE_CONTROL_POINT_CHAR 0x2A39 /**< Heart Rate Control Point characteristic UUID. */ +#define BLE_UUID_HEART_RATE_MEASUREMENT_CHAR 0x2A37 /**< Heart Rate Measurement characteristic UUID. */ +#define BLE_UUID_HID_CONTROL_POINT_CHAR 0x2A4C /**< Hid Control Point characteristic UUID. */ +#define BLE_UUID_HID_INFORMATION_CHAR 0x2A4A /**< Hid Information characteristic UUID. */ +#define BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR 0x2A2A /**< IEEE Regulatory Certification Data List characteristic UUID. */ +#define BLE_UUID_INTERMEDIATE_CUFF_PRESSURE_CHAR 0x2A36 /**< Intermediate Cuff Pressure characteristic UUID. */ +#define BLE_UUID_INTERMEDIATE_TEMPERATURE_CHAR 0x2A1E /**< Intermediate Temperature characteristic UUID. */ +#define BLE_UUID_LOCAL_TIME_INFORMATION_CHAR 0x2A0F /**< Local Time Information characteristic UUID. */ +#define BLE_UUID_MANUFACTURER_NAME_STRING_CHAR 0x2A29 /**< Manufacturer Name String characteristic UUID. */ +#define BLE_UUID_MEASUREMENT_INTERVAL_CHAR 0x2A21 /**< Measurement Interval characteristic UUID. */ +#define BLE_UUID_MODEL_NUMBER_STRING_CHAR 0x2A24 /**< Model Number String characteristic UUID. */ +#define BLE_UUID_UNREAD_ALERT_CHAR 0x2A45 /**< Unread Alert characteristic UUID. */ +#define BLE_UUID_NEW_ALERT_CHAR 0x2A46 /**< New Alert characteristic UUID. */ +#define BLE_UUID_PNP_ID_CHAR 0x2A50 /**< PNP Id characteristic UUID. */ +#define BLE_UUID_PROTOCOL_MODE_CHAR 0x2A4E /**< Protocol Mode characteristic UUID. */ +#define BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR 0x2A52 /**< Record Access Control Point characteristic UUID. */ +#define BLE_UUID_REFERENCE_TIME_INFORMATION_CHAR 0x2A14 /**< Reference Time Information characteristic UUID. */ +#define BLE_UUID_REPORT_CHAR 0x2A4D /**< Report characteristic UUID. */ +#define BLE_UUID_REPORT_MAP_CHAR 0x2A4B /**< Report Map characteristic UUID. */ +#define BLE_UUID_RINGER_CONTROL_POINT_CHAR 0x2A40 /**< Ringer Control Point characteristic UUID. */ +#define BLE_UUID_RINGER_SETTING_CHAR 0x2A41 /**< Ringer Setting characteristic UUID. */ +#define BLE_UUID_SCAN_INTERVAL_WINDOW_CHAR 0x2A4F /**< Scan Interval Window characteristic UUID. */ +#define BLE_UUID_SCAN_REFRESH_CHAR 0x2A31 /**< Scan Refresh characteristic UUID. */ +#define BLE_UUID_SERIAL_NUMBER_STRING_CHAR 0x2A25 /**< Serial Number String characteristic UUID. */ +#define BLE_UUID_SOFTWARE_REVISION_STRING_CHAR 0x2A28 /**< Software Revision String characteristic UUID. */ +#define BLE_UUID_SUPPORTED_NEW_ALERT_CATEGORY_CHAR 0x2A47 /**< Supported New Alert Category characteristic UUID. */ +#define BLE_UUID_SUPPORTED_UNREAD_ALERT_CATEGORY_CHAR 0x2A48 /**< Supported Unread Alert Category characteristic UUID. */ +#define BLE_UUID_SYSTEM_ID_CHAR 0x2A23 /**< System Id characteristic UUID. */ +#define BLE_UUID_TEMPERATURE_MEASUREMENT_CHAR 0x2A1C /**< Temperature Measurement characteristic UUID. */ +#define BLE_UUID_TEMPERATURE_TYPE_CHAR 0x2A1D /**< Temperature Type characteristic UUID. */ +#define BLE_UUID_TIME_ACCURACY_CHAR 0x2A12 /**< Time Accuracy characteristic UUID. */ +#define BLE_UUID_TIME_SOURCE_CHAR 0x2A13 /**< Time Source characteristic UUID. */ +#define BLE_UUID_TIME_UPDATE_CONTROL_POINT_CHAR 0x2A16 /**< Time Update Control Point characteristic UUID. */ +#define BLE_UUID_TIME_UPDATE_STATE_CHAR 0x2A17 /**< Time Update State characteristic UUID. */ +#define BLE_UUID_TIME_WITH_DST_CHAR 0x2A11 /**< Time With Dst characteristic UUID. */ +#define BLE_UUID_TIME_ZONE_CHAR 0x2A0E /**< Time Zone characteristic UUID. */ +#define BLE_UUID_TX_POWER_LEVEL_CHAR 0x2A07 /**< TX Power Level characteristic UUID. */ +#define BLE_UUID_CSC_FEATURE_CHAR 0x2A5C /**< Cycling Speed and Cadence Feature characteristic UUID. */ +#define BLE_UUID_CSC_MEASUREMENT_CHAR 0x2A5B /**< Cycling Speed and Cadence Measurement characteristic UUID. */ +#define BLE_UUID_RSC_FEATURE_CHAR 0x2A54 /**< Running Speed and Cadence Feature characteristic UUID. */ +#define BLE_UUID_SC_CTRLPT_CHAR 0x2A55 /**< Speed and Cadence Control Point UUID. */ +#define BLE_UUID_RSC_MEASUREMENT_CHAR 0x2A53 /**< Running Speed and Cadence Measurement characteristic UUID. */ +#define BLE_UUID_SENSOR_LOCATION_CHAR 0x2A5D /**< Sensor Location characteristic UUID. */ +/** @} */ + +/** @defgroup UUID_CHARACTERISTICS descriptors UUID definitions + * @{ */ +#define BLE_UUID_EXTERNAL_REPORT_REF_DESCR 0x2907 /**< External Report Reference descriptor UUID. */ +#define BLE_UUID_REPORT_REF_DESCR 0x2908 /**< Report Reference descriptor UUID. */ +/** @} */ + +/** @defgroup ALERT_LEVEL_VALUES Definitions for the Alert Level characteristic values + * @{ */ +#define BLE_CHAR_ALERT_LEVEL_NO_ALERT 0x00 /**< No Alert. */ +#define BLE_CHAR_ALERT_LEVEL_MILD_ALERT 0x01 /**< Mild Alert. */ +#define BLE_CHAR_ALERT_LEVEL_HIGH_ALERT 0x02 /**< High Alert. */ +/** @} */ + +#define BLE_SRV_ENCODED_REPORT_REF_LEN 2 /**< The length of an encoded Report Reference Descriptor. */ +#define BLE_CCCD_VALUE_LEN 2 /**< The length of a CCCD value. */ + +/**@brief Type definition for error handler function which will be called in case of an error in + * a service or a service library module. */ +typedef void (*ble_srv_error_handler_t) (uint32_t nrf_error); + +/**@brief Value of a Report Reference descriptor. + * + * @details This is mapping information which maps the parent characteristic to the Report ID(s) and + * Report Type(s) defined within a Report Map characteristic. + */ +typedef struct +{ + uint8_t report_id; /**< Non-zero value if these is more than one instance of the same Report Type */ + uint8_t report_type; /**< Type of Report characteristic (see @ref BLE_SRV_HIDS_REPORT_TYPE) */ +} ble_srv_report_ref_t; + +/**@brief UTF-8 string data type. + * + * @note The type can only hold a pointer to the string data (i.e. not the actual data). + */ +typedef struct +{ + uint16_t length; /**< String length. */ + uint8_t * p_str; /**< String data. */ +} ble_srv_utf8_str_t; + +/**@brief Security settings structure. + * @details This structure contains the security options needed during initialization of the + * service. + */ +typedef struct +{ + ble_gap_conn_sec_mode_t read_perm; /**< Read permissions. */ + ble_gap_conn_sec_mode_t write_perm; /**< Write permissions. */ +} ble_srv_security_mode_t; + +/**@brief Security settings structure. + * @details This structure contains the security options needed during initialization of the + * service. It can be used when the charecteristics contains cccd. + */ +typedef struct +{ + ble_gap_conn_sec_mode_t cccd_write_perm; + ble_gap_conn_sec_mode_t read_perm; /**< Read permissions. */ + ble_gap_conn_sec_mode_t write_perm; /**< Write permissions. */ +} ble_srv_cccd_security_mode_t; + +/**@brief Function for decoding a CCCD value, and then testing if notification is + * enabled. + * + * @param[in] p_encoded_data Buffer where the encoded CCCD is stored. + * + * @return TRUE if notification is enabled, FALSE otherwise. + */ +static __INLINE bool ble_srv_is_notification_enabled(uint8_t * p_encoded_data) +{ + uint16_t cccd_value = uint16_decode(p_encoded_data); + return ((cccd_value & BLE_GATT_HVX_NOTIFICATION) != 0); +} + +/**@brief Function for decoding a CCCD value, and then testing if indication is + * enabled. + * + * @param[in] p_encoded_data Buffer where the encoded CCCD is stored. + * + * @return TRUE if indication is enabled, FALSE otherwise. + */ +static __INLINE bool ble_srv_is_indication_enabled(uint8_t * p_encoded_data) +{ + uint16_t cccd_value = uint16_decode(p_encoded_data); + return ((cccd_value & BLE_GATT_HVX_INDICATION) != 0); +} + +/**@brief Function for encoding a Report Reference Descriptor. + * + * @param[in] p_encoded_buffer The buffer of the encoded data. + * @param[in] p_report_ref Report Reference value to be encoded. + * + * @return Length of the encoded data. + */ +uint8_t ble_srv_report_ref_encode(uint8_t * p_encoded_buffer, + const ble_srv_report_ref_t * p_report_ref); + +/**@brief Function for making UTF-8 structure refer to an ASCII string. + * + * @param[out] p_utf8 UTF-8 structure to be set. + * @param[in] p_ascii ASCII string to be referred to. + */ +void ble_srv_ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii); + +#endif // BLE_SRV_COMMON_H__ + +/** @} */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/src/ble_bas.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,335 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +#include "ble_bas.h" +#include <string.h> +#include "nordic_common.h" +#include "ble_srv_common.h" +#include "blocking.h" +#include "app_error.h" + +#define INVALID_BATTERY_LEVEL 255 + + +/**@brief Function for handling the Connect event. + * + * @param[in] p_bas Battery Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_bas_t * p_bas, ble_evt_t * p_ble_evt) +{ + p_bas->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; +} + + +/**@brief Function for handling the Disconnect event. + * + * @param[in] p_bas Battery Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_disconnect(ble_bas_t * p_bas, ble_evt_t * p_ble_evt) +{ + UNUSED_PARAMETER(p_ble_evt); + p_bas->conn_handle = BLE_CONN_HANDLE_INVALID; +} + + +/**@brief Function for handling the Write event. + * + * @param[in] p_bas Battery Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_write(ble_bas_t * p_bas, ble_evt_t * p_ble_evt) +{ + if (p_bas->is_notification_supported) + { + ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; + + if ( + (p_evt_write->handle == p_bas->battery_level_handles.cccd_handle) + && + (p_evt_write->len == 2) + ) + { + // CCCD written, call application event handler + if (p_bas->evt_handler != NULL) + { + ble_bas_evt_t evt; + + if (ble_srv_is_notification_enabled(p_evt_write->data)) + { + evt.evt_type = BLE_BAS_EVT_NOTIFICATION_ENABLED; + } + else + { + evt.evt_type = BLE_BAS_EVT_NOTIFICATION_DISABLED; + } + + p_bas->evt_handler(p_bas, &evt); + } + } + } +} + + +void ble_bas_on_ble_evt(ble_bas_t * p_bas, ble_evt_t * p_ble_evt) +{ + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_bas, p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_bas, p_ble_evt); + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_bas, p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + + +/**@brief Function for adding the Battery Level characteristic. + * + * @param[in] p_bas Battery Service structure. + * @param[in] p_bas_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t battery_level_char_add(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init) +{ + uint32_t err_code; + ble_gatts_char_md_t char_md; + ble_gatts_attr_md_t cccd_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + uint8_t initial_battery_level; + uint8_t encoded_report_ref[BLE_SRV_ENCODED_REPORT_REF_LEN]; + uint8_t init_len; + + // Add Battery Level characteristic + if (p_bas->is_notification_supported) + { + memset(&cccd_md, 0, sizeof(cccd_md)); + + // According to BAS_SPEC_V10, the read operation on cccd should be possible without + // authentication. + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + cccd_md.write_perm = p_bas_init->battery_level_char_attr_md.cccd_write_perm; + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + } + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.read = 1; + char_md.char_props.notify = (p_bas->is_notification_supported) ? 1 : 0; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = (p_bas->is_notification_supported) ? &cccd_md : NULL; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BATTERY_LEVEL_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_bas_init->battery_level_char_attr_md.read_perm; + attr_md.write_perm = p_bas_init->battery_level_char_attr_md.write_perm; + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + initial_battery_level = p_bas_init->initial_batt_level; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = sizeof(uint8_t); + attr_char_value.init_offs = 0; + attr_char_value.max_len = sizeof(uint8_t); + attr_char_value.p_value = &initial_battery_level; + + err_code = sd_ble_gatts_characteristic_add(p_bas->service_handle, &char_md, + &attr_char_value, + &p_bas->battery_level_handles); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (p_bas_init->p_report_ref != NULL) + { + // Add Report Reference descriptor + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_REPORT_REF_DESCR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_bas_init->battery_level_report_read_perm; + BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + init_len = ble_srv_report_ref_encode(encoded_report_ref, p_bas_init->p_report_ref); + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = init_len; + attr_char_value.init_offs = 0; + attr_char_value.max_len = attr_char_value.init_len; + attr_char_value.p_value = encoded_report_ref; + + err_code = sd_ble_gatts_descriptor_add(p_bas->battery_level_handles.value_handle, + &attr_char_value, + &p_bas->report_ref_handle); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + else + { + p_bas->report_ref_handle = BLE_GATT_HANDLE_INVALID; + } + + return NRF_SUCCESS; +} + + +uint32_t ble_bas_init(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init) +{ + uint32_t err_code; + ble_uuid_t ble_uuid; + + // Initialize service structure + p_bas->evt_handler = p_bas_init->evt_handler; + p_bas->conn_handle = BLE_CONN_HANDLE_INVALID; + p_bas->is_notification_supported = p_bas_init->support_notification; + p_bas->battery_level_last = INVALID_BATTERY_LEVEL; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BATTERY_SERVICE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, + &ble_uuid, + &p_bas->service_handle); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add battery level characteristic + return battery_level_char_add(p_bas, p_bas_init); +} + + +uint32_t ble_bas_battery_level_update(ble_bas_t * p_bas, uint8_t battery_level) +{ + uint32_t err_code = NRF_SUCCESS; + + if (battery_level != p_bas->battery_level_last) + { + uint16_t len = sizeof(uint8_t); + + // Save new battery value + p_bas->battery_level_last = battery_level; + + // Update database + err_code = sd_ble_gatts_value_set(p_bas->battery_level_handles.value_handle, + 0, + &len, + &battery_level); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Send value if connected and notifying + if ((p_bas->conn_handle != BLE_CONN_HANDLE_INVALID) && p_bas->is_notification_supported) + { + ble_gatts_hvx_params_t hvx_params; + + memset(&hvx_params, 0, sizeof(hvx_params)); + len = sizeof(uint8_t); + + hvx_params.handle = p_bas->battery_level_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_NOTIFICATION; + hvx_params.offset = 0; + hvx_params.p_len = &len; + hvx_params.p_data = &battery_level; + + err_code = sd_ble_gatts_hvx(p_bas->conn_handle, &hvx_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + } + else + { + err_code = NRF_ERROR_INVALID_STATE; + } + } + + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/src/ble_dis.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,318 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +#include "ble_dis.h" +#include <stdlib.h> +#include <string.h> +#include "app_error.h" +#include "ble_gatts.h" +#include "ble_srv_common.h" +#include "blocking.h" + +#define BLE_DIS_SYS_ID_LEN 8 /**< Length of System ID Characteristic Value. */ +#define BLE_DIS_PNP_ID_LEN 7 /**< Length of Pnp ID Characteristic Value. */ + +static uint16_t service_handle; +static ble_gatts_char_handles_t manufact_name_handles; +static ble_gatts_char_handles_t model_num_handles; +static ble_gatts_char_handles_t serial_num_handles; +static ble_gatts_char_handles_t hw_rev_handles; +static ble_gatts_char_handles_t fw_rev_handles; +static ble_gatts_char_handles_t sw_rev_handles; +static ble_gatts_char_handles_t sys_id_handles; +static ble_gatts_char_handles_t reg_cert_data_list_handles; +static ble_gatts_char_handles_t pnp_id_handles; + + +/**@brief Function for encoding a System ID. + * + * @param[out] p_encoded_buffer Buffer where the encoded data will be written. + * @param[in] p_sys_id System ID to be encoded. + */ +static void sys_id_encode(uint8_t * p_encoded_buffer, const ble_dis_sys_id_t * p_sys_id) +{ + APP_ERROR_CHECK_BOOL(p_sys_id != NULL); + APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL); + + p_encoded_buffer[0] = (p_sys_id->manufacturer_id & 0x00000000FF); + p_encoded_buffer[1] = (p_sys_id->manufacturer_id & 0x000000FF00) >> 8; + p_encoded_buffer[2] = (p_sys_id->manufacturer_id & 0x0000FF0000) >> 16; + p_encoded_buffer[3] = (p_sys_id->manufacturer_id & 0x00FF000000) >> 24; + p_encoded_buffer[4] = (p_sys_id->manufacturer_id & 0xFF00000000) >> 32; + + p_encoded_buffer[5] = (p_sys_id->organizationally_unique_id & 0x0000FF); + p_encoded_buffer[6] = (p_sys_id->organizationally_unique_id & 0x00FF00) >> 8; + p_encoded_buffer[7] = (p_sys_id->organizationally_unique_id & 0xFF0000) >> 16; +} + + +/**@brief Function for encoding a PnP ID. + * + * @param[out] p_encoded_buffer Buffer where the encoded data will be written. + * @param[in] p_pnp_id PnP ID to be encoded. + */ +static void pnp_id_encode(uint8_t * p_encoded_buffer, const ble_dis_pnp_id_t * p_pnp_id) +{ + uint8_t len = 0; + + APP_ERROR_CHECK_BOOL(p_pnp_id != NULL); + APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL); + + p_encoded_buffer[len++] = p_pnp_id->vendor_id_source; + + len += uint16_encode(p_pnp_id->vendor_id, &p_encoded_buffer[len]); + len += uint16_encode(p_pnp_id->product_id, &p_encoded_buffer[len]); + len += uint16_encode(p_pnp_id->product_version, &p_encoded_buffer[len]); + + APP_ERROR_CHECK_BOOL(len == BLE_DIS_PNP_ID_LEN); +} + + +/**@brief Function for adding the Characteristic. + * + * @param[in] uuid UUID of characteristic to be added. + * @param[in] p_char_value Initial value of characteristic to be added. + * @param[in] char_len Length of initial value. This will also be the maximum value. + * @param[in] dis_attr_md Security settings of characteristic to be added. + * @param[out] p_handles Handles of new characteristic. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t char_add(uint16_t uuid, + uint8_t * p_char_value, + uint16_t char_len, + const ble_srv_security_mode_t * dis_attr_md, + ble_gatts_char_handles_t * p_handles) +{ + ble_uuid_t ble_uuid; + ble_gatts_char_md_t char_md; + ble_gatts_attr_t attr_char_value; + ble_gatts_attr_md_t attr_md; + + APP_ERROR_CHECK_BOOL(p_char_value != NULL); + APP_ERROR_CHECK_BOOL(char_len > 0); + + // The ble_gatts_char_md_t structure uses bit fields. So we reset the memory to zero. + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.read = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = NULL; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, uuid); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = dis_attr_md->read_perm; + attr_md.write_perm = dis_attr_md->write_perm; + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = char_len; + attr_char_value.init_offs = 0; + attr_char_value.max_len = char_len; + attr_char_value.p_value = p_char_value; + + return sd_ble_gatts_characteristic_add(service_handle, &char_md, &attr_char_value, p_handles); +} + + +uint32_t ble_dis_init(const ble_dis_init_t * p_dis_init) +{ + uint32_t err_code; + ble_uuid_t ble_uuid; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_DEVICE_INFORMATION_SERVICE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &service_handle); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add characteristics + if (p_dis_init->manufact_name_str.length > 0) + { + err_code = char_add(BLE_UUID_MANUFACTURER_NAME_STRING_CHAR, + p_dis_init->manufact_name_str.p_str, + p_dis_init->manufact_name_str.length, + &p_dis_init->dis_attr_md, + &manufact_name_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->model_num_str.length > 0) + { + err_code = char_add(BLE_UUID_MODEL_NUMBER_STRING_CHAR, + p_dis_init->model_num_str.p_str, + p_dis_init->model_num_str.length, + &p_dis_init->dis_attr_md, + &model_num_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->serial_num_str.length > 0) + { + err_code = char_add(BLE_UUID_SERIAL_NUMBER_STRING_CHAR, + p_dis_init->serial_num_str.p_str, + p_dis_init->serial_num_str.length, + &p_dis_init->dis_attr_md, + &serial_num_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->hw_rev_str.length > 0) + { + err_code = char_add(BLE_UUID_HARDWARE_REVISION_STRING_CHAR, + p_dis_init->hw_rev_str.p_str, + p_dis_init->hw_rev_str.length, + &p_dis_init->dis_attr_md, + &hw_rev_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->fw_rev_str.length > 0) + { + err_code = char_add(BLE_UUID_FIRMWARE_REVISION_STRING_CHAR, + p_dis_init->fw_rev_str.p_str, + p_dis_init->fw_rev_str.length, + &p_dis_init->dis_attr_md, + &fw_rev_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->sw_rev_str.length > 0) + { + err_code = char_add(BLE_UUID_SOFTWARE_REVISION_STRING_CHAR, + p_dis_init->sw_rev_str.p_str, + p_dis_init->sw_rev_str.length, + &p_dis_init->dis_attr_md, + &sw_rev_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->p_sys_id != NULL) + { + uint8_t encoded_sys_id[BLE_DIS_SYS_ID_LEN]; + + sys_id_encode(encoded_sys_id, p_dis_init->p_sys_id); + err_code = char_add(BLE_UUID_SYSTEM_ID_CHAR, + encoded_sys_id, + BLE_DIS_SYS_ID_LEN, + &p_dis_init->dis_attr_md, + &sys_id_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->p_reg_cert_data_list != NULL) + { + err_code = char_add(BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR, + p_dis_init->p_reg_cert_data_list->p_list, + p_dis_init->p_reg_cert_data_list->list_len, + &p_dis_init->dis_attr_md, + ®_cert_data_list_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + if (p_dis_init->p_pnp_id != NULL) + { + uint8_t encoded_pnp_id[BLE_DIS_PNP_ID_LEN]; + + pnp_id_encode(encoded_pnp_id, p_dis_init->p_pnp_id); + err_code = char_add(BLE_UUID_PNP_ID_CHAR, + encoded_pnp_id, + BLE_DIS_PNP_ID_LEN, + &p_dis_init->dis_attr_md, + &pnp_id_handles); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return NRF_SUCCESS; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/src/ble_hrs.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,471 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +#include "ble_hrs.h" +#include <string.h> +#include "ble_util.h" +#include "ble_l2cap.h" +#include "ble_srv_common.h" +#include "blocking.h" +#include "app_error.h" + +#define OPCODE_LENGTH 1 /**< Length of opcode inside Heart Rate Measurement packet. */ +#define HANDLE_LENGTH 2 /**< Length of handle inside Heart Rate Measurement packet. */ +#define MAX_HRM_LEN (BLE_L2CAP_MTU_DEF - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Heart Rate Measurement. */ + +#define INITIAL_VALUE_HRM 0 /**< Initial Heart Rate Measurement value. */ + +// Heart Rate Measurement flag bits +#define HRM_FLAG_MASK_HR_VALUE_16BIT (0x01 << 0) /**< Heart Rate Value Format bit. */ +#define HRM_FLAG_MASK_SENSOR_CONTACT_DETECTED (0x01 << 1) /**< Sensor Contact Detected bit. */ +#define HRM_FLAG_MASK_SENSOR_CONTACT_SUPPORTED (0x01 << 2) /**< Sensor Contact Supported bit. */ +#define HRM_FLAG_MASK_EXPENDED_ENERGY_INCLUDED (0x01 << 3) /**< Energy Expended Status bit. Feature Not Supported */ +#define HRM_FLAG_MASK_RR_INTERVAL_INCLUDED (0x01 << 4) /**< RR-Interval bit. */ + + +/**@brief Function for handling the Connect event. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt) +{ + p_hrs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; +} + + +/**@brief Function for handling the Disconnect event. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_disconnect(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt) +{ + UNUSED_PARAMETER(p_ble_evt); + p_hrs->conn_handle = BLE_CONN_HANDLE_INVALID; +} + + +/**@brief Function for handling write events to the Heart Rate Measurement characteristic. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_evt_write Write event received from the BLE stack. + */ +static void on_hrm_cccd_write(ble_hrs_t * p_hrs, ble_gatts_evt_write_t * p_evt_write) +{ + if (p_evt_write->len == 2) + { + // CCCD written, update notification state + if (p_hrs->evt_handler != NULL) + { + ble_hrs_evt_t evt; + + if (ble_srv_is_notification_enabled(p_evt_write->data)) + { + evt.evt_type = BLE_HRS_EVT_NOTIFICATION_ENABLED; + } + else + { + evt.evt_type = BLE_HRS_EVT_NOTIFICATION_DISABLED; + } + + p_hrs->evt_handler(p_hrs, &evt); + } + } +} + + +/**@brief Function for handling the Write event. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_write(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt) +{ + ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; + + if (p_evt_write->handle == p_hrs->hrm_handles.cccd_handle) + { + on_hrm_cccd_write(p_hrs, p_evt_write); + } +} + + +void ble_hrs_on_ble_evt(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt) +{ + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_hrs, p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_hrs, p_ble_evt); + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_hrs, p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + + +/**@brief Function for encoding a Heart Rate Measurement. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] heart_rate Measurement to be encoded. + * @param[out] p_encoded_buffer Buffer where the encoded data will be written. + * + * @return Size of encoded data. + */ +static uint8_t hrm_encode(ble_hrs_t * p_hrs, uint16_t heart_rate, uint8_t * p_encoded_buffer) +{ + uint8_t flags = 0; + uint8_t len = 1; + int i; + + // Set sensor contact related flags + if (p_hrs->is_sensor_contact_supported) + { + flags |= HRM_FLAG_MASK_SENSOR_CONTACT_SUPPORTED; + } + if (p_hrs->is_sensor_contact_detected) + { + flags |= HRM_FLAG_MASK_SENSOR_CONTACT_DETECTED; + } + + // Encode heart rate measurement + if (heart_rate > 0xff) + { + flags |= HRM_FLAG_MASK_HR_VALUE_16BIT; + len += uint16_encode(heart_rate, &p_encoded_buffer[len]); + } + else + { + p_encoded_buffer[len++] = (uint8_t)heart_rate; + } + + // Encode rr_interval values + if (p_hrs->rr_interval_count > 0) + { + flags |= HRM_FLAG_MASK_RR_INTERVAL_INCLUDED; + } + for (i = 0; i < p_hrs->rr_interval_count; i++) + { + if (len + sizeof(uint16_t) > MAX_HRM_LEN) + { + // Not all stored rr_interval values can fit into the encoded hrm, + // move the remaining values to the start of the buffer. + memmove(&p_hrs->rr_interval[0], + &p_hrs->rr_interval[i], + (p_hrs->rr_interval_count - i) * sizeof(uint16_t)); + break; + } + len += uint16_encode(p_hrs->rr_interval[i], &p_encoded_buffer[len]); + } + p_hrs->rr_interval_count -= i; + + // Add flags + p_encoded_buffer[0] = flags; + + return len; +} + + +/**@brief Function for adding the Heart Rate Measurement characteristic. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_hrs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t heart_rate_measurement_char_add(ble_hrs_t * p_hrs, + const ble_hrs_init_t * p_hrs_init) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_md_t cccd_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + uint8_t encoded_initial_hrm[MAX_HRM_LEN]; + + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + cccd_md.write_perm = p_hrs_init->hrs_hrm_attr_md.cccd_write_perm; + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.notify = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = &cccd_md; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_MEASUREMENT_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_hrs_init->hrs_hrm_attr_md.read_perm; + attr_md.write_perm = p_hrs_init->hrs_hrm_attr_md.write_perm; + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 1; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = hrm_encode(p_hrs, INITIAL_VALUE_HRM, encoded_initial_hrm); + attr_char_value.init_offs = 0; + attr_char_value.max_len = MAX_HRM_LEN; + attr_char_value.p_value = encoded_initial_hrm; + + return sd_ble_gatts_characteristic_add(p_hrs->service_handle, + &char_md, + &attr_char_value, + &p_hrs->hrm_handles); +} + + +/**@brief Function for adding the Body Sensor Location characteristic. + * + * @param[in] p_hrs Heart Rate Service structure. + * @param[in] p_hrs_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t body_sensor_location_char_add(ble_hrs_t * p_hrs, const ble_hrs_init_t * p_hrs_init) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.read = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = NULL; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_BODY_SENSOR_LOCATION_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.read_perm = p_hrs_init->hrs_bsl_attr_md.read_perm; + attr_md.write_perm = p_hrs_init->hrs_bsl_attr_md.write_perm; + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = sizeof(uint8_t); + attr_char_value.init_offs = 0; + attr_char_value.max_len = sizeof(uint8_t); + attr_char_value.p_value = p_hrs_init->p_body_sensor_location; + + return sd_ble_gatts_characteristic_add(p_hrs->service_handle, + &char_md, + &attr_char_value, + &p_hrs->bsl_handles); +} + + +uint32_t ble_hrs_init(ble_hrs_t * p_hrs, const ble_hrs_init_t * p_hrs_init) +{ + uint32_t err_code; + ble_uuid_t ble_uuid; + + // Initialize service structure + p_hrs->evt_handler = p_hrs_init->evt_handler; + p_hrs->is_sensor_contact_supported = p_hrs_init->is_sensor_contact_supported; + p_hrs->conn_handle = BLE_CONN_HANDLE_INVALID; + p_hrs->is_sensor_contact_detected = false; + p_hrs->rr_interval_count = 0; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_SERVICE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, + &ble_uuid, + &p_hrs->service_handle); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add heart rate measurement characteristic + err_code = heart_rate_measurement_char_add(p_hrs, p_hrs_init); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (p_hrs_init->p_body_sensor_location != NULL) + { + // Add body sensor location characteristic + err_code = body_sensor_location_char_add(p_hrs, p_hrs_init); + APP_ERROR_CHECK(err_code); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return NRF_SUCCESS; +} + + +uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate) +{ + uint32_t err_code; + + // Send value if connected and notifying + if (p_hrs->conn_handle != BLE_CONN_HANDLE_INVALID) + { + uint8_t encoded_hrm[MAX_HRM_LEN]; + uint16_t len; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + len = hrm_encode(p_hrs, heart_rate, encoded_hrm); + hvx_len = len; + + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_hrs->hrm_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_NOTIFICATION; + hvx_params.offset = 0; + hvx_params.p_len = &hvx_len; + hvx_params.p_data = encoded_hrm; + + err_code = sd_ble_gatts_hvx(p_hrs->conn_handle, &hvx_params); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if ((err_code == NRF_SUCCESS) && (hvx_len != len)) + { + err_code = NRF_ERROR_DATA_SIZE; + } + } + else + { + err_code = NRF_ERROR_INVALID_STATE; + } + + return err_code; +} + + +void ble_hrs_rr_interval_add(ble_hrs_t * p_hrs, uint16_t rr_interval) +{ + if (p_hrs->rr_interval_count == BLE_HRS_MAX_BUFFERED_RR_INTERVALS) + { + // The rr_interval buffer is full, delete the oldest value + memmove(&p_hrs->rr_interval[0], + &p_hrs->rr_interval[1], + (BLE_HRS_MAX_BUFFERED_RR_INTERVALS - 1) * sizeof(uint16_t)); + p_hrs->rr_interval_count--; + } + + // Add new value + p_hrs->rr_interval[p_hrs->rr_interval_count++] = rr_interval; +} + + +bool ble_hrs_rr_interval_buffer_is_full(ble_hrs_t * p_hrs) +{ + return (p_hrs->rr_interval_count == BLE_HRS_MAX_BUFFERED_RR_INTERVALS); +} + + +uint32_t ble_hrs_sensor_contact_supported_set(ble_hrs_t * p_hrs, bool is_sensor_contact_supported) +{ + // Check if we are connected to peer + if (p_hrs->conn_handle == BLE_CONN_HANDLE_INVALID) + { + p_hrs->is_sensor_contact_supported = is_sensor_contact_supported; + return NRF_SUCCESS; + } + else + { + return NRF_ERROR_INVALID_STATE; + } +} + + +void ble_hrs_sensor_contact_detected_update(ble_hrs_t * p_hrs, bool is_sensor_contact_detected) +{ + p_hrs->is_sensor_contact_detected = is_sensor_contact_detected; +} + + +uint32_t ble_hrs_body_sensor_location_set(ble_hrs_t * p_hrs, uint8_t body_sensor_location) +{ + uint16_t len = sizeof(uint8_t); + uint32_t err_code = sd_ble_gatts_value_set(p_hrs->bsl_handles.value_handle, + 0, + &len, + &body_sensor_location); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/src/ble_hts.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,445 @@ +/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. + * + * The information contained herein is property of Nordic Semiconductor ASA. + * Terms and conditions of usage are described in detail in NORDIC + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. + * + * Licensees are granted free, non-transferable use of the information. NO + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from + * the file. + * + */ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +#include "ble_hts.h" +#include <string.h> +#include "nordic_common.h" +#include "ble_l2cap.h" +#include "ble_srv_common.h" +#include "ble_util.h" +#include "blocking.h" +#include "app_error.h" + + +#define OPCODE_LENGTH 1 /**< Length of opcode inside Health Thermometer Measurement packet. */ +#define HANDLE_LENGTH 2 /**< Length of handle inside Health Thermometer Measurement packet. */ +#define MAX_HTM_LEN (BLE_L2CAP_MTU_DEF - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Health Thermometer Measurement. */ + +// Health Thermometer Measurement flag bits +#define HTS_MEAS_FLAG_TEMP_UNITS_BIT (0x01 << 0) /**< Temperature Units flag. */ +#define HTS_MEAS_FLAG_TIME_STAMP_BIT (0x01 << 1) /**< Time Stamp flag. */ +#define HTS_MEAS_FLAG_TEMP_TYPE_BIT (0x01 << 2) /**< Temperature Type flag. */ + + +/**@brief Function for handling the Connect event. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_connect(ble_hts_t * p_hts, ble_evt_t * p_ble_evt) +{ + p_hts->conn_handle = p_ble_evt->evt.gatts_evt.conn_handle; +} + + +/**@brief Function for handling the Disconnect event. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_disconnect(ble_hts_t * p_hts, ble_evt_t * p_ble_evt) +{ + UNUSED_PARAMETER(p_ble_evt); + p_hts->conn_handle = BLE_CONN_HANDLE_INVALID; +} + + +/**@brief Function for handling write events to the Blood Pressure Measurement characteristic. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_evt_write Write event received from the BLE stack. + */ +static void on_cccd_write(ble_hts_t * p_hts, ble_gatts_evt_write_t * p_evt_write) +{ + if (p_evt_write->len == 2) + { + // CCCD written, update indication state + if (p_hts->evt_handler != NULL) + { + ble_hts_evt_t evt; + + if (ble_srv_is_indication_enabled(p_evt_write->data)) + { + evt.evt_type = BLE_HTS_EVT_INDICATION_ENABLED; + } + else + { + evt.evt_type = BLE_HTS_EVT_INDICATION_DISABLED; + } + + p_hts->evt_handler(p_hts, &evt); + } + } +} + + +/**@brief Function for handling the Write event. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_write(ble_hts_t * p_hts, ble_evt_t * p_ble_evt) +{ + ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; + + if (p_evt_write->handle == p_hts->meas_handles.cccd_handle) + { + on_cccd_write(p_hts, p_evt_write); + } +} + + +/**@brief Function for handling the HVC event. + * + * @details Handles HVC events from the BLE stack. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_ble_evt Event received from the BLE stack. + */ +static void on_hvc(ble_hts_t * p_hts, ble_evt_t * p_ble_evt) +{ + ble_gatts_evt_hvc_t * p_hvc = &p_ble_evt->evt.gatts_evt.params.hvc; + + if (p_hvc->handle == p_hts->meas_handles.value_handle) + { + ble_hts_evt_t evt; + + evt.evt_type = BLE_HTS_EVT_INDICATION_CONFIRMED; + p_hts->evt_handler(p_hts, &evt); + } +} + + +void ble_hts_on_ble_evt(ble_hts_t * p_hts, ble_evt_t * p_ble_evt) +{ + switch (p_ble_evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + on_connect(p_hts, p_ble_evt); + break; + + case BLE_GAP_EVT_DISCONNECTED: + on_disconnect(p_hts, p_ble_evt); + break; + + case BLE_GATTS_EVT_WRITE: + on_write(p_hts, p_ble_evt); + break; + + case BLE_GATTS_EVT_HVC: + on_hvc(p_hts, p_ble_evt); + break; + + default: + // No implementation needed. + break; + } +} + + +/**@brief Function for encoding a Health Thermometer Measurement. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_hts_meas Measurement to be encoded. + * @param[out] p_encoded_buffer Buffer where the encoded data will be written. + * + * @return Size of encoded data. + */ +static uint8_t hts_measurement_encode(ble_hts_t * p_hts, + ble_hts_meas_t * p_hts_meas, + uint8_t * p_encoded_buffer) +{ + uint8_t flags = 0; + uint8_t len = 1; + uint32_t encoded_temp; + + // Flags field + if (p_hts_meas->temp_in_fahr_units) + { + flags |= HTS_MEAS_FLAG_TEMP_UNITS_BIT; + } + if (p_hts_meas->time_stamp_present) + { + flags |= HTS_MEAS_FLAG_TIME_STAMP_BIT; + } + + // Temperature Measurement Value field + if (p_hts_meas->temp_in_fahr_units) + { + flags |= HTS_MEAS_FLAG_TEMP_UNITS_BIT; + + encoded_temp = ((p_hts_meas->temp_in_fahr.exponent << 24) & 0xFF000000) | + ((p_hts_meas->temp_in_fahr.mantissa << 0) & 0x00FFFFFF); + } + else + { + encoded_temp = ((p_hts_meas->temp_in_celcius.exponent << 24) & 0xFF000000) | + ((p_hts_meas->temp_in_celcius.mantissa << 0) & 0x00FFFFFF); + } + len += uint32_encode(encoded_temp, &p_encoded_buffer[len]); + + // Time Stamp field + if (p_hts_meas->time_stamp_present) + { + flags |= HTS_MEAS_FLAG_TIME_STAMP_BIT; + len += ble_date_time_encode(&p_hts_meas->time_stamp, &p_encoded_buffer[len]); + } + + // Temperature Type field + if (p_hts_meas->temp_type_present) + { + flags |= HTS_MEAS_FLAG_TEMP_TYPE_BIT; + p_encoded_buffer[len++] = p_hts_meas->temp_type; + } + + // Flags field + p_encoded_buffer[0] = flags; + + return len; +} + + +/**@brief Function for adding Health Thermometer Measurement characteristics. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_hts_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t hts_measurement_char_add(ble_hts_t * p_hts, const ble_hts_init_t * p_hts_init) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_md_t cccd_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + ble_hts_meas_t initial_htm; + uint8_t encoded_htm[MAX_HTM_LEN]; + + memset(&cccd_md, 0, sizeof(cccd_md)); + + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + cccd_md.write_perm = p_hts_init->hts_meas_attr_md.cccd_write_perm; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.indicate = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = &cccd_md; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_TEMPERATURE_MEASUREMENT_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.read_perm = p_hts_init->hts_meas_attr_md.read_perm; + attr_md.write_perm = p_hts_init->hts_meas_attr_md.write_perm; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 1; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + memset(&initial_htm, 0, sizeof(initial_htm)); + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = hts_measurement_encode(p_hts, &initial_htm, encoded_htm); + attr_char_value.init_offs = 0; + attr_char_value.max_len = MAX_HTM_LEN; + attr_char_value.p_value = encoded_htm; + + return sd_ble_gatts_characteristic_add(p_hts->service_handle, + &char_md, + &attr_char_value, + &p_hts->meas_handles); +} + + +/**@brief Function for adding Temperature Type characteristics. + * + * @param[in] p_hts Health Thermometer Service structure. + * @param[in] p_hts_init Information needed to initialize the service. + * + * @return NRF_SUCCESS on success, otherwise an error code. + */ +static uint32_t hts_temp_type_char_add(ble_hts_t * p_hts, const ble_hts_init_t * p_hts_init) +{ + ble_gatts_char_md_t char_md; + ble_gatts_attr_t attr_char_value; + ble_uuid_t ble_uuid; + ble_gatts_attr_md_t attr_md; + uint8_t init_value_temp_type; + uint8_t init_value_encoded[1]; + + memset(&char_md, 0, sizeof(char_md)); + + char_md.char_props.read = 1; + char_md.p_char_user_desc = NULL; + char_md.p_char_pf = NULL; + char_md.p_user_desc_md = NULL; + char_md.p_cccd_md = NULL; + char_md.p_sccd_md = NULL; + + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_TEMPERATURE_TYPE_CHAR); + + memset(&attr_md, 0, sizeof(attr_md)); + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.read_perm = p_hts_init->hts_temp_type_attr_md.read_perm; + attr_md.write_perm = p_hts_init->hts_temp_type_attr_md.write_perm; + attr_md.rd_auth = 0; + attr_md.wr_auth = 0; + attr_md.vlen = 0; + + memset(&attr_char_value, 0, sizeof(attr_char_value)); + + init_value_temp_type = p_hts_init->temp_type; + init_value_encoded[0] = init_value_temp_type; + + attr_char_value.p_uuid = &ble_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = sizeof(uint8_t); + attr_char_value.init_offs = 0; + attr_char_value.max_len = sizeof(uint8_t); + attr_char_value.p_value = init_value_encoded; + + return sd_ble_gatts_characteristic_add(p_hts->service_handle, + &char_md, + &attr_char_value, + &p_hts->temp_type_handles); +} + + +uint32_t ble_hts_init(ble_hts_t * p_hts, const ble_hts_init_t * p_hts_init) +{ + uint32_t err_code; + ble_uuid_t ble_uuid; + + // Initialize service structure + p_hts->evt_handler = p_hts_init->evt_handler; + p_hts->conn_handle = BLE_CONN_HANDLE_INVALID; + p_hts->temp_type = p_hts_init->temp_type; + + // Add service + BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_HEALTH_THERMOMETER_SERVICE); + + err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_hts->service_handle); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add measurement characteristic + err_code = hts_measurement_char_add(p_hts, p_hts_init); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + // Add temperature type characteristic + if (p_hts_init->temp_type_as_characteristic) + { + err_code = hts_temp_type_char_add(p_hts, p_hts_init); + APP_ERROR_CHECK(err_code); + + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + } + + return NRF_SUCCESS; +} + + +uint32_t ble_hts_measurement_send(ble_hts_t * p_hts, ble_hts_meas_t * p_hts_meas) +{ + uint32_t err_code; + + // Send value if connected + if (p_hts->conn_handle != BLE_CONN_HANDLE_INVALID) + { + uint8_t encoded_hts_meas[MAX_HTM_LEN]; + uint16_t len; + uint16_t hvx_len; + ble_gatts_hvx_params_t hvx_params; + + len = hts_measurement_encode(p_hts, p_hts_meas, encoded_hts_meas); + hvx_len = len; + + memset(&hvx_params, 0, sizeof(hvx_params)); + + hvx_params.handle = p_hts->meas_handles.value_handle; + hvx_params.type = BLE_GATT_HVX_INDICATION; + hvx_params.offset = 0; + hvx_params.p_len = &hvx_len; + hvx_params.p_data = encoded_hts_meas; + + err_code = sd_ble_gatts_hvx(p_hts->conn_handle, &hvx_params); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + if ((err_code == NRF_SUCCESS) && (hvx_len != len)) + { + err_code = NRF_ERROR_DATA_SIZE; + } + } + else + { + err_code = NRF_ERROR_INVALID_STATE; + } + + return err_code; +} + + +uint32_t ble_hts_is_indication_enabled(ble_hts_t * p_hts, bool * p_indication_enabled) +{ + uint32_t err_code; + uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN]; + uint16_t len = BLE_CCCD_VALUE_LEN; + + err_code = sd_ble_gatts_value_get(p_hts->meas_handles.cccd_handle, + 0, + &len, + cccd_value_buf); + err_code = blocking_resp_wait(); + if (err_code != NRF_SUCCESS) + { + return err_code; + } + + if (err_code == NRF_SUCCESS) + { + *p_indication_enabled = ble_srv_is_indication_enabled(cccd_value_buf); + } + return err_code; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/services/src/ble_srv_common.c Thu Feb 09 06:08:17 2017 +0000 @@ -0,0 +1,64 @@ +/* Copyright (c) Nordic Semiconductor ASA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * 4. This software must only be used in a processor manufactured by Nordic + * Semiconductor ASA, or in a processor manufactured by a third party that + * is used in combination with a processor manufactured by Nordic Semiconductor. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* Attention! +* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile +* qualification listings, this section of source code must not be modified. +*/ + +#include "ble_srv_common.h" +#include <string.h> +#include "nordic_common.h" +#include "app_error.h" + + +uint8_t ble_srv_report_ref_encode(uint8_t * p_encoded_buffer, + const ble_srv_report_ref_t * p_report_ref) +{ + uint8_t len = 0; + + p_encoded_buffer[len++] = p_report_ref->report_id; + p_encoded_buffer[len++] = p_report_ref->report_type; + + APP_ERROR_CHECK_BOOL(len == BLE_SRV_ENCODED_REPORT_REF_LEN); + return len; +} + + +void ble_srv_ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii) +{ + p_utf8->length = (uint16_t)strlen(p_ascii); + p_utf8->p_str = (uint8_t *)p_ascii; +} +