1 /******************************************************************************
2  *
3  *  Copyright 1999-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 collection of utility functions used the RFCOMM unit
22  *
23  *****************************************************************************/
24 
25 #define LOG_TAG "rfcomm"
26 
27 #include <bluetooth/log.h>
28 
29 #include <cstdint>
30 
31 #include "internal_include/bt_target.h"
32 #include "os/logging/log_adapter.h"
33 #include "osi/include/allocator.h"
34 #include "stack/include/bt_hdr.h"
35 #include "stack/include/port_ext.h"
36 #include "stack/rfcomm/rfc_int.h"
37 #include "types/raw_address.h"
38 
39 using namespace bluetooth;
40 
41 /*******************************************************************************
42  *
43  * Function         rfc_calc_fcs
44  *
45  * Description      Reversed CRC Table , 8-bit, poly=0x07
46  *                  (GSM 07.10 TS 101 369 V6.3.0)
47  ******************************************************************************/
48 static const uint8_t rfc_crctable[] = {
49     0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED,
50     0x7C, 0x09, 0x98, 0xEA, 0x7B, 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A,
51     0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67, 0x38,
52     0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44,
53     0x31, 0xA0, 0xD2, 0x43, 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0,
54     0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
55 
56     0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D,
57     0x0C, 0x79, 0xE8, 0x9A, 0x0B, 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA,
58     0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17, 0x48,
59     0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34,
60     0x41, 0xD0, 0xA2, 0x33, 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0,
61     0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
62 
63     0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D,
64     0x9C, 0xE9, 0x78, 0x0A, 0x9B, 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A,
65     0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87, 0xD8,
66     0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4,
67     0xD1, 0x40, 0x32, 0xA3, 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20,
68     0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
69 
70     0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D,
71     0xEC, 0x99, 0x08, 0x7A, 0xEB, 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A,
72     0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7, 0xA8,
73     0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4,
74     0xA1, 0x30, 0x42, 0xD3, 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50,
75     0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF};
76 
77 /*******************************************************************************
78  *
79  * Function         rfc_calc_fcs
80  *
81  * Description      This function calculate FCS for the RFCOMM frame
82  *                  (GSM 07.10 TS 101 369 V6.3.0)
83  *
84  * Input            len - number of bytes in the message
85  *                  p   - points to message
86  *
87  ******************************************************************************/
rfc_calc_fcs(uint16_t len,uint8_t * p)88 uint8_t rfc_calc_fcs(uint16_t len, uint8_t* p) {
89   uint8_t fcs = 0xFF;
90 
91   while (len--) {
92     fcs = rfc_crctable[fcs ^ *p++];
93   }
94 
95   /* Ones compliment */
96   return (0xFF - fcs);
97 }
98 
99 /*******************************************************************************
100  *
101  * Function         rfc_check_fcs
102  *
103  * Description      This function checks FCS for the RFCOMM frame
104  *                  (GSM 07.10 TS 101 369 V6.3.0)
105  *
106  * Input            len          - number of bytes in the message
107  *                  p            - points to message
108  *                  received_fcs - received FCS
109  *
110  ******************************************************************************/
rfc_check_fcs(uint16_t len,uint8_t * p,uint8_t received_fcs)111 bool rfc_check_fcs(uint16_t len, uint8_t* p, uint8_t received_fcs) {
112   uint8_t fcs = 0xFF;
113 
114   while (len--) {
115     fcs = rfc_crctable[fcs ^ *p++];
116   }
117 
118   /* Ones compliment */
119   fcs = rfc_crctable[fcs ^ received_fcs];
120 
121   /*0xCF is the reversed order of 11110011.*/
122   return (fcs == 0xCF);
123 }
124 
125 /*******************************************************************************
126  *
127  * Function         rfc_alloc_multiplexer_channel
128  *
129  * Description      This function returns existing or new control block for
130  *                  the address.
131  *
132  ******************************************************************************/
rfc_alloc_multiplexer_channel(const RawAddress & bd_addr,bool is_initiator)133 tRFC_MCB* rfc_alloc_multiplexer_channel(const RawAddress& bd_addr,
134                                         bool is_initiator) {
135   int i, j;
136   tRFC_MCB* p_mcb = NULL;
137   log::verbose("bd_addr:{}, is_initiator:{}", bd_addr, is_initiator);
138 
139   for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
140     log::verbose("rfc_cb.port.rfc_mcb[{}] - state:{}, bd_addr:{}", i,
141                  rfc_cb.port.rfc_mcb[i].state, rfc_cb.port.rfc_mcb[i].bd_addr);
142 
143     if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE) &&
144         rfc_cb.port.rfc_mcb[i].bd_addr == bd_addr) {
145       /* Multiplexer channel found do not change anything */
146       /* If there was an inactivity timer running stop it now */
147       if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED)
148         rfc_timer_stop(&rfc_cb.port.rfc_mcb[i]);
149       log::verbose(
150           "rfc_alloc_multiplexer_channel:is_initiator:{}, found, state:{}, "
151           "p_mcb:{}",
152           is_initiator, rfc_cb.port.rfc_mcb[i].state,
153           fmt::ptr(&rfc_cb.port.rfc_mcb[i]));
154       return (&rfc_cb.port.rfc_mcb[i]);
155     }
156   }
157 
158   /* connection with bd_addr does not exist */
159   for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++) {
160     if (j >= MAX_BD_CONNECTIONS) j = 0;
161 
162     p_mcb = &rfc_cb.port.rfc_mcb[j];
163     if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE) {
164       /* New multiplexer control block */
165       alarm_free(p_mcb->mcb_timer);
166       fixed_queue_free(p_mcb->cmd_q, NULL);
167       memset(p_mcb, 0, sizeof(tRFC_MCB));
168       p_mcb->bd_addr = bd_addr;
169       log::verbose(
170           "rfc_alloc_multiplexer_channel:is_initiator:{}, create new p_mcb:{}, "
171           "index:{}",
172           is_initiator, fmt::ptr(&rfc_cb.port.rfc_mcb[j]), j);
173 
174       p_mcb->mcb_timer = alarm_new("rfcomm_mcb.mcb_timer");
175       p_mcb->cmd_q = fixed_queue_new(SIZE_MAX);
176 
177       p_mcb->is_initiator = is_initiator;
178 
179       rfc_timer_start(p_mcb, RFC_MCB_INIT_INACT_TIMER);
180 
181       rfc_cb.rfc.last_mux = (uint8_t)j;
182       return (p_mcb);
183     }
184   }
185   return (NULL);
186 }
187 
188 /*******************************************************************************
189  *
190  * Function         rfc_release_multiplexer_channel
191  *
192  * Description      Release a multiplexer control block
193  *
194  ******************************************************************************/
rfc_release_multiplexer_channel(tRFC_MCB * p_mcb)195 void rfc_release_multiplexer_channel(tRFC_MCB* p_mcb) {
196   /* Remove the MCB from the mapping table */
197   rfc_save_lcid_mcb(NULL, p_mcb->lcid);
198 
199   /* Remove the MCB from the ports */
200   for (int i = 0; i < MAX_RFC_PORTS; i++) {
201     if (rfc_cb.port.port[i].rfc.p_mcb == p_mcb)
202       rfc_cb.port.port[i].rfc.p_mcb = NULL;
203   }
204 
205   rfc_timer_stop(p_mcb);
206   alarm_free(p_mcb->mcb_timer);
207 
208   fixed_queue_free(p_mcb->cmd_q, osi_free);
209 
210   memset(p_mcb, 0, sizeof(tRFC_MCB));
211   p_mcb->state = RFC_MX_STATE_IDLE;
212 }
213 
214 /*******************************************************************************
215  *
216  * Function         rfc_timer_start
217  *
218  * Description      Start RFC Timer
219  *
220  ******************************************************************************/
rfc_timer_start(tRFC_MCB * p_mcb,uint16_t timeout)221 void rfc_timer_start(tRFC_MCB* p_mcb, uint16_t timeout) {
222   log::verbose("- timeout:{} seconds", timeout);
223 
224   uint64_t interval_ms = timeout * 1000;
225   alarm_set_on_mloop(p_mcb->mcb_timer, interval_ms, rfcomm_mcb_timer_timeout,
226                      p_mcb);
227 }
228 
229 /*******************************************************************************
230  *
231  * Function         rfc_timer_stop
232  *
233  * Description      Stop RFC Timer
234  *
235  ******************************************************************************/
rfc_timer_stop(tRFC_MCB * p_mcb)236 void rfc_timer_stop(tRFC_MCB* p_mcb) {
237   log::verbose("");
238 
239   alarm_cancel(p_mcb->mcb_timer);
240 }
241 
242 /*******************************************************************************
243  *
244  * Function         rfc_port_timer_start
245  *
246  * Description      Start RFC Timer
247  *
248  ******************************************************************************/
rfc_port_timer_start(tPORT * p_port,uint16_t timeout)249 void rfc_port_timer_start(tPORT* p_port, uint16_t timeout) {
250   log::verbose("- timeout:{} seconds", timeout);
251 
252   uint64_t interval_ms = timeout * 1000;
253   alarm_set_on_mloop(p_port->rfc.port_timer, interval_ms,
254                      rfcomm_port_timer_timeout, p_port);
255 }
256 
257 /*******************************************************************************
258  *
259  * Function         rfc_port_timer_stop
260  *
261  * Description      Stop RFC Timer
262  *
263  ******************************************************************************/
rfc_port_timer_stop(tPORT * p_port)264 void rfc_port_timer_stop(tPORT* p_port) {
265   log::verbose("");
266 
267   alarm_cancel(p_port->rfc.port_timer);
268 }
269 
270 /*******************************************************************************
271  *
272  * Function         rfc_check_mcb_active
273  *
274  * Description      Check if there are any opened ports on the MCB if not
275  *                  start MCB Inact timer.
276  *
277  * Returns          void
278  *
279  ******************************************************************************/
rfc_check_mcb_active(tRFC_MCB * p_mcb)280 void rfc_check_mcb_active(tRFC_MCB* p_mcb) {
281   uint16_t i;
282 
283   for (i = 0; i < RFCOMM_MAX_DLCI; i++) {
284     if (p_mcb->port_handles[i] != 0) {
285       p_mcb->is_disc_initiator = false;
286       return;
287     }
288   }
289   /* The last port was DISCed.  On the client side start disconnecting Mx */
290   /* On the server side start inactivity timer */
291   if (p_mcb->is_disc_initiator) {
292     p_mcb->is_disc_initiator = false;
293     rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CLOSE_REQ, NULL);
294   } else
295     rfc_timer_start(p_mcb, RFC_MCB_RELEASE_INACT_TIMER);
296 }
297 
rfcomm_port_timer_timeout(void * data)298 void rfcomm_port_timer_timeout(void* data) {
299   tPORT* p_port = (tPORT*)data;
300 
301   rfc_port_sm_execute(p_port, RFC_PORT_EVENT_TIMEOUT, NULL);
302 }
303 
rfcomm_mcb_timer_timeout(void * data)304 void rfcomm_mcb_timer_timeout(void* data) {
305   tRFC_MCB* p_mcb = (tRFC_MCB*)data;
306 
307   rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_TIMEOUT, NULL);
308 }
309 
310 /*******************************************************************************
311  *
312  * Function         rfc_sec_check_complete
313  *
314  * Description      The function called when Security Manager finishes
315  *                  verification of the service side connection
316  *
317  * Returns          void
318  *
319  ******************************************************************************/
rfc_sec_check_complete(RawAddress,tBT_TRANSPORT,void * p_ref_data,tBTM_STATUS res)320 void rfc_sec_check_complete(RawAddress /* bd_addr */,
321                             tBT_TRANSPORT /* transport */, void* p_ref_data,
322                             tBTM_STATUS res) {
323   log::assert_that(p_ref_data != nullptr,
324                    "assert failed: p_ref_data != nullptr");
325   tPORT* p_port = (tPORT*)p_ref_data;
326 
327   /* Verify that PORT is still waiting for Security to complete */
328   if (!p_port->in_use ||
329       ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK) &&
330        (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK)))
331     return;
332 
333   rfc_port_sm_execute((tPORT*)p_ref_data, RFC_PORT_EVENT_SEC_COMPLETE, &res);
334 }
335 
336 /*******************************************************************************
337  *
338  * Function         rfc_port_closed
339  *
340  * Description      The function is called when port is released based on the
341  *                  event received from the lower layer, typically L2CAP
342  *                  connection down, DISC, or DM frame.
343  *
344  * Returns          void
345  *
346  ******************************************************************************/
rfc_port_closed(tPORT * p_port)347 void rfc_port_closed(tPORT* p_port) {
348   tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
349   rfc_port_timer_stop(p_port);
350   p_port->rfc.state = RFC_STATE_CLOSED;
351 
352   /* If multiplexer channel was up mark it as down */
353   if (p_mcb) {
354     p_mcb->port_handles[p_port->dlci] = 0;
355 
356     /* If there are no more ports opened on this MCB release it */
357     rfc_check_mcb_active(p_mcb);
358   }
359 
360   /* Notify port that RFC connection is gone */
361   port_rfc_closed(p_port, PORT_CLOSED);
362 }
363 
364 /*******************************************************************************
365  *
366  * Function         rfc_inc_credit
367  *
368  * Description      The function is called when a credit is received in a UIH
369  *                  frame.  It increments the TX credit count, and if data
370  *                  flow had halted, it restarts it.
371  *
372  * Returns          void
373  *
374  ******************************************************************************/
rfc_inc_credit(tPORT * p_port,uint8_t credit)375 void rfc_inc_credit(tPORT* p_port, uint8_t credit) {
376   if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
377     p_port->credit_tx += credit;
378 
379     log::verbose("rfc_inc_credit:{}", p_port->credit_tx);
380 
381     if (p_port->tx.peer_fc) PORT_FlowInd(p_port->rfc.p_mcb, p_port->dlci, true);
382   }
383 }
384 
385 /*******************************************************************************
386  *
387  * Function         rfc_dec_credit
388  *
389  * Description      The function is called when a UIH frame of user data is
390  *                  sent.  It decrements the credit count.  If credit count
391  *                  Reaches zero, peer_fc is set.
392  *
393  * Returns          void
394  *
395  ******************************************************************************/
rfc_dec_credit(tPORT * p_port)396 void rfc_dec_credit(tPORT* p_port) {
397   if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
398     if (p_port->credit_tx > 0) p_port->credit_tx--;
399 
400     if (p_port->credit_tx == 0) p_port->tx.peer_fc = true;
401   }
402 }
403 
404 /*******************************************************************************
405  *
406  * Function         rfc_check_send_cmd
407  *
408  * Description      This function is called to send an RFCOMM command message
409  *                  or to handle the RFCOMM command message queue.
410  *
411  * Returns          void
412  *
413  ******************************************************************************/
rfc_check_send_cmd(tRFC_MCB * p_mcb,BT_HDR * p_buf)414 void rfc_check_send_cmd(tRFC_MCB* p_mcb, BT_HDR* p_buf) {
415   /* if passed a buffer queue it */
416   if (p_buf != NULL) {
417     if (p_mcb->cmd_q == NULL) {
418       log::error("empty queue: p_mcb = {} p_mcb->lcid = {} cached p_mcb = {}",
419                  fmt::ptr(p_mcb), p_mcb->lcid,
420                  fmt::ptr(rfc_find_lcid_mcb(p_mcb->lcid)));
421     }
422     fixed_queue_enqueue(p_mcb->cmd_q, p_buf);
423   }
424 
425   /* handle queue if L2CAP not congested */
426   while (!p_mcb->l2cap_congested) {
427     BT_HDR* p = (BT_HDR*)fixed_queue_try_dequeue(p_mcb->cmd_q);
428     if (p == NULL) break;
429     if (L2CA_DataWrite(p_mcb->lcid, p) != L2CAP_DW_SUCCESS) {
430       log::warn("Unable to write L2CAP data peer:{} cid:{} len:{}",
431                 p_mcb->bd_addr, p_mcb->lcid, p->len);
432     }
433   }
434 }
435