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