Beispiel #1
0
INT analyzer_init()
{

   HNDLE hDB, hKey;
   char str[80];

   RUNINFO_STR(runinfo_str);
   EXP_PARAM_STR(exp_param_str);
   GLOBAL_PARAM_STR(global_param_str);
   TRIGGER_SETTINGS_STR(trigger_settings_str);

   /* open ODB structures */
   cm_get_experiment_database(&hDB, NULL);
   db_create_record(hDB, 0, "/Runinfo", strcomb((const char **)runinfo_str));
   db_find_key(hDB, 0, "/Runinfo", &hKey);
   if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) !=
       DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB");
      return 0;
   }

   db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb((const char **)exp_param_str));
   db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey);
   if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) !=
       DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init",
             "Cannot open \"/Experiment/Run Parameters\" tree in ODB");
      return 0;
   }

   sprintf(str, "/%s/Parameters/Global", analyzer_name);
   db_create_record(hDB, 0, str, strcomb((const char **)global_param_str));
   db_find_key(hDB, 0, str, &hKey);
   if (db_open_record
       (hDB, hKey, &global_param, sizeof(global_param), MODE_READ, NULL,
        NULL) != DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init", "Cannot open \"%s\" tree in ODB", str);
      return 0;
   }

   db_create_record(hDB, 0, "/Equipment/Trigger/Settings", strcomb((const char **)trigger_settings_str));
   db_find_key(hDB, 0, "/Equipment/Trigger/Settings", &hKey);

   if (db_open_record
       (hDB, hKey, &trigger_settings, sizeof(trigger_settings), MODE_READ, NULL,
        NULL) != DB_SUCCESS) {
      cm_msg(MERROR, "analyzer_init",
             "Cannot open \"/Equipment/Trigger/Settings\" tree in ODB");
      return 0;
   }

   //Initialize gData
   gData = new TEnvi();

   return SUCCESS;
}
Beispiel #2
0
INT master_ODB_set()
{
  INT   status;
  HNDLE hKey;
  char  str[MAX_ODB_PATH];
  char  str_aux[MAX_ODB_PATH];
//  int   i;

  sprintf(str_aux,"/Equipment/MasterGM2/Settings/Globals");
  sprintf(str, str_aux);

  // create ODB structure /Equipment/%s/Settings if it doesn't exist
  status = db_check_record(hDB, 0, str, MASTER_SETTINGS_ODB_STR, TRUE);
  if (status != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
      ss_sleep(3000);
    }
  
  // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);               
      return FE_ERR_ODB;
    }

  // Copy a set of keys from local memory to the database
  db_set_record(hDB, hKey, &master_settings_odb, sizeof(MASTER_SETTINGS_ODB), 0);

  print(str);

  return SUCCESS;
}
Beispiel #3
0
int null_init(HNDLE hkey, void **pinfo)
{
    HNDLE hDB, hkeybd;
    INT size, status;
    NULL_INFO *info;

    /* allocate info structure */
    info = calloc(1, sizeof(NULL_INFO));
    *pinfo = info;

    cm_get_experiment_database(&hDB, NULL);

    /* create NULL settings record */
    status = db_create_record(hDB, hkey, "BD", NULL_SETTINGS_STR);
    if (status != DB_SUCCESS)
        return FE_ERR_ODB;

    db_find_key(hDB, hkey, "BD", &hkeybd);
    size = sizeof(info->settings);
    db_get_record(hDB, hkeybd, &info->settings, &size, 0);

    /* open port */
    info->fd = null_open(info->settings.device);

    if (info->fd < 0)
        return FE_ERR_HW;

    return SUCCESS;
}
Beispiel #4
0
//fill mbank from /equip/environ/variables
INT read_periodic_event(char *pevent, INT off){
  printf("Reading Periodic Event\n");
  HNDLE hDB;
  cm_get_experiment_database(&hDB, NULL);

  HNDLE hkey;
  float temp1[3];
  INT size;
  // get key handle for temp
  db_find_key(hDB, 0, "/Equipment/Temperature/Variables/Input", &hkey);
  // return temp
  size = sizeof(temp1);
  db_get_data(hDB, hkey, &temp1, &size, TID_FLOAT);

  //char *pevent;
  bk_init(pevent);
  float *pdata;
  //  *pdata = temp1[0];
   // create SCLR bank
     bk_create(pevent, "NEW1", TID_FLOAT, &pdata);


// *pdata = 29.3;
printf("%f\n",temp1[0]);

   // close SCLR bank
bk_close(pevent, pdata);
printf("eo fB\n");
   return bk_size(pevent);


}
Beispiel #5
0
/**
 Scan ODB for alarms.
 @return AL_SUCCESS
 */
INT al_get_alarms(char *result, int result_size)
{
   HNDLE hDB, hkey, hsubkey;
   int i, j, n, flag, size;
   char alarm_class[32], msg[256], value_str[256], str[256];
   
   cm_get_experiment_database(&hDB, NULL);
   result[0] = 0;
   n = 0;
   db_find_key(hDB, 0, "/Alarms/Alarms", &hkey);
   if (hkey) {
      /* check global alarm flag */
      flag = TRUE;
      size = sizeof(flag);
      db_get_value(hDB, 0, "/Alarms/Alarm System active", &flag, &size, TID_BOOL, TRUE);
      if (flag) {
         for (i = 0;; i++) {
            db_enum_link(hDB, hkey, i, &hsubkey);
            
            if (!hsubkey)
               break;
            
            size = sizeof(flag);
            db_get_value(hDB, hsubkey, "Triggered", &flag, &size, TID_INT, TRUE);
            if (flag) {
               n++;
               
               size = sizeof(alarm_class);
               db_get_value(hDB, hsubkey, "Alarm Class", alarm_class, &size, TID_STRING,
                            TRUE);
               
                              
               size = sizeof(msg);
               db_get_value(hDB, hsubkey, "Alarm Message", msg, &size, TID_STRING, TRUE);
               
               size = sizeof(j);
               db_get_value(hDB, hsubkey, "Type", &j, &size, TID_INT, TRUE);
               
               if (j == AT_EVALUATED) {
                  size = sizeof(str);
                  db_get_value(hDB, hsubkey, "Condition", str, &size, TID_STRING, TRUE);
                  
                  /* retrieve value */
                  al_evaluate_condition(str, value_str);
                  sprintf(str, msg, value_str);
               } else
                  strlcpy(str, msg, sizeof(str));

               strlcat(result, alarm_class, result_size);
               strlcat(result, ": ", result_size);
               strlcat(result, str, result_size);
               strlcat(result, "\n", result_size);
            }
         }
      }
   }
   
   return n;
}
Beispiel #6
0
INT frontend_init()
{

  // get the LabView filename
  cm_get_experiment_database(&hDB, NULL);
  
  if(db_find_key(hDB, 0, Alarm, &hKey) != SUCCESS){
    db_create_key(hDB,0,Alarm,TID_INT);
  }

  return CM_SUCCESS;
}
Beispiel #7
0
/*!
 * <p>Constructs a PKey object, linked to a MIDAS experiment. It is
 * used to establish a hot-link to a <em>specific</em> sub-key.
 *
 * \param pRootKey   Pointer to the root key.
 * \param strKeyName Name of the key in the ODB, which one wants to link to.
 */
PKey::PKey(PKey *pRootKey, QString *strKeyName)
{
  fExperiment = pRootKey->fExperiment; // pointer to the experiment
  fHDB = pRootKey->GetHdb();           // handle to the ODB
  fValid = (db_find_key(fHDB, pRootKey->GetHkey(), (char *)strKeyName->ascii(), &fHKey) == DB_SUCCESS);
  if (fValid) {
    db_get_key(fHDB, fHKey, &fKey);
  } else {
    QString errMsg = QString("Cannot find %1 entry in online database").arg(*strKeyName);
    MidasMessage(errMsg.latin1());
  }
}
Beispiel #8
0
char *data_block_get_attr(lio_data_block_t *b, char *key)
{
    lio_data_block_attr_t *attr;

    //** See if the key exists
    attr = db_find_key(b->attr_stack, key);

    if (attr == NULL) {
        return(NULL);
    }

    return(attr->value);
}
Beispiel #9
0
/*-- Analyzer Init -------------------------------------------------*/
INT analyzer_init()
{
    HNDLE hDB, hKey;
    char str[80];

    RUNINFO_STR(runinfo_str);            // RUNINFO_STR in experim.h      rn:  not true !?!?!
    EXP_PARAM_STR(exp_param_str);        // EXP_PARAM_STR in experim.h    rn:  not true !!?!?!
    //GLOBAL_PARAM_STR(global_param_str);  // GLOBAL_PARAM_STR in experim.h

    /* open ODB structures */
    cm_get_experiment_database(&hDB, NULL);
    db_create_record(hDB, 0, "/Runinfo", strcomb(runinfo_str));
    db_find_key(hDB, 0, "/Runinfo", &hKey);
    if (db_open_record(hDB, hKey, &runinfo, sizeof(runinfo), MODE_READ, NULL, NULL) !=
            DB_SUCCESS) {
        cm_msg(MERROR, "analyzer_init", "Cannot open \"/Runinfo\" tree in ODB");
        return 0;
    }


    db_create_record(hDB, 0, "/Experiment/Run Parameters", strcomb(exp_param_str));
    db_find_key(hDB, 0, "/Experiment/Run Parameters", &hKey);
    if (db_open_record(hDB, hKey, &exp_param, sizeof(exp_param), MODE_READ, NULL, NULL) !=
            DB_SUCCESS) {
        cm_msg(MERROR, "analyzer_init",
               "Cannot open \"/Experiment/Run Parameters\" tree in ODB");
        return 0;
    }

    //printf("\n---testing testing 123----------------------------------------------- %i  \n\n\n\n ",runinfo.run_number);
    //runnr=runinfo.run_number;


    ParameterInit();


    return SUCCESS;
}
Beispiel #10
0
int main()
{
   int status, size;
   HNDLE hDB, hKey;

   /* connect to experiment */
   status = cm_connect_experiment("", "", "ODBTest", NULL);
   if (status != CM_SUCCESS)
      return 1;

   /* get handle to online database */
   cm_get_experiment_database(&hDB, &hKey);

   /* read key "runinfo/run number" */
   size = sizeof(run_number);
   status =
       db_get_value(hDB, 0, "/runinfo/run number", &run_number, &size, TID_INT, TRUE);
   if (status != DB_SUCCESS) {
      printf("Cannot read run number");
      return 0;
   }

   printf("Current run number is %d\n", run_number);

   /* set new run number */
   run_number++;
   db_set_value(hDB, 0, "/runinfo/run number", &run_number, size, 1, TID_INT);


   /* now open run_number with automatic updates */
   db_find_key(hDB, 0, "/runinfo/run number", &hKey);
   db_open_record(hDB, hKey, &run_number, sizeof(run_number),
                  MODE_READ, run_number_changed, NULL);

   printf("Waiting for run number to change. Hit RETURN to abort\n");

   do {
      cm_yield(1000);
   } while (!ss_kbhit());

   db_close_record(hDB, hKey);

   /* disconnect from experiment */
   cm_disconnect_experiment();

   return 1;
}
Beispiel #11
0
INT master_ODB_init(void)
{
  INT ret = SUCCESS;
  INT status;
  char str[MAX_ODB_PATH];
//  char str_aux[MAX_ODB_PATH];
  HNDLE hKey;

  sprintf(master_settings_odb.trigger_source, "Fake"); //Default to self-generated fake trigger signals
  master_settings_odb.rate = 12;
  sprintf(master_settings_odb.readout_name, "AMC13");
  sprintf(master_settings_odb.sim_name, "CaloSimulatorAMC13");


  dbprintf(str,"/Equipment/MasterGM2/Settings/Globals");
  sprintf(str,"/Equipment/MasterGM2/Settings/Globals");

  // create ODB structure if it doesn't exist
  status = db_check_record(hDB, 0, str, MASTER_SETTINGS_ODB_STR, TRUE);
  if( status != DB_SUCCESS )
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i\n",str,status);
      return FE_ERR_ODB;
    }
  // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);               
      return FE_ERR_ODB;
    }
  // creates hotlink of ODB subtree to structure master_settings_odb that's automatically updated
  if (db_open_record(hDB, hKey, &master_settings_odb, sizeof(MASTER_SETTINGS_ODB), MODE_READ, 
                     master_ODB_update, NULL) != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str);
      return FE_ERR_ODB;
    }
  
  print(str);

  return ret;

}
Beispiel #12
0
INT readout(char* pevent, INT off)
{

DWORD *pdata;

bk_init32(pevent);

bk_create(pevent,"INPT",TID_DWORD,&pdata);

int val = 98;

HNDLE hDB;
HNDLE hKey;

INT size;

cm_get_experiment_database(&hDB,NULL);

db_find_key(hDB,0,"/Equipment/Environment 2/Variables/Secret",&hKey);

size = sizeof(val);

db_get_data(hDB,hKey,&val,&size,TID_INT);

*pdata++ = val;

struct timeval t;
int status;
status = gettimeofday(&t,NULL);

printf(" t_tv.sec: %d",t.tv_sec);

printf(" ro: val is %d",val);

val++;

db_set_value(hDB,0,"/Equipment/Environment 2/Variables/Secret",&val,sizeof(val),1,TID_INT);

bk_close(pevent,pdata);

return bk_size(pevent);

}
Beispiel #13
0
int mscbbus_init(HNDLE hkey, void **pinfo)
{
   HNDLE hDB, hkeybd;
   INT size, status;
   MSCBBUS_INFO *info;

   /* allocate info structure */
   info = calloc(1, sizeof(MSCBBUS_INFO));
   *pinfo = info;

   cm_get_experiment_database(&hDB, NULL);

   /* create MSCBBUS settings record */
   status = db_create_record(hDB, hkey, "BD", MSCBBUS_SETTINGS_STR);
   if (status != DB_SUCCESS)
      return FE_ERR_ODB;

   db_find_key(hDB, hkey, "BD", &hkeybd);
   size = sizeof(info->settings);
   db_get_record(hDB, hkeybd, &info->settings, &size, 0);

   /* open port */
   // check if ethernet submaster
   info->fd = mscb_init(info->settings.mscb_device, sizeof(info->settings.mscb_device), info->settings.pwd, FALSE);

   if (info->fd < 0)
      return FE_ERR_HW;

   /* check if scs_210 is alive */
   status = mscb_addr(info->fd, MCMD_PING16, info->settings.address, 1);
   if (status != MSCB_SUCCESS) {
      if (status == MSCB_SEMAPHORE)
         printf("MSCB used by other process\n");
      else if (status == MSCB_SUBM_ERROR)
         printf("Error: MSCB Submaster not responding\n");
      else
         printf("MSCB Node %d does not respond\n", info->settings.address);

      return FE_ERR_HW;
   }

   return SUCCESS;
}
Beispiel #14
0
int data_block_set_attr(lio_data_block_t *b, char *key, char *val)
{
    lio_data_block_attr_t *attr;

    //** See if the key exists
    attr = db_find_key(b->attr_stack, key);

    if (attr == NULL) {  //** See if we need to add the attribute
        tbx_type_malloc_clear(attr, lio_data_block_attr_t, 1);
        attr->key = strdup(key);
    }

    if (attr->value != NULL) free(attr->value);  //** Free the old value
    attr->value = (val != NULL) ? strdup(val) : NULL;  //** Store the new one

    if (b->attr_stack == NULL) b->attr_stack = tbx_stack_new();
    tbx_stack_push(b->attr_stack, attr);

    return(0);
}
Beispiel #15
0
INT frontend_init()
{
   /* hardware initialization */
  INT status;
  char set_str[80];
  /* Book Setting space */
  TRIGGER_SETTINGS_STR(trigger_settings_str);

  /* Map /equipment/Trigger/settings for the sequencer */
  sprintf(set_str, "/Equipment/Trigger/Settings");
  status = db_create_record(hDB, 0, set_str, strcomb(trigger_settings_str));
  status = db_find_key (hDB, 0, set_str, &hSet);
  if (status != DB_SUCCESS)
    cm_msg(MINFO,"FE","Key %s not found", set_str);


    rs232(CMD_INIT, hSet, &info);
   /* print message and return FE_ERR_HW if frontend should not be started */

   return SUCCESS;
}
Beispiel #16
0
INT fgd_init(EQUIPMENT * pequipment)
{
   int status, size, i, j, index, offset;
   char str[256];
   HNDLE hDB, hKey, hNames, hThreshold;
   FGD_INFO *fgd_info;

   /* allocate private data */
   pequipment->cd_info = calloc(1, sizeof(FGD_INFO));
   fgd_info = (FGD_INFO *) pequipment->cd_info;

   /* get class driver root key */
   cm_get_experiment_database(&hDB, NULL);
   sprintf(str, "/Equipment/%s", pequipment->name);
   db_create_key(hDB, 0, str, TID_KEY);
   db_find_key(hDB, 0, str, &fgd_info->hKeyRoot);

   /* save event format */
   size = sizeof(str);
   db_get_value(hDB, fgd_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE);

   if (equal_ustring(str, "Fixed"))
      fgd_info->format = FORMAT_FIXED;
   else if (equal_ustring(str, "MIDAS"))
      fgd_info->format = FORMAT_MIDAS;
   else if (equal_ustring(str, "YBOS"))
      fgd_info->format = FORMAT_YBOS;

   /* count total number of channels */
   for (i = 0, fgd_info->num_channels = 0; pequipment->driver[i].name[0]; i++) {
      if (pequipment->driver[i].channels == 0) {
         cm_msg(MERROR, "fgd_init", "Driver with zero channels not allowed");
         return FE_ERR_ODB;
      }

      fgd_info->num_channels += pequipment->driver[i].channels;
   }

   if (fgd_info->num_channels == 0) {
      cm_msg(MERROR, "fgd_init", "No channels found in device driver list");
      return FE_ERR_ODB;
   }

   /* Allocate memory for buffers */
   fgd_info->names = (char *) calloc(fgd_info->num_channels, NAME_LENGTH);

   fgd_info->demand = (float *) calloc(fgd_info->num_channels, sizeof(float));
   fgd_info->measured = (float *) calloc(fgd_info->num_channels, sizeof(float));

   fgd_info->temp1 = (float *) calloc(fgd_info->num_channels, sizeof(float));
   fgd_info->temp2 = (float *) calloc(fgd_info->num_channels, sizeof(float));
   fgd_info->temp3 = (float *) calloc(fgd_info->num_channels, sizeof(float));

   fgd_info->update_threshold = (float *) calloc(fgd_info->num_channels, sizeof(float));

   fgd_info->demand_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float));
   fgd_info->measured_mirror = (float *) calloc(fgd_info->num_channels, sizeof(float));

   fgd_info->channel_offset = (INT *) calloc(fgd_info->num_channels, sizeof(INT));
   fgd_info->driver = (void *) calloc(fgd_info->num_channels, sizeof(void *));

   if (!fgd_info->driver) {
      cm_msg(MERROR, "hv_init", "Not enough memory");
      return FE_ERR_ODB;
   }

   /*---- Initialize device drivers ----*/

   /* call init method */
   for (i = 0; pequipment->driver[i].name[0]; i++) {
      sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name);
      status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey);
      if (status != DB_SUCCESS) {
         db_create_key(hDB, fgd_info->hKeyRoot, str, TID_KEY);
         status = db_find_key(hDB, fgd_info->hKeyRoot, str, &hKey);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "hv_init", "Cannot create %s entry in online database", str);
            free_mem(fgd_info);
            return FE_ERR_ODB;
         }
      }

      status = device_driver(&pequipment->driver[i], CMD_INIT, hKey);
      if (status != FE_SUCCESS) {
         free_mem(fgd_info);
         return status;
      }
   }

   /* compose device driver channel assignment */
   for (i = 0, j = 0, index = 0, offset = 0; i < fgd_info->num_channels; i++, j++) {
      while (j >= pequipment->driver[index].channels && pequipment->driver[index].name[0]) {
         offset += j;
         index++;
         j = 0;
      }

      fgd_info->driver[i] = &pequipment->driver[index];
      fgd_info->channel_offset[i] = offset;
   }

   /*---- create demand variables ----*/
   /* get demand from ODB */
   status =
       db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand);
   if (status == DB_SUCCESS) {
      size = sizeof(float) * fgd_info->num_channels;
      db_get_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, &size, TID_FLOAT);
   }
   /* let device driver overwrite demand values, if it supports it */
   for (i = 0; i < fgd_info->num_channels; i++) {
      if (fgd_info->driver[i]->flags & DF_PRIO_DEVICE) {
         device_driver(fgd_info->driver[i], CMD_GET_DEMAND,
                       i - fgd_info->channel_offset[i], &fgd_info->demand[i]);
         fgd_info->demand_mirror[i] = fgd_info->demand[i];
      } else
         fgd_info->demand_mirror[i] = -12345.f; /* use -12345 as invalid value */
   }
   /* write back demand values */
   status =
       db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand);
   if (status != DB_SUCCESS) {
      db_create_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", TID_FLOAT);
      db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Demand", &fgd_info->hKeyDemand);
   }
   size = sizeof(float) * fgd_info->num_channels;
   db_set_data(hDB, fgd_info->hKeyDemand, fgd_info->demand, size,
               fgd_info->num_channels, TID_FLOAT);
   db_open_record(hDB, fgd_info->hKeyDemand, fgd_info->demand,
                  fgd_info->num_channels * sizeof(float), MODE_READ, fgd_demand,
                  pequipment);

   /*---- create measured variables ----*/
   db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Current Measured",
                 fgd_info->measured, sizeof(float) * fgd_info->num_channels,
                 fgd_info->num_channels, TID_FLOAT);
   db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Current Measured", &fgd_info->hKeyMeasured);
   memcpy(fgd_info->measured_mirror, fgd_info->measured,
          fgd_info->num_channels * sizeof(float));

   /*---- create Temp1 measured variables ----*/
   db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp1",
                 fgd_info->temp1, sizeof(float) * fgd_info->num_channels,
                 fgd_info->num_channels, TID_FLOAT);
   db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp1", &fgd_info->hKeyTemp1);

   /*---- create Temp2 measured variables ----*/
   db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp2",
                 fgd_info->temp2, sizeof(float) * fgd_info->num_channels,
                 fgd_info->num_channels, TID_FLOAT);
   db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp2", &fgd_info->hKeyTemp2);

   /*---- create Temp3 measured variables ----*/
   db_merge_data(hDB, fgd_info->hKeyRoot, "Variables/Temp3",
                 fgd_info->temp3, sizeof(float) * fgd_info->num_channels,
                 fgd_info->num_channels, TID_FLOAT);
   db_find_key(hDB, fgd_info->hKeyRoot, "Variables/Temp3", &fgd_info->hKeyTemp3);

   /*---- get default names from device driver ----*/
   for (i = 0; i < fgd_info->num_channels; i++) {
      sprintf(fgd_info->names + NAME_LENGTH * i, "Default%%CH %d", i);
      device_driver(fgd_info->driver[i], CMD_GET_LABEL,
                    i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i);
   }
   db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Names",
                 fgd_info->names, NAME_LENGTH * fgd_info->num_channels,
                 fgd_info->num_channels, TID_STRING);

   /*---- set labels form midas SC names ----*/
   for (i = 0; i < fgd_info->num_channels; i++) {
      fgd_info = (FGD_INFO *) pequipment->cd_info;
      device_driver(fgd_info->driver[i], CMD_SET_LABEL,
                    i - fgd_info->channel_offset[i], fgd_info->names + NAME_LENGTH * i);
   }

   /* open hotlink on channel names */
   if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Names", &hNames) == DB_SUCCESS)
      db_open_record(hDB, hNames, fgd_info->names, NAME_LENGTH*fgd_info->num_channels,
                     MODE_READ, fgd_update_label, pequipment);

   /*---- get default update threshold from device driver ----*/
   for (i = 0; i < fgd_info->num_channels; i++) {
      fgd_info->update_threshold[i] = 1.f;      /* default 1 unit */
      device_driver(fgd_info->driver[i], CMD_GET_THRESHOLD,
                    i - fgd_info->channel_offset[i], &fgd_info->update_threshold[i]);
   }
   db_merge_data(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured",
                 fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels,
                 fgd_info->num_channels, TID_FLOAT);

   /* open hotlink on update threshold */
   if (db_find_key(hDB, fgd_info->hKeyRoot, "Settings/Update Threshold Measured", &hThreshold) == DB_SUCCESS)
     db_open_record(hDB, hThreshold, fgd_info->update_threshold, sizeof(float)*fgd_info->num_channels,
		    MODE_READ, NULL, NULL);
   
   /*---- set initial demand values ----*/
   // fgd_demand(hDB, fgd_info->hKeyDemand, pequipment);

   /* initially read all channels */
   for (i = 0; i < fgd_info->num_channels; i++)
      fgd_read(pequipment, i);

   return FE_SUCCESS;
}
Beispiel #17
0
/**
Trigger a certain alarm.
\code  ...
  lazy.alarm[0] = 0;
  size = sizeof(lazy.alarm);
  db_get_value(hDB, pLch->hKey, "Settings/Alarm Class", lazy.alarm, &size, TID_STRING, TRUE);

  // trigger alarm if defined
  if (lazy.alarm[0])
    al_trigger_alarm("Tape", "Tape full...load new one!", lazy.alarm, "Tape full", AT_INTERNAL);
  ...
\endcode
@param alarm_name Alarm name, defined in /alarms/alarms
@param alarm_message Optional message which goes with alarm
@param default_class If alarm is not yet defined under
                    /alarms/alarms/\<alarm_name\>, a new one
                    is created and this default class is used.
@param cond_str String displayed in alarm condition
@param type Alarm type, one of AT_xxx
@return AL_SUCCESS, AL_INVALID_NAME
*/
INT al_trigger_alarm(const char *alarm_name, const char *alarm_message, const char *default_class, const char *cond_str, INT type)
{
   if (rpc_is_remote())
      return rpc_call(RPC_AL_TRIGGER_ALARM, alarm_name, alarm_message, default_class, cond_str, type);

#ifdef LOCAL_ROUTINES
   {
      int status, size;
      HNDLE hDB, hkeyalarm, hkey;
      char str[256];
      ALARM a;
      BOOL flag;
      ALARM_ODB_STR(alarm_odb_str);

      cm_get_experiment_database(&hDB, NULL);

      /* check online mode */
      flag = TRUE;
      size = sizeof(flag);
      db_get_value(hDB, 0, "/Runinfo/Online Mode", &flag, &size, TID_INT, TRUE);
      if (!flag)
         return AL_SUCCESS;

      /* find alarm */
      sprintf(str, "/Alarms/Alarms/%s", alarm_name);
      db_find_key(hDB, 0, str, &hkeyalarm);
      if (!hkeyalarm) {
         /* alarm must be an internal analyzer alarm, so create a default alarm */
         status = db_create_record(hDB, 0, str, strcomb(alarm_odb_str));
         db_find_key(hDB, 0, str, &hkeyalarm);
         if (!hkeyalarm) {
            cm_msg(MERROR, "al_trigger_alarm", "Cannot create alarm record");
            return AL_ERROR_ODB;
         }

         if (default_class && default_class[0])
            db_set_value(hDB, hkeyalarm, "Alarm Class", default_class, 32, 1, TID_STRING);
         status = TRUE;
         db_set_value(hDB, hkeyalarm, "Active", &status, sizeof(status), 1, TID_BOOL);
      }

      /* set parameters for internal alarms */
      if (type != AT_EVALUATED && type != AT_PERIODIC) {
         db_set_value(hDB, hkeyalarm, "Type", &type, sizeof(INT), 1, TID_INT);
         strcpy(str, cond_str);
         db_set_value(hDB, hkeyalarm, "Condition", str, 256, 1, TID_STRING);
      }

      size = sizeof(a);
      status = db_get_record(hDB, hkeyalarm, &a, &size, 0);
      if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
         /* make sure alarm record has right structure */
         db_check_record(hDB, hkeyalarm, "", strcomb(alarm_odb_str), TRUE);

         size = sizeof(a);
         status = db_get_record(hDB, hkeyalarm, &a, &size, 0);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "al_trigger_alarm", "Cannot get alarm record");
            return AL_ERROR_ODB;
         }
      }

      /* if internal alarm, check if active and check interval */
      if (a.type != AT_EVALUATED && a.type != AT_PERIODIC) {
         /* check global alarm flag */
         flag = TRUE;
         size = sizeof(flag);
         db_get_value(hDB, 0, "/Alarms/Alarm system active", &flag, &size, TID_BOOL, TRUE);
         if (!flag)
            return AL_SUCCESS;

         if (!a.active)
            return AL_SUCCESS;

         if ((INT) ss_time() - (INT) a.checked_last < a.check_interval)
            return AL_SUCCESS;

         /* now the alarm will be triggered, so save time */
         a.checked_last = ss_time();
      }

      /* write back alarm message for internal alarms */
      if (a.type != AT_EVALUATED && a.type != AT_PERIODIC) {
         strncpy(a.alarm_message, alarm_message, 79);
         a.alarm_message[79] = 0;
      }

      /* now trigger alarm class defined in this alarm */
      if (a.alarm_class[0])
         al_trigger_class(a.alarm_class, alarm_message, a.triggered > 0);

      /* check for and trigger "All" class */
      if (db_find_key(hDB, 0, "/Alarms/Classes/All", &hkey) == DB_SUCCESS)
         al_trigger_class("All", alarm_message, a.triggered > 0);

      /* signal alarm being triggered */
      cm_asctime(str, sizeof(str));

      if (!a.triggered)
         strcpy(a.time_triggered_first, str);

      a.triggered++;
      strcpy(a.time_triggered_last, str);

      a.checked_last = ss_time();

      status = db_set_record(hDB, hkeyalarm, &a, sizeof(a), 0);
      if (status != DB_SUCCESS) {
         cm_msg(MERROR, "al_trigger_alarm", "Cannot update alarm record");
         return AL_ERROR_ODB;
      }

   }
#endif                          /* LOCAL_ROUTINES */

   return AL_SUCCESS;
}
Beispiel #18
0
INT al_trigger_class(const char *alarm_class, const char *alarm_message, BOOL first)
/********************************************************************\

  Routine: al_trigger_class

  Purpose: Trigger a certain alarm class

  Input:
    char   *alarm_class     Alarm class, must be defined in
                            /alarms/classes
    char   *alarm_message   Optional message which goes with alarm
    BOOL   first            TRUE if alarm is triggered first time
                            (used for elog)

  Output:

  Function value:
    AL_INVALID_NAME         Alarm class not defined
    AL_SUCCESS              Successful completion

\********************************************************************/
{
   int status, size, state;
   HNDLE hDB, hkeyclass;
   char str[256], command[256], tag[32], url[256];
   ALARM_CLASS ac;
   DWORD now = ss_time();

   tag[0] = 0;

   cm_get_experiment_database(&hDB, NULL);

   /* get alarm class */
   sprintf(str, "/Alarms/Classes/%s", alarm_class);
   db_find_key(hDB, 0, str, &hkeyclass);
   if (!hkeyclass) {
      cm_msg(MERROR, "al_trigger_class", "Alarm class %s not found in ODB", alarm_class);
      return AL_INVALID_NAME;
   }

   size = sizeof(ac);
   status = db_get_record(hDB, hkeyclass, &ac, &size, 0);
   if (status != DB_SUCCESS) {
      cm_msg(MERROR, "al_trigger_class", "Cannot get alarm class record");
      return AL_ERROR_ODB;
   }

   /* write system message */
   if (ac.write_system_message && (now - ac.system_message_last >= (DWORD)ac.system_message_interval)) {
      if (equal_ustring(alarm_class, "All"))
         sprintf(str, "General alarm: %s", alarm_message);
      else
         sprintf(str, "%s: %s", alarm_class, alarm_message);
      cm_msg(MTALK, "al_trigger_class", "%s", str);
      ac.system_message_last = now;
   }

   /* write elog message on first trigger if using internal ELOG */
   size = sizeof(url);
   if (ac.write_elog_message && first &&
       db_get_value(hDB, 0, "/Elog/URL", url, &size, TID_STRING, FALSE) != DB_SUCCESS)
      el_submit(0, "Alarm system", "Alarm", "General", alarm_class, str,
                "", "plain", "", "", 0, "", "", 0, "", "", 0, tag, sizeof(tag));

   /* execute command */
   if (ac.execute_command[0] &&
       ac.execute_interval > 0 && (INT) ss_time() - (INT) ac.execute_last > ac.execute_interval) {
      if (equal_ustring(alarm_class, "All"))
         sprintf(str, "General alarm: %s", alarm_message);
      else
         sprintf(str, "%s: %s", alarm_class, alarm_message);
      sprintf(command, ac.execute_command, str);
      cm_msg(MINFO, "al_trigger_class", "Execute: %s", command);
      ss_system(command);
      ac.execute_last = ss_time();
   }

   /* stop run */
   if (ac.stop_run) {
      state = STATE_STOPPED;
      size = sizeof(state);
      db_get_value(hDB, 0, "/Runinfo/State", &state, &size, TID_INT, TRUE);
      if (state != STATE_STOPPED) {
         cm_msg(MINFO, "al_trigger_class", "Stopping the run from alarm class \'%s\', message \'%s\'", alarm_class, alarm_message);
         cm_transition(TR_STOP, 0, NULL, 0, TR_DETACH, FALSE);
      }
   }

   status = db_set_record(hDB, hkeyclass, &ac, sizeof(ac), 0);
   if (status != DB_SUCCESS) {
      cm_msg(MERROR, "al_trigger_class", "Cannot update alarm class record");
      return AL_ERROR_ODB;
   }

   return AL_SUCCESS;
}
Beispiel #19
0
BOOL al_evaluate_condition(const char *condition, char *value)
{
   HNDLE hDB, hkey;
   int i, j, idx1, idx2, idx, size, state;
   KEY key;
   double value1, value2;
   char value1_str[256], value2_str[256], str[256], op[3], function[80];
   char data[10000];
   DWORD dtime;

   strcpy(str, condition);
   op[1] = op[2] = 0;
   value1 = value2 = 0;
   idx1 = idx2 = 0;

   /* find value and operator */
   for (i = strlen(str) - 1; i > 0; i--)
      if (strchr("<>=!&", str[i]) != NULL)
         break;
   op[0] = str[i];
   for (j = 1; str[i + j] == ' '; j++);
   strlcpy(value2_str, str + i + j, sizeof(value2_str));
   value2 = atof(value2_str);
   str[i] = 0;

   if (i > 0 && strchr("<>=!&", str[i - 1])) {
      op[1] = op[0];
      op[0] = str[--i];
      str[i] = 0;
   }

   i--;
   while (i > 0 && str[i] == ' ')
      i--;
   str[i + 1] = 0;

   /* check if function */
   function[0] = 0;
   if (str[i] == ')') {
      str[i--] = 0;
      if (strchr(str, '(')) {
         *strchr(str, '(') = 0;
         strcpy(function, str);
         for (i = strlen(str) + 1, j = 0; str[i]; i++, j++)
            str[j] = str[i];
         str[j] = 0;
         i = j - 1;
      }
   }

   /* find key */
   if (str[i] == ']') {
      str[i--] = 0;
      if (str[i] == '*') {
         idx1 = -1;
         while (i > 0 && str[i] != '[')
            i--;
         str[i] = 0;
      } else if (strchr(str, '[') &&
                 strchr(strchr(str, '['), '-')) {
         while (i > 0 && isdigit(str[i]))
            i--;
         idx2 = atoi(str + i + 1);
         while (i > 0 && str[i] != '[')
            i--;
         idx1 = atoi(str + i + 1);
         str[i] = 0;
      } else {
         while (i > 0 && isdigit(str[i]))
            i--;
         idx1 = idx2 = atoi(str + i + 1);
         str[i] = 0;
      }
   }

   cm_get_experiment_database(&hDB, NULL);
   db_find_key(hDB, 0, str, &hkey);
   if (!hkey) {
      cm_msg(MERROR, "al_evaluate_condition", "Cannot find key %s to evaluate alarm condition", str);
      if (value)
         strcpy(value, "unknown");
      return FALSE;
   }
   db_get_key(hDB, hkey, &key);

   if (idx1 < 0) {
      idx1 = 0;
      idx2 = key.num_values-1;
   }

   for (idx=idx1; idx<=idx2 ; idx++) {
      
      if (equal_ustring(function, "access")) {
         /* check key access time */
         db_get_key_time(hDB, hkey, &dtime);
         sprintf(value1_str, "%d", dtime);
         value1 = atof(value1_str);
      } else if (equal_ustring(function, "access_running")) {
         /* check key access time if running */
         db_get_key_time(hDB, hkey, &dtime);
         sprintf(value1_str, "%d", dtime);
         size = sizeof(state);
         db_get_value(hDB, 0, "/Runinfo/State", &state, &size, TID_INT, FALSE);
         if (state != STATE_RUNNING)
            strcpy(value1_str, "0");
         value1 = atof(value1_str);
      } else {
         /* get key data and convert to double */
         db_get_key(hDB, hkey, &key);
         size = sizeof(data);
         db_get_data_index(hDB, hkey, data, &size, idx, key.type);
         db_sprintf(value1_str, data, size, 0, key.type);
         value1 = atof(value1_str);
      }

      /* convert boolean values to integers */
      if (key.type == TID_BOOL) {
         value1 = (value1_str[0] == 'Y' || value1_str[0] == 'y' || value1_str[0] == '1');
         value2 = (value2_str[0] == 'Y' || value2_str[0] == 'y' || value2_str[0] == '1');
      }

      /* return value */
      if (value)
         strcpy(value, value1_str);

      /* now do logical operation */
      if (strcmp(op, "=") == 0)
         if (value1 == value2) return TRUE;
      if (strcmp(op, "==") == 0)
         if (value1 == value2) return TRUE;
      if (strcmp(op, "!=") == 0)
         if (value1 != value2) return TRUE;
      if (strcmp(op, "<") == 0)
         if (value1 < value2) return TRUE;
      if (strcmp(op, ">") == 0)
         if (value1 > value2) return TRUE;
      if (strcmp(op, "<=") == 0)
         if (value1 <= value2) return TRUE;
      if (strcmp(op, ">=") == 0)
         if (value1 >= value2) return TRUE;
      if (strcmp(op, "&") == 0)
         if (((unsigned int)value1 & (unsigned int)value2) > 0) return TRUE;
   }

   return FALSE;
}
Beispiel #20
0
/**
Reset (acknoledge) alarm.

@param alarm_name Alarm name, defined in /alarms/alarms
@return AL_SUCCESS, AL_RESETE, AL_INVALID_NAME
*/
INT al_reset_alarm(const char *alarm_name)
{
   int status, size, i;
   HNDLE hDB, hkeyalarm, hkeyclass, hsubkey;
   KEY key;
   char str[256];
   ALARM a;
   ALARM_CLASS ac;

   cm_get_experiment_database(&hDB, NULL);

   if (alarm_name == NULL) {
      /* reset all alarms */
      db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyalarm);
      if (hkeyalarm) {
         for (i = 0;; i++) {
            db_enum_link(hDB, hkeyalarm, i, &hsubkey);

            if (!hsubkey)
               break;

            db_get_key(hDB, hsubkey, &key);
            al_reset_alarm(key.name);
         }
      }
      return AL_SUCCESS;
   }

   /* find alarm and alarm class */
   sprintf(str, "/Alarms/Alarms/%s", alarm_name);
   db_find_key(hDB, 0, str, &hkeyalarm);
   if (!hkeyalarm) {
      /*cm_msg(MERROR, "al_reset_alarm", "Alarm %s not found in ODB", alarm_name);*/
      return AL_INVALID_NAME;
   }

   size = sizeof(a);
   status = db_get_record(hDB, hkeyalarm, &a, &size, 0);
   if (status != DB_SUCCESS) {
      cm_msg(MERROR, "al_reset_alarm", "Cannot get alarm record");
      return AL_ERROR_ODB;
   }

   sprintf(str, "/Alarms/Classes/%s", a.alarm_class);
   db_find_key(hDB, 0, str, &hkeyclass);
   if (!hkeyclass) {
      cm_msg(MERROR, "al_reset_alarm", "Alarm class %s not found in ODB", a.alarm_class);
      return AL_INVALID_NAME;
   }

   size = sizeof(ac);
   status = db_get_record(hDB, hkeyclass, &ac, &size, 0);
   if (status != DB_SUCCESS) {
      cm_msg(MERROR, "al_reset_alarm", "Cannot get alarm class record");
      return AL_ERROR_ODB;
   }

   if (a.triggered) {
      a.triggered = 0;
      a.time_triggered_first[0] = 0;
      a.time_triggered_last[0] = 0;
      a.checked_last = 0;

      ac.system_message_last = 0;
      ac.execute_last = 0;

      status = db_set_record(hDB, hkeyalarm, &a, sizeof(a), 0);
      if (status != DB_SUCCESS) {
         cm_msg(MERROR, "al_reset_alarm", "Cannot update alarm record");
         return AL_ERROR_ODB;
      }
      status = db_set_record(hDB, hkeyclass, &ac, sizeof(ac), 0);
      if (status != DB_SUCCESS) {
         cm_msg(MERROR, "al_reset_alarm", "Cannot update alarm class record");
         return AL_ERROR_ODB;
      }
      cm_msg(MINFO, "al_reset_alarm", "Alarm \"%s\" reset", alarm_name);
      return AL_RESET;
   }

   return AL_SUCCESS;
}
Beispiel #21
0
int cam_init_rpc(char *host_name, char *exp_name, char *fe_name,
                 char *client_name, char *rpc_server)
{
   INT status, i, size;
   char str[256];
   HNDLE hDB, hKey, hRootKey, hSubkey;

   if (rpc_server[0]) {
      /* connect directly to RPC server, not to MIDAS experiment */
      midas_connect = FALSE;

#ifdef OS_WINNT
      {
         WSADATA WSAData;

         /* Start windows sockets */
         if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
            return RPC_NET_ERROR;
      }
#endif

      rpc_set_name(client_name);
      rpc_register_functions(rpc_get_internal_list(0), NULL);
      rpc_register_functions(rpc_get_internal_list(1), NULL);

      status = rpc_client_connect(rpc_server, 1750, client_name, &hConn);
      if (status != RPC_SUCCESS) {
         printf("Cannot connect to RPC server running on %s at port 1750.\n", rpc_server);
         return status;
      }
   } else {
      /* connect to MIDAS experiment */
      midas_connect = TRUE;

      /* turn off message display, turn on message logging */
      cm_set_msg_print(MT_ALL, 0, NULL);

      /* connect to experiment */
      status = cm_connect_experiment(host_name, exp_name, client_name, 0);
      if (status != CM_SUCCESS)
         return CM_UNDEF_EXP;

      /* connect to the database */
      cm_get_experiment_database(&hDB, &hKey);

      /* find CNAF server if not specified */
      strcpy(str, fe_name);
      if (fe_name[0] == '\0') {
         /* find client which exports CNAF function */
         status = db_find_key(hDB, 0, "System/Clients", &hRootKey);
         if (status == DB_SUCCESS) {
            for (i = 0, status = 0;; i++) {
               status = db_enum_key(hDB, hRootKey, i, &hSubkey);
               if (status == DB_NO_MORE_SUBKEYS) {
                  printf("No client currently exports the CNAF functionality.\n");
                  cm_disconnect_experiment();
                  return CM_UNDEF_EXP;
               }

               sprintf(str, "RPC/%d", RPC_CNAF16);
               status = db_find_key(hDB, hSubkey, str, &hKey);
               if (status == DB_SUCCESS) {
                  size = sizeof(str);
                  db_get_value(hDB, hSubkey, "Name", str, &size, TID_STRING, TRUE);
                  break;
               }
            }
         }
      }

      /* register CNAF_RPC call */
      if (cm_connect_client(str, &hConn) == CM_SUCCESS) {
         DWORD data, size, q, x;

         /* test if CNAF function implemented */
         size = sizeof(WORD);
         status =
             rpc_client_call(hConn, RPC_CNAF16, CNAF_TEST, 0, 0, 0, 0, 0, &data, &size,
                             &q, &x);

         if (status != RPC_SUCCESS) {
            printf("CNAF functionality not implemented by frontend %s\n", fe_name);
            cm_disconnect_client(hConn, FALSE);
            return CM_NO_CLIENT;
         }
      } else {
         printf("Cannot connect to frontend %s\n", fe_name);
         return CM_NO_CLIENT;
      }
   }

   return SUCCESS;
}
Beispiel #22
0
INT multi_init(EQUIPMENT * pequipment)
{
   int status, size, i, j, index, ch_offset;
   char str[256];
   HNDLE hDB, hKey, hNamesIn, hNamesOut;
   MULTI_INFO *m_info;
   BOOL partially_disabled;

   /* allocate private data */
   pequipment->cd_info = calloc(1, sizeof(MULTI_INFO));
   m_info = (MULTI_INFO *) pequipment->cd_info;

   /* get class driver root key */
   cm_get_experiment_database(&hDB, NULL);
   sprintf(str, "/Equipment/%s", pequipment->name);
   db_create_key(hDB, 0, str, TID_KEY);
   db_find_key(hDB, 0, str, &m_info->hKeyRoot);

   /* save event format */
   size = sizeof(str);
   db_get_value(hDB, m_info->hKeyRoot, "Common/Format", str, &size, TID_STRING, TRUE);

   if (equal_ustring(str, "Fixed"))
      m_info->format = FORMAT_FIXED;
   else if (equal_ustring(str, "MIDAS"))
      m_info->format = FORMAT_MIDAS;
   else if (equal_ustring(str, "YBOS"))
      m_info->format = FORMAT_YBOS;

   /* count total number of channels */
   for (i = m_info->num_channels_input = m_info->num_channels_output = 0;
        pequipment->driver[i].name[0]; i++) {
      if (pequipment->driver[i].flags & DF_INPUT)
         m_info->num_channels_input += pequipment->driver[i].channels;
      if (pequipment->driver[i].flags & DF_OUTPUT)
         m_info->num_channels_output += pequipment->driver[i].channels;
   }

   if (m_info->num_channels_input == 0 && m_info->num_channels_output == 0) {
      cm_msg(MERROR, "multi_init", "No channels found in device driver list");
      return FE_ERR_ODB;
   }

   /* Allocate memory for buffers */
   if (m_info->num_channels_input) {
      m_info->names_input = (char *) calloc(m_info->num_channels_input, NAME_LENGTH);
      m_info->var_input = (float *) calloc(m_info->num_channels_input, sizeof(float));
      m_info->update_threshold = (float *) calloc(m_info->num_channels_input, sizeof(float));
      m_info->offset_input = (float *) calloc(m_info->num_channels_input, sizeof(float));
      m_info->factor_input = (float *) calloc(m_info->num_channels_input, sizeof(float));
      m_info->input_mirror = (float *) calloc(m_info->num_channels_input, sizeof(float));
      m_info->channel_offset_input = (INT *) calloc(m_info->num_channels_input, sizeof(INT));
      m_info->driver_input = (void *) calloc(m_info->num_channels_input, sizeof(void *));
   }
   
   if (m_info->num_channels_output) {
      m_info->names_output = (char *) calloc(m_info->num_channels_output, NAME_LENGTH);
      m_info->var_output = (float *) calloc(m_info->num_channels_output, sizeof(float));
      m_info->offset_output = (float *) calloc(m_info->num_channels_output, sizeof(float));
      m_info->factor_output = (float *) calloc(m_info->num_channels_output, sizeof(float));
      m_info->output_mirror = (float *) calloc(m_info->num_channels_output, sizeof(float));
      m_info->channel_offset_output = (INT *) calloc(m_info->num_channels_output, sizeof(DWORD));
      m_info->driver_output = (void *) calloc(m_info->num_channels_output, sizeof(void *));
   }
 
   /*---- Create/Read settings ----*/

   if (m_info->num_channels_input) {
      /* Update threshold */
      for (i = 0; i < m_info->num_channels_input; i++)
         m_info->update_threshold[i] = 0.1f;       /* default 0.1 */
      db_merge_data(hDB, m_info->hKeyRoot, "Settings/Update Threshold",
                    m_info->update_threshold, m_info->num_channels_input * sizeof(float),
                    m_info->num_channels_input, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Settings/Update Threshold", &hKey);
      db_open_record(hDB, hKey, m_info->update_threshold,
                     m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL);

      /* Offset */
      for (i = 0; i < m_info->num_channels_input; i++)
         m_info->offset_input[i] = 0.f;    /* default 0 */
      db_merge_data(hDB, m_info->hKeyRoot, "Settings/Input Offset",
                    m_info->offset_input, m_info->num_channels_input * sizeof(float),
                    m_info->num_channels_input, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Settings/Input Offset", &hKey);
      db_open_record(hDB, hKey, m_info->offset_input,
                     m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL);
   }

   for (i = 0; i < m_info->num_channels_output; i++)
      m_info->offset_output[i] = 0.f;

   if (m_info->num_channels_output) {
      db_merge_data(hDB, m_info->hKeyRoot, "Settings/Output Offset",
                    m_info->offset_output, m_info->num_channels_output * sizeof(float),
                    m_info->num_channels_output, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Settings/Output Offset", &hKey);
      db_open_record(hDB, hKey, m_info->offset_output,
                     m_info->num_channels_output * sizeof(float), MODE_READ, NULL, NULL);
   }

   /* Factor */
   for (i = 0; i < m_info->num_channels_input; i++)
      m_info->factor_input[i] = 1.f;    /* default 1 */

   if (m_info->num_channels_input) {
      db_merge_data(hDB, m_info->hKeyRoot, "Settings/Input Factor",
                    m_info->factor_input, m_info->num_channels_input * sizeof(float),
                    m_info->num_channels_input, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Settings/Input factor", &hKey);
      db_open_record(hDB, hKey, m_info->factor_input,
                     m_info->num_channels_input * sizeof(float), MODE_READ, NULL, NULL);
   }

   if (m_info->num_channels_output) {
      for (i = 0; i < m_info->num_channels_output; i++)
         m_info->factor_output[i] = 1.f;
      db_merge_data(hDB, m_info->hKeyRoot, "Settings/Output Factor",
                    m_info->factor_output, m_info->num_channels_output * sizeof(float),
                    m_info->num_channels_output, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Settings/Output factor", &hKey);
      db_open_record(hDB, hKey, m_info->factor_output,
                     m_info->num_channels_output * sizeof(float), MODE_READ, NULL, NULL);
   }

   /*---- Create/Read variables ----*/

   /* Input */
   if (m_info->num_channels_input) {
      db_merge_data(hDB, m_info->hKeyRoot, "Variables/Input",
                    m_info->var_input, m_info->num_channels_input * sizeof(float),
                    m_info->num_channels_input, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Variables/Input", &m_info->hKeyInput);
      memcpy(m_info->input_mirror, m_info->var_input,
             m_info->num_channels_input * sizeof(float));
   }

   /* Output */
   if (m_info->num_channels_output) {
      db_merge_data(hDB, m_info->hKeyRoot, "Variables/Output",
                    m_info->var_output, m_info->num_channels_output * sizeof(float),
                    m_info->num_channels_output, TID_FLOAT);
      db_find_key(hDB, m_info->hKeyRoot, "Variables/Output", &m_info->hKeyOutput);
   }

   /*---- Initialize device drivers ----*/

   /* call init method */
   partially_disabled = FALSE;
   for (i = 0; pequipment->driver[i].name[0]; i++) {
      sprintf(str, "Settings/Devices/%s", pequipment->driver[i].name);
      status = db_find_key(hDB, m_info->hKeyRoot, str, &hKey);
      if (status != DB_SUCCESS) {
         db_create_key(hDB, m_info->hKeyRoot, str, TID_KEY);
         status = db_find_key(hDB, m_info->hKeyRoot, str, &hKey);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "multi_init", "Cannot create %s entry in online database",
                   str);
            free_mem(m_info);
            return FE_ERR_ODB;
         }
      }

      /* check enabled flag */
      size = sizeof(pequipment->driver[i].enabled);
      pequipment->driver[i].enabled = 1;
      sprintf(str, "Settings/Devices/%s/Enabled", pequipment->driver[i].name);
      status = db_get_value(hDB, m_info->hKeyRoot, str, &pequipment->driver[i].enabled, &size, TID_BOOL, TRUE);
      if (status != DB_SUCCESS)
         return FE_ERR_ODB;

      if (pequipment->driver[i].enabled) {
         status = device_driver(&pequipment->driver[i], CMD_INIT, hKey);
         if (status != FE_SUCCESS) {
            free_mem(m_info);
            return status;
         }
      } else
         partially_disabled = TRUE;
   }

   /* compose device driver channel assignment */
   for (i = 0, j = 0, index = 0, ch_offset = 0; i < m_info->num_channels_input; i++, j++) {
      while (pequipment->driver[index].name[0] &&
             (j >= pequipment->driver[index].channels ||
              (pequipment->driver[index].flags & DF_INPUT) == 0)) {
         ch_offset += j;
         index++;
         j = 0;
      }

      m_info->driver_input[i] = &pequipment->driver[index];
      m_info->channel_offset_input[i] = ch_offset;
   }

   for (i = 0, j = 0, index = 0, ch_offset = 0; i < m_info->num_channels_output; i++, j++) {
      while (pequipment->driver[index].name[0] &&
             (j >= pequipment->driver[index].channels ||
              (pequipment->driver[index].flags & DF_OUTPUT) == 0)) {
         ch_offset += j;
         index++;
         j = 0;
      }

      m_info->driver_output[i] = &pequipment->driver[index];
      m_info->channel_offset_output[i] = ch_offset;
   }

   /*---- get default names from device driver ----*/
   if (m_info->num_channels_input) {
      for (i = 0; i < m_info->num_channels_input; i++) {
         sprintf(m_info->names_input + NAME_LENGTH * i, "Input Channel %d", i);
         device_driver(m_info->driver_input[i], CMD_GET_LABEL,
                       i - m_info->channel_offset_input[i], 
                       m_info->names_input + NAME_LENGTH * i);

         /* merge existing names with labels from driver */
         status = db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hKey);
         if (status != DB_SUCCESS) {
            db_create_key(hDB, m_info->hKeyRoot, "Settings/Names Input", TID_STRING);
            db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hKey);
            db_set_data(hDB, hKey, m_info->names_input, NAME_LENGTH, 1, TID_STRING);
         } else {
            size = sizeof(str);
            db_get_data_index(hDB, hKey, str, &size, i, TID_STRING);
            if (!str[0])
               db_set_data_index(hDB, hKey, m_info->names_input+NAME_LENGTH*i, NAME_LENGTH, i, TID_STRING);
         }
      }
   }

   if (m_info->num_channels_output) {
      for (i = 0; i < m_info->num_channels_output; i++) {
         sprintf(m_info->names_output + NAME_LENGTH * i, "Output Channel %d", i);
         device_driver(m_info->driver_output[i], CMD_GET_LABEL, 
                       i - m_info->channel_offset_output[i], 
                       m_info->names_output + NAME_LENGTH * i);

         /* merge existing names with labels from driver */
         status = db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hKey);
         if (status != DB_SUCCESS) {
            db_create_key(hDB, m_info->hKeyRoot, "Settings/Names Output", TID_STRING);
            db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hKey);
            db_set_data(hDB, hKey, m_info->names_input, NAME_LENGTH, 1, TID_STRING);
         } else {
            size = sizeof(str);
            db_get_data_index(hDB, hKey, str, &size, i, TID_STRING);
            if (!str[0])
               db_set_data_index(hDB, hKey, m_info->names_input+NAME_LENGTH*i, NAME_LENGTH, i, TID_STRING);
         }

      }
   }

   /*---- set labels from midas SC names ----*/
   if (m_info->num_channels_input) {
      for (i = 0; i < m_info->num_channels_input; i++) {
         device_driver(m_info->driver_input[i], CMD_SET_LABEL,
                       i - m_info->channel_offset_input[i],
                       m_info->names_input + NAME_LENGTH * i);
      }

      /* open hotlink on input channel names */
      if (db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Input", &hNamesIn) ==
          DB_SUCCESS)
         db_open_record(hDB, hNamesIn, m_info->names_input,
                        NAME_LENGTH * m_info->num_channels_input, MODE_READ,
                        multi_update_label, pequipment);
   }

   for (i = 0; i < m_info->num_channels_output; i++) {
      device_driver(m_info->driver_output[i], CMD_SET_LABEL,
                    i - m_info->channel_offset_output[i],
                    m_info->names_output + NAME_LENGTH * i);
   }

   /* open hotlink on output channel names */
   if (m_info->num_channels_output) {
      if (db_find_key(hDB, m_info->hKeyRoot, "Settings/Names Output", &hNamesOut) ==
          DB_SUCCESS)
         db_open_record(hDB, hNamesOut, m_info->names_output,
                        NAME_LENGTH * m_info->num_channels_output, MODE_READ,
                        multi_update_label, pequipment);

      /* open hot link to output record */
      db_open_record(hDB, m_info->hKeyOutput, m_info->var_output,
                     m_info->num_channels_output * sizeof(float),
                     MODE_READ, multi_output, pequipment);
   }

   /* set initial demand values */
   for (i = 0; i < m_info->num_channels_output; i++) {
      if (pequipment->driver[index].flags & DF_PRIO_DEVICE) {
         /* read default value directly from device bypassing multi-thread buffer */
         device_driver(m_info->driver_output[i], CMD_GET_DEMAND,
                       i - m_info->channel_offset_output[i],
                       &m_info->output_mirror[i]);
      } else {
         /* use default value from ODB */
         m_info->output_mirror[i] = m_info->var_output[i] * m_info->factor_output[i] -
             m_info->offset_output[i];

         device_driver(m_info->driver_output[i], CMD_SET,
                       i - m_info->channel_offset_output[i],
                       m_info->output_mirror[i]);
      }
   }

   if (m_info->num_channels_output)
      db_set_record(hDB, m_info->hKeyOutput, m_info->output_mirror,
                    m_info->num_channels_output * sizeof(float), 0);

   /* initially read all input channels */
   if (m_info->num_channels_input)
      multi_read(pequipment, -1);

   if (partially_disabled)
      return FE_PARTIALLY_DISABLED;
   
   return FE_SUCCESS;
}
Beispiel #23
0
INT frontend_init() {

  INT   status;
  BOOL  default_justfilled = FALSE; // Assuming the worst
  DWORD default_lastfilled = 0;     // Assuming the worst
  DWORD default_timelimit  = 28000; // 8 hours between fills
  // Get handles to database keys if they exist,
  // and create otherwise with default worst-case-scenario values
  status = cm_get_experiment_database(&hDB, NULL);
  if (status != CM_SUCCESS) {
    printf("Warning: Could not connect to ODB database!\n");
    return FE_ERR_HW;
  }

  status = db_find_key(hDB, 0, sLastFilled, &kLastFilled);
  if (status == DB_NO_KEY) {
    db_create_key(hDB, 0, sLastFilled, TID_DWORD);
    db_find_key(hDB, 0, sLastFilled, &kLastFilled);
    status = db_set_value(hDB, 0, sLastFilled, &default_lastfilled, sizeof(default_lastfilled), 1, TID_DWORD);
  }
  if (status != DB_SUCCESS) {
    printf("Warning: Could not access key %s!\n", sLastFilled);
    return FE_ERR_HW;
  }

  status = db_find_key(hDB, 0, sJustFilled, &kJustFilled);
  if (status == DB_NO_KEY) {
    db_create_key(hDB, 0, sJustFilled, TID_BOOL);
    db_find_key(hDB, 0, sJustFilled, &kJustFilled);
    status = db_set_value(hDB, 0, sJustFilled, &default_justfilled, sizeof(default_justfilled), 1, TID_BOOL);
  }
  if (status != DB_SUCCESS) {
    printf("Warning: Could not access key %s!\n", sJustFilled);
    return FE_ERR_HW;
  }

  status = db_find_key(hDB, 0, sTimeLimit, &kTimeLimit);
  if (status == DB_NO_KEY) {
    db_create_key(hDB, 0, sTimeLimit, TID_DWORD);
    db_find_key(hDB, 0, sTimeLimit, &kTimeLimit);
    status = db_set_value(hDB, 0, sTimeLimit, &default_timelimit, sizeof(default_timelimit), 1, TID_DWORD);
  }
  if (status != DB_SUCCESS) {
    printf("Warning: Could not access key %s!\n", sTimeLimit);
    return FE_ERR_HW;
  }

  /*
  // Check if alarm exists and, if not,create it
  // Copied from midas.c which should take care of this, but doesn't
  char str[256];
  sprintf(str, "/Alarms/Alarms/%s", sAlarmName);
  db_find_key(hDB, 0, str, &kAlarm);
  if (!kAlarm) {
  ALARM_ODB_STR(alarm_odb_str); // The initial "run number too large" settings for a default alarm
  status = db_create_record(hDB, 0, str, strcomb(alarm_odb_str));
    db_find_key(hDB, 0, str, &kAlarm);
    if (!kAlarm) {
      cm_msg(MERROR, "ge_ln2_init", "Cannot create alarm record");
      return FE_ERR_HW;
    }
    BOOL al_active    = TRUE;                        // Alarm should be on
    INT  al_type      = AT_EVALUATED;                // Alarm type is evaluated; it looks for a certain ODB value to be too large
    char al_cond[256] = "";                          // Not a "conditional" alarm in the MIDAS sense
    char al_class[32] = "Alarm";                     // When triggered, the alarm will have black letters on red background
    char al_msg[80]   = "Germanium must be filled!"; // Message on banner
    db_set_value(hDB, kAlarm, "Active",        &al_active, sizeof(al_active), 1, TID_BOOL);
    db_set_value(hDB, kAlarm, "Type",          &al_type,   sizeof(al_type),   1, TID_INT);
    db_set_value(hDB, kAlarm, "Condition",      al_cond,   sizeof(al_cond),   1, TID_STRING);
    db_set_value(hDB, kAlarm, "Alarm Class",    al_class,  sizeof(al_class),  1, TID_STRING);
    db_set_value(hDB, kAlarm, "Alarm Message",  al_msg,    sizeof(al_msg),    1, TID_STRING);
    
  }
  */
  return CM_SUCCESS;
}
Beispiel #24
0
INT amc13simulator_ODB_init(void)
{

  INT ret = SUCCESS;
  INT status;
  char str[MAX_ODB_PATH];
  char str_aux[MAX_ODB_PATH];
  HNDLE hKey;
  
  /* reset channel info information */

  int i;
  amc13simulator_settings_odb.sync = 0;
  for (i=0; i<AMC13SIMULATOR_NUM; i++){
      amc13simulator_channel_odb[i].enabled = 0;
      amc13simulator_channel_odb[i].port_no = 0;
      sprintf(amc13simulator_channel_odb[i].ip_addr,"000.000.0.000");
      sprintf(amc13simulator_channel_odb[i].host_name,"");
  }                                                                                                           
  dbprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);

  sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);

  // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist
  status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
      return FE_ERR_ODB;
    }
  
  // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);		  
      return FE_ERR_ODB;
    }
  
  // creates hotlink of ODB subtree to structure amc13simulator_settings_odb that's automatically updated
  if (db_open_record(hDB, hKey, &amc13simulator_settings_odb, sizeof(AMC13SIMULATOR_SETTINGS_ODB), MODE_READ, 
		     amc13simulator_ODB_update, NULL) != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str);
      return FE_ERR_ODB;
    }

  dbprintf("%s(%d): %s sync %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.sync );

  for (i=0; i<AMC13SIMULATOR_NUM; i++){

      sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1);

      // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist                              
      status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE);
      if ( status != DB_SUCCESS ) 
        {
          cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
          return FE_ERR_ODB;
        }
      // returns key handle "hDB" to ODB name "str" for fast access 
      status = db_find_key(hDB, 0, str, &hKey);
      if ( status != DB_SUCCESS )
        {
          cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);
          return FE_ERR_ODB;
        }
      // creates hotlink of ODB subtree to structure amc13simulator_channel_odb[i] that's automatically update   
      if (db_open_record(hDB, hKey, &amc13simulator_channel_odb[i], sizeof(AMC13SIMULATOR_CHANNEL_ODB),
                         MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS)
        {                               
          cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str);
          return FE_ERR_ODB;
        }                                                                                                     

      dbprintf("%s(%d): %s enabled %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled ); 
      dbprintf("%s(%d): %s port no %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no );
      dbprintf("%s(%d): %s ip addr %s\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr );
    }

  return ret;

}
Beispiel #25
0
INT amc13simulator_ODB_set()
{
  INT   status;
  HNDLE hKey;
  char  str[MAX_ODB_PATH];
  char  str_aux[MAX_ODB_PATH];
  int   i;

  sprintf(str_aux,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);
  sprintf(str, str_aux, frontend_index);

  // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist
  status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE);
  if (status != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
      ss_sleep(3000);
    }
  
  // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);		  
      return FE_ERR_ODB;
    }
  
  // Copy a set of keys from local memory to the database
  db_set_record(hDB, hKey, &amc13simulator_settings_odb, sizeof(AMC13SIMULATOR_SETTINGS_ODB), 0);
  
  dbprintf("%s(%d): %s sync %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.sync );

  for (i=0; i<AMC13SIMULATOR_NUM; i++)
    {
      sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1);
 
      // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist
      status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE);
      if (status != DB_SUCCESS) 
        {
          cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
          ss_sleep(3000);
        }

      // returns key handle "hDB" to ODB name "str" for fast access
      status = db_find_key(hDB, 0, str, &hKey);
      if ( status != DB_SUCCESS )
        {
          cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);
          return FE_ERR_ODB; 
        }
 
      // Copy a set of keys from local memory to the database 
      db_set_record(hDB, hKey, &amc13simulator_channel_odb[i], sizeof(AMC13SIMULATOR_CHANNEL_ODB), 0);

      dbprintf("%s(%d): %s enabled %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled );
      dbprintf("%s(%d): %s port no %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no );
      dbprintf("%s(%d): %s ip addr %s\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr );
      
    }
  
  return SUCCESS;

}
Beispiel #26
0
INT amc13simulator_ODB_init(void)
{

  INT ret = SUCCESS;
  INT status;
  char str[MAX_ODB_PATH];
  char str_aux[MAX_ODB_PATH];
  HNDLE hKey;
  
  /* reset channel info information */

  int i;
  amc13simulator_settings_odb.sync = 1;
  amc13simulator_settings_odb.write_root = 1;
  amc13simulator_settings_odb.rider_header = 0;
  amc13simulator_settings_odb.n_seg_x = 9;
  amc13simulator_settings_odb.n_seg_y = 6;
  amc13simulator_settings_odb.seg_size = 3;
  amc13simulator_settings_odb.waveform_length = 589824;
  amc13simulator_settings_odb.n_muons_mean = 400;
  amc13simulator_settings_odb.Emax = 52.8;
  amc13simulator_settings_odb.Elab_max = 3.1;
  amc13simulator_settings_odb.omega_a = 1.438e6;
  amc13simulator_settings_odb.repeat_first_event = 1;
  amc13simulator_settings_odb.laser_pulse = 0;
  amc13simulator_settings_odb.send_to_event_builder = 0;
/*
  for (i=0; i<AMC13SIMULATOR_NUM; i++){
      amc13simulator_channel_odb[i].enabled = 0;
      amc13simulator_channel_odb[i].port_no = 0;
      sprintf(amc13simulator_channel_odb[i].ip_addr,"000.000.0.000");
      sprintf(amc13simulator_channel_odb[i].host_name,"");
      } */                                                                                                          
  dbprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);

  sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);

  // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist
  status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
      return FE_ERR_ODB;
    }
  
  // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);		  
      return FE_ERR_ODB;
    }
  
  // creates hotlink of ODB subtree to structure amc13simulator_settings_odb that's automatically updated
  if (db_open_record(hDB, hKey, &amc13simulator_settings_odb, sizeof(AMC13SIMULATOR_SETTINGS_ODB), MODE_READ, 
		     amc13simulator_ODB_update, NULL) != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str);
      return FE_ERR_ODB;
    }

  
  dbprintf("%s(%d): %s sync %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.sync );
  dbprintf("%s(%d): %s write_root %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.write_root );
  dbprintf("%s(%d): %s rider_header %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.rider_header );
  dbprintf("%s(%d): %s n_seg_x %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_x );
  dbprintf("%s(%d): %s n_seg_y %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_y );
  dbprintf("%s(%d): %s seg_size %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.seg_size );
  dbprintf("%s(%d): %s waveform length %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.waveform_length );
  dbprintf("%s(%d): %s n_muons_mean %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_muons_mean );
  dbprintf("%s(%d): %s Emax %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.Emax );
  dbprintf("%s(%d): %s Elab_max %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.Elab_max );
  dbprintf("%s(%d): %s omega_a %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.omega_a );
  dbprintf("%s(%d): %s repeat_first_event %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.repeat_first_event);
  dbprintf("%s(%d): %s laser_pulse %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.laser_pulse);


  /*
  for (i=0; i<AMC13SIMULATOR_NUM; i++){

      sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1);

      // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist                              
      status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE);
      if ( status != DB_SUCCESS ) 
        {
          cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
          return FE_ERR_ODB;
        }
      // returns key handle "hDB" to ODB name "str" for fast access 
      status = db_find_key(hDB, 0, str, &hKey);
      if ( status != DB_SUCCESS )
        {
          cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);
          return FE_ERR_ODB;
        }
      // creates hotlink of ODB subtree to structure amc13simulator_channel_odb[i] that's automatically update   
      if (db_open_record(hDB, hKey, &amc13simulator_channel_odb[i], sizeof(AMC13SIMULATOR_CHANNEL_ODB),
                         MODE_READ, amc13simulator_ODB_update, NULL) != DB_SUCCESS)
        {                               
          cm_msg(MERROR, __FILE__, "Cannot open [%s] settings in ODB",str);
          return FE_ERR_ODB;
        }                                                                                                     

      dbprintf("%s(%d): %s enabled %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled ); 
      dbprintf("%s(%d): %s port no %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no );
      dbprintf("%s(%d): %s ip addr %s\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr );
    }
  */
  return ret;

}
Beispiel #27
0
INT register_equipment(void)
{
   INT index, size, status;
   char str[256];
   EQUIPMENT_INFO *eq_info;
   EQUIPMENT_STATS *eq_stats;
   HNDLE hKey;

   /* get current ODB run state */
   size = sizeof(run_state);
   run_state = STATE_STOPPED;
   db_get_value(hDB, 0, "/Runinfo/State", &run_state, &size, TID_INT, TRUE);
   size = sizeof(run_number);
   run_number = 1;
   status = db_get_value(hDB, 0, "/Runinfo/Run number", &run_number, &size, TID_INT, TRUE);
   assert(status == SUCCESS);

   /* scan EQUIPMENT table from mevb.C */
   for (index = 0; equipment[index].name[0]; index++) {
      eq_info = &equipment[index].info;
      eq_stats = &equipment[index].stats;

      if (eq_info->event_id == 0) {
         printf("\nEvent ID 0 for %s not allowed\n", equipment[index].name);
         cm_disconnect_experiment();
         ss_sleep(5000);
         exit(0);
      }

      /* init status */
      equipment[index].status = EB_SUCCESS;

      sprintf(str, "/Equipment/%s/Common", equipment[index].name);

      /* get last event limit from ODB */
      if (eq_info->eq_type != EQ_SLOW) {
         db_find_key(hDB, 0, str, &hKey);
         size = sizeof(double);
         if (hKey)
            db_get_value(hDB, hKey, "Event limit", &eq_info->event_limit, &size, TID_DOUBLE, TRUE);
      }

      /* Create common subtree */
      status = db_check_record(hDB, 0, str, EQUIPMENT_COMMON_STR, TRUE);
      if (status != DB_SUCCESS) {
         printf("Cannot check equipment record, status = %d\n", status);
         ss_sleep(3000);
      }
      db_find_key(hDB, 0, str, &hKey);

      if (equal_ustring(eq_info->format, "FIXED"))
         equipment[index].format = FORMAT_FIXED;
      else                      /* default format is MIDAS */
         equipment[index].format = FORMAT_MIDAS;

      gethostname(eq_info->frontend_host, sizeof(eq_info->frontend_host));
      strcpy(eq_info->frontend_name, frontend_name);
      strcpy(eq_info->frontend_file_name, frontend_file_name);

      /* set record from equipment[] table in frontend.c */
      db_set_record(hDB, hKey, eq_info, sizeof(EQUIPMENT_INFO), 0);

      /* get record once at the start equipment info */
      size = sizeof(EQUIPMENT_INFO);
      db_get_record(hDB, hKey, eq_info, &size, 0);

    /*---- Create just the key , leave it empty ---------------------------------*/
      sprintf(str, "/Equipment/%s/Variables", equipment[index].name);
      db_create_key(hDB, 0, str, TID_KEY);
      db_find_key(hDB, 0, str, &hKey);
      equipment[index].hkey_variables = hKey;

    /*---- Create and initialize statistics tree -------------------*/
      sprintf(str, "/Equipment/%s/Statistics", equipment[index].name);

      status = db_check_record(hDB, 0, str, EQUIPMENT_STATISTICS_STR, TRUE);
      if (status != DB_SUCCESS) {
         printf("Cannot create/check statistics record, error %d\n", status);
         ss_sleep(3000);
      }

      status = db_find_key(hDB, 0, str, &hKey);
      if (status != DB_SUCCESS) {
         printf("Cannot find statistics record, error %d\n", status);
         ss_sleep(3000);
      }

      eq_stats->events_sent = 0;
      eq_stats->events_per_sec = 0;
      eq_stats->kbytes_per_sec = 0;

      /* open hot link to statistics tree */
      status = db_open_record(hDB, hKey, eq_stats, sizeof(EQUIPMENT_STATS)
                              , MODE_WRITE, NULL, NULL);
      if (status != DB_SUCCESS) {
         cm_msg(MERROR, "register_equipment",
                "Cannot open statistics record, error %d. Probably other FE is using it", status);
         ss_sleep(3000);
      }

    /*---- open event buffer ---------------------------------------*/
      if (eq_info->buffer[0]) {
         status = bm_open_buffer(eq_info->buffer, 2 * max_event_size, &equipment[index].buffer_handle);
         if (status != BM_SUCCESS && status != BM_CREATED) {
            cm_msg(MERROR, "register_equipment",
                   "Cannot open event buffer. Try to reduce EVENT_BUFFER_SIZE in midas.h \
          and rebuild the system.");
            return 0;
         }

	 if (1)
	   {
	     int level = 0;
	     bm_get_buffer_level(equipment[index].buffer_handle, &level);
	     printf("Buffer %s, level %d, info: \n", eq_info->buffer, level);
	   }

         /* set the default buffer cache size */
         bm_set_cache_size(equipment[index].buffer_handle, 0, SERVER_CACHE_SIZE);
      } else {
Beispiel #28
0
INT amc13simulator_ODB_get()
{
  INT   status;
  HNDLE hKey;
  char  str[MAX_ODB_PATH];
  char  str_aux[MAX_ODB_PATH];
  int   size;

  sprintf(str_aux,"/Equipment/AMC13Simulator%02d/Settings/Globals",frontend_index);
  sprintf(str, str_aux, frontend_index);

  // create ODB structure /Equipment/%s/Settings/Globals if doesn't exist
  status = db_check_record(hDB, 0, str, AMC13SIMULATOR_SETTINGS_ODB_STR, TRUE);
  if (status != DB_SUCCESS) 
    {
      cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
      ss_sleep(3000);
    }
  
      // returns key handle "hDB" to ODB name "str" for fast access 
  status = db_find_key(hDB, 0, str, &hKey);
  if ( status != DB_SUCCESS ) 
    {
      cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);		  
      return FE_ERR_ODB;
    }
  
  // Copy a set of keys from local memory to the database
  size = sizeof(AMC13SIMULATOR_SETTINGS_ODB);
  db_get_record(hDB, hKey, &amc13simulator_settings_odb, &size, 0);
  
  
  dbprintf("%s(%d): %s sync %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.sync );
  dbprintf("%s(%d): %s write_root %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.write_root );
  dbprintf("%s(%d): %s rider_header %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.rider_header );
  dbprintf("%s(%d): %s n_seg_x %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_x );
  dbprintf("%s(%d): %s n_seg_y %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_seg_y );
  dbprintf("%s(%d): %s seg_size %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.seg_size );
  dbprintf("%s(%d): %s waveform length %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.waveform_length );
  dbprintf("%s(%d): %s n_muons_mean %d\n",  __func__, __LINE__, str, amc13simulator_settings_odb.n_muons_mean );
  dbprintf("%s(%d): %s Emax %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.Emax );
  dbprintf("%s(%d): %s Elab_max %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.Elab_max );
  dbprintf("%s(%d): %s omega_a %f\n",  __func__, __LINE__, str, amc13simulator_settings_odb.omega_a );
  dbprintf("%s(%d): %s repeat_first_event %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.repeat_first_event);
  dbprintf("%s(%d): %s laser_pulse %d\n",__func__,__LINE__,str,amc13simulator_settings_odb.laser_pulse);

  /*
  int   i;                                                                                                     
  for (i=0; i<AMC13SIMULATOR_NUM; i++)
    {
      sprintf(str,"/Equipment/AMC13Simulator%02d/Settings/Channel%02d",frontend_index,i+1);

      // create ODB structure /Equipment/%s/Settings/Channel%02d if doesn't exist 
      status = db_check_record(hDB, 0, str, AMC13SIMULATOR_CHANNEL_ODB_STR, TRUE);
      if (status != DB_SUCCESS)
        {
          cm_msg(MERROR, __FILE__, "Cannot create [%s] entry in ODB, err = %i",str,status);
          ss_sleep(3000);
        }
 
      // returns key handle "hDB" to ODB name "str" for fast access
      status = db_find_key(hDB, 0, str, &hKey); 
      if ( status != DB_SUCCESS )
	{
          cm_msg(MERROR, __FILE__, "Cannot find [%s] key in ODB, err = %i",str,status);
          return FE_ERR_ODB;
        }

      // Copy a set of keys from local memory to the database                                                  
      size = sizeof(AMC13SIMULATOR_CHANNEL_ODB);
      db_get_record(hDB, hKey, &amc13simulator_channel_odb[i], &size, 0);
 
      dbprintf("%s(%d): %s enabled %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].enabled );
      dbprintf("%s(%d): %s port no %d\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].port_no );
      dbprintf("%s(%d): %s ip addr %s\n",  __func__, __LINE__, str, amc13simulator_channel_odb[i].ip_addr );
    }
  */
  return SUCCESS;

}
Beispiel #29
0
/**
Scan ODB for alarms.
@return AL_SUCCESS
*/
INT al_check()
{
   if (rpc_is_remote())
      return rpc_call(RPC_AL_CHECK);

#ifdef LOCAL_ROUTINES
   {
   INT i, status, size, semaphore;
   HNDLE hDB, hkeyroot, hkey;
   KEY key;
   ALARM a;
   char str[256], value[256];
   time_t now;
   PROGRAM_INFO program_info;
   BOOL flag;
   
   ALARM_CLASS_STR(alarm_class_str);
   ALARM_ODB_STR(alarm_odb_str);
   ALARM_PERIODIC_STR(alarm_periodic_str);
   
   cm_get_experiment_database(&hDB, NULL);
   
   if (hDB == 0)
      return AL_SUCCESS;     /* called from server not yet connected */
   
   /* check online mode */
   flag = TRUE;
   size = sizeof(flag);
   db_get_value(hDB, 0, "/Runinfo/Online Mode", &flag, &size, TID_INT, TRUE);
   if (!flag)
      return AL_SUCCESS;
   
   /* check global alarm flag */
   flag = TRUE;
   size = sizeof(flag);
   db_get_value(hDB, 0, "/Alarms/Alarm system active", &flag, &size, TID_BOOL, TRUE);
   if (!flag)
      return AL_SUCCESS;
   
   /* request semaphore */
   cm_get_experiment_semaphore(&semaphore, NULL, NULL, NULL);
   status = ss_semaphore_wait_for(semaphore, 100);
   if (status == SS_TIMEOUT)
      return AL_SUCCESS;        /* someone else is doing alarm business */
   if (status != SS_SUCCESS) {
      printf("al_check: Something is wrong with our semaphore, ss_semaphore_wait_for() returned %d, aborting.\n", status);
      //abort(); // DOES NOT RETURN
      printf("al_check: Cannot abort - this will lock you out of odb. From this point, MIDAS will not work correctly. Please read the discussion at https://midas.triumf.ca/elog/Midas/945\n");
      // NOT REACHED
      return AL_SUCCESS;
   }
   
   /* check ODB alarms */
   db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
   if (!hkeyroot) {
      /* create default ODB alarm */
      status = db_create_record(hDB, 0, "/Alarms/Alarms/Demo ODB", strcomb(alarm_odb_str));
      db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
      if (!hkeyroot) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
      
      status = db_create_record(hDB, 0, "/Alarms/Alarms/Demo periodic", strcomb(alarm_periodic_str));
      db_find_key(hDB, 0, "/Alarms/Alarms", &hkeyroot);
      if (!hkeyroot) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
      
      /* create default alarm classes */
      status = db_create_record(hDB, 0, "/Alarms/Classes/Alarm", strcomb(alarm_class_str));
      status = db_create_record(hDB, 0, "/Alarms/Classes/Warning", strcomb(alarm_class_str));
      if (status != DB_SUCCESS) {
         ss_semaphore_release(semaphore);
         return AL_SUCCESS;
      }
   }
   
   for (i = 0;; i++) {
      status = db_enum_key(hDB, hkeyroot, i, &hkey);
      if (status == DB_NO_MORE_SUBKEYS)
         break;
      
      db_get_key(hDB, hkey, &key);
      
      size = sizeof(a);
      status = db_get_record(hDB, hkey, &a, &size, 0);
      if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
         /* make sure alarm record has right structure */
         db_check_record(hDB, hkey, "", strcomb(alarm_odb_str), TRUE);
         size = sizeof(a);
         status = db_get_record(hDB, hkey, &a, &size, 0);
         if (status != DB_SUCCESS || a.type < 1 || a.type > AT_LAST) {
            cm_msg(MERROR, "al_check", "Cannot get alarm record");
            continue;
         }
      }
      
      /* check periodic alarm only when active */
      if (a.active &&
          a.type == AT_PERIODIC &&
          a.check_interval > 0 && (INT) ss_time() - (INT) a.checked_last > a.check_interval) {
         /* if checked_last has not been set, set it to current time */
         if (a.checked_last == 0) {
            a.checked_last = ss_time();
            db_set_record(hDB, hkey, &a, size, 0);
         } else
            al_trigger_alarm(key.name, a.alarm_message, a.alarm_class, "", AT_PERIODIC);
      }
      
      /* check alarm only when active and not internal */
      if (a.active &&
          a.type == AT_EVALUATED &&
          a.check_interval > 0 && (INT) ss_time() - (INT) a.checked_last > a.check_interval) {
         /* if condition is true, trigger alarm */
         if (al_evaluate_condition(a.condition, value)) {
            sprintf(str, a.alarm_message, value);
            al_trigger_alarm(key.name, str, a.alarm_class, "", AT_EVALUATED);
         } else {
            a.checked_last = ss_time();
            status = db_set_value(hDB, hkey, "Checked last", &a.checked_last, sizeof(DWORD), 1, TID_DWORD);
            if (status != DB_SUCCESS) {
               cm_msg(MERROR, "al_check", "Cannot change alarm record");
               continue;
            }
         }
      }
   }
   
   /* check /programs alarms */
   db_find_key(hDB, 0, "/Programs", &hkeyroot);
   if (hkeyroot) {
      for (i = 0;; i++) {
         status = db_enum_key(hDB, hkeyroot, i, &hkey);
         if (status == DB_NO_MORE_SUBKEYS)
            break;
         
         db_get_key(hDB, hkey, &key);
         
         /* don't check "execute on xxx" */
         if (key.type != TID_KEY)
            continue;
         
         size = sizeof(program_info);
         status = db_get_record(hDB, hkey, &program_info, &size, 0);
         if (status != DB_SUCCESS) {
            cm_msg(MERROR, "al_check", "Cannot get program info record");
            continue;
         }
         
         now = ss_time();
         
         rpc_get_name(str);
         str[strlen(key.name)] = 0;
         if (!equal_ustring(str, key.name) && cm_exist(key.name, FALSE) == CM_NO_CLIENT) {
            if (program_info.first_failed == 0) {
               program_info.first_failed = (DWORD) now;
               db_set_record(hDB, hkey, &program_info, sizeof(program_info), 0);
            }
            
            /* fire alarm when not running for more than what specified in check interval */
            if (now - program_info.first_failed >= program_info.check_interval / 1000) {
               /* if not running and alarm calss defined, trigger alarm */
               if (program_info.alarm_class[0]) {
                  sprintf(str, "Program %s is not running", key.name);
                  al_trigger_alarm(key.name, str, program_info.alarm_class,
                                   "Program not running", AT_PROGRAM);
               }
               
               /* auto restart program */
               if (program_info.auto_restart && program_info.start_command[0]) {
                  ss_system(program_info.start_command);
                  program_info.first_failed = 0;
                  cm_msg(MTALK, "al_check", "Program %s restarted", key.name);
               }
            }
         } else {
            if (program_info.first_failed != 0) {
               program_info.first_failed = 0;
               db_set_record(hDB, hkey, &program_info, sizeof(program_info), 0);
            }
         }
      }
   }
   
   ss_semaphore_release(semaphore);
   }
#endif                          /* LOCAL_COUTINES */

   return SUCCESS;
}