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