From 4c371de8f979cd6c83f39ffd9dac582ddc75977f Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Thu, 21 Jan 2016 01:00:37 +0100 Subject: [PATCH] more preprocessor magic --- src/Lookup.h | 87 ------------------------------------------ src/Switch.cpp | 58 ++++++++++++++-------------- src/lookup.h | 19 +++++++-- src/lookup/general.lst | 43 +++++++++++---------- src/lookup/rcv.lst | 24 ++++++------ src/lookup/snd.lst | 25 ++++++------ 6 files changed, 89 insertions(+), 167 deletions(-) delete mode 100644 src/Lookup.h diff --git a/src/Lookup.h b/src/Lookup.h deleted file mode 100644 index dc07b7f..0000000 --- a/src/Lookup.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Switch.cpp - * - * Created on: 29.09.2015 - * Author: jdi - */ - -#include "table.h" - -static table rcv_lookup = { // - { 1, table::STRING, "type" }, //+string - { 2, table::STRING, "hostname" }, //+string - { 3, table::HEX, "mac" }, //+byte[6] - { 4, table::DEC, "ip_addr" }, //+byte[4] - { 5, table::DEC, "ip_mask" }, //+byte[4] - { 6, table::DEC, "gateway" }, //+byte[4] - { 7, table::STRING, "firmware_version" },//+ - { 8, table::STRING, "hardware_version" },//+ - { 9, table::DEC, "dhcp" }, //+bool byte - { 10, table::DEC, "ports" }, //+byte, maybe number of ports - - { 4352, table::HEX, "igmp_snooping" }, //??? - { 4096, table::HEX, "port_settings" }, //+per port - { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? - - { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool,second byte port id - { 8448, table::HEX, "port_vlan_1" }, //??? - { 8450, table::HEX, "port_vlan_2" }, //??? - { 8704, table::HEX, "vlan_enabled" }, //+bool byte - { 8705, table::HEX, "vlan" }, //+one set per vlan - { 8706, table::HEX, "vlan_pvid" }, //+per port - { 8707, table::HEX, "vlan ?????" }, //???? - - { 12288, table::HEX, "qos_basic_enabled" }, //+bool = QoS Mod - { 12289, table::HEX, "qos_basic" }, //+per port ??? - { 12544, table::HEX, "bandwidth_control_1" }, //+per port ??? - { 12545, table::HEX, "bandwidth_control_2" }, //+per port ??? - { 12800, table::HEX, "storm_control" }, //+per port ??? - { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id??? - { 16384, table::HEX, "port_statistics" }, //+per port ??? - { 16896, table::HEX, "cable_test" }, //+per port ??? - { 17152, table::HEX, "loop_prevention" }, //+bool byte - - //{0000," "}, - }; - -static table snd_lookup = { - - // TODO find out if id is unique in response - { 2, table::HEX, "system_info" }, //page sysinfo - { 9, table::HEX, "ip_config" }, //page sysinfo - { 10, table::HEX, "ports" }, //after login - { 512, table::STRING, "login_user" }, //string - { 513, table::STRING, "new_user" }, //string - { 514, table::STRING, "login_password" }, //string - { 515, table::STRING, "new_passwoord" }, //string - { 773, table::HEX, "reboot" }, //bool byte = save config - - { 1280, table::ACTION, "reset" }, // - { 1536, table::ACTION, "flash" }, //update firmware - - { 2200, table::HEX, "vlan" }, //vlan - { 2304, table::ACTION, "save" }, //save - { 2305, table::ACTION, "ping" }, //sent before SET ??? - - { 4352, table::HEX, "igmp_snooping" }, //switching - { 4096, table::HEX, "port_settings" }, //switching - { 4608, table::HEX, "port_trunk" }, //byte[5] last byte bitmask?? - - { 8192, table::HEX, "mtu_vlan" }, //byte[2] first byte bool, second byte port id - { 8449, table::HEX, "port_vlan ???" }, //??? - { 8448, table::HEX, "port_vlan" }, //open page - { 8704, table::HEX, "vlan_enabled" }, //??? get vlan / set status - { 8705, table::HEX, "vlan" }, // per vlan - { 8706, table::HEX, "vlan_pvid" }, //per port - - { 12288, table::HEX, "qos_basic_enabled" }, //bool = QoS Mod - { 12289, table::HEX, "qos_basic" }, //per port - { 12544, table::HEX, "bandwidth_control_1" }, //per port - { 12545, table::HEX, "bandwidth_control_2" }, //per port - { 12800, table::HEX, "storm_control" }, //??? - { 16640, table::HEX, "port_mirror" }, //byte[10] second byte port id?? - { 16384, table::HEX, "port_statistics" }, //??? - { 17152, table::HEX, "loop_prevention" }, //bool byte - - //{0000," "}, - }; diff --git a/src/Switch.cpp b/src/Switch.cpp index 9b48777..047a03f 100644 --- a/src/Switch.cpp +++ b/src/Switch.cpp @@ -20,49 +20,47 @@ int Switch::parse(datasets arr) { } int Switch::parse(dataset d) { - auto lookup = (options.flags & FLAG_REVERSE) ? snd_lookup : rcv_lookup; - std::string id = lookup.exists(d.type)?lookup[d.type]:"unknown"; - switch(d.type){ - case TYPE: - device.type = d.value; + switch (d.type) { + case RCV_TYPE: + device.type = d.value; break; - } - - if (id == "mac") { + case RCV_MAC: device.mac = d.value; - } - else if (d.type == "firmware_version") { + break; + case FIRMWARE_VERSION: device.firmware_version = d.value; - } - else if (id == "hardware_version") { + break; + case HARDWARE_VERSION: device.hardware_version = d.value; - } - else if (id == "ports") { + break; + case PORTS: device.ports = d.value[0]; - } - else if (id == "hostname") { + break; + case HOSTNAME: settings.hostname = d.value; - } - else if (id == "ip_addr") { + break; + case IP_ADDR: settings.ip_addr = d.value; - } - else if (id == "ip_mask") { + break; + case IP_MASK: settings.ip_mask = d.value; - } - else if (id == "gateway") { + break; + case GATEWAY: settings.gateway = d.value; - } - else if (id == "dhcp_enabled") { + break; + case DHCP_ENABLED: settings.dhcp = d.value[0]; - } - else if (id == "loop_prevention") { + break; + case LOOP_PREVENTION: settings.loop_prevention = d.value[0]; - } - else if (id == "qos_basic_enabled") { + break; + case QOS_BASIC_ENABLED: settings.qos_enabled = d.value[0]; - } - else if (id == "vlan_enabled") { + break; + case VLAN_ENABLED: settings.vlan_enabled = d.value[0]; + break; } return 0; } + diff --git a/src/lookup.h b/src/lookup.h index 4b72515..a51f9bf 100644 --- a/src/lookup.h +++ b/src/lookup.h @@ -1,17 +1,30 @@ enum { -#define LOOKUP_SET(id, num, type) FOO_ ## id = num, +#define LOOKUP_SET(id, num, type) RCV_ ## id = num, +#include "lookup/rcv.lst" #include "lookup/general.lst" +#undef LOOKUP_SET +#define LOOKUP_SET(id, num, type) SND_ ## id = num, +#include "lookup/snd.lst" +#include "lookup/general.lst" +#undef LOOKUP_SET +#define LOOKUP_SET(id, num, type) id = num, +#include "lookup/rcv.lst" +#include "lookup/snd.lst" +#include "lookup/general.lst" +#undef LOOKUP_SET }; static table rcv_lookup { -#define LOOKUP_SET(id, num, type) { FOO_ ## id, table::type, #id }, +#define LOOKUP_SET(id, num, type) { RCV_ ## id, table::type, #id }, #include "lookup/rcv.lst" #include "lookup/general.lst" +#undef LOOKUP_SET }; static table snd_lookup { -#define LOOKUP_SET(id, num, type) { FOO_ ## id, table::type, #id }, +#define LOOKUP_SET(id, num, type) { SND_ ## id, table::type, #id }, #include "lookup/snd.lst" #include "lookup/general.lst" +#undef LOOKUP_SET }; diff --git a/src/lookup/general.lst b/src/lookup/general.lst index 537b307..0605ef0 100644 --- a/src/lookup/general.lst +++ b/src/lookup/general.lst @@ -1,24 +1,25 @@ -LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //??? -LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port -LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask?? +LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports -LOOKUP_SET(MTU_VLAN, 8192, HEX) //byte[2] first byte bool,second byte port id -LOOKUP_SET(PORT_VLAN_ENABLED, 8448, BOOL) //open page -LOOKUP_SET(PORT_VLAN, 8449, HEX) -LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC) -LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte -LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan -LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port -LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //???? +LOOKUP_SET(IGMP_SNOOPING, 4352, HEX) //??? +LOOKUP_SET(PORTS_SETTINGS, 4096, HEX) //+per port +LOOKUP_SET(PORT_TRUNK, 4608, HEX) //byte[5] last byte bitmask?? -LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod -LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ??? -LOOKUP_SET(BANDWIDTH_CONTROL_INGRESS, 12544, HEX) //+per port ??? -LOOKUP_SET(BANDWIDTH_CONTROL_EGRESS, 12545, HEX) //+per port ??? -LOOKUP_SET(STORM_CONTROL, 12800, HEX) //+per port ??? -LOOKUP_SET(PORT_MIRROR, 16640, HEX) //byte[10] second byte port id??? -LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ??? -LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ??? -LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte +LOOKUP_SET(MTU_VLAN, 8192, HEX) //byte[2] first byte bool,second byte port id +LOOKUP_SET(PORT_VLAN_ENABLED, 8448, BOOL) //open page +LOOKUP_SET(PORT_VLAN, 8449, HEX) +LOOKUP_SET(PORT_VLAN_MAX, 8450, DEC) +LOOKUP_SET(VLAN_ENABLED, 8704, BOOL) //+bool byte +LOOKUP_SET(VLAN, 8705, HEX) //+one set per vlan +LOOKUP_SET(VLAN_PVID, 8706, HEX) //+per port +LOOKUP_SET(VLAN_FOOOO, 8707, DEC) //???? + +LOOKUP_SET(QOS_BASIC_ENABLED, 12288, BOOL) //+bool = QoS Mod +LOOKUP_SET(QOS_BASIC, 12289, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_INGRESS, 12544, HEX) //+per port ??? +LOOKUP_SET(BW_CONTROL_EGRESS, 12545, HEX) //+per port ??? +LOOKUP_SET(STORM_CONTROL, 12800, HEX) //+per port ??? +LOOKUP_SET(PORT_MIRROR, 16640, HEX) //byte[10] second byte port id??? +LOOKUP_SET(PORT_STATISTICS, 16384, HEX) //+per port ??? +LOOKUP_SET(CABLE_TEST, 16896, HEX) //+per port ??? +LOOKUP_SET(LOOP_PREVENTION, 17152, BOOL) //+bool byte -#undef LOOKUP_SET \ No newline at end of file diff --git a/src/lookup/rcv.lst b/src/lookup/rcv.lst index 21d75fd..7380ef2 100644 --- a/src/lookup/rcv.lst +++ b/src/lookup/rcv.lst @@ -1,15 +1,13 @@ -LOOKUP_SET(TYPE, 1, STRING) //+string -LOOKUP_SET(HOSTNAME, 2, STRING) //+string -LOOKUP_SET(MAC, 3, HEX) //+byte[6] -LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4] -LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4] -LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4] -LOOKUP_SET(FIRMWARE_VERSION, 7, STRING) -LOOKUP_SET(HARDWARE_VERSION, 8, STRING) -LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte -LOOKUP_SET(PORTS, 10, DEC) //+byte, maybe number of ports - - -LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX) +LOOKUP_SET(TYPE, 1, STRING) //+string +LOOKUP_SET(HOSTNAME, 2, STRING) //+string +LOOKUP_SET(MAC, 3, HEX) //+byte[6] +LOOKUP_SET(IP_ADDR, 4, DEC) //+byte[4] +LOOKUP_SET(IP_MASK, 5, DEC) //+byte[4] +LOOKUP_SET(GATEWAY, 6, DEC) //+byte[4] +LOOKUP_SET(FIRMWARE_VERSION, 7, STRING) +LOOKUP_SET(HARDWARE_VERSION, 8, STRING) +LOOKUP_SET(DHCP_ENABLED, 9, BOOL) //+bool byte + +LOOKUP_SET(PORT_VLAN_FOOOOO, 2101, HEX) diff --git a/src/lookup/snd.lst b/src/lookup/snd.lst index 4169dac..d254a39 100644 --- a/src/lookup/snd.lst +++ b/src/lookup/snd.lst @@ -1,18 +1,17 @@ -LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo -LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo -LOOKUP_SET(PORTS, 10, HEX) //after login +LOOKUP_SET(SYSTEM_INFO, 2, HEX) //page sysinfo +LOOKUP_SET(IP_CONFIG, 9, HEX) //page sysinfo -LOOKUP_SET(LOGIN_USER, 512, STRING) //string -LOOKUP_SET(NEW_USER, 513, STRING) //string -LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string -LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string -LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config +LOOKUP_SET(LOGIN_USER, 512, STRING) //string +LOOKUP_SET(NEW_USER, 513, STRING) //string +LOOKUP_SET(LOGIN_PASSWORD, 514, STRING) //string +LOOKUP_SET(NEW_PASSWORD, 515, STRING) //string +LOOKUP_SET(REBOOT, 773, BOOL) //bool byte = save config -LOOKUP_SET(RESET, 1280, ACTION) // -LOOKUP_SET(FLASH, 1536, ACTION) //update firmware -LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan -LOOKUP_SET(SAVE, 2304, ACTION) //save -LOOKUP_SET(PING, 2305, ACTION) //sent before SET ??? +LOOKUP_SET(RESET, 1280, ACTION) // +LOOKUP_SET(FLASH, 1536, ACTION) //update firmware +LOOKUP_SET(VLAN_FOOOOOO, 2200, HEX) //vlan +LOOKUP_SET(SAVE, 2304, ACTION) //save +LOOKUP_SET(PING, 2305, ACTION) //sent before SET ???