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