1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_INTER_IPC_FORMAT_H
17 #define OHOS_INTER_IPC_FORMAT_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "iremote_object.h"
23 #include "message_parcel.h"
24 #include "common/sharing_log.h"
25 
26 namespace OHOS {
27 namespace Sharing {
28 
29 enum FormatDataType : uint32_t {
30     FORMAT_TYPE_NONE,
31     FORMAT_TYPE_BOOL,
32     FORMAT_TYPE_UINT16,
33     FORMAT_TYPE_INT32,
34     FORMAT_TYPE_UINT32,
35     FORMAT_TYPE_INT64,
36     FORMAT_TYPE_UINT64,
37     FORMAT_TYPE_FLOAT,
38     FORMAT_TYPE_DOUBLE,
39     FORMAT_TYPE_STRING,
40     FORMAT_TYPE_ADDR,
41     FORMAT_TYPE_OBJECT
42 };
43 
44 struct FormatData {
45     union Val {
46         bool boolVal;
47         uint16_t uint16Val;
48         int32_t int32Val;
49         uint32_t uint32Val;
50         int64_t int64Val;
51         uint64_t uint64Val;
52         float floatVal;
53         double doubleVal;
54     } val = {0};
55 
56     size_t size = 0;
57     uint8_t *addr = nullptr;
58     std::string stringVal = "";
59     sptr<IRemoteObject> objectVal = nullptr;
60     FormatDataType type = FORMAT_TYPE_NONE;
61 };
62 
63 class SceneFormat {
64 public:
65     using Ptr = std::shared_ptr<SceneFormat>;
66     using FormatDataMap = std::map<std::string, FormatData, std::less<>>;
67 
GetFormatMap() const68     const FormatDataMap &GetFormatMap() const
69     {
70         SHARING_LOGD("trace.");
71         return formatMap_;
72     }
73 
74 public:
75     bool PutBoolValue(const std::string_view &key, bool value);
76     bool PutIntValue(const std::string_view &key, int32_t value);
77     bool PutFloatValue(const std::string_view &key, float value);
78     bool PutDoubleValue(const std::string_view &key, double value);
79     bool PutInt64Value(const std::string_view &key, int64_t value);
80     bool PutUint16Value(const std::string_view &key, uint16_t value);
81     bool PutUint32Value(const std::string_view &key, uint32_t value);
82     bool PutUint64Value(const std::string_view &key, uint64_t value);
83     bool PutObjectValue(const std::string_view &key, sptr<IRemoteObject> value);
84     bool PutStringValue(const std::string_view &key, const std::string_view &value);
85 
86     bool GetBoolValue(const std::string_view &key, bool &value) const;
87     bool GetIntValue(const std::string_view &key, int32_t &value) const;
88     bool GetFloatValue(const std::string_view &key, float &value) const;
89     bool GetDoubleValue(const std::string_view &key, double &value) const;
90     bool GetInt64Value(const std::string_view &key, int64_t &value) const;
91     bool GetUint16Value(const std::string_view &key, uint16_t &value) const;
92     bool GetUint32Value(const std::string_view &key, uint32_t &value) const;
93     bool GetUint64Value(const std::string_view &key, uint64_t &value) const;
94     bool GetStringValue(const std::string_view &key, std::string &value) const;
95     bool GetObjectValue(const std::string_view &key, sptr<IRemoteObject> &value) const;
96 
97 private:
98     FormatDataMap formatMap_;
99 };
100 
101 class SceneFormatParcel {
102 public:
103     static bool Unmarshalling(MessageParcel &parcel, SceneFormat &format);
104     static bool Marshalling(MessageParcel &parcel, const SceneFormat &format);
105 };
106 
107 } // namespace Sharing
108 } // namespace OHOS
109 #endif