nt9856x/code/application/source/cardv/SrcCode/System/SF_SysStrgMng.c

321 lines
8.5 KiB
C
Executable File

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <memory.h>
#include "DCF.h"
#include "sf_type.h"
#include "sf_log.h"
#include "SF_SysStrgMng.h"
#include <sf_param_common.h>
#include <sf_param_struct.h>
#include <FileSysTsk.h>
typedef struct
{
int event;
int dev_id;
int mounted_status;
}STRG_CB_PARAM;
static SF_ST_MMC_DEV *sf_mmc_dev[MMC_DEV_MAX_NUM] = {NULL};
void SF_StrgInit(void)
{
for(int i = 0; i < MMC_DEV_MAX_NUM; i++)
{
sf_mmc_dev[i] = (SF_ST_MMC_DEV *)malloc(sizeof(SF_ST_MMC_DEV));
if(sf_mmc_dev[i] == NULL) {
// 处理内存分配失败的情况
}
sf_mmc_dev[i]->dcf_handle = -1;
sf_mmc_dev[i]->node = MMC_DEV_NODE_STATE_NO;
sf_mmc_dev[i]->moumted_state = MMC_DEV_MOUNT_STATE_NO;
sf_mmc_dev[i]->space_state = MMC_DEV_SPACE_STATE_NO;
sf_mmc_dev[i]->loop_state = MMC_DEV_LOOP_STATE_NO;
}
}
void SF_StrgUnInit(void)
{
for(int i = 0; i < MMC_DEV_MAX_NUM; i++)
{
if(sf_mmc_dev[i] != NULL) {
free(sf_mmc_dev[i]);
sf_mmc_dev[i] = NULL;
}
}
}
void SF_StrgSetDcfHandle(UINT32 strg_id, UINT32 handle)
{
sf_mmc_dev[strg_id]->dcf_handle = handle;
}
static UINT32 SF_StrgSpaceCheckFull(MMC_DEV_TYPE mmc_dev)
{
UINT64 diskFree = 0;
UINT32 ret = 0;
/*check disk free size*/
if(mmc_dev == MMC_DEV_SD)
{
diskFree = FileSys_GetDiskInfo(FST_INFO_FREE_SPACE);
}
else
{
diskFree = FileSys_GetEMMCDiskInfo(FST_INFO_FREE_SPACE);
}
diskFree = diskFree/1024/1024;
if(diskFree < 30) /* 30MB */
{
ret = TRUE;
}
else
{
ret = FALSE;
}
MLOGD("[%s:%d]sd card diskFree=%lluM\n", __FUNCTION__, __LINE__, diskFree);
return ret;
}
static int SF_StrgNodeCheck(const char *devName)
{
int tryCnt = 200;
int ret = access(devName, F_OK);
while (ret != 0 && tryCnt--)
{
usleep(10*1000);
ret = access(devName, F_OK);
}
if (tryCnt < 0)
{
printf("no found %s\n", devName);
return -1;
}
return ret;
}
void SF_StrgRegister(SF_MESSAGE_BUF_S *pMessageBuf)
{
MLOGD("[dev_id:%d, evt:%d]\n",pMessageBuf->arg2, pMessageBuf->arg1);
UIMenuStoreInfo *puiPara = sf_ui_para_get();
if(pMessageBuf->arg2 == MMC_DEV_SD)
{
if(pMessageBuf->arg1 == CMD_SD_STRG_CB_INSERTED)
{
sf_mmc_dev[MMC_DEV_SD]->node = MMC_DEV_NODE_STATE_EXIST;
}
else if(pMessageBuf->arg1 == CMD_SD_STRG_CB_REMOVED)
{
sf_mmc_dev[MMC_DEV_SD]->node = MMC_DEV_NODE_STATE_NO_EXIST;
}
else if(pMessageBuf->arg1 == CMD_SD_STRG_CB_MOUNT_FINISH)
{
sf_mmc_dev[MMC_DEV_SD]->moumted_state = MMC_DEV_MOUNT_STATE_MOUNTED;
}
else if(pMessageBuf->arg1 == CMD_SD_STRG_CB_UNMOUNT_FINISH)
{
sf_mmc_dev[MMC_DEV_SD]->moumted_state = MMC_DEV_MOUNT_STATE_UNMOUNTED;
}
UINT32 isfull = SF_StrgSpaceCheckFull(MMC_DEV_SD);
if(isfull == 1)
{
sf_mmc_dev[MMC_DEV_SD]->space_state = MMC_DEV_SPACE_STATE_NO_ENOUGH;
}
else if(isfull == 0)
{
sf_mmc_dev[MMC_DEV_SD]->space_state = MMC_DEV_SPACE_STATE_ENOUGH;
}
if(puiPara->SdLoopSwitch == 1)
{
sf_mmc_dev[MMC_DEV_SD]->loop_state = MMC_DEV_LOOP_STATE_ON;
}
else if(puiPara->SdLoopSwitch == 0)
{
sf_mmc_dev[MMC_DEV_SD]->loop_state = MMC_DEV_LOOP_STATE_OFF;
}
}
else if(pMessageBuf->arg2 == MMC_DEV_EMMC)
{
if(SF_StrgNodeCheck(EMMC_DEVICE_NAME) == 0)
{
sf_mmc_dev[MMC_DEV_EMMC]->node = MMC_DEV_NODE_STATE_EXIST;
}
else
{
sf_mmc_dev[MMC_DEV_EMMC]->node = MMC_DEV_NODE_STATE_NO_EXIST;
}
if(pMessageBuf->arg1 == CMD_SD_STRG_CB_MOUNT_FINISH)
{
sf_mmc_dev[MMC_DEV_EMMC]->moumted_state = MMC_DEV_MOUNT_STATE_MOUNTED;
}
else if(pMessageBuf->arg1 == CMD_SD_STRG_CB_UNMOUNT_FINISH)
{
sf_mmc_dev[MMC_DEV_EMMC]->moumted_state = MMC_DEV_MOUNT_STATE_UNMOUNTED;
}
UINT32 isfull = SF_StrgSpaceCheckFull(MMC_DEV_EMMC);
if(isfull == 1)
{
sf_mmc_dev[MMC_DEV_EMMC]->space_state = MMC_DEV_SPACE_STATE_NO_ENOUGH;
}
else if(isfull == 0)
{
sf_mmc_dev[MMC_DEV_EMMC]->space_state = MMC_DEV_SPACE_STATE_ENOUGH;
}
if(puiPara->SdLoopSwitch == 1)
{
sf_mmc_dev[MMC_DEV_EMMC]->loop_state = MMC_DEV_LOOP_STATE_ON;
}
else if(puiPara->SdLoopSwitch == 0)
{
sf_mmc_dev[MMC_DEV_EMMC]->loop_state = MMC_DEV_LOOP_STATE_OFF;
}
}
for(int i = 0; i < 2; i++)
{
MLOGD("node:%d, mount:%d, space:%d, loop:%d\n",
sf_mmc_dev[i]->node, sf_mmc_dev[i]->moumted_state,
sf_mmc_dev[i]->space_state, sf_mmc_dev[i]->loop_state);
}
}
SF_ST_MMC_DEV *SF_StrgCheckWorkableDev(void)
{
MMC_DEV_TYPE workable_dev = MMC_DEV_NO;
MLOGD("sf mmc check workable dev start!!!\n");
if((sf_mmc_dev[MMC_DEV_SD]->node == MMC_DEV_NODE_STATE_NO_EXIST) &&
(sf_mmc_dev[MMC_DEV_EMMC]->node == MMC_DEV_NODE_STATE_NO_EXIST))
{
workable_dev = MMC_DEV_NO;
}
else if(sf_mmc_dev[MMC_DEV_EMMC]->node == MMC_DEV_NODE_STATE_NO_EXIST) //EMMC节点不存在
{
if(sf_mmc_dev[MMC_DEV_SD]->moumted_state == MMC_DEV_MOUNT_STATE_MOUNTED)
{
if(sf_mmc_dev[MMC_DEV_SD]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_SD;
}
else if(sf_mmc_dev[MMC_DEV_SD]->space_state == MMC_DEV_SPACE_STATE_NO_ENOUGH)
{
if(sf_mmc_dev[MMC_DEV_SD]->loop_state == MMC_DEV_LOOP_STATE_ON)
{
workable_dev = MMC_DEV_SD;
}
}
}
}
else if(sf_mmc_dev[MMC_DEV_SD]->node == MMC_DEV_NODE_STATE_NO_EXIST) //SD节点不存在
{
if(sf_mmc_dev[MMC_DEV_EMMC]->moumted_state == MMC_DEV_MOUNT_STATE_MOUNTED)
{
if(sf_mmc_dev[MMC_DEV_EMMC]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_EMMC;
}
else if(sf_mmc_dev[MMC_DEV_EMMC]->space_state == MMC_DEV_SPACE_STATE_NO_ENOUGH)
{
if(sf_mmc_dev[MMC_DEV_EMMC]->loop_state == MMC_DEV_LOOP_STATE_ON)
{
workable_dev = MMC_DEV_EMMC;
}
}
}
}
else //MMC节点都存在
{
if((sf_mmc_dev[MMC_DEV_SD]->moumted_state == MMC_DEV_MOUNT_STATE_UNMOUNTED) &&
(sf_mmc_dev[MMC_DEV_EMMC]->moumted_state == MMC_DEV_MOUNT_STATE_UNMOUNTED))
{
workable_dev = MMC_DEV_NO;
}
else if(sf_mmc_dev[MMC_DEV_SD]->moumted_state == MMC_DEV_MOUNT_STATE_UNMOUNTED)
{
if(sf_mmc_dev[MMC_DEV_EMMC]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_EMMC;
}
else
{
if(sf_mmc_dev[MMC_DEV_EMMC]->loop_state == MMC_DEV_LOOP_STATE_ON)
{
workable_dev = MMC_DEV_EMMC;
}
}
}
else if(sf_mmc_dev[MMC_DEV_EMMC]->moumted_state == MMC_DEV_MOUNT_STATE_UNMOUNTED)
{
if(sf_mmc_dev[MMC_DEV_SD]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_SD;
}
else
{
if(sf_mmc_dev[MMC_DEV_SD]->loop_state == MMC_DEV_LOOP_STATE_ON)
{
workable_dev = MMC_DEV_SD;
}
}
}
else
{
if(sf_mmc_dev[MMC_DEV_EMMC]->space_state == MMC_DEV_SPACE_STATE_ENOUGH &&
sf_mmc_dev[MMC_DEV_SD]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_EMMC;
}
else if(sf_mmc_dev[MMC_DEV_EMMC]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_EMMC;
}
else if(sf_mmc_dev[MMC_DEV_SD]->space_state == MMC_DEV_SPACE_STATE_ENOUGH)
{
workable_dev = MMC_DEV_SD;
}
else
{
if(sf_mmc_dev[MMC_DEV_SD]->loop_state == MMC_DEV_LOOP_STATE_ON)
{
workable_dev = MMC_DEV_SD;
}
}
}
}
MLOGD("sf mmc check workable dev end, dev_id = %d!!!\n", workable_dev);
for(int i = 0; i < 2; i++)
{
MLOGD("node:%d, mount:%d, space:%d, loop:%d\n",
sf_mmc_dev[i]->node, sf_mmc_dev[i]->moumted_state,
sf_mmc_dev[i]->space_state, sf_mmc_dev[i]->loop_state);
}
if(workable_dev != MMC_DEV_NO)
{
MLOGD("dcf_handle = %d\n", sf_mmc_dev[workable_dev]->dcf_handle);
return sf_mmc_dev[workable_dev];
}
else
return NULL;
}