1 /****************************************************************************** 2 * 3 * Copyright 2014 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #pragma once 20 21 #include <stdbool.h> 22 #include <stdint.h> 23 24 #include "osi/include/thread.h" 25 #include "vendor.h" 26 27 typedef enum { 28 DATA_TYPE_UNKNOWN = 0, 29 DATA_TYPE_COMMAND = 1, 30 DATA_TYPE_ACL = 2, 31 DATA_TYPE_SCO = 3, 32 DATA_TYPE_EVENT = 4 33 } serial_data_type_t; 34 35 typedef void (*data_ready_cb)(serial_data_type_t type); 36 37 typedef struct { 38 // Called when the HAL detects inbound data. 39 // Data |type| may be ACL, SCO, or EVENT. 40 // Executes in the context of the thread supplied to |init|. 41 data_ready_cb data_ready; 42 43 /* 44 // Called when the HAL detects inbound astronauts named Dave. 45 // HAL will deny all requests to open the pod bay doors after this. 46 dave_ready_cb dave_ready; 47 */ 48 } hci_hal_callbacks_t; 49 50 typedef struct hci_hal_t { 51 // Initialize the HAL, with |upper_callbacks| and |upper_thread| to run in the 52 // context of. 53 bool (*init)(const hci_hal_callbacks_t* upper_callbacks, 54 thread_t* upper_thread); 55 56 // Connect to the underlying hardware, and let data start flowing. 57 bool (*open)(void); 58 // Disconnect from the underlying hardware, and close the HAL. 59 // "Daisy, Daisy..." 60 void (*close)(void); 61 62 // Retrieve up to |max_size| bytes for ACL, SCO, or EVENT data packets into 63 // |buffer|. Only guaranteed to be correct in the context of a data_ready 64 // callback of the corresponding type. 65 size_t (*read_data)(serial_data_type_t type, uint8_t* buffer, 66 size_t max_size); 67 // The upper layer must call this to notify the HAL that it has finished 68 // reading a packet of the specified |type|. Underlying implementations that 69 // use shared channels for multiple data types depend on this to know when 70 // to reinterpret the data stream. 71 void (*packet_finished)(serial_data_type_t type); 72 // Transmit COMMAND, ACL, or SCO data packets. 73 // |data| may not be NULL. |length| must be greater than zero. 74 // 75 // IMPORTANT NOTE: 76 // Depending on the underlying implementation, the byte right 77 // before the beginning of |data| may be borrowed during this call 78 // and then restored to its original value. 79 // This is safe in the bluetooth context, because there is always a buffer 80 // header that prefixes data you're sending. 81 uint16_t (*transmit_data)(serial_data_type_t type, uint8_t* data, 82 uint16_t length); 83 } hci_hal_t; 84 85 // Gets the correct hal implementation, as compiled for. 86 const hci_hal_t* hci_hal_get_interface(void); 87 88 const hci_hal_t* hci_hal_h4_get_interface(void); 89 const hci_hal_t* hci_hal_h4_get_test_interface(vendor_t* vendor_interface); 90 91 const hci_hal_t* hci_hal_mct_get_interface(void); 92 const hci_hal_t* hci_hal_mct_get_test_interface(vendor_t* vendor_interface); 93