hci.c (c26f2c7ba71221a2122afaf0cfbfa38f1ce5c589) | hci.c (9cb529638dc3804924769e6ab74ce09dae0413a5) |
---|---|
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 --- 8230 unchanged lines hidden (view full) --- 8239 hci_stack->le_scan_filter_duplicates = enabled ? 1 : 0; 8240} 8241 8242void gap_set_scan_phys(uint8_t phys){ 8243 // LE Coded and LE 1M PHY 8244 hci_stack->le_scan_phys = phys & 0x05; 8245} 8246 | 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 --- 8230 unchanged lines hidden (view full) --- 8239 hci_stack->le_scan_filter_duplicates = enabled ? 1 : 0; 8240} 8241 8242void gap_set_scan_phys(uint8_t phys){ 8243 // LE Coded and LE 1M PHY 8244 hci_stack->le_scan_phys = phys & 0x05; 8245} 8246 |
8247uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 8248 hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 8249 if (!conn){ 8250 // disallow if le connection is already outgoing 8251 if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 8252 log_error("le connection already active"); 8253 return ERROR_CODE_COMMAND_DISALLOWED; 8254 } | 8247uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type) { 8248 // disallow le connection if outgoing already active 8249 if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 8250 log_error("le connect already active"); 8251 return ERROR_CODE_COMMAND_DISALLOWED; 8252 } |
8255 | 8253 |
8256 log_info("gap_connect: no connection exists yet, creating context"); | 8254 hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 8255 if (conn == NULL) { |
8257 conn = create_connection_for_bd_addr_and_type(addr, addr_type, HCI_ROLE_MASTER); | 8256 conn = create_connection_for_bd_addr_and_type(addr, addr_type, HCI_ROLE_MASTER); |
8258 if (!conn){ | 8257 if (conn == false){ 8258 // alloc failed |
8259 log_info("gap_connect: failed to alloc hci_connection_t"); | 8259 log_info("gap_connect: failed to alloc hci_connection_t"); |
8260 return BTSTACK_MEMORY_ALLOC_FAILED; // don't sent packet to controller | 8260 return BTSTACK_MEMORY_ALLOC_FAILED; |
8261 } | 8261 } |
8262 8263 // set le connecting state 8264 if (hci_is_le_connection_type(addr_type)){ 8265 hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; | 8262 } else { 8263 switch (conn->state) { 8264 case RECEIVED_DISCONNECTION_COMPLETE: 8265 // connection was just disconnected, reset state and allow re-connect 8266 conn->role = HCI_ROLE_MASTER; 8267 break; 8268 default: 8269 return ERROR_CODE_COMMAND_DISALLOWED; |
8266 } | 8270 } |
8267 8268 conn->state = SEND_CREATE_CONNECTION; 8269 log_info("gap_connect: send create connection next"); 8270 hci_run(); 8271 return ERROR_CODE_SUCCESS; | |
8272 } | 8271 } |
8273 8274 if (!hci_is_le_connection(conn) || 8275 (conn->state == SEND_CREATE_CONNECTION) || 8276 (conn->state == SENT_CREATE_CONNECTION)) { 8277 hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 8278 log_error("gap_connect: classic connection or connect is already being created"); 8279 return GATT_CLIENT_IN_WRONG_STATE; 8280 } | |
8281 | 8272 |
8282 // check if connection was just disconnected 8283 if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 8284 log_info("gap_connect: send create connection (again)"); 8285 conn->state = SEND_CREATE_CONNECTION; 8286 hci_run(); 8287 return ERROR_CODE_SUCCESS; | 8273 // set le connecting state 8274 if (hci_is_le_connection_type(addr_type)){ 8275 hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; |
8288 } 8289 | 8276 } 8277 |
8290 log_info("gap_connect: context exists with state %u", conn->state); 8291 hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); | 8278 // trigger connect 8279 log_info("gap_connect: send create connection next"); 8280 conn->state = SEND_CREATE_CONNECTION; |
8292 hci_run(); 8293 return ERROR_CODE_SUCCESS; 8294} 8295 8296// @assumption: only a single outgoing LE Connection exists 8297static hci_connection_t * gap_get_outgoing_connection(void){ 8298 btstack_linked_item_t *it; 8299 for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ --- 2207 unchanged lines hidden --- | 8281 hci_run(); 8282 return ERROR_CODE_SUCCESS; 8283} 8284 8285// @assumption: only a single outgoing LE Connection exists 8286static hci_connection_t * gap_get_outgoing_connection(void){ 8287 btstack_linked_item_t *it; 8288 for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ --- 2207 unchanged lines hidden --- |