1 /*
2 * Copyright (C) 2023 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 "gsm_cb_gsm_codec.h"
17
18 #include "cdma_sms_common.h"
19 #include "gsm_pdu_hex_value.h"
20 #include "securec.h"
21 #include "sms_common_utils.h"
22 #include "string_utils.h"
23 #include "telephony_log_wrapper.h"
24 #include "text_coder.h"
25
26 namespace OHOS {
27 namespace Telephony {
28 static constexpr uint8_t SMS_BYTE_BIT = 8;
29 static constexpr uint8_t GSM_CODE_BIT = 7;
30 static constexpr uint8_t MAX_PAGE_PDU_LEN = 82;
31 static constexpr uint8_t MAX_ETWS_PDU_LEN = 56;
32 static constexpr uint8_t MAX_PAGE_NUM = 15;
33 static constexpr uint16_t GSM_ETWS_BASE_MASK = 0x1100;
34 static constexpr uint8_t GSM_CB_HEADER_LEN = 6;
35
GsmCbGsmCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,std::shared_ptr<GsmCbPduDecodeBuffer> buffer,std::shared_ptr<GsmCbCodec> cbCodec)36 GsmCbGsmCodec::GsmCbGsmCodec(std::shared_ptr<GsmCbCodec::GsmCbMessageHeader> header,
37 std::shared_ptr<GsmCbPduDecodeBuffer> buffer, std::shared_ptr<GsmCbCodec> cbCodec)
38 {
39 cbHeader_ = header;
40 cbPduBuffer_ = buffer;
41 cbCodec_ = cbCodec;
42 }
43
~GsmCbGsmCodec()44 GsmCbGsmCodec::~GsmCbGsmCodec() {}
45
46 /**
47 * refer to 3GPP TS 23.041 V4.1.0 9.4 Message Format on the Radio Network – MS/UE Interface
48 * refer to 3GPP TS 23.041 V4.1.0 9.4.1.2 Message Parameter
49 */
Decode2gHeader()50 bool GsmCbGsmCodec::Decode2gHeader()
51 {
52 if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbPduBuffer_->GetSize() == 0) {
53 TELEPHONY_LOGE("CB pdu data error.");
54 return false;
55 }
56
57 cbHeader_->bEtwsMessage = false;
58 uint8_t oneByte = 0;
59 if (!cbPduBuffer_->GetOneByte(oneByte)) {
60 TELEPHONY_LOGE("get data error.");
61 return false;
62 }
63 uint8_t temp = oneByte;
64 cbHeader_->serialNum.geoScope = (temp & HEX_VALUE_C0) >> HEX_VALUE_06;
65 cbHeader_->serialNum.msgCode = (temp & HEX_VALUE_3F) << HEX_VALUE_04;
66
67 if (!cbPduBuffer_->GetOneByte(oneByte)) {
68 TELEPHONY_LOGE("get data error.");
69 return false;
70 }
71 temp = oneByte;
72 cbHeader_->serialNum.msgCode |= (temp & HEX_VALUE_F0) >> HEX_VALUE_04;
73 cbHeader_->serialNum.updateNum = temp & HEX_VALUE_0F;
74
75 if (!cbPduBuffer_->GetOneByte(oneByte)) {
76 TELEPHONY_LOGE("get data error.");
77 return false;
78 }
79 temp = oneByte;
80
81 if (!cbPduBuffer_->GetOneByte(oneByte)) {
82 TELEPHONY_LOGE("get data error.");
83 return false;
84 }
85 cbHeader_->msgId = (temp << HEX_VALUE_08) | oneByte;
86 if ((cbHeader_->msgId & HEX_VALUE_FFF8) == GSM_ETWS_BASE_MASK && cbPduBuffer_->GetSize() <= MAX_ETWS_PDU_LEN) {
87 if (!Decode2gHeaderEtws()) {
88 TELEPHONY_LOGE("etws head error.");
89 return false;
90 }
91 } else {
92 if (!Decode2gHeaderCommonCb()) {
93 TELEPHONY_LOGE("common cb head error.");
94 return false;
95 }
96 }
97 return true;
98 }
99
Decode2gHeaderEtws()100 bool GsmCbGsmCodec::Decode2gHeaderEtws()
101 {
102 uint8_t oneByte = 0;
103 cbHeader_->cbEtwsType = GsmCbCodec::ETWS_PRIMARY;
104 cbHeader_->bEtwsMessage = true;
105 if (!cbPduBuffer_->GetOneByte(oneByte)) {
106 TELEPHONY_LOGE("get data error.");
107 return false;
108 }
109 cbHeader_->warningType = (oneByte & HEX_VALUE_FE) >> 1;
110 if (cbPduBuffer_->GetSize() > GSM_CB_HEADER_LEN && cbHeader_->msgId >= GSM_ETWS_BASE_MASK) {
111 cbHeader_->warningType = cbHeader_->msgId - GSM_ETWS_BASE_MASK;
112 }
113 if (!cbPduBuffer_->GetOneByte(oneByte)) {
114 TELEPHONY_LOGE("get data error.");
115 return false;
116 }
117 cbHeader_->totalPages = 1;
118 bool activatePopup = (oneByte & HEX_VALUE_80) != 0;
119 TELEPHONY_LOGI("activatePopup:%{public}d.", activatePopup);
120 return true;
121 }
122
Decode2gHeaderCommonCb()123 bool GsmCbGsmCodec::Decode2gHeaderCommonCb()
124 {
125 if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbCodec_ == nullptr || cbPduBuffer_->GetSize() == 0) {
126 TELEPHONY_LOGE("CB pdu data error.");
127 return false;
128 }
129 uint8_t oneByte = 0;
130 if (!cbPduBuffer_->GetOneByte(oneByte)) {
131 TELEPHONY_LOGE("get data error.");
132 return false;
133 }
134 uint8_t dcs = oneByte;
135
136 if (!cbPduBuffer_->GetOneByte(oneByte)) {
137 TELEPHONY_LOGE("get data error.");
138 return false;
139 }
140 uint8_t temp = oneByte;
141 cbHeader_->totalPages = temp & HEX_VALUE_0F;
142 cbHeader_->page = (temp & HEX_VALUE_F0) >> HEX_VALUE_04;
143
144 if (!cbPduBuffer_->GetOneByte(oneByte)) {
145 TELEPHONY_LOGE("get data error.");
146 return false;
147 }
148 unsigned short iosTemp = oneByte;
149
150 if (!cbPduBuffer_->GetOneByte(oneByte)) {
151 TELEPHONY_LOGE("get data error.");
152 return false;
153 }
154 iosTemp |= (oneByte << SMS_BYTE_BIT);
155 cbCodec_->DecodeCbMsgDCS(dcs, iosTemp, cbHeader_->dcs);
156 cbHeader_->langType = cbHeader_->dcs.langType;
157 cbHeader_->recvTime = static_cast<time_t>(cbCodec_->GetRecvTime());
158 if (cbHeader_->totalPages > MAX_PAGE_NUM) {
159 TELEPHONY_LOGE("CB Page Count is over MAX[%{public}d]", cbHeader_->totalPages);
160 cbPduBuffer_->SetPointer(0);
161 }
162 if ((cbHeader_->msgId & HEX_VALUE_FFF8) == GSM_ETWS_BASE_MASK) {
163 cbHeader_->bEtwsMessage = true;
164 cbHeader_->cbEtwsType = GsmCbCodec::ETWS_GSM;
165 if (cbHeader_->msgId >= GSM_ETWS_BASE_MASK) {
166 cbHeader_->warningType = cbHeader_->msgId - GSM_ETWS_BASE_MASK;
167 }
168 }
169 if (cbPduBuffer_->GetCurPosition() >= HEX_VALUE_02) {
170 cbPduBuffer_->SetPointer(cbPduBuffer_->GetCurPosition() - HEX_VALUE_02);
171 }
172 return true;
173 }
174
175 /**
176 * refer to 3GPP TS 23.041 V4.1.0 9.4.2.2 Message Parameter
177 */
Decode2gCbMsg()178 bool GsmCbGsmCodec::Decode2gCbMsg()
179 {
180 if (cbPduBuffer_ == nullptr || cbHeader_ == nullptr || cbCodec_ == nullptr ||
181 cbPduBuffer_->GetCurPosition() >= cbPduBuffer_->GetSize()) {
182 TELEPHONY_LOGE("CB pdu data error.");
183 return false;
184 }
185
186 uint16_t dataLen = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
187 switch (cbHeader_->dcs.codingScheme) {
188 case DATA_CODING_7BIT: {
189 if (!Decode2gCbMsg7bit(dataLen)) {
190 TELEPHONY_LOGE("decode cb 7bit error.");
191 return false;
192 }
193 break;
194 }
195 case DATA_CODING_8BIT:
196 case DATA_CODING_UCS2: {
197 if (dataLen < HEX_VALUE_02) {
198 TELEPHONY_LOGE("dataLen error.");
199 return false;
200 }
201 if (cbHeader_->dcs.iso639Lang[0]) {
202 TELEPHONY_LOGI("dcs.iso639Lang");
203 cbPduBuffer_->SetPointer(cbPduBuffer_->GetCurPosition() + HEX_VALUE_02);
204 dataLen -= HEX_VALUE_02;
205 }
206
207 if (dataLen == 0 || cbPduBuffer_->pduBuffer_ == nullptr ||
208 (cbPduBuffer_->GetCurPosition() + dataLen) > cbPduBuffer_->GetSize()) {
209 TELEPHONY_LOGE("CB pdu data error.");
210 return false;
211 }
212 for (uint8_t i = cbPduBuffer_->GetCurPosition(); i < cbPduBuffer_->GetSize(); i++) {
213 messageRaw_.push_back(cbPduBuffer_->pduBuffer_[i]);
214 }
215 cbCodec_->SetCbMessageRaw(messageRaw_);
216 break;
217 }
218 default:
219 break;
220 }
221 return true;
222 }
223
Decode2gCbMsg7bit(uint16_t dataLen)224 bool GsmCbGsmCodec::Decode2gCbMsg7bit(uint16_t dataLen)
225 {
226 uint8_t pageData[MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT + 1] = { 0 };
227 std::vector<uint8_t> dataPdu;
228 cbCodec_->GetPduData(dataPdu);
229
230 if (dataPdu.size() == 0) {
231 TELEPHONY_LOGE("dataPdu empty.");
232 return false;
233 }
234 uint16_t unpackLen = SmsCommonUtils::Unpack7bitCharForCBPdu(
235 dataPdu.data(), dataLen, 0x00, pageData, MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT + 1);
236
237 uint16_t offset = 0;
238 if (cbHeader_->dcs.iso639Lang[0] && unpackLen >= GsmCbCodec::CB_IOS639_LANG_SIZE) {
239 unpackLen = unpackLen - GsmCbCodec::CB_IOS639_LANG_SIZE;
240 offset = GsmCbCodec::CB_IOS639_LANG_SIZE;
241 }
242 if (offset + unpackLen >= (MAX_PAGE_PDU_LEN * SMS_BYTE_BIT / GSM_CODE_BIT) + 1) {
243 TELEPHONY_LOGE("CB pdu data error.");
244 return false;
245 }
246 for (uint8_t i = 0; i < unpackLen; i++) {
247 messageRaw_.push_back(static_cast<char>(pageData[i]));
248 }
249 cbCodec_->SetCbMessageRaw(messageRaw_);
250 return true;
251 }
252
DecodeEtwsMsg()253 bool GsmCbGsmCodec::DecodeEtwsMsg()
254 {
255 if (cbPduBuffer_ == nullptr || cbPduBuffer_->pduBuffer_ == nullptr || cbPduBuffer_->GetSize() == 0 ||
256 cbPduBuffer_->GetSize() > MAX_ETWS_PDU_LEN) {
257 TELEPHONY_LOGE("CB pdu data error.");
258 return false;
259 }
260 uint8_t total = cbPduBuffer_->GetSize() - cbPduBuffer_->GetCurPosition();
261 return Decode2gCbMsg7bit(total);
262 }
263 } // namespace Telephony
264 } // namespace OHOS