Break out Bluetooth functions in own module
This commit is contained in:
parent
36bbcbe182
commit
d1e0ca32db
5 changed files with 152 additions and 232 deletions
9
Makefile
9
Makefile
|
@ -11,7 +11,7 @@ LDFLAGS = `pkg-config --libs glib-2.0` `pkg-config --libs dbus-glib-1` `pkg-conf
|
||||||
|
|
||||||
PRGNAME = metawatch
|
PRGNAME = metawatch
|
||||||
|
|
||||||
MEMBERS = metawatch crc16ccitt mw_utility mw_main
|
MEMBERS = metawatch crc16ccitt mw_utility mw_main bt_helper
|
||||||
|
|
||||||
# no need to change anything below this line
|
# no need to change anything below this line
|
||||||
# ------------------------------------------
|
# ------------------------------------------
|
||||||
|
@ -24,13 +24,14 @@ SOURCES = $(patsubst %,%.c,$(MEMBERS))
|
||||||
OBJS = $(patsubst %,%.o,$(MEMBERS))
|
OBJS = $(patsubst %,%.o,$(MEMBERS))
|
||||||
DEPS = $(patsubst %,%.d,$(MEMBERS))
|
DEPS = $(patsubst %,%.d,$(MEMBERS))
|
||||||
|
|
||||||
all: $(PRGNAME)
|
all: $(PRGNAME) libmetawatch.a
|
||||||
|
|
||||||
$(PRGNAME): $(OBJS)
|
$(PRGNAME): $(OBJS)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS)
|
$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
libmetawatch.a: metawatch.o crc16ccitt.o mw_utility.o
|
libmetawatch.a: metawatch.o crc16ccitt.o mw_utility.o bt_helper.o
|
||||||
ar -cvq libmetawatch.a metawatch.o crc16ccitt.o mw_utility.o
|
ar -cvq libmetawatch.a metawatch.o crc16ccitt.o mw_utility.o bt_helper.o
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.d $(PRGNAME)
|
rm -f *.o *.d $(PRGNAME)
|
||||||
|
|
||||||
|
|
136
bt_helper.c
Normal file
136
bt_helper.c
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
/*
|
||||||
|
* (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the
|
||||||
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
|
* Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <bluetooth/bluetooth.h>
|
||||||
|
#include <bluetooth/rfcomm.h>
|
||||||
|
|
||||||
|
|
||||||
|
int open_socket(bdaddr_t *bdaddr, uint8_t channel)
|
||||||
|
{
|
||||||
|
struct sockaddr_rc addr;
|
||||||
|
int sk, opt;
|
||||||
|
|
||||||
|
sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||||
|
if (sk < 0) {
|
||||||
|
fprintf(stderr, "Can't create socket: %s (%d)\n",
|
||||||
|
strerror(errno), errno);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.rc_family = AF_BLUETOOTH;
|
||||||
|
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
|
||||||
|
|
||||||
|
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||||
|
fprintf(stderr, "Can't bind socket: %s (%d)\n", strerror(errno), errno);
|
||||||
|
close(sk);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set link mode */
|
||||||
|
opt = 0;
|
||||||
|
opt |= RFCOMM_LM_MASTER;
|
||||||
|
opt |= RFCOMM_LM_AUTH;
|
||||||
|
/*
|
||||||
|
opt |= RFCOMM_LM_ENCRYPT;
|
||||||
|
opt |= RFCOMM_LM_SECURE;
|
||||||
|
*/
|
||||||
|
if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
|
||||||
|
fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)", strerror(errno), errno);
|
||||||
|
close(sk);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.rc_family = AF_BLUETOOTH;
|
||||||
|
bacpy(&addr.rc_bdaddr, bdaddr);
|
||||||
|
addr.rc_channel = channel;
|
||||||
|
|
||||||
|
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||||
|
fprintf(stderr, "Can't connect: %s (%d)\n", strerror(errno), errno);
|
||||||
|
close(sk);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sk;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mbaswap(bdaddr_t *dst, const bdaddr_t *src)
|
||||||
|
{
|
||||||
|
register unsigned char *d = (unsigned char *) dst;
|
||||||
|
register const unsigned char *s = (const unsigned char *) src;
|
||||||
|
register int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 6; i++)
|
||||||
|
d[i] = s[5-i];
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mbachk(const char *str)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (strlen(str) != 17)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
while (*str) {
|
||||||
|
if (!isxdigit(*str++))
|
||||||
|
return -1;
|
||||||
|
if (!isxdigit(*str++))
|
||||||
|
return -1;
|
||||||
|
if (*str == 0)
|
||||||
|
break;
|
||||||
|
if (*str++ != ':')
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int str2ba(const char *str, bdaddr_t *ba)
|
||||||
|
{
|
||||||
|
bdaddr_t b;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (mbachk(str) < 0) {
|
||||||
|
memset(ba, 0, sizeof(*ba));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < 6; i++, str += 3)
|
||||||
|
b.b[i] = strtol(str, NULL, 16);
|
||||||
|
|
||||||
|
mbaswap(ba, &b);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
8
bt_helper.h
Normal file
8
bt_helper.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef _BT_HELPER_H
|
||||||
|
#define _BT_HELPER_H
|
||||||
|
|
||||||
|
int str2ba(const char *str, bdaddr_t *ba);
|
||||||
|
int open_socket(bdaddr_t *bdaddr, uint8_t channel);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <metawatch.h>
|
#include <metawatch.h>
|
||||||
#include <crc16ccitt.h>
|
#include <crc16ccitt.h>
|
||||||
#include <mw_utility.h>
|
#include <mw_utility.h>
|
||||||
|
#include <bt_helper.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// GMainLoop *mloop;
|
// GMainLoop *mloop;
|
||||||
|
@ -175,120 +176,6 @@ void mw_get_real_time_clock_response_cb(mwdevice_t *mwdevice, struct tm *mw_tm,
|
||||||
gtk_button_set_label(rtc_button, label_str);
|
gtk_button_set_label(rtc_button, label_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
int open_socket(bdaddr_t *bdaddr, uint8_t channel)
|
|
||||||
{
|
|
||||||
struct sockaddr_rc addr;
|
|
||||||
int sk, opt;
|
|
||||||
|
|
||||||
sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
||||||
if (sk < 0) {
|
|
||||||
fprintf(stderr, "Can't create socket: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
f = 1;
|
|
||||||
if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
|
|
||||||
fprintf(stderr, "Can't set flushable: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.rc_family = AF_BLUETOOTH;
|
|
||||||
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
|
|
||||||
|
|
||||||
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
||||||
fprintf(stderr, "Can't bind socket: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set link mode */
|
|
||||||
opt = 0;
|
|
||||||
opt |= RFCOMM_LM_MASTER;
|
|
||||||
opt |= RFCOMM_LM_AUTH;
|
|
||||||
/*
|
|
||||||
opt |= RFCOMM_LM_ENCRYPT;
|
|
||||||
opt |= RFCOMM_LM_SECURE;
|
|
||||||
*/
|
|
||||||
if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
|
|
||||||
fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.rc_family = AF_BLUETOOTH;
|
|
||||||
bacpy(&addr.rc_bdaddr, bdaddr);
|
|
||||||
addr.rc_channel = channel;
|
|
||||||
|
|
||||||
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
||||||
fprintf(stderr, "Can't connect: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sk;
|
|
||||||
}
|
|
||||||
|
|
||||||
void baswap(bdaddr_t *dst, const bdaddr_t *src)
|
|
||||||
{
|
|
||||||
register unsigned char *d = (unsigned char *) dst;
|
|
||||||
register const unsigned char *s = (const unsigned char *) src;
|
|
||||||
register int i;
|
|
||||||
|
|
||||||
for (i = 0; i < 6; i++)
|
|
||||||
d[i] = s[5-i];
|
|
||||||
}
|
|
||||||
|
|
||||||
int bachk(const char *str)
|
|
||||||
{
|
|
||||||
if (!str)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (strlen(str) != 17)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
while (*str) {
|
|
||||||
if (!isxdigit(*str++))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!isxdigit(*str++))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (*str == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (*str++ != ':')
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int str2ba(const char *str, bdaddr_t *ba)
|
|
||||||
{
|
|
||||||
bdaddr_t b;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (bachk(str) < 0) {
|
|
||||||
memset(ba, 0, sizeof(*ba));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < 6; i++, str += 3)
|
|
||||||
b.b[i] = strtol(str, NULL, 16);
|
|
||||||
|
|
||||||
baswap(ba, &b);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
|
gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
|
||||||
{
|
{
|
||||||
mwdata_t *mdata = (mwdata_t *)udata;
|
mwdata_t *mdata = (mwdata_t *)udata;
|
||||||
|
|
116
mw_main.c
116
mw_main.c
|
@ -34,6 +34,8 @@
|
||||||
#include "crc16ccitt.h"
|
#include "crc16ccitt.h"
|
||||||
#include "mw_utility.h"
|
#include "mw_utility.h"
|
||||||
|
|
||||||
|
#include "bt_helper.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GMainLoop *mloop;
|
GMainLoop *mloop;
|
||||||
mwdevice_t mwdevice;
|
mwdevice_t mwdevice;
|
||||||
|
@ -439,120 +441,6 @@ int feed_menu(mwdata_t *mdata)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int open_socket(bdaddr_t *bdaddr, uint8_t channel)
|
|
||||||
{
|
|
||||||
struct sockaddr_rc addr;
|
|
||||||
int sk, opt;
|
|
||||||
//int f;
|
|
||||||
|
|
||||||
sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
||||||
if (sk < 0) {
|
|
||||||
fprintf(stderr, "Can't create socket: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
f = 1;
|
|
||||||
if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
|
|
||||||
fprintf(stderr, "Can't set flushable: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.rc_family = AF_BLUETOOTH;
|
|
||||||
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
|
|
||||||
|
|
||||||
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
||||||
fprintf(stderr, "Can't bind socket: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set link mode */
|
|
||||||
opt = 0;
|
|
||||||
opt |= RFCOMM_LM_MASTER;
|
|
||||||
opt |= RFCOMM_LM_AUTH;
|
|
||||||
/*
|
|
||||||
opt |= RFCOMM_LM_ENCRYPT;
|
|
||||||
opt |= RFCOMM_LM_SECURE;
|
|
||||||
*/
|
|
||||||
if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
|
|
||||||
fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.rc_family = AF_BLUETOOTH;
|
|
||||||
bacpy(&addr.rc_bdaddr, bdaddr);
|
|
||||||
addr.rc_channel = channel;
|
|
||||||
|
|
||||||
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
||||||
fprintf(stderr, "Can't connect: %s (%d)\n",
|
|
||||||
strerror(errno), errno);
|
|
||||||
close(sk);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sk;
|
|
||||||
}
|
|
||||||
|
|
||||||
void baswap(bdaddr_t *dst, const bdaddr_t *src)
|
|
||||||
{
|
|
||||||
register unsigned char *d = (unsigned char *) dst;
|
|
||||||
register const unsigned char *s = (const unsigned char *) src;
|
|
||||||
register int i;
|
|
||||||
|
|
||||||
for (i = 0; i < 6; i++)
|
|
||||||
d[i] = s[5-i];
|
|
||||||
}
|
|
||||||
|
|
||||||
int bachk(const char *str)
|
|
||||||
{
|
|
||||||
if (!str)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (strlen(str) != 17)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
while (*str) {
|
|
||||||
if (!isxdigit(*str++))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!isxdigit(*str++))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (*str == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (*str++ != ':')
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int str2ba(const char *str, bdaddr_t *ba)
|
|
||||||
{
|
|
||||||
bdaddr_t b;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (bachk(str) < 0) {
|
|
||||||
memset(ba, 0, sizeof(*ba));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < 6; i++, str += 3)
|
|
||||||
b.b[i] = strtol(str, NULL, 16);
|
|
||||||
|
|
||||||
baswap(ba, &b);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
|
gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue