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 "mms_header.h"
17
18 #include <chrono>
19 #include <cinttypes>
20 #include <iostream>
21 #include <utility>
22 #include <random>
23
24 #include "telephony_log_wrapper.h"
25 #include "mms_charset.h"
26
27 namespace OHOS {
28 namespace Telephony {
29 using namespace std;
MmsHeader()30 MmsHeader::MmsHeader()
31 {
32 InitOctetHandleFun();
33 InitLongHandleFun();
34 InitStringHandleFun();
35 InitTextStringHandleFun();
36 }
37
InitOctetHandleFun()38 void MmsHeader::InitOctetHandleFun()
39 {
40 memberFuncMap_[MMS_DELIVERY_REPORT] = &MmsHeader::DecodeFieldOctetValue; // MmsBoolType
41 memberFuncMap_[MMS_READ_REPORT] = &MmsHeader::DecodeFieldOctetValue;
42 memberFuncMap_[MMS_REPORT_ALLOWED] = &MmsHeader::DecodeFieldOctetValue;
43 memberFuncMap_[MMS_STORE] = &MmsHeader::DecodeFieldOctetValue;
44 memberFuncMap_[MMS_STORED] = &MmsHeader::DecodeFieldOctetValue;
45 memberFuncMap_[MMS_TOTALS] = &MmsHeader::DecodeFieldOctetValue;
46 memberFuncMap_[MMS_QUOTAS] = &MmsHeader::DecodeFieldOctetValue;
47 memberFuncMap_[MMS_DISTRIBUTION_INDICATOR] = &MmsHeader::DecodeFieldOctetValue;
48 memberFuncMap_[MMS_ADAPTATION_ALLOWED] = &MmsHeader::DecodeFieldOctetValue;
49 memberFuncMap_[MMS_MESSAGE_CLASS] = &MmsHeader::DecodeFieldOctetValue;
50 memberFuncMap_[MMS_PRIORITY] = &MmsHeader::DecodeFieldOctetValue; // MmsPriority
51 memberFuncMap_[MMS_RESPONSE_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsResponseStatus
52 memberFuncMap_[MMS_SENDER_VISIBILITY] = &MmsHeader::DecodeFieldOctetValue; // MmsSenderVisibilityType
53 memberFuncMap_[MMS_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsStatus
54 memberFuncMap_[MMS_RETRIEVE_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsRetrieveStatus
55 memberFuncMap_[MMS_READ_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsReadStatus
56 memberFuncMap_[MMS_REPLY_CHARGING] = &MmsHeader::DecodeFieldOctetValue; // MmsReplyCharging
57 memberFuncMap_[MMS_MM_STATE] = &MmsHeader::DecodeFieldOctetValue; // MmsMmState
58 memberFuncMap_[MMS_MM_FLAGS] = &MmsHeader::DecodeFieldMMFlag; // MmsMmFlags
59 memberFuncMap_[MMS_STORE_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsStoreStatus
60 memberFuncMap_[MMS_MBOX_TOTALS] = &MmsHeader::DecodeFieldMBox; // MmsMboxTotals
61 memberFuncMap_[MMS_MBOX_QUOTAS] = &MmsHeader::DecodeFieldMBox; // MmsMboxQuotas
62 memberFuncMap_[MMS_RECOMMENDED_RETRIEVAL_MODE] =
63 &MmsHeader::DecodeFieldOctetValue; // MmsRecommendedRetrievalMode
64 memberFuncMap_[MMS_CONTENT_CLASS] = &MmsHeader::DecodeFieldOctetValue; // MmsContentClass
65 memberFuncMap_[MMS_CANCEL_STATUS] = &MmsHeader::DecodeFieldOctetValue; // MmsCancelStatus
66 memberFuncMap_[MMS_MESSAGE_TYPE] = &MmsHeader::DecodeMmsMsgType;
67 memberFuncMap_[MMS_ATTRIBUTES] = &MmsHeader::DecodeFieldOctetValue;
68 memberFuncMap_[MMS_DRM_CONTENT] = &MmsHeader::DecodeFieldOctetValue;
69 }
70
InitLongHandleFun()71 void MmsHeader::InitLongHandleFun()
72 {
73 memberFuncMap_[MMS_CONTENT_TYPE] = &MmsHeader::DecodeMmsContentType;
74 memberFuncMap_[MMS_ELEMENT_DESCRIPTOR] = &MmsHeader::DecodeMmsContentType;
75 memberFuncMap_[MMS_DATE] = &MmsHeader::DecodeFieldLongValue;
76 memberFuncMap_[MMS_REPLY_CHARGING_SIZE] = &MmsHeader::DecodeFieldLongValue;
77 memberFuncMap_[MMS_MESSAGE_SIZE] = &MmsHeader::DecodeFieldLongValue;
78
79 memberFuncMap_[MMS_MESSAGE_COUNT] = &MmsHeader::DecodeFieldIntegerValue;
80 memberFuncMap_[MMS_START] = &MmsHeader::DecodeFieldIntegerValue;
81 memberFuncMap_[MMS_LIMIT] = &MmsHeader::DecodeFieldIntegerValue;
82 memberFuncMap_[MMS_MMS_VERSION] = &MmsHeader::DecodeFieldIntegerValue;
83
84 memberFuncMap_[MMS_DELIVERY_TIME] = &MmsHeader::DecodeFieldDate;
85 memberFuncMap_[MMS_EXPIRY] = &MmsHeader::DecodeFieldDate;
86 memberFuncMap_[MMS_REPLY_CHARGING_DEADLINE] = &MmsHeader::DecodeFieldDate; // MmsReplyChargingDeadline
87 memberFuncMap_[MMS_PREVIOUSLY_SENT_DATE] = &MmsHeader::DecodeFieldPreviouslySentDate;
88 }
89
InitStringHandleFun()90 void MmsHeader::InitStringHandleFun()
91 {
92 memberFuncMap_[MMS_RESPONSE_TEXT] = &MmsHeader::DecodeFieldEncodedStringValue;
93 memberFuncMap_[MMS_SUBJECT] = &MmsHeader::DecodeFieldEncodedStringValue;
94 memberFuncMap_[MMS_RETRIEVE_TEXT] = &MmsHeader::DecodeFieldEncodedStringValue;
95 memberFuncMap_[MMS_PREVIOUSLY_SENT_BY] = &MmsHeader::DecodeFieldEncodedStringValue;
96
97 memberFuncMap_[MMS_STORE_STATUS_TEXT] = &MmsHeader::DecodeFieldEncodedStringValue;
98 memberFuncMap_[MMS_RECOMMENDED_RETRIEVAL_MODE_TEXT] = &MmsHeader::DecodeFieldEncodedStringValue;
99 memberFuncMap_[MMS_STATUS_TEXT] = &MmsHeader::DecodeFieldEncodedStringValue;
100
101 memberFuncMap_[MMS_BCC] = &MmsHeader::DecodeFieldAddressModelValue;
102 memberFuncMap_[MMS_CC] = &MmsHeader::DecodeFieldAddressModelValue;
103 memberFuncMap_[MMS_TO] = &MmsHeader::DecodeFieldAddressModelValue;
104
105 memberFuncMap_[MMS_FROM] = &MmsHeader::DecodeFromValue;
106 }
107
InitTextStringHandleFun()108 void MmsHeader::InitTextStringHandleFun()
109 {
110 memberFuncMap_[MMS_CONTENT_LOCATION] = &MmsHeader::DecodeFieldTextStringValue;
111 memberFuncMap_[MMS_TRANSACTION_ID] = &MmsHeader::DecodeFieldTextStringValue;
112 memberFuncMap_[MMS_MESSAGE_ID] = &MmsHeader::DecodeFieldTextStringValue;
113 memberFuncMap_[MMS_REPLACE_ID] = &MmsHeader::DecodeFieldTextStringValue;
114 memberFuncMap_[MMS_CANCEL_ID] = &MmsHeader::DecodeFieldTextStringValue;
115 memberFuncMap_[MMS_APPLIC_ID] = &MmsHeader::DecodeFieldTextStringValue;
116 memberFuncMap_[MMS_REPLY_APPLIC_ID] = &MmsHeader::DecodeFieldTextStringValue;
117 memberFuncMap_[MMS_REPLY_CHARGING_ID] = &MmsHeader::DecodeFieldTextStringValue;
118 memberFuncMap_[MMS_AUX_APPLIC_INFO] = &MmsHeader::DecodeFieldTextStringValue;
119 }
120
DumpMmsHeader()121 void MmsHeader::DumpMmsHeader()
122 {
123 bool find = false;
124 std::string fieldName;
125 for (auto it = octetValueMap_.begin(); it != octetValueMap_.end(); it++) {
126 find = FindHeaderFieldName(it->first, fieldName);
127 TELEPHONY_LOGI("%{public}s : 0x%{public}02X", find ? fieldName.c_str() : "unkown field", it->second);
128 }
129
130 for (auto it = longValueMap_.begin(); it != longValueMap_.end(); it++) {
131 find = FindHeaderFieldName(it->first, fieldName);
132 TELEPHONY_LOGI("%{public}s : %{public}" PRId64 "", find ? fieldName.c_str() : "unkown field", it->second);
133 }
134
135 for (auto it = textValueMap_.begin(); it != textValueMap_.end(); it++) {
136 find = FindHeaderFieldName(it->first, fieldName);
137 TELEPHONY_LOGI("%{public}s : %{public}s", find ? fieldName.c_str() : "unkown field", it->second.c_str());
138 }
139
140 for (auto it = encodeStringsMap_.begin(); it != encodeStringsMap_.end(); it++) {
141 find = FindHeaderFieldName(it->first, fieldName);
142 std::string enString;
143 if (find) {
144 it->second.GetEncodeString(enString);
145 }
146 TELEPHONY_LOGI("%{public}s : %{public}s", find ? fieldName.c_str() : "unkown field", enString.c_str());
147 }
148
149 for (auto it = addressMap_.begin(); it != addressMap_.end(); it++) {
150 find = FindHeaderFieldName(it->first, fieldName);
151 std::vector<MmsAddress> address = it->second;
152 for (auto itAddress : address) {
153 std::string enString = itAddress.GetAddressString();
154 TELEPHONY_LOGI("%{public}s : %{public}s", find ? fieldName.c_str() : "unkown field", enString.c_str());
155 }
156 }
157 }
158
DecodeMmsHeader(MmsDecodeBuffer & decodeBuffer)159 bool MmsHeader::DecodeMmsHeader(MmsDecodeBuffer &decodeBuffer)
160 {
161 const uint8_t setHighestBitOne = 0x80;
162 uint8_t fieldCode = 0xff;
163 uint8_t oneByte = 0;
164 int32_t len = 0;
165 while (decodeBuffer.GetOneByte(oneByte)) {
166 fieldCode = (oneByte | setHighestBitOne);
167 auto decodeFunc = memberFuncMap_.find(fieldCode);
168 if (decodeFunc != memberFuncMap_.end()) {
169 auto fun = decodeFunc->second;
170 if (fun != nullptr && !(this->*fun)(fieldCode, decodeBuffer, len)) {
171 TELEPHONY_LOGE("The fieldId[%{public}d] decode header fail.", fieldCode);
172 return false;
173 }
174 if (fieldCode == MMS_CONTENT_TYPE) {
175 break;
176 }
177 } else {
178 TELEPHONY_LOGI("DecodeMmsMsgUnKnownField:%{public}02X", fieldCode);
179 DecodeMmsMsgUnKnownField(decodeBuffer);
180 }
181 }
182 return true;
183 }
184
EncodeMmsHeader(MmsEncodeBuffer & encodeBuffer)185 bool MmsHeader::EncodeMmsHeader(MmsEncodeBuffer &encodeBuffer)
186 {
187 uint8_t messageType = 0;
188 const uint8_t defTranScactionIdLen = 24;
189 const int64_t defVersion = static_cast<int64_t>(MmsVersionType::MMS_VERSION_1_3);
190 if (!GetOctetValue(MMS_MESSAGE_TYPE, messageType)) {
191 TELEPHONY_LOGE("Not Set Mms Message Type Error.");
192 return false;
193 }
194
195 std::string transactionId = "";
196 if (!GetStringValue(MMS_TRANSACTION_ID, transactionId) && IsHaveTransactionId(messageType)) {
197 SetTextValue(MMS_TRANSACTION_ID, MakeTransactionId(defTranScactionIdLen));
198 }
199
200 int64_t mmsVersion = 0;
201 if (!GetLongValue(MMS_MMS_VERSION, mmsVersion)) {
202 mmsVersion = defVersion;
203 SetLongValue(MMS_MMS_VERSION, mmsVersion);
204 }
205 if (mmsVersion < static_cast<int64_t>(MmsVersionType::MMS_VERSION_1_0) ||
206 mmsVersion > static_cast<int64_t>(MmsVersionType::MMS_VERSION_1_3)) {
207 SetLongValue(MMS_MMS_VERSION, defVersion);
208 }
209
210 if (!EncodeCommontFieldValue(encodeBuffer)) {
211 TELEPHONY_LOGE("Encode Commont Header Field Err.");
212 return false;
213 }
214 switch (messageType) {
215 case MMS_MSGTYPE_SEND_REQ:
216 return EncodeMmsSendReq(encodeBuffer);
217 case MMS_MSGTYPE_SEND_CONF:
218 return EncodeMmsSendConf(encodeBuffer);
219 case MMS_MSGTYPE_NOTIFICATION_IND:
220 return EncodeMmsNotificationInd(encodeBuffer);
221 case MMS_MSGTYPE_NOTIFYRESP_IND:
222 return EnocdeMmsNotifyRespInd(encodeBuffer);
223 case MMS_MSGTYPE_RETRIEVE_CONF:
224 return EnocdeMmsRetrieveConf(encodeBuffer);
225 case MMS_MSGTYPE_ACKNOWLEDGE_IND:
226 return EnocdeMmsAcknowledgeInd(encodeBuffer);
227 case MMS_MSGTYPE_DELIVERY_IND:
228 return EnocdeMmsDeliveryInd(encodeBuffer);
229 case MMS_MSGTYPE_READ_REC_IND:
230 return EncodeMmsReadRecInd(encodeBuffer);
231 case MMS_MSGTYPE_READ_ORIG_IND:
232 return EncodeMmsReadOrigInd(encodeBuffer);
233 default:
234 TELEPHONY_LOGE("Mms Message Type unSupported Err.");
235 return false;
236 }
237 }
238
GetOctetValue(uint8_t fieldId,uint8_t & value) const239 bool MmsHeader::GetOctetValue(uint8_t fieldId, uint8_t &value) const
240 {
241 value = 0;
242 auto it = octetValueMap_.find(fieldId);
243 if (it != octetValueMap_.end()) {
244 value = it->second;
245 return true;
246 }
247 return false;
248 }
249
SetOctetValue(uint8_t fieldId,uint8_t value)250 bool MmsHeader::SetOctetValue(uint8_t fieldId, uint8_t value)
251 {
252 if (mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_OCTET_TYPE) {
253 TELEPHONY_LOGE("The fieldId[%{public}02X] is not belong to octet.", fieldId);
254 return false;
255 }
256 if (CheckBooleanValue(fieldId, value)) {
257 return true;
258 }
259
260 switch (fieldId) {
261 case MMS_MESSAGE_TYPE:
262 if (value < static_cast<uint8_t>(MmsMsgType::MMS_MSGTYPE_SEND_REQ) ||
263 value > static_cast<uint8_t>(MmsMsgType::MMS_MSGTYPE_FORWARD_CONF)) {
264 TELEPHONY_LOGE("The MMS_MESSAGE_TYPE value invalid.");
265 return false;
266 }
267 break;
268 case MMS_PRIORITY:
269 if (value < static_cast<uint8_t>(MmsPriority::MMS_LOW) ||
270 value > static_cast<uint8_t>(MmsPriority::MMS_HIGH)) {
271 TELEPHONY_LOGE("The MMS_PRIORITY value invalid.");
272 return false;
273 }
274 break;
275 case MMS_RESPONSE_STATUS:
276 if (!CheckResponseStatus(value)) {
277 TELEPHONY_LOGE("The MMS_RESPONSE_STATUS value invalid.");
278 return false;
279 }
280 break;
281 case MMS_SENDER_VISIBILITY:
282 if (value < static_cast<uint8_t>(MmsSenderVisibility::MMS_HIDE) ||
283 value > static_cast<uint8_t>(MmsSenderVisibility::MMS_SHOW)) {
284 TELEPHONY_LOGE("The MMS_SENDER_VISIBILITY value invalid.");
285 return false;
286 }
287 break;
288 case MMS_STATUS:
289 if (value < static_cast<uint8_t>(MmsStatus::MMS_EXPIRED) ||
290 value > static_cast<uint8_t>(MmsStatus::MMS_UNREACHABLE)) {
291 TELEPHONY_LOGE("The MMS_STATUS value invalid.");
292 return false;
293 }
294 break;
295 case MMS_RETRIEVE_STATUS:
296 if (!CheckRetrieveStatus(value)) {
297 TELEPHONY_LOGE("The MMS_RETRIEVE_STATUS value invalid.");
298 return false;
299 }
300 break;
301 case MMS_READ_STATUS:
302 if (value < static_cast<uint8_t>(MmsReadStatus::MMS_READ) ||
303 value > static_cast<uint8_t>(MmsReadStatus::MMS_DELETED_WITHOUT_BEING_READ)) {
304 TELEPHONY_LOGE("The MMS_READ_STATUS value invalid.");
305 return false;
306 }
307 break;
308 case MMS_REPLY_CHARGING:
309 if (value < static_cast<uint8_t>(MmsReplyCharging::MMS_REQUESTED) ||
310 value > static_cast<uint8_t>(MmsReplyCharging::MMS_ACCEPTED_TEXT_ONLY)) {
311 TELEPHONY_LOGE("The MMS_REPLY_CHARGING value invalid.");
312 return false;
313 }
314 break;
315 case MMS_MM_STATE:
316 if (value < static_cast<uint8_t>(MmsMmState::MMS_MM_STATE_DRAFT) ||
317 value > static_cast<uint8_t>(MmsMmState::MMS_MM_STATE_FORWARDED)) {
318 TELEPHONY_LOGE("The MMS_MM_STATE value invalid.");
319 return false;
320 }
321 break;
322 case MMS_MM_FLAGS:
323 if (value < static_cast<uint8_t>(MmsMmFlags::MMS_ADD_TOKEN) ||
324 value > static_cast<uint8_t>(MmsMmFlags::MMS_FILTER_TOKEN)) {
325 TELEPHONY_LOGE("The MMS_MM_FLAGS value invalid.");
326 return false;
327 }
328 break;
329 case MMS_STORE_STATUS:
330 if (!CheckStoreStatus(value)) {
331 TELEPHONY_LOGE("The MMS_STORE_STATUS value invalid.");
332 return false;
333 }
334 break;
335 case MMS_CONTENT_CLASS:
336 if (value < static_cast<uint8_t>(MmsContentClass::MMS_TEXT) ||
337 value > static_cast<uint8_t>(MmsContentClass::MMS_CONTENT_RICH)) {
338 TELEPHONY_LOGE("The MMS_CONTENT_CLASS value invalid.");
339 return false;
340 }
341 break;
342 case MMS_CANCEL_STATUS:
343 if (value < static_cast<uint8_t>(MmsCancelStatus::MMS_CANCEL_REQUEST_SUCCESSFULLY_RECEIVED) ||
344 value > static_cast<uint8_t>(MmsCancelStatus::MMS_CANCEL_REQUEST_CORRUPTED)) {
345 TELEPHONY_LOGE("The MMS_CANCEL_STATUS value invalid.");
346 return false;
347 }
348 break;
349 case MMS_MESSAGE_CLASS:
350 if (value < static_cast<uint8_t>(MmsMessageClass::PERSONAL) ||
351 value > static_cast<uint8_t>(MmsMessageClass::AUTO)) {
352 TELEPHONY_LOGE("The MMS_MESSAGE_CLASS value invalid.");
353 return false;
354 }
355 break;
356 default:
357 TELEPHONY_LOGE("The fieldId[%{public}02X] value invalid.", fieldId);
358 return false;
359 }
360 auto ret = octetValueMap_.emplace(fieldId, value);
361 return ret.second;
362 }
363
GetLongValue(uint8_t fieldId,int64_t & value) const364 bool MmsHeader::GetLongValue(uint8_t fieldId, int64_t &value) const
365 {
366 value = 0;
367 auto it = longValueMap_.find(fieldId);
368 if (it != longValueMap_.end()) {
369 value = it->second;
370 return true;
371 }
372 return false;
373 }
374
SetLongValue(uint8_t fieldId,int64_t value)375 bool MmsHeader::SetLongValue(uint8_t fieldId, int64_t value)
376 {
377 if (mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_INTEGER_TYPE &&
378 mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_LONG_TYPE) {
379 TELEPHONY_LOGE("The fieldId[%{public}02X] value is not belong to LongValue.", fieldId);
380 return false;
381 }
382 auto ret = longValueMap_.emplace(fieldId, value);
383 return ret.second;
384 }
385
GetTextValue(uint8_t fieldId,std::string & value) const386 bool MmsHeader::GetTextValue(uint8_t fieldId, std::string &value) const
387 {
388 value.clear();
389 auto it = textValueMap_.find(fieldId);
390 if (it != textValueMap_.end()) {
391 value.assign(it->second);
392 return true;
393 }
394 return false;
395 }
396
SetTextValue(uint8_t fieldId,std::string value)397 bool MmsHeader::SetTextValue(uint8_t fieldId, std::string value)
398 {
399 if (value.empty()) {
400 TELEPHONY_LOGE("fieldId[%{public}d] The Value Is Empty Error.", fieldId);
401 return false;
402 }
403 if (mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_TEXT_TYPE) {
404 TELEPHONY_LOGE("The fieldId[%{public}02X] value is not belong to TextValue.", fieldId);
405 return false;
406 }
407 auto ret = textValueMap_.emplace(fieldId, value);
408 return ret.second;
409 }
410
GetEncodeStringValue(uint8_t fieldId,MmsEncodeString & value) const411 bool MmsHeader::GetEncodeStringValue(uint8_t fieldId, MmsEncodeString &value) const
412 {
413 auto it = encodeStringsMap_.find(fieldId);
414 if (it != encodeStringsMap_.end()) {
415 value = it->second;
416 return true;
417 }
418 return false;
419 }
420
SetEncodeStringValue(uint8_t fieldId,uint32_t charset,const std::string & value)421 bool MmsHeader::SetEncodeStringValue(uint8_t fieldId, uint32_t charset, const std::string &value)
422 {
423 if (value.empty()) {
424 TELEPHONY_LOGE("fieldId[%{public}d] The Value Is Empty Error.", fieldId);
425 return false;
426 }
427 if (mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_ENCODE_TEXT_TYPE) {
428 TELEPHONY_LOGE("The fieldId[%{public}02X] value is not belong to EncodeString.", fieldId);
429 return false;
430 }
431 MmsEncodeString encodeString;
432 if (!encodeString.SetEncodeString(charset, value)) {
433 TELEPHONY_LOGE("MmsHeader SetEncodeString fail.");
434 return false;
435 }
436 auto ret = encodeStringsMap_.emplace(fieldId, encodeString);
437 return ret.second;
438 }
439
AddHeaderAddressValue(uint8_t fieldId,MmsAddress & address)440 bool MmsHeader::AddHeaderAddressValue(uint8_t fieldId, MmsAddress &address)
441 {
442 std::string strAddress = address.GetAddressString();
443 if (strAddress.empty() && fieldId != MMS_FROM) {
444 TELEPHONY_LOGE("Address is empty error.");
445 return false;
446 }
447 if (mmsHaderCateg_.FindFieldDes(fieldId) != MmsFieldValueType::MMS_FIELD_ENCODE_ADDR_TYPE) {
448 TELEPHONY_LOGE("The fieldId[%{public}02X] value is not belong to EncodeString.", fieldId);
449 return false;
450 }
451 auto it = addressMap_.find(fieldId);
452 if (it != addressMap_.end()) {
453 it->second.push_back(address);
454 return true;
455 } else {
456 std::vector<MmsAddress> addressValue;
457 addressValue.push_back(address);
458 auto ret = addressMap_.emplace(fieldId, addressValue);
459 return ret.second;
460 }
461 }
462
GetHeaderAllAddressValue(uint8_t fieldId,std::vector<MmsAddress> & addressValue)463 bool MmsHeader::GetHeaderAllAddressValue(uint8_t fieldId, std::vector<MmsAddress> &addressValue)
464 {
465 addressValue.clear();
466 auto it = addressMap_.find(fieldId);
467 if (it != addressMap_.end()) {
468 addressValue.assign(it->second.begin(), it->second.end());
469 return true;
470 }
471 return false;
472 }
473
GetStringValue(uint8_t fieldId,std::string & value) const474 bool MmsHeader::GetStringValue(uint8_t fieldId, std::string &value) const
475 {
476 if (GetTextValue(fieldId, value)) {
477 return true;
478 } else {
479 value.clear();
480 MmsEncodeString eValue;
481 if (!GetEncodeStringValue(fieldId, eValue)) {
482 TELEPHONY_LOGE("The fieldId[%{public}d] GetEncodeStringValue fail.", fieldId);
483 return false;
484 }
485 eValue.GetEncodeString(value);
486 }
487 return true;
488 }
489
GetHeaderContentType()490 MmsContentType &MmsHeader::GetHeaderContentType()
491 {
492 return mmsContentType_;
493 }
494
FindHeaderFieldName(const uint8_t fieldId,std::string & fieldName)495 bool MmsHeader::FindHeaderFieldName(const uint8_t fieldId, std::string &fieldName)
496 {
497 for (unsigned int i = 0; i < sizeof(mmsHeaderNames) / sizeof(mmsHeaderNames[0]); i++) {
498 if (fieldId == mmsHeaderNames[i].key) {
499 fieldName.clear();
500 fieldName.append(mmsHeaderNames[i].value);
501 return true;
502 }
503 }
504 return false;
505 }
506
DecodeMmsMsgType(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)507 bool MmsHeader::DecodeMmsMsgType(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
508 {
509 std::string fieldName;
510 if (!FindHeaderFieldName(fieldId, fieldName)) {
511 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader FindHeaderFieldName fail.", fieldId);
512 return false;
513 }
514
515 uint8_t oneByte = 0;
516 if (!buff.GetOneByte(oneByte)) {
517 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
518 return false;
519 }
520 if (oneByte < MMS_MSGTYPE_SEND_REQ || oneByte > MMS_MSGTYPE_CANCEL_CONF) {
521 TELEPHONY_LOGE("MmsHeader message type not support.");
522 return false;
523 }
524 octetValueMap_.emplace(fieldId, oneByte);
525 return true;
526 }
527
528 /**
529 * @brief DecodeFieldAddressModelValue
530 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.21 From Field
531 * From-value = Value-length (Address-present-token Encoded-string-value | Insert-address-token)
532 * Address-present-token = <Octet 128>
533 * Insert-address-token = <Octet 129>
534 * @param fieldId
535 * @param buff
536 * @param len
537 * @return true
538 * @return false
539 */
DecodeFieldAddressModelValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)540 bool MmsHeader::DecodeFieldAddressModelValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
541 {
542 std::string fieldName;
543 if (!FindHeaderFieldName(fieldId, fieldName)) {
544 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader FindHeaderFieldName fail.", fieldId);
545 return false;
546 }
547
548 MmsEncodeString encodeString;
549 encodeString.DecodeEncodeString(buff);
550
551 std::string encodeAddress;
552 encodeString.GetEncodeString(encodeAddress);
553 MmsAddress address(encodeAddress);
554
555 std::vector<MmsAddress> vecAddress;
556 auto it = addressMap_.find(fieldId);
557 if (it != addressMap_.end()) {
558 vecAddress = it->second;
559 it->second.push_back(address);
560 } else {
561 vecAddress.push_back(address);
562 addressMap_.emplace(fieldId, vecAddress);
563 }
564 return true;
565 }
566
DecodeFieldOctetValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)567 bool MmsHeader::DecodeFieldOctetValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
568 {
569 std::string fieldName;
570 if (!FindHeaderFieldName(fieldId, fieldName)) {
571 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader FindHeaderFieldName fail.", fieldId);
572 return false;
573 }
574 uint8_t oneByte = 0;
575 if (!buff.GetOneByte(oneByte)) {
576 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
577 return false;
578 }
579 octetValueMap_.emplace(fieldId, oneByte);
580 return true;
581 }
582
DecodeFieldLongValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)583 bool MmsHeader::DecodeFieldLongValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
584 {
585 std::string fieldName;
586 if (!FindHeaderFieldName(fieldId, fieldName)) {
587 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
588 return false;
589 }
590
591 uint64_t value = 0;
592 if (!buff.DecodeLongInteger(value)) {
593 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeLongInteger fail.", fieldId);
594 return false;
595 }
596 auto ret = longValueMap_.emplace(fieldId, value);
597 return ret.second;
598 }
599
MakeTransactionId(uint32_t len)600 std::string MmsHeader::MakeTransactionId(uint32_t len)
601 {
602 const uint32_t englishLettersNum = 25;
603 const uint32_t digitalNum = 9;
604 const uint32_t typeNum = 2;
605
606 std::default_random_engine e;
607 std::string transactionId = "";
608 std::uniform_int_distribution<unsigned> typeNumRandom(0, typeNum);
609 std::uniform_int_distribution<unsigned> digitalNumRandom(0, digitalNum);
610 std::uniform_int_distribution<unsigned> englishLettersNumRandom(0, englishLettersNum);
611 for (uint16_t i = 0; i < len; i++) {
612 switch (typeNumRandom(e)) {
613 case 0:
614 transactionId += 'A' + (englishLettersNumRandom(e));
615 break;
616 case 1:
617 transactionId += '0' + (digitalNumRandom(e));
618 break;
619 default:
620 transactionId += 'a' + (englishLettersNumRandom(e));
621 }
622 }
623 return transactionId;
624 }
625
DecodeFieldTextStringValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)626 bool MmsHeader::DecodeFieldTextStringValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
627 {
628 std::string fieldName;
629 if (!FindHeaderFieldName(fieldId, fieldName)) {
630 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
631 return false;
632 }
633
634 std::string tempString;
635 uint32_t tempLen = 0;
636 if (!buff.DecodeText(tempString, tempLen)) {
637 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeText fail.", fieldId);
638 return false;
639 }
640
641 len = (int32_t)tempLen;
642 textValueMap_.emplace(fieldId, tempString);
643 return true;
644 }
645
DecodeFieldEncodedStringValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)646 bool MmsHeader::DecodeFieldEncodedStringValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
647 {
648 std::string fieldName;
649 if (!FindHeaderFieldName(fieldId, fieldName)) {
650 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
651 return false;
652 }
653
654 MmsEncodeString encodeString;
655 if (!encodeString.DecodeEncodeString(buff)) {
656 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeEncodeString fail.", fieldId);
657 return false;
658 }
659 encodeStringsMap_.emplace(fieldId, encodeString);
660 return true;
661 }
662
663 /**
664 * @brief DecodeFromValue
665 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.21 From Field
666 * From-value = Value-length (Address-present-token Encoded-string-value | Insert-address-token)
667 * Address-present-token = <Octet 128>
668 * Insert-address-token = <Octet 129>
669 * @param fieldId
670 * @param buff
671 * @param len
672 * @return true
673 * @return false
674 */
DecodeFromValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)675 bool MmsHeader::DecodeFromValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
676 {
677 // Value-length (Address-present-token Encoded-string-value | Insert-address-token)
678 const int addressPresentToken = 128;
679 const int insertAddressToken = 129;
680 std::string fieldName;
681 if (!FindHeaderFieldName(fieldId, fieldName)) {
682 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
683 return false;
684 }
685
686 uint32_t valueLength = 0;
687 if (!buff.DecodeValueLength(valueLength)) {
688 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeValueLength fail.", fieldId);
689 return false;
690 }
691
692 uint8_t oneByte = 0;
693 if (!buff.GetOneByte(oneByte)) {
694 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
695 return false;
696 }
697
698 if (oneByte == addressPresentToken) {
699 // Address-present-token
700 MmsEncodeString encodeString;
701 encodeString.DecodeEncodeString(buff);
702
703 std::string encodeAddress;
704 encodeString.GetEncodeString(encodeAddress);
705 MmsAddress address(encodeAddress);
706
707 std::vector<MmsAddress> vecAddress;
708 vecAddress.push_back(address);
709 addressMap_.emplace(fieldId, vecAddress);
710 return true;
711 } else if (oneByte == insertAddressToken) {
712 // Insert-address-token
713 MmsAddress address("Insert-address-token");
714 std::vector<MmsAddress> vecAddress;
715 vecAddress.push_back(address);
716 addressMap_.emplace(fieldId, vecAddress);
717 return true;
718 }
719 return false;
720 }
721
TrimString(std::string & str)722 void MmsHeader::TrimString(std::string &str)
723 {
724 const unsigned char minStringLen = 2;
725 if (str.length() < minStringLen) {
726 return;
727 }
728 if (str.at(0) != '<' || str.at(str.length() - 1) != '>') {
729 return;
730 }
731 str.erase(0, 1);
732 str.erase(str.length() - 1, str.length());
733 return;
734 }
735
GetSmilFileName(std::string & smileFileName)736 bool MmsHeader::GetSmilFileName(std::string &smileFileName)
737 {
738 smileFileName = "";
739 const uint8_t startValue = static_cast<uint8_t>(ContentParam::CT_P_START_VALUE);
740 const uint8_t paramStart = static_cast<uint8_t>(ContentParam::CT_P_START);
741 uint8_t fieldIds[] = {startValue, paramStart};
742
743 for (unsigned int i = 0; i < sizeof(fieldIds); i++) {
744 auto start = mmsContentType_.GetContentParam().GetParamMap().find(fieldIds[i]);
745 if (start != mmsContentType_.GetContentParam().GetParamMap().end()) {
746 smileFileName = start->second;
747 return true;
748 }
749 }
750 return true;
751 }
752
DecodeMmsContentType(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)753 bool MmsHeader::DecodeMmsContentType(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
754 {
755 std::string fieldName;
756 if (!FindHeaderFieldName(fieldId, fieldName)) {
757 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader FindHeaderFieldName fail.", fieldId);
758 return false;
759 }
760 if (mmsContentType_.DecodeMmsContentType(buff, len)) {
761 return true;
762 }
763 return false;
764 }
765
DecodeMmsMsgUnKnownField(MmsDecodeBuffer & decodeBuffer)766 bool MmsHeader::DecodeMmsMsgUnKnownField(MmsDecodeBuffer &decodeBuffer)
767 {
768 const uint8_t minFieldValue = 0x80;
769 uint8_t oneByte = 0;
770 while (oneByte < minFieldValue) {
771 if (!decodeBuffer.GetOneByte(oneByte)) {
772 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
773 return false;
774 }
775 }
776 decodeBuffer.DecreasePointer(1);
777 return true;
778 }
779
DecodeFieldIntegerValue(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)780 bool MmsHeader::DecodeFieldIntegerValue(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
781 {
782 std::string fieldName;
783 if (!FindHeaderFieldName(fieldId, fieldName)) {
784 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
785 return false;
786 }
787
788 uint64_t value = 0;
789 if (!buff.DecodeInteger(value)) {
790 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeInteger fail.", fieldId);
791 return false;
792 }
793 auto ret = longValueMap_.emplace(fieldId, value);
794 return ret.second;
795 }
796
797 /**
798 * @brief DecodeFieldDate
799 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.14
800 * Value-length (Absolute-token Date-value | Relative-token Delta-seconds-value)
801 * Absolute-token = <Octet 128>
802 * Relative-token = <Octet 129>
803 * @param fieldId
804 * @param buff
805 * @param len
806 * @return true
807 * @return false
808 */
DecodeFieldDate(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)809 bool MmsHeader::DecodeFieldDate(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
810 {
811 std::string fieldName;
812 if (!FindHeaderFieldName(fieldId, fieldName)) {
813 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
814 return false;
815 }
816
817 const uint8_t relativeToken = 0x81;
818 uint32_t length = 0;
819 if (!buff.DecodeValueLength(length)) {
820 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeValueLength fail.", fieldId);
821 return false;
822 }
823 /* Absolute-token or Relative-token */
824 uint8_t token = 0;
825 if (!buff.GetOneByte(token)) {
826 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
827 return false;
828 }
829 /* Date-value or Delta-seconds-value */
830 uint64_t timeValue = 0;
831 if (!buff.DecodeLongInteger(timeValue)) {
832 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeLongInteger fail.", fieldId);
833 return false;
834 }
835 if (relativeToken == token) {
836 /* need to convert the Delta-seconds-value
837 * into Date-value */
838 chrono::system_clock::duration timePoint = chrono::system_clock::now().time_since_epoch();
839 long timeStamp = chrono::duration_cast<chrono::seconds>(timePoint).count();
840 timeValue += static_cast<uint64_t>(timeStamp);
841 }
842 auto ret = longValueMap_.emplace(fieldId, timeValue);
843 return ret.second;
844 }
845
846 /**
847 * @brief DecodeFieldPreviouslySentDate
848 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.24 X-Mms-Previously-Sent-Date Field
849 * Previously-sent-date-value = Value-length Forwarded-count-value Date-value
850 * Forwarded-count-value = Integer-value
851 * Date-value = Long-integer
852 * @param fieldId
853 * @param buff
854 * @param len
855 * @return true
856 * @return false
857 */
DecodeFieldPreviouslySentDate(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)858 bool MmsHeader::DecodeFieldPreviouslySentDate(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
859 {
860 std::string fieldName;
861 if (!FindHeaderFieldName(fieldId, fieldName)) {
862 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
863 return false;
864 }
865
866 /* Previously-sent-date-value =
867 * Value-length Forwarded-count-value Date-value */
868 /* parse value-length */
869 uint32_t length = 0;
870 uint64_t count = 0;
871 uint64_t perviouslySentDate = 0;
872 if (!buff.DecodeValueLength(length)) {
873 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeValueLength fail.", fieldId);
874 return false;
875 }
876 /* parse Forwarded-count-value */
877 if (!buff.DecodeInteger(count)) {
878 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeInteger fail.", fieldId);
879 return false;
880 }
881 if (!buff.DecodeLongInteger(perviouslySentDate)) {
882 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeLongInteger fail.", fieldId);
883 return false;
884 }
885 auto ret = longValueMap_.emplace(fieldId, perviouslySentDate);
886 return ret.second;
887 }
888
889 /**
890 * @brief DecodeFieldMBox
891 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.25
892 * Value-length (Message-quota-token | Size-quota-token) Integer-Value
893 * Message-quota-token = <Octet 128>
894 * Size-quota-token = <Octet 129>
895 * @param fieldId
896 * @param buff
897 * @param len
898 * @return true
899 * @return false
900 */
DecodeFieldMBox(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)901 bool MmsHeader::DecodeFieldMBox(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
902 {
903 std::string fieldName;
904 if (!FindHeaderFieldName(fieldId, fieldName)) {
905 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
906 return false;
907 }
908
909 /* Value-length
910 * (Message-total-token | Size-total-token) Integer-Value */
911 uint32_t length = 0;
912 if (!buff.DecodeValueLength(length)) {
913 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeValueLength fail.", fieldId);
914 return false;
915 }
916 uint8_t token = 0;
917 if (!buff.GetOneByte(token)) {
918 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
919 return false;
920 }
921 uint64_t value = 0;
922 if (!buff.DecodeInteger(value)) {
923 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeInteger fail.", fieldId);
924 return false;
925 }
926 return true;
927 }
928
929 /**
930 * @brief DecodeFieldMMFlag
931 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.32 X-Mms-MM-Flags Field
932 * Value-length ( Add-token | Remove-token | Filter-token ) Encoded-string-value
933 * Add-token = <Octet 128>
934 * Remove-token = <Octet 129>
935 * Filter-token = <Octet 130>
936 * @param fieldId
937 * @param buff
938 * @param len
939 * @return true
940 * @return false
941 */
DecodeFieldMMFlag(uint8_t fieldId,MmsDecodeBuffer & buff,int32_t & len)942 bool MmsHeader::DecodeFieldMMFlag(uint8_t fieldId, MmsDecodeBuffer &buff, int32_t &len)
943 {
944 std::string fieldName;
945 if (!FindHeaderFieldName(fieldId, fieldName)) {
946 TELEPHONY_LOGE("MmsHeader FindHeaderFieldName fail.");
947 return false;
948 }
949
950 uint32_t length = 0;
951 if (!buff.DecodeValueLength(length)) {
952 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeValueLength fail.", fieldId);
953 return false;
954 }
955 uint8_t token = 0;
956 if (!buff.GetOneByte(token)) {
957 TELEPHONY_LOGE("MmsHeader GetOneByte fail.");
958 return false;
959 }
960 MmsEncodeString encodeString;
961 if (!encodeString.DecodeEncodeString(buff)) {
962 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader DecodeEncodeString fail.", fieldId);
963 return false;
964 }
965 return true;
966 }
967
IsHaveBody()968 bool MmsHeader::IsHaveBody()
969 {
970 uint8_t msgType = 0;
971 GetOctetValue(MMS_MESSAGE_TYPE, msgType);
972 if (msgType == MMS_MSGTYPE_SEND_REQ || msgType == MMS_MSGTYPE_RETRIEVE_CONF) {
973 return true;
974 }
975 return false;
976 }
977
EncodeOctetValue(MmsEncodeBuffer & buff,uint8_t fieldId,uint8_t value)978 bool MmsHeader::EncodeOctetValue(MmsEncodeBuffer &buff, uint8_t fieldId, uint8_t value)
979 {
980 if (!buff.WriteByte(fieldId)) {
981 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
982 return false;
983 }
984 if (!buff.WriteByte(value)) {
985 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
986 return false;
987 }
988 return true;
989 }
990
EncodeShortIntegerValue(MmsEncodeBuffer & buff,uint8_t fieldId,int64_t value)991 bool MmsHeader::EncodeShortIntegerValue(MmsEncodeBuffer &buff, uint8_t fieldId, int64_t value)
992 {
993 if (!buff.WriteByte(fieldId)) {
994 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
995 return false;
996 }
997 if (!buff.EncodeShortInteger(value)) {
998 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeShortInteger fail.", fieldId);
999 return false;
1000 }
1001 return true;
1002 }
1003
EncodeTextStringValue(MmsEncodeBuffer & buff,uint8_t fieldId,std::string value)1004 bool MmsHeader::EncodeTextStringValue(MmsEncodeBuffer &buff, uint8_t fieldId, std::string value)
1005 {
1006 if (value.empty()) {
1007 TELEPHONY_LOGE("fieldId[%{public}d] EncodeTextStringValue Value Empty fail.", fieldId);
1008 return false;
1009 }
1010 if (!buff.WriteByte(fieldId)) {
1011 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
1012 return false;
1013 }
1014 if (!buff.EncodeText(value)) {
1015 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeText fail.", fieldId);
1016 return false;
1017 }
1018 return true;
1019 }
1020
EncodeEncodeStringValue(MmsEncodeBuffer & buff,uint8_t fieldId,MmsEncodeString value)1021 bool MmsHeader::EncodeEncodeStringValue(MmsEncodeBuffer &buff, uint8_t fieldId, MmsEncodeString value)
1022 {
1023 std::string valueUtf8;
1024 if (!value.GetEncodeString(valueUtf8)) {
1025 TELEPHONY_LOGE("fieldId[%{public}d] GetEncodeString Error", fieldId);
1026 return false;
1027 }
1028 if (valueUtf8.empty()) {
1029 TELEPHONY_LOGE("fieldId[%{public}d] GetEncodeString Empty Error", fieldId);
1030 return false;
1031 }
1032
1033 if (!buff.WriteByte(fieldId)) {
1034 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
1035 return false;
1036 }
1037 if (!value.EncodeEncodeString(buff)) {
1038 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeEncodeString fail.", fieldId);
1039 return false;
1040 }
1041 return true;
1042 }
1043
EncodeLongIntergerValue(MmsEncodeBuffer & buff,uint8_t fieldId,int64_t value)1044 bool MmsHeader::EncodeLongIntergerValue(MmsEncodeBuffer &buff, uint8_t fieldId, int64_t value)
1045 {
1046 if (!buff.WriteByte(fieldId)) {
1047 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
1048 return false;
1049 }
1050 if (!buff.EncodeLongInteger(value)) {
1051 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeLongInteger fail.", fieldId);
1052 return false;
1053 }
1054 return true;
1055 }
1056
EncodeOctetValueFromMap(MmsEncodeBuffer & buff,uint8_t fieldId)1057 bool MmsHeader::EncodeOctetValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId)
1058 {
1059 uint8_t value = 0;
1060 if (!GetOctetValue(fieldId, value)) {
1061 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
1062 return false;
1063 }
1064 if (!EncodeOctetValue(buff, fieldId, value)) {
1065 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeOctetValue fail.", fieldId);
1066 return false;
1067 }
1068 return true;
1069 }
1070
EncodeTextStringValueFromMap(MmsEncodeBuffer & buff,uint8_t fieldId)1071 bool MmsHeader::EncodeTextStringValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId)
1072 {
1073 std::string value = "";
1074 if (!GetTextValue(fieldId, value)) {
1075 TELEPHONY_LOGE("The fieldId[%{public}d] GetTextValue fail.", fieldId);
1076 return false;
1077 }
1078 if (!EncodeTextStringValue(buff, fieldId, value)) {
1079 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeTextStringValue fail.", fieldId);
1080 return false;
1081 }
1082 return true;
1083 }
1084
EnocdeEncodeStringValueFromMap(MmsEncodeBuffer & buff,uint8_t fieldId)1085 bool MmsHeader::EnocdeEncodeStringValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId)
1086 {
1087 MmsEncodeString value;
1088 if (!GetEncodeStringValue(fieldId, value)) {
1089 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader GetEncodeStringValue fail.", fieldId);
1090 return false;
1091 }
1092 if (!EncodeEncodeStringValue(buff, fieldId, value)) {
1093 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeEncodeStringValue fail.", fieldId);
1094 return false;
1095 }
1096 return true;
1097 }
1098
EnocdeShortIntegerValueFromMap(MmsEncodeBuffer & buff,uint8_t fieldId)1099 bool MmsHeader::EnocdeShortIntegerValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId)
1100 {
1101 int64_t value = 0;
1102 if (!GetLongValue(fieldId, value)) {
1103 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader GetLongValue fail.", fieldId);
1104 return false;
1105 }
1106 if (!EncodeShortIntegerValue(buff, fieldId, value)) {
1107 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeShortIntegerValue fail.", fieldId);
1108 return false;
1109 }
1110 return true;
1111 }
1112
EncodeLongIntergerValueFromMap(MmsEncodeBuffer & buff,uint8_t fieldId)1113 bool MmsHeader::EncodeLongIntergerValueFromMap(MmsEncodeBuffer &buff, uint8_t fieldId)
1114 {
1115 int64_t value = 0;
1116 if (!GetLongValue(fieldId, value)) {
1117 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader GetLongValue fail.", fieldId);
1118 return false;
1119 }
1120 if (!EncodeLongIntergerValue(buff, fieldId, value)) {
1121 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeLongIntergerValue fail.", fieldId);
1122 return false;
1123 }
1124 return true;
1125 }
1126
EncodeFieldExpriyValue(MmsEncodeBuffer & buff,int64_t value)1127 bool MmsHeader::EncodeFieldExpriyValue(MmsEncodeBuffer &buff, int64_t value)
1128 {
1129 if (!buff.WriteByte(MMS_EXPIRY)) {
1130 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1131 return false;
1132 }
1133
1134 const uint8_t relativeToken = 129;
1135 MmsEncodeBuffer tempBuf;
1136 if (!tempBuf.EncodeOctet(relativeToken)) {
1137 TELEPHONY_LOGE("MmsHeader EncodeOctet fail.");
1138 return false;
1139 }
1140 if (!tempBuf.EncodeLongInteger(value)) {
1141 TELEPHONY_LOGE("MmsHeader EncodeLongInteger fail.");
1142 return false;
1143 }
1144 if (!buff.EncodeValueLength(tempBuf.GetCurPosition())) {
1145 TELEPHONY_LOGE("MmsHeader EncodeValueLength fail.");
1146 return false;
1147 }
1148 if (!buff.WriteBuffer(tempBuf)) {
1149 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1150 return false;
1151 }
1152 return true;
1153 }
1154
1155 /**
1156 * @brief EncodeFieldFromValue
1157 * OMA-TS-MMS_ENC-V1_3-20110913-A section:7.3.21 From Field
1158 * From-value = Value-length (Address-present-token Encoded-string-value | Insert-address-token)
1159 * Address-present-token = <Octet 128>
1160 * Insert-address-token = <Octet 129>
1161 * @param buff
1162 * @param addr
1163 * @return true
1164 * @return false
1165 */
EncodeFieldFromValue(MmsEncodeBuffer & buff,std::vector<MmsAddress> & addr)1166 bool MmsHeader::EncodeFieldFromValue(MmsEncodeBuffer &buff, std::vector<MmsAddress> &addr)
1167 {
1168 auto it = addressMap_.find(MMS_FROM);
1169 if (it != addressMap_.end()) {
1170 if (!buff.WriteByte(MMS_FROM)) {
1171 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1172 return false;
1173 }
1174 // Value-length (Address-present-token Encoded-string-value | Insert-address-token)
1175 const uint8_t addressPresentToken = 128;
1176 const uint8_t insertAddressToken = 129;
1177 MmsEncodeBuffer tempBuff;
1178 if (addr.empty() || addr[0].GetAddressString().empty()) {
1179 if (!tempBuff.WriteByte(insertAddressToken)) {
1180 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1181 return false;
1182 }
1183 } else {
1184 if (!tempBuff.WriteByte(addressPresentToken)) {
1185 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1186 return false;
1187 }
1188
1189 MmsEncodeString encodeString;
1190 encodeString.SetAddressString(addr[0]);
1191 if (!encodeString.EncodeEncodeString(tempBuff)) {
1192 TELEPHONY_LOGE("MmsHeader EncodeEncodeString fail.");
1193 return false;
1194 }
1195 }
1196 if (!buff.EncodeValueLength(tempBuff.GetCurPosition())) {
1197 TELEPHONY_LOGE("MmsHeader EncodeValueLength fail.");
1198 return false;
1199 }
1200 if (!buff.WriteBuffer(tempBuff)) {
1201 TELEPHONY_LOGE("MmsHeader WriteByte fail.");
1202 return false;
1203 }
1204 addressMap_.erase(it);
1205 }
1206 return true;
1207 }
1208
EncodeMultipleAddressValue(MmsEncodeBuffer & buff,uint8_t fieldId,std::vector<MmsAddress> & addrs)1209 bool MmsHeader::EncodeMultipleAddressValue(MmsEncodeBuffer &buff, uint8_t fieldId, std::vector<MmsAddress> &addrs)
1210 {
1211 if (addrs.empty()) {
1212 TELEPHONY_LOGE("MmsHeader address is invalid.");
1213 return false;
1214 }
1215
1216 for (auto addr : addrs) {
1217 if (!buff.WriteByte(fieldId)) {
1218 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader WriteByte fail.", fieldId);
1219 return false;
1220 }
1221 MmsEncodeString encodeString;
1222 encodeString.SetAddressString(addr);
1223 if (!encodeString.EncodeEncodeString(buff)) {
1224 TELEPHONY_LOGE("The fieldId[%{public}d] MmsHeader EncodeEncodeString fail.", fieldId);
1225 return false;
1226 }
1227 }
1228 return true;
1229 }
1230
EcondeFieldMessageClassValue(MmsEncodeBuffer & buff)1231 bool MmsHeader::EcondeFieldMessageClassValue(MmsEncodeBuffer &buff)
1232 {
1233 uint8_t valueOctet = 0;
1234 if (GetOctetValue(MMS_MESSAGE_CLASS, valueOctet)) {
1235 return EncodeOctetValue(buff, MMS_MESSAGE_CLASS, valueOctet);
1236 }
1237
1238 std::string strValue = "";
1239 if (GetTextValue(MMS_MESSAGE_CLASS, strValue)) {
1240 if (strValue == "personal") {
1241 valueOctet = static_cast<uint8_t>(MmsMessageClass::PERSONAL);
1242 } else if (strValue == "advertisement") {
1243 valueOctet = static_cast<uint8_t>(MmsMessageClass::ADVERTISEMENT);
1244 } else if (strValue == "informational") {
1245 valueOctet = static_cast<uint8_t>(MmsMessageClass::INFORMATIONAL);
1246 } else if (strValue == "auto") {
1247 valueOctet = static_cast<uint8_t>(MmsMessageClass::AUTO);
1248 }
1249
1250 if (valueOctet != 0) {
1251 return EncodeOctetValue(buff, MMS_MESSAGE_CLASS, valueOctet);
1252 }
1253 return EncodeTextStringValue(buff, MMS_MESSAGE_CLASS, strValue);
1254 }
1255 return false;
1256 }
1257
EncodeCommontFieldValue(MmsEncodeBuffer & buff)1258 bool MmsHeader::EncodeCommontFieldValue(MmsEncodeBuffer &buff)
1259 {
1260 uint8_t messageType = 0;
1261 GetOctetValue(MMS_MESSAGE_TYPE, messageType);
1262 if (!EncodeOctetValue(buff, MMS_MESSAGE_TYPE, messageType)) {
1263 TELEPHONY_LOGE("MmsHeader EncodeOctetValue fail.");
1264 return false;
1265 }
1266
1267 std::string transactionId = "";
1268 if (GetTextValue(MMS_TRANSACTION_ID, transactionId)) {
1269 if (!EncodeTextStringValue(buff, MMS_TRANSACTION_ID, transactionId)) {
1270 TELEPHONY_LOGE("MmsHeader EncodeTextStringValue fail.");
1271 return false;
1272 }
1273 }
1274
1275 int64_t version = 0;
1276 GetLongValue(MMS_MMS_VERSION, version); // 8bit shortInteger
1277 if (!EncodeShortIntegerValue(buff, MMS_MMS_VERSION, version)) {
1278 TELEPHONY_LOGE("MmsHeader EncodeShortIntegerValue fail.");
1279 return false;
1280 }
1281 return true;
1282 }
1283
1284 /**
1285 * @brief EncodeMmsSendReq
1286 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.1.1 Send Request
1287 * @param buff
1288 * @return true
1289 * @return false
1290 */
EncodeMmsSendReq(MmsEncodeBuffer & buff)1291 bool MmsHeader::EncodeMmsSendReq(MmsEncodeBuffer &buff)
1292 {
1293 // FROM
1294 std::vector<MmsAddress> fromAddress;
1295 GetHeaderAllAddressValue(MMS_FROM, fromAddress);
1296 if (!EncodeFieldFromValue(buff, fromAddress)) {
1297 TELEPHONY_LOGE("encode mandatory from field error.");
1298 return false;
1299 }
1300 // Optional
1301 int64_t mmsExpriy = 0;
1302 if (GetLongValue(MMS_EXPIRY, mmsExpriy)) {
1303 EncodeFieldExpriyValue(buff, mmsExpriy);
1304 }
1305 // CC BCC TO
1306 for (auto it = addressMap_.begin(); it != addressMap_.end(); it++) {
1307 EncodeMultipleAddressValue(buff, it->first, it->second);
1308 }
1309
1310 EcondeFieldMessageClassValue(buff); // MMS_MESSAGE_CLASS
1311 EncodeLongIntergerValueFromMap(buff, MMS_DATE);
1312 EnocdeEncodeStringValueFromMap(buff, MMS_SUBJECT);
1313 EncodeTextStringValueFromMap(buff, MMS_MESSAGE_CLASS);
1314 EncodeOctetValueFromMap(buff, MMS_PRIORITY);
1315 EncodeOctetValueFromMap(buff, MMS_SENDER_VISIBILITY);
1316 EncodeOctetValueFromMap(buff, MMS_DELIVERY_REPORT);
1317 EncodeOctetValueFromMap(buff, MMS_READ_REPORT);
1318 return true;
1319 }
1320
1321 /**
1322 * @brief EncodeMmsSendConf
1323 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.1.2 Send Confirmationt
1324 * @param buff
1325 * @return true
1326 * @return false
1327 */
EncodeMmsSendConf(MmsEncodeBuffer & buff)1328 bool MmsHeader::EncodeMmsSendConf(MmsEncodeBuffer &buff)
1329 {
1330 // Mandatory
1331 if (!EncodeOctetValueFromMap(buff, MMS_RESPONSE_STATUS)) {
1332 TELEPHONY_LOGE("encode mandatory response status field error.");
1333 return false;
1334 }
1335 // Optional
1336 EncodeTextStringValueFromMap(buff, MMS_MESSAGE_ID);
1337 return true;
1338 }
1339
1340 /**
1341 * @brief EncodeMmsNotificationInd
1342 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.2 Multimedia Message Notification
1343 * @param buff
1344 * @return true
1345 * @return false
1346 */
EncodeMmsNotificationInd(MmsEncodeBuffer & buff)1347 bool MmsHeader::EncodeMmsNotificationInd(MmsEncodeBuffer &buff)
1348 {
1349 // Mandatory
1350 if (!EncodeTextStringValueFromMap(buff, MMS_CONTENT_LOCATION)) {
1351 TELEPHONY_LOGE("encode mandatory content location field error.");
1352 return false;
1353 }
1354 if (!EncodeLongIntergerValueFromMap(buff, MMS_MESSAGE_SIZE)) {
1355 TELEPHONY_LOGE("encode mandatory message size field error.");
1356 return false;
1357 }
1358 int64_t mmsExpriy = 0;
1359 if (!GetLongValue(MMS_EXPIRY, mmsExpriy)) {
1360 TELEPHONY_LOGE("get mandatory expriy field no set error.");
1361 return false;
1362 }
1363 if (!EncodeFieldExpriyValue(buff, mmsExpriy)) {
1364 TELEPHONY_LOGE("encode mandatory expriy field error.");
1365 return false;
1366 }
1367 if (!EcondeFieldMessageClassValue(buff)) { // MMS_MESSAGE_CLASS
1368 TELEPHONY_LOGE("encode mandatory message class field error.");
1369 return false;
1370 }
1371
1372 // Optional
1373 std::vector<MmsAddress> fromAddress;
1374 if (GetHeaderAllAddressValue(MMS_FROM, fromAddress)) {
1375 EncodeFieldFromValue(buff, fromAddress);
1376 }
1377 EncodeOctetValueFromMap(buff, MMS_CONTENT_CLASS);
1378 EnocdeEncodeStringValueFromMap(buff, MMS_SUBJECT);
1379 EncodeOctetValueFromMap(buff, MMS_DELIVERY_REPORT);
1380 return true;
1381 }
1382
1383 /**
1384 * @brief EnocdeMmsNotifyRespInd
1385 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.2 Multimedia Message Notification
1386 * @param buff
1387 * @return true
1388 * @return false
1389 */
EnocdeMmsNotifyRespInd(MmsEncodeBuffer & buff)1390 bool MmsHeader::EnocdeMmsNotifyRespInd(MmsEncodeBuffer &buff)
1391 {
1392 // Mandatory
1393 if (!EncodeOctetValueFromMap(buff, MMS_STATUS)) {
1394 TELEPHONY_LOGE("encode mandatory mms status field error.");
1395 return false;
1396 }
1397 // Optional
1398 EncodeOctetValueFromMap(buff, MMS_REPORT_ALLOWED);
1399 return true;
1400 }
1401
1402 /**
1403 * @brief EnocdeMmsRetrieveConf
1404 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.3 Retrieval of Multimedia Message
1405 * @param buff
1406 * @return true
1407 * @return false
1408 */
EnocdeMmsRetrieveConf(MmsEncodeBuffer & buff)1409 bool MmsHeader::EnocdeMmsRetrieveConf(MmsEncodeBuffer &buff)
1410 {
1411 // Mandatory
1412 if (!EncodeLongIntergerValueFromMap(buff, MMS_DATE)) {
1413 TELEPHONY_LOGE("encode mandatory mms date field error.");
1414 return false;
1415 }
1416
1417 // Optional
1418 EncodeTextStringValueFromMap(buff, MMS_MESSAGE_ID); // Conditional
1419 std::vector<MmsAddress> fromAddress;
1420 if (GetHeaderAllAddressValue(MMS_FROM, fromAddress)) {
1421 EncodeFieldFromValue(buff, fromAddress);
1422 }
1423 std::vector<MmsAddress> toAddress;
1424 if (GetHeaderAllAddressValue(MMS_TO, toAddress)) {
1425 if (!EncodeMultipleAddressValue(buff, MMS_TO, toAddress)) {
1426 TELEPHONY_LOGE("MmsHeader EncodeMultipleAddressValue fail.");
1427 return false;
1428 }
1429 }
1430 std::vector<MmsAddress> ccAddress;
1431 if (GetHeaderAllAddressValue(MMS_CC, ccAddress)) {
1432 if (!EncodeMultipleAddressValue(buff, MMS_CC, ccAddress)) {
1433 TELEPHONY_LOGE("MmsHeader EncodeMultipleAddressValue fail.");
1434 return false;
1435 }
1436 }
1437 EcondeFieldMessageClassValue(buff); // MMS_MESSAGE_CLASS
1438 EnocdeEncodeStringValueFromMap(buff, MMS_SUBJECT);
1439 EncodeOctetValueFromMap(buff, MMS_PRIORITY);
1440 EncodeOctetValueFromMap(buff, MMS_DELIVERY_REPORT);
1441 EncodeOctetValueFromMap(buff, MMS_READ_REPORT);
1442 EncodeOctetValueFromMap(buff, MMS_RETRIEVE_STATUS);
1443 EnocdeEncodeStringValueFromMap(buff, MMS_RETRIEVE_TEXT);
1444 // MMS_PREVIOUSLY_SENT_BY unSupported
1445 // MMS_PREVIOUSLY_SENT_DATE unSupported
1446 // MMS_MM_STATE unSupported
1447 // MMS_MM_FLAGS unSupported
1448 // MMS_REPLY_CHARGING unSupported
1449 // MMS_REPLY_CHARGING_DEADLINE unSupported
1450 // MMS_REPLY_CHARGING_SIZE unSupported
1451 // MMS_REPLY_CHARGING_ID unSupported
1452 // MMS_DISTRIBUTION_INDICATOR unSupported
1453 // MMS_APPLIC_ID unSupported
1454 // MMS_REPLY_APPLIC_ID unSupported
1455 // MMS_AUX_APPLIC_INFO unSupported
1456 // MMS_CONTENT_CLASS unSupported
1457 // MMS_DRM_CONTENT unSupported
1458 // MMS_REPLACE_ID unSupported
1459 return true;
1460 }
1461
1462 /**
1463 * @brief EnocdeMmsAcknowledgeInd
1464 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.4 Delivery Acknowledgement
1465 * @param buff
1466 * @return true
1467 * @return false
1468 */
EnocdeMmsAcknowledgeInd(MmsEncodeBuffer & buff)1469 bool MmsHeader::EnocdeMmsAcknowledgeInd(MmsEncodeBuffer &buff)
1470 {
1471 // Optional
1472 EncodeOctetValueFromMap(buff, MMS_REPORT_ALLOWED);
1473 return true;
1474 }
1475
1476 /**
1477 * @brief EnocdeMmsDeliveryInd
1478 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.6 Delivery Reporting
1479 * @param buff
1480 * @return true
1481 * @return false
1482 */
EnocdeMmsDeliveryInd(MmsEncodeBuffer & buff)1483 bool MmsHeader::EnocdeMmsDeliveryInd(MmsEncodeBuffer &buff)
1484 {
1485 // Mandatory
1486 std::vector<MmsAddress> toAddress;
1487 if (!GetHeaderAllAddressValue(MMS_TO, toAddress)) {
1488 TELEPHONY_LOGE("get mandatory mms to field no set error.");
1489 return false;
1490 }
1491 if (!EncodeMultipleAddressValue(buff, MMS_TO, toAddress)) {
1492 TELEPHONY_LOGE("encode mandatory mms to field error.");
1493 return false;
1494 }
1495 if (!EncodeTextStringValueFromMap(buff, MMS_MESSAGE_ID)) {
1496 TELEPHONY_LOGE("encode mandatory mms message id field error.");
1497 return false;
1498 }
1499 if (!EncodeLongIntergerValueFromMap(buff, MMS_DATE)) {
1500 TELEPHONY_LOGE("encode mandatory mms date field error.");
1501 return false;
1502 }
1503 if (!EncodeOctetValueFromMap(buff, MMS_STATUS)) {
1504 TELEPHONY_LOGE("encode mandatory mms status field error.");
1505 return false;
1506 }
1507 // Optional
1508 return true;
1509 }
1510
1511 /**
1512 * @brief EncodeMmsReadRecInd
1513 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.7.2 PDU Read Report
1514 * @param buff
1515 * @return true
1516 * @return false
1517 */
EncodeMmsReadRecInd(MmsEncodeBuffer & buff)1518 bool MmsHeader::EncodeMmsReadRecInd(MmsEncodeBuffer &buff)
1519 {
1520 // Mandatory
1521 if (!EncodeTextStringValueFromMap(buff, MMS_MESSAGE_ID)) {
1522 TELEPHONY_LOGE("encode mandatory mms message id field error.");
1523 return false;
1524 }
1525 std::vector<MmsAddress> toAddress;
1526 if (!GetHeaderAllAddressValue(MMS_TO, toAddress)) {
1527 TELEPHONY_LOGE("get mandatory mms to field no set error.");
1528 return false;
1529 }
1530 if (!EncodeMultipleAddressValue(buff, MMS_TO, toAddress)) {
1531 TELEPHONY_LOGE("encode mandatory mms to field error.");
1532 return false;
1533 }
1534
1535 std::vector<MmsAddress> fromAddress;
1536 GetHeaderAllAddressValue(MMS_FROM, fromAddress);
1537 if (!EncodeFieldFromValue(buff, fromAddress)) {
1538 TELEPHONY_LOGE("encode mandatory mms from field error.");
1539 return false;
1540 }
1541
1542 if (!EncodeOctetValueFromMap(buff, MMS_READ_STATUS)) {
1543 TELEPHONY_LOGE("encode mandatory mms read status field error.");
1544 return false;
1545 }
1546 // Optional
1547 EncodeLongIntergerValueFromMap(buff, MMS_DATE);
1548 // MMS_APPLIC_ID unSupported
1549 // MMS_REPLY_APPLIC_ID unSupported
1550 // MMS_AUX_APPLIC_INFO unSupported
1551 return true;
1552 }
1553
1554 /**
1555 * @brief EncodeMmsReadOrigInd
1556 * OMA-TS-MMS_ENC-V1_3-20110913-A section:6.7.2 PDU Read Report
1557 * @param buff
1558 * @return true
1559 * @return false
1560 */
EncodeMmsReadOrigInd(MmsEncodeBuffer & buff)1561 bool MmsHeader::EncodeMmsReadOrigInd(MmsEncodeBuffer &buff)
1562 {
1563 // Mandatory
1564 if (!EncodeTextStringValueFromMap(buff, MMS_MESSAGE_ID)) {
1565 TELEPHONY_LOGE("encode mandatory mms message id field error.");
1566 return false;
1567 }
1568 std::vector<MmsAddress> toAddress;
1569 if (!GetHeaderAllAddressValue(MMS_TO, toAddress)) {
1570 TELEPHONY_LOGE("get mandatory mms to field no set error.");
1571 return false;
1572 }
1573 if (!EncodeMultipleAddressValue(buff, MMS_TO, toAddress)) {
1574 TELEPHONY_LOGE("encode mandatory mms to field error.");
1575 return false;
1576 }
1577
1578 std::vector<MmsAddress> fromAddress;
1579 GetHeaderAllAddressValue(MMS_FROM, fromAddress);
1580 if (!EncodeFieldFromValue(buff, fromAddress)) {
1581 TELEPHONY_LOGE("encode mandatory mms from field error.");
1582 return false;
1583 }
1584
1585 if (!EncodeOctetValueFromMap(buff, MMS_READ_STATUS)) {
1586 TELEPHONY_LOGE("encode mandatory mms read status field error.");
1587 return false;
1588 }
1589 if (!EncodeLongIntergerValueFromMap(buff, MMS_DATE)) {
1590 TELEPHONY_LOGE("encode mandatory mms date field error.");
1591 return false;
1592 }
1593 // Optional
1594 // MMS_APPLIC_ID unSupported
1595 // MMS_REPLY_APPLIC_ID unSupported
1596 // MMS_AUX_APPLIC_INFO unSupported
1597 return true;
1598 }
1599
IsHaveTransactionId(uint8_t messageType)1600 bool MmsHeader::IsHaveTransactionId(uint8_t messageType)
1601 {
1602 if (messageType == MMS_MSGTYPE_SEND_REQ || messageType == MMS_MSGTYPE_SEND_CONF ||
1603 messageType == MMS_MSGTYPE_NOTIFICATION_IND || messageType == MMS_MSGTYPE_NOTIFYRESP_IND ||
1604 messageType == MMS_MSGTYPE_RETRIEVE_CONF || messageType == MMS_MSGTYPE_ACKNOWLEDGE_IND) {
1605 return true;
1606 }
1607 return false;
1608 }
1609
CheckResponseStatus(uint8_t value)1610 bool MmsHeader::CheckResponseStatus(uint8_t value)
1611 {
1612 if (value >= static_cast<uint8_t>(MmsResponseStatus::MMS_OK) &&
1613 value <= static_cast<uint8_t>(MmsResponseStatus::MMS_ERROR_UNSUPPORTED_MESSAGE)) {
1614 return true;
1615 }
1616 if (value >= static_cast<uint8_t>(MmsResponseStatus::MMS_ERROR_TRANSIENT_FAILURE) &&
1617 value <= static_cast<uint8_t>(MmsResponseStatus::MMS_ERROR_TRANSIENT_PARTIAL_SUCCESS)) {
1618 return true;
1619 }
1620 if (value >= static_cast<uint8_t>(MmsResponseStatus::MMS_ERROR_PERMANENT_FAILURE) &&
1621 value <= static_cast<uint8_t>(MmsResponseStatus::MMS_ERROR_PERMANENT_LACK_OF_PREPAID)) {
1622 return true;
1623 }
1624 return false;
1625 }
1626
CheckRetrieveStatus(uint8_t value)1627 bool MmsHeader::CheckRetrieveStatus(uint8_t value)
1628 {
1629 if (value == static_cast<uint8_t>(MmsRetrieveStatus::MMS_RETRIEVE_STATUS_OK)) {
1630 return true;
1631 }
1632 if (value >= static_cast<uint8_t>(MmsRetrieveStatus::MMS_RETRIEVE_STATUS_ERROR_TRANSIENT_FAILURE) &&
1633 value <= static_cast<uint8_t>(MmsRetrieveStatus::MMS_RETRIEVE_STATUS_ERROR_TRANSIENT_NETWORK_PROBLEM)) {
1634 return true;
1635 }
1636 if (value >= static_cast<uint8_t>(MmsRetrieveStatus::MMS_RETRIEVE_STATUS_ERROR_PERMANENT_FAILURE) &&
1637 value <= static_cast<uint8_t>(MmsRetrieveStatus::MMS_RETRIEVE_STATUS_ERROR_PERMANENT_CONTENT_UNSUPPORTED)) {
1638 return true;
1639 }
1640 return false;
1641 }
1642
CheckStoreStatus(uint8_t value)1643 bool MmsHeader::CheckStoreStatus(uint8_t value)
1644 {
1645 if (value == static_cast<uint8_t>(MmsStoreStatus::MMS_STORE_STATUS_SUCCESS)) {
1646 return true;
1647 }
1648 if (value >= static_cast<uint8_t>(MmsStoreStatus::MMS_STORE_STATUS_ERROR_TRANSIENT_FAILURE) &&
1649 value <= static_cast<uint8_t>(MmsStoreStatus::MMS_STORE_STATUS_ERROR_TRANSIENT_NETWORK_PROBLEM)) {
1650 return true;
1651 }
1652 if (value >= static_cast<uint8_t>(MmsStoreStatus::MMS_STORE_STATUS_ERROR_PERMANENT_FAILURE) &&
1653 value <= static_cast<uint8_t>(MmsStoreStatus::MMS_STORE_STATUS_ERROR_PERMANENT_MMBOX_FULL)) {
1654 return true;
1655 }
1656 return false;
1657 }
1658
CheckBooleanValue(uint8_t fieldId,uint8_t value)1659 bool MmsHeader::CheckBooleanValue(uint8_t fieldId, uint8_t value)
1660 {
1661 switch (fieldId) {
1662 case MMS_DELIVERY_REPORT:
1663 case MMS_READ_REPORT:
1664 case MMS_REPORT_ALLOWED:
1665 case MMS_STORE:
1666 case MMS_STORED:
1667 case MMS_TOTALS:
1668 case MMS_QUOTAS:
1669 case MMS_DISTRIBUTION_INDICATOR:
1670 case MMS_DRM_CONTENT:
1671 case MMS_ADAPTATION_ALLOWED:
1672 if (value < static_cast<uint8_t>(MmsBoolType::MMS_YES) ||
1673 value > static_cast<uint8_t>(MmsBoolType::MMS_NO)) {
1674 return false;
1675 }
1676 break;
1677 default:
1678 return false;
1679 }
1680
1681 auto ret = octetValueMap_.emplace(fieldId, value);
1682 return ret.second;
1683 }
1684 } // namespace Telephony
1685 } // namespace OHOS
1686