1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_JS_DEBUGGER_CONFIG_H
17 #define OHOS_ACELITE_JS_DEBUGGER_CONFIG_H
18 #include <cstdint>
19 #include <cstdio>
20 #include "js_config.h"
21 #include "memory_heap.h"
22 
23 /**
24  * For now DEBUGGER is only supported in Simulator
25  */
26 namespace OHOS {
27 namespace ACELite {
28 // the protocol type the engine supports
29 enum DebugProtocol : uint8_t {
30     TCP,
31     SERIAL
32 };
33 // the protocol channel type the engine supports
34 enum DebugChannel : uint8_t {
35     WEBSOCKET,
36     RAWPACKET
37 };
38 const uint16_t DEFAULT_PORT = 5001; // default port
39 struct DebuggerConfig : public MemoryHeap {
40     DebugProtocol protocol;   // use tcp as default
41     DebugChannel channel;     // use websocket as default
42     uint32_t heapSize;        // the JS heap size want to setup, count in bytes
43     uint16_t port;            // used for tcp, default is 5001
44     bool startDebuggerServer; // default is false
45     bool snapshotMode;        // if run in snapshot mode
DebuggerConfigDebuggerConfig46     DebuggerConfig()
47         : protocol(TCP),
48           channel(WEBSOCKET),
49           heapSize(0),
50           port(DEFAULT_PORT),
51           startDebuggerServer(false),
52           snapshotMode(false)
53     {
54     }
DebuggerConfigDebuggerConfig55     DebuggerConfig(DebugProtocol debugProtocol,
56                    DebugChannel debugChannel,
57                    uint32_t size,
58                    uint16_t debuggerPort,
59                    bool startServer,
60                    bool isSnapshotMode)
61         : protocol(debugProtocol),
62           channel(debugChannel),
63           heapSize(size),
64           port(debuggerPort),
65           startDebuggerServer(startServer),
66           snapshotMode(isSnapshotMode)
67     {
68     }
69 };
70 
71 /**
72  * @class Debugger
73  *
74  * @brief: Enable to start jerry ensign debugger for current JS application.
75  *
76  * This is only available for windows simulator
77  */
78 class Debugger final : public MemoryHeap {
79 public:
80     /**
81      * @brief Get the debugger instance
82      *
83      * @return an instance of debugger
84      */
85     static Debugger &GetInstance();
86     Debugger() = default;
87     ~Debugger() = default;
88 
89     /**
90      * @brief Get state of the debugger
91      *
92      * @return true to enable, false to disenable
93      */
94     bool IsDebuggerEnabled();
95 
96     /**
97      * @brief Make the debugger start
98      */
99     void StartDebugger();
100 
101     /**
102      * @brief Tear down the debugger
103      */
104     void TearDownDebugger();
105 
106     /**
107      * @brief Configure debugger based on the config file
108      *
109      * @param [out] config: The configuration of the debugger
110      */
111     void ConfigEngineDebugger(DebuggerConfig &config);
112 
113     /**
114      * @brief Print out the log into stdout
115      *
116      * @param [in] str: Information about log
117      */
118     void Output(const char *str);
119 
120     /**
121      * @brief Flush the output buffer of stream
122      */
123     void FlushOutput();
124 
125     /**
126      * @brief Setup jerry external context if needed
127      */
128     void SetupJSContext();
129 
130     /**
131      * @brief Release jerry external context
132      */
133     void ReleaseJSContext();
134 
135 private:
136 #if IS_ENABLED(ENGINE_DEBUGGER)
137 #if (JS_ENGINE_EXTERNAL_CONTEXT == 1)
138     void *engineContext_ = nullptr;
139 #endif // JS_ENGINE_EXTERNAL_CONTEXT
140     bool debuggerStarted_ = false;
141     DebuggerConfig debuggerConfig_ = {TCP, WEBSOCKET, 0, DEFAULT_PORT, false, false};
142 #endif // ENABLED(ENGINE_DEBUGGER)
143 };
144 } // namespace ACELite
145 } // namespace OHOS
146 #endif // OHOS_ACELITE_JS_DEBUGGER_CONFIG_H
147