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 <cstring>
17
18 #include "client_executor/include/i_aie_client.inl"
19 #include "utils/aie_client_const.h"
20 #include "utils/log/aie_log.h"
21 #include "utils/utils.h"
22
23 using namespace OHOS::AI;
24
25 namespace {
26 const int INT_1 = 1;
27 const int INT_2 = 2;
28 const int INT_1024 = 1024;
29 const int INT_65535 = 65535;
30 }
31
32 /**
33 * Constructs ConfigInfo parameters.
34 */
GetConfigInfo(ConfigInfo & configInfo)35 void GetConfigInfo(ConfigInfo &configInfo)
36 {
37 configInfo = {.description = CONFIG_DESCRIPTION};
38 }
39
40 /**
41 * Constructs ConfigInfo parameters,including specified description.
42 */
GetConfigInfo2(ConfigInfo & configInfo,const char * description)43 void GetConfigInfo2(ConfigInfo &configInfo, const char *description)
44 {
45 configInfo = {.description = description};
46 }
47
48 /**
49 * Constructs ClientInfo parameters.
50 */
GetClientInfo(ClientInfo & clientInfo)51 void GetClientInfo(ClientInfo &clientInfo)
52 {
53 const char *str = CLIENT_EXTEND_MSG;
54 char *extendMsg = const_cast<char*>(str);
55 int len = strlen(str) + 1;
56
57 clientInfo = {
58 .clientVersion = CLIENT_VERSION_VALID,
59 .clientId = INVALID_CLIENT_ID,
60 .sessionId = INVALID_SESSION_ID,
61 .serverUid = INVALID_UID,
62 .clientUid = INVALID_UID,
63 .extendLen = len,
64 .extendMsg = (unsigned char*)extendMsg,
65 };
66 }
67
68 /**
69 * Constructs AlgorithmInfo parameters.
70 */
GetSyncAlgorithmInfo(AlgorithmInfo & algorithmInfo,bool isAsync,int algorithmType)71 void GetSyncAlgorithmInfo(AlgorithmInfo &algorithmInfo, bool isAsync, int algorithmType)
72 {
73 const char *str = ALGORITHM_EXTEND_MSG;
74 char *extendMsg = const_cast<char*>(str);
75 int extendLen = strlen(str) + 1;
76
77 algorithmInfo = {
78 .clientVersion = CLIENT_VERSION_VALID,
79 .isAsync = isAsync,
80 .algorithmType = algorithmType,
81 .algorithmVersion = ALGORITHM_VERSION_VALID,
82 .isCloud = GetRandomBool(),
83 .operateId = GetRandomInt(INT_65535),
84 .requestId = GetRandomInt(INT_65535),
85 .extendLen = extendLen,
86 .extendMsg = (unsigned char*)extendMsg,
87 };
88 }
89
90 /**
91 * Constructs DataInfo.
92 */
GetDataInfo(bool isDataInfoNull=true,const char * dataString=DATA_INFO_DEFAULT)93 DataInfo GetDataInfo(bool isDataInfoNull = true, const char *dataString = DATA_INFO_DEFAULT)
94 {
95 // Sets default dataInfo to null.
96 DataInfo dataInfo = {
97 .data = nullptr,
98 .length = 0,
99 };
100
101 // Sets dataInfo to specified value.
102 if (!isDataInfoNull) {
103 const char *str = dataString;
104 char *data = const_cast<char*>(str);
105 int length = strlen(str) + 1;
106
107 dataInfo = {
108 .data = reinterpret_cast<unsigned char *>(data),
109 .length = length,
110 };
111 }
112
113 return dataInfo;
114 }
115
116 /**
117 * Constructs DataInfo with a big length data.
118 */
GetBigDataInfo(bool isDataInfoNull=true)119 DataInfo GetBigDataInfo(bool isDataInfoNull = true)
120 {
121 // Sets default dataInfo to null.
122 DataInfo dataInfo = {
123 .data = nullptr,
124 .length = 0,
125 };
126
127 // Sets dataInfo to specified value.
128 if (!isDataInfoNull) {
129 #ifdef __LINUX__
130 int length = INT_2 * INT_1024 * INT_1024; // 2 MB long data, the unit is Byte here.
131 #else // liteos device has no enough remaining memory to contain 2MB long data.
132 int length = INT_1 * INT_1024 * INT_1024; // 1 MB long data, the unit is Byte here.
133 #endif
134
135 char *data = reinterpret_cast<char *>(malloc(length));
136
137 dataInfo = {
138 .data = reinterpret_cast<unsigned char *>(data),
139 .length = length,
140 };
141 }
142
143 return dataInfo;
144 }
145
146 /**
147 * Release DataInfo.
148 */
FreeDataInfo(DataInfo & dataInfo)149 void FreeDataInfo(DataInfo &dataInfo)
150 {
151 if (dataInfo.data != nullptr) {
152 free(dataInfo.data);
153 dataInfo.data = nullptr;
154 dataInfo.length = 0;
155 }
156 }