OSDN Git Service

Replace BT_HDR => BT_HDR_RIGID
[android-x86/system-bt.git] / bta / pan / bta_pan_act.cc
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 file contains the pan action functions for the state machine.
22  *
23  ******************************************************************************/
24
25 #define LOG_TAG "bluetooth"
26
27 #include <cstdint>
28
29 // PAN_INCLUDED
30 #include "bt_target.h"  // Must be first to define build configuration
31 #if (PAN_INCLUDED == TRUE)
32
33 #include "bta/include/bta_pan_co.h"
34 #include "bta/pan/bta_pan_int.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/fixed_queue.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"  // UNUSED_ATTR
39 #include "stack/include/bt_types.h"
40 #include "stack/include/pan_api.h"
41 #include "types/raw_address.h"
42
43 /* RX and TX data flow mask */
44 #define BTA_PAN_RX_MASK 0x0F
45 #define BTA_PAN_TX_MASK 0xF0
46
47 /*******************************************************************************
48  *
49  * Function    bta_pan_pm_conn_busy
50  *
51  * Description set pan pm connection busy state
52  *
53  * Params      p_scb: state machine control block of pan connection
54  *
55  * Returns     void
56  *
57  ******************************************************************************/
58 static void bta_pan_pm_conn_busy(tBTA_PAN_SCB* p_scb) {
59   if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
60     bta_sys_busy(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
61 }
62
63 /*******************************************************************************
64  *
65  * Function    bta_pan_pm_conn_idle
66  *
67  * Description set pan pm connection idle state
68  *
69  * Params      p_scb: state machine control block of pan connection
70  *
71  * Returns     void
72  *
73  ******************************************************************************/
74 static void bta_pan_pm_conn_idle(tBTA_PAN_SCB* p_scb) {
75   if ((p_scb != NULL) && (p_scb->state != BTA_PAN_IDLE_ST))
76     bta_sys_idle(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
77 }
78
79 /*******************************************************************************
80  *
81  * Function         bta_pan_conn_state_cback
82  *
83  * Description      Connection state callback from Pan profile
84  *
85  *
86  * Returns          void
87  *
88  ******************************************************************************/
89 static void bta_pan_conn_state_cback(uint16_t handle, const RawAddress& bd_addr,
90                                      tPAN_RESULT state, bool is_role_change,
91                                      uint8_t src_role, uint8_t dst_role) {
92   tBTA_PAN_SCB* p_scb;
93   tBTA_PAN_CONN* p_buf = (tBTA_PAN_CONN*)osi_malloc(sizeof(tBTA_PAN_CONN));
94
95   if ((state == PAN_SUCCESS) && !is_role_change) {
96     p_buf->hdr.event = BTA_PAN_CONN_OPEN_EVT;
97     p_scb = bta_pan_scb_by_handle(handle);
98     if (p_scb == NULL) {
99       /* allocate an scb */
100       p_scb = bta_pan_scb_alloc();
101     }
102     /* we have exceeded maximum number of connections */
103     if (!p_scb) {
104       PAN_Disconnect(handle);
105       return;
106     }
107
108     p_scb->handle = handle;
109     p_scb->local_role = src_role;
110     p_scb->peer_role = dst_role;
111     p_scb->pan_flow_enable = true;
112     p_scb->bd_addr = bd_addr;
113     p_scb->data_queue = fixed_queue_new(SIZE_MAX);
114
115     if (src_role == PAN_ROLE_CLIENT)
116       p_scb->app_id = bta_pan_cb.app_id[0];
117     else if (src_role == PAN_ROLE_NAP_SERVER)
118       p_scb->app_id = bta_pan_cb.app_id[2];
119   } else if ((state != PAN_SUCCESS) && !is_role_change) {
120     p_buf->hdr.event = BTA_PAN_CONN_CLOSE_EVT;
121   } else {
122     return;
123   }
124
125   p_buf->result = state;
126   p_buf->hdr.layer_specific = handle;
127
128   bta_sys_sendmsg(p_buf);
129 }
130
131 /*******************************************************************************
132  *
133  * Function         bta_pan_data_flow_cb
134  *
135  * Description      Data flow status callback from PAN
136  *
137  *
138  * Returns          void
139  *
140  ******************************************************************************/
141 static void bta_pan_data_flow_cb(uint16_t handle, tPAN_RESULT result) {
142   tBTA_PAN_SCB* p_scb;
143
144   p_scb = bta_pan_scb_by_handle(handle);
145   if (p_scb == NULL) return;
146
147   if (result == PAN_TX_FLOW_ON) {
148     BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
149     p_buf->layer_specific = handle;
150     p_buf->event = BTA_PAN_BNEP_FLOW_ENABLE_EVT;
151     bta_sys_sendmsg(p_buf);
152     bta_pan_co_rx_flow(handle, p_scb->app_id, true);
153   } else if (result == PAN_TX_FLOW_OFF) {
154     p_scb->pan_flow_enable = false;
155     bta_pan_co_rx_flow(handle, p_scb->app_id, false);
156   }
157 }
158
159 /*******************************************************************************
160  *
161  * Function         bta_pan_data_buf_ind_cback
162  *
163  * Description      data indication callback from pan profile
164  *
165  *
166  * Returns          void
167  *
168  ******************************************************************************/
169 static void bta_pan_data_buf_ind_cback(uint16_t handle, const RawAddress& src,
170                                        const RawAddress& dst, uint16_t protocol,
171                                        BT_HDR* p_buf, bool ext, bool forward) {
172   tBTA_PAN_SCB* p_scb = bta_pan_scb_by_handle(handle);
173   if (p_scb == NULL) {
174     return;
175   }
176
177   if (sizeof(BT_HDR) + sizeof(tBTA_PAN_DATA_PARAMS) + p_buf->len >
178       PAN_BUF_SIZE) {
179     android_errorWriteLog(0x534e4554, "63146237");
180     APPL_TRACE_ERROR("%s: received buffer length too large: %d", __func__,
181                      p_buf->len);
182     return;
183   }
184
185   BT_HDR* p_new_buf = (BT_HDR*)osi_malloc(PAN_BUF_SIZE);
186   memcpy((uint8_t*)(p_new_buf + 1) + sizeof(tBTA_PAN_DATA_PARAMS),
187          (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
188   p_new_buf->len = p_buf->len;
189   p_new_buf->offset = sizeof(tBTA_PAN_DATA_PARAMS);
190
191   /* copy params into the space before the data */
192   ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->src = src;
193   ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->dst = dst;
194   ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->protocol = protocol;
195   ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->ext = ext;
196   ((tBTA_PAN_DATA_PARAMS*)p_new_buf)->forward = forward;
197
198   fixed_queue_enqueue(p_scb->data_queue, p_new_buf);
199   BT_HDR_RIGID* p_event = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
200   p_event->layer_specific = handle;
201   p_event->event = BTA_PAN_RX_FROM_BNEP_READY_EVT;
202   bta_sys_sendmsg(p_event);
203 }
204
205 /*******************************************************************************
206  *
207  * Function         bta_pan_pfilt_ind_cback
208  *
209  * Description
210  *
211  *
212  * Returns          void
213  *
214  ******************************************************************************/
215 static void bta_pan_pfilt_ind_cback(uint16_t handle, bool indication,
216                                     tBNEP_RESULT result, uint16_t num_filters,
217                                     uint8_t* p_filters) {
218   bta_pan_co_pfilt_ind(
219       handle, indication,
220       (tBTA_PAN_STATUS)((result == BNEP_SUCCESS) ? BTA_PAN_SUCCESS
221                                                  : BTA_PAN_FAIL),
222       num_filters, p_filters);
223 }
224
225 /*******************************************************************************
226  *
227  * Function         bta_pan_mfilt_ind_cback
228  *
229  * Description
230  *
231  *
232  * Returns          void
233  *
234  ******************************************************************************/
235 static void bta_pan_mfilt_ind_cback(uint16_t handle, bool indication,
236                                     tBNEP_RESULT result, uint16_t num_mfilters,
237                                     uint8_t* p_mfilters) {
238   bta_pan_co_mfilt_ind(
239       handle, indication,
240       (tBTA_PAN_STATUS)((result == BNEP_SUCCESS) ? BTA_PAN_SUCCESS
241                                                  : BTA_PAN_FAIL),
242       num_mfilters, p_mfilters);
243 }
244
245 /*******************************************************************************
246  *
247  * Function         bta_pan_has_multiple_connections
248  *
249  * Description      Check whether there are multiple GN/NAP connections to
250  *                  different devices
251  *
252  *
253  * Returns          bool
254  *
255  ******************************************************************************/
256 static bool bta_pan_has_multiple_connections(uint8_t app_id) {
257   tBTA_PAN_SCB* p_scb = NULL;
258   bool found = false;
259   RawAddress bd_addr;
260
261   for (uint8_t index = 0; index < BTA_PAN_NUM_CONN; index++) {
262     p_scb = &bta_pan_cb.scb[index];
263     if (p_scb->in_use && app_id == p_scb->app_id) {
264       /* save temp bd_addr */
265       bd_addr = p_scb->bd_addr;
266       found = true;
267       break;
268     }
269   }
270
271   /* If cannot find a match then there is no connection at all */
272   if (!found) return false;
273
274   /* Find whether there is another connection with different device other than
275      PANU.
276       Could be same service or different service */
277   for (uint8_t index = 0; index < BTA_PAN_NUM_CONN; index++) {
278     p_scb = &bta_pan_cb.scb[index];
279     if (p_scb->in_use && p_scb->app_id != bta_pan_cb.app_id[0] &&
280         bd_addr != p_scb->bd_addr) {
281       return true;
282     }
283   }
284   return false;
285 }
286
287 /*******************************************************************************
288  *
289  * Function         bta_pan_enable
290  *
291  * Description
292  *
293  *
294  *
295  * Returns          void
296  *
297  ******************************************************************************/
298 void bta_pan_enable(tBTA_PAN_DATA* p_data) {
299   tPAN_REGISTER reg_data;
300
301   bta_pan_cb.p_cback = p_data->api_enable.p_cback;
302
303   reg_data.pan_conn_state_cb = bta_pan_conn_state_cback;
304   reg_data.pan_bridge_req_cb = NULL;
305   reg_data.pan_data_buf_ind_cb = bta_pan_data_buf_ind_cback;
306   reg_data.pan_data_ind_cb = NULL;
307   reg_data.pan_pfilt_ind_cb = bta_pan_pfilt_ind_cback;
308   reg_data.pan_mfilt_ind_cb = bta_pan_mfilt_ind_cback;
309   reg_data.pan_tx_data_flow_cb = bta_pan_data_flow_cb;
310
311   PAN_Register(&reg_data);
312
313   bta_pan_cb.flow_mask = bta_pan_co_init(&bta_pan_cb.q_level);
314   bta_pan_cb.p_cback(BTA_PAN_ENABLE_EVT, NULL);
315 }
316
317 /*******************************************************************************
318  *
319  * Function         bta_pan_set_role
320  *
321  * Description
322  *
323  * Returns          void
324  *
325  ******************************************************************************/
326 void bta_pan_set_role(tBTA_PAN_DATA* p_data) {
327   tPAN_RESULT status;
328   tBTA_PAN bta_pan;
329
330   bta_pan_cb.app_id[0] = p_data->api_set_role.user_app_id;
331   bta_pan_cb.app_id[2] = p_data->api_set_role.nap_app_id;
332
333   /* set security correctly in api and here */
334   status =
335       PAN_SetRole(p_data->api_set_role.role, p_data->api_set_role.user_name,
336                   p_data->api_set_role.nap_name);
337
338   bta_pan.set_role.role = p_data->api_set_role.role;
339   if (status == PAN_SUCCESS) {
340     if (p_data->api_set_role.role & PAN_ROLE_NAP_SERVER)
341       bta_sys_add_uuid(UUID_SERVCLASS_NAP);
342     else
343       bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
344
345     if (p_data->api_set_role.role & PAN_ROLE_CLIENT)
346       bta_sys_add_uuid(UUID_SERVCLASS_PANU);
347     else
348       bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
349
350     bta_pan.set_role.status = BTA_PAN_SUCCESS;
351   }
352   /* if status is not success clear everything */
353   else {
354     PAN_SetRole(0, NULL, NULL);
355     bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
356     bta_sys_remove_uuid(UUID_SERVCLASS_GN);
357     bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
358     bta_pan.set_role.status = BTA_PAN_FAIL;
359   }
360   bta_pan_cb.p_cback(BTA_PAN_SET_ROLE_EVT, &bta_pan);
361 }
362
363 /*******************************************************************************
364  *
365  * Function         bta_pan_disable
366  *
367  * Description
368  *
369  *
370  *
371  * Returns          void
372  *
373  ******************************************************************************/
374 void bta_pan_disable(void) {
375   BT_HDR* p_buf;
376   tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
377   uint8_t i;
378
379   /* close all connections */
380   PAN_SetRole(0, NULL, NULL);
381
382 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
383   bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
384   bta_sys_remove_uuid(UUID_SERVCLASS_GN);
385   bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
386 #endif  // BTA_EIR_CANNED_UUID_LIST
387   /* free all queued up data buffers */
388   for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
389     if (p_scb->in_use) {
390       while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue)) !=
391              NULL)
392         osi_free(p_buf);
393
394       bta_pan_co_close(p_scb->handle, p_scb->app_id);
395     }
396   }
397
398   PAN_Deregister();
399 }
400
401 /*******************************************************************************
402  *
403  * Function         bta_pan_open
404  *
405  * Description
406  *
407  * Returns          void
408  *
409  ******************************************************************************/
410 void bta_pan_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
411   tPAN_RESULT status;
412   tBTA_PAN bta_pan;
413
414   status = PAN_Connect(p_data->api_open.bd_addr, p_data->api_open.local_role,
415                        p_data->api_open.peer_role, &p_scb->handle);
416   APPL_TRACE_DEBUG("%s pan connect status: %d", __func__, status);
417
418   if (status == PAN_SUCCESS) {
419     p_scb->bd_addr = p_data->api_open.bd_addr;
420     p_scb->local_role = p_data->api_open.local_role;
421     p_scb->peer_role = p_data->api_open.peer_role;
422     bta_pan.opening.bd_addr = p_data->api_open.bd_addr;
423     bta_pan.opening.handle = p_scb->handle;
424     bta_pan_cb.p_cback(BTA_PAN_OPENING_EVT, &bta_pan);
425
426   } else {
427     bta_pan_scb_dealloc(p_scb);
428     bta_pan.open.bd_addr = p_data->api_open.bd_addr;
429     bta_pan.open.status = BTA_PAN_FAIL;
430     bta_pan.open.local_role = p_data->api_open.local_role;
431     bta_pan.open.peer_role = p_data->api_open.peer_role;
432     bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
433   }
434 }
435
436 /*******************************************************************************
437  *
438  * Function         bta_pan_close
439  *
440  * Description
441  *
442  *
443  *
444  * Returns          void
445  *
446  ******************************************************************************/
447 void bta_pan_api_close(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
448   tBTA_PAN_CONN* p_buf = (tBTA_PAN_CONN*)osi_malloc(sizeof(tBTA_PAN_CONN));
449
450   PAN_Disconnect(p_scb->handle);
451
452   /*
453    * Send an event to BTA so that application will get the connection
454    * close event.
455    */
456   p_buf->hdr.event = BTA_PAN_CONN_CLOSE_EVT;
457   p_buf->hdr.layer_specific = p_scb->handle;
458
459   bta_sys_sendmsg(p_buf);
460 }
461
462 /*******************************************************************************
463  *
464  * Function         bta_pan_conn_open
465  *
466  * Description      process connection open event
467  *
468  * Returns          void
469  *
470  ******************************************************************************/
471 void bta_pan_conn_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
472   tBTA_PAN bta_pan;
473
474   APPL_TRACE_DEBUG("%s pan connection result: %d", __func__,
475                    p_data->conn.result);
476
477   bta_pan.open.bd_addr = p_scb->bd_addr;
478   bta_pan.open.handle = p_scb->handle;
479   bta_pan.open.local_role = p_scb->local_role;
480   bta_pan.open.peer_role = p_scb->peer_role;
481
482   if (p_data->conn.result == PAN_SUCCESS) {
483     bta_pan.open.status = BTA_PAN_SUCCESS;
484     p_scb->pan_flow_enable = true;
485     p_scb->app_flow_enable = true;
486     bta_sys_conn_open(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
487   } else {
488     bta_pan_scb_dealloc(p_scb);
489     bta_pan.open.status = BTA_PAN_FAIL;
490   }
491
492   p_scb->pan_flow_enable = true;
493   p_scb->app_flow_enable = true;
494
495   /* If app_id is NAP/GN, check whether there are multiple connections.
496      If there are, provide a special app_id to dm to enforce central role only.
497      */
498   if (p_scb->app_id == bta_pan_cb.app_id[2] &&
499       bta_pan_has_multiple_connections(p_scb->app_id)) {
500     p_scb->app_id = BTA_APP_ID_PAN_MULTI;
501   }
502
503   bta_sys_conn_open(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
504   bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
505 }
506
507 /*******************************************************************************
508  *
509  * Function         bta_pan_conn_close
510  *
511  * Description      process connection close event
512  *
513  *
514  *
515  * Returns          void
516  *
517  ******************************************************************************/
518 void bta_pan_conn_close(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
519   tBTA_PAN bta_pan;
520   BT_HDR* p_buf;
521
522   bta_pan.close.handle = p_data->hdr.layer_specific;
523
524   bta_sys_conn_close(BTA_ID_PAN, p_scb->app_id, p_scb->bd_addr);
525
526   /* free all queued up data buffers */
527   while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue)) != NULL)
528     osi_free(p_buf);
529
530   bta_pan_scb_dealloc(p_scb);
531
532   bta_pan_cb.p_cback(BTA_PAN_CLOSE_EVT, &bta_pan);
533 }
534
535 /*******************************************************************************
536  *
537  * Function         bta_pan_rx_path
538  *
539  * Description      Handle data on the RX path (data sent from the phone to
540  *                  BTA).
541  *
542  *
543  * Returns          void
544  *
545  ******************************************************************************/
546 void bta_pan_rx_path(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
547   /* if data path configured for rx pull */
548   if ((bta_pan_cb.flow_mask & BTA_PAN_RX_MASK) == BTA_PAN_RX_PULL) {
549     /* if we can accept data */
550     if (p_scb->pan_flow_enable) {
551       /* call application callout function for rx path */
552       bta_pan_co_rx_path(p_scb->handle, p_scb->app_id);
553     }
554   }
555   /* else data path configured for rx push */
556   else {
557   }
558 }
559
560 /*******************************************************************************
561  *
562  * Function         bta_pan_tx_path
563  *
564  * Description      Handle the TX data path (data sent from BTA to the phone).
565  *
566  *
567  * Returns          void
568  *
569  ******************************************************************************/
570 void bta_pan_tx_path(tBTA_PAN_SCB* p_scb, UNUSED_ATTR tBTA_PAN_DATA* p_data) {
571   bta_pan_pm_conn_busy(p_scb);
572   /* call application callout function for tx path */
573   bta_pan_co_tx_path(p_scb->handle, p_scb->app_id);
574
575   /* free data that exceeds queue level */
576   while (fixed_queue_length(p_scb->data_queue) > bta_pan_cb.q_level) {
577     BT_HDR* p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_scb->data_queue);
578     if (p_buf != nullptr) {
579       osi_free(p_buf);
580     }
581   }
582
583   bta_pan_pm_conn_idle(p_scb);
584 }
585
586 /*******************************************************************************
587  *
588  * Function         bta_pan_tx_flow
589  *
590  * Description      Set the application flow control state.
591  *
592  *
593  * Returns          void
594  *
595  ******************************************************************************/
596 void bta_pan_tx_flow(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
597   p_scb->app_flow_enable = p_data->ci_tx_flow.enable;
598 }
599
600 /*******************************************************************************
601  *
602  * Function         bta_pan_write_buf
603  *
604  * Description      Handle a bta_pan_ci_rx_writebuf() and send data to PAN.
605  *
606  *
607  * Returns          void
608  *
609  ******************************************************************************/
610 void bta_pan_write_buf(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
611   if ((bta_pan_cb.flow_mask & BTA_PAN_RX_MASK) == BTA_PAN_RX_PUSH_BUF) {
612     bta_pan_pm_conn_busy(p_scb);
613
614     PAN_WriteBuf(p_scb->handle, ((tBTA_PAN_DATA_PARAMS*)p_data)->dst,
615                  ((tBTA_PAN_DATA_PARAMS*)p_data)->src,
616                  ((tBTA_PAN_DATA_PARAMS*)p_data)->protocol, (BT_HDR*)p_data,
617                  ((tBTA_PAN_DATA_PARAMS*)p_data)->ext);
618     bta_pan_pm_conn_idle(p_scb);
619   }
620 }
621
622 /*******************************************************************************
623  *
624  * Function         bta_pan_free_buf
625  *
626  * Description      Frees the data buffer during closing state
627  *
628  *
629  * Returns          void
630  *
631  ******************************************************************************/
632 void bta_pan_free_buf(UNUSED_ATTR tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data) {
633   osi_free(p_data);
634 }
635
636 #endif /* PAN_INCLUDED */