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