Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/uuid.h" |
| 2 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 6 | #include <array> |
| 7 | #include <random> |
| 8 | #include <string_view> |
| 9 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 10 | #include "glog/logging.h" |
| 11 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 12 | namespace aos { |
| 13 | namespace { |
| 14 | char ToHex(int val) { |
| 15 | if (val < 10) { |
| 16 | return val + '0'; |
| 17 | } else { |
| 18 | return val - 10 + 'a'; |
| 19 | } |
| 20 | } |
| 21 | } // namespace |
| 22 | |
| 23 | UUID UUID::Random() { |
| 24 | std::random_device rd; |
| 25 | std::mt19937 gen(rd()); |
| 26 | |
| 27 | std::uniform_int_distribution<> dis(0, 15); |
| 28 | std::uniform_int_distribution<> dis2(8, 11); |
| 29 | |
| 30 | UUID result; |
| 31 | |
| 32 | // UUID4 is implemented per https://www.cryptosys.net/pki/uuid-rfc4122.html |
| 33 | int i; |
| 34 | for (i = 0; i < 8; i++) { |
| 35 | result.data_[i] = ToHex(dis(gen)); |
| 36 | } |
| 37 | result.data_[i] = '-'; |
| 38 | ++i; |
| 39 | for (; i < 13; i++) { |
| 40 | result.data_[i] = ToHex(dis(gen)); |
| 41 | } |
| 42 | result.data_[i] = '-'; |
| 43 | ++i; |
| 44 | result.data_[i] = '4'; |
| 45 | ++i; |
| 46 | for (; i < 18; i++) { |
| 47 | result.data_[i] = ToHex(dis(gen)); |
| 48 | } |
| 49 | result.data_[i] = '-'; |
| 50 | ++i; |
| 51 | result.data_[i] = ToHex(dis2(gen)); |
| 52 | ++i; |
| 53 | for (; i < 23; i++) { |
| 54 | result.data_[i] = ToHex(dis(gen)); |
| 55 | } |
| 56 | result.data_[i] = '-'; |
| 57 | ++i; |
| 58 | for (; i < 36; i++) { |
| 59 | result.data_[i] = ToHex(dis(gen)); |
| 60 | } |
| 61 | |
| 62 | return result; |
| 63 | } |
| 64 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 65 | UUID UUID::Zero() { return FromString("00000000-0000-0000-0000-000000000000"); } |
| 66 | |
| 67 | UUID UUID::FromString(std::string_view str) { |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 68 | UUID result; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 69 | CHECK_EQ(str.size(), kSize); |
| 70 | |
| 71 | std::copy(str.begin(), str.end(), result.data_.begin()); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | UUID UUID::BootUUID() { |
| 76 | int fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY); |
| 77 | PCHECK(fd != -1); |
| 78 | |
| 79 | UUID result; |
| 80 | CHECK_EQ(static_cast<ssize_t>(kSize), read(fd, result.data_.begin(), kSize)); |
| 81 | close(fd); |
| 82 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 83 | return result; |
| 84 | } |
| 85 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 86 | } // namespace aos |