1 /*
2  * Copyright (C) 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 <cstddef>
17 #include <cstdlib>
18 
19 #include "iproxy_server.h"
20 #include "ohos_errno.h"
21 #include "ohos_init.h"
22 #include "samgr_lite.h"
23 #include "service.h"
24 #include "dhcp_ipc_lite_adapter.h"
25 #include "dhcp_server_service_impl.h"
26 #include "dhcp_logger.h"
27 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerFeature");
28 using namespace OHOS::DHCP;
29 
30 static std::shared_ptr<DhcpServerServiceImpl> g_dhcpServerServiceImpl = DhcpServerServiceImpl::GetInstance();
31 
32 typedef struct DhcpServerApi {
33     INHERIT_SERVER_IPROXY;
34 } DhcpServerApi;
35 
36 typedef struct DhcpServerFeature {
37     INHERIT_FEATURE;
38     INHERIT_IUNKNOWNENTRY(DhcpServerApi);
39     Identity identity;
40     Service *parent;
41 } DhcpServerFeature;
42 
GetName(Feature * feature)43 static const char *GetName(Feature *feature)
44 {
45     return DHCP_FEATRUE_SERVER;
46 }
47 
OnInitialize(Feature * feature,Service * parent,Identity identity)48 static void OnInitialize(Feature *feature, Service *parent, Identity identity)
49 {
50     if (feature != nullptr) {
51         DhcpServerFeature *serverFeature = (DhcpServerFeature *)feature;
52         serverFeature->identity = identity;
53         serverFeature->parent = parent;
54     }
55     if (g_dhcpServerServiceImpl != nullptr) {
56         g_dhcpServerServiceImpl->OnStart();
57     }
58 }
59 
OnStop(Feature * feature,Identity identity)60 static void OnStop(Feature *feature, Identity identity)
61 {
62     if (g_dhcpServerServiceImpl != nullptr) {
63         g_dhcpServerServiceImpl->OnStop();
64     }
65     if (feature != nullptr) {
66         DhcpServerFeature *serverFeature = (DhcpServerFeature *)feature;
67         serverFeature->identity.queueId = nullptr;
68         serverFeature->identity.featureId = -1;
69         serverFeature->identity.serviceId = -1;
70     }
71 }
72 
OnMessage(Feature * feature,Request * request)73 static BOOL OnMessage(Feature *feature, Request *request)
74 {
75     return TRUE;
76 }
77 
Invoke(IServerProxy * proxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)78 static int Invoke(IServerProxy *proxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
79 {
80     DHCP_LOGI("[DhcpServerFeature] begin to call Invoke, funcId is %{public}d", funcId);
81     if (g_dhcpServerServiceImpl != nullptr) {
82         return g_dhcpServerServiceImpl->OnRemoteRequest(funcId, req, reply);
83     }
84     return EC_FAILURE;
85 }
86 
87 static DhcpServerFeature g_serverFeature = {
88     .GetName = GetName,
89     .OnInitialize = OnInitialize,
90     .OnStop = OnStop,
91     .OnMessage = OnMessage,
92     SERVER_IPROXY_IMPL_BEGIN,
93     .Invoke = Invoke,
94     IPROXY_END,
95     .identity = {-1, -1, nullptr},
96 };
97 
Init(void)98 static void Init(void)
99 {
100     DHCP_LOGI("[DhcpServerFeature] Init start.");
101     BOOL ret = SAMGR_GetInstance()->RegisterFeature(DHCP_SERVICE_LITE, (Feature *)&g_serverFeature);
102     if (ret == FALSE) {
103         DHCP_LOGE("[DhcpServerFeature] register feature fail.");
104         return;
105     }
106     ret = SAMGR_GetInstance()->RegisterFeatureApi(DHCP_SERVICE_LITE,
107         DHCP_FEATRUE_SERVER, GET_IUNKNOWN(g_serverFeature));
108     if (ret == FALSE) {
109         DHCP_LOGE("[DhcpServerFeature] register feature api fail.");
110     }
111 }
112 SYSEX_FEATURE_INIT(Init);
113