hunting/hal/src/HalCpp.cpp
2024-06-17 19:35:40 +08:00

90 lines
3.1 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "HalCpp.h"
#include "HalMakePtr.h"
#include "IHalCpp.h"
#include "ILog.h"
#include "SdCardHal.h"
#include "StatusCode.h"
#include "WifiHal.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
StatusCode HalCpp::Init(void)
{
LogInfo("HalCpp::Init\n");
HalMakePtr::GetInstance()->CreateWifiHal(mWifiHal);
std::shared_ptr<WifiHal> wifiImpl = std::dynamic_pointer_cast<WifiHal>(mWifiHal);
if (nullptr != wifiImpl) {
wifiImpl->Init();
}
HalMakePtr::GetInstance()->CreateSdCardHal(mSdCardHal);
std::shared_ptr<SdCardHal> sdCardImpl = std::dynamic_pointer_cast<SdCardHal>(mSdCardHal);
if (nullptr != sdCardImpl) {
sdCardImpl->Init();
}
HalMakePtr::GetInstance()->CreateAllKeysHal(mKeys);
HalMakePtr::GetInstance()->CreateAllLedsHal(mLeds);
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::UnInit(void)
{
LogInfo("HalCpp::UnInit\n");
std::shared_ptr<SdCardHal> sdCardImpl = std::dynamic_pointer_cast<SdCardHal>(mSdCardHal);
if (nullptr != sdCardImpl) {
sdCardImpl->UnInit();
}
mWifiHal.reset();
mSdCardHal.reset();
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::GetWifiHal(std::shared_ptr<VWifiHal> &wifi)
{
wifi = mWifiHal;
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::GetSdCardHal(std::shared_ptr<VSdCardHal> &sdCard)
{
sdCard = mSdCardHal;
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::GetAllLeds(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
LogInfo("GetAllLeds, size = %d\n", mLeds.size());
for (auto &led : mLeds) {
std::shared_ptr<VLedHal> ledControl = std::dynamic_pointer_cast<VLedHal>(led);
if (nullptr == ledControl) {
LogError("ledControl is nullptr\n");
continue;
}
allLeds.insert(std::make_pair(led->GetLedName(), ledControl));
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
LogInfo("GetAllKeys\n");
for (auto &key : mKeys) {
std::shared_ptr<VKeyHal> keyControl = std::dynamic_pointer_cast<VKeyHal>(key);
if (nullptr == keyControl) {
LogError("keyControl is nullptr\n");
continue;
}
allKeys.insert(std::make_pair(key->GetKeyName(), keyControl));
}
return CreateStatusCode(STATUS_CODE_OK);
}