Introducing diagnostic manager class.
[apps/agl-service-can-low-level.git] / src / diagnostic / diagnostic-manager.hpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 #include <vector>
21
22 #include "uds/uds.h"
23 #include "can/can-bus.hpp"
24 #include "can/can-message.hpp"
25 #include "obd2/active-diagnostic-request.hpp"
26
27 #include "low-can-binding.hpp"
28
29 /* Private: Each CAN bus needs its own set of shim functions, so this should
30  * match the maximum CAN controller count.
31  */
32 #define MAX_SHIM_COUNT can_bus_t.get_can_devices().size()
33
34 /**
35  * @brief The core structure for running the diagnostics module on the VI.
36  *
37  * @desc This stores details about the active requests and shims required to connect
38  * the diagnostics library to the VI's CAN peripheral.
39  */
40 class diagnostic_manager_t {
41         protected:
42         void shims_logger(const char* m);
43         void shims_timer();
44
45         private:
46                 DiagnosticShims shims_; /*!< shims_ - An array of shim functions for each CAN bus that plug the diagnostics
47                                                                  * library (uds-c) into the VI's CAN peripheral.*/
48                 can_bus_dev_t* bus_; /*!< bus_ - A reference to the CAN bus that should be used for all standard OBD-II requests, if the bus is not
49                                                           * explicitly spcified in the request. If NULL, all requests require an explicit bus.*/
50                 std::queue<active_diagnostic_request_t> recurringRequests_; /*!< recurringRequests - A queue of active, recurring diagnostic requests. When
51                                                                                                                                   * a response is received for a recurring request or it times out, it is
52                                                                                                                                   * popped from the queue and pushed onto the back. */
53                 std::vector<active_diagnostic_request_t> nonrecurringRequests_; /*!< nonrecurringRequests - A list of active one-time diagnostic requests. When a
54                                                                                                                                           * response is received for a non-recurring request or it times out, it is
55                                                                                                                                           * removed from this list and placed back in the free list.*/
56                 std::vector<active_diagnostic_request_t> freeRequestEntries_; /*!< freeRequestEntries - A list of all available slots for active diagnostic
57                                                                                                                                         * requests. This free list is backed by statically allocated entries in
58                                                                                                                                         * the requestListEntries attribute.*/
59                 std::vector<active_diagnostic_request_t> requestListEntries_[50]; /*!< requestListEntries - Static allocation for all active diagnostic requests.*/
60
61                 bool initialized_; /*!< * initialized - True if the DiagnosticsManager has been initialized with shims. It will interface with the uds-c lib*/
62
63         public:
64                 diagnostic_manager_t(can_bus_dev_t& bus);
65                 void init_diagnostic_shims();
66  
67                 void checkSupportedPids(const active_diagnostic_request_t& request,
68                 const DiagnosticResponse& response, float parsedPayload);
69
70                 bool addRecurringRequest(DiagnosticRequest* request, const char* name,
71                         bool waitForMultipleResponses, const DiagnosticResponseDecoder decoder,
72                         const DiagnosticResponseCallback callback, float frequencyHz);
73
74                 void reset();
75
76                 void add_request(int pid);
77 };