1 /******************************************************************************
2  *
3  *  Copyright (C) 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_COMMAND = 1,
29   DATA_TYPE_ACL     = 2,
30   DATA_TYPE_SCO     = 3,
31   DATA_TYPE_EVENT   = 4
32 } serial_data_type_t;
33 
34 typedef void (*data_ready_cb)(serial_data_type_t type);
35 
36 typedef struct {
37   // Called when the HAL detects inbound data.
38   // Data |type| may be ACL, SCO, or EVENT.
39   // Executes in the context of the thread supplied to |init|.
40   data_ready_cb data_ready;
41 
42   /*
43   // Called when the HAL detects inbound astronauts named Dave.
44   // HAL will deny all requests to open the pod bay doors after this.
45   dave_ready_cb dave_ready;
46   */
47 } hci_hal_callbacks_t;
48 
49 typedef struct hci_hal_t {
50   // Initialize the HAL, with |upper_callbacks| and |upper_thread| to run in the context of.
51   bool (*init)(const hci_hal_callbacks_t *upper_callbacks, thread_t *upper_thread);
52 
53   // Connect to the underlying hardware, and let data start flowing.
54   bool (*open)(void);
55   // Disconnect from the underlying hardware, and close the HAL.
56   // "Daisy, Daisy..."
57   void (*close)(void);
58 
59   // Retrieve up to |max_size| bytes for ACL, SCO, or EVENT data packets into
60   // |buffer|. Only guaranteed to be correct in the context of a data_ready
61   // callback of the corresponding type.
62   size_t (*read_data)(serial_data_type_t type, uint8_t *buffer, size_t max_size);
63   // The upper layer must call this to notify the HAL that it has finished
64   // reading a packet of the specified |type|. Underlying implementations that
65   // use shared channels for multiple data types depend on this to know when
66   // to reinterpret the data stream.
67   void (*packet_finished)(serial_data_type_t type);
68   // Transmit COMMAND, ACL, or SCO data packets.
69   // |data| may not be NULL. |length| must be greater than zero.
70   //
71   // IMPORTANT NOTE:
72   // Depending on the underlying implementation, the byte right
73   // before the beginning of |data| may be borrowed during this call
74   // and then restored to its original value.
75   // This is safe in the bluetooth context, because there is always a buffer
76   // header that prefixes data you're sending.
77   uint16_t (*transmit_data)(serial_data_type_t type, uint8_t *data, uint16_t length);
78 } hci_hal_t;
79 
80 // Gets the correct hal implementation, as compiled for.
81 const hci_hal_t *hci_hal_get_interface(void);
82 
83 const hci_hal_t *hci_hal_h4_get_interface(void);
84 const hci_hal_t *hci_hal_h4_get_test_interface(vendor_t *vendor_interface);
85 
86 const hci_hal_t *hci_hal_mct_get_interface(void);
87 const hci_hal_t *hci_hal_mct_get_test_interface(vendor_t *vendor_interface);
88