1/*
2 * Copyright (C) 2008 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
17#ifndef _WIFI_NETWORK_H
18#define _WIFI_NETWORK_H
19
20#include <sys/types.h>
21
22#include <utils/List.h>
23
24#include "Property.h"
25
26class PropertyManager;
27
28class KeyManagementMask {
29public:
30    static const uint32_t UNKNOWN   = 0;
31    static const uint32_t NONE      = 0x01;
32    static const uint32_t WPA_PSK   = 0x02;
33    static const uint32_t WPA_EAP   = 0x04;
34    static const uint32_t IEEE8021X = 0x08;
35    static const uint32_t ALL       = WPA_PSK | WPA_EAP | IEEE8021X;
36};
37
38class SecurityProtocolMask {
39public:
40    static const uint32_t WPA = 0x01;
41    static const uint32_t RSN = 0x02;
42};
43
44class AuthenticationAlgorithmMask {
45public:
46    static const uint32_t OPEN   = 0x01;
47    static const uint32_t SHARED = 0x02;
48    static const uint32_t LEAP   = 0x04;
49};
50
51class PairwiseCiphersMask {
52public:
53    static const uint32_t NONE = 0x00;
54    static const uint32_t TKIP = 0x01;
55    static const uint32_t CCMP = 0x02;
56};
57
58class GroupCiphersMask {
59public:
60    static const uint32_t WEP40  = 0x01;
61    static const uint32_t WEP104 = 0x02;
62    static const uint32_t TKIP   = 0x04;
63    static const uint32_t CCMP   = 0x08;
64};
65
66class Supplicant;
67class Controller;
68class WifiController;
69
70class WifiNetwork {
71    class WifiNetworkIntegerProperty : public IntegerProperty {
72    protected:
73        WifiNetwork *mWn;
74    public:
75        WifiNetworkIntegerProperty(WifiNetwork *wn, const char *name, bool ro,
76                                   int elements);
77        virtual ~WifiNetworkIntegerProperty() {}
78        virtual int set(int idx, int value) = 0;
79        virtual int get(int idx, int *buffer) = 0;
80    };
81    friend class WifiNetwork::WifiNetworkIntegerProperty;
82
83    class WifiNetworkStringProperty : public StringProperty {
84    protected:
85        WifiNetwork *mWn;
86    public:
87        WifiNetworkStringProperty(WifiNetwork *wn, const char *name, bool ro,
88                                 int elements);
89        virtual ~WifiNetworkStringProperty() {}
90        virtual int set(int idx, const char *value) = 0;
91        virtual int get(int idx, char *buffer, size_t max) = 0;
92    };
93    friend class WifiNetwork::WifiNetworkStringProperty;
94
95    class WifiNetworkEnabledProperty : public WifiNetworkIntegerProperty {
96    public:
97        WifiNetworkEnabledProperty(WifiNetwork *wn);
98        virtual ~WifiNetworkEnabledProperty() {};
99        int set(int idx, int value);
100        int get(int idx, int *buffer);
101    };
102
103    class WifiNetworkPriorityProperty : public WifiNetworkIntegerProperty {
104    public:
105        WifiNetworkPriorityProperty(WifiNetwork *wn);
106        virtual ~WifiNetworkPriorityProperty() {};
107        int set(int idx, int value);
108        int get(int idx, int *buffer);
109    };
110
111    class WifiNetworkDefaultKeyIndexProperty : public WifiNetworkIntegerProperty {
112    public:
113        WifiNetworkDefaultKeyIndexProperty(WifiNetwork *wn);
114        virtual ~WifiNetworkDefaultKeyIndexProperty() {};
115        int set(int idx, int value);
116        int get(int idx, int *buffer);
117    };
118
119    class WifiNetworkSsidProperty : public WifiNetworkStringProperty {
120    public:
121        WifiNetworkSsidProperty(WifiNetwork *wn);
122        virtual ~WifiNetworkSsidProperty() {};
123        int set(int idx, const char *value);
124        int get(int idx, char *buffer, size_t max);
125    };
126
127    class WifiNetworkBssidProperty : public WifiNetworkStringProperty {
128    public:
129        WifiNetworkBssidProperty(WifiNetwork *wn);
130        virtual ~WifiNetworkBssidProperty() {};
131        int set(int idx, const char *value);
132        int get(int idx, char *buffer, size_t max);
133    };
134
135    class WifiNetworkPskProperty : public WifiNetworkStringProperty {
136    public:
137        WifiNetworkPskProperty(WifiNetwork *wn);
138        virtual ~WifiNetworkPskProperty() {};
139        int set(int idx, const char *value);
140        int get(int idx, char *buffer, size_t max);
141    };
142
143    class WifiNetworkKeyManagementProperty : public WifiNetworkStringProperty {
144    public:
145        WifiNetworkKeyManagementProperty(WifiNetwork *wn);
146        virtual ~WifiNetworkKeyManagementProperty() {};
147        int set(int idx, const char *value);
148        int get(int idx, char *buffer, size_t max);
149    };
150
151    class WifiNetworkAuthAlgorithmsProperty : public WifiNetworkStringProperty {
152    public:
153        WifiNetworkAuthAlgorithmsProperty(WifiNetwork *wn);
154        virtual ~WifiNetworkAuthAlgorithmsProperty() {};
155        int set(int idx, const char *value);
156        int get(int idx, char *buffer, size_t max);
157    };
158
159    class WifiNetworkProtocolsProperty : public WifiNetworkStringProperty {
160    public:
161        WifiNetworkProtocolsProperty(WifiNetwork *wn);
162        virtual ~WifiNetworkProtocolsProperty() {};
163        int set(int idx, const char *value);
164        int get(int idx, char *buffer, size_t max);
165    };
166
167    class WifiNetworkWepKeyProperty : public WifiNetworkStringProperty {
168    public:
169        WifiNetworkWepKeyProperty(WifiNetwork *wn);
170        virtual ~WifiNetworkWepKeyProperty() {};
171        int set(int idx, const char *value);
172        int get(int idx, char *buffer, size_t max);
173    };
174
175    class WifiNetworkPairwiseCiphersProperty : public WifiNetworkStringProperty {
176    public:
177        WifiNetworkPairwiseCiphersProperty(WifiNetwork *wn);
178        virtual ~WifiNetworkPairwiseCiphersProperty() {};
179        int set(int idx, const char *value);
180        int get(int idx, char *buffer, size_t max);
181    };
182
183    class WifiNetworkGroupCiphersProperty : public WifiNetworkStringProperty {
184    public:
185        WifiNetworkGroupCiphersProperty(WifiNetwork *wn);
186        virtual ~WifiNetworkGroupCiphersProperty() {};
187        int set(int idx, const char *value);
188        int get(int idx, char *buffer, size_t max);
189    };
190
191    class WifiNetworkHiddenSsidProperty : public WifiNetworkStringProperty {
192    public:
193        WifiNetworkHiddenSsidProperty(WifiNetwork *wn);
194        virtual ~WifiNetworkHiddenSsidProperty() {};
195        int set(int idx, const char *value);
196        int get(int idx, char *buffer, size_t max);
197    };
198
199private:
200    Supplicant *mSuppl;
201    WifiController *mController;
202
203    /*
204     * Unique network id - normally provided by supplicant
205     */
206    int mNetid;
207
208    /*
209     * The networks' SSID. Can either be an ASCII string,
210     * which must be enclosed in double quotation marks
211     * (ie: "MyNetwork"), or a string of hex digits which
212     * are not enclosed in quotes (ie: 01ab7893)
213     */
214    char *mSsid;
215
216    /*
217     * When set, this entry should only be used
218     * when associating with the AP having the specified
219     * BSSID. The value is a string in the format of an
220     * Ethernet MAC address
221     */
222    char *mBssid;
223
224    /*
225     *  Pre-shared key for use with WPA-PSK
226     */
227    char *mPsk;
228
229    /*
230     * Up to four WEP keys. Either in ASCII string enclosed in
231     * double quotes, or a string of hex digits
232     */
233    char *mWepKeys[4];
234
235    /*
236     * Default WEP key index, ranging from 0 -> NUM_WEP_KEYS -1
237     */
238    int mDefaultKeyIndex;
239
240    /*
241     * Priority determines the preference given to a network by
242     * supplicant when choosing an access point with which
243     * to associate
244     */
245    int mPriority;
246
247    /*
248     * This is a network that does not broadcast it's SSID, so an
249     * SSID-specific probe request must be used for scans.
250     */
251    char *mHiddenSsid;
252
253    /*
254     * The set of key management protocols supported by this configuration.
255     */
256    uint32_t mKeyManagement;
257
258    /*
259     * The set of security protocols supported by this configuration.
260     */
261    uint32_t mProtocols;
262
263    /*
264     * The set of authentication protocols supported by this configuration.
265     */
266    uint32_t mAuthAlgorithms;
267
268    /*
269     * The set of pairwise ciphers for WPA supported by this configuration.
270     */
271    uint32_t mPairwiseCiphers;
272
273    /*
274     * The set of group ciphers for WPA supported by this configuration.
275     */
276    uint32_t mGroupCiphers;
277
278    /*
279     * Set if this Network is enabled
280     */
281    bool mEnabled;
282
283    char *mPropNamespace;
284    struct {
285        WifiNetworkEnabledProperty               *propEnabled;
286        WifiNetworkSsidProperty                  *propSsid;
287        WifiNetworkBssidProperty                 *propBssid;
288        WifiNetworkPskProperty                   *propPsk;
289        WifiNetworkWepKeyProperty                *propWepKey;
290        WifiNetworkDefaultKeyIndexProperty       *propDefKeyIdx;
291        WifiNetworkPriorityProperty              *propPriority;
292        WifiNetworkKeyManagementProperty  *propKeyManagement;
293        WifiNetworkProtocolsProperty      *propProtocols;
294        WifiNetworkAuthAlgorithmsProperty *propAuthAlgorithms;
295        WifiNetworkPairwiseCiphersProperty       *propPairwiseCiphers;
296        WifiNetworkGroupCiphersProperty          *propGroupCiphers;
297        WifiNetworkHiddenSsidProperty            *propHiddenSsid;
298    } mStaticProperties;
299private:
300    WifiNetwork();
301
302public:
303    WifiNetwork(WifiController *c, Supplicant *suppl, int networkId);
304    WifiNetwork(WifiController *c, Supplicant *suppl, const char *data);
305
306    virtual ~WifiNetwork();
307
308    WifiNetwork *clone();
309    int attachProperties(PropertyManager *pm, const char *nsName);
310    int detachProperties(PropertyManager *pm, const char *nsName);
311
312    int getNetworkId() { return mNetid; }
313    const char *getSsid() { return mSsid; }
314    const char *getBssid() { return mBssid; }
315    const char *getPsk() { return mPsk; }
316    const char *getWepKey(int idx) { return mWepKeys[idx]; }
317    int getDefaultKeyIndex() { return mDefaultKeyIndex; }
318    int getPriority() { return mPriority; }
319    const char *getHiddenSsid() { return mHiddenSsid; }
320    uint32_t getKeyManagement() { return mKeyManagement; }
321    uint32_t getProtocols() { return mProtocols; }
322    uint32_t getAuthAlgorithms() { return mAuthAlgorithms; }
323    uint32_t getPairwiseCiphers() { return mPairwiseCiphers; }
324    uint32_t getGroupCiphers() { return mGroupCiphers; }
325    bool getEnabled() { return mEnabled; }
326    Controller *getController() { return (Controller *) mController; }
327
328    int setEnabled(bool enabled);
329    int setSsid(const char *ssid);
330    int setBssid(const char *bssid);
331    int setPsk(const char *psk);
332    int setWepKey(int idx, const char *key);
333    int setDefaultKeyIndex(int idx);
334    int setPriority(int pri);
335    int setHiddenSsid(const char *ssid);
336    int setKeyManagement(uint32_t mask);
337    int setProtocols(uint32_t mask);
338    int setAuthAlgorithms(uint32_t mask);
339    int setPairwiseCiphers(uint32_t mask);
340    int setGroupCiphers(uint32_t mask);
341
342    // XXX:Should this really be exposed?.. meh
343    int refresh();
344
345private:
346    int parseKeyManagementMask(const char *buffer, uint32_t *mask);
347    int parseProtocolsMask(const char *buffer, uint32_t *mask);
348    int parseAuthAlgorithmsMask(const char *buffer, uint32_t *mask);
349    int parsePairwiseCiphersMask(const char *buffer, uint32_t *mask);
350    int parseGroupCiphersMask(const char *buffer, uint32_t *mask);
351    void createProperties();
352};
353
354typedef android::List<WifiNetwork *> WifiNetworkCollection;
355
356#endif
357