Home | History | Annotate | Download | only in Connection_Managment
      1 /*
      2  * externalSec.c
      3  *
      4  * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  *  * Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *  * Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in
     15  *    the documentation and/or other materials provided with the
     16  *    distribution.
     17  *  * Neither the name Texas Instruments nor the names of its
     18  *    contributors may be used to endorse or promote products derived
     19  *    from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /** \file externalSec.c
     35  * \brief station externalSec implementation
     36  *
     37  * \see externalSec.h
     38 */
     39 
     40 /****************************************************************************
     41  *                                                                          *
     42  *   MODULE:	station externalSec		                                    *
     43  *   PURPOSE:   station uexternalSec implementation						    *
     44  *                                                                          *
     45  ****************************************************************************/
     46 
     47 #define __FILE_ID__  FILE_ID_134
     48 #include "osApi.h"
     49 #include "report.h"
     50 #include "rsnApi.h"
     51 #include "smeApi.h"
     52 #include "mainSecSm.h"
     53 #include "externalSec.h"
     54 #include "connApi.h"
     55 
     56 TI_STATUS externalSecSM_Nop(struct externalSec_t *pExternalSec);
     57 TI_STATUS externalSecSM_start(mainSec_t *pMainSec);
     58 TI_STATUS externalSecSM_stop(mainSec_t *pMainSec);
     59 TI_STATUS externalSecSM_setPort(struct externalSec_t *pExternalSec);
     60 TI_STATUS externalSecSM_Unexpected(struct externalSec_t *pExternalSec);
     61 /**
     62 *
     63 * Function  - externalSec_config.
     64 *
     65 * \b Description:
     66 *
     67 * Called by mainSecSM (mainSec_config).
     68 * builds the SM and register the mainSec start and stop events.
     69 *
     70 * \b ARGS:
     71 *
     72 *
     73 * \b RETURNS:
     74 *
     75 *  TI_STATUS - 0 on success, any other value on failure.
     76 *
     77 */
     78 TI_STATUS externalSec_config(mainSec_t *pMainSec)
     79 {
     80     struct externalSec_t	 *pExtSec = pMainSec-> pExternalSec;
     81 	TI_STATUS                status = TI_NOK;
     82 
     83 	/** Station externalSec State Machine matrix */
     84     fsm_actionCell_t externalSec_matrix[EXTERNAL_SEC_NUM_STATES][EXTERNAL_SEC_NUM_EVENTS] =
     85 	{
     86     	/* next state and actions for IDLE state */
     87         {
     88             {EXTERNAL_SEC_STATE_WAIT,(fsm_Action_t)externalSecSM_Nop},       /*EXTERNAL_SEC_EVENT_START */
     89             {EXTERNAL_SEC_STATE_IDLE,(fsm_Action_t)externalSecSM_Unexpected},       /*EXTERNAL_SEC_EVENT_COMPLETE*/
     90             {EXTERNAL_SEC_STATE_IDLE,(fsm_Action_t)externalSecSM_Nop}        /*EXTERNAL_SEC_EVENT_STOP */
     91         },
     92 
     93     	/* next state and actions for Wait state */
     94     	{
     95             {EXTERNAL_SEC_STATE_WAIT,(fsm_Action_t)externalSecSM_Unexpected},/*EXTERNAL_SEC_EVENT_START */
     96             {EXTERNAL_SEC_STATE_IDLE,(fsm_Action_t)externalSecSM_setPort},    /*EXTERNAL_SEC_EVENT_COMPLETE*/
     97             {EXTERNAL_SEC_STATE_IDLE,(fsm_Action_t)externalSecSM_Nop}        /*EXTERNAL_SEC_EVENT_STOP */
     98     	}
     99 	};
    100 
    101     pExtSec->hOs = pMainSec->hOs;
    102     pExtSec->hReport = pMainSec->hReport;
    103     pExtSec->pParent = pMainSec;
    104     pMainSec->start = (mainSecSmStart_t)externalSecSM_start;
    105     pMainSec->stop = (mainSecSmStart_t)externalSecSM_stop;
    106     pExtSec->currentState = EXTERNAL_SEC_STATE_IDLE;
    107 
    108     status = fsm_Config(pExtSec->pExternalSecSm,
    109 						&externalSec_matrix[0][0],
    110 						EXTERNAL_SEC_NUM_STATES,
    111 						EXTERNAL_SEC_NUM_EVENTS,
    112 						NULL, pExtSec->hOs);
    113 
    114 
    115 	return status;
    116 }
    117 
    118 /**
    119 *
    120 * Function  - externalSec_create.
    121 *
    122 * \b Description:
    123 *
    124 * Called by mainSecSM (mainSec_create).
    125 * Registers the function 'rsn_UnicastKeyRecv()' at the distributor to receive KEY frames upon receiving a KEY_RECV event.
    126 *
    127 * \b ARGS:
    128 *
    129 *
    130 * \b RETURNS:
    131 *
    132 *  TI_STATUS - 0 on success, any other value on failure.
    133 *
    134 */
    135 struct externalSec_t* externalSec_create(TI_HANDLE hOs)
    136 {
    137     struct externalSec_t    *pHandle;
    138     TI_STATUS		        status;
    139 
    140     /* allocate association context memory */
    141     pHandle = (struct externalSec_t*)os_memoryAlloc(hOs, sizeof(struct externalSec_t));
    142     if (pHandle == NULL)
    143     {
    144         return NULL;
    145     }
    146 
    147     os_memoryZero(hOs, pHandle, sizeof(struct externalSec_t));
    148 
    149     /* allocate memory for association state machine */
    150     status = fsm_Create(hOs,&pHandle->pExternalSecSm, EXTERNAL_SEC_NUM_STATES, EXTERNAL_SEC_NUM_EVENTS);
    151 
    152     if (status != TI_OK)
    153     {
    154         os_memoryFree(hOs, pHandle, sizeof(struct externalSec_t));
    155         return NULL;
    156     }
    157 
    158     return pHandle;
    159 }
    160 
    161 /**
    162 *
    163 * Function  - externalSec_Destroy.
    164 *
    165 * \b Description:
    166 *
    167 * Called by mainSecSM (mainSec_unload).
    168 *
    169 * \b ARGS:
    170 *
    171 *
    172 * \b RETURNS:
    173 *
    174 *  TI_STATUS - 0 on success, any other value on failure.
    175 *
    176 */
    177 TI_STATUS externalSec_Destroy (struct externalSec_t *pExternalSec)
    178 {
    179     TI_STATUS	status;
    180 
    181     if (pExternalSec == NULL)
    182     {
    183         return TI_NOK;
    184     }
    185     status = fsm_Unload(pExternalSec->hOs, pExternalSec->pExternalSecSm);
    186     if (status != TI_OK)
    187     {
    188         /* report failure but don't stop... */
    189         TRACE0(pExternalSec->hReport, REPORT_SEVERITY_ERROR, "EXTERNAL SECURITY: Error releasing FSM memory \n");
    190     }
    191 
    192     os_memoryFree(pExternalSec->hOs, pExternalSec, sizeof(struct externalSec_t));
    193     return TI_OK;
    194 }
    195 
    196 /**
    197 *
    198 * Function  - externalSecSM_start.
    199 *
    200 * \b Description:
    201 *
    202 * Called upon the EXTERNAL_SEC_EVENT_START event
    203 *
    204 * \b ARGS:
    205 *
    206 *
    207 * \b RETURNS:
    208 *
    209 *  TI_STATUS - 0 on success, any other value on failure.
    210 *
    211 */
    212 TI_STATUS externalSecSM_start(mainSec_t *pMainSec)
    213 {
    214 	/* called by the rsn_start() */
    215 	return externalSec_event(pMainSec->pExternalSec, EXTERNAL_SEC_EVENT_START, pMainSec->pExternalSec);
    216 }
    217 
    218 /**
    219 *
    220 * Function  - externalSecSM_stop.
    221 *
    222 * \b Description:
    223 *
    224 * Called upon the EXTERNAL_SEC_EVENT_STOP event
    225 *
    226 * \b ARGS:
    227 *
    228 *
    229 * \b RETURNS:
    230 *
    231 *  TI_STATUS - 0 on success, any other value on failure.
    232 *
    233 */
    234 TI_STATUS externalSecSM_stop(mainSec_t *pMainSec)
    235 {
    236 	/* called by the rsn_stop() */
    237     return externalSec_event(pMainSec->pExternalSec, EXTERNAL_SEC_EVENT_STOP, pMainSec->pExternalSec);
    238 }
    239 
    240 /**
    241 *
    242 * Function  - externalSec_event.
    243 *
    244 * \b Description:
    245 *
    246 * Called by the  rsn_PortStatus_Set() API upon external set port status cmd.
    247 *
    248 * \b ARGS:
    249 *
    250 *
    251 * \b RETURNS:
    252 *
    253 *  TI_STATUS - 0 on success, any other value on failure.
    254 *
    255 */
    256 TI_STATUS externalSec_event(struct externalSec_t *pExternalSec, TI_UINT8 event, void *pData)
    257 {
    258     TI_STATUS   status;
    259     TI_UINT8    nextState;
    260 
    261     status = fsm_GetNextState(pExternalSec->pExternalSecSm,
    262                               pExternalSec->currentState,
    263                               event,
    264                               &nextState);
    265     if (status != TI_OK)
    266     {
    267         TRACE0(pExternalSec->hReport, REPORT_SEVERITY_ERROR, "EXTERNAL_SEC_SM: ERROR: failed getting next state\n");
    268         return TI_NOK;
    269 	}
    270 
    271     TRACE3(pExternalSec->hReport, REPORT_SEVERITY_INFORMATION, "STATION_EXT_SEC_SM: <currentState = %d, event = %d> --> nextState = %d\n", pExternalSec->currentState, event, nextState);
    272     status = fsm_Event(pExternalSec->pExternalSecSm,
    273                        &pExternalSec->currentState,
    274                        event,
    275                        pData);
    276 
    277     return status;
    278 }
    279 
    280 
    281 /**
    282 *
    283 * Function  - externalSecSM_setPort.
    284 *
    285 * \b Description:
    286 *
    287 * Call the connection report status API.
    288 *
    289 * \b ARGS:
    290 *
    291 *
    292 * \b RETURNS:
    293 *
    294 *  TI_STATUS - 0 on success, any other value on failure.
    295 *
    296 */
    297 TI_STATUS externalSecSM_setPort(struct externalSec_t *pExternalSec)
    298 {
    299     TI_STATUS       status = TI_OK;
    300     struct _rsn_t   *pRsn;
    301 
    302     pRsn = pExternalSec->pParent->pParent;
    303     if (TI_TRUE == pExternalSec->bPortStatus)
    304     {
    305         status  = conn_reportRsnStatus(pRsn->hConn, (mgmtStatus_e)STATUS_SUCCESSFUL);
    306     }
    307     else
    308     {
    309         status = conn_reportRsnStatus(pRsn->hConn, (mgmtStatus_e)STATUS_SECURITY_FAILURE);
    310     }
    311 
    312     return status;
    313 }
    314 
    315 
    316 TI_STATUS externalSec_rsnComplete(struct externalSec_t *pExternalSec)
    317 {
    318 	return externalSec_event(pExternalSec, EXTERNAL_SEC_EVENT_COMPLETE, pExternalSec);
    319 }
    320 
    321 /**
    322 *
    323 * Function  - externalSecSM_Nop.
    324 *
    325 * \b Description:
    326 *
    327 * Do nothing
    328 *
    329 * \b ARGS:
    330 *
    331 * \b RETURNS: TI_OK
    332 *
    333 */
    334 TI_STATUS externalSecSM_Nop(struct externalSec_t *pExternalSec)
    335 {
    336     return(TI_OK);
    337 }
    338 
    339 /**
    340 *
    341 * Function  - externalSecSM_Unexpected.
    342 *
    343 * \b Description:
    344 *
    345 * Do nothing
    346 *
    347 * \b ARGS:
    348 *
    349 * \b RETURNS: TI_STATUS
    350 *
    351 */
    352 TI_STATUS externalSecSM_Unexpected(struct externalSec_t *pExternalSec)
    353 {
    354     TRACE0(pExternalSec->hReport, REPORT_SEVERITY_ERROR, "EXTERNAL_SEC_SM: ERROR UnExpected Event\n");
    355     return(TI_OK);
    356 }
    357