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 "frameworks/bridge/codec/standard_function_codec.h"
17
18 #include "base/log/log.h"
19 #include "frameworks/bridge/codec/standard_codec_buffer_operator.h"
20
21 namespace OHOS::Ace::Framework {
22
EncodeFunctionCall(const FunctionCall & functionCall,std::vector<uint8_t> & resultBuffer)23 bool StandardFunctionCodec::EncodeFunctionCall(const FunctionCall& functionCall, std::vector<uint8_t>& resultBuffer)
24 {
25 if (functionCall.GetArgs().size() > MAX_PARAMETERS_COUNT) {
26 LOGW("Too many args for a function call");
27 return false;
28 }
29
30 StandardCodecBufferWriter bufferWriter(resultBuffer);
31 bufferWriter.WriteData(CodecData(functionCall.GetFuncName()));
32 bufferWriter.WriteDataList(functionCall.GetArgs());
33 return true;
34 }
35
DecodeFunctionCall(const std::vector<uint8_t> & buffer,FunctionCall & functionCall)36 bool StandardFunctionCodec::DecodeFunctionCall(const std::vector<uint8_t>& buffer, FunctionCall& functionCall)
37 {
38 StandardCodecBufferReader bufferReader(buffer);
39 CodecData funcName;
40 if (!bufferReader.ReadData(funcName)) {
41 LOGW("Decode funcName failed");
42 return false;
43 }
44
45 std::vector<CodecData> args;
46 if (!bufferReader.ReadDataList(args)) {
47 LOGW("Decode args failed");
48 return false;
49 }
50
51 functionCall.SetFuncName(funcName.GetStringValue());
52 functionCall.SetArgs(std::move(args));
53 return true;
54 }
55
DecodePlatformMessage(const std::vector<uint8_t> & buffer,CodecData & platformMessage)56 bool StandardFunctionCodec::DecodePlatformMessage(const std::vector<uint8_t>& buffer, CodecData& platformMessage)
57 {
58 StandardCodecBufferReader bufferReader(buffer);
59 if (!bufferReader.ReadData(platformMessage)) {
60 LOGW("Decode platform message failed");
61 return false;
62 }
63 return true;
64 }
65
66 } // namespace OHOS::Ace::Framework