1 /*
2 * Copyright (c) 2022 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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16
17 #include "hks_ability.h"
18
19 #include <stddef.h>
20
21 #include "hks_log.h"
22 #include "hks_type.h"
23
24 static struct HksAbility g_abilityList[HKS_ABILITY_MAX_SIZE] = {{0}};
25
RegisterAbility(uint32_t id,void * func)26 int32_t RegisterAbility(uint32_t id, void *func)
27 {
28 for (int i = 0; i < HKS_ABILITY_MAX_SIZE; i++) {
29 if (g_abilityList[i].id == id) {
30 return HKS_ERROR_ALREADY_EXISTS;
31 } else if (g_abilityList[i].id != 0) {
32 continue;
33 }
34 g_abilityList[i].id = id;
35 g_abilityList[i].func = func;
36 HKS_LOG_I("register ability i = %" LOG_PUBLIC "d, id = 0x%" LOG_PUBLIC "x", i, id);
37 return HKS_SUCCESS;
38 }
39 HKS_LOG_E("register failed: exceed max number of abilities, id = 0x%" LOG_PUBLIC "x", id);
40 return HKS_ERROR_BAD_STATE;
41 }
42
GetAbility(uint32_t id)43 void *GetAbility(uint32_t id)
44 {
45 for (int i = 0; i < HKS_ABILITY_MAX_SIZE; i++) {
46 if (g_abilityList[i].id == id) {
47 return g_abilityList[i].func;
48 }
49 }
50 return NULL;
51 }