你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu Bluetooth 调试

[日期:2012-06-07] 来源:Linux社区  作者:jmhgg88 [字体:大 中 小]

接下来看io_accept_event这个接口:

  1. static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data)  
  2. {  
  3.     GIOChannel *io;  
  4.     int nsk;  
  5.   
  6.     if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {  
  7.         g_io_channel_unref(chan);  
  8.         return FALSE;  
  9.     }  
  10.   
  11.     if (data == &l2cap_sock) {  
  12.         struct sockaddr_l2 addr;  
  13.         socklen_t len = sizeof(addr);  
  14.   
  15.         nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len);  
  16.     } else if (data == &unix_sock) {  
  17.         struct sockaddr_un addr;  
  18.         socklen_t len = sizeof(addr);  
  19.   
  20.         nsk = accept(unix_sock, (struct sockaddr *) &addr, &len);  
  21.     } else  
  22.         return FALSE;  
  23.   
  24.     if (nsk < 0) {  
  25.         error("Can't accept connection: %s", strerror(errno));  
  26.         return TRUE;  
  27.     }  
  28.   
  29.     //here we accept the connect from client, and then generate a new socket, the new socket   
  30.     //is for really data transfer, and here we can see we use io_session_event func to deal with    
  31.     //the new socket for processing the coming data.   
  32.     io = g_io_channel_unix_new(nsk);  
  33.     g_io_channel_set_close_on_unref(io, TRUE);  
  34.   
  35.     g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,  
  36.                     io_session_event, data);  
  37.   
  38.     g_io_channel_unref(io);  
  39.   
  40.     return TRUE;  
  41. }  
这个接口实现接受client过来的连接,并且为这个连接创建新的socket,这个www.linuxidc.com新创建的socket就是用来为这次连接传输数据的。对这次连接数据的处理函数

为其中注册的io_session_event接口。下面看io_session_event数据处理接口:

  1. static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data)  
  2. {  
  3.     sdp_pdu_hdr_t hdr;  
  4.     uint8_t *buf;  
  5.     int sk, len, size;  
  6.   
  7.     if (cond & G_IO_NVAL)  
  8.         return FALSE;  
  9.   
  10.     sk = g_io_channel_unix_get_fd(chan);  
  11.   
  12.     if (cond & (G_IO_HUP | G_IO_ERR)) {  
  13.         sdp_svcdb_collect_all(sk);  
  14.         return FALSE;  
  15.     }  
  16.   
  17.     //first receive the sdp pdu hdr, connecting msg i guess   
  18.     len = recv(sk, &hdr, sizeof(sdp_pdu_hdr_t), MSG_PEEK);  
  19.     if (len <= 0) {  
  20.         sdp_svcdb_collect_all(sk);  
  21.         return FALSE;  
  22.     }  
  23.   
  24.     size = sizeof(sdp_pdu_hdr_t) + ntohs(hdr.plen);  
  25.     buf = malloc(size);  
  26.     if (!buf)  
  27.         return TRUE;  
  28.   
  29.     //then receive the real data   
  30.     len = recv(sk, buf, size, 0);  
  31.     if (len <= 0) {  
  32.         sdp_svcdb_collect_all(sk);  
  33.         free(buf);  
  34.         return FALSE;  
  35.     }  
  36.   
  37.     //here we will process the data   
  38.     handle_request(sk, buf, len);  
  39.   
  40.     return TRUE;  
  41. }  
这里首先recv了sdp头结构数据,然后再recv其他数据。我们看到他真正处理的是第二次接收的数据。把数据传送给接口handle_request处理。handle_request接口将上面recv到的数据转化为
sdp_req_t结构体,然后辗转调用到process_request这个函数,并将sdp_req_t结构传入,sdp_req_t结构如下:
  1. typedef struct request {  
  2.     bdaddr_t device;  
  3.     bdaddr_t bdaddr;  
  4.     int      local;  
  5.     int      sock;  
  6.     int      mtu;  
  7.     int      flags;  
  8.     uint8_t  *buf;  
  9.     int      len;  
  10. } sdp_req_t;  
linux
相关资讯       bluetoothd 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款