• 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_pdu_hex_value.h"
17 #include "gsm_sms_param_codec.h"
18 #include "gsm_sms_tpdu_decode.h"
19 #include "gsm_sms_tpdu_encode.h"
20 #include "securec.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 using namespace std;
26 static constexpr uint8_t SLIDE_DATA_STEP = 2;
27 static constexpr uint16_t MAX_DECODE_LEN = 520;
28 static constexpr uint16_t MAX_ENCODE_LEN = 255;
29 
GsmSmsTpduCodec()30 GsmSmsTpduCodec::GsmSmsTpduCodec()
31 {
32     uDataCodec_ = std::make_shared<GsmUserDataPdu>();
33     paramCodec_ = std::make_shared<GsmSmsParamCodec>();
34 }
35 
~GsmSmsTpduCodec()36 GsmSmsTpduCodec::~GsmSmsTpduCodec() {}
37 
EncodeSmsPdu(std::shared_ptr<SmsTpdu> sourceData,char * pdu,uint16_t pduLen,uint16_t & bufLen)38 bool GsmSmsTpduCodec::EncodeSmsPdu(std::shared_ptr<SmsTpdu> sourceData, char *pdu, uint16_t pduLen, uint16_t &bufLen)
39 {
40     if (pduLen == 0 || pduLen > MAX_ENCODE_LEN) {
41         TELEPHONY_LOGE("pduLen Error.");
42         return false;
43     }
44     if (sourceData == nullptr) {
45         TELEPHONY_LOGE("nullptr error.");
46         return false;
47     }
48 
49     auto tpduEncode = std::make_shared<GsmSmsTpduEncode>(uDataCodec_, paramCodec_, shared_from_this());
50     if (tpduEncode == nullptr) {
51         TELEPHONY_LOGE("nullptr Error.");
52         return false;
53     }
54 
55     bool encodeRet = 0;
56     SmsWriteBuffer pduBuffer;
57     switch (sourceData->tpduType) {
58         case SMS_TPDU_SUBMIT:
59             encodeRet = tpduEncode->EncodeSubmitPdu(pduBuffer, &(sourceData->data.submit));
60             break;
61         case SMS_TPDU_DELIVER:
62             encodeRet = tpduEncode->EncodeDeliverPdu(pduBuffer, &(sourceData->data.deliver));
63             break;
64         case SMS_TPDU_DELIVER_REP:
65             encodeRet = tpduEncode->EncodeDeliverReportPdu(pduBuffer, &(sourceData->data.deliverRep));
66             break;
67         case SMS_TPDU_STATUS_REP:
68             encodeRet = tpduEncode->EncodeStatusReportPdu(pduBuffer, &(sourceData->data.statusRep));
69             break;
70         default:
71             break;
72     }
73     bufLen = pduBuffer.GetIndex();
74     if (!encodeRet || bufLen > pduLen) {
75         TELEPHONY_LOGE("encode sms fail");
76         return false;
77     }
78     auto bufferRet = pduBuffer.GetPduBuffer();
79     if (bufferRet == nullptr) {
80         TELEPHONY_LOGE("bufferRet nullptr");
81         return false;
82     }
83     std::vector<uint8_t> buf = *(bufferRet);
84     for (uint16_t locate = 0; locate < bufLen; locate++) {
85         pdu[locate] = static_cast<char>(buf[locate]);
86     }
87     TELEPHONY_LOGI("encode sms success");
88     return true;
89 }
90 
DecodeSmsPdu(const uint8_t * pTpdu,uint16_t TpduLen,struct SmsTpdu * pSmsTpdu)91 bool GsmSmsTpduCodec::DecodeSmsPdu(const uint8_t *pTpdu, uint16_t TpduLen, struct SmsTpdu *pSmsTpdu)
92 {
93     if (pTpdu == nullptr || pSmsTpdu == nullptr || TpduLen == 0 || TpduLen > MAX_DECODE_LEN) {
94         TELEPHONY_LOGE("nullptr error. or TpduLen Error.");
95         return false;
96     }
97 
98     string pduData(TpduLen, '\0');
99     for (int locate = 0; locate < TpduLen; locate++) {
100         pduData[locate] = static_cast<char>(pTpdu[locate]);
101     }
102     TELEPHONY_LOGI("TpduLen:%{public}d", TpduLen);
103 
104     SmsReadBuffer buffer(pduData);
105     std::shared_ptr<GsmSmsTpduDecode> tpduDecode =
106         std::make_shared<GsmSmsTpduDecode>(uDataCodec_, paramCodec_, shared_from_this());
107     if (tpduDecode == nullptr) {
108         TELEPHONY_LOGE("nullptr error.");
109         return false;
110     }
111 
112     const char mti = pTpdu[0] & HEX_VALUE_03;
113     bool decodeResult = false;
114     switch (mti) {
115         case TYPE_INDICATOR_DELIVER:
116             pSmsTpdu->tpduType = SMS_TPDU_DELIVER;
117             decodeResult = tpduDecode->DecodeDeliver(buffer, &(pSmsTpdu->data.deliver));
118             break;
119         case TYPE_INDICATOR_SUBMIT:
120             pSmsTpdu->tpduType = SMS_TPDU_SUBMIT;
121             decodeResult = tpduDecode->DecodeSubmit(buffer, &(pSmsTpdu->data.submit));
122             break;
123         case TYPE_INDICATOR_STATUS_REP:
124             pSmsTpdu->tpduType = SMS_TPDU_STATUS_REP;
125             decodeResult = tpduDecode->DecodeStatusReport(buffer, &(pSmsTpdu->data.statusRep));
126             break;
127         default:
128             break;
129     }
130     TELEPHONY_LOGI("buffer index:%{public}d", buffer.GetIndex());
131     if (decodeResult && buffer.GetIndex() <= (TpduLen + 1)) {
132         TELEPHONY_LOGI("decode sms success");
133         return true;
134     }
135     TELEPHONY_LOGE("decode sms fail");
136     return false;
137 }
138 
ParsePid(const uint8_t pid)139 enum SmsPid GsmSmsTpduCodec::ParsePid(const uint8_t pid)
140 {
141     return (enum SmsPid)pid;
142 }
143 
DebugTpdu(SmsReadBuffer & buffer,const enum SmsParseType type)144 void GsmSmsTpduCodec::DebugTpdu(SmsReadBuffer &buffer, const enum SmsParseType type)
145 {
146     char tpduTmp[(MAX_ENCODE_LEN * SLIDE_DATA_STEP) + 1];
147     if (memset_s(tpduTmp, sizeof(tpduTmp), 0x00, sizeof(tpduTmp)) != EOK) {
148         TELEPHONY_LOGE("memset_s error.");
149         return;
150     }
151     uint8_t oneByte = 0;
152     uint8_t step = SLIDE_DATA_STEP;
153     uint16_t tpduLen = buffer.GetSize();
154     for (uint16_t i = 0; i < tpduLen; i++) {
155         if (sizeof(tpduTmp) <= (i * step)) {
156             TELEPHONY_LOGE("data error.");
157             return;
158         }
159         const uint16_t len = sizeof(tpduTmp) - (i * step);
160         if (!buffer.PickOneByteFromIndex(i, oneByte)) {
161             TELEPHONY_LOGE("buffer error.");
162             return;
163         }
164         if (snprintf_s(tpduTmp + (i * step), len - 1, len - 1, "%02X", oneByte) < 0) {
165             TELEPHONY_LOGE("DebugTpdu snprintf_s error");
166             return;
167         }
168     }
169 }
170 } // namespace Telephony
171 } // namespace OHOS