/*===========================================================================
                           OEMDeviceNotifier.c

  This file contains sample source to the IDeviceNotifier interface.

  Please search for OEM-Note in the file for points of interest.

        Copyright © 2003 QUALCOMM Incorporated.
               All Rights Reserved.
            QUALCOMM Proprietary/GTDR
============================================================================*/
#include "AEE.h"
#include "AEEShell.h"
#include "AEEStdLib.h"
#include "OEMDeviceNotifier.h"

OBJECT(CDeviceNotifier)
{
   const AEEVTBL(IDeviceNotifier) * pvt;    // primary IDNS interface
   
	// Class member variables
	uint32		      m_nRefs;		   // references to us
	IShell *	         m_pIShell;		// copy of Shell pointer

   uint32            m_dwMask;
};


/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
int               AEEDeviceNotifier_New(IShell * ps, AEECLSID cls, void ** ppif);

static uint32     CDeviceNotifier_AddRef(IDeviceNotifier * po);
static uint32     CDeviceNotifier_Release(IDeviceNotifier * po);
static void       CDeviceNotifier_SetMask(IDeviceNotifier * po, const uint32 * dwMask);

static const VTBL(IDeviceNotifier) gpvtIDeviceNotifier = {
   CDeviceNotifier_AddRef, 
   CDeviceNotifier_Release,
   CDeviceNotifier_SetMask, 
};


/*===========================================================================

Function:  AEEDeviceNotifier_New()

Description:
   This is called from the shell when an app tries to create an instance of a
   class ID associated with this module.

Parapoters:
   IShell* pShell:  IShell pointer
   AEECLSID cls:    Class to create
   void** ppof:     pointer to created interface pointer

Return Value:  None

Componts:      None

Side Effects:  None

===========================================================================*/

int AEEDeviceNotifier_New(IShell * ps, AEECLSID cls, void ** ppif) {
   CDeviceNotifier * pMe = NULL;

   if (AEECLSID_DEVICENOTIFIER != cls) {
      return ECLASSNOTSUPPORT;
   }

   // init me
   pMe = (CDeviceNotifier *) MALLOC(sizeof(CDeviceNotifier));
   if ((CDeviceNotifier *) 0 == pMe) {
      return ENOMEMORY;
   }
   pMe->pvt = &gpvtIDeviceNotifier;

	pMe->m_nRefs      = 1;
   pMe->m_dwMask     = 0;
	pMe->m_pIShell    = ps;
   ISHELL_AddRef(ps);

   *ppif = pMe;

   return SUCCESS;
}

static uint32 CDeviceNotifier_AddRef(IDeviceNotifier * po) {
   return (++((CDeviceNotifier *)po)->m_nRefs);
}

static uint32 CDeviceNotifier_Release(IDeviceNotifier * po) {
	CDeviceNotifier * pMe = (CDeviceNotifier *) po;

	if (--pMe->m_nRefs != 0)
		return pMe->m_nRefs;

	// Ref count is zero. So, release memory associated with this object.

	// Release interfaces
	ISHELL_Release(pMe->m_pIShell);
	FREE(pMe);

	return 0;
}

static void CDeviceNotifier_SetMask(IDeviceNotifier * po, const uint32 * pdwMask) {
   uint32 dw;

   CDeviceNotifier * pMe = (CDeviceNotifier *) po;

   for (pMe->m_dwMask = 0; 0 != (dw = *pdwMask); pdwMask++) {
      pMe->m_dwMask |= dw;
   }
}