Skip to contentSkip to conversation

minicom over /dev/rfcomm0

Bluez-users Mark S. Townsley 3 participants 8 messages Dec 19, 2006 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

Hi:

In many examples that I found online where people are trying to make their cellphone surf web or somehow talk to their cellphone from Bluez, I notice that they can often do minicom over /dev/rfcomm0 in the setup.

If I want to access the serial port service like that, but from my C program, do I just treat /dev/rfcomm0 as a serial device and use standard Unix serial programming libraries from my C program to do so? Just curious. Thanks in advanced for any tip.

Mark

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#1 lore raw

Mark,
Here is a library I made on C++ based on the code from hcitool and some other code I found on the web, that let's you use the RFcomm layer as a serial port. It might need a lot of changes, it is just what I needed. Thanks
Manuel

Quoted 19 lines

Hi:

In many examples that I found online where people are trying to make their cellphone surf web or somehow talk to their cellphone from Bluez, I notice that they can often do minicom over /dev/rfcomm0 in the setup.

If I want to access the serial port service like that, but from my C program, do I just treat /dev/rfcomm0 as a serial device and use standard Unix serial programming libraries from my C program to do so? Just curious. Thanks in advanced for any tip.

Mark
------------------------------------------------------------------------

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
------------------------------------------------------------------------

Quoted 5 lines

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

/***************************************************************************
* Copyright (C) 2006 by Manuel Naranjo *
* [email protected] *

 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *

* *
* *
* This class is a C++ warper of a Bluetooth RFcomm Socket *
***************************************************************************/

#include "rfcomm.h"

#include <string>
#include <iostream>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

RfComm::RfComm(void){
	rfcommSocket = -1;
	bt_addr = "00:00:00:00:00:00";
}
RfComm::RfComm(string addr){
	rfcommSocket = -1;
	bt_addr = addr;
}
RfComm::~RfComm(void){
	Close();
}
void RfComm::Close(void){
	if (rfcommSocket != -1)
		close(rfcommSocket);

}

void RfComm::Open(void){
	bdaddr_t t_bdaddr;
	str2ba(bt_addr.c_str(), &t_bdaddr);

	struct sockaddr_rc sockaddr;
	sockaddr.rc_family = AF_BLUETOOTH;
	sockaddr.rc_bdaddr = t_bdaddr;
	sockaddr.rc_channel = (uint8_t) 1;

	rfcommSocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

	if (connect(rfcommSocket, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) < 0) {
		std::cerr << "Error While Openning RFComm Connection" <<endl;
    	rfcommSocket = -1;

}
}

void RfComm::setAddress(string new_addr){
	Close();
	bt_addr = new_addr;
}
int RfComm::Write(string message){
	int result = -1;
	if (rfcommSocket==-1){
		std::cerr << "Not a valid socket, aborting"<<endl;
		return result;
	}

	result = send(rfcommSocket, (void *)message.c_str(), message.size(), 0);

	if (result < 0)
		std::cerr << "There was an error when trying to write"<<endl;	

	return result;
}
string RfComm::Read(void){
	char response[64];
	string out = "";
	if (recv(rfcommSocket, response, 64, 0) < 0) {
		std::cerr << "There was an error while trying to read"<<endl;
		return NULL;

}

	out+=response;
	return out;
}
int RfComm::getLinkQuality(uint8_t * lq){
	struct hci_conn_info_req *cr;
	bdaddr_t bdaddr;

	int dd;
	int dev_id;

	str2ba(bt_addr.c_str(), &bdaddr);
	
	dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
	if (dev_id < 0) {
		std::cerr <<"Not connected, can't read Link Quality" << endl;
		return -1;
	}	

	dd = hci_open_dev(dev_id);
	if (dd < 0) {
		std::cerr << "HCI device open failed";
		return -1;
	}

	cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
	if (!cr) {
		std::cerr << "Can't allocate memory";
		return -1;
	}

	bacpy(&cr->bdaddr, &bdaddr);
	cr->type = ACL_LINK;
	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
		std::cerr << "Get connection info failed";
		return -1;
	}

	if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), lq, 1000) < 0) {
		std::cerr << "HCI read_link_quality request failed";
		return -1;
	}

	close(dd);
	free(cr);
	return 0;
}
int RfComm::getRSSI(int8_t * rssi){
	struct hci_conn_info_req *cr;
	bdaddr_t bdaddr;	
	int dd, dev_id;
	int8_t temp;
	str2ba(bt_addr.c_str(), &bdaddr);

	if (dev_id < 0) {
		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
		if (dev_id < 0) {
			std::cerr << "Not connected, can't read RSSI." << endl;
			return -1;
		}
	}

	dd = hci_open_dev(dev_id);
	if (dd < 0) {
		std::cerr << "HCI device open failed" << endl;
		return -1;
	}

	cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
	if (!cr) {
		std::cerr << "Can't allocate memory" << endl;
		return -1;
	}

	bacpy(&cr->bdaddr, &bdaddr);
	cr->type = ACL_LINK;
	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
		std::cerr << "Get connection info failed" << endl;
		return -1;
	}

	if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &temp, 1000) < 0) {
		std::cerr << "Read RSSI failed" << endl;
		return -1;
	}

	*rssi = temp;
	
	close(dd);
	free(cr);
	return 0;
}
int RfComm::getTransmitPowerLevel(int8_t * level, uint8_t * type){
	struct hci_conn_info_req *cr;
	bdaddr_t bdaddr;
	uint8_t temp;
	int dd, dev_id;
	
	str2ba(bt_addr.c_str(), &bdaddr);
	
	if (dev_id < 0) {
		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
		if (dev_id < 0) {
			std::cerr << "Not connected. Can't read Transmit Power Level." << endl;
			return -1;
		}
	}

	dd = hci_open_dev(dev_id);
	if (dd < 0) {
		std::cerr << "HCI device open failed" << endl;
		return -1;
	}

	cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
	if (!cr) {
		std::cerr << "Can't allocate memory" << endl;
		return -1;
	}

	bacpy(&cr->bdaddr, &bdaddr);
	cr->type = ACL_LINK;
	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
		std::cerr << "Get connection info failed" << endl;
		return -1;
	}

	if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), temp, level, 1000) < 0) {
		std::cerr << "HCI read transmit power level request failed" << endl;
		return -1;
	}

	*type =temp;

	close(dd);
	free(cr);
	
	return 0;
}

static int find_conn(int s, int dev_id, long arg)

{
	struct hci_conn_list_req *cl;
	struct hci_conn_info *ci;
	int i;

	if (!(cl = (hci_conn_list_req*)malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
		std::cerr << "Can't allocate memory";
		exit(1);
	}
	cl->dev_id = dev_id;
	cl->conn_num = 10;
	ci = cl->conn_info;

	if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
		std::cerr <<"Can't get connection list";
		exit(1);
	}

	for (i = 0; i < cl->conn_num; i++, ci++)
		if (!bacmp((bdaddr_t *) arg, &ci->bdaddr))
			return 1;

	return 0;
}

/***************************************************************************
* Copyright (C) 2006 by Manuel Naranjo *
* [email protected] *

 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *

***************************************************************************/

#ifndef __AIRcableRFCOMM_h__
#define __AIRcableRFCOMM_h__
#include <string>

#define ERROR 0xFFFF

using namespace std;

extern "C++"{	
	class RfComm{
		
		private:
			int		rfcommSocket;
			string	bt_addr;

		public:
			//constructor
			RfComm(void);

			RfComm(string bt_addr);

			~RfComm(void);

			void Open(void);

			void Close(void);

			int Write(string in);

			string Read(void);

			void setAddress(string new_addr);

			int getLinkQuality(uint8_t * lq);

			int getRSSI(int8_t * rssi);

			int getTransmitPowerLevel(int8_t * level, uint8_t * type);
	};
}; //C++

static int find_conn(int s, int dev_id, long arg);

#endif //__AIRcableRFCOMM_h__

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#2 lore raw

Thanks for the code. I have something simular in the way how I open RFCOMM as a socket.
So I think I am on the right track.

However, in reading your code, what I do not understand is what part of it are "serial" in nature?
In a quick glance, all are socket (and RFCOMM sockets) based. I don't see anything
that is serial in nature in your code (like open() a serial port to get a file descriptor etc....)

Sorry if I am asking stupid questions.

Mark

Quoted 444 lines · Manuel Naranjo

Mark,
Here is a library I made on C++ based on the code from hcitool and some other code I found on the web, that let's you use the RFcomm layer as a serial port. It might need a lot of changes, it is just what I needed. Thanks
Manuel

Quoted 8 lines

Hi:

In many examples that I found online where people are trying to make their cellphone surf web or somehow talk to their cellphone from Bluez, I notice that they can often do minicom over /dev/rfcomm0 in the setup.

Quoted 10 lines

If I want to access the serial port service like that, but from my C program, do I just treat /dev/rfcomm0 as a serial device and use standard Unix serial programming libraries from my C program to do so? Just curious. Thanks in advanced for any tip.

Mark
------------------------------------------------------------------------

-------------------------------------------------------------------------

Quoted 2 lines

Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share

your

Quoted 2 lines

opinions on IT & business topics through brief surveys - and earn cash

http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

Quoted 7 lines

------------------------------------------------------------------------

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

/***************************************************************************
* Copyright (C) 2006 by Manuel Naranjo *
* [email protected] *
*
*
* This program is free software; you can redistribute it and/or
modify *
* it under the terms of the GNU Library General Public License as
*
* published by the Free Software Foundation; either version 2 of
the *
* License, or (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
*
* but WITHOUT ANY WARRANTY; without even the implied warranty
of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* GNU General Public License for more
details. *
*
*
* You should have received a copy of the GNU Library General Public
*
* License along with this program; if not, write to the
*
* Free Software Foundation, Inc.,
*
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
*
*
* This class is a C++ warper of a Bluetooth RFcomm
Socket *

***************************************************************************/

#include "rfcomm.h"

#include <string>
#include <iostream>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

RfComm::RfComm(void){
        rfcommSocket = -1;
        bt_addr = "00:00:00:00:00:00";
}
RfComm::RfComm(string addr){
        rfcommSocket = -1;
        bt_addr = addr;
}
RfComm::~RfComm(void){
        Close();
}
void RfComm::Close(void){
        if (rfcommSocket != -1)
                close(rfcommSocket);

}

void RfComm::Open(void){
        bdaddr_t t_bdaddr;
        str2ba(bt_addr.c_str(), &t_bdaddr);

        struct sockaddr_rc sockaddr;
        sockaddr.rc_family = AF_BLUETOOTH;
        sockaddr.rc_bdaddr = t_bdaddr;
        sockaddr.rc_channel = (uint8_t) 1;

        rfcommSocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

        if (connect(rfcommSocket, (struct sockaddr *) &sockaddr,
sizeof(sockaddr)) < 0) {
                std::cerr << "Error While Openning RFComm Connection"
<<endl;
        rfcommSocket = -1;
        }
}
void RfComm::setAddress(string new_addr){
        Close();
        bt_addr = new_addr;
}
int RfComm::Write(string message){
        int result = -1;
        if (rfcommSocket==-1){
                std::cerr << "Not a valid socket, aborting"<<endl;
                return result;
        }

        result = send(rfcommSocket, (void *)message.c_str(), message.size(),

0);

if (result < 0)
std::cerr << "There was an error when trying to
write"<<endl;

        return result;
}
string RfComm::Read(void){
        char response[64];
        string out = "";
        if (recv(rfcommSocket, response, 64, 0) < 0) {
                std::cerr << "There was an error while trying to
read"<<endl;
                return NULL;
        }

        out+=response;
        return out;
}
int RfComm::getLinkQuality(uint8_t * lq){
        struct hci_conn_info_req *cr;
        bdaddr_t bdaddr;

        int dd;
        int dev_id;

        str2ba(bt_addr.c_str(), &bdaddr);

        dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
        if (dev_id < 0) {
                std::cerr <<"Not connected, can't read Link Quality" <<
endl;
                return -1;
        }

        dd = hci_open_dev(dev_id);
        if (dd < 0) {
                std::cerr << "HCI device open failed";
                return -1;
        }

        cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct
hci_conn_info));
        if (!cr) {
                std::cerr << "Can't allocate memory";
                return -1;
        }

        bacpy(&cr->bdaddr, &bdaddr);
        cr->type = ACL_LINK;
        if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                std::cerr << "Get connection info failed";
                return -1;
        }

        if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), lq,
1000) < 0) {
                std::cerr << "HCI read_link_quality request failed";
                return -1;
        }

        close(dd);
        free(cr);
        return 0;
}
int RfComm::getRSSI(int8_t * rssi){
        struct hci_conn_info_req *cr;
        bdaddr_t bdaddr;
        int dd, dev_id;
        int8_t temp;
        str2ba(bt_addr.c_str(), &bdaddr);

        if (dev_id < 0) {
                dev_id = hci_for_each_dev(HCI_UP, find_conn, (long)
&bdaddr);
                if (dev_id < 0) {
                        std::cerr << "Not connected, can't read RSSI." <<
endl;
                        return -1;
                }
        }

        dd = hci_open_dev(dev_id);
        if (dd < 0) {
                std::cerr << "HCI device open failed" << endl;
                return -1;
        }

        cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct
hci_conn_info));
        if (!cr) {
                std::cerr << "Can't allocate memory" << endl;
                return -1;
        }

        bacpy(&cr->bdaddr, &bdaddr);
        cr->type = ACL_LINK;
        if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                std::cerr << "Get connection info failed" << endl;
                return -1;
        }

        if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &temp, 1000) <
0) {
                std::cerr << "Read RSSI failed" << endl;
                return -1;
        }

        *rssi = temp;

        close(dd);
        free(cr);
        return 0;
}
int RfComm::getTransmitPowerLevel(int8_t * level, uint8_t * type){
        struct hci_conn_info_req *cr;
        bdaddr_t bdaddr;
        uint8_t temp;
        int dd, dev_id;

        str2ba(bt_addr.c_str(), &bdaddr);

        if (dev_id < 0) {
                dev_id = hci_for_each_dev(HCI_UP, find_conn, (long)
&bdaddr);
                if (dev_id < 0) {
                        std::cerr << "Not connected. Can't read Transmit
Power Level." << endl;
                        return -1;
                }
        }

        dd = hci_open_dev(dev_id);
        if (dd < 0) {
                std::cerr << "HCI device open failed" << endl;
                return -1;
        }

        cr = (hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct
hci_conn_info));
        if (!cr) {
                std::cerr << "Can't allocate memory" << endl;
                return -1;
        }

        bacpy(&cr->bdaddr, &bdaddr);
        cr->type = ACL_LINK;
        if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                std::cerr << "Get connection info failed" << endl;
                return -1;
        }

        if (hci_read_transmit_power_level(dd,
htobs(cr->conn_info->handle), temp, level, 1000) < 0) {
                std::cerr << "HCI read transmit power level request
failed" << endl;
                return -1;
        }

        *type =temp;

        close(dd);
        free(cr);

        return 0;
}

static int find_conn(int s, int dev_id, long arg)

{
        struct hci_conn_list_req *cl;
        struct hci_conn_info *ci;
        int i;

        if (!(cl = (hci_conn_list_req*)malloc(10 * sizeof(*ci) +
sizeof(*cl)))) {
                std::cerr << "Can't allocate memory";
                exit(1);
        }
        cl->dev_id = dev_id;
        cl->conn_num = 10;
        ci = cl->conn_info;

        if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
                std::cerr <<"Can't get connection list";
                exit(1);
        }

        for (i = 0; i < cl->conn_num; i++, ci++)
                if (!bacmp((bdaddr_t *) arg, &ci->bdaddr))
                        return 1;

        return 0;
}

/***************************************************************************
* Copyright (C) 2006 by Manuel Naranjo *
* [email protected] *
*
*
* This program is free software; you can redistribute it and/or
modify *
* it under the terms of the GNU Library General Public License as
*
* published by the Free Software Foundation; either version 2 of
the *
* License, or (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
*
* but WITHOUT ANY WARRANTY; without even the implied warranty
of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* GNU General Public License for more
details. *
*
*
* You should have received a copy of the GNU Library General Public
*
* License along with this program; if not, write to the
*
* Free Software Foundation, Inc.,
*
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*

***************************************************************************/

#ifndef __AIRcableRFCOMM_h__
#define __AIRcableRFCOMM_h__
#include <string>

#define ERROR 0xFFFF

using namespace std;

extern "C++"{
        class RfComm{

                private:
                        int             rfcommSocket;
                        string  bt_addr;

                public:
                        //constructor
                        RfComm(void);

                        RfComm(string bt_addr);

                        ~RfComm(void);

                        void Open(void);

                        void Close(void);

                        int Write(string in);

                        string Read(void);

                        void setAddress(string new_addr);

                        int getLinkQuality(uint8_t * lq);

                        int getRSSI(int8_t * rssi);

                        int getTransmitPowerLevel(int8_t * level, uint8_t
* type);
        };
}; //C++

static int find_conn(int s, int dev_id, long arg);

#endif //__AIRcableRFCOMM_h__

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

Quoted 7 lines

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#3 lore raw

You are right, I was going to add a SerialStream for this, but then I decided it is useless, it is a socket indeed. If you want to open as a serial port open the file /dev/rfcomm0 as if it is a normal serial port.

Quoted 489 lines

Thanks for the code. I have something simular in the way how I open RFCOMM as a socket.
So I think I am on the right track.

However, in reading your code, what I do not understand is what part of it are "serial" in nature?
In a quick glance, all are socket (and RFCOMM sockets) based. I don't see anything
that is serial in nature in your code (like open() a serial port to get a file descriptor etc....)

Sorry if I am asking stupid questions.

Mark

On 12/19/06, *Manuel Naranjo* <[email protected] <mailto:[email protected]>> wrote:

    Mark,
    Here is a library I made on C++ based on the code from hcitool and
    some
    other code I found on the web, that let's you use the RFcomm layer
    as a
    serial port. It might need a lot of changes, it is just what I
    needed.
    Thanks
    Manuel
    >
    > Hi:
    >
    > In many examples that I found online where people are trying to make
    > their cellphone surf web or somehow talk to their cellphone from
    > Bluez, I notice that they can often do minicom over /dev/rfcomm0 in
    > the setup.
    >
    > If I want to access the serial port service like that, but from my C
    > program, do I just treat /dev/rfcomm0 as a serial device and use
    > standard Unix serial programming libraries from my C program to
    do so?
    > Just curious.  Thanks in advanced for any tip.
    >
    >
    > Mark
    >
    ------------------------------------------------------------------------
    >
    >
    -------------------------------------------------------------------------

    > Take Surveys. Earn Cash. Influence the Future of IT
    > Join SourceForge.net's Techsay panel and you'll get the chance
    to share your
    > opinions on IT & business topics through brief surveys - and
    earn cash
    >
    http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
    <http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV>
    >
    ------------------------------------------------------------------------

    >
    > _______________________________________________
    > Bluez-users mailing list
    > [email protected]
    <mailto:[email protected]>
    > https://lists.sourceforge.net/lists/listinfo/bluez-users
    >



    /***************************************************************************
    *   Copyright (C) 2006 by Manuel Naranjo   *
    *   [email protected] <mailto:[email protected]>   *
    *                                                                        
    *
    *   This program is free software; you can redistribute it and/or
    modify  *
    *   it under the terms of the GNU Library General Public License
    as       *
    *   published by the Free Software Foundation; either version 2 of
    the    *
    *   License, or (at your option) any later
    version.                       *
    *                                                                        
    *
    *   This program is distributed in the hope that it will be
    useful,       *
    *   but WITHOUT ANY WARRANTY; without even the implied warranty
    of        *
    *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
    the         *
    *   GNU General Public License for more
    details.                          *
    *                                                                        
    *
    *   You should have received a copy of the GNU Library General
    Public     *
    *   License along with this program; if not, write to
    the                 *
    *   Free Software Foundation,
    Inc.,                                       *
    *   59 Temple Place - Suite 330, Boston, MA  02111-1307,
    USA.             *
    *                                                                                                                                                
    *
    *                                                                                                                                                
    *
    *   This class is a C++ warper of a Bluetooth RFcomm
    Socket                       *
    ***************************************************************************/

    #include "rfcomm.h"

    #include <string>
    #include <iostream>
    #include <sys/param.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>

    #include <bluetooth/bluetooth.h>
    #include <bluetooth/rfcomm.h>
    #include <bluetooth/hci.h>
    #include <bluetooth/hci_lib.h>

    RfComm::RfComm(void){
            rfcommSocket = -1;
            bt_addr = "00:00:00:00:00:00";
    }

    RfComm::RfComm(string addr){
            rfcommSocket = -1;
            bt_addr = addr;
    }

    RfComm::~RfComm(void){
            Close();
    }

    void RfComm::Close(void){
            if (rfcommSocket != -1)
                    close(rfcommSocket);

    }

    void RfComm::Open(void){
            bdaddr_t t_bdaddr;
            str2ba(bt_addr.c_str(), &t_bdaddr);

            struct sockaddr_rc sockaddr;
            sockaddr.rc_family = AF_BLUETOOTH;
            sockaddr.rc_bdaddr = t_bdaddr;
             sockaddr.rc_channel = (uint8_t) 1;

            rfcommSocket = socket(AF_BLUETOOTH, SOCK_STREAM,
    BTPROTO_RFCOMM);

            if (connect(rfcommSocket, (struct sockaddr *) &sockaddr,
    sizeof(sockaddr)) < 0) {
                    std::cerr << "Error While Openning RFComm
    Connection" <<endl;
            rfcommSocket = -1;
            }
    }

    void RfComm::setAddress(string new_addr){
            Close();
            bt_addr = new_addr;
    }

    int RfComm::Write(string message){
            int result = -1;
            if (rfcommSocket==-1){
                    std::cerr << "Not a valid socket, aborting"<<endl;
                    return result;
            }

            result = send(rfcommSocket, (void *)message.c_str(),
    message.size(), 0);

            if (result < 0)
                    std::cerr << "There was an error when trying to
    write"<<endl;

            return result;
    }

    string RfComm::Read(void){
            char response[64];
            string out = "";
            if (recv(rfcommSocket, response, 64, 0) < 0) {
                    std::cerr << "There was an error while trying to
    read"<<endl;
                    return NULL;
            }

            out+=response;
            return out;
    }

    int RfComm::getLinkQuality(uint8_t * lq){
            struct hci_conn_info_req *cr;
            bdaddr_t bdaddr;

            int dd;
            int dev_id;

            str2ba(bt_addr.c_str(), &bdaddr);

            dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
            if (dev_id < 0) {
                    std::cerr <<"Not connected, can't read Link
    Quality" << endl;
                    return -1;
            }

            dd = hci_open_dev(dev_id);
            if (dd < 0) {
                    std::cerr << "HCI device open failed";
                    return -1;
            }

            cr = (hci_conn_info_req*)malloc(sizeof(*cr) +
    sizeof(struct hci_conn_info));
            if (!cr) {
                    std::cerr << "Can't allocate memory";
                    return -1;
            }

            bacpy(&cr->bdaddr, &bdaddr);
            cr->type = ACL_LINK;
            if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                    std::cerr << "Get connection info failed";
                    return -1;
            }

            if (hci_read_link_quality(dd,
    htobs(cr->conn_info->handle), lq, 1000) < 0) {
                    std::cerr << "HCI read_link_quality request failed";
                    return -1;
            }

            close(dd);
            free(cr);
            return 0;
    }

    int RfComm::getRSSI(int8_t * rssi){
            struct hci_conn_info_req *cr;
            bdaddr_t bdaddr;
            int dd, dev_id;
            int8_t temp;
            str2ba(bt_addr.c_str(), &bdaddr);

            if (dev_id < 0) {
                    dev_id = hci_for_each_dev(HCI_UP, find_conn,
    (long) &bdaddr);
                    if (dev_id < 0) {
                            std::cerr << "Not connected, can't read
    RSSI." << endl;
                            return -1;
                    }
            }

            dd = hci_open_dev(dev_id);
            if (dd < 0) {
                    std::cerr << "HCI device open failed" << endl;
                    return -1;
            }

            cr = (hci_conn_info_req*)malloc(sizeof(*cr) +
    sizeof(struct hci_conn_info));
            if (!cr) {
                    std::cerr << "Can't allocate memory" << endl;
                    return -1;
            }

            bacpy(&cr->bdaddr, &bdaddr);
            cr->type = ACL_LINK;
            if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                    std::cerr << "Get connection info failed" << endl;
                    return -1;
            }

            if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &temp,
    1000) < 0) {
                    std::cerr << "Read RSSI failed" << endl;
                    return -1;
            }

            *rssi = temp;

            close(dd);
            free(cr);
            return 0;
    }

    int RfComm::getTransmitPowerLevel(int8_t * level, uint8_t * type){
            struct hci_conn_info_req *cr;
            bdaddr_t bdaddr;
            uint8_t temp;
            int dd, dev_id;

            str2ba(bt_addr.c_str(), &bdaddr);

            if (dev_id < 0) {
                    dev_id = hci_for_each_dev(HCI_UP, find_conn,
    (long) &bdaddr);
                    if (dev_id < 0) {
                            std::cerr << "Not connected. Can't read
    Transmit Power Level." << endl;
                            return -1;
                    }
            }

            dd = hci_open_dev(dev_id);
            if (dd < 0) {
                    std::cerr << "HCI device open failed" << endl;
                    return -1;
            }

            cr = (hci_conn_info_req*)malloc(sizeof(*cr) +
    sizeof(struct hci_conn_info));
            if (!cr) {
                    std::cerr << "Can't allocate memory" << endl;
                    return -1;
            }

            bacpy(&cr->bdaddr, &bdaddr);
            cr->type = ACL_LINK;
            if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
                    std::cerr << "Get connection info failed" << endl;
                    return -1;
            }

            if (hci_read_transmit_power_level(dd,
    htobs(cr->conn_info->handle), temp, level, 1000) < 0) {
                    std::cerr << "HCI read transmit power level
    request failed" << endl;
                    return -1;
            }

            *type =temp;

            close(dd);
            free(cr);

            return 0;
    }

    static int find_conn(int s, int dev_id, long arg)
    {
            struct hci_conn_list_req *cl;
            struct hci_conn_info *ci;
            int i;

            if (!(cl = (hci_conn_list_req*)malloc(10 * sizeof(*ci) +
    sizeof(*cl)))) {
                    std::cerr << "Can't allocate memory";
                    exit(1);
            }
            cl->dev_id = dev_id;
            cl->conn_num = 10;
            ci = cl->conn_info;

            if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
                    std::cerr <<"Can't get connection list";
                    exit(1);
            }

            for (i = 0; i < cl->conn_num; i++, ci++)
                    if (!bacmp((bdaddr_t *) arg, &ci->bdaddr))
                            return 1;

            return 0;
    }





    /***************************************************************************
    *   Copyright (C) 2006 by Manuel Naranjo   *
    *   [email protected] <mailto:[email protected]>    *
    *                                                                        
    *
    *   This program is free software; you can redistribute it and/or
    modify  *
    *   it under the terms of the GNU Library General Public License
    as       *
    *   published by the Free Software Foundation; either version 2 of
    the    *
    *   License, or (at your option) any later
    version.                       *
    *                                                                        
    *
    *   This program is distributed in the hope that it will be
    useful,       *
    *   but WITHOUT ANY WARRANTY; without even the implied warranty
    of        *
    *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
    the         *
    *   GNU General Public License for more
    details.                          *
    *                                                                        
    *
    *   You should have received a copy of the GNU Library General
    Public     *
    *   License along with this program; if not, write to
    the                 *
    *   Free Software Foundation,
    Inc.,                                       *
    *   59 Temple Place - Suite 330, Boston, MA  02111-1307,
    USA.             *
    ***************************************************************************/

    #ifndef __AIRcableRFCOMM_h__
    #define __AIRcableRFCOMM_h__
    #include <string>

    #define ERROR 0xFFFF

    using namespace std;

    extern "C++"{
            class RfComm{

                    private:
                            int             rfcommSocket;
                            string  bt_addr;

                    public:
                            //constructor
                            RfComm(void);

                            RfComm(string bt_addr);

                            ~RfComm(void);

                            void Open(void);

                            void Close(void);

                            int Write(string in);

                            string Read(void);

                            void setAddress(string new_addr);

                            int getLinkQuality(uint8_t * lq);

                            int getRSSI(int8_t * rssi);

                            int getTransmitPowerLevel(int8_t * level,
    uint8_t * type);
            };
    }; //C++

    static int find_conn(int s, int dev_id, long arg);

    #endif //__AIRcableRFCOMM_h__


    -------------------------------------------------------------------------
    Take Surveys. Earn Cash. Influence the Future of IT
    Join SourceForge.net's Techsay panel and you'll get the chance to
    share your
    opinions on IT & business topics through brief surveys - and earn cash
    http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
    <http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV>

    _______________________________________________
    Bluez-users mailing list
    [email protected]
    <mailto:[email protected]>
    https://lists.sourceforge.net/lists/listinfo/bluez-users

------------------------------------------------------------------------

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
------------------------------------------------------------------------

Quoted 5 lines

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#4 lore raw
In reply to Mark S. Townsley

Hi,

Quoted 9 lines · Mark S. Townsley

In many examples that I found online where people are trying to make their cellphone surf web or somehow talk to their cellphone from Bluez, I notice that they can often do minicom over /dev/ rfcomm0 in the setup.

If I want to access the serial port service like that, but from my C program, do I just treat /dev/rfcomm0 as a serial device and use standard Unix serial programming libraries from my C program to do so? Just curious. Thanks in advanced for any tip.

You sure can.

You can of course use the code provided by Manuel too but then your code won't work without rfcomm/bluetooth. The nice thing is that if you keep it generic you can test your application using any old serial connection. Or use a pipe in the filesystem etc..

Caveat: I can't get mgetty to work by feeding it /dev/rfcommX though :
( It should work, must be one of those things ;)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#5 lore raw
Quoted 9 lines

You sure can.

You can of course use the code provided by Manuel too but then your code won't work without rfcomm/bluetooth. The nice thing is that if you keep it generic you can test your application using any old serial connection. Or use a pipe in the filesystem etc..

The idea with mine is that I needed to measure RSSI and wanted some control over the BlueZ Stack :D

Quoted 4 lines

Caveat: I can't get mgetty to work by feeding it /dev/rfcommX though :
( It should work, must be one of those things ;)

Quoted 11 lines

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#6 lore raw
In reply to Bas Schulte

Hi Bas: thanks for your reply.

So are you implying that if I do not go through RFCOMM and all the sockets stuff, I can
actually open /dev/rfcomm0 and get back a file descriptor just like any other serial port?

Thanks

Mark

Quoted 26 lines · Bas Schulte

Hi,

Quoted 9 lines · Mark S. Townsley

In many examples that I found online where people are trying to make their cellphone surf web or somehow talk to their cellphone from Bluez, I notice that they can often do minicom over /dev/ rfcomm0 in the setup.

If I want to access the serial port service like that, but from my C program, do I just treat /dev/rfcomm0 as a serial device and use standard Unix serial programming libraries from my C program to do so? Just curious. Thanks in advanced for any tip.

You sure can.

You can of course use the code provided by Manuel too but then your code won't work without rfcomm/bluetooth. The nice thing is that if you keep it generic you can test your application using any old serial connection. Or use a pipe in the filesystem etc..

Caveat: I can't get mgetty to work by feeding it /dev/rfcommX though :
( It should work, must be one of those things ;)

Quoted 11 lines

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#7 lore raw

Hi,

Quoted 6 lines · Mark S. Townsley

Hi Bas: thanks for your reply.

So are you implying that if I do not go through RFCOMM and all the sockets stuff, I can
actually open /dev/rfcomm0 and get back a file descriptor just like any other serial port?

Yep. I actually just managed to get mgetty working ;)

Here's how:

# rfcomm listen 3 4

This creates /dev/rfcomm3, available on channel 4. It spits out something like:

Waiting for connection on channel 4
Press CTRL-C for hangup

Just let this guy run.

Then start mgetty, in another terminal:

# mgetty -r -b /dev/rfcomm3

Before all of this, I did a "sdptool add --channel=4 SP" to let the client know there's a Serial Port service on channel 4.

From another box, I connected to this channel and got a nice "login:
" prompt.

Minor thing is that mgetty complains a bit about some ioctl's didn't go right, I don't think it likes being started from the command line, but it does work.

I'd say this pretty much shows you can just use plain serial io against these devices ;)

I don't (yet) understand exactly how rfcomm works and in what state it creates the device, for instance, "rfcomm bind ..." doesn't work.

Cheers,

Bas.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users