Skip to content

Commit

Permalink
Changes to make architecture more consistent in how hardware componen…
Browse files Browse the repository at this point in the history
…ts (HAL) are represented
  • Loading branch information
phandinhlan committed Jan 12, 2020
1 parent 7040533 commit 9920a37
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 56 deletions.
13 changes: 6 additions & 7 deletions Sim/RomCode/RomCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
#include "Nand/Hal/NandHal.h"

std::function<bool(std::string)> _SetExecuteFunc;
std::unique_ptr<CustomProtocolInterface> _CustomProtocolInterface = nullptr;
CustomProtocolInterface* _CustomProtocolInterface = nullptr;

extern "C"
{
void __declspec(dllexport) __stdcall Initialize(NandHal* nandHal, BufferHal* bufferHal, CustomProtocolInterface* customProtocolInterface)
{
_CustomProtocolInterface = customProtocolInterface;
}

void __declspec(dllexport) __stdcall Execute()
{
if (_CustomProtocolInterface == nullptr)
Expand Down Expand Up @@ -44,12 +49,6 @@ extern "C"
}
}

void __declspec(dllexport) __stdcall SetCustomProtocolIpcName(const std::string& protocolIpcName)
{
_CustomProtocolInterface = std::make_unique<CustomProtocolInterface>();
_CustomProtocolInterface->Init(protocolIpcName.c_str());
}

void __declspec(dllexport) __stdcall SetExecuteCallback(std::function<bool(std::string)> setExecuteFunc)
{
_SetExecuteFunc = setExecuteFunc;
Expand Down
24 changes: 5 additions & 19 deletions Sim/SimFramework/FirmwareCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
HMODULE _DllInstance;
HMODULE _NewDllInstance;

typedef void(__stdcall *fInitialize)(std::shared_ptr<NandHal> nandHal, std::shared_ptr<BufferHal> bufferHal);
typedef void(__stdcall *fInitialize)(NandHal* nandHal, BufferHal* bufferHal, CustomProtocolInterface* customProtocolInterface);
typedef void(__stdcall *fSetIpcName)(const std::string& ipcName);
typedef void(__stdcall *fExecute)();
typedef void(__stdcall *fExecuteCallback)(std::function<bool(std::string)> callback);
typedef void(__stdcall *fShutdown)();

FirmwareCore::FirmwareCore() : _Execute(nullptr), _NewExecute(nullptr), _NandHal(nullptr), _BufferHal(nullptr)
FirmwareCore::FirmwareCore() : _Execute(nullptr), _NewExecute(nullptr), _NandHal(nullptr), _BufferHal(nullptr), _CustomProtocolInterface(nullptr)
{
_DllInstance = NULL;
_NewDllInstance = NULL;
Expand All @@ -27,29 +27,19 @@ bool FirmwareCore::SetExecute(std::string Filename)
return false;
}

// search the Initialize and execute
auto initialize = (fInitialize)GetProcAddress(_NewDllInstance, "Initialize");
if (initialize)
{
initialize(_NandHal, _BufferHal);
initialize(_NandHal, _BufferHal, _CustomProtocolInterface);
}

// search the SetIpcName functions and execute
auto setIpcName = (fSetIpcName)GetProcAddress(_NewDllInstance, "SetCustomProtocolIpcName");
if (setIpcName)
{
setIpcName(_CustomProtocolIpcName);
}

// resolve function address here
auto execute = (fExecute)GetProcAddress(_NewDllInstance, "Execute");
if (!execute)
{
FreeLibrary(_NewDllInstance);
return false;
}

// search the SetExecuteCall and set
auto setExecuteCallback = (fExecuteCallback)GetProcAddress(_NewDllInstance, "SetExecuteCallback");
if (setExecuteCallback)
{
Expand Down Expand Up @@ -114,13 +104,9 @@ void FirmwareCore::SwapExecute()
_NewDllInstance = NULL;
}

void FirmwareCore::SetHalComponents(std::shared_ptr<NandHal> nandHal, std::shared_ptr<BufferHal> bufferHal)
void FirmwareCore::SetHalComponents(NandHal* nandHal, BufferHal* bufferHal, CustomProtocolInterface* customProtocolInterface)
{
_NandHal = nandHal;
_BufferHal = bufferHal;
}

void FirmwareCore::SetIpcNames(const std::string& customProtocolIpcName)
{
_CustomProtocolIpcName = customProtocolIpcName;
_CustomProtocolInterface = customProtocolInterface;
}
10 changes: 5 additions & 5 deletions Sim/SimFramework/FirmwareCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "SimFrameworkBase/FrameworkThread.h"
#include "Nand/Hal/NandHal.h"
#include "Buffer/Hal/BufferHal.h"
#include "HostComm/CustomProtocol/CustomProtocolInterface.h"

class FirmwareCore : public FrameworkThread
{
Expand All @@ -16,18 +17,17 @@ class FirmwareCore : public FrameworkThread
FirmwareCore();
bool SetExecute(std::string Filename);
void Unload();
void SetHalComponents(std::shared_ptr<NandHal> nandHal, std::shared_ptr<BufferHal> bufferHal);
void SetIpcNames(const std::string& customProtocolIpcName);
void SetHalComponents(NandHal* nandHal, BufferHal* bufferHal, CustomProtocolInterface* customProtocolInterface);

private:
void SwapExecute();

private:
std::function<void()> _Execute;
std::function<void()> _NewExecute;
std::shared_ptr<NandHal> _NandHal;
std::shared_ptr<BufferHal> _BufferHal;
std::string _CustomProtocolIpcName;
NandHal* _NandHal;
BufferHal* _BufferHal;
CustomProtocolInterface* _CustomProtocolInterface;
};

#endif
9 changes: 7 additions & 2 deletions Sim/SimFramework/Framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void Framework::Init(const std::string& configFileName, std::string ipcNamesPref
}
}

_FirmwareCore->SetIpcNames(customProtocolIpcName);
_CustomProtocolInterface = std::make_shared<CustomProtocolInterface>();
_CustomProtocolInterface->Init(customProtocolIpcName.c_str(), _BufferHal.get());
}

void Framework::SetupNandHal(JSONParser& parser)
Expand Down Expand Up @@ -184,10 +185,11 @@ void Framework::GetFirmwareCoreInfo(JSONParser& parser)
void Framework::operator()()
{
std::future<void> nandHal;
std::future<void> customProtocolInterface;
std::future<void> firmwareMain;

// Load ROM
_FirmwareCore->SetHalComponents(_NandHal, _BufferHal);
_FirmwareCore->SetHalComponents(_NandHal.get(), _BufferHal.get(), _CustomProtocolInterface.get());
_FirmwareCore->SetExecute(this->_RomCodePath);

while (State::Exit != _State)
Expand All @@ -197,6 +199,7 @@ void Framework::operator()()
case State::Start:
{
nandHal = std::async(std::launch::async, &NandHal::operator(), _NandHal);
customProtocolInterface = std::async(std::launch::async, &CustomProtocolInterface::operator(), _CustomProtocolInterface);
firmwareMain = std::async(std::launch::async, &FirmwareCore::operator(), _FirmwareCore);

_State = State::Run;
Expand Down Expand Up @@ -237,9 +240,11 @@ void Framework::operator()()

_FirmwareCore->Stop();
_NandHal->Stop();
_CustomProtocolInterface->Stop();

firmwareMain.wait();
nandHal.wait();
customProtocolInterface.wait();

_FirmwareCore->Unload();
}
2 changes: 2 additions & 0 deletions Sim/SimFramework/Framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "FirmwareCore.h"
#include "HostComm/Ipc/MessageServer.hpp"
#include "HostComm/CustomProtocol/CustomProtocolCommand.h"
#include "HostComm/CustomProtocol/CustomProtocolInterface.h"

class JSONParser;

Expand Down Expand Up @@ -66,6 +67,7 @@ class Framework
std::shared_ptr<MessageServer<CustomProtocolCommand>> _ProtocolServer;
std::shared_ptr<BufferHal> _BufferHal;
std::shared_ptr<NandHal> _NandHal;
std::shared_ptr<CustomProtocolInterface> _CustomProtocolInterface;
std::shared_ptr<FirmwareCore> _FirmwareCore;
std::string _RomCodePath;
};
Expand Down
5 changes: 5 additions & 0 deletions Sim/SimFramework/SimFramework.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HostComm\HostComm.vcxproj">
<Project>{fdf75646-6e43-4a8a-bc13-d597b68754c2}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\..\packages\tencent.rapidjson.1.1.1\build\tencent.rapidjson.targets" Condition="Exists('..\..\packages\tencent.rapidjson.1.1.1\build\tencent.rapidjson.targets')" />
Expand Down
30 changes: 7 additions & 23 deletions Sim/SimpleFtl/SimpleFtlCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
#include "Nand/Hal/NandHal.h"
#include "SimpleFtl.h"

std::shared_ptr<CustomProtocolInterface> _CustomProtocolInterface = nullptr;
std::shared_ptr<BufferHal> _BufferHal = nullptr;
CustomProtocolInterface* _CustomProtocolInterface = nullptr;
SimpleFtl _SimpleFtl;
std::future<void> _CustomProtocolInterfaceFuture;

extern "C"
{
void __declspec(dllexport) __stdcall Initialize(std::shared_ptr<NandHal> nandHal, std::shared_ptr<BufferHal> bufferHal)
void __declspec(dllexport) __stdcall Initialize(NandHal* nandHal, BufferHal* bufferHal, CustomProtocolInterface* customProtocolInterface)
{
_SimpleFtl.SetNandHal(nandHal.get());
_SimpleFtl.SetBufferHal(bufferHal.get());
_BufferHal = bufferHal;
_SimpleFtl.SetNandHal(nandHal);
_SimpleFtl.SetBufferHal(bufferHal);
_SimpleFtl.SetProtocol(customProtocolInterface);
_CustomProtocolInterface = customProtocolInterface;
}

void __declspec(dllexport) __stdcall Execute()
Expand All @@ -34,23 +33,8 @@ extern "C"
_SimpleFtl();
}

void __declspec(dllexport) __stdcall SetCustomProtocolIpcName(const std::string& protocolIpcName)
{
_CustomProtocolInterface = std::make_shared<CustomProtocolInterface>();
_CustomProtocolInterface->Init(protocolIpcName.c_str(), _BufferHal.get());
_SimpleFtl.SetProtocol(_CustomProtocolInterface.get());

_CustomProtocolInterfaceFuture = std::async(std::launch::async, &CustomProtocolInterface::operator(), _CustomProtocolInterface);
}

void __declspec(dllexport) __stdcall Shutdown()
{
if (_CustomProtocolInterface == nullptr)
{
return;
}

_CustomProtocolInterface->Stop();
_CustomProtocolInterfaceFuture.wait();

}
};
1 change: 1 addition & 0 deletions SsdSim.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Nand", "Sim\Nand\Nand.vcxpr
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimFramework", "Sim\SimFramework\SimFramework.vcxproj", "{89F1CB64-54C0-4448-8D36-7BFC3D7A8CC4}"
ProjectSection(ProjectDependencies) = postProject
{FDF75646-6E43-4A8A-BC13-D597B68754C2} = {FDF75646-6E43-4A8A-BC13-D597B68754C2}
{E2C37474-B57F-49F8-B5BB-EB427C95A2E1} = {E2C37474-B57F-49F8-B5BB-EB427C95A2E1}
EndProjectSection
EndProject
Expand Down

0 comments on commit 9920a37

Please sign in to comment.