1951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen/*
2951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Copyright (C) 2011 The Android Open Source Project
3951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
4951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Licensed under the Apache License, Version 2.0 (the "License");
5951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * you may not use this file except in compliance with the License.
6951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * You may obtain a copy of the License at
7951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
8951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *      http://www.apache.org/licenses/LICENSE-2.0
9951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen *
10951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * Unless required by applicable law or agreed to in writing, software
11951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * distributed under the License is distributed on an "AS IS" BASIS,
12951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * See the License for the specific language governing permissions and
14951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen * limitations under the License.
15951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen */
16951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
17951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#ifndef __CC_HELPER_H__
18951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#define __CC_HELPER_H__
19951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
20951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <stdint.h>
21951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <common_time/ICommonClock.h>
22951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#include <utils/threads.h>
23951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
24951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chennamespace android {
25951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
26951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// CCHelper is a simple wrapper class to help with centralizing access to the
27951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// Common Clock service and implementing lifetime managment, as well as to
28951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// implement a simple policy of making a basic attempt to reconnect to the
29951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// common clock service when things go wrong.
30951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen//
31951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// On platforms which run the native common_time service in auto-disable mode,
32951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// the service will go into networkless mode whenever it has no active clients.
33951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// It tracks active clients using registered CommonClockListeners (the callback
34951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// interface for onTimelineChanged) since this provides a convienent death
35951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// handler notification for when the service's clients die unexpectedly.  This
36951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// means that users of the common time service should really always have a
37951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// CommonClockListener, unless they know that the time service is not running in
38951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// auto disabled mode, or that there is at least one other registered listener
39951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// active in the system.  The CCHelper makes this a little easier by sharing a
40951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// ref counted ICommonClock interface across all clients and automatically
41951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// registering and unregistering a listener whenever there are CCHelper
42951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen// instances active in the process.
43951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chenclass CCHelper {
44951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  public:
45951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    CCHelper();
46951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    ~CCHelper();
47951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
48951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t isCommonTimeValid(bool* valid, uint32_t* timelineID);
49951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t commonTimeToLocalTime(int64_t commonTime, int64_t* localTime);
50951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t localTimeToCommonTime(int64_t localTime, int64_t* commonTime);
51951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t getCommonTime(int64_t* commonTime);
52951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t getCommonFreq(uint64_t* freq);
53951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t getLocalTime(int64_t* localTime);
54951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    status_t getLocalFreq(uint64_t* freq);
55951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
56951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen  private:
57951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    class CommonClockListener : public BnCommonClockListener {
58951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen      public:
59951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen        void onTimelineChanged(uint64_t timelineID);
60951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    };
61951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
62951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static bool verifyClock_l();
63951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
64951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static Mutex lock_;
65951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static sp<ICommonClock> common_clock_;
66951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static sp<ICommonClockListener> common_clock_listener_;
67951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen    static uint32_t ref_count_;
68951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen};
69951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
70951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen
71951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen}  // namespace android
72951bd8d1ad9581a414e171ad8605a9515d0ad667Mike J. Chen#endif  // __CC_HELPER_H__
73