34 #ifndef WINDOWS_HAL_HPP
35 #define WINDOWS_HAL_HPP
39 #include <minwindef.h>
94 if( pferror != PACKET_NO_ERROR && pferror != PACKET_RECVTIMEOUT_ERROR )
return net_fatal;
95 if( pferror == PACKET_RECVTIMEOUT_ERROR )
return net_trfail;
144 if( addr == NULL )
goto error_nofree;
145 net_iface_l->local_addr = *addr;
148 if(
mallocPacketHandle( &net_iface_l->handle ) != PACKET_NO_ERROR )
goto error_nofree;
149 if(
openInterfaceByAddr( net_iface_l->handle, &pfaddr, 1 ) != PACKET_NO_ERROR )
goto error_free_handle;
151 *net_iface = net_iface_l;
173 DWORD wait_result = WaitForSingleObject( lock_c, timeout );
174 if( wait_result == WAIT_TIMEOUT )
return oslock_held;
175 else if( wait_result == WAIT_OBJECT_0 )
return oslock_ok;
176 else return oslock_fail;
182 wait_result = WaitForSingleObject( lock_c, timeout );
183 if( wait_result == WAIT_OBJECT_0 ) {
184 if( thread_id == GetCurrentThreadId() ) {
185 result = oslock_self;
186 ReleaseMutex( lock_c );
189 thread_id = GetCurrentThreadId();
191 }
else if( wait_result == WAIT_TIMEOUT ) result = oslock_held;
192 else result = oslock_fail;
208 lock_c = CreateMutex( NULL,
false, NULL );
209 if( lock_c == NULL )
return false;
218 if( type == oslock_recursive ) {
219 return lock_l( INFINITE );
221 return nonreentrant_lock_l( INFINITE );
228 if( type == oslock_recursive ) {
231 return nonreentrant_lock_l( 0 );
238 ReleaseMutex( lock_c );
270 CONDITION_VARIABLE condition;
276 InitializeSRWLock( &lock );
277 InitializeConditionVariable( &condition );
286 AcquireSRWLockExclusive( &lock );
296 BOOL result = SleepConditionVariableSRW( &condition, &lock, INFINITE, 0 );
298 if( result == TRUE ) {
300 ReleaseSRWLockExclusive( &lock );
310 AcquireSRWLockExclusive( &lock );
311 if(
waiting() ) WakeAllConditionVariable( &condition );
312 ReleaseSRWLockExclusive( &lock );
379 SRWLOCK retiredTimersLock;
380 void cleanupRetiredTimers() {
381 AcquireSRWLockExclusive( &retiredTimersLock );
382 while( !retiredTimers.empty() ) {
384 retiredTimers.pop_front();
385 ReleaseSRWLockExclusive( &retiredTimersLock );
387 if( retired_arg->
rm )
delete retired_arg->
inner_arg;
389 AcquireSRWLockExclusive( &retiredTimersLock );
391 ReleaseSRWLockExclusive( &retiredTimersLock );
399 InitializeSRWLock( &retiredTimersLock );
414 cleanupRetiredTimers();
415 if( timerQueueMap.find(type) == timerQueueMap.end() ) {
416 timerQueueMap[type].queue_handle = CreateTimerQueue();
417 InitializeSRWLock( &timerQueueMap[type].lock );
419 outer_arg->
queue_handle = timerQueueMap[type].queue_handle;
421 outer_arg->
func = func;
422 outer_arg->
queue =
this;
423 outer_arg->
type = type;
426 AcquireSRWLockExclusive( &timerQueueMap[type].lock );
427 CreateTimerQueueTimer( &outer_arg->
timer_handle, timerQueueMap[type].queue_handle, WindowsTimerQueueHandler, (
void *) outer_arg, micros/1000, 0, 0 );
428 timerQueueMap[type].arg_list.push_front(outer_arg);
429 ReleaseSRWLockExclusive( &timerQueueMap[type].lock );
439 TimerQueueMap_t::iterator iter = timerQueueMap.find( type );
440 if( iter == timerQueueMap.end() )
return false;
441 AcquireSRWLockExclusive( &timerQueueMap[type].lock );
442 while( ! timerQueueMap[type].arg_list.empty() ) {
444 timerQueueMap[type].arg_list.pop_front();
445 ReleaseSRWLockExclusive( &timerQueueMap[type].lock );
449 AcquireSRWLockExclusive( &timerQueueMap[type].lock );
451 ReleaseSRWLockExclusive( &timerQueueMap[type].lock );
489 virtual unsigned long sleep(
unsigned long micros ) {
490 Sleep( micros/1000 );
545 arg_inner->
func =
function;
546 arg_inner->
arg = arg;
548 if( thread_id == NULL )
return false;
557 if( WaitForSingleObject( thread_id, INFINITE ) != WAIT_OBJECT_0 )
return false;
558 exit_code = arg_inner->
ret;
583 #define NETCLOCK_HZ_OTHER 1056000000
584 #define NETCLOCK_HZ_NANO 1000000000
585 #define ONE_WAY_PHY_DELAY 8000
586 #define NANOSECOND_CLOCK_PART_DESCRIPTION "I217-LM"
587 #define NETWORK_CARD_ID_PREFIX "\\\\.\\"
588 #define OID_INTEL_GET_RXSTAMP 0xFF020264
589 #define OID_INTEL_GET_TXSTAMP 0xFF020263
590 #define OID_INTEL_GET_SYSTIM 0xFF020262
591 #define OID_INTEL_SET_SYSTIM 0xFF020261
596 class WindowsTimestamper : public HWTimestamper {
600 LARGE_INTEGER tsc_hz;
601 LARGE_INTEGER netclock_hz;
602 DWORD readOID( NDIS_OID oid,
void *output_buffer, DWORD size, DWORD *size_returned ) {
603 NDIS_OID oid_l = oid;
604 DWORD rc = DeviceIoControl(
606 IOCTL_NDIS_QUERY_GLOBAL_STATS,
613 if( rc == 0 )
return GetLastError();
614 return ERROR_SUCCESS;
616 Timestamp nanoseconds64ToTimestamp( uint64_t time ) {
619 timestamp.
seconds_ls = (time / 1000000000) & 0xFFFFFFFF;
620 timestamp.
seconds_ms = (uint16_t)((time / 1000000000) >> 32);
623 uint64_t scaleNativeClockToNanoseconds( uint64_t time ) {
624 long double scaled_output = ((
long double)netclock_hz.QuadPart)/1000000000;
625 scaled_output = ((
long double) time)/scaled_output;
626 return (uint64_t) scaled_output;
628 uint64_t scaleTSCClockToNanoseconds( uint64_t time ) {
629 long double scaled_output = ((
long double)tsc_hz.QuadPart)/1000000000;
630 scaled_output = ((
long double) time)/scaled_output;
631 return (uint64_t) scaled_output;
658 uint64_t now_net, now_tsc;
661 memset( buf, 0xFF,
sizeof( buf ));
662 if(( result = readOID(
OID_INTEL_GET_SYSTIM, buf,
sizeof(buf), &returned )) != ERROR_SUCCESS )
return false;
664 now_net = (((uint64_t)buf[1]) << 32) | buf[0];
665 now_net = scaleNativeClockToNanoseconds( now_net );
666 *device_time = nanoseconds64ToTimestamp( now_net );
669 now_tsc = (((uint64_t)buf[3]) << 32) | buf[2];
670 now_tsc = scaleTSCClockToNanoseconds( now_tsc );
671 *system_time = nanoseconds64ToTimestamp( now_tsc );
688 DWORD buf[4], buf_tmp[4];
692 while(( result = readOID(
OID_INTEL_GET_TXSTAMP, buf_tmp,
sizeof(buf_tmp), &returned )) == ERROR_SUCCESS ) {
693 memcpy( buf, buf_tmp,
sizeof( buf ));
695 if( result != ERROR_GEN_FAILURE ) {
696 fprintf( stderr,
"Error is: %d\n", result );
699 if( returned !=
sizeof(buf_tmp) )
return -72;
700 tx_r = (((uint64_t)buf[1]) << 32) | buf[0];
701 tx_s = scaleNativeClockToNanoseconds( tx_r );
703 timestamp = nanoseconds64ToTimestamp( tx_s );
720 DWORD buf[4], buf_tmp[4];
724 uint16_t packet_sequence_id;
725 while(( result = readOID(
OID_INTEL_GET_RXSTAMP, buf_tmp,
sizeof(buf_tmp), &returned )) == ERROR_SUCCESS ) {
726 memcpy( buf, buf_tmp,
sizeof( buf ));
728 if( result != ERROR_GEN_FAILURE )
return -1;
729 if( returned !=
sizeof(buf_tmp) )
return -72;
730 packet_sequence_id = *((uint32_t *) buf+3) >> 16;
731 if(
PLAT_ntohs( packet_sequence_id ) != sequenceId )
return -72;
732 rx_r = (((uint64_t)buf[1]) << 32) | buf[0];
733 rx_s = scaleNativeClockToNanoseconds( rx_r );
735 timestamp = nanoseconds64ToTimestamp( rx_s );
760 if (pipe_ != 0 && pipe_ != INVALID_HANDLE_VALUE)
761 ::CloseHandle(pipe_);
782 uint32_t sync_count, uint32_t pdelay_count,
PortState port_state);
virtual net_result send(LinkLayerAddress *addr, uint8_t *payload, size_t length, bool timestamp)
Sends a packet to a remote address.
Definition: windows_hal.hpp:78
Definition: avbts_osnet.hpp:330
int type
Definition: windows_hal.hpp:341
Definition: ieee1588.hpp:101
Definition: ieee1588.hpp:445
virtual ~WindowsPCAPNetworkInterface()
Definition: windows_hal.hpp:117
packet_error_t
Definition: packet.hpp:60
virtual bool join(OSThreadExitCode &exit_code)
Joins a terminated thread.
Definition: windows_hal.hpp:556
WindowsPCAPNetworkInterface()
Definition: windows_hal.hpp:125
struct packet_handle * pfhandle_t
Definition: packet.hpp:75
virtual unsigned getPayloadOffset()
Gets the offset to the start of data in the Layer 2 Frame.
Definition: windows_hal.hpp:111
Definition: windows_hal.hpp:166
virtual int HWTimestamper_rxtimestamp(PortIdentity *identity, uint16_t sequenceId, Timestamp ×tamp, unsigned &clock_value, bool last)
Gets the RX timestamp.
Definition: windows_hal.hpp:718
Definition: avbts_ostimer.hpp:62
friend VOID CALLBACK WindowsTimerQueueHandler(PVOID arg_in, BOOLEAN ignore)
Definition: windows_hal.hpp:481
Definition: windows_hal.hpp:465
HANDLE queue_handle
Definition: windows_hal.hpp:356
virtual OSTimer * createTimer()
Creates a new timer.
Definition: windows_hal.hpp:509
std::list< WindowsTimerQueueHandlerArg * > TimerArgList_t
Definition: windows_hal.hpp:350
uint8_t addr[ETHER_ADDR_OCTETS]
Definition: packet.hpp:70
Definition: avbts_ostimerq.hpp:49
OSCondition * createCondition()
Creates OSCondition class.
Definition: windows_hal.hpp:322
virtual bool start(OSThreadFunction function, void *arg)
Starts a new thread.
Definition: windows_hal.hpp:543
#define OID_INTEL_GET_TXSTAMP
Definition: windows_hal.hpp:589
WindowsTimer()
Definition: windows_hal.hpp:497
void toOctetArray(uint8_t *address_octet_array)
Gets first 6 bytes from ethernet address of object LinkLayerAddress.
Definition: avbts_osnet.hpp:137
packet_error_t openInterfaceByAddr(pfhandle_t pfhandle, packet_addr_t *addr, int timeout)
Opens the interface by link layer address. Uses libpcap.
Definition: avbts_port.hpp:76
void freePacketHandle(pfhandle_t pfhandle)
Close the packet handle.
bool wait_prelock()
Acquire a new lock and increment the condition counter.
Definition: windows_hal.hpp:285
void * arg
Definition: linux_hal_common.hpp:494
Definition: avbts_clock.hpp:77
Definition: avbts_oscondition.hpp:106
Definition: ieee1588.hpp:93
#define OID_INTEL_GET_SYSTIM
Definition: windows_hal.hpp:590
Definition: linux_hal_common.hpp:492
OSLock * createLock(OSLockType type)
Creates a new lock mechanism.
Definition: windows_hal.hpp:253
bool initialize(OSLockType type)
Initializes lock interface.
Definition: windows_hal.hpp:207
uint8_t _version
8 bit version value
Definition: ieee1588.hpp:251
bool initialize()
Definition: windows_hal.hpp:275
packet_error_t recvFrame(pfhandle_t pfhandle, packet_addr_t *addr, uint8_t *payload, size_t &length)
Receives a frame.
bool waiting()
Checks if OS is waiting.
Definition: avbts_oscondition.hpp:96
bool cancelEvent(int type, unsigned *event)
Cancels an event from the queue.
Definition: windows_hal.hpp:438
packet_error_t packetBind(struct packet_handle *handle, uint16_t ethertype)
Set filters for packet handler.
Definition: windows_hal.hpp:503
void(* ostimerq_handler)(void *)
Definition: avbts_ostimerq.hpp:42
virtual bool createInterface(OSNetworkInterface **net_iface, InterfaceLabel *label, HWTimestamper *timestamper)
Create a network interface.
Definition: windows_hal.hpp:141
Definition: windows_hal.hpp:336
OSLockType
Definition: avbts_oslock.hpp:44
Definition: windows_hal.hpp:746
PortState
Definition: ptptypes.hpp:48
packet_error_t sendFrame(pfhandle_t pfhandle, packet_addr_t *addr, uint16_t ethertype, uint8_t *payload, size_t length)
Sends a frame through an interface using libpcap.
OSLockResult trylock()
Tries to acquire lock.
Definition: windows_hal.hpp:227
WindowsLock()
Definition: windows_hal.hpp:200
void closeInterface(pfhandle_t pfhandle)
Close the interface.
WindowsThread()
Definition: windows_hal.hpp:566
OSThreadFunction func
Definition: linux_hal_common.hpp:493
Definition: avbts_ostimerq.hpp:91
virtual OSTimerQueue * createOSTimerQueue(IEEE1588Clock *clock)
Creates a new timer queue.
Definition: windows_hal.hpp:472
Definition: avbts_osnet.hpp:52
bool wait()
Waits for a condition and decrements the condition counter when the condition is met.
Definition: windows_hal.hpp:295
#define PTP_ETHERTYPE
Definition: ptptypes.hpp:43
std::map< int, TimerQueue_t > TimerQueueMap_t
Definition: windows_hal.hpp:368
TimerArgList_t arg_list
Definition: windows_hal.hpp:355
Definition: avbts_oscondition.hpp:42
OSLockResult
Definition: avbts_oslock.hpp:52
void up()
Counts up waiting condition.
Definition: avbts_oscondition.hpp:80
#define OID_INTEL_GET_RXSTAMP
Definition: windows_hal.hpp:588
~WindowsNamedPipeIPC()
Definition: windows_hal.hpp:759
ostimerq_handler func
Definition: windows_hal.hpp:340
uint16_t seconds_ms
32 bit seconds MSB value
Definition: ieee1588.hpp:250
packet_error_t mallocPacketHandle(pfhandle_t *pfhandle_r)
Allocate memory for the packet handle.
Definition: avbts_osnet.hpp:277
bool addEvent(unsigned long micros, int type, ostimerq_handler func, event_descriptor_t *arg, bool rm, unsigned *event)
Create a new event and add it to the timer queue.
Definition: windows_hal.hpp:412
virtual int HWTimestamper_txtimestamp(PortIdentity *identity, uint16_t sequenceId, Timestamp ×tamp, unsigned &clock_value, bool last)
Gets the TX timestamp.
Definition: windows_hal.hpp:686
Definition: windows_hal.hpp:531
OSLockResult unlock()
Releases lock.
Definition: windows_hal.hpp:237
#define PACKET_HDR_LENGTH
Definition: packet.hpp:43
virtual bool HWTimestamper_gettime(Timestamp *system_time, Timestamp *device_time, uint32_t *local_clock, uint32_t *nominal_clock_rate)
Get the cross timestamping information. The gPTP subsystem uses these samples to calculate ratios whi...
Definition: windows_hal.hpp:654
WindowsNamedPipeIPC()
Definition: windows_hal.hpp:755
HANDLE queue_handle
Definition: windows_hal.hpp:338
Definition: windows_hal.hpp:246
VOID CALLBACK WindowsTimerQueueHandler(PVOID arg_in, BOOLEAN ignore)
Definition: windows_hal.hpp:132
Definition: PeerList.hpp:68
void down()
Conds down waiting condition.
Definition: avbts_oscondition.hpp:88
WindowsTimerQueue()
Definition: windows_hal.hpp:398
Definition: avbts_ostimer.hpp:42
DWORD WINAPI OSThreadCallback(LPVOID input)
virtual void getLinkLayerAddress(LinkLayerAddress *addr)
Gets the link layer address (MAC) of the network adapter.
Definition: windows_hal.hpp:104
bool signal()
Sends a signal to unblock other threads.
Definition: windows_hal.hpp:309
OSThreadExitCode
Definition: avbts_osthread.hpp:44
Definition: windows_hal.hpp:572
Definition: avbts_oslock.hpp:98
Definition: IPCListener.hpp:47
Definition: avbts_osthread.hpp:54
virtual bool init(OS_IPC_ARG *arg=NULL)
Initializes the IPC arguments.
net_result
Definition: avbts_osnet.hpp:272
#define ONE_WAY_PHY_DELAY
Definition: windows_hal.hpp:585
Definition: packet.hpp:69
Definition: windows_hal.hpp:354
Definition: avbts_oslock.hpp:57
OSThreadExitCode ret
Definition: linux_hal_common.hpp:495
Definition: avbts_osipc.hpp:56
Definition: avbts_osipc.hpp:46
long double FrequencyRatio
Definition: ptptypes.hpp:39
Definition: windows_hal.hpp:373
Definition: windows_hal.hpp:320
virtual unsigned long sleep(unsigned long micros)
Sleep for an amount of time.
Definition: windows_hal.hpp:489
OSLockResult lock()
Acquires lock.
Definition: windows_hal.hpp:217
Definition: windows_hal.hpp:266
bool rm
Definition: windows_hal.hpp:342
uint32_t seconds_ls
32 bit seconds LSB value
Definition: ieee1588.hpp:249
SRWLOCK lock
Definition: windows_hal.hpp:357
event_descriptor_t * inner_arg
Definition: windows_hal.hpp:339
virtual bool update(int64_t ml_phoffset, int64_t ls_phoffset, FrequencyRatio ml_freqoffset, FrequencyRatio ls_freq_offset, uint64_t local_time, uint32_t sync_count, uint32_t pdelay_count, PortState port_state)
Updates IPC interface values.
Definition: avbts_osthread.hpp:78
WindowsTimerQueue * queue
Definition: windows_hal.hpp:343
Definition: ieee1588.hpp:222
OSThreadExitCode(* OSThreadFunction)(void *)
Definition: avbts_osthread.hpp:49
OSThread * createThread()
Creates a new windows thread.
Definition: windows_hal.hpp:578
HANDLE timer_handle
Definition: windows_hal.hpp:337
virtual net_result nrecv(LinkLayerAddress *addr, uint8_t *payload, size_t &length)
Receives a packet from a remote address.
Definition: windows_hal.hpp:91
TimerQueue_t * timer_queue
Definition: windows_hal.hpp:344
Definition: windows_hal.hpp:64
uint32_t nanoseconds
32 bit nanoseconds value
Definition: ieee1588.hpp:248