mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
net: fix compiler warning
sixlowpan_assocresp.c: In function ‘sixlowpan_assoc_resp’:
sixlowpan_assocresp.c:48:3: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]
48 | strncpy(arg.ifr_name, ifname, IFNAMSIZ);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
parent
f102f6f405
commit
8ad4ae5508
28 changed files with 182 additions and 130 deletions
|
|
@ -1,5 +1,26 @@
|
|||
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
|
||||
/*
|
||||
/****************************************************************************
|
||||
* apps/canutils/cansend/cansend.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) *
|
||||
*
|
||||
* cansend.c - send CAN-frames via CAN_RAW sockets
|
||||
*
|
||||
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
|
||||
|
|
@ -40,7 +61,11 @@
|
|||
*
|
||||
* Send feedback to <linux-can@vger.kernel.org>
|
||||
*
|
||||
*/
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -58,112 +83,141 @@
|
|||
|
||||
static void print_usage_send(char *prg)
|
||||
{
|
||||
fprintf(stderr, "%s - send CAN-frames via CAN_RAW sockets.\n", prg);
|
||||
fprintf(stderr, "\nUsage: %s <device> <can_frame>.\n", prg);
|
||||
fprintf(stderr, "\n<can_frame>:\n");
|
||||
fprintf(stderr, " <can_id>#{data} for 'classic' CAN 2.0 data frames\n");
|
||||
fprintf(stderr, " <can_id>#R{len} for 'classic' CAN 2.0 data frames\n");
|
||||
fprintf(stderr, " <can_id>##<flags>{data} for CAN FD frames\n\n");
|
||||
fprintf(stderr, "<can_id>:\n"
|
||||
" 3 (SFF) or 8 (EFF) hex chars\n");
|
||||
fprintf(stderr, "{data}:\n"
|
||||
" 0..8 (0..64 CAN FD) ASCII hex-values (optionally separated by '.')\n");
|
||||
fprintf(stderr, "{len}:\n"
|
||||
" an optional 0..8 value as RTR frames can contain a valid dlc field\n");
|
||||
fprintf(stderr, "<flags>:\n"
|
||||
" a single ASCII Hex value (0 .. F) which defines canfd_frame.flags\n\n");
|
||||
fprintf(stderr, "Examples:\n");
|
||||
fprintf(stderr, " 5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / 123##1 / 213##311223344 /\n"
|
||||
" 1F334455#1122334455667788 / 123#R / 00000123#R3\n\n");
|
||||
fprintf(stderr, "%s - send CAN-frames via CAN_RAW sockets.\n", prg);
|
||||
fprintf(stderr, "\nUsage: %s <device> <can_frame>.\n", prg);
|
||||
fprintf(stderr, "\n<can_frame>:\n");
|
||||
fprintf(stderr,
|
||||
" <can_id>#{data} ""for 'classic' CAN 2.0 data frames\n");
|
||||
fprintf(stderr,
|
||||
" <can_id>#R{len} for 'classic' CAN 2.0 data frames\n");
|
||||
fprintf(stderr,
|
||||
" <can_id>##<flags>{data} for CAN FD frames\n\n");
|
||||
fprintf(stderr, "<can_id>:\n"
|
||||
" 3 (SFF) or 8 (EFF) hex chars\n");
|
||||
fprintf(stderr, "{data}:\n"
|
||||
" 0..8 (0..64 CAN FD) ASCII hex-values "
|
||||
"(optionally separated by '.')\n");
|
||||
fprintf(stderr, "{len}:\n"
|
||||
" an optional 0..8 value "
|
||||
"as RTR frames can contain a valid dlc field\n");
|
||||
fprintf(stderr, "<flags>:\n"
|
||||
" a single ASCII Hex value (0 .. F) "
|
||||
"which defines canfd_frame.flags\n\n");
|
||||
fprintf(stderr, "Examples:\n");
|
||||
fprintf(stderr, " 5A1#11.2233.44556677.88 / 123#DEADBEEF / "
|
||||
"5AA# / 123##1 / 213##311223344 /\n"
|
||||
" 1F334455#1122334455667788 / 123#R / 00000123#R3\n\n");
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int s; /* can raw socket */
|
||||
int required_mtu;
|
||||
int mtu;
|
||||
int enable_canfd = 1;
|
||||
struct sockaddr_can addr;
|
||||
struct canfd_frame frame;
|
||||
struct ifreq ifr;
|
||||
/* can raw socket */
|
||||
|
||||
/* check command line options */
|
||||
if (argc != 3) {
|
||||
print_usage_send(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
int s;
|
||||
int required_mtu;
|
||||
int mtu;
|
||||
int enable_canfd = 1;
|
||||
struct sockaddr_can addr;
|
||||
struct canfd_frame frame;
|
||||
struct ifreq ifr;
|
||||
|
||||
/* parse CAN frame */
|
||||
required_mtu = parse_canframe(argv[2], &frame);
|
||||
if (!required_mtu){
|
||||
fprintf(stderr, "\nWrong CAN-frame format!\n\n");
|
||||
print_usage_send(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
/* check command line options */
|
||||
|
||||
/* open socket */
|
||||
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
if (argc != 3)
|
||||
{
|
||||
print_usage_send(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
|
||||
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
|
||||
ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
|
||||
if (!ifr.ifr_ifindex) {
|
||||
perror("if_nametoindex");
|
||||
return 1;
|
||||
}
|
||||
/* parse CAN frame */
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.can_family = AF_CAN;
|
||||
addr.can_ifindex = ifr.ifr_ifindex;
|
||||
required_mtu = parse_canframe(argv[2], &frame);
|
||||
if (!required_mtu)
|
||||
{
|
||||
fprintf(stderr, "\nWrong CAN-frame format!\n\n");
|
||||
print_usage_send(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (required_mtu > (int)CAN_MTU) {
|
||||
/* open socket */
|
||||
|
||||
/* check if the frame fits into the CAN netdevice */
|
||||
if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
|
||||
perror("SIOCGIFMTU");
|
||||
return 1;
|
||||
}
|
||||
mtu = ifr.ifr_mtu;
|
||||
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
|
||||
{
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mtu != CANFD_MTU) {
|
||||
printf("CAN interface is not CAN FD capable - sorry.\n");
|
||||
return 1;
|
||||
}
|
||||
strlcpy(ifr.ifr_name, argv[1], IFNAMSIZ);
|
||||
ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
|
||||
if (!ifr.ifr_ifindex)
|
||||
{
|
||||
perror("if_nametoindex");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* interface is ok - try to switch the socket into CAN FD mode */
|
||||
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
|
||||
&enable_canfd, sizeof(enable_canfd))){
|
||||
printf("error when enabling CAN FD support\n");
|
||||
return 1;
|
||||
}
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.can_family = AF_CAN;
|
||||
addr.can_ifindex = ifr.ifr_ifindex;
|
||||
|
||||
/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
|
||||
frame.len = can_dlc2len(can_len2dlc(frame.len));
|
||||
}
|
||||
if (required_mtu > (int)CAN_MTU)
|
||||
{
|
||||
/* check if the frame fits into the CAN netdevice */
|
||||
|
||||
/* disable default receive filter on this RAW socket */
|
||||
/* This is obsolete as we do not read from the socket at all, but for */
|
||||
/* this reason we can remove the receive list in the Kernel to save a */
|
||||
/* little (really a very little!) CPU usage. */
|
||||
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
|
||||
if (ioctl(s, SIOCGIFMTU, &ifr) < 0)
|
||||
{
|
||||
perror("SIOCGIFMTU");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
perror("bind");
|
||||
return 1;
|
||||
}
|
||||
mtu = ifr.ifr_mtu;
|
||||
|
||||
/* send frame */
|
||||
if (write(s, &frame, required_mtu) != required_mtu) {
|
||||
perror("write");
|
||||
return 1;
|
||||
}
|
||||
if (mtu != CANFD_MTU)
|
||||
{
|
||||
printf("CAN interface is not CAN FD capable - sorry.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(s);
|
||||
/* interface is ok - try to switch the socket into CAN FD mode */
|
||||
|
||||
return 0;
|
||||
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
|
||||
&enable_canfd, sizeof(enable_canfd)))
|
||||
{
|
||||
printf("error when enabling CAN FD support\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
|
||||
|
||||
frame.len = can_dlc2len(can_len2dlc(frame.len));
|
||||
}
|
||||
|
||||
/* disable default receive filter on this RAW socket
|
||||
* This is obsolete as we do not read from the socket at all, but for
|
||||
* this reason we can remove the receive list in the Kernel to save a
|
||||
* little (really a very little!) CPU usage.
|
||||
*/
|
||||
|
||||
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
|
||||
|
||||
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* send frame */
|
||||
|
||||
if (write(s, &frame, required_mtu) != required_mtu)
|
||||
{
|
||||
perror("write");
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -361,8 +361,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
/* set the device name */
|
||||
|
||||
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
|
||||
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
|
||||
strlcpy(ifr.ifr_name, argv[1], IFNAMSIZ);
|
||||
|
||||
ifr.ifr_ifru.ifru_can_data.arbi_bitrate =
|
||||
canspeed / 1000; /* Convert bit/s to kbit/s */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue