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_client_service_impl.h"
26 
27 using namespace OHOS::DHCP;
28 static std::shared_ptr<DhcpClientServiceImpl> g_dhcpClientServiceImpl = DhcpClientServiceImpl::GetInstance();
29 
30 typedef struct DhcpClientApi {
31     INHERIT_SERVER_IPROXY;
32 } DhcpClientApi;
33 
34 typedef struct DhcpClientFeature {
35     INHERIT_FEATURE;
36     INHERIT_IUNKNOWNENTRY(DhcpClientApi);
37     Identity identity;
38     Service *parent;
39 } DhcpClientFeature;
40 
GetName(Feature * feature)41 static const char *GetName(Feature *feature)
42 {
43     return DHCP_FEATRUE_CLIENT;
44 }
45 
OnInitialize(Feature * feature,Service * parent,Identity identity)46 static void OnInitialize(Feature *feature, Service *parent, Identity identity)
47 {
48     if (feature != NULL) {
49         DhcpClientFeature *serverFeature = (DhcpClientFeature *)feature;
50         serverFeature->identity = identity;
51         serverFeature->parent = parent;
52     }
53     if (g_dhcpClientServiceImpl != NULL) {
54         g_dhcpClientServiceImpl->OnStart();
55     }
56 }
57 
OnStop(Feature * feature,Identity identity)58 static void OnStop(Feature *feature, Identity identity)
59 {
60     if (g_dhcpClientServiceImpl != NULL) {
61         g_dhcpClientServiceImpl->OnStop();
62     }
63     if (feature != NULL) {
64         DhcpClientFeature *serverFeature = (DhcpClientFeature *)feature;
65         serverFeature->identity.queueId = NULL;
66         serverFeature->identity.featureId = -1;
67         serverFeature->identity.serviceId = -1;
68     }
69 }
70 
OnMessage(Feature * feature,Request * request)71 static BOOL OnMessage(Feature *feature, Request *request)
72 {
73     return TRUE;
74 }
75 
Invoke(IServerProxy * proxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)76 static int Invoke(IServerProxy *proxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
77 {
78     LOGI("[DhcpClientFeature] begin to call Invoke, funcId is %{public}d", funcId);
79     if (g_dhcpClientServiceImpl != NULL) {
80         return g_dhcpClientServiceImpl->OnRemoteRequest(funcId, req, reply);
81     }
82     return EC_FAILURE;
83 }
84 
85 static DhcpServerFeature g_serverFeature = {
86     .GetName = GetName,
87     .OnInitialize = OnInitialize,
88     .OnStop = OnStop,
89     .OnMessage = OnMessage,
90     SERVER_IPROXY_IMPL_BEGIN,
91     .Invoke = Invoke,
92     IPROXY_END,
93     .identity = {-1, -1, NULL},
94 };
95 
Init(void)96 static void Init(void)
97 {
98     LOGI("[DhcpClientFeature] Init start.");
99     BOOL ret = SAMGR_GetInstance()->RegisterFeature(DHCP_CLIENT_LITE, (Feature *)&g_serverFeature);
100     if (ret == FALSE) {
101         LOGE("[DhcpClientFeature] register feature fail.");
102         return;
103     }
104     ret = SAMGR_GetInstance()->RegisterFeatureApi(DHCP_CLIENT_LITE,
105         DHCP_FEATRUE_CLIENT, GET_IUNKNOWN(g_serverFeature));
106     if (ret == FALSE) {
107         LOGE("[DhcpClientFeature] register feature api fail.");
108     }
109 }
110 SYSEX_FEATURE_INIT(Init);
111