X Tutup
Skip to content

Commit f1c141c

Browse files
committed
network: can: introduce config_parse_can_control_mode()
1 parent 0fa2984 commit f1c141c

File tree

5 files changed

+55
-46
lines changed

5 files changed

+55
-46
lines changed

src/network/networkd-can.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define CAN_TERMINATION_OHM_VALUE 120
1414

1515
int can_set_netlink_message(Link *link, sd_netlink_message *m) {
16-
struct can_ctrlmode cm = {};
1716
int r;
1817

1918
assert(link);
@@ -80,37 +79,12 @@ int can_set_netlink_message(Link *link, sd_netlink_message *m) {
8079
return log_link_debug_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
8180
}
8281

83-
if (link->network->can_fd_mode >= 0) {
84-
cm.mask |= CAN_CTRLMODE_FD;
85-
SET_FLAG(cm.flags, CAN_CTRLMODE_FD, link->network->can_fd_mode);
86-
log_link_debug(link, "Setting FD mode to '%s'.", yes_no(link->network->can_fd_mode));
87-
}
88-
89-
if (link->network->can_non_iso >= 0) {
90-
cm.mask |= CAN_CTRLMODE_FD_NON_ISO;
91-
SET_FLAG(cm.flags, CAN_CTRLMODE_FD_NON_ISO, link->network->can_non_iso);
92-
log_link_debug(link, "Setting FD non-ISO mode to '%s'.", yes_no(link->network->can_non_iso));
93-
}
94-
95-
if (link->network->can_triple_sampling >= 0) {
96-
cm.mask |= CAN_CTRLMODE_3_SAMPLES;
97-
SET_FLAG(cm.flags, CAN_CTRLMODE_3_SAMPLES, link->network->can_triple_sampling);
98-
log_link_debug(link, "Setting triple-sampling to '%s'.", yes_no(link->network->can_triple_sampling));
99-
}
100-
101-
if (link->network->can_berr_reporting >= 0) {
102-
cm.mask |= CAN_CTRLMODE_BERR_REPORTING;
103-
SET_FLAG(cm.flags, CAN_CTRLMODE_BERR_REPORTING, link->network->can_berr_reporting);
104-
log_link_debug(link, "Setting bus error reporting to '%s'.", yes_no(link->network->can_berr_reporting));
105-
}
106-
107-
if (link->network->can_listen_only >= 0) {
108-
cm.mask |= CAN_CTRLMODE_LISTENONLY;
109-
SET_FLAG(cm.flags, CAN_CTRLMODE_LISTENONLY, link->network->can_listen_only);
110-
log_link_debug(link, "Setting listen-only mode to '%s'.", yes_no(link->network->can_listen_only));
111-
}
82+
if (link->network->can_control_mode_mask != 0) {
83+
struct can_ctrlmode cm = {
84+
.mask = link->network->can_control_mode_mask,
85+
.flags = link->network->can_control_mode_flags,
86+
};
11287

113-
if (cm.mask != 0) {
11488
r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
11589
if (r < 0)
11690
return log_link_debug_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
@@ -213,3 +187,43 @@ int config_parse_can_restart_usec(
213187
*restart_usec = usec;
214188
return 0;
215189
}
190+
191+
int config_parse_can_control_mode(
192+
const char* unit,
193+
const char *filename,
194+
unsigned line,
195+
const char *section,
196+
unsigned section_line,
197+
const char *lvalue,
198+
int ltype,
199+
const char *rvalue,
200+
void *data,
201+
void *userdata) {
202+
203+
Network *network = userdata;
204+
uint32_t mask = ltype;
205+
int r;
206+
207+
assert(filename);
208+
assert(lvalue);
209+
assert(rvalue);
210+
assert(userdata);
211+
assert(mask != 0);
212+
213+
if (isempty(rvalue)) {
214+
network->can_control_mode_mask &= ~mask;
215+
network->can_control_mode_flags &= ~mask;
216+
return 0;
217+
}
218+
219+
r = parse_boolean(rvalue);
220+
if (r < 0) {
221+
log_syntax(unit, LOG_WARNING, filename, line, r,
222+
"Failed to parse CAN control mode '%s', ignoring: %s", lvalue, rvalue);
223+
return 0;
224+
}
225+
226+
network->can_control_mode_mask |= mask;
227+
SET_FLAG(network->can_control_mode_flags, mask, r);
228+
return 0;
229+
}

src/network/networkd-can.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* SPDX-License-Identifier: LGPL-2.1-or-later */
22
#pragma once
33

4+
#include <linux/can/netlink.h>
5+
46
#include "sd-netlink.h"
57

68
#include "conf-parser.h"
@@ -11,3 +13,4 @@ int can_set_netlink_message(Link *link, sd_netlink_message *m);
1113

1214
CONFIG_PARSER_PROTOTYPE(config_parse_can_bitrate);
1315
CONFIG_PARSER_PROTOTYPE(config_parse_can_restart_usec);
16+
CONFIG_PARSER_PROTOTYPE(config_parse_can_control_mode);

src/network/networkd-network-gperf.gperf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,13 @@ CAN.BitRate, config_parse_can_bitrate,
348348
CAN.SamplePoint, config_parse_permille, 0, offsetof(Network, can_sample_point)
349349
CAN.DataBitRate, config_parse_can_bitrate, 0, offsetof(Network, can_data_bitrate)
350350
CAN.DataSamplePoint, config_parse_permille, 0, offsetof(Network, can_data_sample_point)
351-
CAN.FDMode, config_parse_tristate, 0, offsetof(Network, can_fd_mode)
352-
CAN.FDNonISO, config_parse_tristate, 0, offsetof(Network, can_non_iso)
353351
CAN.RestartSec, config_parse_can_restart_usec, 0, offsetof(Network, can_restart_us)
354-
CAN.TripleSampling, config_parse_tristate, 0, offsetof(Network, can_triple_sampling)
355-
CAN.BusErrorReporting, config_parse_tristate, 0, offsetof(Network, can_berr_reporting)
352+
CAN.ListenOnly, config_parse_can_control_mode, CAN_CTRLMODE_LISTENONLY, 0
353+
CAN.TripleSampling, config_parse_can_control_mode, CAN_CTRLMODE_3_SAMPLES, 0
354+
CAN.BusErrorReporting, config_parse_can_control_mode, CAN_CTRLMODE_BERR_REPORTING, 0
355+
CAN.FDMode, config_parse_can_control_mode, CAN_CTRLMODE_FD, 0
356+
CAN.FDNonISO, config_parse_can_control_mode, CAN_CTRLMODE_FD_NON_ISO, 0
356357
CAN.Termination, config_parse_tristate, 0, offsetof(Network, can_termination)
357-
CAN.ListenOnly, config_parse_tristate, 0, offsetof(Network, can_listen_only)
358358
QDisc.Parent, config_parse_qdisc_parent, _QDISC_KIND_INVALID, 0
359359
QDisc.Handle, config_parse_qdisc_handle, _QDISC_KIND_INVALID, 0
360360
BFIFO.Parent, config_parse_qdisc_parent, QDISC_KIND_BFIFO, 0

src/network/networkd-network.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
413413
.ipv6_accept_ra_route_metric = DHCP_ROUTE_METRIC,
414414
.ipv6_accept_ra_start_dhcp6_client = IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES,
415415

416-
.can_triple_sampling = -1,
417-
.can_berr_reporting = -1,
418416
.can_termination = -1,
419-
.can_listen_only = -1,
420-
.can_fd_mode = -1,
421-
.can_non_iso = -1,
422417
};
423418

424419
r = config_parse_many(

src/network/networkd-network.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,9 @@ struct Network {
268268
uint32_t can_data_bitrate;
269269
unsigned can_data_sample_point;
270270
usec_t can_restart_us;
271-
int can_triple_sampling;
272-
int can_berr_reporting;
271+
uint32_t can_control_mode_mask;
272+
uint32_t can_control_mode_flags;
273273
int can_termination;
274-
int can_listen_only;
275-
int can_fd_mode;
276-
int can_non_iso;
277274

278275
/* sysctl settings */
279276
AddressFamily ip_forward;

0 commit comments

Comments
 (0)
X Tutup