summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/src/rc_command_factory.cc
blob: ecfc1dcda4232427888fd7dae6293fe542d211a9 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 Copyright (c) 2018, Ford Motor Company
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.

 Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following
 disclaimer in the documentation and/or other materials provided with the
 distribution.

 Neither the name of the copyright holders nor the names of their contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */
#include <iostream>

#include "rc_rpc_plugin/rc_command_factory.h"
#include "rc_rpc_plugin/commands/mobile/button_press_request.h"
#include "rc_rpc_plugin/commands/mobile/button_press_response.h"
#include "rc_rpc_plugin/commands/mobile/get_interior_vehicle_data_request.h"
#include "rc_rpc_plugin/commands/mobile/get_interior_vehicle_data_response.h"
#include "rc_rpc_plugin/commands/hmi/rc_get_interior_vehicle_data_consent_request.h"
#include "rc_rpc_plugin/commands/hmi/rc_get_interior_vehicle_data_consent_response.h"
#include "rc_rpc_plugin/commands/mobile/set_interior_vehicle_data_request.h"
#include "rc_rpc_plugin/commands/mobile/set_interior_vehicle_data_response.h"
#include "rc_rpc_plugin/commands/mobile/on_interior_vehicle_data_notification.h"
#include "rc_rpc_plugin/commands/hmi/rc_button_press_request.h"
#include "rc_rpc_plugin/commands/hmi/rc_button_press_response.h"
#include "rc_rpc_plugin/commands/hmi/rc_get_interior_vehicle_data_request.h"
#include "rc_rpc_plugin/commands/hmi/rc_get_interior_vehicle_data_response.h"
#include "rc_rpc_plugin/commands/hmi/rc_on_interior_vehicle_data_notification.h"
#include "rc_rpc_plugin/commands/hmi/rc_on_remote_control_settings_notification.h"
#include "rc_rpc_plugin/commands/hmi/rc_set_interior_vehicle_data_request.h"
#include "rc_rpc_plugin/commands/hmi/rc_set_interior_vehicle_data_response.h"

#include "interfaces/MOBILE_API.h"
#include "interfaces/HMI_API.h"

#include "rc_rpc_plugin/resource_allocation_manager.h"
#include "rc_rpc_plugin/interior_data_cache.h"

CREATE_LOGGERPTR_GLOBAL(logger_, "RemoteControlModule")
namespace application_manager {
using rc_rpc_plugin::ResourceAllocationManager;
using rc_rpc_plugin::InteriorDataCache;
using rc_rpc_plugin::RCCommandParams;

template <typename RCCommandType>
class RCCommandCreator : public CommandCreator {
 public:
  RCCommandCreator(const RCCommandParams& params) : params_(params) {}

 private:
  bool CanBeCreated() const override {
    return true;
  }

  CommandSharedPtr create(
      const commands::MessageSharedPtr& message) const override {
    CommandSharedPtr command(new RCCommandType(message, params_));
    return command;
  }

  RCCommandParams params_;
};

struct RCInvalidCommand {};

template <>
class RCCommandCreator<RCInvalidCommand> : public CommandCreator {
 public:
  RCCommandCreator(const RCCommandParams& params) {
    UNUSED(params);
  }

 private:
  bool CanBeCreated() const override {
    return false;
  }

  CommandSharedPtr create(
      const commands::MessageSharedPtr& message) const override {
    UNUSED(message);
    return CommandSharedPtr();
  }
};

struct RCCommandCreatorFactory {
  RCCommandCreatorFactory(const RCCommandParams& params) : params_(params) {}

  template <typename RCCommandType>
  CommandCreator& GetCreator() {
    LOG4CXX_AUTO_TRACE(logger_);
    static RCCommandCreator<RCCommandType> res(params_);
    return res;
  }
  const RCCommandParams params_;
};
}

namespace rc_rpc_plugin {
using namespace application_manager;

RCCommandFactory::RCCommandFactory(const RCCommandParams& params)
    : params_(params) {}

CommandSharedPtr RCCommandFactory::CreateCommand(
    const app_mngr::commands::MessageSharedPtr& message,
    app_mngr::commands::Command::CommandSource source) {
  if (app_mngr::commands::Command::SOURCE_HMI == source) {
    hmi_apis::messageType::eType message_type =
        static_cast<hmi_apis::messageType::eType>(
            (*message)[strings::params][strings::message_type].asInt());

    hmi_apis::FunctionID::eType function_id =
        static_cast<hmi_apis::FunctionID::eType>(
            (*message)[strings::params][strings::function_id].asInt());

    return get_hmi_creator_factory(function_id, message_type).create(message);
  } else {
    mobile_apis::messageType::eType message_type =
        static_cast<mobile_apis::messageType::eType>(
            (*message)[strings::params][strings::message_type].asInt());

    mobile_apis::FunctionID::eType function_id =
        static_cast<mobile_apis::FunctionID::eType>(
            (*message)[strings::params][strings::function_id].asInt());

    return get_mobile_creator_factory(function_id, message_type)
        .create(message);
  }
}

bool RCCommandFactory::IsAbleToProcess(
    const int32_t function_id,
    const application_manager::commands::Command::CommandSource message_source)
    const {
  using app_mngr::commands::Command;
  if (Command::SOURCE_HMI == message_source) {
    return get_hmi_creator_factory(
               static_cast<hmi_apis::FunctionID::eType>(function_id),
               hmi_apis::messageType::INVALID_ENUM).CanBeCreated();
  } else {
    return get_mobile_creator_factory(
               static_cast<mobile_api::FunctionID::eType>(function_id),
               mobile_api::messageType::INVALID_ENUM).CanBeCreated();
  }
}

CommandCreator& RCCommandFactory::get_mobile_creator_factory(
    mobile_api::FunctionID::eType id,
    mobile_api::messageType::eType message_type) const {
  LOG4CXX_DEBUG(logger_,
                "CreateMobileCommand function_id: " << id << " message_type: "
                                                    << message_type);
  RCCommandCreatorFactory rc_factory(params_);

  switch (id) {
    case mobile_apis::FunctionID::ButtonPressID: {
      return mobile_api::messageType::request == message_type
                 ? rc_factory.GetCreator<commands::ButtonPressRequest>()
                 : rc_factory.GetCreator<commands::ButtonPressResponse>();
    }
    case mobile_apis::FunctionID::GetInteriorVehicleDataID: {
      return mobile_api::messageType::request == message_type
                 ? rc_factory
                       .GetCreator<commands::GetInteriorVehicleDataRequest>()
                 : rc_factory
                       .GetCreator<commands::GetInteriorVehicleDataResponse>();
    }
    case mobile_apis::FunctionID::SetInteriorVehicleDataID: {
      return mobile_api::messageType::request == message_type
                 ? rc_factory
                       .GetCreator<commands::SetInteriorVehicleDataRequest>()
                 : rc_factory
                       .GetCreator<commands::SetInteriorVehicleDataResponse>();
    }
    case mobile_apis::FunctionID::OnInteriorVehicleDataID: {
      return rc_factory
          .GetCreator<commands::OnInteriorVehicleDataNotification>();
    }
    default: { return rc_factory.GetCreator<RCInvalidCommand>(); }
  }
}

CommandCreator& RCCommandFactory::get_hmi_creator_factory(
    hmi_apis::FunctionID::eType id,
    hmi_apis::messageType::eType message_type) const {
  LOG4CXX_DEBUG(logger_,
                "CreateHMICommand function_id: " << id << " message_type: "
                                                 << message_type);

  RCCommandCreatorFactory rc_factory(params_);

  switch (id) {
    case hmi_apis::FunctionID::Buttons_ButtonPress: {
      return hmi_apis::messageType::request == message_type
                 ? rc_factory.GetCreator<commands::RCButtonPressRequest>()
                 : rc_factory.GetCreator<commands::RCButtonPressResponse>();
    }
    case hmi_apis::FunctionID::RC_GetInteriorVehicleData: {
      return hmi_apis::messageType::request == message_type
                 ? rc_factory
                       .GetCreator<commands::RCGetInteriorVehicleDataRequest>()
                 : rc_factory.GetCreator<
                       commands::RCGetInteriorVehicleDataResponse>();
    }
    case hmi_apis::FunctionID::RC_GetInteriorVehicleDataConsent: {
      return hmi_apis::messageType::request == message_type
                 ? rc_factory.GetCreator<
                       commands::RCGetInteriorVehicleDataConsentRequest>()
                 : rc_factory.GetCreator<
                       commands::RCGetInteriorVehicleDataConsentResponse>();
    }
    case hmi_apis::FunctionID::RC_SetInteriorVehicleData: {
      return hmi_apis::messageType::request == message_type
                 ? rc_factory
                       .GetCreator<commands::RCSetInteriorVehicleDataRequest>()
                 : rc_factory.GetCreator<
                       commands::RCSetInteriorVehicleDataResponse>();
    }
    case hmi_apis::FunctionID::RC_OnInteriorVehicleData: {
      return rc_factory
          .GetCreator<commands::RCOnInteriorVehicleDataNotification>();
    }
    case hmi_apis::FunctionID::RC_OnRemoteControlSettings: {
      return rc_factory
          .GetCreator<commands::RCOnRemoteControlSettingsNotification>();
    }
    default: { return rc_factory.GetCreator<RCInvalidCommand>(); }
  }
}
}