1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-14 06:23:07 +01:00

allow to set XDR mappings from user tasks

This commit is contained in:
andreas
2023-10-24 18:20:01 +02:00
parent 4b3c6f13b4
commit f0643b636a
8 changed files with 162 additions and 61 deletions

View File

@@ -1,4 +1,5 @@
#include "GwXDRMappings.h"
#include "GWConfig.h"
#include "N2kMessages.h"
double PtoBar(double v)
@@ -96,7 +97,7 @@ static GwXDRType::TypeCode findTypeMapping(GwXDRCategory category, int field)
return GwXDRType::UNKNOWN;
}
//category,direction,selector,field,instanceMode,instance,name
String GwXDRMappingDef::toString()
String GwXDRMappingDef::toString() const
{
String rt = "";
rt += String((int)category);
@@ -233,6 +234,82 @@ GwXDRMappings::GwXDRMappings(GwLog *logger, GwConfigHandler *config)
this->logger = logger;
this->config = config;
}
bool GwXDRMappings::addFixedMapping(const GwXDRMappingDef &mapping){
GwXDRMappingDef *nm=new GwXDRMappingDef(mapping);
bool res=addMapping(nm);
if (! res){
LOG_DEBUG(GwLog::ERROR,"unable to add fixed mapping %s",mapping.toString().c_str());
return false;
}
return true;
}
bool GwXDRMappings::addMapping(GwXDRMappingDef *def)
{
if (def)
{
int typeIndex = 0;
LOG_DEBUG(GwLog::LOG, "add xdr mapping %s",
def->toString().c_str());
// n2k: find first matching type mapping
GwXDRType::TypeCode code = findTypeMapping(def->category, def->field);
if (code == GwXDRType::UNKNOWN)
{
LOG_DEBUG(GwLog::ERROR, "no type mapping for %s", def->toString().c_str());
return false;
}
GwXDRType *type = findType(code, &typeIndex);
if (!type)
{
LOG_DEBUG(GwLog::ERROR, "no type definition for %s", def->toString().c_str());
return false;
}
long n2kkey = def->n2kKey();
auto it = n2kMap.find(n2kkey);
GwXDRMapping *mapping = new GwXDRMapping(def, type);
if (it == n2kMap.end())
{
LOG_DEBUG(GwLog::LOG, "insert mapping with key %ld", n2kkey);
GwXDRMapping::MappingList mappings;
mappings.push_back(mapping);
n2kMap[n2kkey] = mappings;
}
else
{
LOG_DEBUG(GwLog::LOG, "append mapping with key %ld", n2kkey);
it->second.push_back(mapping);
}
// for nmea0183 there could be multiple entries
// as potentially there are different units that we can handle
// so after we inserted the definition we do additional type lookups
while (type != NULL)
{
String n183key = GwXDRMappingDef::n183key(def->xdrName,
type->xdrtype, type->xdrunit);
auto it = n183Map.find(n183key);
if (it == n183Map.end())
{
LOG_DEBUG(GwLog::LOG, "insert mapping with n183key %s", n183key.c_str());
GwXDRMapping::MappingList mappings;
mappings.push_back(mapping);
n183Map[n183key] = mappings;
}
else
{
LOG_DEBUG(GwLog::LOG, "append mapping with n183key %s", n183key.c_str());
it->second.push_back(mapping);
}
type = findType(code, &typeIndex);
if (!type)
break;
mapping = new GwXDRMapping(def, type);
}
return true;
}
else
{
return false;
}
}
#define MAX_MAPPINGS 100
void GwXDRMappings::begin()
@@ -258,61 +335,10 @@ void GwXDRMappings::begin()
GwXDRMappingDef *def = GwXDRMappingDef::fromString(cfg->asCString());
if (def)
{
int typeIndex = 0;
LOG_DEBUG(GwLog::DEBUG, "read xdr mapping %s from %s",
def->toString().c_str(),namebuf);
//n2k: find first matching type mapping
GwXDRType::TypeCode code = findTypeMapping(def->category, def->field);
if (code == GwXDRType::UNKNOWN)
{
LOG_DEBUG(GwLog::DEBUG, "no type mapping for %s", def->toString().c_str());
continue;
}
GwXDRType *type = findType(code, &typeIndex);
if (!type)
{
LOG_DEBUG(GwLog::DEBUG, "no type definition for %s", def->toString().c_str());
continue;
}
long n2kkey = def->n2kKey();
auto it = n2kMap.find(n2kkey);
GwXDRMapping *mapping = new GwXDRMapping(def, type);
if (it == n2kMap.end())
{
LOG_DEBUG(GwLog::DEBUG, "insert mapping with key %ld", n2kkey);
GwXDRMapping::MappingList mappings;
mappings.push_back(mapping);
n2kMap[n2kkey] = mappings;
}
else
{
LOG_DEBUG(GwLog::DEBUG, "append mapping with key %ld", n2kkey);
it->second.push_back(mapping);
}
//for nmea0183 there could be multiple entries
//as potentially there are different units that we can handle
//so after we inserted the definition we do additional type lookups
while (type != NULL)
{
String n183key = GwXDRMappingDef::n183key(def->xdrName,
type->xdrtype, type->xdrunit);
auto it = n183Map.find(n183key);
if (it == n183Map.end())
{
LOG_DEBUG(GwLog::DEBUG, "insert mapping with n183key %s", n183key.c_str());
GwXDRMapping::MappingList mappings;
mappings.push_back(mapping);
n183Map[n183key] = mappings;
}
else
{
LOG_DEBUG(GwLog::DEBUG, "append mapping with n183key %s", n183key.c_str());
it->second.push_back(mapping);
}
type = findType(code, &typeIndex);
if (!type)
break;
mapping=new GwXDRMapping(def,type);
bool res=addMapping(def);
if (! res){
LOG_DEBUG(GwLog::ERROR,"unable to add mapping from %s",cfg);
delete cfg;
}
}
else{

View File

@@ -1,7 +1,6 @@
#ifndef _GWXDRMAPPINGS_H
#define _GWXDRMAPPINGS_H
#include "GwLog.h"
#include "GWConfig.h"
#include "GwBoatData.h"
#include <WString.h>
#include <vector>
@@ -115,7 +114,7 @@ class GwXDRMappingDef{
category=XDRTEMP;
}
//category,direction,selector,field,instanceMode,instance,name
String toString();
String toString() const;
static GwXDRMappingDef *fromString(String s);
//we allow 100 entities of code,selector and field nid
static unsigned long n2kKey(GwXDRCategory category, int selector, int field)
@@ -200,6 +199,7 @@ class GwXDRFoundMapping : public GwBoatItemNameProvider{
//the class GwXDRMappings is not intended to be deleted
//the deletion will leave memory leaks!
class GwConfigHandler;
class GwXDRMappings{
static const int MAX_UNKNOWN=200;
static const int ESIZE=13;
@@ -212,8 +212,10 @@ class GwXDRMappings{
char *unknowAsString=NULL;
GwXDRFoundMapping selectMapping(GwXDRMapping::MappingList *list,int instance,const char * key);
bool addUnknown(GwXDRCategory category,int selector,int field=0,int instance=-1);
bool addMapping(GwXDRMappingDef *mapping);
public:
GwXDRMappings(GwLog *logger,GwConfigHandler *config);
bool addFixedMapping(const GwXDRMappingDef &mapping);
void begin();
//get the mappings
//the returned mapping will exactly contain one mapping def