[BACK]Return to bluetooth.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libbluetooth

File: [cvs.NetBSD.org] / src / lib / libbluetooth / bluetooth.c (download)

Revision 1.1, Mon Jun 19 15:44:36 2006 UTC (13 years, 4 months ago) by gdamore
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9, netbsd-8-base, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, mjf-devfs2-base, mjf-devfs2, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, localcount-20160914, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, abandoned-netbsd-4-base, abandoned-netbsd-4, HEAD

Initial import of bluetooth stack on behalf of Iain Hibbert.  (plunky@,
NetBSD Foundation Membership still pending.)  This stack was written by
Iain under sponsorship from Itronix Inc.

The stack includes support for rfcomm networking (networking via your
bluetooth enabled cell phone), hid devices (keyboards/mice), and headsets.

Drivers for both PCMCIA and USB bluetooth controllers are included.

/*	$NetBSD: bluetooth.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $	*/

/*
 * bluetooth.c
 *
 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: bluetooth.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $
 * $FreeBSD: src/lib/libbluetooth/bluetooth.c,v 1.2 2004/03/05 08:10:17 markm Exp $
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: bluetooth.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $");

#include <bluetooth.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define _PATH_BT_HOSTS		"/etc/bluetooth/hosts"
#define _PATH_BT_PROTOCOLS	"/etc/bluetooth/protocols"
#define MAXALIASES		 35

static FILE		*hostf = NULL;
static int		 host_stayopen = 0;
static struct hostent	 host;
static bdaddr_t		 host_addr;
static char		*host_addr_ptrs[2];
static char		*host_aliases[MAXALIASES];

static FILE		*protof = NULL;
static int		 proto_stayopen = 0;
static struct protoent	 proto;
static char		*proto_aliases[MAXALIASES];

static char		 buf[BUFSIZ + 1];

static int bt_hex_byte   (char const *str);
static int bt_hex_nibble (char nibble);

struct hostent *
bt_gethostbyname(char const *name)
{
	struct hostent	*p;
	char		**cp;

	bt_sethostent(host_stayopen);
	while ((p = bt_gethostent()) != NULL) {
		if (strcasecmp(p->h_name, name) == 0)
			break;
		for (cp = p->h_aliases; *cp != 0; cp++)
			if (strcasecmp(*cp, name) == 0)
				goto found;
	}
found:
	bt_endhostent();

	return (p);
}

struct hostent *
bt_gethostbyaddr(char const *addr, socklen_t len, int type)
{
	struct hostent	*p;

	if (type != AF_BLUETOOTH || len != sizeof(bdaddr_t)) {
		h_errno = NO_RECOVERY;
		return (NULL);
	}

	bt_sethostent(host_stayopen);
	while ((p = bt_gethostent()) != NULL)
		if (p->h_addrtype == type && memcmp(p->h_addr, addr, len) == 0)
			break;
	bt_endhostent();

	return (p);
}

struct hostent *
bt_gethostent(void)
{
	char	*p, *cp, **q;

	if (hostf == NULL)
		hostf = fopen(_PATH_BT_HOSTS, "r");

	if (hostf == NULL) {
		h_errno = NETDB_INTERNAL;
		return (NULL);
	}
again:
	if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
		h_errno = HOST_NOT_FOUND;
		return (NULL);
	}
	if (*p == '#')
		goto again;
	if ((cp = strpbrk(p, "#\n")) == NULL)
		goto again;
	*cp = 0;
	if ((cp = strpbrk(p, " \t")) == NULL)
		goto again;
	*cp++ = 0;
	if (bt_aton(p, &host_addr) == 0)
		goto again;
	host_addr_ptrs[0] = (char *) &host_addr;
	host_addr_ptrs[1] = NULL;
	host.h_addr_list = host_addr_ptrs;
	host.h_length = sizeof(host_addr);
	host.h_addrtype = AF_BLUETOOTH;
	while (*cp == ' ' || *cp == '\t')
		cp++;
	host.h_name = cp;
	q = host.h_aliases = host_aliases;
	if ((cp = strpbrk(cp, " \t")) != NULL)
		*cp++ = 0;
	while (cp != NULL && *cp != 0) {
		if (*cp == ' ' || *cp == '\t') {
			cp++;
			continue;
		}
		if (q < &host_aliases[MAXALIASES - 1])
			*q++ = cp;
		if ((cp = strpbrk(cp, " \t")) != NULL)
			*cp++ = 0;
	}
	*q = NULL;
	h_errno = NETDB_SUCCESS;

	return (&host);
}

void
bt_sethostent(int stayopen)
{
	if (hostf == NULL)
		hostf = fopen(_PATH_BT_HOSTS, "r");
	else
		rewind(hostf);

	host_stayopen = stayopen;
}

void
bt_endhostent(void)
{
	if (hostf != NULL && host_stayopen == 0) {
		(void) fclose(hostf);
		hostf = NULL;
	}
}

struct protoent *
bt_getprotobyname(char const *name)
{
	struct protoent	 *p;
	char		**cp;

	bt_setprotoent(proto_stayopen);
	while ((p = bt_getprotoent()) != NULL) {
		if (strcmp(p->p_name, name) == 0)
			break;
		for (cp = p->p_aliases; *cp != 0; cp++)
			if (strcmp(*cp, name) == 0)
				goto found;
	}
found:
	bt_endprotoent();

	return (p);
}

struct protoent *
bt_getprotobynumber(int num)
{
	struct protoent	*p;

	bt_setprotoent(proto_stayopen);
	while ((p = bt_getprotoent()) != NULL)
		if (p->p_proto == num)
			break;
	bt_endprotoent();

	return (p);
}

struct protoent *
bt_getprotoent(void)
{
	char	*p, *cp, **q;

	if (protof == NULL)
		protof = fopen(_PATH_BT_PROTOCOLS, "r");

	if (protof == NULL)
		return (NULL);
again:
	if ((p = fgets(buf, sizeof(buf), protof)) == NULL)
		return (NULL);
	if (*p == '#')
		goto again;
	if ((cp = strpbrk(p, "#\n")) == NULL)
		goto again;
	*cp = '\0';
	proto.p_name = p;
	if ((cp = strpbrk(p, " \t")) == NULL)
		goto again;
	*cp++ = '\0';
	while (*cp == ' ' || *cp == '\t')
		cp++;
	if ((p = strpbrk(cp, " \t")) != NULL)
		*p++ = '\0';
	proto.p_proto = (int)strtol(cp, NULL, 0);
	q = proto.p_aliases = proto_aliases;
	if (p != NULL) {
		cp = p;
		while (cp != NULL && *cp != 0) {
			if (*cp == ' ' || *cp == '\t') {
				cp++;
				continue;
			}
			if (q < &proto_aliases[MAXALIASES - 1])
				*q++ = cp;
			if ((cp = strpbrk(cp, " \t")) != NULL)
				*cp++ = '\0';
		}
	}
	*q = NULL;

	return (&proto);
}

void
bt_setprotoent(int stayopen)
{
	if (protof == NULL)
		protof = fopen(_PATH_BT_PROTOCOLS, "r");
	else
		rewind(protof);

	proto_stayopen = stayopen;
}

void
bt_endprotoent(void)
{
	if (protof != NULL) {
		(void) fclose(protof);
		protof = NULL;
	}
}

char const *
bt_ntoa(bdaddr_t const *ba, char *str)
{
	static char	buffer[24];

	if (str == NULL)
		str = buffer;

	sprintf(str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
		ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);

	return (str);
}

int
bt_aton(char const *str, bdaddr_t *ba)
{
	int	 i, b;
	char	*end = NULL;

	memset(ba, 0, sizeof(*ba));

	for (i = 5, end = strchr(str, ':');
	     i > 0 && *str != '\0' && end != NULL;
	     i --, str = end + 1, end = strchr(str, ':')) {
		switch (end - str) {
		case 1:
			b = bt_hex_nibble(str[0]);
			break;

		case 2:
			b = bt_hex_byte(str);
			break;

		default:
			b = -1;
			break;
		}

		if (b < 0)
			return (0);

		ba->b[i] = b;
	}

	if (i != 0 || end != NULL || *str == 0)
		return (0);

	switch (strlen(str)) {
	case 1:
		b = bt_hex_nibble(str[0]);
		break;

	case 2:
		b = bt_hex_byte(str);
		break;

	default:
		b = -1;
		break;
	}

	if (b < 0)
		return (0);

	ba->b[i] = b;

	return (1);
}

static int
bt_hex_byte(char const *str)
{
	int	n1, n2;

	if ((n1 = bt_hex_nibble(str[0])) < 0)
		return (-1);

	if ((n2 = bt_hex_nibble(str[1])) < 0)
		return (-1);

	return ((((n1 & 0x0f) << 4) | (n2 & 0x0f)) & 0xff);
}

static int
bt_hex_nibble(char nibble)
{
	if ('0' <= nibble && nibble <= '9')
		return (nibble - '0');

	if ('a' <= nibble && nibble <= 'f')
		return (nibble - 'a' + 0xa);

	if ('A' <= nibble && nibble <= 'F')
		return (nibble - 'A' + 0xa);

	return (-1);
}