You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.0 KiB
102 lines
3.0 KiB
#include "channel_factory.hpp"
|
|
|
|
#include <toml++/toml.hpp>
|
|
|
|
using namespace iflytop;
|
|
using namespace std;
|
|
|
|
static IDataChannel::OnData_t s_ondata;
|
|
static map<string, IDataChannel *> s_channelMap;
|
|
static list<IDataChannel *> s_channelList;
|
|
|
|
// [server]
|
|
// cmdport=19004
|
|
// wsport=19005
|
|
|
|
// [[channels]]
|
|
// type="zexcan"
|
|
// name="zcan"
|
|
// ifname="can0"
|
|
// baudrate=500000
|
|
// enable=true
|
|
|
|
// [[channels]]
|
|
// type="uart"
|
|
// name="printer"
|
|
// ifname="/dev/ttyS1"
|
|
// baudrate=115200
|
|
// enable=true
|
|
|
|
// [[channels]]
|
|
// type="inputkey"
|
|
// name="emergency-key"
|
|
// pinnum="GPIO2-A3"
|
|
// enable=true
|
|
|
|
// [[channels]]
|
|
// type="uart"
|
|
// name="lis"
|
|
// ifname="/dev/ttyS2"
|
|
// baudrate=115200
|
|
// enable=true
|
|
|
|
map<string, IDataChannel *> ChannelFactory::createChannels(toml::table &config) {
|
|
auto logger = GET_LOGGER(ChannelFactory);
|
|
|
|
try {
|
|
if (auto channels = config["channels"].as_array()) {
|
|
for (size_t i = 0; i < channels->size(); i++) {
|
|
string type = config["channels"][i]["type"].value_or("");
|
|
|
|
/**
|
|
* @brief
|
|
* Build zexcan channel
|
|
*/
|
|
if (type == "zexcan") {
|
|
string name = config["channels"][i]["name"].value_or("zcan");
|
|
string ifname = config["channels"][i]["ifname"].value_or("can0");
|
|
int baudrate = config["channels"][i]["baudrate"].value_or(500000);
|
|
bool enable = config["channels"][i]["enable"].value_or(true);
|
|
|
|
if (enable) {
|
|
logger->info("==================create channel {}=================", name);
|
|
logger->info("create channel: {}, type={}, ifname={}, baudrate={}", name, type, ifname, baudrate);
|
|
|
|
ZExCanChannel *canChannel = new ZExCanChannel(type, ifname, baudrate);
|
|
canChannel->initialize();
|
|
canChannel->registerOnDataCallback([](IDataChannel *ch, const string &data) {
|
|
if (s_ondata) s_ondata(ch, data);
|
|
});
|
|
s_channelMap[canChannel->getChannelName()] = canChannel;
|
|
s_channelList.push_back(canChannel);
|
|
logger->info("");
|
|
} else {
|
|
logger->warn("skip channel: {}, enable=false", name);
|
|
}
|
|
}
|
|
//
|
|
else if (type == "uart") {
|
|
string name = config["channels"][i]["name"].value_or("uart");
|
|
string ifname = config["channels"][i]["ifname"].value_or("/dev/ttyS1");
|
|
int baudrate = config["channels"][i]["baudrate"].value_or(115200);
|
|
bool enable = config["channels"][i]["enable"].value_or(true);
|
|
|
|
if (enable) {
|
|
logger->info("create channel: {}, type={}, ifname={}, baudrate={}", name, type, ifname, baudrate);
|
|
} else {
|
|
logger->warn("skip channel: {}, enable=false", name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (const toml::parse_error &err) {
|
|
logger->error("parse config error, {}", err);
|
|
}
|
|
|
|
return s_channelMap;
|
|
}
|
|
|
|
void ChannelFactory::regOnChannelData(IDataChannel::OnData_t ondata) { s_ondata = ondata; }
|
|
|
|
list<IDataChannel *> ChannelFactory::getChannels() { return s_channelList; }
|