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