Directory: | ./ |
---|---|
File: | pdserv-1.1.0/src/lib/interface.cpp |
Date: | 2024-12-15 04:08:34 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 47 | 73 | 64.4% |
Branches: | 8 | 34 | 23.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************** | ||
2 | * | ||
3 | * $Id$ | ||
4 | * | ||
5 | * Copyright 2010 Richard Hacker (lerichi at gmx dot net) | ||
6 | * | ||
7 | * This file is part of the pdserv library. | ||
8 | * | ||
9 | * The pdserv library is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU Lesser General Public License as published | ||
11 | * by the Free Software Foundation, either version 3 of the License, or (at | ||
12 | * your option) any later version. | ||
13 | * | ||
14 | * The pdserv library is distributed in the hope that it will be useful, but | ||
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
17 | * License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public License | ||
20 | * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | *****************************************************************************/ | ||
23 | |||
24 | #include "Main.h" | ||
25 | #include "Task.h" | ||
26 | #include "Signal.h" | ||
27 | #include "Event.h" | ||
28 | #include "Parameter.h" | ||
29 | #include "../DataType.h" | ||
30 | #include "pdserv.h" | ||
31 | |||
32 | typedef std::vector<PdServ::DataType*> CompoundVector; | ||
33 | 1 | CompoundVector compoundType; | |
34 | |||
35 | static const PdServ::DataType& getDataType(int dt); | ||
36 | |||
37 | ///////////////////////////////////////////////////////////////////////////// | ||
38 | 10 | struct pdserv* pdserv_create( const char *name, const char *version, | |
39 | int (*gettime)(struct timespec*)) | ||
40 | { | ||
41 | return reinterpret_cast<struct pdserv*>( | ||
42 |
1/2✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
10 | new Main(name, version, gettime)); |
43 | } | ||
44 | |||
45 | ///////////////////////////////////////////////////////////////////////////// | ||
46 | 10 | void pdserv_config_file( struct pdserv* pdserv, const char *name) | |
47 | { | ||
48 | 10 | reinterpret_cast<Main*>(pdserv)->setConfigFile(name); | |
49 | 10 | } | |
50 | |||
51 | ///////////////////////////////////////////////////////////////////////////// | ||
52 | 20 | struct pdtask* pdserv_create_task(struct pdserv* pdserv, double tsample, | |
53 | const char *name) | ||
54 | { | ||
55 | return reinterpret_cast<struct pdtask*>( | ||
56 | 20 | reinterpret_cast<Main*>(pdserv)->addTask(tsample, name)); | |
57 | } | ||
58 | |||
59 | ///////////////////////////////////////////////////////////////////////////// | ||
60 | ✗ | int pdserv_create_compound( const char *name, size_t size) | |
61 | { | ||
62 | ✗ | int dt = pd_datatype_end + compoundType.size(); | |
63 | ✗ | compoundType.push_back(new PdServ::DataType(name, size)); | |
64 | ✗ | return dt; | |
65 | } | ||
66 | |||
67 | ///////////////////////////////////////////////////////////////////////////// | ||
68 | ✗ | void pdserv_compound_add_field(int compound, const char *name, | |
69 | int data_type, size_t offset, size_t ndims, const size_t *dim) | ||
70 | { | ||
71 | ✗ | compoundType[compound - pd_datatype_end] | |
72 | ✗ | ->addField(name, getDataType(data_type), offset, ndims, dim); | |
73 | } | ||
74 | |||
75 | ///////////////////////////////////////////////////////////////////////////// | ||
76 | 10 | void pdserv_exit(struct pdserv* pdserv) | |
77 | { | ||
78 | 10 | for (CompoundVector::const_iterator it = compoundType.begin(); | |
79 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
|
10 | it != compoundType.end(); ++it) |
80 | ✗ | delete *it; | |
81 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | delete reinterpret_cast<Main*>(pdserv); |
82 | 10 | } | |
83 | |||
84 | ///////////////////////////////////////////////////////////////////////////// | ||
85 | 1241 | void pdserv_get_parameters(struct pdserv* pdserv, struct pdtask *task, | |
86 | const struct timespec *t) | ||
87 | { | ||
88 | 1241 | reinterpret_cast<Main*>(pdserv)->getParameters( | |
89 | reinterpret_cast<Task*>(task), t); | ||
90 | 1241 | } | |
91 | |||
92 | ///////////////////////////////////////////////////////////////////////////// | ||
93 | ✗ | void pdserv_update_statistics(struct pdtask* task, | |
94 | double exec_time, double cycle_time, unsigned int overrun) | ||
95 | { | ||
96 | ✗ | reinterpret_cast<Task*>(task)->updateStatistics( | |
97 | exec_time, cycle_time, overrun); | ||
98 | } | ||
99 | |||
100 | ///////////////////////////////////////////////////////////////////////////// | ||
101 | 1308 | void pdserv_update(struct pdtask* task, const struct timespec *t) | |
102 | { | ||
103 | 1308 | reinterpret_cast<Task*>(task)->update(t); | |
104 | 1308 | } | |
105 | |||
106 | ///////////////////////////////////////////////////////////////////////////// | ||
107 | 120 | static const PdServ::DataType& getDataType(int dt) | |
108 | { | ||
109 |
4/12✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 60 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 30 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
120 | switch (dt) { |
110 | ✗ | case pd_boolean_T: return PdServ::DataType::boolean; | |
111 | ✗ | case pd_uint8_T: return PdServ::DataType::uint8; | |
112 | ✗ | case pd_uint16_T: return PdServ::DataType::uint16; | |
113 | 20 | case pd_uint32_T: return PdServ::DataType::uint32; | |
114 | ✗ | case pd_uint64_T: return PdServ::DataType::uint64; | |
115 | ✗ | case pd_sint8_T: return PdServ::DataType::int8; | |
116 | 10 | case pd_sint16_T: return PdServ::DataType::int16; | |
117 | 60 | case pd_sint32_T: return PdServ::DataType::int32; | |
118 | ✗ | case pd_sint64_T: return PdServ::DataType::int64; | |
119 | 30 | case pd_double_T: return PdServ::DataType::float64; | |
120 | ✗ | case pd_single_T: return PdServ::DataType::float32; | |
121 | ✗ | default: | |
122 | ✗ | return *compoundType[dt - pd_datatype_end]; | |
123 | } | ||
124 | } | ||
125 | |||
126 | ///////////////////////////////////////////////////////////////////////////// | ||
127 | 20 | const struct pdevent *pdserv_event (struct pdserv* pdserv, const char *path, | |
128 | int prio, size_t n, const char **message) | ||
129 | { | ||
130 | 20 | Main *main = reinterpret_cast<Main*>(pdserv); | |
131 | |||
132 | 20 | Event *e = main->addEvent(path, prio, n, message); | |
133 | |||
134 | 20 | return reinterpret_cast<const struct pdevent *>(e); | |
135 | } | ||
136 | |||
137 | ///////////////////////////////////////////////////////////////////////////// | ||
138 | 2 | void pdserv_event_set(const struct pdevent *event, | |
139 | size_t element, char state, const timespec *t) | ||
140 | { | ||
141 | 2 | reinterpret_cast<const Event*>(event)->set(element, state != 0, t); | |
142 | 2 | } | |
143 | |||
144 | ///////////////////////////////////////////////////////////////////////////// | ||
145 | 60 | struct pdvariable *pdserv_signal( | |
146 | struct pdtask* pdtask, | ||
147 | unsigned int decimation, | ||
148 | const char *path, | ||
149 | int datatype, | ||
150 | const void *addr, | ||
151 | size_t n, | ||
152 | const size_t *dim | ||
153 | ) | ||
154 | { | ||
155 | 60 | Task *task = reinterpret_cast<Task*>(pdtask); | |
156 | |||
157 | 60 | PdServ::Variable *v = task->main->addSignal( | |
158 | 60 | task, decimation, path, getDataType(datatype), addr, n, dim); | |
159 | |||
160 | 60 | return reinterpret_cast<struct pdvariable *>(v); | |
161 | } | ||
162 | |||
163 | ///////////////////////////////////////////////////////////////////////////// | ||
164 | 60 | struct pdvariable *pdserv_parameter( | |
165 | struct pdserv* pdserv, | ||
166 | const char *path, | ||
167 | unsigned int mode, | ||
168 | int datatype, | ||
169 | void *addr, | ||
170 | size_t n, | ||
171 | const size_t *dim, | ||
172 | paramtrigger_t trigger = 0, | ||
173 | void *priv_data = 0 | ||
174 | ) | ||
175 | { | ||
176 | 60 | Main *main = reinterpret_cast<Main*>(pdserv); | |
177 | |||
178 | 60 | Parameter *p = main->addParameter( | |
179 | 60 | path, mode, getDataType(datatype), addr, n, dim); | |
180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (trigger) |
181 | ✗ | p->trigger = trigger; | |
182 | 60 | p->priv_data = priv_data; | |
183 | |||
184 | return reinterpret_cast<struct pdvariable *> | ||
185 | 60 | (static_cast<PdServ::Variable*>(p)); | |
186 | } | ||
187 | |||
188 | ///////////////////////////////////////////////////////////////////////////// | ||
189 | ✗ | void pdserv_set_alias(struct pdvariable *var, const char *alias) | |
190 | { | ||
191 | ✗ | reinterpret_cast<PdServ::Variable*>(var)->alias = alias; | |
192 | } | ||
193 | |||
194 | ///////////////////////////////////////////////////////////////////////////// | ||
195 | ✗ | void pdserv_set_unit(struct pdvariable *var, const char *unit) | |
196 | { | ||
197 | ✗ | reinterpret_cast<PdServ::Variable*>(var)->unit = unit; | |
198 | } | ||
199 | |||
200 | ///////////////////////////////////////////////////////////////////////////// | ||
201 | ✗ | void pdserv_set_comment(struct pdvariable *var, const char *comment) | |
202 | { | ||
203 | ✗ | reinterpret_cast<PdServ::Variable*>(var)->comment = comment; | |
204 | } | ||
205 | |||
206 | ///////////////////////////////////////////////////////////////////////////// | ||
207 | 10 | int pdserv_prepare(struct pdserv* pdserv) | |
208 | { | ||
209 | 10 | return reinterpret_cast<Main*>(pdserv)->run(); | |
210 | 3 | } | |
211 |