goep_client: refactor goep packet generation

This commit is contained in:
Matthias Ringwald 2018-08-30 11:14:36 +02:00
parent bedf64e313
commit eef3970d70

View File

@ -151,24 +151,25 @@ static void goep_client_handle_connection_close(goep_client_t * context){
static void goep_client_packet_handler_rfcomm(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
goep_client_t * context = goep_client;
switch (packet_type){
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
case RFCOMM_EVENT_CHANNEL_OPENED:
goep_client_handle_connection_opened(goep_client, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet));
return;
case RFCOMM_EVENT_CAN_SEND_NOW:
goep_client_emit_can_send_now_event(goep_client);
goep_client_emit_can_send_now_event(context);
break;
case RFCOMM_EVENT_CHANNEL_CLOSED:
goep_client_handle_connection_close(goep_client);
goep_client_handle_connection_close(context);
break;
default:
break;
}
break;
case RFCOMM_DATA_PACKET:
goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size);
context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size);
break;
default:
break;
@ -179,33 +180,43 @@ static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t
UNUSED(packet_type);
UNUSED(channel);
UNUSED(size);
goep_client_t * context = goep_client;
switch (packet[0]){
case SDP_EVENT_QUERY_RFCOMM_SERVICE:
goep_client->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
context->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
break;
case SDP_EVENT_QUERY_COMPLETE:
if (sdp_event_query_complete_get_status(packet)){
log_info("GOEP client, SDP query failed 0x%02x", sdp_event_query_complete_get_status(packet));
goep_client->state = GOEP_INIT;
context->state = GOEP_INIT;
goep_client_emit_connected_event(goep_client, sdp_event_query_complete_get_status(packet));
break;
}
if (goep_client->bearer_port == 0){
if (context->bearer_port == 0){
log_info("Remote GOEP RFCOMM Server Channel not found");
goep_client->state = GOEP_INIT;
context->state = GOEP_INIT;
goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
break;
}
log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->bearer_port);
rfcomm_create_channel(&goep_client_packet_handler_rfcomm, goep_client->bd_addr, goep_client->bearer_port, &goep_client->bearer_cid);
log_info("Remote GOEP RFCOMM Server Channel: %u", context->bearer_port);
rfcomm_create_channel(&goep_client_packet_handler_rfcomm, context->bd_addr, context->bearer_port, &context->bearer_cid);
break;
}
}
static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){
if (context->bearer_l2cap){
// TODO: implement l2cap variant
return NULL;
} else {
return rfcomm_get_outgoing_buffer();
}
}
static void goep_client_packet_append(const uint8_t * data, uint16_t len){
uint8_t * buffer = rfcomm_get_outgoing_buffer();
goep_client_t * context = goep_client;
uint8_t * buffer = goep_client_get_outgoing_buffer(context);
uint16_t pos = big_endian_read_16(buffer, 1);
memcpy(&buffer[pos], data, len);
pos += len;
@ -214,21 +225,27 @@ static void goep_client_packet_append(const uint8_t * data, uint16_t len){
static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){
UNUSED(goep_cid);
rfcomm_reserve_packet_buffer();
uint8_t * buffer = rfcomm_get_outgoing_buffer();
goep_client_t * context = goep_client;
if (context->bearer_l2cap){
// TODO: implement l2cap variant
} else {
rfcomm_reserve_packet_buffer();
}
uint8_t * buffer = goep_client_get_outgoing_buffer(context);
buffer[0] = opcode;
big_endian_store_16(buffer, 1, 3);
// store opcode for parsing of response
goep_client->obex_opcode = opcode;
context->obex_opcode = opcode;
}
static void goep_client_packet_add_connection_id(uint16_t goep_cid){
UNUSED(goep_cid);
goep_client_t * context = goep_client;
// add connection_id header if set, must be first header if used
if (goep_client->obex_connection_id != OBEX_CONNECTION_ID_INVALID){
if (context->obex_connection_id != OBEX_CONNECTION_ID_INVALID){
uint8_t header[5];
header[0] = OBEX_HEADER_CONNECTION_ID;
big_endian_store_32(header, 1, goep_client->obex_connection_id);
big_endian_store_32(header, 1, context->obex_connection_id);
goep_client_packet_append(&header[0], sizeof(header));
}
}
@ -241,44 +258,54 @@ void goep_client_init(void){
}
uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){
if (goep_client->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
goep_client->client_handler = handler;
goep_client->state = GOEP_W4_SDP;
memcpy(goep_client->bd_addr, addr, 6);
sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, goep_client->bd_addr, uuid);
*out_cid = goep_client->cid;
goep_client_t * context = goep_client;
if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED;
context->client_handler = handler;
context->state = GOEP_W4_SDP;
memcpy(context->bd_addr, addr, 6);
sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, context->bd_addr, uuid);
*out_cid = context->cid;
return 0;
}
uint8_t goep_client_disconnect(uint16_t goep_cid){
UNUSED(goep_cid);
rfcomm_disconnect(goep_client->bearer_cid);
goep_client_t * context = goep_client;
rfcomm_disconnect(context->bearer_cid);
return 0;
}
void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){
UNUSED(goep_cid);
goep_client->obex_connection_id = connection_id;
goep_client_t * context = goep_client;
context->obex_connection_id = connection_id;
}
uint8_t goep_client_get_request_opcode(uint16_t goep_cid){
UNUSED(goep_cid);
return goep_client->obex_opcode;
goep_client_t * context = goep_client;
return context->obex_opcode;
}
void goep_client_request_can_send_now(uint16_t goep_cid){
UNUSED(goep_cid);
rfcomm_request_can_send_now_event(goep_client->bearer_cid);
goep_client_t * context = goep_client;
if (context->bearer_l2cap){
// TODO: implement l2cap variant
} else {
rfcomm_request_can_send_now_event(context->bearer_cid);
}
}
void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){
UNUSED(goep_cid);
goep_client_t * context = goep_client;
goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT);
uint8_t fields[4];
fields[0] = obex_version_number;
fields[1] = flags;
// workaround: limit OBEX packet len to RFCOMM MTU to avoid handling of fragemented packets
maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu);
maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu);
big_endian_store_16(fields, 2, maximum_obex_packet_length);
goep_client_packet_append(&fields[0], sizeof(fields));
}
@ -318,8 +345,9 @@ void goep_client_add_header_application_parameters(uint16_t goep_cid, uint16_t l
void goep_client_add_header_name(uint16_t goep_cid, const char * name){
UNUSED(goep_cid);
goep_client_t * context = goep_client;
int len_incl_zero = strlen(name) + 1;
uint8_t * buffer = rfcomm_get_outgoing_buffer();
uint8_t * buffer = goep_client_get_outgoing_buffer(context);
uint16_t pos = big_endian_read_16(buffer, 1);
buffer[pos++] = OBEX_HEADER_NAME;
big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2);
@ -345,7 +373,13 @@ void goep_client_add_header_type(uint16_t goep_cid, const char * type){
int goep_client_execute(uint16_t goep_cid){
UNUSED(goep_cid);
uint8_t * buffer = rfcomm_get_outgoing_buffer();
goep_client_t * context = goep_client;
uint8_t * buffer = goep_client_get_outgoing_buffer(context);
uint16_t pos = big_endian_read_16(buffer, 1);
return rfcomm_send_prepared(goep_client->bearer_cid, pos);
if (context->bearer_l2cap){
// TODO: implement l2cap variant
return -1;
} else {
return rfcomm_send_prepared(context->bearer_cid, pos);
}
}