1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <optional>
22 #include <string>
23 #include <tuple>
24 
25 namespace android::kver {
26 
27 // KMI version describes the version of the stable kernel module interface.
28 // Example: 5.4-android12-0
29 class KmiVersion {
30  public:
31   // Parse a string like "5.4-android12-0" to a KmiVersion object.
32   // Return nullopt if any error.
33   static std::optional<KmiVersion> Parse(const std::string& s);
34 
35   // String representation of the KMI version object.
36   // e.g. "5.4-android12-0"
37   std::string string() const;
38 
39   // Getters of each field.
40   uint64_t version() const { return version_; }
41   uint64_t patch_level() const { return patch_level_; }
42   uint64_t android_release() const { return release_; }
43   uint64_t generation() const { return gen_; }
44 
45   // Let a KMI version be w.x-androidz-k.
46   // Returns (w, x, z, k).
47   std::tuple<uint64_t, uint64_t, uint64_t, uint64_t> tuple() const;
48 
49  private:
50   friend class KernelRelease;
51   KmiVersion() = default;
52   uint64_t version_ = 0;
53   uint64_t patch_level_ = 0;
54   uint64_t release_ = 0;
55   uint64_t gen_ = 0;
56 };
57 
58 inline bool operator==(const KmiVersion& left, const KmiVersion& right) {
59   return left.tuple() == right.tuple();
60 }
61 
62 inline bool operator!=(const KmiVersion& left, const KmiVersion& right) {
63   return left.tuple() != right.tuple();
64 }
65 
66 }  // namespace android::kver
67