1 /*
2 * Copyright (c) 2021 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 "driver_installer_full.h"
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include "hdf_base.h"
22 #include "hdf_driver_installer.h"
23 #include "hdf_log.h"
24 #include "osal_mem.h"
25 #include "securec.h"
26 #include "service_control.h"
27
28 #define HDF_LOG_TAG driver_installer_full
29 #define MAX_CMD_LEN 256
30 #define PARAM_CNT 2
31
32 static struct DriverInstaller *g_fullInstaller = NULL;
DriverInstallerFullStartDeviceHost(uint32_t devHostId,const char * devHostName)33 int DriverInstallerFullStartDeviceHost(uint32_t devHostId, const char* devHostName)
34 {
35 char hostIdStr[MAX_CMD_LEN] = {0};
36 int ret;
37
38 if (snprintf_s(hostIdStr, sizeof(hostIdStr), sizeof(hostIdStr) - 1, " %d", devHostId) < 0) {
39 HDF_LOGE("starting device host, snprintf_s failed");
40 return HDF_FAILURE;
41 }
42 const char *args[] = { hostIdStr, devHostName };
43 ret = ServiceControlWithExtra(devHostName, START, args, PARAM_CNT);
44 HDF_LOGI("%{public}s %{public}s %{public}d %{public}d", __func__, devHostName, devHostId, ret);
45 return HDF_SUCCESS;
46 }
47
DriverInstallerFullStopDeviceHost(uint32_t devHostId,const char * devHostName)48 int DriverInstallerFullStopDeviceHost(uint32_t devHostId, const char* devHostName)
49 {
50 int ret;
51 ret = ServiceControlWithExtra(devHostName, STOP, NULL, 0);
52 HDF_LOGI("%{public}s %{public}s %{public}d %{public}d", __func__, devHostName, devHostId, ret);
53 return ret;
54 }
DriverInstallerFullConstruct(struct DriverInstaller * inst)55 static void DriverInstallerFullConstruct(struct DriverInstaller *inst)
56 {
57 struct IDriverInstaller *pvtbl = (struct IDriverInstaller *)inst;
58 pvtbl->StartDeviceHost = DriverInstallerFullStartDeviceHost;
59 pvtbl->StopDeviceHost = DriverInstallerFullStopDeviceHost;
60 }
61
DriverInstallerFullCreate(void)62 struct HdfObject *DriverInstallerFullCreate(void)
63 {
64 if (g_fullInstaller == NULL) {
65 g_fullInstaller = (struct DriverInstaller *)OsalMemCalloc(sizeof(struct DriverInstaller));
66 if (g_fullInstaller != NULL) {
67 DriverInstallerFullConstruct(g_fullInstaller);
68 }
69 }
70
71 return (struct HdfObject *)g_fullInstaller;
72 }
73
DriverInstallerFullRelease(struct HdfObject * object)74 void DriverInstallerFullRelease(struct HdfObject *object)
75 {
76 struct DriverInstaller *installer = (struct DriverInstaller *)object;
77 if (g_fullInstaller == installer) {
78 g_fullInstaller = NULL;
79 }
80 if (installer != NULL) {
81 OsalMemFree(installer);
82 }
83 }
84