1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <hdf_log.h> 17094332d3Sopenharmony_ci#include <hdf_remote_service.h> 18094332d3Sopenharmony_ci#include <hdf_sbuf.h> 19094332d3Sopenharmony_ci#include <servmgr_hdi.h> 20094332d3Sopenharmony_ci#include <unistd.h> 21094332d3Sopenharmony_ci#include <sys/time.h> 22094332d3Sopenharmony_ci#include <stdio.h> 23094332d3Sopenharmony_ci#include "cdcacm.h" 24094332d3Sopenharmony_ci#include "osal_time.h" 25094332d3Sopenharmony_ci#include "usb_dev_test.h" 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_ci#define HDF_LOG_TAG cdc_acm_read 28094332d3Sopenharmony_ci 29094332d3Sopenharmony_cistatic struct HdfSBuf *g_data; 30094332d3Sopenharmony_cistatic struct HdfSBuf *g_reply; 31094332d3Sopenharmony_cistatic struct HdfRemoteService *g_acmService; 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_ci#define STR_LEN 8192 34094332d3Sopenharmony_ci#define SLEEP_READ 100000 35094332d3Sopenharmony_cistatic void TestRead(FILE *fp) 36094332d3Sopenharmony_ci{ 37094332d3Sopenharmony_ci HdfSbufFlush(g_reply); 38094332d3Sopenharmony_ci int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_READ, g_data, g_reply); 39094332d3Sopenharmony_ci if (status) { 40094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Dispatch USB_SERIAL_READ failed status = %{public}d", __func__, status); 41094332d3Sopenharmony_ci return; 42094332d3Sopenharmony_ci } 43094332d3Sopenharmony_ci const char *tmp = HdfSbufReadString(g_reply); 44094332d3Sopenharmony_ci if (tmp && strlen(tmp) > 0) { 45094332d3Sopenharmony_ci struct timeval time; 46094332d3Sopenharmony_ci char str[STR_LEN] = {0}; 47094332d3Sopenharmony_ci gettimeofday(&time, NULL); 48094332d3Sopenharmony_ci int32_t ret = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, recv data[%s] from host\n", 49094332d3Sopenharmony_ci time.tv_sec, time.tv_usec, tmp); 50094332d3Sopenharmony_ci if (ret < 0) { 51094332d3Sopenharmony_ci HDF_LOGE("%{public}s: snprintf_s failed", __func__); 52094332d3Sopenharmony_ci return; 53094332d3Sopenharmony_ci } 54094332d3Sopenharmony_ci (void)fwrite(str, strlen(str), 1, fp); 55094332d3Sopenharmony_ci fflush(fp); 56094332d3Sopenharmony_ci } 57094332d3Sopenharmony_ci usleep(SLEEP_READ); 58094332d3Sopenharmony_ci} 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ciint32_t AcmRead(int32_t argc, const char *argv[]) 61094332d3Sopenharmony_ci{ 62094332d3Sopenharmony_ci (void)argc; 63094332d3Sopenharmony_ci (void)argv; 64094332d3Sopenharmony_ci int32_t status; 65094332d3Sopenharmony_ci struct HDIServiceManager *servmgr = HDIServiceManagerGet(); 66094332d3Sopenharmony_ci if (servmgr == NULL) { 67094332d3Sopenharmony_ci HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__); 68094332d3Sopenharmony_ci return HDF_FAILURE; 69094332d3Sopenharmony_ci } 70094332d3Sopenharmony_ci g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm"); 71094332d3Sopenharmony_ci HDIServiceManagerRelease(servmgr); 72094332d3Sopenharmony_ci if (g_acmService == NULL) { 73094332d3Sopenharmony_ci HDF_LOGE("%{public}s: GetService err", __func__); 74094332d3Sopenharmony_ci return HDF_FAILURE; 75094332d3Sopenharmony_ci } 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_ci g_data = HdfSbufTypedObtain(SBUF_IPC); 78094332d3Sopenharmony_ci g_reply = HdfSbufTypedObtain(SBUF_IPC); 79094332d3Sopenharmony_ci if (g_data == NULL || g_reply == NULL) { 80094332d3Sopenharmony_ci HDF_LOGE("%{public}s: GetService err", __func__); 81094332d3Sopenharmony_ci return HDF_FAILURE; 82094332d3Sopenharmony_ci } 83094332d3Sopenharmony_ci 84094332d3Sopenharmony_ci status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply); 85094332d3Sopenharmony_ci if (status) { 86094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Dispatch USB_SERIAL_OPEN err", __func__); 87094332d3Sopenharmony_ci return HDF_FAILURE; 88094332d3Sopenharmony_ci } 89094332d3Sopenharmony_ci FILE *fp = fopen("/data/acm_read_xts", "a+"); 90094332d3Sopenharmony_ci if (fp == NULL) { 91094332d3Sopenharmony_ci HDF_LOGE("%{public}s: fopen err", __func__); 92094332d3Sopenharmony_ci return HDF_FAILURE; 93094332d3Sopenharmony_ci } 94094332d3Sopenharmony_ci while (1) { 95094332d3Sopenharmony_ci TestRead(fp); 96094332d3Sopenharmony_ci } 97094332d3Sopenharmony_ci (void)fclose(fp); 98094332d3Sopenharmony_ci status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply); 99094332d3Sopenharmony_ci if (status) { 100094332d3Sopenharmony_ci HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__); 101094332d3Sopenharmony_ci return HDF_FAILURE; 102094332d3Sopenharmony_ci } 103094332d3Sopenharmony_ci 104094332d3Sopenharmony_ci HdfSbufRecycle(g_data); 105094332d3Sopenharmony_ci HdfSbufRecycle(g_reply); 106094332d3Sopenharmony_ci 107094332d3Sopenharmony_ci return 0; 108094332d3Sopenharmony_ci} 109