summaryrefslogtreecommitdiff
path: root/system/stack/rfcomm/port_utils.cc
blob: 6ee02fc31754124e6ba28daafa82c6f46985c385 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/******************************************************************************
 *
 *  Copyright 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.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  Port Emulation entity utilities
 *
 ******************************************************************************/

#include <base/logging.h>

#include <cstdint>
#include <cstring>

#include "bt_target.h"
#include "osi/include/allocator.h"
#include "osi/include/mutex.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/btm_client_interface.h"
#include "stack/rfcomm/port_int.h"
#include "stack/rfcomm/rfc_int.h"
#include "types/raw_address.h"

static const tPORT_STATE default_port_pars = {
    PORT_BAUD_RATE_9600,
    PORT_8_BITS,
    PORT_ONESTOPBIT,
    PORT_PARITY_NO,
    PORT_ODD_PARITY,
    PORT_FC_OFF,
    0, /* No rx_char */
    PORT_XON_DC1,
    PORT_XOFF_DC3,
};

/*******************************************************************************
 *
 * Function         port_allocate_port
 *
 * Description      Look through the Port Control Blocks for a free one.  Note
 *                  that one server can open several ports with the same SCN
 *                  if it can support simulteneous requests from different
 *                  clients.
 *
 * Returns          Pointer to the PORT or NULL if not found
 *
 ******************************************************************************/
tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr) {
  uint8_t port_index = rfc_cb.rfc.last_port_index + static_cast<uint8_t>(1);
  // Loop at most MAX_RFC_PORTS items
  for (int loop_counter = 0; loop_counter < MAX_RFC_PORTS;
       loop_counter++, port_index++) {
    if (port_index >= MAX_RFC_PORTS) {
      port_index = 0;
    }
    tPORT* p_port = &rfc_cb.port.port[port_index];
    if (!p_port->in_use) {
      // Assume that we already called port_release_port on this
      memset(p_port, 0, sizeof(tPORT));
      p_port->in_use = true;
      // handle is a port handle starting from 1
      p_port->handle = port_index + static_cast<uint8_t>(1);
      // During the open set default state for the port connection
      port_set_defaults(p_port);
      p_port->rfc.port_timer = alarm_new("rfcomm_port.port_timer");
      p_port->dlci = dlci;
      p_port->bd_addr = bd_addr;
      rfc_cb.rfc.last_port_index = port_index;
      RFCOMM_TRACE_DEBUG(
          "%s: rfc_cb.port.port[%d]:%p chosen, "
          "last_port_index:%d, bd_addr=%s",
          __func__, port_index, p_port, rfc_cb.rfc.last_port_index,
          bd_addr.ToString().c_str());
      return p_port;
    }
  }
  LOG(WARNING) << __func__ << ": running out of free ports for dlci "
               << std::to_string(dlci) << ", bd_addr " << bd_addr;
  return nullptr;
}

/*******************************************************************************
 *
 * Function         port_set_defaults
 *
 * Description      Set defualt port parameters
 *
 *
 ******************************************************************************/
void port_set_defaults(tPORT* p_port) {
  p_port->ev_mask = 0;
  p_port->p_callback = nullptr;
  p_port->port_ctrl = 0;
  p_port->error = 0;
  p_port->line_status = 0;
  p_port->rx_flag_ev_pending = false;
  p_port->peer_mtu = RFCOMM_DEFAULT_MTU;

  p_port->user_port_pars = default_port_pars;
  p_port->peer_port_pars = default_port_pars;

  p_port->credit_tx = 0;
  p_port->credit_rx = 0;

  memset(&p_port->local_ctrl, 0, sizeof(p_port->local_ctrl));
  memset(&p_port->peer_ctrl, 0, sizeof(p_port->peer_ctrl));
  memset(&p_port->rx, 0, sizeof(p_port->rx));
  memset(&p_port->tx, 0, sizeof(p_port->tx));

  p_port->tx.queue = fixed_queue_new(SIZE_MAX);
  p_port->rx.queue = fixed_queue_new(SIZE_MAX);
}

/*******************************************************************************
 *
 * Function         port_select_mtu
 *
 * Description      Select MTU which will best serve connection from our
 *                  point of view.
 *                  If our device is 1.2 or lower we calculate how many DH5s
 *                  fit into 1 RFCOMM buffer.
 *
 *
 ******************************************************************************/
void port_select_mtu(tPORT* p_port) {
  uint16_t packet_size;

  /* Will select MTU only if application did not setup something */
  if (p_port->mtu == 0) {
    /* find packet size which connection supports */
    packet_size =
        get_btm_client_interface().peer.BTM_GetMaxPacketSize(p_port->bd_addr);
    if (packet_size == 0) {
      /* something is very wrong */
      LOG(WARNING) << __func__ << ": bad packet size 0 for" << p_port->bd_addr;
      p_port->mtu = RFCOMM_DEFAULT_MTU;
    } else {
      /* We try to negotiate MTU that each packet can be split into whole
      number of max packets.  For example if link is 1.2 max packet size is 339
      bytes.
      At first calculate how many whole packets it is.  MAX L2CAP is 1691 + 4
      overhead.
      1695, that will be 5 Dh5 packets.  Now maximum RFCOMM packet is
      5 * 339 = 1695. Minus 4 bytes L2CAP header 1691.  Minus RFCOMM 6 bytes
      header overhead 1685

      For EDR 2.0 packet size is 1027.  So we better send RFCOMM packet as 1
      3DH5 packet
      1 * 1027 = 1027.  Minus 4 bytes L2CAP header 1023.  Minus RFCOMM 6 bytes
      header overhead 1017 */
      if ((L2CAP_MTU_SIZE + L2CAP_PKT_OVERHEAD) >= packet_size) {
        p_port->mtu = ((L2CAP_MTU_SIZE + L2CAP_PKT_OVERHEAD) / packet_size *
                       packet_size) -
                      RFCOMM_DATA_OVERHEAD - L2CAP_PKT_OVERHEAD;
        RFCOMM_TRACE_DEBUG("%s: selected %d based on connection speed",
                           __func__, p_port->mtu);
      } else {
        p_port->mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
        RFCOMM_TRACE_DEBUG("%s: selected %d based on l2cap PDU size", __func__,
                           p_port->mtu);
      }
    }
  } else {
    RFCOMM_TRACE_DEBUG("%s: application selected %d", __func__, p_port->mtu);
  }
  p_port->credit_rx_max = (PORT_RX_HIGH_WM / p_port->mtu);
  if (p_port->credit_rx_max > PORT_RX_BUF_HIGH_WM)
    p_port->credit_rx_max = PORT_RX_BUF_HIGH_WM;
  p_port->credit_rx_low = (PORT_RX_LOW_WM / p_port->mtu);
  if (p_port->credit_rx_low > PORT_RX_BUF_LOW_WM)
    p_port->credit_rx_low = PORT_RX_BUF_LOW_WM;
  p_port->rx_buf_critical = (PORT_RX_CRITICAL_WM / p_port->mtu);
  if (p_port->rx_buf_critical > PORT_RX_BUF_CRITICAL_WM)
    p_port->rx_buf_critical = PORT_RX_BUF_CRITICAL_WM;
  RFCOMM_TRACE_DEBUG(
      "%s: credit_rx_max %d, credit_rx_low %d, rx_buf_critical %d", __func__,
      p_port->credit_rx_max, p_port->credit_rx_low, p_port->rx_buf_critical);
}

/*******************************************************************************
 *
 * Function         port_release_port
 *
 * Description      Release port control block.
 *
 * Returns          Pointer to the PORT or NULL if not found
 *
 ******************************************************************************/
void port_release_port(tPORT* p_port) {
  RFCOMM_TRACE_DEBUG("%s p_port: %p state: %d keep_handle: %d", __func__,
                     p_port, p_port->rfc.state, p_port->keep_port_handle);

  mutex_global_lock();
  BT_HDR* p_buf;
  while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->rx.queue)) !=
         nullptr) {
    osi_free(p_buf);
  }
  p_port->rx.queue_size = 0;

  while ((p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue)) !=
         nullptr) {
    osi_free(p_buf);
  }
  p_port->tx.queue_size = 0;
  mutex_global_unlock();

  alarm_cancel(p_port->rfc.port_timer);

  p_port->state = PORT_CONNECTION_STATE_CLOSED;

  if (p_port->rfc.state == RFC_STATE_CLOSED) {
    if (p_port->rfc.p_mcb) {
      p_port->rfc.p_mcb->port_handles[p_port->dlci] = 0;

      /* If there are no more ports opened on this MCB release it */
      rfc_check_mcb_active(p_port->rfc.p_mcb);
    }

    rfc_port_timer_stop(p_port);

    mutex_global_lock();
    fixed_queue_free(p_port->tx.queue, nullptr);
    p_port->tx.queue = nullptr;
    fixed_queue_free(p_port->rx.queue, nullptr);
    p_port->rx.queue = nullptr;
    mutex_global_unlock();

    if (p_port->keep_port_handle) {
      RFCOMM_TRACE_DEBUG("%s Re-initialize handle: %d", __func__,
                         p_port->handle);

      /* save event mask and callback */
      uint32_t mask = p_port->ev_mask;
      tPORT_CALLBACK* p_port_cb = p_port->p_callback;
      tPORT_STATE user_port_pars = p_port->user_port_pars;

      port_set_defaults(p_port);

      /* restore */
      p_port->ev_mask = mask;
      p_port->p_callback = p_port_cb;
      p_port->user_port_pars = user_port_pars;
      p_port->mtu = p_port->keep_mtu;

      p_port->state = PORT_CONNECTION_STATE_OPENING;
      p_port->rfc.p_mcb = nullptr;
      if (p_port->is_server) p_port->dlci &= 0xfe;

      p_port->local_ctrl.modem_signal = p_port->default_signal_state;
      p_port->bd_addr = RawAddress::kAny;
    } else {
      RFCOMM_TRACE_DEBUG("%s Clean-up handle: %d", __func__, p_port->handle);
      alarm_free(p_port->rfc.port_timer);
      memset(p_port, 0, sizeof(tPORT));
    }
  }
}

/*******************************************************************************
 *
 * Function         port_find_mcb
 *
 * Description      This function checks if connection exists to device with
 *                  the address.
 *
 ******************************************************************************/
tRFC_MCB* port_find_mcb(const RawAddress& bd_addr) {
  for (tRFC_MCB& mcb : rfc_cb.port.rfc_mcb) {
    if ((mcb.state != RFC_MX_STATE_IDLE) && (mcb.bd_addr == bd_addr)) {
      /* Multiplexer channel found do not change anything */
      VLOG(1) << __func__ << ": found bd_addr=" << bd_addr
              << ", rfc_mcb=" << &mcb << ", lcid=" << loghex(mcb.lcid);
      return &mcb;
    }
  }
  VLOG(1) << __func__ << ": not found, bd_addr:" << bd_addr;
  return nullptr;
}

/*******************************************************************************
 *
 * Function         port_find_mcb_dlci_port
 *
 * Description      Find port on the multiplexer channel based on DLCI.  If
 *                  this port with DLCI not found try to use even DLCI.  This
 *                  is for the case when client is establishing connection on
 *                  none-initiator MCB.
 *
 * Returns          Pointer to the PORT or NULL if not found
 *
 ******************************************************************************/
tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci) {
  if (!p_mcb) {
    LOG(ERROR) << __func__ << ": p_mcb is null, dlci=" << std::to_string(dlci);
    return nullptr;
  }

  if (dlci > RFCOMM_MAX_DLCI) {
    LOG(WARNING) << __func__ << ": DLCI " << std::to_string(dlci)
                 << " is too large, bd_addr=" << p_mcb->bd_addr
                 << ", p_mcb=" << p_mcb;
    return nullptr;
  }

  uint8_t handle = p_mcb->port_handles[dlci];
  if (handle == 0) {
    LOG(INFO) << __func__ << ": Cannot find allocated RFCOMM app port for DLCI "
              << std::to_string(dlci) << " on " << p_mcb->bd_addr
              << ", p_mcb=" << p_mcb;
    return nullptr;
  }
  return &rfc_cb.port.port[handle - 1];
}

/*******************************************************************************
 *
 * Function         port_find_dlci_port
 *
 * Description      Find port with DLCI not assigned to multiplexer channel
 *
 * Returns          Pointer to the PORT or NULL if not found
 *
 ******************************************************************************/
tPORT* port_find_dlci_port(uint8_t dlci) {
  for (tPORT& port : rfc_cb.port.port) {
    if (port.in_use && (port.rfc.p_mcb == nullptr)) {
      if (port.dlci == dlci) {
        return &port;
      } else if ((dlci & 0x01) && (port.dlci == (dlci - 1))) {
        port.dlci++;
        return &port;
      }
    }
  }
  return nullptr;
}

/*******************************************************************************
 *
 * Function         port_find_port
 *
 * Description      Find port with DLCI, address
 *
 * Returns          Pointer to the PORT or NULL if not found
 *
 ******************************************************************************/
tPORT* port_find_port(uint8_t dlci, const RawAddress& bd_addr) {
  for (tPORT& port : rfc_cb.port.port) {
    if (port.in_use && (port.dlci == dlci) && (port.bd_addr == bd_addr)) {
      return &port;
    }
  }
  return nullptr;
}

/*******************************************************************************
 *
 * Function         port_flow_control_user
 *
 * Description      Check the current user flow control and if necessary return
 *                  events to be send to the user based on the user's specified
 *                  flow control type.
 *
 * Returns          event mask to be returned to the application
 *
 ******************************************************************************/
uint32_t port_flow_control_user(tPORT* p_port) {
  uint32_t event = 0;

  /* Flow control to the user can be caused by flow controlling by the peer */
  /* (FlowInd, or flow control by the peer RFCOMM (Fcon) or internally if */
  /* tx_queue is full */
  bool fc = p_port->tx.peer_fc || !p_port->rfc.p_mcb ||
            !p_port->rfc.p_mcb->peer_ready ||
            (p_port->tx.queue_size > PORT_TX_HIGH_WM) ||
            (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM);

  if (p_port->tx.user_fc == fc) return (0);

  p_port->tx.user_fc = fc;

  if (fc)
    event = PORT_EV_FC;
  else
    event = PORT_EV_FC | PORT_EV_FCS;

  return (event);
}

/*******************************************************************************
 *
 * Function         port_get_signal_changes
 *
 * Description      Check modem signals that has been changed
 *
 * Returns          event mask to be returned to the application
 *
 ******************************************************************************/
uint32_t port_get_signal_changes(tPORT* p_port, uint8_t old_signals,
                                 uint8_t signal) {
  uint8_t changed_signals = (signal ^ old_signals);
  uint32_t events = 0;

  if (changed_signals & PORT_DTRDSR_ON) {
    events |= PORT_EV_DSR;

    if (signal & PORT_DTRDSR_ON) events |= PORT_EV_DSRS;
  }

  if (changed_signals & PORT_CTSRTS_ON) {
    events |= PORT_EV_CTS;

    if (signal & PORT_CTSRTS_ON) events |= PORT_EV_CTSS;
  }

  if (changed_signals & PORT_RING_ON) events |= PORT_EV_RING;

  if (changed_signals & PORT_DCD_ON) {
    events |= PORT_EV_RLSD;

    if (signal & PORT_DCD_ON) events |= PORT_EV_RLSDS;
  }

  return (p_port->ev_mask & events);
}

/*******************************************************************************
 *
 * Function         port_flow_control_peer
 *
 * Description      Send flow control messages to the peer for both enabling
 *                  and disabling flow control, for both credit-based and
 *                  TS 07.10 flow control mechanisms.
 *
 * Returns          nothing
 *
 ******************************************************************************/
void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count) {
  if (!p_port->rfc.p_mcb) return;

  /* If using credit based flow control */
  if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
    /* if want to enable flow from peer */
    if (enable) {
      /* update rx credits */
      if (count > p_port->credit_rx) {
        p_port->credit_rx = 0;
      } else {
        p_port->credit_rx -= count;
      }

      /* If credit count is less than low credit watermark, and user */
      /* did not force flow control, send a credit update */
      /* There might be a special case when we just adjusted rx_max */
      if ((p_port->credit_rx <= p_port->credit_rx_low) && !p_port->rx.user_fc &&
          (p_port->credit_rx_max > p_port->credit_rx)) {
        rfc_send_credit(p_port->rfc.p_mcb, p_port->dlci,
                        (uint8_t)(p_port->credit_rx_max - p_port->credit_rx));

        p_port->credit_rx = p_port->credit_rx_max;

        p_port->rx.peer_fc = false;
      }
    }
    /* else want to disable flow from peer */
    else {
      /* if client registered data callback, just do what they want */
      if (p_port->p_data_callback || p_port->p_data_co_callback) {
        p_port->rx.peer_fc = true;
      }
      /* if queue count reached credit rx max, set peer fc */
      else if (fixed_queue_length(p_port->rx.queue) >= p_port->credit_rx_max) {
        p_port->rx.peer_fc = true;
      }
    }
  }
  /* else using TS 07.10 flow control */
  else {
    /* if want to enable flow from peer */
    if (enable) {
      /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
      /* check if it can be resumed now */
      if (p_port->rx.peer_fc && (p_port->rx.queue_size < PORT_RX_LOW_WM) &&
          (fixed_queue_length(p_port->rx.queue) < PORT_RX_BUF_LOW_WM)) {
        p_port->rx.peer_fc = false;

        /* If user did not force flow control allow traffic now */
        if (!p_port->rx.user_fc)
          RFCOMM_FlowReq(p_port->rfc.p_mcb, p_port->dlci, true);
      }
    }
    /* else want to disable flow from peer */
    else {
      /* if client registered data callback, just do what they want */
      if (p_port->p_data_callback || p_port->p_data_co_callback) {
        p_port->rx.peer_fc = true;
        RFCOMM_FlowReq(p_port->rfc.p_mcb, p_port->dlci, false);
      }
      /* Check the size of the rx queue.  If it exceeds certain */
      /* level and flow control has not been sent to the peer do it now */
      else if (((p_port->rx.queue_size > PORT_RX_HIGH_WM) ||
                (fixed_queue_length(p_port->rx.queue) > PORT_RX_BUF_HIGH_WM)) &&
               !p_port->rx.peer_fc) {
        RFCOMM_TRACE_EVENT("PORT_DataInd Data reached HW. Sending FC set.");

        p_port->rx.peer_fc = true;
        RFCOMM_FlowReq(p_port->rfc.p_mcb, p_port->dlci, false);
      }
    }
  }
}