From 92d6a185bbd3a2966161215a3d3a807ee9efa1b4 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 24 Jul 2024 18:11:11 -0400 Subject: [PATCH] Updates --- src/tmx/TmxUtils/src/SNMPClient.cpp | 69 +++++++++++++---------- src/tmx/TmxUtils/src/SNMPClient.h | 12 ++++ src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp | 26 ++++----- 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/tmx/TmxUtils/src/SNMPClient.cpp b/src/tmx/TmxUtils/src/SNMPClient.cpp index 5c8a5ff52..17fd45ff7 100644 --- a/src/tmx/TmxUtils/src/SNMPClient.cpp +++ b/src/tmx/TmxUtils/src/SNMPClient.cpp @@ -168,39 +168,10 @@ namespace tmx::utils if (status == STAT_SUCCESS && response && response->errstat == SNMP_ERR_NOERROR ) { if ( request_type == request_type::GET ) { - for (auto vars = response->variables; vars; vars = vars->next_variable) - { - // Get value of variable depending on ASN.1 type - // Variable could be a integer, string, bitstring, ojbid, counter : defined here https://github.com/net-snmp/net-snmp/blob/master/include/net-snmp/types.h - // get Integer value - if (vars->type == ASN_INTEGER && vars->val.integer) - { - val.type = snmp_response_obj::response_type::INTEGER; - val.val_int = *vars->val.integer; - } - else if (vars->type == ASN_OCTET_STR && vars->val.string) - { - size_t str_len = vars->val_len; - for (size_t i = 0; i < str_len; ++i) - { - val.val_string.push_back(vars->val.string[i]); - } - val.type = snmp_response_obj::response_type::STRING; - } - } + process_snmp_get_response(val, *response); } else if( request_type == request_type::SET){ - - if(val.type == snmp_response_obj::response_type::INTEGER){ - FILE_LOG(logDEBUG) << "Success in SET for OID: " << input_oid << " Value: " << val.val_int << std::endl; - } - - else if(val.type == snmp_response_obj::response_type::STRING){ - FILE_LOG(logDEBUG) << "Success in SET for OID: " << input_oid << " Value:" << std::endl; - for(auto data : val.val_string){ - FILE_LOG(logDEBUG) << data ; - } - } + process_snmp_set_response(val, input_oid); } else { log_error(status, request_type, response); @@ -227,6 +198,42 @@ namespace tmx::utils return port_; } + void snmp_client::process_snmp_get_response( snmp_response_obj &val, const snmp_pdu &response) const { + for (auto vars = response.variables; vars; vars = vars->next_variable) + { + // Get value of variable depending on ASN.1 type + // Variable could be a integer, string, bitstring, ojbid, counter : defined here https://github.com/net-snmp/net-snmp/blob/master/include/net-snmp/types.h + // get Integer value + if (vars->type == ASN_INTEGER && vars->val.integer) + { + val.type = snmp_response_obj::response_type::INTEGER; + val.val_int = *vars->val.integer; + } + else if (vars->type == ASN_OCTET_STR && vars->val.string) + { + size_t str_len = vars->val_len; + for (size_t i = 0; i < str_len; ++i) + { + val.val_string.push_back(vars->val.string[i]); + } + val.type = snmp_response_obj::response_type::STRING; + } + } + } + + void snmp_client::process_snmp_set_response( snmp_response_obj &val, const std::string &input_oid) const { + if(val.type == snmp_response_obj::response_type::INTEGER){ + FILE_LOG(logDEBUG) << "Success in SET for OID: " << input_oid << " Value: " << val.val_int << std::endl; + } + + else if(val.type == snmp_response_obj::response_type::STRING){ + FILE_LOG(logDEBUG) << "Success in SET for OID: " << input_oid << " Value:" << std::endl; + for(auto data : val.val_string){ + FILE_LOG(logDEBUG) << data ; + } + } + } + void snmp_client::log_error(const int &status, const request_type &request_type, const snmp_pdu *response) const { diff --git a/src/tmx/TmxUtils/src/SNMPClient.h b/src/tmx/TmxUtils/src/SNMPClient.h index 75eaa9360..36debe609 100644 --- a/src/tmx/TmxUtils/src/SNMPClient.h +++ b/src/tmx/TmxUtils/src/SNMPClient.h @@ -68,6 +68,18 @@ namespace tmx::utils int snmp_version_ = 3; // default to 3 since previous versions not compatable currently /*Time after which the the snmp request times out*/ int timeout_ = 10000; + /** + * @brief Helper method for populating snmp_respons_obj with SNMP get response. + * @param val response object + * @param response pdu + */ + void process_snmp_get_response( snmp_response_obj &val, const snmp_pdu &response) const; + /** + * @brief Helper method for logging successful SNMP set responses + * @param val response object + * @param input_oid OID + */ + void process_snmp_set_response( snmp_response_obj &val, const std::string &input_oid) const; public: /** @brief Constructor for Traffic Signal Controller Service client. diff --git a/src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp b/src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp index 6fa1fcfcf..967a3c186 100644 --- a/src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp +++ b/src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp @@ -33,27 +33,27 @@ namespace SpatPlugin { void SpatPlugin::UpdateConfigSettings() { if (this->IsPluginState(IvpPluginState_registered)) { - std::string signalGroupMappingJson; + std::string signal_group_mapping_json; std::string ip_address; unsigned int port; - std::string scIp; - unsigned int scSNMPPort; - std::string intersectionName; - unsigned int intersectionId; - GetConfigValue("SignalGroupMapping", signalGroupMappingJson, &data_lock); + std::string signal_controller_ip; + unsigned int signal_controller_snmp_port; + std::string intersection_name; + unsigned int intersection_id; + GetConfigValue("SignalGroupMapping", signal_group_mapping_json, &data_lock); GetConfigValue("Local_IP", ip_address, &data_lock); GetConfigValue("Local_UDP_Port", port, &data_lock); - GetConfigValue("TSC_IP", scIp, &data_lock); - GetConfigValue("TSC_Remote_SNMP_Port", scSNMPPort,&data_lock); - GetConfigValue("Intersection_Name", intersectionName,&data_lock); - GetConfigValue("Intersection_Id", intersectionId, &data_lock); + GetConfigValue("TSC_IP", signal_controller_ip, &data_lock); + GetConfigValue("TSC_Remote_SNMP_Port", signal_controller_snmp_port,&data_lock); + GetConfigValue("Intersection_Name", intersection_name,&data_lock); + GetConfigValue("Intersection_Id", intersection_id, &data_lock); GetConfigValue("SPAT_Mode", spatMode, &data_lock); if (scConnection) { - scConnection.reset(new SignalControllerConnection(ip_address, port, signalGroupMappingJson, scIp, scSNMPPort, intersectionName, intersectionId)); + scConnection.reset(new SignalControllerConnection(ip_address, port, signal_group_mapping_json, signal_controller_ip, signal_controller_snmp_port, intersection_name, intersection_id)); } else { - scConnection = std::make_unique(ip_address, port, signalGroupMappingJson, scIp, scSNMPPort, intersectionName, intersectionId); + scConnection = std::make_unique(ip_address, port, signal_group_mapping_json, signal_controller_ip, signal_controller_snmp_port, intersection_name, intersection_id); } auto connected = scConnection->initializeSignalControllerConnection(); if ( connected ) { @@ -81,7 +81,7 @@ namespace SpatPlugin { } } else { - PLOG(tmx::utils::logERROR) << "Traffic Signal Controller at " << scIp << ":" << scSNMPPort << " failed!"; + PLOG(tmx::utils::logERROR) << "Traffic Signal Controller at " << signal_controller_ip << ":" << signal_controller_snmp_port << " failed!"; SetStatus(keyConnectionStatus, "DISCONNECTED"); this->isConnected = false;