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