Loading system/btif/src/btif_config.cc +29 −53 Original line number Diff line number Diff line Loading @@ -22,13 +22,14 @@ #include <assert.h> #include <ctype.h> #include <pthread.h> #include <stdio.h> #include <string> #include <string.h> #include <time.h> #include <unistd.h> #include <mutex> #include "bt_types.h" #include "btcore/include/bdaddr.h" #include "btcore/include/module.h" Loading Loading @@ -123,15 +124,14 @@ bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type) return true; } static pthread_mutex_t lock; // protects operations on |config|. static std::mutex config_lock; // protects operations on |config|. static config_t *config; static alarm_t *config_timer; // Module lifecycle functions static future_t *init(void) { pthread_mutex_init(&lock, NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); if (is_factory_reset()) delete_config_files(); Loading Loading @@ -197,14 +197,11 @@ static future_t *init(void) { LOG_EVENT_INT(BT_CONFIG_SOURCE_TAG_NUM, btif_config_source); pthread_mutex_unlock(&lock); return future_new_immediate(FUTURE_SUCCESS); error: alarm_free(config_timer); config_free(config); pthread_mutex_unlock(&lock); pthread_mutex_destroy(&lock); config_timer = NULL; config = NULL; btif_config_source = NOT_LOADED; Loading Loading @@ -235,7 +232,6 @@ static future_t *clean_up(void) { alarm_free(config_timer); config_free(config); pthread_mutex_destroy(&lock); config_timer = NULL; config = NULL; return future_new_immediate(FUTURE_SUCCESS); Loading @@ -253,11 +249,8 @@ bool btif_config_has_section(const char *section) { assert(config != NULL); assert(section != NULL); pthread_mutex_lock(&lock); bool ret = config_has_section(config, section); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_has_section(config, section); } bool btif_config_exist(const char *section, const char *key) { Loading @@ -265,11 +258,8 @@ bool btif_config_exist(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); bool ret = config_has_key(config, section, key); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_has_key(config, section, key); } bool btif_config_get_int(const char *section, const char *key, int *value) { Loading @@ -278,11 +268,10 @@ bool btif_config_get_int(const char *section, const char *key, int *value) { assert(key != NULL); assert(value != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); bool ret = config_has_key(config, section, key); if (ret) *value = config_get_int(config, section, key, *value); pthread_mutex_unlock(&lock); return ret; } Loading @@ -292,9 +281,8 @@ bool btif_config_set_int(const char *section, const char *key, int value) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_set_int(config, section, key, value); pthread_mutex_unlock(&lock); return true; } Loading @@ -306,16 +294,15 @@ bool btif_config_get_str(const char *section, const char *key, char *value, int assert(value != NULL); assert(size_bytes != NULL); pthread_mutex_lock(&lock); { std::unique_lock<std::mutex> lock(config_lock); const char *stored_value = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!stored_value) return false; strlcpy(value, stored_value, *size_bytes); *size_bytes = strlen(value) + 1; } *size_bytes = strlen(value) + 1; return true; } Loading @@ -325,10 +312,8 @@ bool btif_config_set_str(const char *section, const char *key, const char *value assert(key != NULL); assert(value != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_set_string(config, section, key, value); pthread_mutex_unlock(&lock); return true; } Loading @@ -339,9 +324,8 @@ bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, s assert(value != NULL); assert(length != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); const char *value_str = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!value_str) return false; Loading @@ -365,10 +349,8 @@ size_t btif_config_get_bin_length(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); const char *value_str = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!value_str) return 0; Loading @@ -393,9 +375,10 @@ bool btif_config_set_bin(const char *section, const char *key, const uint8_t *va str[(i * 2) + 1] = lookup[value[i] & 0x0F]; } pthread_mutex_lock(&lock); { std::unique_lock<std::mutex> lock(config_lock); config_set_string(config, section, key, str); pthread_mutex_unlock(&lock); } osi_free(str); return true; Loading Loading @@ -428,11 +411,8 @@ bool btif_config_remove(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); bool ret = config_remove_key(config, section, key); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_remove_key(config, section, key); } void btif_config_save(void) { Loading @@ -456,18 +436,15 @@ bool btif_config_clear(void) { alarm_cancel(config_timer); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_free(config); config = config_new_empty(); if (config == NULL) { pthread_mutex_unlock(&lock); if (config == NULL) return false; } bool ret = config_save(config, CONFIG_FILE_PATH); btif_config_source = RESET; pthread_mutex_unlock(&lock); return ret; } Loading @@ -482,13 +459,12 @@ static void btif_config_write(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_pa assert(config != NULL); assert(config_timer != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH); config_t *config_paired = config_new_clone(config); btif_config_remove_unpaired(config_paired); config_save(config_paired, CONFIG_FILE_PATH); config_free(config_paired); pthread_mutex_unlock(&lock); } static void btif_config_remove_unpaired(config_t *conf) { Loading system/btif/src/btif_dm.cc +5 −8 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ #include "btif_dm.h" #include <assert.h> #include <pthread.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> Loading @@ -39,6 +38,8 @@ #include <time.h> #include <unistd.h> #include <mutex> #include <hardware/bluetooth.h> #include "bdaddr.h" Loading Loading @@ -208,7 +209,7 @@ static uid_set_t* uid_set = NULL; /* A circular array to keep track of the most recent bond events */ static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1]; static pthread_mutex_t bond_event_lock; static std::mutex bond_event_lock; /* |btif_num_bond_events| keeps track of the total number of events and can be greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */ Loading Loading @@ -297,7 +298,6 @@ static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec) void btif_dm_init(uid_set_t* set) { uid_set = set; pthread_mutex_init(&bond_event_lock, NULL); } void btif_dm_cleanup(void) Loading @@ -306,7 +306,6 @@ void btif_dm_cleanup(void) uid_set_destroy(uid_set); uid_set = NULL; } pthread_mutex_destroy(&bond_event_lock); } bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id, Loading Loading @@ -3529,7 +3528,7 @@ static char* btif_get_default_local_name() { static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, bt_bond_function_t function, bt_bond_state_t state) { pthread_mutex_lock(&bond_event_lock); std::unique_lock<std::mutex> lock(bond_event_lock); btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index]; memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t)); Loading Loading @@ -3567,11 +3566,10 @@ static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, event->timestamp.tv_nsec / 1000000; metrics_pair_event(0, ts, cod, device_type); pthread_mutex_unlock(&bond_event_lock); } void btif_debug_bond_event_dump(int fd) { pthread_mutex_lock(&bond_event_lock); std::unique_lock<std::mutex> lock(bond_event_lock); dprintf(fd, "\nBond Events: \n"); dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events); if (btif_num_bond_events > 0) Loading Loading @@ -3624,5 +3622,4 @@ void btif_debug_bond_event_dump(int fd) { } dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state); } pthread_mutex_unlock(&bond_event_lock); } system/btif/src/btif_rc.cc +9 −33 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ /***************************************************************************** * * Filename: btif_rc.c * Filename: btif_rc.cc * * Description: Bluetooth AVRC implementation * Loading @@ -31,6 +31,8 @@ #include <time.h> #include <unistd.h> #include <mutex> #include <hardware/bluetooth.h> #include <hardware/bt_rc.h> Loading Loading @@ -181,7 +183,7 @@ typedef struct { } btif_rc_device_cb_t; typedef struct { pthread_mutex_t lock; std::mutex lock; btif_rc_device_cb_t rc_multi_cb[BTIF_RC_NUM_CONN]; } rc_cb_t; Loading @@ -195,7 +197,7 @@ typedef struct { typedef struct { pthread_mutex_t lbllock; std::recursive_mutex lbllock; rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION]; } rc_device_t; Loading Loading @@ -275,7 +277,6 @@ static void send_metamsg_rsp (btif_rc_device_cb_t *p_dev, int index, uint8_t lab static void register_volumechange(uint8_t label, btif_rc_device_cb_t *p_dev); #endif static void lbl_init(); static void lbl_destroy(); static void init_all_transactions(); static bt_status_t get_transaction(rc_transaction_t **ptransaction); static void release_transaction(uint8_t label); Loading Loading @@ -2204,7 +2205,7 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, { tAVRC_RESPONSE avrc_rsp; BTIF_TRACE_EVENT("%s: event_id: %s", __func__, dump_rc_notification_event_id(event_id)); pthread_mutex_lock(&btif_rc_cb.lock); std::unique_lock<std::mutex> lock(btif_rc_cb.lock); memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); Loading Loading @@ -2264,7 +2265,6 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, default: BTIF_TRACE_WARNING("%s: Unhandled event ID: 0x%x", __func__, event_id); pthread_mutex_unlock(&btif_rc_cb.lock); return BT_STATUS_UNHANDLED; } Loading @@ -2289,7 +2289,6 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, } } } pthread_mutex_unlock(&btif_rc_cb.lock); return BT_STATUS_SUCCESS; } Loading Loading @@ -4478,7 +4477,6 @@ static void cleanup() memset(&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); } lbl_destroy(); BTIF_TRACE_EVENT("%s: completed", __func__); } Loading Loading @@ -4506,7 +4504,6 @@ static void cleanup_ctrl() } memset(&btif_rc_cb, 0, sizeof(rc_cb_t)); lbl_destroy(); BTIF_TRACE_EVENT("%s: completed", __func__); } Loading Loading @@ -5356,7 +5353,7 @@ const btrc_ctrl_interface_t *btif_rc_ctrl_get_interface(void) *******************************************************************************/ static void initialize_transaction(int lbl) { pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex>(device.lbllock); if (lbl < MAX_TRANSACTIONS_PER_SESSION) { if (alarm_is_scheduled(device.transaction[lbl].txn_timer)) { clear_cmd_timeout(lbl); Loading @@ -5365,7 +5362,6 @@ static void initialize_transaction(int lbl) device.transaction[lbl].in_use=false; device.transaction[lbl].handle=0; } pthread_mutex_unlock(&device.lbllock); } /******************************************************************************* Loading @@ -5378,11 +5374,6 @@ static void initialize_transaction(int lbl) void lbl_init() { memset(&device,0,sizeof(rc_device_t)); pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&(device.lbllock), &attr); pthread_mutexattr_destroy(&attr); init_all_transactions(); } Loading Loading @@ -5415,7 +5406,7 @@ void init_all_transactions() rc_transaction_t *get_transaction_by_lbl(uint8_t lbl) { rc_transaction_t *transaction = NULL; pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex> lock(device.lbllock); /* Determine if this is a valid label */ if (lbl < MAX_TRANSACTIONS_PER_SESSION) Loading @@ -5431,7 +5422,6 @@ rc_transaction_t *get_transaction_by_lbl(uint8_t lbl) } } pthread_mutex_unlock(&device.lbllock); return transaction; } Loading @@ -5448,7 +5438,7 @@ bt_status_t get_transaction(rc_transaction_t **ptransaction) { bt_status_t result = BT_STATUS_NOMEM; uint8_t i=0; pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex> lock(device.lbllock); // Check for unused transactions for (i=0; i<MAX_TRANSACTIONS_PER_SESSION; i++) Loading @@ -5463,7 +5453,6 @@ bt_status_t get_transaction(rc_transaction_t **ptransaction) } } pthread_mutex_unlock(&device.lbllock); return result; } Loading @@ -5487,19 +5476,6 @@ void release_transaction(uint8_t lbl) } } /******************************************************************************* ** ** Function lbl_destroy ** ** Description Cleanup of the mutex ** ** Returns void *******************************************************************************/ void lbl_destroy() { pthread_mutex_destroy(&(device.lbllock)); } /******************************************************************************* ** Function sleep_ms ** Loading system/btif/src/btif_sdp_server.cc +38 −45 Original line number Diff line number Diff line Loading @@ -31,6 +31,8 @@ #include <stdlib.h> #include <string.h> #include <mutex> #include <hardware/bluetooth.h> #include <hardware/bt_sdp.h> Loading @@ -42,7 +44,8 @@ #include "osi/include/allocator.h" #include "utl.h" static pthread_mutex_t sdp_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; // Protects the sdp_slots array from concurrent access. static std::recursive_mutex sdp_lock; /** * The need for a state variable have been reduced to two states. Loading Loading @@ -108,7 +111,7 @@ bt_status_t sdp_server_init() void sdp_server_cleanup() { BTIF_TRACE_DEBUG("Sdp Server %s", __func__); pthread_mutex_lock(&sdp_lock); std::unique_lock<std::recursive_mutex> lock(sdp_lock); int i; for(i = 0; i < MAX_SDP_SLOTS; i++) { Loading @@ -117,7 +120,6 @@ void sdp_server_cleanup() */ free_sdp_slot(i); } pthread_mutex_unlock(&sdp_lock); } int get_sdp_records_size(bluetooth_sdp_record* in_record, int count) { Loading Loading @@ -185,32 +187,27 @@ void copy_sdp_records(bluetooth_sdp_record* in_records, * user1_ptr and * user2_ptr. */ static int alloc_sdp_slot(bluetooth_sdp_record* in_record) { int i; int record_size = get_sdp_records_size(in_record, 1); /* We are optimists here, and preallocate the record. * This is to reduce the time we hold the sdp_lock. */ bluetooth_sdp_record* record = (bluetooth_sdp_record*)osi_malloc(record_size); copy_sdp_records(in_record, record, 1); /* We are optimists here, and preallocate the record. * This is to reduce the time we hold the sdp_lock. */ pthread_mutex_lock(&sdp_lock); for(i = 0; i < MAX_SDP_SLOTS; i++) { std::unique_lock<std::recursive_mutex> lock(sdp_lock); for (int i = 0; i < MAX_SDP_SLOTS; i++) { if(sdp_slots[i].state == SDP_RECORD_FREE) { sdp_slots[i].state = SDP_RECORD_ALLOCED; sdp_slots[i].record_data = record; break; return i; } } } pthread_mutex_unlock(&sdp_lock); if(i >= MAX_SDP_SLOTS) { APPL_TRACE_ERROR("%s() failed - no more free slots!", __func__); /* Rearly the optimist is too optimistic, and cleanup is needed...*/ osi_free(record); return -1; } return i; } static int free_sdp_slot(int id) { int handle = -1; Loading @@ -219,16 +216,17 @@ static int free_sdp_slot(int id) { APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); return handle; } pthread_mutex_lock(&sdp_lock); { std::unique_lock<std::recursive_mutex> lock(sdp_lock); handle = sdp_slots[id].sdp_handle; sdp_slots[id].sdp_handle = 0; if(sdp_slots[id].state != SDP_RECORD_FREE) { if (sdp_slots[id].state != SDP_RECORD_FREE) { /* safe a copy of the pointer, and free after unlock() */ record = sdp_slots[id].record_data; } sdp_slots[id].state = SDP_RECORD_FREE; pthread_mutex_unlock(&sdp_lock); } if (record != NULL) { osi_free(record); Loading @@ -244,31 +242,26 @@ static int free_sdp_slot(int id) { * SDP_RECORD_CREATE_INITIATED. */ static const sdp_slot_t* start_create_sdp(int id) { sdp_slot_t* sdp_slot; if (id >= MAX_SDP_SLOTS) { APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); return NULL; } pthread_mutex_lock(&sdp_lock); if(sdp_slots[id].state == SDP_RECORD_ALLOCED) { sdp_slot = &(sdp_slots[id]); } else { std::unique_lock<std::recursive_mutex> lock(sdp_lock); if (sdp_slots[id].state != SDP_RECORD_ALLOCED) { /* The record have been removed before this event occurred - e.g. deinit */ sdp_slot = NULL; } pthread_mutex_unlock(&sdp_lock); if(sdp_slot == NULL) { APPL_TRACE_ERROR("%s() failed - state for id %d is " "sdp_slots[id].state = %d expected %d", __func__, id, sdp_slots[id].state, SDP_RECORD_ALLOCED); return NULL; } return sdp_slot; return &(sdp_slots[id]); } static void set_sdp_handle(int id, int handle) { pthread_mutex_lock(&sdp_lock); std::unique_lock<std::recursive_mutex> lock(sdp_lock); sdp_slots[id].sdp_handle = handle; pthread_mutex_unlock(&sdp_lock); BTIF_TRACE_DEBUG("%s() id=%d to handle=0x%08x", __func__, id, handle); } Loading system/btif/src/btif_sock_l2cap.cc +210 −225 Original line number Diff line number Diff line Loading @@ -20,13 +20,14 @@ #include "btif_sock_l2cap.h" #include <errno.h> #include <pthread.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <mutex> #include <hardware/bt_sock.h> #include "osi/include/allocator.h" Loading Loading @@ -90,7 +91,7 @@ typedef struct l2cap_socket { static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock); static pthread_mutex_t state_lock; static std::mutex state_lock; l2cap_socket *socks = NULL; static uid_set_t* uid_set = NULL; Loading Loading @@ -203,25 +204,20 @@ static char packet_put_tail_l(l2cap_socket *sock, const void *data, uint32_t len static inline void bd_copy(uint8_t* dest, uint8_t* src, bool swap) { if (swap) { int i; for (i =0; i < 6 ;i++) for (int i=0; i < 6; i++) dest[i] = src[5-i]; } else { memcpy(dest, src, 6); } else memcpy(dest, src, 6); } static char is_inited(void) { char ret; pthread_mutex_lock(&state_lock); ret = pth != -1; pthread_mutex_unlock(&state_lock); return ret; std::unique_lock<std::mutex> lock(state_lock); return pth != -1; } /* only call with mutex taken */ /* only call with std::mutex taken */ static l2cap_socket *btsock_l2cap_find_by_id_l(uint32_t id) { l2cap_socket *sock = socks; Loading Loading @@ -362,25 +358,19 @@ fail_sockpair: bt_status_t btsock_l2cap_init(int handle, uid_set_t* set) { APPL_TRACE_DEBUG("%s handle = %d", __func__); pthread_mutex_init(&state_lock, NULL); pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); pth = handle; socks = NULL; uid_set = set; pthread_mutex_unlock(&state_lock); return BT_STATUS_SUCCESS; } bt_status_t btsock_l2cap_cleanup() { pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); pth = -1; while (socks) btsock_l2cap_free_l(socks); pthread_mutex_unlock(&state_lock); pthread_mutex_destroy(&state_lock); return BT_STATUS_SUCCESS; } Loading Loading @@ -414,17 +404,21 @@ static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START *p_start, uint32_t i { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; if (p_start->status != BTA_JV_SUCCESS) { APPL_TRACE_ERROR("Error starting l2cap_listen - status: 0x%04x", p_start->status); btsock_l2cap_free_l(sock); return; } else { sock->handle = p_start->handle; APPL_TRACE_DEBUG("on_srv_l2cap_listen_started() sock->handle =%d id:%d", sock->handle, sock->id); if(sock->server_psm_sent == false) { if (!send_app_psm_or_chan_l(sock)) { //closed Loading @@ -435,24 +429,22 @@ static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START *p_start, uint32_t i } } } } pthread_mutex_unlock(&state_lock); } static void on_cl_l2cap_init(tBTA_JV_L2CAP_CL_INIT *p_init, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; if (p_init->status != BTA_JV_SUCCESS) { btsock_l2cap_free_l(sock); } else { sock->handle = p_init->handle; } return; } pthread_mutex_unlock(&state_lock); sock->handle = p_init->handle; } /** Loading @@ -464,7 +456,7 @@ static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN *p_open, l2cap_socket l2cap_socket *accept_rs; uint32_t new_listen_id; // Mutex locked by caller // std::mutex locked by caller accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); accept_rs->connected = true; accept_rs->security = sock->security; Loading Loading @@ -505,7 +497,7 @@ static void on_srv_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN *p_open, l2cap_socke l2cap_socket *accept_rs; uint32_t new_listen_id; // mutex locked by caller // std::mutex locked by caller accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); if (accept_rs) { Loading Loading @@ -582,11 +574,13 @@ static void on_l2cap_connect(tBTA_JV *p_data, uint32_t id) tBTA_JV_L2CAP_OPEN *psm_open = &p_data->l2c_open; tBTA_JV_L2CAP_LE_OPEN *le_open = &p_data->l2c_le_open; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (!sock) { APPL_TRACE_ERROR("on_l2cap_connect on unknown socket"); } else { return; } if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) { if (!sock->server) on_cl_l2cap_le_connect_l(le_open, sock); Loading @@ -601,16 +595,16 @@ static void on_l2cap_connect(tBTA_JV *p_data, uint32_t id) else btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); } static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE * p_close, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; APPL_TRACE_DEBUG("on_l2cap_close, slot id:%d, fd:%d, %s:%d, server:%d", sock->id, sock->our_fd, sock->fixed_chan ? "fixed_chan" : "PSM", sock->channel, sock->server); Loading @@ -621,16 +615,16 @@ static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE * p_close, uint32_t id) } btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); } static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG *p, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; sock->outgoing_congest = p->cong ? 1 : 0; //mointer the fd for any outgoing data if (!sock->outgoing_congest) { Loading @@ -639,8 +633,6 @@ static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG *p, uint32_t id) } } pthread_mutex_unlock(&state_lock); } static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) { Loading @@ -652,17 +644,17 @@ static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) int app_uid = -1; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (!sock->outgoing_congest) { // monitor the fd for any outgoing data APPL_TRACE_DEBUG("on_l2cap_write_done: adding fd to btsock_thread..."); btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); } } pthread_mutex_unlock(&state_lock); uid_set_add_tx(uid_set, app_uid, len); } Loading @@ -676,17 +668,16 @@ static void on_l2cap_write_fixed_done(void* req_id, uint16_t len, uint32_t id) } int app_uid = -1; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (!sock->outgoing_congest) { //monitor the fd for any outgoing data btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); } } pthread_mutex_unlock(&state_lock); uid_set_add_tx(uid_set, app_uid, len); } Loading @@ -697,9 +688,11 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) int app_uid = -1; uint32_t bytes_read = 0; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (sock->fixed_chan) { /* we do these differently */ Loading @@ -719,7 +712,6 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) } } else { uint8_t buffer[L2CAP_MAX_SDU_LENGTH]; uint32_t count; Loading @@ -738,8 +730,6 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) } } } } pthread_mutex_unlock(&state_lock); uid_set_add_rx(uid_set, app_uid, bytes_read); } Loading Loading @@ -824,19 +814,17 @@ const tL2CAP_ERTM_INFO obex_l2c_etm_opt = void on_l2cap_psm_assigned(int id, int psm) { /* Setup ETM settings: * mtu will be set below */ pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); l2cap_socket *sock = btsock_l2cap_find_by_id_l(id); if (!sock) { APPL_TRACE_ERROR("%s: Error: sock is null", __func__); return; } if (sock) { sock->channel = psm; if (btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) btsock_l2cap_free_l(sock); } else { APPL_TRACE_ERROR("%s: Error: sock is null", __func__); } pthread_mutex_unlock(&state_lock); } static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock) { Loading Loading @@ -915,11 +903,9 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char *name, const bt_bda return BT_STATUS_NOT_READY; // TODO: This is kind of bad to lock here, but it is needed for the current design. pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_alloc_l(name, addr, listen, flags); if (!sock) { pthread_mutex_unlock(&state_lock); return BT_STATUS_NOMEM; } Loading Loading @@ -976,7 +962,6 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char *name, const bt_bda } else { btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); return stat; } Loading Loading @@ -1028,9 +1013,11 @@ void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) char drop_it = false; /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to hold the lock. */ pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(user_id); if (sock) { if (!sock) return; if ((flags & SOCK_THREAD_FD_RD) && !sock->server) { // app sending data if (sock->connected) { Loading Loading @@ -1105,6 +1092,4 @@ void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) btsock_l2cap_free_l(sock); } } pthread_mutex_unlock(&state_lock); } Loading
system/btif/src/btif_config.cc +29 −53 Original line number Diff line number Diff line Loading @@ -22,13 +22,14 @@ #include <assert.h> #include <ctype.h> #include <pthread.h> #include <stdio.h> #include <string> #include <string.h> #include <time.h> #include <unistd.h> #include <mutex> #include "bt_types.h" #include "btcore/include/bdaddr.h" #include "btcore/include/module.h" Loading Loading @@ -123,15 +124,14 @@ bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type) return true; } static pthread_mutex_t lock; // protects operations on |config|. static std::mutex config_lock; // protects operations on |config|. static config_t *config; static alarm_t *config_timer; // Module lifecycle functions static future_t *init(void) { pthread_mutex_init(&lock, NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); if (is_factory_reset()) delete_config_files(); Loading Loading @@ -197,14 +197,11 @@ static future_t *init(void) { LOG_EVENT_INT(BT_CONFIG_SOURCE_TAG_NUM, btif_config_source); pthread_mutex_unlock(&lock); return future_new_immediate(FUTURE_SUCCESS); error: alarm_free(config_timer); config_free(config); pthread_mutex_unlock(&lock); pthread_mutex_destroy(&lock); config_timer = NULL; config = NULL; btif_config_source = NOT_LOADED; Loading Loading @@ -235,7 +232,6 @@ static future_t *clean_up(void) { alarm_free(config_timer); config_free(config); pthread_mutex_destroy(&lock); config_timer = NULL; config = NULL; return future_new_immediate(FUTURE_SUCCESS); Loading @@ -253,11 +249,8 @@ bool btif_config_has_section(const char *section) { assert(config != NULL); assert(section != NULL); pthread_mutex_lock(&lock); bool ret = config_has_section(config, section); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_has_section(config, section); } bool btif_config_exist(const char *section, const char *key) { Loading @@ -265,11 +258,8 @@ bool btif_config_exist(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); bool ret = config_has_key(config, section, key); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_has_key(config, section, key); } bool btif_config_get_int(const char *section, const char *key, int *value) { Loading @@ -278,11 +268,10 @@ bool btif_config_get_int(const char *section, const char *key, int *value) { assert(key != NULL); assert(value != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); bool ret = config_has_key(config, section, key); if (ret) *value = config_get_int(config, section, key, *value); pthread_mutex_unlock(&lock); return ret; } Loading @@ -292,9 +281,8 @@ bool btif_config_set_int(const char *section, const char *key, int value) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_set_int(config, section, key, value); pthread_mutex_unlock(&lock); return true; } Loading @@ -306,16 +294,15 @@ bool btif_config_get_str(const char *section, const char *key, char *value, int assert(value != NULL); assert(size_bytes != NULL); pthread_mutex_lock(&lock); { std::unique_lock<std::mutex> lock(config_lock); const char *stored_value = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!stored_value) return false; strlcpy(value, stored_value, *size_bytes); *size_bytes = strlen(value) + 1; } *size_bytes = strlen(value) + 1; return true; } Loading @@ -325,10 +312,8 @@ bool btif_config_set_str(const char *section, const char *key, const char *value assert(key != NULL); assert(value != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_set_string(config, section, key, value); pthread_mutex_unlock(&lock); return true; } Loading @@ -339,9 +324,8 @@ bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, s assert(value != NULL); assert(length != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); const char *value_str = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!value_str) return false; Loading @@ -365,10 +349,8 @@ size_t btif_config_get_bin_length(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); const char *value_str = config_get_string(config, section, key, NULL); pthread_mutex_unlock(&lock); if (!value_str) return 0; Loading @@ -393,9 +375,10 @@ bool btif_config_set_bin(const char *section, const char *key, const uint8_t *va str[(i * 2) + 1] = lookup[value[i] & 0x0F]; } pthread_mutex_lock(&lock); { std::unique_lock<std::mutex> lock(config_lock); config_set_string(config, section, key, str); pthread_mutex_unlock(&lock); } osi_free(str); return true; Loading Loading @@ -428,11 +411,8 @@ bool btif_config_remove(const char *section, const char *key) { assert(section != NULL); assert(key != NULL); pthread_mutex_lock(&lock); bool ret = config_remove_key(config, section, key); pthread_mutex_unlock(&lock); return ret; std::unique_lock<std::mutex> lock(config_lock); return config_remove_key(config, section, key); } void btif_config_save(void) { Loading @@ -456,18 +436,15 @@ bool btif_config_clear(void) { alarm_cancel(config_timer); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); config_free(config); config = config_new_empty(); if (config == NULL) { pthread_mutex_unlock(&lock); if (config == NULL) return false; } bool ret = config_save(config, CONFIG_FILE_PATH); btif_config_source = RESET; pthread_mutex_unlock(&lock); return ret; } Loading @@ -482,13 +459,12 @@ static void btif_config_write(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_pa assert(config != NULL); assert(config_timer != NULL); pthread_mutex_lock(&lock); std::unique_lock<std::mutex> lock(config_lock); rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH); config_t *config_paired = config_new_clone(config); btif_config_remove_unpaired(config_paired); config_save(config_paired, CONFIG_FILE_PATH); config_free(config_paired); pthread_mutex_unlock(&lock); } static void btif_config_remove_unpaired(config_t *conf) { Loading
system/btif/src/btif_dm.cc +5 −8 Original line number Diff line number Diff line Loading @@ -30,7 +30,6 @@ #include "btif_dm.h" #include <assert.h> #include <pthread.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> Loading @@ -39,6 +38,8 @@ #include <time.h> #include <unistd.h> #include <mutex> #include <hardware/bluetooth.h> #include "bdaddr.h" Loading Loading @@ -208,7 +209,7 @@ static uid_set_t* uid_set = NULL; /* A circular array to keep track of the most recent bond events */ static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1]; static pthread_mutex_t bond_event_lock; static std::mutex bond_event_lock; /* |btif_num_bond_events| keeps track of the total number of events and can be greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */ Loading Loading @@ -297,7 +298,6 @@ static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec) void btif_dm_init(uid_set_t* set) { uid_set = set; pthread_mutex_init(&bond_event_lock, NULL); } void btif_dm_cleanup(void) Loading @@ -306,7 +306,6 @@ void btif_dm_cleanup(void) uid_set_destroy(uid_set); uid_set = NULL; } pthread_mutex_destroy(&bond_event_lock); } bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id, Loading Loading @@ -3529,7 +3528,7 @@ static char* btif_get_default_local_name() { static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, bt_bond_function_t function, bt_bond_state_t state) { pthread_mutex_lock(&bond_event_lock); std::unique_lock<std::mutex> lock(bond_event_lock); btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index]; memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t)); Loading Loading @@ -3567,11 +3566,10 @@ static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, event->timestamp.tv_nsec / 1000000; metrics_pair_event(0, ts, cod, device_type); pthread_mutex_unlock(&bond_event_lock); } void btif_debug_bond_event_dump(int fd) { pthread_mutex_lock(&bond_event_lock); std::unique_lock<std::mutex> lock(bond_event_lock); dprintf(fd, "\nBond Events: \n"); dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events); if (btif_num_bond_events > 0) Loading Loading @@ -3624,5 +3622,4 @@ void btif_debug_bond_event_dump(int fd) { } dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state); } pthread_mutex_unlock(&bond_event_lock); }
system/btif/src/btif_rc.cc +9 −33 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ /***************************************************************************** * * Filename: btif_rc.c * Filename: btif_rc.cc * * Description: Bluetooth AVRC implementation * Loading @@ -31,6 +31,8 @@ #include <time.h> #include <unistd.h> #include <mutex> #include <hardware/bluetooth.h> #include <hardware/bt_rc.h> Loading Loading @@ -181,7 +183,7 @@ typedef struct { } btif_rc_device_cb_t; typedef struct { pthread_mutex_t lock; std::mutex lock; btif_rc_device_cb_t rc_multi_cb[BTIF_RC_NUM_CONN]; } rc_cb_t; Loading @@ -195,7 +197,7 @@ typedef struct { typedef struct { pthread_mutex_t lbllock; std::recursive_mutex lbllock; rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION]; } rc_device_t; Loading Loading @@ -275,7 +277,6 @@ static void send_metamsg_rsp (btif_rc_device_cb_t *p_dev, int index, uint8_t lab static void register_volumechange(uint8_t label, btif_rc_device_cb_t *p_dev); #endif static void lbl_init(); static void lbl_destroy(); static void init_all_transactions(); static bt_status_t get_transaction(rc_transaction_t **ptransaction); static void release_transaction(uint8_t label); Loading Loading @@ -2204,7 +2205,7 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, { tAVRC_RESPONSE avrc_rsp; BTIF_TRACE_EVENT("%s: event_id: %s", __func__, dump_rc_notification_event_id(event_id)); pthread_mutex_lock(&btif_rc_cb.lock); std::unique_lock<std::mutex> lock(btif_rc_cb.lock); memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); Loading Loading @@ -2264,7 +2265,6 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, default: BTIF_TRACE_WARNING("%s: Unhandled event ID: 0x%x", __func__, event_id); pthread_mutex_unlock(&btif_rc_cb.lock); return BT_STATUS_UNHANDLED; } Loading @@ -2289,7 +2289,6 @@ static bt_status_t register_notification_rsp(btrc_event_id_t event_id, } } } pthread_mutex_unlock(&btif_rc_cb.lock); return BT_STATUS_SUCCESS; } Loading Loading @@ -4478,7 +4477,6 @@ static void cleanup() memset(&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); } lbl_destroy(); BTIF_TRACE_EVENT("%s: completed", __func__); } Loading Loading @@ -4506,7 +4504,6 @@ static void cleanup_ctrl() } memset(&btif_rc_cb, 0, sizeof(rc_cb_t)); lbl_destroy(); BTIF_TRACE_EVENT("%s: completed", __func__); } Loading Loading @@ -5356,7 +5353,7 @@ const btrc_ctrl_interface_t *btif_rc_ctrl_get_interface(void) *******************************************************************************/ static void initialize_transaction(int lbl) { pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex>(device.lbllock); if (lbl < MAX_TRANSACTIONS_PER_SESSION) { if (alarm_is_scheduled(device.transaction[lbl].txn_timer)) { clear_cmd_timeout(lbl); Loading @@ -5365,7 +5362,6 @@ static void initialize_transaction(int lbl) device.transaction[lbl].in_use=false; device.transaction[lbl].handle=0; } pthread_mutex_unlock(&device.lbllock); } /******************************************************************************* Loading @@ -5378,11 +5374,6 @@ static void initialize_transaction(int lbl) void lbl_init() { memset(&device,0,sizeof(rc_device_t)); pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&(device.lbllock), &attr); pthread_mutexattr_destroy(&attr); init_all_transactions(); } Loading Loading @@ -5415,7 +5406,7 @@ void init_all_transactions() rc_transaction_t *get_transaction_by_lbl(uint8_t lbl) { rc_transaction_t *transaction = NULL; pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex> lock(device.lbllock); /* Determine if this is a valid label */ if (lbl < MAX_TRANSACTIONS_PER_SESSION) Loading @@ -5431,7 +5422,6 @@ rc_transaction_t *get_transaction_by_lbl(uint8_t lbl) } } pthread_mutex_unlock(&device.lbllock); return transaction; } Loading @@ -5448,7 +5438,7 @@ bt_status_t get_transaction(rc_transaction_t **ptransaction) { bt_status_t result = BT_STATUS_NOMEM; uint8_t i=0; pthread_mutex_lock(&device.lbllock); std::unique_lock<std::recursive_mutex> lock(device.lbllock); // Check for unused transactions for (i=0; i<MAX_TRANSACTIONS_PER_SESSION; i++) Loading @@ -5463,7 +5453,6 @@ bt_status_t get_transaction(rc_transaction_t **ptransaction) } } pthread_mutex_unlock(&device.lbllock); return result; } Loading @@ -5487,19 +5476,6 @@ void release_transaction(uint8_t lbl) } } /******************************************************************************* ** ** Function lbl_destroy ** ** Description Cleanup of the mutex ** ** Returns void *******************************************************************************/ void lbl_destroy() { pthread_mutex_destroy(&(device.lbllock)); } /******************************************************************************* ** Function sleep_ms ** Loading
system/btif/src/btif_sdp_server.cc +38 −45 Original line number Diff line number Diff line Loading @@ -31,6 +31,8 @@ #include <stdlib.h> #include <string.h> #include <mutex> #include <hardware/bluetooth.h> #include <hardware/bt_sdp.h> Loading @@ -42,7 +44,8 @@ #include "osi/include/allocator.h" #include "utl.h" static pthread_mutex_t sdp_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; // Protects the sdp_slots array from concurrent access. static std::recursive_mutex sdp_lock; /** * The need for a state variable have been reduced to two states. Loading Loading @@ -108,7 +111,7 @@ bt_status_t sdp_server_init() void sdp_server_cleanup() { BTIF_TRACE_DEBUG("Sdp Server %s", __func__); pthread_mutex_lock(&sdp_lock); std::unique_lock<std::recursive_mutex> lock(sdp_lock); int i; for(i = 0; i < MAX_SDP_SLOTS; i++) { Loading @@ -117,7 +120,6 @@ void sdp_server_cleanup() */ free_sdp_slot(i); } pthread_mutex_unlock(&sdp_lock); } int get_sdp_records_size(bluetooth_sdp_record* in_record, int count) { Loading Loading @@ -185,32 +187,27 @@ void copy_sdp_records(bluetooth_sdp_record* in_records, * user1_ptr and * user2_ptr. */ static int alloc_sdp_slot(bluetooth_sdp_record* in_record) { int i; int record_size = get_sdp_records_size(in_record, 1); /* We are optimists here, and preallocate the record. * This is to reduce the time we hold the sdp_lock. */ bluetooth_sdp_record* record = (bluetooth_sdp_record*)osi_malloc(record_size); copy_sdp_records(in_record, record, 1); /* We are optimists here, and preallocate the record. * This is to reduce the time we hold the sdp_lock. */ pthread_mutex_lock(&sdp_lock); for(i = 0; i < MAX_SDP_SLOTS; i++) { std::unique_lock<std::recursive_mutex> lock(sdp_lock); for (int i = 0; i < MAX_SDP_SLOTS; i++) { if(sdp_slots[i].state == SDP_RECORD_FREE) { sdp_slots[i].state = SDP_RECORD_ALLOCED; sdp_slots[i].record_data = record; break; return i; } } } pthread_mutex_unlock(&sdp_lock); if(i >= MAX_SDP_SLOTS) { APPL_TRACE_ERROR("%s() failed - no more free slots!", __func__); /* Rearly the optimist is too optimistic, and cleanup is needed...*/ osi_free(record); return -1; } return i; } static int free_sdp_slot(int id) { int handle = -1; Loading @@ -219,16 +216,17 @@ static int free_sdp_slot(int id) { APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); return handle; } pthread_mutex_lock(&sdp_lock); { std::unique_lock<std::recursive_mutex> lock(sdp_lock); handle = sdp_slots[id].sdp_handle; sdp_slots[id].sdp_handle = 0; if(sdp_slots[id].state != SDP_RECORD_FREE) { if (sdp_slots[id].state != SDP_RECORD_FREE) { /* safe a copy of the pointer, and free after unlock() */ record = sdp_slots[id].record_data; } sdp_slots[id].state = SDP_RECORD_FREE; pthread_mutex_unlock(&sdp_lock); } if (record != NULL) { osi_free(record); Loading @@ -244,31 +242,26 @@ static int free_sdp_slot(int id) { * SDP_RECORD_CREATE_INITIATED. */ static const sdp_slot_t* start_create_sdp(int id) { sdp_slot_t* sdp_slot; if (id >= MAX_SDP_SLOTS) { APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); return NULL; } pthread_mutex_lock(&sdp_lock); if(sdp_slots[id].state == SDP_RECORD_ALLOCED) { sdp_slot = &(sdp_slots[id]); } else { std::unique_lock<std::recursive_mutex> lock(sdp_lock); if (sdp_slots[id].state != SDP_RECORD_ALLOCED) { /* The record have been removed before this event occurred - e.g. deinit */ sdp_slot = NULL; } pthread_mutex_unlock(&sdp_lock); if(sdp_slot == NULL) { APPL_TRACE_ERROR("%s() failed - state for id %d is " "sdp_slots[id].state = %d expected %d", __func__, id, sdp_slots[id].state, SDP_RECORD_ALLOCED); return NULL; } return sdp_slot; return &(sdp_slots[id]); } static void set_sdp_handle(int id, int handle) { pthread_mutex_lock(&sdp_lock); std::unique_lock<std::recursive_mutex> lock(sdp_lock); sdp_slots[id].sdp_handle = handle; pthread_mutex_unlock(&sdp_lock); BTIF_TRACE_DEBUG("%s() id=%d to handle=0x%08x", __func__, id, handle); } Loading
system/btif/src/btif_sock_l2cap.cc +210 −225 Original line number Diff line number Diff line Loading @@ -20,13 +20,14 @@ #include "btif_sock_l2cap.h" #include <errno.h> #include <pthread.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <mutex> #include <hardware/bt_sock.h> #include "osi/include/allocator.h" Loading Loading @@ -90,7 +91,7 @@ typedef struct l2cap_socket { static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock); static pthread_mutex_t state_lock; static std::mutex state_lock; l2cap_socket *socks = NULL; static uid_set_t* uid_set = NULL; Loading Loading @@ -203,25 +204,20 @@ static char packet_put_tail_l(l2cap_socket *sock, const void *data, uint32_t len static inline void bd_copy(uint8_t* dest, uint8_t* src, bool swap) { if (swap) { int i; for (i =0; i < 6 ;i++) for (int i=0; i < 6; i++) dest[i] = src[5-i]; } else { memcpy(dest, src, 6); } else memcpy(dest, src, 6); } static char is_inited(void) { char ret; pthread_mutex_lock(&state_lock); ret = pth != -1; pthread_mutex_unlock(&state_lock); return ret; std::unique_lock<std::mutex> lock(state_lock); return pth != -1; } /* only call with mutex taken */ /* only call with std::mutex taken */ static l2cap_socket *btsock_l2cap_find_by_id_l(uint32_t id) { l2cap_socket *sock = socks; Loading Loading @@ -362,25 +358,19 @@ fail_sockpair: bt_status_t btsock_l2cap_init(int handle, uid_set_t* set) { APPL_TRACE_DEBUG("%s handle = %d", __func__); pthread_mutex_init(&state_lock, NULL); pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); pth = handle; socks = NULL; uid_set = set; pthread_mutex_unlock(&state_lock); return BT_STATUS_SUCCESS; } bt_status_t btsock_l2cap_cleanup() { pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); pth = -1; while (socks) btsock_l2cap_free_l(socks); pthread_mutex_unlock(&state_lock); pthread_mutex_destroy(&state_lock); return BT_STATUS_SUCCESS; } Loading Loading @@ -414,17 +404,21 @@ static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START *p_start, uint32_t i { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; if (p_start->status != BTA_JV_SUCCESS) { APPL_TRACE_ERROR("Error starting l2cap_listen - status: 0x%04x", p_start->status); btsock_l2cap_free_l(sock); return; } else { sock->handle = p_start->handle; APPL_TRACE_DEBUG("on_srv_l2cap_listen_started() sock->handle =%d id:%d", sock->handle, sock->id); if(sock->server_psm_sent == false) { if (!send_app_psm_or_chan_l(sock)) { //closed Loading @@ -435,24 +429,22 @@ static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START *p_start, uint32_t i } } } } pthread_mutex_unlock(&state_lock); } static void on_cl_l2cap_init(tBTA_JV_L2CAP_CL_INIT *p_init, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; if (p_init->status != BTA_JV_SUCCESS) { btsock_l2cap_free_l(sock); } else { sock->handle = p_init->handle; } return; } pthread_mutex_unlock(&state_lock); sock->handle = p_init->handle; } /** Loading @@ -464,7 +456,7 @@ static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN *p_open, l2cap_socket l2cap_socket *accept_rs; uint32_t new_listen_id; // Mutex locked by caller // std::mutex locked by caller accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); accept_rs->connected = true; accept_rs->security = sock->security; Loading Loading @@ -505,7 +497,7 @@ static void on_srv_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN *p_open, l2cap_socke l2cap_socket *accept_rs; uint32_t new_listen_id; // mutex locked by caller // std::mutex locked by caller accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); if (accept_rs) { Loading Loading @@ -582,11 +574,13 @@ static void on_l2cap_connect(tBTA_JV *p_data, uint32_t id) tBTA_JV_L2CAP_OPEN *psm_open = &p_data->l2c_open; tBTA_JV_L2CAP_LE_OPEN *le_open = &p_data->l2c_le_open; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (!sock) { APPL_TRACE_ERROR("on_l2cap_connect on unknown socket"); } else { return; } if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) { if (!sock->server) on_cl_l2cap_le_connect_l(le_open, sock); Loading @@ -601,16 +595,16 @@ static void on_l2cap_connect(tBTA_JV *p_data, uint32_t id) else btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); } static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE * p_close, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; APPL_TRACE_DEBUG("on_l2cap_close, slot id:%d, fd:%d, %s:%d, server:%d", sock->id, sock->our_fd, sock->fixed_chan ? "fixed_chan" : "PSM", sock->channel, sock->server); Loading @@ -621,16 +615,16 @@ static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE * p_close, uint32_t id) } btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); } static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG *p, uint32_t id) { l2cap_socket *sock; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; sock->outgoing_congest = p->cong ? 1 : 0; //mointer the fd for any outgoing data if (!sock->outgoing_congest) { Loading @@ -639,8 +633,6 @@ static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG *p, uint32_t id) } } pthread_mutex_unlock(&state_lock); } static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) { Loading @@ -652,17 +644,17 @@ static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) int app_uid = -1; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (!sock->outgoing_congest) { // monitor the fd for any outgoing data APPL_TRACE_DEBUG("on_l2cap_write_done: adding fd to btsock_thread..."); btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); } } pthread_mutex_unlock(&state_lock); uid_set_add_tx(uid_set, app_uid, len); } Loading @@ -676,17 +668,16 @@ static void on_l2cap_write_fixed_done(void* req_id, uint16_t len, uint32_t id) } int app_uid = -1; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (!sock->outgoing_congest) { //monitor the fd for any outgoing data btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); } } pthread_mutex_unlock(&state_lock); uid_set_add_tx(uid_set, app_uid, len); } Loading @@ -697,9 +688,11 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) int app_uid = -1; uint32_t bytes_read = 0; pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(id); if (sock) { if (!sock) return; app_uid = sock->app_uid; if (sock->fixed_chan) { /* we do these differently */ Loading @@ -719,7 +712,6 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) } } else { uint8_t buffer[L2CAP_MAX_SDU_LENGTH]; uint32_t count; Loading @@ -738,8 +730,6 @@ static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) } } } } pthread_mutex_unlock(&state_lock); uid_set_add_rx(uid_set, app_uid, bytes_read); } Loading Loading @@ -824,19 +814,17 @@ const tL2CAP_ERTM_INFO obex_l2c_etm_opt = void on_l2cap_psm_assigned(int id, int psm) { /* Setup ETM settings: * mtu will be set below */ pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); l2cap_socket *sock = btsock_l2cap_find_by_id_l(id); if (!sock) { APPL_TRACE_ERROR("%s: Error: sock is null", __func__); return; } if (sock) { sock->channel = psm; if (btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) btsock_l2cap_free_l(sock); } else { APPL_TRACE_ERROR("%s: Error: sock is null", __func__); } pthread_mutex_unlock(&state_lock); } static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock) { Loading Loading @@ -915,11 +903,9 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char *name, const bt_bda return BT_STATUS_NOT_READY; // TODO: This is kind of bad to lock here, but it is needed for the current design. pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_alloc_l(name, addr, listen, flags); if (!sock) { pthread_mutex_unlock(&state_lock); return BT_STATUS_NOMEM; } Loading Loading @@ -976,7 +962,6 @@ static bt_status_t btsock_l2cap_listen_or_connect(const char *name, const bt_bda } else { btsock_l2cap_free_l(sock); } pthread_mutex_unlock(&state_lock); return stat; } Loading Loading @@ -1028,9 +1013,11 @@ void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) char drop_it = false; /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to hold the lock. */ pthread_mutex_lock(&state_lock); std::unique_lock<std::mutex> lock(state_lock); sock = btsock_l2cap_find_by_id_l(user_id); if (sock) { if (!sock) return; if ((flags & SOCK_THREAD_FD_RD) && !sock->server) { // app sending data if (sock->connected) { Loading Loading @@ -1105,6 +1092,4 @@ void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) btsock_l2cap_free_l(sock); } } pthread_mutex_unlock(&state_lock); }