aboutsummaryrefslogtreecommitdiffstats
path: root/src/diagnostic/active-diagnostic-request.cpp
blob: 7a43a0d4152586bfbf7ca934de5b5460866b16ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author "Romain Forlot" <romain.forlot@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "active-diagnostic-request.hpp"

bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
{
	return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_) ? true : false;
}

active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
{
	if (this != &adr)
	{
		bus_ = adr.bus_;
		id_ = adr.id_;
		handle_ = adr.handle_;
		name_ = adr.name_;
		decoder_ = adr.decoder_;
		callback_ = adr.callback_;
		recurring_ = adr.recurring_; 
		wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
		in_flight_ = adr.in_flight_;
		frequency_clock_ = adr.frequency_clock_;
		timeout_clock_ = adr.timeout_clock_;
	}
	
	return *this;
}

active_diagnostic_request_t::active_diagnostic_request_t()
	: bus_{nullptr}, id_{0}, handle_{nullptr}, name_{""},
	  decoder_{nullptr}, callback_{nullptr}, recurring_{false}, wait_for_multiple_responses_{false},
	  in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
{}

active_diagnostic_request_t::active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
		const std::string& name, bool wait_for_multiple_responses,
		const DiagnosticResponseDecoder decoder,
		const DiagnosticResponseCallback callback, float frequencyHz)
	: bus_{bus}, id_{request->arbitration_id}, handle_{nullptr}, name_{name},
	  decoder_{decoder}, callback_{callback}, recurring_{frequencyHz ? true : false}, wait_for_multiple_responses_{wait_for_multiple_responses},
	  in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
{}

uint32_t active_diagnostic_request_t::get_id() const
{
	return id_;
}

std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
{
	return bus_;
}

DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
{
	return handle_;
}

bool active_diagnostic_request_t::get_recurring() const
{
	return recurring_;
}

bool active_diagnostic_request_t::get_in_flight() const
{
	return in_flight_;
}

frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
{
	return frequency_clock_;
}

frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
{
	return timeout_clock_;
}

void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
{
	handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
}

void active_diagnostic_request_t::set_in_flight(bool val)
{
	in_flight_ = val;
}

bool active_diagnostic_request_t::should_send()
{
	return !get_in_flight() && (
			(!get_recurring() && !request_completed()) ||
			(get_recurring() && get_frequency_clock().elapsed(true)));
}

bool active_diagnostic_request_t::timed_out()
{
	// don't use staggered start with the timeout clock
	return timeout_clock_.elapsed(false);
}

/// @brief Returns true if a sufficient response has been received for a
/// diagnostic request.
///
/// This is true when at least one response has been received and the request is
/// configured to not wait for multiple responses. Functional broadcast requests
/// may often wish to wait the full 100ms for modules to respond.
bool active_diagnostic_request_t::response_received() const
{
	return !wait_for_multiple_responses_ &&
				handle_->completed;
}

/// @brief Returns true if the request has timed out waiting for a response,
/// or a sufficient number of responses has been received.
bool active_diagnostic_request_t::request_completed()
{
	return response_received() || 
		(timed_out() && diagnostic_request_sent(handle_));
}