-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathco_od.h
299 lines (276 loc) · 7.5 KB
/
co_od.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2017 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
/**
* @file
* @brief Object dictionary interface
*/
#ifndef CO_OD_H
#define CO_OD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "co_api.h"
#include "co_main.h"
#include "co_sdo.h"
/**
* Traverse and call function on all subindexes in an object.
*
* This function traverses all subindexes until \a fn returns a value
* other than 0.
*
* This function can e.g. be used to search for an entry.
*
* @param net network handle
* @param obj object descriptor
* @param fn function to call
* @param arg opaque argument for function
* @param max_subindex max subindex for traversal
*
* @return entry descriptor for subindex where \a fn != 0, or NULL
*/
static inline const co_entry_t * co_obj_traverse (
co_net_t * net,
const co_obj_t * obj,
int (*fn) (
co_net_t * net,
const co_obj_t * obj,
const co_entry_t * entry,
uintptr_t arg),
uintptr_t arg,
uint8_t max_subindex)
{
const co_entry_t * entry = obj->entries;
uint8_t subindex;
do
{
subindex = entry->subindex;
if (fn (net, obj, entry, arg) != 0)
return entry;
entry++;
} while (subindex < max_subindex);
return NULL;
}
/**
* Traverse and call function with state on all subindexes in an
* object.
*
* This function traverses all subindexes and calls \a fn supplying
* the current state.
*
* This function can e.g. be used to compute a property of the object,
* such as the total bit-length of all subindexes.
*
* @param net network handle
* @param obj object descriptor
* @param fn function to call
* @param arg opaque argument for function
* @param state initial state
* @param max_subindex max subindex for traversal
*
* @return final state
*/
static inline int co_obj_reduce (
co_net_t * net,
const co_obj_t * obj,
int (*fn) (co_net_t * net, const co_entry_t * entry, uintptr_t arg, int state),
uintptr_t arg,
int state,
uint8_t max_subindex)
{
const co_entry_t * entry = obj->entries;
uint8_t subindex;
do
{
subindex = entry->subindex;
state = fn (net, entry, arg, state);
entry++;
} while (subindex < max_subindex);
return state;
}
/**
* Set dictionary default values
*
* This function sets the default value for dictionary entries that
* have default values. Only indices that are within the given minimum
* and maximum values are considered.
*
* @param net network handle
* @param min minimum index
* @param max maximum index
*/
void co_od_set_defaults (co_net_t * net, uint16_t min, uint16_t max);
/**
* Zero dictionary values
*
* This function sets dictionary entries to zero. Only indices that
* are within the given minimum and maximum values are considered.
*
* @param net network handle
* @param min minimum index
* @param max maximum index
*/
void co_od_zero (co_net_t * net, uint16_t min, uint16_t max);
/**
* Load dictionary from store
*
* This function loads dictionary values from a store.
*
* @param net network handle
* @param store store identifier
*
* @return sdo abort code
*/
uint32_t co_od_load (co_net_t * net, co_store_t store);
/**
* Reset dictionary
*
* This function resets dictionary values related to the given
* store. Only indices that are within the minimum and maximum values
* are considered. The following operations will be performed:
*
* -# Zero values
* -# Set default values
* -# Load stored values
*
* @param net network handle
* @param store store identifier
* @param min minimum index
* @param max maximum index
*/
void co_od_reset (co_net_t * net, co_store_t store, uint16_t min, uint16_t max);
/**
* Save dictionary in store
*
* This function saves dictionary values in a store. Only indices that
* are within the minimum and maximum values are considered.
*
* @param net network handle
* @param store store identifier
* @param min minimum index
* @param max maximum index
*
* @return sdo abort code
*/
uint32_t co_od_store (co_net_t * net, co_store_t store, uint16_t min, uint16_t max);
/**
* Find object in dictionary
*
* This function finds the object with the given index.
*
* @param net network handle
* @param index index to find
*
* @return object descriptor, or NULL if not found
*/
const co_obj_t * co_obj_find (co_net_t * net, uint16_t index);
/**
* Find entry in object
*
* This function finds the entry descriptor with the given subindex.
*
* @param net network handle
* @param obj object descriptor
* @param subindex subindex to find
*
* @return entry descriptor, or NULL if not found
*/
const co_entry_t * co_entry_find (
co_net_t * net,
const co_obj_t * obj,
uint8_t subindex);
/**
* Get subindex pointer
*
* This function returns a pointer to the subindex value. Note that
* accessing the value in this manner is not thread-safe.
*
* @param net network handle
* @param obj object descriptor
* @param entry entry descriptor
* @param subindex subindex
* @param ptr result on success
*
* @return 0 or CO_SDO_ABORT_GENERAL if object has no storage
*/
uint32_t co_od_get_ptr (
co_net_t * net,
const co_obj_t * obj,
const co_entry_t * entry,
uint8_t subindex,
uint8_t ** ptr);
/**
* Get subindex value
*
* This function returns the subindex value. The value is read
* atomically. This function is thread-safe.
*
* @param net network handle
* @param obj object descriptor
* @param entry entry descriptor
* @param subindex subindex
* @param value value on success
*
* @return 0 or sdo abort code
*/
uint32_t co_od_get_value (
co_net_t * net,
const co_obj_t * obj,
const co_entry_t * entry,
uint8_t subindex,
uint64_t * value);
/**
* Set subindex value
*
* This function sets the subindex value. The value is written
* atomically. This function is thread-safe.
*
* @param net network handle
* @param obj object descriptor
* @param entry entry descriptor
* @param subindex subindex
* @param value value to set
*
* @return 0 or sdo abort code
*/
uint32_t co_od_set_value (
co_net_t * net,
const co_obj_t * obj,
const co_entry_t * entry,
uint8_t subindex,
uint64_t value);
/**
* Trigger notification callback
*
* This functions triggers the notification callback of the subindex,
* if any.
*
* @param net network handle
* @param obj object descriptor
* @param entry entry descriptor
* @param subindex subindex
* @param event event type
* @param value optional value
*/
void co_od_notify (
co_net_t * net,
const co_obj_t * obj,
const co_entry_t * entry,
uint8_t subindex,
od_notify_event_t event,
uint32_t value);
#ifdef __cplusplus
}
#endif
#endif /* CO_OD_H */