1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "dcamera_softbus_latency.h"
17
18#include "anonymous_string.h"
19#include "distributed_camera_constants.h"
20#include "distributed_camera_errno.h"
21#include "distributed_hardware_log.h"
22#include "dcamera_softbus_adapter.h"
23#include "softbus_bus_center.h"
24#include "softbus_common.h"
25
26namespace OHOS {
27namespace DistributedHardware {
28IMPLEMENT_SINGLE_INSTANCE(DCameraSoftbusLatency);
29
30constexpr static int32_t MICROSECONDS = 1000;
31
32static void OnTimeSyncResult(const TimeSyncResultInfo *info, int32_t retCode)
33{
34    if (info == nullptr) {
35        DHLOGE("Camera info is null");
36        return;
37    }
38    int32_t microsecond = info->result.millisecond * MICROSECONDS + info->result.microsecond;
39    DHLOGD("DCameraSoftbusLatency OnTimeSyncResult retcode %{public}d, millisecond: %{public}d, microsecond %{public}d "
40        "microsecond_ %{public}d", retCode, info->result.millisecond, info->result.microsecond, microsecond);
41    DCameraSoftbusLatency::GetInstance().SetTimeSyncInfo(microsecond, info->target.targetNetworkId);
42}
43
44int32_t DCameraSoftbusLatency::StartSoftbusTimeSync(const std::string& devId)
45{
46    DHLOGI("StartSoftbusTimeSync latency start.");
47    {
48        std::lock_guard<std::mutex> lock(micLock_);
49        if (refCount_[devId] > REF_INITIAL) {
50            refCount_[devId]++;
51            DHLOGD("No need to start time offset, refCount just plus one and now is: %{public}d.", refCount_[devId]);
52            return DCAMERA_OK;
53        }
54    }
55    ITimeSyncCb timeSyncCb;
56    timeSyncCb.onTimeSyncResult = OnTimeSyncResult;
57    int32_t ret = StartTimeSync(DCAMERA_PKG_NAME.c_str(), devId.c_str(), LOW_ACCURACY, NORMAL_PERIOD,
58        &timeSyncCb);
59    if (ret != DCAMERA_OK) {
60        DHLOGE("DCameraSoftbusLatency:: StartSoftbusTimeSync failed networkId %{public}s",
61            GetAnonyString(devId).c_str());
62    }
63    {
64        std::lock_guard<std::mutex> lock(micLock_);
65        refCount_[devId]++;
66        offsets_.emplace(devId, 0);
67    }
68    DHLOGI("DCameraSoftbusLatency:: StartSoftbusTimeSync success ");
69    return DCAMERA_OK;
70}
71
72int32_t DCameraSoftbusLatency::StopSoftbusTimeSync(const std::string& devId)
73{
74    DHLOGI("DCameraSoftbusLatency::StopSoftbusTimeSync start.");
75    {
76        std::lock_guard<std::mutex> lock(micLock_);
77        if (refCount_[devId] == REF_INITIAL) {
78            DHLOGD("No need to stop time offset, refCount is zero.");
79            return DCAMERA_OK;
80        }
81        if (refCount_[devId] > REF_NORMAL) {
82            refCount_[devId]--;
83            DHLOGD("No need to stop time offset, refCount just minus one and now is: %{public}d.", refCount_[devId]);
84            return DCAMERA_OK;
85        }
86    }
87    int32_t ret = StopTimeSync(DCAMERA_PKG_NAME.c_str(), devId.c_str());
88    if (ret != DCAMERA_OK) {
89        DHLOGE("DCameraSoftbusLatency:: StopSoftbusTimeSync failed ret:%{public}d", ret);
90    }
91    {
92        std::lock_guard<std::mutex> lock(micLock_);
93        refCount_[devId]--;
94        offsets_.erase(devId);
95        refCount_.erase(devId);
96    }
97    return DCAMERA_OK;
98}
99
100void DCameraSoftbusLatency::SetTimeSyncInfo(const int32_t microsecond, const std::string& devId)
101{
102    std::lock_guard<std::mutex> lock(offsetLock_);
103    offsets_[devId] = microsecond;
104}
105
106int32_t DCameraSoftbusLatency::GetTimeSyncInfo(const std::string& devId)
107{
108    std::lock_guard<std::mutex> lock(offsetLock_);
109    auto dev = offsets_.find(devId);
110    if (dev == offsets_.end()) {
111        return DCAMERA_OK;
112    }
113    return offsets_[devId];
114}
115} // namespace DistributedHardware
116}