2009-10-29 20:25:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-29 22:00:24 +00:00
|
|
|
/*
|
|
|
|
* hci_h4_transport.c
|
|
|
|
*
|
2009-05-19 21:49:12 +00:00
|
|
|
* HCI Transport API implementation for basic H4 protocol
|
2009-04-29 22:00:24 +00:00
|
|
|
*
|
2009-05-19 21:49:12 +00:00
|
|
|
* Created by Matthias Ringwald on 4/29/09.
|
2009-04-29 22:00:24 +00:00
|
|
|
*/
|
2009-05-08 21:37:54 +00:00
|
|
|
#include <termios.h> /* POSIX terminal control definitions */
|
|
|
|
#include <fcntl.h> /* File control definitions */
|
|
|
|
#include <unistd.h> /* UNIX standard function definitions */
|
|
|
|
#include <stdio.h>
|
2010-05-28 21:34:12 +00:00
|
|
|
#include <string.h>
|
2009-04-29 22:00:24 +00:00
|
|
|
|
2009-05-09 17:45:45 +00:00
|
|
|
#include "hci.h"
|
2010-06-04 18:09:01 +00:00
|
|
|
#include "hci_transport.h"
|
2009-05-26 22:50:30 +00:00
|
|
|
#include "hci_dump.h"
|
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
typedef enum {
|
|
|
|
H4_W4_PACKET_TYPE,
|
|
|
|
H4_W4_EVENT_HEADER,
|
|
|
|
H4_W4_EVENT_PAYLOAD,
|
|
|
|
H4_W4_ACL_HEADER,
|
|
|
|
H4_W4_ACL_PAYLOAD
|
|
|
|
} H4_STATE;
|
|
|
|
|
2009-07-09 18:49:43 +00:00
|
|
|
typedef struct hci_transport_h4 {
|
|
|
|
hci_transport_t transport;
|
|
|
|
data_source_t *ds;
|
|
|
|
} hci_transport_h4_t;
|
|
|
|
|
2009-06-06 19:52:36 +00:00
|
|
|
// single instance
|
2009-07-09 18:49:43 +00:00
|
|
|
static hci_transport_h4_t * hci_transport_h4 = NULL;
|
2009-06-06 19:52:36 +00:00
|
|
|
|
2009-08-09 13:00:09 +00:00
|
|
|
static int h4_process(struct data_source *ds);
|
2010-07-18 16:14:55 +00:00
|
|
|
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
|
2009-08-09 13:00:09 +00:00
|
|
|
static hci_uart_config_t *hci_uart_config;
|
2009-05-08 21:37:54 +00:00
|
|
|
|
2010-07-18 16:14:55 +00:00
|
|
|
static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
|
2009-05-10 20:23:56 +00:00
|
|
|
|
|
|
|
// packet reader state machine
|
2009-05-08 21:37:54 +00:00
|
|
|
static H4_STATE h4_state;
|
|
|
|
static int bytes_to_read;
|
|
|
|
static int read_pos;
|
2009-05-10 20:23:56 +00:00
|
|
|
// static uint8_t hci_event_buffer[255+2]; // maximal payload + 2 bytes header
|
2010-07-25 12:28:23 +00:00
|
|
|
static uint8_t hci_packet[HCI_ACL_3DH5_SIZE]; // bigger than largest packet
|
2009-05-08 21:37:54 +00:00
|
|
|
|
2010-05-28 21:34:12 +00:00
|
|
|
#ifdef TEST_LONG_INVALID_REMOTE_NAME
|
|
|
|
// test event with remote name of 255 bytes length
|
|
|
|
const uint8_t remoteNameEvent[] = { 0x07, 0xFF, 0x00, 0xC4, 0xC6, 0x65, 0x5A, 0x12, 0x00, 0x4D, 0x69,
|
|
|
|
0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, 0x74, 0xAE, 0x20, 0x57, 0x69, 0x72, 0x65, 0x6C, 0x65,
|
|
|
|
0x73, 0x73, 0x20, 0x4E, 0x6F, 0x74, 0x65, 0x62, 0x6F, 0x6F, 0x6B, 0x20, 0x50, 0x72, 0x65,
|
|
|
|
0x73, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x20, 0x4D, 0x6F, 0x75, 0x73, 0x65, 0x20, 0x38, 0x30,
|
|
|
|
0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
const int remoteNameEventSize = sizeof(remoteNameEvent);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-04-29 22:00:24 +00:00
|
|
|
// prototypes
|
2009-05-08 21:37:54 +00:00
|
|
|
static int h4_open(void *transport_config){
|
2009-05-10 20:23:56 +00:00
|
|
|
hci_uart_config = (hci_uart_config_t*) transport_config;
|
2009-05-08 21:37:54 +00:00
|
|
|
struct termios toptions;
|
2009-06-06 19:52:36 +00:00
|
|
|
int fd = open(hci_uart_config->device_name, O_RDWR | O_NOCTTY | O_NDELAY);
|
2009-05-08 21:37:54 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
perror("init_serialport: Unable to open port ");
|
2009-05-17 20:51:48 +00:00
|
|
|
perror(hci_uart_config->device_name);
|
2009-05-08 21:37:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcgetattr(fd, &toptions) < 0) {
|
|
|
|
perror("init_serialport: Couldn't get term attributes");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-10 20:23:56 +00:00
|
|
|
speed_t brate = hci_uart_config->baudrate; // let you override switch below if needed
|
|
|
|
switch(hci_uart_config->baudrate) {
|
2009-05-08 21:37:54 +00:00
|
|
|
case 57600: brate=B57600; break;
|
|
|
|
case 115200: brate=B115200; break;
|
2009-05-25 21:06:00 +00:00
|
|
|
#ifdef B230400
|
|
|
|
case 230400: brate=B230400; break;
|
|
|
|
#endif
|
|
|
|
#ifdef B460800
|
|
|
|
case 460800: brate=B460800; break;
|
|
|
|
#endif
|
|
|
|
#ifdef B921600
|
|
|
|
case 921600: brate=B921600; break;
|
|
|
|
#endif
|
2009-05-08 21:37:54 +00:00
|
|
|
}
|
|
|
|
cfsetispeed(&toptions, brate);
|
|
|
|
cfsetospeed(&toptions, brate);
|
|
|
|
|
|
|
|
// 8N1
|
|
|
|
toptions.c_cflag &= ~PARENB;
|
|
|
|
toptions.c_cflag &= ~CSTOPB;
|
|
|
|
toptions.c_cflag &= ~CSIZE;
|
|
|
|
toptions.c_cflag |= CS8;
|
|
|
|
|
2009-05-10 20:23:56 +00:00
|
|
|
if (hci_uart_config->flowcontrol) {
|
2009-05-08 21:37:54 +00:00
|
|
|
// with flow control
|
|
|
|
toptions.c_cflag |= CRTSCTS;
|
|
|
|
} else {
|
|
|
|
// no flow control
|
|
|
|
toptions.c_cflag &= ~CRTSCTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
|
|
|
|
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
|
|
|
|
|
|
|
|
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
|
|
|
|
toptions.c_oflag &= ~OPOST; // make raw
|
|
|
|
|
|
|
|
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
|
|
|
|
toptions.c_cc[VMIN] = 1;
|
|
|
|
toptions.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
|
|
|
|
perror("init_serialport: Couldn't set term attributes");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-06 19:52:36 +00:00
|
|
|
// set up data_source
|
2009-07-09 18:49:43 +00:00
|
|
|
hci_transport_h4->ds = malloc(sizeof(data_source_t));
|
|
|
|
if (!hci_transport_h4) return -1;
|
|
|
|
hci_transport_h4->ds->fd = fd;
|
|
|
|
hci_transport_h4->ds->process = h4_process;
|
2009-08-09 13:00:09 +00:00
|
|
|
run_loop_add_data_source(hci_transport_h4->ds);
|
2009-06-06 19:52:36 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
// init state machine
|
|
|
|
bytes_to_read = 1;
|
|
|
|
h4_state = H4_W4_PACKET_TYPE;
|
|
|
|
read_pos = 0;
|
2009-05-26 22:50:30 +00:00
|
|
|
|
2009-04-29 22:00:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
static int h4_close(){
|
2009-07-12 19:42:27 +00:00
|
|
|
// first remove run loop handler
|
2009-08-09 13:00:09 +00:00
|
|
|
run_loop_remove_data_source(hci_transport_h4->ds);
|
2009-07-12 19:42:27 +00:00
|
|
|
|
|
|
|
// close device
|
2009-07-09 18:49:43 +00:00
|
|
|
close(hci_transport_h4->ds->fd);
|
|
|
|
free(hci_transport_h4->ds);
|
2009-07-12 19:42:27 +00:00
|
|
|
|
|
|
|
// free struct
|
2009-07-09 18:49:43 +00:00
|
|
|
hci_transport_h4->ds = NULL;
|
2009-04-29 22:00:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-16 17:23:22 +00:00
|
|
|
static int h4_send_packet(uint8_t packet_type, uint8_t * packet, int size){
|
2009-07-09 18:49:43 +00:00
|
|
|
if (hci_transport_h4->ds == NULL) return -1;
|
|
|
|
if (hci_transport_h4->ds->fd == 0) return -1;
|
2009-05-26 22:50:30 +00:00
|
|
|
hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
|
2009-07-09 18:49:43 +00:00
|
|
|
write(hci_transport_h4->ds->fd, &packet_type, 1);
|
2009-05-10 20:23:56 +00:00
|
|
|
char *data = (char*) packet;
|
|
|
|
while (size > 0) {
|
2009-07-09 18:49:43 +00:00
|
|
|
int bytes_written = write(hci_transport_h4->ds->fd, data, size);
|
2009-05-10 20:23:56 +00:00
|
|
|
if (bytes_written < 0) {
|
|
|
|
return bytes_written;
|
|
|
|
}
|
|
|
|
data += bytes_written;
|
|
|
|
size -= bytes_written;
|
|
|
|
}
|
2009-04-29 22:00:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-18 16:14:55 +00:00
|
|
|
static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
|
|
|
|
packet_handler = handler;
|
2009-04-29 22:00:24 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 13:00:09 +00:00
|
|
|
static int h4_process(struct data_source *ds) {
|
2009-07-09 18:49:43 +00:00
|
|
|
if (hci_transport_h4->ds->fd == 0) return -1;
|
2009-06-06 19:52:36 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
// read up to bytes_to_read data in
|
2009-07-09 18:49:43 +00:00
|
|
|
int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
|
2009-05-08 21:37:54 +00:00
|
|
|
if (bytes_read < 0) {
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
bytes_to_read -= bytes_read;
|
|
|
|
read_pos += bytes_read;
|
|
|
|
if (bytes_to_read > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// act
|
|
|
|
switch (h4_state) {
|
2010-05-28 21:34:12 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
case H4_W4_PACKET_TYPE:
|
2009-05-10 20:23:56 +00:00
|
|
|
if (hci_packet[0] == HCI_EVENT_PACKET){
|
2009-05-08 21:37:54 +00:00
|
|
|
read_pos = 0;
|
2009-05-10 20:23:56 +00:00
|
|
|
bytes_to_read = HCI_EVENT_PKT_HDR;
|
2009-05-08 21:37:54 +00:00
|
|
|
h4_state = H4_W4_EVENT_HEADER;
|
2009-05-10 20:23:56 +00:00
|
|
|
} else if (hci_packet[0] == HCI_ACL_DATA_PACKET){
|
|
|
|
read_pos = 0;
|
|
|
|
bytes_to_read = HCI_ACL_DATA_PKT_HDR;
|
|
|
|
h4_state = H4_W4_ACL_HEADER;
|
|
|
|
} else {
|
2010-01-09 21:27:47 +00:00
|
|
|
fprintf(stderr, "h4_process: invalid packet type 0x%02x\n", hci_packet[0]);
|
2010-01-09 00:40:01 +00:00
|
|
|
read_pos = 0;
|
|
|
|
bytes_to_read = 1;
|
2009-05-08 21:37:54 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-05-28 21:34:12 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
case H4_W4_EVENT_HEADER:
|
2009-05-10 20:23:56 +00:00
|
|
|
bytes_to_read = hci_packet[1];
|
2009-05-08 21:37:54 +00:00
|
|
|
h4_state = H4_W4_EVENT_PAYLOAD;
|
|
|
|
break;
|
2010-05-28 21:34:12 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
case H4_W4_EVENT_PAYLOAD:
|
2010-05-28 21:34:12 +00:00
|
|
|
|
|
|
|
#ifdef TEST_LONG_INVALID_REMOTE_NAME
|
|
|
|
if (hci_packet[0] == HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE){
|
|
|
|
hci_packet[1] = remoteNameEvent[1];
|
|
|
|
memcpy( &hci_packet[9], &remoteNameEvent[9], remoteNameEventSize);
|
|
|
|
read_pos = remoteNameEventSize;
|
|
|
|
}
|
|
|
|
#endif
|
2009-06-08 21:27:23 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos);
|
2010-07-18 16:14:55 +00:00
|
|
|
packet_handler(HCI_EVENT_PACKET, hci_packet, read_pos);
|
2009-05-08 21:37:54 +00:00
|
|
|
h4_state = H4_W4_PACKET_TYPE;
|
|
|
|
read_pos = 0;
|
|
|
|
bytes_to_read = 1;
|
|
|
|
break;
|
2010-05-28 21:34:12 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
case H4_W4_ACL_HEADER:
|
2009-05-10 20:23:56 +00:00
|
|
|
bytes_to_read = READ_BT_16( hci_packet, 2);
|
|
|
|
h4_state = H4_W4_ACL_PAYLOAD;
|
2009-05-08 21:37:54 +00:00
|
|
|
break;
|
2010-05-28 21:34:12 +00:00
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
case H4_W4_ACL_PAYLOAD:
|
2009-05-26 22:50:30 +00:00
|
|
|
hci_dump_packet( HCI_ACL_DATA_PACKET, 1, hci_packet, read_pos);
|
2010-07-18 16:14:55 +00:00
|
|
|
packet_handler(HCI_ACL_DATA_PACKET, hci_packet, read_pos);
|
2009-05-10 20:23:56 +00:00
|
|
|
h4_state = H4_W4_PACKET_TYPE;
|
|
|
|
read_pos = 0;
|
|
|
|
bytes_to_read = 1;
|
2009-05-08 21:37:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-29 22:00:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-08 21:37:54 +00:00
|
|
|
static const char * h4_get_transport_name(){
|
2009-04-29 22:00:24 +00:00
|
|
|
return "H4";
|
|
|
|
}
|
|
|
|
|
2010-07-18 16:14:55 +00:00
|
|
|
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
|
2009-05-08 21:37:54 +00:00
|
|
|
}
|
2009-04-29 22:00:24 +00:00
|
|
|
|
2009-06-06 19:52:36 +00:00
|
|
|
// get h4 singleton
|
|
|
|
hci_transport_t * hci_transport_h4_instance() {
|
|
|
|
if (hci_transport_h4 == NULL) {
|
2009-07-09 18:49:43 +00:00
|
|
|
hci_transport_h4 = 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;
|
2010-08-16 17:23:22 +00:00
|
|
|
hci_transport_h4->transport.send_packet = h4_send_packet;
|
2010-07-18 16:14:55 +00:00
|
|
|
hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler;
|
2009-07-09 18:49:43 +00:00
|
|
|
hci_transport_h4->transport.get_transport_name = h4_get_transport_name;
|
2009-06-06 19:52:36 +00:00
|
|
|
}
|
2009-07-12 15:33:07 +00:00
|
|
|
return (hci_transport_t *) hci_transport_h4;
|
2009-06-06 19:52:36 +00:00
|
|
|
}
|