1cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao/*
2cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * Copyright (C) 2016 The Android Open Source Project
3cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao *
4cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * Licensed under the Apache License, Version 2.0 (the "License");
5cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * you may not use this file except in compliance with the License.
6cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * You may obtain a copy of the License at
7cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao *
8cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao *      http://www.apache.org/licenses/LICENSE-2.0
9cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao *
10cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * Unless required by applicable law or agreed to in writing, software
11cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * distributed under the License is distributed on an "AS IS" BASIS,
12cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * See the License for the specific language governing permissions and
14cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * limitations under the License.
15cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao */
16cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
17cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raopackage com.android.server.wifi.util;
18cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
19cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raoimport android.annotation.NonNull;
20cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raoimport android.os.Message;
21cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
22e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Raoimport com.android.internal.annotations.VisibleForTesting;
23cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raoimport com.android.internal.util.AsyncChannel;
24cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raoimport com.android.server.wifi.WifiInjector;
25cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raoimport com.android.server.wifi.WifiLog;
26cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
27cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao/**
28cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * This class subclasses AsyncChannel and adds logging
29cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao * to the sendMessage() API
30cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao */
31cda805612a020057f7363b8e89be72ca9d6958f9Sohani Raopublic class WifiAsyncChannel extends AsyncChannel {
32cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    private static final String LOG_TAG = "WifiAsyncChannel";
33cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    private WifiLog mLog;
34cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    private String mTag;
35cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    /**
36cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * AsyncChannelWithLogging constructor
37cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     */
38cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    public WifiAsyncChannel(String serviceTag) {
39cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        mTag = LOG_TAG + "." + serviceTag;
40cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    }
41cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
42cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    @NonNull
43cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    private WifiLog getOrInitLog() {
44cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        // Lazy initization of mLog
45cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        if (mLog == null) {
46cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            mLog = WifiInjector.getInstance().makeLog(mTag);
47cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        }
48cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        return mLog;
49cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    }
50cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
51cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    /**
52cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * Send a message to the destination handler.
53cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     *
54cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * @param msg
55cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     */
56cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    @Override
57cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    public void sendMessage(Message msg) {
58cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        getOrInitLog().trace("sendMessage message=%")
59cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .c(msg.what)
60cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .flush();
61cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        super.sendMessage(msg);
62cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    }
63cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
64cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    /**
65cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * Reply to srcMsg
66cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     *
67cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * @param srcMsg
68cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * @param dstMsg
69cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     */
70cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    @Override
71cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    public void replyToMessage(Message srcMsg, Message dstMsg) {
72cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        getOrInitLog()
73cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao                .trace("replyToMessage recvdMessage=% sendingUid=% sentMessage=%")
74cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao                .c(srcMsg.what)
75cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao                .c(srcMsg.sendingUid)
76cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao                .c(dstMsg.what)
77cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao                .flush();
78cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        super.replyToMessage(srcMsg, dstMsg);
79cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    }
80cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao
81cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    /**
82cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * Send the Message synchronously.
83cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     *
84cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * @param msg to send
85cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     * @return reply message or null if an error.
86cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao     */
87cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    @Override
88cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    public Message sendMessageSynchronously(Message msg) {
89cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        getOrInitLog().trace("sendMessageSynchronously.send message=%")
90cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .c(msg.what)
91cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .flush();
92cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        Message replyMessage = super.sendMessageSynchronously(msg);
93cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        getOrInitLog().trace("sendMessageSynchronously.recv message=% sendingUid=%")
94cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .c(replyMessage.what)
95cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .c(replyMessage.sendingUid)
96cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao            .flush();
97cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao        return replyMessage;
98cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao    }
99e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Rao
100e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Rao    @VisibleForTesting
101e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Rao    public void setWifiLog(WifiLog log) {
102e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Rao        mLog = log;
103e7ebc3fba71f4f996794bc5fa939674f8b4b2c5aSohani Rao    }
104cda805612a020057f7363b8e89be72ca9d6958f9Sohani Rao}
105