added port for Android 4.x devices with MediaTek MT65xx Bluetooth chipset

This commit is contained in:
matthias.ringwald@gmail.com 2015-02-06 09:40:12 +00:00
parent f5a72a7640
commit 00894323b4
10 changed files with 497 additions and 0 deletions

View File

@ -0,0 +1,8 @@
/** BTstack Daemon Respawn */
int main(int argc, char * argv[]){
while (1){
system("/system/bin/BTstackDaemon");
}
return 0;
}

110
platforms/mtk/Makefile Normal file
View File

@ -0,0 +1,110 @@
# Toolchain config
NDK = /Projects/Android/android-ndk-r9d
ADB = adb
TOOLCHAIN = /tmp/ndk-standalone
SYSROOT = $(TOOLCHAIN)/sysroot
GCC = $(TOOLCHAIN)/bin/arm-linux-androideabi-gcc
STRIP = $(TOOLCHAIN)/bin/arm-linux-androideabi-strip
CFLAGS = -march=armv7-a -mfloat-abi=softfp -I$(SYSROOT)/usr/include
LDFLAGS = -Wl,--fix-cortex-a8 -L$(SYSROOT)/usr/lib
BTSTACK_ROOT = ../..
CFLAGS += -I$(BTSTACK_ROOT)/include
CFLAGS += -I$(BTSTACK_ROOT)/src/
CFLAGS += -I$(BTSTACK_ROOT)/ble
CFLAGS += -I.
VPATH += $(BTSTACK_ROOT)/src
VPATH += $(BTSTACK_ROOT)/ble
VPATH += $(BTSTACK_ROOT)/platforms/posix/src
VPATH += $(BTSTACK_ROOT)/example/daemon
libBTstack_OBJS = \
hci_cmds.o \
hci_dump.o \
linked_list.o \
run_loop.o \
sdp_util.o \
utils.o \
btstack.o \
run_loop_posix.o \
socket_connection.o \
BTdaemon_OBJS = $(libBTstack_OBJS) \
att_dispatch.o \
central_device_db_memory.o \
att.o \
att_server.o \
gatt_client.o \
sm.o \
btstack_memory.o \
hci.o \
l2cap.o \
l2cap_signaling.o \
memory_pool.o \
remote_device_db_memory.o \
rfcomm.o \
sdp.o \
sdp_client.o \
sdp_parser.o \
sdp_query_rfcomm.o \
sdp_query_util.o \
daemon.o \
hci_transport_h4_mtk.o \
all: $(TOOLCHAIN) BTstackDaemon libBTstack.so BTstackDaemonRespawn inquiry le_scan rfcomm-echo dist
dist:
rm -f *.o
clean:
rm -f BTstackDaemon BTstackDaemonRespawn le_scan inquiry rfcomm-echo *.so *.o
$(TOOLCHAIN):
$(NDK)/build/tools/make-standalone-toolchain.sh --install-dir=$@
libbluetoothdrv.so: bluetoothdrv-stub.o
$(GCC) $(LDFLAGS) -shared -o libbluetoothdrv.so bluetoothdrv-stub.o
.c.o:
$(GCC) $(CFLAGS) -o $@ -c $<
install:
./installer.sh
run:
$(ADB) shell le_scan
shell:
$(ADB) shell
BTstackDaemon: $(BTdaemon_OBJS) libbluetoothdrv.so
$(BTSTACK_ROOT)/tools/get_version.sh
$(GCC) $(LDFLAGS) -o $@ libbluetoothdrv.so $^
BTstackDaemonRespawn: BTstackDaemonRespawn.o
$(GCC) $(LDFLAGS) -o $@ BTstackDaemonRespawn.o
libBTstack.so: $(libBTstack_OBJS)
$(GCC) $(LDFLAGS) -shared -Wl,-soname,/system/lib/libBTstack.so -o $@ $^
inquiry: inquiry.o
$(GCC) $(LDFLAGS) -Wl,-rpath,/system/lib libBTstack.so -o $@ $(BTSTACK_ROOT)/example/daemon/inquiry.o
rfcomm-echo: rfcomm-echo.o
$(GCC) $(LDFLAGS) -Wl,-rpath,/system/lib libBTstack.so -o $@ $(BTSTACK_ROOT)/example/daemon/rfcomm-echo.o
le_scan: le_scan.o
$(GCC) $(LDFLAGS) -Wl,-rpath,/system/lib libBTstack.so -o $@ $(BTSTACK_ROOT)/example/daemon/le_scan.o
# minimal_peripheral: minimal_peripheral.o
# $(GCC) $(LDFLAGS) libs/libbluetoothdrv.so -o $@ minimal_peripheral.o
hci_dump:
-killall PacketLogger
$(ADB) shell su root chmod 666 /data/btstack/hci_dump.pklg
$(ADB) pull /data/btstack/hci_dump.pklg 2> /dev/null
open hci_dump.pklg

View File

@ -0,0 +1,21 @@
// stub library to allow linking against libbluetoothdrv.so without having a copy of it
#include "bluetoothdrv.h"
// @returns fd for Bluetooth UART if successfull
int mtk_bt_enable(void){
return 0;
}
// disables Bluetooth
void mtk_bt_disable(int fd){
}
//
int mtk_bt_write(int fd, uint8_t * buffer, size_t len){
return 0;
}
// @returns number of bytes read
int mtk_bt_read(int fd, uint8_t * buffer, size_t len){
return 0;
}

View File

@ -0,0 +1,16 @@
// libbluetoothdrv.so wrapper around libbluetooth_mtk.so
#pragma once
#include <stdint.h>
// @returns fd for Bluetooth UART if successfull
int mtk_bt_enable(void);
// disables Bluetooth
void mtk_bt_disable(int fd);
//
int mtk_bt_write(int fd, uint8_t * buffer, size_t len);
// @returns number of bytes read
int mtk_bt_read(int fd, uint8_t * buffer, size_t len);

View File

@ -0,0 +1,28 @@
// btstack-config.h created by configure for BTstack Thu Apr 24 14:34:39 CEST 2014
#ifndef __BTSTACK_CONFIG
#define __BTSTACK_CONFIG
#define HAVE_TRANSPORT_H4
#define UART_DEVICE "/dev/ttyS0"
#define UART_SPEED 115200
#define USE_POWERMANAGEMENT
#define USE_POSIX_RUN_LOOP
#define HAVE_SDP
#define HAVE_RFCOMM
#define HAVE_BLE
#define REMOTE_DEVICE_DB remote_device_db_memory
// #define HAVE_SO_NOSIGPIPE
#define HAVE_TIME
#define HAVE_MALLOC
#define HAVE_BZERO
#define HAVE_HCI_DUMP
#define ENABLE_LOG_INFO
#define ENABLE_LOG_ERROR
#define HCI_ACL_PAYLOAD_SIZE 1021
#define SDP_DES_DUMP
#define BTSTACK_UNIX "/data/btstack/BTstack"
#define BTSTACK_LOG_FILE "/data/btstack/hci_dump.pklg"
#endif

32
platforms/mtk/deinstaller.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
echo "BTstack Deinstaller for RugGear/Mediatek devices"
# make /system writable
if adb shell mount | grep -q "/system ext4 ro" ; then
echo "- remounting /system as read/write"
adb shell su root mount -o remount,rw /emmc@android /system
fi
if adb shell mount | grep -q "/system ext4 ro" ; then
echo "- remounting failed, abort"
fi
echo "- /system mounted as read/write"
if adb shell stat /system/bin/BTstackDaemon | grep -q "regular file" ; then
echo "- deleting BTstackDaemon and BTstackDaemonRespawn"
adb shell su root rm /system/bin/BTstackDaemon
adb shell su root rm /system/bin/BTstackDaemonRespawn
fi
echo "- stopping Bluetooth daemon"
adb shell su root setprop ctl.stop mtkbt
# put mtkbt back in place
echo "- restore mtkbk"
adb shell su root cp /system/bin/mtkbt_orig /system/bin/mtkbt
echo "- start Bluetooth daemon"
adb shell su root setprop ctl.start mtkbt
echo "DONE"

View File

@ -0,0 +1,176 @@
/*
* Copyright (C) 2009-2012 by Matthias Ringwald
*
* 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.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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 MATTHIAS
* RINGWALD 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.
*
* Please inquire about commercial licensing options at btstack@ringwald.ch
*
*/
/*
* hci_h4_transport.c
*
* HCI Transport API implementation for basic H4 protocol over POSIX
*
* Created by Matthias Ringwald on 4/29/09.
*/
#include "btstack-config.h"
#include <stdio.h>
#include "bluetoothdrv.h"
#include <string.h>
#include "debug.h"
#include "hci.h"
#include "hci_transport.h"
static int h4_process(struct data_source *ds);
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
typedef struct hci_transport_h4 {
hci_transport_t transport;
data_source_t *ds;
/* power management support, e.g. used by iOS */
timer_source_t sleep_timer;
} hci_transport_h4_t;
// single instance
static hci_transport_h4_t * hci_transport_h4 = NULL;
static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
// packet bufffers
static uint8_t hci_packet_out[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
static uint8_t hci_packet_in[1+HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
static int h4_open(void *transport_config){
int fd = mtk_bt_enable();
if (fd < 0) {
log_error("mtk_bt_enable failed");
return -1;
}
// set up data_source
hci_transport_h4->ds = (data_source_t*) malloc(sizeof(data_source_t));
if (!hci_transport_h4->ds) return -1;
hci_transport_h4->ds->fd = fd;
hci_transport_h4->ds->process = h4_process;
run_loop_add_data_source(hci_transport_h4->ds);
return 0;
}
static int h4_close(void *transport_config){
mtk_bt_disable(hci_transport_h4->ds->fd);
// first remove run loop handler
run_loop_remove_data_source(hci_transport_h4->ds);
// free struct
free(hci_transport_h4->ds);
hci_transport_h4->ds = NULL;
return 0;
}
static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
if (hci_transport_h4->ds == NULL) return -1;
// preapare packet
hci_packet_out[0] = packet_type;
memcpy(&hci_packet_out[1], packet, size);
// send
int res = mtk_bt_write(hci_transport_h4->ds->fd, hci_packet_out, size + 1);
return 0;
}
static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
packet_handler = handler;
}
static int h4_process(struct data_source *ds) {
if (hci_transport_h4->ds->fd == 0) return -1;
// read up to bytes_to_read data in
ssize_t bytes_read = mtk_bt_read(hci_transport_h4->ds->fd, &hci_packet_in[0], sizeof(hci_packet_in));
if (bytes_read == 0) return 0;
// iterate over packets
uint16_t pos = 0;
while (pos < bytes_read) {
uint16_t packet_len;
switch(hci_packet_in[pos]){
case HCI_EVENT_PACKET:
packet_len = hci_packet_in[pos+2] + 3;
break;
case HCI_ACL_DATA_PACKET:
packet_len = READ_BT_16(hci_packet_in, pos + 3) + 5;
break;
default:
log_error("h4_process: invalid packet type 0x%02x\n", hci_packet_in[pos]);
return;
}
packet_handler(hci_packet_in[pos], &hci_packet_in[pos+1], packet_len-1);
pos += packet_len;
}
return 0;
}
static const char * h4_get_transport_name(void){
return "H4 MTK";
}
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
}
// get h4 singleton
hci_transport_t * hci_transport_h4_instance() {
if (hci_transport_h4 == NULL) {
hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t));
hci_transport_h4->ds = NULL;
hci_transport_h4->transport.open = h4_open;
hci_transport_h4->transport.close = h4_close;
hci_transport_h4->transport.send_packet = h4_send_packet;
hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler;
hci_transport_h4->transport.get_transport_name = h4_get_transport_name;
hci_transport_h4->transport.set_baudrate = NULL;
hci_transport_h4->transport.can_send_packet_now = NULL;
}
return (hci_transport_t *) hci_transport_h4;
}

69
platforms/mtk/installer.sh Executable file
View File

@ -0,0 +1,69 @@
#!/bin/bash
export DIR=`dirname $0`
echo "BTstack Installer for RugGear/Mediatek devices"
echo "from: $DIR"
# make /system writable
if adb shell mount | grep -q "/system ext4 ro" ; then
echo "- remounting /system as read/write"
adb shell su root mount -o remount,rw /emmc@android /system
fi
if adb shell mount | grep -q "/system ext4 ro" ; then
echo "- remounting failed, abort"
fi
echo "- /system mounted as read/write"
if adb shell stat /system/bin/mtkbt | grep -q "regular file" ; then
echo "- backup mtkbt"
adb shell su root mv /system/bin/mtkbt /system/bin/mtkbt_orig
fi
echo "- stopping Bluetooth daemon"
adb shell su root setprop ctl.stop mtkbt
echo "- transfer files to device"
adb shell su root mkdir -p /system/btstack
adb shell su root chmod 777 /system/btstack
adb push $DIR/BTstackDaemon /system/btstack
adb push $DIR/BTstackDaemonRespawn /system/btstack
adb push $DIR/libBTstack.so /system/btstack
adb push $DIR/inquiry /system/btstack
adb push $DIR/le_scan /system/btstack
adb push $DIR/rfcomm-echo /system/btstack
echo "- put files in place"
adb shell su root mv /system/btstack/BTstackDaemon /system/bin
adb shell su root chmod 755 /system/bin/BTstackDaemon
adb shell su root mv /system/btstack/BTstackDaemonRespawn /system/bin
adb shell su root chmod 755 /system/bin/BTstackDaemonRespawn
adb shell su root touch /system/bin/mtkbt
adb shell su root rm /system/bin/mtkbt
adb shell su root ln -s /system/bin/BTstackDaemonRespawn /system/bin/mtkbt
adb shell su root mkdir -p /system/btstack
adb shell su root chmod 755 /system/btstack
adb shell su root chown bluetooth:bluetooth /system/btstack
adb shell su root mv /system/btstack/libBTstack.so /system/lib
adb shell su root chmod 755 /system/lib/libBTstack.so
adb shell su root mv /system/btstack/inquiry /system/bin
adb shell su root chmod 755 /system/bin/inquiry
adb shell su root mv /system/btstack/le_scan /system/bin
adb shell su root chmod 755 /system/bin/le_scan
adb shell su root mv /system/btstack/rfcomm-echo /system/bin
adb shell su root chmod 755 /system/bin/rfcomm-echo
adb shell su root rm -r /system/btstack
echo "- create /data/bstack for unix socket and log files"
adb shell su root mkdir /data/btstack
adb shell su root chown bluetooth:bluetooth /data/btstack
echo "- start BTstack daemon"
adb shell su root setprop ctl.start mtkbt
echo "DONE"

30
platforms/mtk/readme.txt Normal file
View File

@ -0,0 +1,30 @@
//
// BTstack port for RugGear Anrdroid 4.x devices with MediaTek Chipsets
//
It replaces MediaTek's Bluetooth Stack but doesn't integrate with the OS. BTstack libbtstack is required to communicate with the BTstack Server. It supports the LE Central and SPP.
Technical details:
- the binary for the Mediatek Bluetooth Server is replaced by the BTstackDaemonRespawn binary
- It links against MediaTeks's libbluetoothdrv.so that provides a standard HCI Transport implementation via POSIX API
Compilation:
It depends on the Android NDK. Please update NDK, ADB at the beginning of the Makefile
$ make
Install:
$ ./installer.sh
It will try to backup the original mtkbt Bluetooth server. Use at your own risk resp. double check the script first.
Deinstall:
$ ./deinstaller.sh
This tries to put mtkbk back in place.
Quick test:
$ adb shell le_scan
To start an LE discovery
Getting the packet log:
$ make hci_dump

7
platforms/mtk/restart.sh Executable file
View File

@ -0,0 +1,7 @@
echo "Restarting Bluetooth daemon"
echo "- stop Bluetooth daemon"
adb shell su root setprop ctl.stop mtkbt
echo "- start Bluetooth daemon"
adb shell su root setprop ctl.start mtkbt