software/drivers/sd8887/src/wlan/mapp/mlanutl/timestamp.c
/**
 *@brief      Send Timestamps
 *
 *@param argc Number of arguments
 *@param argv Pointer to the arguments array
 *
 *@return     MLAN_STATUS_SUCCESS/MLAN_STATUS_FAILURE
 **/
@brief      Send Timestamps
@param argc Number of arguments
@param argv Pointer to the arguments array


@brief Send Timestamps @param argc Number of arguments @param argv Pointer to the arguments array
int
send_timestamp(int argc, char *argv[])
{
	int sockfd;
	struct ifreq if_idx;
	struct ifreq if_mac;
	int tx_len = 0, i;
	char sendbuf[BUF_SIZ];
	char buff[BUF_SIZ];
	struct ether_header *eh = (struct ether_header *)sendbuf;
	struct sockaddr_ll socket_address;
	char ifName[IFNAMSIZ], ip[50];
	struct timeval delta;
	fd_set readfs, errorfs;
	int res, siocgstamp = 1, siocgstampns = 1;
	int so_timestamping_flags = SOF_TIMESTAMPING_TX_HARDWARE;
	struct ifreq hwtstamp;
	struct hwtstamp_config hwconfig;
	so_timestamping_flags |= SOF_TIMESTAMPING_RAW_HARDWARE;
	so_timestamping_flags |= SOF_TIMESTAMPING_TX_HARDWARE;
	so_timestamping_flags |= SOF_TIMESTAMPING_SYS_HARDWARE;
	if (argc > 4) {
		strcpy(ifName, argv[1]);
		strcpy(ip, argv[4]);
	} else {
		fprintf(stderr, "invalid no. of args for send_timestamp\n");
		exit(1);
	}
	if ((sockfd = socket(PF_PACKET, SOCK_RAW, ETH_P_802_EX1)) == -1) {
		perror("socket");
	}
	memset(&hwtstamp, 0, sizeof(hwtstamp));
	strncpy(hwtstamp.ifr_name, ifName, sizeof(hwtstamp.ifr_name));
	hwtstamp.ifr_data = (void *)&hwconfig;
	memset(&hwconfig, 0, sizeof(hwconfig));
	hwconfig.tx_type =
		(so_timestamping_flags & SOF_TIMESTAMPING_TX_HARDWARE) ?
		HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
	hwconfig.rx_filter =
		(so_timestamping_flags & SOF_TIMESTAMPING_RX_HARDWARE) ?
		HWTSTAMP_FILTER_PTP_V1_L4_SYNC : HWTSTAMP_FILTER_NONE;
	memset(&if_idx, 0, sizeof(struct ifreq));
	strncpy(if_idx.ifr_name, ifName, IFNAMSIZ - 1);
	if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0)
		perror("SIOCGIFINDEX");
	memset(&if_mac, 0, sizeof(struct ifreq));
	strncpy(if_mac.ifr_name, ifName, IFNAMSIZ - 1);
	if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0)
		perror("SIOCGIFHWADDR");
	if (so_timestamping_flags &&
	    setsockopt(sockfd, SOL_SOCKET, SO_TIMESTAMPING,
		       &so_timestamping_flags,
		       sizeof(so_timestamping_flags)) < 0)
		perror("setsockopt SO_TIMESTAMPING");
	if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
		       ifName, IFNAMSIZ - 1) == -1) {
		perror("bind");
		exit(1);
	}
	memset(sendbuf, 0, BUF_SIZ);
	memcpy(eh->ether_shost, (u_int8_t *) & if_mac.ifr_hwaddr.sa_data,
	       MLAN_MAC_ADDR_LENGTH);
	eh->ether_type = htons(ETH_P_802_EX1);
	tx_len += sizeof(struct ether_header);
	get_mac(ifName, ip);
	for (i = 0; i < MLAN_MAC_ADDR_LENGTH; i++) {
		eh->ether_dhost[i] = (uint8_t) inter.mac[i];
	}
	socket_address.sll_ifindex = if_idx.ifr_ifindex;
	socket_address.sll_halen = ETH_ALEN;
	memcpy(&socket_address.sll_addr, (uint8_t *) & inter.mac,
	       MLAN_MAC_ADDR_LENGTH);
	clock_gettime(CLOCK_REALTIME, &send_time);
	sprintf(buff, "%lld.%lld", (long long)send_time.tv_sec,
		(long long)send_time.tv_nsec);
	strcpy((sendbuf + tx_len), buff);
	res = sendto(sockfd, sendbuf, tx_len + strlen(buff), 0,
		     (struct sockaddr *)&socket_address,
		     sizeof(struct sockaddr_ll));
	if (res < 0)
		perror("Send ");
	fprintf(stdout, "Application time : %lld.%09lld (sent)\n",
		(long long)send_time.tv_sec, (long long)send_time.tv_nsec);
	delta.tv_sec = 5;
	delta.tv_usec = 0;
	FD_ZERO(&readfs);
	FD_ZERO(&errorfs);
	FD_SET(sockfd, &readfs);
	FD_SET(sockfd, &errorfs);
	res = select(sockfd + 1, &readfs, 0, &errorfs, &delta);
	if (res > 0) {
		recvpacket(sockfd, MSG_ERRQUEUE, siocgstamp, siocgstampns);
	}
	return MLAN_STATUS_SUCCESS;
}
int send_timestamp ( int argc , char * argv [ ] ) { int sockfd ; struct ifreq if_idx ; struct ifreq if_mac ; int tx_len = 0 , i ; char sendbuf [ BUF_SIZ ] ; char buff [ BUF_SIZ ] ; struct ether_header * eh = ( struct ether_header * ) sendbuf ; struct sockaddr_ll socket_address ; char ifName [ IFNAMSIZ ] , ip [ 50 ] ; struct timeval delta ; fd_set readfs , errorfs ; int res , siocgstamp = 1 , siocgstampns = 1 ; int so_timestamping_flags = SOF_TIMESTAMPING_TX_HARDWARE ; struct ifreq hwtstamp ; struct hwtstamp_config hwconfig ; so_timestamping_flags |= SOF_TIMESTAMPING_RAW_HARDWARE ; so_timestamping_flags |= SOF_TIMESTAMPING_TX_HARDWARE ; so_timestamping_flags |= SOF_TIMESTAMPING_SYS_HARDWARE ; if ( argc > 4 ) { strcpy ( ifName , argv [ 1 ] ) ; strcpy ( ip , argv [ 4 ] ) ; } else { fprintf ( stderr , " \n " ) ; exit ( 1 ) ; } if ( ( sockfd = socket ( PF_PACKET , SOCK_RAW , ETH_P_802_EX1 ) ) == -1 ) { perror ( " " ) ; } memset ( & hwtstamp , 0 , sizeof ( hwtstamp ) ) ; strncpy ( hwtstamp . ifr_name , ifName , sizeof ( hwtstamp . ifr_name ) ) ; hwtstamp . ifr_data = ( void * ) & hwconfig ; memset ( & hwconfig , 0 , sizeof ( hwconfig ) ) ; hwconfig . tx_type = ( so_timestamping_flags & SOF_TIMESTAMPING_TX_HARDWARE ) ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF ; hwconfig . rx_filter = ( so_timestamping_flags & SOF_TIMESTAMPING_RX_HARDWARE ) ? HWTSTAMP_FILTER_PTP_V1_L4_SYNC : HWTSTAMP_FILTER_NONE ; memset ( & if_idx , 0 , sizeof ( struct ifreq ) ) ; strncpy ( if_idx . ifr_name , ifName , IFNAMSIZ - 1 ) ; if ( ioctl ( sockfd , SIOCGIFINDEX , & if_idx ) < 0 ) perror ( " " ) ; memset ( & if_mac , 0 , sizeof ( struct ifreq ) ) ; strncpy ( if_mac . ifr_name , ifName , IFNAMSIZ - 1 ) ; if ( ioctl ( sockfd , SIOCGIFHWADDR , & if_mac ) < 0 ) perror ( " " ) ; if ( so_timestamping_flags && setsockopt ( sockfd , SOL_SOCKET , SO_TIMESTAMPING , & so_timestamping_flags , sizeof ( so_timestamping_flags ) ) < 0 ) perror ( " " ) ; if ( setsockopt ( sockfd , SOL_SOCKET , SO_BINDTODEVICE , ifName , IFNAMSIZ - 1 ) == -1 ) { perror ( " " ) ; exit ( 1 ) ; } memset ( sendbuf , 0 , BUF_SIZ ) ; memcpy ( eh -> ether_shost , ( u_int8_t * ) & if_mac . ifr_hwaddr . sa_data , MLAN_MAC_ADDR_LENGTH ) ; eh -> ether_type = htons ( ETH_P_802_EX1 ) ; tx_len += sizeof ( struct ether_header ) ; get_mac ( ifName , ip ) ; for ( i = 0 ; i < MLAN_MAC_ADDR_LENGTH ; i ++ ) { eh -> ether_dhost [ i ] = ( uint8_t ) inter . mac [ i ] ; } socket_address . sll_ifindex = if_idx . ifr_ifindex ; socket_address . sll_halen = ETH_ALEN ; memcpy ( & socket_address . sll_addr , ( uint8_t * ) & inter . mac , MLAN_MAC_ADDR_LENGTH ) ; clock_gettime ( CLOCK_REALTIME , & send_time ) ; sprintf ( buff , " " , ( long long ) send_time . tv_sec , ( long long ) send_time . tv_nsec ) ; strcpy ( ( sendbuf + tx_len ) , buff ) ; res = sendto ( sockfd , sendbuf , tx_len + strlen ( buff ) , 0 , ( struct sockaddr * ) & socket_address , sizeof ( struct sockaddr_ll ) ) ; if ( res < 0 ) perror ( " " ) ; fprintf ( stdout , " \n " , ( long long ) send_time . tv_sec , ( long long ) send_time . tv_nsec ) ; delta . tv_sec = 5 ; delta . tv_usec = 0 ; FD_ZERO ( & readfs ) ; FD_ZERO ( & errorfs ) ; FD_SET ( sockfd , & readfs ) ; FD_SET ( sockfd , & errorfs ) ; res = select ( sockfd + 1 , & readfs , 0 , & errorfs , & delta ) ; if ( res > 0 ) { recvpacket ( sockfd , MSG_ERRQUEUE , siocgstamp , siocgstampns ) ; } return MLAN_STATUS_SUCCESS ; }
@brief      Send Timestamps
@param argc Number of arguments
@param argv Pointer to the arguments array
@brief Send Timestamps @param argc Number of arguments @param argv Pointer to the arguments array
/* Get interface name */ /* Open RAW socket to send on */ /* Get the index of the interface to send on */ /* Get the MAC address of the interface to send on */ /* Construct the Ethernet header */ /* Ethernet header */ /* Index of the network device */ /* Address length */ /* Send packet */