1 /*
2  * Copyright 2019 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 <cstdint>
20 
21 namespace bluetooth {
22 namespace l2cap {
23 
24 struct SignalId {
25  public:
SignalIdSignalId26   constexpr SignalId(uint8_t value) : value_(value) {}
SignalIdSignalId27   constexpr SignalId() : value_(1) {}
28   ~SignalId() = default;
29 
ValueSignalId30   uint8_t Value() const {
31     return value_;
32   }
33 
IsValidSignalId34   bool IsValid() const {
35     return value_ != 0;
36   }
37 
38   friend bool operator==(const SignalId& lhs, const SignalId& rhs);
39   friend bool operator!=(const SignalId& lhs, const SignalId& rhs);
40 
41   struct SignalId& operator++();    // Prefix increment operator.
42   struct SignalId operator++(int);  // Postfix increment operator.
43   struct SignalId& operator--();    // Prefix decrement operator.
44   struct SignalId operator--(int);  // Postfix decrement operator.
45 
46  private:
47   uint8_t value_;
48 };
49 
50 constexpr SignalId kInvalidSignalId{0};
51 constexpr SignalId kInitialSignalId{1};
52 
53 inline bool operator==(const SignalId& lhs, const SignalId& rhs) {
54   return lhs.value_ == rhs.value_;
55 }
56 
57 inline bool operator!=(const SignalId& lhs, const SignalId& rhs) {
58   return !(lhs == rhs);
59 }
60 
61 inline struct SignalId& SignalId::operator++() {
62   value_++;
63   if (value_ == 0) value_++;
64   return *this;
65 }
66 
67 inline struct SignalId SignalId::operator++(int) {
68   struct SignalId tmp = *this;
69   ++*this;
70   return tmp;
71 }
72 
73 inline struct SignalId& SignalId::operator--() {
74   value_--;
75   if (value_ == 0) value_ = 0xff;
76   return *this;
77 }
78 
79 inline struct SignalId SignalId::operator--(int) {
80   struct SignalId tmp = *this;
81   --*this;
82   return tmp;
83 }
84 
85 }  // namespace l2cap
86 }  // namespace bluetooth
87