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 <hdf_log.h> 19#include <hdf_remote_service.h> 20#include <hdf_sbuf.h> 21#include <osal_time.h> 22#include <servmgr_hdi.h> 23#include <signal.h> 24#include <unistd.h> 25#include "cdcacm.h" 26#include "usb_dev_test.h" 27 28#define HDF_LOG_TAG cdc_acm_speed_read 29 30static struct HdfSBuf *g_data; 31static struct HdfSBuf *g_reply; 32static struct HdfRemoteService *g_acmService; 33static bool g_readRuning = false; 34static void TestSpeed(void) 35{ 36 HdfSbufFlush(g_reply); 37 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, 38 USB_SERIAL_READ_SPEED, g_data, g_reply); 39 if (status) { 40 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %{public}d", 41 __func__, status); 42 return; 43 } 44} 45 46static void GetTempSpeed(void) 47{ 48 float speed; 49 HdfSbufFlush(g_reply); 50 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, 51 USB_SERIAL_READ_GET_TEMP_SPEED, g_data, g_reply); 52 if (status) { 53 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_GET_TEMP_SPEED failed status = %{public}d", 54 __func__, status); 55 return; 56 } 57 if (!HdfSbufReadFloat(g_reply, &speed)) { 58 HDF_LOGE("%{public}s: HdfSbufReadFloat failed", __func__); 59 return; 60 } 61 if (speed > 0) { 62 printf("speed : %f MB/s\n", speed); 63 } 64} 65 66static void ReadSpeedDone(void) 67{ 68 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, 69 USB_SERIAL_READ_SPEED_DONE, g_data, g_reply); 70 if (status) { 71 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %{public}d", 72 __func__, status); 73 return; 74 } 75} 76 77static void StopReadSpeedTest(int32_t signo) 78{ 79 ReadSpeedDone(); 80 g_readRuning = false; 81 printf("AcmSpeedRead exit.\n"); 82} 83 84int32_t AcmSpeedRead(int32_t argc, const char *argv[]) 85{ 86 (void)argc; 87 (void)argv; 88 int32_t status; 89 struct HDIServiceManager *servmgr = HDIServiceManagerGet(); 90 if (servmgr == NULL) { 91 HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__); 92 return HDF_FAILURE; 93 } 94 g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm"); 95 HDIServiceManagerRelease(servmgr); 96 if (g_acmService == NULL) { 97 HDF_LOGE("%{public}s: GetService err", __func__); 98 return HDF_FAILURE; 99 } 100 101 g_data = HdfSbufTypedObtain(SBUF_IPC); 102 g_reply = HdfSbufTypedObtain(SBUF_IPC); 103 if (g_data == NULL || g_reply == NULL) { 104 HDF_LOGE("%{public}s: GetService err", __func__); 105 return HDF_FAILURE; 106 } 107 108 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply); 109 if (status) { 110 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_OPEN err", __func__); 111 return HDF_FAILURE; 112 } 113 114 (void)signal(SIGINT, StopReadSpeedTest); 115 TestSpeed(); 116 g_readRuning = true; 117 while (g_readRuning) { 118 sleep(0x2); 119 if (g_readRuning) { 120 GetTempSpeed(); 121 } 122 } 123 124 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply); 125 if (status) { 126 HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__); 127 return HDF_FAILURE; 128 } 129 130 HdfSbufRecycle(g_data); 131 HdfSbufRecycle(g_reply); 132 133 return 0; 134} 135