Ji Soo Shin | 9035e7c | 2022-01-05 11:58:21 +0100 | [diff] [blame] | 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 | #include <libprotocan/Signal.h> |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | namespace android::hardware::automotive::protocan { |
| 22 | |
| 23 | static uint8_t calculateLastByteMask(uint16_t start, uint8_t length) { |
| 24 | unsigned lastByteBits = (start + length) % 8; |
| 25 | unsigned lastBytePadding = (8 - lastByteBits) % 8; |
| 26 | return 0xFF >> lastBytePadding; |
| 27 | } |
| 28 | |
| 29 | static uint8_t calculateFirstByteMask(uint16_t firstByte, uint8_t firstBit, uint16_t lastByte, |
| 30 | uint8_t lastMask) { |
| 31 | uint8_t firstMask = 0xFF << firstBit; |
| 32 | if (firstByte == lastByte) firstMask &= lastMask; |
| 33 | return firstMask; |
| 34 | } |
| 35 | |
| 36 | Signal::Signal(uint16_t start, uint8_t length, value defVal) |
| 37 | : maxValue((1u << length) - 1), |
| 38 | kFirstByte(start / 8), |
| 39 | kFirstBit(start % 8), |
| 40 | kFirstByteBits(8 - kFirstBit), |
| 41 | kLastByte((start + length - 1) / 8), |
| 42 | kLastMask(calculateLastByteMask(start, length)), |
| 43 | kFirstMask(calculateFirstByteMask(kFirstByte, kFirstBit, kLastByte, kLastMask)), |
| 44 | kDefVal(defVal) { |
| 45 | CHECK(length > 0) << "Signal length must not be zero"; |
| 46 | } |
| 47 | |
| 48 | Signal::value Signal::get(const can::V1_0::CanMessage& msg) const { |
| 49 | CHECK(msg.payload.size() > kLastByte) |
| 50 | << "Message is too short. Did you call MessageDef::validate?"; |
| 51 | |
| 52 | Signal::value v = 0; |
| 53 | if (kLastByte != kFirstByte) v = kLastMask & msg.payload[kLastByte]; |
| 54 | |
| 55 | for (int i = kLastByte - 1; i > kFirstByte; i--) { |
| 56 | v = (v << 8) | msg.payload[i]; |
| 57 | } |
| 58 | |
| 59 | return (v << kFirstByteBits) | ((msg.payload[kFirstByte] & kFirstMask) >> kFirstBit); |
| 60 | } |
| 61 | |
| 62 | void Signal::set(can::V1_0::CanMessage& msg, Signal::value val) const { |
| 63 | CHECK(msg.payload.size() > kLastByte) |
| 64 | << "Signal requires message of length " << (kLastByte + 1) |
| 65 | << " which is beyond message length of " << msg.payload.size(); |
| 66 | |
| 67 | uint8_t firstByte = val << kFirstBit; |
| 68 | val >>= kFirstByteBits; |
| 69 | |
| 70 | msg.payload[kFirstByte] = (msg.payload[kFirstByte] & ~kFirstMask) | (firstByte & kFirstMask); |
| 71 | |
| 72 | for (int i = kFirstByte + 1; i < kLastByte; i++) { |
| 73 | msg.payload[i] = val & 0xFF; |
| 74 | val >>= 8; |
| 75 | } |
| 76 | |
| 77 | if (kLastByte != kFirstByte) { |
| 78 | msg.payload[kLastByte] = (msg.payload[kLastByte] & ~kLastMask) | (val & kLastMask); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void Signal::setDefault(can::V1_0::CanMessage& msg) const { set(msg, kDefVal); } |
| 83 | |
| 84 | } // namespace android::hardware::automotive::protocan |