]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/htimerNtp.c
18565cc00de6b5afd302f28522b45a22205995b7
[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_all.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   debug(2,10) ("htimerRoot: start\n");               
44   while((timer=htimerRoot_cut_expired(&d->objectEntry, pact_time))){
45     debug(2,10) ("htimerRoot: AID-0x%x\n",timer->objectEntryAID->aid);               
46     timer->func(d,timer->objectEntryAID,pact_time);
47   }
48   debug(2,10) ("htimerRoot: finished\n");               
49
50
51 /*****************************************************************************/
52 UL_HTIMER_IMP(htimerUnicastCommon, \
53       ObjectEntryAID, \
54       HTimFncUserNode, \
55       htimUnicast.common, \
56       htim) 
57
58 void 
59 htimerUnicastCommon_update_root_timer(ObjectEntry *objectEntry,
60       ObjectEntryAID *objectEntryAID) {
61   NtpTime nextExpire;
62
63   //if first item is changed -> update root timer queue
64   if (htimerUnicastCommon_first_changed(objectEntryAID)) {
65     //deatach old timer from this queue (if any)
66     htimerRoot_detach(objectEntry,
67         &objectEntryAID->htimUnicast.commonNode);
68     if (htimerUnicastCommon_next_expire(objectEntryAID,&nextExpire)!=0) {
69       //setup new values
70       objectEntryAID->htimUnicast.commonNode.func=
71           htimerUnicastCommon_run_expired;
72       objectEntryAID->htimUnicast.commonNode.objectEntryAID=
73           objectEntryAID;
74       htimerRoot_set_expire(
75           &objectEntryAID->htimUnicast.commonNode,
76           nextExpire);
77       //insert new timer to root htimer node
78       htimerRoot_add(objectEntry,
79           &objectEntryAID->htimUnicast.commonNode);
80       debug(2,10) ("htimerUnicastCommon: root updated, wakeup\n");               
81       //wake-up sending thread to process event
82       ORTEDomainWakeUpSendingThread(objectEntry);
83     }
84   }
85 }
86
87 void 
88 htimerUnicastCommon_run_expired(ORTEDomain *d,
89                        ObjectEntryAID *objectEntryAID, 
90                        ul_htim_time_t *pact_time) {
91   HTimFncUserNode  *timer;
92   int              retValue;
93   pthread_rwlock_t *lock;
94   
95   while((timer=htimerUnicastCommon_cut_expired(objectEntryAID, pact_time))){
96     if ((lock=timer->lock)) //after proc. timer->func can be timer freed
97       pthread_rwlock_wrlock(timer->lock);
98     debug(2,10) ("htimerUnicastCommon: %s\n",
99                   timer->name);               
100     retValue=timer->func(d,timer->arg1);
101     if (lock) 
102       pthread_rwlock_unlock(timer->lock);
103     if (retValue==2)  //object deleted
104       return;
105   }
106   htimerUnicastCommon_update_root_timer(&d->objectEntry,objectEntryAID);
107
108
109 /*****************************************************************************/
110 UL_HTIMER_IMP(htimerUnicastSendMetatraffic, \
111       ObjectEntryAID, \
112       HTimFncUserNode, \
113       htimUnicast.sendMetatraffic, \
114       htim) 
115
116 void 
117 htimerUnicastSendMetatraffic_update_root_timer(ObjectEntry *objectEntry,
118       ObjectEntryAID *objectEntryAID) {
119   NtpTime nextExpire;
120
121   //if first item is changed -> update root timer queue
122   if (htimerUnicastSendMetatraffic_first_changed(objectEntryAID)) {
123     //deatach old timer from this queue (if any)
124     htimerRoot_detach(objectEntry,
125         &objectEntryAID->htimUnicast.sendMetatrafficNode);
126     if (htimerUnicastSendMetatraffic_next_expire(objectEntryAID,&nextExpire)!=0) {
127       //setup new values
128       objectEntryAID->htimUnicast.sendMetatrafficNode.func=
129           htimerUnicastSendMetatraffic_run_expired;
130       objectEntryAID->htimUnicast.sendMetatrafficNode.objectEntryAID=
131           objectEntryAID;
132       htimerRoot_set_expire(
133           &objectEntryAID->htimUnicast.sendMetatrafficNode,
134           nextExpire);
135       //insert new timer to root htimer node
136       htimerRoot_add(objectEntry,
137           &objectEntryAID->htimUnicast.sendMetatrafficNode);
138       debug(2,10) ("htimerUnicastMetatraffic: root updated, wakeup\n");               
139       //wake-up sending thread to process event
140       ORTEDomainWakeUpSendingThread(objectEntry);
141     }
142   }
143 }
144
145 void 
146 htimerUnicastSendMetatraffic_run_expired(ORTEDomain *d,
147     ObjectEntryAID *objectEntryAID,
148     ul_htim_time_t *pact_time) {
149   HTimFncUserNode *timer;
150   int             retValue;
151   
152   while((timer=htimerUnicastSendMetatraffic_cut_expired(objectEntryAID, pact_time))){
153     if (timer->lock)
154       pthread_rwlock_wrlock(timer->lock);
155     debug(2,10) ("htimerUnicastMetatraffic: %s\n",
156                   timer->name);               
157     retValue=timer->func(d,timer->arg1);
158     while (d->mbSend.needSend) {
159       ORTESendData(d,objectEntryAID,ORTE_TRUE);
160       timer->func(d,timer->arg1);
161     }
162     if (timer->lock)
163       pthread_rwlock_unlock(timer->lock);
164   }
165   htimerUnicastSendMetatraffic_update_root_timer(&d->objectEntry,objectEntryAID);
166   if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
167       ORTESendData(d,objectEntryAID,ORTE_TRUE);
168   }
169
170
171 /*****************************************************************************/
172 UL_HTIMER_IMP(htimerUnicastSendUserData, \
173       ObjectEntryAID, \
174       HTimFncUserNode, \
175       htimUnicast.sendUserData, \
176       htim) 
177
178 void 
179 htimerUnicastSendUserData_update_root_timer(ObjectEntry *objectEntry,
180       ObjectEntryAID *objectEntryAID) {
181   NtpTime nextExpire;
182
183   //if first item is changed -> update root timer queue
184   if (htimerUnicastSendUserData_first_changed(objectEntryAID)) {
185     //deatach old timer from this queue (if any)
186     htimerRoot_detach(objectEntry,
187         &objectEntryAID->htimUnicast.sendUserDataNode);
188     if (htimerUnicastSendUserData_next_expire(objectEntryAID,&nextExpire)!=0) {
189       //setup new values
190       objectEntryAID->htimUnicast.sendUserDataNode.func=
191           htimerUnicastSendUserData_run_expired;
192       objectEntryAID->htimUnicast.sendUserDataNode.objectEntryAID=
193           objectEntryAID;
194       htimerRoot_set_expire(
195           &objectEntryAID->htimUnicast.sendUserDataNode,
196           nextExpire);
197       //insert new timer to root htimer node
198       htimerRoot_add(objectEntry,
199           &objectEntryAID->htimUnicast.sendUserDataNode);
200       debug(2,10) ("htimerUnicastUserdata: root updated, wakeup\n");               
201       //wake-up sending thread to process event
202       ORTEDomainWakeUpSendingThread(objectEntry);
203     }
204   }
205 }
206
207 void 
208 htimerUnicastSendUserData_run_expired(ORTEDomain *d,
209     ObjectEntryAID *objectEntryAID, 
210     ul_htim_time_t *pact_time) {
211   HTimFncUserNode *timer;
212   int             retValue;
213   
214   while((timer=htimerUnicastSendUserData_cut_expired(objectEntryAID, pact_time))){
215     if (timer->lock)
216       pthread_rwlock_wrlock(timer->lock);
217     retValue=timer->func(d,timer->arg1);
218     while (d->mbSend.needSend) {
219       ORTESendData(d,objectEntryAID,ORTE_FALSE);
220       timer->func(d,timer->arg1);
221     }
222     if (timer->lock)
223       pthread_rwlock_unlock(timer->lock);
224   }
225   htimerUnicastSendUserData_update_root_timer(&d->objectEntry,objectEntryAID);
226   if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
227       ORTESendData(d,objectEntryAID,ORTE_FALSE);
228   }
229 }
230
231 /*********************************************************************/
232 NtpTime
233 getActualNtpTime(void) {
234   NtpTime               result;
235 #ifndef CONFIG_ORTE_RT
236   struct timeval        time;
237
238   gettimeofday(&time,NULL);
239   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
240   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
241 #else
242   struct timespec        time;
243
244   clock_gettime(CLOCK_REALTIME, &time);
245   time.tv_nsec/=1000;  //conver to us
246   NtpTimeAssembFromUs(result,time.tv_sec,time.tv_nsec);
247 #endif
248   return result;
249 }