libnl 3.8.0
can.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2012 Benedikt Spranger <b.spranger@linutronix.de>
4 */
5
6/**
7 * @ingroup link
8 * @defgroup can CAN
9 * Controller Area Network link module
10 *
11 * @details
12 * \b Link Type Name: "can"
13 *
14 * @route_doc{link_can, CAN Documentation}
15 *
16 * @{
17 */
18
19#include "nl-default.h"
20
21#include <linux/can/netlink.h>
22
23#include <netlink/netlink.h>
24#include <netlink/attr.h>
25#include <netlink/utils.h>
26#include <netlink/object.h>
27#include <netlink/route/rtnl.h>
28#include <netlink/route/link/can.h>
29
30#include "nl-route.h"
31#include "link-api.h"
32
33/** @cond SKIP */
34#define CAN_HAS_BITTIMING (1<<0)
35#define CAN_HAS_BITTIMING_CONST (1<<1)
36#define CAN_HAS_CLOCK (1<<2)
37#define CAN_HAS_STATE (1<<3)
38#define CAN_HAS_CTRLMODE (1<<4)
39#define CAN_HAS_RESTART_MS (1<<5)
40#define CAN_HAS_RESTART (1<<6)
41#define CAN_HAS_BERR_COUNTER (1<<7)
42#define CAN_HAS_DATA_BITTIMING (1<<8)
43#define CAN_HAS_DATA_BITTIMING_CONST (1<<9)
44
45struct can_info {
46 uint32_t ci_state;
47 uint32_t ci_restart;
48 uint32_t ci_restart_ms;
49 struct can_ctrlmode ci_ctrlmode;
50 struct can_bittiming ci_bittiming;
51 struct can_bittiming_const ci_bittiming_const;
52 struct can_clock ci_clock;
53 struct can_berr_counter ci_berr_counter;
54 uint32_t ci_mask;
55 struct can_bittiming ci_data_bittiming;
56 struct can_bittiming_const ci_data_bittiming_const;
57};
58
59/** @endcond */
60
61static struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
62 [IFLA_CAN_STATE] = { .type = NLA_U32 },
63 [IFLA_CAN_CTRLMODE] = { .minlen = sizeof(struct can_ctrlmode) },
64 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
65 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
66 [IFLA_CAN_BITTIMING] = { .minlen = sizeof(struct can_bittiming) },
67 [IFLA_CAN_BITTIMING_CONST]
68 = { .minlen = sizeof(struct can_bittiming_const) },
69 [IFLA_CAN_CLOCK] = { .minlen = sizeof(struct can_clock) },
70 [IFLA_CAN_BERR_COUNTER] = { .minlen = sizeof(struct can_berr_counter) },
71 [IFLA_CAN_DATA_BITTIMING]
72 = { .minlen = sizeof(struct can_bittiming) },
73 [IFLA_CAN_DATA_BITTIMING_CONST]
74 = { .minlen = sizeof(struct can_bittiming_const) },
75};
76
77static int can_alloc(struct rtnl_link *link)
78{
79 struct can_info *ci;
80
81 if (link->l_info)
82 memset(link->l_info, 0, sizeof(*ci));
83 else {
84 ci = calloc(1, sizeof(*ci));
85 if (!ci)
86 return -NLE_NOMEM;
87
88 link->l_info = ci;
89 }
90
91 return 0;
92}
93
94static int can_parse(struct rtnl_link *link, struct nlattr *data,
95 struct nlattr *xstats)
96{
97 struct nlattr *tb[IFLA_CAN_MAX+1];
98 struct can_info *ci;
99 int err;
100
101 NL_DBG(3, "Parsing CAN link info\n");
102
103 if ((err = nla_parse_nested(tb, IFLA_CAN_MAX, data, can_policy)) < 0)
104 goto errout;
105
106 if ((err = can_alloc(link)) < 0)
107 goto errout;
108
109 ci = link->l_info;
110
111 if (tb[IFLA_CAN_STATE]) {
112 ci->ci_state = nla_get_u32(tb[IFLA_CAN_STATE]);
113 ci->ci_mask |= CAN_HAS_STATE;
114 }
115
116 if (tb[IFLA_CAN_RESTART]) {
117 ci->ci_restart = nla_get_u32(tb[IFLA_CAN_RESTART]);
118 ci->ci_mask |= CAN_HAS_RESTART;
119 }
120
121 if (tb[IFLA_CAN_RESTART_MS]) {
122 ci->ci_restart_ms = nla_get_u32(tb[IFLA_CAN_RESTART_MS]);
123 ci->ci_mask |= CAN_HAS_RESTART_MS;
124 }
125
126 if (tb[IFLA_CAN_CTRLMODE]) {
127 nla_memcpy(&ci->ci_ctrlmode, tb[IFLA_CAN_CTRLMODE],
128 sizeof(ci->ci_ctrlmode));
129 ci->ci_mask |= CAN_HAS_CTRLMODE;
130 }
131
132 if (tb[IFLA_CAN_BITTIMING]) {
133 nla_memcpy(&ci->ci_bittiming, tb[IFLA_CAN_BITTIMING],
134 sizeof(ci->ci_bittiming));
135 ci->ci_mask |= CAN_HAS_BITTIMING;
136 }
137
138 if (tb[IFLA_CAN_BITTIMING_CONST]) {
139 nla_memcpy(&ci->ci_bittiming_const,
140 tb[IFLA_CAN_BITTIMING_CONST],
141 sizeof(ci->ci_bittiming_const));
142 ci->ci_mask |= CAN_HAS_BITTIMING_CONST;
143 }
144
145 if (tb[IFLA_CAN_CLOCK]) {
146 nla_memcpy(&ci->ci_clock, tb[IFLA_CAN_CLOCK],
147 sizeof(ci->ci_clock));
148 ci->ci_mask |= CAN_HAS_CLOCK;
149 }
150
151 if (tb[IFLA_CAN_BERR_COUNTER]) {
152 nla_memcpy(&ci->ci_berr_counter, tb[IFLA_CAN_BERR_COUNTER],
153 sizeof(ci->ci_berr_counter));
154 ci->ci_mask |= CAN_HAS_BERR_COUNTER;
155 }
156
157 if (tb[IFLA_CAN_DATA_BITTIMING]) {
158 nla_memcpy(&ci->ci_data_bittiming, tb[IFLA_CAN_DATA_BITTIMING],
159 sizeof(ci->ci_data_bittiming));
160 ci->ci_mask |= CAN_HAS_DATA_BITTIMING;
161 }
162
163 if (tb[IFLA_CAN_DATA_BITTIMING_CONST]) {
164 nla_memcpy(&ci->ci_data_bittiming_const, tb[IFLA_CAN_DATA_BITTIMING_CONST],
165 sizeof(ci->ci_data_bittiming_const));
166 ci->ci_mask |= CAN_HAS_DATA_BITTIMING_CONST;
167 }
168
169 err = 0;
170errout:
171 return err;
172}
173
174static void can_free(struct rtnl_link *link)
175{
176 struct can_info *ci = link->l_info;
177
178 free(ci);
179 link->l_info = NULL;
180}
181
182static char *print_can_state (uint32_t state)
183{
184 char *text;
185
186 switch (state)
187 {
188 case CAN_STATE_ERROR_ACTIVE:
189 text = "error active";
190 break;
191 case CAN_STATE_ERROR_WARNING:
192 text = "error warning";
193 break;
194 case CAN_STATE_ERROR_PASSIVE:
195 text = "error passive";
196 break;
197 case CAN_STATE_BUS_OFF:
198 text = "bus off";
199 break;
200 case CAN_STATE_STOPPED:
201 text = "stopped";
202 break;
203 case CAN_STATE_SLEEPING:
204 text = "sleeping";
205 break;
206 default:
207 text = "unknown state";
208 }
209
210 return text;
211}
212
213static void can_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
214{
215 struct can_info *ci = link->l_info;
216 char buf [64];
217
218 rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf, sizeof(buf));
219 nl_dump(p, "bitrate %d %s <%s>",
220 ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
221}
222
223static void can_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
224{
225 struct can_info *ci = link->l_info;
226 char buf [64];
227
228 rtnl_link_can_ctrlmode2str(ci->ci_ctrlmode.flags, buf, sizeof(buf));
229 nl_dump(p, " bitrate %d %s <%s>",
230 ci->ci_bittiming.bitrate, print_can_state(ci->ci_state), buf);
231
232 if (ci->ci_mask & CAN_HAS_RESTART) {
233 if (ci->ci_restart)
234 nl_dump_line(p," restarting\n");
235 }
236
237 if (ci->ci_mask & CAN_HAS_RESTART_MS) {
238 nl_dump_line(p," restart interval %d ms\n",
239 ci->ci_restart_ms);
240 }
241
242 if (ci->ci_mask & CAN_HAS_BITTIMING) {
243 nl_dump_line(p," sample point %f %%\n",
244 ((float) ci->ci_bittiming.sample_point)/10);
245 nl_dump_line(p," time quanta %d ns\n",
246 ci->ci_bittiming.tq);
247 nl_dump_line(p," propagation segment %d tq\n",
248 ci->ci_bittiming.prop_seg);
249 nl_dump_line(p," phase buffer segment1 %d tq\n",
250 ci->ci_bittiming.phase_seg1);
251 nl_dump_line(p," phase buffer segment2 %d tq\n",
252 ci->ci_bittiming.phase_seg2);
253 nl_dump_line(p," synchronisation jump width %d tq\n",
254 ci->ci_bittiming.sjw);
255 nl_dump_line(p," bitrate prescaler %d\n",
256 ci->ci_bittiming.brp);
257 }
258
259 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST) {
260 nl_dump_line(p," minimum tsig1 %d tq\n",
261 ci->ci_bittiming_const.tseg1_min);
262 nl_dump_line(p," maximum tsig1 %d tq\n",
263 ci->ci_bittiming_const.tseg1_max);
264 nl_dump_line(p," minimum tsig2 %d tq\n",
265 ci->ci_bittiming_const.tseg2_min);
266 nl_dump_line(p," maximum tsig2 %d tq\n",
267 ci->ci_bittiming_const.tseg2_max);
268 nl_dump_line(p," maximum sjw %d tq\n",
269 ci->ci_bittiming_const.sjw_max);
270 nl_dump_line(p," minimum brp %d\n",
271 ci->ci_bittiming_const.brp_min);
272 nl_dump_line(p," maximum brp %d\n",
273 ci->ci_bittiming_const.brp_max);
274 nl_dump_line(p," brp increment %d\n",
275 ci->ci_bittiming_const.brp_inc);
276 }
277
278 if (ci->ci_mask & CAN_HAS_CLOCK) {
279 nl_dump_line(p," base freq %u Hz\n", ci->ci_clock.freq);
280
281 }
282
283 if (ci->ci_mask & CAN_HAS_BERR_COUNTER) {
284 nl_dump_line(p," bus error RX %d\n",
285 ci->ci_berr_counter.rxerr);
286 nl_dump_line(p," bus error TX %d\n",
287 ci->ci_berr_counter.txerr);
288 }
289
290 return;
291}
292
293static int can_clone(struct rtnl_link *dst, struct rtnl_link *src)
294{
295 struct can_info *cdst, *csrc = src->l_info;
296 int ret;
297
298 dst->l_info = NULL;
299 ret = rtnl_link_set_type(dst, "can");
300 if (ret < 0)
301 return ret;
302
303 cdst = malloc(sizeof(*cdst));
304 if (!cdst)
305 return -NLE_NOMEM;
306
307 *cdst = *csrc;
308 dst->l_info = cdst;
309
310 return 0;
311}
312
313static int can_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
314{
315 struct can_info *ci = link->l_info;
316 struct nlattr *data;
317
318 data = nla_nest_start(msg, IFLA_INFO_DATA);
319 if (!data)
320 return -NLE_MSGSIZE;
321
322 if (ci->ci_mask & CAN_HAS_RESTART)
323 NLA_PUT_U32(msg, IFLA_CAN_RESTART, ci->ci_restart);
324
325 if (ci->ci_mask & CAN_HAS_RESTART_MS)
326 NLA_PUT_U32(msg, IFLA_CAN_RESTART_MS, ci->ci_restart_ms);
327
328 if (ci->ci_mask & CAN_HAS_CTRLMODE)
329 NLA_PUT(msg, IFLA_CAN_CTRLMODE, sizeof(ci->ci_ctrlmode),
330 &ci->ci_ctrlmode);
331
332 if (ci->ci_mask & CAN_HAS_BITTIMING)
333 NLA_PUT(msg, IFLA_CAN_BITTIMING, sizeof(ci->ci_bittiming),
334 &ci->ci_bittiming);
335
336 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
337 NLA_PUT(msg, IFLA_CAN_BITTIMING_CONST,
338 sizeof(ci->ci_bittiming_const),
339 &ci->ci_bittiming_const);
340
341 if (ci->ci_mask & CAN_HAS_CLOCK)
342 NLA_PUT(msg, IFLA_CAN_CLOCK, sizeof(ci->ci_clock),
343 &ci->ci_clock);
344
345 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING)
346 NLA_PUT(msg, IFLA_CAN_DATA_BITTIMING, sizeof(ci->ci_data_bittiming),
347 &ci->ci_data_bittiming);
348
349 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING_CONST)
350 NLA_PUT(msg, IFLA_CAN_DATA_BITTIMING_CONST, sizeof(ci->ci_data_bittiming_const),
351 &ci->ci_data_bittiming_const);
352
353 nla_nest_end(msg, data);
354
355nla_put_failure:
356
357 return 0;
358}
359
360static struct rtnl_link_info_ops can_info_ops = {
361 .io_name = "can",
362 .io_alloc = can_alloc,
363 .io_parse = can_parse,
364 .io_dump = {
365 [NL_DUMP_LINE] = can_dump_line,
366 [NL_DUMP_DETAILS] = can_dump_details,
367 },
368 .io_clone = can_clone,
369 .io_put_attrs = can_put_attrs,
370 .io_free = can_free,
371};
372
373/** @cond SKIP */
374#define IS_CAN_LINK_ASSERT(link) \
375 if ((link)->l_info_ops != &can_info_ops) { \
376 APPBUG("Link is not a CAN link. set type \"can\" first."); \
377 return -NLE_OPNOTSUPP; \
378 }
379/** @endcond */
380
381/**
382 * @name CAN Object
383 * @{
384 */
385
386/**
387 * Check if link is a CAN link
388 * @arg link Link object
389 *
390 * @return True if link is a CAN link, otherwise false is returned.
391 */
393{
394 return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "can");
395}
396
397/**
398 * Restart CAN device
399 * @arg link Link object
400 *
401 * @return 0 on success or a negative error code
402 */
404{
405 struct can_info *ci = link->l_info;
406
407 IS_CAN_LINK_ASSERT(link);
408
409 ci->ci_restart = 1;
410 ci->ci_restart |= CAN_HAS_RESTART;
411
412 return 0;
413}
414
415/**
416 * Get CAN base frequency
417 * @arg link Link object
418 * @arg freq frequency in Hz
419 *
420 * @return 0 on success or a negative error code
421 */
422int rtnl_link_can_freq(struct rtnl_link *link, uint32_t *freq)
423{
424 struct can_info *ci = link->l_info;
425
426 IS_CAN_LINK_ASSERT(link);
427 if (!freq)
428 return -NLE_INVAL;
429
430 if (ci->ci_mask & CAN_HAS_CLOCK)
431 *freq = ci->ci_clock.freq;
432 else
433 return -NLE_AGAIN;
434
435 return 0;
436}
437
438/**
439 * Get CAN state
440 * @arg link Link object
441 * @arg state CAN bus state
442 * @return 0 on success or a negative error code
443 */
444int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state)
445{
446 struct can_info *ci = link->l_info;
447
448 IS_CAN_LINK_ASSERT(link);
449 if (!state)
450 return -NLE_INVAL;
451
452 *state = ci->ci_state;
453
454 return 0;
455}
456
457/**
458 * Get CAN RX bus error count
459 * @arg link Link object
460 *
461 * @return RX bus error count on success or a negative error code
462 */
464{
465 struct can_info *ci = link->l_info;
466
467 IS_CAN_LINK_ASSERT(link);
468
469 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
470 return ci->ci_berr_counter.rxerr;
471 else
472 return -NLE_AGAIN;
473}
474
475/**
476 * Get CAN TX bus error count
477 * @arg link Link object
478 *
479 * @return TX bus error count on success or a negative error code
480 */
482{
483 struct can_info *ci = link->l_info;
484
485 IS_CAN_LINK_ASSERT(link);
486
487 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
488 return ci->ci_berr_counter.txerr;
489 else
490 return -NLE_AGAIN;
491}
492
493/**
494 * Get CAN bus error count
495 * @arg link Link object
496 * @arg berr Bus error count
497 *
498 * @return 0 on success or a negative error code
499 */
500int rtnl_link_can_berr(struct rtnl_link *link, struct can_berr_counter *berr)
501{
502 struct can_info *ci = link->l_info;
503
504 IS_CAN_LINK_ASSERT(link);
505 if (!berr)
506 return -NLE_INVAL;
507
508 if (ci->ci_mask & CAN_HAS_BERR_COUNTER)
509 *berr = ci->ci_berr_counter;
510 else
511 return -NLE_AGAIN;
512
513 return 0;
514}
515
516/**
517 * Get CAN hardware-dependent bit-timing constant
518 * @arg link Link object
519 * @arg bt_const Bit-timing constant
520 *
521 * @return 0 on success or a negative error code
522 */
524 struct can_bittiming_const *bt_const)
525{
526 struct can_info *ci = link->l_info;
527
528 IS_CAN_LINK_ASSERT(link);
529 if (!bt_const)
530 return -NLE_INVAL;
531
532 if (ci->ci_mask & CAN_HAS_BITTIMING_CONST)
533 *bt_const = ci->ci_bittiming_const;
534 else
535 return -NLE_AGAIN;
536
537 return 0;
538}
539
540/**
541 * Get CAN device bit-timing
542 * @arg link Link object
543 * @arg bit_timing CAN bit-timing
544 *
545 * @return 0 on success or a negative error code
546 */
548 struct can_bittiming *bit_timing)
549{
550 struct can_info *ci = link->l_info;
551
552 IS_CAN_LINK_ASSERT(link);
553 if (!bit_timing)
554 return -NLE_INVAL;
555
556 if (ci->ci_mask & CAN_HAS_BITTIMING)
557 *bit_timing = ci->ci_bittiming;
558 else
559 return -NLE_AGAIN;
560
561 return 0;
562}
563
564/**
565 * Set CAN device bit-timing
566 * @arg link Link object
567 * @arg bit_timing CAN bit-timing
568 *
569 * @return 0 on success or a negative error code
570 */
572 const struct can_bittiming *bit_timing)
573{
574 struct can_info *ci = link->l_info;
575
576 IS_CAN_LINK_ASSERT(link);
577 if (!bit_timing)
578 return -NLE_INVAL;
579
580 ci->ci_bittiming = *bit_timing;
581 ci->ci_mask |= CAN_HAS_BITTIMING;
582
583 return 0;
584}
585
586/**
587 * Get CAN device bit-timing
588 * @arg link Link object
589 * @arg bitrate CAN bitrate
590 *
591 * @return 0 on success or a negative error code
592 */
593int rtnl_link_can_get_bitrate(struct rtnl_link *link, uint32_t *bitrate)
594{
595 struct can_info *ci = link->l_info;
596
597 IS_CAN_LINK_ASSERT(link);
598 if (!bitrate)
599 return -NLE_INVAL;
600
601 if (ci->ci_mask & CAN_HAS_BITTIMING)
602 *bitrate = ci->ci_bittiming.bitrate;
603 else
604 return -NLE_AGAIN;
605
606 return 0;
607}
608
609/**
610 * Set CAN device bit-rate
611 * @arg link Link object
612 * @arg bitrate CAN bitrate
613 *
614 * @return 0 on success or a negative error code
615 */
616int rtnl_link_can_set_bitrate(struct rtnl_link *link, uint32_t bitrate)
617{
618 struct can_info *ci = link->l_info;
619
620 IS_CAN_LINK_ASSERT(link);
621
622 ci->ci_bittiming.bitrate = bitrate;
623 ci->ci_mask |= CAN_HAS_BITTIMING;
624
625 return 0;
626}
627
628/**
629 * Get CAN device sample point
630 * @arg link Link object
631 * @arg sp CAN sample point
632 *
633 * @return 0 on success or a negative error code
634 */
635int rtnl_link_can_get_sample_point(struct rtnl_link *link, uint32_t *sp)
636{
637 struct can_info *ci = link->l_info;
638
639 IS_CAN_LINK_ASSERT(link);
640 if (!sp)
641 return -NLE_INVAL;
642
643 if (ci->ci_mask & CAN_HAS_BITTIMING)
644 *sp = ci->ci_bittiming.sample_point;
645 else
646 return -NLE_AGAIN;
647
648 return 0;
649}
650
651/**
652 * Set CAN device sample point
653 * @arg link Link object
654 * @arg sp CAN sample point
655 *
656 * @return 0 on success or a negative error code
657 */
658int rtnl_link_can_set_sample_point(struct rtnl_link *link, uint32_t sp)
659{
660 struct can_info *ci = link->l_info;
661
662 IS_CAN_LINK_ASSERT(link);
663
664 ci->ci_bittiming.sample_point = sp;
665 ci->ci_mask |= CAN_HAS_BITTIMING;
666
667 return 0;
668}
669
670/**
671 * Get CAN device restart intervall
672 * @arg link Link object
673 * @arg interval Restart intervall in ms
674 *
675 * @return 0 on success or a negative error code
676 */
677int rtnl_link_can_get_restart_ms(struct rtnl_link *link, uint32_t *interval)
678{
679 struct can_info *ci = link->l_info;
680
681 IS_CAN_LINK_ASSERT(link);
682 if (!interval)
683 return -NLE_INVAL;
684
685 if (ci->ci_mask & CAN_HAS_RESTART_MS)
686 *interval = ci->ci_restart_ms;
687 else
688 return -NLE_AGAIN;
689
690 return 0;
691}
692
693/**
694 * Set CAN device restart intervall
695 * @arg link Link object
696 * @arg interval Restart intervall in ms
697 *
698 * @return 0 on success or a negative error code
699 */
700int rtnl_link_can_set_restart_ms(struct rtnl_link *link, uint32_t interval)
701{
702 struct can_info *ci = link->l_info;
703
704 IS_CAN_LINK_ASSERT(link);
705
706 ci->ci_restart_ms = interval;
707 ci->ci_mask |= CAN_HAS_RESTART_MS;
708
709 return 0;
710}
711
712/**
713 * Get CAN control mode
714 * @arg link Link object
715 * @arg ctrlmode CAN control mode
716 *
717 * @return 0 on success or a negative error code
718 */
719int rtnl_link_can_get_ctrlmode(struct rtnl_link *link, uint32_t *ctrlmode)
720{
721 struct can_info *ci = link->l_info;
722
723 IS_CAN_LINK_ASSERT(link);
724 if (!ctrlmode)
725 return -NLE_INVAL;
726
727 if (ci->ci_mask & CAN_HAS_CTRLMODE)
728 *ctrlmode = ci->ci_ctrlmode.flags;
729 else
730 return -NLE_AGAIN;
731
732 return 0;
733}
734
735/**
736 * Set a CAN Control Mode
737 * @arg link Link object
738 * @arg ctrlmode CAN control mode
739 *
740 * @return 0 on success or a negative error code
741 */
742int rtnl_link_can_set_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
743{
744 struct can_info *ci = link->l_info;
745
746 IS_CAN_LINK_ASSERT(link);
747
748 ci->ci_ctrlmode.flags |= ctrlmode;
749 ci->ci_ctrlmode.mask |= ctrlmode;
750 ci->ci_mask |= CAN_HAS_CTRLMODE;
751
752 return 0;
753}
754
755/**
756 * Unset a CAN Control Mode
757 * @arg link Link object
758 * @arg ctrlmode CAN control mode
759 *
760 * @return 0 on success or a negative error code
761 */
762int rtnl_link_can_unset_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
763{
764 struct can_info *ci = link->l_info;
765
766 IS_CAN_LINK_ASSERT(link);
767
768 ci->ci_ctrlmode.flags &= ~ctrlmode;
769 ci->ci_ctrlmode.mask |= ctrlmode;
770 ci->ci_mask |= CAN_HAS_CTRLMODE;
771
772 return 0;
773}
774
775/**
776 * Get CAN FD hardware-dependent data bit-timing constant
777 * @arg link Link object
778 * @arg data_bt_const CAN FD data bit-timing constant
779 *
780 * @return 0 on success or a negative error code
781 */
783 struct can_bittiming_const *data_bt_const)
784{
785 struct can_info *ci = link->l_info;
786
787 IS_CAN_LINK_ASSERT(link);
788 if (!data_bt_const)
789 return -NLE_INVAL;
790
791 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING_CONST)
792 *data_bt_const = ci->ci_data_bittiming_const;
793 else
794 return -NLE_AGAIN;
795
796 return 0;
797}
798
799/**
800 * Set CAN FD device data bit-timing-const
801 * @arg link Link object
802 * @arg data_bit_timing CAN FD data bit-timing
803 *
804 * @return 0 on success or a negative error code
805 */
807 const struct can_bittiming_const *data_bt_const)
808{
809 struct can_info *ci = link->l_info;
810
811 IS_CAN_LINK_ASSERT(link);
812 if (!data_bt_const)
813 return -NLE_INVAL;
814
815 ci->ci_data_bittiming_const = *data_bt_const;
816 ci->ci_mask |= CAN_HAS_DATA_BITTIMING_CONST;
817
818 return 0;
819}
820
821/**
822 * Get CAN FD device data bit-timing
823 * @arg link Link object
824 * @arg data_bit_timing CAN FD data bit-timing
825 *
826 * @return 0 on success or a negative error code
827 */
829 struct can_bittiming *data_bit_timing)
830{
831 struct can_info *ci = link->l_info;
832
833 IS_CAN_LINK_ASSERT(link);
834 if (!data_bit_timing)
835 return -NLE_INVAL;
836
837 if (ci->ci_mask & CAN_HAS_DATA_BITTIMING)
838 *data_bit_timing = ci->ci_data_bittiming;
839 else
840 return -NLE_AGAIN;
841
842 return 0;
843}
844
845/**
846 * Set CAN FD device data bit-timing
847 * @arg link Link object
848 * @arg data_bit_timing CAN FD data bit-timing
849 *
850 * @return 0 on success or a negative error code
851 */
853 const struct can_bittiming *data_bit_timing)
854{
855 struct can_info *ci = link->l_info;
856
857 IS_CAN_LINK_ASSERT(link);
858 if (!data_bit_timing)
859 return -NLE_INVAL;
860
861 ci->ci_data_bittiming = *data_bit_timing;
862 ci->ci_mask |= CAN_HAS_DATA_BITTIMING;
863
864 return 0;
865}
866
867/** @} */
868
869/**
870 * @name Control Mode Translation
871 * @{
872 */
873
874static const struct trans_tbl can_ctrlmode[] = {
875 __ADD(CAN_CTRLMODE_LOOPBACK, loopback),
876 __ADD(CAN_CTRLMODE_LISTENONLY, listen-only),
877 __ADD(CAN_CTRLMODE_3_SAMPLES, triple-sampling),
878 __ADD(CAN_CTRLMODE_ONE_SHOT, one-shot),
879 __ADD(CAN_CTRLMODE_BERR_REPORTING, berr-reporting),
880 __ADD(CAN_CTRLMODE_FD, fd),
881 __ADD(CAN_CTRLMODE_PRESUME_ACK, presume-ack),
882 __ADD(CAN_CTRLMODE_FD_NON_ISO, fd-non-iso),
883};
884
885char *rtnl_link_can_ctrlmode2str(int ctrlmode, char *buf, size_t len)
886{
887 return __flags2str(ctrlmode, buf, len, can_ctrlmode,
888 ARRAY_SIZE(can_ctrlmode));
889}
890
891int rtnl_link_can_str2ctrlmode(const char *name)
892{
893 return __str2flags(name, can_ctrlmode, ARRAY_SIZE(can_ctrlmode));
894}
895
896/** @} */
897
898static void _nl_init can_init(void)
899{
900 rtnl_link_register_info(&can_info_ops);
901}
902
903static void _nl_exit can_exit(void)
904{
905 rtnl_link_unregister_info(&can_info_ops);
906}
907
908/** @} */
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:710
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:159
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:230
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:351
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:906
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:1033
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:969
@ NLA_U32
32 bit integer
Definition: attr.h:37
int rtnl_link_can_berr_rx(struct rtnl_link *link)
Get CAN RX bus error count.
Definition: can.c:463
int rtnl_link_can_set_restart_ms(struct rtnl_link *link, uint32_t interval)
Set CAN device restart intervall.
Definition: can.c:700
int rtnl_link_can_get_restart_ms(struct rtnl_link *link, uint32_t *interval)
Get CAN device restart intervall.
Definition: can.c:677
int rtnl_link_can_get_ctrlmode(struct rtnl_link *link, uint32_t *ctrlmode)
Get CAN control mode.
Definition: can.c:719
int rtnl_link_can_freq(struct rtnl_link *link, uint32_t *freq)
Get CAN base frequency.
Definition: can.c:422
int rtnl_link_can_get_bittiming(struct rtnl_link *link, struct can_bittiming *bit_timing)
Get CAN device bit-timing.
Definition: can.c:547
int rtnl_link_can_unset_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
Unset a CAN Control Mode.
Definition: can.c:762
int rtnl_link_can_set_data_bittiming_const(struct rtnl_link *link, const struct can_bittiming_const *data_bt_const)
Set CAN FD device data bit-timing-const.
Definition: can.c:806
int rtnl_link_is_can(struct rtnl_link *link)
Check if link is a CAN link.
Definition: can.c:392
int rtnl_link_can_get_data_bittiming(struct rtnl_link *link, struct can_bittiming *data_bit_timing)
Get CAN FD device data bit-timing.
Definition: can.c:828
int rtnl_link_can_restart(struct rtnl_link *link)
Restart CAN device.
Definition: can.c:403
int rtnl_link_can_set_sample_point(struct rtnl_link *link, uint32_t sp)
Set CAN device sample point.
Definition: can.c:658
int rtnl_link_can_set_data_bittiming(struct rtnl_link *link, const struct can_bittiming *data_bit_timing)
Set CAN FD device data bit-timing.
Definition: can.c:852
int rtnl_link_can_get_sample_point(struct rtnl_link *link, uint32_t *sp)
Get CAN device sample point.
Definition: can.c:635
int rtnl_link_can_set_bittiming(struct rtnl_link *link, const struct can_bittiming *bit_timing)
Set CAN device bit-timing.
Definition: can.c:571
int rtnl_link_can_set_bitrate(struct rtnl_link *link, uint32_t bitrate)
Set CAN device bit-rate.
Definition: can.c:616
int rtnl_link_can_get_data_bittiming_const(struct rtnl_link *link, struct can_bittiming_const *data_bt_const)
Get CAN FD hardware-dependent data bit-timing constant.
Definition: can.c:782
int rtnl_link_can_get_bitrate(struct rtnl_link *link, uint32_t *bitrate)
Get CAN device bit-timing.
Definition: can.c:593
int rtnl_link_can_berr_tx(struct rtnl_link *link)
Get CAN TX bus error count.
Definition: can.c:481
int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state)
Get CAN state.
Definition: can.c:444
int rtnl_link_can_get_bt_const(struct rtnl_link *link, struct can_bittiming_const *bt_const)
Get CAN hardware-dependent bit-timing constant.
Definition: can.c:523
int rtnl_link_can_set_ctrlmode(struct rtnl_link *link, uint32_t ctrlmode)
Set a CAN Control Mode.
Definition: can.c:742
int rtnl_link_can_berr(struct rtnl_link *link, struct can_berr_counter *berr)
Get CAN bus error count.
Definition: can.c:500
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:1017
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:21
Dumping parameters.
Definition: types.h:32
Attribute validation policy.
Definition: attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:65