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