2 Copyright © 2007-2011, The AROS Development Team. All rights reserved.
6 #include <aros/symbolsets.h>
7 #include <aros/debug.h>
8 #include <proto/exec.h>
10 #include <proto/timer.h>
13 #include "uuid_private.h"
14 #include LC_LIBDEFS_FILE
16 static int UUID_Init(LIBBASETYPEPTR LIBBASE
)
21 /* Set up global lock (class and interface locks are separate!) */
22 InitSemaphore(&LIBBASE
->uuid_GlobalLock
);
24 D(bug("[UUID] UUID Init\n"));
26 LIBBASE
->uuid_Initialized
= 0;
28 /* I need timer.device in order to obtain system time */
29 if (OpenDevice("timer.device", UNIT_MICROHZ
, &LIBBASE
->uuid_TR
.tr_node
, 0))
31 D(bug("[UUID] Could not open timer.device. ABORT!\n"));
35 /* get the system time and convert it to UUID time */
37 time_now
= LIBBASE
->uuid_NextUpdate
= LIBBASE
->uuid_LastTime
=
38 ((uint64_t)tv
.tv_secs
+ 2922) * 10000000 +
39 ((uint64_t)tv
.tv_micro
) * 10 +
40 ((uint64_t)0x01B21DD213814000LL
);
42 D(bug("[UUID] UUID time: 0x%08lx%08lx\n",
43 (uint32_t)((LIBBASE
->uuid_LastTime
>> 32) & 0xffffffff),
44 (uint32_t)((LIBBASE
->uuid_LastTime
& 0xffffffff))
47 /* Seed the random generator */
48 time_now
/= UUIDS_PER_TICK
;
49 UUIDBase
->uuid_RandomSeed
= (time_now
>> 32) ^ time_now
;
50 UUIDBase
->uuid_UUIDs_ThisTick
= 0;
52 /* Try to open dos.library for GetVar/SetVar */
53 DOSBase
= (void *)OpenLibrary("dos.library", 0);
56 D(bug("[UUID] dos.library opened. Trying to get the UUID state.\n"));
58 /* DOS is there. Try to get the last UUID state. */
59 if (GetVar("uuid_state", (UBYTE
*)&LIBBASE
->uuid_State
, sizeof(uuid_state_t
),
60 GVF_BINARY_VAR
| GVF_DONT_NULL_TERM
) == sizeof(uuid_state_t
))
62 D(bug("[UUID] got last UUID state\n"));
63 LIBBASE
->uuid_Initialized
= 1;
67 D(bug("[UUID] no UUID state found. Staying uninitlaized\n"));
71 D(bug("[UUID] dos.library not yet available. I will try later."));
76 static int UUID_Expunge(LIBBASETYPEPTR LIBBASE
)
78 D(bug("[UUID] Expunge. Byebye.\n"));
80 CloseDevice(&LIBBASE
->uuid_TR
.tr_node
);
85 SetVar("uuid_state", (UBYTE
*)&LIBBASE
->uuid_State
, sizeof(uuid_state_t
),
86 GVF_GLOBAL_ONLY
| GVF_SAVE_VAR
| LV_VAR
);
88 CloseLibrary((void *)DOSBase
);
94 ADD2INITLIB(UUID_Init
, 0)
95 ADD2EXPUNGELIB(UUID_Expunge
, 0)