1package com.android.server.locksettings;
2
3import android.hardware.weaver.V1_0.IWeaver;
4import android.hardware.weaver.V1_0.WeaverConfig;
5import android.hardware.weaver.V1_0.WeaverReadResponse;
6import android.hardware.weaver.V1_0.WeaverStatus;
7import android.hidl.base.V1_0.DebugInfo;
8import android.os.IHwBinder;
9import android.os.IHwBinder.DeathRecipient;
10import android.os.RemoteException;
11import android.util.Pair;
12
13import android.util.Log;
14
15import java.util.ArrayList;
16import java.util.Arrays;
17
18public class MockWeaverService implements IWeaver {
19
20    private static final int MAX_SLOTS = 8;
21    private static final int KEY_LENGTH = 256 / 8;
22    private static final int VALUE_LENGTH = 256 / 8;
23
24    private Pair<ArrayList<Byte>, ArrayList<Byte>>[] slots = new Pair[MAX_SLOTS];
25    @Override
26    public void getConfig(getConfigCallback cb) throws RemoteException {
27        WeaverConfig config = new WeaverConfig();
28        config.keySize = KEY_LENGTH;
29        config.valueSize = VALUE_LENGTH;
30        config.slots = MAX_SLOTS;
31        cb.onValues(WeaverStatus.OK, config);
32    }
33
34    @Override
35    public int write(int slotId, ArrayList<Byte> key, ArrayList<Byte> value)
36            throws RemoteException {
37        if (slotId < 0 || slotId >= MAX_SLOTS) {
38            throw new RuntimeException("Invalid slot id");
39        }
40        slots[slotId] = Pair.create((ArrayList<Byte>) key.clone(), (ArrayList<Byte>) value.clone());
41        return WeaverStatus.OK;
42    }
43
44    @Override
45    public void read(int slotId, ArrayList<Byte> key, readCallback cb) throws RemoteException {
46        if (slotId < 0 || slotId >= MAX_SLOTS) {
47            throw new RuntimeException("Invalid slot id");
48        }
49
50        WeaverReadResponse response = new WeaverReadResponse();
51        if (key.equals(slots[slotId].first)) {
52            response.value.addAll(slots[slotId].second);
53            cb.onValues(WeaverStatus.OK, response);
54        } else {
55            cb.onValues(WeaverStatus.FAILED, response);
56        }
57    }
58
59    @Override
60    public IHwBinder asBinder() {
61        throw new UnsupportedOperationException();
62    }
63
64    @Override
65    public ArrayList<String> interfaceChain() throws RemoteException {
66        throw new UnsupportedOperationException();
67    }
68
69    @Override
70    public String interfaceDescriptor() throws RemoteException {
71        throw new UnsupportedOperationException();
72    }
73
74    @Override
75    public void setHALInstrumentation() throws RemoteException {
76        throw new UnsupportedOperationException();
77    }
78
79    @Override
80    public boolean linkToDeath(DeathRecipient recipient, long cookie) throws RemoteException {
81        throw new UnsupportedOperationException();
82    }
83
84    @Override
85    public void ping() throws RemoteException {
86        throw new UnsupportedOperationException();
87    }
88
89    @Override
90    public DebugInfo getDebugInfo() throws RemoteException {
91        throw new UnsupportedOperationException();
92    }
93
94    @Override
95    public void notifySyspropsChanged() throws RemoteException {
96        throw new UnsupportedOperationException();
97    }
98
99    @Override
100    public boolean unlinkToDeath(DeathRecipient recipient) throws RemoteException {
101        throw new UnsupportedOperationException();
102    }
103
104    @Override
105    public ArrayList<byte[]> getHashChain() throws RemoteException {
106        throw new UnsupportedOperationException();
107    }
108}
109