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 <android/hardware/automotive/can/1.0/types.h>
20 #include <libprotocan/Signal.h>
21 
22 namespace android::hardware::automotive::protocan {
23 
24 class MessageCounter {
25  public:
26   const Signal::value upperBound;
27 
28   MessageCounter(Signal signal);
29 
30   /**
31    * Parse CAN message sent by external ECU to determine current counter value.
32    */
33   void read(const can::V1_0::CanMessage& msg);
34 
35   /**
36    * States whether current counter value is determined.
37    */
38   bool isReady() const;
39 
40   /**
41    * Increment current counter value and set it in a new message.
42    *
43    * Caller must check isReady() at least once before calling this method.
44    */
45   void increment(can::V1_0::CanMessage& msg);
46 
47  private:
48   const Signal mSignal;
49 
50   std::optional<Signal::value> mCurrent = std::nullopt;
51 
52   Signal::value next() const;
53 };
54 
55 }  // namespace android::hardware::automotive::protocan
56