Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / uuid / uuid_init.c
blob0606003019ad7cbdff5205d6b7b1c4e8e6e7e789
1 /*
2 Copyright © 2007-2008, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 1
8 #include <aros/symbolsets.h>
9 #include <aros/debug.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <proto/timer.h>
13 #include <dos/var.h>
15 #include "uuid_private.h"
16 #include LC_LIBDEFS_FILE
18 static int UUID_Init(LIBBASETYPEPTR LIBBASE)
20 struct timeval tv;
21 uuid_time_t time_now;
23 /* Set up global lock (class and interface locks are separate!) */
24 InitSemaphore(&LIBBASE->uuid_GlobalLock);
26 D(bug("[UUID] UUID Init\n"));
28 LIBBASE->uuid_Initialized = 0;
30 /* I need timer.device in order to obtain system time */
31 if (OpenDevice("timer.device", UNIT_MICROHZ, &LIBBASE->uuid_TR, 0))
33 D(bug("[UUID] Could not open timer.device. ABORT!\n"));
34 return FALSE;
37 /* get the system time and convert it to UUID time */
38 GetSysTime(&tv);
39 time_now = LIBBASE->uuid_NextUpdate = LIBBASE->uuid_LastTime =
40 ((uint64_t)tv.tv_secs + 2922) * 10000000 +
41 ((uint64_t)tv.tv_micro) * 10 +
42 ((uint64_t)0x01B21DD213814000LL);
44 D(bug("[UUID] UUID time: 0x%08lx%08lx\n",
45 (uint32_t)((LIBBASE->uuid_LastTime >> 32) & 0xffffffff),
46 (uint32_t)((LIBBASE->uuid_LastTime & 0xffffffff))
47 ));
49 /* Seed the random generator */
50 time_now /= UUIDS_PER_TICK;
51 UUIDBase->uuid_RandomSeed = (time_now >> 32) ^ time_now;
52 UUIDBase->uuid_UUIDs_ThisTick = 0;
54 /* Try to open dos.library for GetVar/SetVar */
55 DOSBase = OpenLibrary("dos.library", 0);
56 if (DOSBase)
58 D(bug("[UUID] dos.library opened. Trying to get the UUID state.\n"));
60 /* DOS is there. Try to get the last UUID state. */
61 if (GetVar("uuid_state", (UBYTE*)&LIBBASE->uuid_State, sizeof(uuid_state_t),
62 GVF_BINARY_VAR | GVF_DONT_NULL_TERM) == sizeof(uuid_state_t))
64 D(bug("[UUID] got last UUID state\n"));
65 LIBBASE->uuid_Initialized = 1;
67 else
69 D(bug("[UUID] no UUID state found. Staying uninitlaized\n"));
72 else
73 D(bug("[UUID] dos.library not yet available. I will try later."));
75 return TRUE;
78 static int UUID_Expunge(LIBBASETYPEPTR LIBBASE)
80 D(bug("[UUID] Expunge. Byebye.\n"));
82 CloseDevice(&LIBBASE->uuid_TR);
84 if (DOSBase)
86 /* Store the state */
87 SetVar("uuid_state", (UBYTE*)&LIBBASE->uuid_State, sizeof(uuid_state_t),
88 GVF_GLOBAL_ONLY | GVF_SAVE_VAR | LV_VAR);
90 CloseLibrary(DOSBase);
93 return TRUE;
96 ADD2INITLIB(UUID_Init, 0)
97 ADD2EXPUNGELIB(UUID_Expunge, 0)