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
32static const char *GetName(Service *service);
33static BOOL Initialize(Service *service, Identity identity);
34static BOOL MessageHandle(Service *service, Request *msg);
35static TaskConfig GetTaskConfig(Service *service);
36static const char *FEATURE_GetName(Feature *feature);
37static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
38static void FEATURE_OnStop(Feature *feature, Identity identity);
39static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
40
41Service g_maintenExample1 = {GetName, Initialize, MessageHandle, GetTaskConfig};
42Service g_maintenExample2 = {GetName, Initialize, MessageHandle, GetTaskConfig};
43Service g_maintenExample3 = {GetName, Initialize, MessageHandle, GetTaskConfig};
44Service g_maintenExample4 = {GetName, Initialize, MessageHandle, GetTaskConfig};
45Feature g_maintenFeature1 = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage};
46Feature g_maintenFeature2 = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage};
47IUnknownEntry g_demoApi = {DEFAULT_IUNKNOWN_ENTRY_BEGIN, DEFAULT_IUNKNOWN_ENTRY_END};
48
49static 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
60static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
61{
62    (void)feature;
63    (void)parent;
64    (void)identity;
65}
66
67static void FEATURE_OnStop(Feature *feature, Identity identity)
68{
69    (void)feature;
70    (void)identity;
71}
72
73static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
74{
75    (void)feature;
76    (void)request;
77    return FALSE;
78}
79
80static 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
97static BOOL Initialize(Service *service, Identity identity)
98{
99    (void)identity;
100    (void)service;
101    return TRUE;
102}
103
104static BOOL MessageHandle(Service *service, Request *msg)
105{
106    (void)service;
107    (void)msg;
108    return FALSE;
109}
110
111static 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
123static 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
132static 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
144static void S1Init(void)
145{
146    SInit(&g_maintenExample1);
147}
148
149static void S2Init(void)
150{
151    SInit(&g_maintenExample2);
152}
153
154static void S3Init(void)
155{
156    SInit(&g_maintenExample3);
157}
158
159static void S4Init(void)
160{
161    SInit(&g_maintenExample4);
162}
163
164static void F1Init(void)
165{
166    FInit(&g_maintenFeature1);
167}
168
169static void F2Init(void)
170{
171    FInit(&g_maintenFeature2);
172}
173
174SYSEX_SERVICE_INIT(S1Init);
175SYSEX_SERVICE_INIT(S2Init);
176SYSEX_SERVICE_INIT(S3Init);
177SYSEX_SERVICE_INIT(S4Init);
178SYSEX_FEATURE_INIT(F1Init);
179SYSEX_FEATURE_INIT(F2Init);
180