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 "example.h"
17#include <stdint.h>
18#include <feature.h>
19#include <securec.h>
20#include <ohos_init.h>
21#include <samgr_lite.h>
22#include <cmsis_os.h>
23#include "time_adapter.h"
24
25typedef struct BootTestExample {
26    Service service;
27    Feature feature;
28} BootTestExample;
29
30static const char *GetName(Service *service);
31static BOOL Initialize(Service *service, Identity identity);
32static BOOL MessageHandle(Service *service, Request *msg);
33static TaskConfig GetTaskConfig(Service *service);
34static const char *FEATURE_GetName(Feature *feature);
35static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
36static void FEATURE_OnStop(Feature *feature, Identity identity);
37static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
38static BootTestExample g_example[] = {
39    {
40        .service = {GetName, Initialize, MessageHandle, GetTaskConfig},
41        .feature = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage}
42    }, {
43        .service = {GetName, Initialize, MessageHandle, GetTaskConfig},
44        .feature = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage}
45    }, {
46        .service = {GetName, Initialize, MessageHandle, GetTaskConfig},
47        .feature = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage}
48    }, {
49        .service = {GetName, Initialize, MessageHandle, GetTaskConfig},
50        .feature = {FEATURE_GetName, FEATURE_OnInitialize, FEATURE_OnStop, FEATURE_OnMessage}
51    }
52};
53
54static uint32_t g_initIndex = 0;
55
56static const char *FEATURE_GetName(Feature *feature)
57{
58    // test cases service 0
59    if (feature == &g_example[0].feature) {
60        return BOOT_SYS_FEATURE1;
61    }
62    // test cases service 1
63    if (feature == &g_example[1].feature) {
64        return BOOT_SYS_FEATURE2;
65    }
66    // test cases service 2
67    if (feature == &g_example[2].feature) {
68        return BOOT_SYSEX_FEATURE1;
69    }
70    // test cases service 3
71    if (feature == &g_example[3].feature) {
72        return BOOT_SYSEX_FEATURE2;
73    }
74    return NULL;
75}
76
77static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
78{
79    (void)identity;
80    printf("[Boot Test][TaskID:%u][Step:%u][Reg Finish S:%s, F:%s]Time: %llu!\n",
81           (int)osThreadGetId(), g_initIndex++, parent->GetName(parent), feature->GetName(feature),
82           SAMGR_GetProcessTime());
83}
84
85static void FEATURE_OnStop(Feature *feature, Identity identity)
86{
87    (void)feature;
88    (void)identity;
89}
90
91static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
92{
93    printf("[Boot Test][TaskID:%u][Step:%u][F:%s] msgId<%d> \n",
94           (int)osThreadGetId(), g_initIndex++, feature->GetName(feature), request->msgId);
95    return FALSE;
96}
97
98static const char *GetName(Service *service)
99{
100    // test cases service 0
101    if (service == &g_example[0].service) {
102        return BOOT_SYS_SERVICE1;
103    }
104    // test cases service 1
105    if (service == &g_example[1].service) {
106        return BOOT_SYS_SERVICE2;
107    }
108    // test cases service 2
109    if (service == &g_example[2].service) {
110        return BOOT_SYSEX_SERVICE1;
111    }
112    // test cases service 3
113    if (service == &g_example[3].service) {
114        return BOOT_SYSEX_SERVICE2;
115    }
116    return NULL;
117}
118
119static BOOL Initialize(Service *service, Identity identity)
120{
121    (void)identity;
122    printf("[Boot Test][TaskID:%u][Step:%u][Reg Finish S:%s]Time: %llu!\n",
123           (int)osThreadGetId(), g_initIndex++, service->GetName(service), SAMGR_GetProcessTime());
124    return TRUE;
125}
126
127static BOOL MessageHandle(Service *service, Request *msg)
128{
129    printf("[Boot Test][TaskID:%u][Step:%u][S:%s] msgId<%d> \n",
130           (int)osThreadGetId(), g_initIndex++, service->GetName(service), msg->msgId);
131    return FALSE;
132}
133
134static TaskConfig GetTaskConfig(Service *service)
135{
136    (void)service;
137    TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL,
138                         0x400, 2, SHARED_TASK};
139    return config;
140}
141
142static void MInit(void)
143{
144    printf("[Boot Test][TaskID:%u][Step:%u][CORE INIT]Time: %llu!\n",
145           (int)osThreadGetId(), g_initIndex++, SAMGR_GetProcessTime());
146}
147static void MRun(void)
148{
149    printf("[Boot Test][TaskID:%u][Step:%u][SYS RUN]Time: %llu!\n",
150           (int)osThreadGetId(), g_initIndex++, SAMGR_GetProcessTime());
151}
152
153static void SInit(BootTestExample *demo)
154{
155    SAMGR_GetInstance()->RegisterService(&demo->service);
156
157    // test cases service 2
158    printf((demo < &g_example[2]) ?
159           "[Boot Test][TaskID:%u][Step:%u][SYS Reg S:%s]Time: %llu!\n" :
160           "[Boot Test][TaskID:%u][Step:%u][SYSEX Reg S:%s]Time: %llu!\n",
161           (int)osThreadGetId(), g_initIndex++, demo->service.GetName(&demo->service), SAMGR_GetProcessTime());
162}
163
164static void FInit(BootTestExample *demo)
165{
166    SAMGR_GetInstance()->RegisterFeature(demo->service.GetName(&demo->service), &demo->feature);
167
168    // test cases service 2
169    printf((demo < &g_example[2]) ?
170           "[Boot Test][TaskID:%u][Step:%u][SYS Reg S:%s, F:%s]Time: %llu!\n" :
171           "[Boot Test][TaskID:%u][Step:%u][SYSEX Reg S:%s, F:%s]Time: %llu!\n",
172           (int)osThreadGetId(), g_initIndex++, demo->service.GetName(&demo->service),
173           demo->feature.GetName(&demo->feature), SAMGR_GetProcessTime());
174}
175
176static void S1Init(void)
177{
178    // test cases service 0
179    SInit(&g_example[0]);
180}
181
182static void S2Init(void)
183{
184    // test cases service 1
185    SInit(&g_example[1]);
186}
187
188static void F1Init(void)
189{
190    // test cases feature 0
191    FInit(&g_example[0]);
192}
193
194static void F2Init(void)
195{
196    // test cases feature 1
197    FInit(&g_example[1]);
198}
199
200static void S3Init(void)
201{
202    // test cases service 2
203    SInit(&g_example[2]);
204}
205
206static void S4Init(void)
207{
208    // test cases service 3
209    SInit(&g_example[3]);
210}
211
212static void F3Init(void)
213{
214    // test cases feature 2
215    FInit(&g_example[2]);
216}
217
218static void F4Init(void)
219{
220    // test cases feature 3
221    FInit(&g_example[3]);
222}
223
224CORE_INIT(MInit);
225SYS_RUN(MRun);
226// init pri first
227SYS_SERVICE_INIT_PRI(S1Init, 0);
228// init pri second
229SYS_SERVICE_INIT_PRI(S2Init, 1);
230// init pri first
231SYS_FEATURE_INIT_PRI(F1Init, 0);
232// init pri second
233SYS_FEATURE_INIT_PRI(F2Init, 1);
234// init pri first
235SYSEX_SERVICE_INIT_PRI(S3Init, 0);
236// init pri second
237SYSEX_SERVICE_INIT_PRI(S4Init, 1);
238// init pri first
239SYSEX_FEATURE_INIT_PRI(F3Init, 0);
240// init pri second
241SYSEX_FEATURE_INIT_PRI(F4Init, 1);
242