1 /*
2 * Copyright (c) 2020 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 "ohos_init.h"
17 #include "samgr_lite.h"
18 #include "service.h"
19 #include "common.h"
20 #include "hiview_def.h"
21 #include "hiview_config.h"
22 #include "hiview_service.h"
23 #include "hiview_util.h"
24
25 static const char *GetName(Service *service);
26 static BOOL Initialize(Service *service, Identity identity);
27 static TaskConfig GetTaskConfig(Service *service);
28 static BOOL MessageHandle(Service *service, Request *request);
29 static void Output(IUnknown *iUnknown, int16 msgId, uint16 type);
30
31 static HiviewService g_hiviewService = {
32 .GetName = GetName,
33 .Initialize = Initialize,
34 .MessageHandle = MessageHandle,
35 .GetTaskConfig = GetTaskConfig,
36 DEFAULT_IUNKNOWN_ENTRY_BEGIN,
37 .Output = Output,
38 DEFAULT_IUNKNOWN_ENTRY_END,
39 };
40
41 static HiviewInitFunc g_hiviewInitFuncList[HIVIEW_CMP_TYPE_MAX] = { NULL };
42 static HiviewMsgHandle g_hiviewMsgHandleList[HIVIEW_MSG_MAX] = { NULL };
43 static void InitHiviewComponent(void);
44
Init(void)45 static void Init(void)
46 {
47 SAMGR_GetInstance()->RegisterService((Service *)&g_hiviewService);
48 SAMGR_GetInstance()->RegisterDefaultFeatureApi(HIVIEW_SERVICE, GET_IUNKNOWN(g_hiviewService));
49 InitHiviewComponent();
50 }
51 SYS_SERVICE_INIT(Init);
52
GetName(Service * service)53 static const char *GetName(Service *service)
54 {
55 (void)service;
56 return HIVIEW_SERVICE;
57 }
58
Initialize(Service * service,Identity identity)59 static BOOL Initialize(Service *service, Identity identity)
60 {
61 HiviewService *hiviewService = NULL;
62
63 if (service == NULL) {
64 return FALSE;
65 }
66 hiviewService = (HiviewService *)service;
67 hiviewService->identity = identity;
68 /* The communication of task can be use after the service is running. */
69 g_hiviewConfig.hiviewInited = TRUE;
70 HIVIEW_UartPrint("hiview init success.");
71
72 return TRUE;
73 }
74
MessageHandle(Service * service,Request * request)75 static BOOL MessageHandle(Service *service, Request *request)
76 {
77 (void)service;
78 if ((request == NULL) || (request->msgId >= HIVIEW_MSG_MAX)) {
79 return TRUE;
80 }
81 if (g_hiviewMsgHandleList[request->msgId] != NULL) {
82 (*(g_hiviewMsgHandleList[request->msgId]))(request);
83 }
84
85 return TRUE;
86 }
87
GetTaskConfig(Service * service)88 static TaskConfig GetTaskConfig(Service *service)
89 {
90 (void)service;
91 TaskConfig config = { LEVEL_LOW, PRI_NORMAL, HIVIEW_STACK_SIZE, 10, SINGLE_TASK };
92 return config;
93 }
94
Output(IUnknown * iUnknown,int16 msgId,uint16 type)95 static void Output(IUnknown *iUnknown, int16 msgId, uint16 type)
96 {
97 HiviewService *service = GET_OBJECT(iUnknown, HiviewService, iUnknown);
98 Request request = {
99 .msgId = msgId,
100 .msgValue = type,
101 .data = NULL,
102 .len = 0
103 };
104 SAMGR_SendRequest(&(service->identity), &request, NULL);
105 }
106
InitHiviewComponent(void)107 static void InitHiviewComponent(void)
108 {
109 uint8 i;
110 for (i = 0; i < HIVIEW_CMP_TYPE_MAX; i++) {
111 if (g_hiviewInitFuncList[i] != NULL) {
112 (*(g_hiviewInitFuncList[i]))();
113 }
114 }
115 }
116
HiviewRegisterInitFunc(HiviewComponentType type,HiviewInitFunc func)117 void HiviewRegisterInitFunc(HiviewComponentType type, HiviewInitFunc func)
118 {
119 g_hiviewInitFuncList[type] = func;
120 }
121
HiviewRegisterMsgHandle(HiviewInnerMessage type,HiviewMsgHandle func)122 void HiviewRegisterMsgHandle(HiviewInnerMessage type, HiviewMsgHandle func)
123 {
124 g_hiviewMsgHandleList[type] = func;
125 }
126
HiviewSendMessage(const char * srvName,int16 msgId,uint16 msgValue)127 void HiviewSendMessage(const char *srvName, int16 msgId, uint16 msgValue)
128 {
129 static HiviewInterface *hiviewInfterface = NULL;
130
131 if (hiviewInfterface == NULL) {
132 IUnknown *hiviewDefApi = SAMGR_GetInstance()->GetDefaultFeatureApi(srvName);
133 if (hiviewDefApi == NULL) {
134 return;
135 }
136 hiviewDefApi->QueryInterface(hiviewDefApi, 0, (void **)&hiviewInfterface);
137 }
138 hiviewInfterface->Output((IUnknown *)hiviewInfterface, msgId, msgValue);
139 }
140