1. 목적

ㄱ. 리눅스 상에서 블루투스를 통한 통신을 하기 위하여 rfcomm 방식의 통신의 예를 통해 통신하여 본다.


2. 본론

ㄱ. 먼저 블루투스 통신은 크게 2가지로 나뉜다. 

a. TCP 같은 기능을 하는 RFCOMM : TCP는 65535개의 포트를 지원하지만 RFCOMM은 30개의 포트를 지원함

b. UDP와 같은 기능을 하는 L2CAP


ㄴ. 아래의 소스코드는 RFCOMM 예제이이다. RFCOMM 프로토콜은 더욱 믿을 수 있는 연결을 지원한다(TCP와 유사함)


ㄷ. 다음의 명령어를 통해 서버측 디바이스의 블루투스 address를 알아 볼 수 있다.

$ hcitool dev

Devices:
hci0 00:1F:81:03:11:22


ㄹ. RFCOMM server.c 소스

  1. /*  
  2. compilation step  
  3.  
  4. # gcc -o server server.c -lbluetooth -lpthread  
  5.  
  6. */   
  7. #include <stdio.h>   
  8. #include <stdlib.h>   
  9. #include <string.h>   
  10. #include <unistd.h>   
  11. #include <signal.h>   
  12. #include <pthread.h>   
  13. #include <sys/socket.h>   
  14. #include <bluetooth/bluetooth.h>   
  15. #include <bluetooth/rfcomm.h>   
  16.   
  17. int s,client ;   
  18. void ctrl_c_handler(int signal);   
  19. void close_sockets();   
  20. void *readMsg();   
  21. void *sendMsg();   
  22.   
  23. int main(int argc,char **argv){   
  24. (void) signal(SIGINT,ctrl_c_handler);   
  25.   
  26. pthread_t readT, writeT;   
  27. char *message1 = "Read thread\n";   
  28. char *message2 = "Write thread\n";   
  29. int iret1, iret2;   
  30.   
  31. struct sockaddr_rc loc_addr={ 0 },client_addr={ 0 };   
  32. char buf[18] = { 0 };   
  33.   
  34. unsigned int opt = sizeof(client_addr) ;   
  35.   
  36.   
  37. //allocate socket   
  38. s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM) ;   
  39.   
  40.   
  41. //bind socket to port 1 of the first available   
  42. loc_addr.rc_family = AF_BLUETOOTH ;   
  43. str2ba("00:1F:81:00:00:01",&loc_addr.rc_bdaddr) ;//hci0; server device address is given   
  44. loc_addr.rc_channel = 1 ; //port (maximum should be 30 for RFCOMM)   
  45.   
  46. bind(s,(struct sockaddr *)&loc_addr,sizeof(loc_addr));   
  47. printf("Binding success\n");   
  48.   
  49. //put socket into listen mode   
  50. listen(s,1) ;   
  51. printf("socket in listen mode\n");   
  52. //accept one connection   
  53.   
  54. client = accept(s,(struct sockaddr *)&client_addr,&opt);   
  55. ba2str(&client_addr.rc_bdaddr,buf);   
  56. fprintf(stdout,"Connection accepted from %s\n",buf);   
  57.   
  58. /* Create independent threads each of which will execute function */   
  59.   
  60. iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);   
  61. iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);   
  62.   
  63. pthread_join(readT,NULL);   
  64. pthread_join(writeT,NULL);   
  65.   
  66. close_sockets() ;   
  67. return 0 ;   
  68. }   
  69.   
  70. void *sendMsg(){   
  71. char msg[25] ;   
  72. int status ;   
  73.   
  74. do{   
  75. memset(msg,0,sizeof(msg));   
  76. fgets(msg,24,stdin);   
  77. if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;   
  78. status = send(client,msg,strlen(msg),0);   
  79. fprintf(stdout,"Status = %d\n",status);   
  80. }while(status > 0);   
  81. }   
  82.   
  83. void *readMsg(){   
  84. int bytes_read;   
  85. char buf[1024] = { 0 };   
  86. do{   
  87. memset(buf,0,sizeof(buf));   
  88. //read data from the client   
  89. bytes_read = recv(client,buf,sizeof(buf),0) ;   
  90. fprintf(stdout,"Bytes read = %d\n",bytes_read);   
  91. if(bytes_read <= 0)break;   
  92. fprintf(stdout,"<<>> %s",buf);   
  93. }while(1);   
  94. }   
  95.   
  96. void close_sockets(){   
  97. //close connection   
  98. close(client);   
  99. close(s) ;   
  100. printf("sockets closed\n");   
  101. }   
  102.   
  103. void ctrl_c_handler(int signal) {   
  104. printf("Catched signal: %d ... !!\n", signal);   
  105. close_sockets();   
  106. exit(0);   
  107. //(void) signal(SIGINT, SIG_DFL);   
  108. }   


ㅁ. RFCOMM client.c 소스

  1. /*  
  2. compilation step  
  3.  
  4. # gcc -o client client.c -lbluetooth -lpthread  
  5.  
  6. */   
  7.   
  8. #include <stdio.h>   
  9. #include <stdlib.h>   
  10. #include <signal.h>   
  11. #include <pthread.h>   
  12. #include <unistd.h>   
  13. #include <sys/socket.h>   
  14. #include <bluetooth/bluetooth.h>   
  15. #include <bluetooth/rfcomm.h>   
  16.   
  17. int s ;   
  18. void ctrl_c_handler(int signal);   
  19. void close_sockets();   
  20. void *readMsg();   
  21. void *sendMsg();   
  22.   
  23. int main(int argc,char **argv){   
  24. (void) signal(SIGINT,ctrl_c_handler);   
  25.   
  26. pthread_t readT, writeT;   
  27. char *message1 = "Read thread\n";   
  28. char *message2 = "Write thread\n";   
  29. int iret1, iret2;   
  30.   
  31. struct sockaddr_rc addr= { 0 };   
  32. int status ;   
  33. char dest[18] = "00:1F:81:00:00:01";   
  34. char msg[25];   
  35.   
  36. //allocate a socket   
  37. s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);   
  38. addr.rc_family = AF_BLUETOOTH ;   
  39. addr.rc_channel = 1 ;   
  40. str2ba(dest,&addr.rc_bdaddr);   
  41.   
  42. //connect to server   
  43. printf("going 2 connect\n");   
  44. status = connect(s,(struct sockaddr *)&addr,sizeof(addr)) ;   
  45.   
  46. //send a message   
  47. if(0 == status){   
  48. printf("connect success\n");   
  49.   
  50. /* Create independent threads each of which will execute function */   
  51.   
  52. iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);   
  53. iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);   
  54.   
  55. pthread_join(readT,NULL);   
  56. pthread_join(writeT,NULL);   
  57.   
  58. }   
  59.   
  60.   
  61. close_sockets();   
  62. return 0;   
  63. }   
  64.   
  65. void *sendMsg(){   
  66. char msg[25] ;   
  67. int status ;   
  68.   
  69. do{   
  70. memset(msg,0,sizeof(msg));   
  71. fgets(msg,24,stdin);   
  72. if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;   
  73. status = send(s,msg,strlen(msg),0);   
  74. fprintf(stdout,"Status = %d\n",status);   
  75. }while(status > 0);   
  76. }   
  77.   
  78. void *readMsg(){   
  79. int bytes_read;   
  80. char buf[1024] = { 0 };   
  81. do{   
  82. memset(buf,0,sizeof(buf));   
  83. //read data from the client   
  84. bytes_read = recv(s,buf,sizeof(buf),0) ;   
  85. fprintf(stdout,"Bytes read = %d\n",bytes_read);   
  86. if(bytes_read <= 0)break;   
  87. fprintf(stdout,"<<>> %s",buf);   
  88. }while(1);   
  89. }   
  90.   
  91. void close_sockets(){   
  92. close(s);   
  93. fprintf(stdout,"Close sockets\n");   
  94. }   
  95.   
  96. void ctrl_c_handler(int signal){   
  97. fprintf(stdout,"Interrupt caught[NO: %d ]\n",signal);   
  98. close_sockets();   
  99. exit(0);   
  100. }   


3. 결론

ㄱ. 블루투스의 RFCOMM  프로토콜 통신의 예를 통해 학습하여 보았다.

Posted by 시크한공돌이
,