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 "short_message.h"
17 
18 #include "sms_service_manager_client.h"
19 #include "string_utils.h"
20 #include "telephony_errors.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
ReadFromParcel(Parcel & parcel)25 bool ShortMessage::ReadFromParcel(Parcel &parcel)
26 {
27     if (!parcel.ReadString16(visibleMessageBody_)) {
28         return false;
29     }
30     if (!parcel.ReadString16(visibleRawAddress_)) {
31         return false;
32     }
33 
34     int8_t msgClass = 0;
35     if (!parcel.ReadInt8(msgClass)) {
36         return false;
37     }
38     messageClass_ = static_cast<ShortMessage::SmsMessageClass>(msgClass);
39 
40     int8_t simMsgStatus = 0;
41     if (!parcel.ReadInt8(simMsgStatus)) {
42         return false;
43     }
44     simMessageStatus_ = static_cast<ShortMessage::SmsSimMessageStatus>(simMsgStatus);
45 
46     if (!parcel.ReadString16(scAddress_)) {
47         return false;
48     }
49     if (!parcel.ReadInt64(scTimestamp_)) {
50         return false;
51     }
52     if (!parcel.ReadBool(isReplaceMessage_)) {
53         return false;
54     }
55     if (!parcel.ReadInt32(status_)) {
56         return false;
57     }
58     if (!parcel.ReadBool(isSmsStatusReportMessage_)) {
59         return false;
60     }
61     if (!parcel.ReadBool(hasReplyPath_)) {
62         return false;
63     }
64     if (!parcel.ReadInt32(protocolId_)) {
65         return false;
66     }
67     if (!parcel.ReadUInt8Vector(&pdu_)) {
68         return false;
69     }
70     if (!parcel.ReadString(rawUserData_)) {
71         return false;
72     }
73     if (!parcel.ReadInt32(indexOnSim_)) {
74         return false;
75     }
76     return true;
77 }
78 
Marshalling(Parcel & parcel) const79 bool ShortMessage::Marshalling(Parcel &parcel) const
80 {
81     if (!parcel.WriteString16(visibleMessageBody_)) {
82         return false;
83     }
84     if (!parcel.WriteString16(visibleRawAddress_)) {
85         return false;
86     }
87     if (!parcel.WriteInt8(static_cast<int8_t>(messageClass_))) {
88         return false;
89     }
90     if (!parcel.WriteInt8(static_cast<int8_t>(simMessageStatus_))) {
91         return false;
92     }
93     if (!parcel.WriteString16(scAddress_)) {
94         return false;
95     }
96     if (!parcel.WriteInt64(scTimestamp_)) {
97         return false;
98     }
99     if (!parcel.WriteBool(isReplaceMessage_)) {
100         return false;
101     }
102     if (!parcel.WriteInt32(status_)) {
103         return false;
104     }
105     if (!parcel.WriteBool(isSmsStatusReportMessage_)) {
106         return false;
107     }
108     if (!parcel.WriteBool(hasReplyPath_)) {
109         return false;
110     }
111     if (!parcel.WriteInt32(protocolId_)) {
112         return false;
113     }
114     if (!parcel.WriteUInt8Vector(pdu_)) {
115         return false;
116     }
117     if (!parcel.WriteString(rawUserData_)) {
118         return false;
119     }
120     if (!parcel.WriteInt32(indexOnSim_)) {
121         return false;
122     }
123     return true;
124 }
125 
UnMarshalling(Parcel & parcel)126 ShortMessage ShortMessage::UnMarshalling(Parcel &parcel)
127 {
128     ShortMessage param;
129     param.ReadFromParcel(parcel);
130     return param;
131 }
132 
GetVisibleMessageBody() const133 std::u16string ShortMessage::GetVisibleMessageBody() const
134 {
135     return visibleMessageBody_;
136 }
137 
GetVisibleRawAddress() const138 std::u16string ShortMessage::GetVisibleRawAddress() const
139 {
140     return visibleRawAddress_;
141 }
142 
GetMessageClass() const143 ShortMessage::SmsMessageClass ShortMessage::GetMessageClass() const
144 {
145     return messageClass_;
146 }
147 
GetScAddress(std::u16string & smscAddress) const148 int32_t ShortMessage::GetScAddress(std::u16string &smscAddress) const
149 {
150     smscAddress = scAddress_;
151     return TELEPHONY_ERR_SUCCESS;
152 }
153 
GetScTimestamp() const154 int64_t ShortMessage::GetScTimestamp() const
155 {
156     return scTimestamp_;
157 }
158 
IsReplaceMessage() const159 bool ShortMessage::IsReplaceMessage() const
160 {
161     return isReplaceMessage_;
162 }
163 
GetStatus() const164 int32_t ShortMessage::GetStatus() const
165 {
166     return status_;
167 }
168 
IsSmsStatusReportMessage() const169 bool ShortMessage::IsSmsStatusReportMessage() const
170 {
171     return isSmsStatusReportMessage_;
172 }
173 
HasReplyPath() const174 bool ShortMessage::HasReplyPath() const
175 {
176     return hasReplyPath_;
177 }
178 
GetIccMessageStatus() const179 ShortMessage::SmsSimMessageStatus ShortMessage::GetIccMessageStatus() const
180 {
181     return simMessageStatus_;
182 }
183 
GetProtocolId() const184 int32_t ShortMessage::GetProtocolId() const
185 {
186     return protocolId_;
187 }
188 
GetPdu() const189 std::vector<unsigned char> ShortMessage::GetPdu() const
190 {
191     return pdu_;
192 }
193 
GetRawUserData() const194 std::string ShortMessage::GetRawUserData() const
195 {
196     return rawUserData_;
197 }
198 
CreateMessage(std::vector<unsigned char> & pdu,std::u16string specification,ShortMessage & messageObj)199 int32_t ShortMessage::CreateMessage(
200     std::vector<unsigned char> &pdu, std::u16string specification, ShortMessage &messageObj)
201 {
202     if (pdu.size() <= 0) {
203         TELEPHONY_LOGE("CreateMessage fail pdu invalid");
204         return TELEPHONY_ERR_ARGUMENT_INVALID;
205     }
206     std::string indicates = StringUtils::ToUtf8(specification);
207     auto client = DelayedSingleton<SmsServiceManagerClient>::GetInstance();
208     int32_t errorCode = client->CreateMessage(StringUtils::StringToHex(pdu), indicates, messageObj);
209     if (errorCode != TELEPHONY_ERR_SUCCESS) {
210         TELEPHONY_LOGE("CreateMessage fail errorCode:%{public}d", errorCode);
211         return errorCode;
212     }
213     return errorCode;
214 }
215 
CreateIccMessage(std::vector<unsigned char> & pdu,std::string specification,int32_t index)216 ShortMessage ShortMessage::CreateIccMessage(std::vector<unsigned char> &pdu, std::string specification, int32_t index)
217 {
218     ShortMessage message;
219     if (pdu.size() <= MIN_ICC_PDU_LEN) {
220         return message;
221     }
222 
223     unsigned char simStatus = pdu.at(0);
224     TELEPHONY_LOGI("simStatus =%{public}d", simStatus);
225     if (simStatus == SMS_SIM_MESSAGE_STATUS_READ || simStatus == SMS_SIM_MESSAGE_STATUS_UNREAD ||
226         simStatus == SMS_SIM_MESSAGE_STATUS_SENT || simStatus == SMS_SIM_MESSAGE_STATUS_UNSENT) {
227         message.simMessageStatus_ = static_cast<SmsSimMessageStatus>(simStatus);
228         std::vector<unsigned char> pduTemp(pdu.begin() + MIN_ICC_PDU_LEN, pdu.end());
229 
230         auto client = DelayedSingleton<SmsServiceManagerClient>::GetInstance();
231         int32_t errorCode = client->CreateMessage(StringUtils::StringToHex(pduTemp), specification, message);
232         if (errorCode != TELEPHONY_ERR_SUCCESS) {
233             TELEPHONY_LOGE("CreateMessage fail errorCode:%{public}d", errorCode);
234             return message;
235         }
236         message.indexOnSim_ = index;
237     } else {
238         message.simMessageStatus_ = SMS_SIM_MESSAGE_STATUS_FREE;
239     }
240     return message;
241 }
242 
GetIndexOnSim() const243 int32_t ShortMessage::GetIndexOnSim() const
244 {
245     return indexOnSim_;
246 }
247 } // namespace Telephony
248 } // namespace OHOS