summaryrefslogtreecommitdiffstats
path: root/src/RadioImpl.h
blob: 841c72bd47b78e42672fbd09c6b99c59e4fab99f (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
// 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