1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci#include "usb_ddk_hash.h" 16094332d3Sopenharmony_ci#include <functional> 17094332d3Sopenharmony_ci#include <mutex> 18094332d3Sopenharmony_ci#include <unordered_map> 19094332d3Sopenharmony_ci#include <iostream> 20094332d3Sopenharmony_ci 21094332d3Sopenharmony_ci#include "hdf_base.h" 22094332d3Sopenharmony_ci#include "usbd_wrapper.h" 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_cistatic std::unordered_map<uint64_t, InterfaceInfo> g_hashMap; 25094332d3Sopenharmony_cistd::mutex g_mapMutex; 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_ciconstexpr size_t MAX_HASH_RECORD = 1000; 28094332d3Sopenharmony_ci 29094332d3Sopenharmony_ciint32_t UsbDdkHash(const InterfaceInfo &info, uint64_t &hashVal) 30094332d3Sopenharmony_ci{ 31094332d3Sopenharmony_ci std::lock_guard<std::mutex> lock(g_mapMutex); 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_ci if (g_hashMap.size() > MAX_HASH_RECORD) { 34094332d3Sopenharmony_ci return HDF_ERR_OUT_OF_RANGE; 35094332d3Sopenharmony_ci } 36094332d3Sopenharmony_ci 37094332d3Sopenharmony_ci hashVal = static_cast<uint64_t>(std::hash<uint64_t> {}(info.addr)); 38094332d3Sopenharmony_ci g_hashMap.emplace(hashVal, info); 39094332d3Sopenharmony_ci return HDF_SUCCESS; 40094332d3Sopenharmony_ci} 41094332d3Sopenharmony_ci 42094332d3Sopenharmony_ciint32_t UsbDdkUnHash(uint64_t hashVal, uint64_t &addr) 43094332d3Sopenharmony_ci{ 44094332d3Sopenharmony_ci std::lock_guard<std::mutex> lock(g_mapMutex); 45094332d3Sopenharmony_ci if (auto ret = g_hashMap.find(hashVal); ret == g_hashMap.end()) { 46094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 47094332d3Sopenharmony_ci } 48094332d3Sopenharmony_ci auto mappedVal = g_hashMap[hashVal]; 49094332d3Sopenharmony_ci addr = mappedVal.addr; 50094332d3Sopenharmony_ci return HDF_SUCCESS; 51094332d3Sopenharmony_ci} 52094332d3Sopenharmony_ci 53094332d3Sopenharmony_civoid UsbDdkDelHashRecord(uint64_t hashVal) 54094332d3Sopenharmony_ci{ 55094332d3Sopenharmony_ci std::lock_guard<std::mutex> lock(g_mapMutex); 56094332d3Sopenharmony_ci g_hashMap.erase(hashVal); 57094332d3Sopenharmony_ci} 58094332d3Sopenharmony_ci 59094332d3Sopenharmony_cibool UsbDdkGetRecordByVal(const InterfaceInfo &info, uint64_t &hashVal) 60094332d3Sopenharmony_ci{ 61094332d3Sopenharmony_ci std::lock_guard<std::mutex> lock(g_mapMutex); 62094332d3Sopenharmony_ci for (auto it = g_hashMap.begin(); it != g_hashMap.end(); it++) { 63094332d3Sopenharmony_ci if (it->second.busNum == info.busNum && it->second.devNum == info.devNum) { 64094332d3Sopenharmony_ci hashVal = it->first; 65094332d3Sopenharmony_ci return true; 66094332d3Sopenharmony_ci } 67094332d3Sopenharmony_ci } 68094332d3Sopenharmony_ci return false; 69094332d3Sopenharmony_ci}