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/Checksum.h> 21 #include <libprotocan/MessageCounter.h> 22 #include <libprotocan/Signal.h> 23 24 namespace android::hardware::automotive::protocan { 25 26 /** 27 * CAN message definition (not the actual message data). 28 * 29 * Describes static message properties (message ID, signals etc). 30 */ 31 class MessageDef { 32 public: 33 const can::V1_0::CanMessageId id; 34 35 /** 36 * Create message definition. 37 * 38 * Currently only constant length messages are supported. 39 * 40 * \param id CAN message ID 41 * \param len CAN message length 42 * \param signals CAN signal definitions 43 * \param counter Designated CAN signal definition for message counter, if the message has one 44 * \param checksum Designated CAN signal definition for payload checksum, if the message has one 45 */ 46 MessageDef(can::V1_0::CanMessageId id, uint16_t len, std::map<std::string, Signal> signals, 47 std::optional<Signal> counter = std::nullopt, 48 std::optional<Checksum> checksum = std::nullopt); 49 50 const Signal& operator[](const std::string& signalName) const; 51 52 can::V1_0::CanMessage makeDefault() const; 53 MessageCounter makeCounter() const; 54 55 void updateChecksum(can::V1_0::CanMessage& msg) const; 56 57 /** 58 * Validate the message payload is large enough to hold all the signals. 59 */ 60 bool validate(const can::V1_0::CanMessage& msg) const; 61 62 private: 63 const uint16_t kLen; 64 const std::map<std::string, Signal> kSignals; 65 const std::optional<Signal> kCounter; 66 const std::optional<Checksum> kChecksum; 67 }; 68 69 } // namespace android::hardware::automotive::protocan 70