• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "cf_api.h"
17 
18 #include "securec.h"
19 
20 #include "cf_ability.h"
21 #include "cf_log.h"
22 #include "cf_magic.h"
23 #include "cf_memory.h"
24 #include "cf_object_ability_define.h"
25 #include "cf_result.h"
26 #include "cf_type.h"
27 
28 typedef struct {
29     CfObject object;
30     CfObjectAbilityFunc func;
31     CfBase *base;
32 } CfLifeCtx;
33 
CfLifeGet(const CfObject * object,const CfParamSet * in,CfParamSet ** out)34 static int32_t CfLifeGet(const CfObject *object, const CfParamSet *in, CfParamSet **out)
35 {
36     CF_LOG_I("enter get");
37     if ((object == NULL) || (in == NULL) || (out == NULL)) {
38         CF_LOG_E("input params invalid");
39         return CF_NULL_POINTER;
40     }
41 
42     CfLifeCtx *tmp = (CfLifeCtx *)object;
43     int32_t ret = tmp->func.get(tmp->base, in, out);
44     CF_LOG_I("leave get ret = %d", ret);
45     return ret;
46 }
47 
CfLifeCheck(const CfObject * object,const CfParamSet * in,CfParamSet ** out)48 static int32_t CfLifeCheck(const CfObject *object, const CfParamSet *in, CfParamSet **out)
49 {
50     CF_LOG_I("enter check");
51     if ((object == NULL) || (in == NULL) || (out == NULL)) {
52         CF_LOG_E("input params invalid");
53         return CF_NULL_POINTER;
54     }
55 
56     CfLifeCtx *tmp = (CfLifeCtx *)object;
57     int32_t ret = tmp->func.check(tmp->base, in, out);
58     CF_LOG_I("leave check ret = %d", ret);
59     return ret;
60 }
61 
CfLifeDestroy(CfObject ** object)62 static void CfLifeDestroy(CfObject **object)
63 {
64     CF_LOG_I("enter: destroy object");
65     if ((object == NULL) || (*object == NULL)) {
66         CF_LOG_I("param is null");
67         return;
68     }
69 
70     CfLifeCtx *tmp = (CfLifeCtx *)*object;
71     tmp->func.destroy(&tmp->base);
72     CfFree(tmp);
73     *object = NULL;
74     CF_LOG_I("leave: destroy object");
75 }
76 
CfCreate(CfObjectType objType,const CfEncodingBlob * in,CfObject ** object)77 CF_API_EXPORT int32_t CfCreate(CfObjectType objType, const CfEncodingBlob *in, CfObject **object)
78 {
79     CF_LOG_I("enter: create object [%d]", objType);
80     if ((in == NULL) || (object == NULL)) {
81         CF_LOG_E("input params invalid");
82         return CF_NULL_POINTER;
83     }
84 
85     CfObjectAbilityFunc *func = (CfObjectAbilityFunc *)GetAbility(CF_ABILITY(CF_ABILITY_TYPE_OBJECT, objType));
86     if ((func == NULL) || (func->base.type != CF_MAGIC(CF_MAGIC_TYPE_OBJ_FUNC, objType))) {
87         CF_LOG_E("invalid func type");
88         return CF_INVALID_PARAMS;
89     }
90 
91     CfLifeCtx *tmp = CfMalloc(sizeof(CfLifeCtx));
92     if (tmp == NULL) {
93         CF_LOG_E("malloc ctx failed");
94         return CF_ERR_MALLOC;
95     }
96 
97     int32_t ret = func->create(in, &tmp->base);
98     if (ret != CF_SUCCESS) {
99         CF_LOG_E("create object resource failed, ret = %d", ret);
100         CfFree(tmp);
101         return ret;
102     }
103     (void)memcpy_s(&tmp->func, sizeof(CfObjectAbilityFunc), func, sizeof(CfObjectAbilityFunc));
104 
105     tmp->object.get = CfLifeGet;
106     tmp->object.check = CfLifeCheck;
107     tmp->object.destroy = CfLifeDestroy;
108     *object = &tmp->object;
109 
110     CF_LOG_I("leave: create object success");
111     return CF_SUCCESS;
112 }
113 
114