summaryrefslogtreecommitdiff
path: root/tools/AmbSignalMapper/lib/Intel/IviPoc/templates/ambtmpl_plugin.h
blob: 9e22f554eaacd34f0795fa7b9dfc20dae3ed78f3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*****************************************************************
Copyright (C) 2014  Intel Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *****************************************************************/

#ifndef AMBTMPL_PLUGIN_H_
#define AMBTMPL_PLUGIN_H_

#include <map>
#include <memory>
#include <tgmath.h>
#include <limits>
#include <byteswap.h>
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

#include <canbus.h>
#include <canobserver.h>

#include <ambpluginimpl.h>
#include "ambtmpl_cansignal.h"

using namespace boost::interprocess;

// forward declaration
class CANSignal;
class CANMessage;

class AmbTmplPlugin : public AmbPluginImpl, public CANObserver {

public:
    AmbTmplPlugin(AbstractRoutingEngine* re, const std::map<std::string, std::string>& config, AbstractSink& parent);
    virtual ~AmbTmplPlugin(); // has to be virtual because of unit tests

    // from AbstractSink
public:

    /*! uuid() is a unique identifier of the plugin
      * @return a guid-style unique identifier
      */
    const std::string uuid() const { return "/*GENERATED_CODE*/"; }

    /*!
     * \brief setProperty is called when a sink requests to set a value for a given property.
     * This is only called if the source supports the Set Operation.
     * \param request the requested property to set.
     * \return returns a pointer to the new value for the property.
     */
    AsyncPropertyReply *setProperty(const AsyncSetPropertyRequest& request );

    /*!
     * \brief supportedOperations
     * \return returns the supported operations.
     */
    int supportedOperations() const;


    // from CANObserver
public:
    /*!
     * Called when error occurred on the bus.
     * \fn errorOccured
     * \param error \link CANObserver#CANError Bus error code \endlink
     */
    virtual void errorOccured(CANObserver::CANError error);/* socket error */
    /*!
     * Called when standard frame was is received from the bus.
     * \fn standardFrameReceived
     * \param frame Received frame
     */
    virtual void standardFrameReceived(const can_frame& frame);/* SFF was present */
    /*!
     * Called when extended frame was is received from the bus.
     * \fn extendedFrameReceived
     * \param frame Received frame
     */
    virtual void extendedFrameReceived(const can_frame& frame);/* EFF was present */
    /*!
     * Called when error frame was received from the bus.
     * \fn errorFrameReceived
     * \param frame Error frame
     */
    virtual void errorFrameReceived(const can_frame& frame);/* error frame */
    /*!
     * Called when remote transmission frame was received from the bus.
     * \fn remoteTransmissionRequest
     * \param frame RTR frame
     */
    virtual void remoteTransmissionRequest(const can_frame& frame);/* remote transmission request (SFF/EFF is still present)*/

    /**
    * Sends standard(11bit) CAN frame over the bus
    * \fn sendStandardFrame
    * \param frame CAN frame to be sent
    * \return True if frame was sent
    */
    bool sendStandardFrame(const can_frame& frame);

    /**
    * Sends extended(29bit) CAN frame over the bus
    * \fn sendExtendedFrame
    * \param frame CAN frame to be sent
    * \return True if frame was sent
    */
    bool sendExtendedFrame(const can_frame& frame);

    /*!
     * Second phase of the plugin initialization.
     * \fn init
     */
    virtual void init();

    void eraseTimer(const canid_t& id)
    {
        scoped_lock<interprocess_recursive_mutex> lock(mutex);
        auto it = timers.find(id);
        if(it != timers.end()){
            timers.erase(it);
        }
    }

protected:

    void registerMessage(const canid_t& canId, const __u8& canDlc)
    {
        LOG_MESSAGE("registered message: " << canId);
    }

    template<typename Signal, typename... Rest>
    void registerMessage(const canid_t& canId, const __u8& canDlc, Signal* canSignal, Rest... rest)
    {
        static_assert(std::is_base_of<CANSignal, Signal>::value, "CANSignal has to be a base of Signal");

        if(!canSignal)
            return;

        std::shared_ptr<AbstractPropertyType> prop(AmbPluginImpl::addPropertySupport(Zone::None, canSignal->factory()));
        if(prop) {
            canSignal->setAmbProperty(prop);
            auto messageIt = messages.find(canId);
            if(messageIt == messages.end()){
                messageIt = messages.insert(make_pair(canId, CANMessage(canId, canDlc))).first;
            }
            auto& message = messageIt->second;
            message.addSignal(prop->name, std::shared_ptr<CANSignal>(canSignal));
            propertyToMessage[prop->name] = &message;
        }

        registerMessage(canId, canDlc, rest...);
    }

private:

    void printFrame(const can_frame& frame) const;
    void onMessage(const can_frame& frame);
    bool sendValue(AbstractPropertyType* value);
    void registerMessages();

    static void timerDestroyNotify(gpointer data);
    static gboolean timeoutCallback(gpointer data);
//
// data:
//
    std::map< canid_t, CANMessage > messages;
    std::map< VehicleProperty::Property, CANMessage* > propertyToMessage;

    std::string interface;

    /**
    * CANBus class instance
    * \property canBus
    * \private
    */
    std::unique_ptr<CANBus> canBus;

    typedef std::tuple<AmbTmplPlugin*, CANBus*, int, const can_frame> TimerData;
    std::map< canid_t, guint > timers;
    interprocess_recursive_mutex mutex;
    uint announcementIntervalTimer;
    uint announcementCount;
};

#endif /* AMBTMPL_PLUGIN_H_ */