giuliomoro@135
|
1 #ifndef CLOCK_SYNC_H_INCLUDED
|
giuliomoro@135
|
2 #define CLOCK_SYNC_H_INCLUDED
|
giuliomoro@135
|
3 #include "stats.hpp"
|
giuliomoro@135
|
4 #include "UdpServer.h"
|
giuliomoro@135
|
5 #include "UdpClient.h"
|
giuliomoro@135
|
6 #include "Clock.h"
|
giuliomoro@135
|
7 #include "VirtualClock.h"
|
giuliomoro@135
|
8
|
giuliomoro@135
|
9 enum ptpMessageConsts{
|
giuliomoro@135
|
10 kSyncMessageLength=sizeof(myClock_t)+sizeof(int)
|
giuliomoro@135
|
11 };
|
giuliomoro@135
|
12 enum ptpMessageType{
|
giuliomoro@135
|
13 kSync=0,
|
giuliomoro@135
|
14 kFollowUp=1,
|
giuliomoro@135
|
15 kDelayReq=2,
|
giuliomoro@135
|
16 kDelayResp=3,
|
giuliomoro@135
|
17 kNone=4
|
giuliomoro@135
|
18 };
|
giuliomoro@135
|
19
|
giuliomoro@135
|
20 enum ptpStatus{
|
giuliomoro@135
|
21 kSyncSent,
|
giuliomoro@135
|
22 kFollowUpSent,
|
giuliomoro@135
|
23 kDelayReqSent,
|
giuliomoro@135
|
24 kDelayRespSent
|
giuliomoro@135
|
25 };
|
giuliomoro@135
|
26
|
giuliomoro@135
|
27 class ClockSync{
|
giuliomoro@135
|
28 private:
|
giuliomoro@135
|
29 MovingAverage<double> movingAverage;
|
giuliomoro@135
|
30 UdpServer server;
|
giuliomoro@135
|
31 UdpClient client;
|
giuliomoro@135
|
32 bool slave;
|
giuliomoro@135
|
33 int bufferLength;
|
giuliomoro@135
|
34 int clockSyncType;
|
giuliomoro@135
|
35 int expectedClockSyncType;
|
giuliomoro@135
|
36 myClock_t clockSyncTimestamp;
|
giuliomoro@135
|
37 myClock_t localTimestamp;
|
giuliomoro@135
|
38 myClock_t T1;
|
giuliomoro@135
|
39 myClock_t T1p;
|
giuliomoro@135
|
40 myClock_t T2;
|
giuliomoro@135
|
41 myClock_t T2p;
|
giuliomoro@135
|
42 int receiveLoopSleepUs;
|
giuliomoro@135
|
43 int receiveLoopTimeout;
|
giuliomoro@135
|
44 char buffer[kSyncMessageLength];
|
giuliomoro@135
|
45 VirtualClock *virtualClock;
|
giuliomoro@141
|
46 void resetTs();
|
giuliomoro@141
|
47 bool areTsValid();
|
giuliomoro@141
|
48 void processOffset(double offset);
|
giuliomoro@135
|
49 public:
|
giuliomoro@135
|
50 ClockSync(){};
|
giuliomoro@135
|
51 ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock);
|
giuliomoro@135
|
52 void init(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock);
|
giuliomoro@135
|
53 void* getBuffer();
|
giuliomoro@135
|
54 bool isSlave();
|
giuliomoro@135
|
55 bool isMaster();
|
giuliomoro@135
|
56 int getType();
|
giuliomoro@135
|
57 myClock_t getTimestamp();
|
giuliomoro@135
|
58 void setVirtualClock(VirtualClock &aVirtualClock);
|
giuliomoro@135
|
59 void setPort(int aPort);
|
giuliomoro@135
|
60 void setType(int clockSyncType);
|
giuliomoro@135
|
61 void setTimestamp(myClock_t timestamp);
|
giuliomoro@135
|
62 void print();
|
giuliomoro@135
|
63 /**
|
giuliomoro@135
|
64 * sends a clockSync without blocking, checks results and returns the timestamp
|
giuliomoro@135
|
65 * immediately after the clockSync has been sent or -1 if there was an error or timeout expired.
|
giuliomoro@135
|
66 */
|
giuliomoro@135
|
67 myClock_t send();
|
giuliomoro@135
|
68 /**
|
giuliomoro@135
|
69 * receives a clockSync without blocking, checks results and returns the timestamp
|
giuliomoro@135
|
70 * immediately after the clockSync has been received, or -1 if there was an error
|
giuliomoro@135
|
71 * or 0 if timeout expired.
|
giuliomoro@135
|
72 */
|
giuliomoro@135
|
73 myClock_t receive();
|
giuliomoro@135
|
74 int masterSendSync();
|
giuliomoro@135
|
75 int receiveLoop();
|
giuliomoro@135
|
76 int slaveHandleMessage();
|
giuliomoro@135
|
77 int masterHandleMessage();
|
giuliomoro@135
|
78 int sendReceiveLoop();
|
giuliomoro@135
|
79 operator void*(){
|
giuliomoro@135
|
80 return getBuffer();
|
giuliomoro@135
|
81 }
|
giuliomoro@135
|
82 };
|
giuliomoro@135
|
83
|
giuliomoro@141
|
84 #endif /* CLOCK_SYNC_H_INCLUDED */
|