summaryrefslogtreecommitdiff
path: root/stack/pan/pan_utils.cc
blob: b2eed2dede40e73eba8cd81e3b1eeca6e5b0eda3 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/******************************************************************************
 *
 *  Copyright (C) 1999-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/*****************************************************************************
 *
 *  This file contains main functions to support PAN profile
 *  commands and events.
 *
 *****************************************************************************/

#include <base/logging.h>
#include <stdio.h>
#include <string.h>
#include "bnep_api.h"
#include "bt_common.h"
#include "btm_api.h"
#include "hcidefs.h"
#include "l2c_api.h"
#include "pan_api.h"
#include "pan_int.h"
#include "sdp_api.h"
#include "sdpdefs.h"

static const uint8_t pan_proto_elem_data[] = {
    0x35, 0x18,       /* data element sequence of length 0x18 bytes */
    0x35, 0x06,       /* data element sequence for L2CAP descriptor */
    0x19, 0x01, 0x00, /* UUID for L2CAP - 0x0100 */
    0x09, 0x00, 0x0F, /* PSM for BNEP - 0x000F */
    0x35, 0x0E,       /* data element seqence for BNEP descriptor */
    0x19, 0x00, 0x0F, /* UUID for BNEP - 0x000F */
    0x09, 0x01,
    0x00, /* BNEP specific parameter 0 -- Version of BNEP = version 1 = 0x0001
           */
    0x35,
    0x06, /* BNEP specific parameter 1 -- Supported network packet type list */
    0x09, 0x08, 0x00, /* network packet type IPv4 = 0x0800 */
    0x09, 0x08, 0x06  /* network packet type ARP  = 0x0806 */
};

/*******************************************************************************
 *
 * Function         pan_register_with_sdp
 *
 * Description
 *
 * Returns
 *
 ******************************************************************************/
uint32_t pan_register_with_sdp(uint16_t uuid, uint8_t sec_mask,
                               const char* p_name, const char* p_desc) {
  uint32_t sdp_handle;
  uint16_t browse_list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  uint16_t security = 0;
  uint32_t proto_len = (uint32_t)pan_proto_elem_data[1];

  /* Create a record */
  sdp_handle = SDP_CreateRecord();

  if (sdp_handle == 0) {
    PAN_TRACE_ERROR("PAN_SetRole - could not create SDP record");
    return 0;
  }

  /* Service Class ID List */
  SDP_AddServiceClassIdList(sdp_handle, 1, &uuid);

  /* Add protocol element sequence from the constant string */
  SDP_AddAttribute(sdp_handle, ATTR_ID_PROTOCOL_DESC_LIST,
                   DATA_ELE_SEQ_DESC_TYPE, proto_len,
                   (uint8_t*)(pan_proto_elem_data + 2));

  /* Language base */
  SDP_AddLanguageBaseAttrIDList(sdp_handle, LANG_ID_CODE_ENGLISH,
                                LANG_ID_CHAR_ENCODE_UTF8, LANGUAGE_BASE_ID);

  /* Profile descriptor list */
  SDP_AddProfileDescriptorList(sdp_handle, uuid, PAN_PROFILE_VERSION);

  /* Service Name */
  SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
                   (uint8_t)(strlen(p_name) + 1), (uint8_t*)p_name);

  /* Service description */
  SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_DESCRIPTION, TEXT_STR_DESC_TYPE,
                   (uint8_t)(strlen(p_desc) + 1), (uint8_t*)p_desc);

  /* Security description */
  if (sec_mask) {
    UINT16_TO_BE_FIELD(&security, 0x0001);
  }
  SDP_AddAttribute(sdp_handle, ATTR_ID_SECURITY_DESCRIPTION, UINT_DESC_TYPE, 2,
                   (uint8_t*)&security);

#if (PAN_SUPPORTS_ROLE_NAP == TRUE)
  if (uuid == UUID_SERVCLASS_NAP) {
    uint16_t NetAccessType = 0x0005;      /* Ethernet */
    uint32_t NetAccessRate = 0x0001312D0; /* 10Mb/sec */
    uint8_t array[10], *p;

    /* Net access type. */
    p = array;
    UINT16_TO_BE_STREAM(p, NetAccessType);
    SDP_AddAttribute(sdp_handle, ATTR_ID_NET_ACCESS_TYPE, UINT_DESC_TYPE, 2,
                     array);

    /* Net access rate. */
    p = array;
    UINT32_TO_BE_STREAM(p, NetAccessRate);
    SDP_AddAttribute(sdp_handle, ATTR_ID_MAX_NET_ACCESS_RATE, UINT_DESC_TYPE, 4,
                     array);

    /* Register with Security Manager for the specific security level */
    if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_NAP, sec_mask,
                               BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_NAP)) ||
        (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_NAP,
                               sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_NAP))) {
      PAN_TRACE_ERROR("PAN Security Registration failed for PANU");
    }
  }
#endif
#if (PAN_SUPPORTS_ROLE_GN == TRUE)
  if (uuid == UUID_SERVCLASS_GN) {
    if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_GN, sec_mask,
                               BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_GN)) ||
        (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_GN, sec_mask,
                               BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_GN))) {
      PAN_TRACE_ERROR("PAN Security Registration failed for GN");
    }
  }
#endif
#if (PAN_SUPPORTS_ROLE_PANU == TRUE)
  if (uuid == UUID_SERVCLASS_PANU) {
    if ((!BTM_SetSecurityLevel(true, p_name, BTM_SEC_SERVICE_BNEP_PANU,
                               sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_PANU)) ||
        (!BTM_SetSecurityLevel(false, p_name, BTM_SEC_SERVICE_BNEP_PANU,
                               sec_mask, BT_PSM_BNEP, BTM_SEC_PROTO_BNEP,
                               UUID_SERVCLASS_PANU))) {
      PAN_TRACE_ERROR("PAN Security Registration failed for PANU");
    }
  }
#endif

  /* Make the service browsable */
  SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse_list);

  return sdp_handle;
}

/*******************************************************************************
 *
 * Function         pan_allocate_pcb
 *
 * Description
 *
 * Returns
 *
 ******************************************************************************/
tPAN_CONN* pan_allocate_pcb(const RawAddress& p_bda, uint16_t handle) {
  uint16_t i;

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
        pan_cb.pcb[i].handle == handle)
      return NULL;
  }

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
        pan_cb.pcb[i].rem_bda == p_bda)
      return NULL;
  }

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state == PAN_STATE_IDLE) {
      memset(&(pan_cb.pcb[i]), 0, sizeof(tPAN_CONN));
      pan_cb.pcb[i].rem_bda = p_bda;
      pan_cb.pcb[i].handle = handle;
      return &(pan_cb.pcb[i]);
    }
  }
  return NULL;
}

/*******************************************************************************
 *
 * Function         pan_get_pcb_by_handle
 *
 * Description
 *
 * Returns
 *
 ******************************************************************************/
tPAN_CONN* pan_get_pcb_by_handle(uint16_t handle) {
  uint16_t i;

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE &&
        pan_cb.pcb[i].handle == handle)
      return &(pan_cb.pcb[i]);
  }

  return NULL;
}

/*******************************************************************************
 *
 * Function         pan_get_pcb_by_addr
 *
 * Description
 *
 * Returns
 *
 ******************************************************************************/
tPAN_CONN* pan_get_pcb_by_addr(const RawAddress& p_bda) {
  uint16_t i;

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state == PAN_STATE_IDLE) continue;

    if (pan_cb.pcb[i].rem_bda == p_bda) return &(pan_cb.pcb[i]);

    /*
    if (pan_cb.pcb[i].mfilter_present &&
        p_bda == pan_cb.pcb[i].multi_cast_bridge)
        return &(pan_cb.pcb[i]);
    */
  }

  return NULL;
}

/*******************************************************************************
 *
 * Function         pan_close_all_connections
 *
 * Description
 *
 * Returns          void
 *
 ******************************************************************************/
void pan_close_all_connections(void) {
  uint16_t i;

  for (i = 0; i < MAX_PAN_CONNS; i++) {
    if (pan_cb.pcb[i].con_state != PAN_STATE_IDLE) {
      BNEP_Disconnect(pan_cb.pcb[i].handle);
      pan_cb.pcb[i].con_state = PAN_STATE_IDLE;
    }
  }

  pan_cb.active_role = PAN_ROLE_INACTIVE;
  pan_cb.num_conns = 0;
  return;
}

/*******************************************************************************
 *
 * Function         pan_release_pcb
 *
 * Description      This function releases a PCB.
 *
 * Returns          void
 *
 ******************************************************************************/
void pan_release_pcb(tPAN_CONN* p_pcb) {
  /* Drop any response pointer we may be holding */
  memset(p_pcb, 0, sizeof(tPAN_CONN));
  p_pcb->con_state = PAN_STATE_IDLE;
}

/*******************************************************************************
 *
 * Function         pan_dump_status
 *
 * Description      This function dumps the pan control block and connection
 *                  blocks information
 *
 * Returns          none
 *
 ******************************************************************************/
void pan_dump_status(void) {
#if (PAN_SUPPORTS_DEBUG_DUMP == TRUE)
  uint16_t i;
  tPAN_CONN* p_pcb;

  PAN_TRACE_DEBUG("PAN role %x, active role %d, num_conns %d", pan_cb.role,
                  pan_cb.active_role, pan_cb.num_conns);

  for (i = 0, p_pcb = pan_cb.pcb; i < MAX_PAN_CONNS; i++, p_pcb++) {
    VLOG(1) << +i << " state:" << p_pcb->con_state
            << ", handle:" << p_pcb->handle << ", src" << p_pcb->src_uuid
            << ", BD:" << p_pcb->rem_bda;
  }
#endif
}