1 /*
2  * Copyright 2016-2023, Cypress Semiconductor Corporation or
3  * an affiliate of Cypress Semiconductor Corporation.  All rights reserved.
4  *
5  * This software, including source code, documentation and related
6  * materials ("Software") is owned by Cypress Semiconductor Corporation
7  * or one of its affiliates ("Cypress") and is protected by and subject to
8  * worldwide patent protection (United States and foreign),
9  * United States copyright laws and international treaty provisions.
10  * Therefore, you may use this Software only as provided in the license
11  * agreement accompanying the software package from which you
12  * obtained this Software ("EULA").
13  * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
14  * non-transferable license to copy, modify, and compile the Software
15  * source code solely for use in connection with Cypress's
16  * integrated circuit products.  Any reproduction, modification, translation,
17  * compilation, or representation of this Software except as specified
18  * above is prohibited without the express written permission of Cypress.
19  *
20  * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
23  * reserves the right to make changes to the Software without notice. Cypress
24  * does not assume any liability arising out of the application or use of the
25  * Software or any product or circuit described in the Software. Cypress does
26  * not authorize its products for use in any products where a malfunction or
27  * failure of the Cypress product may reasonably be expected to result in
28  * significant property damage, injury or death ("High Risk Product"). By
29  * including Cypress's product in a High Risk Product, the manufacturer
30  * of such system or application assumes all risk of such use and in doing
31  * so agrees to indemnify Cypress against all liability.
32  */
33 
34 /** @file
35  *
36  * Generic types
37  *
38  */
39 #pragma once
40 
41 #include "wiced_data_types.h"
42 #include "wiced_result.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48  /**
49  *  @addtogroup  bt_types  Wiced BT Types
50  *  @ingroup     gentypes
51  *
52  *  WICED BT Types.
53  *
54  *  @{
55  */
56 
57 #define BT_PSM_SDP                      0x0001 /**< Service Discovery Protocol) */
58 #define BT_PSM_RFCOMM                   0x0003 /**< RFCOMM with TS 07.10 */
59 #define BT_PSM_TCS                      0x0005 /**< (deprecated) */
60 #define BT_PSM_CTP                      0x0007 /**< (deprecated) */
61 #define BT_PSM_BNEP                     0x000F /**< Bluetooth Network Encapsulation Protocol */
62 #define BT_PSM_HIDC                     0x0011 /**< Human Interface Device (Control) */
63 #define BT_PSM_HIDI                     0x0013 /**< Human Interface Device (Interrupt) */
64 #define BT_PSM_UPNP                     0x0015 /**< Universal PlugNPlay */
65 #define BT_PSM_AVCTP                    0x0017 /**< Audio/Video Control Transport Protocol */
66 #define BT_PSM_AVDTP                    0x0019 /**< Audio/Video Distribution Transport Protocol */
67 #define BT_PSM_AVCTP_13                 0x001B /**< Advanced Control - Browsing */
68 #define BT_PSM_UDI_CP                   0x001D /**< Unrestricted Digital Information C-Plane  */
69 #define BT_PSM_ATT                      0x001F /**< Attribute Protocol  */
70 #define BT_PSM_3DS                      0x0021 /**< 3D sync */
71 #define BT_LE_PSM_IPSP                  0x0023 /**< Internet Protocol Support (IPv6/6LoWPAN) */
72 #define BT_LE_PSM_EATT                  0x0027 /**< Enhanced Attribute protocol */
73 
74 
75 #define BT_EIR_FLAGS_TYPE                   0x01    /**< EIR Flag Type */
76 #define BT_EIR_MORE_16BITS_UUID_TYPE        0x02    /**< EIR 16 bits UUID Type */
77 #define BT_EIR_COMPLETE_16BITS_UUID_TYPE    0x03    /**< EIR Complete 16 bits UUID Type */
78 #define BT_EIR_MORE_32BITS_UUID_TYPE        0x04    /**< EIR 32 bits UUID Type */
79 #define BT_EIR_COMPLETE_32BITS_UUID_TYPE    0x05    /**< EIR Complete 32 bits UUID Type */
80 #define BT_EIR_MORE_128BITS_UUID_TYPE       0x06    /**< EIR 128 bits UUID Type */
81 #define BT_EIR_COMPLETE_128BITS_UUID_TYPE   0x07    /**< EIR 128 bits UUID Type */
82 #define BT_EIR_SHORTENED_LOCAL_NAME_TYPE    0x08    /**< EIR Shorthand Local Name Type */
83 #define BT_EIR_COMPLETE_LOCAL_NAME_TYPE     0x09    /**< EIR Complete Local Name Type */
84 #define BT_EIR_TX_POWER_LEVEL_TYPE          0x0A    /**< EIR TX Power Level Type */
85 #define BT_EIR_OOB_BD_ADDR_TYPE             0x0C    /**< EIR OOB BDA Type */
86 #define BT_EIR_OOB_COD_TYPE                 0x0D    /**< EIR OOB COD Type */
87 #define BT_EIR_OOB_SSP_HASH_C_TYPE          0x0E    /**< EIR OOB SSP HASH C Type */
88 #define BT_EIR_OOB_SSP_RAND_R_TYPE          0x0F    /**< EIR OOB SSP RAND R Type */
89 #define BT_EIR_OOB_SSP_HASH_C_256_TYPE      0x1D    /**< EIR OOB SSP HASH C256 Type */
90 #define BT_EIR_OOB_SSP_RAND_R_256_TYPE      0x1E    /**< EIR OOB SSP RAND R256 Type */
91 #define BT_EIR_3D_SYNC_TYPE                 0x3D    /**< EIR 3D SYNC Type */
92 #define BT_EIR_MANUFACTURER_SPECIFIC_TYPE   0xFF    /**< EIR Manufacturer Specific Type*/
93 
94 #define BT_OOB_COD_SIZE            3    /**< OOB Class of Device Size */
95 #define BT_OOB_HASH_C_SIZE         16   /**< OOB Hash C size */
96 #define BT_OOB_RAND_R_SIZE         16   /**< OOB Rand R size */
97 #define BT_OOB_HASH_C_256_SIZE     16   /**< OOB Hash C-256 Size */
98 #define BT_OOB_RAND_R_256_SIZE     16   /**< OOB Rand R-256 Size */
99 
100 #ifndef WICED_BT_DEVICE_ADDRESS
101 #define WICED_BT_DEVICE_ADDRESS /**< Device Bluetooth Address */
102 #define BD_ADDR_LEN     6       /**< Device Bluetooth Address Length */
103 /** */
104 typedef uint8_t         wiced_bt_device_address_t[BD_ADDR_LEN]; /**< Device address length */
105 #endif
106 
107 /** Result/Status */
108 typedef wiced_result_t  wiced_bt_dev_status_t;
109 
110 /** Pointer to Device Address */
111 typedef uint8_t *BD_ADDR_PTR;
112 
113 #define BD_NAME_LEN     248     /**< Local Name Length */
114 
115 typedef uint8_t wiced_bt_remote_name_t[BD_NAME_LEN + 1];        /**< Device name */
116 
117 typedef uint8_t *wiced_bt_device_address_ptr_t;                 /**< Device address Pointer */
118 
119 #define DEV_CLASS_LEN   3  /**< Device class Length */
120 typedef uint8_t         wiced_bt_dev_class_t[DEV_CLASS_LEN];    /**< Device class */
121 
122 #define BD_FEATURES_LEN 8                                /**< Features length */
123 typedef uint8_t wiced_bt_features_t[BD_FEATURES_LEN]; /**< Features supported data structure */
124 
125 #define MAX_UUID_SIZE              16  /**< Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */
126 
127 #define BT_DB_HASH_LEN 16   /**< Database Hash length  */
128 typedef uint8_t wiced_bt_db_hash_t[BT_DB_HASH_LEN]; /**< BLE database hash */
129 
130 #define WICED_BT_GATT_CLIENT_SUPPORTED_FEATURE_OCTET_LEN 1  /**< GATT Client Supported feature length */
131 /** GATT Client Support features */
132 typedef uint8_t wiced_bt_gatt_client_supported_features_t[WICED_BT_GATT_CLIENT_SUPPORTED_FEATURE_OCTET_LEN];
133 
134 #define WICED_BT_GATT_SERVER_SUPPORTED_FEATURE_OCTET_LEN 1 /**< GATT Server Supported feature length */
135 /** GATT Server Support features */
136 typedef uint8_t wiced_bt_gatt_server_supported_features_t[WICED_BT_GATT_SERVER_SUPPORTED_FEATURE_OCTET_LEN];
137 
138 /** UUID Type */
139 typedef struct
140 {
141 #define LEN_UUID_16     2   /**< 2 Byte UUID */
142 #define LEN_UUID_32     4   /**< 4 Byte UUID */
143 #define LEN_UUID_128    16  /**< 16 Byte UUID */
144 
145     uint8_t        len;     /**< UUID length */
146 
147     /** UUID Data */
148     union
149     {
150         uint16_t    uuid16; /**< 16-bit UUID */
151         uint32_t    uuid32; /**< 32-bit UUID */
152         uint8_t     uuid128[MAX_UUID_SIZE]; /**< 128-bit UUID */
153     } uu;
154 
155 } wiced_bt_uuid_t;
156 
157 #define BT_OCTET8_LEN    8      /**< octet length: 8 */
158 typedef uint8_t BT_OCTET8[BT_OCTET8_LEN];   /**< octet array: size 8 */
159 
160 #define BT_OCTET16_LEN    16                /**< octet length: 16 */
161 typedef uint8_t BT_OCTET16[BT_OCTET16_LEN]; /**< octet array: size 16 */
162 
163 #define BT_OCTET32_LEN    32    /**< octet length: 32 */
164 typedef uint8_t BT_OCTET32[BT_OCTET32_LEN];   /**< octet array: size 32 */
165 
166 /** Bluetooth QoS defintions */
167 typedef struct {
168     uint8_t         qos_flags;              /**< Quality of service flag */
169     uint8_t         service_type;           /**< service type (NO_TRAFFIC, BEST_EFFORT, or GUARANTEED) */
170     uint32_t        token_rate;             /**< token rate (bytes/second) */
171     uint32_t        token_bucket_size;      /**< token bucket size (bytes) */
172     uint32_t        peak_bandwidth;         /**< peak bandwidth (bytes/second) */
173     uint32_t        latency;                /**< latency (microseconds) */
174     uint32_t        delay_variation;        /**< delay variation (microseconds) */
175 } wiced_bt_flow_spec_t;
176 
177 /* Values for swiced_bt_flow_spec_t service_type */
178 #define NO_TRAFFIC      0   /**< No Traffic */
179 #define BEST_EFFORT     1   /**< Best Effort */
180 #define GUARANTEED      2   /**< Guaranteed */
181 
182 /**
183  * @anchor WICED_BT_TRANSPORT_TYPE
184  * @name Transport types
185  * @{
186  */
187 #define BT_TRANSPORT_BR_EDR         1       /**< BR/EDR transport */
188 #define BT_TRANSPORT_LE             2       /**< BLE transport */
189 typedef uint8_t wiced_bt_transport_t;       /**< Transport type (see @ref WICED_BT_TRANSPORT_TYPE "BT Transport Types") */
190 /** @} WICED_BT_TRANSPORT_TYPE */
191 
192 /**
193  * @anchor WICED_BT_DEVICE_TYPE
194  * @name Device Types
195  * @{
196  */
197 #define BT_DEVICE_TYPE_BREDR        0x01    /**< BR/EDR device */
198 #define BT_DEVICE_TYPE_BLE          0x02    /**< LE device */
199 #define BT_DEVICE_TYPE_BREDR_BLE    0x03    /**< Dual Mode device */
200 typedef uint8_t wiced_bt_device_type_t;     /**< Bluetooth device type (see @ref WICED_BT_DEVICE_TYPE "BT Device Types") */
201 /** @} WICED_BT_DEVICE_TYPE */
202 
203 /**
204  * @anchor WICED_BT_ADDR_TYPE
205  * @name Address Types
206  * @{
207  */
208 #define BLE_ADDR_PUBLIC             0x00        /**< Public address */
209 #define BLE_ADDR_RANDOM             0x01        /**< Random address */
210 #define BLE_ADDR_PUBLIC_ID          0x02        /**< Public ID      */
211 #define BLE_ADDR_RANDOM_ID          0x03        /**< Random ID      */
212 
213 /** BLE device address type (see @ref WICED_BT_ADDR_TYPE "BT Address Types") */
214 typedef uint8_t wiced_bt_ble_address_type_t;
215 
216 /** @} WICED_BT_ADDR_TYPE */
217 
218 /** Wiced Ble Address structure */
219 typedef struct
220 {
221     wiced_bt_ble_address_type_t type;   /**< BLE Address Type */
222     wiced_bt_device_address_t   bda;    /**< BLE Address */
223 } wiced_bt_ble_address_t;
224 
225 #define LINK_KEY_LEN    16      /**< Link Key Len */
226 typedef uint8_t wiced_bt_link_key_t[LINK_KEY_LEN];  /**< Link Key */
227 
228 #define DRB_OVERHEAD_SIZE   (sizeof (uint16_t) + sizeof (uint16_t))     /**< length and offset */
229 
230 /**
231  * Data Received Buffer (DRB) used to receive data from the peer.
232  * The size of the DRB allocated is \ref DRB_OVERHEAD_SIZE + size of MTU (or the maximum data to be received)
233  * from the peer
234 */
235 typedef struct
236 {
237     uint16_t            drb_data_len;      /**< The amount of data in the DRB              */
238     uint16_t            drb_data_offset;   /**< The starting offset of the data in the DRB */
239     uint8_t drb_data[1];                   /**< The actual received data starts here, the next bytes received
240                                             * continue, upto a max of MTU size configured
241                                             */
242 } tDRB;
243 
244 /** ECRB: Enhanced Credit Based Connection types */
245 #define L2CAP_ECRB_MAX_CHANNELS_PER_CMD 5 /**< ECRB Max Channels per Command */
246 #define EATT_CHANNELS_PER_TRANSACTION   L2CAP_ECRB_MAX_CHANNELS_PER_CMD /**< EATT Max Channel per TX */
247 
248 /**
249  * list of ECRB lcid's
250  *
251  * LCID: L2CAP Channel ID
252  * L2CAP channel ids are assigned by L2CAP during the channel creation process.
253  * The list is passed to the application using #wiced_bt_gatt_eatt_connection_indication_event_t
254  */
255 typedef uint16_t wiced_bt_ecrb_cid_list_t[L2CAP_ECRB_MAX_CHANNELS_PER_CMD];
256 typedef uint16_t wiced_bt_gatt_eatt_conn_id_list[L2CAP_ECRB_MAX_CHANNELS_PER_CMD]; /**< list of EATT connection id's */
257 
258 /********************************************************************************
259 ** Macros to get and put bytes to and from a stream (Little Endian format).
260 */
261 /** Covert UINT128 to Stream array */
262 #define UINT128_TO_STREAM(p, u128) {*(p)++ = (uint8_t)(u128);       *(p)++ = (uint8_t)((u128) >> 8);*(p)++ = (uint8_t)((u128) >> 16); *(p)++ = (uint8_t)((u128) >> 24); \
263                                     *(p)++ = (uint8_t)((u128) >> 32); *(p)++ = (uint8_t)((u128) >> 40);*(p)++ = (uint8_t)((u128) >> 48); *(p)++ = (uint8_t)((u128) >> 56); \
264                                     *(p)++ = (uint8_t)((u128) >> 64); *(p)++ = (uint8_t)((u128) >> 72);*(p)++ = (uint8_t)((u128) >> 80); *(p)++ = (uint8_t)((u128) >> 88); \
265                                     *(p)++ = (uint8_t)((u128) >> 96); *(p)++ = (uint8_t)((u128) >> 104);*(p)++ = (uint8_t)((u128) >> 112); *(p)++ = (uint8_t)((u128) >> 120); }
266 /** Covert uint64_t to Stream array */
267 #define UINT64_TO_STREAM(p, u64) {*(p)++ = (uint8_t)(u64);       *(p)++ = (uint8_t)((u64) >> 8);*(p)++ = (uint8_t)((u64) >> 16); *(p)++ = (uint8_t)((u64) >> 24); \
268                                     *(p)++ = (uint8_t)((u64) >> 32); *(p)++ = (uint8_t)((u64) >> 40);*(p)++ = (uint8_t)((u64) >> 48); *(p)++ = (uint8_t)((u64) >> 56);}
269 /** Covert UINT48 to Stream array */
270 #define UINT48_TO_STREAM(p, u48) {*(p)++ = (uint8_t)(u48); *(p)++ = (uint8_t)((u48) >> 8); *(p)++ = (uint8_t)((u48) >> 16); *(p)++ = (uint8_t)((u48) >> 24); *(p)++ = (uint8_t)((u48) >> 32); *(p)++ = (uint8_t)((u48) >> 40);}
271 /** Covert UINT40 to Stream array */
272 #define UINT40_TO_STREAM(p, u40) {*(p)++ = (uint8_t)(u40); *(p)++ = (uint8_t)((u40) >> 8); *(p)++ = (uint8_t)((u40) >> 16); *(p)++ = (uint8_t)((u40) >> 24); \
273                                   *(p)++ = (uint8_t)((u40) >> 32);}
274 /** Covert UINT24 to Stream array */
275 #define UINT24_TO_STREAM(p, u24) {*(p)++ = (uint8_t)(u24); *(p)++ = (uint8_t)((u24) >> 8); *(p)++ = (uint8_t)((u24) >> 16);}
276 /** Covert uint8_t to Stream array */
277 #define UINT8_TO_STREAM(p, u8)   {*(p)++ = (uint8_t)(u8);}
278 /** Covert int8_t to Stream array */
279 #define INT8_TO_STREAM(p, u8)    {*(p)++ = (int8_t)(u8);}
280 /** Covert 32 bytes to Stream array */
281 #define ARRAY32_TO_STREAM(p, a)  {register unsigned int ijk; for (ijk = 0; ijk < 32;           ijk++) *(p)++ = (uint8_t) a[31 - ijk];}
282 /** Covert 16 bytes to Stream array */
283 #define ARRAY16_TO_STREAM(p, a)  {register unsigned int ijk; for (ijk = 0; ijk < 16;           ijk++) *(p)++ = (uint8_t) a[15 - ijk];}
284 /** Covert 8 bytes to Stream array */
285 #define ARRAY8_TO_STREAM(p, a)   {register unsigned int ijk; for (ijk = 0; ijk < 8;            ijk++) *(p)++ = (uint8_t) a[7 - ijk];}
286 /** Covert LAP to Stream array */
287 #define LAP_TO_STREAM(p, a)      {register unsigned int ijk; for (ijk = 0; ijk < LAP_LEN;      ijk++) *(p)++ = (uint8_t) a[LAP_LEN - 1 - ijk];}
288 /** Covert Device class to Stream array */
289 #define DEVCLASS_TO_STREAM(p, a) {register unsigned int ijk; for (ijk = 0; ijk < DEV_CLASS_LEN;ijk++) *(p)++ = (uint8_t) a[DEV_CLASS_LEN - 1 - ijk];}
290 /** Covert array to Stream array */
291 #ifndef ARRAY_TO_STREAM
292 #define ARRAY_TO_STREAM(p, a, len) {register unsigned int ijk; for (ijk = 0; ijk < len;        ijk++) *(p)++ = (uint8_t) ((uint8_t *)a)[ijk];}
293 #endif
294 /** Reverse Array */
295 #define REVERSE_ARRAY_TO_STREAM(p, a, len)  {register unsigned int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[len - 1 - ijk];}
296 /** Convert byte stream to UINT24 */
297 #define STREAM_TO_UINT24(u32, p) {u32 = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + ((((uint32_t)(*((p) + 2)))) << 16) ); (p) += 3;}
298 /** Convert byte stream to UINT40 */
299 #define STREAM_TO_UINT40(u40, p) {u40 = (((uint64_t)(*(p))) + ((((uint64_t)(*((p) + 1)))) << 8) + ((((uint64_t)(*((p) + 2)))) << 16) + ((((uint64_t)(*((p) + 3)))) << 24) + ((((uint64_t)(*((p) + 4)))) << 32)); (p) += 5;}
300 /** Convert byte stream to ARRAY32 */
301 #define STREAM_TO_ARRAY32(a, p)  {register unsigned int ijk; register uint8_t *_pa = (uint8_t *)a + 31; for (ijk = 0; ijk < 32; ijk++) *_pa-- = *p++;}
302 /** Convert byte stream to ARRAY16 */
303 #define STREAM_TO_ARRAY16(a, p)  {register unsigned int ijk; register uint8_t *_pa = (uint8_t *)a + 15; for (ijk = 0; ijk < 16; ijk++) *_pa-- = *p++;}
304 /** Convert byte stream to ARRAY8 */
305 #define STREAM_TO_ARRAY8(a, p)   {register unsigned int ijk; register uint8_t *_pa = (uint8_t *)a + 7; for (ijk = 0; ijk < 8; ijk++) *_pa-- = *p++;}
306 /** Convert byte stream to Device Class */
307 #define STREAM_TO_DEVCLASS(a, p) {register unsigned int ijk; register uint8_t *_pa = (uint8_t *)a + DEV_CLASS_LEN - 1; for (ijk = 0; ijk < DEV_CLASS_LEN; ijk++) *_pa-- = *p++;}
308 /** Convert byte stream to LAP */
309 #define STREAM_TO_LAP(a, p)      {register unsigned int ijk; register uint8_t *plap = (uint8_t *)a + LAP_LEN - 1; for (ijk = 0; ijk < LAP_LEN; ijk++) *plap-- = *p++;}
310 /** Convert byte stream to Array */
311 #define STREAM_TO_ARRAY(a, p, len) {register unsigned int ijk; for (ijk = 0; ijk < len; ijk++) ((uint8_t *) a)[ijk] = *p++;}
312 /** Reverse Stream to Array */
313 #define REVERSE_STREAM_TO_ARRAY(a, p, len) {register unsigned int ijk; register uint8_t *_pa = (uint8_t *)a + len - 1; for (ijk = 0; ijk < len; ijk++) *_pa-- = *p++;}
314 
315 /** Convert byte stream to uint8_t */
316 #define STREAM_TO_UINT8(u8, p)   {u8 = (uint8_t)(*(p)); (p) += 1;}
317 /** Convert byte stream to int8_t */
318 #define STREAM_TO_INT8(i8, p)   {i8 = (int8_t)(*(p)); (p) += 1;}
319 /** String copy */
320 #define BCM_STRNCPY_S(x1,x2,x3,x4)  strncpy((x1),(x3),(x4))
321 
322 /* Based on the BT Controller ARM architecture, or possibly other hosts, we can optimize these macros. */
323 #if ((defined STACK_INSIDE_BT_CTRLR) && (STACK_INSIDE_BT_CTRLR == TRUE)) || (defined OPTIMISE_FOR_LITTLE_ENDIAN)
324 /** Covert uint32_t to Stream */
325 #define UINT32_TO_STREAM(p, u32) {*(uint32_t *)(p) = u32; (p) += 4;}
326 /** Covert uint16_t to Stream */
327 #define UINT16_TO_STREAM(p, u16) {*(uint16_t *)(p) = u16; (p) += 2;}
328 /** Covert Stream to uint32_t */
329 #define STREAM_TO_UINT32(u32, p) {u32 = *(uint32_t *)(p); (p) += 4;}
330 /** Covert Stream to uint16_t */
331 #define STREAM_TO_UINT16(u16, p) {u16 = *(uint16_t *)(p); (p) += 2;}
332 #else
333 /** Covert uint32_t to Stream */
334 #define UINT32_TO_STREAM(p, u32) {*(p)++ = (uint8_t)(u32); *(p)++ = (uint8_t)((u32) >> 8); *(p)++ = (uint8_t)((u32) >> 16); *(p)++ = (uint8_t)((u32) >> 24);}
335 /** Covert uint16_t to Stream */
336 #define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);}
337 /** Covert Stream to uint32_t */
338 #define STREAM_TO_UINT32(u32, p) {u32 = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + ((((uint32_t)(*((p) + 2)))) << 16) + ((((uint32_t)(*((p) + 3)))) << 24)); (p) += 4;}
339 /** Covert Stream to uint16_t */
340 #define STREAM_TO_UINT16(u16, p) {u16 = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); (p) += 2;}
341 #endif
342 /** Copy BDA to stream */
343 extern uint8_t *BTU_copyBdaToStream(uint8_t *pStream, uint8_t *pBDA);
344 /** Copy Stream to BDA */
345 extern uint8_t *BTU_copyStreamToBda(uint8_t *pBDA, uint8_t *pStream);
346 
347 /** Convert BDA to stream */
348 #define BDADDR_TO_STREAM(p, a)   { p = BTU_copyBdaToStream(p, (uint8_t *)(a)); }
349 /** Convert stream to BDA */
350 #define STREAM_TO_BDADDR(a, p)   { p = BTU_copyStreamToBda((uint8_t *)(a), p);}
351 
352 
353 /********************************************************************************
354 ** Macros to get and put bytes to and from a field (Little Endian format).
355 ** These are the same as to stream, except the pointer is not incremented.
356 */
357 /** Covert uint32_t to Field*/
358 #define UINT32_TO_FIELD(p, u32) {*(uint8_t *)(p) = (uint8_t)(u32); *((uint8_t *)(p)+1) = (uint8_t)((u32) >> 8); *((uint8_t *)(p)+2) = (uint8_t)((u32) >> 16); *((uint8_t *)(p)+3) = (uint8_t)((u32) >> 24);}
359 /** Covert UINT24 to Field*/
360 #define UINT24_TO_FIELD(p, u24) {*(uint8_t *)(p) = (uint8_t)(u24); *((uint8_t *)(p)+1) = (uint8_t)((u24) >> 8); *((uint8_t *)(p)+2) = (uint8_t)((u24) >> 16);}
361 /** Covert uint16_t to Field*/
362 #define UINT16_TO_FIELD(p, u16) {*(uint8_t *)(p) = (uint8_t)(u16); *((uint8_t *)(p)+1) = (uint8_t)((u16) >> 8);}
363 /** Covert uint8_t to Field*/
364 #define UINT8_TO_FIELD(p, u8)   {*(uint8_t *)(p) = (uint8_t)(u8);}
365 
366 
367 /********************************************************************************
368 ** Macros to get and put bytes to and from a stream (Big Endian format)
369 */
370 /** Covert UINT128 to Stream */
371 #define UINT128_TO_BE_STREAM(p, u128) {*(p)++ = (uint8_t)(u128)>> 120;       *(p)++ = (uint8_t)((u128) >> 112);*(p)++ = (uint8_t)((u128) >> 104); *(p)++ = (uint8_t)((u128) >> 96); \
372                                     *(p)++ = (uint8_t)((u128) >> 88); *(p)++ = (uint8_t)((u128) >> 80);*(p)++ = (uint8_t)((u128) >> 72); *(p)++ = (uint8_t)((u128) >> 64); \
373                                     *(p)++ = (uint8_t)((u128) >> 56); *(p)++ = (uint8_t)((u128) >> 48);*(p)++ = (uint8_t)((u128) >> 40); *(p)++ = (uint8_t)((u128) >> 32); \
374                                     *(p)++ = (uint8_t)((u128) >> 24); *(p)++ = (uint8_t)((u128) >> 16);*(p)++ = (uint8_t)((u128) >> 8); *(p)++ = (uint8_t)(u128); }
375 /** Covert uint64_t to Stream */
376 #define UINT64_TO_BE_STREAM(p, u64) {*(p)++ = (uint8_t)((u64) >> 56); *(p)++ = (uint8_t)((u64) >> 48);*(p)++ = (uint8_t)((u64) >> 40); *(p)++ = (uint8_t)((u64) >> 32); \
377                                      *(p)++ = (uint8_t)((u64) >> 24); *(p)++ = (uint8_t)((u64) >> 16);*(p)++ = (uint8_t)((u64) >> 8); *(p)++ = (uint8_t)(u64); }
378 /** Covert uint32_t to Stream */
379 #define UINT32_TO_BE_STREAM(p, u32) {*(p)++ = (uint8_t)((u32) >> 24);  *(p)++ = (uint8_t)((u32) >> 16); *(p)++ = (uint8_t)((u32) >> 8); *(p)++ = (uint8_t)(u32); }
380 /** Covert UINT24 to Stream */
381 #define UINT24_TO_BE_STREAM(p, u24) {*(p)++ = (uint8_t)((u24) >> 16); *(p)++ = (uint8_t)((u24) >> 8); *(p)++ = (uint8_t)(u24);}
382 /** Covert uint16_t to Stream */
383 #define UINT16_TO_BE_STREAM(p, u16) {*(p)++ = (uint8_t)((u16) >> 8); *(p)++ = (uint8_t)(u16);}
384 /** Covert uint8_t to Stream */
385 #define UINT8_TO_BE_STREAM(p, u8)   {*(p)++ = (uint8_t)(u8);}
386 /** Covert Array to Stream */
387 #define ARRAY_TO_BE_STREAM(p, a, len) {register unsigned int ijk; for (ijk = 0; ijk < len; ijk++) *(p)++ = (uint8_t) a[ijk];}
388 
389 /** Stream to uint8_t */
390 #define BE_STREAM_TO_UINT8(u8, p)   {u8 = (uint8_t)(*(p)); (p) += 1;}
391 /** Stream to uint16_t */
392 #define BE_STREAM_TO_UINT16(u16, p) {u16 = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); (p) += 2;}
393 /** Stream to UINT24 */
394 #define BE_STREAM_TO_UINT24(u32, p) {u32 = (((uint32_t)(*((p) + 2))) + ((uint32_t)(*((p) + 1)) << 8) + ((uint32_t)(*(p)) << 16)); (p) += 3;}
395 /** Stream to uint32_t */
396 #define BE_STREAM_TO_UINT32(u32, p) {u32 = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) + ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); (p) += 4;}
397 /** Stream to uint64_t */
398 #define BE_STREAM_TO_UINT64(u64, p) {u64 = ((UINT64)(*((p) + 7)) + ((UINT64)(*((p) + 6)) << 8) + ((UINT64)(*((p) + 5)) << 16) + ((UINT64)(*((p) + 4)) << 24) + \
399                                            ((uint64_t)(*((p) + 3)) << 32) + ((uint64_t)(*((p) + 2)) << 40) + ((uint64_t)(*((p) + 1)) << 48) + ((uint64_t)(*(p)) << 56)); (p) += 8;}
400 /** Covert Array to Stream*/
401 #define BE_STREAM_TO_ARRAY(p, a, len) {register unsigned int ijk; for (ijk = 0; ijk < len; ijk++) ((uint8_t *) a)[ijk] = *p++;}
402 
403 
404 /********************************************************************************
405 ** Macros to get and put bytes to and from a field (Big Endian format).
406 ** These are the same as to stream, except the pointer is not incremented.
407 */
408 /** uint32_t to Field */
409 #define UINT32_TO_BE_FIELD(p, u32) {*(uint8_t *)(p) = (uint8_t)((u32) >> 24);  *((uint8_t *)(p)+1) = (uint8_t)((u32) >> 16); *((uint8_t *)(p)+2) = (uint8_t)((u32) >> 8); *((uint8_t *)(p)+3) = (uint8_t)(u32); }
410 /** UINT24 to Field */
411 #define UINT24_TO_BE_FIELD(p, u24) {*(uint8_t *)(p) = (uint8_t)((u24) >> 16); *((uint8_t *)(p)+1) = (uint8_t)((u24) >> 8); *((uint8_t *)(p)+2) = (uint8_t)(u24);}
412 /** uint16_t to Field */
413 #define UINT16_TO_BE_FIELD(p, u16) {*(uint8_t *)(p) = (uint8_t)((u16) >> 8); *((uint8_t *)(p)+1) = (uint8_t)(u16);}
414 /** uint8_t to Field */
415 #define UINT8_TO_BE_FIELD(p, u8)   {*(uint8_t *)(p) = (uint8_t)(u8);}
416 
417 /* Macros to Print the BD_ADDRESS */
418 /** Expand BDA */
419 #define EXPAND_BDADDR(bd)                 bd[0],bd[1],bd[2],bd[3],bd[4],bd[5]
420 /** Format BDA */
421 #define BDADDR_FORMAT                     "%02x:%02x:%02x:%02x:%02x:%02x"
422 /** Number of Elements */
423 #define NUM_ELEMENTS(x)                   (sizeof(x) / sizeof((x)[0]))
424 
425 #ifndef MIN
426 /** Min */
427 #define MIN( a, b )                                             ( ( (a) < (b) ) ? (a) : (b) )
428 #endif
429 
430 #ifndef MAX
431 /** Max */
432 #define MAX( a, b )                                             ( ( (a) > (b) ) ? (a) : (b) )
433 #endif
434 
435 #if defined(_WIN32) || defined(_WIN64)
436 /** Declare 4 byte align for Windows */
437     #define WICED_DECLARE_ALIGNED4(datatype, var) __declspec(align(4)) datatype var
438 #elif defined(__ICCARM__)
439 /** Declare 4 byte align for IAR */
440     #define WICED_DECLARE_ALIGNED4(datatype, var) _Pragma("data_alignment=4") datatype var
441 #else
442 /** Declare 4 byte align for GCC */
443     #define WICED_DECLARE_ALIGNED4(datatype, var) datatype var __attribute__((aligned(4)))
444 #endif
445 
446 /** get aligned size */
447 #define ALIGN_SIZE(value,align_to) (((value) + (align_to) - 1) & ~((align_to) - 1))
448 
449 /** Wiced BT Trace Type */
450 typedef enum {
451     WICED_BT_TRACE_DEBUG,
452     WICED_BT_TRACE_ERROR,
453     WICED_BT_TRACE_WARN,
454     WICED_BT_TRACE_API,
455     WICED_BT_TRACE_EVENT,
456 }wiced_bt_trace_type_t;
457 
458 /**@} bt_types */
459 
460 #ifdef __cplusplus
461 }
462 #endif
463