yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /******************************************************************************
|
| 2 | *
|
| 3 | * Copyright (C) 2009-2012 Broadcom Corporation
|
| 4 | *
|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 | * you may not use this file except in compliance with the License.
|
| 7 | * You may owfain a copy of the License at:
|
| 8 | *
|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 10 | *
|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 | * See the License for the specific language governing permissions and
|
| 15 | * limitations under the License.
|
| 16 | *
|
| 17 | ******************************************************************************/
|
| 18 |
|
| 19 |
|
| 20 | /*****************************************************************************
|
| 21 | *
|
| 22 | * Filename: wlan_sm.c
|
| 23 | *
|
| 24 | * Description: Generic wlan state machine API
|
| 25 | *
|
| 26 | *****************************************************************************/
|
| 27 | #include "wlan_sm.h"
|
| 28 | #include "wifi_util.h"
|
| 29 |
|
| 30 | /*****************************************************************************
|
| 31 | ** Constants & Macros
|
| 32 | ******************************************************************************/
|
| 33 |
|
| 34 |
|
| 35 | /*****************************************************************************
|
| 36 | ** Local type definitions
|
| 37 | ******************************************************************************/
|
| 38 | typedef struct {
|
| 39 | wlan_sm_state_t state;
|
| 40 | wlan_sm_handler_t *p_handlers;
|
| 41 | } wlan_sm_cb_t;
|
| 42 |
|
| 43 | /*****************************************************************************
|
| 44 | ** Static variables
|
| 45 | ******************************************************************************/
|
| 46 |
|
| 47 | /*****************************************************************************
|
| 48 | ** Static functions
|
| 49 | ******************************************************************************/
|
| 50 |
|
| 51 | /*****************************************************************************
|
| 52 | ** Externs
|
| 53 | ******************************************************************************/
|
| 54 |
|
| 55 | /*****************************************************************************
|
| 56 | ** Functions
|
| 57 | ******************************************************************************/
|
| 58 |
|
| 59 | /*****************************************************************************
|
| 60 | **
|
| 61 | ** Function wlan_sm_init
|
| 62 | **
|
| 63 | ** Description Initializes the state machine with the state handlers
|
| 64 | ** The caller should ensure that the table and the corresponding
|
| 65 | ** states match. The location that 'p_handlers' points to shall
|
| 66 | ** be available until the wlan_sm_shutdown API is invoked.
|
| 67 | **
|
| 68 | ** Returns Returns a pointer to the initialized state machine handle.
|
| 69 | **
|
| 70 | ******************************************************************************/
|
| 71 |
|
| 72 | wlan_sm_handle_t wlan_sm_init(const wlan_sm_handler_t *p_handlers, wlan_sm_state_t initial_state)
|
| 73 | {
|
| 74 | wlan_sm_cb_t *p_cb = NULL;
|
| 75 |
|
| 76 | if (p_handlers == NULL)
|
| 77 | {
|
| 78 | wf_log("p_handlers is NULL");
|
| 79 | return NULL;
|
| 80 | }
|
| 81 |
|
| 82 | p_cb = (wlan_sm_cb_t*) malloc(sizeof(wlan_sm_cb_t));
|
| 83 | if (p_cb == NULL)//wlocwork
|
| 84 | {
|
| 85 | wf_log("p_cb is NULL");
|
| 86 | return NULL;
|
| 87 | }
|
| 88 | p_cb->state = initial_state;
|
| 89 | p_cb->p_handlers = (wlan_sm_handler_t*)p_handlers;
|
| 90 |
|
| 91 | /* Send wlan_SM_ENTER_EVT to the initial state */
|
| 92 | p_cb->p_handlers[initial_state](WLAN_SM_ENTER_EVT, NULL);
|
| 93 |
|
| 94 | return (wlan_sm_handle_t)p_cb;
|
| 95 | }
|
| 96 |
|
| 97 | /*****************************************************************************
|
| 98 | **
|
| 99 | ** Function wlan_sm_shutdown
|
| 100 | **
|
| 101 | ** Description Tears down the state machine
|
| 102 | **
|
| 103 | ** Returns None
|
| 104 | **
|
| 105 | ******************************************************************************/
|
| 106 | void wlan_sm_shutdown(wlan_sm_handle_t handle)
|
| 107 | {
|
| 108 | wlan_sm_cb_t *p_cb = (wlan_sm_cb_t*)handle;
|
| 109 |
|
| 110 | if (p_cb == NULL)
|
| 111 | {
|
| 112 | wf_log("Invalid handle");
|
| 113 | return;
|
| 114 | }
|
| 115 | free((void*)p_cb);
|
| 116 | }
|
| 117 |
|
| 118 | /*****************************************************************************
|
| 119 | **
|
| 120 | ** Function wlan_sm_get_state
|
| 121 | **
|
| 122 | ** Description Fetches the current state of the state machine
|
| 123 | **
|
| 124 | ** Returns Current state
|
| 125 | **
|
| 126 | ******************************************************************************/
|
| 127 | wlan_sm_state_t wlan_sm_get_state(wlan_sm_handle_t handle)
|
| 128 | {
|
| 129 | wlan_sm_cb_t *p_cb = (wlan_sm_cb_t*)handle;
|
| 130 |
|
| 131 | if (p_cb == NULL)
|
| 132 | {
|
| 133 | wf_log("Invalid handle");
|
| 134 | return 0;
|
| 135 | }
|
| 136 |
|
| 137 | return p_cb->state;
|
| 138 | }
|
| 139 |
|
| 140 | /*****************************************************************************
|
| 141 | **
|
| 142 | ** Function wlan_sm_dispatch
|
| 143 | **
|
| 144 | ** Description Dispatches the 'event' along with 'data' to the current state handler
|
| 145 | **
|
| 146 | ** Returns WF_STATUS_SUCCESS on success
|
| 147 | ** WF_STATUS_UNHANDLED if event was not processed
|
| 148 | ** WF_STATUS_FAIL otherwise
|
| 149 | **
|
| 150 | ******************************************************************************/
|
| 151 | wf_status_t wlan_sm_dispatch(wlan_sm_handle_t handle, wlan_sm_event_t event,
|
| 152 | void *data)
|
| 153 | {
|
| 154 | wf_status_t status = WF_STATUS_SUCCESS;
|
| 155 |
|
| 156 | wlan_sm_cb_t *p_cb = (wlan_sm_cb_t*)handle;
|
| 157 |
|
| 158 | if (p_cb == NULL)
|
| 159 | {
|
| 160 | wf_log("Invalid handle");
|
| 161 | return WF_STATUS_FAIL;
|
| 162 | }
|
| 163 |
|
| 164 | if (p_cb->p_handlers[p_cb->state](event, data) == FALSE)
|
| 165 | return WF_STATUS_UNHANDLED;
|
| 166 |
|
| 167 | return status;
|
| 168 | }
|
| 169 |
|
| 170 | /*****************************************************************************
|
| 171 | **
|
| 172 | ** Function wlan_sm_change_state
|
| 173 | **
|
| 174 | ** Description Make a transition to the new 'state'. The 'WLAN_SM_EXIT_EVT'
|
| 175 | ** shall be invoked before exiting the current state. The
|
| 176 | ** 'WLAN_SM_ENTER_EVT' shall be invoked before entering the new state
|
| 177 | **
|
| 178 | ** Returns WF_STATUS_SUCCESS on success
|
| 179 | ** WF_STATUS_UNHANDLED if event was not processed
|
| 180 | ** WF_STATUS_FAIL otherwise
|
| 181 | **
|
| 182 | ******************************************************************************/
|
| 183 | wf_status_t wlan_sm_change_state(wlan_sm_handle_t handle, wlan_sm_state_t state)
|
| 184 | {
|
| 185 | wf_status_t status = WF_STATUS_SUCCESS;
|
| 186 | wlan_sm_cb_t *p_cb = (wlan_sm_cb_t*)handle;
|
| 187 |
|
| 188 | if (p_cb == NULL)
|
| 189 | {
|
| 190 | wf_log("Invalid handle");
|
| 191 | return WF_STATUS_FAIL;
|
| 192 | }
|
| 193 |
|
| 194 | /* Send exit event to the current state */
|
| 195 | if (p_cb->p_handlers[p_cb->state](WLAN_SM_EXIT_EVT, NULL) == FALSE)
|
| 196 | status = WF_STATUS_UNHANDLED;
|
| 197 |
|
| 198 | /* Change to the new state */
|
| 199 | p_cb->state = state;
|
| 200 |
|
| 201 | /* Send enter event to the new state */
|
| 202 | if (p_cb->p_handlers[p_cb->state](WLAN_SM_ENTER_EVT, NULL) == FALSE)
|
| 203 | status = WF_STATUS_UNHANDLED;
|
| 204 |
|
| 205 | return status;
|
| 206 | }
|