hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2008-2010 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 * Watchdog support.
 *
 * Not shareable (we can't tell whether the handler would always be
 * available in all processes).
 */
 
#include <errno.h>
#include <stdlib.h>
#include <memory.h>
#include <copperplate/heapobj.h>
#include <copperplate/threadobj.h>
#include <vxworks/errnoLib.h>
#include "wdLib.h"
#include "tickLib.h"
 
#define wd_magic    0x3a4b5c6d
 
static struct wind_wd *get_wd(WDOG_ID wdog_id)
{
   struct wind_wd *wd = (struct wind_wd *)wdog_id;
 
   if (wd == NULL || ((intptr_t)wd & (sizeof(intptr_t)-1)) != 0)
       return NULL;
 
   if (wd->magic != wd_magic)
       return NULL;
 
   if (timerobj_lock(&wd->tmobj))
       return NULL;
 
   return wd->magic != wd_magic ? NULL : wd;
}
 
static inline void put_wd(struct wind_wd *wd)
{
   timerobj_unlock(&wd->tmobj);
}
 
static void watchdog_handler(struct timerobj *tmobj)
{
   struct wind_wd *wd = container_of(tmobj, struct wind_wd, tmobj);
   wd->handler(wd->arg);
}
 
WDOG_ID wdCreate(void)
{
   struct wind_wd *wd;
   struct service svc;
   int ret;
 
   CANCEL_DEFER(svc);
 
   wd = pvmalloc(sizeof(*wd));
   if (wd == NULL)
       goto fail;
 
   ret = timerobj_init(&wd->tmobj);
   if (ret) {
       pvfree(wd);
   fail:
       errno = S_memLib_NOT_ENOUGH_MEMORY;
       wd = NULL;
       goto out;
   }
 
   wd->magic = wd_magic;
out:
   CANCEL_RESTORE(svc);
 
   return (WDOG_ID)wd;
}
 
STATUS wdDelete(WDOG_ID wdog_id)
{
   struct wind_wd *wd;
   struct service svc;
   int ret = OK;
 
   CANCEL_DEFER(svc);
 
   wd = get_wd(wdog_id);
   if (wd == NULL) {
       errno = S_objLib_OBJ_ID_ERROR;
       ret = ERROR;
       goto out;
   }
 
   timerobj_destroy(&wd->tmobj);
   wd->magic = ~wd_magic;
   pvfree(wd);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
STATUS wdStart(WDOG_ID wdog_id, int delay, void (*handler)(long), long arg)
{
   struct itimerspec it;
   struct service svc;
   struct wind_wd *wd;
   int ret;
 
   CANCEL_DEFER(svc);
 
   wd = get_wd(wdog_id);
   if (wd == NULL)
       goto objid_error;
 
   wd->handler = handler;
   wd->arg = arg;
 
   clockobj_ticks_to_timeout(&wind_clock, delay, &it.it_value);
   it.it_interval.tv_sec = 0;
   it.it_interval.tv_nsec = 0;
   ret = timerobj_start(&wd->tmobj, watchdog_handler, &it);
   if (ret) {
   objid_error:
       CANCEL_RESTORE(svc);
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
   CANCEL_RESTORE(svc);
 
   return OK;
}
 
STATUS wdCancel(WDOG_ID wdog_id)
{
   struct wind_wd *wd;
   struct service svc;
   int ret;
 
   CANCEL_DEFER(svc);
 
   wd = get_wd(wdog_id);
   if (wd == NULL)
       goto objid_error;
 
   ret = timerobj_stop(&wd->tmobj);
   if (ret) {
   objid_error:
       CANCEL_RESTORE(svc);
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
   CANCEL_RESTORE(svc);
 
   return OK;
}