HRESULT Library_spot_net_security_native_Microsoft_SPOT_Net_Security_SslNative::DataAvailable___STATIC__I4__OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* socket = stack.Arg0().Dereference();
    int       result;    
    CLR_INT32 handle;

    FAULT_ON_NULL(socket);

    handle = socket[ Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::FIELD__m_Handle ].NumericByRef().s4;

    result = SSL_DataAvailable( handle );

    // ThrowOnError expects anything other than 0 to be a failure - so return 0 if we don't have an error
    TINYCLR_CHECK_HRESULT(ThrowOnError( stack, result >= 0 ? 0 : result ));

    stack.SetResult_I4( result );

    TINYCLR_NOCLEANUP();    
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::Read___BOOLEAN( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_HARDWARE();
    TINYCLR_HEADER();

    CLR_UINT32 state;
        
    CLR_RT_HeapBlock* pThis = stack.This();  FAULT_ON_NULL(pThis);
    
    if(pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_NativeEventDispatcher::FIELD__m_disposed ].NumericByRef().s1 != 0)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
    }
    
    // Read state from Hardware
    state = ::CPU_GPIO_GetPinState( pThis[ FIELD__m_portId ].NumericByRefConst().u4 );
    
    // Return value to the managed application.
    stack.SetResult_Boolean( state == 0 ? false : true ) ;
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_Messaging_EndPoint::_ctor___VOID__mscorlibSystemType__U4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_MESSAGING();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*               pThis = stack.This(); 
    CLR_RT_HeapBlock_EndPoint::Port port;
    CLR_RT_HeapBlock_EndPoint*      ep;
    CLR_RT_HeapBlock*               hbType = stack.Arg1().Dereference();

    FAULT_ON_NULL(pThis);
    FAULT_ON_NULL(hbType);

    if(CLR_RT_ReflectionDef_Index::Convert( *hbType, port.m_type ) == false) TINYCLR_SET_AND_LEAVE(CLR_E_NULL_REFERENCE);
    port.m_id = stack.Arg2().NumericByRefConst().u4;

    ep = CLR_RT_HeapBlock_EndPoint::FindEndPoint( port ); if(ep) TINYCLR_SET_AND_LEAVE(CLR_E_BUSY);

    TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_EndPoint::CreateInstance( port, *pThis, pThis[ FIELD__m_handle ] ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_Time_native_Microsoft_SPOT_Time_TimeService::get_Settings___STATIC__MicrosoftSPOTTimeTimeServiceSettings( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER(); 

    TimeService_Settings    settings;
    CLR_RT_HeapBlock&       top = stack.PushValueAndClear();
    CLR_RT_HeapBlock*       managedSettings = NULL;

    TINYCLR_CHECK_HRESULT(TimeService_LoadSettings(&settings));    

    TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_TimeServiceSettings ));
    managedSettings = top.Dereference();

    managedSettings[ ManagedSettings::FIELD__PrimaryServerIP     ].SetInteger( settings.PrimaryServerIP );
    managedSettings[ ManagedSettings::FIELD__AlternateServerIP   ].SetInteger( settings.AlternateServerIP );
    managedSettings[ ManagedSettings::FIELD__RefreshTime         ].SetInteger( settings.RefreshTime );
    managedSettings[ ManagedSettings::FIELD__Tolerance           ].SetInteger( settings.Tolerance );
    managedSettings[ ManagedSettings::FIELD__ForceSyncAtWakeUp   ].SetBoolean( 0 != (settings.Flags & TimeService_Settings_Flags_ForceSyncAtWakeUp) );
    managedSettings[ ManagedSettings::FIELD__AutoDayLightSavings ].SetBoolean( 0 != (settings.Flags & TimeService_Settings_Flags_AutoDST) );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_CryptoState::LoadAsymmetricKey( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis;
    CLR_RT_HeapBlock_Array* array;

    pThis = stack.This(); FAULT_ON_NULL(pThis);

    //
    // Init RSA key.
    //
    array = pThis[ Library_spot_native_Microsoft_SPOT_Cryptography_Key_RSA::FIELD__m_modulus ].DereferenceArray(); FAULT_ON_NULL(array);
    memcpy( &m_keyAsymmetric.module, array->GetFirstElement(), sizeof(m_keyAsymmetric.module) );

    array = pThis[ Library_spot_native_Microsoft_SPOT_Cryptography_Key_RSA::FIELD__m_exponent ].DereferenceArray(); FAULT_ON_NULL(array);
    memcpy( &m_keyAsymmetric.exponent, array->GetFirstElement(), sizeof(m_keyAsymmetric.exponent) );

    m_keyAsymmetric.exponent_len = RSA_KEY_SIZE_BYTES / sizeof(DWORD);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_RuntimeType::GetName( CLR_RT_HeapBlock& arg, bool fFullName, CLR_RT_HeapBlock& res )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_TypeDef_Instance td;
    CLR_UINT32              levels;
    char                    rgBuffer[ 256 ];
    LPSTR                   szBuffer;
    size_t                  iBuffer;

    TINYCLR_CHECK_HRESULT(GetTypeDescriptor( arg, td, &levels ));
    
    szBuffer = rgBuffer;
    iBuffer  = MAXSTRLEN(rgBuffer);

    TINYCLR_CHECK_HRESULT(g_CLR_RT_TypeSystem.BuildTypeName( td, szBuffer, iBuffer, fFullName ? CLR_RT_TypeSystem::TYPENAME_FLAGS_FULL : 0, levels ));

    TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance( res, rgBuffer ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_BitConverter::GetBytes___STATIC__SZARRAY_U1__R8(CLR_RT_StackFrame& stack)
{
	NATIVE_PROFILE_CLR_CORE();
	TINYCLR_HEADER();

#if !defined(TINYCLR_EMULATED_FLOATINGPOINT)
	double input = stack.Arg0().NumericByRefConst().r8;
#else
	CLR_INT64 input = stack.Arg0().NumericByRefConst().r8;
#endif

	TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValueAndClear(), 8, g_CLR_RT_WellKnownTypes.m_UInt8));
	{
		BYTE* p = stack.TopValue().DereferenceArray()->GetFirstElement();
#if !defined(TINYCLR_EMULATED_FLOATINGPOINT)
		*reinterpret_cast<double*>(p) = input;
#else
		*reinterpret_cast<CLR_INT64*>(p) = input;
#endif
	}
	TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_CryptoState::Buffer::Allocate( CLR_RT_HeapBlock& ref, int size )
{
    TINYCLR_HEADER();

    ref.SetObjectReference( NULL );

    if(size)
    {
        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( ref, size, g_CLR_RT_WellKnownTypes.m_UInt8 ));

        m_array = ref.DereferenceArray();
    }
    else
    {
        m_array = NULL;
    }

    m_offset = 0;
    m_count  = size;

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZARRAY_U1(CLR_RT_StackFrame& stack)
{
	NATIVE_PROFILE_CLR_CORE();
	TINYCLR_HEADER();

	CLR_RT_HeapBlock_Array* pArray = stack.Arg0().DereferenceArray();
	FAULT_ON_NULL_ARG(pArray);

	if (pArray->m_numOfElements == 0)
	{
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(""));
	}
	else
	{
		BYTE* p = pArray->GetFirstElement();
		char* pOutput = ByteArrayToHex(p, 0, pArray->m_numOfElements);
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(pOutput));
		delete[] pOutput;
	}

	TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_Messaging_EndPoint::ReplyRaw___VOID__MicrosoftSPOTMessagingMessage__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_MESSAGING();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*                            pThis;
    CLR_RT_HeapBlock*                            pMsg;
    CLR_RT_HeapBlock_Array*                      pData;
    CLR_RT_HeapBlock_EndPoint*                   ep;
    CLR_Messaging_Commands::Messaging_Reply*     rpc = NULL;
    CLR_UINT32                                   len;

    pThis = stack.This()                   ; FAULT_ON_NULL(pThis);
    pMsg  = stack.Arg1().Dereference()     ; FAULT_ON_NULL(pMsg );
    pData = stack.Arg2().DereferenceArray(); FAULT_ON_NULL(pData);

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_EndPoint::ExtractInstance( pThis[ FIELD__m_handle ], ep ));

    len = sizeof(CLR_RT_HeapBlock_EndPoint::Address) + pData->m_numOfElements;
    rpc = (CLR_Messaging_Commands::Messaging_Reply*)CLR_RT_Memory::Allocate( len ); CHECK_ALLOCATION(rpc);

    rpc->m_addr.m_seq         = pMsg[ Library_spot_native_Microsoft_SPOT_Messaging_Message::FIELD__m_seq      ].NumericByRefConst  ().u4;
    rpc->m_addr.m_to          = ep->m_addr;
    rpc->m_addr.m_from.m_type = pMsg[ Library_spot_native_Microsoft_SPOT_Messaging_Message::FIELD__m_selector ].ReflectionDataConst().GetTypeHash();
    rpc->m_addr.m_from.m_id   = pMsg[ Library_spot_native_Microsoft_SPOT_Messaging_Message::FIELD__m_id       ].NumericByRefConst  ().u4;

    memcpy( rpc->m_data, pData->GetFirstElement(), pData->m_numOfElements );

    CLR_EE_MSG_EVENT_RPC(CLR_Messaging_Commands::c_Messaging_Reply,len,rpc,WP_Flags::c_NonCritical);

    TINYCLR_CLEANUP();

    if(rpc)
    {
        CLR_RT_Memory::Release( rpc );
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_spot_io_native_Microsoft_SPOT_IO_NativeFindFile::GetFileInfo___STATIC__MicrosoftSPOTIONativeFileInfo__STRING( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_IO();
    TINYCLR_HEADER();

    FS_FILEINFO                  fileInfo;
    BOOL                         found;
    UnicodeString                pathW;
    FileSystemVolume*            driver;

    CLR_RT_HeapBlock&            top = stack.PushValueAndClear();

    TINYCLR_CHECK_HRESULT(Library_spot_io_native_Microsoft_SPOT_IO_NativeIO::FindVolume( stack.Arg0(), driver, pathW ));

    TINYCLR_CHECK_HRESULT(driver->GetFileInfo( pathW, &fileInfo, &found ));

    if(found)
    {
        CLR_RT_HeapBlock* managedFi;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_NativeFileInfo ));

        managedFi = top.Dereference();

        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Attributes     ].SetInteger( (CLR_UINT32)fileInfo.Attributes );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__CreationTime   ].SetInteger( (CLR_INT64)fileInfo.CreationTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastAccessTime ].SetInteger( (CLR_INT64)fileInfo.LastAccessTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastWriteTime  ].SetInteger( (CLR_INT64)fileInfo.LastWriteTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Size           ].SetInteger( (CLR_INT64)fileInfo.Size );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__FileName       ].SetObjectReference( NULL );
    }
    else
    {
        top.SetObjectReference( NULL );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_io_native_Microsoft_SPOT_IO_NativeFindFile::GetNext___MicrosoftSPOTIONativeFileInfo( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_IO();
    TINYCLR_HEADER();

    CLR_RT_FindFile* ff;
    FS_FILEINFO*     fi;
    BOOL             found;
    
    CLR_RT_HeapBlock& top = stack.PushValueAndClear();

    TINYCLR_CHECK_HRESULT(GetFindFile( stack, ff ));

    TINYCLR_CHECK_HRESULT(ff->GetNext( &fi, &found ));

    if(found)
    {
        CLR_RT_HeapBlock* managedFi;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_NativeFileInfo ));

        managedFi = top.Dereference();

        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Attributes     ].SetInteger( (CLR_UINT32)fi->Attributes );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__CreationTime   ].SetInteger( (CLR_INT64)fi->CreationTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastAccessTime ].SetInteger( (CLR_INT64)fi->LastAccessTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastWriteTime  ].SetInteger( (CLR_INT64)fi->LastWriteTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Size           ].SetInteger( (CLR_INT64)fi->Size );

        TINYCLR_CHECK_HRESULT(ff->CreateFilenameString( managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__FileName ] ));
    }
    else
    {
        top.SetObjectReference( NULL );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Cryptoki::GetSlotsInternal___STATIC__SZARRAY_MicrosoftSPOTCryptokiSlot( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CK_ULONG                i;
    CK_SLOT_ID              slots[NETMF_CRYPTOKI_MAX_SLOTS];
    CK_ULONG                count = NETMF_CRYPTOKI_MAX_SLOTS;
    CLR_RT_HeapBlock_Array* pSlots;
    CLR_RT_HeapBlock        ref;
    CLR_RT_HeapBlock*       pSlotRef;

    CRYPTOKI_CHECK_RESULT(stack, C_GetSlotList(CK_FALSE, slots, &count));

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(ref, (CLR_UINT32)count, g_CLR_RT_WellKnownTypes.m_CryptokiSlot));

    pSlots = ref.DereferenceArray();

    pSlotRef = (CLR_RT_HeapBlock*)pSlots->GetFirstElement();
            
    for(i=0; i<count; i++)
    {
        CLR_RT_HeapBlock* pSlot;
        
        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( pSlotRef[i], g_CLR_RT_WellKnownTypes.m_CryptokiSlot ));

        pSlot = pSlotRef[i].Dereference();
      
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotIndex    ].SetInteger        ((CLR_INT32)slots[i]);
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_disposed     ].SetBoolean        (false              );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_evtDispatcher].SetObjectReference(NULL               );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotEvent    ].SetObjectReference(NULL               );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotInfo     ].SetObjectReference(NULL               );
    }

    stack.SetResult_Object(pSlots);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::SignInit___VOID__MicrosoftSPOTCryptokiSession__MicrosoftSPOTCryptokiMechanism__SystemSecurityCryptographyCryptoKey( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pSession   = stack.Arg1().Dereference();
    CLR_RT_HeapBlock*       pMech      = stack.Arg2().Dereference();
    CLR_RT_HeapBlock*       pKeyHandle = stack.Arg3().Dereference();
    CLR_RT_HeapBlock_Array* pArrParam;
    CK_MECHANISM            mech;
    CK_OBJECT_HANDLE        hKey;
    CK_SESSION_HANDLE       hSession;

    FAULT_ON_NULL_ARG(pSession);
    FAULT_ON_NULL_ARG(pMech);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    mech.mechanism = pMech[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Mechanism::FIELD__Type].NumericByRef().u4;

    pArrParam = pMech[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Mechanism::FIELD__Parameter].DereferenceArray();

    if(pArrParam != NULL && pArrParam->m_numOfElements > 0)
    {
        mech.pParameter     = pArrParam->GetFirstElement();
        mech.ulParameterLen = pArrParam->m_numOfElements;
    }
    else
    {
        mech.pParameter = NULL;
        mech.ulParameterLen = 0;
    }

    hKey = (CK_OBJECT_HANDLE)pKeyHandle[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiObject::FIELD__m_handle].NumericByRef().s4;

    CRYPTOKI_CHECK_RESULT(stack, C_SignInit(hSession, &mech, hKey));

    TINYCLR_NOCLEANUP();
}
Exemplo n.º 15
0
HRESULT FAT_FINDFILES::FindNext( FS_FILEINFO *fi, BOOL *fileFound )
{
    TINYCLR_HEADER();
    
    FAT_FILE fileInfo;
    FAT_Directory* dirEntry;
    
    if(!fi || !fileFound)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_FILE_IO);
    }

    do
    {
        if(fileInfo.Parse( m_logicDisk, &m_entryEnum ) != S_OK)
        {
            *fileFound = FALSE;
            TINYCLR_SET_AND_LEAVE(S_OK);
        }
        
        dirEntry = fileInfo.GetDirectoryEntry();

        if(!dirEntry) TINYCLR_SET_AND_LEAVE(CLR_E_FILE_IO);
    }
    while(dirEntry->DIR_Name[0] == '.' || dirEntry->DIR_Attr == ATTR_VOLUME_ID); // Skip the "." and ".." and the Volume_ID entries

    fi->Attributes     = dirEntry->DIR_Attr;
    fi->Size           = dirEntry->Get_DIR_FileSize();
    fi->CreationTime   = FAT_Utility::FATTimeToTicks( dirEntry->Get_DIR_CrtDate()   , dirEntry->Get_DIR_CrtTime(), dirEntry->DIR_CrtTimeTenth );
    fi->LastAccessTime = FAT_Utility::FATTimeToTicks( dirEntry->Get_DIR_LstAccDate(), 0                    , 0                          );
    fi->LastWriteTime  = FAT_Utility::FATTimeToTicks( dirEntry->Get_DIR_WrtDate()   , dirEntry->Get_DIR_WrtTime(), 0                          );

    TINYCLR_CHECK_HRESULT(fileInfo.CopyFileName( (LPWSTR)(fi->FileName), fi->FileNameSize ));

    *fileFound = TRUE;

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Threading_Monitor::Enter___STATIC__VOID__OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_StackFrame* caller = NULL;
        
    if(stack.Arg0().Dereference() == NULL)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_ARGUMENT_NULL);
    }
    
    switch(stack.m_customState)
    {
    case 0:
        {
            caller = stack.Caller(); FAULT_ON_NULL(caller); // We need to set the constraint on the caller, not on us...

            hr = g_CLR_RT_ExecutionEngine.LockObject( stack.Arg0(), caller->m_owningSubThread, TIMEOUT_INFINITE, false );
            if(hr == CLR_E_THREAD_WAITING)
            {
                stack.m_customState = 1;
            }

            TINYCLR_LEAVE();
        }
        break;

    case 1:
        TINYCLR_SET_AND_LEAVE(S_OK);
        break;

    default:
        TINYCLR_SET_AND_LEAVE(CLR_E_FAIL);
    }

    TINYCLR_NOCLEANUP();
}
static HRESULT Microsoft_SPOT_Hardware_InterruptPort_EnableInterrupt( CLR_RT_HeapBlock* pThis, CLR_RT_HeapBlock_NativeEventDispatcher* pIOPort )
{    
    TINYCLR_HEADER();
    {
        CLR_INT32 &flags              = pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_flags              ].NumericByRef().s4;
        CLR_UINT32  portId            = pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_portId             ].NumericByRef().u4;
        CLR_INT32  glitchFilterEnable = pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_glitchFilterEnable ].NumericByRef().s4;
        
        GPIO_RESISTOR  resistorMode  = (GPIO_RESISTOR)pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_resistorMode      ].NumericByRef().s4;
        GPIO_INT_EDGE  interruptMode = (GPIO_INT_EDGE)pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_interruptMode     ].NumericByRef().s4;
        
        
        if(flags & GPIO_PortParams::c_Disposed)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_PIN_DEAD);
        }

        if(flags & GPIO_PortParams::c_InterruptDisabled)
        {   
            flags &= ~GPIO_PortParams::c_InterruptDisabled;

            if(
                !::CPU_GPIO_EnableInputPin2(
                                             portId,
                                             glitchFilterEnable,
                                             Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::IsrProcedure,
                                             pIOPort,
                                             interruptMode,
                                             resistorMode
                                           )
              )
            {
                TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
            }
        }
    }
    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_NativeEventDispatcher::Dispose___VOID__BOOLEAN( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
        
    CLR_RT_HeapBlock_NativeEventDispatcher *pNativeDisp = NULL;
    
    CLR_RT_HeapBlock*  pThis = stack.This();  FAULT_ON_NULL(pThis);
    
    TINYCLR_CHECK_HRESULT(GetEventDispatcher( stack, pNativeDisp ));
    
    // Cleanup the HAL queue from the instance of assiciated CLR_RT_HeapBlock_NativeEventDispatcher 
    pNativeDisp->RemoveFromHALQueue();
    
    // Calls driver to enable interrupts.  Consider that there could be no driver 
    // associated to this object so check that the driver methods are set 
    // we will be tolerant in this case and not throw any exception
    if(pNativeDisp->m_DriverMethods != NULL)
    {
        TINYCLR_CHECK_HRESULT(pNativeDisp->m_DriverMethods->m_CleanupProc( pNativeDisp )); 
    }
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Math::Round___STATIC__R8__R8( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    double d = stack.Arg0().NumericByRefConst().r8;
    double hi = d + 0.5;
    double res = System::Math::Floor( hi );

    //If the number was in the middle of two integers, we need to round to the even one.
    if(res==hi)
    {
        if(System::Math::Fmod( res, 2.0 ) != 0)
        {
            //Rounding up made the number odd so we should round down.
            res -= 1.0;
        }
    }

    stack.SetResult_R8( res );

    TINYCLR_NOCLEANUP_NOLABEL();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_HardwareProvider::NativeIsSupportedBaudRate___BOOLEAN__I4__BYREF_U4( CLR_RT_StackFrame& stack )
{
	TINYCLR_HEADER();
    CLR_RT_HeapBlock  hbBR;
    CLR_UINT32        port;
   
    CLR_UINT32        SupportedBaudrate;

    port              = stack.Arg1().NumericByRef().u4;

    TINYCLR_CHECK_HRESULT(hbBR.LoadFromReference( stack.Arg2() ));
    SupportedBaudrate = hbBR.NumericByRef().u4;

    if (CPU_USART_IsBaudrateSupported(port,SupportedBaudrate) == TRUE)
        stack.SetResult_Boolean( true );
    else
        stack.SetResult_Boolean( false );

    hbBR.SetInteger( (CLR_UINT32)SupportedBaudrate );
    TINYCLR_CHECK_HRESULT(hbBR.StoreToReference( stack.Arg2(), 0 ));        

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_native_Microsoft_SPOT_Reflection::GetAssemblyFromHash___STATIC__mscorlibSystemReflectionAssembly__U4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock& top  = stack.PushValueAndClear();
    CLR_UINT32        hash = stack.Arg0().NumericByRefConst().u4;

    TINYCLR_FOREACH_ASSEMBLY_IN_CURRENT_APPDOMAIN(g_CLR_RT_TypeSystem)
    {
        if(pASSM->ComputeAssemblyHash() == hash)
        {
            CLR_RT_Assembly_Index idx; idx.Set( pASSM->m_idx );

            top.SetReflection( idx );

            TINYCLR_SET_AND_LEAVE(S_OK);
        }
    }
    TINYCLR_FOREACH_ASSEMBLY_IN_CURRENT_APPDOMAIN_END();

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiDigest::DigestKeyInternal___VOID__SystemSecurityCryptographyCryptoKey( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* pThis      = stack.This();
    CLR_RT_HeapBlock* phKey      = stack.Arg1().Dereference(); 
    CLR_RT_HeapBlock* pSession   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE hSession;
    CK_OBJECT_HANDLE  hKey;

    FAULT_ON_NULL_ARG(phKey);
    FAULT_ON_NULL(pSession);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    hKey = (CK_OBJECT_HANDLE)phKey[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiObject::FIELD__m_handle].NumericByRef().s4;

    CRYPTOKI_CHECK_RESULT(stack, C_DigestKey(hSession, hKey));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_windows_devices_native_Windows_Devices_Gpio_GpioPin::Write___VOID__WindowsDevicesGpioGpioPinValue( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    BOOL state;
    CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

    if (pThis[ FIELD__m_disposed ].NumericByRef().s1 != 0)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
    }

    pThis[ FIELD__m_lastOutputValue ].NumericByRef().s4 = stack.Arg1().NumericByRef().s4;
    state = (stack.Arg1().NumericByRef().s4 != GpioPinValue_Low);

    // If the current drive mode is set to output, write the value to the pin.
    if (pThis[ FIELD__m_driveMode ].NumericByRefConst().s4 == GpioPinDriveMode_Output)
    {
        ::CPU_GPIO_SetPinState( pThis[ FIELD__m_pinNumber ].NumericByRefConst().u4, state );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_windows_devices_native_Windows_Devices_Gpio_GpioPin::set_DebounceTimeout___VOID__mscorlibSystemTimeSpan( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    GPIO_PIN pin;
    CLR_INT64 value;
    CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

    if (pThis[ FIELD__m_disposed ].NumericByRef().s1 != 0)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);
    }

    value = (CLR_INT64_TEMP_CAST) stack.Arg1().NumericByRef().s8 / TIME_CONVERSION__TO_MILLISECONDS;
    pin = (GPIO_PIN)( pThis[ FIELD__m_pinNumber ].NumericByRefConst().u4 );
    if (!::CPU_GPIO_SetPinDebounce( pin, value ))
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
    }
    TINYCLR_SET_AND_LEAVE(stack.NotImplementedStub());

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_I2CDevice__Configuration::GetInitialConfig(CLR_RT_HeapBlock& ref, I2C_USER_CONFIGURATION& nativeConfig)
{
    NATIVE_PROFILE_CLR_HARDWARE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* config = ref.Dereference();
    FAULT_ON_NULL(config);

    nativeConfig.Address   = config[ FIELD__Address      ].NumericByRef().u2;
    nativeConfig.ClockRate = config[ FIELD__ClockRateKhz ].NumericByRef().s4;

    if(nativeConfig.Address > c_MaxI2cAddress)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
    }

    if(nativeConfig.ClockRate < c_MimimumClockRateKhz || c_MaximumClockRateKhz < nativeConfig.ClockRate)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER)
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiVerify::VerifyUpdateInternal___VOID__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis    = stack.This();
    CLR_RT_HeapBlock_Array* pData    = stack.Arg1().DereferenceArray(); 
    CLR_INT32               offset   = stack.Arg2().NumericByRef().s4;
    CLR_INT32               len      = stack.Arg3().NumericByRef().s4;
    CLR_RT_HeapBlock*       pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE       hSession;

    FAULT_ON_NULL_ARG(pData);

    if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    CRYPTOKI_CHECK_RESULT(stack, C_VerifyUpdate(hSession, pData->GetElement(offset), len));

    TINYCLR_NOCLEANUP();
}
 HRESULT CLR_RT_HeapBlock_Timer::CreateInstance( CLR_UINT32 flags, CLR_RT_HeapBlock& owner, CLR_RT_HeapBlock& tmRef )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Timer* timer = NULL;

    //
    // Create a request and stop the calling thread.
    //
    timer = EVENTCACHE_EXTRACT_NODE(g_CLR_RT_EventCache,CLR_RT_HeapBlock_Timer,DATATYPE_TIMER_HEAD); CHECK_ALLOCATION(timer);

    {
        CLR_RT_ProtectFromGC gc( *timer );

        timer->Initialize();

        timer->m_flags               = flags;
        timer->m_timeExpire          = TIMEOUT_INFINITE;
        timer->m_timeFrequency       = TIMEOUT_INFINITE;
        timer->m_timeLastExpiration  = 0;
        timer->m_ticksLastExpiration = 0;

        g_CLR_RT_ExecutionEngine.m_timers.LinkAtBack( timer );

        TINYCLR_SET_AND_LEAVE(CLR_RT_ObjectToEvent_Source::CreateInstance( timer, owner, tmRef ));
    }

    TINYCLR_CLEANUP();

    if(FAILED(hr))
    {
        if(timer) timer->ReleaseWhenDead();
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::BindConnectHelper( CLR_RT_StackFrame& stack, bool fBind )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* socket = stack.Arg0().Dereference();
    CLR_INT32 handle;
    SOCK_sockaddr addr;
    CLR_UINT32 addrLen = sizeof(addr);
    CLR_INT32 ret;
    bool fThrowOnWouldBlock = false;

    FAULT_ON_NULL(socket);
    handle = socket[ FIELD__m_Handle ].NumericByRef().s4;

    TINYCLR_CHECK_HRESULT(MarshalSockAddress( &addr, addrLen, stack.Arg1() ));

    if(fBind)
    {
        ret = SOCK_bind( handle, &addr, addrLen );
    }
    else
    {
        ret = SOCK_connect( handle, &addr, addrLen );
        
        fThrowOnWouldBlock = (stack.Arg2().NumericByRefConst().s4 != 0);
        
        if(!fThrowOnWouldBlock && SOCK_getlasterror() == SOCK_EWOULDBLOCK)
        {
            TINYCLR_SET_AND_LEAVE(S_OK);            
        }
    }

    TINYCLR_CHECK_HRESULT(ThrowOnError( stack, ret ));

    TINYCLR_NOCLEANUP();
}
static HRESULT Microsoft_SPOT_Hardware_InterruptPort_DisableInterrupt( CLR_RT_HeapBlock* pThis, CLR_RT_HeapBlock_NativeEventDispatcher* pIOPort )

{
    NATIVE_PROFILE_CLR_HARDWARE();
    TINYCLR_HEADER();
    {
        CLR_INT32 &flags              = pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_flags             ].NumericByRef().s4;
        CLR_UINT32  portId            = pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_portId            ].NumericByRef().u4;
        
        GPIO_RESISTOR  resistorMode   =(GPIO_RESISTOR)pThis[ Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::FIELD__m_resistorMode      ].NumericByRef().s4;
        
        if(flags & GPIO_PortParams::c_Disposed)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_PIN_DEAD);
        }

        if(( flags & GPIO_PortParams::c_InterruptDisabled ) == 0)
        {
            flags |= GPIO_PortParams::c_InterruptDisabled;

            if(
                !::CPU_GPIO_EnableInputPin2(
                                             portId,
                                             false, 
                                             NULL,
                                             0,
                                             GPIO_INT_NONE,
                                             resistorMode
                                           )
              )
            {
                TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
            }
        }
    }
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Object::GetType___SystemType( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_TypeDescriptor desc;
    CLR_RT_ReflectionDef_Index idx;
    CLR_RT_HeapBlock& arg0 = stack.Arg0();
    CLR_RT_HeapBlock* pObj;

    TINYCLR_CHECK_HRESULT(desc.InitializeFromObject( arg0 ));

    pObj = arg0.Dereference();

    if(pObj && pObj->DataType() == DATATYPE_REFLECTION)
    {
        idx.m_kind               = REFLECTION_TYPE;
        idx.m_levels             = 0;
        idx.m_data.m_type.m_data = desc.m_handlerCls.m_data;
    }
    else
    {
        idx = desc.m_reflex;
    }

    {
        CLR_RT_HeapBlock& top = stack.PushValue();
        CLR_RT_HeapBlock* hbObj;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex(top, g_CLR_RT_WellKnownTypes.m_TypeStatic));
        
        hbObj = top.Dereference();
        hbObj->SetReflection( idx );
    }

    TINYCLR_NOCLEANUP();
}