1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#ifndef HDC_FORWARD_H
16cc290419Sopenharmony_ci#define HDC_FORWARD_H
17cc290419Sopenharmony_ci#include "common.h"
18cc290419Sopenharmony_ci
19cc290419Sopenharmony_cinamespace Hdc {
20cc290419Sopenharmony_ciclass HdcForwardBase : public HdcTaskBase {
21cc290419Sopenharmony_cipublic:
22cc290419Sopenharmony_ci    explicit HdcForwardBase(HTaskInfo hTaskInfo);
23cc290419Sopenharmony_ci    virtual ~HdcForwardBase();
24cc290419Sopenharmony_ci    bool CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize) override;
25cc290419Sopenharmony_ci    bool BeginForward(const string &command, string &sError);
26cc290419Sopenharmony_ci    void StopTask() override;
27cc290419Sopenharmony_ci    bool ReadyForRelease() override;
28cc290419Sopenharmony_ci    int fds[2];
29cc290419Sopenharmony_ci
30cc290419Sopenharmony_ciprotected:
31cc290419Sopenharmony_ci    enum FORWARD_TYPE {
32cc290419Sopenharmony_ci        FORWARD_TCP,
33cc290419Sopenharmony_ci        FORWARD_DEVICE,
34cc290419Sopenharmony_ci        FORWARD_JDWP,
35cc290419Sopenharmony_ci        FORWARD_ARK,
36cc290419Sopenharmony_ci        FORWARD_ABSTRACT,
37cc290419Sopenharmony_ci        FORWARD_RESERVED,
38cc290419Sopenharmony_ci        FORWARD_FILESYSTEM,
39cc290419Sopenharmony_ci    };
40cc290419Sopenharmony_ci    struct ContextForward {
41cc290419Sopenharmony_ci        FORWARD_TYPE type;
42cc290419Sopenharmony_ci        bool masterSlave;
43cc290419Sopenharmony_ci        bool checkPoint;
44cc290419Sopenharmony_ci        bool ready;
45cc290419Sopenharmony_ci        bool finish;
46cc290419Sopenharmony_ci        int fd;
47cc290419Sopenharmony_ci        uint32_t id;
48cc290419Sopenharmony_ci        uv_tcp_t tcp;
49cc290419Sopenharmony_ci        uv_pipe_t pipe;
50cc290419Sopenharmony_ci        HdcFileDescriptor *fdClass;
51cc290419Sopenharmony_ci        HdcForwardBase *thisClass;
52cc290419Sopenharmony_ci        string path;
53cc290419Sopenharmony_ci        string lastError;
54cc290419Sopenharmony_ci        string localArgs[2];
55cc290419Sopenharmony_ci        string remoteArgs[2];
56cc290419Sopenharmony_ci        string remoteParamenters;
57cc290419Sopenharmony_ci    };
58cc290419Sopenharmony_ci    using HCtxForward = struct ContextForward *;
59cc290419Sopenharmony_ci    struct ContextForwardIO {
60cc290419Sopenharmony_ci        HCtxForward ctxForward;
61cc290419Sopenharmony_ci        uint8_t *bufIO;
62cc290419Sopenharmony_ci    };
63cc290419Sopenharmony_ci
64cc290419Sopenharmony_ci    virtual bool SetupJdwpPoint(HCtxForward ctxPoint)
65cc290419Sopenharmony_ci    {
66cc290419Sopenharmony_ci        return false;
67cc290419Sopenharmony_ci    }
68cc290419Sopenharmony_ci    virtual bool SetupArkPoint(HCtxForward ctxPoint)
69cc290419Sopenharmony_ci    {
70cc290419Sopenharmony_ci        return false;
71cc290419Sopenharmony_ci    }
72cc290419Sopenharmony_ci    bool SetupPointContinue(HCtxForward ctx, int status);
73cc290419Sopenharmony_ci
74cc290419Sopenharmony_ciprivate:
75cc290419Sopenharmony_ci    static void ListenCallback(uv_stream_t *server, const int status);
76cc290419Sopenharmony_ci    static void ConnectTarget(uv_connect_t *connection, int status);
77cc290419Sopenharmony_ci    static void ReadForwardBuf(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
78cc290419Sopenharmony_ci    static void AllocForwardBuf(uv_handle_t *handle, size_t sizeSuggested, uv_buf_t *buf);
79cc290419Sopenharmony_ci    static void SendCallbackForwardBuf(uv_write_t *req, int status);
80cc290419Sopenharmony_ci    static void OnFdRead(uv_fs_t *req);
81cc290419Sopenharmony_ci
82cc290419Sopenharmony_ci    bool SetupPoint(HCtxForward ctxPoint);
83cc290419Sopenharmony_ci    void *MallocContext(bool masterSlave);
84cc290419Sopenharmony_ci    bool SlaveConnect(uint8_t *bufCmd, const int bufSize, bool bCheckPoint, string &sError);
85cc290419Sopenharmony_ci    bool SendToTask(const uint32_t cid, const uint16_t command, uint8_t *bufPtr, const int bufSize);
86cc290419Sopenharmony_ci    bool FilterCommand(uint8_t *bufCmdIn, uint32_t *idOut, uint8_t **pContentBuf);
87cc290419Sopenharmony_ci    void *AdminContext(const uint8_t op, const uint32_t id, HCtxForward hInput);
88cc290419Sopenharmony_ci    bool DoForwardBegin(HCtxForward ctx);
89cc290419Sopenharmony_ci    int SendForwardBuf(HCtxForward ctx, uint8_t *bufPtr, const int size);
90cc290419Sopenharmony_ci    bool CheckNodeInfo(const char *nodeInfo, string as[2]);
91cc290419Sopenharmony_ci    void FreeContext(HCtxForward ctxIn, const uint32_t id, bool bNotifyRemote);
92cc290419Sopenharmony_ci    int LoopFdRead(HCtxForward ctx);
93cc290419Sopenharmony_ci    void FreeContextCallBack(HCtxForward ctx);
94cc290419Sopenharmony_ci    void FreeJDWP(HCtxForward ctx);
95cc290419Sopenharmony_ci    void OnAccept(uv_stream_t *server, HCtxForward ctxClient, uv_stream_t *client);
96cc290419Sopenharmony_ci    bool DetechForwardType(HCtxForward ctxPoint);
97cc290419Sopenharmony_ci    bool SetupTCPPoint(HCtxForward ctxPoint);
98cc290419Sopenharmony_ci    bool SetupDevicePoint(HCtxForward ctxPoint);
99cc290419Sopenharmony_ci    bool SetupFilePoint(HCtxForward ctxPoint);
100cc290419Sopenharmony_ci    bool ForwardCommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize);
101cc290419Sopenharmony_ci    bool CommandForwardCheckResult(HCtxForward ctx, uint8_t *payload);
102cc290419Sopenharmony_ci    bool LocalAbstractConnect(uv_pipe_t *pipe, string &sNodeCfg);
103cc290419Sopenharmony_ci
104cc290419Sopenharmony_ci    map<uint32_t, HCtxForward> mapCtxPoint;
105cc290419Sopenharmony_ci    string taskCommand;
106cc290419Sopenharmony_ci    const uint8_t forwardParameterBufSize = 8;
107cc290419Sopenharmony_ci    const string filesystemSocketPrefix = "/tmp/";
108cc290419Sopenharmony_ci    const string harmonyReservedSocketPrefix = "/dev/socket/";
109cc290419Sopenharmony_ci    // set true to enable slave check when forward create
110cc290419Sopenharmony_ci    const bool slaveCheckWhenBegin = false;
111cc290419Sopenharmony_ci    std::mutex ctxPointMutex;
112cc290419Sopenharmony_ci    std::mutex ctxFreeMutex;
113cc290419Sopenharmony_ci};
114cc290419Sopenharmony_ci}  // namespace Hdc
115cc290419Sopenharmony_ci#endif
116