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