Add:SD card hal code.

This commit is contained in:
Fancy code 2024-04-26 19:24:03 +08:00
parent 2fe56a23ae
commit ef0a94eb55
10 changed files with 188 additions and 2 deletions

View File

@ -72,4 +72,8 @@ set(APP_MANAGER_DEVICE_IP "localhost")
# set(APP_MANAGER_DEVICE_IP "192.168.1.29")
set(APP_MANAGER_HTTP_SERVER_PORT "8080")
set(APP_MANAGER_TCP_SERVER_PORT "9876")
# ------------ build AppManager end ------------ #
# ------------ build AppManager end ------------ #
# ------------ build sd card ------------ #
# set(SD_CARD_DEV "dev/test")
# ------------ build sd card end ------------ #

View File

@ -16,6 +16,8 @@ include_directories(
# ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/build/lib
# )
add_definitions(-DSD_CARD_DEV=\"${SD_CARD_DEV}\")
aux_source_directory(./abstract ABSTRACT_SRC_FILES)
aux_source_directory(./src IMPL_SRC_FILES)

View File

@ -44,6 +44,9 @@ void VCameraHal::SetCameraMonitor(std::shared_ptr<VCameraHalMonitor> &monitor)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
}
void VSdCardHalMonitor::ReportEvent(const SdCardHalStatus &status)
{
}
std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
{
static auto instance = std::make_shared<IHalCpp>();
@ -82,4 +85,8 @@ StatusCode IHalCpp::GetCameraHal(std::map<CameraType, std::shared_ptr<VCameraHal
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IHalCpp::GetSdCardHal(std::shared_ptr<VSdCardHal> &sdCard)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -36,6 +36,15 @@ enum class CameraEvent
PICTIRUE_AND_VIDEO,
END
};
enum class SdCardHalStatus
{
MOUNTED = 0,
UNMOUNTED,
INSERTED,
PULL_OUT,
ERROR,
END
};
typedef struct camera_report_event
{
camera_report_event(const std::string &fileName, const std::string &filePath, const CameraEvent &eventType,
@ -90,6 +99,19 @@ public:
virtual ~VCameraHal() = default;
virtual void SetCameraMonitor(std::shared_ptr<VCameraHalMonitor> &monitor);
};
class VSdCardHalMonitor
{
public:
VSdCardHalMonitor() = default;
virtual ~VSdCardHalMonitor() = default;
virtual void ReportEvent(const SdCardHalStatus &status);
};
class VSdCardHal
{
public:
VSdCardHal() = default;
virtual ~VSdCardHal() = default;
};
class IHalCpp
{
public:
@ -102,5 +124,6 @@ public:
virtual StatusCode GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
virtual StatusCode GetWifiHal(std::shared_ptr<VWifiHal> &wifi);
virtual StatusCode GetCameraHal(std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
virtual StatusCode GetSdCardHal(std::shared_ptr<VSdCardHal> &sdCard);
};
#endif

View File

@ -15,18 +15,36 @@
#include "HalCpp.h"
#include "HalMakePtr.h"
#include "ILog.h"
#include "SdCardHal.h"
StatusCode HalCpp::Init(void)
{
LogInfo("HalCpp::Init\n");
HalMakePtr::GetInstance()->CreateWifiHal(mWifiHal);
HalMakePtr::GetInstance()->CreateSdCardHal(mSdCardHal);
std::shared_ptr<SdCardHal> sdCardImpl = std::dynamic_pointer_cast<SdCardHal>(mSdCardHal);
if (nullptr != sdCardImpl) {
sdCardImpl->Init();
}
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)
{
HalMakePtr::GetInstance()->CreateWifiHal(wifi);
wifi = mWifiHal;
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HalCpp::GetSdCardHal(std::shared_ptr<VSdCardHal> &sdCard)
{
sdCard = mSdCardHal;
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -15,6 +15,7 @@
#ifndef HALCPP_H
#define HALCPP_H
#include "IHalCpp.h"
#include <memory>
class HalCpp : public IHalCpp
{
public:
@ -23,8 +24,11 @@ public:
StatusCode Init(void) override;
StatusCode UnInit(void) override;
StatusCode GetWifiHal(std::shared_ptr<VWifiHal> &wifi) override;
StatusCode GetSdCardHal(std::shared_ptr<VSdCardHal> &sdCard) override;
private:
std::vector<std::shared_ptr<VLedHal>> mLedHals;
std::shared_ptr<VWifiHal> mWifiHal;
std::shared_ptr<VSdCardHal> mSdCardHal;
};
#endif

View File

@ -16,6 +16,7 @@
#include "Hal.h"
#include "HalCpp.h"
#include "ILog.h"
#include "SdCardHal.h"
#include "WifiHal.h"
StatusCode CreateHalModule(void)
{
@ -85,4 +86,10 @@ StatusCode HalMakePtr::CreateCameraHal(std::shared_ptr<VCameraHal> &impl)
{
LogWarning("CreateCameraHal.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode HalMakePtr::CreateSdCardHal(std::shared_ptr<VSdCardHal> &impl)
{
LogWarning("CreateSdCardHal.\n");
impl = std::make_shared<SdCardHal>();
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -36,5 +36,6 @@ public:
virtual StatusCode CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl);
virtual StatusCode CreateWifiHal(std::shared_ptr<VWifiHal> &impl);
virtual StatusCode CreateCameraHal(std::shared_ptr<VCameraHal> &impl);
virtual StatusCode CreateSdCardHal(std::shared_ptr<VSdCardHal> &impl);
};
#endif

88
hal/src/SdCardHal.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
* 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 <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
SdCardHal::SdCardHal()
{
mThreadRuning = false;
}
void SdCardHal::Init(void)
{
auto detectThread = [](std::shared_ptr<SdCardHal> 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 *dev = "/dev/mmcblk0";
mThreadRuning = true;
while (mThreadRuning) {
fd = open(dev, O_RDONLY);
if (fd < 0) {
LogInfo("sdCardHal: %s open failed.\n", dev);
if (SdCardHalStatus::PULL_OUT != status) {
status = SdCardHalStatus::PULL_OUT;
// TODO: pull out
}
goto CONTINUE;
}
struct stat sdStat;
if (fstat(fd, &sdStat) < 0) {
LogInfo("sdCardHal: %s fstat failed.\n", dev);
if (SdCardHalStatus::ERROR != status) {
status = SdCardHalStatus::ERROR;
// TODO: error
}
goto CONTINUE;
}
if (!S_ISBLK(sdStat.st_mode)) {
LogInfo("sdCardHal: %s is not block device.\n", dev);
if (SdCardHalStatus::PULL_OUT != status) {
status = SdCardHalStatus::PULL_OUT;
// TODO: pull out
}
}
else {
LogInfo("sdCardHal: %s is inserted.\n", dev);
if (SdCardHalStatus::INSERTED != status) {
status = SdCardHalStatus::INSERTED;
// TODO: inserted
}
}
CONTINUE:
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
}
}

32
hal/src/SdCardHal.h Normal file
View File

@ -0,0 +1,32 @@
/*
* 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.
*/
#ifndef SD_CARD_HAL_H
#define SD_CARD_HAL_H
#include "IHalCpp.h"
#include <thread>
class SdCardHal : public VSdCardHal, public std::enable_shared_from_this<SdCardHal>
{
public:
SdCardHal();
virtual ~SdCardHal() = default;
void Init(void);
void UnInit(void);
void DevDetectingThread(void);
private:
bool mThreadRuning;
std::thread mDevDetectingThread;
};
#endif