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     if ((object == NULL) || (in == NULL) || (out == NULL)) {
37         CF_LOG_E("input params invalid");
38         return CF_NULL_POINTER;
39     }
40 
41     CfLifeCtx *tmp = (CfLifeCtx *)object;
42     int32_t ret = tmp->func.get(tmp->base, in, out);
43     return ret;
44 }
45 
CfLifeCheck(const CfObject * object,const CfParamSet * in,CfParamSet ** out)46 static int32_t CfLifeCheck(const CfObject *object, const CfParamSet *in, CfParamSet **out)
47 {
48     if ((object == NULL) || (in == NULL) || (out == NULL)) {
49         CF_LOG_E("input params invalid");
50         return CF_NULL_POINTER;
51     }
52 
53     CfLifeCtx *tmp = (CfLifeCtx *)object;
54     int32_t ret = tmp->func.check(tmp->base, in, out);
55     return ret;
56 }
57 
CfLifeDestroy(CfObject ** object)58 static void CfLifeDestroy(CfObject **object)
59 {
60     if ((object == NULL) || (*object == NULL)) {
61         CF_LOG_I("param is null");
62         return;
63     }
64 
65     CfLifeCtx *tmp = (CfLifeCtx *)*object;
66     tmp->func.destroy(&tmp->base);
67     CfFree(tmp);
68     *object = NULL;
69 }
70 
CfCreate(CfObjectType objType,const CfEncodingBlob * in,CfObject ** object)71 CF_API_EXPORT int32_t CfCreate(CfObjectType objType, const CfEncodingBlob *in, CfObject **object)
72 {
73     CF_LOG_I("enter: create object [%d]", objType);
74     if ((in == NULL) || (object == NULL)) {
75         CF_LOG_E("input params invalid");
76         return CF_NULL_POINTER;
77     }
78 
79     CfObjectAbilityFunc *func = (CfObjectAbilityFunc *)GetAbility(CF_ABILITY(CF_ABILITY_TYPE_OBJECT, objType));
80     if ((func == NULL) || (func->base.type != CF_MAGIC(CF_MAGIC_TYPE_OBJ_FUNC, objType))) {
81         CF_LOG_E("invalid func type");
82         return CF_INVALID_PARAMS;
83     }
84 
85     CfLifeCtx *tmp = CfMalloc(sizeof(CfLifeCtx), 0);
86     if (tmp == NULL) {
87         CF_LOG_E("malloc ctx failed");
88         return CF_ERR_MALLOC;
89     }
90 
91     int32_t ret = func->create(in, &tmp->base);
92     if (ret != CF_SUCCESS) {
93         CF_LOG_E("create object resource failed, ret = %d", ret);
94         CfFree(tmp);
95         return ret;
96     }
97     (void)memcpy_s(&tmp->func, sizeof(CfObjectAbilityFunc), func, sizeof(CfObjectAbilityFunc));
98 
99     tmp->object.get = CfLifeGet;
100     tmp->object.check = CfLifeCheck;
101     tmp->object.destroy = CfLifeDestroy;
102     *object = &tmp->object;
103 
104     return CF_SUCCESS;
105 }
106 
107