next up previous contents
Next: The device create Up: Methods Previous: The device destroy

The class initialise method

All device classes must have at least the class_initialise method. This method is called once by the method_finder the first time a device of this class (or a device belonging to a subclass of this class) is created. This is taken care of by the method_finder. It is used to initialise class specific data which are required by that class. Amongst other things it should initialise the class structure and the default object (cf. above). It can also be used to do things required by the class like forking other processes. The class_initialise method of a class should not rely on the class_initialise method of any of its superclasses. Class_initialise methods are called from bottom to top i.e. class_initialise of the DevServerClass is called last.

The class initialise method must have following calling syntax -

static long class_initialise(long *error)

Here is an example class initialise method (for the AGPowerSupplyClass) -

/*======================================================================
 Function:      static long class_initialise()

 Description:   Initialise the AGPowerSupplyClass, is called once for
                this class per process. class_initialise() will initialise
                the class structure (aGPowerSupplyClass) and the default 
                powersupply device (aGPowerSupply).

 Arg(s) In:     none

 Arg(s) Out:    long *error - pointer to error code if routine fails.
 =======================================================================*/

static long class_initialise(error)
long *error;
{
   AGPowerSupply ps;
   int state;
  
/*
 * AGPowerSupplyClass is a subclass of PowerSupplyClass
 */

   aGPowerSupplyClass->devserver_class.superclass = (DevServerClass)powerSupplyClass;
   aGPowerSupplyClass->devserver_class.class_name = (char*)malloc(sizeof("AGPowerSupplyClass"));
   sprintf(aGPowerSupplyClass->devserver_class.class_name,"AGPowerSupplyClass");

/*
 * commands implemented for the AG PowerSUpply class
 */

   aGPowerSupplyClass->devserver_class.n_commands = n_commands;
   aGPowerSupplyClass->devserver_class.commands_list = commands_list;

   aGPowerSupplyClass->devserver_class.class_inited = 1;
/*
 * initialise the template powersupply so that DevMethodCreate has
 * default values for creating a powersupply, these values will be
 * overridden by the static database (if defined there). 
 */

   aGPowerSupply->devserver.class_pointer = (DevServerClass)aGPowerSupplyClass;
/*
 * default is to start with powersupply switched OFF; the state 
 * variable gets (ab)used during initialisation to interpret the
 * initial state of the powersupply: 0==DEVOFF, 1==DEVON. this is
 * because the database doesn't support the normal state variables
 * like DEVON, DEVSTANDBY, DEVINSERTED, etc.
 */
   aGPowerSupply->devserver.state = 0;
   aGPowerSupply->devserver.n_state = aGPowerSupply->devserver.state;
   aGPowerSupply->powersupply.set_val = 0.0;
   aGPowerSupply->powersupply.read_val = 0.0;
   aGPowerSupply->powersupply.channel = 1;
   aGPowerSupply->powersupply.n_ave = 1;
   aGPowerSupply->powersupply.conv_unit = (char*)malloc(sizeof("AMP"));
   sprintf(aGPowerSupply->powersupply.conv_unit,"AMP");
   aGPowerSupply->powersupply.set_offset = 0.0,
   aGPowerSupply->powersupply.read_offset = 0.0;
   aGPowerSupply->powersupply.set_u_limit = AG_MAX_CUR;
   aGPowerSupply->powersupply.set_l_limit = AG_MIN_CUR;
   aGPowerSupply->powersupply.polarity = 1.0;

/*
 * interrogate the static database for default values 
 */
  
   ps = aGPowerSupply;
   res_table[0].resource_adr = &(ps->devserver.state);
   res_table[1].resource_adr = &(ps->powersupply.set_val);
   res_table[2].resource_adr = &(ps->powersupply.channel);
   res_table[3].resource_adr = &(ps->powersupply.n_ave);
   res_table[4].resource_adr = &(ps->powersupply.conv_unit);
   res_table[5].resource_adr = &(ps->powersupply.set_offset);
   res_table[6].resource_adr = &(ps->powersupply.read_offset);
   res_table[7].resource_adr = &(ps->powersupply.set_u_limit);
   res_table[8].resource_adr = &(ps->powersupply.set_l_limit);
   res_table[9].resource_adr = &(ps->powersupply.polarity);

   if(db_getresource("CLASS/AGPS/DEFAULT",res_table,res_tab_size,error))
   {
      printf("class_initialise(): db_getresource() failed, error %d\n",error);
      return(DS_NOTOK);
   }
   else
   {
      printf("default values after searching the static database\n\n");
      printf("CLASS/AGPS/DEFAULT/state         D_LONG_TYPE    %6d\n",
              ps->devserver.state);
      printf("CLASS/AGPS/DEFAULT/set_val       D_FLOAT_TYPE   %6.0f\n",
              ps->powersupply.set_val);
      printf("CLASS/AGPS/DEFAULT/channel       D_SHORT_TYPE   %6d\n",
              ps->powersupply.channel);
      printf("CLASS/AGPS/DEFAULT/n_ave         D_SHORT_TYPE   %6d\n",
              ps->powersupply.n_ave);
      printf("CLASS/AGPS/DEFAULT/conv_unit     D_STRING_TYPE  %6s\n",
              ps->powersupply.conv_unit);
      printf("CLASS/AGPS/DEFAULT/set_offset    D_FLOAT_TYPE   %6.0f\n",
              ps->powersupply.set_offset);
      printf("CLASS/AGPS/DEFAULT/read_offset   D_FLOAT_TYPE   %6.0f\n",
              ps->powersupply.read_offset);
      printf("CLASS/AGPS/DEFAULT/set_u_limit   D_FLOAT_TYPE   %6.0f\n",
              ps->powersupply.set_u_limit);
      printf("CLASS/AGPS/DEFAULT/set_l_limit   D_FLOAT_TYPE   %6.0f\n",
              ps->powersupply.set_l_limit);
      printf("CLASS/AGPS/DEFAULT/polarity      D_SHORT_TYPE   %6d\n",
              ps->powersupply.polarity);
   }

   printf("returning from class_initialise()\n");
   return(DS_OK);
}



Andy Goetz
Tue Jan 28 13:58:13 MET 1997