• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-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_data_controller.h"
17 #include "telephony_log_wrapper.h"
18 #include "call_control_manager.h"
19 
20 namespace OHOS {
21 namespace Telephony {
OnReceiveMsg(const char * data,uint32_t dataLen)22 void DistributedDataController::OnReceiveMsg(const char* data, uint32_t dataLen)
23 {
24     if (dataLen > DISTRIBUTED_MAX_RECV_DATA_LEN) {
25         TELEPHONY_LOGE("recv invalid distributed data len %{public}d", dataLen);
26         return;
27     }
28     std::string recvData(data, dataLen);
29     cJSON *msg = cJSON_Parse(recvData.c_str());
30     if (msg == nullptr) {
31         TELEPHONY_LOGE("json string invalid");
32         return;
33     }
34     int32_t msgType = static_cast<int32_t>(DistributedMsgType::UNKNOWN);
35     if (!GetInt32Value(msg, DISTRIBUTED_MSG_TYPE, msgType)) {
36         cJSON_Delete(msg);
37         return;
38     }
39     TELEPHONY_LOGI("recv data, msg type %{public}d", msgType);
40     switch (msgType) {
41         case static_cast<int32_t>(DistributedMsgType::MUTE):
42             HandleMuted(msg);
43             break;
44         case static_cast<int32_t>(DistributedMsgType::MUTE_RINGER):
45             HandleMuteRinger();
46             break;
47         default:
48             HandleRecvMsg(msgType, msg);
49             break;
50     }
51     cJSON_Delete(msg);
52 }
53 
SetMuted(bool isMute)54 void DistributedDataController::SetMuted(bool isMute)
55 {
56     if (session_ == nullptr) {
57         TELEPHONY_LOGE("session is null");
58         return;
59     }
60     TELEPHONY_LOGI("send muted %{public}d", isMute);
61     auto data = CreateMuteMsg(DistributedMsgType::MUTE, isMute);
62     if (data.empty()) {
63         return;
64     }
65     session_->SendMsg(data.c_str(), static_cast<uint32_t>(data.length()));
66 }
67 
MuteRinger()68 void DistributedDataController::MuteRinger()
69 {
70     if (session_ == nullptr) {
71         return;
72     }
73     TELEPHONY_LOGI("send mute ringer");
74     auto data = CreateMuteRingerMsg(DistributedMsgType::MUTE_RINGER);
75     if (data.empty()) {
76         return;
77     }
78     session_->SendMsg(data.c_str(), static_cast<uint32_t>(data.length()));
79 }
80 
GetInt32Value(const cJSON * msg,const std::string & name,int32_t & value)81 bool DistributedDataController::GetInt32Value(const cJSON *msg, const std::string &name, int32_t &value)
82 {
83     cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str());
84     if (dataJson == nullptr || !cJSON_IsNumber(dataJson)) {
85         TELEPHONY_LOGE("%{public}s not contain or not number", name.c_str());
86         return false;
87     }
88     value = static_cast<int32_t>(dataJson->valueint);
89     return true;
90 }
91 
GetStringValue(const cJSON * msg,const std::string & name,std::string & value)92 bool DistributedDataController::GetStringValue(const cJSON *msg, const std::string &name, std::string &value)
93 {
94     cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str());
95     if (dataJson == nullptr || !cJSON_IsString(dataJson)) {
96         TELEPHONY_LOGE("%{public}s not contain or not string", name.c_str());
97         return false;
98     }
99     char *data = cJSON_GetStringValue(dataJson);
100     if (data == nullptr) {
101         TELEPHONY_LOGE("get null %{public}s", name.c_str());
102         return false;
103     }
104     value = data;
105     return true;
106 }
107 
GetBoolValue(const cJSON * msg,const std::string & name,bool & value)108 bool DistributedDataController::GetBoolValue(const cJSON *msg, const std::string &name, bool &value)
109 {
110     cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str());
111     if (dataJson == nullptr || !cJSON_IsBool(dataJson)) {
112         TELEPHONY_LOGE("%{public}s not contain or not bool", name.c_str());
113         return false;
114     }
115     value = cJSON_IsTrue(dataJson);
116     return true;
117 }
118 
HandleMuted(const cJSON * msg)119 void DistributedDataController::HandleMuted(const cJSON *msg)
120 {
121     bool isMuted = false;
122     if (!GetBoolValue(msg, DISTRIBUTED_ITEM_MUTE, isMuted)) {
123         return;
124     }
125     TELEPHONY_LOGI("set muted %{public}d", isMuted);
126     auto controlManager = DelayedSingleton<CallControlManager>::GetInstance();
127     if (controlManager != nullptr) {
128         controlManager->SetMuted(isMuted);
129     }
130 }
131 
CreateMuteMsg(DistributedMsgType msgType,bool isMute)132 std::string DistributedDataController::CreateMuteMsg(DistributedMsgType msgType, bool isMute)
133 {
134     std::string data = "";
135     cJSON *muteMsg = cJSON_CreateObject();
136     if (muteMsg == nullptr) {
137         TELEPHONY_LOGE("create json msg fail");
138         return data;
139     }
140     do {
141         if (cJSON_AddNumberToObject(muteMsg, DISTRIBUTED_MSG_TYPE, static_cast<int32_t>(msgType)) == nullptr) {
142             TELEPHONY_LOGE("add msg type fail");
143             break;
144         }
145         if (cJSON_AddBoolToObject(muteMsg, DISTRIBUTED_ITEM_MUTE, isMute) == nullptr) {
146             TELEPHONY_LOGE("add mute value fail");
147             break;
148         }
149         char *jsonData = cJSON_PrintUnformatted(muteMsg);
150         if (jsonData != nullptr) {
151             data = jsonData;
152             free(jsonData);
153             jsonData = nullptr;
154         }
155     } while (false);
156     cJSON_Delete(muteMsg);
157     return data;
158 }
159 
CreateMuteRingerMsg(DistributedMsgType msgType)160 std::string DistributedDataController::CreateMuteRingerMsg(DistributedMsgType msgType)
161 {
162     std::string data = "";
163     cJSON *muteRingerMsg = cJSON_CreateObject();
164     if (muteRingerMsg == nullptr) {
165         TELEPHONY_LOGE("create json msg fail");
166         return data;
167     }
168     do {
169         if (cJSON_AddNumberToObject(muteRingerMsg, DISTRIBUTED_MSG_TYPE, static_cast<int32_t>(msgType)) == nullptr) {
170             TELEPHONY_LOGE("add msg type fail");
171             break;
172         }
173         char *jsonData = cJSON_PrintUnformatted(muteRingerMsg);
174         if (jsonData != nullptr) {
175             data = jsonData;
176             free(jsonData);
177             jsonData = nullptr;
178         }
179     } while (false);
180     cJSON_Delete(muteRingerMsg);
181     return data;
182 }
183 
HandleMuteRinger()184 void DistributedDataController::HandleMuteRinger()
185 {
186     TELEPHONY_LOGI("mute ringer");
187     auto controlManager = DelayedSingleton<CallControlManager>::GetInstance();
188     if (controlManager != nullptr) {
189         controlManager->MuteRinger();
190     }
191 }
192 
193 } // namespace Telephony
194 } // namespace OHOS
195