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 "avcall_state.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
AVCallState()20 AVCallState::AVCallState()
21 {
22 }
23 
Marshalling(Parcel& parcel) const24 bool AVCallState::Marshalling(Parcel& parcel) const
25 {
26     return parcel.WriteString(mask_.to_string()) &&
27         parcel.WriteInt32(avCallState_) &&
28         parcel.WriteBool(isAVCallMuted_);
29 }
30 
Unmarshalling(Parcel& parcel)31 AVCallState *AVCallState::Unmarshalling(Parcel& parcel)
32 {
33     std::string mask;
34     CHECK_AND_RETURN_RET_LOG(parcel.ReadString(mask) && mask.length() == AVCALL_STATE_KEY_MAX,
35         nullptr, "mask not valid");
36     CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos,
37         nullptr, "mask string not 0 or 1");
38 
39     auto *result = new (std::nothrow) AVCallState();
40     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVCallState failed");
41     result->mask_ = AVCallStateMaskType(mask);
42     if (!parcel.ReadInt32(result->avCallState_) ||
43         !parcel.ReadBool(result->isAVCallMuted_)) {
44         SLOGE("Read AVCallState failed");
45         delete result;
46         result = nullptr;
47         return nullptr;
48     }
49     return result;
50 }
51 
IsValid() const52 bool AVCallState::IsValid() const
53 {
54     return avCallState_ >= AVCALL_STATE_IDLE &&
55         avCallState_ < AVCALL_STATE_MAX;
56 }
57 
SetAVCallState(int32_t avCallState)58 void AVCallState::SetAVCallState(int32_t avCallState)
59 {
60     mask_.set(AVCALL_STATE_KEY_STATE);
61     avCallState_ = avCallState;
62 }
63 
64 // LCOV_EXCL_START
GetAVCallState() const65 int32_t AVCallState::GetAVCallState() const
66 {
67     return avCallState_;
68 }
69 // LCOV_EXCL_STOP
70 
SetAVCallMuted(bool isAVCallMuted)71 void AVCallState::SetAVCallMuted(bool isAVCallMuted)
72 {
73     mask_.set(AVCALL_STATE_KEY_IS_MUTED);
74     isAVCallMuted_ = isAVCallMuted;
75 }
76 
77 // LCOV_EXCL_START
IsAVCallMuted() const78 bool AVCallState::IsAVCallMuted() const
79 {
80     return isAVCallMuted_;
81 }
82 
GetMask() const83 AVCallState::AVCallStateMaskType AVCallState::GetMask() const
84 {
85     return mask_;
86 }
87 
CopyToByMask(AVCallStateMaskType& mask, AVCallState& out) const88 bool AVCallState::CopyToByMask(AVCallStateMaskType& mask, AVCallState& out) const
89 {
90     bool result = false;
91     auto intersection = mask_ & mask;
92     for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
93         if (intersection.test(i)) {
94             cloneActions[i](*this, out);
95             out.mask_.set(i);
96             result = true;
97         }
98     }
99     return result;
100 }
101 // LCOV_EXCL_STOP
102 
CopyFrom(const AVCallState& in)103 bool AVCallState::CopyFrom(const AVCallState& in)
104 {
105     bool result = false;
106     for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
107         if (in.mask_.test(i)) {
108             cloneActions[i](in, *this);
109             mask_.set(i);
110             result = true;
111         }
112     }
113     return result;
114 }
115 
CloneAVCallState(const AVCallState& from, AVCallState& to)116 void AVCallState::CloneAVCallState(const AVCallState& from, AVCallState& to)
117 {
118     to.avCallState_ = from.avCallState_;
119 }
120 
CloneAVCallIsMuted(const AVCallState& from, AVCallState& to)121 void AVCallState::CloneAVCallIsMuted(const AVCallState& from, AVCallState& to)
122 {
123     to.isAVCallMuted_ = from.isAVCallMuted_;
124 }
125 } // namespace OHOS::AVSession
126