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