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