1/*
2 * Copyright (C) 2012 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#define __STDC_LIMIT_MACROS
18
19#define LOG_TAG "common_time"
20#include <utils/Log.h>
21
22#include <inttypes.h>
23#include <stdint.h>
24
25#include <utils/Errors.h>
26#include <utils/LinearTransform.h>
27
28#include "common_clock.h"
29
30namespace android {
31
32CommonClock::CommonClock() {
33    cur_slew_        = 0;
34    cur_trans_valid_ = false;
35
36    cur_trans_.a_zero = 0;
37    cur_trans_.b_zero = 0;
38    cur_trans_.a_to_b_numer = local_to_common_freq_numer_ = 1;
39    cur_trans_.a_to_b_denom = local_to_common_freq_denom_ = 1;
40    duration_trans_ = cur_trans_;
41}
42
43bool CommonClock::init(uint64_t local_freq) {
44    Mutex::Autolock lock(&lock_);
45
46    if (!local_freq)
47        return false;
48
49    uint64_t numer = kCommonFreq;
50    uint64_t denom = local_freq;
51
52    LinearTransform::reduce(&numer, &denom);
53    if ((numer > UINT32_MAX) || (denom > UINT32_MAX)) {
54        ALOGE("Overflow in CommonClock::init while trying to reduce %" PRIu64 "/%" PRIu64,
55             kCommonFreq, local_freq);
56        return false;
57    }
58
59    cur_trans_.a_to_b_numer = local_to_common_freq_numer_ =
60        static_cast<uint32_t>(numer);
61    cur_trans_.a_to_b_denom = local_to_common_freq_denom_ =
62        static_cast<uint32_t>(denom);
63    duration_trans_ = cur_trans_;
64
65    return true;
66}
67
68status_t CommonClock::localToCommon(int64_t local, int64_t *common_out) const {
69    Mutex::Autolock lock(&lock_);
70
71    if (!cur_trans_valid_)
72        return INVALID_OPERATION;
73
74    if (!cur_trans_.doForwardTransform(local, common_out))
75        return INVALID_OPERATION;
76
77    return OK;
78}
79
80status_t CommonClock::commonToLocal(int64_t common, int64_t *local_out) const {
81    Mutex::Autolock lock(&lock_);
82
83    if (!cur_trans_valid_)
84        return INVALID_OPERATION;
85
86    if (!cur_trans_.doReverseTransform(common, local_out))
87        return INVALID_OPERATION;
88
89    return OK;
90}
91
92int64_t CommonClock::localDurationToCommonDuration(int64_t localDur) const {
93    int64_t ret;
94    duration_trans_.doForwardTransform(localDur, &ret);
95    return ret;
96}
97
98void CommonClock::setBasis(int64_t local, int64_t common) {
99    Mutex::Autolock lock(&lock_);
100
101    cur_trans_.a_zero = local;
102    cur_trans_.b_zero = common;
103    cur_trans_valid_ = true;
104}
105
106void CommonClock::resetBasis() {
107    Mutex::Autolock lock(&lock_);
108
109    cur_trans_.a_zero = 0;
110    cur_trans_.b_zero = 0;
111    cur_trans_valid_ = false;
112}
113
114status_t CommonClock::setSlew(int64_t change_time, int32_t ppm) {
115    Mutex::Autolock lock(&lock_);
116
117    int64_t new_local_basis;
118    int64_t new_common_basis;
119
120    if (cur_trans_valid_) {
121        new_local_basis = change_time;
122        if (!cur_trans_.doForwardTransform(change_time, &new_common_basis)) {
123            ALOGE("Overflow when attempting to set slew rate to %d", ppm);
124            return INVALID_OPERATION;
125        }
126    } else {
127        new_local_basis = 0;
128        new_common_basis = 0;
129    }
130
131    cur_slew_ = ppm;
132    uint32_t n1 = local_to_common_freq_numer_;
133    uint32_t n2 = 1000000 + cur_slew_;
134
135    uint32_t d1 = local_to_common_freq_denom_;
136    uint32_t d2 = 1000000;
137
138    // n1/d1 has already been reduced, no need to do so here.
139    LinearTransform::reduce(&n1, &d2);
140    LinearTransform::reduce(&n2, &d1);
141    LinearTransform::reduce(&n2, &d2);
142
143    cur_trans_.a_zero = new_local_basis;
144    cur_trans_.b_zero = new_common_basis;
145    cur_trans_.a_to_b_numer = n1 * n2;
146    cur_trans_.a_to_b_denom = d1 * d2;
147
148    return OK;
149}
150
151}  // namespace android
152