/// <summary> /// The execution entry point. /// </summary> public static void Run() { // We are booting; try to retrieve the boot count. s_numBootsExtendedWeakReference = ExtendedWeakReference.RecoverOrCreate( typeof(TypeUniqueToOurApp), // The unique type that identifies the data. 0, // The ID of the specific data item. ExtendedWeakReference.c_SurviveBoot); // The CLR should try to persist data across a reboot. // Indicate how important this data is. The CLR discards // OkayToThrowAway items first, then NiceToHave items, then // Important items, then Critical items, and finally System items. // In practice, System items are virtually never discarded. s_numBootsExtendedWeakReference.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important; // Try to get the persisted data, initializing it if it is not // available. The Target property of the extended weak reference // must be cast to the actual type of the object. The Target // property is cast to prevent the object from being // garbage-collected unexpectedly. NumBoots numBoots = (NumBoots)s_numBootsExtendedWeakReference.Target; if (numBoots == null) { // The object could not be found in flash memory, so create the // object and initialize it. Debug.Print( "The device was booted for the first time, or the boot counter was lost. Initializing the boot counter to 1."); numBoots = new NumBoots(1); } else { // The object was found in flash memory; increment the boot // counter. numBoots = new NumBoots(numBoots.BootCount + 1); Debug.Print("Successfully read boot counter. This is boot #" + numBoots.BootCount); } // Set the Target property of the extended weak reference to the // boot count object, triggering persistence. s_numBootsExtendedWeakReference.Target = numBoots; // The CLR needs some time to asynchronously store the data in // flash. In this sample application, we use a two-second Sleep. // A real application would simply continue without delay. This app // only needs to sleep because the application is about to end. // The system provides no guarantee about when the data will // actually be stored in flash memory. When the data is actually // stored depends on the size of the object and the speed of the // flash memory. // System.Threading.Thread.Sleep(2000); }
public static object LoadFromFlash(Type dataType, uint id) { ewr = ExtendedWeakReference.RecoverOrCreate(dataType, id, ExtendedWeakReference.c_SurvivePowerdown | ExtendedWeakReference.c_SurviveBoot); // Indicate how important this data is. The CLR discards from first to last: // OkayToThrowAway items // NiceToHave items // Important items // Critical items // System items. // In practice, System items are virtually never discarded. ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.System; return ewr.Target; }
public static Options LoadFromFlash(uint id) { ewr = ExtendedWeakReference.RecoverOrCreate(typeof(string), id, ExtendedWeakReference.c_SurvivePowerdown | ExtendedWeakReference.c_SurviveBoot); // Indicate how important this data is. The CLR discards from first to last: // OkayToThrowAway items // NiceToHave items // Important items // Critical items // System items. // In practice, System items are virtually never discarded. ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.System; Options res = null; if (ewr.Target != null) res = Options.FromXml((string)ewr.Target); return res ?? new Options(); }
private TetrisApp() { // Create the object that configures the GPIO pins to buttons. GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null); // Create ExtendedWeakReference for high score table highScoreEWD = ExtendedWeakReference.RecoverOrCreate( typeof(TetrisApp), 0, ExtendedWeakReference.c_SurvivePowerdown); // Set persistance priority highScoreEWD.Priority = (int)ExtendedWeakReference.PriorityLevel.Important; // Try to recover previously saved HighScore HighScore = (HighScoreTable)highScoreEWD.Target; // If nothing was recovered - create new if (HighScore == null) HighScore = new HighScoreTable(); }
static ResourceUtility() { try { s_ewr = ExtendedWeakReference.RecoverOrCreate(typeof(ResourceUtility), s_idSelectorCultureInfo, ExtendedWeakReference.c_SurviveBoot | ExtendedWeakReference.c_SurvivePowerdown); s_ewr.Priority = (int)ExtendedWeakReference.PriorityLevel.System; string ciName = (string)s_ewr.Target; if (ciName != null) { CultureInfo ci = new CultureInfo(ciName); CurrentUICultureInternal = ci; } } catch { } }
public static void Main() { //try to recover the data or create the weak reference bootInfoExtendedWeakReference = ExtendedWeakReference.RecoverOrCreate( typeof(MyUniqueSelectorType), //unique type to prevent other code from accessing our data 0, //id that refers to the data we care about ExtendedWeakReference.c_SurvivePowerdown | ExtendedWeakReference.c_SurviveBoot); //set how important the data is bootInfoExtendedWeakReference.Priority = (int)ExtendedWeakReference.PriorityLevel.Important; //try to get the persisted data //if we get the data, then putting it in the field myBootInfo creates a //strong reference to it, preventing the GC from collecting it MyBootInfo myBootInfo = (MyBootInfo)bootInfoExtendedWeakReference.Target; if (myBootInfo == null) { //the data could not be obtained Debug.Print("This is the first time the device booted or the data was lost."); myBootInfo = new MyBootInfo(1); //first initialization } else { //the data could be obtained, display info Debug.Print("Number of boots = " + myBootInfo.BootCount); myBootInfo = new MyBootInfo(myBootInfo.BootCount + 1); //increment number of boots } //setting the Target property causes to write the data to flash memory bootInfoExtendedWeakReference.Target = myBootInfo; //give the system time to save it to flash System.Threading.Thread.Sleep(2000); }
/// <summary> /// Static constructor so that we can load the certificate map from EWR prior to usage. /// </summary> static CertificateStore() { s_ewrCertLookup = ExtendedWeakReference.RecoverOrCreate(typeof(_CertLookup_), (uint)0, ExtendedWeakReference.c_SurvivePowerdown); if (s_ewrCertLookup.Target == null) { s_ewrCertLookup.Priority = (int)ExtendedWeakReference.PriorityLevel.Critical; s_personalCertData = new _CertLookup_(); s_ewrCertLookup.Target = s_personalCertData; } else { s_personalCertData = s_ewrCertLookup.Target as _CertLookup_; } s_ewrCALookup = ExtendedWeakReference.RecoverOrCreate(typeof(_CertLookup_), (uint)1, ExtendedWeakReference.c_SurvivePowerdown); if (s_ewrCALookup.Target == null) { s_ewrCALookup.Priority = (int)ExtendedWeakReference.PriorityLevel.Critical; s_CACertData = new _CALookup_(); s_ewrCALookup.Target = s_CACertData; } else { s_CACertData = s_ewrCALookup.Target as _CALookup_; } }
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; }
private static void LoadCalibrationPoints() { ewrCalibrationPoints = ExtendedWeakReference.RecoverOrCreate(typeof(CalibrationPointsID), 0, ExtendedWeakReference.c_SurvivePowerdown | ExtendedWeakReference.c_SurviveBoot); ewrCalibrationPoints.Priority = (int)ExtendedWeakReference.PriorityLevel.System; calibrationPoints = (CalibrationPoints)ewrCalibrationPoints.Target; }
//--// static public ExtendedWeakReference RecoverOrCreate(Type selector, uint id, uint flags) { ExtendedWeakReference wr = ExtendedWeakReference.Recover(selector, id); return((wr != null) ? wr : new ExtendedWeakReference(null, selector, id, flags)); }
public static Layout Load(uint id) { ewr = ExtendedWeakReference.RecoverOrCreate(typeof(string), id, ExtendedWeakReference.c_SurvivePowerdown | ExtendedWeakReference.c_SurviveBoot); ewr.Priority = (int)ExtendedWeakReference.PriorityLevel.System; Layout res = null; if (ewr.Target != null) res = Layout.FromXml((string)ewr.Target); return res ?? new Layout(); }
private void LoadData() { ConfigEWR = ExtendedWeakReference.RecoverOrCreate(typeof(Config), 0, ExtendedWeakReference.c_SurviveBoot | ExtendedWeakReference.c_SurvivePowerdown); ConfigEWR.Priority = (int)ExtendedWeakReference.PriorityLevel.Critical; wifiNetworks.Clear(); if(EnableWifiSetupNetworks) APDetails.AddSetupAPs(wifiNetworks); Config config = ConfigEWR.Target as Config; if (config == null) { config = new Config(); config.UniqueID = ""; var random = new Random(); for (int i = 0; i < 20; i++) { config.UniqueID += random.Next(10).ToString(); } ConfigEWR.Target = config; Debug.Print("Created new configuration in flash"); } else { if (config.APDetails != null) for (var i = 0; i < config.APDetails.Length; i++) { wifiNetworks.Insert(i, config.APDetails[i]); Debug.Print("Loaded WiFi config for SSID " + config.APDetails[i].SSID); } } DeviceUniqueID = config.UniqueID; Debug.Print("Device unique ID is " + DeviceUniqueID); }
public static TouchCalibrationPoints Load() { _ewr = ExtendedWeakReference.RecoverOrCreate(typeof(TouchCalibrationPoints), 0, ExtendedWeakReference.c_SurvivePowerdown); _ewr.Priority = (int)ExtendedWeakReference.PriorityLevel.Important; return _ewr.Target == null ? null : new TouchCalibrationPoints((TouchCalibrationData)_ewr.Target); }
public void Initialize(Type appType) { _ewrSettings = ExtendedWeakReference.RecoverOrCreate(appType, 0, ExtendedWeakReference.c_SurvivePowerdown); _ewrSettings.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important; if (_ewrSettings.Target==null) { _currentSettings = new SettingsEntity() { ConfigUrl = "https://vauthscus.blob.core.windows.net/deviceconfig/", NodeId = "85331753-a1ef-4aaa-bfa1-4fa00343bf49", HaveConfigId = "", WifiSSID = "cloudgate", WifiPassword = "******", // because we don't have onboarding figured out yet GatewayUrl = "", BootCount = 0, DroppedMessages = 0 }; CommitChanges(); } else { _currentSettings = (SettingsEntity)_ewrSettings.Target; } }