52 #include "safec_lib.h"
57 #include "secure_wrapper.h"
60 #define TR181_SNMPOID_FILE "/etc/tr181_snmpOID.conf"
61 #define interface_STB "STB"
62 #define interface_CM "CM"
63 #define SNMP_AGENT_CM_IP_ADDRESS "192.168.100.1"
64 #define SNMP_AGENT_STB_IP_ADDRESS "127.0.0.1"
65 #define SNMP_COMMUNITY "hDaFHJG7"
67 GHashTable* hostIf_snmpAdapter::ifHash = NULL;
68 GHashTable* hostIf_snmpAdapter::m_notifyHash = NULL;
69 GMutex* hostIf_snmpAdapter::m_mutex = NULL;
70 map<string, vector<pair <string, string>>> hostIf_snmpAdapter::tr181Map;
93 g_hash_table_destroy(m_notifyHash);
105 ifstream fileStream (TR181_SNMPOID_FILE);
106 char delimeter[] =
" \t\n\r\f\v";
109 if (fileStream.is_open())
111 while(getline(fileStream, line))
113 int pos = line.find(
'=');
114 if(pos != string::npos)
116 string OID_value,interface_value;
117 string key = line.substr(0, pos);
118 int result = line.find(interface_STB);
121 interface_value = interface_STB;
122 line.erase(line.find(interface_value));
123 OID_value = line.substr(pos+1);
127 interface_value = interface_CM;
128 line.erase(line.find(interface_value));
129 OID_value = line.substr(pos+1);
131 key.erase(0, key.find_first_not_of(delimeter));
132 key.erase(key.find_last_not_of(delimeter) + 1);
134 OID_value.erase(0, OID_value.find_first_not_of(delimeter));
135 OID_value.erase(OID_value.find_last_not_of(delimeter) + 1);
137 tr181Map.insert( { key, {{ OID_value, interface_value }} } );
143 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] Error opening %s fileStream.", __FUNCTION__, __LINE__, TR181_SNMPOID_FILE );
162 ifHash = g_hash_table_new(NULL,NULL);
170 RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,
"Caught exception, not able create SNMP Device RDK Central instance..\n");
172 g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
177 GList* hostIf_snmpAdapter::getAllInstances()
180 return g_hash_table_get_keys(ifHash);
188 g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
193 void hostIf_snmpAdapter::closeAllInstances()
197 GList* tmp_list = g_hash_table_get_values (ifHash);
202 tmp_list = tmp_list->next;
208 void hostIf_snmpAdapter::getLock()
212 m_mutex = g_mutex_new();
214 g_mutex_lock(m_mutex);
217 void hostIf_snmpAdapter::releaseLock()
219 g_mutex_unlock(m_mutex);
222 GHashTable* hostIf_snmpAdapter::getNotifyHash()
227 return m_notifyHash = g_hash_table_new(g_str_hash, g_str_equal);
237 int ret = NOT_HANDLED;
238 char cmd[BUFF_LENGTH_256] = { 0 };
239 char resultBuff[BUFF_LENGTH_256] = { 0 };
240 char delimeter[] =
" \t\n\r\f\v";
241 map<string, vector<pair <string, string>>>::iterator it;
242 string consoleString(
"");
247 it = tr181Map.find(stMsgData->
paramName);
248 if (it != tr181Map.end())
250 string value = it->second[0].second;
251 if (value.compare(interface_STB) == 0)
253 snprintf (cmd, BUFF_LENGTH_256,
"snmpget -OQ -Ir -v 2c -c %s %s %s",
255 SNMP_AGENT_STB_IP_ADDRESS,
256 it->second[0].first.c_str());
260 snprintf (cmd, BUFF_LENGTH_256,
"snmpget -OQ -Ir -v 2c -c %s %s %s",
262 SNMP_AGENT_CM_IP_ADDRESS,
263 it->second[0].first.c_str());
266 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s] %s\n", __FUNCTION__, cmd);
270 int pos = consoleString.find(
'=');
271 if(pos != string::npos)
273 string subStr = consoleString.substr(pos + 1);
274 subStr.erase(0, subStr.find_first_not_of(delimeter));
275 subStr.erase(subStr.find_last_not_of(delimeter) + 1);
290 stMsgData->
paramtype = hostIf_StringType;
298 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] %s NOT found in the map.\n", __FUNCTION__, __LINE__, stMsgData->
paramName );
311 int ret = NOT_HANDLED;
312 char cmd[BUFF_LENGTH_256] = { 0 };
313 char resultBuff[BUFF_LENGTH_256] = { 0 };
314 map<string, vector<pair <string, string>>>::iterator it;
318 #define CMD(cmd, length, args...) ({ snprintf(cmd, length, args); fp = (v_secure_popen("r", args); )})
320 #define CMD(cmd, length, args...) ({ snprintf(cmd, length, args); })
325 it = tr181Map.find(stMsgData->
paramName);
326 if (it != tr181Map.end())
328 string value = it->second[0].second;
331 case hostIf_StringType:
332 if (value.compare(interface_STB) == 0){
333 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s s %s",
334 SNMP_COMMUNITY, SNMP_AGENT_STB_IP_ADDRESS,
335 it->second[0].first.c_str(),
339 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s s %s",
340 SNMP_COMMUNITY, SNMP_AGENT_CM_IP_ADDRESS,
341 it->second[0].first.c_str(),
346 case hostIf_IntegerType:
347 if (value.compare(interface_STB) == 0)
349 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s i %d",
350 SNMP_COMMUNITY, SNMP_AGENT_STB_IP_ADDRESS,
351 it->second[0].first.c_str(),
355 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s i %d",
356 SNMP_COMMUNITY, SNMP_AGENT_CM_IP_ADDRESS,
357 it->second[0].first.c_str(),
362 case hostIf_UnsignedIntType:
363 if (value.compare(interface_STB) == 0)
365 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s u %d",
367 SNMP_AGENT_STB_IP_ADDRESS,
368 it->second[0].first.c_str(),
372 CMD(cmd, BUFF_LENGTH_256,
"snmpset -v 2c -c %s %s %s u %d",
374 SNMP_AGENT_CM_IP_ADDRESS,
375 it->second[0].first.c_str(),
380 case hostIf_BooleanType:
381 case hostIf_DateTimeType:
382 case hostIf_UnsignedLongType:
384 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] %s not supported type %d\n", __FUNCTION__, __LINE__, stMsgData->
paramName, stMsgData->
paramtype);
388 RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,
"[%s] %s\n", __FUNCTION__, cmd);
391 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s]: cannot run command [%s]\n", __FUNCTION__, cmd);
393 }
else if (fgets (resultBuff, BUFF_LENGTH_256, fp) == NULL) {
394 RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s]: cannot read output from command [%s]\n", __FUNCTION__, cmd);
395 v_secure_pclose (fp);
398 ret = v_secure_pclose(fp);
401 RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF,
"[%s]: command [%s] returned [%s]\n", __FUNCTION__, cmd, resultBuff);
403 ret = read_command_output (cmd, resultBuff, BUFF_LENGTH_256);
405 stMsgData->
faultCode = (OK == ret)?fcNoFault:fcRequestDenied;
410 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%d] %s NOT found in the map.\n", __FUNCTION__,__LINE__, stMsgData->
paramName );