Skip to content

Commit

Permalink
net: pkt: time: introduce ns timestamp helper
Browse files Browse the repository at this point in the history
A little refactoring that simplifies dealing with nanosecond timestamp
values in packets and further decouples calling code from PTP:

Benefits:
- simplifies calling code by removing redundant conversions.
- prepares for removing PTP dependencies from net_pkt.

Signed-off-by: Florian Grandel <[email protected]>
  • Loading branch information
fg-cfh authored and carlescufi committed Sep 29, 2023
1 parent 8a25b03 commit 36402b6
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 49 deletions.
18 changes: 5 additions & 13 deletions drivers/ieee802154/ieee802154_dw1000.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,11 @@ static inline void dwt_irq_handle_rx(const struct device *dev, uint32_t sys_stat

if (IS_ENABLED(CONFIG_NET_PKT_TIMESTAMP)) {
uint8_t ts_buf[sizeof(uint64_t)] = {0};
struct net_ptp_time timestamp;
uint64_t ts_fsec;
uint64_t ts_nsec;

memcpy(ts_buf, rx_inf_reg.rx_time, DWT_RX_TIME_RX_STAMP_LEN);
ts_fsec = sys_get_le64(ts_buf) * DWT_TS_TIME_UNITS_FS;
timestamp.second = (ts_fsec / 1000000) / NSEC_PER_SEC;
timestamp.nanosecond = (ts_fsec / 1000000) % NSEC_PER_SEC;
net_pkt_set_timestamp(pkt, &timestamp);
ts_nsec = (sys_get_le64(ts_buf) * DWT_TS_TIME_UNITS_FS) / 1000000U;
net_pkt_set_timestamp_ns(pkt, ts_nsec);
}

/* See 4.7.2 Estimating the receive signal power */
Expand Down Expand Up @@ -787,7 +784,6 @@ static int dwt_tx(const struct device *dev, enum ieee802154_tx_mode tx_mode,
struct dwt_context *ctx = dev->data;
size_t len = frag->len;
uint32_t tx_time = 0;
struct net_ptp_time *txts;
uint64_t tmp_fs;
uint32_t tx_fctrl;
uint8_t sys_ctrl = DWT_SYS_CTRL_TXSTRT;
Expand All @@ -808,8 +804,7 @@ static int dwt_tx(const struct device *dev, enum ieee802154_tx_mode tx_mode,
* tx_time is the high 32-bit of the 40-bit system
* time value at which to send the message.
*/
txts = net_pkt_timestamp(pkt);
tmp_fs = txts->second * NSEC_PER_SEC + txts->nanosecond;
tmp_fs = net_pkt_timestamp_ns(pkt);
tmp_fs *= 1000U * 1000U;

tx_time = (tmp_fs / DWT_TS_TIME_UNITS_FS) >> 8;
Expand Down Expand Up @@ -866,7 +861,6 @@ static int dwt_tx(const struct device *dev, enum ieee802154_tx_mode tx_mode,

if (IS_ENABLED(CONFIG_NET_PKT_TIMESTAMP)) {
uint8_t ts_buf[sizeof(uint64_t)] = {0};
struct net_ptp_time timestamp;

k_sem_take(&ctx->dev_lock, K_FOREVER);
dwt_register_read(dev, DWT_TX_TIME_ID,
Expand All @@ -879,9 +873,7 @@ static int dwt_tx(const struct device *dev, enum ieee802154_tx_mode tx_mode,
k_sem_give(&ctx->dev_lock);

tmp_fs = sys_get_le64(ts_buf) * DWT_TS_TIME_UNITS_FS;
timestamp.second = (tmp_fs / 1000000) / NSEC_PER_SEC;
timestamp.nanosecond = (tmp_fs / 1000000) % NSEC_PER_SEC;
net_pkt_set_timestamp(pkt, &timestamp);
net_pkt_set_timestamp_ns(pkt, tmp_fs / 1000000U);
}

atomic_clear_bit(&ctx->state, DWT_STATE_TX);
Expand Down
17 changes: 3 additions & 14 deletions drivers/ieee802154/ieee802154_nrf5.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,7 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);

#if defined(CONFIG_NET_PKT_TIMESTAMP)
struct net_ptp_time timestamp = {
.second = rx_frame->time / USEC_PER_SEC,
.nanosecond =
(rx_frame->time % USEC_PER_SEC) * NSEC_PER_USEC
};

net_pkt_set_timestamp(pkt, &timestamp);
net_pkt_set_timestamp_ns(pkt, rx_frame->time * NSEC_PER_USEC);
#endif

LOG_DBG("Caught a packet (%u) (LQI: %u)",
Expand Down Expand Up @@ -407,12 +401,7 @@ static int handle_ack(struct nrf5_802154_data *nrf5_radio)
net_pkt_set_ieee802154_rssi_dbm(ack_pkt, nrf5_radio->ack_frame.rssi);

#if defined(CONFIG_NET_PKT_TIMESTAMP)
struct net_ptp_time timestamp = {
.second = nrf5_radio->ack_frame.time / USEC_PER_SEC,
.nanosecond = (nrf5_radio->ack_frame.time % USEC_PER_SEC) * NSEC_PER_USEC
};

net_pkt_set_timestamp(ack_pkt, &timestamp);
net_pkt_set_timestamp_ns(ack_pkt, nrf5_radio->ack_frame.time * NSEC_PER_USEC);
#endif

net_pkt_cursor_init(ack_pkt);
Expand Down Expand Up @@ -531,7 +520,7 @@ static bool nrf5_tx_at(struct nrf5_802154_data *nrf5_radio, struct net_pkt *pkt,
* expects a timestamp pointing to start of SHR.
*/
uint64_t tx_at = nrf_802154_timestamp_phr_to_shr_convert(
net_ptp_time_to_ns(net_pkt_timestamp(pkt)) / NSEC_PER_USEC);
net_pkt_timestamp_ns(pkt) / NSEC_PER_USEC);

return nrf_802154_transmit_raw_at(payload, tx_at, &metadata);
}
Expand Down
28 changes: 26 additions & 2 deletions include/zephyr/net/net_pkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,16 @@ static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
pkt->timestamp.second = timestamp->second;
pkt->timestamp.nanosecond = timestamp->nanosecond;
}

static inline net_time_t net_pkt_timestamp_ns(struct net_pkt *pkt)
{
return net_ptp_time_to_ns(&pkt->timestamp);
}

static inline void net_pkt_set_timestamp_ns(struct net_pkt *pkt, net_time_t timestamp)
{
pkt->timestamp = ns_to_net_ptp_time(timestamp);
}
#else
static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
{
Expand All @@ -941,6 +951,19 @@ static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
ARG_UNUSED(pkt);
ARG_UNUSED(timestamp);
}

static inline net_time_t net_pkt_timestamp_ns(struct net_pkt *pkt)
{
ARG_UNUSED(pkt);

return 0;
}

static inline void net_pkt_set_timestamp_ns(struct net_pkt *pkt, net_time_t timestamp)
{
ARG_UNUSED(pkt);
ARG_UNUSED(timestamp);
}
#endif /* CONFIG_NET_PKT_TIMESTAMP || CONFIG_NET_PKT_TXTIME */

#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
Expand Down Expand Up @@ -971,7 +994,7 @@ static inline void net_pkt_set_create_time(struct net_pkt *pkt,
#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */

/**
* @deprecated Use @ref net_pkt_timestamp instead.
* @deprecated Use @ref net_pkt_timestamp or @ref net_pkt_timestamp_ns instead.
*/
static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
{
Expand All @@ -985,7 +1008,8 @@ static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
}

/**
* @deprecated Use @ref net_pkt_set_timestamp instead.
* @deprecated Use @ref net_pkt_set_timestamp or @ref net_pkt_set_timestamp_ns
* instead.
*/
static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
{
Expand Down
16 changes: 3 additions & 13 deletions modules/openthread/platform/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ enum net_verdict ieee802154_handle_ack(struct net_if *iface, struct net_pkt *pkt
ack_frame.mInfo.mRxInfo.mRssi = net_pkt_ieee802154_rssi_dbm(pkt);

#if defined(CONFIG_NET_PKT_TIMESTAMP)
struct net_ptp_time *pkt_time = net_pkt_timestamp(pkt);

/* OpenThread expects the timestamp to point to the end of SFD */
ack_frame.mInfo.mRxInfo.mTimestamp = pkt_time->second * USEC_PER_SEC +
pkt_time->nanosecond / NSEC_PER_USEC;
ack_frame.mInfo.mRxInfo.mTimestamp = net_pkt_timestamp_ns(pkt) / NSEC_PER_USEC;
#endif

return NET_OK;
Expand Down Expand Up @@ -396,9 +392,7 @@ void transmit_message(struct k_work *tx_job)
#if defined(CONFIG_NET_PKT_TXTIME)
uint32_t tx_at = sTransmitFrame.mInfo.mTxInfo.mTxDelayBaseTime +
sTransmitFrame.mInfo.mTxInfo.mTxDelay;
struct net_ptp_time timestamp =
ns_to_net_ptp_time(convert_32bit_us_wrapped_to_64bit_ns(tx_at));
net_pkt_set_timestamp(tx_pkt, &timestamp);
net_pkt_set_timestamp_ns(tx_pkt, convert_32bit_us_wrapped_to_64bit_ns(tx_at));
#endif
tx_err =
radio_api->tx(radio_dev, IEEE802154_TX_MODE_TXTIME_CCA, tx_pkt, tx_payload);
Expand Down Expand Up @@ -476,11 +470,7 @@ static void openthread_handle_received_frame(otInstance *instance,
recv_frame.mInfo.mRxInfo.mAckedWithFramePending = net_pkt_ieee802154_ack_fpb(pkt);

#if defined(CONFIG_NET_PKT_TIMESTAMP)
struct net_ptp_time *pkt_time = net_pkt_timestamp(pkt);

/* OpenThread expects the timestamp to point to the end of SFD */
recv_frame.mInfo.mRxInfo.mTimestamp =
pkt_time->second * USEC_PER_SEC + pkt_time->nanosecond / NSEC_PER_USEC;
recv_frame.mInfo.mRxInfo.mTimestamp = net_pkt_timestamp_ns(pkt) / NSEC_PER_USEC;
#endif

if (net_pkt_ieee802154_arb(pkt) && net_pkt_ieee802154_fv2015(pkt)) {
Expand Down
5 changes: 1 addition & 4 deletions subsys/net/ip/net_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1656,10 +1656,7 @@ static void set_pkt_txtime(struct net_pkt *pkt, const struct msghdr *msghdr)
if (cmsg->cmsg_len == CMSG_LEN(sizeof(uint64_t)) &&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_TXTIME) {
struct net_ptp_time txtime =
ns_to_net_ptp_time(*(net_time_t *)CMSG_DATA(cmsg));

net_pkt_set_timestamp(pkt, &txtime);
net_pkt_set_timestamp_ns(pkt, *(net_time_t *)CMSG_DATA(cmsg));
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/net/socket/udp/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ static int eth_fake_send(const struct device *dev, struct net_pkt *pkt)
return 0;
}

txtime = net_ptp_time_to_ns(net_pkt_timestamp(pkt));
txtime = net_pkt_timestamp_ns(pkt);
if (txtime != TEST_TXTIME) {
test_failed = true;
} else {
Expand Down
3 changes: 1 addition & 2 deletions tests/subsys/openthread/radio_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ ZTEST(openthread_radio, test_tx_test)
zassert_equal(power, set_txpower_mock_fake.arg1_val);
zassert_equal(1, tx_mock_fake.call_count);
zassert_equal_ptr(frm->mPsdu, tx_mock_fake.arg3_val->data, NULL);
zassert_equal(expected_target_time,
net_ptp_time_to_ns(net_pkt_timestamp(tx_mock_fake.arg2_val)));
zassert_equal(expected_target_time, net_pkt_timestamp_ns(tx_mock_fake.arg2_val));
zassert_equal(IS_ENABLED(CONFIG_NET_PKT_TXTIME) ? IEEE802154_TX_MODE_TXTIME_CCA
: IEEE802154_TX_MODE_DIRECT,
tx_mock_fake.arg1_val);
Expand Down

0 comments on commit 36402b6

Please sign in to comment.