v2.0.0
Loading...
Searching...
No Matches
fiff_id.cpp
Go to the documentation of this file.
1//=============================================================================================================
36
37//=============================================================================================================
38// INCLUDES
39//=============================================================================================================
40
41#include "fiff_id.h"
42#include "fiff_file.h"
43
44#include <QNetworkInterface>
45#include <QDateTime>
46
47#include <ctime>
48#include <QDebug>
49
50//=============================================================================================================
51// USED NAMESPACES
52//=============================================================================================================
53
54using namespace FIFFLIB;
55
56//=============================================================================================================
57// DEFINE MEMBER METHODS
58//=============================================================================================================
59
61: version(-1)
62{
63 machid[0] = -1;
64 machid[1] = -1;
65 time.secs = -1;
66 time.usecs = -1;
67}
68
69//=============================================================================================================
70
71FiffId::FiffId(const FiffId& p_FiffId)
72: version(p_FiffId.version)
73{
74 machid[0] = p_FiffId.machid[0];
75 machid[1] = p_FiffId.machid[1];
76 time.secs = p_FiffId.time.secs;
77 time.usecs = p_FiffId.time.usecs;
78}
79
80//=============================================================================================================
81
85
86//=============================================================================================================
87
89{
90 FiffId id;
91 id.version = FIFFC_VERSION;
92
93 int fixed_id[2];
94 get_machid(fixed_id);
95 /*
96 * Internet address in the first two words
97 */
98 id.machid[0] = fixed_id[0];
99 id.machid[1] = fixed_id[1];
100 /*
101 * Time in the third and fourth words
102 */
103 /*
104 * Time in the third and fourth words
105 * Since practically no system gives times in
106 * true micro seconds, the last three digits
107 * are randomized to insure uniqueness.
108 */
109 {
110 id.time.secs = QDateTime::currentMSecsSinceEpoch()/1000;
111 id.time.usecs = rand() % 1000;
112 }
113 return id;
114}
115
116//=============================================================================================================
117
119{
120 version = -1;
121 machid[0] = -1;
122 machid[1] = -1;
123 time.secs = -1;
124 time.usecs = -1;
125}
126
127//=============================================================================================================
128
129bool FiffId::get_machid(int *fixed_id)
130{
131 QList<QString> possibleHardwareAdresses;
132
133 #ifndef WASMBUILD
134 QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
135
136 fixed_id[0] = 0;
137 fixed_id[1] = 0;
138 if ( !ifaces.isEmpty() ) {
139 for(int i = 0; i < ifaces.size(); ++i) {
140 unsigned int flags = ifaces[i].flags();
141 bool isLoopback = static_cast<bool>(flags & QNetworkInterface::IsLoopBack);
142 bool isP2P = static_cast<bool>(flags & QNetworkInterface::IsPointToPoint);
143 bool isRunning = static_cast<bool>(flags & QNetworkInterface::IsRunning);
144 // If this interface isn't running, we don't care about it
145 if ( !isRunning ) continue;
146 // We only want valid interfaces that aren't loopback/virtual and not point to point
147 if ( !ifaces[i].isValid() || isLoopback || isP2P ) continue;
148 possibleHardwareAdresses << ifaces[i].hardwareAddress();
149 }
150 if (possibleHardwareAdresses.size() > 0) {
151 // We take the first address as machine identifier
152 QStringList hexPresentation = possibleHardwareAdresses[0].split(":");
153 if(hexPresentation.size() == 6) {
154 fixed_id[0] = QString(hexPresentation[0] + hexPresentation[1] + hexPresentation[2]).toInt(nullptr,16);
155 fixed_id[1] = QString(hexPresentation[3] + hexPresentation[4] + hexPresentation[5]).toInt(nullptr,16);
156 return true;
157 }
158 }
159 }
160 #endif
161
162 return false;
163}
164
165//=============================================================================================================
166
167void FiffId::print() const
168{
169 if(!isEmpty()) {
170 qInfo("\t%d.%d 0x%x%x %d %d\n",this->version>>16,this->version & 0xFFFF,this->machid[0],this->machid[1],this->time.secs,this->time.usecs);
171 }
172}
173
174//=============================================================================================================
175
177{
178 QString strOut = QString("%1%2").arg(machid[0],8,16,QChar('0')).arg(machid[1],8,16,QChar('0'));
179
180// to do...
181// macid is 6 bytes of data->12 chars.
182// here macid is stored in two integers --> 8 bytes --> 16 chars.
183// some versions of sinuhe store the significant chars at the beginning of the 16 chars.
184// other versions sotre the at the end. I don't know on what it depends on.
185// clue 1: version 1.3 stores it at the beginning. (padding with 4 '0' chars at the end).
186// clue 2: version 1.2 stores it at the ending chars. (padding with 4 '0' chars at the beginning of the 16).
187// I've no idea if this behaviour is solid...
188// int thresholdMayorVersion(1);
189// int thresholdMinorVersion(2);
190
191// int thresholdVersionInt(static_cast<int>(thresholdMayorVersion*pow(2.,16))+thresholdMinorVersion);
192// if(version > thresholdVersionInt) //if this.version > 65538
193// {
194// strOut.chop(4);
195// } else
196// {
197// strOut.right(strOut.size()-4);
198// }
199
200 int step=2;
201 for(int i=step;i < strOut.size(); i+=step+1)
202 {
203 strOut.insert(i,QChar(':'));
204 }
205
206 return strOut.toUpper();
207}
208
209//=============================================================================================================
210
212{
213 static FiffId defaultFiffId;
214 return defaultFiffId;
215}
216
217//=============================================================================================================
218
219QString FiffId::toString() const
220{
221 time_t secs = time.secs;
222 struct tm *ltime = localtime(&secs);
223 char timebuf[100];
224 strftime(timebuf, sizeof(timebuf), "%c", ltime);
225 return QString("%1.%2 0x%3%4 %5")
226 .arg(version >> 16)
227 .arg(version & 0xFFFF)
228 .arg(machid[0], 0, 16)
229 .arg(machid[1], 0, 16)
230 .arg(QString::fromLatin1(timebuf));
231}
FiffId class declaration.
Header file describing the numerical values used in fif files.
#define FIFFC_VERSION
Definition fiff_file.h:222
FIFF file I/O and data structures (raw, epochs, evoked, covariance, forward).
fiff_int_t machid[2]
Definition fiff_id.h:180
QString toString() const
Definition fiff_id.cpp:219
bool isEmpty() const
Definition fiff_id.h:189
QString toMachidString() const
Definition fiff_id.cpp:176
FiffTime time
Definition fiff_id.h:181
static bool get_machid(int *fixed_id)
Definition fiff_id.cpp:129
static FiffId new_file_id()
Definition fiff_id.cpp:88
fiff_int_t version
Definition fiff_id.h:179
void print() const
Definition fiff_id.cpp:167
static FiffId & getDefault()
Definition fiff_id.cpp:211