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