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, /* prefix */
37 ul_htim_queue_t, /* root_t */
38 ul_htim_node_t, /* item_t */
39 ul_htim_time_t, /* key_t */
40 timers, /* root_field */
42 expires, /* item_key */
43 ul_htimer_cmp_fnc) /* cmp_fnc */
46 GAVL_FLES_INT_IMP(ul_htim_queue, ul_htim_queue_t, ul_htim_node_t, ul_htim_time_t,
47 timers, node, expires, ul_htimer_cmp_fnc, GAVL_FAFTER,
48 root->first_changed=1,;,root->first_changed=1)
50 /*****************************************************************************/
51 UL_HTIMER_IMP(htimerRoot, \
57 htimerRoot_run_expired(ORTEDomain *d,
58 ul_htim_time_t *pact_time) {
59 HTimFncRootNode *timer;
61 debug(2,10) ("htimerRoot: start\n");
62 while((timer=htimerRoot_cut_expired(&d->objectEntry, pact_time))){
63 debug(2,10) ("htimerRoot: AID-0x%x\n",timer->objectEntryAID->aid);
64 timer->func(d,timer->objectEntryAID,pact_time);
66 debug(2,10) ("htimerRoot: finished\n");
69 /*****************************************************************************/
70 UL_HTIMER_IMP(htimerUnicastCommon, \
77 htimerUnicastCommon_update_root_timer(ObjectEntry *objectEntry,
78 ObjectEntryAID *objectEntryAID) {
81 //if first item is changed -> update root timer queue
82 if (htimerUnicastCommon_first_changed(objectEntryAID)) {
83 //deatach old timer from this queue (if any)
84 htimerRoot_detach(objectEntry,
85 &objectEntryAID->htimUnicast.commonNode);
86 if (htimerUnicastCommon_next_expire(objectEntryAID,&nextExpire)!=0) {
88 objectEntryAID->htimUnicast.commonNode.func=
89 htimerUnicastCommon_run_expired;
90 objectEntryAID->htimUnicast.commonNode.objectEntryAID=
92 htimerRoot_set_expire(
93 &objectEntryAID->htimUnicast.commonNode,
95 //insert new timer to root htimer node
96 htimerRoot_add(objectEntry,
97 &objectEntryAID->htimUnicast.commonNode);
98 debug(2,10) ("htimerUnicastCommon: root updated, wakeup\n");
99 //wake-up sending thread to process event
100 ORTEDomainWakeUpSendingThread(objectEntry);
106 htimerUnicastCommon_run_expired(ORTEDomain *d,
107 ObjectEntryAID *objectEntryAID,
108 ul_htim_time_t *pact_time) {
109 HTimFncUserNode *timer;
111 pthread_rwlock_t *lock;
113 while((timer=htimerUnicastCommon_cut_expired(objectEntryAID, pact_time))){
114 if ((lock=timer->lock)) //after proc. timer->func can be timer freed
115 pthread_rwlock_wrlock(timer->lock);
116 debug(2,10) ("htimerUnicastCommon: %s\n",
118 retValue=timer->func(d,timer->arg1);
120 pthread_rwlock_unlock(timer->lock);
121 if (retValue==2) //object deleted
124 htimerUnicastCommon_update_root_timer(&d->objectEntry,objectEntryAID);
127 /*****************************************************************************/
128 UL_HTIMER_IMP(htimerUnicastSendMetatraffic, \
131 htimUnicast.sendMetatraffic, \
135 htimerUnicastSendMetatraffic_update_root_timer(ObjectEntry *objectEntry,
136 ObjectEntryAID *objectEntryAID) {
139 //if first item is changed -> update root timer queue
140 if (htimerUnicastSendMetatraffic_first_changed(objectEntryAID)) {
141 //deatach old timer from this queue (if any)
142 htimerRoot_detach(objectEntry,
143 &objectEntryAID->htimUnicast.sendMetatrafficNode);
144 if (htimerUnicastSendMetatraffic_next_expire(objectEntryAID,&nextExpire)!=0) {
146 objectEntryAID->htimUnicast.sendMetatrafficNode.func=
147 htimerUnicastSendMetatraffic_run_expired;
148 objectEntryAID->htimUnicast.sendMetatrafficNode.objectEntryAID=
150 htimerRoot_set_expire(
151 &objectEntryAID->htimUnicast.sendMetatrafficNode,
153 //insert new timer to root htimer node
154 htimerRoot_add(objectEntry,
155 &objectEntryAID->htimUnicast.sendMetatrafficNode);
156 debug(2,10) ("htimerUnicastMetatraffic: root updated, wakeup\n");
157 //wake-up sending thread to process event
158 ORTEDomainWakeUpSendingThread(objectEntry);
164 htimerUnicastSendMetatraffic_run_expired(ORTEDomain *d,
165 ObjectEntryAID *objectEntryAID,
166 ul_htim_time_t *pact_time) {
167 HTimFncUserNode *timer;
170 while((timer=htimerUnicastSendMetatraffic_cut_expired(objectEntryAID, pact_time))){
172 pthread_rwlock_wrlock(timer->lock);
173 debug(2,10) ("htimerUnicastMetatraffic: %s\n",
175 retValue=timer->func(d,timer->arg1);
176 while (d->taskSend.mb.needSend) {
177 ORTESendData(d,objectEntryAID,ORTE_TRUE);
178 timer->func(d,timer->arg1);
181 pthread_rwlock_unlock(timer->lock);
183 htimerUnicastSendMetatraffic_update_root_timer(&d->objectEntry,objectEntryAID);
184 if (d->taskSend.mb.cdrCodec.wptr>RTPS_HEADER_LENGTH) {
185 ORTESendData(d,objectEntryAID,ORTE_TRUE);
189 /*****************************************************************************/
190 UL_HTIMER_IMP(htimerUnicastSendUserData, \
193 htimUnicast.sendUserData, \
197 htimerUnicastSendUserData_update_root_timer(ObjectEntry *objectEntry,
198 ObjectEntryAID *objectEntryAID) {
201 //if first item is changed -> update root timer queue
202 if (htimerUnicastSendUserData_first_changed(objectEntryAID)) {
203 //deatach old timer from this queue (if any)
204 htimerRoot_detach(objectEntry,
205 &objectEntryAID->htimUnicast.sendUserDataNode);
206 if (htimerUnicastSendUserData_next_expire(objectEntryAID,&nextExpire)!=0) {
208 objectEntryAID->htimUnicast.sendUserDataNode.func=
209 htimerUnicastSendUserData_run_expired;
210 objectEntryAID->htimUnicast.sendUserDataNode.objectEntryAID=
212 htimerRoot_set_expire(
213 &objectEntryAID->htimUnicast.sendUserDataNode,
215 //insert new timer to root htimer node
216 htimerRoot_add(objectEntry,
217 &objectEntryAID->htimUnicast.sendUserDataNode);
218 debug(2,10) ("htimerUnicastUserdata: root updated, wakeup\n");
219 //wake-up sending thread to process event
220 ORTEDomainWakeUpSendingThread(objectEntry);
226 htimerUnicastSendUserData_run_expired(ORTEDomain *d,
227 ObjectEntryAID *objectEntryAID,
228 ul_htim_time_t *pact_time) {
229 HTimFncUserNode *timer;
232 while((timer=htimerUnicastSendUserData_cut_expired(objectEntryAID, pact_time))){
234 pthread_rwlock_wrlock(timer->lock);
235 retValue=timer->func(d,timer->arg1);
236 while (d->taskSend.mb.needSend) {
237 ORTESendData(d,objectEntryAID,ORTE_FALSE);
238 timer->func(d,timer->arg1);
241 pthread_rwlock_unlock(timer->lock);
243 htimerUnicastSendUserData_update_root_timer(&d->objectEntry,objectEntryAID);
244 if (d->taskSend.mb.cdrCodec.wptr>RTPS_HEADER_LENGTH) {
245 ORTESendData(d,objectEntryAID,ORTE_FALSE);
249 /*********************************************************************/
251 getActualNtpTime(void) {
253 #ifndef CONFIG_ORTE_RT
256 gettimeofday(&time,NULL);
257 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
258 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
260 struct timespec time;
262 clock_gettime(CLOCK_REALTIME, &time);
263 time.tv_nsec/=1000; //conver to us
264 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_nsec);