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 private interface file for the BTA data gateway.
22  *
23  ******************************************************************************/
24 #ifndef BTA_PAN_INT_H
25 #define BTA_PAN_INT_H
26 
27 #include <cstdint>
28 
29 #include "bta/include/bta_api.h"
30 #include "bta/include/bta_sec_api.h"
31 #include "bta/include/bta_pan_api.h"
32 #include "bta/sys/bta_sys.h"
33 #include "osi/include/fixed_queue.h"
34 #include "stack/include/bt_hdr.h"
35 #include "stack/include/pan_api.h"
36 #include "types/raw_address.h"
37 
38 /*****************************************************************************
39  *  Constants
40  ****************************************************************************/
41 
42 /* PAN events */
43 enum {
44   /* these events are handled by the state machine */
45   BTA_PAN_API_CLOSE_EVT = BTA_SYS_EVT_START(BTA_ID_PAN),
46   BTA_PAN_CI_TX_READY_EVT,
47   BTA_PAN_CI_RX_READY_EVT,
48   BTA_PAN_CI_TX_FLOW_EVT,
49   BTA_PAN_CI_RX_WRITE_EVT,
50   BTA_PAN_CI_RX_WRITEBUF_EVT,
51   BTA_PAN_CONN_OPEN_EVT,
52   BTA_PAN_CONN_CLOSE_EVT,
53   BTA_PAN_BNEP_FLOW_ENABLE_EVT,
54   BTA_PAN_RX_FROM_BNEP_READY_EVT,
55 
56   /* these events are handled outside of the state machine */
57   BTA_PAN_API_ENABLE_EVT,
58   BTA_PAN_API_DISABLE_EVT,
59   BTA_PAN_API_SET_ROLE_EVT,
60   BTA_PAN_API_OPEN_EVT
61 };
62 
63 /* state machine states */
64 enum { BTA_PAN_IDLE_ST, BTA_PAN_OPEN_ST, BTA_PAN_CLOSING_ST };
65 
66 /*****************************************************************************
67  *  Data types
68  ****************************************************************************/
69 
70 /* data type for BTA_PAN_API_ENABLE_EVT */
71 typedef struct {
72   BT_HDR_RIGID hdr;        /* Event header */
73   tBTA_PAN_CBACK* p_cback; /* PAN callback function */
74 } tBTA_PAN_API_ENABLE;
75 
76 /* data type for BTA_PAN_API_REG_ROLE_EVT */
77 typedef struct {
78   BT_HDR_RIGID hdr;                         /* Event header */
79   char user_name[BTA_SERVICE_NAME_LEN + 1]; /* Service name */
80   char nap_name[BTA_SERVICE_NAME_LEN + 1];  /* Service name */
81   tBTA_PAN_ROLE role;
82   uint8_t user_app_id;
83   uint8_t nap_app_id;
84 } tBTA_PAN_API_SET_ROLE;
85 
86 /* data type for BTA_PAN_API_OPEN_EVT */
87 typedef struct {
88   BT_HDR_RIGID hdr;         /* Event header */
89   tBTA_PAN_ROLE local_role; /* local role */
90   tBTA_PAN_ROLE peer_role;  /* peer role */
91   RawAddress bd_addr;       /* peer bdaddr */
92 } tBTA_PAN_API_OPEN;
93 
94 /* data type for BTA_PAN_CI_TX_FLOW_EVT */
95 typedef struct {
96   BT_HDR_RIGID hdr; /* Event header */
97   bool enable; /* Flow control setting */
98 } tBTA_PAN_CI_TX_FLOW;
99 
100 /* data type for BTA_PAN_CONN_OPEN_EVT */
101 typedef struct {
102   BT_HDR_RIGID hdr; /* Event header */
103   tPAN_RESULT result;
104 
105 } tBTA_PAN_CONN;
106 
107 /* pan data param */
108 typedef struct {
109   BT_HDR_RIGID hdr;
110   RawAddress src;
111   RawAddress dst;
112   uint16_t protocol;
113   bool ext;
114   bool forward;
115 
116 } tBTA_PAN_DATA_PARAMS;
117 
118 /* union of all data types */
119 typedef union {
120   BT_HDR_RIGID hdr;
121   tBTA_PAN_API_ENABLE api_enable;
122   tBTA_PAN_API_SET_ROLE api_set_role;
123   tBTA_PAN_API_OPEN api_open;
124   tBTA_PAN_CI_TX_FLOW ci_tx_flow;
125   tBTA_PAN_CONN conn;
126   tBTA_PAN_DATA_PARAMS params;
127 } tBTA_PAN_DATA;
128 
129 /* state machine control block */
130 typedef struct {
131   RawAddress bd_addr; /* peer bdaddr */
132   fixed_queue_t*
133       data_queue;    /* Queue of buffers waiting to be passed to application */
134   uint16_t handle;   /* BTA PAN/BNEP handle */
135   bool in_use;       /* scb in use */
136   tBTA_SEC sec_mask; /* Security mask */
137   bool pan_flow_enable;     /* BNEP flow control state */
138   bool app_flow_enable;     /* Application flow control state */
139   uint8_t state;            /* State machine state */
140   tBTA_PAN_ROLE local_role; /* local role */
141   tBTA_PAN_ROLE peer_role;  /* peer role */
142   uint8_t app_id;           /* application id for the connection */
143 
144 } tBTA_PAN_SCB;
145 
146 /* main control block */
147 typedef struct {
148   tBTA_PAN_SCB scb[BTA_PAN_NUM_CONN]; /* state machine control blocks */
149   tBTA_PAN_CBACK* p_cback;            /* PAN callback function */
150   uint8_t app_id[3];                  /* application id for PAN roles */
151   uint8_t flow_mask;                  /* Data flow mask */
152   uint8_t q_level; /* queue level set by application for TX data */
153 
154 } tBTA_PAN_CB;
155 
156 /*****************************************************************************
157  *  Global data
158  ****************************************************************************/
159 
160 /* PAN control block */
161 extern tBTA_PAN_CB bta_pan_cb;
162 
163 /*****************************************************************************
164  *  Function prototypes
165  ****************************************************************************/
166 tBTA_PAN_SCB* bta_pan_scb_alloc(void);
167 void bta_pan_scb_dealloc(tBTA_PAN_SCB* p_scb);
168 uint8_t bta_pan_scb_to_idx(tBTA_PAN_SCB* p_scb);
169 tBTA_PAN_SCB* bta_pan_scb_by_handle(uint16_t handle);
170 bool bta_pan_hdl_event(const BT_HDR_RIGID* p_msg);
171 
172 /* action functions */
173 void bta_pan_enable(tBTA_PAN_DATA* p_data);
174 void bta_pan_disable(void);
175 void bta_pan_set_role(tBTA_PAN_DATA* p_data);
176 void bta_pan_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
177 void bta_pan_api_close(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
178 void bta_pan_rx_path(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
179 void bta_pan_tx_path(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
180 void bta_pan_tx_flow(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
181 void bta_pan_conn_open(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
182 void bta_pan_conn_close(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
183 void bta_pan_write_buf(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
184 void bta_pan_free_buf(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
185 
186 #endif /* BTA_PAN_INT_H */
187