• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "esim_controller.h"
17 
18 #include <algorithm>
19 #include <dlfcn.h>
20 #include <thread>
21 
22 #include "ffrt.h"
23 #include "telephony_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 
28 static const std::string CHECK_CMD_HEAD = "D0";
29 static const std::string CHECK_CMD_TAG = "81";
30 static const std::string CHECK_GET_INPUT_TAG = "23";
31 static const std::string VERIFY_BIND_START_DATA = "0456657269667942696E645374617274";
32 static const std::string VERIFY_BIND_END_DATA = "0456657269667942696E64456E64";
33 static const std::string ESIM_CA_LIBPATH = "libesim_ca.z.so";
34 
35 constexpr size_t CHECK_CMD_HEAD_START = 0;
36 constexpr size_t CHECK_CMD_TAG_START = 4;
37 constexpr size_t CHECK_GET_INPUT_TAG_START = 10;
38 constexpr size_t GET_INPUT_DATA_START = 26;
39 constexpr size_t COMPARE_EQUAL_VALUE = 0;
40 
EsimController()41 EsimController::EsimController() {}
42 
~EsimController()43 EsimController::~EsimController() {}
44 
ChecIsVerifyBindCommand(const std::string & cmdData)45 bool EsimController::ChecIsVerifyBindCommand(const std::string &cmdData)
46 {
47     std::string checkCmdData = cmdData;
48     std::transform(checkCmdData.begin(), checkCmdData.end(), checkCmdData.begin(), ::toupper);
49     if (checkCmdData.compare(CHECK_CMD_HEAD_START, CHECK_CMD_HEAD.length(),
50         CHECK_CMD_HEAD) != COMPARE_EQUAL_VALUE) {
51         return false;
52     }
53     if (checkCmdData.compare(CHECK_CMD_TAG_START, CHECK_CMD_TAG.length(),
54         CHECK_CMD_TAG) != COMPARE_EQUAL_VALUE) {
55         return false;
56     }
57     if (checkCmdData.compare(CHECK_GET_INPUT_TAG_START, CHECK_GET_INPUT_TAG.length(),
58         CHECK_GET_INPUT_TAG) != COMPARE_EQUAL_VALUE) {
59         return false;
60     }
61     if (checkCmdData.compare(GET_INPUT_DATA_START, VERIFY_BIND_START_DATA.length(),
62         VERIFY_BIND_START_DATA) == COMPARE_EQUAL_VALUE) {
63         return true;
64     }
65     return (checkCmdData.compare(GET_INPUT_DATA_START, VERIFY_BIND_END_DATA.length(),
66         VERIFY_BIND_END_DATA) == COMPARE_EQUAL_VALUE);
67 }
68 
ProcessCommandMessage(int slotId,const std::string & cmdData)69 void EsimController::ProcessCommandMessage(int slotId, const std::string &cmdData)
70 {
71     TELEPHONY_LOGI("EsimController:Start process verify bind message.");
72     ffrt::submit([=]() {
73         this->ProcessCommandByCa(slotId, cmdData);
74     });
75 }
76 
ProcessCommandByCa(int slotId,const std::string & cmdData)77 void EsimController::ProcessCommandByCa(int slotId, const std::string &cmdData)
78 {
79     std::lock_guard<std::mutex> locker(caMutex_);
80     void *handler = dlopen(ESIM_CA_LIBPATH.c_str(), RTLD_LAZY);
81     if (handler == NULL) {
82         TELEPHONY_LOGE("open lib: %{public}s failed", ESIM_CA_LIBPATH.c_str());
83         return;
84     }
85 
86     VerifyBind func = (VerifyBind)dlsym(handler, "CAEsimStartEuiccCheckBinding");
87     if (func == NULL) {
88         TELEPHONY_LOGE("dlsym CAEsimStartEuiccCheckBinding failed, error:%{public}s", dlerror());
89     } else {
90         func(slotId, cmdData.c_str(), cmdData.length());
91     }
92     dlclose(handler);
93 }
94 } // namespace Telephony
95 } // namespace OHOS
96