• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "all_in_one_impl.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 #include <hdf_base.h>
21 
22 #include "defines.h"
23 #include "executor_impl_common.h"
24 #include "iam_logger.h"
25 #include "securec.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "PIN_AUTH_IMPL_A"
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace PinAuth {
33 namespace {
34 constexpr uint32_t ENROLL_PIN = 0;
35 constexpr uint32_t AUTH_PIN = 1;
36 
37 constexpr size_t MAX_SCHEDULE_SIZE = 50;
38 }
39 
AllInOneImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi)40 AllInOneImpl::AllInOneImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi)
41     : pinHdi_(pinHdi),
42       threadPool_("pin_async")
43 {
44     threadPool_.Start(1);
45 }
46 
~AllInOneImpl()47 AllInOneImpl::~AllInOneImpl()
48 {
49     threadPool_.Stop();
50 }
51 
GetExecutorInfo(HdiExecutorInfo & info)52 int32_t AllInOneImpl::GetExecutorInfo(HdiExecutorInfo &info)
53 {
54     IAM_LOGI("start");
55     if (pinHdi_ == nullptr) {
56         IAM_LOGE("pinHdi_ is nullptr");
57         return HDF_FAILURE;
58     }
59     info.sensorId = SENSOR_ID;
60     info.executorMatcher = EXECUTOR_MATCHER;
61     info.executorRole = HdiExecutorRole::ALL_IN_ONE;
62     info.authType = HdiAuthType::PIN;
63     uint32_t eslRet = 0;
64     int32_t result = pinHdi_->GetExecutorInfo(HdiExecutorRole::ALL_IN_ONE, info.publicKey, eslRet,
65         info.maxTemplateAcl);
66     if (result != SUCCESS) {
67         IAM_LOGE("Get all in one ExecutorInfo failed, fail code:%{public}d", result);
68         return HDF_FAILURE;
69     }
70     info.esl = static_cast<HdiExecutorSecureLevel>(eslRet);
71     return HDF_SUCCESS;
72 }
73 
OnRegisterFinish(const std::vector<uint64_t> & templateIdList,const std::vector<uint8_t> & frameworkPublicKey,const std::vector<uint8_t> & extraInfo)74 int32_t AllInOneImpl::OnRegisterFinish(const std::vector<uint64_t> &templateIdList,
75     const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo)
76 {
77     IAM_LOGI("start");
78     static_cast<void>(extraInfo);
79     if (pinHdi_ == nullptr) {
80         IAM_LOGE("pinHdi_ is nullptr");
81         return HDF_FAILURE;
82     }
83     int32_t result = pinHdi_->SetAllInOneFwkParam(templateIdList, frameworkPublicKey);
84     if (result != SUCCESS) {
85         IAM_LOGE("Verify templateData failed");
86         return HDF_FAILURE;
87     }
88 
89     return HDF_SUCCESS;
90 }
91 
SendMessage(uint64_t scheduleId,int32_t srcRole,const std::vector<uint8_t> & msg)92 int32_t AllInOneImpl::SendMessage(uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t> &msg)
93 {
94     static_cast<void>(scheduleId);
95     static_cast<void>(srcRole);
96     static_cast<void>(msg);
97     IAM_LOGI("send message success");
98     return HDF_SUCCESS;
99 }
100 
EnrollInner(uint64_t scheduleId,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj,std::vector<uint8_t> & algoParameter,uint32_t & algoVersion)101 int32_t AllInOneImpl::EnrollInner(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
102     const sptr<HdiIExecutorCallback> &callbackObj, std::vector<uint8_t> &algoParameter, uint32_t &algoVersion)
103 {
104     IAM_LOGI("start");
105     static_cast<void>(extraInfo);
106     if (pinHdi_->GenerateAlgoParameter(algoParameter, algoVersion) != SUCCESS) {
107         IAM_LOGE("Generate algorithm parameter failed");
108         CallError(callbackObj, GENERAL_ERROR);
109         return GENERAL_ERROR;
110     }
111 
112     ScheduleInfo scheduleInfo = {
113         .scheduleId = scheduleId,
114         .commandId = ENROLL_PIN,
115         .callback = callbackObj,
116         .templateId = 0,
117         .algoParameter = algoParameter,
118     };
119     if (!scheduleList_.AddScheduleInfo(scheduleInfo)) {
120         IAM_LOGE("Add scheduleInfo failed");
121         CallError(callbackObj, GENERAL_ERROR);
122         return GENERAL_ERROR;
123     }
124 
125     return HDF_SUCCESS;
126 }
127 
Enroll(uint64_t scheduleId,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj)128 int32_t AllInOneImpl::Enroll(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
129     const sptr<HdiIExecutorCallback> &callbackObj)
130 {
131     IAM_LOGI("start");
132     if (callbackObj == nullptr) {
133         IAM_LOGE("callbackObj is nullptr");
134         return HDF_ERR_INVALID_PARAM;
135     }
136     if (pinHdi_ == nullptr) {
137         IAM_LOGE("pinHdi_ is nullptr");
138         CallError(callbackObj, INVALID_PARAMETERS);
139         return HDF_SUCCESS;
140     }
141     std::vector<uint8_t> algoParameter;
142     uint32_t algoVersion = 0;
143     int32_t result = EnrollInner(scheduleId, extraInfo, callbackObj, algoParameter, algoVersion);
144     if (result != SUCCESS) {
145         IAM_LOGE("EnrollInner failed, fail code : %{public}d", result);
146         return HDF_SUCCESS;
147     }
148 
149     std::vector<uint8_t> challenge;
150     std::string pinComplexityReg = "";
151     result = callbackObj->OnGetData(algoParameter, 0, algoVersion, challenge, pinComplexityReg);
152     if (result != SUCCESS) {
153         IAM_LOGE("Enroll Pin failed, fail code : %{public}d", result);
154         CallError(callbackObj, GENERAL_ERROR);
155         // If the enroll fails, delete scheduleId of scheduleMap
156         scheduleList_.DelScheduleInfo(scheduleId);
157     }
158 
159     return HDF_SUCCESS;
160 }
161 
AuthenticateInner(uint64_t scheduleId,uint64_t templateId,std::vector<uint8_t> & algoParameter,const sptr<HdiIExecutorCallback> & callbackObj,const std::vector<uint8_t> & extraInfo)162 int32_t AllInOneImpl::AuthenticateInner(uint64_t scheduleId, uint64_t templateId, std::vector<uint8_t> &algoParameter,
163     const sptr<HdiIExecutorCallback> &callbackObj, const std::vector<uint8_t> &extraInfo)
164 {
165     IAM_LOGI("start");
166     OHOS::UserIam::PinAuth::PinCredentialInfo infoRet = {};
167     int32_t result = pinHdi_->QueryPinInfo(templateId, infoRet);
168     if (result != SUCCESS) {
169         IAM_LOGE("Get TemplateInfo failed, fail code : %{public}d", result);
170         CallError(callbackObj, result);
171         return GENERAL_ERROR;
172     }
173     if (infoRet.remainTimes == 0 || infoRet.freezingTime > 0) {
174         IAM_LOGE("Pin authentication is now frozen state");
175         CallError(callbackObj, LOCKED);
176         return GENERAL_ERROR;
177     }
178     ScheduleInfo scheduleInfo = {
179         .scheduleId = scheduleId,
180         .commandId = AUTH_PIN,
181         .callback = callbackObj,
182         .templateId = templateId,
183         .algoParameter = algoParameter,
184         .extraInfo = extraInfo,
185     };
186     if (!scheduleList_.AddScheduleInfo(scheduleInfo)) {
187         IAM_LOGE("Add scheduleInfo failed");
188         CallError(callbackObj, GENERAL_ERROR);
189         return GENERAL_ERROR;
190     }
191 
192     return SUCCESS;
193 }
194 
Authenticate(uint64_t scheduleId,const std::vector<uint64_t> & templateIdList,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj)195 int32_t AllInOneImpl::Authenticate(uint64_t scheduleId, const std::vector<uint64_t>& templateIdList,
196     const std::vector<uint8_t> &extraInfo, const sptr<HdiIExecutorCallback> &callbackObj)
197 {
198     IAM_LOGI("start");
199     if (callbackObj == nullptr) {
200         IAM_LOGE("callbackObj is nullptr");
201         return HDF_ERR_INVALID_PARAM;
202     }
203     if (pinHdi_ == nullptr || templateIdList.size() != 1) {
204         IAM_LOGE("pinHdi_ is nullptr or templateIdList size not 1");
205         CallError(callbackObj, INVALID_PARAMETERS);
206         return HDF_SUCCESS;
207     }
208     OHOS::UserIam::PinAuth::PinAlgoParam pinAlgoParam = {};
209     int32_t result = pinHdi_->AllInOneAuth(scheduleId, templateIdList[0], extraInfo, pinAlgoParam);
210     if (result != SUCCESS) {
211         IAM_LOGE("Get algorithm parameter failed, fail code : %{public}d", result);
212         CallError(callbackObj, result);
213         return HDF_SUCCESS;
214     }
215     IAM_LOGI("algorithm parameter len:%{public}zu version:%{public}u",
216         pinAlgoParam.algoParameter.size(), pinAlgoParam.algoVersion);
217     result = AuthenticateInner(scheduleId, templateIdList[0], pinAlgoParam.algoParameter, callbackObj, extraInfo);
218     if (result != SUCCESS) {
219         IAM_LOGE("AuthenticateInner failed, fail code : %{public}d", result);
220         return HDF_SUCCESS;
221     }
222 
223     std::string pinComplexityReg = "";
224     result = callbackObj->OnGetData(pinAlgoParam.algoParameter, pinAlgoParam.subType, pinAlgoParam.algoVersion,
225         pinAlgoParam.challenge, pinComplexityReg);
226     if (result != SUCCESS) {
227         IAM_LOGE("Authenticate Pin failed, fail code : %{public}d", result);
228         CallError(callbackObj, GENERAL_ERROR);
229         // If the authentication fails, delete scheduleId of scheduleMap
230         scheduleList_.DelScheduleInfo(scheduleId);
231     }
232 
233     return HDF_SUCCESS;
234 }
235 
AuthPin(PinAuthParam & pinAuthParam,const std::vector<uint8_t> & extraInfo,std::vector<uint8_t> & resultTlv)236 int32_t AllInOneImpl::AuthPin(PinAuthParam &pinAuthParam,
237     const std::vector<uint8_t> &extraInfo, std::vector<uint8_t> &resultTlv)
238 {
239     if (pinHdi_ == nullptr) {
240         IAM_LOGE("pinHdi_ is nullptr");
241         return HDF_FAILURE;
242     }
243     int32_t result = pinHdi_->AuthPin(pinAuthParam, extraInfo, resultTlv);
244     if (result != SUCCESS) {
245         IAM_LOGE("Auth Pin failed, fail code : %{public}d", result);
246         return result;
247     }
248     threadPool_.AddTask([hdi = pinHdi_, id = pinAuthParam.templateId]() {
249         if (hdi == nullptr) {
250             return;
251         }
252         hdi->WriteAntiBrute(id);
253     });
254     IAM_LOGI("Auth Pin success");
255     return result;
256 }
257 
FillPinEnrollParam(PinEnrollParam & pinEnrollParam,uint64_t subType,const std::vector<uint8_t> & salt,const std::vector<uint8_t> & pinData,uint32_t pinLength)258 int32_t AllInOneImpl::FillPinEnrollParam(PinEnrollParam &pinEnrollParam, uint64_t subType, const std::vector<uint8_t>
259     &salt, const std::vector<uint8_t> &pinData, uint32_t pinLength)
260 {
261     if (pinHdi_ == nullptr) {
262         IAM_LOGE("pinHdi_ is nullptr");
263         return pinHdi_->PinResultToCoAuthResult(RESULT_GENERAL_ERROR);
264     }
265     pinEnrollParam.subType = subType;
266     pinEnrollParam.pinLength = pinLength;
267     if (salt.size() != CONST_SALT_LEN || pinData.size() != CONST_PIN_DATA_LEN) {
268         LOG_ERROR("get bad params!");
269         return pinHdi_->PinResultToCoAuthResult(RESULT_BAD_PARAM);
270     }
271     if (memcpy_s(&(pinEnrollParam.salt[0]), CONST_SALT_LEN, salt.data(),
272         CONST_SALT_LEN) != EOK) {
273         LOG_ERROR("copy salt to pinEnrollParam fail!");
274         return pinHdi_->PinResultToCoAuthResult(RESULT_BAD_COPY);
275     }
276     if (memcpy_s(&(pinEnrollParam.pinData[0]), CONST_PIN_DATA_LEN,  pinData.data(),
277         CONST_PIN_DATA_LEN) != EOK) {
278         LOG_ERROR("copy pinData to pinEnrollParam fail!");
279         return pinHdi_->PinResultToCoAuthResult(RESULT_BAD_COPY);
280     }
281     return SUCCESS;
282 }
283 
FillPinAuthParam(PinAuthParam & pinAuthParam,uint64_t scheduleId,uint64_t templateId,const std::vector<uint8_t> & pinData,uint32_t pinLength)284 int32_t AllInOneImpl::FillPinAuthParam(PinAuthParam &pinAuthParam, uint64_t scheduleId, uint64_t templateId,
285     const std::vector<uint8_t> &pinData, uint32_t pinLength)
286 {
287     if (pinHdi_ == nullptr) {
288         IAM_LOGE("pinHdi_ is nullptr");
289         return pinHdi_->PinResultToCoAuthResult(RESULT_GENERAL_ERROR);
290     }
291     if (pinData.size() != CONST_PIN_DATA_LEN) {
292         LOG_ERROR("bad pinData len!");
293         return pinHdi_->PinResultToCoAuthResult(RESULT_BAD_PARAM);
294     }
295 
296     pinAuthParam.scheduleId = scheduleId;
297     pinAuthParam.templateId = templateId;
298     pinAuthParam.pinLength = pinLength;
299     if (memcpy_s(&(pinAuthParam.pinData[0]), CONST_PIN_DATA_LEN, pinData.data(), pinData.size()) != EOK) {
300         LOG_ERROR("mem copy pinData to pinAuthParam fail!");
301         return pinHdi_->PinResultToCoAuthResult(RESULT_BAD_COPY);
302     }
303     return SUCCESS;
304 }
305 
EnrollPinInner(ScheduleInfo & scheduleInfo,uint64_t authSubType,const std::vector<uint8_t> & pinData,uint32_t pinLength,std::vector<uint8_t> & resultTlv)306 int32_t AllInOneImpl::EnrollPinInner(ScheduleInfo &scheduleInfo, uint64_t authSubType, const std::vector<uint8_t>
307     &pinData, uint32_t pinLength, std::vector<uint8_t> &resultTlv)
308 {
309     if (pinHdi_ == nullptr) {
310         IAM_LOGE("pinHdi_ is nullptr");
311         return HDF_FAILURE;
312     }
313     PinEnrollParam pinEnrollParam = {};
314     pinEnrollParam.scheduleId = scheduleInfo.scheduleId;
315     int32_t result = FillPinEnrollParam(pinEnrollParam, authSubType, scheduleInfo.algoParameter, pinData, pinLength);
316     if (result != SUCCESS) {
317         IAM_LOGE("Fill Enroll Pin Param failed, fail code : %{public}d", result);
318         return result;
319     }
320     result = pinHdi_->EnrollPin(pinEnrollParam, resultTlv);
321     if (result != SUCCESS) {
322         IAM_LOGE("Enroll Pin failed, fail code : %{public}d", result);
323     }
324     return result;
325 }
326 
AuthPinInner(ScheduleInfo & scheduleInfo,uint64_t authSubType,const std::vector<uint8_t> & pinData,uint32_t pinLength,std::vector<uint8_t> & resultTlv)327 int32_t AllInOneImpl::AuthPinInner(ScheduleInfo &scheduleInfo, uint64_t authSubType, const std::vector<uint8_t>
328     &pinData, uint32_t pinLength, std::vector<uint8_t> &resultTlv)
329 {
330     PinAuthParam pinAuthParam = {};
331     int32_t result = FillPinAuthParam(pinAuthParam, scheduleInfo.scheduleId, scheduleInfo.templateId,
332         pinData, pinLength);
333     if (result != SUCCESS) {
334         IAM_LOGE("Fill Auth Pin Param failed, fail code : %{public}d", result);
335         return result;
336     }
337     result = AuthPin(pinAuthParam, scheduleInfo.extraInfo, resultTlv);
338     if (result != SUCCESS) {
339         IAM_LOGE("Auth Pin failed, fail code : %{public}d", result);
340     }
341     return result;
342 }
343 
SetData(uint64_t scheduleId,uint64_t authSubType,const std::vector<uint8_t> & data,uint32_t pinLength,int32_t resultCode)344 int32_t AllInOneImpl::SetData(uint64_t scheduleId, uint64_t authSubType, const std::vector<uint8_t> &data,
345     uint32_t pinLength, int32_t resultCode)
346 {
347     IAM_LOGI("start");
348     std::vector<uint8_t> resultTlv;
349     ScheduleInfo scheduleInfo;
350     if (!scheduleList_.GetAndDelScheduleInfo(scheduleId, scheduleInfo)) {
351         IAM_LOGE("Get ScheduleInfo failed");
352         return HDF_FAILURE;
353     }
354     if (resultCode != SUCCESS) {
355         IAM_LOGE("SetData failed, resultCode is %{public}d", resultCode);
356         CallError(scheduleInfo.callback, resultCode);
357         return HDF_SUCCESS;
358     }
359     int32_t result = GENERAL_ERROR;
360     switch (scheduleInfo.commandId) {
361         case ENROLL_PIN:
362             result = EnrollPinInner(scheduleInfo, authSubType, data, pinLength, resultTlv);
363             break;
364         case AUTH_PIN:
365             result = AuthPinInner(scheduleInfo, authSubType, data, pinLength, resultTlv);
366             break;
367         default:
368             IAM_LOGE("Error commandId");
369     }
370 
371     if (scheduleInfo.callback->OnResult(result, resultTlv) != SUCCESS) {
372         IAM_LOGE("callback OnResult failed");
373     }
374     return HDF_SUCCESS;
375 }
376 
Delete(uint64_t templateId)377 int32_t AllInOneImpl::Delete(uint64_t templateId)
378 {
379     IAM_LOGI("start");
380     if (pinHdi_ == nullptr) {
381         IAM_LOGE("pinHdi_ is nullptr");
382         return HDF_FAILURE;
383     }
384     int32_t result = pinHdi_->DeleteTemplate(templateId);
385     if (result != SUCCESS) {
386         IAM_LOGE("Verify templateData failed, fail code : %{public}d", result);
387         return HDF_FAILURE;
388     }
389 
390     return HDF_SUCCESS;
391 }
392 
Cancel(uint64_t scheduleId)393 int32_t AllInOneImpl::Cancel(uint64_t scheduleId)
394 {
395     IAM_LOGI("start");
396     ScheduleInfo scheduleInfo;
397     if (!scheduleList_.GetAndDelScheduleInfo(scheduleId, scheduleInfo)) {
398         IAM_LOGE("scheduleId %{public}x is not found", (uint16_t)scheduleId);
399         return HDF_FAILURE;
400     }
401     CallError(scheduleInfo.callback, CANCELED);
402     return HDF_SUCCESS;
403 }
404 
GetProperty(const std::vector<uint64_t> & templateIdList,const std::vector<int32_t> & propertyTypes,HdiProperty & property)405 int32_t AllInOneImpl::GetProperty(
406     const std::vector<uint64_t> &templateIdList, const std::vector<int32_t> &propertyTypes, HdiProperty &property)
407 {
408     IAM_LOGI("start");
409     if (pinHdi_ == nullptr) {
410         IAM_LOGE("pinHdi_ is nullptr");
411         return HDF_FAILURE;
412     }
413 
414     if (templateIdList.size() == 0) {
415         IAM_LOGE("templateIdList size is 0");
416         return HDF_ERR_INVALID_PARAM;
417     }
418 
419     uint64_t templateId = templateIdList[0];
420     OHOS::UserIam::PinAuth::PinCredentialInfo infoRet = {};
421     int32_t result = pinHdi_->QueryPinInfo(templateId, infoRet);
422     if (result != SUCCESS) {
423         IAM_LOGE("Get TemplateInfo failed, fail code : %{public}d", result);
424         return HDF_FAILURE;
425     }
426 
427     property.authSubType = infoRet.subType;
428     property.remainAttempts = infoRet.remainTimes;
429     property.lockoutDuration = infoRet.freezingTime;
430     property.nextFailLockoutDuration = infoRet.nextFailLockoutDuration;
431     property.credentialLength = infoRet.credentialLength;
432     return HDF_SUCCESS;
433 }
434 
SendCommand(int32_t commandId,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj)435 int32_t AllInOneImpl::SendCommand(int32_t commandId, const std::vector<uint8_t> &extraInfo,
436     const sptr<HdiIExecutorCallback> &callbackObj)
437 {
438     if (callbackObj == nullptr) {
439         IAM_LOGE("callbackObj is nullptr");
440         return HDF_ERR_INVALID_PARAM;
441     }
442     if (pinHdi_ == nullptr) {
443         IAM_LOGE("pinHdi_ is nullptr");
444         return HDF_FAILURE;
445     }
446     int32_t result = GENERAL_ERROR;
447     switch (commandId) {
448         case RESTART_LOCKOUT_DURATION:
449             result = pinHdi_->RestartLockoutDuration(extraInfo);
450             IAM_LOGI("restart lockout duration, result is %{public}d", result);
451             break;
452         default:
453             result = pinHdi_->PinResultToCoAuthResult(RESULT_OPERATION_NOT_SUPPORT);
454             IAM_LOGD("not support CommandId : %{public}d", commandId);
455         }
456     if (callbackObj->OnResult(result, {}) != HDF_SUCCESS) {
457         IAM_LOGE("callback result is %{public}d", result);
458         return HDF_FAILURE;
459     }
460     return HDF_SUCCESS;
461 }
462 
Abandon(uint64_t scheduleId,uint64_t templateId,const std::vector<uint8_t> & extraInfo,const sptr<HdiIExecutorCallback> & callbackObj)463 int32_t AllInOneImpl::Abandon(uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &extraInfo,
464     const sptr<HdiIExecutorCallback> &callbackObj)
465 {
466     IAM_LOGI("start");
467     if (callbackObj == nullptr) {
468         IAM_LOGE("callbackObj is nullptr");
469         return HDF_ERR_INVALID_PARAM;
470     }
471     if (pinHdi_ == nullptr) {
472         IAM_LOGE("pinHdi_ is nullptr");
473         CallError(callbackObj, INVALID_PARAMETERS);
474         return HDF_SUCCESS;
475     }
476 
477     std::vector<uint8_t> resultTlv;
478     int32_t result = pinHdi_->Abandon(scheduleId, templateId, extraInfo, resultTlv);
479     if (result != SUCCESS) {
480         IAM_LOGE("Abandon Pin failed, fail code : %{public}d", result);
481         CallError(callbackObj, GENERAL_ERROR);
482     }
483 
484     if (callbackObj->OnResult(result, resultTlv) != SUCCESS) {
485         IAM_LOGE("callback OnResult failed");
486     }
487     return HDF_SUCCESS;
488 }
489 
AddScheduleInfo(const ScheduleInfo & scheduleInfo)490 bool AllInOneImpl::ScheduleList::AddScheduleInfo(const ScheduleInfo &scheduleInfo)
491 {
492     IAM_LOGI("start");
493     if (scheduleInfo.callback == nullptr) {
494         IAM_LOGE("callback is nullptr");
495         return false;
496     }
497 
498     std::optional<ScheduleInfo> optScheduleInfo = std::nullopt;
499     {
500         std::lock_guard<std::mutex> guard(mutex_);
501         for (auto iter = scheduleInfoList_.begin(); iter != scheduleInfoList_.end(); ++iter) {
502             if ((*iter).scheduleId == scheduleInfo.scheduleId) {
503                 IAM_LOGE("scheduleId %{public}x already exist", (uint16_t)(scheduleInfo.scheduleId));
504                 return false;
505             }
506         }
507 
508         if (scheduleInfoList_.size() >= MAX_SCHEDULE_SIZE) {
509             optScheduleInfo = scheduleInfoList_.front();
510             scheduleInfoList_.pop_front();
511         }
512         scheduleInfoList_.emplace_back(scheduleInfo);
513     }
514 
515     if (optScheduleInfo.has_value()) {
516         IAM_LOGE("scheduleId %{public}x force stop", (uint16_t)(optScheduleInfo.value().scheduleId));
517         CallError(optScheduleInfo.value().callback, GENERAL_ERROR);
518     }
519     return true;
520 }
521 
GetAndDelScheduleInfo(uint64_t scheduleId,ScheduleInfo & scheduleInfo)522 bool AllInOneImpl::ScheduleList::GetAndDelScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo)
523 {
524     IAM_LOGI("start");
525     std::lock_guard<std::mutex> guard(mutex_);
526     for (auto iter = scheduleInfoList_.begin(); iter != scheduleInfoList_.end(); ++iter) {
527         if ((*iter).scheduleId == scheduleId) {
528             scheduleInfo = (*iter);
529             scheduleInfoList_.erase(iter);
530             return true;
531         }
532     }
533 
534     IAM_LOGE("Get scheduleId not exist");
535     return false;
536 }
537 
DelScheduleInfo(uint64_t scheduleId)538 void AllInOneImpl::ScheduleList::DelScheduleInfo(uint64_t scheduleId)
539 {
540     IAM_LOGI("start");
541     std::lock_guard<std::mutex> guard(mutex_);
542     for (auto iter = scheduleInfoList_.begin(); iter != scheduleInfoList_.end(); ++iter) {
543         if ((*iter).scheduleId == scheduleId) {
544             scheduleInfoList_.erase(iter);
545             return;
546         }
547     }
548     IAM_LOGE("Delete scheduleId not exist");
549 }
550 } // PinAuth
551 } // HDI
552 } // OHOS