13f085823Sopenharmony_ci/*
23f085823Sopenharmony_ci * Copyright (c) 2021-2022 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_agent.h"
173f085823Sopenharmony_ci
183f085823Sopenharmony_ci#include <cstring>
193f085823Sopenharmony_ci#include <cerrno>
203f085823Sopenharmony_ci
213f085823Sopenharmony_ci#include <poll.h>
223f085823Sopenharmony_ci#include <unistd.h>
233f085823Sopenharmony_ci#include <sys/socket.h>
243f085823Sopenharmony_ci#include <netinet/in.h>
253f085823Sopenharmony_ci#include <arpa/inet.h>
263f085823Sopenharmony_ci
273f085823Sopenharmony_cinamespace OHOS {
283f085823Sopenharmony_cinamespace DistributeSystemTest {
293f085823Sopenharmony_ciusing namespace std;
303f085823Sopenharmony_ciusing namespace OHOS::HiviewDFX;
313f085823Sopenharmony_ci
323f085823Sopenharmony_ciDistributedAgent::DistributedAgent()
333f085823Sopenharmony_ci{
343f085823Sopenharmony_ci    agentPort_ = 0;
353f085823Sopenharmony_ci    agentIpAddr_ = "";
363f085823Sopenharmony_ci    mpthCmdProcess_ = nullptr;
373f085823Sopenharmony_ci    mbStop_ = false;
383f085823Sopenharmony_ci    clientSockFd_ = -1;
393f085823Sopenharmony_ci    agentCfg_ = DistributedCfg();
403f085823Sopenharmony_ci}
413f085823Sopenharmony_ci
423f085823Sopenharmony_ciDistributedAgent::~DistributedAgent()
433f085823Sopenharmony_ci{
443f085823Sopenharmony_ci    if (mpthCmdProcess_ != nullptr) {
453f085823Sopenharmony_ci        mbStop_ = true;
463f085823Sopenharmony_ci        if (mpthCmdProcess_->joinable()) {
473f085823Sopenharmony_ci            mpthCmdProcess_->join();
483f085823Sopenharmony_ci        }
493f085823Sopenharmony_ci    }
503f085823Sopenharmony_ci    close(clientSockFd_);
513f085823Sopenharmony_ci    clientSockFd_ = -1;
523f085823Sopenharmony_ci}
533f085823Sopenharmony_ci
543f085823Sopenharmony_ci/*
553f085823Sopenharmony_ci * The result of init test environment is returned. if false is return, the init operation failed.
563f085823Sopenharmony_ci * Note that, the test environment should be inited for the test case.
573f085823Sopenharmony_ci */
583f085823Sopenharmony_cibool DistributedAgent::SetUp()
593f085823Sopenharmony_ci{
603f085823Sopenharmony_ci    return true;
613f085823Sopenharmony_ci}
623f085823Sopenharmony_ci
633f085823Sopenharmony_ci/*
643f085823Sopenharmony_ci * The result of reset test environment is returned. if false is return, the reset operation failed.
653f085823Sopenharmony_ci * Note that, the test environment should be reset by the test case in the end.
663f085823Sopenharmony_ci */
673f085823Sopenharmony_cibool DistributedAgent::TearDown()
683f085823Sopenharmony_ci{
693f085823Sopenharmony_ci    return true;
703f085823Sopenharmony_ci}
713f085823Sopenharmony_ci
723f085823Sopenharmony_ciint DistributedAgent::InitAgentServer()
733f085823Sopenharmony_ci{
743f085823Sopenharmony_ci    HiLog::Info(DistributedAgent::LABEL, "begin create agent server.\n");
753f085823Sopenharmony_ci    volatile int serverSockFd = socket(AF_INET, SOCK_STREAM, 0);
763f085823Sopenharmony_ci    if (serverSockFd < 0) {
773f085823Sopenharmony_ci        return -1;
783f085823Sopenharmony_ci    }
793f085823Sopenharmony_ci
803f085823Sopenharmony_ci    int num = 1;
813f085823Sopenharmony_ci    if (setsockopt(serverSockFd, SOL_SOCKET, SO_REUSEADDR, &num, sizeof(num))) {
823f085823Sopenharmony_ci        close(serverSockFd);
833f085823Sopenharmony_ci        serverSockFd = -1;
843f085823Sopenharmony_ci        return serverSockFd;
853f085823Sopenharmony_ci    }
863f085823Sopenharmony_ci
873f085823Sopenharmony_ci    struct sockaddr_in addr;
883f085823Sopenharmony_ci    errno_t ret = EOK;
893f085823Sopenharmony_ci    ret = memset_s(&addr, sizeof(addr), 0, sizeof(addr));
903f085823Sopenharmony_ci    if (ret != EOK) {
913f085823Sopenharmony_ci        return -1;
923f085823Sopenharmony_ci    }
933f085823Sopenharmony_ci    addr.sin_family = AF_INET;
943f085823Sopenharmony_ci    if (agentIpAddr_ != "") {
953f085823Sopenharmony_ci        inet_pton(AF_INET, agentIpAddr_.c_str(), &addr.sin_addr);
963f085823Sopenharmony_ci    } else {
973f085823Sopenharmony_ci        addr.sin_addr.s_addr = htonl(INADDR_ANY);
983f085823Sopenharmony_ci    }
993f085823Sopenharmony_ci
1003f085823Sopenharmony_ci    addr.sin_port = htons(agentPort_);
1013f085823Sopenharmony_ci    int err = ::bind(serverSockFd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr));
1023f085823Sopenharmony_ci    if (err < 0) {
1033f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "agent bind error.\n");
1043f085823Sopenharmony_ci        close(serverSockFd);
1053f085823Sopenharmony_ci        serverSockFd = -1;
1063f085823Sopenharmony_ci        return serverSockFd;
1073f085823Sopenharmony_ci    }
1083f085823Sopenharmony_ci
1093f085823Sopenharmony_ci    if (listen(serverSockFd, 1) < 0) {
1103f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "agent listen error.\n");
1113f085823Sopenharmony_ci        close(serverSockFd);
1123f085823Sopenharmony_ci        serverSockFd = -1;
1133f085823Sopenharmony_ci        return serverSockFd;
1143f085823Sopenharmony_ci    }
1153f085823Sopenharmony_ci
1163f085823Sopenharmony_ci    mpthCmdProcess_ = std::make_unique<std::thread>([=]() {
1173f085823Sopenharmony_ci        DoCmdServer(serverSockFd);
1183f085823Sopenharmony_ci    });
1193f085823Sopenharmony_ci
1203f085823Sopenharmony_ci    OnLocalInit();
1213f085823Sopenharmony_ci    return serverSockFd;
1223f085823Sopenharmony_ci}
1233f085823Sopenharmony_ci
1243f085823Sopenharmony_ciint DistributedAgent::DoCmdServer(int serverSockFd)
1253f085823Sopenharmony_ci{
1263f085823Sopenharmony_ci    char buff[MAX_BUFF_LEN] = {0};
1273f085823Sopenharmony_ci    char returnValue[MAX_BUFF_LEN] = {0};
1283f085823Sopenharmony_ci    int rlen = 0;
1293f085823Sopenharmony_ci    int receiveLen = 0;
1303f085823Sopenharmony_ci    struct sockaddr_in clientAddr;
1313f085823Sopenharmony_ci    socklen_t sinSize = 0;
1323f085823Sopenharmony_ci    int clientSockFd = -1;
1333f085823Sopenharmony_ci    sinSize = sizeof(struct sockaddr_in);
1343f085823Sopenharmony_ci    bzero(&clientAddr, sizeof(clientAddr));
1353f085823Sopenharmony_ci    receiveLen = DistributedAgent::RECE_LEN;
1363f085823Sopenharmony_ci
1373f085823Sopenharmony_ci    while (receiveLen > 0) {
1383f085823Sopenharmony_ci        HiLog::Info(DistributedAgent::LABEL, "wait client .......\n");
1393f085823Sopenharmony_ci        if ((clientSockFd = accept(serverSockFd, reinterpret_cast<struct sockaddr *>(&clientAddr), &sinSize)) > 0) {
1403f085823Sopenharmony_ci            break;
1413f085823Sopenharmony_ci        }
1423f085823Sopenharmony_ci        receiveLen--;
1433f085823Sopenharmony_ci        continue;
1443f085823Sopenharmony_ci    }
1453f085823Sopenharmony_ci    if (receiveLen <= 0) {
1463f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "test case runner can not work because I can not accept it.");
1473f085823Sopenharmony_ci        close(serverSockFd);
1483f085823Sopenharmony_ci        return -1;
1493f085823Sopenharmony_ci    }
1503f085823Sopenharmony_ci    clientSockFd_ = clientSockFd;
1513f085823Sopenharmony_ci    HiLog::Info(DistributedAgent::LABEL, "accept testcase runner IP:%s port:%d \n",
1523f085823Sopenharmony_ci                inet_ntoa(clientAddr.sin_addr), clientAddr.sin_port);
1533f085823Sopenharmony_ci    while (mbStop_ != true) {
1543f085823Sopenharmony_ci        errno_t ret = EOK;
1553f085823Sopenharmony_ci        ret = memset_s(buff, sizeof(buff), 0, MAX_BUFF_LEN);
1563f085823Sopenharmony_ci        if (ret != EOK) {
1573f085823Sopenharmony_ci            return -1;
1583f085823Sopenharmony_ci        }
1593f085823Sopenharmony_ci        // every cmd length less than MAX_BUFF_LEN bytes;
1603f085823Sopenharmony_ci        int recvCmdLen = recv(clientSockFd_, buff, DST_COMMAND_HEAD_LEN, 0);
1613f085823Sopenharmony_ci        if (static_cast<unsigned long>(recvCmdLen) <  DST_COMMAND_HEAD_LEN) {
1623f085823Sopenharmony_ci            if (!recvCmdLen) {
1633f085823Sopenharmony_ci                HiLog::Info(DistributedAgent::LABEL, "agent connect socket closed, IP:%s .\n",
1643f085823Sopenharmony_ci                            inet_ntoa(clientAddr.sin_addr));
1653f085823Sopenharmony_ci                mbStop_ = true;
1663f085823Sopenharmony_ci            }
1673f085823Sopenharmony_ci            continue;
1683f085823Sopenharmony_ci        }
1693f085823Sopenharmony_ci        auto pcline = reinterpret_cast<DistributedMsg *>(buff);
1703f085823Sopenharmony_ci        pcline->cmdTestType = ntohs(pcline->cmdTestType);
1713f085823Sopenharmony_ci        pcline->len = ntohs(pcline->len);
1723f085823Sopenharmony_ci        HiLog::Info(DistributedAgent::LABEL, "test agent get message type:%d .\n", pcline->cmdTestType);
1733f085823Sopenharmony_ci        if (pcline->len > 0 && static_cast<unsigned long>(pcline->len) <= (MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN)) {
1743f085823Sopenharmony_ci            receiveLen = recv(clientSockFd_, pcline->alignmentCmd, static_cast<size_t>(pcline->len), 0);
1753f085823Sopenharmony_ci            HiLog::Info(DistributedAgent::LABEL, "agent get message cmd=%s .\n", pcline->alignmentCmd);
1763f085823Sopenharmony_ci            switch (pcline->cmdTestType) {
1773f085823Sopenharmony_ci                case DST_COMMAND_CALL: {
1783f085823Sopenharmony_ci                    errno_t ret = EOK;
1793f085823Sopenharmony_ci                    unsigned int cmdLen = ntohs(*reinterpret_cast<int *>(pcline->alignmentCmd));
1803f085823Sopenharmony_ci                    rlen = sizeof(int);
1813f085823Sopenharmony_ci                    // check cmdLen length < 100, copy command + args data ;
1823f085823Sopenharmony_ci                    char *pAlignmentCmd = new char[cmdLen + 1];
1833f085823Sopenharmony_ci                    ret = memcpy_s(pAlignmentCmd, cmdLen + 1, pcline->alignmentCmd + rlen, cmdLen);
1843f085823Sopenharmony_ci                    if (ret != EOK) {
1853f085823Sopenharmony_ci                        delete []pAlignmentCmd;
1863f085823Sopenharmony_ci                        return -1;
1873f085823Sopenharmony_ci                    }
1883f085823Sopenharmony_ci                    pAlignmentCmd[cmdLen] = '\0';
1893f085823Sopenharmony_ci                    rlen += cmdLen + 1;
1903f085823Sopenharmony_ci                    unsigned int eValueLen = ntohs(*reinterpret_cast<int *>(pcline->alignmentCmd + rlen));
1913f085823Sopenharmony_ci                    rlen += sizeof(int);
1923f085823Sopenharmony_ci                    char *pszEValue = new char[eValueLen + 1];
1933f085823Sopenharmony_ci                    ret = memcpy_s(pszEValue, eValueLen + 1, pcline->alignmentCmd + rlen, eValueLen);
1943f085823Sopenharmony_ci                    if (ret != EOK) {
1953f085823Sopenharmony_ci                        delete []pAlignmentCmd;
1963f085823Sopenharmony_ci                        delete []pszEValue;
1973f085823Sopenharmony_ci                        return -1;
1983f085823Sopenharmony_ci                    }
1993f085823Sopenharmony_ci                    pszEValue[eValueLen] = '\0';
2003f085823Sopenharmony_ci                    int nresult = OnProcessCmd(pAlignmentCmd, cmdLen, pszEValue, eValueLen);
2013f085823Sopenharmony_ci                    ret = memset_s(returnValue, sizeof(returnValue), 0, MAX_BUFF_LEN);
2023f085823Sopenharmony_ci                    if (ret != EOK) {
2033f085823Sopenharmony_ci                        delete []pAlignmentCmd;
2043f085823Sopenharmony_ci                        delete []pszEValue;
2053f085823Sopenharmony_ci                        return -1;
2063f085823Sopenharmony_ci                    }
2073f085823Sopenharmony_ci                    auto pclinereturn = reinterpret_cast<DistributedMsg *>(returnValue);
2083f085823Sopenharmony_ci                    pclinereturn->no = pcline->no;
2093f085823Sopenharmony_ci                    pclinereturn->cmdTestType = htons(DST_COMMAND_CALL);
2103f085823Sopenharmony_ci                    sprintf_s(pclinereturn->alignmentCmd, (MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN), "%d", nresult);
2113f085823Sopenharmony_ci                    rlen = strlen(pclinereturn->alignmentCmd) + 1;
2123f085823Sopenharmony_ci                    pclinereturn->len = htons(rlen);
2133f085823Sopenharmony_ci                    HiLog::Info(DistributedAgent::LABEL, "agent get message :%s .\n",
2143f085823Sopenharmony_ci                                pclinereturn->alignmentCmd);
2153f085823Sopenharmony_ci                    send(clientSockFd_, pclinereturn, static_cast<size_t>(rlen + DST_COMMAND_HEAD_LEN), 0);
2163f085823Sopenharmony_ci                    delete []pAlignmentCmd;
2173f085823Sopenharmony_ci                    delete []pszEValue;
2183f085823Sopenharmony_ci                    break;
2193f085823Sopenharmony_ci                }
2203f085823Sopenharmony_ci                case DST_COMMAND_MSG: {
2213f085823Sopenharmony_ci                    errno_t ret = EOK;
2223f085823Sopenharmony_ci                    int nresult = 0;
2233f085823Sopenharmony_ci                    ret = memset_s(returnValue, sizeof(returnValue), 0, MAX_BUFF_LEN);
2243f085823Sopenharmony_ci                    if (ret != EOK) {
2253f085823Sopenharmony_ci                        return -1;
2263f085823Sopenharmony_ci                    }
2273f085823Sopenharmony_ci                    auto pclinereturn = reinterpret_cast<DistributedMsg *>(returnValue);
2283f085823Sopenharmony_ci                    pclinereturn->no = pcline->no;
2293f085823Sopenharmony_ci                    pclinereturn->cmdTestType = htons(DST_COMMAND_MSG);
2303f085823Sopenharmony_ci                    pclinereturn->len = MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN;
2313f085823Sopenharmony_ci                    std::string resultcmd = pclinereturn->alignmentCmd;
2323f085823Sopenharmony_ci                    nresult = OnProcessMsg(pcline->alignmentCmd, pcline->len, resultcmd, pclinereturn->len);
2333f085823Sopenharmony_ci                    if (resultcmd == "") {
2343f085823Sopenharmony_ci                        resultcmd = "agent return message";
2353f085823Sopenharmony_ci                    }
2363f085823Sopenharmony_ci                    if (strcpy_s(pclinereturn->alignmentCmd, pclinereturn->len, resultcmd.c_str()) != EOK) {
2373f085823Sopenharmony_ci                        return -1;
2383f085823Sopenharmony_ci                    }
2393f085823Sopenharmony_ci                    pclinereturn->len = htons(nresult);
2403f085823Sopenharmony_ci                    send(clientSockFd_, pclinereturn, static_cast<size_t>(nresult + DST_COMMAND_HEAD_LEN), 0);
2413f085823Sopenharmony_ci                    break;
2423f085823Sopenharmony_ci                }
2433f085823Sopenharmony_ci                case DST_COMMAND_NOTIFY: {
2443f085823Sopenharmony_ci                    OnNotifyImf(pcline);
2453f085823Sopenharmony_ci                    break;
2463f085823Sopenharmony_ci                }
2473f085823Sopenharmony_ci                case DST_COMMAND_END:
2483f085823Sopenharmony_ci                    mbStop_ = true;
2493f085823Sopenharmony_ci                    break;
2503f085823Sopenharmony_ci                default:
2513f085823Sopenharmony_ci                    break;
2523f085823Sopenharmony_ci            }
2533f085823Sopenharmony_ci        }
2543f085823Sopenharmony_ci        if (EAGAIN == errno) {
2553f085823Sopenharmony_ci            continue;
2563f085823Sopenharmony_ci        }
2573f085823Sopenharmony_ci    }
2583f085823Sopenharmony_ci    return 0;
2593f085823Sopenharmony_ci}
2603f085823Sopenharmony_ci
2613f085823Sopenharmony_civoid DistributedAgent::OnNotifyImf(DistributedMsg *pcline)
2623f085823Sopenharmony_ci{
2633f085823Sopenharmony_ci    char alignmentCmd[DistributedAgent::CMD_LENGTH];
2643f085823Sopenharmony_ci    char szMsg[MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH];
2653f085823Sopenharmony_ci    int cmdNo = 0;
2663f085823Sopenharmony_ci    errno_t ret = EOK;
2673f085823Sopenharmony_ci    ret = memset_s(alignmentCmd, sizeof(alignmentCmd), 0, DistributedAgent::CMD_LENGTH);
2683f085823Sopenharmony_ci    if (ret != EOK) {
2693f085823Sopenharmony_ci        return;
2703f085823Sopenharmony_ci    }
2713f085823Sopenharmony_ci    (void)memset_s(szMsg,
2723f085823Sopenharmony_ci        MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH, 0,
2733f085823Sopenharmony_ci        MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH);
2743f085823Sopenharmony_ci    for (cmdNo = 0; cmdNo < DistributedAgent::CMD_LENGTH; cmdNo++) {
2753f085823Sopenharmony_ci        if (pcline->alignmentCmd[cmdNo] == ':') {
2763f085823Sopenharmony_ci            break;
2773f085823Sopenharmony_ci        }
2783f085823Sopenharmony_ci    }
2793f085823Sopenharmony_ci    if (cmdNo >= DistributedAgent::CMD_LENGTH) {
2803f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "error command.\n");
2813f085823Sopenharmony_ci    }  else {
2823f085823Sopenharmony_ci        errno_t ret = EOK;
2833f085823Sopenharmony_ci        ret = memcpy_s(alignmentCmd, sizeof(alignmentCmd), pcline->alignmentCmd, cmdNo);
2843f085823Sopenharmony_ci        if (ret != EOK) {
2853f085823Sopenharmony_ci            return;
2863f085823Sopenharmony_ci        }
2873f085823Sopenharmony_ci        ret = memcpy_s(szMsg, MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH,
2883f085823Sopenharmony_ci            pcline->alignmentCmd + cmdNo + 1, pcline->len - cmdNo - 1);
2893f085823Sopenharmony_ci        if (ret != EOK) {
2903f085823Sopenharmony_ci            return;
2913f085823Sopenharmony_ci        }
2923f085823Sopenharmony_ci        OnNotify(alignmentCmd, szMsg, pcline->len - cmdNo);
2933f085823Sopenharmony_ci    }
2943f085823Sopenharmony_ci}
2953f085823Sopenharmony_ci
2963f085823Sopenharmony_ci/*
2973f085823Sopenharmony_ci * function : when testcase need this device to do something, or tell device something, agent process it.
2983f085823Sopenharmony_ci *     this interface is opened for user.
2993f085823Sopenharmony_ci * param :
3003f085823Sopenharmony_ci *     strMsg: message from testcase .
3013f085823Sopenharmony_ci *     len : length of strMsg
3023f085823Sopenharmony_ci *     strReturnValue: return message buffer
3033f085823Sopenharmony_ci *     returnbuflen: max length of strReturnValue
3043f085823Sopenharmony_ci * return : real length of strReturnValue filled
3053f085823Sopenharmony_ci */
3063f085823Sopenharmony_ciint DistributedAgent::OnProcessMsg(const std::string &strMsg, int msgLen,
3073f085823Sopenharmony_ci    std::string &strReturnValue, int returnBufLen)
3083f085823Sopenharmony_ci{
3093f085823Sopenharmony_ci    // default test code
3103f085823Sopenharmony_ci    std::string returnStr = "agent return message";
3113f085823Sopenharmony_ci    int returnLen = returnStr.size();
3123f085823Sopenharmony_ci    if (strReturnValue != "" && returnLen <= returnBufLen) {
3133f085823Sopenharmony_ci        strReturnValue = returnStr;
3143f085823Sopenharmony_ci    }
3153f085823Sopenharmony_ci    return returnLen;
3163f085823Sopenharmony_ci}
3173f085823Sopenharmony_ci
3183f085823Sopenharmony_ci/*
3193f085823Sopenharmony_ci * function : execute command from testcase.
3203f085823Sopenharmony_ci * param :
3213f085823Sopenharmony_ci *     strCommand: command from testcase ,format is : command_string:args_string.
3223f085823Sopenharmony_ci *     cmdLen : length of strCommand
3233f085823Sopenharmony_ci *     strExpectValue: expectvalue string
3243f085823Sopenharmony_ci *     expectvaluelen: length of strExpectValue
3253f085823Sopenharmony_ci * return : return integer value, default 0 is returned.
3263f085823Sopenharmony_ci */
3273f085823Sopenharmony_ciint DistributedAgent::OnProcessCmd(const std::string &strCommand, int cmdLen,
3283f085823Sopenharmony_ci    const std::string &strExpectValue, int expectValueLen)
3293f085823Sopenharmony_ci{
3303f085823Sopenharmony_ci    int nresult = 0;
3313f085823Sopenharmony_ci    char alignmentCmd[DistributedAgent::CMD_LENGTH];
3323f085823Sopenharmony_ci    char szArgs[MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH];
3333f085823Sopenharmony_ci    int cmdNo = 0;
3343f085823Sopenharmony_ci    errno_t ret = EOK;
3353f085823Sopenharmony_ci    ret = memset_s(alignmentCmd, sizeof(alignmentCmd), 0, DistributedAgent::CMD_LENGTH);
3363f085823Sopenharmony_ci    if (ret != EOK) {
3373f085823Sopenharmony_ci        return -1;
3383f085823Sopenharmony_ci    }
3393f085823Sopenharmony_ci    (void)memset_s(szArgs,
3403f085823Sopenharmony_ci        MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH, 0,
3413f085823Sopenharmony_ci        MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH);
3423f085823Sopenharmony_ci    for (cmdNo = 0; cmdNo < DistributedAgent::CMD_LENGTH; cmdNo++) {
3433f085823Sopenharmony_ci        if (strCommand[cmdNo] == ':') {
3443f085823Sopenharmony_ci            break;
3453f085823Sopenharmony_ci        }
3463f085823Sopenharmony_ci    }
3473f085823Sopenharmony_ci    if (cmdNo >= DistributedAgent::CMD_LENGTH) {
3483f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "error command.\n");
3493f085823Sopenharmony_ci        nresult = -1;
3503f085823Sopenharmony_ci        return nresult;
3513f085823Sopenharmony_ci    }
3523f085823Sopenharmony_ci
3533f085823Sopenharmony_ci    ret = memcpy_s(alignmentCmd, sizeof(alignmentCmd), strCommand.c_str(), cmdNo);
3543f085823Sopenharmony_ci    if (ret != EOK) {
3553f085823Sopenharmony_ci        return -1;
3563f085823Sopenharmony_ci    }
3573f085823Sopenharmony_ci    ret = memcpy_s(szArgs,
3583f085823Sopenharmony_ci        MAX_BUFF_LEN / DistributedAgent::HALF_NUM - DistributedAgent::CMD_LENGTH,
3593f085823Sopenharmony_ci        strCommand.c_str() + cmdNo + 1,
3603f085823Sopenharmony_ci        cmdLen - cmdNo - 1);
3613f085823Sopenharmony_ci    if (ret != EOK) {
3623f085823Sopenharmony_ci        return -1;
3633f085823Sopenharmony_ci    }
3643f085823Sopenharmony_ci
3653f085823Sopenharmony_ci    nresult = OnProcessCmd(alignmentCmd, cmdNo, szArgs, cmdLen - cmdNo, strExpectValue, expectValueLen);
3663f085823Sopenharmony_ci    return nresult;
3673f085823Sopenharmony_ci}
3683f085823Sopenharmony_ci
3693f085823Sopenharmony_ciint DistributedAgent::OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs,
3703f085823Sopenharmony_ci    int argsLen, const std::string &strExpectValue, int expectValueLen)
3713f085823Sopenharmony_ci{
3723f085823Sopenharmony_ci    int nresult = 0;
3733f085823Sopenharmony_ci    HiLog::Info(DistributedAgent::LABEL, "this is a agent.\n");
3743f085823Sopenharmony_ci    if (static_cast<int>(strExpectValue.size()) > expectValueLen ||
3753f085823Sopenharmony_ci        static_cast<int>(strCommand.size()) > cmdLen ||
3763f085823Sopenharmony_ci        static_cast<int>(strArgs.size()) > argsLen) {
3773f085823Sopenharmony_ci        return -1;
3783f085823Sopenharmony_ci    }
3793f085823Sopenharmony_ci    return nresult;
3803f085823Sopenharmony_ci}
3813f085823Sopenharmony_ci
3823f085823Sopenharmony_ciint DistributedAgent::Start(std::string cfgFile)
3833f085823Sopenharmony_ci{
3843f085823Sopenharmony_ci    std::string agentPort;
3853f085823Sopenharmony_ci    agentCfg_.OpenCfg(cfgFile);
3863f085823Sopenharmony_ci    if (!agentCfg_.GetCfgVal("agentport", agentPort)) {
3873f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "agent can not get port.\n");
3883f085823Sopenharmony_ci        return 0;
3893f085823Sopenharmony_ci    }
3903f085823Sopenharmony_ci    if (sscanf_s(agentPort.c_str(), "%d", &agentPort_) < 1) {
3913f085823Sopenharmony_ci        agentPort_ = DEFAULT_AGENT_PORT;
3923f085823Sopenharmony_ci    }
3933f085823Sopenharmony_ci    return InitAgentServer();
3943f085823Sopenharmony_ci}
3953f085823Sopenharmony_ci
3963f085823Sopenharmony_civoid DistributedAgent::Join()
3973f085823Sopenharmony_ci{
3983f085823Sopenharmony_ci    if (mpthCmdProcess_ != nullptr) {
3993f085823Sopenharmony_ci        mpthCmdProcess_->join();
4003f085823Sopenharmony_ci    }
4013f085823Sopenharmony_ci}
4023f085823Sopenharmony_ci
4033f085823Sopenharmony_ciint DistributedAgent::OnNotify(const std::string &notifyType, const std::string &msg, int msgLen)
4043f085823Sopenharmony_ci{
4053f085823Sopenharmony_ci    if (strcmp(notifyType.c_str(), "testcasename")) {
4063f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "onNotify: %s.\n", msg.c_str());
4073f085823Sopenharmony_ci    }
4083f085823Sopenharmony_ci    if (msgLen < 0) {
4093f085823Sopenharmony_ci        HiLog::Error(DistributedAgent::LABEL, "msgLen < 0.");
4103f085823Sopenharmony_ci    }
4113f085823Sopenharmony_ci    return 0;
4123f085823Sopenharmony_ci}
4133f085823Sopenharmony_ci
4143f085823Sopenharmony_ciint DistributedAgent::Stop()
4153f085823Sopenharmony_ci{
4163f085823Sopenharmony_ci    return 0;
4173f085823Sopenharmony_ci}
4183f085823Sopenharmony_ci} // namespace DistributeSystemTest
4193f085823Sopenharmony_ci} // namespace OHOS
420