giuliomoro@135: #ifndef CLOCK_SYNC_H_INCLUDED giuliomoro@135: #define CLOCK_SYNC_H_INCLUDED giuliomoro@135: #include "stats.hpp" giuliomoro@135: #include "UdpServer.h" giuliomoro@135: #include "UdpClient.h" giuliomoro@135: #include "Clock.h" giuliomoro@135: #include "VirtualClock.h" giuliomoro@144: #ifdef USE_JUCE giuliomoro@144: #else giuliomoro@144: #include giuliomoro@144: extern I2c_Codec* gAudioCodec; giuliomoro@144: #endif /* USE_JUCE */ giuliomoro@135: giuliomoro@135: enum ptpMessageConsts{ giuliomoro@135: kSyncMessageLength=sizeof(myClock_t)+sizeof(int) giuliomoro@135: }; giuliomoro@135: enum ptpMessageType{ giuliomoro@135: kSync=0, giuliomoro@135: kFollowUp=1, giuliomoro@135: kDelayReq=2, giuliomoro@135: kDelayResp=3, giuliomoro@135: kNone=4 giuliomoro@135: }; giuliomoro@135: giuliomoro@135: enum ptpStatus{ giuliomoro@135: kSyncSent, giuliomoro@135: kFollowUpSent, giuliomoro@135: kDelayReqSent, giuliomoro@135: kDelayRespSent giuliomoro@135: }; giuliomoro@135: giuliomoro@135: class ClockSync{ giuliomoro@135: private: giuliomoro@135: MovingAverage movingAverage; giuliomoro@135: UdpServer server; giuliomoro@135: UdpClient client; giuliomoro@135: bool slave; giuliomoro@135: int bufferLength; giuliomoro@135: int clockSyncType; giuliomoro@135: int expectedClockSyncType; giuliomoro@135: myClock_t clockSyncTimestamp; giuliomoro@135: myClock_t localTimestamp; giuliomoro@135: myClock_t T1; giuliomoro@135: myClock_t T1p; giuliomoro@135: myClock_t T2; giuliomoro@135: myClock_t T2p; giuliomoro@135: int receiveLoopSleepUs; giuliomoro@135: int receiveLoopTimeout; giuliomoro@135: char buffer[kSyncMessageLength]; giuliomoro@135: VirtualClock *virtualClock; giuliomoro@141: void resetTs(); giuliomoro@141: bool areTsValid(); giuliomoro@141: void processOffset(double offset); giuliomoro@135: public: giuliomoro@135: ClockSync(){}; giuliomoro@135: ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock); giuliomoro@135: void init(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock); giuliomoro@135: void* getBuffer(); giuliomoro@135: bool isSlave(); giuliomoro@135: bool isMaster(); giuliomoro@135: int getType(); giuliomoro@135: myClock_t getTimestamp(); giuliomoro@135: void setVirtualClock(VirtualClock &aVirtualClock); giuliomoro@135: void setPort(int aPort); giuliomoro@135: void setType(int clockSyncType); giuliomoro@135: void setTimestamp(myClock_t timestamp); giuliomoro@135: void print(); giuliomoro@135: /** giuliomoro@135: * sends a clockSync without blocking, checks results and returns the timestamp giuliomoro@135: * immediately after the clockSync has been sent or -1 if there was an error or timeout expired. giuliomoro@135: */ giuliomoro@135: myClock_t send(); giuliomoro@135: /** giuliomoro@135: * receives a clockSync without blocking, checks results and returns the timestamp giuliomoro@135: * immediately after the clockSync has been received, or -1 if there was an error giuliomoro@135: * or 0 if timeout expired. giuliomoro@135: */ giuliomoro@135: myClock_t receive(); giuliomoro@135: int masterSendSync(); giuliomoro@135: int receiveLoop(); giuliomoro@135: int slaveHandleMessage(); giuliomoro@135: int masterHandleMessage(); giuliomoro@135: int sendReceiveLoop(); giuliomoro@135: operator void*(){ giuliomoro@135: return getBuffer(); giuliomoro@135: } giuliomoro@135: }; giuliomoro@135: giuliomoro@141: #endif /* CLOCK_SYNC_H_INCLUDED */