flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
nsd_linux.c File Reference
#include "../../network_stack_driver.h"
#include "../../ptp_defs.h"
#include "../../task_ptp.h"
#include <arpa/inet.h>
#include <asm-generic/errno-base.h>
#include <asm/socket.h>
#include <fcntl.h>
#include <linux/ethtool.h>
#include <linux/net_tstamp.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <inttypes.h>
#include <features.h>
#include <asm-generic/unistd.h>
#include <bits/time.h>
#include <linux/if_packet.h>
#include <netinet/ether.h>
#include <poll.h>
#include <pthread.h>
#include <sys/timex.h>

Go to the source code of this file.

Macros

#define LINUX_NSD_TS_DEBUG   (0)
 
#define LINUX_NSD_TX_ENQUEUE_DEBUG   (0)
 
#define PHY_FILE_NAME_SIZE   (16)
 
#define PRINT_HWADDR(a)   MSG("%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);
 
#define NOTIF_QUIT_TRANSCEIVER_THREAD   'Q'
 
#define CLOCKFD   3
 
#define FD_TO_CLOCKID(fd)   ((clockid_t)((((unsigned int)~fd) << 3) | CLOCKFD))
 
#define CLOCKID_TO_FD(clk)   ((unsigned int)~((clk) >> 3))
 
#define NAME_BUF_SIZE   (256)
 
#define MSG_BUF_SIZE   (1600)
 
#define CTRL_BUF_SIZE   (256)
 
#define PPB_TO_TUNING_SCALER   (((double)(1 << 16)) / 1000.0)
 

Functions

static void * nsd_thread (void *arg)
 
static void post_notification (char c)
 
bool linux_nsd_preinit (const char *ifn)
 
void linux_nsd_cleanup (void)
 
static void socket_join_igmp (int fd)
 
void ptp_nsd_igmp_join_leave (bool join)
 
static int open_udp_socket (PtpDelayMechanism dm, uint16_t port, const char *hint)
 
static int open_raw_socket (PtpDelayMechanism dm, bool bind_socket, const char *hint)
 
static void enable_timestamping (int sfd)
 
void ptp_nsd_init (PtpTransportType tp, PtpDelayMechanism dm)
 
void ptp_nsd_transmit_msg (RawPtpMessage *pMsg, uint32_t uid)
 
void ptp_nsd_get_interface_address (uint8_t *hwa)
 
void linux_adjust_clock (double tuning_ppb)
 
void linux_set_time (uint32_t seconds, uint32_t nanoseconds)
 
void linux_get_time (TimestampU *pTime)
 

Variables

static int event_fd = -1
 
static int general_fd = -1
 
static PtpTransportType TP = -1
 
static PtpDelayMechanism DM = -1
 
static uint16_t if_idx
 
static char if_name [IFNAMSIZ]
 
static uint8_t if_hwaddr [IFHWADDRLEN]
 
static struct sockaddr_in if_ipaddr
 
static uint16_t phc_index
 
static char phc_file_name [(16)]
 
static int phc_fd
 
static clockid_t phc_clkid
 
static int notif_q [2]
 
static int matching_q [2]
 
static pthread_t transceiver_thread
 
static char name_buf [(256)]
 
static char msg_buf [(1600)]
 
static char rx_ctrl_buf [(256)]
 

Macro Definition Documentation

◆ CLOCKFD

#define CLOCKFD   3

Definition at line 72 of file nsd_linux.c.

◆ CLOCKID_TO_FD

#define CLOCKID_TO_FD (   clk)    ((unsigned int)~((clk) >> 3))

Definition at line 74 of file nsd_linux.c.

◆ CTRL_BUF_SIZE

#define CTRL_BUF_SIZE   (256)

Definition at line 390 of file nsd_linux.c.

◆ FD_TO_CLOCKID

#define FD_TO_CLOCKID (   fd)    ((clockid_t)((((unsigned int)~fd) << 3) | CLOCKFD))

Definition at line 73 of file nsd_linux.c.

◆ LINUX_NSD_TS_DEBUG

#define LINUX_NSD_TS_DEBUG   (0)

Definition at line 33 of file nsd_linux.c.

◆ LINUX_NSD_TX_ENQUEUE_DEBUG

#define LINUX_NSD_TX_ENQUEUE_DEBUG   (0)

Definition at line 34 of file nsd_linux.c.

◆ MSG_BUF_SIZE

#define MSG_BUF_SIZE   (1600)

Definition at line 388 of file nsd_linux.c.

◆ NAME_BUF_SIZE

#define NAME_BUF_SIZE   (256)

Definition at line 386 of file nsd_linux.c.

◆ NOTIF_QUIT_TRANSCEIVER_THREAD

#define NOTIF_QUIT_TRANSCEIVER_THREAD   'Q'

Definition at line 65 of file nsd_linux.c.

◆ PHY_FILE_NAME_SIZE

#define PHY_FILE_NAME_SIZE   (16)

Definition at line 51 of file nsd_linux.c.

◆ PPB_TO_TUNING_SCALER

#define PPB_TO_TUNING_SCALER   (((double)(1 << 16)) / 1000.0)

Definition at line 616 of file nsd_linux.c.

◆ PRINT_HWADDR

#define PRINT_HWADDR (   a)    MSG("%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);

Definition at line 63 of file nsd_linux.c.

Function Documentation

◆ enable_timestamping()

static void enable_timestamping ( int  sfd)
static

Definition at line 343 of file nsd_linux.c.

◆ linux_adjust_clock()

void linux_adjust_clock ( double  tuning_ppb)

Adjust the PHC.

Parameters
tuning_ppbclock adjustment

Definition at line 618 of file nsd_linux.c.

◆ linux_get_time()

void linux_get_time ( TimestampU pTime)

Query PHC time.

Parameters
pTimepointer to a timestamp object

Definition at line 639 of file nsd_linux.c.

◆ linux_nsd_cleanup()

void linux_nsd_cleanup ( void  )

Clean up the Linux network stack driver.

Definition at line 207 of file nsd_linux.c.

◆ linux_nsd_preinit()

bool linux_nsd_preinit ( const char *  ifn)

Preinitialize the Linux network stack driver.

Parameters
ifnname of the targeted network interface (e.g. eth0)
Returns
initialization successful

Definition at line 76 of file nsd_linux.c.

◆ linux_set_time()

void linux_set_time ( uint32_t  seconds,
uint32_t  nanoseconds 
)

Set PHC time.

Parameters
secondsseconds part
nanosecondsnanoseconds part

Definition at line 632 of file nsd_linux.c.

◆ nsd_thread()

static void * nsd_thread ( void *  arg)
static

Definition at line 393 of file nsd_linux.c.

◆ open_raw_socket()

static int open_raw_socket ( PtpDelayMechanism  dm,
bool  bind_socket,
const char *  hint 
)
static

Definition at line 295 of file nsd_linux.c.

◆ open_udp_socket()

static int open_udp_socket ( PtpDelayMechanism  dm,
uint16_t  port,
const char *  hint 
)
static

Definition at line 246 of file nsd_linux.c.

◆ post_notification()

static void post_notification ( char  c)
static

Definition at line 67 of file nsd_linux.c.

◆ ptp_nsd_get_interface_address()

void ptp_nsd_get_interface_address ( uint8_t *  hwa)

Fetch the Ethernet network interface hardware address.

Parameters
hwapointer to a 6-byte array where the hardware address is going to be stored to

Definition at line 609 of file nsd_linux.c.

◆ ptp_nsd_igmp_join_leave()

void ptp_nsd_igmp_join_leave ( bool  join)

Join or leave IGMP groups associated with the current PTP profile.

Definition at line 232 of file nsd_linux.c.

◆ ptp_nsd_init()

void ptp_nsd_init ( PtpTransportType  tp,
PtpDelayMechanism  dm 
)

Initialize or reinitialize the Network Stack Driver.

Parameters
tpPTP transport type
dmPTP delay mechanism

Definition at line 499 of file nsd_linux.c.

◆ ptp_nsd_transmit_msg()

void ptp_nsd_transmit_msg ( RawPtpMessage pMsg,
uint32_t  uid 
)

Transmit a PTP message.

Parameters
pMsgpointer to a RawPtpMessage
uidUID of the message

Definition at line 552 of file nsd_linux.c.

◆ socket_join_igmp()

static void socket_join_igmp ( int  fd)
static

Definition at line 214 of file nsd_linux.c.

Variable Documentation

◆ DM

PtpDelayMechanism DM = -1
static

Definition at line 42 of file nsd_linux.c.

◆ event_fd

int event_fd = -1
static

Definition at line 37 of file nsd_linux.c.

◆ general_fd

int general_fd = -1
static

Definition at line 38 of file nsd_linux.c.

◆ if_hwaddr

uint8_t if_hwaddr[IFHWADDRLEN]
static

Definition at line 47 of file nsd_linux.c.

◆ if_idx

uint16_t if_idx
static

Definition at line 45 of file nsd_linux.c.

◆ if_ipaddr

struct sockaddr_in if_ipaddr
static

Definition at line 48 of file nsd_linux.c.

◆ if_name

char if_name[IFNAMSIZ]
static

Definition at line 46 of file nsd_linux.c.

◆ matching_q

int matching_q[2]
static

Definition at line 59 of file nsd_linux.c.

◆ msg_buf

char msg_buf[(1600)]
static

Definition at line 389 of file nsd_linux.c.

◆ name_buf

char name_buf[(256)]
static

Definition at line 387 of file nsd_linux.c.

◆ notif_q

int notif_q[2]
static

Definition at line 58 of file nsd_linux.c.

◆ phc_clkid

clockid_t phc_clkid
static

Definition at line 55 of file nsd_linux.c.

◆ phc_fd

int phc_fd
static

Definition at line 54 of file nsd_linux.c.

◆ phc_file_name

char phc_file_name[(16)]
static

Definition at line 53 of file nsd_linux.c.

◆ phc_index

uint16_t phc_index
static

Definition at line 52 of file nsd_linux.c.

◆ rx_ctrl_buf

char rx_ctrl_buf[(256)]
static

Definition at line 391 of file nsd_linux.c.

◆ TP

PtpTransportType TP = -1
static

Definition at line 41 of file nsd_linux.c.

◆ transceiver_thread

pthread_t transceiver_thread
static

Definition at line 60 of file nsd_linux.c.