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 <stdio.h>
17094332d3Sopenharmony_ci#include <sys/time.h>
18094332d3Sopenharmony_ci#include <hdf_log.h>
19094332d3Sopenharmony_ci#include <hdf_remote_service.h>
20094332d3Sopenharmony_ci#include <hdf_sbuf.h>
21094332d3Sopenharmony_ci#include <osal_time.h>
22094332d3Sopenharmony_ci#include <servmgr_hdi.h>
23094332d3Sopenharmony_ci#include <signal.h>
24094332d3Sopenharmony_ci#include <unistd.h>
25094332d3Sopenharmony_ci#include "cdcacm.h"
26094332d3Sopenharmony_ci#include "usb_dev_test.h"
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ci#define HDF_LOG_TAG   cdc_acm_speed_read
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_cistatic struct HdfSBuf *g_data;
31094332d3Sopenharmony_cistatic struct HdfSBuf *g_reply;
32094332d3Sopenharmony_cistatic struct HdfRemoteService *g_acmService;
33094332d3Sopenharmony_cistatic bool g_readRuning = false;
34094332d3Sopenharmony_cistatic void TestSpeed(void)
35094332d3Sopenharmony_ci{
36094332d3Sopenharmony_ci    HdfSbufFlush(g_reply);
37094332d3Sopenharmony_ci    int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
38094332d3Sopenharmony_ci        USB_SERIAL_READ_SPEED, g_data, g_reply);
39094332d3Sopenharmony_ci    if (status) {
40094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %{public}d",
41094332d3Sopenharmony_ci            __func__, status);
42094332d3Sopenharmony_ci        return;
43094332d3Sopenharmony_ci    }
44094332d3Sopenharmony_ci}
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_cistatic void GetTempSpeed(void)
47094332d3Sopenharmony_ci{
48094332d3Sopenharmony_ci    float speed;
49094332d3Sopenharmony_ci    HdfSbufFlush(g_reply);
50094332d3Sopenharmony_ci    int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
51094332d3Sopenharmony_ci        USB_SERIAL_READ_GET_TEMP_SPEED, g_data, g_reply);
52094332d3Sopenharmony_ci    if (status) {
53094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_GET_TEMP_SPEED failed status = %{public}d",
54094332d3Sopenharmony_ci            __func__, status);
55094332d3Sopenharmony_ci        return;
56094332d3Sopenharmony_ci    }
57094332d3Sopenharmony_ci    if (!HdfSbufReadFloat(g_reply, &speed)) {
58094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: HdfSbufReadFloat failed", __func__);
59094332d3Sopenharmony_ci        return;
60094332d3Sopenharmony_ci    }
61094332d3Sopenharmony_ci    if (speed > 0) {
62094332d3Sopenharmony_ci        printf("speed : %f MB/s\n", speed);
63094332d3Sopenharmony_ci    }
64094332d3Sopenharmony_ci}
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_cistatic void ReadSpeedDone(void)
67094332d3Sopenharmony_ci{
68094332d3Sopenharmony_ci    int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
69094332d3Sopenharmony_ci        USB_SERIAL_READ_SPEED_DONE, g_data, g_reply);
70094332d3Sopenharmony_ci    if (status) {
71094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %{public}d",
72094332d3Sopenharmony_ci            __func__, status);
73094332d3Sopenharmony_ci        return;
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci}
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_cistatic void StopReadSpeedTest(int32_t signo)
78094332d3Sopenharmony_ci{
79094332d3Sopenharmony_ci    ReadSpeedDone();
80094332d3Sopenharmony_ci    g_readRuning = false;
81094332d3Sopenharmony_ci    printf("AcmSpeedRead exit.\n");
82094332d3Sopenharmony_ci}
83094332d3Sopenharmony_ci
84094332d3Sopenharmony_ciint32_t AcmSpeedRead(int32_t argc, const char *argv[])
85094332d3Sopenharmony_ci{
86094332d3Sopenharmony_ci    (void)argc;
87094332d3Sopenharmony_ci    (void)argv;
88094332d3Sopenharmony_ci    int32_t status;
89094332d3Sopenharmony_ci    struct HDIServiceManager *servmgr = HDIServiceManagerGet();
90094332d3Sopenharmony_ci    if (servmgr == NULL) {
91094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__);
92094332d3Sopenharmony_ci        return HDF_FAILURE;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci    g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
95094332d3Sopenharmony_ci    HDIServiceManagerRelease(servmgr);
96094332d3Sopenharmony_ci    if (g_acmService == NULL) {
97094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: GetService err", __func__);
98094332d3Sopenharmony_ci        return HDF_FAILURE;
99094332d3Sopenharmony_ci    }
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    g_data = HdfSbufTypedObtain(SBUF_IPC);
102094332d3Sopenharmony_ci    g_reply = HdfSbufTypedObtain(SBUF_IPC);
103094332d3Sopenharmony_ci    if (g_data == NULL || g_reply == NULL) {
104094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: GetService err", __func__);
105094332d3Sopenharmony_ci        return HDF_FAILURE;
106094332d3Sopenharmony_ci    }
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_ci    status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
109094332d3Sopenharmony_ci    if (status) {
110094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_OPEN err", __func__);
111094332d3Sopenharmony_ci        return HDF_FAILURE;
112094332d3Sopenharmony_ci    }
113094332d3Sopenharmony_ci
114094332d3Sopenharmony_ci    (void)signal(SIGINT, StopReadSpeedTest);
115094332d3Sopenharmony_ci    TestSpeed();
116094332d3Sopenharmony_ci    g_readRuning = true;
117094332d3Sopenharmony_ci    while (g_readRuning) {
118094332d3Sopenharmony_ci        sleep(0x2);
119094332d3Sopenharmony_ci        if (g_readRuning) {
120094332d3Sopenharmony_ci            GetTempSpeed();
121094332d3Sopenharmony_ci        }
122094332d3Sopenharmony_ci    }
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ci    status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
125094332d3Sopenharmony_ci    if (status) {
126094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__);
127094332d3Sopenharmony_ci        return HDF_FAILURE;
128094332d3Sopenharmony_ci    }
129094332d3Sopenharmony_ci
130094332d3Sopenharmony_ci    HdfSbufRecycle(g_data);
131094332d3Sopenharmony_ci    HdfSbufRecycle(g_reply);
132094332d3Sopenharmony_ci
133094332d3Sopenharmony_ci    return 0;
134094332d3Sopenharmony_ci}
135