/*******************************************************************************
  Function:	
    UINT16 WF_CMDisconnect(void)

  Summary:
    Commands the MRF24W to close any open connections and/or to cease
    attempting to connect.

  Description:
    Directs the Connection Manager to close any open connection or connection 
    attempt in progress.  No further attempts to connect are taken until 
    WF_CMConnect() is called.  Generates the event 
    WF_EVENT_CONNECTION_PERMANENTLY_LOST when the connection is successfully
    terminated.
    
  Precondition:
    MACInit must be called.

  Parameters:
    None.

  Returns:
    Operation results. Success or Failure
  	
  Remarks:
    None.
  *****************************************************************************/
UINT16 WF_CMDisconnect(void)
{
    UINT8  hdrBuf[2];

	/* WARNING !!! : 
	* Disconnect is allowed only in connected state. 
	* If module FW is in the midst of connection ( or reconenction) process, then
	* disconnect can hammer connection process, and furthermore it may cause
	* fatal failure in module FW operation. To be safte to use disconnect, we strongly
	* recommend you to disable module FW connection manager by uncommenting
	* #define DISABLE_MODULE_FW_CONNECT_MANAGER_IN_INFRASTRUCTURE	
	* in WF_Config.h
	*/
	if (!WF_CMIsDisconnectAllowed())
		return WF_ERROR_DISCONNECT_FAILED;

    hdrBuf[0] = WF_MGMT_REQUEST_TYPE;
    hdrBuf[1] = WF_CM_DISCONNECT_SUBYTPE;

    SendMgmtMsg(hdrBuf,
                sizeof(hdrBuf),
                NULL,
                0);
 
    /* wait for mgmt response, free after it comes in, don't need data bytes */
    WaitForMgmtResponse(WF_CM_DISCONNECT_SUBYTPE, FREE_MGMT_BUFFER);

    /* set state to no connection */
    SetLogicalConnectionState(FALSE);
    
    return WF_SUCCESS;
}    
/*******************************************************************************
  Function:
    uint16_t WF_CMDisconnect(void)

  Summary:
    Commands the MRF24W to close any open connections and/or to cease
    attempting to connect.

  Description:
    Directs the Connection Manager to close the open connection.
    No further attempts to connect are taken until WF_CMConnect() is called.

  Precondition:
    MACInit must be called.

  Parameters:
    None.

  Returns:
    Operation results. Success or Failure

  Remarks:
    Disconnection can work only in the connected state.
    To use this API safely, we recommend to disable module FW connection
    manager by setting WF_MODULE_CONNECTION_MANAGER == WF_DISABLED
    in drv_wifi_config.h
  *****************************************************************************/
uint16_t WF_CMDisconnect(void)
{
    uint8_t  hdrBuf[2];

    /* Check if we can call disconnect. Disconnect can work only in the connected state */
    if (!WF_CMIsDisconnectAllowed())
    {
        return WF_ERROR_DISCONNECT_FAILED;
    }

    hdrBuf[0] = WF_MGMT_REQUEST_TYPE;
    hdrBuf[1] = WF_CM_DISCONNECT_SUBYTPE;

    SendMgmtMsg(hdrBuf,
                sizeof(hdrBuf),
                NULL,
                0);

    /* wait for mgmt response, free after it comes in, don't need data bytes */
    WaitForMgmtResponse(WF_CM_DISCONNECT_SUBYTPE, FREE_MGMT_BUFFER);

    /* set state to no connection */
    SetLogicalConnectionState(false);

    return WF_SUCCESS;
}