• 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 "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.ReadInt32(indexOnSim_)) {
71         return false;
72     }
73     return true;
74 }
75 
Marshalling(Parcel & parcel) const76 bool ShortMessage::Marshalling(Parcel &parcel) const
77 {
78     if (!parcel.WriteString16(visibleMessageBody_)) {
79         return false;
80     }
81     if (!parcel.WriteString16(visibleRawAddress_)) {
82         return false;
83     }
84     if (!parcel.WriteInt8(static_cast<int8_t>(messageClass_))) {
85         return false;
86     }
87     if (!parcel.WriteInt8(static_cast<int8_t>(simMessageStatus_))) {
88         return false;
89     }
90     if (!parcel.WriteString16(scAddress_)) {
91         return false;
92     }
93     if (!parcel.WriteInt64(scTimestamp_)) {
94         return false;
95     }
96     if (!parcel.WriteBool(isReplaceMessage_)) {
97         return false;
98     }
99     if (!parcel.WriteInt32(status_)) {
100         return false;
101     }
102     if (!parcel.WriteBool(isSmsStatusReportMessage_)) {
103         return false;
104     }
105     if (!parcel.WriteBool(hasReplyPath_)) {
106         return false;
107     }
108     if (!parcel.WriteInt32(protocolId_)) {
109         return false;
110     }
111     if (!parcel.WriteUInt8Vector(pdu_)) {
112         return false;
113     }
114     if (!parcel.WriteInt32(indexOnSim_)) {
115         return false;
116     }
117     return true;
118 }
119 
UnMarshalling(Parcel & parcel)120 ShortMessage ShortMessage::UnMarshalling(Parcel &parcel)
121 {
122     ShortMessage param;
123     param.ReadFromParcel(parcel);
124     return param;
125 }
126 
GetVisibleMessageBody() const127 std::u16string ShortMessage::GetVisibleMessageBody() const
128 {
129     return visibleMessageBody_;
130 }
131 
GetVisibleRawAddress() const132 std::u16string ShortMessage::GetVisibleRawAddress() const
133 {
134     return visibleRawAddress_;
135 }
136 
GetMessageClass() const137 ShortMessage::SmsMessageClass ShortMessage::GetMessageClass() const
138 {
139     return messageClass_;
140 }
141 
GetScAddress(std::u16string & smscAddress) const142 int32_t ShortMessage::GetScAddress(std::u16string &smscAddress) const
143 {
144     smscAddress = scAddress_;
145     return TELEPHONY_ERR_SUCCESS;
146 }
147 
GetScTimestamp() const148 int64_t ShortMessage::GetScTimestamp() const
149 {
150     return scTimestamp_;
151 }
152 
IsReplaceMessage() const153 bool ShortMessage::IsReplaceMessage() const
154 {
155     return isReplaceMessage_;
156 }
157 
GetStatus() const158 int32_t ShortMessage::GetStatus() const
159 {
160     return status_;
161 }
162 
IsSmsStatusReportMessage() const163 bool ShortMessage::IsSmsStatusReportMessage() const
164 {
165     return isSmsStatusReportMessage_;
166 }
167 
HasReplyPath() const168 bool ShortMessage::HasReplyPath() const
169 {
170     return hasReplyPath_;
171 }
172 
GetIccMessageStatus() const173 ShortMessage::SmsSimMessageStatus ShortMessage::GetIccMessageStatus() const
174 {
175     return simMessageStatus_;
176 }
177 
GetProtocolId() const178 int32_t ShortMessage::GetProtocolId() const
179 {
180     return protocolId_;
181 }
182 
GetPdu() const183 std::vector<unsigned char> ShortMessage::GetPdu() const
184 {
185     return pdu_;
186 }
187 
CreateMessage(std::vector<unsigned char> & pdu,std::u16string specification,ShortMessage & messageObj)188 int32_t ShortMessage::CreateMessage(
189     std::vector<unsigned char> &pdu, std::u16string specification, ShortMessage &messageObj)
190 {
191     if (pdu.size() <= 0) {
192         TELEPHONY_LOGE("CreateMessage fail pdu invalid");
193         return TELEPHONY_ERR_ARGUMENT_INVALID;
194     }
195     std::string indicates = StringUtils::ToUtf8(specification);
196     auto client = DelayedSingleton<SmsServiceManagerClient>::GetInstance();
197     int32_t errorCode = client->CreateMessage(StringUtils::StringToHex(pdu), indicates, messageObj);
198     if (errorCode != TELEPHONY_ERR_SUCCESS) {
199         TELEPHONY_LOGE("CreateMessage fail errorCode:%{public}d", errorCode);
200         return errorCode;
201     }
202     return errorCode;
203 }
204 
CreateIccMessage(std::vector<unsigned char> & pdu,std::string specification,int32_t index)205 ShortMessage ShortMessage::CreateIccMessage(std::vector<unsigned char> &pdu, std::string specification, int32_t index)
206 {
207     ShortMessage message;
208     if (pdu.size() <= MIN_ICC_PDU_LEN) {
209         return message;
210     }
211 
212     unsigned char simStatus = pdu.at(0);
213     TELEPHONY_LOGI("simStatus =%{public}d", simStatus);
214     if (simStatus == SMS_SIM_MESSAGE_STATUS_READ || simStatus == SMS_SIM_MESSAGE_STATUS_UNREAD ||
215         simStatus == SMS_SIM_MESSAGE_STATUS_SENT || simStatus == SMS_SIM_MESSAGE_STATUS_UNSENT) {
216         message.simMessageStatus_ = static_cast<SmsSimMessageStatus>(simStatus);
217         std::vector<unsigned char> pduTemp(pdu.begin() + MIN_ICC_PDU_LEN, pdu.end());
218 
219         auto client = DelayedSingleton<SmsServiceManagerClient>::GetInstance();
220         int32_t errorCode = client->CreateMessage(StringUtils::StringToHex(pduTemp), specification, message);
221         if (errorCode != TELEPHONY_ERR_SUCCESS) {
222             TELEPHONY_LOGE("CreateMessage fail errorCode:%{public}d", errorCode);
223             return message;
224         }
225         message.indexOnSim_ = index;
226     } else {
227         message.simMessageStatus_ = SMS_SIM_MESSAGE_STATUS_FREE;
228     }
229     return message;
230 }
231 
GetIndexOnSim() const232 int32_t ShortMessage::GetIndexOnSim() const
233 {
234     return indexOnSim_;
235 }
236 } // namespace Telephony
237 } // namespace OHOS