/* * This file is part of aasdk library project. * Copyright (C) 2018 f1x.studio (Michal Szwaj) * * aasdk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * aasdk 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with aasdk. If not, see . */ #include #include #include #include #include #include namespace aasdk { namespace channel { namespace sensor { SensorServiceChannel::SensorServiceChannel(boost::asio::io_service::strand& strand, messenger::IMessenger::Pointer messenger) : ServiceChannel(strand, std::move(messenger), messenger::ChannelId::SENSOR) { } void SensorServiceChannel::receive(ISensorServiceChannelEventHandler::Pointer eventHandler) { auto receivePromise = messenger::ReceivePromise::defer(strand_); receivePromise->then(std::bind(&SensorServiceChannel::messageHandler, this->shared_from_this(), std::placeholders::_1, eventHandler), std::bind(&ISensorServiceChannelEventHandler::onChannelError, eventHandler, std::placeholders::_1)); messenger_->enqueueReceive(channelId_, std::move(receivePromise)); } messenger::ChannelId SensorServiceChannel::getId() const { return channelId_; } void SensorServiceChannel::sendChannelOpenResponse(const proto::messages::ChannelOpenResponse& response, SendPromise::Pointer promise) { auto message(std::make_shared(channelId_, messenger::EncryptionType::ENCRYPTED, messenger::MessageType::CONTROL)); message->insertPayload(messenger::MessageId(proto::ids::ControlMessage::CHANNEL_OPEN_RESPONSE).getData()); message->insertPayload(response); this->send(std::move(message), std::move(promise)); } void SensorServiceChannel::messageHandler(messenger::Message::Pointer message, ISensorServiceChannelEventHandler::Pointer eventHandler) { messenger::MessageId messageId(message->getPayload()); common::DataConstBuffer payload(message->getPayload(), messageId.getSizeOf()); switch(messageId.getId()) { case proto::ids::SensorChannelMessage::SENSOR_START_REQUEST: this->handleSensorStartRequest(payload, std::move(eventHandler)); break; case proto::ids::ControlMessage::CHANNEL_OPEN_REQUEST: this->handleChannelOpenRequest(payload, std::move(eventHandler)); break; default: AASDK_LOG(error) << "[SensorServiceChannel] message not handled: " << messageId.getId(); this->receive(std::move(eventHandler)); break; } } void SensorServiceChannel::sendSensorEventIndication(const proto::messages::SensorEventIndication& indication, SendPromise::Pointer promise) { auto message(std::make_shared(channelId_, messenger::EncryptionType::ENCRYPTED, messenger::MessageType::SPECIFIC)); message->insertPayload(messenger::MessageId(proto::ids::SensorChannelMessage::SENSOR_EVENT_INDICATION).getData()); message->insertPayload(indication); this->send(std::move(message), std::move(promise)); } void SensorServiceChannel::sendSensorStartResponse(const proto::messages::SensorStartResponseMessage& response, SendPromise::Pointer promise) { auto message(std::make_shared(channelId_, messenger::EncryptionType::ENCRYPTED, messenger::MessageType::SPECIFIC)); message->insertPayload(messenger::MessageId(proto::ids::SensorChannelMessage::SENSOR_START_RESPONSE).getData()); message->insertPayload(response); this->send(std::move(message), std::move(promise)); } void SensorServiceChannel::handleSensorStartRequest(const common::DataConstBuffer& payload, ISensorServiceChannelEventHandler::Pointer eventHandler) { proto::messages::SensorStartRequestMessage request; if(request.ParseFromArray(payload.cdata, payload.size)) { eventHandler->onSensorStartRequest(request); } else { eventHandler->onChannelError(error::Error(error::ErrorCode::PARSE_PAYLOAD)); } } void SensorServiceChannel::handleChannelOpenRequest(const common::DataConstBuffer& payload, ISensorServiceChannelEventHandler::Pointer eventHandler) { proto::messages::ChannelOpenRequest request; if(request.ParseFromArray(payload.cdata, payload.size)) { eventHandler->onChannelOpenRequest(request); } else { eventHandler->onChannelError(error::Error(error::ErrorCode::PARSE_PAYLOAD)); } } } } }