Sunday, July 4, 2010

Bluetooth RFCOMM example in C

1) Packages needed:- bluetooth,bluez,libbluetooth, libbluetooth-dev and pthread support

*Below is a text chat client-server example which uses bluetooth as a medium for communication.
*RFCOMM protocol is used here because it's more reliable(similar to TCP).
*It uses the pthread library for multithreading.
* You can use the command hcitool to list the bluetooth devices in your computer.
$ hcitool dev
Devices:
hci0 00:1F:81:00:01:00

SERVER (server.c)
[code]
/*
compilation step

# gcc -o server server.c -lbluetooth -lpthread

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int s,client ;
void ctrl_c_handler(int signal);
void close_sockets();
void *readMsg();
void *sendMsg();

int main(int argc,char **argv){
(void) signal(SIGINT,ctrl_c_handler);

pthread_t readT, writeT;
char *message1 = "Read thread\n";
char *message2 = "Write thread\n";
int iret1, iret2;

struct sockaddr_rc loc_addr={ 0 },client_addr={ 0 };
char buf[18] = { 0 };

unsigned int opt = sizeof(client_addr) ;


//allocate socket
s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM) ;


//bind socket to port 1 of the first available
loc_addr.rc_family = AF_BLUETOOTH ;
str2ba("00:1F:81:00:00:01",&loc_addr.rc_bdaddr) ;//hci0; server device address is given
loc_addr.rc_channel = 1 ; //port (maximum should be 30 for RFCOMM)

bind(s,(struct sockaddr *)&loc_addr,sizeof(loc_addr));
printf("Binding success\n");

//put socket into listen mode
listen(s,1) ;
printf("socket in listen mode\n");
//accept one connection

client = accept(s,(struct sockaddr *)&client_addr,&opt);
ba2str(&client_addr.rc_bdaddr,buf);
fprintf(stdout,"Connection accepted from %s\n",buf);

/* Create independent threads each of which will execute function */

iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);
iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);

pthread_join(readT,NULL);
pthread_join(writeT,NULL);

close_sockets() ;
return 0 ;
}

void *sendMsg(){
char msg[25] ;
int status ;

do{
memset(msg,0,sizeof(msg));
fgets(msg,24,stdin);
if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;
status = send(client,msg,strlen(msg),0);
fprintf(stdout,"Status = %d\n",status);
}while(status > 0);
}

void *readMsg(){
int bytes_read;
char buf[1024] = { 0 };
do{
memset(buf,0,sizeof(buf));
//read data from the client
bytes_read = recv(client,buf,sizeof(buf),0) ;
fprintf(stdout,"Bytes read = %d\n",bytes_read);
if(bytes_read <= 0)break;
fprintf(stdout,"<<>> %s",buf);
}while(1);
}

void close_sockets(){
//close connection
close(client);
close(s) ;
printf("sockets closed\n");
}

void ctrl_c_handler(int signal) {
printf("Catched signal: %d ... !!\n", signal);
close_sockets();
exit(0);
//(void) signal(SIGINT, SIG_DFL);
}



CLIENT(client.c)

/*
compilation step

# gcc -o client client.c -lbluetooth -lpthread

*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int s ;
void ctrl_c_handler(int signal);
void close_sockets();
void *readMsg();
void *sendMsg();

int main(int argc,char **argv){
(void) signal(SIGINT,ctrl_c_handler);

pthread_t readT, writeT;
char *message1 = "Read thread\n";
char *message2 = "Write thread\n";
int iret1, iret2;

struct sockaddr_rc addr= { 0 };
int status ;
char dest[18] = "00:1F:81:00:00:01";
char msg[25];

//allocate a socket
s = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
addr.rc_family = AF_BLUETOOTH ;
addr.rc_channel = 1 ;
str2ba(dest,&addr.rc_bdaddr);

//connect to server
printf("going 2 connect\n");
status = connect(s,(struct sockaddr *)&addr,sizeof(addr)) ;

//send a message
if(0 == status){
printf("connect success\n");

/* Create independent threads each of which will execute function */

iret1 = pthread_create(&readT,NULL,readMsg,(void*) message1);
iret2 = pthread_create(&writeT,NULL,sendMsg,(void*) message2);

pthread_join(readT,NULL);
pthread_join(writeT,NULL);

}


close_sockets();
return 0;
}

void *sendMsg(){
char msg[25] ;
int status ;

do{
memset(msg,0,sizeof(msg));
fgets(msg,24,stdin);
if(strncmp("EXIT",msg,4)==0 || strncmp("exit",msg,4)==0)break;
status = send(s,msg,strlen(msg),0);
fprintf(stdout,"Status = %d\n",status);
}while(status > 0);
}

void *readMsg(){
int bytes_read;
char buf[1024] = { 0 };
do{
memset(buf,0,sizeof(buf));
//read data from the client
bytes_read = recv(s,buf,sizeof(buf),0) ;
fprintf(stdout,"Bytes read = %d\n",bytes_read);
if(bytes_read <= 0)break;
fprintf(stdout,"<<>> %s",buf);
}while(1);
}

void close_sockets(){
close(s);
fprintf(stdout,"Close sockets\n");
}

void ctrl_c_handler(int signal){
fprintf(stdout,"Interrupt caught[NO: %d ]\n",signal);
close_sockets();
exit(0);
}

[/code]

No comments:

Post a Comment