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 "napi_mms_pdu.h"
17
18 #include "ability.h"
19 #include "datashare_helper.h"
20 #include "datashare_predicates.h"
21 #include "telephony_log_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 const std::string SMS_PROFILE_MMS_PDU_URI = "datashare:///com.ohos.smsmmsability/sms_mms/mms_pdu";
26 static constexpr const char *PDU_CONTENT = "pdu_content";
27 static constexpr const char *ID = "id";
28 static constexpr uint8_t SLIDE_STEP = 2;
29 static constexpr uint8_t HEX_VALUE_F0 = 0xF0;
30 static constexpr uint8_t HEX_VALUE_0F = 0x0F;
31
DeleteMmsPdu(NapiMmsPduHelper & pduHelper)32 void NAPIMmsPdu::DeleteMmsPdu(NapiMmsPduHelper &pduHelper)
33 {
34 if (GetMmsPdu(pduHelper).empty()) {
35 TELEPHONY_LOGE("mmsPdu_ is nullptr");
36 return;
37 }
38 std::shared_ptr<DataShare::DataShareHelper> dbHelper = pduHelper.GetDataAbilityHelper();
39 if (dbHelper == nullptr) {
40 TELEPHONY_LOGE("dbHelper is nullptr");
41 return;
42 }
43
44 Uri uri(SMS_PROFILE_MMS_PDU_URI);
45
46 DataShare::DataSharePredicates predicates;
47 predicates.EqualTo(ID, pduHelper.GetDbUrl());
48 int32_t result = dbHelper->Delete(uri, predicates);
49 dbHelper->Release();
50 mmsPdu_ = "";
51 TELEPHONY_LOGI("result:%{public}d", result);
52 }
53
InsertMmsPdu(NapiMmsPduHelper & pduHelper,const std::string & mmsPdu)54 bool NAPIMmsPdu::InsertMmsPdu(NapiMmsPduHelper &pduHelper, const std::string &mmsPdu)
55 {
56 std::shared_ptr<DataShare::DataShareHelper> dbHelper = pduHelper.GetDataAbilityHelper();
57 if (dbHelper == nullptr) {
58 TELEPHONY_LOGE("dbHelper is nullptr");
59 return false;
60 }
61 std::string targetMmsPdu;
62 for (size_t i = 0; i < mmsPdu.size(); i++) {
63 targetMmsPdu += static_cast<char>((mmsPdu[i] & 0x0F) | 0xF0);
64 targetMmsPdu += static_cast<char>((mmsPdu[i] & 0xF0) | 0x0F);
65 }
66 Uri uri(SMS_PROFILE_MMS_PDU_URI);
67 DataShare::DataShareValuesBucket bucket;
68 bucket.Put(PDU_CONTENT, targetMmsPdu);
69 int32_t result = dbHelper->Insert(uri, bucket);
70 dbHelper->Release();
71 std::string dbUrl = std::to_string(result);
72 TELEPHONY_LOGI("insert db, dbUrl:%{public}s,pduLen:%{public}d,targetPduLen:%{public}d", dbUrl.c_str(),
73 static_cast<uint32_t>(mmsPdu.size()), static_cast<uint32_t>(targetMmsPdu.size()));
74 pduHelper.SetDbUrl(dbUrl);
75 pduHelper.NotifyAll();
76 return result >= 0 ? true : false;
77 }
78
GetMmsPdu(NapiMmsPduHelper & pduHelper)79 std::string NAPIMmsPdu::GetMmsPdu(NapiMmsPduHelper &pduHelper)
80 {
81 if (!QueryMmsPdu(pduHelper)) {
82 return "";
83 }
84 return mmsPdu_;
85 }
86
SetMmsPdu(const std::string & mmsPdu)87 void NAPIMmsPdu::SetMmsPdu(const std::string &mmsPdu)
88 {
89 mmsPdu_ = mmsPdu;
90 }
91
QueryMmsPdu(NapiMmsPduHelper & pduHelper)92 bool NAPIMmsPdu::QueryMmsPdu(NapiMmsPduHelper &pduHelper)
93 {
94 std::shared_ptr<DataShare::DataShareHelper> dbHelper = pduHelper.GetDataAbilityHelper();
95 if (dbHelper == nullptr) {
96 TELEPHONY_LOGE("dbHelper is nullptr");
97 return false;
98 }
99 Uri uri(SMS_PROFILE_MMS_PDU_URI);
100 std::vector<std::string> colume;
101 DataShare::DataSharePredicates predicates;
102 predicates.EqualTo(ID, pduHelper.GetDbUrl());
103 auto resultSet = dbHelper->Query(uri, predicates, colume);
104 if (resultSet == nullptr) {
105 TELEPHONY_LOGE("resultSet is nullptr");
106 dbHelper->Release();
107 return false;
108 }
109 int count;
110 resultSet->GetRowCount(count);
111 if (count <= 0) {
112 TELEPHONY_LOGE("pdu count: %{public}d error", count);
113 resultSet->Close();
114 dbHelper->Release();
115 return false;
116 }
117 int columnIndex;
118 std::vector<uint8_t> blobValue;
119 for (int row = 0; row < count; row++) {
120 resultSet->GoToRow(row);
121 resultSet->GetColumnIndex(PDU_CONTENT, columnIndex);
122 resultSet->GetBlob(columnIndex, blobValue);
123 }
124 resultSet->Close();
125 dbHelper->Release();
126 std::string mmsPdu;
127 char pduChar = 0x00;
128 for (size_t i = 0; i + 1 < blobValue.size(); i = i + SLIDE_STEP) {
129 pduChar = (blobValue[i] & HEX_VALUE_0F) | (blobValue[i + 1] & HEX_VALUE_F0);
130 mmsPdu += static_cast<char>(pduChar);
131 }
132 TELEPHONY_LOGI("mmsPdu size:%{public}d", static_cast<uint32_t>(mmsPdu.size()));
133 SetMmsPdu(mmsPdu);
134 return true;
135 }
136 } // namespace Telephony
137 } // namespace OHOS