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