• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/MessageInjector.h>
18 
19 #include <android-base/logging.h>
20 
21 #include <thread>
22 
23 namespace android::hardware::automotive::protocan {
24 
25 /** Whether to log injected messages. */
26 static constexpr bool kSuperVerbose = true;
27 
28 using namespace std::literals::chrono_literals;
29 
30 using can::V1_0::CanMessage;
31 using can::V1_0::CanMessageId;
32 using can::V1_0::ICanBus;
33 using can::V1_0::Result;
34 
MessageInjector(MessageDef msgDef,std::optional<std::chrono::milliseconds> interMessageDelay)35 MessageInjector::MessageInjector(MessageDef msgDef,
36                                  std::optional<std::chrono::milliseconds> interMessageDelay)
37     : kMsgDef(std::move(msgDef)),
38       kInterMessageDelay(interMessageDelay),
39       mCounter(msgDef.makeCounter()) {}
40 
inject(const CanMessage & msg)41 void MessageInjector::inject(const CanMessage& msg) { inject({msg}); }
42 
inject(const std::initializer_list<can::V1_0::CanMessage> msgs)43 void MessageInjector::inject(const std::initializer_list<can::V1_0::CanMessage> msgs) {
44   std::lock_guard<std::mutex> lock(mMessagesGuard);
45   for (const auto& msg : msgs) {
46     if constexpr (kSuperVerbose) {
47       LOG(VERBOSE) << "Message scheduled for injection: " << toString(msg);
48     }
49 
50     mMessages.push(msg);
51   }
52 }
53 
processQueueLocked(can::V1_0::ICanBus & bus)54 void MessageInjector::processQueueLocked(can::V1_0::ICanBus& bus) {
55   if (mMessages.empty() || !mCounter.isReady()) return;
56 
57   auto paddingMessagesCount = mCounter.upperBound - (mMessages.size() % mCounter.upperBound);
58   auto padMessage = kMsgDef.makeDefault();
59   for (unsigned i = 0; i < paddingMessagesCount; i++) {
60     mMessages.push(padMessage);
61   }
62 
63   while (!mMessages.empty()) {
64     auto&& outMsg = mMessages.front();
65 
66     mCounter.increment(outMsg);
67     kMsgDef.updateChecksum(outMsg);
68 
69     if constexpr (kSuperVerbose) {
70       LOG(VERBOSE) << "Injecting message: " << toString(outMsg);
71     }
72     auto result = bus.send(outMsg);
73     if (result != Result::OK) {
74       LOG(ERROR) << "Message injection failed: " << toString(result);
75     }
76 
77     mMessages.pop();
78 
79     // This would block onReceive, but the class is not supposed to be used in production anyway
80     // (see MessageInjector docstring).
81     if (kInterMessageDelay.has_value()) {
82       std::this_thread::sleep_for(*kInterMessageDelay);
83     }
84   }
85 }
86 
onReceive(ICanBus & bus,const CanMessage & msg)87 void MessageInjector::onReceive(ICanBus& bus, const CanMessage& msg) {
88     if (!kMsgDef.validate(msg)) return;
89 
90     std::lock_guard<std::mutex> lock(mMessagesGuard);
91 
92     mCounter.read(msg);
93     processQueueLocked(bus);
94 }
95 
MessageInjectorManager(std::initializer_list<std::shared_ptr<MessageInjector>> injectors)96 MessageInjectorManager::MessageInjectorManager(
97     std::initializer_list<std::shared_ptr<MessageInjector>> injectors) {
98   std::transform(injectors.begin(), injectors.end(), std::inserter(mInjectors, mInjectors.end()),
99                  [](const std::shared_ptr<MessageInjector>& injector) {
100                    return std::make_pair(injector->kMsgDef.id, std::move(injector));
101                  });
102 }
103 
onReceive(sp<ICanBus> bus,const CanMessage & msg)104 void MessageInjectorManager::onReceive(sp<ICanBus> bus, const CanMessage& msg) {
105   auto it = mInjectors.find(msg.id);
106   if (it == mInjectors.end()) return;
107   it->second->onReceive(*bus, msg);
108 }
109 
110 }  // namespace android::hardware::automotive::protocan
111