1/****************************************************************************** 2 * 3 * Copyright (C) 2009-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 * 22 * Filename: btif_sock.c 23 * 24 * Description: Bluetooth Socket Interface 25 * 26 * 27 ***********************************************************************************/ 28 29#include <hardware/bluetooth.h> 30#include <hardware/bt_sock.h> 31 32#define LOG_TAG "BTIF_SOCK" 33#include "btif_common.h" 34#include "btif_util.h" 35 36#include "bd.h" 37 38#include "bta_api.h" 39#include "btif_sock_thread.h" 40#include "btif_sock_rfc.h" 41#include <cutils/log.h> 42#define info(fmt, ...) ALOGI ("btif_sock: %s: " fmt,__FUNCTION__, ## __VA_ARGS__) 43#define debug(fmt, ...) ALOGD ("btif_sock: %s: " fmt,__FUNCTION__, ## __VA_ARGS__) 44#define error(fmt, ...) ALOGE ("btif_sock: ## ERROR : %s: " fmt "##",__FUNCTION__, ## __VA_ARGS__) 45#define asrt(s) if(!(s)) ALOGE ("btif_sock: ## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) 46 47static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, 48 const uint8_t* uuid, int channel, int* sock_fd, int flags); 49static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type, 50 const uint8_t* uuid, int channel, int* sock_fd, int flags); 51 52static void btsock_signaled(int fd, int type, int flags, uint32_t user_id); 53 54/******************************************************************************* 55** 56** Function btsock_ini 57** 58** Description initializes the bt socket interface 59** 60** Returns bt_status_t 61** 62*******************************************************************************/ 63static btsock_interface_t sock_if = { 64 sizeof(sock_if), 65 btsock_listen, 66 btsock_connect 67 }; 68btsock_interface_t *btif_sock_get_interface() 69{ 70 return &sock_if; 71} 72bt_status_t btif_sock_init() 73{ 74 debug(""); 75 76 77 static volatile int binit; 78 if(!binit) 79 { 80 //fix me, the process doesn't exit right now. don't set the init flag for now 81 //binit = 1; 82 debug("btsock initializing..."); 83 btsock_thread_init(); 84 int handle = btsock_thread_create(btsock_signaled, NULL); 85 if(handle >= 0 && btsock_rfc_init(handle) == BT_STATUS_SUCCESS) 86 { 87 debug("btsock successfully initialized"); 88 return BT_STATUS_SUCCESS; 89 } 90 } 91 else error("btsock interface already initialized"); 92 return BT_STATUS_FAIL; 93} 94void btif_sock_cleanup() 95{ 96 debug(""); 97 btsock_rfc_cleanup(); 98 debug("leaving"); 99} 100 101static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, 102 const uint8_t* service_uuid, int channel, int* sock_fd, int flags) 103{ 104 if((service_uuid == NULL && channel <= 0) || sock_fd == NULL) 105 { 106 error("invalid parameters, uuid:%p, channel:%d, sock_fd:%p", service_uuid, channel, sock_fd); 107 return BT_STATUS_PARM_INVALID; 108 } 109 *sock_fd = -1; 110 bt_status_t status = BT_STATUS_FAIL; 111 switch(type) 112 { 113 case BTSOCK_RFCOMM: 114 status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, flags); 115 break; 116 case BTSOCK_L2CAP: 117 error("bt l2cap socket type not supported, type:%d", type); 118 status = BT_STATUS_UNSUPPORTED; 119 break; 120 case BTSOCK_SCO: 121 error("bt sco socket not supported, type:%d", type); 122 status = BT_STATUS_UNSUPPORTED; 123 break; 124 default: 125 error("unknown bt socket type:%d", type); 126 status = BT_STATUS_UNSUPPORTED; 127 break; 128 } 129 return status; 130} 131static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type, 132 const uint8_t* uuid, int channel, int* sock_fd, int flags) 133{ 134 if((uuid == NULL && channel <= 0) || bd_addr == NULL || sock_fd == NULL) 135 { 136 error("invalid parameters, bd_addr:%p, uuid:%p, channel:%d, sock_fd:%p", 137 bd_addr, uuid, channel, sock_fd); 138 return BT_STATUS_PARM_INVALID; 139 } 140 *sock_fd = -1; 141 bt_status_t status = BT_STATUS_FAIL; 142 switch(type) 143 { 144 case BTSOCK_RFCOMM: 145 status = btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags); 146 break; 147 case BTSOCK_L2CAP: 148 error("bt l2cap socket type not supported, type:%d", type); 149 status = BT_STATUS_UNSUPPORTED; 150 break; 151 case BTSOCK_SCO: 152 error("bt sco socket not supported, type:%d", type); 153 status = BT_STATUS_UNSUPPORTED; 154 break; 155 default: 156 error("unknown bt socket type:%d", type); 157 status = BT_STATUS_UNSUPPORTED; 158 break; 159 } 160 return status; 161} 162static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) 163{ 164 switch(type) 165 { 166 case BTSOCK_RFCOMM: 167 btsock_rfc_signaled(fd, flags, user_id); 168 break; 169 case BTSOCK_L2CAP: 170 error("bt l2cap socket type not supported, fd:%d, flags:%d", fd, flags); 171 break; 172 case BTSOCK_SCO: 173 error("bt sco socket type not supported, fd:%d, flags:%d", fd, flags); 174 break; 175 default: 176 error("unknown socket type:%d, fd:%d, flags:%d", type, fd, flags); 177 break; 178 } 179} 180 181 182 183