16f2c2defSopenharmony_ci/*
26f2c2defSopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
36f2c2defSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46f2c2defSopenharmony_ci * you may not use this file except in compliance with the License.
56f2c2defSopenharmony_ci * You may obtain a copy of the License at
66f2c2defSopenharmony_ci *
76f2c2defSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86f2c2defSopenharmony_ci *
96f2c2defSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106f2c2defSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116f2c2defSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126f2c2defSopenharmony_ci * See the License for the specific language governing permissions and
136f2c2defSopenharmony_ci * limitations under the License.
146f2c2defSopenharmony_ci */
156f2c2defSopenharmony_ci
166f2c2defSopenharmony_ci#include "ohos_init.h"
176f2c2defSopenharmony_ci#include "samgr_lite.h"
186f2c2defSopenharmony_ci#include "service.h"
196f2c2defSopenharmony_ci#include "common.h"
206f2c2defSopenharmony_ci#include "hiview_def.h"
216f2c2defSopenharmony_ci#include "hiview_config.h"
226f2c2defSopenharmony_ci#include "hiview_service.h"
236f2c2defSopenharmony_ci#include "hiview_util.h"
246f2c2defSopenharmony_ci
256f2c2defSopenharmony_cistatic const char *GetName(Service *service);
266f2c2defSopenharmony_cistatic BOOL Initialize(Service *service, Identity identity);
276f2c2defSopenharmony_cistatic TaskConfig GetTaskConfig(Service *service);
286f2c2defSopenharmony_cistatic BOOL MessageHandle(Service *service, Request *request);
296f2c2defSopenharmony_cistatic void Output(IUnknown *iUnknown, int16 msgId, uint16 type);
306f2c2defSopenharmony_ci
316f2c2defSopenharmony_cistatic HiviewService g_hiviewService = {
326f2c2defSopenharmony_ci    .GetName = GetName,
336f2c2defSopenharmony_ci    .Initialize = Initialize,
346f2c2defSopenharmony_ci    .MessageHandle = MessageHandle,
356f2c2defSopenharmony_ci    .GetTaskConfig = GetTaskConfig,
366f2c2defSopenharmony_ci    DEFAULT_IUNKNOWN_ENTRY_BEGIN,
376f2c2defSopenharmony_ci    .Output = Output,
386f2c2defSopenharmony_ci    DEFAULT_IUNKNOWN_ENTRY_END,
396f2c2defSopenharmony_ci};
406f2c2defSopenharmony_ci
416f2c2defSopenharmony_cistatic HiviewInitFunc g_hiviewInitFuncList[HIVIEW_CMP_TYPE_MAX] = { NULL };
426f2c2defSopenharmony_cistatic HiviewMsgHandle g_hiviewMsgHandleList[HIVIEW_MSG_MAX] = { NULL };
436f2c2defSopenharmony_cistatic void InitHiviewComponent(void);
446f2c2defSopenharmony_ci
456f2c2defSopenharmony_cistatic void Init(void)
466f2c2defSopenharmony_ci{
476f2c2defSopenharmony_ci    SAMGR_GetInstance()->RegisterService((Service *)&g_hiviewService);
486f2c2defSopenharmony_ci    SAMGR_GetInstance()->RegisterDefaultFeatureApi(HIVIEW_SERVICE, GET_IUNKNOWN(g_hiviewService));
496f2c2defSopenharmony_ci    InitHiviewComponent();
506f2c2defSopenharmony_ci}
516f2c2defSopenharmony_ciSYS_SERVICE_INIT(Init);
526f2c2defSopenharmony_ci
536f2c2defSopenharmony_cistatic const char *GetName(Service *service)
546f2c2defSopenharmony_ci{
556f2c2defSopenharmony_ci    (void)service;
566f2c2defSopenharmony_ci    return HIVIEW_SERVICE;
576f2c2defSopenharmony_ci}
586f2c2defSopenharmony_ci
596f2c2defSopenharmony_cistatic BOOL Initialize(Service *service, Identity identity)
606f2c2defSopenharmony_ci{
616f2c2defSopenharmony_ci    HiviewService *hiviewService = NULL;
626f2c2defSopenharmony_ci
636f2c2defSopenharmony_ci    if (service == NULL) {
646f2c2defSopenharmony_ci        return FALSE;
656f2c2defSopenharmony_ci    }
666f2c2defSopenharmony_ci    hiviewService = (HiviewService *)service;
676f2c2defSopenharmony_ci    hiviewService->identity = identity;
686f2c2defSopenharmony_ci    /* The communication of task can be use after the service is running. */
696f2c2defSopenharmony_ci    g_hiviewConfig.hiviewInited = TRUE;
706f2c2defSopenharmony_ci    HIVIEW_UartPrint("hiview init success.");
716f2c2defSopenharmony_ci
726f2c2defSopenharmony_ci    return TRUE;
736f2c2defSopenharmony_ci}
746f2c2defSopenharmony_ci
756f2c2defSopenharmony_cistatic BOOL MessageHandle(Service *service, Request *request)
766f2c2defSopenharmony_ci{
776f2c2defSopenharmony_ci    (void)service;
786f2c2defSopenharmony_ci    if ((request == NULL) || (request->msgId >= HIVIEW_MSG_MAX)) {
796f2c2defSopenharmony_ci        return TRUE;
806f2c2defSopenharmony_ci    }
816f2c2defSopenharmony_ci    if (g_hiviewMsgHandleList[request->msgId] != NULL) {
826f2c2defSopenharmony_ci        (*(g_hiviewMsgHandleList[request->msgId]))(request);
836f2c2defSopenharmony_ci    }
846f2c2defSopenharmony_ci
856f2c2defSopenharmony_ci    return TRUE;
866f2c2defSopenharmony_ci}
876f2c2defSopenharmony_ci
886f2c2defSopenharmony_cistatic TaskConfig GetTaskConfig(Service *service)
896f2c2defSopenharmony_ci{
906f2c2defSopenharmony_ci    (void)service;
916f2c2defSopenharmony_ci    TaskConfig config = { LEVEL_LOW, PRI_NORMAL, HIVIEW_STACK_SIZE, 10, SINGLE_TASK };
926f2c2defSopenharmony_ci    return config;
936f2c2defSopenharmony_ci}
946f2c2defSopenharmony_ci
956f2c2defSopenharmony_cistatic void Output(IUnknown *iUnknown, int16 msgId, uint16 type)
966f2c2defSopenharmony_ci{
976f2c2defSopenharmony_ci    HiviewService *service = GET_OBJECT(iUnknown, HiviewService, iUnknown);
986f2c2defSopenharmony_ci    Request request = {
996f2c2defSopenharmony_ci        .msgId =  msgId,
1006f2c2defSopenharmony_ci        .msgValue = type,
1016f2c2defSopenharmony_ci        .data = NULL,
1026f2c2defSopenharmony_ci        .len = 0
1036f2c2defSopenharmony_ci    };
1046f2c2defSopenharmony_ci    SAMGR_SendRequest(&(service->identity), &request, NULL);
1056f2c2defSopenharmony_ci}
1066f2c2defSopenharmony_ci
1076f2c2defSopenharmony_cistatic void InitHiviewComponent(void)
1086f2c2defSopenharmony_ci{
1096f2c2defSopenharmony_ci    uint8 i;
1106f2c2defSopenharmony_ci    for (i = 0; i < HIVIEW_CMP_TYPE_MAX; i++) {
1116f2c2defSopenharmony_ci        if (g_hiviewInitFuncList[i] != NULL) {
1126f2c2defSopenharmony_ci            (*(g_hiviewInitFuncList[i]))();
1136f2c2defSopenharmony_ci        }
1146f2c2defSopenharmony_ci    }
1156f2c2defSopenharmony_ci}
1166f2c2defSopenharmony_ci
1176f2c2defSopenharmony_civoid HiviewRegisterInitFunc(HiviewComponentType type, HiviewInitFunc func)
1186f2c2defSopenharmony_ci{
1196f2c2defSopenharmony_ci    g_hiviewInitFuncList[type] = func;
1206f2c2defSopenharmony_ci}
1216f2c2defSopenharmony_ci
1226f2c2defSopenharmony_civoid HiviewRegisterMsgHandle(HiviewInnerMessage type, HiviewMsgHandle func)
1236f2c2defSopenharmony_ci{
1246f2c2defSopenharmony_ci    g_hiviewMsgHandleList[type] = func;
1256f2c2defSopenharmony_ci}
1266f2c2defSopenharmony_ci
1276f2c2defSopenharmony_civoid HiviewSendMessage(const char *srvName, int16 msgId, uint16 msgValue)
1286f2c2defSopenharmony_ci{
1296f2c2defSopenharmony_ci    static HiviewInterface *hiviewInfterface = NULL;
1306f2c2defSopenharmony_ci
1316f2c2defSopenharmony_ci    if (hiviewInfterface == NULL) {
1326f2c2defSopenharmony_ci        IUnknown *hiviewDefApi = SAMGR_GetInstance()->GetDefaultFeatureApi(srvName);
1336f2c2defSopenharmony_ci        if (hiviewDefApi == NULL) {
1346f2c2defSopenharmony_ci            return;
1356f2c2defSopenharmony_ci        }
1366f2c2defSopenharmony_ci        hiviewDefApi->QueryInterface(hiviewDefApi, 0, (void **)&hiviewInfterface);
1376f2c2defSopenharmony_ci    }
1386f2c2defSopenharmony_ci    hiviewInfterface->Output((IUnknown *)hiviewInfterface, msgId, msgValue);
1396f2c2defSopenharmony_ci}
140