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