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 "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|, blocking until max_size bytes read if |block| is true.
61   // Only guaranteed to be correct in the context of a data_ready callback
62   // of the corresponding type.
63   size_t (*read_data)(serial_data_type_t type, uint8_t *buffer, size_t max_size, bool block);
64   // The upper layer must call this to notify the HAL that it has finished
65   // reading a packet of the specified |type|. Underlying implementations that
66   // use shared channels for multiple data types depend on this to know when
67   // to reinterpret the data stream.
68   void (*packet_finished)(serial_data_type_t type);
69   // Transmit COMMAND, ACL, or SCO data packets.
70   // |data| may not be NULL. |length| must be greater than zero.
71   //
72   // IMPORTANT NOTE:
73   // Depending on the underlying implementation, the byte right
74   // before the beginning of |data| may be borrowed during this call
75   // and then restored to its original value.
76   // This is safe in the bluetooth context, because there is always a buffer
77   // header that prefixes data you're sending.
78   uint16_t (*transmit_data)(serial_data_type_t type, uint8_t *data, uint16_t length);
79 } hci_hal_t;
80 
81 // Gets the correct hal implementation, as compiled for.
82 const hci_hal_t *hci_hal_get_interface(void);
83 
84 const hci_hal_t *hci_hal_h4_get_interface(void);
85 const hci_hal_t *hci_hal_h4_get_test_interface(vendor_t *vendor_interface);
86 
87 const hci_hal_t *hci_hal_mct_get_interface(void);
88 const hci_hal_t *hci_hal_mct_get_test_interface(vendor_t *vendor_interface);
89