NetworkManager.h revision c4a895b7094461c98101924cf096680bfb7856f1
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 _NETWORKMANAGER_H
18#define _NETWORKMANAGER_H
19
20#include <utils/List.h>
21#include <sysutils/SocketListener.h>
22
23#include "Controller.h"
24#include "PropertyManager.h"
25#include "IControllerHandler.h"
26#include "IDhcpEventHandlers.h"
27
28class InterfaceConfig;
29class DhcpClient;
30
31class NetworkManager : public IControllerHandler, public IDhcpEventHandlers {
32    static NetworkManager *sInstance;
33
34    class ControllerBinding {
35        Controller      *mController;
36        InterfaceConfig *mCurrentCfg;
37        InterfaceConfig *mBoundCfg;
38
39    public:
40        ControllerBinding(Controller *c);
41        virtual ~ControllerBinding() {}
42
43        InterfaceConfig *getCurrentCfg() { return mCurrentCfg; }
44        InterfaceConfig *getBoundCfg() { return mCurrentCfg; }
45        Controller *getController() { return mController; }
46
47        void setCurrentCfg(InterfaceConfig *cfg);
48        void setBoundCfg(InterfaceConfig *cfg);
49    };
50
51    typedef android::List<ControllerBinding *> ControllerBindingCollection;
52
53private:
54    ControllerBindingCollection *mControllerBindings;
55    SocketListener              *mBroadcaster;
56    PropertyManager             *mPropMngr;
57    DhcpClient                  *mDhcp;
58    int                         mLastDhcpState;
59
60public:
61    virtual ~NetworkManager();
62
63    int run();
64
65    int attachController(Controller *controller);
66
67    Controller *findController(const char *name);
68
69    void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
70    SocketListener *getBroadcaster() { return mBroadcaster; }
71    PropertyManager *getPropMngr() { return mPropMngr; }
72
73    static NetworkManager *Instance();
74
75private:
76    int startControllers();
77    int stopControllers();
78
79    NetworkManager(PropertyManager *propMngr);
80    ControllerBinding *lookupBinding(Controller *c);
81
82    void onInterfaceConnected(Controller *c);
83    void onInterfaceDisconnected(Controller *c);
84    void onControllerSuspending(Controller *c);
85    void onControllerResumed(Controller *c);
86
87    void onDhcpStateChanged(Controller *c, int state);
88    void onDhcpEvent(Controller *c, int event);
89    void onDhcpLeaseUpdated(Controller *c,
90                            struct in_addr *addr, struct in_addr *net,
91                            struct in_addr *brd,
92                            struct in_addr *gw, struct in_addr *dns1,
93                            struct in_addr *dns2);
94};
95#endif
96