mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
Now that time_t is unconditionally 64-bit (signed int64_t) and the
struct timespec fields tv_sec / tv_nsec are wide enough on their own,
the explicit (uint64_t)/(int64_t)/(int) casts that used to guard the
multiplications and subtractions in *_us / *_ms / *_ns helpers are no
longer needed. Drop them to keep the timekeeping math readable.
In the same spirit, this commit also normalises the printf-style format
specifiers and casts used to print tv_sec / tv_nsec / tv_usec values.
The prior code was a mix of "%d"/"%u"/"%ld"/"%lu"/"%lld" with matching
(int)/(unsigned long)/(long long) casts; some formats truncated time_t
on 32-bit hosts, others mismatched signedness or width. Replace all
such cases with the portable POSIX-recommended forms:
- tv_sec (time_t, signed, impl-defined width) -> %jd + (intmax_t)
- tv_nsec (long, signed) -> %ld (no cast)
- tv_usec (suseconds_t / long) -> %ld (no cast)
Also drop two stale `(FAR const time_t *)&ts.tv_sec` casts that are
unnecessary now that ts.tv_sec is plain time_t.
Arithmetic-cleanup files (existing scope):
- benchmarks/cyclictest/cyclictest.c: timediff_us()
- benchmarks/sd_bench/sd_bench_main.c: get_time_delta_us()
- examples/oneshot/oneshot_main.c: maxus computation
- examples/watchdog/watchdog_main.c: current_time_ms (x2)
- industry/nxmodbus/nxmb_internal.h: nxmb_util_clock_ms()
- netutils/ntpclient/ntpclient.c: timespec2ntp()
- netutils/ptpd/ptpd.c: ptp_adjtime()
- system/dd/dd_main.c: elapsed accounting
- testing/drivers/drivertest/drivertest_posix_timer.c:
get_timestamp()
- testing/drivers/sd_stress/sd_stress_main.c:get_time_delta()
- testing/sched/getprime/getprime_main.c: elapsed accounting
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c:
timespec_avg()
Printf-format-fix files (new in this revision):
- examples/adjtime/adjtime_main.c
- examples/charger/charger_main.c
- examples/netpkt/netpkt_ethercat.c
- fsutils/mkfatfs/mkfatfs.c
- graphics/tiff/tiff_initialize.c
- netutils/ptpd/ptpd.c
- nshlib/nsh_timcmds.c
- system/coredump/coredump.c
- system/ptpd/ptpd_main.c
- testing/drivers/drivertest/drivertest_oneshot.c
- testing/mm/kasantest/kasantest.c
- testing/ostest/semtimed.c
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
- testing/sched/timerjitter/timerjitter.c
- testing/testsuites/kernel/time/cases/clock_test_clock01.c
- testing/testsuites/kernel/time/cases/clock_test_smoke.c
No behavioural change.
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
131 lines
3.7 KiB
C
131 lines
3.7 KiB
C
/****************************************************************************
|
|
* apps/examples/netpkt/netpkt_ethercat.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#include <net/if.h>
|
|
#include <netinet/if_ether.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/ip.h>
|
|
#include <netpacket/packet.h>
|
|
#include <sys/socket.h>
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
static void usage(void)
|
|
{
|
|
printf("usage: netpkt_ethercat [<ifname> [times]]\n");
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
int main(int argc, FAR const char *argv[])
|
|
{
|
|
struct timespec send_time;
|
|
struct timespec recv_time;
|
|
struct sockaddr_ll addr;
|
|
uint8_t sendbuff[60] =
|
|
{
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9c, 0x7f, 0x81, 0x3c, 0x1e, 0x8e,
|
|
0x88, 0xa4, 0x0e, 0x10, 0x07, 0x4a, 0x00, 0x00, 0x30, 0x01, 0x02, 0x00,
|
|
};
|
|
|
|
uint8_t recvbuff[100];
|
|
FAR const char *ifname = "eth0";
|
|
int repeat = 10;
|
|
int num_packets = 0;
|
|
int len;
|
|
int ifindex;
|
|
int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
|
|
|
if (sockfd == -1)
|
|
{
|
|
perror("ERROR: creating socket failed");
|
|
return -errno;
|
|
}
|
|
|
|
if (argc >= 2)
|
|
{
|
|
ifname = argv[1];
|
|
}
|
|
|
|
ifindex = if_nametoindex(ifname);
|
|
|
|
if (argc >= 3)
|
|
{
|
|
repeat = atoi(argv[2]);
|
|
}
|
|
|
|
if (ifindex == 0)
|
|
{
|
|
printf("Failed to get index of device %s\n", ifname);
|
|
close(sockfd);
|
|
usage();
|
|
return -errno;
|
|
}
|
|
|
|
addr.sll_family = AF_PACKET;
|
|
addr.sll_ifindex = ifindex;
|
|
addr.sll_protocol = htons(ETH_P_ALL);
|
|
if (bind(sockfd, (FAR const struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
{
|
|
perror("ERROR: binding socket failed");
|
|
close(sockfd);
|
|
usage();
|
|
return -errno;
|
|
}
|
|
|
|
for (num_packets = 0; num_packets < repeat; num_packets++)
|
|
{
|
|
clock_gettime(CLOCK_REALTIME, &send_time);
|
|
if (sendto(sockfd, sendbuff, 60, 0, NULL, 0) < 0)
|
|
{
|
|
perror("ERROR: sendto socket failed");
|
|
continue;
|
|
}
|
|
|
|
len = recvfrom(sockfd, recvbuff, 100, 0, NULL, NULL);
|
|
clock_gettime(CLOCK_REALTIME, &recv_time);
|
|
printf("Data recv: %d bytes, spent time %jd ns\n", len,
|
|
(intmax_t)(recv_time.tv_sec - send_time.tv_sec) *
|
|
NSEC_PER_SEC + recv_time.tv_nsec - send_time.tv_nsec);
|
|
usleep(1000);
|
|
}
|
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
}
|