VirtualBox

Changeset 69067 in vbox for trunk/src/VBox/Devices/Samples


Ignore:
Timestamp:
Oct 13, 2017 8:41:13 AM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
118349
Message:

Devices/Samples: Separate the Playground device from the sample device, because the latter should be simple and clean, while the former is meant as a showcase for unusual things and therefore will have special needs.

Location:
trunk/src/VBox/Devices/Samples
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Samples/DevPlayground.cpp

    r67579 r69067  
    88 *
    99 * To enable this device for a particular VM:
    10  * VBoxManage setextradata vmname VBoxInternal/PDM/Devices/playground/Path .../obj/VBoxSampleDevice/VBoxSampleDevice
     10 * VBoxManage setextradata vmname VBoxInternal/PDM/Devices/playground/Path .../obj/VBoxPlaygroundDevice/VBoxPlaygroundDevice
    1111 * VBoxManage setextradata vmname VBoxInternal/Devices/playground/0/Config/Whatever1 0
    1212 */
    1313
    1414/*
    15  * Copyright (C) 2009-2016 Oracle Corporation
     15 * Copyright (C) 2009-2017 Oracle Corporation
    1616 *
    1717 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3333#include <VBox/err.h>
    3434#include <VBox/log.h>
     35
     36#include <VBox/com/assert.h>
     37#include <VBox/com/defs.h>
     38#include <VBox/com/string.h>
     39#include <VBox/com/Guid.h>
     40#include <VBox/com/VirtualBox.h>
    3541
    3642#include <iprt/assert.h>
     
    6773
    6874
     75#define PLAYGROUND_SSM_VERSION 3
     76
     77
    6978/*********************************************************************************************************************************
    7079*   Device Functions                                                                                                             *
     
    119128
    120129/**
    121  * @interface_method_impl{PDMDEVREG,pfnDestruct}
    122  */
    123 static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns)
    124 {
    125     /*
    126      * Check the versions here as well since the destructor is *always* called.
    127      */
    128     PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
     130 * @callback_method_impl{FNSSMDEVSAVEEXEC}
     131 */
     132static DECLCALLBACK(int) devPlaygroundSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
     133{
     134    PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
     135
     136    /* dummy (real devices would need to save their state here) */
     137    RT_NOREF(pThis);
     138
     139    /* Demo of some API stuff - very unusual, think twice if there's no better
     140     * solution which doesn't need API interaction. */
     141    HRESULT hrc = S_OK;
     142    com::Bstr bstrSnapName;
     143    com::Guid uuid(COM_IIDOF(ISnapshot));
     144    ISnapshot *pSnap = (ISnapshot *)PDMDevHlpQueryGenericUserObject(pDevIns, uuid.raw());
     145    if (pSnap)
     146    {
     147        hrc = pSnap->COMGETTER(Name)(bstrSnapName.asOutParam());
     148        AssertComRCReturn(hrc, VERR_INVALID_STATE);
     149    }
     150    com::Utf8Str strSnapName(bstrSnapName);
     151    SSMR3PutStrZ(pSSM, strSnapName.c_str());
     152    LogRel(("Playground: saving state of snapshot '%s', hrc=%Rhrc\n", strSnapName.c_str(), hrc));
     153
     154    return VINF_SUCCESS;
     155}
     156
     157/**
     158 * @callback_method_impl{FNSSMDEVLOADEXEC}
     159 */
     160static DECLCALLBACK(int) devPlaygroundLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
     161{
     162    PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
     163
     164    if (uVersion > PLAYGROUND_SSM_VERSION)
     165        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
     166    Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
     167
     168    /* dummy (real devices would need to load their state here) */
     169    RT_NOREF(pThis);
     170
     171    /* Reading the stuff written to saved state, just a demo. */
     172    char szSnapName[256];
     173    int rc = SSMR3GetStrZ(pSSM, szSnapName, sizeof(szSnapName));
     174    AssertRCReturn(rc, rc);
     175    LogRel(("Playground: loading state of snapshot '%s'\n", szSnapName));
    129176
    130177    return VINF_SUCCESS;
     
    138185{
    139186    RT_NOREF(iInstance, pCfg);
     187    int rc = VINF_SUCCESS;
    140188
    141189    /*
     
    171219            PCIDevSetHeaderType(&pFun->PciDev, 0x80); /* normal, multifunction device */
    172220
    173         int rc = PDMDevHlpPCIRegisterEx(pDevIns, &pFun->PciDev, iPciFun, 0 /*fFlags*/, iPciDevNo, iPciFun,
     221        rc = PDMDevHlpPCIRegisterEx(pDevIns, &pFun->PciDev, iPciFun, 0 /*fFlags*/, iPciDevNo, iPciFun,
    174222                                        pThis->aPciFuns[iPciFun].szName);
    175223        AssertLogRelRCReturn(rc, rc);
    176224
    177225        /* First region. */
    178         RTGCPHYS const cbFirst = iPciFun == 0 ? 8*_1G64 : iPciFun * _4K;
     226        RTGCPHYS const cbFirst = iPciFun == 0 ? 8*_1M : iPciFun * _4K;
    179227        rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 0, cbFirst,
    180228                                            (PCIADDRESSSPACE)(  PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
     
    193241
    194242        /* Second region. */
    195         RTGCPHYS const cbSecond = iPciFun == 0  ? 256*_1G64 : iPciFun * _32K;
     243        RTGCPHYS const cbSecond = iPciFun == 0  ? 32*_1M : iPciFun * _32K;
    196244        rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 2, cbSecond,
    197245                                            (PCIADDRESSSPACE)(  PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
     
    213261    }
    214262
    215     return VINF_SUCCESS;
    216 }
    217 
    218 RT_C_DECLS_BEGIN
    219 extern const PDMDEVREG g_DevicePlayground;
    220 RT_C_DECLS_END
     263    /*
     264     * Save state handling.
     265     */
     266    rc = PDMDevHlpSSMRegister(pDevIns, PLAYGROUND_SSM_VERSION, sizeof(*pThis), devPlaygroundSaveExec, devPlaygroundLoadExec);
     267    if (RT_FAILURE(rc))
     268        return rc;
     269
     270    return VINF_SUCCESS;
     271}
     272
     273
     274/**
     275 * @interface_method_impl{PDMDEVREG,pfnDestruct}
     276 */
     277static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns)
     278{
     279    /*
     280     * Check the versions here as well since the destructor is *always* called.
     281     */
     282    PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
     283
     284    return VINF_SUCCESS;
     285}
     286
    221287
    222288/**
    223289 * The device registration structure.
    224290 */
    225 const PDMDEVREG g_DevicePlayground =
     291static const PDMDEVREG g_DevicePlayground =
    226292{
    227293    /* u32Version */
     
    274340    PDM_DEVREG_VERSION
    275341};
     342
     343
     344/**
     345 * Register devices provided by the plugin.
     346 *
     347 * @returns VBox status code.
     348 * @param   pCallbacks      Pointer to the callback table.
     349 * @param   u32Version      VBox version number.
     350 */
     351extern "C" DECLEXPORT(int) VBoxDevicesRegister(PPDMDEVREGCB pCallbacks, uint32_t u32Version)
     352{
     353    LogFlow(("VBoxPlaygroundDevice::VBoxDevicesRegister: u32Version=%#x pCallbacks->u32Version=%#x\n", u32Version, pCallbacks->u32Version));
     354
     355    AssertLogRelMsgReturn(u32Version >= VBOX_VERSION,
     356                          ("VirtualBox version %#x, expected %#x or higher\n", u32Version, VBOX_VERSION),
     357                          VERR_VERSION_MISMATCH);
     358    AssertLogRelMsgReturn(pCallbacks->u32Version == PDM_DEVREG_CB_VERSION,
     359                          ("callback version %#x, expected %#x\n", pCallbacks->u32Version, PDM_DEVREG_CB_VERSION),
     360                          VERR_VERSION_MISMATCH);
     361
     362    return pCallbacks->pfnRegister(pCallbacks, &g_DevicePlayground);
     363}
     364
  • trunk/src/VBox/Devices/Samples/Makefile.kmk

    r63885 r69067  
    55
    66#
    7 # Copyright (C) 2009-2016 Oracle Corporation
     7# Copyright (C) 2009-2017 Oracle Corporation
    88#
    99# This file is part of VirtualBox Open Source Edition (OSE), as
     
    2626VBoxSampleDevice_TEMPLATE = VBOXR3
    2727VBoxSampleDevice_SOURCES  = \
    28         VBoxSampleDevice.cpp \
    29         DevPlayground.cpp
     28        VBoxSampleDevice.cpp
    3029VBoxSampleDevice_LIBS     = \
    3130        $(LIB_RUNTIME) \
    32         $(LIB_VMM) \
    33         $(LIB_REM)
     31        $(LIB_VMM)
    3432
    3533#
     
    4240VBoxSampleDriver_LIBS     = \
    4341        $(LIB_RUNTIME) \
    44         $(LIB_VMM) \
    45         $(LIB_REM)
     42        $(LIB_VMM)
     43
     44
     45#
     46# VBoxPlaygroundDevice - A device module demonstrating some unusual features.
     47#
     48DLLS += VBoxPlaygroundDevice
     49VBoxPlaygroundDevice_TEMPLATE = VBOXR3
     50VBoxPlaygroundDevice_SOURCES  = \
     51        DevPlayground.cpp
     52VBoxPlaygroundDevice_LIBS     = \
     53        $(PATH_STAGE_LIB)/VBoxCOM$(VBOX_SUFF_LIB) \
     54        $(LIB_RUNTIME) \
     55        $(LIB_VMM)
     56
     57ifdef VBOX_WITH_XPCOM
     58 ## @todo may be worth creating the VBOX_XPCOM SDK def, or just a SDK_VBOXXPCOM.
     59 VBoxPlaygroundDevice_DEFS += VBOX_WITH_XPCOM
     60 ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
     61  VBoxPlaygroundDevice_DEFS += VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
     62 endif
     63 VBoxPlaygroundDevice_INCS += \
     64        $(VBOX_XPCOM_INCS)
     65 VBoxPlaygroundDevice_INTERMEDIATES += \
     66        $(VBOX_PATH_SDK)/bindings/xpcom/include/VirtualBox_XPCOM.h
     67 VBoxPlaygroundDevice_LIBS += \
     68        $(LIB_XPCOM)
     69else  # COM
     70 VBoxPlaygroundDevice_INCS += \
     71        $(VBOX_PATH_SDK)/bindings/mscom/include
     72 VBoxPlaygroundDevice_INTERMEDIATES += \
     73        $(VBOX_PATH_SDK)/bindings/mscom/include/VirtualBox.h
     74endif # COM
     75
    4676
    4777include $(FILE_KBUILD_SUB_FOOTER)
  • trunk/src/VBox/Devices/Samples/VBoxSampleDevice.cpp

    r63911 r69067  
    158158                          VERR_VERSION_MISMATCH);
    159159
    160     /* Two devices in this module. */
    161     extern const PDMDEVREG g_DevicePlayground;
    162     int rc = pCallbacks->pfnRegister(pCallbacks, &g_DeviceSample);
    163     if (RT_SUCCESS(rc))
    164         rc = pCallbacks->pfnRegister(pCallbacks, &g_DevicePlayground);
    165     return rc;
     160    return pCallbacks->pfnRegister(pCallbacks, &g_DeviceSample);
    166161}
    167162
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette