#ifndef _IPMI_TASK_H_
#define _IPMI_TASK_H_

#include<iostream>
#include<string>
#include "ace/Task.h"
#include "ace/Message_Queue.h"
#include "ace/INET_Addr.h"
#include "ace/Thread_Manager.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/Trace.h"
#include "ace/Svc_Handler.h"
#include "ace/OS.h"
#include "ace/Log_Msg.h"
#include "ipmi_main.h"                                                                                                                             
                                                                                                                             
using namespace std;

class Ipmi_Task : public ACE_Task<ACE_MT_SYNCH>
{
  private:
          static os_handler_t *os_hnd;     
          
  public:           
          Ipmi_Task();   
          virtual int open(); 
          virtual int svc(); 
          static void*  worker();
          os_handler_t* fetch_handler();   
         
};

#endif
#include "ipmi_main.h"
#include "ipmi_task.h"
#include "Server.h"

// IPMI_Domain_Name_Len = 32
// IPMI_Entity_Name_Len = 64
// IPMI_Sensor_Name_Len = 98

ipmi_main::ipmi_main()
{
                                           
  cout<<"Calling the constructor"<<endl;
  thresh_sensor_read  ="";
  discre_sensor_read  ="";
  command             ="";   
  entityinfo          =""; 
  sdrinfo             ="";
  mcinfo              ="";
  power_info          =""; 
  reset_info          =""; 
  retry               =0; 
  org_retry           =0;
 
}

void
ipmi_main::read_thresh_sensor(ipmi_sensor_t             *sensor,
                   int                       err,
                   enum ipmi_value_present_e value_present,
                   unsigned int              raw_value,
                   double                    val,
                   ipmi_states_t             *states,
                   void                      *cb_data)
{

  printf("Inside the sensor threshold reading\n");
 
  int entity_id, entity_instance;   
  char entity_name[64]; 
  char sensor_name[98];
  char sensor_val[30];
  string tmp_val,tmp_name; 
  ipmi_main *tmp_obj ;

  ipmi_entity_t *entity;   
  
  if(err)
  {
   goto out ;  
  }
  memset(sensor_val,'\0',sizeof(sensor_val));
  
/*ipmi_main *tmp_obj = (ipmi_main *) cb_data;       
  ipmi_entity_t *entity = ipmi_sensor_get_entity(sensor); */ 

  tmp_obj = (ipmi_main *) cb_data;   
  entity  = ipmi_sensor_get_entity(sensor); 
  if(!entity) 
  {
   goto out ; 
  }  
  entity_id = ipmi_entity_get_entity_id(entity);
  printf("Entity_id=%d\n",entity_id);    
  entity_instance = ipmi_entity_get_entity_instance(entity); 
  printf("Entity_instance=%d\n",entity_instance); 
  memset(entity_name,'\0',sizeof(entity_name));
  ipmi_entity_get_name(entity, entity_name, sizeof(entity_name));
  printf("Entity_name=%s\n",entity_name);  
  ipmi_sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
  printf("Sensor_name=%s\n",sensor_name);    
    
  switch (value_present)
    {
    case IPMI_NO_VALUES_PRESENT:
        printf("  no value present\n");
        break;
    case IPMI_BOTH_VALUES_PRESENT:
        {
            const char *percent = "";
            const char *base;
            const char *mod_use = "";
            const char *modifier = "";
            const char *rate;
                                                                                                                             
            base = ipmi_sensor_get_base_unit_string(sensor);
            if (ipmi_sensor_get_percentage(sensor))
                percent = "%";
            switch (ipmi_sensor_get_modifier_unit_use(sensor)) {
            case IPMI_MODIFIER_UNIT_NONE:
                break;
            case IPMI_MODIFIER_UNIT_BASE_DIV_MOD:
                mod_use = "/";
                modifier = ipmi_sensor_get_modifier_unit_string(sensor);
                break;
            case IPMI_MODIFIER_UNIT_BASE_MULT_MOD:
                mod_use = "*";
                modifier = ipmi_sensor_get_modifier_unit_string(sensor);
                break;
            }
            rate = ipmi_sensor_get_rate_unit_string(sensor);
                                                                                                                             
            printf("Sensor Reading= %lf%s %s%s%s%s\n", val, percent,
                   base, mod_use, modifier, rate);
            sprintf(sensor_val,"%lf",val);  
            printf("Sensor Reading converted =%s\n",sensor_val);
            tmp_name = sensor_name;
            tmp_val  = sensor_val; 
            tmp_obj->thresh_sensor_read = tmp_obj->thresh_sensor_read + tmp_name +" = "+tmp_val+"$";
            cout<<"sensorname_val:"<<tmp_obj->thresh_sensor_read<<endl;                                
         break;   
        }
        /* FALLTHROUGH */
    case IPMI_RAW_VALUE_PRESENT:
        printf("Sensor Reading=%d", raw_value);
        sprintf(sensor_val,"%d",raw_value);
        tmp_name = sensor_name;
        tmp_val  = sensor_val;  
        tmp_obj->thresh_sensor_read = tmp_obj->thresh_sensor_read + tmp_name +" = "+tmp_val+"$";  
        cout<<"Raw sensorname_val:"<<tmp_obj->thresh_sensor_read<<endl;  
    }
     
out:
    if(err)
    {
     printf("Error: reading the threshold sensor\n");             
    }   
   
}

void 
ipmi_main::read_discrete_sensor(ipmi_sensor_t *sensor,
                    int           err,
                    ipmi_states_t *states,
                    void          *cb_data)
{
  printf("Inside the read_discrete_sensor\n"); 
  int i;
  
  int entity_id, entity_instance;
  char entity_name[64];
  char sensor_name[98];                                      
//  ipmi_main *tmp_obj = (ipmi_main *) cb_data;
  ipmi_main *tmp_obj;
  ipmi_entity_t *entity; 
         
  if(err)
  {
   goto out;   
  }
                                                                             
// ipmi_entity_t *entity = ipmi_sensor_get_entity(sensor);

  entity = ipmi_sensor_get_entity(sensor); 
  if(!entity)
  {
   goto out ;
  }
  
  tmp_obj = (ipmi_main *) cb_data;
    
  entity_id = ipmi_entity_get_entity_id(entity);
  printf("Entity_id=%d\n",entity_id);
  entity_instance = ipmi_entity_get_entity_instance(entity);
  printf("Entity_instance=%d\n",entity_instance);
  memset(entity_name,'\0',sizeof(entity_name));
  ipmi_entity_get_name(entity, entity_name, sizeof(entity_name));
  printf("Entity_name=%s\n",entity_name);
  ipmi_sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
  printf("Sensor_name=%s\n",sensor_name); 

    
  for (i=0; i<15; i++) 
  {
        int val, rv, sta_val;
                                                                                                                             
        rv = ipmi_sensor_discrete_event_readable(sensor, i, &val);
        if (rv || !val)
            continue;
        
        char tmp_state[30],tmp_statval[30];
        string s1,s2,sname; 

        memset(tmp_state,'\0',sizeof(tmp_state));
        memset(tmp_statval,'\0',sizeof(tmp_statval));      
                     
        sta_val = ipmi_is_state_set(states, i);    
        printf("  state %d value is %d\n", i, sta_val);
        sprintf(tmp_state,"%d",i);        
        sprintf(tmp_statval,"%d",sta_val);
        s1 = tmp_state;
        s2 = tmp_statval;
        sname = sensor_name;
        sname = sname + s1;
        
        tmp_obj->discre_sensor_read = tmp_obj->discre_sensor_read + sname + " = " + s2 + "$";               
        cout<<"Discrete name_val:"<<tmp_obj->discre_sensor_read<<endl;  
  }
   
out:
    if(err) 
    {
     printf("Error: reading the discrete sensor\n");  
    }  
}


void 
ipmi_main::handle_sensor(ipmi_entity_t *entity, ipmi_sensor_t *sensor, void *cb_data)
{
  int rv; 
  printf("Inside the handle_sensor\n");    
  char sensor_name[98];
  ipmi_main *tmp_obj = (ipmi_main *) cb_data;
  ipmi_sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
  printf("Sensor_name=%s\n",sensor_name);
  
  //Finding the sensor whether threshold or discrete
  if (ipmi_sensor_get_event_reading_type(sensor)
        == IPMI_EVENT_READING_TYPE_THRESHOLD)
  {
    // Handling the threshold sensors 
    rv = ipmi_sensor_get_reading(sensor, read_thresh_sensor, tmp_obj); 
    if(rv)
    {
     printf("Error:in reading threshold sensor\n");          
    }  
  }
  else
  {
    // Handling the discete sensors     
    rv = ipmi_sensor_get_states(sensor, read_discrete_sensor, tmp_obj);
    if(rv)  
    {
     printf("Error:in reading discrete sensor\n");   
    }  
  }
                                               
} 


void
ipmi_main::handle_entity(ipmi_entity_t *entity, void *cb_data)
{
  printf("Inside the handle_entity\n");
  char entity_name[64];
  ipmi_main *tmp_obj = (ipmi_main *) cb_data; 
  memset(entity_name,'\0',sizeof(entity_name));
  ipmi_entity_get_name(entity, entity_name, sizeof(entity_name));
  printf("Entity_name=%s\n",entity_name); 
  ipmi_entity_iterate_sensors(entity, handle_sensor, tmp_obj);

}

void ipmi_main::entity_info(ipmi_entity_t *entity, void *cb_data)
{
  printf("Inside the entity_info\n");
  char entity_name[64];
  ipmi_main *tmp_obj = (ipmi_main *) cb_data;
  memset(entity_name,'\0',sizeof(entity_name));
  ipmi_entity_get_name(entity, entity_name, sizeof(entity_name));
  printf("Entity_name=%s\n",entity_name);
  tmp_obj->entityinfo = tmp_obj->entityinfo + "entityname = " + entity_name + "$";                                         
       
}


void
ipmi_main::sdrs_fetched(ipmi_sdr_info_t *sdrs,
             int             err,
             int             changed,
             unsigned int    count,
             void            *cb_data)
{
  printf("Inside the sdrs_fetched\n");
  ipmi_main *tmp_obj = (ipmi_main *) cb_data; 
  unsigned int i;
  int rv; 
  if(err) 
  {
   printf("Error: Fetching in sdr\n");
   goto out_err;        
  }
 
  for(i=0;i<count;i++)
  {
    ipmi_sdr_t sdr;
    rv = ipmi_get_sdr_by_index(sdrs, i, &sdr);
    if (rv)
    continue;

    printf("Record_ID     =%d\n",sdr.record_id);
    printf("Type          =%d\n",sdr.type);       
    printf("Major_version =%d\n",sdr.major_version);
    printf("Minor_version =%d\n",sdr.minor_version);
    char rid[40];
    char rver[40]; 
    string sdrid,sdrver,sdrtype;
       
    memset(rid,'\0',sizeof(rid));
    memset(rver,'\0',sizeof(rver));
        
    sprintf(rid,"%d",sdr.record_id);
    sprintf(rver,"%d.%d",sdr.major_version,sdr.minor_version);  
    
    sdrid  = rid;
    sdrver = rver;    
                                                                               
    switch(sdr.type)
       {
         case IPMI_SDR_FULL_SENSOR_RECORD:
              printf("sdr_full_sensor_record\n");
              sdrtype ="sdr_full_sensor_record"; 
              break;
         case IPMI_SDR_COMPACT_SENSOR_RECORD:
              printf("sdr_compact_sensor_record\n");
              sdrtype ="sdr_compact_sensor_record";  
              break;
         case IPMI_SDR_ENITY_ASSOCIATION_RECORD:
              printf("sdr_entity_assocation_record\n");
              sdrtype ="sdr_entity_assocation_record";
              break;   
         case IPMI_SDR_DR_ENITY_ASSOCIATION_RECORD:
              printf("sdr_dr_entity_assocation_record\n");
              sdrtype ="sdr_dr_entity_assocation_record";
              break;
         case IPMI_SDR_GENERIC_DEVICE_LOCATOR_RECORD:
              printf("sdr_generic_device_locator_record\n"); 
              sdrtype ="sdr_generic_device_locator_record"; 
              break;
         case IPMI_SDR_FRU_DEVICE_LOCATOR_RECORD:
              printf("sdr_fru_device_locator_record\n");   
              sdrtype ="sdr_fru_device_locator_record";         
              break;
         case IPMI_SDR_MC_DEVICE_LOCATOR_RECORD:
              printf("sdr_mc_device_locator_record\n");
              sdrtype ="sdr_mc_device_locator_record"; 
              break;
         case IPMI_SDR_MC_CONFIRMATION_RECORD:
              printf("sdr_mc_confirmation_record\n");
              sdrtype ="sdr_mc_confirmation_record"; 
              break;  
         case IPMI_SDR_MC_BMC_MESSAGE_CHANNEL_RECORD:
              printf("sdr_mc_bmc_message_channel_record\n");
              sdrtype ="sdr_mc_bmc_message_channel_record";  
              break;
         default:
               printf("unknown_record\n");      
               sdrtype ="unknown_record";  
       }

      tmp_obj->sdrinfo = tmp_obj->sdrinfo +"sdrid = " + sdrid + "|" + "sdrtype = " + sdrtype + "|" + "sdrversion = " + sdrver + "$";         
      
  }
                    
     
 out_err:
     ipmi_sdr_info_destroy(sdrs, NULL, NULL);  
  
}

void ipmi_main::handle_mc(ipmi_domain_t *domain, ipmi_mc_t *mc, void *cb_data)
{

  printf("Inside the handle_mc\n");
  int sta,dev_id,manu_id,prod_id,maj_ver,min_ver;   
  char mc_name[64];
  
  string t_name; 
  ipmi_main *tmp_obj = (ipmi_main *) cb_data;

  memset(mc_name,'\0',sizeof(mc_name));       
  ipmi_mc_get_name(mc, mc_name, sizeof(mc_name));    
  printf("mc_name %s\n",mc_name);
  t_name = mc_name;
  
 tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_name = " + mc_name + "|" ;
       
  sta = ipmi_mc_provides_device_sdrs(mc);
  if(sta)
   {
    printf("mc has device SDRs\n");       
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_have_device_sdrs = true|";  
   }
  else
   { 
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_have_device_sdrs = false|";
   }

  sta = ipmi_mc_chassis_support(mc);
  if(sta)
   {
    printf("mc support chassis command\n");  
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_chassis_command = true|";   
   }
  else
   {
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_chassis_command = false|"; 
   } 
 
  sta = ipmi_mc_sensor_device_support(mc); 
  if(sta)
   {
    printf("mc_support sensor command\n");   
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_sensor_command = true|"; 
   }
  else
   {
    tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_sensor_command = false|";       
   } 

  sta = ipmi_mc_bridge_support(mc);
  if(sta) 
  {
   printf("mc_support bridge command\n");   
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_bridge_command = true|";
  }
  else
  {
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_bridge_command = false|";   
  }
  
  sta = ipmi_mc_fru_inventory_support(mc); 
  if(sta)
  {
   printf("mc_support fru inventory command\n"); 
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_fru_inventory_command = true|"; 
  }   
  else
  {
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_support_fru_inventory_command = false|";      
  }   

  sta = ipmi_mc_sel_device_support(mc);
  if(sta)
  {
   printf("mc_have event log\n");     
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_have_event_log = true|";   
  }
  else
  {
   tmp_obj->mcinfo = tmp_obj->mcinfo + "mc_have_event_log = false|";    
  }
  
  char ch_id[30], ch_maid[30], ch_prodid[30],ch_ver[30];   
  string t_id , t_maid , t_prodid , t_ver;
     
  memset(ch_id,'\0',sizeof(ch_id));
  memset(ch_maid,'\0',sizeof(ch_maid)); 
  memset(ch_prodid,'\0',sizeof(ch_prodid));
  memset(ch_ver,'\0',sizeof(ch_ver));  
   
  dev_id  = ipmi_mc_device_id(mc);              
  printf("dev_id       =%d\n",dev_id); 
  manu_id = ipmi_mc_manufacturer_id(mc);
  printf("manu_id      =%d\n",manu_id); 
  prod_id = ipmi_mc_product_id(mc); 
  printf("product_id   =%d\n",prod_id); 
  maj_ver = ipmi_mc_major_version(mc);
  min_ver = ipmi_mc_minor_version(mc); 
  printf("IPMI_version =%d.%d",maj_ver,min_ver);
   
  sprintf(ch_id,"%d",dev_id);
  sprintf(ch_maid,"%d",manu_id);
  sprintf(ch_prodid,"%d",prod_id); 
  sprintf(ch_ver,"%d.%d",maj_ver,min_ver); 
      
  t_id      = ch_id;
  t_maid    = ch_maid;
  t_prodid  = ch_prodid;
  t_ver     = ch_ver;
    
  tmp_obj->mcinfo = tmp_obj->mcinfo + "device_id = " + t_id + "|" + "manufact_id = " + t_maid + "|" + "product_id = " + t_prodid + "|" + "ipmi_ver = " + t_ver + "$";

    
}

void ipmi_main::handle_sdr(ipmi_domain_t *domain, ipmi_mc_t *mc, void *cb_data)
{
  printf("Inside the handle_sdr\n");
    
  int rv;
  ipmi_sdr_info_t *sdrs;
  ipmi_main *tmp_obj = (ipmi_main *) cb_data; 
  rv = ipmi_sdr_info_alloc(domain, mc, 0, 0, &sdrs);
  if(rv)
  {
   printf("Error:Allocating sdr info\n");
   goto out; 
  }
                                                                                                                             
  rv = ipmi_sdr_fetch(sdrs, sdrs_fetched, tmp_obj);
  if(rv)
  {
   printf("Error:fetching sdr\n");
   ipmi_sdr_info_destroy(sdrs, NULL, NULL);
  } 
                                                                                                                 
out: 
    if(rv) 
     {
       printf("Error:in allocating sdr info\n");    
     }
}


void ipmi_main::handle_power_control(ipmi_entity_t *entity, ipmi_control_t *control, void *cb_data)
{
  printf("Inside the handle_power_control\n");
  int control_type;  
     
  control_type = ipmi_control_get_type(control);

  if(control_type == IPMI_CONTROL_POWER)
  {
   int rv;
   int val =0;     
   ipmi_main *tmp_obj = (ipmi_main *) cb_data;  
   rv = ipmi_control_set_val(control, &val, NULL, NULL);  
   if(rv)
    {
     printf("Error:setting the control\n");
     tmp_obj->power_info = "Power off provision not avaliable$>";  
    }
   else
    {   
     tmp_obj->power_info = "Power off success$>";    
    }
  }
    
}

void ipmi_main::handle_reset_control(ipmi_entity_t *entity, ipmi_control_t *control, void *cb_data)
{
  printf("Inside the handle_reset_control\n");

  int control_type;
                                                                                                                             
  control_type = ipmi_control_get_type(control);
                                                                                                                             
  if(control_type == IPMI_CONTROL_ONE_SHOT_RESET)
  {
   int rv;
   int val =1;
   ipmi_main *tmp_obj = (ipmi_main *) cb_data;
   rv = ipmi_control_set_val(control, &val, NULL, NULL);
   if(rv)
    {
     tmp_obj->reset_info = "Reset provision not avaliable$>"; 
     printf("Error:setting the one_shot_reset control\n");
    }
    else
    {   
     tmp_obj->reset_info = "Reset success$>";    
    }
  }
      
}

void ipmi_main::handle_power(ipmi_entity_t *entity, void *cb_data)
{
  printf("Inside the handle_power\n");     
  int entity_id;
  entity_id = ipmi_entity_get_entity_id(entity);
  if(entity_id == IPMI_ENTITY_ID_SYSTEM_CHASSIS)
  {
    ipmi_main *tmp_obj = (ipmi_main *) cb_data;
    ipmi_entity_iterate_controls(entity,handle_power_control,tmp_obj);        
  }
  
}

void ipmi_main::handle_reset(ipmi_entity_t *entity, void *cb_data)
{
  printf("Inside the handle_reset\n");
  int entity_id;
  entity_id = ipmi_entity_get_entity_id(entity);
  if(entity_id == IPMI_ENTITY_ID_SYSTEM_CHASSIS)
  {
    ipmi_main *tmp_obj = (ipmi_main *) cb_data;
    ipmi_entity_iterate_controls(entity,handle_reset_control,tmp_obj);
  }
    
}

void ipmi_main::close_timeout(void *cb_data)
{
  printf("Inside the close_timeout\n");
  ipmi_main *tmp_obj = (ipmi_main *) cb_data;
  string response;
  response = response + "Timeout no response$>" ;
 
  // form the output 
  // put in to the parser task
                                              
}

void ipmi_main::shutdown(void *cb_data)
{
  printf("Inside the shutdown\n");
  ipmi_main *tmp_obj = (ipmi_main *) cb_data;
  string response ="";
  string temp =""; 

  if(tmp_obj->command == "get_sensors_detail") 
  {
    printf("shutdown get_sensors_detail\n"); 
    temp = tmp_obj->thresh_sensor_read + tmp_obj->discre_sensor_read;        
    
    if(temp == "")
    {
      printf("empty-->get_sensors_detail\n");  
      response = response + "Sensor detail not avaliable$>";      
    }
    else 
    {
      printf("no empty-->get_sensors_detail\n"); 
      response = response + temp + ">";
    }            
  }
  
  if(tmp_obj->command == "get_entity_info")
  { 
    printf("shutdown get_entity_info\n");
    temp = tmp_obj->entityinfo;    
    if(temp == "")
    {
     printf("empty-->get_entity_info\n"); 
     response = response + "Entity detail not avaliable$>"; 
    }
    else
    {
     printf("no empty-->get_entity_info\n");
     response = response + temp + ">";
    }   
  }

  if(tmp_obj->command == "get_sdr_info")
  {  
    temp = tmp_obj->sdrinfo;   
    if(temp == "") 
    {
      response = response + "SDR detail not avaliable$>"; 
    }  
    else
    {
     response = response + temp + ">"; 
    }
  }
   
  if(tmp_obj->command == "get_mc_info")
  { 
    temp = tmp_obj->mcinfo;
    if(temp == "")
    {
     response = response + "MC detail not avaliable$>";     
    }
    else
    {
     response = response + temp + ">"; 
    }     
  }

  if(tmp_obj->command == "power_control")
  {   
    temp = tmp_obj->power_info;
    if(temp == "")
    {
     response = response + "power control not avaliable$>";
    }
    else
    {
     response = response + temp + ">";    
    } 
  }
   
 if(tmp_obj->command == "reset_control")
 {
   temp = tmp_obj->reset_info;
   if(temp == "")
   {
    response = response + "reset control not avaliable$>";     
   }
   else
   {
    response = response + temp + ">";  
   }
 }

 
}
void ipmi_main::handle_domain(ipmi_domain_t *domain,
           int           err,
           unsigned int  conn_num,
           unsigned int  port_num,
           int           still_connected,
           void          *user_data)
{

  printf("Inside the handle_domain\n");
  ipmi_main *tmp_obj = (ipmi_main *) user_data;
  int rv; 
  if(err)
  {       
   printf("err:%d\n",err);
   printf("Error_reason:%s\n", strerror(err));
   tmp_obj->retry++;
   printf("retry:%d\n",tmp_obj->retry);      
   printf("org_retry:%d\n",tmp_obj->org_retry);                                                
                                            
   if(tmp_obj->retry == tmp_obj->org_retry) 
    {
      rv = ipmi_domain_close(domain, close_timeout, tmp_obj);    
      if(rv)
      {
       printf("Error: in close domain\n");    
      }
      else
      {
       printf("Domain close sucess\n");  
      }
    } 
   goto out;   
  } 
       
    
  // list of command handling
  // get_sensors_detail
  // get_entity_info
  // get_sdr_info
  // get_mc_info
          
  if(tmp_obj->command == "get_sensors_detail")  
  {
   printf("going get_sensors_detail\n");  
   rv = ipmi_domain_iterate_entities(domain, handle_entity, tmp_obj); 
   if(rv)
    {
     printf("Error:iterate_entity in getting sensor\n"); 
    } 
   ipmi_domain_close(domain, shutdown, tmp_obj);            
  }
  if(tmp_obj->command == "get_entity_info")
  {
   printf("going get_entity_info\n"); 
   rv = ipmi_domain_iterate_entities(domain, entity_info, tmp_obj);     
   if(rv)
    {
      printf("Error:iterate_entity \n"); 
    }
   ipmi_domain_close(domain, shutdown, tmp_obj);
  }
  if(tmp_obj->command == "get_sdr_info") 
  {
   
     rv = ipmi_domain_iterate_mcs(domain, handle_sdr, tmp_obj);
     if(rv)
     {
      printf("Error:iterate_mcs\n");
     }

     ipmi_domain_close(domain, shutdown, tmp_obj);                                                                 
                                                         
  }
  if(tmp_obj->command == "get_mc_info")
  {
    
   rv = ipmi_domain_iterate_mcs(domain, handle_mc, tmp_obj);             
   if(rv)
   {
    printf("Error:iterate_mcs\n");    
   }

   ipmi_domain_close(domain, shutdown, tmp_obj);

  } 
  if(tmp_obj->command == "power_control") 
  {
   rv = ipmi_domain_iterate_entities(domain, handle_power, tmp_obj);      
   if(rv)
   {
    printf("Error:iterate_entities in power_control\n"); 
   }
   ipmi_domain_close(domain, shutdown, tmp_obj);  
  }

  if(tmp_obj->command == "reset_control") 
  {
   rv = ipmi_domain_iterate_entities(domain, handle_reset, tmp_obj);   
   if(rv) 
   {
    printf("Error:iterate_entities in reset_control\n"); 
   }      
   ipmi_domain_close(domain, shutdown, tmp_obj);  
  }

  cout<<"END"<<endl;   

out:
 if(err)
  {
   printf("Domain error\n");  
  }  
}

//static os_handler_t *os_hnd;                                                                                                      
int ipmi_main::main1(string inputmsg)
{

// inputmsg ="[EMAIL PROTECTED]|623|3|1000000|md5|admin|guest|guest123|get_sensors_detail|";

 printf("Calling IMPI_MAIN INPUT  %s \n",inputmsg.c_str());    
   
 string::size_type pos1,pos2; 
 pos1 = 0;    
 pos2 = inputmsg.find("#",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");      
  return 0;     
 }
 
 printf("hea_pos:%d\n",pos2); 
 memset(this->m_header,'\0',sizeof(this->m_header));  
 memcpy (this->m_header, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_header));
 printf("header :%s:\n",this->m_header); 
    
 pos1 = pos2+1; 
 pos2 = inputmsg.find("@",pos1); 
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("comm_pos:%d\n",pos2); 
 memset(this->m_command,'\0',sizeof(this->m_command)); 
 memcpy (this->m_command, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_command));
 printf("main_command :%s:\n",this->m_command);   

 pos1 = pos2+1;                                                                                    
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("ip_pos:%d\n",pos2);                                                                                         
 memset(this->m_ipaddr,'\0',sizeof(this->m_ipaddr));
 memcpy (this->m_ipaddr, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_ipaddr));
 printf("ipaddr :%s:\n",this->m_ipaddr); 

 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("port_pos:%d\n",pos2);
 memset(this->m_port,'\0',sizeof(this->m_port));
 memcpy (this->m_port, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_port));
 printf("port :%s:\n",this->m_port);
                                                                                                                             
 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("retry_pos:%d\n",pos2);
 memset(this->m_retry,'\0',sizeof(this->m_retry));
 memcpy (this->m_retry, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_retry));
 printf("retry :%s:\n",this->m_retry);
 this->org_retry = atoi(this->m_retry);  
 printf("org_retry :%d\n",this->org_retry); 

 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("timeout_pos:%d\n",pos2);
 memset(this->m_timeout,'\0',sizeof(this->m_timeout));
 memcpy (this->m_timeout, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_timeout));
 printf("timeout :%s:\n",this->m_timeout);
 
 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("auth_pos:%d\n",pos2);
 memset(this->m_authtype,'\0',sizeof(this->m_authtype));
 memcpy (this->m_authtype, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_authtype));
 printf("authtype :%s:\n",this->m_authtype);
 unsigned int authcode;
 string tmp_auth = this->m_authtype;
 if(tmp_auth == "none") 
  {
   printf("auth = none\n");
   authcode = 0;  
  }
 if(tmp_auth == "md2")
  {
   printf("auth = md2\n"); 
   authcode = 1;
  }
 if(tmp_auth == "md5")
  {
   printf("auth = md5\n");
   authcode = 2;
  }
 if(tmp_auth == "straight")
  {
   printf("auth = straight\n");
   authcode = 4;
  }
 if(tmp_auth == "oem")
  {
   printf("auth = oem\n");
   authcode = 5;
  }
                                            

 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("privil_pos:%d\n",pos2);
 memset(this->m_privilege,'\0',sizeof(this->m_privilege));
 memcpy (this->m_privilege, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_privilege));
 printf("privilege :%s:\n",this->m_privilege);
 string tmp_priv;
 unsigned int priv_code;
 tmp_priv = this->m_privilege;
 if(tmp_priv == "callback") 
  {
   printf("privilege = callback\n");
   priv_code = 1;    
  }
 if(tmp_priv == "user")
  {
   printf("privilege = user\n");
   priv_code = 2; 
  }   
  if(tmp_priv == "operator")
  {
   printf("privilege = operator\n");
   priv_code = 3;
  }  
  if(tmp_priv == "admin")
  {
   printf("privilege = admin\n");
   priv_code = 4;
  }  
  if(tmp_priv == "oem")
  {
   printf("privilege = oem\n");
   priv_code = 5;
  }
  
 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("user_pos:%d\n",pos2);
 memset(this->m_user,'\0',sizeof(this->m_user));
 memcpy (this->m_user, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_user));
 printf("username :%s:\n",this->m_user);
 unsigned int user_length;
 user_length = strlen(this->m_user);
 printf("username_length:%d\n",user_length);  
 
 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("pass_pos:%d\n",pos2);
 memset(this->m_pass,'\0',sizeof(this->m_pass));
 memcpy (this->m_pass, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_pass));
 printf("password :%s:\n",this->m_pass);
 unsigned int pass_length;
 pass_length = strlen(this->m_pass);
 printf("pass_length:%d\n",pass_length);  

 pos1 = pos2+1;
 pos2 = inputmsg.find("|",pos1);
 if(pos2 == string::npos)
 {
  printf("ipmi message wrong format\n");
  return 0;
 }
 printf("subcommand_pos:%d\n",pos2);
 memset(this->m_subcommand,'\0',sizeof(this->m_subcommand));
 memcpy (this->m_subcommand, (inputmsg.substr(pos1,pos2-pos1)).c_str(),sizeof(this->m_subcommand));
 printf("password :%s:\n",this->m_subcommand);
 
 this->command = this->m_subcommand;
   
 
  char *tip[2];
  char *tport[2];

  tip[0]   = this->m_ipaddr;
  tport[0] = this->m_port;

  Ipmi_Task *tas = BPing_Server::instance()->get_ipmi_task();

  os_handler_t *os_hnd = tas->fetch_handler();
     
  int rv;
  ipmi_con_t  *con;   
                 
  //setting lan connection
  // authentication type   
  //  NONE      0
  //  MD2       1
  //  MD5       2
  //  STRAIGHT  4
  //  OEM       5
  
  // privilege levels
  // CALLBACK   1 
  // USER       2
  // OPERATOR   3
  // ADMIN      4
  // OEM        5 

  // IPMI_USERNAME_MAX  16
  // IPMI_PASSWORD_MAX  20 

    
    rv = ipmi_ip_setup_con(tip, tport, 1, authcode,  priv_code, (void*) this->m_user, user_length,(void *) this->m_pass, pass_length, os_hnd, NULL, &con);
  
  if (rv)
  {
    printf("Error:Unable to setting up LAN connection\n");   
    return 0;    
  }
           
  rv = ipmi_open_domain("", &con, 1, handle_domain, this, NULL, NULL, NULL, 0, NULL);  
  if (rv)
  { 
    printf("Error:Unable to open domain\n");      
    return 0; 
  }  
    
}

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Openipmi-developer mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openipmi-developer

Reply via email to