aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/OVR_SerialFormat.cpp
blob: 80def570e1010c5d0daad28658db04b10c16cb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/************************************************************************************

Filename    :   OVR_System.cpp
Content     :   General kernel initialization/cleanup, including that
                of the memory allocator.
Created     :   September 19, 2012
Notes       : 

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "OVR_SerialFormat.h"

#ifdef SERIAL_FORMAT_UNIT_TEST
#include "Kernel/OVR_Log.h"
#include "Kernel/OVR_Rand.h"
#endif


#include <cctype>
#include <ctime>

namespace OVR {


//// Serial Format Detection

SerialFormatType DetectBufferFormat(uint8_t firstByte, int sizeInBytes)
{
	switch (firstByte)
	{
	case SerialFormatType_DK2:
		if (sizeInBytes == 12)
		{
			return SerialFormatType_DK2;
		}
		break;
	default:
		break;
	}

	return SerialFormatType_Invalid;
}


//// DK2 Helpers

static bool ValidDK2ProductId(int x)
{
	switch (x)
	{
	case DK2ProductId_DK1:
	case DK2ProductId_DK2:
	case DK2ProductId_Refurb:
		return true;
	default:
		break;
	}

	return false;
}

static bool ValidDK2PartId(int x)
{
	switch (x)
	{
	case DK2PartId_HMD:
	case DK2PartId_PTC:
	case DK2PartId_Carton:
		return true;
	default:
		break;
	}

	return false;
}


//// DK2BinarySerialFormat

bool DK2BinarySerialFormat::FromBuffer(const uint8_t buffer[12], bool allowUnknownTypes)
{
	// Format Type must be 0
	
	int formatType = buffer[0];

	if (formatType != SerialFormatType_DK2)
	{
		return false;
	}

	// Product Id

	int productId = buffer[1] >> 4;

	if (!allowUnknownTypes && !ValidDK2ProductId(productId))
	{
		return false;
	}

	ProductId = (DK2ProductId)productId;

	// Part Id

	int partId = buffer[1] & 15;

	if (!allowUnknownTypes && !ValidDK2PartId(partId))
	{
		return false;
	}

	PartId = (DK2PartId)partId;

	// Minutes Since Epoch (May 1, 2014)

	MinutesSinceEpoch = buffer[4] | ((uint32_t)buffer[3] << 8) | ((uint32_t)buffer[2] << 16);

	// Unit number on that day

	UnitNumber = buffer[6] | ((uint32_t)buffer[5] << 8);

	// Hash of MAC address

	MacHash[0] = buffer[7];
	MacHash[1] = buffer[8];
	MacHash[2] = buffer[9];
	MacHash[3] = buffer[10];
	MacHash[4] = buffer[11];

	return true;
}

void DK2BinarySerialFormat::ToBuffer(uint8_t buffer[12])
{
	// Serialize to buffer
	buffer[0] = SerialFormatType_DK2;
	buffer[1] = (uint8_t)((ProductId << 4) | (PartId));
	buffer[2] = (uint8_t)(MinutesSinceEpoch >> 16);
	buffer[3] = (uint8_t)(MinutesSinceEpoch >> 8);
	buffer[4] = (uint8_t)MinutesSinceEpoch;
	buffer[5] = (uint8_t)(UnitNumber >> 8);
	buffer[6] = (uint8_t)UnitNumber;

	buffer[7] = MacHash[0];
	buffer[8] = MacHash[1];
	buffer[9] = MacHash[2];
	buffer[10] = MacHash[3];
	buffer[11] = MacHash[4];
}

bool DK2BinarySerialFormat::operator==(const DK2BinarySerialFormat& rhs)
{
	if (ProductId != rhs.ProductId)
		return false;
	if (PartId != rhs.PartId)
		return false;
	if (MinutesSinceEpoch != rhs.MinutesSinceEpoch)
		return false;
	if (UnitNumber != rhs.UnitNumber)
		return false;
	for (int ii = 0; ii < 5; ++ii)
	{
		if (MacHash[ii] != rhs.MacHash[ii])
			return false;
	}
	return true;
}


//// DK2PrintedSerialFormat

// Base-32 Crockford decoding rules:
// 0 o O => 0
// 1 i | I L l => 1
// 2, 3, 4, 5, 6, 7, 8, 9 => 2 - 9
// a, b, c, d, e, f, g, h => 10 - 17
// j, k => 18, 19
// m, n => 20, 21
// p, q, r, s, t => 22, 23, 24, 25, 26
// v, w, x, y, z => 27, 28, 29, 30, 31
static const char Base32FromChar[256] = {
	// Null - Unit Separator
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31
	// (sp)!"#$%&'()*+,-./
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47
	// 0123456789:;<=>?
	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 48 - 63
	// @A-Z[\]^_
	-1, 10, 11, 12, 13, 14, 15, 16, 17,  1, 18, 19,  1, 20, 21,  0, // 64 - 79
	22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, // 80 - 95
	// `a-z{|}~DEL
	-1, 10, 11, 12, 13, 14, 15, 16, 17,  1, 18, 19,  1, 20, 21,  0, // 96 - 111
	22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31, -1,  1, -1, -1, -1, // 112 - 127

	// Extended ASCII:
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

// Base-32 Crockford encoding rules:
// 0-9 => 0-9
// 10 - 17 => a, b, c, d, e, f, g, h
// 18, 19 => j, k
// 20, 21 => m, n
// 22, 23, 24, 25, 26 => p, q, r, s, t
// 27, 28, 29, 30, 31 => v, w, x, y, z
static const char* CharFromBase32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";

bool DK2PrintedSerialFormat::FromBase32(const char* str, bool allowUnknownTypes)
{
	// Note: Truncated strings get caught by returning negative values from the table like other invalid characters

	// Product Id

	int productId = Base32FromChar[(unsigned char)str[0]];
	if (productId < 0 || (!allowUnknownTypes && !ValidDK2ProductId(productId)))
	{
		return false;
	}

	ProductId = (DK2ProductId)productId;

	// Label Type

	int labelType = Base32FromChar[(unsigned char)str[1]];
	if (labelType < 0 || (!allowUnknownTypes && !ValidDK2PartId(labelType)))
	{
		return false;
	}

	LabelType = (DK2LabelType)labelType;

	uint8_t dataBytes[7];
	for (int ii = 0; ii < 7; ++ii)
	{
		int c = Base32FromChar[(unsigned char)str[2 + ii]];
		if (c < 0) return false;
		dataBytes[ii] = (uint8_t)c;
	}

	// Minutes Since Epoch

	MinutesSinceEpoch = dataBytes[3] | ((uint32_t)dataBytes[2] << 5) | ((uint32_t)dataBytes[1] << 10) | ((uint32_t)dataBytes[0] << 15);

	// Unit Number

	UnitNumber = dataBytes[6] | ((uint32_t)dataBytes[5] << 5) | ((uint32_t)dataBytes[4] << 10);

	// MAC Hash

	for (int ii = 0; ii < 3; ++ii)
	{
		int c = Base32FromChar[(unsigned char)str[9 + ii]];
		if (c < 0)
		{
			return false;
		}

		MacHashLow[ii] = (uint8_t)c;
	}

	// String must be exactly 12 characters
	if (str[12] != '\0')
	{
		return false;
	}

	return true;
}

String DK2PrintedSerialFormat::ToBase32()
{
	String s;

	s += CharFromBase32[ProductId];
	s += CharFromBase32[LabelType];
	s += CharFromBase32[(MinutesSinceEpoch >> 15) & 31];
	s += CharFromBase32[(MinutesSinceEpoch >> 10) & 31];
	s += CharFromBase32[(MinutesSinceEpoch >> 5) & 31];
	s += CharFromBase32[MinutesSinceEpoch & 31];
	s += CharFromBase32[(UnitNumber >> 10) & 31];
	s += CharFromBase32[(UnitNumber >> 5) & 31];
	s += CharFromBase32[UnitNumber & 31];
	s += CharFromBase32[MacHashLow[0] & 31];
	s += CharFromBase32[MacHashLow[1] & 31];
	s += CharFromBase32[MacHashLow[2] & 31];

	return s;
}

bool DK2PrintedSerialFormat::operator==(const DK2PrintedSerialFormat& rhs)
{
	if (ProductId != rhs.ProductId)
		return false;
	if (LabelType != rhs.LabelType)
		return false;
	if (MinutesSinceEpoch != rhs.MinutesSinceEpoch)
		return false;
	if (UnitNumber != rhs.UnitNumber)
		return false;
	for (int ii = 0; ii < 3; ++ii)
	{
		if (MacHashLow[ii] != rhs.MacHashLow[ii])
			return false;
	}
	return true;
}

bool DK2PrintedSerialFormat::operator==(const DK2BinarySerialFormat& rhs)
{
	if (ProductId != rhs.ProductId)
		return false;
	if (LabelType != rhs.PartId)
		return false;
	if (MinutesSinceEpoch != rhs.MinutesSinceEpoch)
		return false;
	if (UnitNumber != rhs.UnitNumber)
		return false;
	for (int ii = 0; ii < 3; ++ii)
	{
		if (MacHashLow[ii] != (rhs.MacHash[ii] & 31))
			return false;
	}
	return true;
}

void DK2PrintedSerialFormat::FromBinary(const DK2BinarySerialFormat& bin)
{
	ProductId = bin.ProductId;
	LabelType = bin.PartId;
	MinutesSinceEpoch = bin.MinutesSinceEpoch;
	UnitNumber = bin.UnitNumber;
	MacHashLow[0] = bin.MacHash[0] & 31;
	MacHashLow[1] = bin.MacHash[1] & 31;
	MacHashLow[2] = bin.MacHash[2] & 31;
}




} // OVR