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 the Serial Port API code
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bt_port_api"
26
27 #include <base/logging.h>
28 #include <string.h>
29
30 #include "osi/include/log.h"
31 #include "osi/include/mutex.h"
32
33 #include "bt_common.h"
34 #include "l2c_api.h"
35 #include "port_api.h"
36 #include "port_int.h"
37 #include "rfc_int.h"
38 #include "rfcdefs.h"
39 #include "sdp_api.h"
40
41 #define error(fmt, ...) \
42 LOG_ERROR("## ERROR : %s: " fmt "##", __func__, ##__VA_ARGS__)
43
44 /* Mapping from PORT_* result codes to human readable strings. */
45 static const char* result_code_strings[] = {"Success",
46 "Unknown error",
47 "Already opened",
48 "Command pending",
49 "App not registered",
50 "No memory",
51 "No resources",
52 "Bad BD address",
53 "Unspecified error",
54 "Bad handle",
55 "Not opened",
56 "Line error",
57 "Start failed",
58 "Parameter negotiation failed",
59 "Port negotiation failed",
60 "Sec failed",
61 "Peer connection failed",
62 "Peer failed",
63 "Peer timeout",
64 "Closed",
65 "TX full",
66 "Local closed",
67 "Local timeout",
68 "TX queue disabled",
69 "Page timeout",
70 "Invalid SCN",
71 "Unknown result code"};
72
73 /*******************************************************************************
74 *
75 * Function RFCOMM_CreateConnection
76 *
77 * Description RFCOMM_CreateConnection function is used from the
78 * application to establish serial port connection to the peer
79 * device, or allow RFCOMM to accept a connection from the peer
80 * application.
81 *
82 * Parameters: scn - Service Channel Number as registered with
83 * the SDP (server) or obtained using SDP from
84 * the peer device (client).
85 * is_server - true if requesting application is a server
86 * mtu - Maximum frame size the application can accept
87 * bd_addr - address of the peer (client)
88 * mask - specifies events to be enabled. A value
89 * of zero disables all events.
90 * p_handle - OUT pointer to the handle.
91 * p_mgmt_cb - pointer to callback function to receive
92 * connection up/down events.
93 * Notes:
94 *
95 * Server can call this function with the same scn parameter multiple times if
96 * it is ready to accept multiple simulteneous connections.
97 *
98 * DLCI for the connection is (scn * 2 + 1) if client originates connection on
99 * existing none initiator multiplexer channel. Otherwise it is (scn * 2).
100 * For the server DLCI can be changed later if client will be calling it using
101 * (scn * 2 + 1) dlci.
102 *
103 ******************************************************************************/
RFCOMM_CreateConnection(uint16_t uuid,uint8_t scn,bool is_server,uint16_t mtu,const RawAddress & bd_addr,uint16_t * p_handle,tPORT_CALLBACK * p_mgmt_cb)104 int RFCOMM_CreateConnection(uint16_t uuid, uint8_t scn, bool is_server,
105 uint16_t mtu, const RawAddress& bd_addr,
106 uint16_t* p_handle, tPORT_CALLBACK* p_mgmt_cb) {
107 *p_handle = 0;
108
109 if ((scn == 0) || (scn >= PORT_MAX_RFC_PORTS)) {
110 // Server Channel Number (SCN) should be in range [1, 30]
111 LOG(ERROR) << __func__ << ": Invalid SCN, bd_addr=" << bd_addr
112 << ", scn=" << static_cast<int>(scn)
113 << ", is_server=" << is_server
114 << ", mtu=" << static_cast<int>(mtu)
115 << ", uuid=" << loghex(uuid);
116 return (PORT_INVALID_SCN);
117 }
118
119 // For client that originates connection on the existing none initiator
120 // multiplexer channel, DLCI should be odd.
121 uint8_t dlci;
122 tRFC_MCB* p_mcb = port_find_mcb(bd_addr);
123 if (p_mcb && !p_mcb->is_initiator && !is_server) {
124 dlci = static_cast<uint8_t>((scn << 1) + 1);
125 } else {
126 dlci = (scn << 1);
127 }
128
129 // On the client side, do not allow the same (dlci, bd_addr) to be opened
130 // twice by application
131 tPORT* p_port;
132 if (!is_server) {
133 p_port = port_find_port(dlci, bd_addr);
134 if (p_port != nullptr) {
135 // if existing port is also a client port, error out
136 if (!p_port->is_server) {
137 LOG(ERROR) << __func__ << ": already at opened state "
138 << static_cast<int>(p_port->state)
139 << ", RFC_state=" << static_cast<int>(p_port->rfc.state)
140 << ", MCB_state="
141 << (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0)
142 << ", bd_addr=" << bd_addr << ", scn=" << std::to_string(scn)
143 << ", is_server=" << is_server << ", mtu=" << mtu
144 << ", uuid=" << loghex(uuid) << ", dlci=" << +dlci
145 << ", p_mcb=" << p_mcb
146 << ", port=" << std::to_string(p_port->handle);
147 *p_handle = p_port->handle;
148 return (PORT_ALREADY_OPENED);
149 }
150 }
151 }
152
153 // On the server side, always allocate a new port.
154 p_port = port_allocate_port(dlci, bd_addr);
155 if (p_port == nullptr) {
156 LOG(ERROR) << __func__ << ": no resources, bd_addr=" << bd_addr
157 << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
158 << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
159 << ", dlci=" << +dlci;
160 return PORT_NO_RESOURCES;
161 }
162 *p_handle = p_port->handle;
163
164 // Get default signal state
165 switch (uuid) {
166 case UUID_PROTOCOL_OBEX:
167 p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE;
168 break;
169 case UUID_SERVCLASS_SERIAL_PORT:
170 p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE;
171 break;
172 case UUID_SERVCLASS_LAN_ACCESS_USING_PPP:
173 p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE;
174 break;
175 case UUID_SERVCLASS_DIALUP_NETWORKING:
176 case UUID_SERVCLASS_FAX:
177 p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE;
178 break;
179 default:
180 p_port->default_signal_state =
181 (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
182 break;
183 }
184
185 // Assign port specific values
186 p_port->state = PORT_STATE_OPENING;
187 p_port->uuid = uuid;
188 p_port->is_server = is_server;
189 p_port->scn = scn;
190 p_port->ev_mask = 0;
191
192 // Find MTU
193 // If the MTU is not specified (0), keep MTU decision until the PN frame has
194 // to be send at that time connection should be established and we will know
195 // for sure our prefered MTU
196 uint16_t rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
197 if (mtu) {
198 p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu;
199 } else {
200 p_port->mtu = rfcomm_mtu;
201 }
202
203 // Other states
204 // server doesn't need to release port when closing
205 if (is_server) {
206 p_port->keep_port_handle = true;
207 // keep mtu that user asked, p_port->mtu could be updated during param
208 // negotiation
209 p_port->keep_mtu = p_port->mtu;
210 }
211 p_port->local_ctrl.modem_signal = p_port->default_signal_state;
212 p_port->local_ctrl.fc = false;
213 p_port->p_mgmt_callback = p_mgmt_cb;
214 p_port->bd_addr = bd_addr;
215
216 LOG(INFO) << __func__ << ": bd_addr=" << bd_addr
217 << ", scn=" << std::to_string(scn) << ", is_server=" << is_server
218 << ", mtu=" << mtu << ", uuid=" << loghex(uuid)
219 << ", dlci=" << std::to_string(dlci)
220 << ", signal_state=" << loghex(p_port->default_signal_state)
221 << ", p_port=" << p_port;
222
223 // If this is not initiator of the connection need to just wait
224 if (p_port->is_server) {
225 return (PORT_SUCCESS);
226 }
227
228 // Open will be continued after security checks are passed
229 return port_open_continue(p_port);
230 }
231
232 /*******************************************************************************
233 *
234 * Function RFCOMM_RemoveConnection
235 *
236 * Description This function is called to close the specified connection.
237 *
238 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
239 *
240 ******************************************************************************/
RFCOMM_RemoveConnection(uint16_t handle)241 int RFCOMM_RemoveConnection(uint16_t handle) {
242 tPORT* p_port;
243
244 RFCOMM_TRACE_API("RFCOMM_RemoveConnection() handle:%d", handle);
245
246 /* Check if handle is valid to avoid crashing */
247 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
248 RFCOMM_TRACE_ERROR("RFCOMM_RemoveConnection() BAD handle:%d", handle);
249 return (PORT_BAD_HANDLE);
250 }
251 p_port = &rfc_cb.port.port[handle - 1];
252
253 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
254 RFCOMM_TRACE_EVENT("RFCOMM_RemoveConnection() Not opened:%d", handle);
255 return (PORT_SUCCESS);
256 }
257
258 p_port->state = PORT_STATE_CLOSING;
259
260 port_start_close(p_port);
261
262 return (PORT_SUCCESS);
263 }
264
265 /*******************************************************************************
266 *
267 * Function RFCOMM_RemoveServer
268 *
269 * Description This function is called to close the server port.
270 *
271 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
272 *
273 ******************************************************************************/
RFCOMM_RemoveServer(uint16_t handle)274 int RFCOMM_RemoveServer(uint16_t handle) {
275 /* Check if handle is valid to avoid crashing */
276 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
277 LOG(ERROR) << __func__ << ": bad handle " << handle;
278 return (PORT_BAD_HANDLE);
279 }
280 tPORT* p_port = &rfc_cb.port.port[handle - 1];
281
282 /* Do not report any events to the client any more. */
283 p_port->p_mgmt_callback = nullptr;
284
285 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
286 VLOG(1) << __func__ << ": handle " << handle << " not opened";
287 return (PORT_SUCCESS);
288 }
289 LOG(INFO) << __func__ << ": handle=" << handle;
290
291 /* this port will be deallocated after closing */
292 p_port->keep_port_handle = false;
293 p_port->state = PORT_STATE_CLOSING;
294
295 port_start_close(p_port);
296
297 return (PORT_SUCCESS);
298 }
299
300 /*******************************************************************************
301 *
302 * Function PORT_SetEventCallback
303 *
304 * Description This function is called to provide an address of the
305 * function which will be called when one of the events
306 * specified in the mask occures.
307 *
308 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
309 * p_callback - address of the callback function which should
310 * be called from the RFCOMM when an event
311 * specified in the mask occures.
312 *
313 *
314 ******************************************************************************/
PORT_SetEventCallback(uint16_t port_handle,tPORT_CALLBACK * p_port_cb)315 int PORT_SetEventCallback(uint16_t port_handle, tPORT_CALLBACK* p_port_cb) {
316 tPORT* p_port;
317
318 /* Check if handle is valid to avoid crashing */
319 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
320 return (PORT_BAD_HANDLE);
321 }
322
323 p_port = &rfc_cb.port.port[port_handle - 1];
324
325 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
326 return (PORT_NOT_OPENED);
327 }
328
329 RFCOMM_TRACE_API("PORT_SetEventCallback() handle:%d", port_handle);
330
331 p_port->p_callback = p_port_cb;
332
333 return (PORT_SUCCESS);
334 }
335 /*******************************************************************************
336 *
337 * Function PORT_ClearKeepHandleFlag
338 *
339 * Description Clear the keep handle flag, which will cause not to keep the
340 * port handle open when closed
341 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
342 *
343 ******************************************************************************/
344
PORT_ClearKeepHandleFlag(uint16_t port_handle)345 int PORT_ClearKeepHandleFlag(uint16_t port_handle) {
346 tPORT* p_port;
347
348 /* Check if handle is valid to avoid crashing */
349 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
350 return (PORT_BAD_HANDLE);
351 }
352
353 p_port = &rfc_cb.port.port[port_handle - 1];
354 p_port->keep_port_handle = 0;
355 return (PORT_SUCCESS);
356 }
357
358 /*******************************************************************************
359 *
360 * Function PORT_SetCODataCallback
361 *
362 * Description This function is when a data packet is received
363 *
364 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
365 * p_callback - address of the callback function which should
366 * be called from the RFCOMM when data packet
367 * is received.
368 *
369 *
370 ******************************************************************************/
PORT_SetDataCOCallback(uint16_t port_handle,tPORT_DATA_CO_CALLBACK * p_port_cb)371 int PORT_SetDataCOCallback(uint16_t port_handle,
372 tPORT_DATA_CO_CALLBACK* p_port_cb) {
373 tPORT* p_port;
374
375 RFCOMM_TRACE_API("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle,
376 p_port_cb);
377
378 /* Check if handle is valid to avoid crashing */
379 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
380 return (PORT_BAD_HANDLE);
381 }
382
383 p_port = &rfc_cb.port.port[port_handle - 1];
384
385 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
386 return (PORT_NOT_OPENED);
387 }
388
389 p_port->p_data_co_callback = p_port_cb;
390
391 return (PORT_SUCCESS);
392 }
393
394 /*******************************************************************************
395 *
396 * Function PORT_SetEventMask
397 *
398 * Description This function is called to close the specified connection.
399 *
400 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
401 * mask - Bitmask of the events the host is interested in
402 *
403 ******************************************************************************/
PORT_SetEventMask(uint16_t port_handle,uint32_t mask)404 int PORT_SetEventMask(uint16_t port_handle, uint32_t mask) {
405 tPORT* p_port;
406
407 RFCOMM_TRACE_API("PORT_SetEventMask() handle:%d mask:0x%x", port_handle,
408 mask);
409
410 /* Check if handle is valid to avoid crashing */
411 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) {
412 return (PORT_BAD_HANDLE);
413 }
414
415 p_port = &rfc_cb.port.port[port_handle - 1];
416
417 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
418 return (PORT_NOT_OPENED);
419 }
420
421 p_port->ev_mask = mask;
422
423 return (PORT_SUCCESS);
424 }
425
426 /*******************************************************************************
427 *
428 * Function PORT_CheckConnection
429 *
430 * Description This function returns PORT_SUCCESS if connection referenced
431 * by handle is up and running
432 *
433 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
434 * bd_addr - OUT bd_addr of the peer
435 * p_lcid - OUT L2CAP's LCID
436 *
437 ******************************************************************************/
PORT_CheckConnection(uint16_t handle,RawAddress * bd_addr,uint16_t * p_lcid)438 int PORT_CheckConnection(uint16_t handle, RawAddress* bd_addr,
439 uint16_t* p_lcid) {
440 /* Check if handle is valid to avoid crashing */
441 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
442 return (PORT_BAD_HANDLE);
443 }
444 tPORT* p_port = &rfc_cb.port.port[handle - 1];
445 RFCOMM_TRACE_DEBUG(
446 "%s: handle=%d, in_use=%d, port_state=%d, p_mcb=%p, peer_ready=%d, "
447 "rfc_state=%d",
448 __func__, handle, p_port->in_use, p_port->state, p_port->rfc.p_mcb,
449 (p_port->rfc.p_mcb ? p_port->rfc.p_mcb->peer_ready : -1),
450 p_port->rfc.state);
451
452 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
453 return (PORT_NOT_OPENED);
454 }
455
456 if (!p_port->rfc.p_mcb || !p_port->rfc.p_mcb->peer_ready ||
457 (p_port->rfc.state != RFC_STATE_OPENED)) {
458 return (PORT_LINE_ERR);
459 }
460
461 *bd_addr = p_port->rfc.p_mcb->bd_addr;
462 if (p_lcid) *p_lcid = p_port->rfc.p_mcb->lcid;
463
464 return (PORT_SUCCESS);
465 }
466
467 /*******************************************************************************
468 *
469 * Function PORT_IsOpening
470 *
471 * Description This function returns true if there is any RFCOMM connection
472 * opening in process.
473 *
474 * Parameters: true if any connection opening is found
475 * bd_addr - bd_addr of the peer
476 *
477 ******************************************************************************/
PORT_IsOpening(RawAddress * bd_addr)478 bool PORT_IsOpening(RawAddress* bd_addr) {
479 /* Check for any rfc_mcb which is in the middle of opening. */
480 for (auto& multiplexer_cb : rfc_cb.port.rfc_mcb) {
481 if ((multiplexer_cb.state > RFC_MX_STATE_IDLE) &&
482 (multiplexer_cb.state < RFC_MX_STATE_CONNECTED)) {
483 *bd_addr = multiplexer_cb.bd_addr;
484 return true;
485 }
486
487 if (multiplexer_cb.state == RFC_MX_STATE_CONNECTED) {
488 bool found_port = false;
489 tPORT* p_port = nullptr;
490
491 for (tPORT& port : rfc_cb.port.port) {
492 if (port.rfc.p_mcb == &multiplexer_cb) {
493 found_port = true;
494 p_port = &port;
495 break;
496 }
497 }
498
499 if ((!found_port) ||
500 (found_port && (p_port->rfc.state < RFC_STATE_OPENED))) {
501 /* Port is not established yet. */
502 *bd_addr = multiplexer_cb.bd_addr;
503 return true;
504 }
505 }
506 }
507
508 return false;
509 }
510
511 /*******************************************************************************
512 *
513 * Function PORT_SetState
514 *
515 * Description This function configures connection according to the
516 * specifications in the tPORT_STATE structure.
517 *
518 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
519 * p_settings - Pointer to a tPORT_STATE structure containing
520 * configuration information for the connection.
521 *
522 *
523 ******************************************************************************/
PORT_SetState(uint16_t handle,tPORT_STATE * p_settings)524 int PORT_SetState(uint16_t handle, tPORT_STATE* p_settings) {
525 tPORT* p_port;
526 uint8_t baud_rate;
527
528 RFCOMM_TRACE_API("PORT_SetState() handle:%d", handle);
529
530 /* Check if handle is valid to avoid crashing */
531 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
532 return (PORT_BAD_HANDLE);
533 }
534
535 p_port = &rfc_cb.port.port[handle - 1];
536
537 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
538 return (PORT_NOT_OPENED);
539 }
540
541 if (p_port->line_status) {
542 return (PORT_LINE_ERR);
543 }
544
545 RFCOMM_TRACE_API("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
546 p_settings->fc_type);
547
548 baud_rate = p_port->user_port_pars.baud_rate;
549 p_port->user_port_pars = *p_settings;
550
551 /* for now we've been asked to pass only baud rate */
552 if (baud_rate != p_settings->baud_rate) {
553 port_start_par_neg(p_port);
554 }
555 return (PORT_SUCCESS);
556 }
557
558 /*******************************************************************************
559 *
560 * Function PORT_GetState
561 *
562 * Description This function is called to fill tPORT_STATE structure
563 * with the curremt control settings for the port
564 *
565 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
566 * p_settings - Pointer to a tPORT_STATE structure in which
567 * configuration information is returned.
568 *
569 ******************************************************************************/
PORT_GetState(uint16_t handle,tPORT_STATE * p_settings)570 int PORT_GetState(uint16_t handle, tPORT_STATE* p_settings) {
571 tPORT* p_port;
572
573 RFCOMM_TRACE_API("PORT_GetState() handle:%d", handle);
574
575 /* Check if handle is valid to avoid crashing */
576 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
577 return (PORT_BAD_HANDLE);
578 }
579
580 p_port = &rfc_cb.port.port[handle - 1];
581
582 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
583 return (PORT_NOT_OPENED);
584 }
585
586 if (p_port->line_status) {
587 return (PORT_LINE_ERR);
588 }
589
590 *p_settings = p_port->user_port_pars;
591 return (PORT_SUCCESS);
592 }
593
594 /*******************************************************************************
595 *
596 * Function PORT_FlowControl_MaxCredit
597 *
598 * Description This function directs a specified connection to pass
599 * flow control message to the peer device. Enable flag passed
600 * shows if port can accept more data. It also sends max credit
601 * when data flow enabled
602 *
603 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
604 * enable - enables data flow
605 *
606 ******************************************************************************/
607
PORT_FlowControl_MaxCredit(uint16_t handle,bool enable)608 int PORT_FlowControl_MaxCredit(uint16_t handle, bool enable) {
609 tPORT* p_port;
610 bool old_fc;
611 uint32_t events;
612
613 RFCOMM_TRACE_API("PORT_FlowControl() handle:%d enable: %d", handle, enable);
614
615 /* Check if handle is valid to avoid crashing */
616 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
617 return (PORT_BAD_HANDLE);
618 }
619
620 p_port = &rfc_cb.port.port[handle - 1];
621
622 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
623 return (PORT_NOT_OPENED);
624 }
625
626 if (!p_port->rfc.p_mcb) {
627 return (PORT_NOT_OPENED);
628 }
629
630 p_port->rx.user_fc = !enable;
631
632 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
633 if (!p_port->rx.user_fc) {
634 port_flow_control_peer(p_port, true, p_port->credit_rx);
635 }
636 } else {
637 old_fc = p_port->local_ctrl.fc;
638
639 /* FC is set if user is set or peer is set */
640 p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
641
642 if (p_port->local_ctrl.fc != old_fc) port_start_control(p_port);
643 }
644
645 /* Need to take care of the case when we could not deliver events */
646 /* to the application because we were flow controlled */
647 if (enable && (p_port->rx.queue_size != 0)) {
648 events = PORT_EV_RXCHAR;
649 if (p_port->rx_flag_ev_pending) {
650 p_port->rx_flag_ev_pending = false;
651 events |= PORT_EV_RXFLAG;
652 }
653
654 events &= p_port->ev_mask;
655 if (p_port->p_callback && events) {
656 p_port->p_callback(events, p_port->handle);
657 }
658 }
659 return (PORT_SUCCESS);
660 }
661
662 /*******************************************************************************
663 *
664 * Function PORT_ReadData
665 *
666 * Description Normally not GKI aware application will call this function
667 * after receiving PORT_EV_RXCHAR event.
668 *
669 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
670 * p_data - Data area
671 * max_len - Byte count requested
672 * p_len - Byte count received
673 *
674 ******************************************************************************/
PORT_ReadData(uint16_t handle,char * p_data,uint16_t max_len,uint16_t * p_len)675 int PORT_ReadData(uint16_t handle, char* p_data, uint16_t max_len,
676 uint16_t* p_len) {
677 tPORT* p_port;
678 BT_HDR* p_buf;
679 uint16_t count;
680
681 RFCOMM_TRACE_API("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
682
683 /* Initialize this in case of an error */
684 *p_len = 0;
685
686 /* Check if handle is valid to avoid crashing */
687 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
688 return (PORT_BAD_HANDLE);
689 }
690
691 p_port = &rfc_cb.port.port[handle - 1];
692
693 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
694 return (PORT_NOT_OPENED);
695 }
696
697 if (p_port->line_status) {
698 return (PORT_LINE_ERR);
699 }
700
701 if (fixed_queue_is_empty(p_port->rx.queue)) return (PORT_SUCCESS);
702
703 count = 0;
704
705 while (max_len) {
706 p_buf = (BT_HDR*)fixed_queue_try_peek_first(p_port->rx.queue);
707 if (p_buf == NULL) break;
708
709 if (p_buf->len > max_len) {
710 memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, max_len);
711 p_buf->offset += max_len;
712 p_buf->len -= max_len;
713
714 *p_len += max_len;
715
716 mutex_global_lock();
717
718 p_port->rx.queue_size -= max_len;
719
720 mutex_global_unlock();
721
722 break;
723 } else {
724 memcpy(p_data, (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
725
726 *p_len += p_buf->len;
727 max_len -= p_buf->len;
728
729 mutex_global_lock();
730
731 p_port->rx.queue_size -= p_buf->len;
732
733 if (max_len) {
734 p_data += p_buf->len;
735 }
736
737 osi_free(fixed_queue_try_dequeue(p_port->rx.queue));
738
739 mutex_global_unlock();
740
741 count++;
742 }
743 }
744
745 if (*p_len == 1) {
746 RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d %x",
747 p_port->rx.queue_size, *p_len, (p_data[0]));
748 } else {
749 RFCOMM_TRACE_EVENT("PORT_ReadData queue:%d returned:%d",
750 p_port->rx.queue_size, *p_len);
751 }
752
753 /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
754 /* check if it can be resumed now */
755 port_flow_control_peer(p_port, true, count);
756
757 return (PORT_SUCCESS);
758 }
759
760 /*******************************************************************************
761 *
762 * Function port_write
763 *
764 * Description This function when a data packet is received from the apper
765 * layer task.
766 *
767 * Parameters: p_port - pointer to address of port control block
768 * p_buf - pointer to address of buffer with data,
769 *
770 ******************************************************************************/
port_write(tPORT * p_port,BT_HDR * p_buf)771 static int port_write(tPORT* p_port, BT_HDR* p_buf) {
772 /* We should not allow to write data in to server port when connection is not
773 * opened */
774 if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED)) {
775 osi_free(p_buf);
776 return (PORT_CLOSED);
777 }
778
779 /* Keep the data in pending queue if peer does not allow data, or */
780 /* Peer is not ready or Port is not yet opened or initial port control */
781 /* command has not been sent */
782 if (p_port->tx.peer_fc || !p_port->rfc.p_mcb ||
783 !p_port->rfc.p_mcb->peer_ready ||
784 (p_port->rfc.state != RFC_STATE_OPENED) ||
785 ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) !=
786 (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) {
787 if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM) ||
788 (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_CRITICAL_WM)) {
789 RFCOMM_TRACE_WARNING("PORT_Write: Queue size: %d", p_port->tx.queue_size);
790
791 osi_free(p_buf);
792
793 if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR))
794 p_port->p_callback(PORT_EV_ERR, p_port->handle);
795
796 return (PORT_TX_FULL);
797 }
798
799 RFCOMM_TRACE_EVENT(
800 "PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d "
801 "ctrl_state %x",
802 p_port->tx.peer_fc,
803 (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready), p_port->rfc.state,
804 p_port->port_ctrl);
805
806 fixed_queue_enqueue(p_port->tx.queue, p_buf);
807 p_port->tx.queue_size += p_buf->len;
808
809 return (PORT_CMD_PENDING);
810 } else {
811 RFCOMM_TRACE_EVENT("PORT_Write : Data is being sent");
812
813 RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
814 return (PORT_SUCCESS);
815 }
816 }
817
818 /*******************************************************************************
819 *
820 * Function PORT_WriteDataCO
821 *
822 * Description Normally not GKI aware application will call this function
823 * to send data to the port by callout functions
824 *
825 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
826 * fd - socket fd
827 * p_len - Byte count returned
828 *
829 ******************************************************************************/
PORT_WriteDataCO(uint16_t handle,int * p_len)830 int PORT_WriteDataCO(uint16_t handle, int* p_len) {
831 tPORT* p_port;
832 BT_HDR* p_buf;
833 uint32_t event = 0;
834 int rc = 0;
835 uint16_t length;
836
837 RFCOMM_TRACE_API("PORT_WriteDataCO() handle:%d", handle);
838 *p_len = 0;
839
840 /* Check if handle is valid to avoid crashing */
841 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
842 return (PORT_BAD_HANDLE);
843 }
844 p_port = &rfc_cb.port.port[handle - 1];
845
846 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
847 RFCOMM_TRACE_WARNING("PORT_WriteDataByFd() no port state:%d",
848 p_port->state);
849 return (PORT_NOT_OPENED);
850 }
851
852 if (!p_port->peer_mtu) {
853 RFCOMM_TRACE_ERROR("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
854 return (PORT_UNKNOWN_ERROR);
855 }
856 int available = 0;
857 // if(ioctl(fd, FIONREAD, &available) < 0)
858 if (!p_port->p_data_co_callback(handle, (uint8_t*)&available,
859 sizeof(available),
860 DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE)) {
861 RFCOMM_TRACE_ERROR(
862 "p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, "
863 "available:%d",
864 available);
865 return (PORT_UNKNOWN_ERROR);
866 }
867 if (available == 0) return PORT_SUCCESS;
868 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len
869 */
870 length = RFCOMM_DATA_BUF_SIZE -
871 (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
872
873 /* If there are buffers scheduled for transmission check if requested */
874 /* data fits into the end of the queue */
875 mutex_global_lock();
876
877 p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue);
878 if ((p_buf != NULL) &&
879 (((int)p_buf->len + available) <= (int)p_port->peer_mtu) &&
880 (((int)p_buf->len + available) <= (int)length)) {
881 // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len,
882 // available, 0) != available)
883 if (!p_port->p_data_co_callback(
884 handle, (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len,
885 available, DATA_CO_CALLBACK_TYPE_OUTGOING))
886
887 {
888 error(
889 "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, "
890 "available:%d",
891 available);
892 mutex_global_unlock();
893 return (PORT_UNKNOWN_ERROR);
894 }
895 // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data,
896 // max_len);
897 p_port->tx.queue_size += (uint16_t)available;
898
899 *p_len = available;
900 p_buf->len += (uint16_t)available;
901
902 mutex_global_unlock();
903
904 return (PORT_SUCCESS);
905 }
906
907 mutex_global_unlock();
908
909 // int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu;
910
911 // max_read = available < max_read ? available : max_read;
912
913 while (available) {
914 /* if we're over buffer high water mark, we're done */
915 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) ||
916 (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM)) {
917 port_flow_control_user(p_port);
918 event |= PORT_EV_FC;
919 RFCOMM_TRACE_EVENT(
920 "tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d",
921 p_port->tx.queue_size, fixed_queue_length(p_port->tx.queue),
922 available);
923 break;
924 }
925
926 /* continue with rfcomm data write */
927 p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE);
928 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
929 p_buf->layer_specific = handle;
930
931 if (p_port->peer_mtu < length) length = p_port->peer_mtu;
932 if (available < (int)length) length = (uint16_t)available;
933 p_buf->len = length;
934 p_buf->event = BT_EVT_TO_BTU_SP_DATA;
935
936 // memcpy ((uint8_t *)(p_buf + 1) + p_buf->offset, p_data, length);
937 // if(recv(fd, (uint8_t *)(p_buf + 1) + p_buf->offset, (int)length, 0) !=
938 // (int)length)
939 if (!p_port->p_data_co_callback(handle,
940 (uint8_t*)(p_buf + 1) + p_buf->offset,
941 length, DATA_CO_CALLBACK_TYPE_OUTGOING)) {
942 error(
943 "p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d",
944 length);
945 return (PORT_UNKNOWN_ERROR);
946 }
947
948 RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length);
949
950 rc = port_write(p_port, p_buf);
951
952 /* If queue went below the threashold need to send flow control */
953 event |= port_flow_control_user(p_port);
954
955 if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR;
956
957 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break;
958
959 *p_len += length;
960 available -= (int)length;
961 }
962 if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
963 event |= PORT_EV_TXEMPTY;
964
965 /* Mask out all events that are not of interest to user */
966 event &= p_port->ev_mask;
967
968 /* Send event to the application */
969 if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->handle);
970
971 return (PORT_SUCCESS);
972 }
973
974 /*******************************************************************************
975 *
976 * Function PORT_WriteData
977 *
978 * Description Normally not GKI aware application will call this function
979 * to send data to the port.
980 *
981 * Parameters: handle - Handle returned in the RFCOMM_CreateConnection
982 * p_data - Data area
983 * max_len - Byte count requested
984 * p_len - Byte count received
985 *
986 ******************************************************************************/
PORT_WriteData(uint16_t handle,const char * p_data,uint16_t max_len,uint16_t * p_len)987 int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
988 uint16_t* p_len) {
989 tPORT* p_port;
990 BT_HDR* p_buf;
991 uint32_t event = 0;
992 int rc = 0;
993 uint16_t length;
994
995 RFCOMM_TRACE_API("PORT_WriteData() max_len:%d", max_len);
996
997 *p_len = 0;
998
999 /* Check if handle is valid to avoid crashing */
1000 if ((handle == 0) || (handle > MAX_RFC_PORTS)) {
1001 return (PORT_BAD_HANDLE);
1002 }
1003 p_port = &rfc_cb.port.port[handle - 1];
1004
1005 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) {
1006 RFCOMM_TRACE_WARNING("PORT_WriteData() no port state:%d", p_port->state);
1007 return (PORT_NOT_OPENED);
1008 }
1009
1010 if (!max_len || !p_port->peer_mtu) {
1011 RFCOMM_TRACE_ERROR("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
1012 return (PORT_UNKNOWN_ERROR);
1013 }
1014
1015 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len
1016 */
1017 length = RFCOMM_DATA_BUF_SIZE -
1018 (uint16_t)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
1019
1020 /* If there are buffers scheduled for transmission check if requested */
1021 /* data fits into the end of the queue */
1022 mutex_global_lock();
1023
1024 p_buf = (BT_HDR*)fixed_queue_try_peek_last(p_port->tx.queue);
1025 if ((p_buf != NULL) && ((p_buf->len + max_len) <= p_port->peer_mtu) &&
1026 ((p_buf->len + max_len) <= length)) {
1027 memcpy((uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
1028 p_port->tx.queue_size += max_len;
1029
1030 *p_len = max_len;
1031 p_buf->len += max_len;
1032
1033 mutex_global_unlock();
1034
1035 return (PORT_SUCCESS);
1036 }
1037
1038 mutex_global_unlock();
1039
1040 while (max_len) {
1041 /* if we're over buffer high water mark, we're done */
1042 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) ||
1043 (fixed_queue_length(p_port->tx.queue) > PORT_TX_BUF_HIGH_WM))
1044 break;
1045
1046 /* continue with rfcomm data write */
1047 p_buf = (BT_HDR*)osi_malloc(RFCOMM_DATA_BUF_SIZE);
1048 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
1049 p_buf->layer_specific = handle;
1050
1051 if (p_port->peer_mtu < length) length = p_port->peer_mtu;
1052 if (max_len < length) length = max_len;
1053 p_buf->len = length;
1054 p_buf->event = BT_EVT_TO_BTU_SP_DATA;
1055
1056 memcpy((uint8_t*)(p_buf + 1) + p_buf->offset, p_data, length);
1057
1058 RFCOMM_TRACE_EVENT("PORT_WriteData %d bytes", length);
1059
1060 rc = port_write(p_port, p_buf);
1061
1062 /* If queue went below the threashold need to send flow control */
1063 event |= port_flow_control_user(p_port);
1064
1065 if (rc == PORT_SUCCESS) event |= PORT_EV_TXCHAR;
1066
1067 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) break;
1068
1069 *p_len += length;
1070 max_len -= length;
1071 p_data += length;
1072 }
1073 if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
1074 event |= PORT_EV_TXEMPTY;
1075
1076 /* Mask out all events that are not of interest to user */
1077 event &= p_port->ev_mask;
1078
1079 /* Send event to the application */
1080 if (p_port->p_callback && event) (p_port->p_callback)(event, p_port->handle);
1081
1082 return (PORT_SUCCESS);
1083 }
1084
1085 /*******************************************************************************
1086 *
1087 * Function RFCOMM_Init
1088 *
1089 * Description This function is called to initialize RFCOMM layer
1090 *
1091 ******************************************************************************/
RFCOMM_Init(void)1092 void RFCOMM_Init(void) {
1093 memset(&rfc_cb, 0, sizeof(tRFC_CB)); /* Init RFCOMM control block */
1094
1095 rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
1096
1097 #if defined(RFCOMM_INITIAL_TRACE_LEVEL)
1098 rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL;
1099 #else
1100 rfc_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
1101 #endif
1102
1103 rfcomm_l2cap_if_init();
1104 }
1105
1106 /*******************************************************************************
1107 *
1108 * Function PORT_SetTraceLevel
1109 *
1110 * Description Set the trace level for RFCOMM. If called with 0xFF, it
1111 * simply reads the current trace level.
1112 *
1113 * Returns the new (current) trace level
1114 *
1115 ******************************************************************************/
PORT_SetTraceLevel(uint8_t new_level)1116 uint8_t PORT_SetTraceLevel(uint8_t new_level) {
1117 if (new_level != 0xFF) rfc_cb.trace_level = new_level;
1118
1119 return (rfc_cb.trace_level);
1120 }
1121
1122 /*******************************************************************************
1123 *
1124 * Function PORT_GetResultString
1125 *
1126 * Description This function returns the human-readable string for a given
1127 * result code.
1128 *
1129 * Returns a pointer to the human-readable string for the given result.
1130 *
1131 ******************************************************************************/
PORT_GetResultString(const uint8_t result_code)1132 const char* PORT_GetResultString(const uint8_t result_code) {
1133 if (result_code > PORT_ERR_MAX) {
1134 return result_code_strings[PORT_ERR_MAX];
1135 }
1136
1137 return result_code_strings[result_code];
1138 }
1139