2 * $Id: htimerNtp.c,v 0.0.0.1 2003/08/21
4 * DEBUG: section 2 HTimer for NtpTime
6 * -------------------------------------------------------------------
8 * Open Real-Time Ethernet
10 * Copyright (C) 2001-2006
11 * Department of Control Engineering FEE CTU Prague, Czech Republic
12 * http://dce.felk.cvut.cz
13 * http://www.ocera.org
15 * Author: Petr Smolik petr.smolik@wo.cz
17 * Project Responsible: Zdenek Hanzalek
18 * --------------------------------------------------------------------
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
32 #define UL_HTIMER_INC_FROM_BASE
36 GAVL_FLES_INT_DEC(ul_htim_queue, ul_htim_queue_t, ul_htim_node_t, ul_htim_time_t,
37 timers, node, expires, ul_htimer_cmp_fnc)
40 GAVL_FLES_INT_IMP(ul_htim_queue, ul_htim_queue_t, ul_htim_node_t, ul_htim_time_t,
41 timers, node, expires, ul_htimer_cmp_fnc, GAVL_FAFTER,
42 root->first_changed=1,;,root->first_changed=1)
44 /*****************************************************************************/
45 UL_HTIMER_IMP(htimerRoot, \
51 htimerRoot_run_expired(ORTEDomain *d,
52 ul_htim_time_t *pact_time) {
53 HTimFncRootNode *timer;
55 debug(2,10) ("htimerRoot: start\n");
56 while((timer=htimerRoot_cut_expired(&d->objectEntry, pact_time))){
57 debug(2,10) ("htimerRoot: AID-0x%x\n",timer->objectEntryAID->aid);
58 timer->func(d,timer->objectEntryAID,pact_time);
60 debug(2,10) ("htimerRoot: finished\n");
63 /*****************************************************************************/
64 UL_HTIMER_IMP(htimerUnicastCommon, \
71 htimerUnicastCommon_update_root_timer(ObjectEntry *objectEntry,
72 ObjectEntryAID *objectEntryAID) {
75 //if first item is changed -> update root timer queue
76 if (htimerUnicastCommon_first_changed(objectEntryAID)) {
77 //deatach old timer from this queue (if any)
78 htimerRoot_detach(objectEntry,
79 &objectEntryAID->htimUnicast.commonNode);
80 if (htimerUnicastCommon_next_expire(objectEntryAID,&nextExpire)!=0) {
82 objectEntryAID->htimUnicast.commonNode.func=
83 htimerUnicastCommon_run_expired;
84 objectEntryAID->htimUnicast.commonNode.objectEntryAID=
86 htimerRoot_set_expire(
87 &objectEntryAID->htimUnicast.commonNode,
89 //insert new timer to root htimer node
90 htimerRoot_add(objectEntry,
91 &objectEntryAID->htimUnicast.commonNode);
92 debug(2,10) ("htimerUnicastCommon: root updated, wakeup\n");
93 //wake-up sending thread to process event
94 ORTEDomainWakeUpSendingThread(objectEntry);
100 htimerUnicastCommon_run_expired(ORTEDomain *d,
101 ObjectEntryAID *objectEntryAID,
102 ul_htim_time_t *pact_time) {
103 HTimFncUserNode *timer;
105 pthread_rwlock_t *lock;
107 while((timer=htimerUnicastCommon_cut_expired(objectEntryAID, pact_time))){
108 if ((lock=timer->lock)) //after proc. timer->func can be timer freed
109 pthread_rwlock_wrlock(timer->lock);
110 debug(2,10) ("htimerUnicastCommon: %s\n",
112 retValue=timer->func(d,timer->arg1);
114 pthread_rwlock_unlock(timer->lock);
115 if (retValue==2) //object deleted
118 htimerUnicastCommon_update_root_timer(&d->objectEntry,objectEntryAID);
121 /*****************************************************************************/
122 UL_HTIMER_IMP(htimerUnicastSendMetatraffic, \
125 htimUnicast.sendMetatraffic, \
129 htimerUnicastSendMetatraffic_update_root_timer(ObjectEntry *objectEntry,
130 ObjectEntryAID *objectEntryAID) {
133 //if first item is changed -> update root timer queue
134 if (htimerUnicastSendMetatraffic_first_changed(objectEntryAID)) {
135 //deatach old timer from this queue (if any)
136 htimerRoot_detach(objectEntry,
137 &objectEntryAID->htimUnicast.sendMetatrafficNode);
138 if (htimerUnicastSendMetatraffic_next_expire(objectEntryAID,&nextExpire)!=0) {
140 objectEntryAID->htimUnicast.sendMetatrafficNode.func=
141 htimerUnicastSendMetatraffic_run_expired;
142 objectEntryAID->htimUnicast.sendMetatrafficNode.objectEntryAID=
144 htimerRoot_set_expire(
145 &objectEntryAID->htimUnicast.sendMetatrafficNode,
147 //insert new timer to root htimer node
148 htimerRoot_add(objectEntry,
149 &objectEntryAID->htimUnicast.sendMetatrafficNode);
150 debug(2,10) ("htimerUnicastMetatraffic: root updated, wakeup\n");
151 //wake-up sending thread to process event
152 ORTEDomainWakeUpSendingThread(objectEntry);
158 htimerUnicastSendMetatraffic_run_expired(ORTEDomain *d,
159 ObjectEntryAID *objectEntryAID,
160 ul_htim_time_t *pact_time) {
161 HTimFncUserNode *timer;
164 while((timer=htimerUnicastSendMetatraffic_cut_expired(objectEntryAID, pact_time))){
166 pthread_rwlock_wrlock(timer->lock);
167 debug(2,10) ("htimerUnicastMetatraffic: %s\n",
169 retValue=timer->func(d,timer->arg1);
170 while (d->taskSend.mb.needSend) {
171 ORTESendData(d,objectEntryAID,ORTE_TRUE);
172 timer->func(d,timer->arg1);
175 pthread_rwlock_unlock(timer->lock);
177 htimerUnicastSendMetatraffic_update_root_timer(&d->objectEntry,objectEntryAID);
178 if (d->taskSend.mb.cdrCodec.wptr>RTPS_HEADER_LENGTH) {
179 ORTESendData(d,objectEntryAID,ORTE_TRUE);
183 /*****************************************************************************/
184 UL_HTIMER_IMP(htimerUnicastSendUserData, \
187 htimUnicast.sendUserData, \
191 htimerUnicastSendUserData_update_root_timer(ObjectEntry *objectEntry,
192 ObjectEntryAID *objectEntryAID) {
195 //if first item is changed -> update root timer queue
196 if (htimerUnicastSendUserData_first_changed(objectEntryAID)) {
197 //deatach old timer from this queue (if any)
198 htimerRoot_detach(objectEntry,
199 &objectEntryAID->htimUnicast.sendUserDataNode);
200 if (htimerUnicastSendUserData_next_expire(objectEntryAID,&nextExpire)!=0) {
202 objectEntryAID->htimUnicast.sendUserDataNode.func=
203 htimerUnicastSendUserData_run_expired;
204 objectEntryAID->htimUnicast.sendUserDataNode.objectEntryAID=
206 htimerRoot_set_expire(
207 &objectEntryAID->htimUnicast.sendUserDataNode,
209 //insert new timer to root htimer node
210 htimerRoot_add(objectEntry,
211 &objectEntryAID->htimUnicast.sendUserDataNode);
212 debug(2,10) ("htimerUnicastUserdata: root updated, wakeup\n");
213 //wake-up sending thread to process event
214 ORTEDomainWakeUpSendingThread(objectEntry);
220 htimerUnicastSendUserData_run_expired(ORTEDomain *d,
221 ObjectEntryAID *objectEntryAID,
222 ul_htim_time_t *pact_time) {
223 HTimFncUserNode *timer;
226 while((timer=htimerUnicastSendUserData_cut_expired(objectEntryAID, pact_time))){
228 pthread_rwlock_wrlock(timer->lock);
229 retValue=timer->func(d,timer->arg1);
230 while (d->taskSend.mb.needSend) {
231 ORTESendData(d,objectEntryAID,ORTE_FALSE);
232 timer->func(d,timer->arg1);
235 pthread_rwlock_unlock(timer->lock);
237 htimerUnicastSendUserData_update_root_timer(&d->objectEntry,objectEntryAID);
238 if (d->taskSend.mb.cdrCodec.wptr>RTPS_HEADER_LENGTH) {
239 ORTESendData(d,objectEntryAID,ORTE_FALSE);
243 /*********************************************************************/
245 getActualNtpTime(void) {
247 #ifndef CONFIG_ORTE_RT
250 gettimeofday(&time,NULL);
251 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
252 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
254 struct timespec time;
256 clock_gettime(CLOCK_REALTIME, &time);
257 time.tv_nsec/=1000; //conver to us
258 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_nsec);