1 /*
2 * Copyright (c) 2024 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 #ifndef OH_COMMONEVENT_PARAMETERS_PARSE_H
16 #define OH_COMMONEVENT_PARAMETERS_PARSE_H
17
18 #include <cstring>
19 #include <string>
20
21 #include "array_wrapper.h"
22 #include "bool_wrapper.h"
23 #include "double_wrapper.h"
24 #include "event_log_wrapper.h"
25 #include "int_wrapper.h"
26 #include "long_wrapper.h"
27 #include "oh_commonevent_wrapper.h"
28 #include "stdlib.h"
29 #include "string_wrapper.h"
30 #include "want_params_wrapper.h"
31 #include "zchar_wrapper.h"
32 namespace OHOS {
33 namespace EventFwk {
34
35 const int8_t I32_TYPE = 0;
36 const int8_t DOUBLE_TYPE = 1;
37 const int8_t STR_TYPE = 2;
38 const int8_t BOOL_TYPE = 3;
39 const int8_t I64_TYPE = 4;
40 const int8_t CHAR_TYPE = 5;
41 const int8_t I32_PTR_TYPE = 6;
42 const int8_t I64_PTR_TYPE = 7;
43 const int8_t BOOL_PTR_TYPE = 8;
44 const int8_t DOUBLE_PTR_TYPE = 9;
45 const int8_t STR_PTR_TYPE = 10;
46
47 template<class TBase, class T, class NativeT>
GetDataFromParams(const CArrParameters & parameters,const std::string & key,const NativeT defaultVal)48 NativeT GetDataFromParams(const CArrParameters& parameters, const std::string& key, const NativeT defaultVal)
49 {
50 TBase* ao = TBase::Query(parameters.wantParams.GetParam(key));
51 if (ao == nullptr) {
52 EVENT_LOGE("No value");
53 return defaultVal;
54 }
55 return T::Unbox(ao);
56 }
57
58 template<class T, class NativeT>
SetDataToParams(CArrParameters & parameters,const std::string & key,const NativeT value)59 CommonEvent_ErrCode SetDataToParams(CArrParameters& parameters, const std::string& key, const NativeT value)
60 {
61 parameters.wantParams.SetParam(key, T::Box(value));
62 return COMMONEVENT_ERR_OK;
63 }
64
65 template<class TBase, class T, class NativeT>
GetDataArrayFromParams(const CArrParameters & parameters,const std::string & key,NativeT ** array)66 int32_t GetDataArrayFromParams(const CArrParameters& parameters, const std::string& key, NativeT** array)
67 {
68 AAFwk::IArray* ao = AAFwk::IArray::Query(parameters.wantParams.GetParam(key));
69 if (ao == nullptr) {
70 EVENT_LOGE("No value");
71 return 0;
72 }
73
74 long size = 0;
75 if (ao->GetLength(size) != COMMONEVENT_ERR_OK) {
76 EVENT_LOGD("fail to get length");
77 return 0;
78 }
79 *array = static_cast<NativeT*>(malloc(sizeof(NativeT) * size));
80 if (*array == nullptr) {
81 EVENT_LOGE("malloc fail");
82 return 0;
83 }
84 parameters.allocatedPointers.push_back(*array);
85 for (long i = 0; i < size; i++) {
86 sptr<AAFwk::IInterface> interface = nullptr;
87 if (ao->Get(i, interface) == COMMONEVENT_ERR_OK) {
88 TBase* iValue = TBase::Query(interface);
89 if (iValue != nullptr) {
90 (*array)[i] = T::Unbox(iValue);
91 }
92 }
93 }
94 return size;
95 }
96
97 template<class T, class NativeT>
SetDataArrayToParams(CArrParameters & parameters,const std::string & key,const NativeT * array,const size_t count,const AAFwk::InterfaceID iid)98 CommonEvent_ErrCode SetDataArrayToParams(CArrParameters& parameters, const std::string& key, const NativeT* array,
99 const size_t count, const AAFwk::InterfaceID iid)
100 {
101 sptr<AAFwk::IArray> ao = new (std::nothrow) AAFwk::Array(count, iid);
102 if (ao == nullptr) {
103 return COMMONEVENT_ERR_ALLOC_MEMORY_FAILED;
104 } else {
105 for (size_t i = 0; i < count; ++i) {
106 ao->Set(i, T::Box(array[i]));
107 }
108 parameters.wantParams.SetParam(key, ao);
109 return COMMONEVENT_ERR_OK;
110 }
111 }
112
113 int32_t GetStringFromParams(const CArrParameters& parameters, const std::string& key, char** str);
114 } // namespace EventFwk
115 } // namespace OHOS
116 #endif