fsm/src/fm10k/IES.cpp
DataHoarder 6d07061f4d
Some checks failed
continuous-integration/drone/push Build is failing
WiP: Added IES, start configuration
2021-10-29 05:47:28 +02:00

188 lines
4.6 KiB
C++

#include "IES.h"
#include <unordered_map>
#include "IES_SDK.h"
#include "IES_CONFIGURATIONS.h"
namespace FM10K{
namespace IES_Static{
static bool IES_INITIALIZED = false;
static fm_semaphore semaphore;
static fm_int sw = 0;
using TlvType = FM10K::IES::TlvType;
}
}
std::unique_ptr<FM10K::IES> FM10K::IES::instance = nullptr;
bool FM10K::IES::init() {
if(IES_Static::IES_INITIALIZED){
return true;
}
/*if(fmOSInitialize() != FM_OK){
std::abort();
}
if(fmSetLoggingType(FM_LOG_TYPE_CONSOLE, 0, nullptr) != FM_OK){
std::abort();
}*/
fmCreateSemaphore("seq", FM_SEM_BINARY, &IES_Static::semaphore, 0);
if(fmInitialize(eventHandler) != FM_OK){
std::abort();
}
fm_timestamp wait = {3, 0};
fmCaptureSemaphore(&IES_Static::semaphore, &wait);
if(fmSetSwitchState(IES_Static::sw, true) != FM_OK){
std::abort();
}
IES_Static::IES_INITIALIZED = true;
return true;
}
void FM10K::IES::eventHandler(int event, int sw, void *ptr) {
if(!instance){
return;
}
switch (event) {
case FM_EVENT_SWITCH_INSERTED:
getInstance().handleSwitchInsertedEvent(sw);
break;
case FM_EVENT_PORT:
if(ptr){
getInstance().handlePortEvent(sw, *static_cast<fm_eventPort*>(ptr));
}
break;
case FM_EVENT_PKT_RECV:
printf("packet received\n");
break;
}
}
FM10K::IES &FM10K::IES::getInstance() {
if(!instance){
instance = std::unique_ptr<IES>(new IES());
}
return *instance;
}
void FM10K::IES::handleSwitchInsertedEvent(int sw) {
printf("Switch #%d inserted!\n", sw);
if (sw == IES_Static::sw){
fmReleaseSemaphore(&IES_Static::semaphore);
}
}
void FM10K::IES::handlePortEvent(int sw, _fm_eventPort& portEvent) {
printf("port event: port %d is %s\n", portEvent.port, (portEvent.linkStatus ? "up" : "down"));
}
bool FM10K::IES::replaceConfigEntry(const std::string &key, FM10K::IES::TlvType type, const std::string &value) {
if(m_initialized){
return false;
}
if(type == TlvType::Null){
m_configuration.erase(key);
}else{
m_configuration[key] = {type, value};
}
return true;
}
bool FM10K::IES::addConfigEntry(const std::string &key, FM10K::IES::TlvType type, const std::string &value) {
if(m_initialized){
return false;
}
if(m_configuration.find(key) != m_configuration.end()){
return false;
}
m_configuration[key] = {type, value};
return true;
}
bool FM10K::IES::addConfigEntry(const std::string &key, const std::string &value) {
return addConfigEntry(key, TlvType::Text, value);
}
bool FM10K::IES::addConfigEntry(const std::string &key, int32_t value) {
return addConfigEntry(key, TlvType::Integer, std::to_string(value));
}
bool FM10K::IES::addConfigEntry(const std::string &key, uint32_t value) {
return addConfigEntry(key, TlvType::UnsignedInteger, std::to_string(value));
}
bool FM10K::IES::addConfigEntry(const std::string &key, uint64_t value) {
return addConfigEntry(key, TlvType::UnsignedInteger64, std::to_string(value));
}
bool FM10K::IES::addConfigEntry(const std::string &key, bool value) {
return addConfigEntry(key, TlvType::Boolean, value ? "true" : "false");
}
std::string FM10K::IES::getPlatformConfiguration() const {
std::string output;
output.reserve(1024 * 8); //Reserve 8 KiB by default
output += "# Autogenerated file. Do not edit, changes WILL NOT be preserved.\n\n";
for(auto& it : m_configuration){
output += it.first;
output += " ";
switch (it.second.first) {
case TlvType::Text:
output += "text";
break;
case TlvType::Integer:
case TlvType::UnsignedInteger:
case TlvType::UnsignedInteger64:
output += "int";
break;
case TlvType::Boolean:
output += "bool";
break;
case TlvType::Null:
break;
}
output += " ";
output += it.second.second;
output += "\n";
}
return output;
}
bool FM10K::IES::loadKnownConfiguration(const std::string &key) {
if(IES_Static::knownConfigurations.find(key) == IES_Static::knownConfigurations.end()){
return false;
}
for(auto& it : IES_Static::knownConfigurations.at(key)){
if(!addConfigEntry(it.first, it.second.first, it.second.second)){
return false;
}
}
return true;
}
FM10K::IES::IES() = default;