Lely core libraries 2.3.2
time.c
Go to the documentation of this file.
1
24#include "co.h"
25
26#if !LELY_NO_CO_TIME
27
28#include <lely/co/dev.h>
29#include <lely/co/obj.h>
30#include <lely/co/sdo.h>
31#include <lely/co/time.h>
32#include <lely/co/val.h>
33#include <lely/util/endian.h>
34#include <lely/util/errnum.h>
35#include <lely/util/time.h>
36
37#include <assert.h>
38#include <stdlib.h>
39
41struct __co_time {
49 co_unsigned32_t cobid;
61 void *data;
62};
63
69static void co_time_update(co_time_t *time);
70
77static co_unsigned32_t co_1012_dn_ind(
78 co_sub_t *sub, struct co_sdo_req *req, void *data);
79
85static int co_time_recv(const struct can_msg *msg, void *data);
86
92static int co_time_timer(const struct timespec *tp, void *data);
93
94void
95co_time_of_day_get(const co_time_of_day_t *tod, struct timespec *tp)
96{
97 assert(tod);
98 assert(tp);
99
100 // Convert the CANopen time (seconds since January 1, 1984) to the Unix
101 // epoch (seconds since January 1, 1970). This is a difference of 14
102 // years and 3 leap days.
103 co_time_diff_t td = { .ms = tod->ms, .days = tod->days };
104 co_time_diff_get(&td, tp);
105 tp->tv_sec += (14 * 365 + 3) * 24 * 60 * 60;
106}
107
108void
109co_time_of_day_set(co_time_of_day_t *tod, const struct timespec *tp)
110{
111 assert(tod);
112 assert(tp);
113
114 // Convert the Unix epoch (seconds since January 1, 1970) to the CANopen
115 // time (seconds since January 1, 1984). This is a difference of 14
116 // years and 3 leap days.
117 co_time_diff_t td = { .ms = tod->ms, .days = tod->days };
118 // clang-format off
119 co_time_diff_set(&td, &(struct timespec){
120 tp->tv_sec - (14 * 365 + 3) * 24 * 60 * 60,
121 tp->tv_nsec });
122 // clang-format on
123}
124
125void
126co_time_diff_get(const co_time_diff_t *td, struct timespec *tp)
127{
128 assert(td);
129 assert(tp);
130
131 tp->tv_sec = (time_t)td->days * 24 * 60 * 60 + td->ms / 1000;
132 tp->tv_nsec = (long)(td->ms % 1000) * 1000000;
133}
134
135void
136co_time_diff_set(co_time_diff_t *td, const struct timespec *tp)
137{
138 assert(td);
139 assert(tp);
140
141 // Compute the number of milliseconds since midnight.
142 td->ms = (tp->tv_sec % (24 * 60 * 60)) * 1000 + tp->tv_nsec / 1000000;
143 // Compute the number of days.
144 td->days = (co_unsigned16_t)(tp->tv_sec / (24 * 60 * 60));
145}
146
147void *
148__co_time_alloc(void)
149{
150 void *ptr = malloc(sizeof(struct __co_time));
151#if !LELY_NO_ERRNO
152 if (!ptr)
153 set_errc(errno2c(errno));
154#endif
155 return ptr;
156}
157
158void
159__co_time_free(void *ptr)
160{
161 free(ptr);
162}
163
164struct __co_time *
165__co_time_init(struct __co_time *time, can_net_t *net, co_dev_t *dev)
166{
167 assert(time);
168 assert(net);
169 assert(dev);
170
171 int errc = 0;
172
173 time->net = net;
174 time->dev = dev;
175
176 time->stopped = 1;
177
178 co_obj_t *obj_1012 = co_dev_find_obj(time->dev, 0x1012);
179 if (!obj_1012) {
180 errc = errnum2c(ERRNUM_NOSYS);
181 goto error_obj_1012;
182 }
183
184 time->cobid = 0;
185
186 time->sub_1013_00 = co_dev_find_sub(time->dev, 0x1013, 0x00);
187
188 time->recv = can_recv_create();
189 if (!time->recv) {
190 errc = get_errc();
191 goto error_create_recv;
192 }
193 can_recv_set_func(time->recv, &co_time_recv, time);
194
195 time->timer = can_timer_create();
196 if (!time->timer) {
197 errc = get_errc();
198 goto error_create_timer;
199 }
201
202 time->start = (struct timespec){ 0, 0 };
203
204 time->ind = NULL;
205 time->data = NULL;
206
207 if (co_time_start(time) == -1) {
208 errc = get_errc();
209 goto error_start;
210 }
211
212 return time;
213
214 // co_time_stop(time);
215error_start:
217error_create_timer:
218 can_recv_destroy(time->recv);
219error_create_recv:
220 co_obj_set_dn_ind(obj_1012, NULL, NULL);
221error_obj_1012:
222 set_errc(errc);
223 return NULL;
224}
225
226void
227__co_time_fini(struct __co_time *time)
228{
229 assert(time);
230
231 co_time_stop(time);
232
234 can_recv_destroy(time->recv);
235}
236
237co_time_t *
239{
240 trace("creating TIME service");
241
242 int errc = 0;
243
244 co_time_t *time = __co_time_alloc();
245 if (!time) {
246 errc = get_errc();
247 goto error_alloc_time;
248 }
249
250 if (!__co_time_init(time, net, dev)) {
251 errc = get_errc();
252 goto error_init_time;
253 }
254
255 return time;
256
257error_init_time:
258 __co_time_free(time);
259error_alloc_time:
260 set_errc(errc);
261 return NULL;
262}
263
264void
266{
267 if (time) {
268 trace("destroying TIME service");
269 __co_time_fini(time);
270 __co_time_free(time);
271 }
272}
273
274int
276{
277 assert(time);
278
279 if (!time->stopped)
280 return 0;
281
282 co_obj_t *obj_1012 = co_dev_find_obj(time->dev, 0x1012);
283 // Retrieve the TIME COB-ID.
284 time->cobid = co_obj_get_val_u32(obj_1012, 0x00);
285 // Set the download indication function for the TIME COB-ID object.
286 co_obj_set_dn_ind(obj_1012, &co_1012_dn_ind, time);
287
288 can_net_get_time(time->net, &time->start);
289
290 co_time_update(time);
291
292 time->stopped = 1;
293
294 return 0;
295}
296
297void
299{
300 assert(time);
301
302 if (time->stopped)
303 return;
304
305 co_time_stop_prod(time);
306
307 can_timer_stop(time->timer);
308 can_recv_stop(time->recv);
309
310 // Remove the download indication function for the TIME COB-ID object.
311 co_obj_t *obj_1012 = co_dev_find_obj(time->dev, 0x1012);
312 co_obj_set_dn_ind(obj_1012, NULL, NULL);
313
314 time->stopped = 1;
315}
316
317int
319{
320 assert(time);
321
322 return time->stopped;
323}
324
325can_net_t *
327{
328 assert(time);
329
330 return time->net;
331}
332
333co_dev_t *
335{
336 assert(time);
337
338 return time->dev;
339}
340
341void
342co_time_get_ind(const co_time_t *time, co_time_ind_t **pind, void **pdata)
343{
344 assert(time);
345
346 if (pind)
347 *pind = time->ind;
348 if (pdata)
349 *pdata = time->data;
350}
351
352void
354{
355 assert(time);
356
357 time->ind = ind;
358 time->data = data;
359}
360
361void
362co_time_start_prod(co_time_t *time, const struct timespec *start,
363 const struct timespec *interval)
364{
365 assert(time);
366
367 if (time->cobid & CO_TIME_COBID_PRODUCER)
368 can_timer_start(time->timer, time->net, start, interval);
369}
370
371void
373{
374 assert(time);
375
376 if (time->cobid & CO_TIME_COBID_PRODUCER)
377 can_timer_stop(time->timer);
378}
379
380static void
382{
383 assert(time);
384
385 if (time->cobid & CO_TIME_COBID_CONSUMER) {
386 // Register the receiver under the specified CAN-ID.
387 uint_least32_t id = time->cobid;
388 uint_least8_t flags = 0;
389 if (id & CO_TIME_COBID_FRAME) {
390 id &= CAN_MASK_EID;
391 flags |= CAN_FLAG_IDE;
392 } else {
393 id &= CAN_MASK_BID;
394 }
395 can_recv_start(time->recv, time->net, id, flags);
396 } else {
397 // Stop the receiver unless we are a consumer.
398 can_recv_stop(time->recv);
399 }
400
401 if (!(time->cobid & CO_TIME_COBID_PRODUCER))
402 // Stop the timer unless we are a producer.
403 can_timer_stop(time->timer);
404}
405
406static co_unsigned32_t
407co_1012_dn_ind(co_sub_t *sub, struct co_sdo_req *req, void *data)
408{
409 assert(sub);
410 assert(co_obj_get_idx(co_sub_get_obj(sub)) == 0x1012);
411 assert(req);
412 co_time_t *time = data;
413 assert(time);
414
415 co_unsigned16_t type = co_sub_get_type(sub);
416 assert(!co_type_is_array(type));
417
418 union co_val val;
419 co_unsigned32_t ac = 0;
420 if (co_sdo_req_dn_val(req, type, &val, &ac) == -1)
421 return ac;
422
423 if (co_sub_get_subidx(sub))
424 return CO_SDO_AC_NO_SUB;
425
426 assert(type == CO_DEFTYPE_UNSIGNED32);
427 co_unsigned32_t cobid = val.u32;
428 co_unsigned32_t cobid_old = co_sub_get_val_u32(sub);
429 if (cobid == cobid_old)
430 return 0;
431
432 // The CAN-ID cannot be changed while the producer or consumer is and
433 // remains active.
434 int active = (cobid & CO_TIME_COBID_PRODUCER)
435 || (cobid & CO_TIME_COBID_CONSUMER);
436 int active_old = (cobid_old & CO_TIME_COBID_PRODUCER)
437 || (cobid_old & CO_TIME_COBID_CONSUMER);
438 uint_least32_t canid = cobid & CAN_MASK_EID;
439 uint_least32_t canid_old = cobid_old & CAN_MASK_EID;
440 if (active && active_old && canid != canid_old)
441 return CO_SDO_AC_PARAM_VAL;
442
443 // A 29-bit CAN-ID is only valid if the frame bit is set.
444 if (!(cobid & CO_TIME_COBID_FRAME)
445 && (cobid & (CAN_MASK_EID ^ CAN_MASK_BID)))
446 return CO_SDO_AC_PARAM_VAL;
447
448 time->cobid = cobid;
449
450 co_sub_dn(sub, &val);
451
452 co_time_update(time);
453 return 0;
454}
455
456static int
457co_time_recv(const struct can_msg *msg, void *data)
458{
459 assert(msg);
460 co_time_t *time = data;
461 assert(time);
462
463 // Ignore remote frames.
464 if (msg->flags & CAN_FLAG_RTR)
465 return 0;
466
467#if !LELY_NO_CANFD
468 // Ignore CAN FD format frames.
469 if (msg->flags & CAN_FLAG_EDL)
470 return 0;
471#endif
472
473 if (msg->len < 6)
474 return 0;
475
476 co_time_of_day_t tod;
477 tod.ms = ldle_u32(msg->data) & UINT32_C(0x0fffffff);
478 tod.days = ldle_u16(msg->data + 4);
479
480 struct timespec tv;
481 co_time_of_day_get(&tod, &tv);
482
483 if (time->ind)
484 time->ind(time, &tv, time->data);
485
486 return 0;
487}
488
489static int
490co_time_timer(const struct timespec *tp, void *data)
491{
492 assert(tp);
493 co_time_t *time = data;
494 assert(time);
495
496 // Update the high-resolution time stamp, if it exists.
497 if (time->sub_1013_00)
498 co_sub_set_val_u32(time->sub_1013_00,
499 (co_unsigned32_t)timespec_diff_usec(
500 tp, &time->start));
501
502 // Convert the time to a TIME_OF_DAY value.
503 co_time_of_day_t tod = { 0, 0 };
504 co_time_of_day_set(&tod, tp);
505
506 struct can_msg msg = CAN_MSG_INIT;
507 msg.id = time->cobid;
508 if (time->cobid & CO_TIME_COBID_FRAME) {
509 msg.id &= CAN_MASK_EID;
510 msg.flags |= CAN_FLAG_IDE;
511 } else {
512 msg.id &= CAN_MASK_BID;
513 }
514 msg.len = 6;
515 stle_u32(msg.data, tod.ms & UINT32_C(0x0fffffff));
516 stle_u16(msg.data + 4, tod.days);
517 can_net_send(time->net, &msg);
518
519 return 0;
520}
521
522#endif // !LELY_NO_CO_TIME
@ CAN_FLAG_IDE
The Identifier Extension (IDE) flag.
Definition: msg.h:43
@ CAN_FLAG_RTR
The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames).
Definition: msg.h:48
#define CAN_MASK_EID
The mask used to extract the 29-bit Extended Identifier from a CAN frame.
Definition: msg.h:34
#define CAN_MASK_BID
The mask used to extract the 11-bit Base Identifier from a CAN frame.
Definition: msg.h:31
#define CAN_MSG_INIT
The static initializer for can_msg.
Definition: msg.h:113
This header file is part of the CANopen library; it contains the device description declarations.
co_obj_t * co_dev_find_obj(const co_dev_t *dev, co_unsigned16_t idx)
Finds an object in the object dictionary of a CANopen device.
Definition: dev.c:279
co_sub_t * co_dev_find_sub(const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
Finds a sub-object in the object dictionary of a CANopen device.
Definition: dev.c:290
void co_time_stop(co_time_t *time)
Stops a TIME service.
Definition: time.c:298
co_time_t * co_time_create(can_net_t *net, co_dev_t *dev)
Creates a new CANopen TIME producer/consumer service.
Definition: time.c:238
void co_time_set_ind(co_time_t *time, co_time_ind_t *ind, void *data)
Sets the indication function invoked when a CANopen time stamp is received.
Definition: time.c:353
void co_time_stop_prod(co_time_t *time)
Stops a CANopen TIME producer.
Definition: time.c:372
void co_time_of_day_get(const co_time_of_day_t *tod, struct timespec *tp)
Loads the absolute time from a CANopen TIME_OF_DAY value.
Definition: time.c:95
int co_time_is_stopped(const co_time_t *time)
Retuns 1 if the specified TIME service is stopped, and 0 if not.
Definition: time.c:318
void co_time_destroy(co_time_t *time)
Destroys a CANopen TIME producer/consumer service.
Definition: time.c:265
static int co_time_recv(const struct can_msg *msg, void *data)
The CAN receive callback function for a TIME consumer service.
Definition: time.c:457
static void co_time_update(co_time_t *time)
Updates and (de)activates a TIME producer/consumer service.
Definition: time.c:381
int co_time_start(co_time_t *time)
Starts a TIME service.
Definition: time.c:275
void co_time_start_prod(co_time_t *time, const struct timespec *start, const struct timespec *interval)
Starts a CANopen TIME producer.
Definition: time.c:362
void co_time_get_ind(const co_time_t *time, co_time_ind_t **pind, void **pdata)
Retrieves the indication function invoked when a CANopen time stamp is received.
Definition: time.c:342
can_net_t * co_time_get_net(const co_time_t *time)
Returns a pointer to the CAN network of a TIME producer/consumer service.
Definition: time.c:326
void co_time_diff_get(const co_time_diff_t *td, struct timespec *tp)
Loads a time difference from a CANopen TIME_DIFFERENCE value.
Definition: time.c:126
void co_time_diff_set(co_time_diff_t *td, const struct timespec *tp)
Stores a time difference into a CANopen TIME_DIFFERENCE value.
Definition: time.c:136
static int co_time_timer(const struct timespec *tp, void *data)
The CAN timer callback function for a TIME producer service.
Definition: time.c:490
void co_time_of_day_set(co_time_of_day_t *tod, const struct timespec *tp)
Stores the absolute time into a CANopen TIME_OF_DAY value.
Definition: time.c:109
co_dev_t * co_time_get_dev(const co_time_t *time)
Returns a pointer to the CANopen device of a TIME producer/consumer service.
Definition: time.c:334
static co_unsigned32_t co_1012_dn_ind(co_sub_t *sub, struct co_sdo_req *req, void *data)
The download indication function for (all sub-objects of) CANopen object 1012 (COB-ID time stamp obje...
Definition: time.c:407
This header file is part of the CANopen library; it contains the time stamp (TIME) object declaration...
#define CO_TIME_COBID_CONSUMER
The bit in the TIME COB-ID specifying whether the device is a consumer.
Definition: time.h:29
#define CO_TIME_COBID_PRODUCER
The bit in the TIME COB-ID specifying whether the device is a producer.
Definition: time.h:32
#define CO_TIME_COBID_FRAME
The bit in the TIME COB-ID specifying whether to use an 11-bit (0) or 29-bit (1) CAN-ID.
Definition: time.h:38
void co_time_ind_t(co_time_t *time, const struct timespec *tp, void *data)
The type of a CANopen TIME indication function, invoked when a time stamp is received.
Definition: time.h:52
This header file is part of the utilities library; it contains the byte order (endianness) function d...
uint_least32_t ldle_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:596
uint_least16_t ldle_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:516
void stle_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in little-endian byte order.
Definition: endian.h:504
void stle_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in little-endian byte order.
Definition: endian.h:582
This header file is part of the utilities library; it contains the native and platform-independent er...
int errnum2c(errnum_t errnum)
Transforms a platform-independent error number to a native error code.
Definition: errnum.c:810
@ ERRNUM_NOSYS
Function not supported.
Definition: errnum.h:184
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:932
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
Definition: errnum.c:46
This header file is part of the CANopen library; it contains the Service Data Object (SDO) declaratio...
int co_sdo_req_dn_val(struct co_sdo_req *req, co_unsigned16_t type, void *val, co_unsigned32_t *pac)
Copies the next segment of the specified CANopen SDO download request to the internal buffer and,...
Definition: sdo.c:170
#define CO_SDO_AC_NO_SUB
SDO abort code: Sub-index does not exist.
Definition: sdo.h:132
#define CO_SDO_AC_PARAM_VAL
SDO abort code: Invalid value for parameter (download only).
Definition: sdo.h:135
void can_timer_stop(can_timer_t *timer)
Stops a CAN timer and unregisters it with a network interface.
Definition: net.c:462
int can_net_send(can_net_t *net, const struct can_msg *msg)
Sends a CAN frame from a network interface.
Definition: net.c:300
void can_timer_start(can_timer_t *timer, can_net_t *net, const struct timespec *start, const struct timespec *interval)
Starts a CAN timer and registers it with a network interface.
Definition: net.c:431
can_timer_t * can_timer_create(void)
Creates a new CAN timer.
Definition: net.c:376
void can_net_get_time(const can_net_t *net, struct timespec *tp)
Retrieves the current time of a CAN network interface.
Definition: net.c:196
void can_timer_set_func(can_timer_t *timer, can_timer_func_t *func, void *data)
Sets the callback function invoked when a CAN timer is triggered.
Definition: net.c:422
void can_recv_stop(can_recv_t *recv)
Stops a CAN frame receiver from processing frames and unregisters it with the network interface.
Definition: net.c:609
void can_recv_set_func(can_recv_t *recv, can_recv_func_t *func, void *data)
Sets the callback function used to process CAN frames with a receiver.
Definition: net.c:578
void can_recv_destroy(can_recv_t *recv)
Destroys a CAN frame receiver.
Definition: net.c:558
void can_recv_start(can_recv_t *recv, can_net_t *net, uint_least32_t id, uint_least8_t flags)
Registers a CAN frame receiver with a network interface and starts processing frames.
Definition: net.c:587
can_recv_t * can_recv_create(void)
Creates a new CAN frame receiver.
Definition: net.c:533
void can_timer_destroy(can_timer_t *timer)
Destroys a CAN timer.
Definition: net.c:401
This header file is part of the CANopen library; it contains the object dictionary declarations.
co_unsigned8_t co_sub_get_subidx(const co_sub_t *sub)
Returns the sub-index of a CANopen sub-object.
Definition: obj.c:559
co_unsigned16_t co_obj_get_idx(const co_obj_t *obj)
Returns the index of a CANopen object.
Definition: obj.c:164
int co_sub_dn(co_sub_t *sub, void *val)
Downloads (moves) a value into a CANopen sub-object if the refuse-write-on-download flag (CO_OBJ_FLAG...
Definition: obj.c:996
void co_obj_set_dn_ind(co_obj_t *obj, co_sub_dn_ind_t *ind, void *data)
Sets the download indication function for a CANopen object.
Definition: obj.c:389
co_obj_t * co_sub_get_obj(const co_sub_t *sub)
Returns the a pointer to the CANopen object containing the specified sub-object.
Definition: obj.c:551
co_unsigned16_t co_sub_get_type(const co_sub_t *sub)
Returns the data type of a CANopen sub-object.
Definition: obj.c:603
This is the internal header file of the CANopen library.
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib....
A CAN network interface.
Definition: net.c:37
A CAN frame receiver.
Definition: net.c:86
A CAN timer.
Definition: net.c:63
A CANopen device.
Definition: dev.h:30
A CANopen object.
Definition: obj.h:31
A CANopen sub-object.
Definition: obj.h:53
A CANopen TIME producer/consumer service.
Definition: time.c:41
can_timer_t * timer
A pointer to the CAN timer.
Definition: time.c:55
void * data
A pointer to user-specified data for ind.
Definition: time.c:61
can_recv_t * recv
A pointer to the CAN frame receiver.
Definition: time.c:53
co_sub_t * sub_1013_00
A pointer to the high-resolution time stamp sub-object.
Definition: time.c:51
int stopped
A flag specifying whether the TIME service is stopped.
Definition: time.c:47
co_unsigned32_t cobid
The TIME COB-ID.
Definition: time.c:49
can_net_t * net
A pointer to a CAN network interface.
Definition: time.c:43
co_time_ind_t * ind
A pointer to the indication function.
Definition: time.c:59
struct timespec start
The creation time of the service.
Definition: time.c:57
co_dev_t * dev
A pointer to a CANopen device.
Definition: time.c:45
A CAN or CAN FD format frame.
Definition: msg.h:87
uint_least8_t data[CAN_MSG_MAX_LEN]
The frame payload (in case of a data frame).
Definition: msg.h:102
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
Definition: msg.h:89
uint_least8_t flags
The flags (any combination of CAN_FLAG_IDE, CAN_FLAG_RTR, CAN_FLAG_FDF, CAN_FLAG_BRS and CAN_FLAG_ESI...
Definition: msg.h:94
uint_least8_t len
The number of bytes in data (or the requested number of bytes in case of a remote frame).
Definition: msg.h:100
A CANopen SDO upload/download request.
Definition: sdo.h:181
A time type with nanosecond resolution.
Definition: time.h:88
long tv_nsec
Nanoseconds [0, 999999999].
Definition: time.h:92
time_t tv_sec
Whole seconds (>= 0).
Definition: time.h:90
int co_type_is_array(co_unsigned16_t type)
Returns 1 if the specified (static) data type is an array, and 0 if not.
Definition: type.c:40
#define CO_DEFTYPE_UNSIGNED32
The data type (and object index) of a 32-bit unsigned integer.
Definition: type.h:50
A union of the CANopen static data types.
Definition: val.h:273
This header file is part of the utilities library; it contains the time function declarations.
int_least64_t timespec_diff_usec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in microseconds) between *t1 and *t2.
Definition: time.h:231
This header file is part of the CANopen library; it contains the CANopen value declarations.