summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/MemoryStoreImpl.java
blob: 3d9e7626b79e6b10c5b06c38f00271f07bfb2bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.IpMemoryStore;
import android.net.ipmemorystore.Blob;
import android.net.ipmemorystore.Status;
import android.util.Log;

import com.android.internal.util.Preconditions;
import com.android.server.wifi.WifiScoreCard.BlobListener;

import java.util.Objects;

/**
 * Connects WifiScoreCard to IpMemoryStore.
 */
final class MemoryStoreImpl implements WifiScoreCard.MemoryStore {
    private static final String TAG = "WifiMemoryStoreImpl";
    private static final boolean DBG = true;

    // The id of the client that stored this data
    public static final String WIFI_FRAMEWORK_IP_MEMORY_STORE_CLIENT_ID = "com.android.server.wifi";

    // The name of the data
    public static final String WIFI_FRAMEWORK_IP_MEMORY_STORE_DATA_NAME = "scorecard.proto";

    @NonNull private final Context mContext;
    @NonNull private final WifiScoreCard mWifiScoreCard;
    @NonNull private final WifiInjector mWifiInjector;
    @Nullable private IpMemoryStore mIpMemoryStore;

    MemoryStoreImpl(Context context, WifiInjector wifiInjector, WifiScoreCard wifiScoreCard) {
        mContext = Preconditions.checkNotNull(context);
        mWifiScoreCard = Preconditions.checkNotNull(wifiScoreCard);
        mWifiInjector = Preconditions.checkNotNull(wifiInjector);
        mIpMemoryStore = null;
    }

    private boolean mBroken = false;
    private void handleException(Exception e) {
        Log.wtf(TAG, "Exception using IpMemoryStore - disabling WifiScoreReport persistence", e);
        mBroken = true;
    }


    @Override
    public void read(final String key, final BlobListener blobListener) {
        if (mBroken) return;
        try {
            mIpMemoryStore.retrieveBlob(
                    key,
                    WIFI_FRAMEWORK_IP_MEMORY_STORE_CLIENT_ID,
                    WIFI_FRAMEWORK_IP_MEMORY_STORE_DATA_NAME,
                    new CatchAFallingBlob(key, blobListener));
        } catch (RuntimeException e) {
            handleException(e);
        }
    }

    /**
     * Listens for a reply to a read request.
     *
     * Note that onBlobRetrieved() is called on a binder thread, so the
     * provided blobListener must be prepared to deal with this.
     *
     */
    private static class CatchAFallingBlob
            implements android.net.ipmemorystore.OnBlobRetrievedListener {
        private final String mL2Key;
        private final WifiScoreCard.BlobListener mBlobListener;

        CatchAFallingBlob(String l2Key, WifiScoreCard.BlobListener blobListener) {
            mL2Key = l2Key;
            mBlobListener = blobListener;
        }

        @Override
        public void onBlobRetrieved(Status status, String l2Key, String name, Blob data) {
            if (!Objects.equals(mL2Key, l2Key)) {
                throw new IllegalArgumentException("l2Key does not match request");
            }
            if (status.isSuccess()) {
                if (data == null) {
                    if (DBG) Log.i(TAG, "Blob is null");
                    mBlobListener.onBlobRetrieved(null);
                    return;
                }
                mBlobListener.onBlobRetrieved(data.data);
            } else {
                if (DBG) Log.e(TAG, "android.net.ipmemorystore.Status " + status);
            }
        }
    }

    @Override
    public void write(String key, byte[] value) {
        if (mBroken) return;
        final Blob blob = new Blob();
        blob.data = value;
        try {
            mIpMemoryStore.storeBlob(
                    key,
                    WIFI_FRAMEWORK_IP_MEMORY_STORE_CLIENT_ID,
                    WIFI_FRAMEWORK_IP_MEMORY_STORE_DATA_NAME,
                    blob,
                    null /* no listener for now, just fire and forget */);
        } catch (RuntimeException e) {
            handleException(e);
        }
    }

    /**
     * Starts using IpMemoryStore.
     */
    public void start() {
        if (mIpMemoryStore != null) {
            Log.w(TAG, "Reconnecting to IpMemoryStore service");
        }
        mIpMemoryStore = mWifiInjector.getIpMemoryStore();
        if (mIpMemoryStore == null) {
            Log.e(TAG, "No IpMemoryStore service!");
            return;
        }
        mWifiScoreCard.installMemoryStore(this);
    }

    /**
     * Stops using IpMemoryStore after performing any outstanding writes.
     */
    public void stop() {
        if (mIpMemoryStore == null) return;
        mWifiScoreCard.doWrites();
        // TODO - Should wait for writes to complete (or time out)
        Log.i(TAG, "Disconnecting from IpMemoryStore service");
        mIpMemoryStore = null;
    }

}