SAM3S8/SD8 Software Package: examples_usb/usb_hid_transfer/HIDTest/buffers.c Source File

SAM3S8/SD8 Software Package 1.2

# buffers.c

    00001 /*++
    00002
    00003 Copyright (c) Microsoft 1998, All Rights Reserved
    00004
    00005 Module Name:
    00006
    00007     buffers.c
    00008
    00009 Abstract:
    00010
    00011     This module contains the code for handling the display of HID report
    00012     buffers for the extended calls dialog box.
    00013
    00014 Environment:
    00015
    00016     User mode
    00017
    00018 Revision History:
    00019
    00020     May-98 : Created
    00021
    00022 --*/
    00023
    00024 #include <windows.h>
    00025 #include <malloc.h>
    00026 #include <setupapi.h>
    00027 #include "hidsdi.h"
    00028 #include "hidpi.h"
    00029 #include "buffers.h"
    00030 #include "strings.h"
    00031
    00032 #define CURRENT_REPORT(pDisp)   (pDisp -> ReportBuffers + pDisp -> iCurrSelectionIndex)
    00033
    00034 BOOLEAN
    00035 BufferDisplay_Init(
    00036     IN  HWND                hCB,
    00037     IN  HWND                hEB,
    00038     IN  INT                 nBuffers,
    00039     IN  INT                 iBufferSize,
    00040     IN  HIDP_REPORT_TYPE    RType,
    00041     OUT PBUFFER_DISPLAY     *ppBufferDisplay
    00042 )
    00043 /*++
    00044 Routine Description:
    00045     This routine initializes the buffer display mechanism for a given report type
    00046
    00047     The display mechanism maintains a list of list of nBuffers, where is each
    00048     is buffer is of iBufferSize and hCB and hEB are handles to the combo box and
    00049     edit box for displaying the buffer.
    00050
    00051     The variable ppBufferDisplay is allocated block which is passed into other
    00052     buffer routines and contains information about the buffer for the other
    00053     routines.
    00054
    00055     This function will return FALSE if there was a problem allocating memory
    00056 --*/
    00057 {
    00058     PBUFFER_DISPLAY pNewDisplay;
    00059     CHAR            *pszBufferHeader;
    00060     CHAR            szBufferName[24];
    00061     INT             iIndex;
    00062     INT             iCBIndex;
    00063
    00064     pNewDisplay = (PBUFFER_DISPLAY) malloc(sizeof(BUFFER_DISPLAY));
    00065
    00066     *ppBufferDisplay = NULL;
    00067
    00068     if (NULL == pNewDisplay)
    00069     {
    00070         return (FALSE);
    00071     }
    00072
    00073     pNewDisplay -> ReportBuffers = (PREPORT_BUFFER) malloc(sizeof(REPORT_BUFFER) * nBuffers);
    00074
    00075     if (NULL == pNewDisplay -> ReportBuffers)
    00076     {
    00077         free(pNewDisplay);
    00078
    00079         return (FALSE);
    00080     }
    00081
    00082     memset(pNewDisplay -> ReportBuffers, 0x00, sizeof(REPORT_BUFFER) * nBuffers);
    00083
    00084     pNewDisplay -> hBufferComboBox = hCB;
    00085     pNewDisplay -> hBufferEditBox = hEB;
    00086     pNewDisplay -> nReportBuffers = nBuffers;
    00087     pNewDisplay -> iBufferSize = iBufferSize;
    00088     pNewDisplay -> ReportType = RType;
    00089
    00090     switch (pNewDisplay -> ReportType)
    00091     {
    00092         case HidP_Input:
    00093             pszBufferHeader = "Input";
    00094             break;
    00095
    00096         case HidP_Output:
    00097             pszBufferHeader = "Output";
    00098             break;
    00099
    00100         case HidP_Feature:
    00101             pszBufferHeader = "Feature";
    00102             break;
    00103
    00104         default:
    00105             pszBufferHeader = "Other";
    00106             break;
    00107     }
    00108
    00109     for (iIndex = 0; iIndex < pNewDisplay -> nReportBuffers; iIndex++)
    00110     {
    00111         wsprintf(szBufferName, "%s Buffer #%d", pszBufferHeader, iIndex);
    00112
    00113         iCBIndex = (INT) SendMessage(pNewDisplay -> hBufferComboBox,
    00114                                      CB_ADDSTRING,
    00115                                      0,
    00116                                      (LPARAM) szBufferName);
    00117
    00118         if (CB_ERR == iCBIndex || CB_ERRSPACE == iCBIndex)
    00119         {
    00120             BufferDisplay_Destroy(pNewDisplay);
    00121             return (FALSE);
    00122         }
    00123
    00124         iCBIndex = (INT) SendMessage(pNewDisplay -> hBufferComboBox,
    00125                                      CB_SETITEMDATA,
    00126                                      iCBIndex,
    00127                                      iIndex);
    00128
    00129         if (CB_ERR == iCBIndex || CB_ERRSPACE == iCBIndex)
    00130         {
    00131             BufferDisplay_Destroy(pNewDisplay);
    00132             return (FALSE);
    00133         }
    00134     }
    00135
    00136     SendMessage(pNewDisplay -> hBufferComboBox, CB_SETCURSEL, 0, 0);
    00137
    00138     BufferDisplay_ChangeSelection(pNewDisplay);
    00139
    00140     *ppBufferDisplay = pNewDisplay;
    00141     return (TRUE);
    00142 }
    00143
    00144 VOID
    00145 BufferDisplay_Destroy(
    00146     IN  PBUFFER_DISPLAY     pBufferDisplay
    00147 )
    00148 /*++
    00149 Routine Description:
    00150     This routine cleans up the buffer display variable that was allocated by
    00151     the initialize routine
    00152 --*/
    00153 {
    00154     INT     iIndex;
    00155
    00156     for (iIndex = 0; iIndex < pBufferDisplay -> nReportBuffers; iIndex++)
    00157     {
    00158         if (NULL != pBufferDisplay -> ReportBuffers[iIndex].pBuffer)
    00159         {
    00160             free(pBufferDisplay -> ReportBuffers[iIndex].pBuffer);
    00161         }
    00162     }
    00163
    00164     free(pBufferDisplay -> ReportBuffers);
    00165     free(pBufferDisplay);
    00166     return;
    00167 }
    00168
    00169 VOID
    00170 BufferDisplay_ChangeSelection(
    00171     IN  PBUFFER_DISPLAY     pBufferDisplay
    00172 )
    00173 /*++
    00174 Routine Description:
    00175     This routine has the selection of a buffer to display via the combo box
    00176 --*/
    00177 {
    00178     INT     iNewIndex;
    00179
    00180     iNewIndex = (INT) SendMessage(pBufferDisplay -> hBufferComboBox,
    00181                                   CB_GETCURSEL,
    00182                                   0,
    00183                                   0);
    00184
    00185     if (CB_ERR == iNewIndex)
    00186     {
    00187         return;
    00188     }
    00189
    00190     iNewIndex = (INT) SendMessage(pBufferDisplay -> hBufferComboBox,
    00191                                   CB_GETITEMDATA,
    00192                                   iNewIndex,
    00193                                   0);
    00194
    00195     if (CB_ERR == iNewIndex)
    00196     {
    00197         return;
    00198     }
    00199
    00200     pBufferDisplay -> iCurrSelectionIndex = iNewIndex;
    00201
    00202     BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
    00203                                &(pBufferDisplay -> ReportBuffers[iNewIndex]));
    00204
    00205     return;
    00206 }
    00207
    00208 VOID
    00209 BufferDisplay_OutputBuffer(
    00210     HWND            hEditBox,
    00211     PREPORT_BUFFER  pReportBuffer
    00212 )
    00213 /*++
    00214 Routine Description:
    00215     This routine outputs to hEditBox a byte representation of pReportBuffer
    00216 --*/
    00217 {
    00218     PCHAR           BufferString;
    00219
    00220     if (0 == pReportBuffer -> iBufferSize || NULL == pReportBuffer -> pBuffer)
    00221     {
    00222         SetWindowText(hEditBox, "");
    00223     }
    00224     else
    00225     {
    00226         /*
    00227         // Create a buffer string the size of the buffer and display
    00228         //   as bytes
    00229         */
    00230
    00231         Strings_CreateDataBufferString(pReportBuffer -> pBuffer,
    00232                                        pReportBuffer -> iBufferSize,
    00233                                        pReportBuffer -> iBufferSize,
    00234                                        1,
    00235                                        &BufferString);
    00236
    00237         if (NULL == BufferString)
    00238         {
    00239             SetWindowText(hEditBox, "");
    00240         }
    00241         else
    00242         {
    00243             SetWindowText(hEditBox, BufferString);
    00244             free(BufferString);
    00245         }
    00246     }
    00247     return;
    00248 }
    00249
    00250 BOOLEAN
    00251 BufferDisplay_UpdateBuffer(
    00252     IN  PBUFFER_DISPLAY     pBufferDisplay,
    00253     IN  PCHAR               pNewBuffer
    00254 )
    00255 /*++
    00256 Routine Description:
    00257     This routine changes the data of the currently active report buffer for the
    00258     given buffer display structure.
    00259
    00260     It returns FALSE if it needed to allocate a new buffer and the memory allocation
    00261     failed.
    00262 --*/
    00263 {
    00264     PREPORT_BUFFER          pCurrentReport;
    00265
    00266     pCurrentReport = CURRENT_REPORT(pBufferDisplay);
    00267
    00268     if (NULL == pCurrentReport -> pBuffer)
    00269     {
    00270         pCurrentReport -> pBuffer = malloc(pBufferDisplay -> iBufferSize);
    00271         if ((NULL == pCurrentReport) || (NULL == pCurrentReport -> pBuffer))
    00272         {
    00273             return (FALSE);
    00274         }
    00275
    00276         pCurrentReport -> iBufferSize = pBufferDisplay -> iBufferSize;
    00277     }
    00278
    00279     memmove (pCurrentReport -> pBuffer, pNewBuffer, pCurrentReport -> iBufferSize);
    00280
    00281     BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox, pCurrentReport);
    00282
    00283     return (TRUE);
    00284 }
    00285
    00286 INT
    00287 BufferDisplay_GetBufferSize(
    00288     IN  PBUFFER_DISPLAY      pBufferDisplay
    00289 )
    00290 /*++
    00291 Routine Description:
    00292     This routine simply returns the size of the given buffer
    00293 --*/
    00294 {
    00295     return (pBufferDisplay -> iBufferSize);
    00296 }
    00297
    00298 VOID
    00299 BufferDisplay_CopyCurrentBuffer(
    00300     IN  PBUFFER_DISPLAY     pBufferDisplay,
    00301     OUT PCHAR               pCopyBuffer
    00302 )
    00303 /*++
    00304 Routine Description:
    00305     This routine copies the currently active buffer for the given buffer display
    00306     into the buffer passed in by the caller.
    00307
    00308     It is the caller's responsibility to allocate a buffer of the appropriate size
    00309     The appropriate size can be obtain by calling BufferDisplay_GetBufferSize
    00310 --*/
    00311 {
    00312     PREPORT_BUFFER          pCurrentReport;
    00313
    00314     pCurrentReport = CURRENT_REPORT(pBufferDisplay);
    00315
    00316     if (NULL == pCurrentReport -> pBuffer)
    00317     {
    00318         memset(pCopyBuffer, 0x0, pBufferDisplay -> iBufferSize);
    00319     }
    00320     else
    00321     {
    00322         memcpy(pCopyBuffer, pCurrentReport -> pBuffer, pCurrentReport -> iBufferSize);
    00323     }
    00324     return;
    00325 }
    00326
    00327 INT
    00328 BufferDisplay_GetCurrentBufferNumber(
    00329     IN  PBUFFER_DISPLAY      pBufferDisplay
    00330 )
    00331 /*++
    00332 Routine Description:
    00333     This routine returns the buffer number of the current buffer selection
    00334 --*/
    00335 {
    00336     return (pBufferDisplay -> iCurrSelectionIndex);
    00337 }
    00338
    00339 UCHAR
    00340 BufferDisplay_GetCurrentReportID(
    00341     IN  PBUFFER_DISPLAY      pBufferDisplay
    00342 )
    00343 /*++
    00344 Routine Description:
    00345     This routine returns the report ID of the current buffer selection
    00346 --*/
    00347 {
    00348     PREPORT_BUFFER pCurrentReport;
    00349
    00350     pCurrentReport = CURRENT_REPORT(pBufferDisplay);
    00351
    00352     return (pCurrentReport -> ucReportID);
    00353 }
    00354
    00355 VOID
    00356 BufferDisplay_ClearBuffer(
    00357     IN  PBUFFER_DISPLAY pBufferDisplay
    00358 )
    00359 /*++
    00360 Routine Description:
    00361     This routine frees the current report buffer and set's it to NULL
    00362 --*/
    00363 {
    00364     PREPORT_BUFFER pCurrentReport;
    00365
    00366     pCurrentReport = CURRENT_REPORT(pBufferDisplay);
    00367
    00368     if (NULL != pCurrentReport -> pBuffer)
    00369     {
    00370         free(pCurrentReport -> pBuffer);
    00371
    00372         pCurrentReport -> iBufferSize = 0;
    00373         pCurrentReport -> ucReportID = 0;
    00374         pCurrentReport -> pBuffer = NULL;
    00375     }
    00376
    00377     BufferDisplay_OutputBuffer(pBufferDisplay -> hBufferEditBox,
    00378                                pCurrentReport);
    00379     return;
    00380 }
    00381

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines