versadac  1
versadac - Scalable Recorder Firmware
cross.h
1 /*---------------------------------------------------------------------------*/
2 /* Copyright (C) 2006 Woodhead Software & Electonics. All rights reserved. */
3 /*---------------------------------------------------------------------------*/
4 /* This program is protected by international copyright. */
5 /* */
6 /* The use of this software including but not limited to its Source Code */
7 /* is subject to restrictions as agreed in the license agreement between */
8 /* you and Woodhead. */
9 /* Copying or distribution is not allowed unless expressly permitted */
10 /* according to your license agreement with Woodhead. */
11 /*---------------------------------------------------------------------------*/
12 /* */
13 /* Project : STACK PROFINET Controller */
14 /* Component : RPC Interface */
15 /* $Workfile:: cross.h $*/
16 /* $Revision:: 7 $*/
17 /* $Date:: 30/08/10 10:05 $*/
18 /* */
19 /*---------------------------------------------------------------------------*/
20 /* */
21 /* D e s c r i p t i o n : */
22 /* copy and/or cross function */
23 /* */
24 /*---------------------------------------------------------------------------*/
25 
26 #ifndef _PN_CROSS_H_
27 #define _PN_CROSS_H_
28 
29 /*-----------------------------------------------------------------------*/
30 /* Calculate offset between begin of a structure and a field */
31 /*-----------------------------------------------------------------------*/
32 
33 #define STRCT_OFFS(type, field) ((unsigned int)( &((type*)0)->field ))
34 
35 
36 
37 
38 /*-----------------------------------------------------------------------*/
39 /* Alignement variable */
40 /*-----------------------------------------------------------------------*/
41 typedef union _un_align
42 {
43  APP_DWORD _x32;
44  APP_WORD _x16[2];
45  APP_BYTE _x8[4];
46 } UN_ALIGN,APP_FAR* LPUN_ALIGN;
47 
48 #define DECLARE_ALIGNEMENT UN_ALIGN Src;LPUN_ALIGN pDst;
49 #ifdef COPY_BY_BYTE
50 #define DECLARE_COPY_BY_BYTE UN_ALIGN Src;LPUN_ALIGN pDst;
51 #else
52 #define DECLARE_COPY_BY_BYTE
53 #endif
54 
55 
56 /*-----------------------------------------------------------------------*/
57 /* Simple copy (same format for platform and dest) */
58 /* Take care also of supported alignement */
59 /*-----------------------------------------------------------------------*/
60 #define BYTE_CPY(base, offset, val) \
61  OS_ASSERT(base)\
62  *(APP_LPBYTE)(base+offset) = val
63 /*----------------------------------------------*/
64 
65 #ifdef COPY_BY_BYTE
66 #define WORD_CPY(base, offset, val) \
67 {\
68  OS_ASSERT(base); \
69  \
70  Src._x16[0] = val; \
71  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)((APP_LPBYTE)base + offset) ); \
72  \
73  pDst->_x8[0] = Src._x8[0]; \
74  pDst->_x8[1] = Src._x8[1];\
75 }
76 #else
77 #define WORD_CPY(base, offset, val) \
78 OS_ASSERT(base);\
79 *((APP_LPWORD)(base+offset)) = (APP_WORD)(val)
80 #endif
81 /*----------------------------------------------*/
82 
83 #ifdef COPY_BY_BYTE
84 #define DWORD_CPY(base, offset, val) \
85 {\
86  OS_ASSERT(base); \
87  Src._x32 = val; \
88  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)( (APP_LPBYTE)base + offset) ); \
89  pDst->_x8[0] = Src._x8[0]; \
90  pDst->_x8[1] = Src._x8[1]; \
91  pDst->_x8[2] = Src._x8[2]; \
92  pDst->_x8[3] = Src._x8[3];\
93 }
94 #else
95 #define DWORD_CPY(base, offset, val) \
96  *(APP_LPDWORD)(base+offset) = (APP_DWORD)val
97 #endif
98 
99 /*----------------------------------------------*/
100 
101 #ifdef FIX_DWORD_ALIGNMENT
102 
103 #define UUID_CPY(base, offset, val) \
104  WORD_CPY(base, offset , (val).Data1_w1); \
105  WORD_CPY(base, offset + sizeof(APP_WORD) , (val).Data1_w2); \
106  WORD_CPY(base, offset + sizeof(APP_DWORD) , (val).Data2); \
107  WORD_CPY(base, offset + sizeof(APP_DWORD)+sizeof(APP_WORD) , (val).Data3); \
108  OS_MEMCOPY((APP_LPBYTE)base + offset + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val).Data4[0], UUID4LEN)
109 
110 #else /*FIX_DWORD_ALIGNMENT*/
111 
112 #define UUID_CPY(base, offset, val) \
113  DWORD_CPY(base, offset , (val).Data1); \
114  WORD_CPY(base, offset + sizeof(APP_DWORD) , (val).Data2); \
115  WORD_CPY(base, offset + sizeof(APP_DWORD)+sizeof(APP_WORD) , (val).Data3); \
116  OS_MEMCOPY((APP_LPBYTE)base + offset + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val).Data4[0], UUID4LEN)
117 
118 #endif /*FIX_DWORD_ALIGNMENT*/
119 
120 /*-----------------------------------------------------------------------*/
121 /* Cross copy (format for platform and destination are different) */
122 /*-----------------------------------------------------------------------*/
123 
124 
125 #define WORD_FMX(base, offset, val) \
126  Src._x16[0] = (APP_WORD)(val); \
127  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)( (APP_LPBYTE)base + offset) ); \
128  \
129  pDst->_x8[0] = Src._x8[1]; \
130  pDst->_x8[1] = Src._x8[0]
131 /*----------------------------------------------*/
132 
133 #define DWORD_FMX(base, offset, val) \
134  Src._x32 = val; \
135  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)( (APP_LPBYTE)base + offset) ); \
136  pDst->_x8[0] = Src._x8[3]; \
137  pDst->_x8[1] = Src._x8[2]; \
138  pDst->_x8[2] = Src._x8[1]; \
139  pDst->_x8[3] = Src._x8[0]
140 
141 /*----------------------------------------------*/
142 #ifdef FIX_DWORD_ALIGNMENT
143 
144 #define UUID_FMX(base, offset, val) \
145  WORD_FMX((base), (offset) , (val).Data1_w2); \
146  WORD_FMX((base), (offset) + sizeof(APP_WORD) , (val).Data1_w1); \
147  WORD_FMX((base), (offset) + sizeof(APP_DWORD) , (val).Data2); \
148  WORD_FMX((base), (offset) + sizeof(APP_DWORD)+sizeof(APP_WORD) , (val).Data3); \
149  OS_MEMCOPY((APP_LPBYTE)(base) + (offset) + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val).Data4[0], UUID4LEN)
150 
151 #else /*FIX_DWORD_ALIGNMENT*/
152 
153 #define UUID_FMX(base, offset, val) \
154  DWORD_FMX((base), (offset) , (val).Data1); \
155  WORD_FMX((base), (offset) + sizeof(APP_DWORD) , (val).Data2); \
156  WORD_FMX((base), (offset) + sizeof(APP_DWORD)+sizeof(APP_WORD) , (val).Data3); \
157  OS_MEMCOPY((APP_LPBYTE)(base) + (offset) + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val).Data4[0], UUID4LEN)
158 
159 #endif /*FIX_DWORD_ALIGNMENT*/
160 /*-----------------------------------------------------------------------*/
161 /* Copy to Motorola format */
162 /* Take care also of supported alignement */
163 /*-----------------------------------------------------------------------*/
164 #ifdef COPY_BY_BYTE
165 #define CPY_HTONS(base, offset, val) \
166 {\
167  Src._x16[0] = HTONS(val); \
168  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)( (APP_LPBYTE)(base) + offset) );\
169  \
170  pDst->_x8[0] = Src._x8[0]; \
171  pDst->_x8[1] = Src._x8[1];\
172 }
173 #else
174 #define CPY_HTONS(base, offset, val) \
175  *(APP_WORD APP_FAR*)( (APP_LPBYTE)base + offset ) = HTONS(val)
176 #endif
177 /*----------------------------------------------*/
178 
179 #ifdef COPY_BY_BYTE
180 #define CPY_HTONL(base, offset, val) \
181 {\
182  Src._x32 = HTONL(val); \
183  pDst = (LPUN_ALIGN) ( (APP_LPBYTE)( (APP_LPBYTE) base + offset) );\
184  pDst->_x8[0] = Src._x8[0]; \
185  pDst->_x8[1] = Src._x8[1]; \
186  pDst->_x8[2] = Src._x8[2]; \
187  pDst->_x8[3] = Src._x8[3];\
188 }
189 #else
190 #define CPY_HTONL(base, offset, val) \
191  *(APP_DWORD APP_FAR*)( (APP_LPBYTE)base + offset ) = HTONL(val)
192 #endif
193 
194 
195 /*----------------------------------------------*/
196 #ifdef FIX_DWORD_ALIGNMENT
197 
198 #define OS_MAKEWORD(blo, bhi) ((APP_WORD)(((blo) & 0xff) | ((APP_WORD)(((bhi) << 8) & 0xff00))))
199 #define OS_LOBYTE(w) ((APP_BYTE)(w))
200 #define OS_HIBYTE(w) ((APP_BYTE)(((APP_WORD)(w) >> 8) & 0xFF))
201 
202 #define OS_MAKEDWORD(wlo, whi) ((APP_DWORD)(((wlo) & 0xffff) | ((APP_DWORD)(((whi) << 16) & 0xffff0000))))
203 #define OS_LOWORD(dw) ((APP_WORD)(dw))
204 #define OS_HIWORD(dw) ((APP_WORD)(((APP_DWORD)(dw) >> 16) & 0xFFFF))
205 
206 
207 #define ALIGNED_WORD_CPY(a, b) OS_MEMCOPY(&(b), &(a), sizeof(APP_WORD))
208 
209 #define ALIGNED_WORD_HTONS(addr, b) { \
210  APP_WORD w_h; \
211  ALIGNED_WORD_CPY(addr, w_h); \
212  (b) = HTONS(w_h); }
213 
214 
215 #define ALIGNED_DWORD_VALUE(dw) OS_MAKEDWORD((dw##_w1),(dw##_w2))
216 
217 #define ALIGNED_DWORD_ADDRESS(dw) &(dw##_w1)
218 
219 #define ALIGNED_DWORD_ASSIGN(a, b) { \
220  (b##_w1) = OS_LOWORD(a); \
221  (b##_w2) = OS_HIWORD(a); }
222 
223 #define ALIGNED_HTONL(h, n) { \
224  APP_DWORD dw_n = HTONL(OS_MAKEDWORD((h##_w1),(h##_w2))); \
225  (n##_w1) = OS_LOWORD(dw_n); \
226  (n##_w2) = OS_HIWORD(dw_n); }
227 
228 #define ALIGNED_STRCT_OFFS(type, field) ((unsigned int)&(((type*)0)->field##_w1))
229 
230 #define ALIGNED_CPY_HTONL(base, offset, val) { \
231  APP_DWORD dw_n = HTONL(val); \
232  APP_WORD w_n =0; \
233  w_n = OS_LOWORD(dw_n);\
234  ALIGNED_WORD_CPY(w_n, *((APP_WORD APP_FAR*)(((APP_LPBYTE)base) + offset)));\
235  w_n = OS_HIWORD(dw_n);\
236  ALIGNED_WORD_CPY(w_n, *((APP_WORD APP_FAR*)(((APP_LPBYTE)base) + offset + sizeof(APP_WORD))));}
237 
238 #define ALIGNED_CPY_DWORD(base, offset, val) { \
239  APP_DWORD dw_n = (val); \
240  APP_WORD w_n =0; \
241  w_n = OS_LOWORD(dw_n);\
242  ALIGNED_WORD_CPY(w_n, *((APP_WORD APP_FAR*)(((APP_LPBYTE)base) + offset)));\
243  w_n = OS_HIWORD(dw_n);\
244  ALIGNED_WORD_CPY(w_n, *((APP_WORD APP_FAR*)(((APP_LPBYTE)base) + offset + sizeof(APP_WORD))));}
245 
246 #define ALIGNED_FMX_DWORD(base, offset, val) { \
247  APP_DWORD dw = (val); \
248  APP_LPBYTE pby_src = (APP_LPBYTE)&dw; \
249  APP_LPBYTE pby_dst = (((APP_LPBYTE)base) + offset); \
250  pby_dst[3] = pby_src[0]; \
251  pby_dst[2] = pby_src[1]; \
252  pby_dst[1] = pby_src[2]; \
253  pby_dst[0] = pby_src[3]; }
254 
255 #else /*FIX_DWORD_ALIGNMENT*/
256 
257 #define ALIGNED_WORD_CPY(a,b) OS_MEMCOPY(&b, &a, sizeof(APP_WORD))
258 
259 #define ALIGNED_WORD_ASSIGN(val, dest) OS_MEMCOPY(&dest, &val, sizeof(APP_WORD))
260 
261 #define ALIGNED_WORD_HTONS(a, b) {\
262  APP_WORD wa;\
263  OS_MEMCOPY(&wa,&a,sizeof(APP_WORD));\
264  (b) = HTONS(wa);}
265 
266 #define ALIGNED_DWORD_VALUE(dw) (dw)
267 
268 #define ALIGNED_DWORD_ADDRESS(dw) &(dw)
269 
270 #define ALIGNED_DWORD_ASSIGN(a, b) (b) = (a)
271 
272 #define ALIGNED_HTONL(h, n) (n) = HTONL(h)
273 
274 #define ALIGNED_STRCT_OFFS(type, field) STRCT_OFFS(type, field)
275 
276 #define ALIGNED_CPY_HTONL(base, offset, val) CPY_HTONL(base, offset, val)
277 
278 #define ALIGNED_CPY_DWORD(base, offset, val) DWORD_CPY(base, offset, val)
279 
280 #define ALIGNED_FMX_DWORD(base, offset, val) DWORD_FMX(base, offset, val)
281 
282 #endif /*FIX_DWORD_ALIGNMENT*/
283 
284 
285 /*----------------------------------------------*/
286 #ifdef FIX_DWORD_ALIGNMENT
287 #define CPY_HTON_UUID(base, offset, val) \
288  ALIGNED_CPY_HTONL(base, offset, ALIGNED_DWORD_VALUE(val.Data1)); \
289  CPY_HTONS(base, offset + sizeof(APP_DWORD) , val.Data2); \
290  CPY_HTONS(base, offset+sizeof(APP_DWORD)+sizeof(APP_WORD) , val.Data3); \
291  OS_MEMCOPY((APP_LPBYTE)base + offset + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val.Data4[0]), UUID4LEN)
292 #endif
293 
294 #ifndef FIX_DWORD_ALIGNMENT
295 #define CPY_HTON_UUID(base, offset, val) \
296  CPY_HTONL(base, offset, val.Data1); \
297  CPY_HTONS(base, offset + sizeof(APP_DWORD) , val.Data2); \
298  CPY_HTONS(base, offset+sizeof(APP_DWORD)+sizeof(APP_WORD) , val.Data3); \
299  OS_MEMCOPY((APP_LPBYTE)base + offset + sizeof(APP_DWORD) + 2*sizeof(APP_WORD), &(val.Data4[0]), UUID4LEN)
300 
301 #endif
302 
303 
304 #endif
Definition: cross.h:41