diff options
author | Scott Murray <scott.murray@konsulko.com> | 2023-01-03 00:51:50 -0500 |
---|---|---|
committer | Scott Murray <scott.murray@konsulko.com> | 2023-01-19 00:37:11 +0000 |
commit | 7f26a2d06410fd3a2768612b9c9daf869778e480 (patch) | |
tree | 4dfffa78e14c8eadb42a731ae15cc87137e17723 /src/RadioImpl.h | |
parent | 6191be9c1e4b628f60ccaeabcef2aaa9de00800b (diff) |
Repurpose into gRPC servicequillback_17.0.0quillback/17.0.017.0.0
Repurpose repository into a spiritual successor of the previous
binding. The backend code is retained behind a new gRPC API
defined in protos/radio.proto. The simpler synchronous gRPC API
had been used for expediency, this may warrant revisiting to
rework into an async or callback API based server instead. As
well, authentication has been left until some consensus on an
approach can be worked out.
Bug-AGL: SPEC-4665
Signed-off-by: Scott Murray <scott.murray@konsulko.com>
Change-Id: I28b122ce6e0ecfc7504aa08b90394cb1b9e22976
(cherry picked from commit dd23c157bdba1b25bbb50cdb99a60aa597735f43)
Diffstat (limited to 'src/RadioImpl.h')
-rw-r--r-- | src/RadioImpl.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/RadioImpl.h b/src/RadioImpl.h new file mode 100644 index 0000000..841c72b --- /dev/null +++ b/src/RadioImpl.h @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright (C) 2022 Konsulko Group + */ + +#ifndef RADIO_IMPL_H +#define RADIO_IMPL_H + +#include <mutex> +#include <list> +#include <condition_variable> + +#include <grpcpp/ext/proto_server_reflection_plugin.h> +#include <grpcpp/grpcpp.h> +#include <grpcpp/health_check_service_interface.h> + +#include "radio.grpc.pb.h" +extern "C" { +#include "radio_impl.h" +} + +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::ServerWriter; +using grpc::Status; + +using automotivegradelinux::Radio; +using automotivegradelinux::GetFrequencyRequest; +using automotivegradelinux::GetFrequencyResponse; +using automotivegradelinux::SetFrequencyRequest; +using automotivegradelinux::SetFrequencyResponse; +using automotivegradelinux::GetBandRequest; +using automotivegradelinux::GetBandResponse; +using automotivegradelinux::SetBandRequest; +using automotivegradelinux::SetBandResponse; +using automotivegradelinux::GetBandSupportedRequest; +using automotivegradelinux::GetBandSupportedResponse; +using automotivegradelinux::GetBandParametersRequest; +using automotivegradelinux::GetBandParametersResponse; +using automotivegradelinux::GetStereoModeRequest; +using automotivegradelinux::GetStereoModeResponse; +using automotivegradelinux::SetStereoModeRequest; +using automotivegradelinux::SetStereoModeResponse; +using automotivegradelinux::GetStereoModeRequest; +using automotivegradelinux::StartRequest; +using automotivegradelinux::StartResponse; +using automotivegradelinux::StopRequest; +using automotivegradelinux::StopResponse; +using automotivegradelinux::ScanStartRequest; +using automotivegradelinux::ScanStartResponse; +using automotivegradelinux::ScanStopRequest; +using automotivegradelinux::ScanStopResponse; +using automotivegradelinux::GetRDSRequest; +using automotivegradelinux::GetRDSResponse; +using automotivegradelinux::GetQualityRequest; +using automotivegradelinux::GetQualityResponse; +using automotivegradelinux::SetAlternativeFrequencyRequest; +using automotivegradelinux::SetAlternativeFrequencyResponse; +using automotivegradelinux::StatusRequest; +using automotivegradelinux::StatusResponse; + + +class RadioImpl final : public Radio::Service +{ +public: + explicit RadioImpl(); + + bool Detect(); + + Status GetFrequency(ServerContext* context, const GetFrequencyRequest* request, GetFrequencyResponse* response) override; + Status SetFrequency(ServerContext* context, const SetFrequencyRequest* request, SetFrequencyResponse* response) override; + Status GetBand(ServerContext* context, const GetBandRequest* request, GetBandResponse* response) override; + Status SetBand(ServerContext* context, const SetBandRequest* request, SetBandResponse* response) override; + Status GetBandSupported(ServerContext* context, const GetBandSupportedRequest* request, GetBandSupportedResponse* response) override; + Status GetBandParameters(ServerContext* context, const GetBandParametersRequest* request, GetBandParametersResponse* response) override; + Status GetStereoMode(ServerContext* context, const GetStereoModeRequest* request, GetStereoModeResponse* response) override; + Status SetStereoMode(ServerContext* context, const SetStereoModeRequest* request, SetStereoModeResponse* response) override; + Status Start(ServerContext* context, const StartRequest* request, StartResponse* response) override; + Status Stop(ServerContext* context, const StopRequest* request, StopResponse* response) override; + Status ScanStart(ServerContext* context, const ScanStartRequest* request, ScanStartResponse* response) override; + Status ScanStop(ServerContext* context, const ScanStopRequest* request, ScanStopResponse* response) override; + Status GetRDS(ServerContext* context, const GetRDSRequest* request, GetRDSResponse* response) override; + Status GetQuality(ServerContext* context, const GetQualityRequest* request, GetQualityResponse* response) override; + Status SetAlternativeFrequency(ServerContext* context, const SetAlternativeFrequencyRequest* request, SetAlternativeFrequencyResponse* response) override; + Status GetStatusEvents(ServerContext* context, const StatusRequest* request, ServerWriter<StatusResponse>* writer) override; + + void Shutdown() { m_done = true; m_done_cv.notify_all(); } + + void SendStatusResponse(StatusResponse &response); + + static void FrequencyCallback(uint32_t frequency, void *data) { + if (data) + ((RadioImpl*) data)->HandleFrequencyEvent(frequency); + } + + static void ScanCallback(uint32_t frequency, void *data) { + if (data) + ((RadioImpl*) data)->HandleScanEvent(frequency); + } + + static void RDSCallback(void *rds_data, void *data) { + if (data) + ((RadioImpl*) data)->HandleRDSEvent(rds_data); + } + +private: + void HandleFrequencyEvent(uint32_t frequency); + void HandleScanEvent(uint32_t frequency); + void HandleRDSEvent(void *rds_data); + + std::mutex m_clients_mutex; + std::list<std::pair<ServerContext*, ServerWriter<StatusResponse>*> > m_clients; + + std::mutex m_done_mutex; + std::condition_variable m_done_cv; + bool m_done = false; + + radio_impl_ops_t *m_radio_impl_ops = NULL; + bool m_playing = false; +}; + +#endif // RADIO_IMPL_H |