Files
MuditaOS/module-services/service-bluetooth/ServiceBluetooth.cpp
Adam Dobrowolski d3b1a08e68 [EDG-2135] Bluetooth: bluetooth r/w control imporoved
* Improved bluetooth library interface write in one demand
* can start scan
* not used BT options hidden
* still some issues on flood - transaction style interface should be considered
2019-11-20 12:42:03 +01:00

95 lines
3.1 KiB
C++

#include "ServiceBluetooth.hpp"
#include "Service/Service.hpp"
#include "Service/Message.hpp"
#include "MessageType.hpp"
#include "messages/BluetoothMessage.hpp"
#include <log/log.hpp>
#include <service-lwip/ServiceLwIP.hpp>
const char *ServiceBluetooth::serviceName = "ServiceBluetooth";
ServiceBluetooth::ServiceBluetooth() : sys::Service(serviceName) {
LOG_INFO("[ServiceBluetooth] Initializing");
testTimerID = CreateTimer(3000,true);
ReloadTimer(testTimerID);
}
ServiceBluetooth::~ServiceBluetooth() {
LOG_INFO("[ServiceBluetooth] Cleaning resources");
}
// Invoked when timer ticked
void ServiceBluetooth::TickHandler(uint32_t id) {
}
// This code is experimental:
// this means it is an init point of bluetooth feature handling
sys::ReturnCodes ServiceBluetooth::InitHandler() {
LOG_ERROR("Bluetooth experimental!");
worker=std::make_unique<BluetoothWorker>(this);
return sys::ReturnCodes::Success;
}
sys::ReturnCodes ServiceBluetooth::DeinitHandler() {
return sys::ReturnCodes::Success;
}
sys::Message_t ServiceBluetooth::DataReceivedHandler(sys::DataMessage* msg,sys::ResponseMessage* resp) {
try {
switch (static_cast<MessageType>(msg->messageType)) {
case MessageType::BluetoothRequest: {
BluetoothMessage *lmsg = dynamic_cast<BluetoothMessage *>(msg);
LOG_INFO("Bluetooth request!");
switch (lmsg->req) {
case BluetoothMessage::Start:
worker->run();
break;
case BluetoothMessage::Scan:
if(worker->scan()) {
return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Success);
} else {
return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Failure);
}
break;
case BluetoothMessage::PAN: {
/// request lwip first...
LOG_INFO("Request LwIP running!");
// TODO blocking message - wrecks system
// auto ret = message_lwip(this, LwIP_message::Request::Start);
// if (ret != sys::ReturnCodes::Success) {
// LOG_ERROR("Request for LwIP start failed");
// }
// else {
/// TODO request PPP
LOG_INFO("Start PAN");
worker->start_pan();
// }
} break;
case BluetoothMessage::Visible:
worker->set_visible();
break;
default:
break;
}
break;
}
default:
LOG_INFO("BT not handled!");
break;
}
} catch (std::exception &ex) {
LOG_ERROR("Exception on BtService!: %s", ex.what());
}
return std::make_shared<sys::ResponseMessage>();
}
sys::ReturnCodes ServiceBluetooth::SwitchPowerModeHandler(const sys::ServicePowerMode mode)
{
LOG_ERROR("TODO");
return sys::ReturnCodes::Success;
}