1 /*
2 * Copyright (C) 2024 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 "request_apdu_build.h"
17 #include "asn1_constants.h"
18 #include "asn1_utils.h"
19
20 namespace OHOS {
21 namespace Telephony {
22 namespace {
23 const uint32_t CLA_STORE_DATA = 0x80;
24 const uint32_t INS_STORE_DATA = 0xE2;
25 const uint32_t P1_STORE_DATA_INTERM = 0x11;
26 const uint32_t P1_STORE_DATA_END = 0x91;
27 }
28
GetCommands()29 std::list<std::unique_ptr<ApduCommand>> RequestApduBuild::GetCommands()
30 {
31 std::lock_guard<std::mutex> lock(mutex_);
32 std::list<std::unique_ptr<ApduCommand>> apduCommandTempLst(std::move(apduCommandLst_));
33 apduCommandLst_.clear();
34 return apduCommandTempLst;
35 }
36
AddApdu(const ApduData & apduData)37 void RequestApduBuild::AddApdu(const ApduData &apduData)
38 {
39 std::unique_ptr<ApduCommand> apduCommand = std::make_unique<ApduCommand>(channelId_, apduData);
40 apduCommandLst_.push_back(std::move(apduCommand));
41 }
42
ConstructApduData(uint32_t packetTag,uint32_t packetIndex,uint32_t packetLen,const std::string & cmdHex,ApduData & apduData)43 void RequestApduBuild::ConstructApduData(uint32_t packetTag, uint32_t packetIndex, uint32_t packetLen,
44 const std::string &cmdHex, ApduData &apduData)
45 {
46 apduData.cla = CLA_STORE_DATA;
47 apduData.ins = INS_STORE_DATA;
48 apduData.p1 = packetTag;
49 apduData.p2 = packetIndex;
50 apduData.p3 = packetLen;
51 apduData.cmdHex = cmdHex;
52 }
53
BuildStoreData(const std::string & cmdHex)54 void RequestApduBuild::BuildStoreData(const std::string &cmdHex)
55 {
56 int32_t cmdLen = MAX_UINT8 * BYTE_TO_HEX_LEN;
57 int32_t startPos = 0;
58 uint32_t totalLen = static_cast<uint32_t>(cmdHex.length() / BYTE_TO_HEX_LEN);
59 uint32_t totalSubCmds = ((totalLen == 0) ? 1 : ((totalLen + MAX_UINT8 - 1) / MAX_UINT8));
60 uint32_t leastLen = totalLen;
61 /* When handling packet fragmentation, if the last packet of data is less than 255 bytes,
62 it requires special handling outside the loop.
63 */
64 std::lock_guard<std::mutex> lock(mutex_);
65 for (uint32_t i = 1; i < totalSubCmds; ++i) {
66 std::string data = cmdHex.substr(startPos, cmdLen);
67 ApduData apduData;
68 ConstructApduData(P1_STORE_DATA_INTERM, i - 1, MAX_UINT8, data, apduData);
69 AddApdu(apduData);
70 startPos += cmdLen;
71 leastLen -= MAX_UINT8;
72 }
73 std::string lastData = cmdHex.substr(startPos);
74 ApduData lastApduData;
75 ConstructApduData(P1_STORE_DATA_END, totalSubCmds - 1, leastLen, lastData, lastApduData);
76 AddApdu(lastApduData);
77 }
78 } // namespace Telephony
79 } // namespace OHOS