diff options
author | Richard Petri <git@rpls.de> | 2020-03-30 23:05:56 +0200 |
---|---|---|
committer | Richard Petri <git@rpls.de> | 2020-04-01 20:07:20 +0200 |
commit | 74a27268699e7cf83f0dd4c9d1a24d0e46038be8 (patch) | |
tree | 120ea55798c205b3f9cd851917569e56eabf4c80 /src/network/networkd-can.c | |
parent | 6829d8ce6927c151e4391c6a5e69b76906fbd2be (diff) | |
download | systemd-74a27268699e7cf83f0dd4c9d1a24d0e46038be8.tar.gz |
network: can: introduce a config parser function for bitrates
For now, this function is nearly equivalent to the si_uint64 parser, except for
an additional range check as Linux only takes 32-bit values as bitrates. In
future, this may also be used to introduce fancier bitrate config formats.
Diffstat (limited to 'src/network/networkd-can.c')
-rw-r--r-- | src/network/networkd-can.c | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/src/network/networkd-can.c b/src/network/networkd-can.c index 18533843e3..4118fcf859 100644 --- a/src/network/networkd-can.c +++ b/src/network/networkd-can.c @@ -7,10 +7,51 @@ #include "networkd-can.h" #include "networkd-link.h" #include "networkd-manager.h" +#include "parse-util.h" #include "string-util.h" #define CAN_TERMINATION_OHM_VALUE 120 +int config_parse_can_bitrate( + const char* unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + uint32_t *br = data; + uint64_t sz; + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + r = parse_size(rvalue, 1000, &sz); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, + "Failed to parse can bitrate '%s', ignoring: %m", rvalue); + return 0; + } + + /* Linux uses __u32 for bitrates, so the value should not exceed that. */ + if (sz <= 0 || sz > UINT32_MAX) { + log_syntax(unit, LOG_ERR, filename, line, 0, + "Bit rate out of permitted range 1...4294967295"); + return 0; + } + + *br = (uint32_t) sz; + + return 0; +} + static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; @@ -103,11 +144,6 @@ static int link_set_can(Link *link) { .sample_point = link->network->can_sample_point, }; - if (link->network->can_bitrate > UINT32_MAX) { - log_link_error(link, "bitrate (%" PRIu64 ") too big.", link->network->can_bitrate); - return -ERANGE; - } - log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate); if (link->network->can_sample_point > 0) log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10); |