2 * $Id: htimerNtp.c,v 0.0.0.1 2003/08/21
4 * DEBUG: section 2 HTimer for NtpTime
5 * AUTHOR: Petr Smolik petr.smolik@wo.cz
7 * ORTE - OCERA Real-Time Ethernet http://www.ocera.org/
8 * --------------------------------------------------------------------
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.
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.
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)
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)
32 /*****************************************************************************/
33 UL_HTIMER_IMP(htimerRoot, \
39 htimerRoot_run_expired(ORTEDomain *d,
40 ul_htim_time_t *pact_time) {
41 HTimFncRootNode *timer;
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);
48 debug(2,10) ("htimerRoot: finished\n");
51 /*****************************************************************************/
52 UL_HTIMER_IMP(htimerUnicastCommon, \
59 htimerUnicastCommon_update_root_timer(ObjectEntry *objectEntry,
60 ObjectEntryAID *objectEntryAID) {
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) {
70 objectEntryAID->htimUnicast.commonNode.func=
71 htimerUnicastCommon_run_expired;
72 objectEntryAID->htimUnicast.commonNode.objectEntryAID=
74 htimerRoot_set_expire(
75 &objectEntryAID->htimUnicast.commonNode,
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);
88 htimerUnicastCommon_run_expired(ORTEDomain *d,
89 ObjectEntryAID *objectEntryAID,
90 ul_htim_time_t *pact_time) {
91 HTimFncUserNode *timer;
93 pthread_rwlock_t *lock;
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",
100 retValue=timer->func(d,timer->arg1);
102 pthread_rwlock_unlock(timer->lock);
103 if (retValue==2) //object deleted
106 htimerUnicastCommon_update_root_timer(&d->objectEntry,objectEntryAID);
109 /*****************************************************************************/
110 UL_HTIMER_IMP(htimerUnicastSendMetatraffic, \
113 htimUnicast.sendMetatraffic, \
117 htimerUnicastSendMetatraffic_update_root_timer(ObjectEntry *objectEntry,
118 ObjectEntryAID *objectEntryAID) {
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) {
128 objectEntryAID->htimUnicast.sendMetatrafficNode.func=
129 htimerUnicastSendMetatraffic_run_expired;
130 objectEntryAID->htimUnicast.sendMetatrafficNode.objectEntryAID=
132 htimerRoot_set_expire(
133 &objectEntryAID->htimUnicast.sendMetatrafficNode,
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);
146 htimerUnicastSendMetatraffic_run_expired(ORTEDomain *d,
147 ObjectEntryAID *objectEntryAID,
148 ul_htim_time_t *pact_time) {
149 HTimFncUserNode *timer;
152 while((timer=htimerUnicastSendMetatraffic_cut_expired(objectEntryAID, pact_time))){
154 pthread_rwlock_wrlock(timer->lock);
155 debug(2,10) ("htimerUnicastMetatraffic: %s\n",
157 retValue=timer->func(d,timer->arg1);
158 while (d->mbSend.needSend) {
159 ORTESendData(d,objectEntryAID,ORTE_TRUE);
160 timer->func(d,timer->arg1);
163 pthread_rwlock_unlock(timer->lock);
165 htimerUnicastSendMetatraffic_update_root_timer(&d->objectEntry,objectEntryAID);
166 if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
167 ORTESendData(d,objectEntryAID,ORTE_TRUE);
171 /*****************************************************************************/
172 UL_HTIMER_IMP(htimerUnicastSendUserData, \
175 htimUnicast.sendUserData, \
179 htimerUnicastSendUserData_update_root_timer(ObjectEntry *objectEntry,
180 ObjectEntryAID *objectEntryAID) {
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) {
190 objectEntryAID->htimUnicast.sendUserDataNode.func=
191 htimerUnicastSendUserData_run_expired;
192 objectEntryAID->htimUnicast.sendUserDataNode.objectEntryAID=
194 htimerRoot_set_expire(
195 &objectEntryAID->htimUnicast.sendUserDataNode,
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);
208 htimerUnicastSendUserData_run_expired(ORTEDomain *d,
209 ObjectEntryAID *objectEntryAID,
210 ul_htim_time_t *pact_time) {
211 HTimFncUserNode *timer;
214 while((timer=htimerUnicastSendUserData_cut_expired(objectEntryAID, pact_time))){
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);
223 pthread_rwlock_unlock(timer->lock);
225 htimerUnicastSendUserData_update_root_timer(&d->objectEntry,objectEntryAID);
226 if (d->mbSend.cdrStream.length>RTPS_HEADER_LENGTH) {
227 ORTESendData(d,objectEntryAID,ORTE_FALSE);
231 /*********************************************************************/
233 getActualNtpTime(void) {
235 #ifndef CONFIG_ORTE_RT
238 gettimeofday(&time,NULL);
239 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
240 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_usec);
242 struct timespec time;
244 clock_gettime(CLOCK_REALTIME, &time);
245 time.tv_nsec/=1000; //conver to us
246 NtpTimeAssembFromUs(result,time.tv_sec,time.tv_nsec);