1 /*
2 * Copyright (C) 2021 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 "sms_interface_stub.h"
17
18 #include "sms_interface_manager.h"
19 #include "string_utils.h"
20 #include "telephony_log_wrapper.h"
21 #include "telephony_types.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 using namespace std;
SmsInterfaceStub()26 SmsInterfaceStub::SmsInterfaceStub()
27 {
28 memberFuncMap_[TEXT_BASED_SMS_DELIVERY] = &SmsInterfaceStub::OnSendSmsTextRequest;
29 memberFuncMap_[DATA_BASED_SMS_DELIVERY] = &SmsInterfaceStub::OnSendSmsDataRequest;
30 memberFuncMap_[SET_SMSC_ADDRESS] = &SmsInterfaceStub::OnSetSmscAddr;
31 memberFuncMap_[GET_SMSC_ADDRESS] = &SmsInterfaceStub::OnGetSmscAddr;
32 memberFuncMap_[ADD_SIM_MESSAGE] = &SmsInterfaceStub::OnAddSimMessage;
33 memberFuncMap_[DEL_SIM_MESSAGE] = &SmsInterfaceStub::OnDelSimMessage;
34 memberFuncMap_[UPDATE_SIM_MESSAGE] = &SmsInterfaceStub::OnUpdateSimMessage;
35 memberFuncMap_[GET_ALL_SIM_MESSAGE] = &SmsInterfaceStub::OnGetAllSimMessages;
36 memberFuncMap_[SET_CB_CONFIG] = &SmsInterfaceStub::OnSetCBConfig;
37 memberFuncMap_[SET_DEFAULT_SMS_SLOT_ID] = &SmsInterfaceStub::OnSetDefaultSmsSlotId;
38 memberFuncMap_[GET_DEFAULT_SMS_SLOT_ID] = &SmsInterfaceStub::OnGetDefaultSmsSlotId;
39 memberFuncMap_[SPLIT_MESSAGE] = &SmsInterfaceStub::OnSplitMessage;
40 memberFuncMap_[GET_SMS_SEGMENTS_INFO] = &SmsInterfaceStub::OnGetSmsSegmentsInfo;
41 memberFuncMap_[GET_IMS_SHORT_MESSAGE_FORMAT] = &SmsInterfaceStub::OnGetImsShortMessageFormat;
42 memberFuncMap_[IS_IMS_SMS_SUPPORTED] = &SmsInterfaceStub::OnIsImsSmsSupported;
43 memberFuncMap_[HAS_SMS_CAPABILITY] = &SmsInterfaceStub::OnHasSmsCapability;
44 }
45
~SmsInterfaceStub()46 SmsInterfaceStub::~SmsInterfaceStub()
47 {
48 slotSmsInterfaceManagerMap_.clear();
49 memberFuncMap_.clear();
50 }
51
InitModule()52 void SmsInterfaceStub::InitModule()
53 {
54 static bool bInitModule = false;
55 if (!bInitModule) {
56 bInitModule = true;
57 std::lock_guard<std::mutex> lock(mutex_);
58 for (int32_t slotId = 0; slotId < SIM_SLOT_COUNT; ++slotId) {
59 slotSmsInterfaceManagerMap_[slotId] = std::make_shared<SmsInterfaceManager>(slotId);
60 slotSmsInterfaceManagerMap_[slotId]->InitInterfaceManager();
61 TELEPHONY_LOGI("SmsInterfaceStub InitModule slotId = %{public}d", slotId);
62 }
63 }
64 }
65
GetSmsInterfaceManager(int32_t slotId)66 std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager(int32_t slotId)
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 std::map<uint32_t, std::shared_ptr<SmsInterfaceManager>>::iterator iter =
70 slotSmsInterfaceManagerMap_.find(slotId);
71 if (iter != slotSmsInterfaceManagerMap_.end()) {
72 return iter->second;
73 }
74 return nullptr;
75 }
76
GetSmsInterfaceManager()77 std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager()
78 {
79 std::lock_guard<std::mutex> lock(mutex_);
80 for (auto &iter : slotSmsInterfaceManagerMap_) {
81 return iter.second;
82 }
83 return nullptr;
84 }
85
OnSendSmsTextRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)86 void SmsInterfaceStub::OnSendSmsTextRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
87 {
88 int32_t result = 0;
89 sptr<ISendShortMessageCallback> sendCallback = nullptr;
90 sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
91 int32_t slotId = data.ReadInt32();
92 u16string desAddr = data.ReadString16();
93 u16string scAddr = data.ReadString16();
94 u16string text = data.ReadString16();
95 sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
96 sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
97 if (remoteSendCallback != nullptr) {
98 sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
99 }
100 if (remoteDeliveryCallback != nullptr) {
101 deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
102 }
103 TELEPHONY_LOGI("MessageID::TEXT_BASED_SMS_DELIVERY %{public}d", slotId);
104 SendMessage(slotId, desAddr, scAddr, text, sendCallback, deliveryCallback);
105 reply.WriteInt32(result);
106 }
107
OnSendSmsDataRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)108 void SmsInterfaceStub::OnSendSmsDataRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
109 {
110 int32_t result = 0;
111 sptr<ISendShortMessageCallback> sendCallback = nullptr;
112 sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
113 int32_t slotId = data.ReadInt32();
114 u16string desAddr = data.ReadString16();
115 u16string scAddr = data.ReadString16();
116 int16_t port = data.ReadInt16();
117 sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
118 sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
119 if (remoteSendCallback != nullptr) {
120 sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
121 }
122 if (remoteDeliveryCallback != nullptr) {
123 deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
124 }
125 int16_t dataLen = data.ReadInt16();
126 const uint8_t *buffer = reinterpret_cast<const uint8_t *>(data.ReadRawData(dataLen));
127 SendMessage(slotId, desAddr, scAddr, port, buffer, dataLen, sendCallback, deliveryCallback);
128 reply.WriteInt32(result);
129 }
130
OnSetSmscAddr(MessageParcel & data,MessageParcel & reply,MessageOption & option)131 void SmsInterfaceStub::OnSetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
132 {
133 bool result = false;
134 int32_t slotId = data.ReadInt32();
135 std::u16string scAddr = data.ReadString16();
136 result = SetSmscAddr(slotId, scAddr);
137 TELEPHONY_LOGI("SetSmscAddr result %{public}d", result);
138 reply.WriteBool(result);
139 }
140
OnGetSmscAddr(MessageParcel & data,MessageParcel & reply,MessageOption & option)141 void SmsInterfaceStub::OnGetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
142 {
143 std::u16string result;
144 int32_t slotId = data.ReadInt32();
145 result = GetSmscAddr(slotId);
146 TELEPHONY_LOGI("GetSmscAddr result size %{public}zu", result.size());
147 reply.WriteString16(result);
148 }
149
OnAddSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)150 void SmsInterfaceStub::OnAddSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
151 {
152 bool result = false;
153 int32_t slotId = data.ReadInt32();
154 std::u16string smsc = data.ReadString16();
155 std::u16string pdu = data.ReadString16();
156 uint32_t status = data.ReadUint32();
157 result = AddSimMessage(slotId, smsc, pdu, static_cast<SimMessageStatus>(status));
158 TELEPHONY_LOGI("AddSimMessage result %{public}d", result);
159 reply.WriteBool(result);
160 }
161
OnDelSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)162 void SmsInterfaceStub::OnDelSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
163 {
164 bool result = false;
165 int32_t slotId = data.ReadInt32();
166 uint32_t msgIndex = data.ReadUint32();
167 result = DelSimMessage(slotId, msgIndex);
168 TELEPHONY_LOGI("DelSimMessage result %{public}d", result);
169 reply.WriteBool(result);
170 }
171
OnUpdateSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)172 void SmsInterfaceStub::OnUpdateSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
173 {
174 bool result = false;
175 int32_t slotId = data.ReadInt32();
176 uint32_t msgIndex = data.ReadUint32();
177 uint32_t newStatus = data.ReadUint32();
178 std::u16string pdu = data.ReadString16();
179 std::u16string smsc = data.ReadString16();
180 result = UpdateSimMessage(slotId, msgIndex, static_cast<SimMessageStatus>(newStatus), pdu, smsc);
181 TELEPHONY_LOGI("UpdateSimMessage result %{public}d", result);
182 reply.WriteBool(result);
183 }
184
OnGetAllSimMessages(MessageParcel & data,MessageParcel & reply,MessageOption & option)185 void SmsInterfaceStub::OnGetAllSimMessages(MessageParcel &data, MessageParcel &reply, MessageOption &option)
186 {
187 std::vector<ShortMessage> result;
188 int32_t slotId = data.ReadInt32();
189 result = GetAllSimMessages(slotId);
190 TELEPHONY_LOGI("GetAllSimMessages size %{public}zu", result.size());
191
192 int32_t resultLen = static_cast<int32_t>(result.size());
193 reply.WriteInt32(resultLen);
194 for (const auto &v : result) {
195 v.Marshalling(reply);
196 }
197 }
198
OnSetCBConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)199 void SmsInterfaceStub::OnSetCBConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
200 {
201 bool result = false;
202 int32_t slotId = data.ReadInt32();
203 bool enable = data.ReadBool();
204 uint32_t fromMsgId = data.ReadUint32();
205 uint32_t toMsgId = data.ReadUint32();
206 uint8_t ranType = data.ReadUint8();
207 result = SetCBConfig(slotId, enable, fromMsgId, toMsgId, ranType);
208 TELEPHONY_LOGI("OnSetCBConfig result %{public}d", result);
209 reply.WriteBool(result);
210 }
211
OnSetDefaultSmsSlotId(MessageParcel & data,MessageParcel & reply,MessageOption & option)212 void SmsInterfaceStub::OnSetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
213 {
214 bool result = false;
215 int32_t slotId = data.ReadInt32();
216 result = SetDefaultSmsSlotId(slotId);
217 TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
218 reply.WriteBool(result);
219 }
220
OnGetDefaultSmsSlotId(MessageParcel & data,MessageParcel & reply,MessageOption & option)221 void SmsInterfaceStub::OnGetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
222 {
223 int32_t result = 0;
224 result = GetDefaultSmsSlotId();
225 TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
226 reply.WriteInt32(result);
227 }
228
OnSplitMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)229 void SmsInterfaceStub::OnSplitMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
230 {
231 std::vector<std::u16string> result;
232 std::u16string message = data.ReadString16();
233 result = SplitMessage(message);
234 int32_t resultLen = static_cast<int32_t>(result.size());
235 TELEPHONY_LOGI("SplitMessage size %{public}d", resultLen);
236 reply.WriteInt32(resultLen);
237 for (const auto &item : result) {
238 reply.WriteString16(item);
239 }
240 }
241
OnGetSmsSegmentsInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)242 void SmsInterfaceStub::OnGetSmsSegmentsInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
243 {
244 int32_t slotId = data.ReadInt32();
245 std::u16string message = data.ReadString16();
246 bool force7BitCode = data.ReadBool();
247
248 SmsSegmentsInfo segInfo;
249 bool result = GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
250 reply.WriteBool(result);
251
252 if (result) {
253 reply.WriteInt32(segInfo.msgSegCount);
254 reply.WriteInt32(segInfo.msgEncodingCount);
255 reply.WriteInt32(segInfo.msgRemainCount);
256 reply.WriteInt32(static_cast<int32_t>(segInfo.msgCodeScheme));
257 }
258 }
259
OnIsImsSmsSupported(MessageParcel & data,MessageParcel & reply,MessageOption & option)260 void SmsInterfaceStub::OnIsImsSmsSupported(MessageParcel &data, MessageParcel &reply, MessageOption &option)
261 {
262 reply.WriteBool(IsImsSmsSupported());
263 }
264
OnGetImsShortMessageFormat(MessageParcel & data,MessageParcel & reply,MessageOption & option)265 void SmsInterfaceStub::OnGetImsShortMessageFormat(MessageParcel &data, MessageParcel &reply, MessageOption &option)
266 {
267 std::u16string format = GetImsShortMessageFormat();
268 reply.WriteString16(format);
269 }
270
OnHasSmsCapability(MessageParcel & data,MessageParcel & reply,MessageOption & option)271 void SmsInterfaceStub::OnHasSmsCapability(MessageParcel &data, MessageParcel &reply, MessageOption &option)
272 {
273 reply.WriteBool(HasSmsCapability());
274 }
275
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)276 int SmsInterfaceStub::OnRemoteRequest(
277 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
278 {
279 int32_t result = 0;
280 TELEPHONY_LOGI("###################################### OnRemoteRequest");
281 std::u16string myDescripter = SmsInterfaceStub::GetDescriptor();
282 std::u16string remoteDescripter = data.ReadInterfaceToken();
283 if (myDescripter == remoteDescripter) {
284 auto itFunc = memberFuncMap_.find(code);
285 if (itFunc != memberFuncMap_.end()) {
286 auto memberFunc = itFunc->second;
287 if (memberFunc != nullptr) {
288 (this->*memberFunc)(data, reply, option);
289 } else {
290 TELEPHONY_LOGE("memberFunc is nullptr");
291 }
292 } else {
293 TELEPHONY_LOGE("itFunc was not found");
294 }
295 } else {
296 TELEPHONY_LOGE("descriptor checked fail");
297 return result;
298 }
299 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
300 }
301 } // namespace Telephony
302 } // namespace OHOS