-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
279 lines (241 loc) · 9.32 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// based on the ble_peripheral example in RTL8762C-sdk-gcc-v0.0.5.zip
// <https://www.realmcu.com/en/Home/Product/93cc0582-3a3f-4ea8-82ea-76c6504e478a>
// modified by wuwbobo2021 <https://github.com/wuwbobo2021>, <[email protected]>
/*============================================================================*
* Header Files
*============================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <os_sched.h>
#include <trace.h>
#include <ftl.h>
#include <gap.h>
#include <gap_adv.h>
#include <gap_bond_le.h>
#include <profile_server.h>
#include <gap_msg.h>
#include "rtl876x_pinmux.h"
#if F_BT_DLPS_EN
#include <dlps.h>
#include <rtl876x_io_dlps.h>
#endif
#include "simple_ble_service.h"
#include "app_task.h"
#include "peripheral_app.h"
#include "uart.h"
#include "board.h"
/** @defgroup PERIPH_DEMO_MAIN Peripheral Main
* @brief Main file to initialize hardware and BT stack and start task scheduling
* @{
*/
/*============================================================================*
* Constants
*============================================================================*/
/** @brief Default minimum advertising interval when device is discoverable (units of 625us, 160=100ms) */
#define DEFAULT_ADVERTISING_INTERVAL_MIN 320
/** @brief Default maximum advertising interval */
#define DEFAULT_ADVERTISING_INTERVAL_MAX 320
/*============================================================================*
* Variables
*============================================================================*/
/** @brief GAP - scan response data (max size = 31 bytes) */
static const uint8_t scan_rsp_data[] =
{
0x03, /* length */
GAP_ADTYPE_APPEARANCE, /* type="Appearance" */
LO_WORD(GAP_GATT_APPEARANCE_UNKNOWN),
HI_WORD(GAP_GATT_APPEARANCE_UNKNOWN),
};
/** @brief GAP - Advertisement data (max size = 31 bytes, best kept short to conserve power) */
static uint8_t adv_data[9 + 15] =
{
/* Flags */
0x02, /* length */
GAP_ADTYPE_FLAGS, /* type="Flags" */
GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
/* Service */
0x03, /* length */
GAP_ADTYPE_16BIT_COMPLETE,
LO_WORD(GATT_UUID_SIMPLE_PROFILE),
HI_WORD(GATT_UUID_SIMPLE_PROFILE),
/* Local name */
0x10, /* length */
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
/* name string */
};
extern T_SERVER_ID simp_srv_id; //from peripheral_app.c
/*============================================================================*
* Functions
*============================================================================*/
/**
* @brief Initialize peripheral and gap bond manager related parameters
* @return void
*/
void app_le_gap_init(void)
{
/* Device name and device appearance */
char device_name[GAP_DEVICE_NAME_LEN] = "RTL-UART-";
uint8_t local_addr[6];
gap_get_param(GAP_PARAM_BD_ADDR, local_addr);
sprintf(device_name + 9, "%02X%02X%02X", local_addr[3], local_addr[4], local_addr[5]);
memcpy(adv_data + 0x9, device_name, 15);
uint16_t appearance = GAP_GATT_APPEARANCE_UNKNOWN;
uint8_t slave_init_mtu_req = true; //otherwise the MTU will be the minimum size
/* Advertising parameters */
uint8_t adv_evt_type = GAP_ADTYPE_ADV_IND;
uint8_t adv_direct_type = GAP_REMOTE_ADDR_LE_PUBLIC;
uint8_t adv_direct_addr[GAP_BD_ADDR_LEN] = {0};
uint8_t adv_chann_map = GAP_ADVCHAN_ALL;
uint8_t adv_filter_policy = GAP_ADV_FILTER_ANY;
#if F_BT_DLPS_EN
uint16_t adv_int_min = 0x2000;
uint16_t adv_int_max = 0x2000;
#else
uint16_t adv_int_min = DEFAULT_ADVERTISING_INTERVAL_MIN;
uint16_t adv_int_max = DEFAULT_ADVERTISING_INTERVAL_MAX;
#endif
/* GAP Bond Manager parameters */
#if SIMP_SRV_AUTHEN_EN
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_DISPLAY_ONLY;
uint8_t auth_oob = false;
uint8_t auth_use_fix_passkey = true;
uint32_t auth_fix_passkey = AUTHEN_FIXED_PIN;
uint8_t auth_sec_req_enable = true;
uint16_t auth_sec_req_flags = GAP_AUTHEN_BIT_MITM_FLAG;
#else
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;
#endif
/* 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);
le_set_gap_param(GAP_PARAM_SLAVE_INIT_GATT_MTU_REQ, sizeof(slave_init_mtu_req),
&slave_init_mtu_req);
/* Set advertising parameters */
le_adv_set_param(GAP_PARAM_ADV_EVENT_TYPE, sizeof(adv_evt_type), &adv_evt_type);
le_adv_set_param(GAP_PARAM_ADV_DIRECT_ADDR_TYPE, sizeof(adv_direct_type), &adv_direct_type);
le_adv_set_param(GAP_PARAM_ADV_DIRECT_ADDR, sizeof(adv_direct_addr), adv_direct_addr);
le_adv_set_param(GAP_PARAM_ADV_CHANNEL_MAP, sizeof(adv_chann_map), &adv_chann_map);
le_adv_set_param(GAP_PARAM_ADV_FILTER_POLICY, sizeof(adv_filter_policy), &adv_filter_policy);
le_adv_set_param(GAP_PARAM_ADV_INTERVAL_MIN, sizeof(adv_int_min), &adv_int_min);
le_adv_set_param(GAP_PARAM_ADV_INTERVAL_MAX, sizeof(adv_int_max), &adv_int_max);
le_adv_set_param(GAP_PARAM_ADV_DATA, sizeof(adv_data), (void *)adv_data);
le_adv_set_param(GAP_PARAM_SCAN_RSP_DATA, sizeof(scan_rsp_data), (void *)scan_rsp_data);
/* 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 services and register callbacks
* @return void
*/
void app_le_profile_init(void)
{
server_init(2);
simp_srv_id = simp_ble_service_add_service(app_profile_callback);
server_register_app_cb(app_profile_callback);
}
/**
* @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 peformed with the IO initializing.
* @return void
*/
void board_init(void)
{
// Pad_Config() can be added here
board_uart_init();
}
/**
* @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)
{
static uint32_t baudrate = 9600;
if (ftl_load(&baudrate, FTL_APP_STORE_BAUDRATE_OFFSET, 4) != 0) {
baudrate = 9600;
ftl_save(&baudrate, FTL_APP_STORE_BAUDRATE_OFFSET, 4);
}
#if F_BT_DLPS_EN
uart_baudrate_target = baudrate;
uart_config_dlps(true);
#else
driver_uart_init(baudrate);
#endif
le_adv_set_tx_power(GAP_ADV_TX_POW_SET_1M, 0xD0); //7.5 dBm
}
/**
* @brief Contains the power mode settings
* @return void
*/
bool dlps_check_cb(void)
{
return app_allowed_enter_dlps;
}
void io_dlps_enter_cb(void)
{
}
void io_dlps_exit_cb(void)
{
}
void pwr_mgr_init(void)
{
#if F_BT_DLPS_EN
if (! dlps_check_cb_reg(dlps_check_cb))
DBG_DIRECT("Error: dlps_check_cb_reg(dlps_check_cb) failed!");
DLPS_IORegUserDlpsEnterCb(io_dlps_enter_cb);
DLPS_IORegUserDlpsExitCb(io_dlps_exit_cb);
DLPS_IORegister();
lps_mode_set(LPM_DLPS_MODE);
#endif
}
/**
* @brief Contains the initialization of all tasks
* @note There is only one task in BLE Peripheral 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)
{
extern uint32_t random_seed_value;
srand(random_seed_value);
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 PERIPH_DEMO_MAIN */