示例#1
0
            internal X509Certificate Get(string certName)
            {
                int idx = m_names.IndexOf(certName);

                if (-1 == idx)
                {
                    return(null);
                }

                ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(_CertStore_), (uint)m_values[idx]);

                if (ewr == null)
                {
                    return(null);
                }

                X509Certificate cert = ewr.Target as X509Certificate;

                if (cert.Issuer == null || cert.Issuer.Length == 0)
                {
                    cert.Initialize();
                }

                ewr.PushBackIntoRecoverList();

                return(cert);
            }
示例#2
0
        /// <summary>
        /// Load address mapping from persistent storage
        /// </summary>
        public void Load()
        {
#if MICROFRAMEWORK
            ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(Addresses), 0);
            if (ewr != null)
            {
                Addresses addr = ewr.Target as Addresses;
                if (addr != null)
                {
                    _addresses = addr;
                }
            }
#endif
        }
示例#3
0
            private uint GetNextFreeIndex()
            {
                uint idx = 0;

                if (m_values.Count > 0)
                {
                    idx = (uint)m_values[m_values.Count - 1] + 1;

                    // overflow - collapse indexes
                    if (idx == 0)
                    {
                        for (int i = 0; i < m_values.Count; i++)
                        {
                            // if the current index matches the lookup value then we are already in the correct position
                            if (idx == (uint)m_values[i])
                            {
                                idx++;
                            }
                            else
                            {
                                // collapse cert indexes
                                ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(_CertStore_), (uint)m_values[i]);

                                if (ewr != null)
                                {
                                    X509Certificate cert = ewr.Target as X509Certificate;
                                    ewr.Target = null;

                                    ewr          = new ExtendedWeakReference(cert, typeof(_CertStore_), idx, ExtendedWeakReference.c_SurvivePowerdown);
                                    ewr.Priority = (int)ExtendedWeakReference.PriorityLevel.Critical;
                                    ewr.Target   = cert;
                                    ewr.PushBackIntoRecoverList();

                                    m_values[i] = (uint)idx;

                                    // increment active index count only if we have a valid EWR certificate
                                    idx++;
                                }
                            }
                        }
                    }
                }

                return(idx);
            }
示例#4
0
        private static bool ValidateEWR(bool NewData)
        {
            bool success = true;

            for (uint i = 1; i < 10; i++)
            {
                ewrs[i] = ExtendedWeakReference.Recover(typeof(TestObj), i);
                if (ewrs[i] == null)
                {
                    success = false;
                    Log.Exception("Unable to recover object #" + i + " after reboot " + runTracker.RebootCount);
                    ewrs[i] = ExtendedWeakReference.RecoverOrCreate(typeof(TestObj), i,
                                                                    ExtendedWeakReference.c_SurviveBoot |
                                                                    ExtendedWeakReference.c_SurvivePowerdown);
                }
                TestObj obj = (TestObj)ewrs[i].Target;
                if (obj == null)
                {
                    success = false;
                    Log.Exception("Recovered object #" + i + " null after reboot " + runTracker.RebootCount);
                }
                if (runTracker.objHashes[i] != obj.GetHashCode())
                {
                    success = false;
                    Log.Exception("Recovered object #" + i + " hash value does not match stored hash value");
                }
                Log.Comment("Verified object #" + i);
                if (NewData)
                {
                    Log.Comment("Creating new object and setting back to ewr");
                    obj                     = new TestObj();
                    ewrs[i].Target          = obj;
                    runTracker.objHashes[i] = obj.GetHashCode();
                }
                else
                {
                    Log.Comment("Pushing object back into list");
                    ewrs[i].PushBackIntoRecoverList();
                }
            }
            return(success);
        }
示例#5
0
        private void On()
        {
            lock (_hal)
            {
                _hal._powerPort.Write(true);
                Thread.Sleep(200);

                SWReset();
                InitRegisters();
                StartOsc();
                EnableInterrupts();
                Channel    = _channel;
                Power      = _rfPower;
                _rxEnabled = false;

                // CC2420 ext address is a random value. Recover/generate ext address from flash if needed
                {
                    CC2420Address addr = null;
                    // try to recover address
                    ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(CC2420Address), 0);
                    if (addr != null)
                    {
                        // address recovered from flash
                        _hal.ExtendedAddress = addr.extAddress;
                    }
                    else
                    {
                        // save current address in flash
                        ewr = ExtendedWeakReference.RecoverOrCreate(typeof(CC2420Address), 0, ExtendedWeakReference.c_SurvivePowerdown);
                        if (ewr != null)
                        {
                            addr            = new CC2420Address();
                            addr.extAddress = _hal.ExtendedAddress;
                            ewr.Priority    = (int)ExtendedWeakReference.PriorityLevel.System;
                            ewr.Target      = addr;
                            ewr.PushBackIntoRecoverList();
                        }
                    }
                }
            }
        }
示例#6
0
            internal bool Update(string certName, X509Certificate cert)
            {
                int idx = m_names.IndexOf(certName);

                if (-1 == idx)
                {
                    return(false);
                }

                ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(_CertStore_), (uint)m_values[idx]);

                if (ewr == null)
                {
                    return(false);
                }

                ewr.Target = cert;
                ewr.PushBackIntoRecoverList();

                return(true);
            }
示例#7
0
            internal bool Remove(string certName)
            {
                int idx = m_names.IndexOf(certName);

                if (idx == -1)
                {
                    return(false);
                }

                ExtendedWeakReference ewr = ExtendedWeakReference.Recover(typeof(_CertStore_), (uint)m_values[idx]);

                if (ewr != null)
                {
                    m_names.RemoveAt(idx);
                    m_values.RemoveAt(idx);

                    ewr.Target = null;
                }

                return(true);
            }
示例#8
0
        public static void Main()
        {
            // This test suite does not use the Test Runner infrastructure as it requires a
            // reboot of the physical device to verify the references have been saved.  It relies
            // on the test harness to time it out if it fails to complete because of issues with
            // EWR persistence.

            // Skip test on Emulator
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
            {
                StartTestLog("Emulator, skipping...");
                EndTestLog(0, 0, 1);
            }

            else
            {
                ewrs[0] = ExtendedWeakReference.Recover(typeof(TestRunTracker), 0);
                if (ewrs[0] == null)
                {
                    ewrs[0] = ExtendedWeakReference.RecoverOrCreate(
                        typeof(TestRunTracker),
                        0,
                        ExtendedWeakReference.c_SurviveBoot | ExtendedWeakReference.c_SurvivePowerdown);
                    runTracker = new TestRunTracker()
                    {
                        RebootCount = -1, TestState = TestRunTracker.State.Initialize
                    };
                }
                else
                {
                    runTracker = (TestRunTracker)ewrs[0].Target;
                }

                Log.Comment("State: " + runTracker.TestState + ", Reboot Count: " + runTracker.RebootCount);

                switch (runTracker.TestState)
                {
                // Initial state
                case TestRunTracker.State.Initialize:
                    if (runTracker.RebootCount == -1)
                    {
                        StartTestLog("No EWR found, initializing for first run...");
                    }
                    else
                    {
                        StartTestLog("Previous EWR found.  Re-initializing...");
                    }

                    runTracker.TestState   = TestRunTracker.State.HardReboot;
                    runTracker.RebootCount = 0;
                    runTracker.TestState   = TestRunTracker.State.HardReboot;
                    for (uint i = 1; i < 10; i++)
                    {
                        ewrs[i] = ExtendedWeakReference.RecoverOrCreate(typeof(TestObj), i,
                                                                        ExtendedWeakReference.c_SurvivePowerdown |
                                                                        ExtendedWeakReference.c_SurviveBoot);
                        TestObj obj = new TestObj();
                        ewrs[i].Target          = obj;
                        runTracker.objHashes[i] = obj.GetHashCode();
                    }
                    break;

                // HardReboot cases - Reboots 1 - 3
                case TestRunTracker.State.HardReboot:
                    Debug.Print("<TestMethod name=\"HardReboot" + runTracker.RebootCount + "\">");

                    // Validate objects
                    if (ValidateEWR(true))
                    {
                        Debug.Print("    <TestMethodResult Result=\"Pass\">");
                        runTracker.PassCount++;
                    }
                    else
                    {
                        Debug.Print("    <TestMethodResult Result=\"Fail\">");
                        runTracker.FailCount++;
                    }
                    Debug.Print("        <Text><![CDATA[TEST: HardReboot" + runTracker.RebootCount + "]]></Text>" + TimeStamp());
                    Debug.Print("    </TestMethodResult>");
                    Debug.Print("</TestMethod>");

                    // End test
                    if (runTracker.RebootCount >= 3)
                    {
                        runTracker.TestState = TestRunTracker.State.SoftReboot;
                    }

                    break;

                // SoftReboot cases - Reboots 4 - 6
                case TestRunTracker.State.SoftReboot:
                    Debug.Print("<TestMethod name=\"SoftReboot" + runTracker.RebootCount + "\">");

                    // Validate objects
                    if (ValidateEWR(true))
                    {
                        Debug.Print("    <TestMethodResult Result=\"Pass\">");
                        runTracker.PassCount++;
                    }
                    else
                    {
                        Debug.Print("    <TestMethodResult Result=\"Fail\">");
                        runTracker.FailCount++;
                    }
                    Debug.Print("        <Text><![CDATA[TEST: SoftReboot" + runTracker.RebootCount + "]]></Text>" + TimeStamp());
                    Debug.Print("    </TestMethodResult>");
                    Debug.Print("</TestMethod>");

                    // End test
                    if (runTracker.RebootCount >= 6)
                    {
                        runTracker.TestState = TestRunTracker.State.Restore;
                    }
                    break;

                // Restore cases - Reboots 7 - 8
                case TestRunTracker.State.Restore:
                    Debug.Print("<TestMethod name=\"Restore" + runTracker.RebootCount + "\">");
                    if (runTracker.RebootCount == 7)
                    {
                        Log.Comment("Restore and Pushback with Soft Reboot");
                    }
                    else
                    {
                        Log.Comment("Restore and Pushback with Hard Reboot");
                    }

                    // Validate objects
                    if (ValidateEWR(false))
                    {
                        Debug.Print("    <TestMethodResult Result=\"Pass\">");
                        runTracker.PassCount++;
                    }
                    else
                    {
                        Debug.Print("    <TestMethodResult Result=\"Fail\">");
                        runTracker.FailCount++;
                    }
                    Debug.Print("        <Text><![CDATA[TEST: SoftReboot" + runTracker.RebootCount + "]]></Text>" + TimeStamp());
                    Debug.Print("    </TestMethodResult>");
                    Debug.Print("</TestMethod>");

                    if (runTracker.RebootCount > 8)
                    {
                        runTracker.TestState = TestRunTracker.State.Complete;
                    }
                    break;

                // Tests complete
                case TestRunTracker.State.Complete:
                    runTracker.TestState = TestRunTracker.State.Initialize;
                    // Close logs
                    EndTestLog(runTracker.PassCount, runTracker.FailCount, 0);
                    break;
                }
                runTracker.RebootCount++;
                ewrs[0].Target = runTracker;
                // Need to sleep to make sure debug buffer has chance to flush before rebooting
                // This should not be needed for EWR any longer
                System.Threading.Thread.Sleep(15000);

                switch (runTracker.TestState)
                {
                case TestRunTracker.State.HardReboot:
                    PowerState.RebootDevice(false);
                    break;

                case TestRunTracker.State.SoftReboot:
                    PowerState.RebootDevice(true);
                    break;

                case TestRunTracker.State.Restore:
                    if (runTracker.RebootCount == 8)
                    {
                        PowerState.RebootDevice(true);
                    }
                    else
                    {
                        PowerState.RebootDevice(false);
                    }
                    break;
                }
                // fall through to end test suite
            }
        }