/** ***************************************************************************************** * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved. ***************************************************************************************** * @file gap.h * @brief This file contains all the constants and functions prototypes for GAP. * @details This file is used both bredr and le. * @author jane * @date 2017-02-18 * @version v1.0 * ************************************************************************************* */ /*============================================================================* * Define to prevent recursive inclusion *============================================================================*/ #ifndef GAP_H #define GAP_H #ifdef __cplusplus extern "C" { #endif /*============================================================================* * Header Files *============================================================================*/ #include #include #include "upperstack_config.h" #include /** @addtogroup GAP_COMMON GAP Common * @brief GAP common module * @{ */ /*============================================================================* * Macros *============================================================================*/ /** @defgroup GAP_Common_Exported_Macros GAP Common Exported Macros * @{ */ /** @defgroup GAP_Common_Macros GAP Common Macros * @{ */ #define GAP_BD_ADDR_LEN 6 //!< Default Bluetooth Device Address Length. #define GAP_KEY_LEN 16 //!< Default Long Term Key(LTK) length. #define GAP_DEVICE_NAME_LEN (39+1)//!< Max length of device name, if device name length exceeds it, it will be truncated. #define GAP_PASSCODE_MAX 999999//!< Maximum Pairing Passcode/Passkey value. Range of a passkey can be 0 - 999,999. #define GAP_OOB_LEN 16 //!< Length of out of band data. /** * @} */ /** @defgroup BOND_PAIRING_MODE_DEFINES Pairing Modes * @{ */ #define GAP_PAIRING_MODE_NO_PAIRING 0x00 //!< Pairing is not allowed. #define GAP_PAIRING_MODE_PAIRABLE 0x01 //!< Pairable, Wait for a pairing request from master or security request from slave. /** * @} */ /** @defgroup BOND_MITM_DEFINES Mitm And Bonding Flags * @{ */ #define GAP_AUTHEN_BIT_NONE 0 //!< No authentication required. #define GAP_AUTHEN_BIT_BONDING_FLAG 0x0001 //!< Bonding is required #define GAP_AUTHEN_BIT_MITM_FLAG 0x0004 //!< Mitm is preferred #if F_BT_LE_4_2_SC_SUPPORT #define GAP_AUTHEN_BIT_SC_FLAG 0x0008 //!< Secure connection is preferred #define GAP_AUTHEN_BIT_SC_ONLY_FLAG 0x0200 //!< Secure connection only mode for BLE is required #endif #if F_BT_KEY_PRESS_SUPPORT #define GAP_AUTHEN_BIT_KEYPRESS_FLAG 0x0010 //!< Keypress notifaction is preferred #endif #define GAP_AUTHEN_BIT_FORCE_BONDING_FLAG 0x0100 //!< Force bonding is required /** * @} */ /** @defgroup GAP_COMMON_MSG_TYPE GAP Msg Type * @{ */ #define GAP_MSG_WRITE_AIRPLAN_MODE 0x00 //!Example usage * \code{.c} void app_main_task(void *p_param) { uint8_t event; os_msg_queue_create(&io_queue_handle, MAX_NUMBER_OF_IO_MESSAGE, sizeof(T_IO_MSG)); os_msg_queue_create(&evt_queue_handle, MAX_NUMBER_OF_EVENT_MESSAGE, sizeof(uint8_t)); gap_start_bt_stack(evt_queue_handle, io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE); while (true) { if (os_msg_recv(evt_queue_handle, &event, 0xFFFFFFFF) == true) { if (event == EVENT_IO_TO_APP) { T_IO_MSG io_msg; if (os_msg_recv(io_queue_handle, &io_msg, 0) == true) { app_handle_io_msg(io_msg); } } else { gap_handle_msg(event); } } } } * \endcode */ bool gap_start_bt_stack(void *evt_queue, void *io_queue, uint16_t msg_queue_elem_num); /** * @brief Initialize gap lib to use some extended functions. * @return void * Example usage * \code{.c} int main(void) { ... gap_lib_init(); ... } * \endcode */ void gap_lib_init(void); /** * @brief Register callback to gap, when messages in @ref GAP_COMMON_MSG_TYPE happens, it will callback to app. * @param[in] app_callback Callback function provided by the APP to handle gap common messages sent from the GAP. * @arg NULL -> Not send gap common messages to APP. * @arg Other -> Use application defined callback function. * @return void * * Example usage * \code{.c} void app_le_gap_init() { ... le_register_app_cb(app_gap_callback); gap_register_app_cb(app_gap_common_callback); } \endcode */ void gap_register_app_cb(P_FUN_GAP_APP_CB app_callback); /** * @brief Set a GAP parameter. * * NOTE: You can call this function with a GAP Common Parameter type and it will set the * GAP Parameter. GAP Peripheral Parameters are defined in @ref T_GAP_PARAM_TYPE. * If the "len" field sets to the size of a "uint16_t" ,the * "p_value" field must point to a data with type of "uint16_t". * * @param[in] param GAP parameter ID: @ref T_GAP_PARAM_TYPE. * @param[in] len Length of data to write. * @param[in] p_value Pointer to data to write. This is dependent on * the parameter type and WILL be cast to the appropriate * data type (For example: if data type param is uint16, p_value will be cast to * pointer of uint16_t). * @return Operation result. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. * * Example usage * \code{.c} void app_le_gap_init(void) { ... //GAP Bond Manager parameters uint8_t auth_pair_mode = GAP_PAIRING_MODE_PAIRABLE; uint16_t auth_flags = GAP_AUTHEN_BIT_BONDING_FLAG | GAP_AUTHEN_BIT_MITM_FLAG; uint8_t auth_io_cap = GAP_IO_CAP_NO_INPUT_NO_OUTPUT; uint8_t auth_oob = false; // Setup the GAP Bond Manager gap_set_param(GAP_PARAM_BOND_PAIRING_MODE, sizeof(auth_pair_mode), &auth_pair_mode); gap_set_param(GAP_PARAM_BOND_AUTHEN_REQUIREMENTS_FLAGS, sizeof(auth_flags), &auth_flags); gap_set_param(GAP_PARAM_BOND_IO_CAPABILITIES, sizeof(auth_io_cap), &auth_io_cap); gap_set_param(GAP_PARAM_BOND_OOB_ENABLED, sizeof(auth_oob), &auth_oob); ... } * \endcode */ T_GAP_CAUSE gap_set_param(T_GAP_PARAM_TYPE param, uint8_t len, void *p_value); /** * @brief Get a GAP Common parameter. * * NOTE: You can call this function with a GAP Common Parameter type and it will get a * GAP Common Parameter. GAP Common Parameters are defined in @ref T_GAP_PARAM_TYPE. * * @param[in] param GAP parameter type: @ref T_GAP_PARAM_TYPE * @param[in,out] p_value Pointer to location to get the value. This is dependent on * the parameter ID and WILL be cast to the appropriate * data type (For example: if data type param is uint16, p_value will be cast to * pointer of uint16_t). * @return Operation result. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. * * Example usage * \code{.c} void test(void) { uint8_t bt_addr[6]; gap_get_param(GAP_PARAM_BD_ADDR, bt_addr); } * \endcode */ T_GAP_CAUSE gap_get_param(T_GAP_PARAM_TYPE param, void *p_value); /** * @brief Make the device in pairable mode. * @return Operation result. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. */ T_GAP_CAUSE gap_set_pairable_mode(void); /** * @brief Write command to control device to enter airplane mode, the result will be notified by callback regisgtered by @ref gap_register_app_cb. * @param[in] mode the mode of the device * @return Operation result. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. * * Example usage * \code{.c} static T_USER_CMD_PARSE_RESULT cmd_wairplane(T_USER_CMD_PARSED_VALUE *p_parse_value) { T_GAP_CAUSE cause; uint8_t mode = p_parse_value->dw_param[0]; cause = gap_write_airplan_mode(mode); return (T_USER_CMD_PARSE_RESULT)cause; } void app_gap_common_callback(uint8_t cb_type, void *p_cb_data) { T_GAP_CB_DATA cb_data; memcpy(&cb_data, p_cb_data, sizeof(T_GAP_CB_DATA)); APP_PRINT_INFO1("app_gap_common_callback: cb_type = %d", cb_type); switch (cb_type) { case GAP_MSG_WRITE_AIRPLAN_MODE: APP_PRINT_INFO1("GAP_MSG_WRITE_AIRPLAN_MODE: cause 0x%x", cb_data.p_gap_write_airplan_mode_rsp->cause); break; case GAP_MSG_READ_AIRPLAN_MODE: APP_PRINT_INFO2("GAP_MSG_READ_AIRPLAN_MODE: cause 0x%x, mode %d", cb_data.p_gap_read_airplan_mode_rsp->cause, cb_data.p_gap_read_airplan_mode_rsp->mode); break; default: break; } return; } * \endcode */ T_GAP_CAUSE gap_write_airplan_mode(uint8_t mode); /** * @brief Read the airplane mode of the device, the result will be notified by callback regisgtered by @ref gap_register_app_cb. * @return Operation result. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. * * Example usage * \code{.c} static T_USER_CMD_PARSE_RESULT cmd_rairplane(T_USER_CMD_PARSED_VALUE *p_parse_value) { T_GAP_CAUSE cause; cause = gap_read_airplan_mode(); return (T_USER_CMD_PARSE_RESULT)cause; } void app_gap_common_callback(uint8_t cb_type, void *p_cb_data) { T_GAP_CB_DATA cb_data; memcpy(&cb_data, p_cb_data, sizeof(T_GAP_CB_DATA)); APP_PRINT_INFO1("app_gap_common_callback: cb_type = %d", cb_type); switch (cb_type) { case GAP_MSG_WRITE_AIRPLAN_MODE: APP_PRINT_INFO1("GAP_MSG_WRITE_AIRPLAN_MODE: cause 0x%x", cb_data.p_gap_write_airplan_mode_rsp->cause); break; case GAP_MSG_READ_AIRPLAN_MODE: APP_PRINT_INFO2("GAP_MSG_READ_AIRPLAN_MODE: cause 0x%x, mode %d", cb_data.p_gap_read_airplan_mode_rsp->cause, cb_data.p_gap_read_airplan_mode_rsp->mode); break; default: break; } return; } * \endcode */ T_GAP_CAUSE gap_read_airplan_mode(void); /** * @brief Handle GAP message received from BT stack or timeout event for GAP timer. * @param[in] event Event type which is received from APP event queue. * @return None. * * Example usage * \code{.c} void app_main_task(void *p_param) { uint8_t event; os_msg_queue_create(&io_queue_handle, MAX_NUMBER_OF_IO_MESSAGE, sizeof(T_IO_MSG)); os_msg_queue_create(&evt_queue_handle, MAX_NUMBER_OF_EVENT_MESSAGE, sizeof(uint8_t)); gap_start_bt_stack(evt_queue_handle, io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE); while (true) { if (os_msg_recv(evt_queue_handle, &event, 0xFFFFFFFF) == true) { if (event == EVENT_IO_TO_APP) { T_IO_MSG io_msg; if (os_msg_recv(io_queue_handle, &io_msg, 0) == true) { app_handle_io_msg(io_msg); } } else { gap_handle_msg(event); } } } } * \endcode */ void gap_handle_msg(uint8_t event); /** * @brief Release the buffer of GAP supplied. * @param[in] p_buf Point the buffer that needs to release. * @retval GAP_CAUSE_SUCCESS Operation success. * @retval Others Operation failure. * * Example usage * \code{.c} uint8_t *p_data_buf; uint16_t data_offset; T_APP_RESULT simp_ble_service_attr_write_cb(uint8_t conn_id, T_SERVER_ID service_id, uint16_t attrib_index, T_WRITE_TYPE write_type, uint16_t length, uint8_t *p_value, P_FUN_WRITE_IND_POST_PROC *p_write_ind_post_proc) { ...... server_get_write_cmd_data_buffer(conn_id, &p_data_buf, &data_offset); return APP_RESULT_NOT_RELEASE; } void release(void) { if(p_data_buf != NULL) { gap_buffer_free(p_data_buf); p_data_buf = NULL; } } * \endcode */ T_GAP_CAUSE gap_buffer_free(void *p_buf); /** * @brief Send vendor command. * @param[in] op Opcode of command. * @param[in] len Length of parameters. * @param[in] p_param Pointer to parameters to write. * @return Operation result. * @retval GAP_CAUSE_SUCCESS Send request successfully. * @retval Others Failure. * * Example usage * \code{.c} //send vendor command void test(void) { T_GAP_CAUSE cause = GAP_CAUSE_SUCCESS; uint16_t opcode; uint8_t param[2]; param[0] = 0; param[1] = 0; cause = gap_vendor_cmd_req(opcode, 2, param); } //register callback, and handle response void app_le_gap_init(void) { ...... gap_register_app_cb(app_gap_common_callback); } void app_gap_common_callback(uint8_t cb_type, void *p_cb_data) { T_GAP_CB_DATA cb_data; memcpy(&cb_data, p_cb_data, sizeof(T_GAP_CB_DATA)); APP_PRINT_INFO1("app_gap_common_callback: cb_type = %d", cb_type); switch (cb_type) { case GAP_MSG_VENDOR_CMD_RSP: APP_PRINT_INFO3("GAP_MSG_VENDOR_CMD_RSP: command 0x%x, cause 0x%x, param_len %d", cb_data.p_gap_vendor_cmd_rsp->command, cb_data.p_gap_vendor_cmd_rsp->cause, cb_data.p_gap_vendor_cmd_rsp->param_len); break; ...... } * \endcode */ T_GAP_CAUSE gap_vendor_cmd_req(uint16_t op, uint8_t len, uint8_t *p_param); /** @} */ /* End of group GAP_COMMON_EXPORT_Functions */ /** @} */ /* End of group GAP_COMMON */ /*------------------------------------------------------------------- -------------------------------------------------------------------*/ #ifdef __cplusplus } #endif #endif /* GAP_H */