Home | History | Annotate | Download | only in wifi_hal
      1 /*
      2  * Copyright (C) 2017 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 #pragma once
     18 
     19 #include "info.h"
     20 
     21 #include <memory>
     22 #include <mutex>
     23 
     24 // A class that keeps track of the current state of the WiFi HAL. This is
     25 // accomplished by having the HAL call this state tracker which in turn will
     26 // call the appropriate functionality.
     27 //
     28 // This is needed because the HAL must have some form of global state to prevent
     29 // situations where the HAL is initialized while stopping. This class allows
     30 // the Info class to stay relatively simple and this class can focus on just the
     31 // state behavior.
     32 class HalState {
     33 public:
     34     using StopHandler = std::function<void ()>;
     35 
     36     HalState();
     37 
     38     bool init();
     39     bool stop(StopHandler stopHandler);
     40     bool eventLoop();
     41 
     42     Info* info();
     43 
     44 private:
     45     enum class State {
     46         Constructed,
     47         Initialized,
     48         Running,
     49         Stopping,
     50         Stopped,
     51     };
     52 
     53     void onStop(StopHandler stopHandler);
     54 
     55     std::unique_ptr<Info> mInfo;
     56     std::mutex mStateMutex;
     57     State mState;
     58 };
     59 
     60