1 /*
2 * Copyright (c) 2020 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 #include "service_impl.h"
16 #include <string.h>
17 #include "memory_adapter.h"
18 #include "thread_adapter.h"
19 #include "time_adapter.h"
20 #include "feature_impl.h"
21 #include "service_registry.h"
22
23 static const char *GetFeatureName(const FeatureImpl *featureImpl);
24
SAMGR_CreateServiceImpl(Service * service,uint8 step)25 ServiceImpl *SAMGR_CreateServiceImpl(Service *service, uint8 step)
26 {
27 ServiceImpl *impl = (ServiceImpl *)SAMGR_Malloc(sizeof(ServiceImpl));
28 if (impl == NULL) {
29 return NULL;
30 }
31 impl->service = service;
32 impl->defaultApi = NULL;
33 impl->taskPool = NULL;
34 impl->features = VECTOR_Make((VECTOR_Key)GetFeatureName, (VECTOR_Compare)strcmp);
35 impl->serviceId = INVALID_INDEX;
36 impl->inited = SVC_INIT;
37 impl->ops.abnormal = 0;
38 impl->ops.messages = 0;
39 impl->ops.step = step;
40 impl->ops.timestamp = (uint32)SAMGR_GetProcessTime();
41 return impl;
42 }
43
DEFAULT_AddFeature(ServiceImpl * serviceImpl,Feature * feature)44 int16 DEFAULT_AddFeature(ServiceImpl *serviceImpl, Feature *feature)
45 {
46 if (serviceImpl == NULL || feature == NULL) {
47 return INVALID_INDEX;
48 }
49
50 if (VECTOR_Num(&(serviceImpl->features)) >= MAX_FEATURE_NUM) {
51 return INVALID_INDEX;
52 }
53
54 FeatureImpl *impl = FEATURE_CreateInstance(feature);
55 if (impl == NULL) {
56 return INVALID_INDEX;
57 }
58
59 int16 featureId = VECTOR_Add(&(serviceImpl->features), impl);
60 if (featureId == INVALID_INDEX) {
61 SAMGR_Free(impl);
62 }
63 return featureId;
64 }
65
DEFAULT_Initialize(ServiceImpl * impl)66 void DEFAULT_Initialize(ServiceImpl *impl)
67 {
68 if (impl == NULL) {
69 return;
70 }
71 Identity id = {impl->serviceId, INVALID_INDEX, (impl->taskPool != NULL) ? impl->taskPool->queueId : NULL};
72
73 impl->service->Initialize(impl->service, id);
74 const char *serviceName = impl->service->GetName(impl->service);
75 SAMGR_RegisterServiceApi(serviceName, NULL, &id, impl->defaultApi);
76
77 int16 size = VECTOR_Size(&impl->features);
78 int16 i;
79 for (i = 0; i < size; ++i) {
80 FeatureImpl *feature = (FeatureImpl *)VECTOR_At(&(impl->features), i);
81 if (feature == NULL) {
82 continue;
83 }
84 id.featureId = i;
85 feature->feature->OnInitialize(feature->feature, impl->service, id);
86 SAMGR_RegisterServiceApi(serviceName, feature->feature->GetName(feature->feature), &id, feature->iUnknown);
87 }
88 }
89
DEFAULT_MessageHandle(ServiceImpl * serviceImpl,const Identity * identity,Request * msg)90 void DEFAULT_MessageHandle(ServiceImpl *serviceImpl, const Identity *identity, Request *msg)
91 {
92 if (serviceImpl->serviceId != identity->serviceId) {
93 return;
94 }
95
96 if (identity->featureId < 0) {
97 if (serviceImpl->service->MessageHandle != NULL) {
98 serviceImpl->service->MessageHandle(serviceImpl->service, msg);
99 }
100 return;
101 }
102
103 if (VECTOR_Size(&serviceImpl->features) <= identity->featureId) {
104 return;
105 }
106
107 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_At(&(serviceImpl->features), identity->featureId);
108 if (featureImpl == NULL) {
109 return;
110 }
111 featureImpl->feature->OnMessage(featureImpl->feature, msg);
112 }
113
DEFAULT_StopService(ServiceImpl * service)114 void DEFAULT_StopService(ServiceImpl *service)
115 {
116 if (service == NULL) {
117 return;
118 }
119
120 Identity id = {service->serviceId, INVALID_INDEX, (service->taskPool != NULL) ? service->taskPool->queueId : NULL};
121 int16 size = VECTOR_Size(&service->features);
122 int16 i;
123 for (i = 0; i < size; ++i) {
124 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_At(&(service->features), i);
125 if (featureImpl == NULL) {
126 continue;
127 }
128 id.featureId = i;
129 featureImpl->feature->OnStop(featureImpl->feature, id);
130 }
131 service->inited = SVC_INIT;
132 }
133
DEFAULT_GetFeature(ServiceImpl * serviceImpl,const char * featureName)134 FeatureImpl *DEFAULT_GetFeature(ServiceImpl *serviceImpl, const char *featureName)
135 {
136 if (serviceImpl == NULL || featureName == NULL) {
137 return NULL;
138 }
139
140 short pos = VECTOR_FindByKey(&(serviceImpl->features), (void *)featureName);
141 return (FeatureImpl *)VECTOR_At(&(serviceImpl->features), pos);
142 }
143
DEFAULT_GetFeatureId(ServiceImpl * serviceImpl,const char * feature)144 Identity DEFAULT_GetFeatureId(ServiceImpl *serviceImpl, const char *feature)
145 {
146 Identity identity = {INVALID_INDEX, INVALID_INDEX, NULL};
147 if (serviceImpl == NULL) {
148 return identity;
149 }
150
151 identity.serviceId = serviceImpl->serviceId;
152 if (serviceImpl->taskPool != NULL) {
153 identity.queueId = serviceImpl->taskPool->queueId;
154 }
155
156 int16 pos = VECTOR_FindByKey(&(serviceImpl->features), (void *)feature);
157 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_At(&(serviceImpl->features), pos);
158 if (featureImpl == NULL) {
159 return identity;
160 }
161 identity.featureId = pos;
162 return identity;
163 }
164
DEFAULT_DeleteFeature(ServiceImpl * serviceImpl,const char * featureName)165 Feature *DEFAULT_DeleteFeature(ServiceImpl *serviceImpl, const char *featureName)
166 {
167 if (serviceImpl == NULL || featureName == NULL) {
168 return NULL;
169 }
170
171 int16 pos = VECTOR_FindByKey(&(serviceImpl->features), (void *)featureName);
172 if (pos < 0 || !SAMGR_IsNoInterface((FeatureImpl *)VECTOR_At(&serviceImpl->features, pos))) {
173 return NULL;
174 }
175
176 FeatureImpl *featureImpl = (FeatureImpl *)VECTOR_Swap(&(serviceImpl->features), pos, NULL);
177 if (featureImpl == NULL) {
178 return NULL;
179 }
180
181 Identity id = {serviceImpl->serviceId, INVALID_INDEX, NULL};
182 featureImpl->feature->OnStop(featureImpl->feature, id);
183
184 Feature *feature = featureImpl->feature;
185 SAMGR_Free(featureImpl);
186 return feature;
187 }
188
GetFeatureName(const FeatureImpl * featureImpl)189 static const char *GetFeatureName(const FeatureImpl *featureImpl)
190 {
191 if (featureImpl == NULL) {
192 return NULL;
193 }
194 return featureImpl->feature->GetName(featureImpl->feature);
195 }
196