1 /****************************************************************************** 2 * 3 * Copyright (C) 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 #include <string.h> 26 #include "bt_target.h" 27 #include "gki.h" 28 #include "rfcdefs.h" 29 #include "port_api.h" 30 #include "port_int.h" 31 #include "btm_int.h" 32 #include "btm_api.h" 33 #include "rfc_int.h" 34 #include "l2c_api.h" 35 #include "sdp_api.h" 36 37 /* duration of break in 200ms units */ 38 #define PORT_BREAK_DURATION 1 39 40 #include <cutils/log.h> 41 #define info(fmt, ...) ALOGI ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__) 42 #define debug(fmt, ...) ALOGD ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__) 43 #define error(fmt, ...) ALOGE ("## ERROR : %s: " fmt "##",__FUNCTION__, ## __VA_ARGS__) 44 #define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) 45 46 /******************************************************************************* 47 ** 48 ** Function RFCOMM_CreateConnection 49 ** 50 ** Description RFCOMM_CreateConnection function is used from the application 51 ** to establish serial port connection to the peer device, 52 ** or allow RFCOMM to accept a connection from the peer 53 ** application. 54 ** 55 ** Parameters: scn - Service Channel Number as registered with 56 ** the SDP (server) or obtained using SDP from 57 ** the peer device (client). 58 ** is_server - TRUE if requesting application is a server 59 ** mtu - Maximum frame size the application can accept 60 ** bd_addr - BD_ADDR of the peer (client) 61 ** mask - specifies events to be enabled. A value 62 ** of zero disables all events. 63 ** p_handle - OUT pointer to the handle. 64 ** p_mgmt_cb - pointer to callback function to receive 65 ** connection up/down events. 66 ** Notes: 67 ** 68 ** Server can call this function with the same scn parameter multiple times if 69 ** it is ready to accept multiple simulteneous connections. 70 ** 71 ** DLCI for the connection is (scn * 2 + 1) if client originates connection on 72 ** existing none initiator multiplexer channel. Otherwise it is (scn * 2). 73 ** For the server DLCI can be changed later if client will be calling it using 74 ** (scn * 2 + 1) dlci. 75 ** 76 *******************************************************************************/ 77 int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server, 78 UINT16 mtu, BD_ADDR bd_addr, UINT16 *p_handle, 79 tPORT_CALLBACK *p_mgmt_cb) 80 { 81 tPORT *p_port; 82 int i; 83 UINT8 dlci; 84 tRFC_MCB *p_mcb = port_find_mcb (bd_addr); 85 UINT16 rfcomm_mtu; 86 87 RFCOMM_TRACE_API3 ("RFCOMM_CreateConnection() called SCN: %d is_server:%d mtu:%d", 88 scn, is_server, mtu); 89 RFCOMM_TRACE_API6 ("RFCOMM_CreateConnection() BDA: %02x-%02x-%02x-%02x-%02x-%02x", 90 bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); 91 92 *p_handle = 0; 93 94 if (( scn == 0 )||(scn >= PORT_MAX_RFC_PORTS )) 95 { 96 /* Server Channel Number(SCN) should be in range 1...30 */ 97 RFCOMM_TRACE_ERROR0 ("RFCOMM_CreateConnection - invalid SCN"); 98 return (PORT_INVALID_SCN); 99 } 100 101 /* For client that originate connection on the existing none initiator */ 102 /* multiplexer channel DLCI should be odd */ 103 if (p_mcb && !p_mcb->is_initiator && !is_server) 104 dlci = (scn << 1) + 1; 105 else 106 dlci = (scn << 1); 107 108 /* For the server side always allocate a new port. On the client side */ 109 /* do not allow the same (dlci, bd_addr) to be opened twice by application */ 110 if (!is_server && ((p_port = port_find_port (dlci, bd_addr)) != NULL)) 111 { 112 /* if existing port is also a client port */ 113 if (p_port->is_server == FALSE) 114 { 115 RFCOMM_TRACE_ERROR3 ("RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, MCB state:%d", 116 p_port->state, p_port->rfc.state, p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0); 117 return (PORT_ALREADY_OPENED); 118 } 119 } 120 121 if ((p_port = port_allocate_port (dlci, bd_addr)) == NULL) 122 { 123 RFCOMM_TRACE_WARNING0 ("RFCOMM_CreateConnection - no resources"); 124 return (PORT_NO_RESOURCES); 125 } 126 127 p_port->default_signal_state = (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON); 128 129 switch (uuid) 130 { 131 case UUID_PROTOCOL_OBEX: 132 p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE; 133 break; 134 case UUID_SERVCLASS_SERIAL_PORT: 135 p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE; 136 break; 137 case UUID_SERVCLASS_LAN_ACCESS_USING_PPP: 138 p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE; 139 break; 140 case UUID_SERVCLASS_DIALUP_NETWORKING: 141 case UUID_SERVCLASS_FAX: 142 p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE; 143 break; 144 } 145 146 RFCOMM_TRACE_EVENT2 ("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, p_port->default_signal_state); 147 148 *p_handle = p_port->inx; 149 150 p_port->state = PORT_STATE_OPENING; 151 p_port->uuid = uuid; 152 p_port->is_server = is_server; 153 p_port->scn = scn; 154 p_port->ev_mask = 0; 155 156 /* If the MTU is not specified (0), keep MTU decision until the 157 * PN frame has to be send 158 * at that time connection should be established and we 159 * will know for sure our prefered MTU 160 */ 161 162 rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD; 163 164 if (mtu) 165 p_port->mtu = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu; 166 else 167 p_port->mtu = rfcomm_mtu; 168 169 /* server doesn't need to release port when closing */ 170 if( is_server ) 171 { 172 p_port->keep_port_handle = TRUE; 173 174 /* keep mtu that user asked, p_port->mtu could be updated during param negotiation */ 175 p_port->keep_mtu = p_port->mtu; 176 } 177 178 p_port->local_ctrl.modem_signal = p_port->default_signal_state; 179 p_port->local_ctrl.fc = FALSE; 180 181 p_port->p_mgmt_callback = p_mgmt_cb; 182 183 for (i = 0; i < BD_ADDR_LEN; i++) 184 p_port->bd_addr[i] = bd_addr[i]; 185 186 /* If this is not initiator of the connection need to just wait */ 187 if (p_port->is_server) 188 { 189 return (PORT_SUCCESS); 190 } 191 192 /* Open will be continued after security checks are passed */ 193 return port_open_continue (p_port); 194 } 195 196 197 /******************************************************************************* 198 ** 199 ** Function RFCOMM_RemoveConnection 200 ** 201 ** Description This function is called to close the specified connection. 202 ** 203 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 204 ** 205 *******************************************************************************/ 206 int RFCOMM_RemoveConnection (UINT16 handle) 207 { 208 tPORT *p_port; 209 210 RFCOMM_TRACE_API1 ("RFCOMM_RemoveConnection() handle:%d", handle); 211 212 /* Check if handle is valid to avoid crashing */ 213 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 214 { 215 RFCOMM_TRACE_ERROR1 ("RFCOMM_RemoveConnection() BAD handle:%d", handle); 216 return (PORT_BAD_HANDLE); 217 } 218 p_port = &rfc_cb.port.port[handle - 1]; 219 220 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 221 { 222 RFCOMM_TRACE_EVENT1 ("RFCOMM_RemoveConnection() Not opened:%d", handle); 223 return (PORT_SUCCESS); 224 } 225 226 p_port->state = PORT_STATE_CLOSING; 227 228 port_start_close (p_port); 229 230 return (PORT_SUCCESS); 231 } 232 233 /******************************************************************************* 234 ** 235 ** Function RFCOMM_RemoveServer 236 ** 237 ** Description This function is called to close the server port. 238 ** 239 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 240 ** 241 *******************************************************************************/ 242 int RFCOMM_RemoveServer (UINT16 handle) 243 { 244 tPORT *p_port; 245 246 RFCOMM_TRACE_API1 ("RFCOMM_RemoveServer() handle:%d", handle); 247 248 /* Check if handle is valid to avoid crashing */ 249 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 250 { 251 RFCOMM_TRACE_ERROR1 ("RFCOMM_RemoveServer() BAD handle:%d", handle); 252 return (PORT_BAD_HANDLE); 253 } 254 p_port = &rfc_cb.port.port[handle - 1]; 255 256 /* Do not report any events to the client any more. */ 257 p_port->p_mgmt_callback = NULL; 258 259 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 260 { 261 RFCOMM_TRACE_EVENT1 ("RFCOMM_RemoveServer() Not opened:%d", handle); 262 return (PORT_SUCCESS); 263 } 264 265 /* this port will be deallocated after closing */ 266 p_port->keep_port_handle = FALSE; 267 p_port->state = PORT_STATE_CLOSING; 268 269 port_start_close (p_port); 270 271 return (PORT_SUCCESS); 272 } 273 274 /******************************************************************************* 275 ** 276 ** Function PORT_SetEventCallback 277 ** 278 ** Description This function is called to provide an address of the 279 ** function which will be called when one of the events 280 ** specified in the mask occures. 281 ** 282 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 283 ** p_callback - address of the callback function which should 284 ** be called from the RFCOMM when an event 285 ** specified in the mask occures. 286 ** 287 ** 288 *******************************************************************************/ 289 int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb) 290 { 291 tPORT *p_port; 292 293 /* Check if handle is valid to avoid crashing */ 294 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) 295 { 296 return (PORT_BAD_HANDLE); 297 } 298 299 p_port = &rfc_cb.port.port[port_handle - 1]; 300 301 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 302 { 303 return (PORT_NOT_OPENED); 304 } 305 306 RFCOMM_TRACE_API1 ("PORT_SetEventCallback() handle:%d", port_handle); 307 308 p_port->p_callback = p_port_cb; 309 310 return (PORT_SUCCESS); 311 } 312 313 314 /******************************************************************************* 315 ** 316 ** Function PORT_SetDataCallback 317 ** 318 ** Description This function is when a data packet is received 319 ** 320 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 321 ** p_callback - address of the callback function which should 322 ** be called from the RFCOMM when data packet 323 ** is received. 324 ** 325 ** 326 *******************************************************************************/ 327 int PORT_SetDataCallback (UINT16 port_handle, tPORT_DATA_CALLBACK *p_port_cb) 328 { 329 tPORT *p_port; 330 331 RFCOMM_TRACE_API2 ("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle, p_port_cb); 332 333 /* Check if handle is valid to avoid crashing */ 334 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) 335 { 336 return (PORT_BAD_HANDLE); 337 } 338 339 p_port = &rfc_cb.port.port[port_handle - 1]; 340 341 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 342 { 343 return (PORT_NOT_OPENED); 344 } 345 346 p_port->p_data_callback = p_port_cb; 347 348 return (PORT_SUCCESS); 349 } 350 /******************************************************************************* 351 ** 352 ** Function PORT_SetCODataCallback 353 ** 354 ** Description This function is when a data packet is received 355 ** 356 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 357 ** p_callback - address of the callback function which should 358 ** be called from the RFCOMM when data packet 359 ** is received. 360 ** 361 ** 362 *******************************************************************************/ 363 int PORT_SetDataCOCallback (UINT16 port_handle, tPORT_DATA_CO_CALLBACK *p_port_cb) 364 { 365 tPORT *p_port; 366 367 RFCOMM_TRACE_API2 ("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle, p_port_cb); 368 369 /* Check if handle is valid to avoid crashing */ 370 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) 371 { 372 return (PORT_BAD_HANDLE); 373 } 374 375 p_port = &rfc_cb.port.port[port_handle - 1]; 376 377 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 378 { 379 return (PORT_NOT_OPENED); 380 } 381 382 p_port->p_data_co_callback = p_port_cb; 383 384 return (PORT_SUCCESS); 385 } 386 387 388 389 /******************************************************************************* 390 ** 391 ** Function PORT_SetEventMask 392 ** 393 ** Description This function is called to close the specified connection. 394 ** 395 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 396 ** mask - Bitmask of the events the host is interested in 397 ** 398 *******************************************************************************/ 399 int PORT_SetEventMask (UINT16 port_handle, UINT32 mask) 400 { 401 tPORT *p_port; 402 403 RFCOMM_TRACE_API2 ("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask); 404 405 /* Check if handle is valid to avoid crashing */ 406 if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS)) 407 { 408 return (PORT_BAD_HANDLE); 409 } 410 411 p_port = &rfc_cb.port.port[port_handle - 1]; 412 413 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 414 { 415 return (PORT_NOT_OPENED); 416 } 417 418 p_port->ev_mask = mask; 419 420 return (PORT_SUCCESS); 421 } 422 423 424 /******************************************************************************* 425 ** 426 ** Function PORT_CheckConnection 427 ** 428 ** Description This function returns PORT_SUCCESS if connection referenced 429 ** by handle is up and running 430 ** 431 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 432 ** bd_addr - OUT bd_addr of the peer 433 ** p_lcid - OUT L2CAP's LCID 434 ** 435 *******************************************************************************/ 436 int PORT_CheckConnection (UINT16 handle, BD_ADDR bd_addr, UINT16 *p_lcid) 437 { 438 tPORT *p_port; 439 440 RFCOMM_TRACE_API1 ("PORT_CheckConnection() handle:%d", handle); 441 442 /* Check if handle is valid to avoid crashing */ 443 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 444 { 445 return (PORT_BAD_HANDLE); 446 } 447 448 p_port = &rfc_cb.port.port[handle - 1]; 449 450 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 451 { 452 return (PORT_NOT_OPENED); 453 } 454 455 if (!p_port->rfc.p_mcb 456 || !p_port->rfc.p_mcb->peer_ready 457 || (p_port->rfc.state != RFC_STATE_OPENED)) 458 { 459 return (PORT_LINE_ERR); 460 } 461 462 memcpy (bd_addr, p_port->rfc.p_mcb->bd_addr, BD_ADDR_LEN); 463 if (p_lcid) 464 *p_lcid = p_port->rfc.p_mcb->lcid; 465 466 return (PORT_SUCCESS); 467 } 468 469 /******************************************************************************* 470 ** 471 ** Function PORT_IsOpening 472 ** 473 ** Description This function returns TRUE if there is any RFCOMM connection 474 ** opening in process. 475 ** 476 ** Parameters: TRUE if any connection opening is found 477 ** bd_addr - bd_addr of the peer 478 ** 479 *******************************************************************************/ 480 BOOLEAN PORT_IsOpening (BD_ADDR bd_addr) 481 { 482 UINT8 xx, yy; 483 tRFC_MCB *p_mcb = NULL; 484 tPORT *p_port; 485 BOOLEAN found_port; 486 487 /* Check for any rfc_mcb which is in the middle of opening. */ 488 for (xx = 0; xx < MAX_BD_CONNECTIONS; xx++) 489 { 490 if ((rfc_cb.port.rfc_mcb[xx].state > RFC_MX_STATE_IDLE) && 491 (rfc_cb.port.rfc_mcb[xx].state < RFC_MX_STATE_CONNECTED)) 492 { 493 memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN); 494 return TRUE; 495 } 496 497 if (rfc_cb.port.rfc_mcb[xx].state == RFC_MX_STATE_CONNECTED) 498 { 499 found_port = FALSE; 500 p_mcb = &rfc_cb.port.rfc_mcb[xx]; 501 p_port = &rfc_cb.port.port[0]; 502 503 for (yy = 0; yy < MAX_RFC_PORTS; yy++, p_port++) 504 { 505 if (p_port->rfc.p_mcb == p_mcb) 506 { 507 found_port = TRUE; 508 break; 509 } 510 } 511 512 if ((!found_port) || 513 (found_port && (p_port->rfc.state < RFC_STATE_OPENED))) 514 { 515 /* Port is not established yet. */ 516 memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN); 517 return TRUE; 518 } 519 } 520 } 521 522 return FALSE; 523 } 524 525 /******************************************************************************* 526 ** 527 ** Function PORT_SetState 528 ** 529 ** Description This function configures connection according to the 530 ** specifications in the tPORT_STATE structure. 531 ** 532 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 533 ** p_settings - Pointer to a tPORT_STATE structure containing 534 ** configuration information for the connection. 535 ** 536 ** 537 *******************************************************************************/ 538 int PORT_SetState (UINT16 handle, tPORT_STATE *p_settings) 539 { 540 tPORT *p_port; 541 UINT8 baud_rate; 542 543 RFCOMM_TRACE_API1 ("PORT_SetState() handle:%d", handle); 544 545 /* Check if handle is valid to avoid crashing */ 546 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 547 { 548 return (PORT_BAD_HANDLE); 549 } 550 551 p_port = &rfc_cb.port.port[handle - 1]; 552 553 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 554 { 555 return (PORT_NOT_OPENED); 556 } 557 558 if (p_port->line_status) 559 { 560 return (PORT_LINE_ERR); 561 } 562 563 RFCOMM_TRACE_API2 ("PORT_SetState() handle:%d FC_TYPE:0x%x", handle, 564 p_settings->fc_type); 565 566 baud_rate = p_port->user_port_pars.baud_rate; 567 p_port->user_port_pars = *p_settings; 568 569 /* for now we've been asked to pass only baud rate */ 570 if (baud_rate != p_settings->baud_rate) 571 { 572 port_start_par_neg (p_port); 573 } 574 return (PORT_SUCCESS); 575 } 576 577 /******************************************************************************* 578 ** 579 ** Function PORT_GetRxQueueCnt 580 ** 581 ** Description This function return number of buffers on the rx queue. 582 ** 583 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 584 ** p_rx_queue_count - Pointer to return queue count in. 585 ** 586 *******************************************************************************/ 587 int PORT_GetRxQueueCnt (UINT16 handle, UINT16 *p_rx_queue_count) 588 { 589 tPORT *p_port; 590 591 RFCOMM_TRACE_API1 ("PORT_GetRxQueueCnt() handle:%d", handle); 592 593 /* Check if handle is valid to avoid crashing */ 594 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 595 { 596 return (PORT_BAD_HANDLE); 597 } 598 599 p_port = &rfc_cb.port.port[handle - 1]; 600 601 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 602 { 603 return (PORT_NOT_OPENED); 604 } 605 606 if (p_port->line_status) 607 { 608 return (PORT_LINE_ERR); 609 } 610 611 *p_rx_queue_count = p_port->rx.queue_size; 612 613 RFCOMM_TRACE_API2 ("PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d", 614 *p_rx_queue_count, p_port->rx.queue_size); 615 616 return (PORT_SUCCESS); 617 } 618 619 /******************************************************************************* 620 ** 621 ** Function PORT_GetState 622 ** 623 ** Description This function is called to fill tPORT_STATE structure 624 ** with the curremt control settings for the port 625 ** 626 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 627 ** p_settings - Pointer to a tPORT_STATE structure in which 628 ** configuration information is returned. 629 ** 630 *******************************************************************************/ 631 int PORT_GetState (UINT16 handle, tPORT_STATE *p_settings) 632 { 633 tPORT *p_port; 634 635 RFCOMM_TRACE_API1 ("PORT_GetState() handle:%d", handle); 636 637 /* Check if handle is valid to avoid crashing */ 638 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 639 { 640 return (PORT_BAD_HANDLE); 641 } 642 643 p_port = &rfc_cb.port.port[handle - 1]; 644 645 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 646 { 647 return (PORT_NOT_OPENED); 648 } 649 650 if (p_port->line_status) 651 { 652 return (PORT_LINE_ERR); 653 } 654 655 *p_settings = p_port->user_port_pars; 656 return (PORT_SUCCESS); 657 } 658 659 660 /******************************************************************************* 661 ** 662 ** Function PORT_Control 663 ** 664 ** Description This function directs a specified connection to pass control 665 ** control information to the peer device. 666 ** 667 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 668 ** signal = specify the function to be passed 669 ** 670 *******************************************************************************/ 671 int PORT_Control (UINT16 handle, UINT8 signal) 672 { 673 tPORT *p_port; 674 UINT8 old_modem_signal; 675 676 RFCOMM_TRACE_API2 ("PORT_Control() handle:%d signal:0x%x", handle, signal); 677 678 /* Check if handle is valid to avoid crashing */ 679 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 680 { 681 return (PORT_BAD_HANDLE); 682 } 683 684 p_port = &rfc_cb.port.port[handle - 1]; 685 686 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 687 { 688 return (PORT_NOT_OPENED); 689 } 690 691 old_modem_signal = p_port->local_ctrl.modem_signal; 692 p_port->local_ctrl.break_signal = 0; 693 694 switch (signal) 695 { 696 case PORT_SET_CTSRTS: 697 p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON; 698 break; 699 700 case PORT_CLR_CTSRTS: 701 p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON; 702 break; 703 704 case PORT_SET_DTRDSR: 705 p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON; 706 break; 707 708 case PORT_CLR_DTRDSR: 709 p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON; 710 break; 711 712 case PORT_SET_RI: 713 p_port->local_ctrl.modem_signal |= PORT_RING_ON; 714 break; 715 716 case PORT_CLR_RI: 717 p_port->local_ctrl.modem_signal &= ~PORT_RING_ON; 718 break; 719 720 case PORT_SET_DCD: 721 p_port->local_ctrl.modem_signal |= PORT_DCD_ON; 722 break; 723 724 case PORT_CLR_DCD: 725 p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON; 726 break; 727 } 728 729 if (signal == PORT_BREAK) 730 p_port->local_ctrl.break_signal = PORT_BREAK_DURATION; 731 else if (p_port->local_ctrl.modem_signal == old_modem_signal) 732 return (PORT_SUCCESS); 733 734 port_start_control (p_port); 735 736 RFCOMM_TRACE_EVENT4 ("PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d", 737 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0), 738 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0), 739 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0), 740 ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0)); 741 742 return (PORT_SUCCESS); 743 } 744 745 746 /******************************************************************************* 747 ** 748 ** Function PORT_FlowControl 749 ** 750 ** Description This function directs a specified connection to pass 751 ** flow control message to the peer device. Enable flag passed 752 ** shows if port can accept more data. 753 ** 754 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 755 ** enable - enables data flow 756 ** 757 *******************************************************************************/ 758 int PORT_FlowControl (UINT16 handle, BOOLEAN enable) 759 { 760 tPORT *p_port; 761 BOOLEAN old_fc; 762 UINT32 events; 763 764 RFCOMM_TRACE_API2 ("PORT_FlowControl() handle:%d enable: %d", handle, enable); 765 766 /* Check if handle is valid to avoid crashing */ 767 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 768 { 769 return (PORT_BAD_HANDLE); 770 } 771 772 p_port = &rfc_cb.port.port[handle - 1]; 773 774 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 775 { 776 return (PORT_NOT_OPENED); 777 } 778 779 if (!p_port->rfc.p_mcb) 780 { 781 return (PORT_NOT_OPENED); 782 } 783 784 p_port->rx.user_fc = !enable; 785 786 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) 787 { 788 if (!p_port->rx.user_fc) 789 { 790 port_flow_control_peer(p_port, TRUE, 0); 791 } 792 } 793 else 794 { 795 old_fc = p_port->local_ctrl.fc; 796 797 /* FC is set if user is set or peer is set */ 798 p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc); 799 800 if (p_port->local_ctrl.fc != old_fc) 801 port_start_control (p_port); 802 } 803 804 /* Need to take care of the case when we could not deliver events */ 805 /* to the application because we were flow controlled */ 806 if (enable && (p_port->rx.queue_size != 0)) 807 { 808 events = PORT_EV_RXCHAR; 809 if (p_port->rx_flag_ev_pending) 810 { 811 p_port->rx_flag_ev_pending = FALSE; 812 events |= PORT_EV_RXFLAG; 813 } 814 815 events &= p_port->ev_mask; 816 if (p_port->p_callback && events) 817 { 818 p_port->p_callback (events, p_port->inx); 819 } 820 } 821 return (PORT_SUCCESS); 822 } 823 824 825 /******************************************************************************* 826 ** 827 ** Function PORT_GetModemStatus 828 ** 829 ** Description This function retrieves modem control signals. Normally 830 ** application will call this function after a callback 831 ** function is called with notification that one of signals 832 ** has been changed. 833 ** 834 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 835 ** p_signal - specify the pointer to control signals info 836 ** 837 *******************************************************************************/ 838 int PORT_GetModemStatus (UINT16 handle, UINT8 *p_signal) 839 { 840 tPORT *p_port; 841 842 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 843 { 844 return (PORT_BAD_HANDLE); 845 } 846 847 p_port = &rfc_cb.port.port[handle - 1]; 848 849 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 850 { 851 return (PORT_NOT_OPENED); 852 } 853 854 *p_signal = p_port->peer_ctrl.modem_signal; 855 856 RFCOMM_TRACE_API2 ("PORT_GetModemStatus() handle:%d signal:%x", handle, *p_signal); 857 858 return (PORT_SUCCESS); 859 } 860 861 862 /******************************************************************************* 863 ** 864 ** Function PORT_ClearError 865 ** 866 ** Description This function retreives information about a communications 867 ** error and reports current status of a connection. The 868 ** function should be called when an error occures to clear 869 ** the connection error flag and to enable additional read 870 ** and write operations. 871 ** 872 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 873 ** p_errors - pointer of the variable to receive error codes 874 ** p_status - pointer to the tPORT_STATUS structur to receive 875 ** connection status 876 ** 877 *******************************************************************************/ 878 int PORT_ClearError (UINT16 handle, UINT16 *p_errors, tPORT_STATUS *p_status) 879 { 880 tPORT *p_port; 881 882 RFCOMM_TRACE_API1 ("PORT_ClearError() handle:%d", handle); 883 884 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 885 { 886 return (PORT_BAD_HANDLE); 887 } 888 889 p_port = &rfc_cb.port.port[handle - 1]; 890 891 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 892 { 893 return (PORT_NOT_OPENED); 894 } 895 896 *p_errors = p_port->line_status; 897 898 /* This is the only call to clear error status. We can not clear */ 899 /* connection failed status. To clean it port should be closed and reopened */ 900 p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED); 901 902 PORT_GetQueueStatus (handle, p_status); 903 return (PORT_SUCCESS); 904 } 905 906 907 /******************************************************************************* 908 ** 909 ** Function PORT_SendError 910 ** 911 ** Description This function send a communications error to the peer device 912 ** 913 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 914 ** errors - receive error codes 915 ** 916 *******************************************************************************/ 917 int PORT_SendError (UINT16 handle, UINT8 errors) 918 { 919 tPORT *p_port; 920 921 RFCOMM_TRACE_API2 ("PORT_SendError() handle:%d errors:0x%x", handle, errors); 922 923 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 924 { 925 return (PORT_BAD_HANDLE); 926 } 927 928 p_port = &rfc_cb.port.port[handle - 1]; 929 930 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 931 { 932 return (PORT_NOT_OPENED); 933 } 934 935 if (!p_port->rfc.p_mcb) 936 { 937 return (PORT_NOT_OPENED); 938 } 939 940 RFCOMM_LineStatusReq (p_port->rfc.p_mcb, p_port->dlci, errors); 941 return (PORT_SUCCESS); 942 } 943 944 945 /******************************************************************************* 946 ** 947 ** Function PORT_GetQueueStatus 948 ** 949 ** Description This function reports current status of a connection. 950 ** 951 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 952 ** p_status - pointer to the tPORT_STATUS structur to receive 953 ** connection status 954 ** 955 *******************************************************************************/ 956 int PORT_GetQueueStatus (UINT16 handle, tPORT_STATUS *p_status) 957 { 958 tPORT *p_port; 959 960 /* RFCOMM_TRACE_API1 ("PORT_GetQueueStatus() handle:%d", handle); */ 961 962 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 963 { 964 return (PORT_BAD_HANDLE); 965 } 966 967 p_port = &rfc_cb.port.port[handle - 1]; 968 969 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 970 { 971 return (PORT_NOT_OPENED); 972 } 973 974 p_status->in_queue_size = (UINT16) p_port->rx.queue_size; 975 p_status->out_queue_size = (UINT16) p_port->tx.queue_size; 976 977 p_status->mtu_size = (UINT16) p_port->peer_mtu; 978 979 p_status->flags = 0; 980 981 if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON)) 982 p_status->flags |= PORT_FLAG_CTS_HOLD; 983 984 if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON)) 985 p_status->flags |= PORT_FLAG_DSR_HOLD; 986 987 if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON)) 988 p_status->flags |= PORT_FLAG_RLSD_HOLD; 989 990 return (PORT_SUCCESS); 991 } 992 993 994 /******************************************************************************* 995 ** 996 ** Function PORT_Purge 997 ** 998 ** Description This function discards all the data from the output or 999 ** input queues of the specified connection. 1000 ** 1001 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1002 ** purge_flags - specify the action to take. 1003 ** 1004 *******************************************************************************/ 1005 int PORT_Purge (UINT16 handle, UINT8 purge_flags) 1006 { 1007 tPORT *p_port; 1008 BT_HDR *p_buf; 1009 UINT16 count; 1010 UINT32 events; 1011 1012 RFCOMM_TRACE_API2 ("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags); 1013 1014 /* Check if handle is valid to avoid crashing */ 1015 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1016 { 1017 return (PORT_BAD_HANDLE); 1018 } 1019 1020 p_port = &rfc_cb.port.port[handle - 1]; 1021 1022 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1023 { 1024 return (PORT_NOT_OPENED); 1025 } 1026 1027 if (purge_flags & PORT_PURGE_RXCLEAR) 1028 { 1029 PORT_SCHEDULE_LOCK; /* to prevent missing credit */ 1030 1031 count = p_port->rx.queue.count; 1032 1033 while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue)) != NULL) 1034 GKI_freebuf (p_buf); 1035 1036 p_port->rx.queue_size = 0; 1037 1038 PORT_SCHEDULE_UNLOCK; 1039 1040 /* If we flowed controlled peer based on rx_queue size enable data again */ 1041 if (count) 1042 port_flow_control_peer (p_port, TRUE, count); 1043 } 1044 1045 if (purge_flags & PORT_PURGE_TXCLEAR) 1046 { 1047 PORT_SCHEDULE_LOCK; /* to prevent tx.queue_size from being negative */ 1048 1049 while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->tx.queue)) != NULL) 1050 GKI_freebuf (p_buf); 1051 1052 p_port->tx.queue_size = 0; 1053 1054 PORT_SCHEDULE_UNLOCK; 1055 1056 events = PORT_EV_TXEMPTY; 1057 1058 events |= port_flow_control_user (p_port); 1059 1060 events &= p_port->ev_mask; 1061 1062 if ((p_port->p_callback != NULL) && events) 1063 (p_port->p_callback)(events, p_port->inx); 1064 } 1065 1066 return (PORT_SUCCESS); 1067 } 1068 1069 1070 /******************************************************************************* 1071 ** 1072 ** Function PORT_ReadData 1073 ** 1074 ** Description Normally not GKI aware application will call this function 1075 ** after receiving PORT_EV_RXCHAR event. 1076 ** 1077 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1078 ** p_data - Data area 1079 ** max_len - Byte count requested 1080 ** p_len - Byte count received 1081 ** 1082 *******************************************************************************/ 1083 int PORT_ReadData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len) 1084 { 1085 tPORT *p_port; 1086 BT_HDR *p_buf; 1087 UINT16 count; 1088 1089 RFCOMM_TRACE_API2 ("PORT_ReadData() handle:%d max_len:%d", handle, max_len); 1090 1091 /* Initialize this in case of an error */ 1092 *p_len = 0; 1093 1094 /* Check if handle is valid to avoid crashing */ 1095 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1096 { 1097 return (PORT_BAD_HANDLE); 1098 } 1099 1100 p_port = &rfc_cb.port.port[handle - 1]; 1101 1102 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1103 { 1104 return (PORT_NOT_OPENED); 1105 } 1106 1107 if (p_port->line_status) 1108 { 1109 return (PORT_LINE_ERR); 1110 } 1111 1112 p_buf = (BT_HDR *)GKI_getfirst (&p_port->rx.queue); 1113 if (!p_buf) 1114 return (PORT_SUCCESS); 1115 1116 count = 0; 1117 1118 while (max_len && p_buf) 1119 { 1120 if (p_buf->len > max_len) 1121 { 1122 memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, max_len); 1123 p_buf->offset += max_len; 1124 p_buf->len -= max_len; 1125 1126 *p_len += max_len; 1127 1128 PORT_SCHEDULE_LOCK; 1129 1130 p_port->rx.queue_size -= max_len; 1131 1132 PORT_SCHEDULE_UNLOCK; 1133 1134 break; 1135 } 1136 else 1137 { 1138 memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len); 1139 1140 *p_len += p_buf->len; 1141 max_len -= p_buf->len; 1142 1143 PORT_SCHEDULE_LOCK; 1144 1145 p_port->rx.queue_size -= p_buf->len; 1146 1147 if (max_len) 1148 { 1149 p_data += p_buf->len; 1150 p_buf = (BT_HDR *)GKI_getnext (p_buf); 1151 } 1152 1153 GKI_freebuf (GKI_dequeue (&p_port->rx.queue)); 1154 1155 PORT_SCHEDULE_UNLOCK; 1156 1157 count++; 1158 } 1159 } 1160 1161 if (*p_len == 1) 1162 { 1163 RFCOMM_TRACE_EVENT3 ("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size, *p_len, (p_data[0])); 1164 } 1165 else 1166 { 1167 RFCOMM_TRACE_EVENT2 ("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size, *p_len); 1168 } 1169 1170 /* If rfcomm suspended traffic from the peer based on the rx_queue_size */ 1171 /* check if it can be resumed now */ 1172 port_flow_control_peer (p_port, TRUE, count); 1173 1174 return (PORT_SUCCESS); 1175 } 1176 1177 1178 /******************************************************************************* 1179 ** 1180 ** Function PORT_Read 1181 ** 1182 ** Description Normally application will call this function after receiving 1183 ** PORT_EV_RXCHAR event. 1184 ** 1185 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1186 ** pp_buf - pointer to address of buffer with data, 1187 ** 1188 *******************************************************************************/ 1189 int PORT_Read (UINT16 handle, BT_HDR **pp_buf) 1190 { 1191 tPORT *p_port; 1192 BT_HDR *p_buf; 1193 1194 RFCOMM_TRACE_API1 ("PORT_Read() handle:%d", handle); 1195 1196 /* Check if handle is valid to avoid crashing */ 1197 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1198 { 1199 return (PORT_BAD_HANDLE); 1200 } 1201 p_port = &rfc_cb.port.port[handle - 1]; 1202 1203 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1204 { 1205 return (PORT_NOT_OPENED); 1206 } 1207 1208 if (p_port->line_status) 1209 { 1210 return (PORT_LINE_ERR); 1211 } 1212 1213 PORT_SCHEDULE_LOCK; 1214 1215 p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue); 1216 if (p_buf) 1217 { 1218 p_port->rx.queue_size -= p_buf->len; 1219 1220 PORT_SCHEDULE_UNLOCK; 1221 1222 /* If rfcomm suspended traffic from the peer based on the rx_queue_size */ 1223 /* check if it can be resumed now */ 1224 port_flow_control_peer (p_port, TRUE, 1); 1225 } 1226 else 1227 { 1228 PORT_SCHEDULE_UNLOCK; 1229 } 1230 1231 *pp_buf = p_buf; 1232 return (PORT_SUCCESS); 1233 } 1234 1235 1236 /******************************************************************************* 1237 ** 1238 ** Function port_write 1239 ** 1240 ** Description This function when a data packet is received from the apper 1241 ** layer task. 1242 ** 1243 ** Parameters: p_port - pointer to address of port control block 1244 ** p_buf - pointer to address of buffer with data, 1245 ** 1246 *******************************************************************************/ 1247 static int port_write (tPORT *p_port, BT_HDR *p_buf) 1248 { 1249 /* We should not allow to write data in to server port when connection is not opened */ 1250 if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED)) 1251 { 1252 GKI_freebuf (p_buf); 1253 return (PORT_CLOSED); 1254 } 1255 1256 /* Keep the data in pending queue if peer does not allow data, or */ 1257 /* Peer is not ready or Port is not yet opened or initial port control */ 1258 /* command has not been sent */ 1259 if (p_port->tx.peer_fc 1260 || !p_port->rfc.p_mcb 1261 || !p_port->rfc.p_mcb->peer_ready 1262 || (p_port->rfc.state != RFC_STATE_OPENED) 1263 || ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) != 1264 (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED))) 1265 { 1266 if ((p_port->tx.queue_size > PORT_TX_CRITICAL_WM) 1267 || (p_port->tx.queue.count > PORT_TX_BUF_CRITICAL_WM)) 1268 { 1269 RFCOMM_TRACE_WARNING1 ("PORT_Write: Queue size: %d", 1270 p_port->tx.queue_size); 1271 1272 GKI_freebuf (p_buf); 1273 1274 if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR)) 1275 p_port->p_callback (PORT_EV_ERR, p_port->inx); 1276 1277 return (PORT_TX_FULL); 1278 } 1279 1280 RFCOMM_TRACE_EVENT4 ("PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d ctrl_state %x", 1281 p_port->tx.peer_fc, 1282 (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready), 1283 p_port->rfc.state, 1284 p_port->port_ctrl); 1285 1286 GKI_enqueue (&p_port->tx.queue, p_buf); 1287 p_port->tx.queue_size += p_buf->len; 1288 1289 return (PORT_CMD_PENDING); 1290 } 1291 else 1292 { 1293 RFCOMM_TRACE_EVENT0 ("PORT_Write : Data is being sent"); 1294 1295 RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf); 1296 return (PORT_SUCCESS); 1297 } 1298 } 1299 1300 /******************************************************************************* 1301 ** 1302 ** Function PORT_Write 1303 ** 1304 ** Description This function when a data packet is received from the apper 1305 ** layer task. 1306 ** 1307 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1308 ** pp_buf - pointer to address of buffer with data, 1309 ** 1310 *******************************************************************************/ 1311 int PORT_Write (UINT16 handle, BT_HDR *p_buf) 1312 { 1313 tPORT *p_port; 1314 UINT32 event = 0; 1315 int rc; 1316 1317 RFCOMM_TRACE_API1 ("PORT_Write() handle:%d", handle); 1318 1319 /* Check if handle is valid to avoid crashing */ 1320 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1321 { 1322 GKI_freebuf (p_buf); 1323 return (PORT_BAD_HANDLE); 1324 } 1325 1326 p_port = &rfc_cb.port.port[handle - 1]; 1327 1328 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1329 { 1330 GKI_freebuf (p_buf); 1331 return (PORT_NOT_OPENED); 1332 } 1333 1334 if (p_port->line_status) 1335 { 1336 RFCOMM_TRACE_WARNING1 ("PORT_Write: Data dropped line_status:0x%x", 1337 p_port->line_status); 1338 GKI_freebuf (p_buf); 1339 return (PORT_LINE_ERR); 1340 } 1341 1342 rc = port_write (p_port, p_buf); 1343 event |= port_flow_control_user (p_port); 1344 1345 switch (rc) 1346 { 1347 case PORT_TX_FULL: 1348 event |= PORT_EV_ERR; 1349 break; 1350 1351 case PORT_SUCCESS: 1352 event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY); 1353 break; 1354 } 1355 /* Mask out all events that are not of interest to user */ 1356 event &= p_port->ev_mask; 1357 1358 /* Send event to the application */ 1359 if (p_port->p_callback && event) 1360 (p_port->p_callback)(event, p_port->inx); 1361 1362 return (PORT_SUCCESS); 1363 } 1364 /******************************************************************************* 1365 ** 1366 ** Function PORT_WriteDataCO 1367 ** 1368 ** Description Normally not GKI aware application will call this function 1369 ** to send data to the port by callout functions 1370 ** 1371 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1372 ** fd - socket fd 1373 ** p_len - Byte count returned 1374 ** 1375 *******************************************************************************/ 1376 int PORT_WriteDataCO (UINT16 handle, int* p_len) 1377 { 1378 1379 tPORT *p_port; 1380 BT_HDR *p_buf; 1381 UINT32 event = 0; 1382 int rc = 0; 1383 UINT16 length; 1384 1385 RFCOMM_TRACE_API1 ("PORT_WriteDataCO() handle:%d", handle); 1386 int written; 1387 *p_len = 0; 1388 1389 /* Check if handle is valid to avoid crashing */ 1390 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1391 { 1392 return (PORT_BAD_HANDLE); 1393 } 1394 p_port = &rfc_cb.port.port[handle - 1]; 1395 1396 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1397 { 1398 RFCOMM_TRACE_WARNING1 ("PORT_WriteDataByFd() no port state:%d", p_port->state); 1399 return (PORT_NOT_OPENED); 1400 } 1401 1402 if (!p_port->peer_mtu) 1403 { 1404 RFCOMM_TRACE_ERROR1 ("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu); 1405 return (PORT_UNKNOWN_ERROR); 1406 } 1407 int available = 0; 1408 //if(ioctl(fd, FIONREAD, &available) < 0) 1409 if(p_port->p_data_co_callback(handle, (UINT8*)&available, sizeof(available), 1410 DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE) == FALSE) 1411 { 1412 RFCOMM_TRACE_ERROR1("p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, available:%d", available); 1413 return (PORT_UNKNOWN_ERROR); 1414 } 1415 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */ 1416 length = RFCOMM_DATA_POOL_BUF_SIZE - 1417 (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD); 1418 1419 /* If there are buffers scheduled for transmission check if requested */ 1420 /* data fits into the end of the queue */ 1421 PORT_SCHEDULE_LOCK; 1422 1423 if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != NULL) 1424 && (((int)p_buf->len + available) <= (int)p_port->peer_mtu) 1425 && (((int)p_buf->len + available) <= (int)length)) 1426 { 1427 //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, available, 0) != available) 1428 if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, 1429 available, DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE) 1430 1431 { 1432 error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, available:%d", available); 1433 return (PORT_UNKNOWN_ERROR); 1434 } 1435 //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len); 1436 p_port->tx.queue_size += (UINT16)available; 1437 1438 *p_len = available; 1439 p_buf->len += (UINT16)available; 1440 1441 PORT_SCHEDULE_UNLOCK; 1442 1443 return (PORT_SUCCESS); 1444 } 1445 1446 PORT_SCHEDULE_UNLOCK; 1447 1448 //int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu; 1449 1450 //max_read = available < max_read ? available : max_read; 1451 1452 while (available) 1453 { 1454 /* if we're over buffer high water mark, we're done */ 1455 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) 1456 || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM)) 1457 break; 1458 1459 /* continue with rfcomm data write */ 1460 p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID); 1461 if (!p_buf) 1462 break; 1463 1464 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET; 1465 p_buf->layer_specific = handle; 1466 1467 if (p_port->peer_mtu < length) 1468 length = p_port->peer_mtu; 1469 if (available < (int)length) 1470 length = (UINT16)available; 1471 p_buf->len = length; 1472 p_buf->event = BT_EVT_TO_BTU_SP_DATA; 1473 1474 //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length); 1475 //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset, (int)length, 0) != (int)length) 1476 if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset, length, 1477 DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE) 1478 { 1479 error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d", length); 1480 return (PORT_UNKNOWN_ERROR); 1481 } 1482 1483 1484 RFCOMM_TRACE_EVENT1 ("PORT_WriteData %d bytes", length); 1485 1486 rc = port_write (p_port, p_buf); 1487 1488 /* If queue went below the threashold need to send flow control */ 1489 event |= port_flow_control_user (p_port); 1490 1491 if (rc == PORT_SUCCESS) 1492 event |= PORT_EV_TXCHAR; 1493 1494 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) 1495 break; 1496 1497 *p_len += length; 1498 available -= (int)length; 1499 } 1500 if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) 1501 event |= PORT_EV_TXEMPTY; 1502 1503 /* Mask out all events that are not of interest to user */ 1504 event &= p_port->ev_mask; 1505 1506 /* Send event to the application */ 1507 if (p_port->p_callback && event) 1508 (p_port->p_callback)(event, p_port->inx); 1509 1510 return (PORT_SUCCESS); 1511 } 1512 1513 1514 1515 /******************************************************************************* 1516 ** 1517 ** Function PORT_WriteData 1518 ** 1519 ** Description Normally not GKI aware application will call this function 1520 ** to send data to the port. 1521 ** 1522 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1523 ** p_data - Data area 1524 ** max_len - Byte count requested 1525 ** p_len - Byte count received 1526 ** 1527 *******************************************************************************/ 1528 int PORT_WriteData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len) 1529 { 1530 tPORT *p_port; 1531 BT_HDR *p_buf; 1532 UINT32 event = 0; 1533 int rc = 0; 1534 UINT16 length; 1535 1536 RFCOMM_TRACE_API1 ("PORT_WriteData() max_len:%d", max_len); 1537 1538 *p_len = 0; 1539 1540 /* Check if handle is valid to avoid crashing */ 1541 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1542 { 1543 return (PORT_BAD_HANDLE); 1544 } 1545 p_port = &rfc_cb.port.port[handle - 1]; 1546 1547 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1548 { 1549 RFCOMM_TRACE_WARNING1 ("PORT_WriteData() no port state:%d", p_port->state); 1550 return (PORT_NOT_OPENED); 1551 } 1552 1553 if (!max_len || !p_port->peer_mtu) 1554 { 1555 RFCOMM_TRACE_ERROR1 ("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu); 1556 return (PORT_UNKNOWN_ERROR); 1557 } 1558 1559 /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */ 1560 length = RFCOMM_DATA_POOL_BUF_SIZE - 1561 (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD); 1562 1563 /* If there are buffers scheduled for transmission check if requested */ 1564 /* data fits into the end of the queue */ 1565 PORT_SCHEDULE_LOCK; 1566 1567 if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != NULL) 1568 && ((p_buf->len + max_len) <= p_port->peer_mtu) 1569 && ((p_buf->len + max_len) <= length)) 1570 { 1571 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len); 1572 p_port->tx.queue_size += max_len; 1573 1574 *p_len = max_len; 1575 p_buf->len += max_len; 1576 1577 PORT_SCHEDULE_UNLOCK; 1578 1579 return (PORT_SUCCESS); 1580 } 1581 1582 PORT_SCHEDULE_UNLOCK; 1583 1584 while (max_len) 1585 { 1586 /* if we're over buffer high water mark, we're done */ 1587 if ((p_port->tx.queue_size > PORT_TX_HIGH_WM) 1588 || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM)) 1589 break; 1590 1591 /* continue with rfcomm data write */ 1592 p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID); 1593 if (!p_buf) 1594 break; 1595 1596 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET; 1597 p_buf->layer_specific = handle; 1598 1599 if (p_port->peer_mtu < length) 1600 length = p_port->peer_mtu; 1601 if (max_len < length) 1602 length = max_len; 1603 p_buf->len = length; 1604 p_buf->event = BT_EVT_TO_BTU_SP_DATA; 1605 1606 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length); 1607 1608 RFCOMM_TRACE_EVENT1 ("PORT_WriteData %d bytes", length); 1609 1610 rc = port_write (p_port, p_buf); 1611 1612 /* If queue went below the threashold need to send flow control */ 1613 event |= port_flow_control_user (p_port); 1614 1615 if (rc == PORT_SUCCESS) 1616 event |= PORT_EV_TXCHAR; 1617 1618 if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING)) 1619 break; 1620 1621 *p_len += length; 1622 max_len -= length; 1623 p_data += length; 1624 1625 } 1626 if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED)) 1627 event |= PORT_EV_TXEMPTY; 1628 1629 /* Mask out all events that are not of interest to user */ 1630 event &= p_port->ev_mask; 1631 1632 /* Send event to the application */ 1633 if (p_port->p_callback && event) 1634 (p_port->p_callback)(event, p_port->inx); 1635 1636 return (PORT_SUCCESS); 1637 } 1638 1639 1640 /******************************************************************************* 1641 ** 1642 ** Function PORT_Test 1643 ** 1644 ** Description Application can call this function to send RFCOMM Test frame 1645 ** 1646 ** Parameters: handle - Handle returned in the RFCOMM_CreateConnection 1647 ** p_data - Data area 1648 ** max_len - Byte count requested 1649 ** 1650 *******************************************************************************/ 1651 int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len) 1652 { 1653 BT_HDR *p_buf; 1654 tPORT *p_port; 1655 1656 RFCOMM_TRACE_API1 ("PORT_Test() len:%d", len); 1657 1658 if ((handle == 0) || (handle > MAX_RFC_PORTS)) 1659 { 1660 return (PORT_BAD_HANDLE); 1661 } 1662 p_port = &rfc_cb.port.port[handle - 1]; 1663 1664 if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED)) 1665 { 1666 return (PORT_NOT_OPENED); 1667 } 1668 1669 if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu)) 1670 { 1671 return (PORT_UNKNOWN_ERROR); 1672 } 1673 1674 if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) != NULL) 1675 { 1676 1677 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2; 1678 p_buf->len = len; 1679 1680 memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len); 1681 1682 rfc_send_test (p_port->rfc.p_mcb, TRUE, p_buf); 1683 return (PORT_SUCCESS); 1684 } 1685 else 1686 { 1687 return (PORT_NO_MEM); 1688 } 1689 } 1690 1691 /******************************************************************************* 1692 ** 1693 ** Function RFCOMM_Init 1694 ** 1695 ** Description This function is called to initialize RFCOMM layer 1696 ** 1697 *******************************************************************************/ 1698 void RFCOMM_Init (void) 1699 { 1700 memset (&rfc_cb, 0, sizeof (tRFC_CB)); /* Init RFCOMM control block */ 1701 1702 rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS; 1703 1704 #if defined(RFCOMM_INITIAL_TRACE_LEVEL) 1705 rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL; 1706 #else 1707 rfc_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */ 1708 #endif 1709 1710 rfcomm_l2cap_if_init (); 1711 } 1712 1713 /******************************************************************************* 1714 ** 1715 ** Function PORT_SetTraceLevel 1716 ** 1717 ** Description This function sets the trace level for RFCOMM. If called with 1718 ** a value of 0xFF, it simply reads the current trace level. 1719 ** 1720 ** Returns the new (current) trace level 1721 ** 1722 *******************************************************************************/ 1723 UINT8 PORT_SetTraceLevel (UINT8 new_level) 1724 { 1725 if (new_level != 0xFF) 1726 rfc_cb.trace_level = new_level; 1727 1728 return (rfc_cb.trace_level); 1729 } 1730 1731