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 <unistd.h> 21#include <sys/time.h> 22#include <stdio.h> 23#include "cdcacm.h" 24#include "osal_time.h" 25#include "usb_dev_test.h" 26 27#define HDF_LOG_TAG cdc_acm_read 28 29static struct HdfSBuf *g_data; 30static struct HdfSBuf *g_reply; 31static struct HdfRemoteService *g_acmService; 32 33#define STR_LEN 8192 34#define SLEEP_READ 100000 35static void TestRead(FILE *fp) 36{ 37 HdfSbufFlush(g_reply); 38 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_READ, g_data, g_reply); 39 if (status) { 40 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_READ failed status = %{public}d", __func__, status); 41 return; 42 } 43 const char *tmp = HdfSbufReadString(g_reply); 44 if (tmp && strlen(tmp) > 0) { 45 struct timeval time; 46 char str[STR_LEN] = {0}; 47 gettimeofday(&time, NULL); 48 int32_t ret = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, recv data[%s] from host\n", 49 time.tv_sec, time.tv_usec, tmp); 50 if (ret < 0) { 51 HDF_LOGE("%{public}s: snprintf_s failed", __func__); 52 return; 53 } 54 (void)fwrite(str, strlen(str), 1, fp); 55 fflush(fp); 56 } 57 usleep(SLEEP_READ); 58} 59 60int32_t AcmRead(int32_t argc, const char *argv[]) 61{ 62 (void)argc; 63 (void)argv; 64 int32_t status; 65 struct HDIServiceManager *servmgr = HDIServiceManagerGet(); 66 if (servmgr == NULL) { 67 HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__); 68 return HDF_FAILURE; 69 } 70 g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm"); 71 HDIServiceManagerRelease(servmgr); 72 if (g_acmService == NULL) { 73 HDF_LOGE("%{public}s: GetService err", __func__); 74 return HDF_FAILURE; 75 } 76 77 g_data = HdfSbufTypedObtain(SBUF_IPC); 78 g_reply = HdfSbufTypedObtain(SBUF_IPC); 79 if (g_data == NULL || g_reply == NULL) { 80 HDF_LOGE("%{public}s: GetService err", __func__); 81 return HDF_FAILURE; 82 } 83 84 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply); 85 if (status) { 86 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_OPEN err", __func__); 87 return HDF_FAILURE; 88 } 89 FILE *fp = fopen("/data/acm_read_xts", "a+"); 90 if (fp == NULL) { 91 HDF_LOGE("%{public}s: fopen err", __func__); 92 return HDF_FAILURE; 93 } 94 while (1) { 95 TestRead(fp); 96 } 97 (void)fclose(fp); 98 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply); 99 if (status) { 100 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__); 101 return HDF_FAILURE; 102 } 103 104 HdfSbufRecycle(g_data); 105 HdfSbufRecycle(g_reply); 106 107 return 0; 108} 109