• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_errors.h"
21 #include "telephony_log_wrapper.h"
22 #include "telephony_types.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 using namespace std;
SmsInterfaceStub()27 SmsInterfaceStub::SmsInterfaceStub()
28 {
29     memberFuncMap_[TEXT_BASED_SMS_DELIVERY] = &SmsInterfaceStub::OnSendSmsTextRequest;
30     memberFuncMap_[DATA_BASED_SMS_DELIVERY] = &SmsInterfaceStub::OnSendSmsDataRequest;
31     memberFuncMap_[SET_SMSC_ADDRESS] = &SmsInterfaceStub::OnSetSmscAddr;
32     memberFuncMap_[GET_SMSC_ADDRESS] = &SmsInterfaceStub::OnGetSmscAddr;
33     memberFuncMap_[ADD_SIM_MESSAGE] = &SmsInterfaceStub::OnAddSimMessage;
34     memberFuncMap_[DEL_SIM_MESSAGE] = &SmsInterfaceStub::OnDelSimMessage;
35     memberFuncMap_[UPDATE_SIM_MESSAGE] = &SmsInterfaceStub::OnUpdateSimMessage;
36     memberFuncMap_[GET_ALL_SIM_MESSAGE] = &SmsInterfaceStub::OnGetAllSimMessages;
37     memberFuncMap_[SET_CB_CONFIG] = &SmsInterfaceStub::OnSetCBConfig;
38     memberFuncMap_[SET_IMS_SMS_CONFIG] = &SmsInterfaceStub::OnSetImsSmsConfig;
39     memberFuncMap_[SET_DEFAULT_SMS_SLOT_ID] = &SmsInterfaceStub::OnSetDefaultSmsSlotId;
40     memberFuncMap_[GET_DEFAULT_SMS_SLOT_ID] = &SmsInterfaceStub::OnGetDefaultSmsSlotId;
41     memberFuncMap_[SPLIT_MESSAGE] = &SmsInterfaceStub::OnSplitMessage;
42     memberFuncMap_[GET_SMS_SEGMENTS_INFO] = &SmsInterfaceStub::OnGetSmsSegmentsInfo;
43     memberFuncMap_[GET_IMS_SHORT_MESSAGE_FORMAT] = &SmsInterfaceStub::OnGetImsShortMessageFormat;
44     memberFuncMap_[IS_IMS_SMS_SUPPORTED] = &SmsInterfaceStub::OnIsImsSmsSupported;
45     memberFuncMap_[HAS_SMS_CAPABILITY] = &SmsInterfaceStub::OnHasSmsCapability;
46     memberFuncMap_[CREATE_MESSAGE] = &SmsInterfaceStub::OnCreateMessage;
47     memberFuncMap_[MMS_BASE64_ENCODE] = &SmsInterfaceStub::OnGetBase64Encode;
48     memberFuncMap_[MMS_BASE64_DECODE] = &SmsInterfaceStub::OnGetBase64Decode;
49     memberFuncMap_[GET_ENCODE_STRING] = &SmsInterfaceStub::OnGetEncodeStringFunc;
50 }
51 
~SmsInterfaceStub()52 SmsInterfaceStub::~SmsInterfaceStub()
53 {
54     slotSmsInterfaceManagerMap_.clear();
55     memberFuncMap_.clear();
56 }
57 
InitModule()58 void SmsInterfaceStub::InitModule()
59 {
60     static bool bInitModule = false;
61     if (!bInitModule) {
62         bInitModule = true;
63         std::lock_guard<std::mutex> lock(mutex_);
64         for (int32_t slotId = 0; slotId < SIM_SLOT_COUNT; ++slotId) {
65             slotSmsInterfaceManagerMap_[slotId] = std::make_shared<SmsInterfaceManager>(slotId);
66             if (slotSmsInterfaceManagerMap_[slotId] == nullptr) {
67                 TELEPHONY_LOGE(
68                     "SmsInterfaceStub InitModule slotSmsInterfaceManagerMap_[%{public}d] is nullptr", slotId);
69                 return;
70             }
71             slotSmsInterfaceManagerMap_[slotId]->InitInterfaceManager();
72             TELEPHONY_LOGI("SmsInterfaceStub InitModule slotId = %{public}d", slotId);
73         }
74     }
75 }
76 
GetSmsInterfaceManager(int32_t slotId)77 std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager(int32_t slotId)
78 {
79     std::lock_guard<std::mutex> lock(mutex_);
80     std::map<uint32_t, std::shared_ptr<SmsInterfaceManager>>::iterator iter =
81         slotSmsInterfaceManagerMap_.find(slotId);
82     if (iter != slotSmsInterfaceManagerMap_.end()) {
83         return iter->second;
84     }
85     return nullptr;
86 }
87 
GetSmsInterfaceManager()88 std::shared_ptr<SmsInterfaceManager> SmsInterfaceStub::GetSmsInterfaceManager()
89 {
90     std::lock_guard<std::mutex> lock(mutex_);
91     for (const auto &iter : slotSmsInterfaceManagerMap_) {
92         if (iter.second != nullptr) {
93             return iter.second;
94         }
95     }
96     return nullptr;
97 }
98 
OnSendSmsTextRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)99 void SmsInterfaceStub::OnSendSmsTextRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
100 {
101     sptr<ISendShortMessageCallback> sendCallback = nullptr;
102     sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
103     int32_t slotId = data.ReadInt32();
104     u16string desAddr = data.ReadString16();
105     u16string scAddr = data.ReadString16();
106     u16string text = data.ReadString16();
107     sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
108     sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
109     if (remoteSendCallback != nullptr) {
110         sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
111     }
112     if (remoteDeliveryCallback != nullptr) {
113         deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
114     }
115     TELEPHONY_LOGI("MessageID::TEXT_BASED_SMS_DELIVERY %{public}d", slotId);
116     RemoveSpacesInDesAddr(desAddr);
117     int32_t result = SendMessage(slotId, desAddr, scAddr, text, sendCallback, deliveryCallback);
118     reply.WriteInt32(result);
119 }
120 
OnSendSmsDataRequest(MessageParcel & data,MessageParcel & reply,MessageOption & option)121 void SmsInterfaceStub::OnSendSmsDataRequest(MessageParcel &data, MessageParcel &reply, MessageOption &option)
122 {
123     sptr<ISendShortMessageCallback> sendCallback = nullptr;
124     sptr<IDeliveryShortMessageCallback> deliveryCallback = nullptr;
125     int32_t slotId = data.ReadInt32();
126     u16string desAddr = data.ReadString16();
127     u16string scAddr = data.ReadString16();
128     int16_t port = data.ReadInt16();
129     sptr<IRemoteObject> remoteSendCallback = data.ReadRemoteObject();
130     sptr<IRemoteObject> remoteDeliveryCallback = data.ReadRemoteObject();
131     if (remoteSendCallback != nullptr) {
132         sendCallback = iface_cast<ISendShortMessageCallback>(remoteSendCallback);
133     }
134     if (remoteDeliveryCallback != nullptr) {
135         deliveryCallback = iface_cast<IDeliveryShortMessageCallback>(remoteDeliveryCallback);
136     }
137     int16_t dataLen = data.ReadInt16();
138     const uint8_t *buffer = reinterpret_cast<const uint8_t *>(data.ReadRawData(dataLen));
139     RemoveSpacesInDesAddr(desAddr);
140     int32_t result = SendMessage(slotId, desAddr, scAddr, port, buffer, dataLen, sendCallback, deliveryCallback);
141     reply.WriteInt32(result);
142 }
143 
RemoveSpacesInDesAddr(std::u16string & desAddr)144 void SmsInterfaceStub::RemoveSpacesInDesAddr(std::u16string &desAddr)
145 {
146     // Remove spaces in desAddr
147     std::string sourceAddr = StringUtils::ToUtf8(desAddr);
148     std::string resultAddr = sourceAddr;
149     int32_t count = static_cast<int32_t>(sourceAddr.size());
150     int32_t indexDes = 0;
151     int32_t indexResult = 0;
152     while (indexDes < count) {
153         if (desAddr[indexDes] != ' ') {
154             resultAddr[indexResult] = desAddr[indexDes];
155             indexResult++;
156         }
157         indexDes++;
158     }
159     sourceAddr = resultAddr.substr(0, indexResult);
160     desAddr = StringUtils::ToUtf16(sourceAddr);
161 }
162 
OnSetSmscAddr(MessageParcel & data,MessageParcel & reply,MessageOption & option)163 void SmsInterfaceStub::OnSetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
164 {
165     int32_t slotId = data.ReadInt32();
166     std::u16string scAddr = data.ReadString16();
167     int32_t result = SetSmscAddr(slotId, scAddr);
168     TELEPHONY_LOGI("SetSmscAddr result %{public}d", result);
169     reply.WriteInt32(result);
170 }
171 
OnGetSmscAddr(MessageParcel & data,MessageParcel & reply,MessageOption & option)172 void SmsInterfaceStub::OnGetSmscAddr(MessageParcel &data, MessageParcel &reply, MessageOption &option)
173 {
174     std::u16string smscAddress;
175     int32_t slotId = data.ReadInt32();
176     int32_t result = GetSmscAddr(slotId, smscAddress);
177     TELEPHONY_LOGI("GetSmscAddr result size %{public}d", result);
178     if (!reply.WriteInt32(result)) {
179         TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr write reply failed.");
180         return;
181     }
182     if (result != TELEPHONY_ERR_SUCCESS) {
183         TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr result is not TELEPHONY_ERR_SUCCESS.");
184         return;
185     }
186 
187     if (!reply.WriteString16(smscAddress)) {
188         TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr write reply failed.");
189         return;
190     }
191 }
192 
OnAddSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)193 void SmsInterfaceStub::OnAddSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
194 {
195     int32_t slotId = data.ReadInt32();
196     std::u16string smsc = data.ReadString16();
197     std::u16string pdu = data.ReadString16();
198     uint32_t status = data.ReadUint32();
199     int32_t result = AddSimMessage(slotId, smsc, pdu, static_cast<SimMessageStatus>(status));
200     TELEPHONY_LOGI("AddSimMessage result %{public}d", result);
201     reply.WriteInt32(result);
202 }
203 
OnDelSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)204 void SmsInterfaceStub::OnDelSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
205 {
206     int32_t slotId = data.ReadInt32();
207     uint32_t msgIndex = data.ReadUint32();
208     int32_t result = DelSimMessage(slotId, msgIndex);
209     TELEPHONY_LOGI("DelSimMessage result %{public}d", result);
210     reply.WriteInt32(result);
211 }
212 
OnUpdateSimMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)213 void SmsInterfaceStub::OnUpdateSimMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
214 {
215     int32_t slotId = data.ReadInt32();
216     uint32_t msgIndex = data.ReadUint32();
217     uint32_t newStatus = data.ReadUint32();
218     std::u16string pdu = data.ReadString16();
219     std::u16string smsc = data.ReadString16();
220     int32_t result = UpdateSimMessage(slotId, msgIndex, static_cast<SimMessageStatus>(newStatus), pdu, smsc);
221     TELEPHONY_LOGI("UpdateSimMessage result %{public}d", result);
222     reply.WriteInt32(result);
223 }
224 
OnGetAllSimMessages(MessageParcel & data,MessageParcel & reply,MessageOption & option)225 void SmsInterfaceStub::OnGetAllSimMessages(MessageParcel &data, MessageParcel &reply, MessageOption &option)
226 {
227     std::vector<ShortMessage> message;
228     int32_t slotId = data.ReadInt32();
229     int32_t result = GetAllSimMessages(slotId, message);
230     TELEPHONY_LOGI("GetAllSimMessages result %{public}d size %{public}zu", result, message.size());
231     reply.WriteInt32(result);
232     if (result != TELEPHONY_ERR_SUCCESS) {
233         TELEPHONY_LOGE("SmsInterfaceStub::OnGetSmscAddr result is not TELEPHONY_ERR_SUCCESS.");
234         return;
235     }
236     int32_t resultLen = static_cast<int32_t>(message.size());
237     reply.WriteInt32(resultLen);
238     for (const auto &v : message) {
239         v.Marshalling(reply);
240     }
241 }
242 
OnSetCBConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)243 void SmsInterfaceStub::OnSetCBConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
244 {
245     int32_t slotId = data.ReadInt32();
246     bool enable = data.ReadBool();
247     uint32_t fromMsgId = data.ReadUint32();
248     uint32_t toMsgId = data.ReadUint32();
249     uint8_t ranType = data.ReadUint8();
250     int32_t result = SetCBConfig(slotId, enable, fromMsgId, toMsgId, ranType);
251     TELEPHONY_LOGI("OnSetCBConfig result %{public}d", result);
252     reply.WriteInt32(result);
253 }
254 
OnSetImsSmsConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)255 void SmsInterfaceStub::OnSetImsSmsConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
256 {
257     bool result = false;
258     int32_t slotId = data.ReadInt32();
259     int32_t enable = data.ReadInt32();
260     result = SetImsSmsConfig(slotId, enable);
261     TELEPHONY_LOGI("SetImsSmsConfig result %{public}d", result);
262     reply.WriteBool(result);
263 }
264 
OnSetDefaultSmsSlotId(MessageParcel & data,MessageParcel & reply,MessageOption & option)265 void SmsInterfaceStub::OnSetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
266 {
267     int32_t slotId = data.ReadInt32();
268     int32_t result = SetDefaultSmsSlotId(slotId);
269     TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
270     reply.WriteInt32(result);
271 }
272 
OnGetDefaultSmsSlotId(MessageParcel & data,MessageParcel & reply,MessageOption & option)273 void SmsInterfaceStub::OnGetDefaultSmsSlotId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
274 {
275     int32_t result = 0;
276     result = GetDefaultSmsSlotId();
277     TELEPHONY_LOGI("SetDefaultSmsSlotId result %{public}d", result);
278     reply.WriteInt32(result);
279 }
280 
OnSplitMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)281 void SmsInterfaceStub::OnSplitMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
282 {
283     std::vector<std::u16string> splitMessage;
284     std::u16string message = data.ReadString16();
285     int32_t result = SplitMessage(message, splitMessage);
286     reply.WriteInt32(result);
287     if (result != TELEPHONY_ERR_SUCCESS) {
288         TELEPHONY_LOGE("SmsInterfaceStub::OnSplitMessage result is not TELEPHONY_ERR_SUCCESS.");
289         return;
290     }
291     int32_t resultLen = static_cast<int32_t>(splitMessage.size());
292     TELEPHONY_LOGI("SplitMessage size %{public}d", resultLen);
293     reply.WriteInt32(resultLen);
294     for (const auto &item : splitMessage) {
295         reply.WriteString16(item);
296     }
297 }
298 
OnGetSmsSegmentsInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)299 void SmsInterfaceStub::OnGetSmsSegmentsInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
300 {
301     int32_t slotId = data.ReadInt32();
302     std::u16string message = data.ReadString16();
303     bool force7BitCode = data.ReadBool();
304 
305     SmsSegmentsInfo segInfo;
306     int32_t result = GetSmsSegmentsInfo(slotId, message, force7BitCode, segInfo);
307     reply.WriteInt32(result);
308 
309     if (result == TELEPHONY_ERR_SUCCESS) {
310         reply.WriteInt32(segInfo.msgSegCount);
311         reply.WriteInt32(segInfo.msgEncodingCount);
312         reply.WriteInt32(segInfo.msgRemainCount);
313         reply.WriteInt32(static_cast<int32_t>(segInfo.msgCodeScheme));
314     }
315 }
316 
OnIsImsSmsSupported(MessageParcel & data,MessageParcel & reply,MessageOption & option)317 void SmsInterfaceStub::OnIsImsSmsSupported(MessageParcel &data, MessageParcel &reply, MessageOption &option)
318 {
319     int32_t slotId = data.ReadInt32();
320     bool isSupported = false;
321     int32_t result = IsImsSmsSupported(slotId, isSupported);
322     if (!reply.WriteInt32(result)) {
323         TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported write reply failed.");
324         return;
325     }
326     if (result != TELEPHONY_ERR_SUCCESS) {
327         TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported result is not TELEPHONY_ERR_SUCCESS.");
328         return;
329     }
330     if (!reply.WriteBool(isSupported)) {
331         TELEPHONY_LOGE("SmsInterfaceStub::OnIsImsSmsSupported write reply failed.");
332         return;
333     }
334 }
335 
OnGetImsShortMessageFormat(MessageParcel & data,MessageParcel & reply,MessageOption & option)336 void SmsInterfaceStub::OnGetImsShortMessageFormat(MessageParcel &data, MessageParcel &reply, MessageOption &option)
337 {
338     std::u16string format;
339     int32_t result = GetImsShortMessageFormat(format);
340     reply.WriteInt32(result);
341     if (result != TELEPHONY_ERR_SUCCESS) {
342         TELEPHONY_LOGE("SmsInterfaceStub::OnGetImsShortMessageFormat result is not TELEPHONY_ERR_SUCCESS.");
343         return;
344     }
345     reply.WriteString16(format);
346 }
347 
OnHasSmsCapability(MessageParcel & data,MessageParcel & reply,MessageOption & option)348 void SmsInterfaceStub::OnHasSmsCapability(MessageParcel &data, MessageParcel &reply, MessageOption &option)
349 {
350     reply.WriteBool(HasSmsCapability());
351 }
352 
OnCreateMessage(MessageParcel & data,MessageParcel & reply,MessageOption & option)353 void SmsInterfaceStub::OnCreateMessage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
354 {
355     std::string pdu = data.ReadString();
356     std::string specification = data.ReadString();
357     ShortMessage message;
358     int32_t result = CreateMessage(pdu, specification, message);
359 
360     reply.WriteInt32(result);
361     if (result != TELEPHONY_ERR_SUCCESS) {
362         return;
363     }
364     if (!message.Marshalling(reply)) {
365         TELEPHONY_LOGE("SmsInterfaceStub::OnCreateMessage fail");
366     }
367 }
368 
OnGetBase64Encode(MessageParcel & data,MessageParcel & reply,MessageOption & option)369 void SmsInterfaceStub::OnGetBase64Encode(MessageParcel &data, MessageParcel &reply, MessageOption &option)
370 {
371     bool result = false;
372 
373     u16string src = data.ReadString16();
374     std::string dest;
375     result = GetBase64Encode(StringUtils::ToUtf8(src), dest);
376     reply.WriteBool(result);
377     if (!result) {
378         return;
379     }
380     reply.WriteString16(StringUtils::ToUtf16(dest));
381 }
382 
OnGetBase64Decode(MessageParcel & data,MessageParcel & reply,MessageOption & option)383 void SmsInterfaceStub::OnGetBase64Decode(MessageParcel &data, MessageParcel &reply, MessageOption &option)
384 {
385     bool result = false;
386     u16string src = data.ReadString16();
387     std::string dest;
388     result = GetBase64Decode(StringUtils::ToUtf8(src), dest);
389     reply.WriteBool(result);
390     if (!result) {
391         return;
392     }
393     reply.WriteString16(StringUtils::ToUtf16(dest));
394 }
395 
OnGetEncodeStringFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)396 void SmsInterfaceStub::OnGetEncodeStringFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option)
397 {
398     bool result = false;
399     uint32_t charset = data.ReadUint32();
400     uint32_t valLength = data.ReadUint32();
401     u16string strEncodeString = data.ReadString16();
402     std::string str = StringUtils::ToUtf8(strEncodeString);
403     std::string encodeString;
404 
405     if (valLength != str.length()) {
406         TELEPHONY_LOGE("invalid valLength!");
407         return;
408     }
409 
410     result = GetEncodeStringFunc(encodeString, charset, valLength, str);
411     reply.WriteBool(result);
412     if (!result) {
413         return;
414     }
415     reply.WriteString16(StringUtils::ToUtf16(encodeString));
416 }
417 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)418 int SmsInterfaceStub::OnRemoteRequest(
419     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
420 {
421     std::u16string myDescripter = SmsInterfaceStub::GetDescriptor();
422     std::u16string remoteDescripter = data.ReadInterfaceToken();
423     if (myDescripter != remoteDescripter) {
424         TELEPHONY_LOGE("descriptor checked fail");
425         return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
426     }
427 
428     auto itFunc = memberFuncMap_.find(code);
429     if (itFunc != memberFuncMap_.end()) {
430         auto memberFunc = itFunc->second;
431         if (memberFunc != nullptr) {
432             (this->*memberFunc)(data, reply, option);
433             return TELEPHONY_ERR_SUCCESS;
434         }
435     }
436     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
437 }
438 } // namespace Telephony
439 } // namespace OHOS
440