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 "hdi_message_callback_service.h"
17 
18 #include <mutex>
19 
20 #include "context_pool.h"
21 #include "attributes.h"
22 #include "iam_logger.h"
23 #include "iam_check.h"
24 #include "hdi_wrapper.h"
25 
26 #define LOG_TAG "USER_AUTH_SA"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
GetInstance()31 sptr<HdiMessageCallbackService> HdiMessageCallbackService::GetInstance()
32 {
33     static sptr<HdiMessageCallbackService> instance = new HdiMessageCallbackService();
34     IF_FALSE_LOGE_AND_RETURN_VAL(instance != nullptr, nullptr);
35     return instance;
36 }
37 
OnHdiConnect()38 void HdiMessageCallbackService::OnHdiConnect()
39 {
40     auto hdi = HdiWrapper::GetHdiInstance();
41     IF_FALSE_LOGE_AND_RETURN(hdi != nullptr);
42 
43     int32_t ret = hdi->RegisterMessageCallback(GetInstance());
44     IF_FALSE_LOGE_AND_RETURN(ret == HDF_SUCCESS);
45     IAM_LOGI("success");
46 }
47 
OnMessage(uint64_t scheduleId,int32_t destRole,const std::vector<uint8_t> & msg)48 int32_t HdiMessageCallbackService::OnMessage(uint64_t scheduleId, int32_t destRole, const std::vector<uint8_t>& msg)
49 {
50     std::shared_ptr<ScheduleNode> scheduleNode = ContextPool::Instance().SelectScheduleNodeByScheduleId(scheduleId);
51     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleNode != nullptr, HDF_FAILURE);
52 
53     Attributes attr;
54     bool roleRet = attr.SetInt32Value(Attributes::ATTR_SRC_ROLE, SCHEDULER);
55     IF_FALSE_LOGE_AND_RETURN_VAL(roleRet, HDF_FAILURE);
56     bool setExtraInfoRet = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, msg);
57     IF_FALSE_LOGE_AND_RETURN_VAL(setExtraInfoRet, HDF_FAILURE);
58 
59     bool ret = scheduleNode->SendMessage(static_cast<ExecutorRole>(destRole), attr.Serialize());
60     IF_FALSE_LOGE_AND_RETURN_VAL(ret == true, HDF_FAILURE);
61 
62     return HDF_SUCCESS;
63 }
64 } // namespace UserAuth
65 } // namespace UserIam
66 } // namespace OHOS
67