From e139768c51d9370475578e9476cd5eff3b3d99f5 Mon Sep 17 00:00:00 2001 From: David Plotek Date: Sun, 7 Dec 2008 23:20:31 +0100 Subject: [PATCH] outgoing data packet was modified. Now I can send packet and receive it on second device. --- src2/Makefile.omk | 6 +++--- src2/bt_hw.c | 8 +++++++- src2/hciembeded.h | 1 + src2/testapp.c | 39 ++++++++++++++++++++++++++++----------- src2/tiny_bt_hci_cmd.c | 13 +++++++++++-- src2/tiny_bt_hci_cmd.h | 4 +++- src2/tiny_bt_hci_core.c | 37 +++++++++++++++++++++++++++++++------ src2/tiny_bt_hci_core.h | 3 ++- 8 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src2/Makefile.omk b/src2/Makefile.omk index 160ee0d..6e7e3b7 100644 --- a/src2/Makefile.omk +++ b/src2/Makefile.omk @@ -2,12 +2,12 @@ lib_LIBRARIES = tinybt tinybt_SOURCES = tiny_bt_hci_cmd.c tiny_bt_hci_core.c bt_hw.c -bin_PROGRAMS = tiny_bt tiny_bt_sl tiny_bt_con +bin_PROGRAMS = tiny_bt tiny_bt_con tiny_bt_SOURCES = testapp.c tiny_bt_LIBS = tinybt -tiny_bt_sl_SOURCES = testslave.c -tiny_bt_sl_LIBS = tinybt +#tiny_bt_sl_SOURCES = testslave.c +#tiny_bt_sl_LIBS = tinybt tiny_bt_con_SOURCES = testcon.c tiny_bt_con_LIBS = tinybt \ No newline at end of file diff --git a/src2/bt_hw.c b/src2/bt_hw.c index ca59dfd..af5c133 100644 --- a/src2/bt_hw.c +++ b/src2/bt_hw.c @@ -67,7 +67,13 @@ close_socket: int hw_bt_close_dev() { - return close(hw_dd); + while (close(hw_dd) < 0) { + perror("file closing interrupted\n"); + if (errno == EINTR || errno == EIO) + continue; + return -1; + } + return 0; } int hw_bt_write(__u8 *p_array, __u16 length) diff --git a/src2/hciembeded.h b/src2/hciembeded.h index af4cd23..915653c 100644 --- a/src2/hciembeded.h +++ b/src2/hciembeded.h @@ -205,6 +205,7 @@ enum { #define HCI_HOST_BUSY_PAIRING 0x38 /* ACL flags */ +#define ACL_PPP 0x00 #define ACL_CONT 0x01 #define ACL_START 0x02 #define ACL_ACTIVE_BCAST 0x04 diff --git a/src2/testapp.c b/src2/testapp.c index 2d44ade..7404cbd 100644 --- a/src2/testapp.c +++ b/src2/testapp.c @@ -10,6 +10,7 @@ * */ +#include #include #include #include @@ -51,6 +52,8 @@ void callback_app_inquiry_RSSI(bt_address *p_address) if (count == 0) { rem_bd_addr_array[0] = *p_address; + printf("There is first device:"); + printba(p_address); count++; } else { for (i = 0; i < count; i++) { @@ -93,55 +96,69 @@ void callback_app_inquiry_complete(void) printf("There is no remote device suitable for connect\n"); stop=1; } + /* + tiny_bt_finish(); + stop = 1; + printf("Application finished\n"); + */ } void callback_app_connection_complete(__u16 *p_handle) { - char ar[6] = {"hallo "}; - + char ar[11] = {"hallo karel"}; + char w2[15] = {"second messagee"}; + connection_state state = CONNECT; p_connection->con_state = state; printf("Connection complete handle:%d\n", *p_handle); - if (tiny_bt_send_data(ar, 6, p_handle) < 0) { + if (tiny_bt_send_data(ar, 11, p_handle, ACL_START, ACL_PPP) < 0) { printf("unable to send data\n"); } + if (tiny_bt_send_data(w2, 15, p_handle, ACL_CONT, ACL_PPP) < 0) { + printf("unable to send data\n"); + } + sleep(1); tiny_bt_disconnect(p_handle, callback_app_disconnect); } void callback_app_disconnect(__u16 *p_handle, __u8 *p_reason) { printf("disconnection handle %d was completed\n", *p_handle); + tiny_bt_finish(); stop = 1; } -int main(void) +int main(int argc, char* argv[]) { - int ii = 30000; + //int ii = 30000; - if (tiny_bt_init(0) < 0) { + if (tiny_bt_init(atoi(argv[1])) < 0) { perror("device impossible to ititialize\n"); - return -1; + tiny_bt_finish(); + exit(1); } printf("begin of main\n"); if (tiny_bt_read_bd_addr(&p_connection->master.bdaddr, callback_app_read_bd_addr) < 0) { perror("tiny_bt_read_bd_addr error\n"); - return -1; + tiny_bt_finish(); + exit(1); } while (1) { if (stop) - return -1; - //ii--; + break; if (tiny_bt_process() < 0) { perror("process error"); - return -1; + tiny_bt_finish(); + stop = 1; } } + printf("main finished\n"); return 0; } diff --git a/src2/tiny_bt_hci_cmd.c b/src2/tiny_bt_hci_cmd.c index ef11b3d..76bf09f 100644 --- a/src2/tiny_bt_hci_cmd.c +++ b/src2/tiny_bt_hci_cmd.c @@ -131,13 +131,15 @@ int send_hci_disconnect_cmd(__u16 handle) return 0; } -int send_hci_data(char *p_data, __u16 lenght, __u16 *p_chandle) +int send_hci_data(char *p_data, __u16 lenght, __u16 *p_chandle, __u16 PB_flag, __u16 B_flag) { hci_data_request dreq, *p_dreq = &dreq; p_dreq->p_data = p_data; p_dreq->lenght = lenght; p_dreq->p_chandle = p_chandle; + p_dreq->PB_flag = PB_flag; + p_dreq->B_flag = B_flag; if (send_data(p_dreq) < 0) { perror("hci_accept_conn_req_cmd wasn't sent\n"); @@ -177,12 +179,19 @@ int send_data(hci_data_request *p_dreq) { int ii; __u8 array[p_dreq->lenght + 4]; + __u16 handle_flags; + printf("old handle:%d\n", *p_dreq->p_chandle); + p_dreq->PB_flag = p_dreq->PB_flag << 12; + p_dreq->B_flag = p_dreq->B_flag << 12; + handle_flags = *p_dreq->p_chandle | p_dreq->PB_flag | p_dreq->B_flag; + printf("new hadle:%d\n", handle_flags); array[0] = HCI_ACLDATA_PKT; - memcpy(&array[1], p_dreq->p_chandle, 2); + memcpy(&array[1], &handle_flags, 2); memcpy(&array[3], &p_dreq->lenght, 2); memcpy(&array[5], p_dreq->p_data, p_dreq->lenght); + printf("Outgoing data:\n"); for (ii = 0; ii < sizeof(array); ii++) { printf(" %x",array[ii]); } diff --git a/src2/tiny_bt_hci_cmd.h b/src2/tiny_bt_hci_cmd.h index 95a3880..fb72d76 100644 --- a/src2/tiny_bt_hci_cmd.h +++ b/src2/tiny_bt_hci_cmd.h @@ -66,6 +66,8 @@ typedef struct{ char *p_data; __u16 lenght; __u16 *p_chandle; + __u16 PB_flag; + __u16 B_flag; } hci_data_request; @@ -83,7 +85,7 @@ extern int send_hci_read_local_name_cmd(void); extern int send_hci_create_connection_cmd(bt_address *p_dest_addr); extern int send_hci_accept_conn_req_cmd(bt_address *p_address); extern int send_hci_disconnect_cmd(__u16 handle); -extern int send_hci_data(char *p_data, __u16 lenght, __u16 *p_chandle); +extern int send_hci_data(char *p_data, __u16 lenght, __u16 *p_chandle, __u16 PB_flag, __u16 B_flag); extern int send_cmd(hci_cmd_request *p_creq); extern int send_data(hci_data_request *p_dreq); diff --git a/src2/tiny_bt_hci_core.c b/src2/tiny_bt_hci_core.c index 31abb0b..8511739 100644 --- a/src2/tiny_bt_hci_core.c +++ b/src2/tiny_bt_hci_core.c @@ -31,8 +31,10 @@ static void callback_hci_all_commands(void *p_arg, void *p_recbuf) if (p_rp->status) { perror("command status error\n"); + tiny_bt_finish(); //return -1; - exit(-1); // !!!!!!!!!!!!!!!! + /*TODO call some app_callback and do something with error state */ + exit(1); // !!!!!!!!!!!!!!!! } printf("command status with opcode: %d\n",p_rp->opcode); @@ -92,7 +94,7 @@ static void callback_hci_create_connection(void *p_arg, void *p_recbuf) connection_complete_data *p_data = p_arg; evt_conn_complete *p_rp; - p_rp = (void *) (p_recbuf + 3); + p_rp = (void *) (p_recbuf); memcpy(p_data->p_handle, &p_rp->handle, 2); //memcpy(p_data->p_address,&p_rp->bdaddr,6); p_data->callback_app_connection_complete(p_data->p_handle); @@ -226,6 +228,18 @@ int tiny_bt_init(__u8 dev_id) return 0; } +int tiny_bt_finish() +{ + int err; + + if ((err = hw_bt_close_dev()) < 0) { + perror("Impossible to close filedes\n"); + return -1; + } + printf("filedescriptor was closed\n"); + return 0; +} + int tiny_bt_process() { int len, j, index; @@ -265,7 +279,16 @@ int tiny_bt_process() evt_array[index].p_callback(evt_array[index].p_data, p_recbuf); if (evt_array[index].actual_status != PERMANENT) evt_array[index].actual_status = DONE; - + } + + } else if (p_hdr->evt == EVT_CONN_REQUEST) { + if ((index = look_up_inarray(EVT_CONN_REQUEST, 0)) < 0) { + err = -1; + goto no_evt_exit; + } else { + evt_array[index].p_callback(evt_array[index].p_data, p_recbuf); + if (evt_array[index].actual_status != PERMANENT) + evt_array[index].actual_status = DONE; } } else if (p_hdr->evt == EVT_MAX_SLOTS_CHANGE || p_hdr->evt == EVT_NUM_COMP_PKTS) { @@ -307,6 +330,8 @@ int tiny_bt_process() printf("%2.2X ", recbuf[j]); } printf("\n"); + /*TODO call some data callback for process data*/ + return 1; } else goto packet_wrong; } else @@ -325,7 +350,7 @@ packet_wrong: return 0; no_data: - printf("There is no data on socket"); + /*printf("There is no data on socket");*/ return 0; } @@ -563,9 +588,9 @@ int tiny_bt_disconnect(__u16 *p_handle, void(*callback_app_disconnect)(__u16 *p_ } -int tiny_bt_send_data(char *p_data, __u16 lenght, __u16 *p_chandle) +int tiny_bt_send_data(char *p_data, __u16 lenght, __u16 *p_chandle, __u16 PB_flag, __u16 B_flag) { - if (send_hci_data(p_data, lenght, p_chandle) < 0) { + if (send_hci_data(p_data, lenght, p_chandle, PB_flag, B_flag) < 0) { perror ("send_hci_cmd error\n"); return -1; } diff --git a/src2/tiny_bt_hci_core.h b/src2/tiny_bt_hci_core.h index b724c53..398588c 100644 --- a/src2/tiny_bt_hci_core.h +++ b/src2/tiny_bt_hci_core.h @@ -97,6 +97,7 @@ typedef struct{ extern void add_evt_toarray(expect_evt *p_evt); extern int look_up_inarray(__u8 evt_type, __u16 opcode); extern int tiny_bt_init(__u8 dev_id); +extern int tiny_bt_finish(); extern int tiny_bt_process(); extern int tiny_bt_read_bd_addr(bt_address *p_dest, void(*callback_app_read_bd_addr)(bt_address *p_address)); extern int tiny_bt_read_local_name(char *p_name, void(*callback_app_read_local_name)(char *p_name)); @@ -107,6 +108,6 @@ extern void tiny_bt_wait_for_connection(bt_address *p_dest_addr, void(*callback_ extern int tiny_bt_accept_connection(bt_address *p_dest_addr,__u16 *p_handle, void(*callback_app_connection_complete)(__u16 *p_handle)); extern void tiny_bt_disconnect_register(void(*callback_app_disconnect)(__u16 *p_handle, __u8 *p_reason)); extern int tiny_bt_disconnect(__u16 *p_handle, void(*callback_app_disconnect)(__u16 *p_handle, __u8 *p_reason)); -extern int tiny_bt_send_data(char *p_data, __u16 lenght, __u16 *p_chandle); +extern int tiny_bt_send_data(char *p_data, __u16 lenght, __u16 *p_chandle, __u16 PB_flag, __u16 B_flag); #endif -- 2.39.2