gPTP Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
windows/daemon_cl/ipcdef.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 
3 Copyright (c) 2009-2012, Intel Corporation
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 
34 
35 #ifndef IPCDEF_HPP
36 #define IPCDEF_HPP
37 
40 #include <windows.h>
41 #include <stdint.h>
42 #include <ptptypes.hpp>
43 #include <minwinbase.h>
44 #include <debugout.hpp>
45 
46 #define OUTSTANDING_MESSAGES 10
48 #define PIPE_PREFIX "\\\\.\\pipe\\"
49 #define P802_1AS_PIPENAME "gptp-ctrl"
51 typedef struct {
52  int64_t ml_phoffset;
53  int64_t ls_phoffset;
54  FrequencyRatio ml_freqoffset;
55  FrequencyRatio ls_freqoffset;
56  int64_t local_time;
57  uint32_t sync_count;
58  uint32_t pdelay_count;
59  PortState port_state;
60  DWORD process_id;
61 } gPtpTimeData;
62 
63 
64 #pragma pack(push,1)
65 
73 typedef enum { BASE_MSG = 0, CTRL_MSG, QUERY_MSG, OFFSET_MSG } NPIPE_MSG_TYPE;
74 
79 protected:
80  DWORD sz;
83  OVERLAPPED ol_read;
84  DWORD ol_read_req;
85 public:
90  void init() {
91  sz = sizeof( WindowsNPipeMessage );
92  }
98  bool write( HANDLE pipe ) {
99  DWORD bytes_written;
100  DWORD last_error = ERROR_SUCCESS;
101  if( sz == 0 ) return false;
102  if( WriteFile( pipe, this, sz, &bytes_written, NULL ) == 0 ) {
103  last_error = GetLastError();
104  }
105  if( last_error == ERROR_SUCCESS || last_error == ERROR_PIPE_LISTENING ) {
106  return true;
107  }
108  XPTPD_ERROR( "Failed to write to named pipe: %u", last_error );
109  return false;
110  }
118  long read_ol( HANDLE pipe, long offs, HANDLE event ) {
119  DWORD bytes_read;
120  long sz_l = (long) sz;
121  LPOVERLAPPED lol;
122  if( sz_l - offs < 0 || sz_l == 0 ) return -1;
123  if( sz_l - offs == 0 ) return offs;
124  ol_read_req = sz_l-offs;
125  if( event != NULL ) {
126  memset( &ol_read, 0, sizeof( ol_read ));
127  ol_read.hEvent = event;
128  lol = &ol_read;
129  } else {
130  lol = NULL;
131  }
132  if( ReadFile( pipe, ((char *)this)+offs, ol_read_req, &bytes_read, lol ) == 0 ) {
133  int err = GetLastError();
134  if( err != ERROR_IO_PENDING ) {
135  XPTPD_ERROR( "Failed to read %d bytes from named pipe: %u", ol_read_req, err );
136  }
137  return err == ERROR_IO_PENDING ? 0 : -1;
138  }
139  return (bytes_read == sz_l-offs) ? offs+bytes_read : -1;
140  }
147  long read_ol_complete( HANDLE pipe ) {
148  DWORD bytes_read;
149  if( GetOverlappedResult( pipe, &ol_read, &bytes_read, false ) == 0 ) {
150  return -1;
151  }
152  return bytes_read;
153  }
160  long read( HANDLE pipe, long offs ) {
161  return read_ol( pipe, offs, NULL );
162  }
168 };
169 
173 class Offset {
174 public:
175  int64_t ml_phoffset;
177  int64_t ls_phoffset;
179  uint64_t local_time;
180 };
181 
186 private:
187  Offset offset;
188 public:
193  void _init() {
195  type = OFFSET_MSG;
196  }
201  void init() {
202  _init();
203  memset( &this->offset, 0, sizeof( this->offset ));
204  }
214  void init( int64_t ml_phoffset, FrequencyRatio ml_freqoffset, int64_t ls_phoffset, FrequencyRatio ls_freqoffset, uint64_t local_time ) {
215  _init();
216  this->offset.ml_phoffset = ml_phoffset;
217  this->offset.ml_freqoffset = ml_freqoffset;
218  this->offset.ls_phoffset = ls_phoffset;
219  this->offset.ls_freqoffset = ls_freqoffset;
220  this->offset.local_time = local_time;
221  }
227  void init( Offset *offset ) {
228  _init();
229  this->offset = *offset;
230  }
235  int64_t getMasterLocalOffset() { return offset.ml_phoffset; }
245  int64_t getLocalSystemOffset() { return offset.ls_phoffset; }
255  uint64_t getLocalTime() { return offset.local_time; }
256 };
257 
263 typedef enum { ADD_PEER, REMOVE_PEER } CtrlWhich;
270 typedef enum { MAC_ADDR, IP_ADDR, INVALID_ADDR } AddrWhich;
271 
275 class PeerAddr {
276 public:
281  union {
283  uint8_t ip[IP_ADDR_OCTETS];
284  };
290  bool operator==(const PeerAddr &other) const {
291  int result;
292  switch( which ) {
293  case MAC_ADDR:
294  result = memcmp( &other.mac, &mac, ETHER_ADDR_OCTETS );
295  break;
296  case IP_ADDR:
297  result = memcmp( &other.ip, &ip, IP_ADDR_OCTETS );
298  break;
299  default:
300  result = -1; // != 0
301  break;
302  }
303  return (result == 0) ? true : false;
304  }
310  bool operator<(const PeerAddr &other) const {
311  int result;
312  switch( which ) {
313  case MAC_ADDR:
314  result = memcmp( &other.mac, &mac, ETHER_ADDR_OCTETS );
315  break;
316  case IP_ADDR:
317  result = memcmp( &other.ip, &ip, IP_ADDR_OCTETS );
318  break;
319  default:
320  result = 1; // > 0
321  break;
322  }
323  return (result < 0) ? true : false;
324  }
325 };
326 
331 private:
332  CtrlWhich which;
333  PeerAddr addr;
334  uint16_t flags;
335 public:
340  void init() {
341  sz = sizeof(WinNPipeCtrlMessage);
342  type = CTRL_MSG;
343  }
351  void init( CtrlWhich which, PeerAddr addr ) {
352  init();
353  this->which = which;
354  this->addr = addr;
355  flags = 0;
356  }
361  PeerAddr getPeerAddr() { return addr; }
367  void setPeerAddr( PeerAddr addr ) { this->addr = addr; }
372  CtrlWhich getCtrlWhich() { return which; }
378  void setCtrlWhich( CtrlWhich which ) { this->which = which; }
384  uint16_t getFlags() { return flags; }
385 };
386 
393 public:
398  void init() { type = OFFSET_MSG; sz = sizeof(*this); }
399 };
400 
405 typedef union {
409 
414 typedef union {
417 
418 #define NPIPE_MAX_CLIENT_MSG_SZ (sizeof( WindowsNPipeMsgClient ))
419 #define NPIPE_MAX_SERVER_MSG_SZ (sizeof( WindowsNPipeMsgServer ))
420 #define NPIPE_MAX_MSG_SZ (NPIPE_MAX_CLIENT_MSG_SZ > NPIPE_MAX_SERVER_MSG_SZ ? NPIPE_MAX_CLIENT_MSG_SZ : NPIPE_MAX_SERVER_MSG_SZ)
422 #pragma pack(pop)
423 
424 #endif
NPIPE_MSG_TYPE
Definition: windows/daemon_cl/ipcdef.hpp:73
CtrlWhich getCtrlWhich()
Gets control type.
Definition: windows/daemon_cl/ipcdef.hpp:372
CtrlWhich
Definition: windows/daemon_cl/ipcdef.hpp:263
Definition: windows/daemon_cl/ipcdef.hpp:275
DWORD ol_read_req
Definition: windows/daemon_cl/ipcdef.hpp:84
DWORD sz
Definition: windows/daemon_cl/ipcdef.hpp:80
long read_ol(HANDLE pipe, long offs, HANDLE event)
Reads from the pipe.
Definition: windows/daemon_cl/ipcdef.hpp:118
WinNPipeQueryMessage b
Definition: windows/daemon_cl/ipcdef.hpp:407
PeerAddr getPeerAddr()
Gets peer addresses.
Definition: windows/daemon_cl/ipcdef.hpp:361
Definition: windows/daemon_cl/ipcdef.hpp:330
void init()
Initializes interface and clears the Offset message.
Definition: windows/daemon_cl/ipcdef.hpp:201
FrequencyRatio getLocalSystemFreqOffset()
Gets Local to system frequency offset.
Definition: windows/daemon_cl/ipcdef.hpp:250
#define ETHER_ADDR_OCTETS
Definition: ptptypes.hpp:41
FrequencyRatio ls_freqoffset
Definition: windows/daemon_cl/ipcdef.hpp:178
Definition: windows/daemon_cl/ipcdef.hpp:414
void init()
Initializes the interface.
Definition: windows/daemon_cl/ipcdef.hpp:398
Definition: windows/daemon_cl/ipcdef.hpp:78
void init(int64_t ml_phoffset, FrequencyRatio ml_freqoffset, int64_t ls_phoffset, FrequencyRatio ls_freqoffset, uint64_t local_time)
Initializes the interface with specific values.
Definition: windows/daemon_cl/ipcdef.hpp:214
NPIPE_MSG_TYPE type
Definition: windows/daemon_cl/ipcdef.hpp:81
Definition: linux/src/ipcdef.hpp:45
AddrWhich which
Definition: windows/daemon_cl/ipcdef.hpp:277
int64_t ml_phoffset
Definition: windows/daemon_cl/ipcdef.hpp:175
NPIPE_MSG_TYPE getType()
Gets pipe message type.
Definition: windows/daemon_cl/ipcdef.hpp:167
Definition: windows/daemon_cl/ipcdef.hpp:392
PortState
Definition: ptptypes.hpp:48
bool write(HANDLE pipe)
Writes to the named pipe.
Definition: windows/daemon_cl/ipcdef.hpp:98
bool operator<(const PeerAddr &other) const
Implements the operator '<' overloading method.
Definition: windows/daemon_cl/ipcdef.hpp:310
Definition: windows/daemon_cl/ipcdef.hpp:405
#define XPTPD_ERROR(fmt,...)
Definition: debugout.hpp:41
void _init()
Initializes interface.
Definition: windows/daemon_cl/ipcdef.hpp:193
void setPeerAddr(PeerAddr addr)
Sets peer address.
Definition: windows/daemon_cl/ipcdef.hpp:367
FrequencyRatio getMasterLocalFreqOffset()
Gets Master to local frequency offset.
Definition: windows/daemon_cl/ipcdef.hpp:240
void init()
Initializes interace's internal variables.
Definition: windows/daemon_cl/ipcdef.hpp:340
FrequencyRatio ml_freqoffset
Definition: windows/daemon_cl/ipcdef.hpp:176
int64_t getLocalSystemOffset()
Gets local to system phase offset.
Definition: windows/daemon_cl/ipcdef.hpp:245
WinNPipeOffsetUpdateMessage a
Definition: windows/daemon_cl/ipcdef.hpp:415
uint8_t mac[ETHER_ADDR_OCTETS]
Definition: windows/daemon_cl/ipcdef.hpp:282
long read_ol_complete(HANDLE pipe)
Reads completely the overlapped result.
Definition: windows/daemon_cl/ipcdef.hpp:147
uint64_t local_time
Definition: windows/daemon_cl/ipcdef.hpp:179
bool operator==(const PeerAddr &other) const
Implements the operator '==' overloading method.
Definition: windows/daemon_cl/ipcdef.hpp:290
AddrWhich
Definition: windows/daemon_cl/ipcdef.hpp:270
int64_t getMasterLocalOffset()
Gets master to local phase offset.
Definition: windows/daemon_cl/ipcdef.hpp:235
void setCtrlWhich(CtrlWhich which)
Sets control message type.
Definition: windows/daemon_cl/ipcdef.hpp:378
#define IP_ADDR_OCTETS
Definition: ptptypes.hpp:42
void init()
Initializes the interface.
Definition: windows/daemon_cl/ipcdef.hpp:90
void init(CtrlWhich which, PeerAddr addr)
Initializes Interface's internal variables and sets control and addresses values. ...
Definition: windows/daemon_cl/ipcdef.hpp:351
uint16_t getFlags()
Gets internal flags.
Definition: windows/daemon_cl/ipcdef.hpp:384
WinNPipeCtrlMessage a
Definition: windows/daemon_cl/ipcdef.hpp:406
long double FrequencyRatio
Definition: ptptypes.hpp:39
int64_t ls_phoffset
Definition: windows/daemon_cl/ipcdef.hpp:177
void init(Offset *offset)
Initializes the interface based on the Offset structure.
Definition: windows/daemon_cl/ipcdef.hpp:227
OVERLAPPED ol_read
Definition: windows/daemon_cl/ipcdef.hpp:83
uint64_t getLocalTime()
Gets local time.
Definition: windows/daemon_cl/ipcdef.hpp:255
uint8_t ip[IP_ADDR_OCTETS]
Definition: windows/daemon_cl/ipcdef.hpp:283
long read(HANDLE pipe, long offs)
Reads from the pipe.
Definition: windows/daemon_cl/ipcdef.hpp:160
Definition: windows/daemon_cl/ipcdef.hpp:185
Definition: windows/daemon_cl/ipcdef.hpp:173