/* * 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 "SdCardHal.h" #include "ILog.h" #include "LinuxApi.h" #include #include #include #include #include #include #include #include const char *SD_CARD_DEVICE = SD_CARD_DEV; SdCardHal::SdCardHal() { mThreadRuning = false; mStatus = SdCardHalStatus::END; } void SdCardHal::SetSdCardMonitor(std::shared_ptr &monitor) { LogInfo("SetSdCardMonitor.\n"); mMonitor = monitor; monitor->ReportEvent(mStatus); } SdCardHalStatus SdCardHal::GetSdCardStatus(void) { return mStatus; } void SdCardHal::Init(void) { auto detectThread = [](std::shared_ptr sdCardHal) { LogInfo("sdCardHal DevDetectingThread started.\n"); sdCardHal->DevDetectingThread(); }; mDevDetectingThread = std::thread(detectThread, shared_from_this()); } void SdCardHal::UnInit(void) { mThreadRuning = false; if (mDevDetectingThread.joinable()) { mDevDetectingThread.join(); } } void SdCardHal::DevDetectingThread(void) { constexpr int SLEEP_TIME_MS = 100; // SdCardHalStatus status = SdCardHalStatus::END; int fd = -1; // const char *SD_CARD_DEVICE = "/SD_CARD_DEVICE/mmcblk1p1"; mThreadRuning = true; while (mThreadRuning) { fd = fx_open(SD_CARD_DEVICE, O_RDONLY); if (fd < 0) { // LogInfo("sdCardHal: %s open failed.\n", SD_CARD_DEVICE); if (SdCardHalStatus::PULL_OUT != mStatus) { mStatus = SdCardHalStatus::PULL_OUT; ReportDetecedChangedResult(mStatus); } goto CONTINUE; } struct stat sdStat; if (fx_fstat(fd, &sdStat) < 0) { // LogInfo("sdCardHal: %s fstat failed.\n", SD_CARD_DEVICE); if (SdCardHalStatus::ERROR != mStatus) { mStatus = SdCardHalStatus::ERROR; ReportDetecedChangedResult(mStatus); } goto CONTINUE; } if (!S_ISBLK(sdStat.st_mode)) { // LogInfo("sdCardHal: %s is not block device.\n", SD_CARD_DEVICE); if (SdCardHalStatus::PULL_OUT != mStatus) { mStatus = SdCardHalStatus::PULL_OUT; ReportDetecedChangedResult(mStatus); } } else { // LogInfo("sdCardHal: %s is inserted.\n", SD_CARD_DEVICE); if (SdCardHalStatus::INSERTED != mStatus) { mStatus = SdCardHalStatus::INSERTED; ReportDetecedChangedResult(mStatus); } } CONTINUE: std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); } } void SdCardHal::ReportDetecedChangedResult(const SdCardHalStatus &status) { LogInfo("SdCardHalStatus changed: %s.\n", PrintfStatusString(status)); if (SdCardHalStatus::INSERTED == status) { LogInfo("mount sd SD_CARD_DEVICE %s.\n", SD_CARD_MOUNT_PATH); constexpr int BUF_LENGTH = 128; char cmd[BUF_LENGTH] = {0}; snprintf(cmd, BUF_LENGTH, "mount %s %s", SD_CARD_DEV, SD_CARD_MOUNT_PATH); fx_system(cmd); } auto monitor = mMonitor.lock(); if (mMonitor.expired()) { LogWarning("SdCardHal: monitor is expired.\n"); return; } monitor->ReportEvent(status); } const char *SdCardHal::PrintfStatusString(const SdCardHalStatus &status) { switch (status) { case SdCardHalStatus::MOUNTED: return "MOUNTE\n"; case SdCardHalStatus::UNMOUNTED: return "UNMOUNTED."; case SdCardHalStatus::INSERTED: return "INSERTED."; case SdCardHalStatus::PULL_OUT: return "PULL_OUT."; case SdCardHalStatus::END: return "END."; default: return "UNDEFINE."; } }