summaryrefslogtreecommitdiff
path: root/daemons/gptp/linux/src/linux_hal_generic.hpp
blob: 76b090159590b441e9322fe7a730f0ae967ec9ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/******************************************************************************

  Copyright (c) 2012, Intel Corporation
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   3. Neither the name of the Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived from
      this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

******************************************************************************/

#ifndef LINUX_HAL_GENERIC_HPP
#define LINUX_HAL_GENERIC_HPP

#include <linux_hal_common.hpp>

/**@file*/

struct LinuxTimestamperGenericPrivate;
/**
 * @brief Provides LinuxTimestamperGeneric a private type
 */
typedef struct LinuxTimestamperGenericPrivate * LinuxTimestamperGenericPrivate_t;

#ifdef WITH_IGBLIB
struct LinuxTimestamperIGBPrivate;
typedef struct LinuxTimestamperIGBPrivate * LinuxTimestamperIGBPrivate_t;
#endif

/**
 * @brief Linux timestamper generic interface
 */
class LinuxTimestamperGeneric : public LinuxTimestamper {
private:
	int sd;
	int phc_fd;
	Timestamp crstamp_system;
	Timestamp crstamp_device;
	LinuxTimestamperGenericPrivate_t _private;
	bool cross_stamp_good;
	std::list<Timestamp> rxTimestampList;
	LinuxNetworkInterfaceList iface_list;
#ifdef PTP_HW_CROSSTSTAMP
	bool precise_timestamp_enabled;
#endif

	TicketingLock *net_lock;
	clockid_t system_clockid;

#ifdef WITH_IGBLIB
	LinuxTimestamperIGBPrivate_t igb_private;
#endif

	struct clock_map_t
	{
		clockid_t clockid;
		const char *clock_name;
	};

	static clock_map_t system_clock_map[];

	const char *getClockNameFromId( clockid_t clockid ) const;

public:
	/**
	 * @brief Default constructor. Initializes internal variables
	 */
	LinuxTimestamperGeneric();

	/**
	 * @brief Resets frequency adjustment value to zero and calls
	 * linux system calls for frequency adjustment.
	 * @return TRUE if success, FALSE if error.
	 */
	bool resetFrequencyAdjustment();

	/**
	 * @brief  Calls linux system call for adjusting frequency or phase.
	 * @param  tmx [in] Void pointer that must be cast (and filled in correctly) to
	 * the struct timex
	 * @return TRUE if ok, FALSE if error.
	 */
	bool Adjust( void *tmx ) const;

	/**
	 * @brief  Initializes the Hardware timestamp interface
	 * @param  iface_label [in] Network interface label (used to find the phc index)
	 * @param  iface [in] Network interface
	 * @return FALSE in case of error, TRUE if success.
	 */
	virtual bool HWTimestamper_init
	( InterfaceLabel *iface_label, OSNetworkInterface *iface );

	/**
	 * @brief  Reset the Hardware timestamp interface
	 * @return void
	 */
	virtual void HWTimestamper_reset();

	virtual bool HWTimestamper_setsystemclock
	( const char *system_clock_desc );

	/**
	 * @brief  Inserts a new timestamp to the beginning of the
	 * RX timestamp list.
	 * @param tstamp [in] RX timestamp
	 * @return void
	 */
	void pushRXTimestamp( Timestamp *tstamp ) {
		tstamp->_version = version;
		rxTimestampList.push_front(*tstamp);
	}

	/**
	 * @brief  Post initialization procedure.
	 * @param  ifindex struct ifreq.ifr_ifindex value
	 * @param  sd Socket file descriptor
	 * @param  lock [in] Instance of TicketingLock object
	 * @return TRUE if ok. FALSE if error.
	 */
	bool post_init( int ifindex, int sd, TicketingLock *lock );

	/**
	 * @brief  Gets the ptp clock time information
	 * @param  system_time [out] System time
	 * @param  device_time [out] Device time
	 * @param  local_clock Not Used
	 * @param  nominal_clock_rate Not Used
	 * @return TRUE if got the time successfully, FALSE otherwise
	 */
	virtual bool HWTimestamper_gettime
	( Timestamp *system_time, Timestamp *device_time,
	  uint32_t *local_clock, uint32_t *nominal_clock_rate ) const;

	/**
	 * @brief  Gets the TX timestamp from hardware interface
	 * @param  identity PTP port identity
	 * @param  PTPMessageId Message ID
	 * @param  timestamp [out] Timestamp value
	 * @param  clock_value [out] Clock value
	 * @param  last Signalizes that it is the last timestamp to get. When TRUE, releases the lock when its done.
	 * @return GPTP_EC_SUCCESS if no error, GPTP_EC_FAILURE if error and GPTP_EC_EAGAIN to try again.
	 */
	virtual int HWTimestamper_txtimestamp
	( PortIdentity *identity, PTPMessageId messageId, Timestamp &timestamp,
	  unsigned &clock_value, bool last );

	/**
	 * @brief  Gets the RX timestamp from the hardware interface. This
	 * Currently the RX timestamp is retrieved at LinuxNetworkInterface::nrecv method.
	 * @param  identity PTP port identity
	 * @param  PTPMessageId Message ID
	 * @param  timestamp [out] Timestamp value
	 * @param  clock_value [out] Clock value
	 * @param  last Signalizes that it is the last timestamp to get. When TRUE, releases the lock when its done.
     * @return GPTP_EC_SUCCESS if no error, GPTP_EC_FAILURE if error and GPTP_EC_EAGAIN to try again.
	 */
	virtual int HWTimestamper_rxtimestamp
	( PortIdentity *identity, PTPMessageId messageId, Timestamp &timestamp,
	  unsigned &clock_value, bool last ) {
		/* This shouldn't happen. Ever. */
		if( rxTimestampList.empty() ) return GPTP_EC_EAGAIN;
		timestamp = rxTimestampList.back();
		rxTimestampList.pop_back();

		return GPTP_EC_SUCCESS;
	}

	/**
	 * @brief  Adjusts the clock phase
	 * @param  phase_adjust Phase adjustment
	 * @return TRUE if success, FALSE if error.
	 */
	virtual bool HWTimestamper_adjclockphase( int64_t phase_adjust );

	/**
	 * @brief  Adjusts the frequency
	 * @param  freq_offset Frequency adjustment
	 * @return TRUE in case of sucess, FALSE if error.
	 */
	virtual bool HWTimestamper_adjclockrate( float freq_offset ) const;

#ifdef WITH_IGBLIB
	bool HWTimestamper_PPS_start( );
	bool HWTimestamper_PPS_stop();
#endif

	/**
	 * @brief deletes LinuxTimestamperGeneric object
	 */
	virtual ~LinuxTimestamperGeneric();
};


#endif/*LINUX_HAL_GENERIC_HPP*/