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