gPTP Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
linux_hal_common.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright (c) 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 #ifndef LINUX_HAL_COMMON_HPP
35 #define LINUX_HAL_COMMON_HPP
36 
39 #include "avbts_osnet.hpp"
40 #include "avbts_oslock.hpp"
41 #include "avbts_oscondition.hpp"
42 #include "avbts_ostimerq.hpp"
43 #include "avbts_ostimer.hpp"
44 #include "avbts_osthread.hpp"
45 #include "avbts_osipc.hpp"
46 #include "ieee1588.hpp"
47 
48 #include <list>
49 
50 #define ONE_WAY_PHY_DELAY 400
51 #define P8021AS_MULTICAST "\x01\x80\xC2\x00\x00\x0E"
52 #define PTP_DEVICE "/dev/ptpXX"
53 #define PTP_DEVICE_IDX_OFFS 8
54 #define CLOCKFD 3
55 #define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
56 struct timespec;
57 
63 extern Timestamp tsToTimestamp(struct timespec *ts);
64 
65 struct TicketingLockPrivate;
66 
70 typedef struct TicketingLockPrivate * TicketingLockPrivate_t;
71 
82 public:
89  bool lock( bool *got = NULL );
90 
96  bool unlock();
97 
102  bool init();
103 
108  TicketingLock();
109 
113  ~TicketingLock();
114 private:
115  bool init_flag;
116  TicketingLockPrivate_t _private;
117  bool in_use;
118  uint8_t cond_ticket_issue;
119  uint8_t cond_ticket_serving;
120 };
121 
127 public:
131  virtual ~LinuxTimestamper() = 0;
132 
140  virtual bool post_init( int ifindex, int sd, TicketingLock *lock ) = 0;
141 };
142 
147  friend class LinuxNetworkInterfaceFactory;
148 private:
149  LinkLayerAddress local_addr;
150  int sd_event;
151  int sd_general;
152  LinuxTimestamper *timestamper;
153  int ifindex;
154 
155  TicketingLock net_lock;
156 public:
166  virtual net_result send
167  ( LinkLayerAddress *addr, uint8_t *payload, size_t length,
168  bool timestamp );
169 
178  virtual net_result nrecv
179  ( LinkLayerAddress *addr, uint8_t *payload, size_t &length );
180 
185  void disable_clear_rx_queue();
186 
191  void reenable_rx_queue();
192 
198  virtual void getLinkLayerAddress( LinkLayerAddress *addr ) {
199  *addr = local_addr;
200  }
201 
206  virtual unsigned getPayloadOffset() {
207  return 0;
208  }
213 protected:
218 };
219 
223 typedef std::list<LinuxNetworkInterface *> LinuxNetworkInterfaceList;
224 
225 struct LinuxLockPrivate;
229 typedef LinuxLockPrivate * LinuxLockPrivate_t;
230 
234 class LinuxLock : public OSLock {
235  friend class LinuxLockFactory;
236 private:
237  OSLockType type;
238  LinuxLockPrivate_t _private;
239 protected:
244  _private = NULL;
245  }
246 
253  bool initialize( OSLockType type );
254 
258  ~LinuxLock();
259 
264  OSLockResult lock();
265 
271 
277 };
278 
283 public:
290  LinuxLock *lock = new LinuxLock();
291  if (lock->initialize(type) != oslock_ok) {
292  delete lock;
293  lock = NULL;
294  }
295  return lock;
296  }
297 };
298 
299 struct LinuxConditionPrivate;
303 typedef struct LinuxConditionPrivate * LinuxConditionPrivate_t;
304 
308 class LinuxCondition : public OSCondition {
309  friend class LinuxConditionFactory;
310 private:
311  LinuxConditionPrivate_t _private;
312 protected:
313 
318  bool initialize();
319 public:
320 
326  bool wait_prelock();
327 
332  bool wait();
333 
338  bool signal();
339 
340  /*
341  * Default constructor
342  */
343  ~LinuxCondition();
344 
345  /*
346  * Destructor: Deletes internal variables
347  */
348  LinuxCondition() {
349  _private = NULL;
350  }
351 };
352 
357 public:
363  LinuxCondition *result = new LinuxCondition();
364  return result->initialize() ? result : NULL;
365  }
366 };
367 
368 struct LinuxTimerQueueActionArg;
369 
373 typedef std::map < int, struct LinuxTimerQueueActionArg *> LinuxTimerQueueMap_t;
374 
380 void *LinuxTimerQueueHandler( void *arg );
381 
382 struct LinuxTimerQueuePrivate;
386 typedef struct LinuxTimerQueuePrivate * LinuxTimerQueuePrivate_t;
387 
392  friend class LinuxTimerQueueFactory;
393  friend void *LinuxTimerQueueHandler( void * arg );
394 private:
395  LinuxTimerQueueMap_t timerQueueMap;
396  int key;
397  bool stop;
398  LinuxTimerQueuePrivate_t _private;
399  OSLock *lock;
400  void LinuxTimerQueueAction( LinuxTimerQueueActionArg *arg );
401 protected:
406  _private = NULL;
407  }
408 
413  virtual bool init();
414 public:
419 
430  bool addEvent
431  ( unsigned long micros, int type, ostimerq_handler func,
432  event_descriptor_t * arg, bool rm, unsigned *event );
433 
440  bool cancelEvent( int type, unsigned *event );
441 };
442 
447 public:
453  virtual OSTimerQueue *createOSTimerQueue( IEEE1588Clock *clock );
454 };
455 
459 class LinuxTimer : public OSTimer {
460  friend class LinuxTimerFactory;
461  public:
467  virtual unsigned long sleep(unsigned long micros);
468  protected:
473 };
474 
479  public:
484  virtual OSTimer * createTimer() {
485  return new LinuxTimer();
486  }
487 };
488 
492 struct OSThreadArg {
494  void *arg;
496 };
497 
503 void *OSThreadCallback(void *input);
504 
505 struct LinuxThreadPrivate;
509 typedef LinuxThreadPrivate * LinuxThreadPrivate_t;
510 
514 class LinuxThread : public OSThread {
515  friend class LinuxThreadFactory;
516  private:
517  LinuxThreadPrivate_t _private;
518  OSThreadArg *arg_inner;
519  public:
526  virtual bool start(OSThreadFunction function, void *arg);
527 
533  virtual bool join(OSThreadExitCode & exit_code);
534  virtual ~LinuxThread();
535  protected:
536  LinuxThread();
537 };
538 
543  public:
549  return new LinuxThread();
550  }
551 };
552 
557 public:
565  virtual bool createInterface
566  ( OSNetworkInterface **net_iface, InterfaceLabel *label,
567  HWTimestamper *timestamper );
568 };
569 
573 class LinuxIPCArg : public OS_IPC_ARG {
574 private:
575  char *group_name;
576 public:
581  LinuxIPCArg( char *group_name ) {
582  int len = strnlen(group_name,16);
583  this->group_name = new char[len+1];
584  strncpy( this->group_name, group_name, len+1 );
585  this->group_name[len] = '\0';
586  }
590  virtual ~LinuxIPCArg() {
591  delete group_name;
592  }
593  friend class LinuxSharedMemoryIPC;
594 };
595 
596 #define DEFAULT_GROUPNAME "ptp"
601 class LinuxSharedMemoryIPC:public OS_IPC {
602 private:
603  int shm_fd;
604  char *master_offset_buffer;
605  int err;
606 public:
611  shm_fd = 0;
612  err = 0;
613  master_offset_buffer = NULL;
614  };
619 
625  virtual bool init( OS_IPC_ARG *barg = NULL );
626 
639  virtual bool update
640  (int64_t ml_phoffset, int64_t ls_phoffset, FrequencyRatio ml_freqoffset,
641  FrequencyRatio ls_freqoffset, uint64_t local_time, uint32_t sync_count,
642  uint32_t pdelay_count, PortState port_state );
643 
648  void stop();
649 };
650 
651 
652 #endif/*LINUX_HAL_COMMON_HPP*/
struct LinuxTimerQueuePrivate * LinuxTimerQueuePrivate_t
Definition: linux_hal_common.hpp:386
friend void * LinuxTimerQueueHandler(void *arg)
Linux timer queue handler. Deals with linux queues.
bool wait()
Waits until the ready signal condition is met and decrements the counter.
Definition: avbts_osnet.hpp:330
Definition: linux_hal_common.hpp:514
Definition: ieee1588.hpp:101
Definition: ieee1588.hpp:445
virtual OSTimerQueue * createOSTimerQueue(IEEE1588Clock *clock)
Creates Linux timer queue.
Definition: avbts_ostimer.hpp:62
bool init()
Initializes all flags and counters. Create private structures.
bool wait_prelock()
Counts up the amount of times we call the locking mechanism.
void * LinuxTimerQueueHandler(void *arg)
Linux timer queue handler. Deals with linux queues.
virtual bool join(OSThreadExitCode &exit_code)
Joins a new thread.
Definition: linux_hal_common.hpp:234
virtual OSTimer * createTimer()
Creates the linux timer.
Definition: linux_hal_common.hpp:484
Definition: avbts_ostimerq.hpp:49
LinuxSharedMemoryIPC()
Definition: linux_hal_common.hpp:610
virtual net_result nrecv(LinkLayerAddress *addr, uint8_t *payload, size_t &length)
Receives a packet from a remote address.
OSLockResult lock()
Provides a simple lock mechanism.
OSLock * createLock(OSLockType type)
Creates the locking mechanism.
Definition: linux_hal_common.hpp:289
bool addEvent(unsigned long micros, int type, ostimerq_handler func, event_descriptor_t *arg, bool rm, unsigned *event)
Add an event to the timer queue.
std::list< LinuxNetworkInterface * > LinuxNetworkInterfaceList
Definition: linux_hal_common.hpp:223
void disable_clear_rx_queue()
Disables rx socket descriptor and and clears the rx queue.
OSLockResult trylock()
Provides a simple trylock mechanism.
void * arg
Definition: linux_hal_common.hpp:494
LinuxTimerQueue()
Definition: linux_hal_common.hpp:405
Definition: avbts_clock.hpp:77
Definition: avbts_oscondition.hpp:106
Definition: ieee1588.hpp:93
Definition: linux_hal_common.hpp:478
Definition: linux_hal_common.hpp:573
Definition: linux_hal_common.hpp:601
Definition: linux_hal_common.hpp:492
struct LinuxConditionPrivate * LinuxConditionPrivate_t
Definition: linux_hal_common.hpp:303
virtual net_result send(LinkLayerAddress *addr, uint8_t *payload, size_t length, bool timestamp)
Sends a packet to a remote address.
LinuxLock()
Definition: linux_hal_common.hpp:243
virtual void getLinkLayerAddress(LinkLayerAddress *addr)
Gets the local link layer address.
Definition: linux_hal_common.hpp:198
void stop()
unmaps and unlink shared memory
virtual unsigned getPayloadOffset()
Gets the payload offset.
Definition: linux_hal_common.hpp:206
void(* ostimerq_handler)(void *)
Definition: avbts_ostimerq.hpp:42
virtual bool init()
Initializes internal variables.
OSLockType
Definition: avbts_oslock.hpp:44
PortState
Definition: ptptypes.hpp:48
struct TicketingLockPrivate * TicketingLockPrivate_t
Definition: linux_hal_common.hpp:70
bool unlock()
Unlock mechanism. Increments the release counter and unblock other threads waiting for a condition fl...
Definition: linux_hal_common.hpp:146
LinuxTimer()
Definition: linux_hal_common.hpp:472
virtual bool update(int64_t ml_phoffset, int64_t ls_phoffset, FrequencyRatio ml_freqoffset, FrequencyRatio ls_freqoffset, uint64_t local_time, uint32_t sync_count, uint32_t pdelay_count, PortState port_state)
Updates IPC values.
LinuxNetworkInterface()
Definition: linux_hal_common.hpp:217
OSThreadFunction func
Definition: linux_hal_common.hpp:493
Definition: avbts_ostimerq.hpp:91
Definition: avbts_osnet.hpp:52
virtual bool createInterface(OSNetworkInterface **net_iface, InterfaceLabel *label, HWTimestamper *timestamper)
Creates a new interface.
LinuxLockPrivate * LinuxLockPrivate_t
Definition: linux_hal_common.hpp:225
void reenable_rx_queue()
Enables the rx socket descriptor.
Definition: linux_hal_common.hpp:356
virtual unsigned long sleep(unsigned long micros)
Sleeps for a given amount of time in microsseconds.
Definition: avbts_oscondition.hpp:42
OSLockResult
Definition: avbts_oslock.hpp:52
bool cancelEvent(int type, unsigned *event)
Removes an event from the timer queue.
Definition: avbts_osnet.hpp:277
virtual bool start(OSThreadFunction function, void *arg)
Starts a new thread.
std::map< int, struct LinuxTimerQueueActionArg * > LinuxTimerQueueMap_t
Definition: linux_hal_common.hpp:368
Definition: linux_hal_common.hpp:391
void * OSThreadCallback(void *input)
OSThread callback.
Definition: linux_hal_common.hpp:308
virtual bool init(OS_IPC_ARG *barg=NULL)
Initializes shared memory with DEFAULT_GROUPNAME case arg is null.
Definition: avbts_ostimer.hpp:42
Definition: linux_hal_common.hpp:282
virtual ~LinuxIPCArg()
Definition: linux_hal_common.hpp:590
Definition: linux_hal_common.hpp:446
Definition: linux_hal_common.hpp:81
OSThreadExitCode
Definition: avbts_osthread.hpp:44
Definition: avbts_oslock.hpp:98
Definition: linux_hal_common.hpp:459
bool signal()
Unblock all threads that are busy waiting for a condition.
Definition: avbts_osthread.hpp:54
LinuxThreadPrivate * LinuxThreadPrivate_t
Definition: linux_hal_common.hpp:505
net_result
Definition: avbts_osnet.hpp:272
virtual bool post_init(int ifindex, int sd, TicketingLock *lock)=0
Provides a generic method for initializing timestamp interfaces.
OSLockResult unlock()
Provides a simple unlock mechanism.
Definition: linux_hal_common.hpp:126
Definition: avbts_oslock.hpp:57
OSThreadExitCode ret
Definition: linux_hal_common.hpp:495
bool initialize()
Initializes locks and mutex conditions.
Definition: avbts_osipc.hpp:46
OSCondition * createCondition()
Creates LinuxCondition objects.
Definition: linux_hal_common.hpp:362
long double FrequencyRatio
Definition: ptptypes.hpp:39
Definition: linux_hal_common.hpp:542
Timestamp tsToTimestamp(struct timespec *ts)
Converts timestamp in the struct timespec format to Timestamp.
OSThread * createThread()
Creates a new LinuxThread.
Definition: linux_hal_common.hpp:548
Definition: avbts_osthread.hpp:78
bool initialize(OSLockType type)
Initializes all mutexes and locks.
Definition: linux_hal_common.hpp:556
LinuxIPCArg(char *group_name)
Initializes IPCArg object.
Definition: linux_hal_common.hpp:581
bool lock(bool *got=NULL)
Lock mechanism. Gets a ticket and try locking the process.
virtual ~LinuxTimestamper()=0
Definition: ieee1588.hpp:222
OSThreadExitCode(* OSThreadFunction)(void *)
Definition: avbts_osthread.hpp:49