WifiAsyncChannel.java revision cda805612a020057f7363b8e89be72ca9d6958f9
1/*
2 * Copyright (C) 2016 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
17package com.android.server.wifi.util;
18
19import android.annotation.NonNull;
20import android.os.Message;
21
22import com.android.internal.util.AsyncChannel;
23import com.android.server.wifi.WifiInjector;
24import com.android.server.wifi.WifiLog;
25
26/**
27 * This class subclasses AsyncChannel and adds logging
28 * to the sendMessage() API
29 */
30public class WifiAsyncChannel extends AsyncChannel {
31    private static final String LOG_TAG = "WifiAsyncChannel";
32    private WifiLog mLog;
33    private String mTag;
34    /**
35     * AsyncChannelWithLogging constructor
36     */
37    public WifiAsyncChannel(String serviceTag) {
38        mTag = LOG_TAG + "." + serviceTag;
39    }
40
41    @NonNull
42    private WifiLog getOrInitLog() {
43        // Lazy initization of mLog
44        if (mLog == null) {
45            mLog = WifiInjector.getInstance().makeLog(mTag);
46        }
47        return mLog;
48    }
49
50    /**
51     * Send a message to the destination handler.
52     *
53     * @param msg
54     */
55    @Override
56    public void sendMessage(Message msg) {
57        getOrInitLog().trace("sendMessage message=%")
58            .c(msg.what)
59            .flush();
60        super.sendMessage(msg);
61    }
62
63    /**
64     * Reply to srcMsg
65     *
66     * @param srcMsg
67     * @param dstMsg
68     */
69    @Override
70    public void replyToMessage(Message srcMsg, Message dstMsg) {
71        getOrInitLog()
72                .trace("replyToMessage recvdMessage=% sendingUid=% sentMessage=%")
73                .c(srcMsg.what)
74                .c(srcMsg.sendingUid)
75                .c(dstMsg.what)
76                .flush();
77        super.replyToMessage(srcMsg, dstMsg);
78    }
79
80    /**
81     * Send the Message synchronously.
82     *
83     * @param msg to send
84     * @return reply message or null if an error.
85     */
86    @Override
87    public Message sendMessageSynchronously(Message msg) {
88        getOrInitLog().trace("sendMessageSynchronously.send message=%")
89            .c(msg.what)
90            .flush();
91        Message replyMessage = super.sendMessageSynchronously(msg);
92        getOrInitLog().trace("sendMessageSynchronously.recv message=% sendingUid=%")
93            .c(replyMessage.what)
94            .c(replyMessage.sendingUid)
95            .flush();
96        return replyMessage;
97    }
98}
99