1.分析一下初始化的过程
btif/src/btif_core.c
在btif/src/btif_sock.c中
在btif/src/btif_sock_thread.c中
2. listen的过程
2.1 jni层会首先获取协议栈中的socket_profile的接口
在/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
在协议栈的btif/src/bluethooth.c中
返回sock_if函数指针,所以这个sBluetoothSocketInterface就是socket_profile的接口
2.2 jni层会调用socket_profile的接口中的listen
在./packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
createSocketChannelNative中会调用
sBluetoothSocketInterface->listen(...); //这个sBluetoothSocketInterface就是socket_profile的接口
在协议栈btif/src/btif_sock.c中
在协议栈btif/src/btif_rfc.c中
最后的btsock_thread_add_fd 就是调用send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)
向socketpair的fdw端写入命令
在btif/src/btif_sock_thread.c中
2.3 在poll中的线程函数sock_poll_thread会收到这个事件并处理
process_cmd_sock中会recv,此处的cmd=CMD_ADD_FD
在btif/src/btif_sock_thread.c中
add_poll
的作用就是把cmd
.
fd
添加到pollfd集合中去,下次线程中的poll就去轮询这个cmd.fd了
3. connect的过程
在/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
connect
在协议栈btif/src/btif_sock.c中
在btif/src/btif_sock_rfc.c中
btif/src/btif_core.c
- btif_enable_bluetooth_evt
- {
- /* initialize a2dp service */
- btif_av_init();
-
- /* init rfcomm & l2cap api */
- btif_sock_init();
-
- /* init pan */
- btif_pan_init();
-
- /* load did configuration */
- bte_load_did_conf(BTE_DID_CONF_FILE);
- }
在btif/src/btif_sock.c中
- bt_status_t btif_sock_init()
- {
- btsock_thread_init(); //初始化一些全局变量
- btsock_thread_create(btsock_signaled, NULL);
- btsock_rfc_init(handle);
- return BT_STATUS_FAIL;
- }
在btif/src/btif_sock_thread.c中
- int btsock_thread_create(btsock_signaled_cb callback, btsock_cmd_cb cmd_callback)
- {
- int ret = FALSE;
- asrt(callback || cmd_callback);
- lock_slot(&thread_slot_lock);
- int h = alloc_thread_slot();
- unlock_slot(&thread_slot_lock);
- if(h >= 0)
- {
- init_poll(h); //会调用init_cmd_fd中的socketpair创建读写的双通道
- if((ts[h].thread_id = create_thread(sock_poll_thread, (void*)h)) != -1) //创建线程
- {
- dbmsg("h:%d, thread id:%d", h, ts[h].thread_id);
- ts[h].callback = callback;
- ts[h].cmd_callback = cmd_callback;
- }
- else
- {
- free_thread_slot(h);
- h = -1;
- }
- }
- return h;
- }
2. listen的过程
2.1 jni层会首先获取协议栈中的socket_profile的接口
在/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
- static bool initNative(JNIEnv* env, jobject obj) {
- sBluetoothInterface->init(&sBluetoothCallbacks); //首先获取bt协议栈库的接口
- //然后调用协议栈的接口get_profile_interface,传入BT_PROFILE_SOCKETS_ID,就
- sBluetoothSocketInterface = (btsock_interface_t *)sBluetoothInterface->get_profile_interface(BT_PROFILE_SOCKETS_ID);
- }
- static const void* get_profile_interface (const char *profile_id)
- {
- if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
- return btif_sock_get_interface();
- }
2.2 jni层会调用socket_profile的接口中的listen
在./packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
createSocketChannelNative中会调用
sBluetoothSocketInterface->listen(...); //这个sBluetoothSocketInterface就是socket_profile的接口
在协议栈btif/src/btif_sock.c中
- static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
- const uint8_t* service_uuid, int channel, int* sock_fd, int flags)
- {
- //只支持RFCOMM
- btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, flags);
- }
- bt_status_t btsock_rfc_listen(const char* service_name, const uint8_t* service_uuid, int channel,
- int* sock_fd, int flags)
- {
- rfc_slot_t* rs = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, TRUE);
- BTA_JvCreateRecordByUser((void *)rs->id);
- *sock_fd = rs->app_fd; //将app_fd作为用户端监听
- rs->app_fd = -1; //the fd ownership is transferred to app
- status = BT_STATUS_SUCCESS;
- btsock_thread_add_fd(pth, rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, rs->id);
- return status;
- }
向socketpair的fdw端写入命令
在btif/src/btif_sock_thread.c中
- int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id)
- {
- sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id};
- return send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd);
- }
- static void *sock_poll_thread(void *arg)
- {
- struct pollfd pfds[MAX_POLL];
- memset(pfds, 0, sizeof(pfds));
- int h = (int)arg;
- for(;;)
- {
- //线程每次都要prepare_poll_fds,把在add_poll中新增的需要poll的fd,添加到pollfd集合中来
- prepare_poll_fds(h, pfds);
- poll(pfds, ts[h].poll_count, -1); //此处的poll会返回,并继续向下执行
- int need_process_data_fd = TRUE;
- if(pfds[0].revents)
- {
- process_cmd_sock(h); //开始处理cmd
- }
- if(need_process_data_fd)
- process_data_sock(h, pfds, ret);
- }
- return 0;
- }
在btif/src/btif_sock_thread.c中
- static int process_cmd_sock(int h)
- {
- sock_cmd_t cmd = {-1, 0, 0, 0, 0};
- int fd = ts[h].cmd_fdr;
- recv(fd, &cmd, sizeof(cmd), MSG_WAITALL);
- switch(cmd.id)
- {
- case CMD_ADD_FD:
- add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id);
- break;
- }
- return TRUE;
- }
3. connect的过程
在/packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
- static int connectSocketNative(JNIEnv *env, jobject object, jbyteArray address, jint type,
- jbyteArray uuidObj, jint channel, jint flag) {
- if (!sBluetoothSocketInterface) return NULL;
- sBluetoothSocketInterface->connect((bt_bdaddr_t *) addr, (btsock_type_t) type, (const uint8_t*) uuid, channel, &socket_fd, flag);
- }
- static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type,
- const uint8_t* uuid, int channel, int* sock_fd, int flags)
- {
- switch(type)
- {
- case BTSOCK_RFCOMM:
- status = btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags);
- break;
- }
- return status;
- }
在btif/src/btif_sock_rfc.c中
- bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t* service_uuid,
- int channel, int* sock_fd, int flags)
- {
- if(sock_fd == NULL || (service_uuid == NULL && (channel < 1 || channel > 30)))
- {
- dbmsg("invalid rfc channel:%d or sock_fd:%p, uuid:%p", channel, sock_fd,
- service_uuid);
- return BT_STATUS_PARM_INVALID;
- }
- *sock_fd = -1;
- if(!is_init_done())
- return BT_STATUS_NOT_READY;
- int status = BT_STATUS_FAIL;
- lock_slot(&slot_lock);
- rfc_slot_t* rs = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, FALSE);
- if(rs)
- {
- if(is_uuid_empty(service_uuid))
- {
- dbmsg("connecting to rfcomm channel:%d without service discovery", channel);
- if(BTA_JvRfcommConnect(rs->security, rs->role, rs->scn, rs->addr.address,
- rfcomm_cback, (void*)rs->id) == BTA_JV_SUCCESS)
- {
- if(send_app_scn(rs))
- {
- btsock_thread_add_fd(pth, rs->fd, BTSOCK_RFCOMM,
- SOCK_THREAD_FD_RD, rs->id);
- *sock_fd = rs->app_fd;
- rs->app_fd = -1; //the fd ownership is transferred to app
- status = BT_STATUS_SUCCESS;
- }
- else cleanup_rfc_slot(rs);
- }
- else cleanup_rfc_slot(rs);
- }
- else
- {
- tSDP_UUID sdp_uuid;
- sdp_uuid.len = 16;
- memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128));
- logu("service_uuid", service_uuid);
- *sock_fd = rs->app_fd;
- rs->app_fd = -1; //the fd ownership is transferred to app
- status = BT_STATUS_SUCCESS;
- rfc_slot_t* rs_doing_sdp = find_rfc_slot_requesting_sdp();
- if(rs_doing_sdp == NULL)
- {
- BTA_JvStartDiscovery((UINT8*)bd_addr->address, 1, &sdp_uuid, (void*)rs->id);
- rs->f.pending_sdp_request = FALSE;
- rs->f.doing_sdp_request = TRUE;
- }
- else
- {
- rs->f.pending_sdp_request = TRUE;
- rs->f.doing_sdp_request = FALSE;
- }
- btsock_thread_add_fd(pth, rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, rs->id);
- }
- }
- unlock_slot(&slot_lock);
- return status;
- }