1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #ifdef __cplusplus
20 #include <cstddef>
21 #include <cstdint>
22 #else
23 #include <stddef.h>
24 #include <stdint.h>
25 #endif  // _cplusplus
26 
27 /* Define the header of each buffer used in the Bluetooth stack.
28  */
29 typedef struct {
30   uint16_t event;
31   uint16_t len;
32   uint16_t offset;
33   uint16_t layer_specific;
34   uint8_t data[];
35 } BT_HDR;
36 
37 typedef struct {
38   uint16_t event;
39   uint16_t len;
40   uint16_t offset;
41   uint16_t layer_specific;
42   // Note: Removal of flexible array member with no specified size.
43   // This struct may be embedded in any position within other structs
44   // and will not trigger various flexible member compilation issues.
45 } BT_HDR_RIGID;
46 
47 #ifdef __cplusplus
48 template <typename T>
49 T* ToPacketData(BT_HDR* bt_hdr, size_t offset = 0) {
50   return reinterpret_cast<T*>(bt_hdr->data + bt_hdr->offset + offset);
51 }
52 template <typename T>
53 const T* ToPacketData(const BT_HDR* bt_hdr, size_t offset = 0) {
54   return reinterpret_cast<const T*>(bt_hdr->data + bt_hdr->offset + offset);
55 }
56 #endif  // __cplusplus
57 
58 #define BT_HDR_SIZE (sizeof(BT_HDR))
59