1 /*
2 * Copyright (c) 2020-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 <stdint.h>
17 #include <securec.h>
18 #include <ohos_init.h>
19 #include <cmsis_os.h>
20 #include <samgr_maintenance.h>
21 #include "service.h"
22 #include "feature.h"
23 #include "samgr_lite.h"
24
25 #define MAINTEN_SERVICE1 "mntn_s1"
26 #define MAINTEN_SERVICE2 "mntn_s2"
27 #define MAINTEN_SERVICE3 "mntn_s3"
28 #define MAINTEN_SERVICE4 "mntn_s4"
29 #define MAINTEN_FEATURE1 "mntn_f1"
30 #define MAINTEN_FEATURE2 "mntn_f2"
31
32 static const char *GetName(Service *service);
33 static BOOL Initialize(Service *service, Identity identity);
34 static BOOL MessageHandle(Service *service, Request *msg);
35 static TaskConfig GetTaskConfig(Service *service);
36 static const char *FEATURE_GetName(Feature *feature);
37 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
38 static void FEATURE_OnStop(Feature *feature, Identity identity);
39 static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
40
41 Service g_maintenExample1 = {GetName, Initialize, MessageHandle, GetTaskConfig};
42 Service g_maintenExample2 = {GetName, Initialize, MessageHandle, GetTaskConfig};
43 Service g_maintenExample3 = {GetName, Initialize, MessageHandle, GetTaskConfig};
44 Service g_maintenExample4 = {GetName, Initialize, MessageHandle, GetTaskConfig};
45 Feature g_maintenFeature1 = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage};
46 Feature g_maintenFeature2 = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage};
47 IUnknownEntry g_demoApi = {DEFAULT_IUNKNOWN_ENTRY_BEGIN, DEFAULT_IUNKNOWN_ENTRY_END};
48
FEATURE_GetName(Feature * feature)49 static const char *FEATURE_GetName(Feature *feature)
50 {
51 if (feature == &g_maintenFeature1) {
52 return MAINTEN_FEATURE1;
53 }
54 if (feature == &g_maintenFeature2) {
55 return MAINTEN_FEATURE2;
56 }
57 return NULL;
58 }
59
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)60 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
61 {
62 (void)feature;
63 (void)parent;
64 (void)identity;
65 }
66
FEATURE_OnStop(Feature * feature,Identity identity)67 static void FEATURE_OnStop(Feature *feature, Identity identity)
68 {
69 (void)feature;
70 (void)identity;
71 }
72
FEATURE_OnMessage(Feature * feature,Request * request)73 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
74 {
75 (void)feature;
76 (void)request;
77 return FALSE;
78 }
79
GetName(Service * service)80 static const char *GetName(Service *service)
81 {
82 if (service == &g_maintenExample1) {
83 return MAINTEN_SERVICE1;
84 }
85 if (service == &g_maintenExample2) {
86 return MAINTEN_SERVICE2;
87 }
88 if (service == &g_maintenExample3) {
89 return MAINTEN_SERVICE3;
90 }
91 if (service == &g_maintenExample4) {
92 return MAINTEN_SERVICE4;
93 }
94 return NULL;
95 }
96
Initialize(Service * service,Identity identity)97 static BOOL Initialize(Service *service, Identity identity)
98 {
99 (void)identity;
100 (void)service;
101 return TRUE;
102 }
103
MessageHandle(Service * service,Request * msg)104 static BOOL MessageHandle(Service *service, Request *msg)
105 {
106 (void)service;
107 (void)msg;
108 return FALSE;
109 }
110
GetTaskConfig(Service * service)111 static TaskConfig GetTaskConfig(Service *service)
112 {
113 (void)service;
114 TaskConfig config = {LEVEL_HIGH, PRI_NORMAL,
115 0x800, 16, SHARED_TASK};
116 if (service == &g_maintenExample4) {
117 config.priority = PRI_ABOVE_NORMAL;
118 config.stackSize = 0x400;
119 }
120 return config;
121 }
122
SInit(Service * service)123 static void SInit(Service *service)
124 {
125 if (service == &g_maintenExample4) {
126 printf("[Maintenance Test][Before App Service Init]Print Uninitialized App Service\n");
127 SAMGR_PrintServices();
128 }
129 SAMGR_GetInstance()->RegisterService(service);
130 }
131
FInit(Feature * feature)132 static void FInit(Feature *feature)
133 {
134 if (feature == &g_maintenFeature1) {
135 SAMGR_GetInstance()->RegisterFeature(MAINTEN_SERVICE1, feature);
136 SAMGR_GetInstance()->RegisterFeatureApi(MAINTEN_SERVICE1, MAINTEN_FEATURE1, GET_IUNKNOWN(g_demoApi));
137 }
138 if (feature == &g_maintenFeature2) {
139 SAMGR_GetInstance()->RegisterFeature(MAINTEN_SERVICE2, feature);
140 SAMGR_GetInstance()->RegisterDefaultFeatureApi(MAINTEN_SERVICE2, GET_IUNKNOWN(g_demoApi));
141 }
142 }
143
S1Init(void)144 static void S1Init(void)
145 {
146 SInit(&g_maintenExample1);
147 }
148
S2Init(void)149 static void S2Init(void)
150 {
151 SInit(&g_maintenExample2);
152 }
153
S3Init(void)154 static void S3Init(void)
155 {
156 SInit(&g_maintenExample3);
157 }
158
S4Init(void)159 static void S4Init(void)
160 {
161 SInit(&g_maintenExample4);
162 }
163
F1Init(void)164 static void F1Init(void)
165 {
166 FInit(&g_maintenFeature1);
167 }
168
F2Init(void)169 static void F2Init(void)
170 {
171 FInit(&g_maintenFeature2);
172 }
173
174 SYSEX_SERVICE_INIT(S1Init);
175 SYSEX_SERVICE_INIT(S2Init);
176 SYSEX_SERVICE_INIT(S3Init);
177 SYSEX_SERVICE_INIT(S4Init);
178 SYSEX_FEATURE_INIT(F1Init);
179 SYSEX_FEATURE_INIT(F2Init);
180