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 <stdio.h>
21094332d3Sopenharmony_ci#include <sys/time.h>
22094332d3Sopenharmony_ci#include <unistd.h>
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci#include "cdcacm.h"
25094332d3Sopenharmony_ci#include "usb_dev_test.h"
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ci#define HDF_LOG_TAG cdc_acm_write
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_cistatic struct HdfSBuf *g_data;
30094332d3Sopenharmony_cistatic struct HdfSBuf *g_reply;
31094332d3Sopenharmony_cistatic struct HdfRemoteService *g_acmService;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_cistatic void TestWrite(char *buf)
34094332d3Sopenharmony_ci{
35094332d3Sopenharmony_ci    HdfSbufFlush(g_data);
36094332d3Sopenharmony_ci    (void)HdfSbufWriteString(g_data, buf);
37094332d3Sopenharmony_ci    int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_WRITE, g_data, g_reply);
38094332d3Sopenharmony_ci    if (status != HDF_SUCCESS) {
39094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE failed status = %{public}d", __func__, status);
40094332d3Sopenharmony_ci    }
41094332d3Sopenharmony_ci}
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci#define STR_LEN   1024
44094332d3Sopenharmony_ci#define NUM_INPUT 2
45094332d3Sopenharmony_ciint32_t AcmWrite(int32_t argc, const char *argv[])
46094332d3Sopenharmony_ci{
47094332d3Sopenharmony_ci    struct HDIServiceManager *servmgr = HDIServiceManagerGet();
48094332d3Sopenharmony_ci    if (servmgr == NULL) {
49094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__);
50094332d3Sopenharmony_ci        return HDF_FAILURE;
51094332d3Sopenharmony_ci    }
52094332d3Sopenharmony_ci    g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
53094332d3Sopenharmony_ci    HDIServiceManagerRelease(servmgr);
54094332d3Sopenharmony_ci    if (g_acmService == NULL) {
55094332d3Sopenharmony_ci        return HDF_FAILURE;
56094332d3Sopenharmony_ci    }
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    g_data = HdfSbufTypedObtain(SBUF_IPC);
59094332d3Sopenharmony_ci    g_reply = HdfSbufTypedObtain(SBUF_IPC);
60094332d3Sopenharmony_ci    if (g_data == NULL || g_reply == NULL) {
61094332d3Sopenharmony_ci        return HDF_FAILURE;
62094332d3Sopenharmony_ci    }
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_ci    int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
65094332d3Sopenharmony_ci    if (status != HDF_SUCCESS) {
66094332d3Sopenharmony_ci        return HDF_FAILURE;
67094332d3Sopenharmony_ci    }
68094332d3Sopenharmony_ci    if (argc >= NUM_INPUT) {
69094332d3Sopenharmony_ci        struct timeval time;
70094332d3Sopenharmony_ci        char str[STR_LEN] = {0};
71094332d3Sopenharmony_ci        gettimeofday(&time, NULL);
72094332d3Sopenharmony_ci        status = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, send data[%s] to host\n", time.tv_sec,
73094332d3Sopenharmony_ci            time.tv_usec, argv[1]);
74094332d3Sopenharmony_ci        if (status < 0) {
75094332d3Sopenharmony_ci            HDF_LOGE("%{public}s: snprintf_s failed", __func__);
76094332d3Sopenharmony_ci            return HDF_FAILURE;
77094332d3Sopenharmony_ci        }
78094332d3Sopenharmony_ci        FILE *fp = fopen("/data/acm_write_xts", "a+");
79094332d3Sopenharmony_ci        if (fp == NULL) {
80094332d3Sopenharmony_ci            HDF_LOGE("%{public}s: fopen failed", __func__);
81094332d3Sopenharmony_ci            return HDF_FAILURE;
82094332d3Sopenharmony_ci        }
83094332d3Sopenharmony_ci        (void)fwrite(str, strlen(str), 1, fp);
84094332d3Sopenharmony_ci        (void)fclose(fp);
85094332d3Sopenharmony_ci        TestWrite((char *)argv[1]);
86094332d3Sopenharmony_ci    }
87094332d3Sopenharmony_ci    status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
88094332d3Sopenharmony_ci    if (status) {
89094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__);
90094332d3Sopenharmony_ci        return HDF_FAILURE;
91094332d3Sopenharmony_ci    }
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    HdfSbufRecycle(g_data);
94094332d3Sopenharmony_ci    HdfSbufRecycle(g_reply);
95094332d3Sopenharmony_ci    return 0;
96094332d3Sopenharmony_ci}
97