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 #ifndef _HCI_HAL_H_
20 #define _HCI_HAL_H_
21 
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include "osi/pkt_queue.h"
25 #include "stack/bt_types.h"
26 #if SOC_ESP_NIMBLE_CONTROLLER
27 #include "os/os_mbuf.h"
28 #endif
29 typedef enum {
30     DATA_TYPE_COMMAND = 1,
31     DATA_TYPE_ACL     = 2,
32     DATA_TYPE_SCO     = 3,
33     DATA_TYPE_EVENT   = 4
34 } serial_data_type_t;
35 
36 typedef void (*packet_ready_cb)(BT_HDR *packet);
37 typedef void (*adv_rpt_ready_cb)(pkt_linked_item_t *linked_pkt);
38 
39 typedef struct {
40     // Called when the HAL detects inbound data.
41     // Data |type| may be ACL, SCO, or EVENT.
42     // Executes in the context of the thread supplied to |init|.
43     packet_ready_cb packet_ready;
44     adv_rpt_ready_cb adv_rpt_ready;
45 
46     /*
47     // Called when the HAL detects inbound astronauts named Dave.
48     // HAL will deny all requests to open the pod bay doors after this.
49     dave_ready_cb dave_ready;
50     */
51 } hci_hal_callbacks_t;
52 
53 typedef struct hci_hal_t {
54     // Initialize the HAL, with |upper_callbacks| and |upper_thread| to run in the context of.
55     //bool (*init)(const hci_hal_callbacks_t *upper_callbacks);
56 
57     // Connect to the underlying hardware, and let data start flowing.
58     bool (*open)(const hci_hal_callbacks_t *upper_callbacks,  void *task_thread);
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|, blocking until max_size bytes read if |block| is true.
65     // Only guaranteed to be correct in the context of a data_ready callback
66     // of the corresponding type.
67     //size_t (*read_data)(serial_data_type_t type, uint8_t *buffer, 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, uint16_t length);
83 } hci_hal_t;
84 
85 
86 // Gets the correct hal implementation, as compiled for.
87 const hci_hal_t *hci_hal_h4_get_interface(void);
88 #if SOC_ESP_NIMBLE_CONTROLLER
89 int ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg);
90 
91 int ble_hs_rx_data(struct os_mbuf *om, void *arg);
92 #endif
93 
94 
95 #endif /* _HCI_HAL_H */
96