13f085823Sopenharmony_ci/*
23f085823Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f085823Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f085823Sopenharmony_ci * you may not use this file except in compliance with the License.
53f085823Sopenharmony_ci * You may obtain a copy of the License at
63f085823Sopenharmony_ci *
73f085823Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f085823Sopenharmony_ci *
93f085823Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f085823Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f085823Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f085823Sopenharmony_ci * See the License for the specific language governing permissions and
133f085823Sopenharmony_ci * limitations under the License.
143f085823Sopenharmony_ci */
153f085823Sopenharmony_ci
163f085823Sopenharmony_ci#include "distributed_major.h"
173f085823Sopenharmony_ci
183f085823Sopenharmony_ci#include <cstring>
193f085823Sopenharmony_ci#include <map>
203f085823Sopenharmony_ci#include <cerrno>
213f085823Sopenharmony_ci#include <cassert>
223f085823Sopenharmony_ci#include <cstdio>
233f085823Sopenharmony_ci#include <cstdlib>
243f085823Sopenharmony_ci
253f085823Sopenharmony_ci#include <unistd.h>
263f085823Sopenharmony_ci#include <poll.h>
273f085823Sopenharmony_ci#include <sys/socket.h>
283f085823Sopenharmony_ci#include <netinet/in.h>
293f085823Sopenharmony_ci#include <arpa/inet.h>
303f085823Sopenharmony_ci#include "securec.h"
313f085823Sopenharmony_ci
323f085823Sopenharmony_cinamespace OHOS {
333f085823Sopenharmony_cinamespace DistributeSystemTest {
343f085823Sopenharmony_ciusing namespace std;
353f085823Sopenharmony_ciusing namespace testing;
363f085823Sopenharmony_ciusing namespace OHOS::HiviewDFX;
373f085823Sopenharmony_ciDistributeTestEnvironment  *g_pDistributetestEnv = nullptr;
383f085823Sopenharmony_cinamespace {
393f085823Sopenharmony_ci    const int CONNECT_TIME = 3;
403f085823Sopenharmony_ci    const int SLEEP_TIME = 1000;
413f085823Sopenharmony_ci    const int HALF_BUF_LEN = 2;
423f085823Sopenharmony_ci    const int CMD_LENGTH = 50;
433f085823Sopenharmony_ci}
443f085823Sopenharmony_ci
453f085823Sopenharmony_ciDistributeTestEnvironment::DistributeTestEnvironment() : serverPort_(DEFAULT_AGENT_PORT)
463f085823Sopenharmony_ci{
473f085823Sopenharmony_ci}
483f085823Sopenharmony_ci
493f085823Sopenharmony_ciDistributeTestEnvironment::DistributeTestEnvironment(std::string cfgFile) : serverPort_(DEFAULT_AGENT_PORT)
503f085823Sopenharmony_ci{
513f085823Sopenharmony_ci    Init(cfgFile);
523f085823Sopenharmony_ci}
533f085823Sopenharmony_ci
543f085823Sopenharmony_civoid DistributeTestEnvironment::Init(std::string fileName)
553f085823Sopenharmony_ci{
563f085823Sopenharmony_ci    clientCfg_.OpenCfg(fileName);
573f085823Sopenharmony_ci    std::string iplist;
583f085823Sopenharmony_ci    if (!clientCfg_.GetCfgVal("agentlist", iplist)) {
593f085823Sopenharmony_ci        return;
603f085823Sopenharmony_ci    }
613f085823Sopenharmony_ci    std::string::size_type posend = 0;
623f085823Sopenharmony_ci    std::string::size_type pos = 0;
633f085823Sopenharmony_ci    do {
643f085823Sopenharmony_ci        std::string ipaddr;
653f085823Sopenharmony_ci        posend = iplist.find(",", pos);
663f085823Sopenharmony_ci        if (posend != std::string::npos) {
673f085823Sopenharmony_ci            ipaddr = iplist.substr(pos, posend - pos);
683f085823Sopenharmony_ci        } else {
693f085823Sopenharmony_ci            ipaddr = iplist.substr(pos);
703f085823Sopenharmony_ci        }
713f085823Sopenharmony_ci        AddClient(ipaddr);
723f085823Sopenharmony_ci        if (posend >= iplist.length()) {
733f085823Sopenharmony_ci            break;
743f085823Sopenharmony_ci        }
753f085823Sopenharmony_ci        pos = posend + 1;
763f085823Sopenharmony_ci    } while (posend != std::string::npos);
773f085823Sopenharmony_ci    std::string strPort;
783f085823Sopenharmony_ci    if (!clientCfg_.GetCfgVal("agentport", strPort)) {
793f085823Sopenharmony_ci        return;
803f085823Sopenharmony_ci    }
813f085823Sopenharmony_ci    if (sscanf_s(strPort.c_str(), "%d", &serverPort_) < 1) {
823f085823Sopenharmony_ci        serverPort_ = DEFAULT_AGENT_PORT;
833f085823Sopenharmony_ci    }
843f085823Sopenharmony_ci    HiLog::Info(DistributeTestEnvironment::LABEL, "get device port :  %d", serverPort_);
853f085823Sopenharmony_ci}
863f085823Sopenharmony_ci
873f085823Sopenharmony_ciDistributeTestEnvironment::~DistributeTestEnvironment()
883f085823Sopenharmony_ci{
893f085823Sopenharmony_ci}
903f085823Sopenharmony_ci
913f085823Sopenharmony_ciint DistributeTestEnvironment::GetSerial()
923f085823Sopenharmony_ci{
933f085823Sopenharmony_ci    static int serialno = 0;
943f085823Sopenharmony_ci    return serialno++;
953f085823Sopenharmony_ci}
963f085823Sopenharmony_ci
973f085823Sopenharmony_ciint DistributeTestEnvironment::AddClient(std::string ipAddr)
983f085823Sopenharmony_ci{
993f085823Sopenharmony_ci    int count = clientList_.size();
1003f085823Sopenharmony_ci    struct sockaddr_in addr;
1013f085823Sopenharmony_ci    if (inet_pton(AF_INET, ipAddr.c_str(), &addr.sin_addr) == 1) {
1023f085823Sopenharmony_ci        DistDeviceInfo dev;
1033f085823Sopenharmony_ci        dev.devNo = count;
1043f085823Sopenharmony_ci        dev.ipAddr = ipAddr;
1053f085823Sopenharmony_ci        dev.fd = -1;
1063f085823Sopenharmony_ci        clientList_.push_back(dev);
1073f085823Sopenharmony_ci        count++;
1083f085823Sopenharmony_ci    } else {
1093f085823Sopenharmony_ci        return 0;
1103f085823Sopenharmony_ci    }
1113f085823Sopenharmony_ci    return 1;
1123f085823Sopenharmony_ci}
1133f085823Sopenharmony_ci
1143f085823Sopenharmony_ciint DistributeTestEnvironment::ConnectAgent(size_t devNo)
1153f085823Sopenharmony_ci{
1163f085823Sopenharmony_ci    if (devNo >= clientList_.size()) {
1173f085823Sopenharmony_ci        return 0;
1183f085823Sopenharmony_ci    }
1193f085823Sopenharmony_ci    std::string serverIp = clientList_[devNo].ipAddr;
1203f085823Sopenharmony_ci    struct sockaddr_in addr;
1213f085823Sopenharmony_ci    int clientSockFd = socket(AF_INET, SOCK_STREAM, 0);
1223f085823Sopenharmony_ci    if (clientSockFd < 0) {
1233f085823Sopenharmony_ci        return -1;
1243f085823Sopenharmony_ci    }
1253f085823Sopenharmony_ci    bzero(&addr, sizeof(addr));
1263f085823Sopenharmony_ci    addr.sin_family = AF_INET;
1273f085823Sopenharmony_ci    inet_pton(AF_INET, serverIp.c_str(), &addr.sin_addr);
1283f085823Sopenharmony_ci    addr.sin_port = htons(serverPort_);
1293f085823Sopenharmony_ci    int connectCount = 0;
1303f085823Sopenharmony_ci    for (connectCount = 0; connectCount < CONNECT_TIME; connectCount++) {  // try connect to agent 3 times.
1313f085823Sopenharmony_ci        if (!connect(clientSockFd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr))) {
1323f085823Sopenharmony_ci            break;
1333f085823Sopenharmony_ci        }
1343f085823Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));  // delay 10ms
1353f085823Sopenharmony_ci    }
1363f085823Sopenharmony_ci    if (connectCount >= CONNECT_TIME) {
1373f085823Sopenharmony_ci        HiLog::Error(DistributeTestEnvironment::LABEL, "connect to agent %s fail.", serverIp.c_str());
1383f085823Sopenharmony_ci        close(clientSockFd);
1393f085823Sopenharmony_ci        clientSockFd = -1;
1403f085823Sopenharmony_ci        return 0;
1413f085823Sopenharmony_ci    }
1423f085823Sopenharmony_ci    HiLog::Info(DistributeTestEnvironment::LABEL, "connect to agent %s success.", serverIp.c_str());
1433f085823Sopenharmony_ci    clientList_[devNo].fd = clientSockFd;
1443f085823Sopenharmony_ci    return 1;
1453f085823Sopenharmony_ci}
1463f085823Sopenharmony_ci
1473f085823Sopenharmony_civoid DistributeTestEnvironment::SetUp()
1483f085823Sopenharmony_ci{
1493f085823Sopenharmony_ci    // initial connect agent;
1503f085823Sopenharmony_ci    size_t clientNo;
1513f085823Sopenharmony_ci    for (clientNo = 0; clientNo < clientList_.size(); clientNo++) {
1523f085823Sopenharmony_ci        // create connect to agent of ipaddress is , port is 8642.
1533f085823Sopenharmony_ci        ConnectAgent(clientNo);
1543f085823Sopenharmony_ci    }
1553f085823Sopenharmony_ci}
1563f085823Sopenharmony_ci
1573f085823Sopenharmony_civoid DistributeTestEnvironment::TearDown()
1583f085823Sopenharmony_ci{
1593f085823Sopenharmony_ci    size_t clientNo;
1603f085823Sopenharmony_ci    for (clientNo = 0; clientNo < clientList_.size(); clientNo++) {
1613f085823Sopenharmony_ci        close(clientList_[clientNo].fd); // close socket
1623f085823Sopenharmony_ci        clientList_[clientNo].fd = -1;
1633f085823Sopenharmony_ci    }
1643f085823Sopenharmony_ci}
1653f085823Sopenharmony_ci
1663f085823Sopenharmony_cibool DistributeTestEnvironment::SendToAgent(size_t devNo, int cmdType, void *pstrMsg, int len,
1673f085823Sopenharmony_ci    std::function<bool(const std::string &, int)> onProcessReturn)
1683f085823Sopenharmony_ci{
1693f085823Sopenharmony_ci    static int globalCommandNo = 0;
1703f085823Sopenharmony_ci    bool breturn = false;
1713f085823Sopenharmony_ci    devNo = devNo - 1;
1723f085823Sopenharmony_ci    if (devNo >= clientList_.size()) {
1733f085823Sopenharmony_ci        HiLog::Info(DistributeTestEnvironment::LABEL, "can not find no %zu device.", devNo);
1743f085823Sopenharmony_ci        return breturn;
1753f085823Sopenharmony_ci    }
1763f085823Sopenharmony_ci    if (clientList_[devNo].fd <= 0) {
1773f085823Sopenharmony_ci        HiLog::Info(DistributeTestEnvironment::LABEL, "connect is failure %zu device.", devNo);
1783f085823Sopenharmony_ci        return breturn;
1793f085823Sopenharmony_ci    }
1803f085823Sopenharmony_ci    if (pstrMsg == nullptr) {
1813f085823Sopenharmony_ci        return false;
1823f085823Sopenharmony_ci    }
1833f085823Sopenharmony_ci    globalCommandNo++;
1843f085823Sopenharmony_ci    char szrbuf[MAX_BUFF_LEN] = {0};
1853f085823Sopenharmony_ci    auto pCmdMsg = reinterpret_cast<DistributedMsg *>(pstrMsg);
1863f085823Sopenharmony_ci    pCmdMsg->no = globalCommandNo;
1873f085823Sopenharmony_ci    pCmdMsg->cmdTestType = htons(cmdType);
1883f085823Sopenharmony_ci    pCmdMsg->len = htons(len);
1893f085823Sopenharmony_ci    int rlen = send(clientList_[devNo].fd, pCmdMsg, static_cast<size_t>(len + DST_COMMAND_HEAD_LEN), 0);
1903f085823Sopenharmony_ci    if (rlen <= 0) {
1913f085823Sopenharmony_ci        HiLog::Error(DistributeTestEnvironment::LABEL, "agent socket is closed.");
1923f085823Sopenharmony_ci        return breturn;
1933f085823Sopenharmony_ci    }
1943f085823Sopenharmony_ci    // get ret value ;
1953f085823Sopenharmony_ci    switch (cmdType) {
1963f085823Sopenharmony_ci        case DST_COMMAND_CALL:
1973f085823Sopenharmony_ci        case DST_COMMAND_MSG: {
1983f085823Sopenharmony_ci            int times = CONNECT_TIME;
1993f085823Sopenharmony_ci            while (times > 0) {
2003f085823Sopenharmony_ci                rlen = recv(clientList_[devNo].fd, szrbuf, DST_COMMAND_HEAD_LEN, 0);
2013f085823Sopenharmony_ci                if (static_cast<unsigned long>(rlen) >= DST_COMMAND_HEAD_LEN) {
2023f085823Sopenharmony_ci                    auto pCmdTest = reinterpret_cast<DistributedMsg *>(szrbuf);
2033f085823Sopenharmony_ci                    pCmdTest->cmdTestType = ntohs(pCmdTest->cmdTestType);
2043f085823Sopenharmony_ci                    pCmdTest->len = ntohs(pCmdTest->len);
2053f085823Sopenharmony_ci                    if (pCmdTest->len <= 0) {
2063f085823Sopenharmony_ci                        times--;
2073f085823Sopenharmony_ci                        continue;
2083f085823Sopenharmony_ci                    }
2093f085823Sopenharmony_ci                    recv(clientList_[devNo].fd, pCmdTest->alignmentCmd, pCmdTest->len, 0);
2103f085823Sopenharmony_ci                    HiLog::Info(DistributeTestEnvironment::LABEL, "recv agent data : No.%d command type :%d length :%d",
2113f085823Sopenharmony_ci                        pCmdTest->no, pCmdTest->cmdTestType, pCmdTest->len);
2123f085823Sopenharmony_ci                    if ((globalCommandNo == pCmdTest->no) && (cmdType == pCmdTest->cmdTestType)) {
2133f085823Sopenharmony_ci                        // get ret value ;
2143f085823Sopenharmony_ci                        if (onProcessReturn != nullptr) {
2153f085823Sopenharmony_ci                            breturn = onProcessReturn(pCmdTest->alignmentCmd, pCmdTest->len);
2163f085823Sopenharmony_ci                        } else {
2173f085823Sopenharmony_ci                            breturn = true;
2183f085823Sopenharmony_ci                        }
2193f085823Sopenharmony_ci                        break;
2203f085823Sopenharmony_ci                    } else {
2213f085823Sopenharmony_ci                        HiLog::Error(DistributeTestEnvironment::LABEL, "get error message. type is :%d",
2223f085823Sopenharmony_ci                            pCmdTest->cmdTestType);
2233f085823Sopenharmony_ci                    }
2243f085823Sopenharmony_ci                } else {
2253f085823Sopenharmony_ci                    if (!rlen) {
2263f085823Sopenharmony_ci                        // peer socket is closed.
2273f085823Sopenharmony_ci                        HiLog::Error(DistributeTestEnvironment::LABEL, "device socket close.");
2283f085823Sopenharmony_ci                        break;
2293f085823Sopenharmony_ci                    }
2303f085823Sopenharmony_ci                }
2313f085823Sopenharmony_ci                usleep(SLEEP_TIME);
2323f085823Sopenharmony_ci                times--;
2333f085823Sopenharmony_ci            }
2343f085823Sopenharmony_ci            break;
2353f085823Sopenharmony_ci        }
2363f085823Sopenharmony_ci        default: {
2373f085823Sopenharmony_ci            breturn = true;
2383f085823Sopenharmony_ci            break;
2393f085823Sopenharmony_ci        }
2403f085823Sopenharmony_ci    }
2413f085823Sopenharmony_ci    return breturn;
2423f085823Sopenharmony_ci}
2433f085823Sopenharmony_ci
2443f085823Sopenharmony_cibool DistributeTestEnvironment::RunTestCmd(size_t devNo, const std::string &strCommand, int cmdLen,
2453f085823Sopenharmony_ci    const std::string &strExpectValue, int expectValueLen,
2463f085823Sopenharmony_ci    std::function<bool(const std::string &, int)> onProcessReturn)
2473f085823Sopenharmony_ci{
2483f085823Sopenharmony_ci    // send command data length
2493f085823Sopenharmony_ci    char szbuf[MAX_BUFF_LEN];
2503f085823Sopenharmony_ci    bool breturn = false;
2513f085823Sopenharmony_ci    size_t lenptr = 0;
2523f085823Sopenharmony_ci    errno_t ret = EOK;
2533f085823Sopenharmony_ci    ret = memset_s(szbuf, sizeof(szbuf), 0, MAX_BUFF_LEN);
2543f085823Sopenharmony_ci    if (ret != EOK) {
2553f085823Sopenharmony_ci        return breturn;
2563f085823Sopenharmony_ci    }
2573f085823Sopenharmony_ci    // add 2 '\0'
2583f085823Sopenharmony_ci    size_t rlen = cmdLen + expectValueLen + DST_COMMAND_HEAD_LEN + sizeof(int) * HALF_BUF_LEN + HALF_BUF_LEN;
2593f085823Sopenharmony_ci    if (rlen <= MAX_BUFF_LEN) {
2603f085823Sopenharmony_ci        auto pCmdTest = reinterpret_cast<DistributedMsg *>(szbuf);
2613f085823Sopenharmony_ci        pCmdTest->cmdTestType = DST_COMMAND_CALL;
2623f085823Sopenharmony_ci
2633f085823Sopenharmony_ci        // alignmentCmd buff format:
2643f085823Sopenharmony_ci        // cmd_size:int, cmd string, '\0' expectvalue_size:int
2653f085823Sopenharmony_ci        // expectvalue string, '\0'
2663f085823Sopenharmony_ci        lenptr = 0;
2673f085823Sopenharmony_ci        *reinterpret_cast<int *>(pCmdTest->alignmentCmd + lenptr) = htons(cmdLen);
2683f085823Sopenharmony_ci        lenptr += sizeof(int);
2693f085823Sopenharmony_ci        ret = memcpy_s(pCmdTest->alignmentCmd + lenptr, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN - lenptr,
2703f085823Sopenharmony_ci            strCommand.c_str(), cmdLen);
2713f085823Sopenharmony_ci        if (ret != EOK) {
2723f085823Sopenharmony_ci            return breturn;
2733f085823Sopenharmony_ci        }
2743f085823Sopenharmony_ci        lenptr += cmdLen + 1;
2753f085823Sopenharmony_ci        *reinterpret_cast<int *>(pCmdTest->alignmentCmd + lenptr) = htons(expectValueLen);
2763f085823Sopenharmony_ci        lenptr += sizeof(int);
2773f085823Sopenharmony_ci        ret = memcpy_s(pCmdTest->alignmentCmd + lenptr, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN - lenptr,
2783f085823Sopenharmony_ci            strExpectValue.c_str(), expectValueLen);
2793f085823Sopenharmony_ci        if (ret != EOK) {
2803f085823Sopenharmony_ci            return breturn;
2813f085823Sopenharmony_ci        }
2823f085823Sopenharmony_ci        lenptr += expectValueLen + 1;
2833f085823Sopenharmony_ci        pCmdTest->len =  lenptr;
2843f085823Sopenharmony_ci        breturn = SendToAgent(devNo, DST_COMMAND_CALL, pCmdTest, pCmdTest->len, onProcessReturn);
2853f085823Sopenharmony_ci    } else {
2863f085823Sopenharmony_ci        HiLog::Error(DistributeTestEnvironment::LABEL, "command data is too long \n");
2873f085823Sopenharmony_ci    }
2883f085823Sopenharmony_ci    return breturn;
2893f085823Sopenharmony_ci};
2903f085823Sopenharmony_ci
2913f085823Sopenharmony_cibool DistributeTestEnvironment::SendMessage(size_t devNo, const std::string &strMsg, int msgLen,
2923f085823Sopenharmony_ci    std::function<bool(const std::string &, int)> onProcessReturnMsg)
2933f085823Sopenharmony_ci{
2943f085823Sopenharmony_ci    bool breturn = false;
2953f085823Sopenharmony_ci    if ((msgLen + DST_COMMAND_HEAD_LEN) <= MAX_BUFF_LEN) {
2963f085823Sopenharmony_ci        char szbuf[MAX_BUFF_LEN] = {0};
2973f085823Sopenharmony_ci        auto pCmdTest = reinterpret_cast<DistributedMsg *>(szbuf);
2983f085823Sopenharmony_ci        pCmdTest->cmdTestType = DST_COMMAND_MSG;
2993f085823Sopenharmony_ci        errno_t ret = memcpy_s(pCmdTest->alignmentCmd, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN, strMsg.c_str(), msgLen);
3003f085823Sopenharmony_ci        if (ret != EOK) {
3013f085823Sopenharmony_ci            return breturn;
3023f085823Sopenharmony_ci        }
3033f085823Sopenharmony_ci        pCmdTest->len = msgLen;
3043f085823Sopenharmony_ci        breturn = SendToAgent(devNo, DST_COMMAND_MSG, pCmdTest, msgLen, onProcessReturnMsg);
3053f085823Sopenharmony_ci    } else {
3063f085823Sopenharmony_ci        HiLog::Info(DistributeTestEnvironment::LABEL, "message data is too long.\n");
3073f085823Sopenharmony_ci    }
3083f085823Sopenharmony_ci    return breturn;
3093f085823Sopenharmony_ci}
3103f085823Sopenharmony_ci
3113f085823Sopenharmony_cibool DistributeTestEnvironment::Notify(size_t devNo, const std::string &strMsg, int msgLen)
3123f085823Sopenharmony_ci{
3133f085823Sopenharmony_ci    int dstMax = MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN;
3143f085823Sopenharmony_ci    if (msgLen < 0 || msgLen >= dstMax) {
3153f085823Sopenharmony_ci        return false;
3163f085823Sopenharmony_ci    }
3173f085823Sopenharmony_ci
3183f085823Sopenharmony_ci    bool breturn = false;
3193f085823Sopenharmony_ci    if ((msgLen + DST_COMMAND_HEAD_LEN) <= MAX_BUFF_LEN) {
3203f085823Sopenharmony_ci        char szbuf[MAX_BUFF_LEN] = {0};
3213f085823Sopenharmony_ci        auto pCmdTest = reinterpret_cast<DistributedMsg *>(szbuf);
3223f085823Sopenharmony_ci        pCmdTest->cmdTestType = DST_COMMAND_NOTIFY;
3233f085823Sopenharmony_ci        errno_t ret = memcpy_s(pCmdTest->alignmentCmd, dstMax, strMsg.c_str(), msgLen);
3243f085823Sopenharmony_ci        if (ret != EOK) {
3253f085823Sopenharmony_ci            return breturn;
3263f085823Sopenharmony_ci        }
3273f085823Sopenharmony_ci        pCmdTest->alignmentCmd[msgLen] = 0;
3283f085823Sopenharmony_ci        pCmdTest->len = msgLen;
3293f085823Sopenharmony_ci        breturn = SendToAgent(devNo, DST_COMMAND_NOTIFY, pCmdTest, msgLen, nullptr);
3303f085823Sopenharmony_ci    } else {
3313f085823Sopenharmony_ci        HiLog::Info(DistributeTestEnvironment::LABEL, "notify data is too long.\n");
3323f085823Sopenharmony_ci    }
3333f085823Sopenharmony_ci    return breturn;
3343f085823Sopenharmony_ci}
3353f085823Sopenharmony_ci
3363f085823Sopenharmony_ciDistributeTest::DistributeTest()
3373f085823Sopenharmony_ci{
3383f085823Sopenharmony_ci    returnVal_ = 0;
3393f085823Sopenharmony_ci}
3403f085823Sopenharmony_ci
3413f085823Sopenharmony_ciDistributeTest::~DistributeTest()
3423f085823Sopenharmony_ci{
3433f085823Sopenharmony_ci}
3443f085823Sopenharmony_ci
3453f085823Sopenharmony_ciint DistributeTest::CheckStatus()
3463f085823Sopenharmony_ci{
3473f085823Sopenharmony_ci    return 0;
3483f085823Sopenharmony_ci}
3493f085823Sopenharmony_ci
3503f085823Sopenharmony_ci/*
3513f085823Sopenharmony_ci * function : the testcase execute command on agent device, tell device something, agent process it.
3523f085823Sopenharmony_ci *     this interface is opened for user.
3533f085823Sopenharmony_ci * param :
3543f085823Sopenharmony_ci *     devNo: agent device serial number.
3553f085823Sopenharmony_ci *     strCommand: command of the testcase to send.
3563f085823Sopenharmony_ci *     cmdLen : length of command
3573f085823Sopenharmony_ci *     strExpectValue: expected return value
3583f085823Sopenharmony_ci *     expectValueLen: real length of return value
3593f085823Sopenharmony_ci * return : if false is return, execute operation failed.
3603f085823Sopenharmony_ci */
3613f085823Sopenharmony_cibool DistributeTest::RunCmdOnAgent(AGENT_NO devNo, const std::string &strCommand, int cmdLen,
3623f085823Sopenharmony_ci    const std::string &strExpectValue, int expectValueLen)
3633f085823Sopenharmony_ci{
3643f085823Sopenharmony_ci    if (g_pDistributetestEnv != nullptr) {
3653f085823Sopenharmony_ci        return g_pDistributetestEnv->RunTestCmd(devNo, strCommand, cmdLen, strExpectValue, expectValueLen,
3663f085823Sopenharmony_ci            [&](const std::string &strReturn, int strLen)->bool {
3673f085823Sopenharmony_ci                return OnProcessValue(strReturn, strLen);
3683f085823Sopenharmony_ci            });
3693f085823Sopenharmony_ci    }
3703f085823Sopenharmony_ci    return false;
3713f085823Sopenharmony_ci}
3723f085823Sopenharmony_ci
3733f085823Sopenharmony_ci/*
3743f085823Sopenharmony_ci * function : the testcase execute command on agent device, include command parameter.
3753f085823Sopenharmony_ci *     this interface is opened for user.
3763f085823Sopenharmony_ci * param :
3773f085823Sopenharmony_ci *     devNo: agent device serial number.
3783f085823Sopenharmony_ci *     strCmd: command of the testcase to send.
3793f085823Sopenharmony_ci *     strArgs: command parameter.
3803f085823Sopenharmony_ci *     strExpectValue: expected return value
3813f085823Sopenharmony_ci * return : if false is return, execute operation failed.
3823f085823Sopenharmony_ci */
3833f085823Sopenharmony_cibool DistributeTest::RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs,
3843f085823Sopenharmony_ci    const std::string &strExpectValue)
3853f085823Sopenharmony_ci{
3863f085823Sopenharmony_ci    if (g_pDistributetestEnv != nullptr) {
3873f085823Sopenharmony_ci        std::string strBuf = strCmd + ":" + strArgs;
3883f085823Sopenharmony_ci        int cmdLen = strBuf.length() + 1;
3893f085823Sopenharmony_ci        int expectValueLen = strExpectValue.length() + 1;
3903f085823Sopenharmony_ci        return g_pDistributetestEnv->RunTestCmd(devNo, strBuf, cmdLen, strExpectValue, expectValueLen,
3913f085823Sopenharmony_ci            [&](const std::string &strReturn, int strLen)->bool {
3923f085823Sopenharmony_ci                return OnProcessValue(strReturn, strLen);
3933f085823Sopenharmony_ci            });
3943f085823Sopenharmony_ci    }
3953f085823Sopenharmony_ci    return false;
3963f085823Sopenharmony_ci}
3973f085823Sopenharmony_ci
3983f085823Sopenharmony_ci/*
3993f085823Sopenharmony_ci * function : the testcase execute command on agent device, include command parameter and callback.
4003f085823Sopenharmony_ci *     this interface is opened for user.
4013f085823Sopenharmony_ci * param :
4023f085823Sopenharmony_ci *     devNo: agent device serial number.
4033f085823Sopenharmony_ci *     strCmd: command of the testcase to send.
4043f085823Sopenharmony_ci *     strArgs: command parameter.
4053f085823Sopenharmony_ci *     strExpectValue: expected return value
4063f085823Sopenharmony_ci *     onReturnCall: callback function to handle return value and real length of return value.
4073f085823Sopenharmony_ci * return : if false is return, execute operation failed.
4083f085823Sopenharmony_ci */
4093f085823Sopenharmony_cibool DistributeTest::RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs,
4103f085823Sopenharmony_ci    const std::string &strExpectValue, std::function<bool(const std::string &, int)> onReturnCall)
4113f085823Sopenharmony_ci{
4123f085823Sopenharmony_ci    if (g_pDistributetestEnv != nullptr) {
4133f085823Sopenharmony_ci        std::string strBuf = strCmd + ":" + strArgs;
4143f085823Sopenharmony_ci        int cmdLen = strBuf.length() + 1;
4153f085823Sopenharmony_ci        int expectValueLen = strExpectValue.length() + 1;
4163f085823Sopenharmony_ci        return g_pDistributetestEnv->RunTestCmd(devNo, strBuf, cmdLen, strExpectValue, expectValueLen, onReturnCall);
4173f085823Sopenharmony_ci    }
4183f085823Sopenharmony_ci    return false;
4193f085823Sopenharmony_ci}
4203f085823Sopenharmony_ci
4213f085823Sopenharmony_cibool DistributeTest::OnProcessValue(const std::string &szbuf, int len)
4223f085823Sopenharmony_ci{
4233f085823Sopenharmony_ci    if (szbuf == "") {
4243f085823Sopenharmony_ci        return false;
4253f085823Sopenharmony_ci    }
4263f085823Sopenharmony_ci    if (sscanf_s(szbuf.c_str(), "%d", &returnVal_) < 1) {
4273f085823Sopenharmony_ci        return false;
4283f085823Sopenharmony_ci    }
4293f085823Sopenharmony_ci    return true;
4303f085823Sopenharmony_ci}
4313f085823Sopenharmony_ci
4323f085823Sopenharmony_ciint DistributeTest::GetReturnVal()
4333f085823Sopenharmony_ci{
4343f085823Sopenharmony_ci    return returnVal_;
4353f085823Sopenharmony_ci}
4363f085823Sopenharmony_ci
4373f085823Sopenharmony_ci/*
4383f085823Sopenharmony_ci * function : testcase send message to agent device, tell agent device something, agent process it.
4393f085823Sopenharmony_ci *     this interface is opened for user.
4403f085823Sopenharmony_ci * param :
4413f085823Sopenharmony_ci *     devNo: the serial number of agent device.
4423f085823Sopenharmony_ci *     msg : message of the testcase sent to the agent
4433f085823Sopenharmony_ci *     len: length of strMsg
4443f085823Sopenharmony_ci * return : if false is return, send operation failed.
4453f085823Sopenharmony_ci */
4463f085823Sopenharmony_cibool DistributeTest::SendMessage(AGENT_NO devNo, const std::string &msg, int len)
4473f085823Sopenharmony_ci{
4483f085823Sopenharmony_ci    if (g_pDistributetestEnv != nullptr) {
4493f085823Sopenharmony_ci        return g_pDistributetestEnv->SendMessage(devNo, msg, len,
4503f085823Sopenharmony_ci            [&](const std::string &szreturnbuf, int rlen)->bool {
4513f085823Sopenharmony_ci                HiLog::Info(DistributeTestEnvironment::LABEL, "onprocessmsg len :%d.", rlen);
4523f085823Sopenharmony_ci                return OnMsgProc(szreturnbuf, rlen);
4533f085823Sopenharmony_ci            });
4543f085823Sopenharmony_ci    }
4553f085823Sopenharmony_ci    return false;
4563f085823Sopenharmony_ci}
4573f085823Sopenharmony_ci
4583f085823Sopenharmony_ci/*
4593f085823Sopenharmony_ci * function : testcase send message to agent device, include callback.
4603f085823Sopenharmony_ci *     this interface is opened for user.
4613f085823Sopenharmony_ci * param :
4623f085823Sopenharmony_ci *     devNo: the serial number of agent device.
4633f085823Sopenharmony_ci *     msg : message of the testcase sent to the agent
4643f085823Sopenharmony_ci *     len: length of message
4653f085823Sopenharmony_ci *     onProcessReturnMsg: callback function that handles the agent device return message and real
4663f085823Sopenharmony_ci *                         length of return value
4673f085823Sopenharmony_ci * return : if false is return, send operation failed.
4683f085823Sopenharmony_ci */
4693f085823Sopenharmony_cibool DistributeTest::SendMessage(AGENT_NO devNo, const std::string &msg, int len,
4703f085823Sopenharmony_ci    std::function<bool(const std::string &, int)> onProcessReturnMsg)
4713f085823Sopenharmony_ci{
4723f085823Sopenharmony_ci    if (g_pDistributetestEnv != nullptr) {
4733f085823Sopenharmony_ci        return g_pDistributetestEnv->SendMessage(devNo, msg, len, onProcessReturnMsg);
4743f085823Sopenharmony_ci    }
4753f085823Sopenharmony_ci    return false;
4763f085823Sopenharmony_ci}
4773f085823Sopenharmony_ci
4783f085823Sopenharmony_cibool DistributeTest::OnMsgProc(const std::string &szbuf, int len)
4793f085823Sopenharmony_ci{
4803f085823Sopenharmony_ci    return (szbuf == "") ? false : true;
4813f085823Sopenharmony_ci}
4823f085823Sopenharmony_ci
4833f085823Sopenharmony_ci/*
4843f085823Sopenharmony_ci * function : testcase send message to agent device, no return value from agent device.
4853f085823Sopenharmony_ci *     this interface is opened for user.
4863f085823Sopenharmony_ci * param :
4873f085823Sopenharmony_ci *     devNo: the serial number of agent device.
4883f085823Sopenharmony_ci *     notifyType : message of the testcase notify the agent
4893f085823Sopenharmony_ci *     msg: message of the testcase notify the agent, message format: type:message
4903f085823Sopenharmony_ci *     msgLen: length of message
4913f085823Sopenharmony_ci * return : if false is return, notify operation failed.
4923f085823Sopenharmony_ci */
4933f085823Sopenharmony_cibool DistributeTest::Notify(AGENT_NO devNo, const std::string &notifyType, const std::string &msg, int msgLen)
4943f085823Sopenharmony_ci{
4953f085823Sopenharmony_ci    // maybe need justify if the length of notifytype+msg is bigger than MAX_BUFF_LEN/2;
4963f085823Sopenharmony_ci    // the length of notifytype must be less than 50;
4973f085823Sopenharmony_ci    if (notifyType.size() < CMD_LENGTH) {
4983f085823Sopenharmony_ci        if (g_pDistributetestEnv != nullptr) {
4993f085823Sopenharmony_ci            std::string strBuf = notifyType + ":" + msg;
5003f085823Sopenharmony_ci            return g_pDistributetestEnv->Notify(devNo, strBuf, strBuf.length() + 1);
5013f085823Sopenharmony_ci        }
5023f085823Sopenharmony_ci    }
5033f085823Sopenharmony_ci    return false;
5043f085823Sopenharmony_ci}
5053f085823Sopenharmony_ci} // namespace DistributeSystemTest
5063f085823Sopenharmony_ci} // namespace OHOS
507