bnep.c (9da9850b83fde69f1dc00ad3fb029803b8844b54) bnep.c (37e1b22048c5dc562e27aa1edc93fa4a6eda5cb4)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 97 unchanged lines hidden (view full) ---

106
107static gap_security_level_t bnep_security_level;
108
109static bnep_channel_t * bnep_channel_for_l2cap_cid(uint16_t l2cap_cid);
110static void bnep_channel_finalize(bnep_channel_t *channel);
111static void bnep_channel_start_timer(bnep_channel_t *channel, int timeout);
112inline static void bnep_channel_state_add(bnep_channel_t *channel, BNEP_CHANNEL_STATE_VAR event);
113static void bnep_handle_can_send_now(uint16_t cid);
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 97 unchanged lines hidden (view full) ---

106
107static gap_security_level_t bnep_security_level;
108
109static bnep_channel_t * bnep_channel_for_l2cap_cid(uint16_t l2cap_cid);
110static void bnep_channel_finalize(bnep_channel_t *channel);
111static void bnep_channel_start_timer(bnep_channel_t *channel, int timeout);
112inline static void bnep_channel_state_add(bnep_channel_t *channel, BNEP_CHANNEL_STATE_VAR event);
113static void bnep_handle_can_send_now(uint16_t cid);
114static void bnep_emit_open_channel_complete(bnep_channel_t *channel, uint8_t status)
114
115static void bnep_emit_open_channel_complete(bnep_channel_t *channel, uint8_t status, uint8_t setup_connection_response)
115{
116 log_info("BNEP_EVENT_CHANNEL_OPENED status 0x%02x bd_addr: %s, handler %p", status, bd_addr_to_str(channel->remote_addr), channel->packet_handler);
117 if (!channel->packet_handler) return;
118
116{
117 log_info("BNEP_EVENT_CHANNEL_OPENED status 0x%02x bd_addr: %s, handler %p", status, bd_addr_to_str(channel->remote_addr), channel->packet_handler);
118 if (!channel->packet_handler) return;
119
119 uint8_t event[3 + sizeof(bd_addr_t) + 4 * sizeof(uint16_t) + 2];
120 uint8_t event[3 + sizeof(bd_addr_t) + 4 * sizeof(uint16_t) + 3];
120 event[0] = BNEP_EVENT_CHANNEL_OPENED;
121 event[1] = sizeof(event) - 2;
122 event[2] = status;
123 little_endian_store_16(event, 3, channel->l2cap_cid);
124 little_endian_store_16(event, 5, channel->uuid_source);
125 little_endian_store_16(event, 7, channel->uuid_dest);
126 little_endian_store_16(event, 9, channel->max_frame_size);
127 reverse_bd_addr(channel->remote_addr, &event[11]);
128 little_endian_store_16(event, 17, channel->con_handle);
121 event[0] = BNEP_EVENT_CHANNEL_OPENED;
122 event[1] = sizeof(event) - 2;
123 event[2] = status;
124 little_endian_store_16(event, 3, channel->l2cap_cid);
125 little_endian_store_16(event, 5, channel->uuid_source);
126 little_endian_store_16(event, 7, channel->uuid_dest);
127 little_endian_store_16(event, 9, channel->max_frame_size);
128 reverse_bd_addr(channel->remote_addr, &event[11]);
129 little_endian_store_16(event, 17, channel->con_handle);
130 event[19] = setup_connection_response;
129 hci_dump_btstack_event( event, sizeof(event));
130 (*channel->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
131}
132
133static void bnep_emit_channel_timeout(bnep_channel_t *channel)
134{
135 log_info("BNEP_EVENT_CHANNEL_TIMEOUT bd_addr: %s, handler %p", bd_addr_to_str(channel->remote_addr), channel->packet_handler);
136 if (!channel->packet_handler) return;

--- 775 unchanged lines hidden (view full) ---

912 l2cap_request_can_send_now_event(channel->l2cap_cid);
913
914 /* Return the number of processed package bytes = BNEP Type, BNEP Control Type, UUID-Size + 2 * UUID */
915 return 1 + 1 + (2 * uuid_size);
916}
917
918static int bnep_handle_connection_response(bnep_channel_t *channel, uint8_t *packet, uint16_t size)
919{
131 hci_dump_btstack_event( event, sizeof(event));
132 (*channel->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
133}
134
135static void bnep_emit_channel_timeout(bnep_channel_t *channel)
136{
137 log_info("BNEP_EVENT_CHANNEL_TIMEOUT bd_addr: %s, handler %p", bd_addr_to_str(channel->remote_addr), channel->packet_handler);
138 if (!channel->packet_handler) return;

--- 775 unchanged lines hidden (view full) ---

914 l2cap_request_can_send_now_event(channel->l2cap_cid);
915
916 /* Return the number of processed package bytes = BNEP Type, BNEP Control Type, UUID-Size + 2 * UUID */
917 return 1 + 1 + (2 * uuid_size);
918}
919
920static int bnep_handle_connection_response(bnep_channel_t *channel, uint8_t *packet, uint16_t size)
921{
920 uint16_t response_code;
921
922 /* Sanity check packet size */
923 if (size < (1 + 2)) {
924 return 0;
925 }
926
927 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
928 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
929 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
930 return 1 + 2;
931 }
932
922
923 /* Sanity check packet size */
924 if (size < (1 + 2)) {
925 return 0;
926 }
927
928 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
929 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
930 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
931 return 1 + 2;
932 }
933
933 response_code = big_endian_read_16(packet, 1);
934 uint16_t response_code = big_endian_read_16(packet, 1);
934
935 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
936 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
937 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
938 /* Stop timeout timer! */
939 bnep_channel_stop_timer(channel);
935
936 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
937 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
938 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
939 /* Stop timeout timer! */
940 bnep_channel_stop_timer(channel);
940 bnep_emit_open_channel_complete(channel, 0);
941 bnep_emit_open_channel_complete(channel, ERROR_CODE_SUCCESS, response_code);
941 } else {
942 log_error("BNEP_CONNECTION_RESPONSE: Connection to %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
942 } else {
943 log_error("BNEP_CONNECTION_RESPONSE: Connection to %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
944 bnep_emit_open_channel_complete(channel, BNEP_SETUP_CONNECTION_ERROR, response_code);
943 bnep_channel_finalize(channel);
944 }
945 return 1 + 2;
946}
947
948static int bnep_can_handle_extensions(bnep_channel_t * channel){
949 /* Extension are primarily handled in CONNECTED state */
950 if (channel->state == BNEP_CHANNEL_STATE_CONNECTED) return 1;

--- 350 unchanged lines hidden (view full) ---

1301 if (!channel) {
1302 log_error("L2CAP_EVENT_CHANNEL_OPENED but no BNEP channel prepared");
1303 return 1;
1304 }
1305
1306 /* On L2CAP open error discard everything */
1307 if (status) {
1308 /* Emit bnep_open_channel_complete with status and free channel */
945 bnep_channel_finalize(channel);
946 }
947 return 1 + 2;
948}
949
950static int bnep_can_handle_extensions(bnep_channel_t * channel){
951 /* Extension are primarily handled in CONNECTED state */
952 if (channel->state == BNEP_CHANNEL_STATE_CONNECTED) return 1;

--- 350 unchanged lines hidden (view full) ---

1303 if (!channel) {
1304 log_error("L2CAP_EVENT_CHANNEL_OPENED but no BNEP channel prepared");
1305 return 1;
1306 }
1307
1308 /* On L2CAP open error discard everything */
1309 if (status) {
1310 /* Emit bnep_open_channel_complete with status and free channel */
1309 bnep_emit_open_channel_complete(channel, status);
1311 bnep_emit_open_channel_complete(channel, status, 0);
1310
1311 /* Free BNEP channel mempory */
1312 bnep_channel_free(channel);
1313 return 1;
1314 }
1315
1316 switch (channel->state){
1317 case BNEP_CHANNEL_STATE_CLOSED:

--- 226 unchanged lines hidden (view full) ---

1544 /* Stop timeout timer! */
1545 bnep_channel_stop_timer(channel);
1546 emit_connected = 1;
1547 }
1548
1549 bnep_channel_state_remove(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE);
1550 bnep_send_connection_response(channel, channel->response_code);
1551 if (emit_connected){
1312
1313 /* Free BNEP channel mempory */
1314 bnep_channel_free(channel);
1315 return 1;
1316 }
1317
1318 switch (channel->state){
1319 case BNEP_CHANNEL_STATE_CLOSED:

--- 226 unchanged lines hidden (view full) ---

1546 /* Stop timeout timer! */
1547 bnep_channel_stop_timer(channel);
1548 emit_connected = 1;
1549 }
1550
1551 bnep_channel_state_remove(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE);
1552 bnep_send_connection_response(channel, channel->response_code);
1553 if (emit_connected){
1552 bnep_emit_open_channel_complete(channel, 0);
1554 bnep_emit_open_channel_complete(channel, ERROR_CODE_SUCCESS, 0);
1553 }
1554 return;
1555 }
1556 if (channel->state_var & BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET) {
1557 bnep_channel_state_remove(channel, BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET);
1558 if ((channel->net_filter_out_count > 0) && (channel->net_filter_out != NULL)) {
1559 bnep_send_filter_net_type_set(channel, channel->net_filter_out, channel->net_filter_out_count);
1560 channel->net_filter_out_count = 0;

--- 156 unchanged lines hidden ---
1555 }
1556 return;
1557 }
1558 if (channel->state_var & BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET) {
1559 bnep_channel_state_remove(channel, BNEP_CHANNEL_STATE_VAR_SND_FILTER_NET_TYPE_SET);
1560 if ((channel->net_filter_out_count > 0) && (channel->net_filter_out != NULL)) {
1561 bnep_send_filter_net_type_set(channel, channel->net_filter_out, channel->net_filter_out_count);
1562 channel->net_filter_out_count = 0;

--- 156 unchanged lines hidden ---