• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2024, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the temporary storage module for saving, restoring and deleting the temporary key-value pairs.
32  *   All temporary key-value pairs will be deleted after the system reboots.
33  */
34 
35 #include "openthread-posix-config.h"
36 #include "platform-posix.h"
37 
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifdef __linux__
42 #include <sys/sysinfo.h>
43 #else
44 #include <sys/sysctl.h>
45 #endif
46 #include <time.h>
47 
48 #include "tmp_storage.hpp"
49 #include "common/code_utils.hpp"
50 #include "common/debug.hpp"
51 #include "common/encoding.hpp"
52 
53 #if OPENTHREAD_POSIX_CONFIG_TMP_STORAGE_ENABLE
54 namespace ot {
55 namespace Posix {
56 
Init(void)57 void TmpStorage::Init(void)
58 {
59     otError  error = OT_ERROR_NONE;
60     time_t   storedBootTime;
61     time_t   currentBootTime;
62     uint16_t valueLength = sizeof(time_t);
63 
64     VerifyOrDie(SettingsFileInit() == OT_ERROR_NONE, OT_EXIT_FAILURE);
65 
66     currentBootTime = GetBootTime();
67 
68     error = mStorageFile.Get(kKeyBootTime, 0, reinterpret_cast<uint8_t *>(&storedBootTime), &valueLength);
69 
70     // If failed to get the boot time or the current boot time doesn't match the stored boot time, it means
71     // the system has been rebooted.
72     if ((error != OT_ERROR_NONE) || (valueLength != sizeof(time_t)) || !BootTimeMatch(storedBootTime, currentBootTime))
73     {
74         mStorageFile.Wipe();
75         mStorageFile.Set(kKeyBootTime, reinterpret_cast<const uint8_t *>(&currentBootTime),
76                          static_cast<uint16_t>(sizeof(currentBootTime)));
77     }
78 }
79 
Deinit(void)80 void TmpStorage::Deinit(void) { mStorageFile.Deinit(); }
81 
SaveRadioSpinelMetrics(const otRadioSpinelMetrics & aMetrics)82 void TmpStorage::SaveRadioSpinelMetrics(const otRadioSpinelMetrics &aMetrics)
83 {
84     mStorageFile.Set(kKeyRadioSpinelMetrics, reinterpret_cast<const uint8_t *>(&aMetrics),
85                      static_cast<uint16_t>(sizeof(aMetrics)));
86 }
87 
RestoreRadioSpinelMetrics(otRadioSpinelMetrics & aMetrics)88 otError TmpStorage::RestoreRadioSpinelMetrics(otRadioSpinelMetrics &aMetrics)
89 {
90     uint16_t valueLength = sizeof(aMetrics);
91 
92     return mStorageFile.Get(kKeyRadioSpinelMetrics, 0, reinterpret_cast<uint8_t *>(&aMetrics), &valueLength);
93 }
94 
SettingsFileInit(void)95 otError TmpStorage::SettingsFileInit(void)
96 {
97     static constexpr size_t kMaxFileBaseNameSize = 32;
98     char                    fileBaseName[kMaxFileBaseNameSize];
99     const char             *offset = getenv("PORT_OFFSET");
100     uint64_t                eui64;
101 
102     otPlatRadioGetIeeeEui64(gInstance, reinterpret_cast<uint8_t *>(&eui64));
103     eui64 = ot::BigEndian::HostSwap64(eui64);
104 
105     snprintf(fileBaseName, sizeof(fileBaseName), "%s_%" PRIx64 "-tmp", ((offset == nullptr) ? "0" : offset), eui64);
106     VerifyOrDie(strlen(fileBaseName) < kMaxFileBaseNameSize, OT_EXIT_FAILURE);
107 
108     return mStorageFile.Init(fileBaseName);
109 }
110 
GetBootTime(void)111 time_t TmpStorage::GetBootTime(void)
112 {
113     time_t curTime;
114     time_t upTime;
115 
116 #ifdef __linux__
117     struct sysinfo info;
118     VerifyOrDie(sysinfo(&info) == 0, OT_EXIT_ERROR_ERRNO);
119     upTime = info.uptime;
120 #else
121     {
122         int            mib[] = {CTL_KERN, KERN_BOOTTIME};
123         struct timeval boottime;
124         size_t         size = sizeof(boottime);
125 
126         VerifyOrDie(sysctl(mib, OT_ARRAY_LENGTH(mib), &boottime, &size, NULL, 0) == 0, OT_EXIT_ERROR_ERRNO);
127         upTime = boottime.tv_sec;
128     }
129 #endif
130 
131     VerifyOrDie(time(&curTime) != -1, OT_EXIT_ERROR_ERRNO);
132 
133     return (curTime - upTime);
134 }
135 
BootTimeMatch(time_t aBootTimeA,time_t aBootTimeB)136 bool TmpStorage::BootTimeMatch(time_t aBootTimeA, time_t aBootTimeB)
137 {
138     time_t diff = (aBootTimeA > aBootTimeB) ? (aBootTimeA - aBootTimeB) : (aBootTimeB - aBootTimeA);
139 
140     // The uptime and the current time are not strictly synchronized. The calculated boot time has 1 second of jitter.
141     return diff < 2;
142 }
143 
144 } // namespace Posix
145 } // namespace ot
146 #endif // OPENTHREAD_POSIX_CONFIG_TMP_STORAGE_ENABLE
147