home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / sysmgt / dmi / namedir / namedir.c next >
Text File  |  1999-05-11  |  18KB  |  501 lines

  1. /**********************************************************************
  2.     Filename: namedir.c
  3.  
  4.     Copyright (c) 1993 - 1994, International Business Machines, Corporation. All rights reserved.
  5.  
  6.     Description: Example of a direct interface component, built using the
  7.                  skeleton component code. (ciskel.c)
  8.  
  9. ************************* INCLUDES ***********************************/
  10. #define INCL_DOSPROCESS
  11.  
  12. #include "stdio.h"
  13. #include "stdlib.h"
  14. #include "string.h"
  15. #include <os2.h>
  16.  
  17. #include "dmiapi.h"
  18.  
  19. /************************ PUBLICS ************************************/
  20. /* Get the specified attribute. */
  21. DmiCiAttribute_t * DMI_FUNC_ENTRY CiGetAttribute(DMI_UNSIGNED componentId,
  22.                                    DMI_UNSIGNED groupId,
  23.                                    DMI_UNSIGNED attributeId,
  24.                                    DMI_UNSIGNED keyCount,
  25.                                    DmiCiAttribute_t const _FAR *const keyList);
  26.  
  27. /* Get the attribute with the same ID, but one row further within the table. */
  28. DmiCiAttribute_t * DMI_FUNC_ENTRY CiGetNextAttribute(DMI_UNSIGNED componentId,
  29.                                     DMI_UNSIGNED groupId,
  30.                                     DMI_UNSIGNED attributeId,
  31.                                     DMI_UNSIGNED keyCount,
  32.                                     DmiCiAttribute_t const _FAR *const keyList);
  33.  
  34. /* Release the specified attribute. */
  35. enum CiBoolean DMI_FUNC_ENTRY CiReleaseAttribute(DMI_UNSIGNED componentId,
  36.                                     DMI_UNSIGNED groupId,
  37.                                     DMI_UNSIGNED attributeId,
  38.                                     DMI_UNSIGNED keyCount,
  39.                                     DmiCiAttribute_t const _FAR *const keyList,
  40.                                     void const _FAR *const attributeValue);
  41.  
  42. /* Reserve the specified attribute. */
  43. enum CiBoolean DMI_FUNC_ENTRY CiReserveAttribute(DMI_UNSIGNED componentId,
  44.                                     DMI_UNSIGNED groupId,
  45.                                     DMI_UNSIGNED attributeId,
  46.                                     DMI_UNSIGNED keyCount,
  47.                                     DmiCiAttribute_t const _FAR *const keyList,
  48.                                     void const _FAR *const attributeValue);
  49.  
  50. /* Set the specified attribute. */
  51. enum CiBoolean DMI_FUNC_ENTRY CiSetAttribute(DMI_UNSIGNED componentId,
  52.                                    DMI_UNSIGNED groupId,
  53.                                    DMI_UNSIGNED attributeId,
  54.                                    DMI_UNSIGNED keyCount,
  55.                                    DmiCiAttribute_t const _FAR *const keyList,
  56.                                    void const _FAR *const attributeValue);
  57.  
  58. /*********************************************************************/
  59.  
  60. /************************ DEFINES ************************************/
  61.  
  62. /*
  63.     CI_MAX_KEY_COUNT is used to reserve space within the CI skeleton for
  64.     building key lists.  If the instrumentation code is supporting a component
  65.     that has keyed groups, then CI_MAX_KEY_COUNT should be set to the number
  66.     of keys in the group that has the longest key list.  If the component has
  67.     no keyed groups, this can be set to zero.
  68. */
  69.  
  70. #define CI_MAX_KEY_COUNT 1
  71.  
  72. /*********************************************************************/
  73.  
  74. /************************ GLOBALS ************************************/
  75.  
  76. /*
  77.     The actual command control structure for this Component.
  78. */
  79.  
  80. DmiCiControl_t CiCommandBlock = { 0 };
  81.  
  82. /*********************************************************************/
  83.  
  84. /*
  85.     The main entry point of the instrumentation.  The Service Layer calls this
  86.     procedure.  This procedure builds a CiCommand Block, and passes the package
  87.     to DmiCiProcess() in DMIAPI.LIB.  Based on the function ptrs provided,
  88.     DmiCiProcess() function will call those user-provided functions when
  89.     required.
  90. */
  91.  
  92. DMI_UNSIGNED DMI_FUNC_ENTRY DmiCiInvoke(void _FAR *dmiCommand)
  93. {
  94.  
  95. static void _FAR    *thisDmiCommand = (void _FAR *) 0;    
  96. static DMI_UNSIGNED    status = 0;
  97.  
  98. /*
  99.     This static array is where the Ci-style key lists are built.  Note that the
  100.     size of this static array is fairly small since the actual values are not
  101.     copied from the Service Layer request and confirm buffers.  The value
  102.     pointers in the array of CiAttribute objects point back into the buffers
  103.     themselves.
  104. */
  105.  
  106.     static DmiCiAttribute_t    ciKeyList[CI_MAX_KEY_COUNT] = { 0 };
  107.  
  108. /*
  109.     Save the current command block pointer.
  110. */
  111.  
  112.     thisDmiCommand = dmiCommand;   
  113.  
  114.     CiCommandBlock.CiGetAttribute = CiGetAttribute;
  115.     CiCommandBlock.CiGetNextAttribute = CiGetNextAttribute;
  116.     CiCommandBlock.CiReleaseAttribute = CiReleaseAttribute;
  117.     CiCommandBlock.CiReserveAttribute = CiReserveAttribute;
  118.     CiCommandBlock.CiSetAttribute = CiSetAttribute;
  119.  
  120. /*
  121.     Set the maximum number of keys used and the key list pointer.
  122. */
  123.  
  124.     CiCommandBlock.MaxKeyCount = CI_MAX_KEY_COUNT;
  125.     CiCommandBlock.ciKeyList = ciKeyList;
  126.  
  127. /*
  128.     Call the DMIAPI.LIB function to process command.
  129. */
  130.  
  131.     status = DmiCiProcess(&CiCommandBlock, thisDmiCommand);  
  132.  
  133. /*
  134.     Return to the Service Layer.
  135. */
  136.  
  137.     return status;
  138. }
  139.  
  140. /*
  141.     The cancel entry point of the instrumentation.  The Service Layer calls
  142.     this procedure when it has been requested to cancel a DMI command that
  143.     this instrumentation code is handling.  This procedure only sets the global
  144.     cicancelFlag.  If the instrumentation author wishes to use the state of
  145.     the cancel flag, then logic must be added to the user-written
  146.     procedures in the "public" section.
  147. */
  148.  
  149. DMI_UNSIGNED DMI_FUNC_ENTRY DmiCiCancel(void)
  150. {
  151.  
  152.     CiCommandBlock.ciCancelFlag = CiTrue;
  153.     return SLERR_NO_ERROR;
  154.  
  155. }
  156.  
  157. #define FILL_IN
  158.  
  159. #ifdef FILL_IN
  160.  
  161. #define NAMES_INDEX 1
  162. #define NAMES_OS  2
  163. #define NAMES_COMPANY  3
  164. #define NAMES_SL    4
  165. #define NUM_NAMES sizeof Names / sizeof Names[0]
  166.  
  167. typedef struct {
  168.     DMI_UNSIGNED  length;
  169.     char           body[30];
  170. } CMP_STRING;
  171.  
  172. /* This macro builds an initialized CMP_STRING object initialized with
  173.     the string passed into it. */
  174.  
  175. #define MIF_iString(n, s) CMP_STRING n = { sizeof(s) - 1, s }
  176.  
  177.  
  178. typedef struct _Name{
  179.     DMI_UNSIGNED    index;
  180.     CMP_STRING    *osname;
  181.     CMP_STRING    *company;
  182.     CMP_STRING    *SL;
  183. } Name;
  184.  
  185.  
  186. MIF_iString(os1, "OS/2 Warp");
  187. MIF_iString(os2, "OS/2 Warp Connect (PowerPC)");
  188. MIF_iString(os3, "AIX");
  189. MIF_iString(comp1,"IBM Corp.");
  190. MIF_iString(comp2,"IBM Corp.");
  191. MIF_iString(comp3,"IBM Corp.");
  192.  
  193. MIF_iString(sl1,"WARP SL Kit");
  194. MIF_iString(sl2,"WARP for PowerPC SL Kit");
  195. MIF_iString(sl3,"AIX SL Kit");
  196.  
  197.  
  198. Name Names[] = {1, &os1,&comp1,&sl1, 2, &os2,&comp2,
  199.      &sl2, 3, &os3,&comp3,&sl3};
  200.  
  201.  
  202. ULONG APIENTRY InstallCallBack(void *String)
  203. {
  204. return 0;
  205. }
  206.  
  207. static char myMif[] = "NAMEDIR.MIF";
  208.  
  209. main(int argc, char *argv[], char *envp[])
  210. {
  211.    DMI_INT                myComponentID;
  212.    CMP_STRING             *myName;
  213.    DMI_RegisterCiInd_t    *ciRegister;
  214.    DMI_MgmtCommand_t      *dmiCommand;
  215.    DMI_AccessData_t       *accessList;
  216.    SL_ErrorCode_t         retVal;
  217.    DMI_RegisterCnf_t      *RegisterCnfBuf;
  218.    DMI_INT                CnfSize = 4000UL;
  219.  
  220.    ULONG                 Size,x;
  221.    DMI_CiInstallData_t   *ciInstall;
  222.    DMI_FileData_t        *fileList;
  223.    int                   i;
  224.  
  225.    RegisterCnfBuf = (DMI_RegisterCnf_t *)malloc(CnfSize);
  226.  
  227. /* allocate and build the DMI command block to install the MIF file */
  228.  
  229.    Size = (ULONG) (sizeof(DMI_CiInstallData_t) + strlen(myMif) + sizeof(ULONG));   /* get the size of the block */
  230.    ciInstall = (DMI_CiInstallData_t *)malloc(Size);
  231.    memset(ciInstall,0,Size);   /* clear out the whole thing first */
  232.  
  233. /* build up the basic part of the DMI command */
  234.  
  235.    dmiCommand = &(ciInstall->DmiMgmtCommand);
  236.    dmiCommand->iLevelCheck = DMI_LEVEL_CHECK;
  237.    dmiCommand->iCommand = DmiCiInstallCmd;
  238.    dmiCommand->iCmdLen = Size;
  239.    dmiCommand->iRequestCount = 1;
  240.  
  241. /* set the number of files to be installed */
  242.  
  243.    ciInstall->iFileCount = 1;
  244.    fileList = &(ciInstall->DmiFileList[0]);
  245.  
  246. /* set the install type to file and copy in the string that has the file name in it */
  247.  
  248.    fileList->iFileType = MIF_MIF_FILE_NAME_FILE_TYPE;  /* this is a file name */
  249.    fileList->oFileData = sizeof(DMI_CiInstallData_t);
  250.    myName = (CMP_STRING *)((char *)ciInstall + fileList->oFileData);
  251.    myName->length = strlen(myMif);
  252.    memcpy(myName->body,myMif,strlen(myMif));
  253.  
  254.    printf("Preparing to install '");
  255.    for (i=0; i<myName->length; i++) {
  256.       printf("%c",myName->body[i]);
  257.    } /* endfor */
  258.    printf("'\n");
  259.  
  260. /* invoke DMI to actually install the MIF file */
  261.  
  262.    retVal = DmiInvoke((DMI_MgmtCommand_t *)ciInstall);
  263.    if (retVal != SLERR_NO_ERROR) {
  264.       printf("Exiting ciInstall with RC=%x\n",retVal);
  265.       free(ciInstall);
  266.       return retVal;
  267.    } /* endif */
  268.  
  269. /* get the component ID back so we will be able to register the DI componentry with the
  270.     correct component (Installed MIF) */
  271.  
  272.    myComponentID = ciInstall->iComponentId;
  273.    free(ciInstall);
  274.    printf("Installation successful ComponentID=%x.\n",myComponentID);
  275.  
  276.  
  277. /* allocate and build the DMI command block to register the DI componentry */
  278.  
  279.    Size = (ULONG) (sizeof(DMI_RegisterCiInd_t) + (3 * sizeof(DMI_AccessData_t)));   /* get the size of the block */
  280.    ciRegister = malloc(Size);
  281.    memset(ciRegister,0,Size);   /* clear out the whole thing first */
  282.  
  283. /* build up the basic part of the DMI command */
  284.  
  285.    dmiCommand = &(ciRegister->DmiMgmtCommand);
  286.    dmiCommand->iLevelCheck = DMI_LEVEL_CHECK;
  287.    dmiCommand->iCommand = DmiRegisterCiCmd;
  288.    dmiCommand->iCmdLen = Size;
  289.    dmiCommand->iCnfBufLen = CnfSize;
  290.    dmiCommand->pCnfBuf = RegisterCnfBuf;
  291.  
  292. /* specify the component ID and entry points for the CI register */
  293.  
  294.    ciRegister->iComponentId = myComponentID;
  295.    ciRegister->pAccessFunc = DmiCiInvoke;
  296.    ciRegister->pCancelFunc = DmiCiCancel;
  297.  
  298. /* specify the list of attributes that will be supported by this DI componentry */
  299.  
  300.    ciRegister->iAccessListCount = 4;
  301.    accessList = &(ciRegister->DmiAccessList[0]);
  302.    for(x = 1;x != 5;x++){
  303.        accessList->iGroupId = 2;
  304.        accessList->iAttributeId = x;
  305.        accessList++;
  306.    }
  307.  
  308.  
  309. /* actually register with the DMI SL */
  310.  
  311.    printf("Preparing to register component id %d\n",myComponentID);
  312.    retVal = DmiInvoke((DMI_MgmtCommand_t *)ciRegister);
  313.    if (retVal != SLERR_NO_ERROR) {
  314.       printf("Exiting ciRegister with RC=%x\n",retVal);
  315.       free(ciRegister);
  316.       return retVal;
  317.    } 
  318.  
  319.    free(ciRegister);
  320.    printf("I got started\n");
  321.  
  322. /* wait here for requests for information from this DI componentry */
  323.  
  324.    while(1) DosSleep(10000);    /* sleep for 10 seconds at a time, better then burning cycles... */
  325. }
  326.  
  327.  
  328. /*
  329.     The following four procedures are the ones that the instrumenation provider
  330.     needs to "fill in."  None of the procedures above the line should ever
  331.     need to be modified.
  332. */
  333.  
  334. /*
  335.     Return a pointer to a DmiCiAttribute_t object that contains the ID, type, and
  336.     pointer to value for the requested attribute.  The component ID, group ID,
  337.     and attribute ID are passed in as parameters.
  338.  
  339.     If the given group is not a table, then keyCount will be zero, and keyList
  340.     will be a NIL pointer.
  341.  
  342.     If the group is a table a keyList may or may not be given.  If it is
  343.     provided, then the attribute value from the requested row should be
  344.     returned.  If there is no key list, then the attribute value from the
  345.     first row should be returned.
  346. */
  347.  
  348. DmiCiAttribute_t * DMI_FUNC_ENTRY CiGetAttribute(DMI_UNSIGNED componentId,
  349.                                    DMI_UNSIGNED groupId,
  350.                                    DMI_UNSIGNED attributeId,
  351.                                    DMI_UNSIGNED keyCount,
  352.                                    DmiCiAttribute_t const _FAR *const keyList)
  353. {
  354. static DmiCiAttribute_t GetAttr;
  355. ULONG Index;
  356.  
  357.     Index = 0;     /* default to the first element in the table */
  358.     if(keyCount && (keyList != (DmiCiAttribute_t _FAR *)NULL)){   /* need to check the key list */
  359.         Index = *(ULONG *)(keyList->pAttributeValue);  /* get the key value */
  360.         if((Index < 1) || (Index > 3)) return (DmiCiAttribute_t *)NULL;
  361.         Index--;   /* decrement it to allign with the array offset */
  362.     }
  363.     switch((GetAttr.iAttributeId = attributeId)){
  364.         case 1:  /* index number */
  365.             GetAttr.iAttributeType = MIF_INTEGER;
  366.             GetAttr.pAttributeValue = &(Names[Index].index);
  367.             break;
  368.         case 2:  /* Operating System */
  369.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  370.             GetAttr.pAttributeValue = Names[Index].osname;
  371.             break;
  372.         case 3:  /* Operating system manufacturer */
  373.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  374.             GetAttr.pAttributeValue = Names[Index].company;
  375.             break;
  376.         case 4:  /* The SL for the operating system */
  377.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  378.             GetAttr.pAttributeValue = Names[Index].SL;
  379.             break;
  380.         default:    /* this is not an attribute ID we understand */
  381.             return (DmiCiAttribute_t *)NULL;
  382.     }
  383.     return (DmiCiAttribute_t *)&GetAttr;
  384. }
  385.  
  386.  
  387. /*
  388.     Return a pointer to a CiATtribute object that cotains the ID, type, and
  389.     pointer to value for the SUCCESSOR of the specified attribute.  For example:
  390.  
  391.     {1, "Red"}
  392.     {2, "Green"}
  393.     {3, "Blue"}
  394.  
  395.     Assume that the previous is a small table.  Also assume that the table is
  396.     component 2, group 2.  Assume further that the columns of this table are
  397.     attributes 1 and 2, and that attribute 1 is the key.  Then, if the caller
  398.     requests CiGetNextAttribute(2, 2, 2, 1, keyList) and the key list contains
  399.     1, then this procedure should return a DmiCiAttribute_t object with the value
  400.     2.  If 2 is in the key list, the result should be 3.  And finally, if 3 is
  401.     in the key list the procedure should retun NIL as this is the last row in
  402.     the table.
  403. */
  404.  
  405. DmiCiAttribute_t * DMI_FUNC_ENTRY CiGetNextAttribute(DMI_UNSIGNED componentId,
  406.                                        DMI_UNSIGNED groupId,
  407.                                        DMI_UNSIGNED attributeId,
  408.                                        DMI_UNSIGNED keyCount,
  409.                                        DmiCiAttribute_t const _FAR *const keyList)
  410. {
  411. static DmiCiAttribute_t GetAttr;
  412. ULONG Index;
  413.  
  414.     Index = 0;     /* default to the first element in the table */
  415.     if(keyCount && (keyList != (DmiCiAttribute_t _FAR *)NULL)){   /* need to check the key list */
  416.         Index = *(ULONG *)(keyList->pAttributeValue);  /* get the key value */
  417.         Index++;
  418.         if((Index < 1) || (Index > 3)) return (DmiCiAttribute_t *)NULL;
  419.         Index--;   /* decrement it to allign with the array offset */
  420.     }
  421.     switch((GetAttr.iAttributeId = attributeId)){
  422.         case 1:  /* index number */
  423.             GetAttr.iAttributeType = MIF_INTEGER;
  424.             GetAttr.pAttributeValue = &(Names[Index].index);
  425.             break;
  426.         case 2:  /* Operating System */
  427.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  428.             GetAttr.pAttributeValue = Names[Index].osname;
  429.             break;
  430.         case 3:  /* Operating system manufacturer */
  431.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  432.             GetAttr.pAttributeValue = Names[Index].company;
  433.             break;
  434.         case 4:  /* The SL for the operating system */
  435.             GetAttr.iAttributeType = MIF_DISPLAYSTRING;
  436.             GetAttr.pAttributeValue = Names[Index].SL;
  437.             break;
  438.         default:    /* this is not an attribute ID we understand */
  439.             return (DmiCiAttribute_t *)NULL;
  440.     }
  441.     return (DmiCiAttribute_t *)&GetAttr;
  442. }
  443.  
  444. /*
  445.     The caller is asking the instrumentation code to decommit from a set
  446.     operation after a reserve has been issued.  Return CiTrue if successful,
  447.     CiFalse otherwise.
  448. */
  449.  
  450. enum CiBoolean DMI_FUNC_ENTRY CiReleaseAttribute(DMI_UNSIGNED componentId,
  451.                                   DMI_UNSIGNED groupId,
  452.                                   DMI_UNSIGNED attributeId,
  453.                                   DMI_UNSIGNED keyCount,
  454.                                   DmiCiAttribute_t const _FAR *const keyList,
  455.                                   void const _FAR *const attributeValue)
  456. {
  457.     return CiTrue;
  458. }
  459.  
  460. /*
  461.     The caller is asking if the specified attribute could be set if these
  462.     same parameters were passed to the CiSetAttribute procedure.  Return CiTrue
  463.     or CiFalse.
  464. */
  465.  
  466. enum CiBoolean DMI_FUNC_ENTRY CiReserveAttribute(DMI_UNSIGNED componentId,
  467.                                   DMI_UNSIGNED groupId,
  468.                                   DMI_UNSIGNED attributeId,
  469.                                   DMI_UNSIGNED keyCount,
  470.                                   DmiCiAttribute_t const _FAR *const keyList,
  471.                                   void const _FAR *const attributeValue)
  472. {
  473.     return CiTrue;
  474. }
  475.  
  476. /*
  477.     Set the specified attribute with the given value.  If successful, return
  478.     CiTrue, otherwise return CiFalse.  The component ID, group ID, and
  479.     attribute ID are passed in as parameters.
  480.  
  481.     If the given group is not a table, then keyCount will be zero, and keyList
  482.     will be a NIL pointer.
  483.  
  484.     If the group is a table a keyList may or may not be given.  If it is
  485.     provided, then the attribute in the specified row should be set.  If there
  486.     is no key list, then the attribute in the first row should be set.
  487. */
  488.  
  489. enum CiBoolean DMI_FUNC_ENTRY CiSetAttribute(DMI_UNSIGNED componentId,
  490.                               DMI_UNSIGNED groupId,
  491.                               DMI_UNSIGNED attributeId,
  492.                               DMI_UNSIGNED keyCount,
  493.                               DmiCiAttribute_t const _FAR *const keyList,
  494.                               void const _FAR *const attributeValue)
  495. {
  496.     return CiFalse;
  497. }
  498.  
  499. #endif
  500.  
  501.