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