WifiStateMachinePrime.java revision 613dbfbefbe570c2fd5e1e6d959411bf75f6265b
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;
18
19import android.os.Message;
20
21import com.android.internal.util.State;
22import com.android.internal.util.StateMachine;
23
24/**
25 * This class provides the implementation for different WiFi operating modes.
26 *
27 * NOTE: The class is a WIP and is in active development.  It is intended to replace the existing
28 * WifiStateMachine.java class when the rearchitecture is complete.
29 */
30public class WifiStateMachinePrime {
31    private static final String TAG = "WifiStateMachinePrime";
32
33    private ModeStateMachine mModeStateMachine;
34
35    WifiStateMachinePrime() {
36        mModeStateMachine = new ModeStateMachine(TAG);
37    }
38
39    private class ModeStateMachine extends StateMachine {
40        // Commands for the state machine.
41        public static final int CMD_START_CLIENT_MODE    = 0;
42        public static final int CMD_START_SCAN_ONLY_MODE = 1;
43        public static final int CMD_START_SOFT_AP_MODE   = 2;
44        public static final int CMD_DISABLE_WIFI         = 3;
45
46
47        // Create the base modes for WSM.
48        private final State mClientModeState = new ClientModeState();
49        private final State mScanOnlyModeState = new ScanOnlyModeState();
50        private final State mSoftAPModeState = new SoftAPModeState();
51        private final State mWifiDisabledState = new WifiDisabledState();
52
53        // Create the active versions of the modes for WSM.
54        private final State mClientModeActiveState = new ClientModeActiveState();
55        private final State mScanOnlyModeActiveState = new ScanOnlyModeActiveState();
56        private final State mSoftAPModeActiveState = new SoftAPModeActiveState();
57
58        ModeStateMachine(String name) {
59            super(name);
60
61            // CHECKSTYLE:OFF IndentationCheck
62            addState(mClientModeState);
63              addState(mClientModeActiveState, mClientModeState);
64            addState(mScanOnlyModeState);
65              addState(mScanOnlyModeActiveState, mScanOnlyModeState);
66            addState(mSoftAPModeState);
67              addState(mSoftAPModeActiveState, mSoftAPModeState);
68            addState(mWifiDisabledState);
69            // CHECKSTYLE:ON IndentationCheck
70        }
71
72        private String getCurrentMode() {
73            try {
74                return getCurrentState().getName();
75            } catch (NullPointerException e) {
76                // current state is not set
77                return null;
78            }
79        }
80
81        class ClientModeState extends State {
82
83            @Override
84            public boolean processMessage(Message message) {
85                return NOT_HANDLED;
86            }
87
88            @Override
89            public void exit() {
90
91            }
92        }
93
94        class ScanOnlyModeState extends State {
95
96            @Override
97            public boolean processMessage(Message message) {
98                // handle Mode changes and any events requiring setup or restarting services
99
100                return NOT_HANDLED;
101            }
102
103            @Override
104            public void exit() {
105                // tear down running services
106            }
107        }
108
109        class SoftAPModeState extends State {
110
111            @Override
112            public boolean processMessage(Message message) {
113                return NOT_HANDLED;
114            }
115
116            @Override
117            public void exit() {
118            }
119        }
120
121        class WifiDisabledState extends State {
122            @Override
123            public void enter() {
124                // make sure everything is torn down
125            }
126
127            @Override
128            public boolean processMessage(Message message) {
129                return NOT_HANDLED;
130            }
131
132        }
133
134        class ModeActiveState extends State {
135            ActiveModeManager mActiveModeManager;
136
137            @Override
138            public boolean processMessage(Message message) {
139                // handle messages for changing modes here
140                return NOT_HANDLED;
141            }
142
143            @Override
144            public void exit() {
145                // clean up objects from an active state - check with mode handlers to make sure
146                // they are stopping properly.
147                mActiveModeManager.stop();
148            }
149        }
150
151        class ClientModeActiveState extends ModeActiveState {
152            @Override
153            public void enter() {
154                this.mActiveModeManager = new ClientModeManager();
155            }
156        }
157
158        class ScanOnlyModeActiveState extends ModeActiveState {
159            @Override
160            public void enter() {
161                this.mActiveModeManager = new ScanOnlyModeManager();
162            }
163        }
164
165        class SoftAPModeActiveState extends ModeActiveState {
166            @Override
167            public void enter() {
168                // The SoftApManager is not empty at this time, will populate in later CLs.
169                //this.mActiveModeManager = new SoftApManager();
170            }
171        }
172    } // class ModeStateMachine
173}
174