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