86 lines
3.1 KiB
C++
86 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 "SdCardHandle.h"
|
|
#include "ILog.h"
|
|
#include "LinuxApi.h"
|
|
void SdCardHandle::ReportEvent(const SdCardHalStatus &status)
|
|
{
|
|
LogInfo("SdCardHal: ReportEvent.\n");
|
|
auto monitor = mStorageMonitor.lock();
|
|
if (mStorageMonitor.expired()) {
|
|
LogWarning("SdCardHal: monitor is expired.\n");
|
|
return;
|
|
}
|
|
monitor->ReportEvent(StorageEventConvert(status));
|
|
}
|
|
StatusCode SdCardHandle::GetSdCardInfo(SdCardInfo &info)
|
|
{
|
|
info.mEvent = StorageEventConvert(mSdCardHal->GetSdCardStatus());
|
|
return mSdCardHal->GetCapacity(info.mTotalSizeMB, info.mFreeSizeMB, info.mUsedSizeMB);
|
|
}
|
|
void SdCardHandle::SdCardInit(void)
|
|
{
|
|
IHalCpp::GetInstance()->GetSdCardHal(mSdCardHal);
|
|
if (nullptr == mSdCardHal) {
|
|
LogWarning("SdCardHal is nullptr.\n");
|
|
return;
|
|
}
|
|
std::shared_ptr<VSdCardHalMonitor> sdMonitor =
|
|
std::dynamic_pointer_cast<VSdCardHalMonitor>(StorageBase::shared_from_this());
|
|
if (nullptr == sdMonitor) {
|
|
LogWarning("sdMonitor is nullptr.\n");
|
|
return;
|
|
}
|
|
mSdCardHal->SetSdCardMonitor(sdMonitor);
|
|
}
|
|
void SdCardHandle::SdCardUnInit(void)
|
|
{
|
|
mSdCardHal.reset();
|
|
}
|
|
StatusCode SdCardHandle::SdSaveFile(const std::string &sourceFile, const std::string &savePaht)
|
|
{
|
|
LogInfo("SaveFile: %s -> %s", sourceFile.c_str(), savePaht.c_str());
|
|
constexpr int CMD_BUF_SIZE = 128;
|
|
char cmd[CMD_BUF_SIZE] = {0};
|
|
const std::string realSavePah = SD_CARD_MOUNT_PATH + savePaht;
|
|
bool directoryExist = StorageBase::CheckDirectory(realSavePah.c_str());
|
|
if (false == directoryExist) {
|
|
LogError("Directory not exist.\n");
|
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
|
}
|
|
snprintf(cmd, CMD_BUF_SIZE, "cp %s %s", sourceFile.c_str(), realSavePah.c_str());
|
|
fx_system(cmd);
|
|
fx_system("sync");
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
StorageEvent SdCardHandle::StorageEventConvert(const SdCardHalStatus &status)
|
|
{
|
|
switch (status) {
|
|
case SdCardHalStatus::MOUNTED:
|
|
return StorageEvent::SD_CARD_INSERT;
|
|
case SdCardHalStatus::UNMOUNTED:
|
|
return StorageEvent::SD_CARD_REMOVE;
|
|
case SdCardHalStatus::INSERTED:
|
|
return StorageEvent::SD_ABNORMAL;
|
|
case SdCardHalStatus::PULL_OUT_DEVICE_NOT_EXIST:
|
|
case SdCardHalStatus::PULL_OUT_OPEN_FAILED:
|
|
case SdCardHalStatus::PULL_OUT_NOT_BLOCK_DEVICE:
|
|
return StorageEvent::SD_CARD_REMOVE;
|
|
case SdCardHalStatus::ERROR:
|
|
return StorageEvent::SD_ABNORMAL;
|
|
default:
|
|
return StorageEvent::SD_ABNORMAL;
|
|
}
|
|
} |