aboutsummaryrefslogtreecommitdiff
path: root/src/sample/ble_central/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sample/ble_central/main.c')
-rw-r--r--src/sample/ble_central/main.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/src/sample/ble_central/main.c b/src/sample/ble_central/main.c
new file mode 100644
index 0000000..48a1563
--- /dev/null
+++ b/src/sample/ble_central/main.c
@@ -0,0 +1,178 @@
+/**
+*****************************************************************************************
+* Copyright(c) 2017, Realtek Semiconductor Corporation. All rights reserved.
+*****************************************************************************************
+ * @file main.c
+ * @brief Source file for BLE central project, mainly used for initialize modules
+ * @author jane
+ * @date 2017-06-12
+ * @version v1.0
+ **************************************************************************************
+ * @attention
+ * <h2><center>&copy; COPYRIGHT 2017 Realtek Semiconductor Corporation</center></h2>
+ **************************************************************************************
+ */
+
+/*============================================================================*
+ * Header Files
+ *============================================================================*/
+#include <os_sched.h>
+#include <string.h>
+#include <app_task.h>
+#include <trace.h>
+#include <gap.h>
+#include <gap_bond_le.h>
+#include <gap_scan.h>
+#include <profile_client.h>
+#include <gap_msg.h>
+#include <central_app.h>
+#include <simple_ble_client.h>
+#include <gaps_client.h>
+#include <bas_client.h>
+#include <link_mgr.h>
+
+/** @defgroup CENTRAL_DEMO_MAIN Central Main
+ * @brief Main file to initialize hardware and BT stack and start task scheduling
+ * @{
+ */
+
+/*============================================================================*
+ * Constants
+ *============================================================================*/
+/** @brief Default scan interval (units of 0.625ms, 0x10=2.5ms) */
+#define DEFAULT_SCAN_INTERVAL 0x10
+/** @brief Default scan window (units of 0.625ms, 0x10=2.5ms) */
+#define DEFAULT_SCAN_WINDOW 0x10
+
+
+/*============================================================================*
+ * Functions
+ *============================================================================*/
+/**
+ * @brief Initialize central and gap bond manager related parameters
+ * @return void
+ */
+void app_le_gap_init(void)
+{
+ /* Device name and device appearance */
+ uint8_t device_name[GAP_DEVICE_NAME_LEN] = "BLE_CENTRAL";
+ uint16_t appearance = GAP_GATT_APPEARANCE_UNKNOWN;
+
+ /* Scan parameters */
+ uint8_t scan_mode = GAP_SCAN_MODE_ACTIVE;
+ uint16_t scan_interval = DEFAULT_SCAN_INTERVAL;
+ uint16_t scan_window = DEFAULT_SCAN_WINDOW;
+ uint8_t scan_filter_policy = GAP_SCAN_FILTER_ANY;
+ uint8_t scan_filter_duplicate = GAP_SCAN_FILTER_DUPLICATE_ENABLE;
+
+ /* GAP Bond Manager parameters */
+ uint8_t auth_pair_mode = GAP_PAIRING_MODE_PAIRABLE;
+ uint16_t auth_flags = GAP_AUTHEN_BIT_BONDING_FLAG;
+ uint8_t auth_io_cap = GAP_IO_CAP_NO_INPUT_NO_OUTPUT;
+ uint8_t auth_oob = false;
+ uint8_t auth_use_fix_passkey = false;
+ uint32_t auth_fix_passkey = 0;
+ uint8_t auth_sec_req_enable = false;
+ uint16_t auth_sec_req_flags = GAP_AUTHEN_BIT_BONDING_FLAG;
+
+ /* Set device name and device appearance */
+ le_set_gap_param(GAP_PARAM_DEVICE_NAME, GAP_DEVICE_NAME_LEN, device_name);
+ le_set_gap_param(GAP_PARAM_APPEARANCE, sizeof(appearance), &appearance);
+
+ /* Set scan parameters */
+ le_scan_set_param(GAP_PARAM_SCAN_MODE, sizeof(scan_mode), &scan_mode);
+ le_scan_set_param(GAP_PARAM_SCAN_INTERVAL, sizeof(scan_interval), &scan_interval);
+ le_scan_set_param(GAP_PARAM_SCAN_WINDOW, sizeof(scan_window), &scan_window);
+ le_scan_set_param(GAP_PARAM_SCAN_FILTER_POLICY, sizeof(scan_filter_policy),
+ &scan_filter_policy);
+ le_scan_set_param(GAP_PARAM_SCAN_FILTER_DUPLICATES, sizeof(scan_filter_duplicate),
+ &scan_filter_duplicate);
+
+ /* 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);
+ le_bond_set_param(GAP_PARAM_BOND_FIXED_PASSKEY, sizeof(auth_fix_passkey), &auth_fix_passkey);
+ le_bond_set_param(GAP_PARAM_BOND_FIXED_PASSKEY_ENABLE, sizeof(auth_use_fix_passkey),
+ &auth_use_fix_passkey);
+ le_bond_set_param(GAP_PARAM_BOND_SEC_REQ_ENABLE, sizeof(auth_sec_req_enable), &auth_sec_req_enable);
+ le_bond_set_param(GAP_PARAM_BOND_SEC_REQ_REQUIREMENT, sizeof(auth_sec_req_flags),
+ &auth_sec_req_flags);
+
+ /* register gap message callback */
+ le_register_app_cb(app_gap_callback);
+}
+
+/**
+ * @brief Add GATT clients and register callbacks
+ * @return void
+ */
+void app_le_profile_init(void)
+{
+ client_init(3);
+ gaps_client_id = gaps_add_client(app_client_callback, APP_MAX_LINKS);
+ simple_ble_client_id = simp_ble_add_client(app_client_callback, APP_MAX_LINKS);
+ bas_client_id = bas_add_client(app_client_callback, APP_MAX_LINKS);
+}
+
+/**
+ * @brief Contains the initialization of pinmux settings and pad settings
+ * @note All the pinmux settings and pad settings shall be initiated in this function,
+ * but if legacy driver is used, the initialization of pinmux setting and pad setting
+ * should be performed with the IO initializing.
+ * @return void
+ */
+void board_init(void)
+{
+
+}
+
+/**
+ * @brief Contains the initialization of peripherals
+ * @note Both new architecture driver and legacy driver initialization method can be used
+ * @return void
+ */
+void driver_init(void)
+{
+
+}
+
+/**
+ * @brief Contains the power mode settings
+ * @return void
+ */
+void pwr_mgr_init(void)
+{
+}
+
+/**
+ * @brief Contains the initialization of all tasks
+ * @note There is only one task in BLE Central APP, thus only one APP task is init here
+ * @return void
+ */
+void task_init(void)
+{
+ app_task_init();
+}
+
+/**
+ * @brief Entry of APP code
+ * @return int (To avoid compile warning)
+ */
+int main(void)
+{
+ board_init();
+ le_gap_init(APP_MAX_LINKS);
+ gap_lib_init();
+ app_le_gap_init();
+ app_le_profile_init();
+ pwr_mgr_init();
+ task_init();
+ os_sched_start();
+
+ return 0;
+}
+/** @} */ /* End of group CENTRAL_DEMO_MAIN */
+
+