14e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent/*
24e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * Copyright (C) 2015 The Android Open Source Project
34e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent *
44e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
54e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * you may not use this file except in compliance with the License.
64e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * You may obtain a copy of the License at
74e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent *
84e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
94e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent *
104e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * Unless required by applicable law or agreed to in writing, software
114e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
124e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * See the License for the specific language governing permissions and
144e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent * limitations under the License.
154e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent */
164e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
174e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#ifndef ANDROID_HARDWARE_RADIO_SERVICE_H
184e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#define ANDROID_HARDWARE_RADIO_SERVICE_H
194e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
204e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <utils/Vector.h>
214e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent//#include <binder/AppOpsManager.h>
224e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <binder/MemoryDealer.h>
234e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <binder/BinderService.h>
244e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <binder/IAppOpsCallback.h>
254e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <radio/IRadioService.h>
264e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <radio/IRadio.h>
274e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <radio/IRadioClient.h>
284e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <system/radio.h>
294e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#include <hardware/radio.h>
304e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
314e09069a29fc18d0799808cc26f71e9b068e98adEric Laurentnamespace android {
324e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
334e09069a29fc18d0799808cc26f71e9b068e98adEric Laurentclass MemoryHeapBase;
344e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
354e09069a29fc18d0799808cc26f71e9b068e98adEric Laurentclass RadioService :
364e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    public BinderService<RadioService>,
374e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    public BnRadioService
384e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent{
394e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    friend class BinderService<RadioService>;
404e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
414e09069a29fc18d0799808cc26f71e9b068e98adEric Laurentpublic:
424e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    class ModuleClient;
434e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    class Module;
444e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
454e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    static char const* getServiceName() { return "media.radio"; }
464e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
474e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                        RadioService();
484e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual             ~RadioService();
494e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
504e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    // IRadioService
514e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual status_t listModules(struct radio_properties *properties,
524e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                                 uint32_t *numModules);
534e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
544e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual status_t attach(radio_handle_t handle,
554e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                            const sp<IRadioClient>& client,
564e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                            const struct radio_band_config *config,
574e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                            bool withAudio,
584e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                            sp<IRadio>& radio);
594e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
604e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual status_t    onTransact(uint32_t code, const Parcel& data,
614e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                                   Parcel* reply, uint32_t flags);
624e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
634e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual status_t    dump(int fd, const Vector<String16>& args);
644e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
654e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
664e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    class Module : public virtual RefBase {
674e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    public:
684e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
6953810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent       Module(radio_hw_device* hwDevice,
704e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent              struct radio_properties properties);
714e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
724e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual ~Module();
734e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
744e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               sp<ModuleClient> addClient(const sp<IRadioClient>& client,
754e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                                  const struct radio_band_config *config,
764e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                                  bool audio);
774e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
784e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               void removeClient(const sp<ModuleClient>& moduleClient);
794e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
804e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               status_t setMute(bool mute);
814e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
824e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               status_t getMute(bool *mute);
834e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
844e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t dump(int fd, const Vector<String16>& args);
854e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
864e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       const struct radio_hw_device *hwDevice() const { return mHwDevice; }
874e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       const struct radio_properties properties() const { return mProperties; }
884e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       const struct radio_band_config *getDefaultConfig() const ;
894e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
904e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    private:
914e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
9253810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent       void notifyDeviceConnection(bool connected, const char *address);
9353810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent
9453810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        Mutex                         mLock;          // protects  mModuleClients
9553810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        const struct radio_hw_device  *mHwDevice;     // HAL hardware device
9653810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        const struct radio_properties mProperties;    // cached hardware module properties
9753810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        Vector< sp<ModuleClient> >    mModuleClients; // list of attached clients
9853810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        bool                          mMute;          // radio audio source state
9953810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent                                                      // when unmuted, audio is routed to the
10053810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent                                                      // output device selected for media use case.
1014e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    }; // class Module
1024e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1034e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    class CallbackThread : public Thread {
1044e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    public:
1054e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1064e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        CallbackThread(const wp<ModuleClient>& moduleClient);
1074e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1084e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        virtual ~CallbackThread();
1094e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1104e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1114e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        // Thread virtuals
1124e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        virtual bool threadLoop();
1134e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1144e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        // RefBase
1154e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        virtual void onFirstRef();
1164e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1174e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                void exit();
1184e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1194e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                void sendEvent(radio_hal_event_t *halEvent);
1204e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                sp<IMemory> prepareEvent(radio_hal_event_t *halEvent);
1214e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1224e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    private:
12353810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        wp<ModuleClient>      mModuleClient;    // client module the thread belongs to
12453810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        Condition             mCallbackCond;    // condition signaled when a new event is posted
12553810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        Mutex                 mCallbackLock;    // protects mEventQueue
12653810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        Vector< sp<IMemory> > mEventQueue;      // pending callback events
12753810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        sp<MemoryDealer>      mMemoryDealer;    // shared memory for callback event
1284e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    }; // class CallbackThread
1294e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1304e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    class ModuleClient : public BnRadio,
1314e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                   public IBinder::DeathRecipient {
1324e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    public:
1334e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1344e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       ModuleClient(const sp<Module>& module,
1354e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent              const sp<IRadioClient>& client,
1364e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent              const struct radio_band_config *config,
1374e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent              bool audio);
1384e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1394e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual ~ModuleClient();
1404e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1414e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       // IRadio
1424e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual void detach();
1434e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1444e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t setConfiguration(const struct radio_band_config *config);
1454e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1464e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t getConfiguration(struct radio_band_config *config);
1474e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1484e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t setMute(bool mute);
1494e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1504e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t getMute(bool *mute);
1514e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1524e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t scan(radio_direction_t direction, bool skipSubChannel);
1534e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1544e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t step(radio_direction_t direction, bool skipSubChannel);
1554e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1564e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t tune(unsigned int channel, unsigned int subChannel);
1574e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1584e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t cancel();
1594e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1604e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t getProgramInformation(struct radio_program_info *info);
1614e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1624e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t hasControl(bool *hasControl);
1634e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1644e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual status_t dump(int fd, const Vector<String16>& args);
1654e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1664e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               sp<IRadioClient> client() const { return mClient; }
1674e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               wp<Module> module() const { return mModule; }
1684e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               radio_hal_band_config_t halConfig() const;
1694e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               sp<CallbackThread> callbackThread() const { return mCallbackThread; }
1704e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               void setTuner(const struct radio_tuner *tuner);
1714e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               const struct radio_tuner *getTuner() const;
1724e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               bool audio() const { return mAudio; }
1734e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1744e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent               void onCallbackEvent(const sp<IMemory>& event);
1754e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1764e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual void onFirstRef();
1774e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1784e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1794e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       // IBinder::DeathRecipient implementation
1804e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent       virtual void        binderDied(const wp<IBinder> &who);
1814e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1824e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    private:
1834e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
18453810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        mutable Mutex               mLock;           // protects mClient, mConfig and mTuner
18553810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        wp<Module>                  mModule;         // The module this client is attached to
18653810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        sp<IRadioClient>            mClient;         // event callback binder interface
18753810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        radio_band_config_t         mConfig;         // current band configuration
18853810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        sp<CallbackThread>          mCallbackThread; // event callback thread
1894e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent        const bool                  mAudio;
19053810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent        const struct radio_tuner    *mTuner;        // HAL tuner interface. NULL indicates that
19153810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent                                                    // this client does not have control on any
19253810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent                                                    // tuner
1934e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    }; // class ModuleClient
1944e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1954e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1964e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    static void callback(radio_hal_event_t *halEvent, void *cookie);
1974e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
1984e09069a29fc18d0799808cc26f71e9b068e98adEric Laurentprivate:
1994e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
2004e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    virtual void onFirstRef();
2014e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
2024e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    static void convertProperties(radio_properties_t *properties,
2034e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent                                  const radio_hal_properties_t *halProperties);
20453810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent    Mutex               mServiceLock;   // protects mModules
20553810823a6613f54fc0b3c3bc0de267dc1f4e1e5Eric Laurent    volatile int32_t    mNextUniqueId;  // for module ID allocation
2064e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent    DefaultKeyedVector< radio_handle_t, sp<Module> > mModules;
2074e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent};
2084e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
2094e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent} // namespace android
2104e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent
2114e09069a29fc18d0799808cc26f71e9b068e98adEric Laurent#endif // ANDROID_HARDWARE_RADIO_SERVICE_H
212