1 /*
2 * Copyright (c) 2024 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 "distributed_device_status.h"
17
18 namespace OHOS {
19 namespace Notification {
20 namespace {
21 const static std::string WEARABLE = "wearable";
22 const static std::string LITEWEARABLE = "liteWearable";
23 }
24
25 DistributedDeviceStatus::DistributedDeviceStatus() = default;
26
27 DistributedDeviceStatus::~DistributedDeviceStatus() = default;
28
SetDeviceStatus(const std::string & deviceType,const uint32_t status,const uint32_t controlFlag)29 ErrCode DistributedDeviceStatus::SetDeviceStatus(const std::string &deviceType, const uint32_t status,
30 const uint32_t controlFlag)
31 {
32 std::lock_guard<std::mutex> lock(mapLock_);
33 uint32_t oldStatus = deviceStatus_.ReadVal(deviceType);
34 for (uint32_t i = 0; i < STATUS_SIZE; i++) {
35 if (((1 << i) & controlFlag) && ((1 << i) & status)) {
36 oldStatus |= (1 << i);
37 }
38 if (((1 << i) & controlFlag) && !((1 << i) & status)) {
39 oldStatus &= ~(1 << i);
40 }
41 }
42 if (deviceType == LITEWEARABLE) {
43 uint32_t wearableStatus = deviceStatus_.ReadVal(WEARABLE);
44 for (uint32_t i = 0; i < STATUS_SIZE; i++) {
45 if (((1 << i) & controlFlag) && ((1 << i) & status)) {
46 wearableStatus |= (1 << i);
47 }
48 if (((1 << i) & controlFlag) && !((1 << i) & status)) {
49 wearableStatus &= ~(1 << i);
50 }
51 }
52 deviceStatus_.EnsureInsert(WEARABLE, wearableStatus);
53 ANS_LOGI("update lite wearable status %{public}u %{public}u", wearableStatus, status);
54 }
55 deviceStatus_.EnsureInsert(deviceType, oldStatus);
56 ANS_LOGI("update %{public}s status %{public}u %{public}u", deviceType.c_str(), oldStatus, status);
57 return ERR_OK;
58 }
59
GetDeviceStatus(const std::string & deviceType)60 uint32_t DistributedDeviceStatus::GetDeviceStatus(const std::string &deviceType)
61 {
62 std::lock_guard<std::mutex> lock(mapLock_);
63 return deviceStatus_.ReadVal(deviceType);
64 }
65 } // namespace Notification
66 } // namespace OHOS
67