30 #include "secure_wrapper.h"
33 #define MAX_CMD_LEN 256
34 #define MAX_BUF_LEN 256
36 #define MAX_MEDIUM_LEN 64
37 #define CMD_TO_GET_MED_NUM "fdisk -l | grep Disk | egrep -v \"mtdblock|mmcblk\"| wc -l"
38 #define CMD_TO_GET_MED_NAME "fdisk -l | grep Disk | egrep -v \"mtdblock|mmcblk\"| sed -n %dp | awk '{print $2}'"
39 #define CMD_TO_CHECK_SMART_CAP "smartctl --scan | awk 'BEGIN {ORS=\",\"}{ print $1}'"
40 #define CMD_TO_CHECK_SMART_HEALTH "smartctl -A %s | egrep \"%s\" | awk 'BEGIN {ORS=\",\"} {print $9}'"
41 #define STORAGE_PHYMED_SMARTPARAMS "Raw_Read_Error_Rate|\
43 Airflow_Temperature_Cel|\
45 Reallocated_Sector_Ct|\
49 PhysicalMediumMembers_t hostIf_PhysicalMedium::physicalMediumMembers = {{
'\0'}, {
'\0'}, {
'\0'}, {
'\0'}, {
'\0'}, {
'\0'}, {
'\0'},
false, 0, {
'\0'}, 0,
false, {
'\0'},
false};
50 GHashTable *hostIf_PhysicalMedium::phyMedHash = NULL;
51 GMutex *hostIf_PhysicalMedium::m_mutex = NULL;
60 PHYMED_GEN_FAILURE = -1,
61 PHYMED_CMD_EXEC_FAIL = -2,
62 PHYMED_INVALID_PARAM = -3
72 PHYMED_HEALTH_INVALID = 100,
73 PHYMED_HEALTH_OK = 101,
74 PHYMED_HEALTH_FAILING = 102,
75 PHYMED_HEALTH_ERROR = 103,
90 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"Inside constructor for dev_id:%d\n", dev_id);
102 m_mutex = g_mutex_new();
104 g_mutex_lock(m_mutex);
114 g_mutex_unlock(m_mutex);
117 int hostIf_PhysicalMedium::rebuildHash()
120 int storageServiceMaxInstance =0;
121 int phyMedMaxInstance = 0;
132 phyMedHash = g_hash_table_new(NULL, NULL);
135 memset(&stMsgData, 0 ,
sizeof(stMsgData));
136 if(OK == hostIf_StorageSrvc :: get_Device_StorageSrvc_ClientNumberOfEntries(&stMsgData))
138 storageServiceMaxInstance = stMsgData.
paramValue;
139 for(
int storageServiceInstance = 1; storageServiceInstance<= storageServiceMaxInstance; storageServiceInstance++)
141 memset(&stMsgData, 0 ,
sizeof(stMsgData));
142 if((phyMedMaxInstance = getPhysicalMediumNumberOfEntries (storageServiceInstance))!=0)
144 for(
int phyMedInstance = 1; phyMedInstance<= phyMedMaxInstance; phyMedInstance++)
149 g_hash_table_insert(phyMedHash, (gpointer)((storageServiceInstance * 100 ) + phyMedInstance), pRet);
153 RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,
"Caught exception, not able create physical medium instance..\n");
170 hostIf_PhysicalMedium* hostIf_PhysicalMedium::getInstance(
int storageServiceInstanceNumber,
int dev_id)
172 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"Entering [%s]\n", __FUNCTION__);
176 if(NULL == phyMedHash)
180 pRet = (
hostIf_PhysicalMedium *)g_hash_table_lookup(phyMedHash, (gpointer) ((storageServiceInstanceNumber * 100) + dev_id));
185 GList* hostIf_PhysicalMedium::getAllInstances()
188 return g_hash_table_get_keys(phyMedHash);
197 g_hash_table_remove(phyMedHash, (gconstpointer)pDev->dev_id);
203 void hostIf_PhysicalMedium::closeAllInstances()
207 GList* values_list = g_hash_table_get_values (phyMedHash);
208 GList* tmp_list = values_list;
212 tmp_list = tmp_list->next;
215 g_list_free(values_list);
220 int hostIf_PhysicalMedium :: getPhysicalMediumNumberOfEntries(
int StorageServiceId)
223 char cmd[MAX_CMD_LEN]={
'\0'};
225 char buffer[MAX_BUF_LEN]={
'\0'};
227 if (1 == StorageServiceId)
229 snprintf(cmd, MAX_CMD_LEN, CMD_TO_GET_MED_NUM);
230 cmdOp = popen(cmd,
"r");
233 fgets(buffer, MAX_BUF_LEN, cmdOp);
234 num = strtol(buffer,NULL,10);
239 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in execuing the command:%s errno:%d\n", cmd, errno);
246 int hostIf_PhysicalMedium:: get_Device_Service_StorageMedium_ClientNumberOfEntries(
HOSTIF_MsgData_t *stMsgData,
int dev_id)
252 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Received dev_id:%d\n",dev_id);
253 num = getPhysicalMediumNumberOfEntries(dev_id);
255 stMsgData->
paramtype = hostIf_UnsignedIntType;
256 stMsgData->
paramLen =
sizeof(
unsigned int);
258 if(OK != rebuildHash())
278 int ret = get_StorageService_PhyMed_Fields(eName);
282 stMsgData->
paramValue[TR69HOSTIFMGR_MAX_PARAM_LEN -1]=
'\0';
284 stMsgData->
paramtype = hostIf_StringType;
302 int ret = get_StorageService_PhyMed_Fields(eHealth);
306 stMsgData->
paramValue[TR69HOSTIFMGR_MAX_PARAM_LEN -1]=
'\0';
308 stMsgData->
paramtype = hostIf_StringType;
326 int ret = get_StorageService_PhyMed_Fields(eSmartCapable);
331 stMsgData->
paramtype = hostIf_BooleanType;
521 char mediumName[MAX_MEDIUM_LEN]={
'\0'};
522 char buffer[MAX_BUF_LEN]={
'\0'};
523 char dupbuf[MAX_BUF_LEN]={
'\0'};
524 char cmd[MAX_CMD_LEN]={
'\0'};
527 int ipNumOfEntries=0;
529 GList *devList =NULL;
533 memset(&msgData, 0,
sizeof(msgData));
534 ret=getMediumName(mediumName);
539 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"Entering [%s] asking for member %d\n", __FUNCTION__, phyMedMember);
549 if(!isSmartCapable())
555 int health = isMediumHealthOK();
558 case PHYMED_HEALTH_OK:
561 case PHYMED_HEALTH_FAILING:
564 case PHYMED_HEALTH_ERROR:
579 int hostIf_PhysicalMedium::getMediumName(
char* mediumName)
582 char cmd[MAX_CMD_LEN]= {
'\0'};
583 char buffer[MAX_BUF_LEN] = {
'\0'};
586 if(1 == storageServiceInstanceNumber)
588 snprintf(cmd, MAX_CMD_LEN, CMD_TO_GET_MED_NAME, this->dev_id);
589 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Executing command:%s\n",cmd);
590 cmdOp = popen(cmd,
"r");
593 fgets(buffer,MAX_BUF_LEN,cmdOp);
594 buffer[strlen(buffer)-2]=
'\0';
595 strncpy(mediumName,buffer,strlen(buffer));
601 retVal = PHYMED_CMD_EXEC_FAIL;
602 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in execuing the command:%s errno:%d\n", cmd, errno);
608 bool hostIf_PhysicalMedium::isSmartCapable()
611 char cmd[MAX_CMD_LEN]= {
'\0'};
612 char buffer[MAX_BUF_LEN] = {
'\0'};
613 char mediumName[MAX_PHY_MED_NAME_LEN] = {
'\0'};
616 bool smartCapable =
false;
619 if (OK == getMediumName(mediumName))
621 if(1 == storageServiceInstanceNumber)
623 snprintf(cmd,MAX_CMD_LEN,CMD_TO_CHECK_SMART_CAP);
624 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Executing command:%s\n",cmd);
625 cmdOp = popen(cmd,
"r");
628 fgets(buffer,MAX_BUF_LEN,cmdOp);
629 token = strtok_r(buffer,
",",&savePtr);
633 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"%s\n",token);
634 if(strcmp(token,mediumName)==0)
639 }
while((token=strtok_r(NULL,
",",&savePtr )));
645 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in execuing the command:%s errno:%d\n", cmd, errno);
652 int hostIf_PhysicalMedium :: isMediumHealthOK()
655 char cmd[MAX_CMD_LEN]= {
'\0'};
656 char buffer[MAX_BUF_LEN] = {
'\0'};
657 char mediumName[MAX_PHY_MED_NAME_LEN] = {
'\0'};
660 int health = PHYMED_HEALTH_INVALID;
663 if(OK == getMediumName(mediumName))
665 if( 1 == storageServiceInstanceNumber)
667 snprintf(cmd,MAX_CMD_LEN, CMD_TO_CHECK_SMART_HEALTH ,mediumName, STORAGE_PHYMED_SMARTPARAMS);
668 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Executing command:%s\n",cmd);
670 cmdOp = v_secure_popen(
"r",CMD_TO_CHECK_SMART_HEALTH,mediumName,STORAGE_PHYMED_SMARTPARAMS);
672 cmdOp = popen(cmd,
"r");
676 fgets(buffer,MAX_BUF_LEN,cmdOp);
677 token = strtok_r(buffer,
",",&savePtr);
681 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"%s\n",token);
682 if(strcmp(token,
"-")==0)
684 health = (health <=PHYMED_HEALTH_OK)?PHYMED_HEALTH_OK:health;
686 if(strcmp(token,
"FAILING_NOW")==0)
688 health = (health<PHYMED_HEALTH_FAILING)?PHYMED_HEALTH_FAILING:health;
690 if(strcmp(token,
"In_the_past")==0)
692 health = PHYMED_HEALTH_ERROR;
694 }
while((token=strtok_r(NULL,
",",&savePtr )));
698 health=PHYMED_INVALID_PARAM;
699 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in parsing output\n");
702 v_secure_pclose(cmdOp);
709 health = PHYMED_CMD_EXEC_FAIL;
710 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in executing Command:%d \n",errno);