1 /*
2 * Copyright (c) 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 #include "wasm_func.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 RpcServer g_wasmTraceStreamer;
21 extern "C" {
22 using ReplyFunction = void (*)(const char* data, uint32_t len, int finish);
23 ReplyFunction g_reply;
24 uint8_t* g_reqBuf;
25 uint32_t g_reqBufferSize;
26
27 using SendDataCallBack = void (*)(const char* data, int len, int componentId);
28 SendDataCallBack g_sendData = nullptr;
29 uint8_t* g_sendDataBuf;
30 uint32_t g_sendDataBufSize;
31
ResultCallback(const std::string & jsonResult,int finish)32 void ResultCallback(const std::string& jsonResult, int finish)
33 {
34 g_reply(jsonResult.data(), jsonResult.size(), finish);
35 }
Initialize(ReplyFunction replyFunction,uint32_t reqBufferSize)36 EMSCRIPTEN_KEEPALIVE uint8_t* Initialize(ReplyFunction replyFunction, uint32_t reqBufferSize)
37 {
38 g_reply = replyFunction;
39 g_reqBuf = new uint8_t[reqBufferSize];
40 g_reqBufferSize = reqBufferSize;
41 return g_reqBuf;
42 }
43
UpdateTraceTime(int len)44 EMSCRIPTEN_KEEPALIVE int UpdateTraceTime(int len)
45 {
46 return g_wasmTraceStreamer.UpdateTraceTime(g_reqBuf, len);
47 }
48
ThirdPary_SendDataCallback(const char * pluginData,int len,int componentId)49 void ThirdPary_SendDataCallback(const char* pluginData, int len, int componentId)
50 {
51 if (g_sendData) {
52 g_sendData(pluginData, len, componentId);
53 }
54 }
55
TraceStreamer_Set_ThirdParty_DataDealer(SendDataCallBack sendDataCallBack,uint32_t reqBufferSize)56 EMSCRIPTEN_KEEPALIVE uint8_t* TraceStreamer_Set_ThirdParty_DataDealer(SendDataCallBack sendDataCallBack,
57 uint32_t reqBufferSize)
58 {
59 g_sendData = sendDataCallBack;
60 g_sendDataBuf = new uint8_t[reqBufferSize];
61 g_sendDataBufSize = reqBufferSize;
62 return g_sendDataBuf;
63 }
64
TraceStreamer_Plugin_Out_Filter(const char * pluginData,int len,const std::string componentName)65 int TraceStreamer_Plugin_Out_Filter(const char* pluginData, int len, const std::string componentName)
66 {
67 std::map<int, std::string>::iterator itor = g_wasmTraceStreamer.g_thirdPartyConfig.begin();
68 int componentId = 0;
69 for (; itor != g_wasmTraceStreamer.g_thirdPartyConfig.end(); ++itor) {
70 if (itor->second == componentName) {
71 componentId = itor->first;
72 return TraceStreamer_Plugin_Out_SendData(pluginData, len, componentId);
73 }
74 }
75 return -1;
76 }
77
78 // Tell js to call the corresponding third-party parser interface according to the compositeId
TraceStreamer_Plugin_Out_SendData(const char * pluginData,int len,int componentId)79 int TraceStreamer_Plugin_Out_SendData(const char* pluginData, int len, int componentId)
80 {
81 ThirdPary_SendDataCallback(pluginData, len, componentId);
82 return 0;
83 }
84
TraceStreamer_Init_ThirdParty_Config(int dataLen)85 EMSCRIPTEN_KEEPALIVE int TraceStreamer_Init_ThirdParty_Config(int dataLen)
86 {
87 return g_wasmTraceStreamer.TraceStreamer_Init_ThirdParty_Config(g_reqBuf, dataLen);
88 }
89
90 // return 0 while ok, -1 while failed
TraceStreamerParseData(const uint8_t * data,int dataLen)91 EMSCRIPTEN_KEEPALIVE int TraceStreamerParseData(const uint8_t* data, int dataLen)
92 {
93 if (g_wasmTraceStreamer.ParseData(data, dataLen, nullptr)) {
94 return 0;
95 }
96 return -1;
97 }
98 // return 0 while ok, -1 while failed
TraceStreamerParseDataEx(int dataLen)99 EMSCRIPTEN_KEEPALIVE int TraceStreamerParseDataEx(int dataLen)
100 {
101 if (g_wasmTraceStreamer.ParseData(g_reqBuf, dataLen, nullptr)) {
102 return 0;
103 }
104 return -1;
105 }
TraceStreamerParseDataOver()106 EMSCRIPTEN_KEEPALIVE int TraceStreamerParseDataOver()
107 {
108 if (g_wasmTraceStreamer.ParseDataOver(nullptr, 0, nullptr)) {
109 return 0;
110 }
111 return -1;
112 }
TraceStreamerSqlOperate(const uint8_t * sql,int sqlLen)113 EMSCRIPTEN_KEEPALIVE int TraceStreamerSqlOperate(const uint8_t* sql, int sqlLen)
114 {
115 if (g_wasmTraceStreamer.SqlOperate(sql, sqlLen, nullptr)) {
116 return 0;
117 }
118 return -1;
119 }
TraceStreamerSqlOperateEx(int sqlLen)120 EMSCRIPTEN_KEEPALIVE int TraceStreamerSqlOperateEx(int sqlLen)
121 {
122 if (g_wasmTraceStreamer.SqlOperate(g_reqBuf, sqlLen, nullptr)) {
123 return 0;
124 }
125 return -1;
126 }
TraceStreamerReset()127 EMSCRIPTEN_KEEPALIVE int TraceStreamerReset()
128 {
129 g_wasmTraceStreamer.Reset(nullptr, 0, nullptr);
130 return 0;
131 }
132 // return the length of result, -1 while failed
TraceStreamerSqlQuery(const uint8_t * sql,int sqlLen,uint8_t * out,int outLen)133 EMSCRIPTEN_KEEPALIVE int TraceStreamerSqlQuery(const uint8_t* sql, int sqlLen, uint8_t* out, int outLen)
134 {
135 return g_wasmTraceStreamer.WasmSqlQuery(sql, sqlLen, out, outLen);
136 }
137 // return the length of result, -1 while failed
TraceStreamerSqlQueryEx(int sqlLen)138 EMSCRIPTEN_KEEPALIVE int TraceStreamerSqlQueryEx(int sqlLen)
139 {
140 return g_wasmTraceStreamer.WasmSqlQueryWithCallback(g_reqBuf, sqlLen, &ResultCallback);
141 }
TraceStreamerCancel()142 EMSCRIPTEN_KEEPALIVE int TraceStreamerCancel()
143 {
144 g_wasmTraceStreamer.CancelSqlQuery();
145 return 0;
146 }
147 } // extern "C"
148 } // namespace TraceStreamer
149 } // namespace SysTuning
150