1 /*
2 * Copyright (C) 2025-2025 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 "interoperable_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 InteroperableDataController::OnReceiveMsg(const char* data, uint32_t dataLen)
23 {
24 if (dataLen > INTEROPERABLE_MAX_RECV_DATA_LEN) {
25 TELEPHONY_LOGE("recv invalid 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>(InteroperableMsgType::DATA_TYPE_UNKNOWN);
35 if (!GetInt32Value(msg, DATA_TYPE, msgType)) {
36 cJSON_Delete(msg);
37 return;
38 }
39 TELEPHONY_LOGI("recv data, msg type %{public}d", msgType);
40 if (msgType == static_cast<int32_t>(InteroperableMsgType::DATA_TYPE_MUTE)) {
41 HandleMuted(msg);
42 } else if (msgType == static_cast<int32_t>(InteroperableMsgType::DATA_TYPE_MUTE_RINGER)) {
43 HandleMuteRinger();
44 }
45 cJSON_Delete(msg);
46 }
47
SetMuted(bool isMute)48 void InteroperableDataController::SetMuted(bool isMute)
49 {
50 if (session_ == nullptr) {
51 TELEPHONY_LOGE("session is null");
52 return;
53 }
54 TELEPHONY_LOGI("send mute %{public}d msg", isMute);
55 auto data = CreateMuteMsg(InteroperableMsgType::DATA_TYPE_MUTE, isMute);
56 if (data.empty()) {
57 return;
58 }
59 session_->SendMsg(data.c_str(), static_cast<uint32_t>(data.length()));
60 }
61
MuteRinger()62 void InteroperableDataController::MuteRinger()
63 {
64 if (session_ == nullptr) {
65 TELEPHONY_LOGE("session is null");
66 return;
67 }
68 TELEPHONY_LOGI("send mute ringer msg");
69 auto data = CreateMuteRingerMsg(InteroperableMsgType::DATA_TYPE_MUTE_RINGER);
70 if (data.empty()) {
71 return;
72 }
73 session_->SendMsg(data.c_str(), static_cast<uint32_t>(data.length()));
74 }
75
HandleMuted(const cJSON * msg)76 void InteroperableDataController::HandleMuted(const cJSON *msg)
77 {
78 bool isMute = false;
79 if (!GetBoolValue(msg, INTEROPERABLE_ITEM_MUTE, isMute)) {
80 return;
81 }
82 TELEPHONY_LOGI("set muted %{public}d", isMute);
83 DelayedSingleton<CallControlManager>::GetInstance()->SetMuted(isMute);
84 }
85
HandleMuteRinger()86 void InteroperableDataController::HandleMuteRinger()
87 {
88 TELEPHONY_LOGI("set mute ringer");
89 auto controlManager = DelayedSingleton<CallControlManager>::GetInstance()->MuteRinger();
90 }
91
CreateMuteMsg(InteroperableMsgType msgType,bool isMute)92 std::string InteroperableDataController::CreateMuteMsg(InteroperableMsgType msgType, bool isMute)
93 {
94 std::string data = "";
95 cJSON *muteMsg = cJSON_CreateObject();
96 if (muteMsg == nullptr) {
97 TELEPHONY_LOGE("create json msg fail");
98 return data;
99 }
100
101 cJSON_AddNumberToObject(muteMsg, DATA_TYPE, static_cast<int32_t>(msgType));
102 cJSON_AddBoolToObject(muteMsg, INTEROPERABLE_ITEM_MUTE, isMute);
103 char *jsonData = cJSON_PrintUnformatted(muteMsg);
104 if (jsonData != nullptr) {
105 data = jsonData;
106 free(jsonData);
107 jsonData = nullptr;
108 }
109 cJSON_Delete(muteMsg);
110 return data;
111 }
112
CreateMuteRingerMsg(InteroperableMsgType msgType)113 std::string InteroperableDataController::CreateMuteRingerMsg(InteroperableMsgType msgType)
114 {
115 std::string data = "";
116 cJSON *muteRingerMsg = cJSON_CreateObject();
117 if (muteRingerMsg == nullptr) {
118 TELEPHONY_LOGE("create json msg fail");
119 return data;
120 }
121
122 cJSON_AddNumberToObject(muteRingerMsg, DATA_TYPE, static_cast<int32_t>(msgType));
123 char *jsonData = cJSON_PrintUnformatted(muteRingerMsg);
124 if (jsonData != nullptr) {
125 data = jsonData;
126 free(jsonData);
127 jsonData = nullptr;
128 }
129 cJSON_Delete(muteRingerMsg);
130 return data;
131 }
132
GetInt32Value(const cJSON * msg,const std::string & name,int32_t & value)133 bool InteroperableDataController::GetInt32Value(const cJSON *msg, const std::string &name, int32_t &value)
134 {
135 cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str());
136 if (dataJson == nullptr || !cJSON_IsNumber(dataJson)) {
137 TELEPHONY_LOGE("%{public}s not contain or not number", name.c_str());
138 return false;
139 }
140 value = static_cast<int32_t>(dataJson->valueint);
141 return true;
142 }
143
GetBoolValue(const cJSON * msg,const std::string & name,bool & value)144 bool InteroperableDataController::GetBoolValue(const cJSON *msg, const std::string &name, bool &value)
145 {
146 cJSON *dataJson = cJSON_GetObjectItem(msg, name.c_str());
147 if (dataJson == nullptr || !cJSON_IsBool(dataJson)) {
148 TELEPHONY_LOGE("%{public}s not contain or not bool", name.c_str());
149 return false;
150 }
151 value = cJSON_IsTrue(dataJson);
152 return true;
153 }
154 }
155 }