1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __CC_HELPER_H__
18#define __CC_HELPER_H__
19
20#include <stdint.h>
21#include <common_time/ICommonClock.h>
22#include <utils/threads.h>
23
24namespace android {
25
26// CCHelper is a simple wrapper class to help with centralizing access to the
27// Common Clock service and implementing lifetime managment, as well as to
28// implement a simple policy of making a basic attempt to reconnect to the
29// common clock service when things go wrong.
30//
31// On platforms which run the native common_time service in auto-disable mode,
32// the service will go into networkless mode whenever it has no active clients.
33// It tracks active clients using registered CommonClockListeners (the callback
34// interface for onTimelineChanged) since this provides a convienent death
35// handler notification for when the service's clients die unexpectedly.  This
36// means that users of the common time service should really always have a
37// CommonClockListener, unless they know that the time service is not running in
38// auto disabled mode, or that there is at least one other registered listener
39// active in the system.  The CCHelper makes this a little easier by sharing a
40// ref counted ICommonClock interface across all clients and automatically
41// registering and unregistering a listener whenever there are CCHelper
42// instances active in the process.
43class CCHelper {
44  public:
45    CCHelper();
46    ~CCHelper();
47
48    status_t isCommonTimeValid(bool* valid, uint32_t* timelineID);
49    status_t commonTimeToLocalTime(int64_t commonTime, int64_t* localTime);
50    status_t localTimeToCommonTime(int64_t localTime, int64_t* commonTime);
51    status_t getCommonTime(int64_t* commonTime);
52    status_t getCommonFreq(uint64_t* freq);
53    status_t getLocalTime(int64_t* localTime);
54    status_t getLocalFreq(uint64_t* freq);
55
56  private:
57    class CommonClockListener : public BnCommonClockListener {
58      public:
59        void onTimelineChanged(uint64_t timelineID);
60    };
61
62    static bool verifyClock_l();
63
64    static Mutex lock_;
65    static sp<ICommonClock> common_clock_;
66    static sp<ICommonClockListener> common_clock_listener_;
67    static uint32_t ref_count_;
68};
69
70
71}  // namespace android
72#endif  // __CC_HELPER_H__
73