blob: 444eb7a35c587474620eec83f35ab43869a6c09f [file] [log] [blame]
Eric Laurent4e090692015-03-05 15:12:40 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_RADIO_SERVICE_H
18#define ANDROID_HARDWARE_RADIO_SERVICE_H
19
20#include <utils/Vector.h>
21//#include <binder/AppOpsManager.h>
22#include <binder/MemoryDealer.h>
23#include <binder/BinderService.h>
24#include <binder/IAppOpsCallback.h>
25#include <radio/IRadioService.h>
26#include <radio/IRadio.h>
27#include <radio/IRadioClient.h>
28#include <system/radio.h>
29#include <hardware/radio.h>
Eric Laurent01d267e2016-10-21 08:16:10 -070030#include "RadioInterface.h"
31#include "TunerInterface.h"
32#include "TunerCallbackInterface.h"
Eric Laurent4e090692015-03-05 15:12:40 -080033
34namespace android {
35
36class MemoryHeapBase;
37
38class RadioService :
39 public BinderService<RadioService>,
40 public BnRadioService
41{
42 friend class BinderService<RadioService>;
43
44public:
45 class ModuleClient;
46 class Module;
47
48 static char const* getServiceName() { return "media.radio"; }
49
50 RadioService();
51 virtual ~RadioService();
52
53 // IRadioService
54 virtual status_t listModules(struct radio_properties *properties,
55 uint32_t *numModules);
56
57 virtual status_t attach(radio_handle_t handle,
58 const sp<IRadioClient>& client,
59 const struct radio_band_config *config,
60 bool withAudio,
61 sp<IRadio>& radio);
62
63 virtual status_t onTransact(uint32_t code, const Parcel& data,
64 Parcel* reply, uint32_t flags);
65
66 virtual status_t dump(int fd, const Vector<String16>& args);
67
68
69 class Module : public virtual RefBase {
70 public:
71
Eric Laurent01d267e2016-10-21 08:16:10 -070072 Module(sp<RadioInterface> hwDevice,
Eric Laurent4e090692015-03-05 15:12:40 -080073 struct radio_properties properties);
74
75 virtual ~Module();
76
77 sp<ModuleClient> addClient(const sp<IRadioClient>& client,
78 const struct radio_band_config *config,
79 bool audio);
80
81 void removeClient(const sp<ModuleClient>& moduleClient);
82
83 status_t setMute(bool mute);
84
85 status_t getMute(bool *mute);
86
87 virtual status_t dump(int fd, const Vector<String16>& args);
88
Eric Laurent01d267e2016-10-21 08:16:10 -070089 sp<RadioInterface> hwDevice() const { return mHwDevice; }
Eric Laurent4e090692015-03-05 15:12:40 -080090 const struct radio_properties properties() const { return mProperties; }
91 const struct radio_band_config *getDefaultConfig() const ;
92
Eric Laurent4e090692015-03-05 15:12:40 -080093 private:
94
Eric Laurent53810822015-03-12 09:12:01 -070095 void notifyDeviceConnection(bool connected, const char *address);
96
97 Mutex mLock; // protects mModuleClients
Eric Laurent01d267e2016-10-21 08:16:10 -070098 sp<RadioInterface> mHwDevice; // HAL hardware device
Eric Laurent53810822015-03-12 09:12:01 -070099 const struct radio_properties mProperties; // cached hardware module properties
100 Vector< sp<ModuleClient> > mModuleClients; // list of attached clients
101 bool mMute; // radio audio source state
102 // when unmuted, audio is routed to the
103 // output device selected for media use case.
Eric Laurent4e090692015-03-05 15:12:40 -0800104 }; // class Module
105
106 class CallbackThread : public Thread {
107 public:
108
Chih-Hung Hsieh62503ee2016-08-09 14:06:20 -0700109 explicit CallbackThread(const wp<ModuleClient>& moduleClient);
Eric Laurent4e090692015-03-05 15:12:40 -0800110
111 virtual ~CallbackThread();
112
113
114 // Thread virtuals
115 virtual bool threadLoop();
116
117 // RefBase
118 virtual void onFirstRef();
119
120 void exit();
121
122 void sendEvent(radio_hal_event_t *halEvent);
123 sp<IMemory> prepareEvent(radio_hal_event_t *halEvent);
124
125 private:
Eric Laurent53810822015-03-12 09:12:01 -0700126 wp<ModuleClient> mModuleClient; // client module the thread belongs to
127 Condition mCallbackCond; // condition signaled when a new event is posted
128 Mutex mCallbackLock; // protects mEventQueue
129 Vector< sp<IMemory> > mEventQueue; // pending callback events
130 sp<MemoryDealer> mMemoryDealer; // shared memory for callback event
Eric Laurent4e090692015-03-05 15:12:40 -0800131 }; // class CallbackThread
132
133 class ModuleClient : public BnRadio,
Eric Laurent01d267e2016-10-21 08:16:10 -0700134 public IBinder::DeathRecipient,
135 public TunerCallbackInterface {
Eric Laurent4e090692015-03-05 15:12:40 -0800136 public:
137
138 ModuleClient(const sp<Module>& module,
139 const sp<IRadioClient>& client,
140 const struct radio_band_config *config,
141 bool audio);
142
143 virtual ~ModuleClient();
144
145 // IRadio
146 virtual void detach();
147
148 virtual status_t setConfiguration(const struct radio_band_config *config);
149
150 virtual status_t getConfiguration(struct radio_band_config *config);
151
152 virtual status_t setMute(bool mute);
153
154 virtual status_t getMute(bool *mute);
155
156 virtual status_t scan(radio_direction_t direction, bool skipSubChannel);
157
158 virtual status_t step(radio_direction_t direction, bool skipSubChannel);
159
160 virtual status_t tune(unsigned int channel, unsigned int subChannel);
161
162 virtual status_t cancel();
163
164 virtual status_t getProgramInformation(struct radio_program_info *info);
165
166 virtual status_t hasControl(bool *hasControl);
167
168 virtual status_t dump(int fd, const Vector<String16>& args);
169
170 sp<IRadioClient> client() const { return mClient; }
171 wp<Module> module() const { return mModule; }
172 radio_hal_band_config_t halConfig() const;
173 sp<CallbackThread> callbackThread() const { return mCallbackThread; }
Eric Laurent01d267e2016-10-21 08:16:10 -0700174 void setTuner(sp<TunerInterface>& tuner);
175 sp<TunerInterface>& getTuner();
Eric Laurent4e090692015-03-05 15:12:40 -0800176 bool audio() const { return mAudio; }
177
178 void onCallbackEvent(const sp<IMemory>& event);
179
180 virtual void onFirstRef();
181
182
183 // IBinder::DeathRecipient implementation
184 virtual void binderDied(const wp<IBinder> &who);
185
Eric Laurent01d267e2016-10-21 08:16:10 -0700186 // TunerCallbackInterface
187 virtual void onEvent(radio_hal_event_t *event);
188
Eric Laurent4e090692015-03-05 15:12:40 -0800189 private:
190
Eric Laurent53810822015-03-12 09:12:01 -0700191 mutable Mutex mLock; // protects mClient, mConfig and mTuner
192 wp<Module> mModule; // The module this client is attached to
193 sp<IRadioClient> mClient; // event callback binder interface
194 radio_band_config_t mConfig; // current band configuration
195 sp<CallbackThread> mCallbackThread; // event callback thread
Eric Laurent4e090692015-03-05 15:12:40 -0800196 const bool mAudio;
Eric Laurent01d267e2016-10-21 08:16:10 -0700197 sp<TunerInterface> mTuner; // HAL tuner interface. NULL indicates that
Eric Laurent53810822015-03-12 09:12:01 -0700198 // this client does not have control on any
199 // tuner
Eric Laurent4e090692015-03-05 15:12:40 -0800200 }; // class ModuleClient
201
202
203 static void callback(radio_hal_event_t *halEvent, void *cookie);
204
205private:
206
207 virtual void onFirstRef();
208
209 static void convertProperties(radio_properties_t *properties,
210 const radio_hal_properties_t *halProperties);
Eric Laurent53810822015-03-12 09:12:01 -0700211 Mutex mServiceLock; // protects mModules
212 volatile int32_t mNextUniqueId; // for module ID allocation
Eric Laurent4e090692015-03-05 15:12:40 -0800213 DefaultKeyedVector< radio_handle_t, sp<Module> > mModules;
214};
215
216} // namespace android
217
218#endif // ANDROID_HARDWARE_RADIO_SERVICE_H