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 ACM_SERVICE_NAME "usbfn_cdcacm"
29
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 int32_t 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 int32_t TestPropGet(const char *propName)
79 {
80 int32_t 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 int32_t TestPropSet(const char *propName, const char *propValue)
103 {
104 int32_t 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 int32_t TestPropRegist(const char *propName, const char *propValue)
122 {
123 int32_t 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
TestProp(const char * propName,const char * propValue,bool setProp,bool getProp,bool registProp)152 static int32_t TestProp(const char *propName, const char *propValue,
153 bool setProp, bool getProp, bool registProp)
154 {
155 int32_t ret = HDF_SUCCESS;
156 if (DispatcherInit() != HDF_SUCCESS) {
157 return HDF_FAILURE;
158 }
159 if (getProp) {
160 ret = TestPropGet(propName);
161 } else if (setProp) {
162 ret = TestPropSet(propName, propValue);
163 } else if (registProp) {
164 ret = TestPropRegist(propName, propValue);
165 }
166 DispatcherDeInit();
167 return ret;
168 }
169
prop_test(int32_t argc,const char * argv[])170 int32_t prop_test(int32_t argc, const char *argv[])
171 {
172 int32_t ch;
173 const char *propName = NULL;
174 const char *propValue = NULL;
175 bool setProp = false;
176 bool getProp = false;
177 bool registProp = false;
178
179 ch = *(argv[1]);
180 switch (ch) {
181 case 'r':
182 propName = argv[0x2];
183 propValue = argv[0x3];
184 registProp = true;
185 break;
186 case 'g':
187 propName = argv[0x2];
188 getProp = true;
189 break;
190 case 's':
191 propName = argv[0x2];
192 propValue = argv[0x3];
193 setProp = true;
194 break;
195 case 'h':
196 case '?':
197 ShowUsage();
198 return 0;
199 break;
200 default:
201 break;
202 }
203 return TestProp(propName, propValue, setProp, getProp, registProp);
204 }
205
206