1ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson/*
2a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson *  Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
3ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *
4ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * Redistribution and use in source and binary forms, with or without
5ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * modification, are permitted provided that the following conditions are
6ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * met:
7ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *     * Redistributions of source code must retain the above copyright
8ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       notice, this list of conditions and the following disclaimer.
9ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *     * Redistributions in binary form must reproduce the above
10ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       copyright notice, this list of conditions and the following
11ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       disclaimer in the documentation and/or other materials provided
12ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       with the distribution.
13ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *     * Neither the name of The Linux Foundation nor the names of its
14ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       contributors may be used to endorse or promote products derived
15ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *       from this software without specific prior written permission.
16ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson *
17ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson */
29ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
30ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson#include <QService.h>
3192e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed#include <binder/Parcel.h>
3292e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed#include <binder/IPCThreadState.h>
33ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
34ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson#define QSERVICE_DEBUG 0
35ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
36ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilsonusing namespace android;
37ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
38ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilsonnamespace qService {
39ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
40ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon WilsonQService* QService::sQService = NULL;
41ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson// ----------------------------------------------------------------------------
42ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon WilsonQService::QService()
43ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson{
44ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    ALOGD_IF(QSERVICE_DEBUG, "QService Constructor invoked");
45ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
46ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
47ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon WilsonQService::~QService()
48ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson{
49ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    ALOGD_IF(QSERVICE_DEBUG,"QService Destructor invoked");
50ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
51ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
52ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilsonvoid QService::connect(const sp<qClient::IQClient>& client) {
53a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson    ALOGD_IF(QSERVICE_DEBUG,"client connected");
54ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    mClient = client;
55ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
56ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
57a653efede03423aa840da24634f1ec6f20796f1eSimon Wilsonstatus_t QService::dispatch(uint32_t command, const Parcel* inParcel,
58a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson        Parcel* outParcel) {
59a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson    status_t err = (status_t) FAILED_TRANSACTION;
6092e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed    IPCThreadState* ipc = IPCThreadState::self();
6192e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed    //Rewind parcel in case we're calling from the same process
6292e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed    if (ipc->getCallingPid() == getpid())
6392e5d1f467b1c867636de9acdebac440c9989f86Naseer Ahmed        inParcel->setDataPosition(0);
64a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson    if (mClient.get()) {
65a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson        ALOGD_IF(QSERVICE_DEBUG, "Dispatching command: %d", command);
66a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson        err = mClient->notifyCallback(command, inParcel, outParcel);
67ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    }
68a653efede03423aa840da24634f1ec6f20796f1eSimon Wilson    return err;
69ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
70ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
71ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilsonvoid QService::init()
72ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson{
73ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    if(!sQService) {
74ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson        sQService = new QService();
75ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson        sp<IServiceManager> sm = defaultServiceManager();
76ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson        sm->addService(String16("display.qservice"), sQService);
77ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson        if(sm->checkService(String16("display.qservice")) != NULL)
78ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson            ALOGD_IF(QSERVICE_DEBUG, "adding display.qservice succeeded");
79ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson        else
80ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson            ALOGD_IF(QSERVICE_DEBUG, "adding display.qservice failed");
81ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson    }
82ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
83ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson
84ef53c1c84ec55ed50f607d52b7abfbb86239408eSimon Wilson}
85