2 Copyright © 2007-2008, The AROS Development Team. All rights reserved.
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>
15 #include "uuid_private.h"
16 #include LC_LIBDEFS_FILE
18 static int UUID_Init(LIBBASETYPEPTR LIBBASE
)
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"));
37 /* get the system time and convert it to UUID time */
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))
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);
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;
69 D(bug("[UUID] no UUID state found. Staying uninitlaized\n"));
73 D(bug("[UUID] dos.library not yet available. I will try later."));
78 static int UUID_Expunge(LIBBASETYPEPTR LIBBASE
)
80 D(bug("[UUID] Expunge. Byebye.\n"));
82 CloseDevice(&LIBBASE
->uuid_TR
);
87 SetVar("uuid_state", (UBYTE
*)&LIBBASE
->uuid_State
, sizeof(uuid_state_t
),
88 GVF_GLOBAL_ONLY
| GVF_SAVE_VAR
| LV_VAR
);
90 CloseLibrary(DOSBase
);
96 ADD2INITLIB(UUID_Init
, 0)
97 ADD2EXPUNGELIB(UUID_Expunge
, 0)