1 /******************************************************************************
2  *
3  *  Copyright (C) 2012-2014 Broadcom Corporation
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 NFC_TYPES_H
20 #define NFC_TYPES_H
21 
22 /****************************************************************************
23 ** NFC_HDR header definition for NFC messages
24 *****************************************************************************/
25 typedef struct {
26   uint16_t event;
27   uint16_t len;
28   uint16_t offset;
29   uint16_t layer_specific;
30 } NFC_HDR;
31 #define NFC_HDR_SIZE (sizeof(NFC_HDR))
32 
33 /* Mask for NFC_HDR event field */
34 #define NFC_EVT_MASK 0xFF00
35 
36 /****************************************************************************
37 ** NFC_HAL_TASK  definitions
38 *****************************************************************************/
39 
40 /* NCI message for hci persistency data     */
41 #define NFC_HAL_EVT_HCI 0x0400
42 
43 /* NCI message for sending to host stack    */
44 #define NFC_EVT_TO_NFC_NCI 0x4000
45 
46 /*****************************************************************************
47 ** Macros to get and put bytes to and from a stream (Little Endian format).
48 *****************************************************************************/
49 
50 #define UINT32_TO_STREAM(p, u32)     \
51   {                                  \
52     *(p)++ = (uint8_t)(u32);         \
53     *(p)++ = (uint8_t)((u32) >> 8);  \
54     *(p)++ = (uint8_t)((u32) >> 16); \
55     *(p)++ = (uint8_t)((u32) >> 24); \
56   }
57 #define UINT16_TO_STREAM(p, u16)    \
58   {                                 \
59     *(p)++ = (uint8_t)(u16);        \
60     *(p)++ = (uint8_t)((u16) >> 8); \
61   }
62 #define UINT8_TO_STREAM(p, u8) \
63   { *(p)++ = (uint8_t)(u8); }
64 #define INT8_TO_STREAM(p, u8) \
65   { *(p)++ = (int8_t)(u8); }
66 #define ARRAY8_TO_STREAM(p, a)                                    \
67   {                                                               \
68     int ijk;                                                      \
69     for (ijk = 0; ijk < 8; ijk++) *(p)++ = (uint8_t)(a)[7 - ijk]; \
70   }
71 #define ARRAY_TO_STREAM(p, a, len)                                \
72   {                                                               \
73     int ijk;                                                      \
74     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
75   }
76 #define STREAM_TO_UINT8(u8, p) \
77   {                            \
78     (u8) = (uint8_t)(*(p));    \
79     (p) += 1;                  \
80   }
81 #define STREAM_TO_UINT16(u16, p)                                  \
82   {                                                               \
83     (u16) = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \
84     (p) += 2;                                                     \
85   }
86 #define STREAM_TO_UINT32(u32, p)                                      \
87   {                                                                   \
88     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
89              ((((uint32_t)(*((p) + 2)))) << 16) +                     \
90              ((((uint32_t)(*((p) + 3)))) << 24));                     \
91     (p) += 4;                                                         \
92   }
93 #define STREAM_TO_ARRAY8(a, p)                     \
94   {                                                \
95     int ijk;                                       \
96     uint8_t* _pa = (uint8_t*)(a) + 7;              \
97     for (ijk = 0; ijk < 8; ijk++) *_pa-- = *(p)++; \
98   }
99 #define STREAM_TO_ARRAY(a, p, len)                                   \
100   {                                                                  \
101     int ijk;                                                         \
102     for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \
103   }
104 
105 /*****************************************************************************
106 ** Macros to get and put bytes to and from a stream (Big Endian format)
107 *****************************************************************************/
108 
109 #define UINT32_TO_BE_STREAM(p, u32)  \
110   {                                  \
111     *(p)++ = (uint8_t)((u32) >> 24); \
112     *(p)++ = (uint8_t)((u32) >> 16); \
113     *(p)++ = (uint8_t)((u32) >> 8);  \
114     *(p)++ = (uint8_t)(u32);         \
115   }
116 #define UINT24_TO_BE_STREAM(p, u24)  \
117   {                                  \
118     *(p)++ = (uint8_t)((u24) >> 16); \
119     *(p)++ = (uint8_t)((u24) >> 8);  \
120     *(p)++ = (uint8_t)(u24);         \
121   }
122 #define UINT16_TO_BE_STREAM(p, u16) \
123   {                                 \
124     *(p)++ = (uint8_t)((u16) >> 8); \
125     *(p)++ = (uint8_t)(u16);        \
126   }
127 #define UINT8_TO_BE_STREAM(p, u8) \
128   { *(p)++ = (uint8_t)(u8); }
129 #define ARRAY_TO_BE_STREAM(p, a, len)                             \
130   {                                                               \
131     int ijk;                                                      \
132     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
133   }
134 
135 #define BE_STREAM_TO_UINT8(u8, p) \
136   {                               \
137     (u8) = (uint8_t)(*(p));       \
138     (p) += 1;                     \
139   }
140 #define BE_STREAM_TO_UINT16(u16, p)                                       \
141   {                                                                       \
142     (u16) = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); \
143     (p) += 2;                                                             \
144   }
145 #define BE_STREAM_TO_UINT32(u32, p)                                      \
146   {                                                                      \
147     (u32) = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) +    \
148              ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
149     (p) += 4;                                                            \
150   }
151 
152 /*****************************************************************************
153 ** Macros to get and put bytes to and from a field (Big Endian format).
154 ** These are the same as to stream, except the pointer is not incremented.
155 *****************************************************************************/
156 
157 #define UINT8_TO_BE_FIELD(p, u8) \
158   { *(uint8_t*)(p) = (uint8_t)(u8); }
159 
160 #endif /* NFC_TYPES_H */
161