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