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 #ifndef SA_SERVER_ADAPTER_H 17 #define SA_SERVER_ADAPTER_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <set> 22 23 #include "protocol/data_channel/include/i_request.h" 24 #include "protocol/data_channel/include/i_response.h" 25 #include "protocol/retcode_inner/aie_retcode_inner.h" 26 #include "protocol/struct_definition/aie_info_define.h" 27 #include "serializer.h" 28 29 namespace OHOS { 30 namespace AI { 31 const unsigned long long TRANS_ID_MASK = 0x00000000FFFFFFFF; 32 33 class SaServerAdapter { 34 public: 35 explicit SaServerAdapter(int adapterId); 36 ~SaServerAdapter(); 37 38 /** 39 * Save listener to call client async process. 40 * 41 * @param [in] sid Client async callback SVC handle identity 42 */ 43 void SaveEngineListener(SvcIdentity *svcIdentity); 44 45 /** 46 * Delete the listener. 47 */ 48 void ClearEngineListener(); 49 50 /** 51 * Get listener to call client async process. 52 * 53 * @return Client async callback SVC handle. 54 */ 55 SvcIdentity *GetEngineListener(); 56 57 /** 58 * Initialize async task manager to execute algorithm inference asynchronously. 59 * 60 * @param [in] clientInfo Client information. 61 * @param [in] algorithmInfo Algorithm information. 62 * @param [in] inputInfo Data information needed to asynchronous execution algorithm. 63 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 64 */ 65 int AsyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo, const DataInfo &inputInfo); 66 67 /** 68 * Get session ID, according to transaction ID. 69 * 70 * @param transactionId client ID + session ID 71 * @return session ID 72 */ 73 int GetSessionId(long long transactionId) const; 74 int GetAdapterId() const; 75 void IncRef(); 76 void DecRef(); 77 int GetRefCount() const; 78 79 /** 80 * Get transaction ID, according to session ID. 81 * 82 * @param sessionId session ID 83 * @return transaction ID 84 */ 85 long long GetTransactionId(int sessionId) const; 86 87 /** 88 * Load algorithm plugin and model based on algorithm information and client information. 89 * 90 * @param [in] transactionId Client ID + Session ID. 91 * @param [in] algorithmInfo Algorithm information. 92 * @param [in] inputInfo Data information needed to load algorithm plugin. 93 * @param [out] outputInfo The returned data information after loading the algorithm plugin. 94 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 95 */ 96 int LoadAlgorithm(long long transactionId, const AlgorithmInfo &algoInfo, const DataInfo &inputInfo, 97 DataInfo &outputInfo); 98 99 /** 100 * Set the configuration parameters of the engine or plugin. 101 * 102 * @param [in] clientInfo Client information. 103 * @param [in] optionType The type of setting option. 104 * @param [in] inputInfo Configuration parameter needed to set up the engine or plugin. 105 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 106 */ 107 int SetOption(long long transactionId, int optionType, const DataInfo &dataInfo); 108 109 /** 110 * Get the configuration parameters of the engine or plugin. 111 * 112 * @param [in] clientInfo Client information. 113 * @param [in] optionType The type of getting option. 114 * @param [in] inputInfo Parameter information for getting options. 115 * @param [out] outputInfo The configuration parameter information. 116 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 117 */ 118 int GetOption(long long transactionId, int optionType, const DataInfo &dataInfo, DataInfo &outputInfo); 119 120 /** 121 * Unload algorithm plugin and model based on transaction ID and client input information. 122 * 123 * @param [in] transactionId Client ID + Session ID. 124 * @param [in] inputInfo Data information needed to load algorithm plugin. 125 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 126 */ 127 int UnloadAlgorithm(long long transactionId, const DataInfo &inputInfo); 128 129 /** 130 * Execute algorithm inference synchronously. 131 * 132 * @param [in] clientInfo Client information. 133 * @param [in] AlgorithmInfo Algorithm information. 134 * @param [in] inputInfo Data information needed to synchronous execution algorithm. 135 * @param [out] outputInfo Algorithm inference results. 136 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 137 */ 138 int SyncExecute(const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo, const DataInfo &inputInfo, 139 DataInfo &outputInfo); 140 141 private: 142 void Uninitialize(); 143 void SaveTransaction(long long transactionId); 144 void RemoveTransaction(long long transactionId); 145 void ConvertToRequest(const ClientInfo &clientInfo, const AlgorithmInfo &algoInfo, const DataInfo &inputInfo, 146 IRequest *&request); 147 148 private: 149 int adapterId_; 150 std::atomic<int> refCount_; 151 std::mutex mutex_; 152 SvcIdentity svcIdentity_ = {}; 153 using TransactionIds = std::set<long long>; 154 TransactionIds transactionIds_; 155 }; 156 } // namespace AI 157 } // namespace OHOS 158 159 #endif // SA_SERVER_ADAPTER_H 160