provide CLI for L2CAP test

This commit is contained in:
matthias.ringwald@gmail.com 2014-01-24 10:34:30 +00:00
parent e86d389e33
commit 92de07636f
2 changed files with 69 additions and 41 deletions

View File

@ -11,7 +11,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
#include <termios.h>
#include <btstack/hci_cmds.h>
#include <btstack/run_loop.h>
@ -22,43 +24,14 @@
#include "hci_dump.h"
#include "l2cap.h"
void show_usage();
// static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
typedef enum {
W4_OPEN, SEND_ECHO, SEND_DATA, CLOSE_CHANNEL
} state_t;
static state_t state = W4_OPEN;
static uint16_t handle;
static uint16_t local_cid;
static void test_run(){
int result;
switch (state){
case SEND_ECHO:
// send echo request
result = l2cap_send_echo_request(handle, (uint8_t *) "Hello World!", 13);
printf("L2CAP ECHO Request Sent\n");
if (result) break;
state++;
break;
case SEND_DATA:
result = l2cap_send_internal(local_cid, (uint8_t *) "0123456789", 10);
if (result) break;
printf("L2CAP Data Sent\n");
state++;
break;
case CLOSE_CHANNEL:
l2cap_disconnect_internal(local_cid, 0);
printf("L2CAP Channel Closed\n");
state++;
break;
default:
break;
}
}
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t event_addr;
@ -70,7 +43,8 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
case BTSTACK_EVENT_STATE:
// bt stack activated, get started
if (packet[2] == HCI_STATE_WORKING){
l2cap_create_channel_internal(NULL, packet_handler, remote, PSM_SDP, 100);
printf("BTstack L2CAP Test Ready\n");
show_usage();
}
break;
case L2CAP_EVENT_CHANNEL_OPENED:
@ -85,14 +59,6 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
} else {
printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]);
}
// let's do our thing
state = SEND_ECHO;
break;
case DAEMON_EVENT_HCI_PACKET_SENT:
test_run();
break;
default:
@ -105,6 +71,7 @@ static void packet_handler2 (void * connection, uint8_t packet_type, uint16_t ch
}
static void btstack_setup(){
printf("Starting up..\n");
/// GET STARTED ///
btstack_memory_init();
run_loop_init(RUN_LOOP_POSIX);
@ -125,8 +92,69 @@ static void btstack_setup(){
hci_power_control(HCI_POWER_ON);
}
void show_usage(){
printf("\n--- CLI for L2CAP TEST ---\n");
printf("c - create connection to SDP at addr %s\n", bd_addr_to_str(remote));
printf("s - send data\n");
printf("e - send echo request\n");
printf("d - disconnect\n");
printf("Ctrl-c - exit\n");
printf("---\n");
}
int stdin_process(struct data_source *ds){
char buffer;
read(ds->fd, &buffer, 1);
switch (buffer){
case 'c':
printf("Creating L2CAP Connection to %s, PSM SDP\n", bd_addr_to_str(remote));
l2cap_create_channel_internal(NULL, packet_handler, remote, PSM_SDP, 100);
break;
case 's':
printf("Send L2CAP Data\n");
l2cap_send_internal(local_cid, (uint8_t *) "0123456789", 10);
break;
case 'e':
printf("Send L2CAP ECHO Request\n");
l2cap_send_echo_request(handle, (uint8_t *) "Hello World!", 13);
break;
case 'd':
printf("L2CAP Channel Closed\n");
l2cap_disconnect_internal(local_cid, 0);
break;
case '\n':
case '\r':
break;
default:
show_usage();
break;
}
return 0;
}
static data_source_t stdin_source;
void setup_cli(){
struct termios term = {0};
if (tcgetattr(0, &term) < 0)
perror("tcsetattr()");
term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO;
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &term) < 0)
perror("tcsetattr ICANON");
stdin_source.fd = 0; // stdin
stdin_source.process = &stdin_process;
run_loop_add_data_source(&stdin_source);
}
int main(void){
btstack_setup();
setup_cli();
run_loop_execute();
return 0;
}

Binary file not shown.