summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusAttribute.hpp
blob: 2fd5bd35a2ad31c2011577373b232a71106ebb28 (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
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_
#define COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_

#include <cstdint>
#include <tuple>

#include <CommonAPI/DBus/DBusConfig.hpp>
#include <CommonAPI/DBus/DBusEvent.hpp>
#include <CommonAPI/DBus/DBusProxyHelper.hpp>

namespace CommonAPI {
namespace DBus {

template <typename AttributeType_, typename AttributeDepl_ = EmptyDeployment>
class DBusReadonlyAttribute: public AttributeType_ {
public:
    typedef typename AttributeType_::ValueType ValueType;
    typedef AttributeDepl_ ValueTypeDepl;
    typedef typename AttributeType_::AttributeAsyncCallback AttributeAsyncCallback;

    DBusReadonlyAttribute(DBusProxy &_proxy,
                          const char *setMethodSignature, const char *getMethodName,
                          AttributeDepl_ *_depl = nullptr)
        : proxy_(_proxy),
          getMethodName_(getMethodName),
          setMethodSignature_(setMethodSignature),
          depl_(_depl)    {
        if (NULL == getMethodName) {
            COMMONAPI_ERROR(std::string(__FUNCTION__) + ": getMethodName is NULL");
        }
    }

    void getValue(CommonAPI::CallStatus &_status, ValueType &_value, const CommonAPI::CallInfo *_info) const {
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedValue(depl_);
        DBusProxyHelper<
            DBusSerializableArguments<
            >,
            DBusSerializableArguments<
                CommonAPI::Deployable<
                    ValueType,
                    AttributeDepl_
                >
            >
        >::callMethodWithReply(proxy_, getMethodName_, "", (_info ? _info : &defaultCallInfo), _status, deployedValue);
        _value = deployedValue.getValue();
    }

    std::future<CallStatus> getValueAsync(AttributeAsyncCallback _callback, const CommonAPI::CallInfo *_info) {
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedValue(depl_);
        return DBusProxyHelper<
                    DBusSerializableArguments<>,
                    DBusSerializableArguments<CommonAPI::Deployable<ValueType, AttributeDepl_>>
               >::callMethodAsync(proxy_, getMethodName_, "", (_info ? _info : &defaultCallInfo),
                    [_callback](CommonAPI::CallStatus _status, CommonAPI::Deployable<ValueType, AttributeDepl_> _response) {
                        _callback(_status, _response.getValue());
                    },
                    std::make_tuple(deployedValue));
    }

 protected:
    DBusProxy &proxy_;
    const char *getMethodName_;
    const char *setMethodSignature_;
    AttributeDepl_ *depl_;
};

template <typename AttributeType_, typename AttributeDepl_ = EmptyDeployment>
class DBusAttribute: public DBusReadonlyAttribute<AttributeType_, AttributeDepl_> {
public:
    typedef typename AttributeType_::ValueType ValueType;
    typedef typename AttributeType_::AttributeAsyncCallback AttributeAsyncCallback;

    DBusAttribute(DBusProxy &_proxy,
                  const char *_setMethodName, const char *_setMethodSignature, const char *_getMethodName,
                  AttributeDepl_ *_depl = nullptr)
        : DBusReadonlyAttribute<AttributeType_, AttributeDepl_>(_proxy, _setMethodSignature, _getMethodName, _depl),
            setMethodName_(_setMethodName),
            setMethodSignature_(_setMethodSignature) {
        if (NULL == _setMethodName) {
            COMMONAPI_ERROR(std::string(__FUNCTION__) + ": _setMethodName is NULL");
        }
        if (NULL == _setMethodSignature) {
            COMMONAPI_ERROR(std::string(__FUNCTION__) + ": _setMethodSignature is NULL");
        }
    }

    void setValue(const ValueType &_request, CommonAPI::CallStatus &_status, ValueType &_response, const CommonAPI::CallInfo *_info) {
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedRequest(_request, this->depl_);
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedResponse(this->depl_);
        DBusProxyHelper<DBusSerializableArguments<CommonAPI::Deployable<ValueType, AttributeDepl_>>,
                        DBusSerializableArguments<CommonAPI::Deployable<ValueType, AttributeDepl_>> >::callMethodWithReply(
                                this->proxy_,
                                setMethodName_,
                                setMethodSignature_,
                                (_info ? _info : &defaultCallInfo),
                                deployedRequest,
                                _status,
                                deployedResponse);
        _response = deployedResponse.getValue();
    }


    std::future<CallStatus> setValueAsync(const ValueType &_request, AttributeAsyncCallback _callback, const CommonAPI::CallInfo *_info) {
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedRequest(_request, this->depl_);
        CommonAPI::Deployable<ValueType, AttributeDepl_> deployedResponse(this->depl_);
        return DBusProxyHelper<DBusSerializableArguments<CommonAPI::Deployable<ValueType, AttributeDepl_>>,
                               DBusSerializableArguments<CommonAPI::Deployable<ValueType, AttributeDepl_>> >::callMethodAsync(
                                       this->proxy_,
                                       setMethodName_,
                                       setMethodSignature_,
                                       (_info ? _info : &defaultCallInfo),
                                       deployedRequest,
                                       [_callback](CommonAPI::CallStatus _status, CommonAPI::Deployable<ValueType, AttributeDepl_> _response) {
                                            _callback(_status, _response.getValue());
                                       },
                                       std::make_tuple(deployedResponse));
    }

 protected:
    const char* setMethodName_;
    const char* setMethodSignature_;
};

template <typename AttributeType_>
class DBusObservableAttribute: public AttributeType_ {
public:
    typedef typename AttributeType_::ValueType ValueType;
    typedef typename AttributeType_::ValueTypeDepl ValueTypeDepl;
    typedef typename AttributeType_::AttributeAsyncCallback AttributeAsyncCallback;
    typedef typename AttributeType_::ChangedEvent ChangedEvent;

    template <typename... AttributeType_Arguments>
    DBusObservableAttribute(DBusProxy &_proxy,
                            const char *_changedEventName,
                            AttributeType_Arguments... arguments)
         : AttributeType_(_proxy, arguments...),
           changedEvent_(_proxy, _changedEventName, this->setMethodSignature_, this->getMethodName_,
                               std::make_tuple(CommonAPI::Deployable<ValueType, ValueTypeDepl>(this->depl_))) {
    }

    ChangedEvent &getChangedEvent() {
        return changedEvent_;
    }

 protected:
    DBusEvent<ChangedEvent, CommonAPI::Deployable<ValueType, ValueTypeDepl> > changedEvent_;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_