2025-04-10 17:31:33 +08:00

364 lines
13 KiB
C

/******************************************************************************
*(C) Copyright 2018 EIGENCOMM International Ltd.
* All Rights Reserved
******************************************************************************
* Filename: wifiCli.c
*
* Description:
*
* History:
*
* Notes:
*
******************************************************************************/
/*----------------------------------------------------------------------------*
* INCLUDES *
*----------------------------------------------------------------------------*/
#include "string.h"
#include "bsp.h"
#include "bsp_custom.h"
#include "os_common.h"
#include "ostask.h"
#include DEBUG_LOG_HEADER_FILE
#include "plat_config.h"
#include "slpman.h"
#include "networkmgr.h"
#include "sctdef.h"
#include "cms_api.h"
#include "sfdt.h"
#include "cmsis_os2.h"
#include "bsp_spi.h"
#include "slpman.h"
#ifdef FEATURE_CCIO_ENABLE
#include "spi_device.h"
#include "ccio_custom.h"
#endif
#include "switchWifi.h"
#include "wificliif.h"
#include "tcpipmgrutil.h"
#include "lwip/debug.h"
#include "lwip/err.h"
#ifdef FEATURE_WIFI_SWITCH_ENABLE
/*----------------------------------------------------------------------------*
* MACROS *
*----------------------------------------------------------------------------*/
#define SWITCH_WIFI_TASK_STACK_SIZE (1024)
#define SWITCH_WIFI_QUEUE_SIZE (4)
#define SWITCH_WIFI_NWMG_MODE_DEFAULT (0)
#define SWITCH_WIFI_NWMG_MODE_CAT1 (SWITCH_WIFI_NWMG_MODE_DEFAULT)
#define SWITCH_WIFI_NWMG_MODE_WIFI (1)
/*----------------------------------------------------------------------------*
* DATA TYPE DEFINITION *
*----------------------------------------------------------------------------*/
typedef struct _switchWifiObjectT{
uint8_t wanType; //<< refer to <NmWanNetType>
int32_t cid;
NmWifiCliConfiguration *cnf;
}switchWifiObjectT;
/*----------------------------------------------------------------------------*
* GLOBAL VARIABLES *
*----------------------------------------------------------------------------*/
static StaticTask_t gSwitchWifiTask;
static uint8_t gSwitchWifiTaskStack[SWITCH_WIFI_TASK_STACK_SIZE];
osMessageQueueId_t gSwitchWifiQueue = NULL;
static switchWifiObjectT gSwitchWfifiObj = {NM_WAN_NET_PS_CAT1, 0, NULL};
/*----------------------------------------------------------------------------*
* PRIVATE FUNCTION DECLEARATION *
*----------------------------------------------------------------------------*/
static void SFDT_switchWifiTaskEntry(void *arg);
static void SFDT_createSwitchWifiTask(void);
/*----------------------------------------------------------------------------*
* PRIVATE FUNCTIONS *
*----------------------------------------------------------------------------*/
static void SFDT_createSwitchWifiTask(void)
{
osThreadId_t threadId;
osThreadAttr_t thread_Attr;
gSwitchWifiQueue = osMessageQueueNew(SWITCH_WIFI_QUEUE_SIZE,sizeof(queueSwitchWifiT),NULL);
EC_ASSERT(gSwitchWifiQueue,gSwitchWifiQueue,0,0);
memset(&thread_Attr,0,sizeof(osThreadAttr_t));
memset(gSwitchWifiTaskStack,0xA5,SWITCH_WIFI_TASK_STACK_SIZE);
thread_Attr.name = "switch_wifi";
thread_Attr.stack_mem = gSwitchWifiTaskStack;
thread_Attr.stack_size = SWITCH_WIFI_TASK_STACK_SIZE;
thread_Attr.priority = osPriorityNormal;
thread_Attr.cb_mem = &gSwitchWifiTask;
thread_Attr.cb_size = sizeof(StaticTask_t);
threadId = osThreadNew(SFDT_switchWifiTaskEntry, NULL, &thread_Attr);
EC_ASSERT(threadId,threadId,0,0);
return;
}
/**
\fn static void SFDT_switchWifiTaskEntry(void *arg)
\brief
\return
*/
PLAT_FM_RAMCODE static void SFDT_switchWifiTaskEntry(void *arg)
{
NmResult ret;
queueSwitchWifiT queue = {0};
wanSwitchCnfCmdMsg_t cnfMsg = {0};
UINT8 routeCid = SWITCH_DEFAULT_ROUTE_CID;
int primSize = sizeof(cnfMsg);
while(1)
{
if(osMessageQueueGet(gSwitchWifiQueue,&queue,0,osWaitForever) == osOK)
{
ECPLAT_PRINTF(UNILOG_PLA_APP,SFDT_switchWifiTaskEntry_0, P_VALUE, "switchType %e<NmWanNetType>",queue.switchType);
if(queue.switchType == NM_WAN_NET_WIFI_CLI)
{
gSwitchWfifiObj.cnf = (NmWifiCliConfiguration *)queue.config.wifiCfg;
EC_ASSERT(gSwitchWfifiObj.cnf, gSwitchWfifiObj.cnf, 0, 0);
gSwitchWfifiObj.cid = 0xff;//clear cid
if(queue.switchType == gSwitchWfifiObj.wanType)
{
NetMgrWifiWanLinkDown();
}
ret = NetMgrWifiWanLinkUp(gSwitchWfifiObj.cnf);
EC_ASSERT(ret == NM_SUCCESS,ret, 0, 0);
}
else if(queue.switchType == NM_WAN_NET_PS_CAT1)
{
if(gSwitchWfifiObj.cid == queue.config.cid)
{
ECPLAT_PRINTF(UNILOG_PLA_APP,SFDT_switchWifiTaskEntry_1, P_WARNING, "same cid [%d]",queue.config.cid);
cnfMsg.ret = WSAT_ERR_SWITCH_FAIL;
applSendCmsCnf(queue.reqHandler, APPL_RET_FAIL, APPL_WIFI_SWITCH, APPL_SW_WAN_SWITCH_CNF, primSize, &cnfMsg);
continue;
}
gSwitchWfifiObj.cid = queue.config.cid;
if(queue.switchType != gSwitchWfifiObj.wanType)
{
NetMgrWifiWanLinkDown();
}
}
else
{
ECPLAT_PRINTF(UNILOG_PLA_APP,SFDT_switchWifiTaskEntry_2, P_ERROR, "invalid switchType[%d]",queue.switchType);
}
if(gSwitchWfifiObj.cid == SWITCH_DEFAULT_ROUTE_CID)
{
routeCid = CSIO_IPOS_CID_DFT;
}
ret = NetMgrWanDefaultRouteChange(queue.switchType, routeCid);
EC_ASSERT(ret == NM_SUCCESS,ret, 0, 0);
if(gSwitchWfifiObj.wanType != queue.switchType)
{
#ifdef FEATURE_CCIO_ENABLE
//osThreadId_t threadId = NULL;
uint8_t newType = CSIO_DT_OPAQ;
uint8_t newCustFlag = CSIO_OCF_IPOS;
uint8_t newCustExtras = 1 << CSIO_IPOS_CID_DFT;
if(queue.switchType == NM_WAN_NET_PS_CAT1)
{
newCustExtras = (1 << queue.config.cid);
}
else
{
newCustFlag = CSIO_OCF_IPOS_MIFI;
newCustExtras = 1 << CSIO_IPOS_ID_MIFI;
}
gSwitchWfifiObj.wanType = queue.switchType;
//threadId = (osThreadId_t)SFDT_getSlaveThreadId();
//EC_ASSERT(threadId != NULL,threadId,0,0);
//osThreadSuspend(threadId);
NVIC_DisableIRQ(SFDT_SLAVE_MRDY_IRQ);
NVIC_ClearPendingIRQ(SFDT_SLAVE_MRDY_IRQ);
spiDevTransform(0,newType,((newCustFlag & 0xf) << 16 | newCustExtras));
NVIC_EnableIRQ(SFDT_SLAVE_MRDY_IRQ);
//osThreadResume(threadId);
#endif
}
cnfMsg.ret = WSAT_OK;
applSendCmsCnf(queue.reqHandler, APPL_RET_SUCC, APPL_WIFI_SWITCH, APPL_SW_WAN_SWITCH_CNF, primSize, &cnfMsg);
}
}
osThreadExit();
}
/******************************************************************************
* NetMgrWifiWanLinkUpCallback
* Description: Link up one net (netif), called in LWIP task
* input: NetAdptWanDeRegCfg *pRegCfg
* output: err_t
* Comment:
******************************************************************************/
static err_t NetMgrWifiWanLinkUpCallback(void *pInput, UINT16 inputLen, void *pOutput, UINT16 outputLen)
{
NmWifiCliConfiguration *pWifiWanCfg = (NmWifiCliConfiguration *)pInput;
LWIP_UNUSED_ARG(inputLen);
LWIP_UNUSED_ARG(pOutput);
LWIP_UNUSED_ARG(outputLen);
OsaCheck(pWifiWanCfg != PNULL, pWifiWanCfg, 0, 0);
return wifi_client_wan_linkup_callback(pWifiWanCfg);
}
/******************************************************************************
* NetMgrWifiWanLinkDownCallback
* Description: Link down one net (netif), called in LWIP task
* input: NetAdptWanDeRegCfg *pRegCfg
* output: err_t
* Comment:
******************************************************************************/
static err_t NetMgrWifiWanLinkDownCallback(void *pInput, UINT16 inputLen, void *pOutput, UINT16 outputLen)
{
LWIP_UNUSED_ARG(pInput);
LWIP_UNUSED_ARG(inputLen);
LWIP_UNUSED_ARG(pOutput);
LWIP_UNUSED_ARG(outputLen);
return wifi_client_wan_linkdown_callback();
}
/******************************************************************************
* NetMgrWanDefaultRouteChangeCallback
* Description: Link up one net (netif), called in LWIP task
* input: NetAdptWanDeRegCfg *pRegCfg
* output: err_t
* Comment:
******************************************************************************/
static err_t NetMgrWanDefaultRouteChangeCallback(void *pInput, UINT16 inputLen, void *pOutput, UINT16 outputLen)
{
NmWanNetDefaultChangeInfo *pWanDefaultChangeInfo = (NmWanNetDefaultChangeInfo *)pInput;
LWIP_UNUSED_ARG(inputLen);
LWIP_UNUSED_ARG(pOutput);
LWIP_UNUSED_ARG(outputLen);
OsaCheck(pWanDefaultChangeInfo != PNULL, pWanDefaultChangeInfo, 0, 0);
return wifi_client_wan_default_route_change_callback(pWanDefaultChangeInfo);
}
/*----------------------------------------------------------------------------*
* GLOBAL FUNCTIONS *
*----------------------------------------------------------------------------*/
int32_t sfdt_switchWifiInit(void)
{
SFDT_createSwitchWifiTask();
return 0;
}
#ifdef FEATURE_WIFI_SWITCH_ENABLE
/******************************************************************************
* NetMgrWifiWanLinkUp
* Description: active wifi client as wan netif
* input: NmWifiCliConfiguration *pIfCfg
*
*
* output: NmResult
* Comment:
******************************************************************************/
NmResult NetMgrWifiWanLinkUp(NmWifiCliConfiguration *pIfCfg)
{
err_t err;
ECOMM_TRACE(UNILOG_TCPIP_NETMGR, NetMgrWifiWanLinkUp_1, P_SIG, 0,
"NETMANAGER link UP WIFI CLI WAN");
//send the tcpip call message to tcpip thread
err = TcpipMgrLwipCallbackBlockWithoutPsStatusCheck(NetMgrWifiWanLinkUpCallback, pIfCfg, sizeof(NmWifiCliConfiguration), PNULL, 0);
if (err == ERR_OK)
{
return NM_SUCCESS;
}
return NM_FAIL;
}
/******************************************************************************
* NetMgrWifiWanLinkDown
* Description: deactive wifi client as wan netif
* input:
*
*
* output: NmResult
* Comment:
******************************************************************************/
NmResult NetMgrWifiWanLinkDown(void)
{
err_t err;
ECOMM_TRACE(UNILOG_TCPIP_NETMGR, NetMgrWifiWanLinkDown_1, P_SIG, 0,
"NETMANAGER link DOWN WIFI CLI WAN");
//send the tcpip call message to tcpip thread
err = TcpipMgrLwipCallbackBlockWithoutPsStatusCheck(NetMgrWifiWanLinkDownCallback, PNULL, 0, PNULL, 0);
if (err == ERR_OK)
{
return NM_SUCCESS;
}
return NM_FAIL;
}
/******************************************************************************
* NetMgrWanDefaultRouteChange
* Description: deactive wifi client as wan netif
* input:UINT8 type(NmWanNetType),
* UINT8 cid(if the type is NM_WAN_NET_PS_CAT1, need input this parameter)
*
*
* output: NmResult
* Comment:
******************************************************************************/
NmResult NetMgrWanDefaultRouteChange(UINT8 type, UINT8 cid)
{
err_t err;
NmWanNetDefaultChangeInfo defaultChangeInfo;
ECOMM_TRACE(UNILOG_TCPIP_NETMGR, NetMgrWanDefaultRouteChange, P_SIG, 2,
"NETMANAGER WAN DEFAULT ROUTE CHANGE to type %u, cid %u", type, cid);
defaultChangeInfo.type = type;
defaultChangeInfo.cid = cid;
//send the tcpip call message to tcpip thread
err = TcpipMgrLwipCallbackBlockWithoutPsStatusCheck(NetMgrWanDefaultRouteChangeCallback, &defaultChangeInfo, sizeof(NmWanNetDefaultChangeInfo), PNULL, 0);
if (err == ERR_OK)
{
return NM_SUCCESS;
}
return NM_FAIL;
}
#endif
#endif/*FEATURE_WIFI_SWITCH_ENABLE*/