]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/htimerNtp.c
Update of ORTE. Configured to compile for Linux out of box.
[orte.git] / orte / liborte / htimerNtp.c
1 /*
2  *  $Id: htimerNtp.c,v 0.0.0.1          2003/08/21 
3  *
4  *  DEBUG:  section 2                   HTimer for NtpTime
5  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  */ 
21
22 #include "orte.h"
23
24 GAVL_FLES_INT_DEC(ul_htim_queue, ul_htim_queue_t, ul_htim_node_t, ul_htim_time_t,
25   timers, node, expires, ul_htimer_cmp_fnc)
26
27
28 GAVL_FLES_INT_IMP(ul_htim_queue, ul_htim_queue_t, ul_htim_node_t, ul_htim_time_t,
29       timers, node, expires, ul_htimer_cmp_fnc, GAVL_FAFTER, 
30  root->first_changed=1,/**/,root->first_changed=1)
31
32 /*****************************************************************************/
33 UL_HTIMER_IMP(htimerRoot, \
34       ObjectEntry, \
35       HTimFncRootNode, \
36       htimRoot, \
37       htim) 
38 void 
39 htimerRoot_run_expired(ORTEDomain *d, 
40       ul_htim_time_t *pact_time) {
41   HTimFncRootNode *timer;
42   
43   while((timer=htimerRoot_cut_expired(&d->objectEntry, pact_time))){
44     timer->func(d,timer->objectEntryAID,pact_time);
45   }
46
47
48 /*****************************************************************************/
49 UL_HTIMER_IMP(htimerUnicastCommon, \
50       ObjectEntryAID, \
51       HTimFncUserNode, \
52       htimUnicast.common, \
53       htim) 
54
55 void 
56 htimerUnicastCommon_update_root_timer(ObjectEntry *objectEntry,
57       ObjectEntryAID *objectEntryAID) {
58   NtpTime nextExpire;
59
60   //if first item is changed -> update root timer queue
61   if (htimerUnicastCommon_first_changed(objectEntryAID)) {
62     //deatach old timer from this queue (if any)
63     htimerRoot_detach(objectEntry,
64         &objectEntryAID->htimUnicast.commonNode);
65     if (htimerUnicastCommon_next_expire(objectEntryAID,&nextExpire)!=0) {
66       //setup new values
67       objectEntryAID->htimUnicast.commonNode.func=
68           htimerUnicastCommon_run_expired;
69       objectEntryAID->htimUnicast.commonNode.objectEntryAID=
70           objectEntryAID;
71       htimerRoot_set_expire(
72           &objectEntryAID->htimUnicast.commonNode,
73           nextExpire);
74       //insert new timer to root htimer node
75       htimerRoot_add(objectEntry,
76           &objectEntryAID->htimUnicast.commonNode);
77       debug(2,10) ("htimerUnicastCommon: root updated, wakeup\n");               
78       //wake-up sending thread to process event
79       ORTEDomainWakeUpSendingThread(objectEntry);
80     }
81   }
82 }
83
84 void 
85 htimerUnicastCommon_run_expired(ORTEDomain *d,
86                        ObjectEntryAID *objectEntryAID, 
87                        ul_htim_time_t *pact_time) {
88   HTimFncUserNode  *timer;
89   int              retValue;
90   pthread_rwlock_t *lock;
91   
92   while((timer=htimerUnicastCommon_cut_expired(objectEntryAID, pact_time))){
93     if ((lock=timer->lock)) //after proc. timer->func can be timer freed
94       pthread_rwlock_wrlock(timer->lock);
95     debug(2,10) ("htimerUnicastCommon: %s\n",
96                   timer->name);               
97     retValue=timer->func(d,timer->arg1);
98     if (lock) 
99       pthread_rwlock_unlock(timer->lock);
100     if (retValue==2)  //object deleted
101       return;
102   }
103   htimerUnicastCommon_update_root_timer(&d->objectEntry,objectEntryAID);
104
105
106 /*****************************************************************************/
107 UL_HTIMER_IMP(htimerUnicastSendMetatraffic, \
108       ObjectEntryAID, \
109       HTimFncUserNode, \
110       htimUnicast.sendMetatraffic, \
111       htim) 
112
113 void 
114 htimerUnicastSendMetatraffic_update_root_timer(ObjectEntry *objectEntry,
115       ObjectEntryAID *objectEntryAID) {
116   NtpTime nextExpire;
117
118   //if first item is changed -> update root timer queue
119   if (htimerUnicastSendMetatraffic_first_changed(objectEntryAID)) {
120     //deatach old timer from this queue (if any)
121     htimerRoot_detach(objectEntry,
122         &objectEntryAID->htimUnicast.sendMetatrafficNode);
123     if (htimerUnicastSendMetatraffic_next_expire(objectEntryAID,&nextExpire)!=0) {
124       //setup new values
125       objectEntryAID->htimUnicast.sendMetatrafficNode.func=
126           htimerUnicastSendMetatraffic_run_expired;
127       objectEntryAID->htimUnicast.sendMetatrafficNode.objectEntryAID=
128           objectEntryAID;
129       htimerRoot_set_expire(
130           &objectEntryAID->htimUnicast.sendMetatrafficNode,
131           nextExpire);
132       //insert new timer to root htimer node
133       htimerRoot_add(objectEntry,
134           &objectEntryAID->htimUnicast.sendMetatrafficNode);
135       debug(2,10) ("htimerUnicastMetatraffic: root updated, wakeup\n");               
136       //wake-up sending thread to process event
137       ORTEDomainWakeUpSendingThread(objectEntry);
138     }
139   }
140 }
141
142 void 
143 htimerUnicastSendMetatraffic_run_expired(ORTEDomain *d,
144     ObjectEntryAID *objectEntryAID,
145     ul_htim_time_t *pact_time) {
146   HTimFncUserNode *timer;
147   int             retValue;
148   
149   while((timer=htimerUnicastSendMetatraffic_cut_expired(objectEntryAID, pact_time))){
150     if (timer->lock)
151       pthread_rwlock_wrlock(timer->lock);
152     debug(2,10) ("htimerUnicastMetatraffic: %s\n",
153                   timer->name);               
154     retValue=timer->func(d,timer->arg1);
155     while (d->mbSend.needSend) {
156       ORTESendData(d,objectEntryAID,ORTE_TRUE);
157       timer->func(d,timer->arg1);
158     }
159     if (timer->lock)
160       pthread_rwlock_unlock(timer->lock);
161   }
162   htimerUnicastSendMetatraffic_update_root_timer(&d->objectEntry,objectEntryAID);
163   if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
164       ORTESendData(d,objectEntryAID,ORTE_TRUE);
165   }
166
167
168 /*****************************************************************************/
169 UL_HTIMER_IMP(htimerUnicastSendUserData, \
170       ObjectEntryAID, \
171       HTimFncUserNode, \
172       htimUnicast.sendUserData, \
173       htim) 
174
175 void 
176 htimerUnicastSendUserData_update_root_timer(ObjectEntry *objectEntry,
177       ObjectEntryAID *objectEntryAID) {
178   NtpTime nextExpire;
179
180   //if first item is changed -> update root timer queue
181   if (htimerUnicastSendUserData_first_changed(objectEntryAID)) {
182     //deatach old timer from this queue (if any)
183     htimerRoot_detach(objectEntry,
184         &objectEntryAID->htimUnicast.sendUserDataNode);
185     if (htimerUnicastSendUserData_next_expire(objectEntryAID,&nextExpire)!=0) {
186       //setup new values
187       objectEntryAID->htimUnicast.sendUserDataNode.func=
188           htimerUnicastSendUserData_run_expired;
189       objectEntryAID->htimUnicast.sendUserDataNode.objectEntryAID=
190           objectEntryAID;
191       htimerRoot_set_expire(
192           &objectEntryAID->htimUnicast.sendUserDataNode,
193           nextExpire);
194       //insert new timer to root htimer node
195       htimerRoot_add(objectEntry,
196           &objectEntryAID->htimUnicast.sendUserDataNode);
197       debug(2,10) ("htimerUnicastUserdata: root updated, wakeup\n");               
198       //wake-up sending thread to process event
199       ORTEDomainWakeUpSendingThread(objectEntry);
200     }
201   }
202 }
203
204 void 
205 htimerUnicastSendUserData_run_expired(ORTEDomain *d,
206     ObjectEntryAID *objectEntryAID, 
207     ul_htim_time_t *pact_time) {
208   HTimFncUserNode *timer;
209   int             retValue;
210   
211   while((timer=htimerUnicastSendUserData_cut_expired(objectEntryAID, pact_time))){
212     if (timer->lock)
213       pthread_rwlock_wrlock(timer->lock);
214     retValue=timer->func(d,timer->arg1);
215     while (d->mbSend.needSend) {
216       ORTESendData(d,objectEntryAID,ORTE_FALSE);
217       timer->func(d,timer->arg1);
218     }
219     if (timer->lock)
220       pthread_rwlock_unlock(timer->lock);
221   }
222   htimerUnicastSendUserData_update_root_timer(&d->objectEntry,objectEntryAID);
223   if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
224       ORTESendData(d,objectEntryAID,ORTE_FALSE);
225   }
226 }
227
228 /*********************************************************************/
229 NtpTime
230 getActualNtpTime(void) {
231   NtpTime               result;
232 #ifndef CONFIG_ORTE_RT
233   struct timeval        time;
234
235   gettimeofday(&time,NULL);
236   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
237   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
238 #else
239   struct timespec        time;
240
241   clock_gettime(CLOCK_REALTIME, &time);
242   time.tv_nsec/=1000;  //conver to us
243   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_nsec);
244 #endif
245   return result;
246 }