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 "bta_pan_api.h"
27
28 #include <cstdint>
29 #include <cstring>
30 #include <string>
31
32 #include "bta/pan/bta_pan_int.h"
33 #include "bta_api.h"
34 #include "bta_sys.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/compat.h"
37 #include "stack/include/bt_hdr.h"
38 #include "stack/include/main_thread.h"
39 #include "types/raw_address.h"
40
41 static const tBTA_SYS_REG bta_pan_reg = {bta_pan_hdl_event, BTA_PanDisable};
42
43 std::string user_service_name; /* Service name for PANU role */
44 std::string gn_service_name; /* Service name for GN role */
45 std::string nap_service_name; /* Service name for NAP role */
46
47 #ifndef PAN_SECURITY
48 #define PAN_SECURITY \
49 (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_OUT_ENCRYPT)
50 #endif
51
52 /*******************************************************************************
53 *
54 * Function BTA_PanEnable
55 *
56 * Description Enable PAN service. This function must be
57 * called before any other functions in the PAN API are called.
58 * When the enable operation is complete the callback function
59 * will be called with a BTA_PAN_ENABLE_EVT.
60 *
61 * Returns void
62 *
63 ******************************************************************************/
BTA_PanEnable(tBTA_PAN_CBACK p_cback)64 void BTA_PanEnable(tBTA_PAN_CBACK p_cback) {
65 tBTA_PAN_API_ENABLE* p_buf = (tBTA_PAN_API_ENABLE*)osi_malloc(sizeof(tBTA_PAN_API_ENABLE));
66
67 /* register with BTA system manager */
68 bta_sys_register(BTA_ID_PAN, &bta_pan_reg);
69
70 p_buf->hdr.event = BTA_PAN_API_ENABLE_EVT;
71 p_buf->p_cback = p_cback;
72
73 bta_sys_sendmsg(p_buf);
74 }
75
76 /*******************************************************************************
77 *
78 * Function BTA_PanDisable
79 *
80 * Description Disables PAN service.
81 *
82 *
83 * Returns void
84 *
85 ******************************************************************************/
BTA_PanDisable(void)86 void BTA_PanDisable(void) {
87 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
88
89 bta_sys_deregister(BTA_ID_PAN);
90 p_buf->event = BTA_PAN_API_DISABLE_EVT;
91
92 bta_sys_sendmsg(p_buf);
93 }
94
95 /*******************************************************************************
96 *
97 * Function BTA_PanSetRole
98 *
99 * Description Sets PAN roles. When the enable operation is complete
100 * the callback function will be called with a
101 * BTA_PAN_SET_ROLE_EVT.
102 *
103 * Returns void
104 *
105 ******************************************************************************/
BTA_PanSetRole(tBTA_PAN_ROLE role,const tBTA_PAN_ROLE_INFO user_info,const tBTA_PAN_ROLE_INFO nap_info)106 void BTA_PanSetRole(tBTA_PAN_ROLE role, const tBTA_PAN_ROLE_INFO user_info,
107 const tBTA_PAN_ROLE_INFO nap_info) {
108 post_on_bt_main([=]() {
109 tBTA_PAN_DATA data = {
110 .api_set_role =
111 {
112 .hdr =
113 {
114 .event = BTA_PAN_API_SET_ROLE_EVT,
115 },
116 .user_name = {},
117 .nap_name = {},
118 .role = role,
119 },
120 };
121 if (role & BTA_PAN_ROLE_PANU) {
122 if (!user_info.p_srv_name.empty()) {
123 strncpy(data.api_set_role.user_name, user_info.p_srv_name.data(), BTA_SERVICE_NAME_LEN);
124 }
125 data.api_set_role.user_app_id = user_info.app_id;
126 }
127
128 if (role & BTA_PAN_ROLE_NAP) {
129 if (!nap_info.p_srv_name.empty()) {
130 strncpy(data.api_set_role.nap_name, nap_info.p_srv_name.data(), BTA_SERVICE_NAME_LEN);
131 }
132 data.api_set_role.nap_app_id = nap_info.app_id;
133 }
134 bta_pan_set_role(&data);
135 });
136 }
137
138 /*******************************************************************************
139 *
140 * Function BTA_PanOpen
141 *
142 * Description Opens a connection to a peer device.
143 * When connection is open callback function is called
144 * with a BTA_PAN_OPEN_EVT.
145 *
146 *
147 * Returns void
148 *
149 ******************************************************************************/
BTA_PanOpen(const RawAddress & bd_addr,tBTA_PAN_ROLE local_role,tBTA_PAN_ROLE peer_role)150 void BTA_PanOpen(const RawAddress& bd_addr, tBTA_PAN_ROLE local_role, tBTA_PAN_ROLE peer_role) {
151 tBTA_PAN_API_OPEN* p_buf = (tBTA_PAN_API_OPEN*)osi_malloc(sizeof(tBTA_PAN_API_OPEN));
152
153 p_buf->hdr.event = BTA_PAN_API_OPEN_EVT;
154 p_buf->local_role = local_role;
155 p_buf->peer_role = peer_role;
156 p_buf->bd_addr = bd_addr;
157
158 bta_sys_sendmsg(p_buf);
159 }
160
161 /*******************************************************************************
162 *
163 * Function BTA_PanClose
164 *
165 * Description Close a PAN connection to a peer device.
166 *
167 *
168 * Returns void
169 *
170 ******************************************************************************/
BTA_PanClose(uint16_t handle)171 void BTA_PanClose(uint16_t handle) {
172 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
173
174 p_buf->event = BTA_PAN_API_CLOSE_EVT;
175 p_buf->layer_specific = handle;
176
177 bta_sys_sendmsg(p_buf);
178 }
179