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 <ohos_init.h>
19#include <securec.h>
20#include <los_base.h>
21#include <cmsis_os.h>
22#include "iunknown.h"
23#include "feature.h"
24#include "service.h"
25#include "samgr_lite.h"
26#include "time_adapter.h"
27
28typedef struct DefaultFeatureApi {
29    INHERIT_IUNKNOWN;
30    void (*SyncCall)(IUnknown *iUnknown);
31} DefaultFeatureApi;
32
33typedef struct ExampleService {
34    INHERIT_SERVICE;
35    INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
36    Identity identity;
37} ExampleService;
38
39static const char *GetName(Service *service)
40{
41    (void)service;
42    return EXAMPLE_SERVICE;
43}
44
45static uint32_t g_regStep = 0;
46
47static BOOL Initialize(Service *service, Identity identity)
48{
49    ExampleService *example = (ExampleService *)service;
50    example->identity = identity;
51    printf("[Register Test][TaskID:%u][Step:%u][Reg Finish S:%s]Time: %llu!\n",
52           (int)osThreadGetId(), g_regStep++, service->GetName(service), SAMGR_GetProcessTime());
53    return TRUE;
54}
55
56static BOOL MessageHandle(Service *service, Request *msg)
57{
58    printf("[LPC Test][TaskID:%u] msgId<%d>: %s \n", (int)osThreadGetId(), msg->msgId, (char *)msg->data);
59    (void)service;
60    return FALSE;
61}
62
63static TaskConfig GetTaskConfig(Service *service)
64{
65    (void)service;
66    TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL,
67                         0x800, 20, SHARED_TASK};
68    return config;
69}
70
71static volatile uint32 g_asyncStep = 0;
72
73static void SyncCall(IUnknown *iUnknown)
74{
75    (void)iUnknown;
76    printf("[LPC Test][TaskID:%u][Step:%u][SyncCall API] Default Success!\n", (int)osThreadGetId(),
77           g_asyncStep++);
78}
79
80static ExampleService g_example = {
81    .GetName = GetName,
82    .Initialize = Initialize,
83    .MessageHandle = MessageHandle,
84    .GetTaskConfig = GetTaskConfig,
85    DEFAULT_IUNKNOWN_ENTRY_BEGIN,
86    .SyncCall = SyncCall,
87    DEFAULT_IUNKNOWN_ENTRY_END,
88};
89
90static void Init(void)
91{
92    SAMGR_GetInstance()->RegisterService((Service *)&g_example);
93    SAMGR_GetInstance()->RegisterDefaultFeatureApi(EXAMPLE_SERVICE, GET_IUNKNOWN(g_example));
94    printf("[Register Test][TaskID:%u][Step:%u][Reg S:%s]Time: %llu!\n",
95           (int)osThreadGetId(), g_regStep++, EXAMPLE_SERVICE, SAMGR_GetProcessTime());
96}
97
98SYSEX_SERVICE_INIT(Init);
99
100static uint32_t g_discoverStep = 0;
101
102static DefaultFeatureApi *CASE_GetIUnknown(void)
103{
104    DefaultFeatureApi *demoApi = NULL;
105    printf("[Discover Test][TaskID:%u][Step:%u][GetIUnknown S:%s]: BEGIN\n",
106           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
107    IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(EXAMPLE_SERVICE);
108    if (iUnknown == NULL) {
109        printf("[Discover Test][TaskID:%u][Step:%u][GetDefaultFeatureApi S:%s]Error is NULL!\n",
110               (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
111        goto END;
112    }
113    int result = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
114    if (result != 0 || demoApi == NULL) {
115        printf("[Discover Test][TaskID:%u][Step:%u][QueryInterface S:%s]Error is NULL!\n",
116               (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
117        goto END;
118    }
119    printf("[Discover Test][TaskID:%u][Step:%u][GetIUnknown S:%s]Success\n",
120           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
121END:
122    printf("[Discover Test][TaskID:%u][Step:%u][GetIUnknown S:%s]: END\n",
123           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
124    return demoApi;
125}
126
127static void CASE_SyncCall(DefaultFeatureApi *defaultApi)
128{
129    printf("[LPC Test][TaskID:%u][Step:%u][DefaultFeature SyncCall]: BEGIN\n", (int)osThreadGetId(),
130           g_asyncStep++);
131    defaultApi->SyncCall((IUnknown *)defaultApi);
132    printf("[LPC Test][TaskID:%u][Step:%u][DefaultFeature SyncCall]: END\n", (int)osThreadGetId(),
133           g_asyncStep++);
134}
135
136static void CASE_ReleaseIUnknown(DefaultFeatureApi *demoApi)
137{
138    printf("[Discover Test][TaskID:%u][Step:%u][ReleaseIUnknown S:%s]: BEGIN\n",
139           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
140    int32 ref = demoApi->Release((IUnknown *)demoApi);
141    if (ref <= 0) {
142        printf("[Discover Test][TaskID:%u][Step:%u][ReleaseIUnknown S:%s]Error ref is %d!\n",
143               (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE, ref);
144        goto END;
145    }
146    printf("[Discover Test][TaskID:%u][Step:%u][ReleaseIUnknown S:%s]Success\n",
147           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
148END:
149    printf("[Discover Test][TaskID:%u][Step:%u][ReleaseIUnknown S:%s]: END\n",
150           (int)osThreadGetId(), g_discoverStep++, EXAMPLE_SERVICE);
151}
152
153static void CASE_RegisterInvalidService(void)
154{
155    Service service = {.GetName = NULL, .GetTaskConfig = NULL, .Initialize = NULL, .MessageHandle = NULL};
156    BOOL ret = SAMGR_GetInstance()->RegisterService(&service);
157    printf("Register Invalid Service %s\n", ret ? "TRUE" : "FALSE");
158
159    Feature feature = {.GetName = NULL, .OnInitialize = NULL, .OnMessage = NULL, .OnStop = NULL};
160    ret = SAMGR_GetInstance()->RegisterFeature(EXAMPLE_SERVICE, &feature);
161    printf("Register Invalid Feature %s\n", ret ? "TRUE" : "FALSE");
162
163    IUnknownEntry entry = {
164        .ver = DEFAULT_VERSION,
165        .ref = 1,
166        .iUnknown = {
167            .QueryInterface = NULL,
168            .Release = NULL,
169            .AddRef = NULL
170        }
171    };
172    ret = SAMGR_GetInstance()->RegisterDefaultFeatureApi(EXAMPLE_SERVICE, GET_IUNKNOWN(entry));
173    printf("Register Invalid Default Api %s\n", ret ? "TRUE" : "FALSE");
174
175    ret = SAMGR_GetInstance()->RegisterFeatureApi(EXAMPLE_SERVICE, EXAMPLE_FEATURE "2", GET_IUNKNOWN(entry));
176    printf("Register Invalid " EXAMPLE_FEATURE "2 Api %s\n", ret ? "TRUE" : "FALSE");
177}
178
179static void RunTestCase(void)
180{
181    DefaultFeatureApi *defaultApi = CASE_GetIUnknown();
182    CASE_RegisterInvalidService();
183    CASE_SyncCall(defaultApi);
184    CASE_ReleaseIUnknown(defaultApi);
185}
186
187LAYER_INITCALL_DEF(RunTestCase, test, "test");
188