common_time_server.h revision 778c859bfdcce2fa6658c09e65656afc34d3b4e0
16c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen/*
26c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Copyright (C) 2012 The Android Open Source Project
36c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen *
46c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Licensed under the Apache License, Version 2.0 (the "License");
56c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * you may not use this file except in compliance with the License.
66c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * You may obtain a copy of the License at
76c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen *
86c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen *      http://www.apache.org/licenses/LICENSE-2.0
96c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen *
106c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Unless required by applicable law or agreed to in writing, software
116c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * distributed under the License is distributed on an "AS IS" BASIS,
126c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * See the License for the specific language governing permissions and
146c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * limitations under the License.
156c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen */
166c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
176c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#ifndef ANDROID_COMMON_TIME_SERVER_H
186c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#define ANDROID_COMMON_TIME_SERVER_H
196c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
206c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include <arpa/inet.h>
216c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include <stdint.h>
22778c859bfdcce2fa6658c09e65656afc34d3b4e0Elliott Hughes#include <sys/socket.h>
236c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
246c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include <common_time/ICommonClock.h>
256c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include <common_time/local_clock.h>
266c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include <utils/String8.h>
276c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
286c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include "clock_recovery.h"
296c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include "common_clock.h"
306c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#include "common_time_server_packets.h"
316c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
326c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#define RTT_LOG_SIZE 30
336c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
346c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chennamespace android {
356c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
366c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chenclass CommonClockService;
376c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chenclass CommonTimeConfigService;
386c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
396c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen/***** time service implementation *****/
406c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
416c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chenclass CommonTimeServer : public Thread {
426c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen  public:
436c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    CommonTimeServer();
446c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    ~CommonTimeServer();
456c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
466c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool startServices();
476c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
486c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Common Clock API methods
496c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    CommonClock&        getCommonClock()        { return mCommonClock; }
506c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    LocalClock&         getLocalClock()         { return mLocalClock; }
516c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint64_t            getTimelineID();
526c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int32_t             getEstimatedError();
536c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    ICommonClock::State getState();
546c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t            getMasterAddr(struct sockaddr_storage* addr);
556c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t            isCommonTimeValid(bool* valid, uint32_t* timelineID);
566c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
576c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Config API methods
586c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getMasterElectionPriority(uint8_t *priority);
596c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setMasterElectionPriority(uint8_t priority);
606c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getMasterElectionEndpoint(struct sockaddr_storage *addr);
616c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setMasterElectionEndpoint(const struct sockaddr_storage *addr);
626c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getMasterElectionGroupId(uint64_t *id);
636c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setMasterElectionGroupId(uint64_t id);
646c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getInterfaceBinding(String8& ifaceName);
656c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setInterfaceBinding(const String8& ifaceName);
666c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getMasterAnnounceInterval(int *interval);
676c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setMasterAnnounceInterval(int interval);
686c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getClientSyncInterval(int *interval);
696c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setClientSyncInterval(int interval);
706c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getPanicThreshold(int *threshold);
716c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setPanicThreshold(int threshold);
726c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t getAutoDisable(bool *autoDisable);
736c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t setAutoDisable(bool autoDisable);
746c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t forceNetworklessMasterMode();
756c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
766c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Method used by the CommonClockService to notify the core service about
776c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // changes in the number of active common clock clients.
786c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void reevaluateAutoDisableState(bool commonClockHasClients);
796c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
806c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t dumpClockInterface(int fd, const Vector<String16>& args,
816c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                size_t activeClients);
826c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    status_t dumpConfigInterface(int fd, const Vector<String16>& args);
836c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
846c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen  private:
856c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    class PacketRTTLog {
866c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen      public:
876c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        PacketRTTLog() {
886c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen            resetLog();
896c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        }
906c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
916c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        void resetLog() {
926c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen            wrPtr = 0;
936c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen            logFull = 0;
946c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        }
956c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
966c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        void logTX(int64_t txTime);
976c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        void logRX(int64_t txTime, int64_t rxTime);
986c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        void dumpLog(int fd, const CommonClock& cclk);
996c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1006c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen      private:
1016c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        uint32_t wrPtr;
1026c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        bool logFull;
1036c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        int64_t txTimes[RTT_LOG_SIZE];
1046c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        int64_t rxTimes[RTT_LOG_SIZE];
1056c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    };
1066c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1076c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    class TimeoutHelper {
1086c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen      public:
1096c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        TimeoutHelper() : mTimeoutValid(false) { }
1106c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1116c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        void setTimeout(int msec);
1126c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        int msecTillTimeout();
1136c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1146c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen      private:
1156c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        bool        mTimeoutValid;
1166c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        nsecs_t     mEndTime;
1176c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    };
1186c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1196c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool threadLoop();
1206c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1216c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool runStateMachine_l();
1226c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool setupSocket_l();
1236c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1246c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void assignTimelineID();
1256c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool assignDeviceID();
1266c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1276c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static bool arbitrateMaster(uint64_t deviceID1, uint8_t devicePrio1,
1286c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                uint64_t deviceID2, uint8_t devicePrio2);
1296c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1306c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handlePacket();
1316c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleWhoIsMasterRequest (const WhoIsMasterRequestPacket* request,
1326c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                   const sockaddr_storage& srcAddr);
1336c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleWhoIsMasterResponse(const WhoIsMasterResponsePacket* response,
1346c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                   const sockaddr_storage& srcAddr);
1356c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleSyncRequest        (const SyncRequestPacket* request,
1366c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                   const sockaddr_storage& srcAddr);
1376c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleSyncResponse       (const SyncResponsePacket* response,
1386c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                   const sockaddr_storage& srcAddr);
1396c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleMasterAnnouncement (const MasterAnnouncementPacket* packet,
1406c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                   const sockaddr_storage& srcAddr);
1416c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1426c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeout();
1436c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeoutInitial();
1446c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeoutClient();
1456c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeoutMaster();
1466c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeoutRonin();
1476c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool handleTimeoutWaitForElection();
1486c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1496c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool sendWhoIsMasterRequest();
1506c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool sendSyncRequest();
1516c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool sendMasterAnnouncement();
1526c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1536c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool becomeClient(const sockaddr_storage& masterAddr,
1546c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                      uint64_t masterDeviceID,
1556c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                      uint8_t  masterDevicePriority,
1566c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                      uint64_t timelineID,
1576c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                      const char* cause);
1586c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool becomeMaster(const char* cause);
1596c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool becomeRonin(const char* cause);
1606c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool becomeWaitForElection(const char* cause);
1616c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool becomeInitial(const char* cause);
1626c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1636c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void notifyClockSync();
1646c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void notifyClockSyncLoss();
1656c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1666c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    ICommonClock::State mState;
1676c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void setState(ICommonClock::State s);
1686c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1696c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void clearPendingWakeupEvents_l();
1706c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void wakeupThread_l();
1716c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void cleanupSocket_l();
1726c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    void shutdownThread();
1736c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1746c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    inline uint8_t effectivePriority() const {
1756c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        return (mMasterPriority & 0x7F) |
1766c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen               (mForceLowPriority ? 0x00 : 0x80);
1776c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    }
1786c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1796c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    inline bool shouldAutoDisable() const {
1806c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        return (mAutoDisable && !mCommonClockHasClients);
1816c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    }
1826c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1836c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    inline void resetSyncStats() {
1846c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_SyncRequestPending = false;
1856c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_SyncRequestTimeouts = 0;
1866c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_SyncsSentToCurMaster = 0;
1876c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_SyncRespsRXedFromCurMaster = 0;
1886c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_ExpiredSyncRespsRXedFromCurMaster = 0;
1896c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_FirstSyncTX = 0;
1906c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_LastGoodSyncRX = 0;
1916c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mClient_PacketRTTLog.resetLog();
1926c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    }
1936c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1946c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool shouldPanicNotGettingGoodData();
1956c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1966c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Helper to keep track of the state machine's current timeout
1976c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    TimeoutHelper mCurTimeout;
1986c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
1996c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // common clock, local clock abstraction, and clock recovery loop
2006c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    CommonClock mCommonClock;
2016c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    LocalClock mLocalClock;
2026c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    ClockRecoveryLoop mClockRecovery;
2036c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2046c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // implementation of ICommonClock
2056c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    sp<CommonClockService> mICommonClock;
2066c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2076c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // implementation of ICommonTimeConfig
2086c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    sp<CommonTimeConfigService> mICommonTimeConfig;
2096c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2106c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // UDP socket for the time sync protocol
2116c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int mSocket;
2126c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2136c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // eventfd used to wakeup the work thread in response to configuration
2146c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // changes.
2156c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int mWakeupThreadFD;
2166c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2176c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // timestamp captured when a packet is received
2186c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int64_t mLastPacketRxLocalTime;
2196c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2206c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // ID of the timeline that this device is following
2216c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint64_t mTimelineID;
2226c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2236c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // flag for whether the clock has been synced to a timeline
2246c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool mClockSynced;
2256c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2266c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // flag used to indicate that clients should be considered to be lower
2276c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // priority than all of their peers during elections.  This flag is set and
2286c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // cleared by the state machine.  It is set when the client joins a new
2296c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // network.  If the client had been a master in the old network (or an
2306c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // isolated master with no network connectivity) it should defer to any
2316c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // masters which may already be on the network.  It will be cleared whenever
2326c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // the state machine transitions to the master state.
2336c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool mForceLowPriority;
2346c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    inline void setForceLowPriority(bool val) {
2356c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        mForceLowPriority = val;
2366c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen        if (mState == ICommonClock::STATE_MASTER)
2376c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen            mClient_MasterDevicePriority = effectivePriority();
2386c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    }
2396c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2406c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Lock to synchronize access to internal state and configuration.
2416c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    Mutex mLock;
2426c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2436c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Flag updated by the common clock service to indicate that it does or does
2446c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // not currently have registered clients.  When the the auto disable flag is
2456c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // cleared on the common time service, the service will participate in
2466c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // network synchronization whenever it has a valid network interface to bind
2476c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // to.  When the auto disable flag is set on the common time service, it
2486c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // will only participate in network synchronization when it has both a valid
2496c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // interface AND currently active common clock clients.
2506c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool mCommonClockHasClients;
2516c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2526c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Configuration info
2536c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    struct sockaddr_storage mMasterElectionEP;          // Endpoint over which we conduct master election
2546c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    String8                 mBindIface;                 // Endpoint for the service to bind to.
2556c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool                    mBindIfaceValid;            // whether or not the bind Iface is valid.
2566c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool                    mBindIfaceDirty;            // whether or not the bind Iface is valid.
2576c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    struct sockaddr_storage mMasterEP;                  // Endpoint of our current master (if any)
2586c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool                    mMasterEPValid;
2596c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint64_t                mDeviceID;                  // unique ID of this device
2606c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint64_t                mSyncGroupID;               // synchronization group ID of this device.
2616c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint8_t                 mMasterPriority;            // Priority of this device in master election.
2626c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t                mMasterAnnounceIntervalMs;
2636c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t                mSyncRequestIntervalMs;
2646c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t                mPanicThresholdUsec;
2656c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool                    mAutoDisable;
2666c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2676c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Config defaults.
2686c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const char*      kDefaultMasterElectionAddr;
2696c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint16_t   kDefaultMasterElectionPort;
2706c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint64_t   kDefaultSyncGroupID;
2716c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint8_t    kDefaultMasterPriority;
2726c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t   kDefaultMasterAnnounceIntervalMs;
2736c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t   kDefaultSyncRequestIntervalMs;
2746c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t   kDefaultPanicThresholdUsec;
2756c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const bool       kDefaultAutoDisable;
2766c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2776c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Priority mask and shift fields.
2786c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint64_t kDeviceIDMask;
2796c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint8_t  kDevicePriorityMask;
2806c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint8_t  kDevicePriorityHiLowBit;
2816c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t kDevicePriorityShift;
2826c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2836c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    // Unconfgurable constants
2846c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int      kSetupRetryTimeoutMs;
2856c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int64_t  kNoGoodDataPanicThresholdUsec;
2866c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t kRTTDiscardPanicThreshMultiplier;
2876c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2886c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    /*** status while in the Initial state ***/
2896c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int mInitial_WhoIsMasterRequestTimeouts;
2906c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kInitial_NumWhoIsMasterRetries;
2916c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kInitial_WhoIsMasterTimeoutMs;
2926c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
2936c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    /*** status while in the Client state ***/
2946c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint64_t mClient_MasterDeviceID;
2956c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint8_t mClient_MasterDevicePriority;
2966c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    bool mClient_SyncRequestPending;
2976c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int mClient_SyncRequestTimeouts;
2986c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t mClient_SyncsSentToCurMaster;
2996c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t mClient_SyncRespsRXedFromCurMaster;
3006c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    uint32_t mClient_ExpiredSyncRespsRXedFromCurMaster;
3016c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int64_t mClient_FirstSyncTX;
3026c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int64_t mClient_LastGoodSyncRX;
3036c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    PacketRTTLog mClient_PacketRTTLog;
3046c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kClient_NumSyncRequestRetries;
3056c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3066c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3076c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    /*** status while in the Master state ***/
3086c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const uint32_t kDefaultMaster_AnnouncementIntervalMs;
3096c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3106c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    /*** status while in the Ronin state ***/
3116c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    int mRonin_WhoIsMasterRequestTimeouts;
3126c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kRonin_NumWhoIsMasterRetries;
3136c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kRonin_WhoIsMasterTimeoutMs;
3146c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3156c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    /*** status while in the WaitForElection state ***/
3166c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kWaitForElection_TimeoutMs;
3176c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3186c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const int kInfiniteTimeout;
3196c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3206c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static const char* stateToString(ICommonClock::State s);
3216c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static void sockaddrToString(const sockaddr_storage& addr, bool addrValid,
3226c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                                 char* buf, size_t bufLen);
3236c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen    static bool sockaddrMatch(const sockaddr_storage& a1,
3246c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                              const sockaddr_storage& a2,
3256c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen                              bool matchAddressOnly);
3266c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen};
3276c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3286c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen}  // namespace android
3296c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen
3306c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#endif  // ANDROID_COMMON_TIME_SERVER_H
331