]> rtime.felk.cvut.cz Git - orte.git/blob - orte/include/orte/defines_api.h
updated email address - petr@smoliku.cz
[orte.git] / orte / include / orte / defines_api.h
1 /*
2  *  $Id: defines_api.h,v 0.0.0.1        2003/08/21 
3  *
4  *  -------------------------------------------------------------------  
5  *                                ORTE                                 
6  *                      Open Real-Time Ethernet                       
7  *                                                                    
8  *                      Copyright (C) 2001-2006                       
9  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
10  *                      http://dce.felk.cvut.cz                       
11  *                      http://www.ocera.org                          
12  *                                                                    
13  *  Author:              Petr Smolik    petr@smoliku.cz             
14  *  Advisor:             Pavel Pisa                                   
15  *  Project Responsible: Zdenek Hanzalek                              
16  *  --------------------------------------------------------------------
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22  *  
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *  
28  */
29
30 #ifndef _DEFINES_API_H
31 #define _DEFINES_API_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define MAX_INTERFACES            5
38 #define ORTE_DEFAULT_DOMAIN       0
39 #define MAX_STRING_IPADDRESS_LENGTH 4*3+3+1
40 #define MAX_STRING_NTPTIME_LENGTH 30    //need fix
41
42 ////////////////////////////////////////////////////////////////////////////////
43 //Return values
44 #define ORTE_OK                   0
45 #define ORTE_BAD_HANDLE           -1
46 #define ORTE_TIMEOUT              -2
47 #define ORTE_QUEUE_FULL           -3
48
49 ////////////////////////////////////////////////////////////////////////////////
50 //Logging - flags
51 #define LOG_FNONE                 0x00
52 #define LOG_FEXCEPTION            0x01
53 #define LOG_FWARN                 0x02
54 #define LOG_FLOCAL_OBJECT         0x04  
55 #define LOG_FREMOTE_OBJECT        0x08
56 #define LOG_FPERIODIC             0x10
57 #define LOG_FCONTENT              0x20
58
59 /*****************************************************************/
60 /**
61  * SeqNumberCmp - comparison of two sequence numbers
62  * @sn1: source sequential number 1
63  * @sn2: source sequential number 2
64  * 
65  * Return:  1 if sn1 > sn2
66  *         -1 if sn1 < sn2
67  *          0 if sn1 = sn2
68  */
69 #define SeqNumberCmp(sn1, sn2) (                 \
70           (sn1).high>(sn2).high ? 1 :            \
71         ((sn1).high<(sn2).high ? -1 :            \
72           ((sn1).low>(sn2).low ? 1 :             \
73             ((sn1).low<(sn2).low ? -1 : 0)))     \
74     )
75
76 /*****************************************************************/
77 /**
78  * SeqNumberInc - incrementation of a sequence number
79  * @res: result
80  * @sn: sequential number to be incremented
81  * 
82  * res = sn + 1
83  */
84 #define SeqNumberInc(res,sn) {                   \
85       (res) = (sn);                              \
86           if (++(res).low==0) (res).high++;      \
87         }
88
89
90 /*****************************************************************/
91 /**
92  * SeqNumberSub - addition of two sequential numbers
93  * @res: result
94  * @sn1: source sequential number 1
95  * @sn2: source sequential number 2
96  *
97  * res = sn1 + sn2
98  */
99 #define SeqNumberAdd(res,sn1,sn2) {              \
100     (res).low = (sn1).low+(sn2).low;             \
101     (res).high = (sn1).high+(sn2).high;          \
102     if (((res).low < (sn1).low) ||               \
103         ((res).low < (sn2).low)) {               \
104       (res).high++;                              \
105     }                                            \
106 }
107
108 /*****************************************************************/
109 /**
110  * SeqNumberDec - decrementation of a sequence number
111  * @res: result
112  * @sn: sequential number to be decremented
113  * 
114  * res = sn - 1
115  */
116 #define SeqNumberDec(res,sn) {                   \
117       (res) = (sn);                              \
118           if ((res).low--==0) (res).high--;      \
119         }
120
121 /*****************************************************************/
122 /**
123  * SeqNumberSub - substraction of two sequential numbers
124  * @res: result
125  * @sn1: source sequential number 1
126  * @sn2: source sequential number 2
127  *
128  * res = sn1 - sn2
129  */
130 #define SeqNumberSub(res,sn1,sn2) {                  \
131     (res).low = (sn1).low-(sn2).low;                 \
132     (res).high = (sn1).high-(sn2).high;              \
133     if ((res).low > (sn1).low) {                     \
134       (res).high--;                                  \
135     }                                                \
136 }
137
138 /*****************************************************************/
139 /**
140  * NtpTimeCmp - comparation of two NtpTimes
141  * @time1: source time 1
142  * @time2: source time 2
143  *
144  * Return value:
145  * 1 if time 1 > time 2 
146  * -1 if time 1 < time 2
147  * 0 if time 1 = time 2
148  */
149 #define NtpTimeCmp(time1, time2)                     \
150 ((((time1).seconds) > ((time2).seconds)) ? 1 :       \
151  ((((time1).seconds) < ((time2).seconds)) ? -1 :     \
152   ((((time1).fraction) > ((time2).fraction)) ? 1 :   \
153    ((((time1).fraction) < ((time2).fraction)) ? -1 : 0))))
154
155
156 /*****************************************************************/
157 /**
158  * NtpTimeAdd - addition of two NtpTimes
159  * @res: result
160  * @time1: source time 1
161  * @time2: source time 2
162  *
163  * res = time1 + time2
164  */
165 #define NtpTimeAdd(res, time1, time2) {              \
166     (res).seconds  = (time1).seconds + (time2).seconds;   \
167     (res).fraction = (time1).fraction + (time2).fraction; \
168     if (((res).fraction < (time1).fraction) ||       \
169         ((res).fraction < (time2).fraction)) {       \
170       (res).seconds++;                               \
171     }                                                \
172 }
173
174 /*****************************************************************/
175 /**
176  * NtpTimeSub - substraction of two NtpTimes
177  * @res: result
178  * @time1: source time 1
179  * @time2: source time 2
180  *
181  * res = time1 - time2
182  */
183 #define NtpTimeSub(res, time1, time2) {              \
184    (res).seconds  = (time1).seconds - (time2).seconds;   \
185    (res).fraction = (time1).fraction - (time2).fraction; \
186    if ((res).fraction > (time1).fraction) {          \
187      (res).seconds--;                                \
188    }                                                 \
189 }
190
191 /*****************************************************************/
192 /**
193  * NtpTimeAssembFromMs - converts seconds and miliseconds to NtpTime
194  * @time: time given in NtpTime structure
195  * @s: seconds portion of given time
196  * @msec: miliseconds portion of given time
197  */
198 #define NtpTimeAssembFromMs(time, s, msec) {         \
199     register uint32_t ms = msec;                     \
200     (time).seconds  = s;                             \
201     (time).fraction = (ms<<22) + ((ms*393)<<8);      \
202 }
203
204 /**
205  * NtpTimeDisAssembToMs - converts NtpTime to seconds and miliseconds
206  * @s: seconds portion of given time
207  * @msec: miliseconds portion of given time
208  * @time: time given in NtpTime structure
209  */
210 #define NtpTimeDisAssembToMs(s, msec, time) {        \
211     s    = (time).seconds;                           \
212     msec = ((time).fraction - ((time).fraction>>6) - \
213            ((time).fraction>>7) + (1<<21))>>22;      \
214     if ((msec) >= 1000 ) { (msec) -= 1000; (s)++; }  \
215 }
216
217 /**
218  * NtpTimeAssembFromUs - converts seconds and useconds to NtpTime
219  * @time: time given in NtpTime structure
220  * @s: seconds portion of given time
221  * @usec: microseconds portion of given time
222  */
223 #define NtpTimeAssembFromUs(time, s, usec) {         \
224     register uint32_t us = usec;                     \
225     (time).seconds  = s;                             \
226     (time).fraction = (us<<12)+ ((us*99)<<1)+ ((us*15 + ((us*61)>>7))>>4); \
227 }
228
229 /**
230  * NtpTimeDisAssembToUs - converts NtpTime to seconds and useconds
231  * @s: seconds portion of given time
232  * @usec: microseconds portion of given time
233  * @time: time given in NtpTime structure
234  */
235 #define NtpTimeDisAssembToUs(s, usec, time) {        \
236     register uint32_t NtpTemp = (time).fraction;     \
237     s    = (time).seconds;                           \
238     usec = ((time).fraction - (NtpTemp>>5)-(NtpTemp>>7)-(NtpTemp>>8)- \
239             (NtpTemp>>9)-(NtpTemp>>10) - (NtpTemp>>12) - \
240             (NtpTemp>>13)-(NtpTemp>>14) + (1<<11)) >> 12; \
241     if ((usec) >= 1000000) { (usec) -= 1000000; (s)++; } \
242 }
243
244 /**
245  * Domain2Port - converts Domain value to IP Port value
246  * @d: domain
247  * @p: port
248  */
249 #define Domain2Port(d,p) {                   \
250   p = RTPS_DEFAULT_PORT + d*10;              \
251 }
252
253 /**
254  * Domain2PortMulticastUserdata - converts Domain value to userdata IP Port value
255  * @d: domain
256  * @p: port
257  */
258 #define Domain2PortMulticastUserdata(d,p) {  \
259     p = RTPS_DEFAULT_PORT + d*10+1;          \
260 }
261
262 /**
263  * Domain2PortMulticastMetatraffic - converts Domain value to metatraffic IP Port value
264  * @d: domain
265  * @p: port
266  */
267 #define Domain2PortMulticastMetatraffic(d,p) {        \
268     p = RTPS_DEFAULT_PORT + d*10+2;          \
269 }
270
271
272 /* Align an address upward to a boundary, expressed as a number of bytes.
273    E.g. align to an 8-byte boundary with argument of 8.  */
274
275 /*
276  *   (this + boundary - 1)
277  *          &
278  *    ~(boundary - 1)
279  */
280
281 #define ALIGN_ADDRESS(this, boundary) \
282   ((void*)((( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))))
283
284
285 #ifdef __cplusplus
286 } /* extern "C"*/
287 #endif
288
289 #endif /* _DEFINES_API_H */