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 <unistd.h>
17 #include <sys/time.h>
18 #include <stdio.h>
19 #include <hdf_log.h>
20 #include <hdf_remote_service.h>
21 #include <hdf_sbuf.h>
22 #include <servmgr_hdi.h>
23 #include "cdcacm.h"
24 #include "usb_dev_test.h"
25
26 #define HDF_LOG_TAG hcs_prop
27 #define ACM_SERVICE_NAME "usbfn_cdcacm"
28
29 static struct HdfSBuf *g_data;
30 static struct HdfSBuf *g_reply;
31 static struct HdfRemoteService *g_acmService;
32
ShowUsage()33 static void ShowUsage()
34 {
35 HDF_LOGE("Usage options:\n");
36 HDF_LOGE("g : name of getting prop, as: g idProduct");
37 HDF_LOGE("s : name of setting prop, as: s idProduct 0xa4b7");
38 HDF_LOGE("r : regist prop, as: r testa aaaaa");
39 HDF_LOGE("h : show this help message");
40 }
41
DispatcherInit(void)42 static int32_t DispatcherInit(void)
43 {
44 struct HDIServiceManager *servmgr = HDIServiceManagerGet();
45 if (servmgr == NULL) {
46 HDF_LOGE("%s: HDIServiceManagerGet err", __func__);
47 return HDF_FAILURE;
48 }
49 g_acmService = servmgr->GetService(servmgr, ACM_SERVICE_NAME);
50 HDIServiceManagerRelease(servmgr);
51 if (g_acmService == NULL) {
52 HDF_LOGE("%s: GetService err", __func__);
53 return HDF_FAILURE;
54 }
55
56 g_data = HdfSbufTypedObtain(SBUF_IPC);
57 g_reply = HdfSbufTypedObtain(SBUF_IPC);
58 if (g_data == NULL || g_reply == NULL) {
59 HDF_LOGE("%s: GetService err", __func__);
60 return HDF_FAILURE;
61 }
62 return HDF_SUCCESS;
63 }
64
DispatcherDeInit(void)65 static void DispatcherDeInit(void)
66 {
67 HdfSbufRecycle(g_data);
68 HdfSbufRecycle(g_reply);
69 }
70
TestPropGet(const char * propName)71 static int32_t TestPropGet(const char *propName)
72 {
73 int32_t status = -1;
74 const char *propVal = NULL;
75 if (!HdfSbufWriteString(g_data, propName)) {
76 HDF_LOGE("%s:failed to write result", __func__);
77 goto FAIL;
78 }
79 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_GET_PROP, g_data, g_reply);
80 if (status != HDF_SUCCESS) {
81 HDF_LOGE("%s: Dispatch USB_SERIAL_GET_PROP failed status = %d", __func__, status);
82 goto FAIL;
83 }
84 propVal = HdfSbufReadString(g_reply);
85 if (propVal == NULL) {
86 HDF_LOGE("%s:failed to write result", __func__);
87 goto FAIL;
88 }
89 HDF_LOGE("%s: %s = %s\n", __func__, propName, propVal);
90
91 FAIL:
92 return status;
93 }
94
TestPropSet(const char * propName,const char * propValue)95 static int32_t TestPropSet(const char *propName, const char *propValue)
96 {
97 int32_t status = -1;
98 if (!HdfSbufWriteString(g_data, propName)) {
99 HDF_LOGE("%s:failed to write propName : %s", __func__, propName);
100 goto FAIL;
101 }
102 if (!HdfSbufWriteString(g_data, propValue)) {
103 HDF_LOGE("%s:failed to write propValue : %s", __func__, propValue);
104 goto FAIL;
105 }
106 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_SET_PROP, g_data, g_reply);
107 if (status != HDF_SUCCESS) {
108 HDF_LOGE("%s: Dispatch USB_SERIAL_SET_PROP failed", __func__);
109 }
110 FAIL:
111 return status;
112 }
113
TestPropRegist(const char * propName,const char * propValue)114 static int32_t TestPropRegist(const char *propName, const char *propValue)
115 {
116 int32_t status;
117
118 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
119 if (status) {
120 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
121 return HDF_FAILURE;
122 }
123 if (!HdfSbufWriteString(g_data, propName)) {
124 HDF_LOGE("%s:failed to write propName : %s", __func__, propName);
125 goto FAIL;
126 }
127 if (!HdfSbufWriteString(g_data, propValue)) {
128 HDF_LOGE("%s:failed to write propValue : %s", __func__, propValue);
129 goto FAIL;
130 }
131 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_REGIST_PROP, g_data, g_reply);
132 if (status != HDF_SUCCESS) {
133 HDF_LOGE("%s: Dispatch USB_SERIAL_SET_PROP failed status = %d", __func__, status);
134 }
135 FAIL:
136 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
137 if (status) {
138 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
139 return HDF_FAILURE;
140 }
141
142 return status;
143 }
144
145
prop_test(int32_t argc,const char * argv[])146 int32_t prop_test(int32_t argc, const char *argv[])
147 {
148 int32_t ch;
149 int32_t ret;
150 const char *propName = NULL;
151 const char *propValue = NULL;
152 bool setProp = false;
153 bool getProp = false;
154 bool registProp = false;
155
156 ch = *(argv[1]);
157 switch (ch) {
158 case 'r':
159 propName = argv[0x2];
160 propValue = argv[0x3];
161 registProp = true;
162 break;
163 case 'g':
164 propName = argv[0x2];
165 getProp = true;
166 break;
167 case 's':
168 propName = argv[0x2];
169 propValue = argv[0x3];
170 setProp = true;
171 break;
172 case 'h':
173 case '?':
174 ShowUsage();
175 return 0;
176 break;
177 default:
178 break;
179 }
180
181 if (DispatcherInit() != HDF_SUCCESS) {
182 return 1;
183 }
184 if (getProp) {
185 ret = TestPropGet(propName);
186 } else if (setProp) {
187 ret = TestPropSet(propName, propValue);
188 } else if (registProp) {
189 ret = TestPropRegist(propName, propValue);
190 }
191 DispatcherDeInit();
192
193 return 0;
194 }
195
196