1 /******************************************************************************
2 *
3 * Copyright 2004-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This is the implementation of the API for PAN subsystem of BTA,
22 * Broadcom's Bluetooth application layer for mobile phones.
23 *
24 ******************************************************************************/
25
26 #include <cstdint>
27 #include <cstring>
28
29 #include "bta/pan/bta_pan_int.h"
30 #include "internal_include/bt_target.h"
31 #include "osi/include/allocator.h"
32 #include "osi/include/compat.h"
33 #include "stack/include/bt_hdr.h"
34 #include "stack/include/main_thread.h"
35 #include "types/raw_address.h"
36
37 static const tBTA_SYS_REG bta_pan_reg = {bta_pan_hdl_event, BTA_PanDisable};
38 void bta_pan_api_disable(tBTA_PAN_DATA* p_data);
39 void bta_pan_api_enable(tBTA_PAN_DATA* p_data);
40 void bta_pan_api_open(tBTA_PAN_DATA* p_data);
41 void bta_pan_sm_execute(tBTA_PAN_SCB* p_scb, uint16_t event,
42 tBTA_PAN_DATA* p_data);
43
44 std::string user_service_name; /* Service name for PANU role */
45 std::string gn_service_name; /* Service name for GN role */
46 std::string nap_service_name; /* Service name for NAP role */
47
48 #ifndef PAN_SECURITY
49 #define PAN_SECURITY \
50 (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | \
51 BTM_SEC_OUT_ENCRYPT)
52 #endif
53
54 /*******************************************************************************
55 *
56 * Function BTA_PanEnable
57 *
58 * Description Enable PAN service. This function must be
59 * called before any other functions in the PAN API are called.
60 * When the enable operation is complete the callback function
61 * will be called with a BTA_PAN_ENABLE_EVT.
62 *
63 * Returns void
64 *
65 ******************************************************************************/
BTA_PanEnable(tBTA_PAN_CBACK p_cback)66 void BTA_PanEnable(tBTA_PAN_CBACK p_cback) {
67 tBTA_PAN_API_ENABLE* p_buf =
68 (tBTA_PAN_API_ENABLE*)osi_malloc(sizeof(tBTA_PAN_API_ENABLE));
69
70 /* register with BTA system manager */
71 bta_sys_register(BTA_ID_PAN, &bta_pan_reg);
72
73 p_buf->hdr.event = BTA_PAN_API_ENABLE_EVT;
74 p_buf->p_cback = p_cback;
75
76 bta_sys_sendmsg(p_buf);
77 }
78
79 /*******************************************************************************
80 *
81 * Function BTA_PanDisable
82 *
83 * Description Disables PAN service.
84 *
85 *
86 * Returns void
87 *
88 ******************************************************************************/
BTA_PanDisable(void)89 void BTA_PanDisable(void) {
90 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
91
92 bta_sys_deregister(BTA_ID_PAN);
93 p_buf->event = BTA_PAN_API_DISABLE_EVT;
94
95 bta_sys_sendmsg(p_buf);
96 }
97
98 /*******************************************************************************
99 *
100 * Function BTA_PanSetRole
101 *
102 * Description Sets PAN roles. When the enable operation is complete
103 * the callback function will be called with a
104 * BTA_PAN_SET_ROLE_EVT.
105 *
106 * Returns void
107 *
108 ******************************************************************************/
BTA_PanSetRole(tBTA_PAN_ROLE role,const tBTA_PAN_ROLE_INFO user_info,const tBTA_PAN_ROLE_INFO nap_info)109 void BTA_PanSetRole(tBTA_PAN_ROLE role, const tBTA_PAN_ROLE_INFO user_info,
110 const tBTA_PAN_ROLE_INFO nap_info) {
111 post_on_bt_main([=]() {
112 tBTA_PAN_DATA data = {
113 .api_set_role =
114 {
115 .hdr =
116 {
117 .event = BTA_PAN_API_SET_ROLE_EVT,
118 },
119 .user_name = {},
120 .nap_name = {},
121 .role = role,
122 },
123 };
124 if (role & BTA_PAN_ROLE_PANU) {
125 if (!user_info.p_srv_name.empty())
126 strncpy(data.api_set_role.user_name, user_info.p_srv_name.data(),
127 BTA_SERVICE_NAME_LEN);
128 data.api_set_role.user_app_id = user_info.app_id;
129 }
130
131 if (role & BTA_PAN_ROLE_NAP) {
132 if (!nap_info.p_srv_name.empty())
133 strncpy(data.api_set_role.nap_name, nap_info.p_srv_name.data(),
134 BTA_SERVICE_NAME_LEN);
135 data.api_set_role.nap_app_id = nap_info.app_id;
136 }
137 bta_pan_set_role(&data);
138 });
139 }
140
141 /*******************************************************************************
142 *
143 * Function BTA_PanOpen
144 *
145 * Description Opens a connection to a peer device.
146 * When connection is open callback function is called
147 * with a BTA_PAN_OPEN_EVT.
148 *
149 *
150 * Returns void
151 *
152 ******************************************************************************/
BTA_PanOpen(const RawAddress & bd_addr,tBTA_PAN_ROLE local_role,tBTA_PAN_ROLE peer_role)153 void BTA_PanOpen(const RawAddress& bd_addr, tBTA_PAN_ROLE local_role,
154 tBTA_PAN_ROLE peer_role) {
155 tBTA_PAN_API_OPEN* p_buf =
156 (tBTA_PAN_API_OPEN*)osi_malloc(sizeof(tBTA_PAN_API_OPEN));
157
158 p_buf->hdr.event = BTA_PAN_API_OPEN_EVT;
159 p_buf->local_role = local_role;
160 p_buf->peer_role = peer_role;
161 p_buf->bd_addr = bd_addr;
162
163 bta_sys_sendmsg(p_buf);
164 }
165
166 /*******************************************************************************
167 *
168 * Function BTA_PanClose
169 *
170 * Description Close a PAN connection to a peer device.
171 *
172 *
173 * Returns void
174 *
175 ******************************************************************************/
BTA_PanClose(uint16_t handle)176 void BTA_PanClose(uint16_t handle) {
177 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
178
179 p_buf->event = BTA_PAN_API_CLOSE_EVT;
180 p_buf->layer_specific = handle;
181
182 bta_sys_sendmsg(p_buf);
183 }
184