[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bluetooth-dev] Relationship between components



Matthew Palmer wrote:

> On Sun, 11 Feb 2001, Greg KH wrote:
>
> > bt.o _is_ the Bluetooth protocol stack.  It enables other user space
> > programs to connect through it to talk to other devices.
>
> Is there any documentation available that describes how I talk to bt.o
> without the aid of btd?  I know I can work out what's going on from reading
> btd's source, I was just wondering if there was an easier way...
>

I've attached a start on a minimal app to setup bluetooth so that another app
can open /dev/ttyBT* and use it like a char driver. It doesn't setup scan
inquiry or initiate a connection, but it's a head start.

>
> > Think of bt.o like ppp.o.  ppp.o provides the PPP logic to enable you to
> > do TCP/IP over a physical type connection (modem, serial, and hey,
> > bluetooth...)  Does that help out a bit more.
>
> Except that, AFAIK, ppp.o provides a network port which you can route and
> play with, whereas bt.o does not, and it wouldn't be recomended anyway
> because TCP/IP over BT is only one (and not nearly the best) way of using
> BT's abilities...
>
> Hence my current dilemma - talking to bt.o... <g>
>

An app can use /dev/ttyBT* like a char driver or a tty once it's setup over the
physical connection to the hardware. This is what btd does before spawning pppd
and telling it to use /dev/ttBT0. Each of the minor numbers will correspond to
a different RFCOMM channel.

--gmcnutt
/*
 * Note: 1) this is for a kernel mode stack -- you need to insmod bt.o before
 *          executing this
 *       2) if your h/w assumes a different initial baud rate then change it
 *          below
 */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "bluetooth/include/linux/bluetooth/btcommon.h"

int main(int argc, char **argv)
{
	int bt_ldisc = N_BT, ser_fd, bt_fd;
	struct termios t;

	/*
	 * Setup the serial port to the initial baud rate of the bluetooth
	 * device (vendor-specific).
	 */
	if ((ser_fd = open("/dev/ttyS0", O_RDWR)) < 0) {
		perror("open /dev/ttyS0");
		exit(1);
	}

	if (tcgetattr(ser_fd, &t) < 0) {
		perror("tcgetattr");
		exit(1);
	}

	cfmakeraw(&t);
	t.c_cflag &= ~CBAUD;
	t.c_cflag |= B57600 | CS8 | CLOCAL; /* initial baud rate */
	t.c_oflag = 0;
	t.c_lflag = 0;
	t.c_cflag &= ~CRTSCTS; /* flow control off -- depends on vendor! */

	if (tcsetattr(ser_fd, TCSANOW, &t) < 0) {
		perror("tcsetattr");
		exit(1);
	}
	
	if (ioctl(ser_fd, TIOCSETD, &bt_ldisc) < 0) {
		perror("TIOCSETD");
		exit(1);
	}
	

	/*
	 * Setup the bluetooth stack.
	 */
	if ((bt_fd = open("/dev/ttyBT0", O_RDWR)) < 0) {
		perror("open /dev/ttyBT0");
		exit(1);
	}

	if (ioctl(bt_fd, BTINITSTACK) < 0) {
		perror("BTINITSTACK");
	}

	/*
	 * Initialize the h/w (assumed digidongle)
	 */
	//ioctl(bt_fd, BTSETDIGIBAUDRATE, 115200); /* final baud rate */

	/*
	 * Set the serial port baud rate to match the new h/w rate
	 */
	if (tcgetattr(ser_fd, &t) < 0) {
		perror("tcgetattr");
		exit(1);
	}

	cfmakeraw(&t);
	t.c_cflag &= ~CBAUD;
	t.c_cflag |= B115200 | CS8 | CLOCAL; /* initial baud rate */
	t.c_oflag = 0;
	t.c_lflag = 0;
	t.c_cflag &= ~CRTSCTS; /* flow control off */

	if (tcsetattr(ser_fd, TCSANOW, &t) < 0) {
		perror("tcsetattr");
		exit(1);
	}
	
	/*
	 * Now, the app can do whatever it wants over the bluetooth
	 * tty... you can fork/exec it here or just hang around here and
	 * let the app use a different /dev/ttBT*.
	 */

}