Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2557005pxb; Sat, 2 Oct 2021 22:06:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfAjrvzICEXbm5iQ1kY+fQ8ULzwdQ2AdsbilVGDJaWOPGYdkvQVpk4qjRQ/05aUU9+MYlN X-Received: by 2002:a17:906:5282:: with SMTP id c2mr8666903ejm.162.1633237607102; Sat, 02 Oct 2021 22:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633237607; cv=none; d=google.com; s=arc-20160816; b=ApC2GPEwQ8XTtpmGMIavisWIMI5aDFPpqhr44YXVXuZKJgDHwIEeADjOz4psBD9YNG CI95a0PSeiTYzqoJugZ0YfWn3Rt775jRuUD2+rkWhzzh++fxmirMPe1219ehXx9x4nxi DrxXRokj561oXBJm86fMnJQaM7v1ya+2SZ3L65irG97C1cAXNoidsrIvzNzsbcqPvrfb d3X4xbGwDS4HblFenpAu+8Jj1jKcpgh9gLD2UqzYTEagNmf/mL5kSBD40YdhvCRNmSh9 KHno0MhoYobEAzGOehrvODj+ROMa/j9W/Yny315LRigX48mXVONJgIaCmLSgN3ObLBh2 dBSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=+1UzwNguxGE7+FewMiqiToFQApbAkXAnrDJ6VzgkZ1Q=; b=mKiAtjcUWXFU9FT7jsc0qqAIyWfskrRO7ar3EXypY7S/VLmm0OJdUb7Ahs9cj522zb He3d6kLcsFT66QUWCbczsxIByqCzpRjEzTTEkn48TxBs3rtSJzV1X4Iztl0LUtcAwYBO O/KMPeFH61VQcyfkZvH50WuAnKbCciNp1I7WXHslynCWS0GM68zmoF/iAfXlsomVXsS2 ErPZS+1YvFZvIb6lOQgTMYDVYhnOFH/maiWbgyuqzo+oKBVjQ3gmZQrukIsSDx6kFxcs /fxDpQbv84nE9KeneyehAlhzsABX2KatszFTtA383VPFqecCuB4E9T6FhYLmSLTv4iQr CFcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j22si6180730ejy.520.2021.10.02.22.06.23; Sat, 02 Oct 2021 22:06:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229735AbhJCFEn (ORCPT + 99 others); Sun, 3 Oct 2021 01:04:43 -0400 Received: from smtp04.smtpout.orange.fr ([80.12.242.126]:60857 "EHLO smtp.smtpout.orange.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbhJCFEl (ORCPT ); Sun, 3 Oct 2021 01:04:41 -0400 Received: from tomoyo.flets-east.jp ([114.149.34.46]) by smtp.orange.fr with ESMTPA id WtdMmhDZXsoWhWteGm9Qbb; Sun, 03 Oct 2021 07:02:54 +0200 X-ME-Helo: tomoyo.flets-east.jp X-ME-Auth: MDU0YmViZGZmMDIzYiBlMiM2NTczNTRjNWZkZTMwOGRiOGQ4ODf3NWI1ZTMyMzdiODlhOQ== X-ME-Date: Sun, 03 Oct 2021 07:02:54 +0200 X-ME-IP: 114.149.34.46 From: Vincent Mailhol To: Marc Kleine-Budde , linux-can@vger.kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Vincent Mailhol Subject: [RFC PATCH v1 1/3] iplink_can: code refactoring of print_ctrlmode() Date: Sun, 3 Oct 2021 14:01:45 +0900 Message-Id: <20211003050147.569044-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211003050147.569044-1-mailhol.vincent@wanadoo.fr> References: <20211003050147.569044-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We do some code refactoring of print_ctrlmode() in prevision of the upcoming patch: - add a new function argument: enum output_type t in order to specify the output type (i.e. PRINT_{FP,JSON,ANY}). - add a new function argument: const char *key in order to specify the name of the json array (e.g. "ctrlmode"). This will be use in the upcoming patch to specify other entries: "ctrlmode_supported" and "ctrlmode_static". - replace the _PF() macro with the print_flag() function to increase readability. - directly return if none of the flags are set (previously, this check was done before calling the function). Signed-off-by: Vincent Mailhol --- --- ip/iplink_can.c | 50 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/ip/iplink_can.c b/ip/iplink_can.c index 6a26f3ff..c70c420d 100644 --- a/ip/iplink_can.c +++ b/ip/iplink_can.c @@ -88,27 +88,36 @@ static void set_ctrlmode(char *name, char *arg, cm->mask |= flags; } -static void print_ctrlmode(FILE *f, __u32 cm) +static void print_flag(enum output_type t, __u32 *flags, __u32 flag, + const char* name) { - open_json_array(PRINT_ANY, is_json_context() ? "ctrlmode" : "<"); -#define _PF(cmflag, cmname) \ - if (cm & cmflag) { \ - cm &= ~cmflag; \ - print_string(PRINT_ANY, NULL, cm ? "%s," : "%s", cmname); \ + if (*flags & flag) { + *flags &= ~flag; + print_string(t, NULL, *flags ? "%s, " : "%s", name); } - _PF(CAN_CTRLMODE_LOOPBACK, "LOOPBACK"); - _PF(CAN_CTRLMODE_LISTENONLY, "LISTEN-ONLY"); - _PF(CAN_CTRLMODE_3_SAMPLES, "TRIPLE-SAMPLING"); - _PF(CAN_CTRLMODE_ONE_SHOT, "ONE-SHOT"); - _PF(CAN_CTRLMODE_BERR_REPORTING, "BERR-REPORTING"); - _PF(CAN_CTRLMODE_FD, "FD"); - _PF(CAN_CTRLMODE_FD_NON_ISO, "FD-NON-ISO"); - _PF(CAN_CTRLMODE_PRESUME_ACK, "PRESUME-ACK"); - _PF(CAN_CTRLMODE_CC_LEN8_DLC, "CC-LEN8-DLC"); -#undef _PF - if (cm) - print_hex(PRINT_ANY, NULL, "%x", cm); - close_json_array(PRINT_ANY, "> "); +} + +static void print_ctrlmode(enum output_type t, __u32 flags, const char* key) +{ + if (!flags) + return; + + open_json_array(t, is_json_context() ? key : "<"); + + print_flag(t, &flags, CAN_CTRLMODE_LOOPBACK, "LOOPBACK"); + print_flag(t, &flags, CAN_CTRLMODE_LISTENONLY, "LISTEN-ONLY"); + print_flag(t, &flags, CAN_CTRLMODE_3_SAMPLES, "TRIPLE-SAMPLING"); + print_flag(t, &flags, CAN_CTRLMODE_ONE_SHOT, "ONE-SHOT"); + print_flag(t, &flags, CAN_CTRLMODE_BERR_REPORTING, "BERR-REPORTING"); + print_flag(t, &flags, CAN_CTRLMODE_FD, "FD"); + print_flag(t, &flags, CAN_CTRLMODE_FD_NON_ISO, "FD-NON-ISO"); + print_flag(t, &flags, CAN_CTRLMODE_PRESUME_ACK, "PRESUME-ACK"); + print_flag(t, &flags, CAN_CTRLMODE_CC_LEN8_DLC, "CC-LEN8-DLC"); + + if (flags) + print_hex(t, NULL, "%x", flags); + + close_json_array(t, "> "); } static int can_parse_opt(struct link_util *lu, int argc, char **argv, @@ -282,8 +291,7 @@ static void can_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_CAN_CTRLMODE]) { struct can_ctrlmode *cm = RTA_DATA(tb[IFLA_CAN_CTRLMODE]); - if (cm->flags) - print_ctrlmode(f, cm->flags); + print_ctrlmode(PRINT_ANY, cm->flags, "ctrlmode"); } if (tb[IFLA_CAN_STATE]) { -- 2.32.0