1/******************************************************************************
2 *
3 *  Copyright (C) 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#include <string.h>
26
27#include "bt_target.h"
28
29#include "bt_common.h"
30#include "bt_utils.h"
31#include "bta_api.h"
32#include "bta_pan_api.h"
33#include "bta_pan_int.h"
34#include "bta_sys.h"
35#include "osi/include/osi.h"
36#include "pan_api.h"
37
38#if (BTA_PAN_INCLUDED == TRUE)
39
40static const tBTA_SYS_REG bta_pan_reg = {bta_pan_hdl_event, BTA_PanDisable};
41
42/*******************************************************************************
43 *
44 * Function         BTA_PanEnable
45 *
46 * Description      Enable PAN service.  This function must be
47 *                  called before any other functions in the PAN API are called.
48 *                  When the enable operation is complete the callback function
49 *                  will be called with a BTA_PAN_ENABLE_EVT.
50 *
51 * Returns          void
52 *
53 ******************************************************************************/
54void BTA_PanEnable(tBTA_PAN_CBACK p_cback) {
55  tBTA_PAN_API_ENABLE* p_buf =
56      (tBTA_PAN_API_ENABLE*)osi_malloc(sizeof(tBTA_PAN_API_ENABLE));
57
58  /* register with BTA system manager */
59  bta_sys_register(BTA_ID_PAN, &bta_pan_reg);
60
61  p_buf->hdr.event = BTA_PAN_API_ENABLE_EVT;
62  p_buf->p_cback = p_cback;
63
64  bta_sys_sendmsg(p_buf);
65}
66
67/*******************************************************************************
68 *
69 * Function         BTA_PanDisable
70 *
71 * Description      Disables PAN service.
72 *
73 *
74 * Returns          void
75 *
76 ******************************************************************************/
77void BTA_PanDisable(void) {
78  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
79
80  bta_sys_deregister(BTA_ID_PAN);
81  p_buf->event = BTA_PAN_API_DISABLE_EVT;
82
83  bta_sys_sendmsg(p_buf);
84}
85
86/*******************************************************************************
87 *
88 * Function         BTA_PanSetRole
89 *
90 * Description      Sets PAN roles. When the enable operation is complete
91 *                  the callback function will be called with a
92 *                  BTA_PAN_SET_ROLE_EVT.
93 *
94 * Returns          void
95 *
96 ******************************************************************************/
97void BTA_PanSetRole(tBTA_PAN_ROLE role, tBTA_PAN_ROLE_INFO* p_user_info,
98                    tBTA_PAN_ROLE_INFO* p_gn_info,
99                    tBTA_PAN_ROLE_INFO* p_nap_info) {
100  tBTA_PAN_API_SET_ROLE* p_buf =
101      (tBTA_PAN_API_SET_ROLE*)osi_calloc(sizeof(tBTA_PAN_API_SET_ROLE));
102
103  p_buf->hdr.event = BTA_PAN_API_SET_ROLE_EVT;
104  p_buf->role = role;
105
106  if (p_user_info && (role & BTA_PAN_ROLE_PANU)) {
107    if (p_user_info->p_srv_name)
108      strlcpy(p_buf->user_name, p_user_info->p_srv_name, BTA_SERVICE_NAME_LEN);
109
110    p_buf->user_app_id = p_user_info->app_id;
111    p_buf->user_sec_mask = p_user_info->sec_mask;
112  }
113
114  if (p_gn_info && (role & BTA_PAN_ROLE_GN)) {
115    if (p_gn_info->p_srv_name)
116      strlcpy(p_buf->gn_name, p_gn_info->p_srv_name, BTA_SERVICE_NAME_LEN);
117
118    p_buf->gn_app_id = p_gn_info->app_id;
119    p_buf->gn_sec_mask = p_gn_info->sec_mask;
120  }
121
122  if (p_nap_info && (role & BTA_PAN_ROLE_NAP)) {
123    if (p_nap_info->p_srv_name)
124      strlcpy(p_buf->nap_name, p_nap_info->p_srv_name, BTA_SERVICE_NAME_LEN);
125
126    p_buf->nap_app_id = p_nap_info->app_id;
127    p_buf->nap_sec_mask = p_nap_info->sec_mask;
128  }
129
130  bta_sys_sendmsg(p_buf);
131}
132
133/*******************************************************************************
134 *
135 * Function         BTA_PanOpen
136 *
137 * Description      Opens a connection to a peer device.
138 *                  When connection is open callback function is called
139 *                  with a BTA_PAN_OPEN_EVT.
140 *
141 *
142 * Returns          void
143 *
144 ******************************************************************************/
145void BTA_PanOpen(const RawAddress& bd_addr, tBTA_PAN_ROLE local_role,
146                 tBTA_PAN_ROLE peer_role) {
147  tBTA_PAN_API_OPEN* p_buf =
148      (tBTA_PAN_API_OPEN*)osi_malloc(sizeof(tBTA_PAN_API_OPEN));
149
150  p_buf->hdr.event = BTA_PAN_API_OPEN_EVT;
151  p_buf->local_role = local_role;
152  p_buf->peer_role = peer_role;
153  p_buf->bd_addr = bd_addr;
154
155  bta_sys_sendmsg(p_buf);
156}
157
158/*******************************************************************************
159 *
160 * Function         BTA_PanClose
161 *
162 * Description      Close a PAN  connection to a peer device.
163 *
164 *
165 * Returns          void
166 *
167 ******************************************************************************/
168void BTA_PanClose(uint16_t handle) {
169  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
170
171  p_buf->event = BTA_PAN_API_CLOSE_EVT;
172  p_buf->layer_specific = handle;
173
174  bta_sys_sendmsg(p_buf);
175}
176#else
177
178void BTA_PanEnable(UNUSED_ATTR tBTA_PAN_CBACK p_cback) {}
179
180void BTA_PanDisable(void) {}
181
182void BTA_PanSetRole(UNUSED_ATTR tBTA_PAN_ROLE role,
183                    UNUSED_ATTR tBTA_PAN_ROLE_INFO* p_user_info,
184                    UNUSED_ATTR tBTA_PAN_ROLE_INFO* p_gn_info,
185                    UNUSED_ATTR tBTA_PAN_ROLE_INFO* p_nap_info) {}
186
187void BTA_PanOpen(UNUSED_ATTR const RawAddress& bd_addr,
188                 UNUSED_ATTR tBTA_PAN_ROLE local_role,
189                 UNUSED_ATTR tBTA_PAN_ROLE peer_role) {}
190
191void BTA_PanClose(UNUSED_ATTR uint16_t handle) {}
192
193#endif /* BTA_PAN_INCLUDED */
194