# Include <include. h>
# Include <ts_s_file.h>
# Include <stdlib. h>
/*************************************** ****************************************
FUNC: Delete the standard file path. EG/example/New file. txt
Date: 2014-7-27
Note: When filesize is set to 0, the file length is not determined.
**************************************** ***************************************/
Uint8 tqfs_filedelete (uint8 * path)
{
Ts_s_open_file open_file;
Stringcoppy (path, & open_file.path [0]);
Return 0;
}
/*************************************** ****************************************
FUNC: Fill in the basic information of the file container
Date: 2014-7-27
Note: When filesize is set to 0, the file length is not determined.
**************************************** ***************************************/
Void ts_s_fileroominfor (file_infor * fi, void * file, uint32 filesize, uint8 filevirtue)
{
Uint8 Buf [14];
Bufferfill (FI, 0xff, sizeof (file_infor); // If the configuration is not set, the status is not modified.
Getfilename (file, & Buf [0]);
Stringcoppy (& Buf [0], & fi-> filename [0]);
Getfileformat (file, & Buf [0]);
Stringcoppy (& Buf [0], & fi-> fileformat [0]);
Fi-> filetype = ft_readyroom; // file type to be allocated
Buffercoppy (& nowtime, & fi-> createdate, sizeof (date_time ));
Fi-> page_status = ps_beuse; // The page will be used soon
If (filesize) Fi-> filesize = filesize;
Fi-> fileid = rand (); // The File ID is a random number, but the ID of the same file must be the same
Fi-> fileue ue = filevirtue; // readable, writable, hidden, and other attributes
}
/*************************************** ****************************************
FUNC: create a file space on a page
Date: 2014-7-27
Note: When the page is relative, the homepage of the NAS is regarded as the first page.
**************************************** ***************************************/
Uint32 tqfs_createfiloom (tqfs_open_file * open_file, uint32 page, file_infor * fi)
{
Fi-> fileid = page;
Fi-> fileindex = 0;
Debug ("create object file in page [% d]", page, infor_infor );
Write_fileinfor (page, FI );
Ts_s_setcursor (open_file, page, 0 );
Return 0;
}
/*************************************** ****************************************
FUNC: Set the read/write operation cursor
Date: 2014-7-27
Note: When the page is relative, the homepage of the NAS is regarded as the first page.
**************************************** ***************************************/
Void tqfs_setcursor (ts_s_open_file * open_file, uint32 page, uint16 offset)
{
Open_file-> pagecursor = page;
Open_file-> offsetcursor = offset;
}
/*************************************** ****************************************
FUNC: Read the page content
Date: 2014-7-27
Note: Return status
**************************************** ***************************************/
Ts_s_error tqfs_readpagecontext (ts_s_open_file * open_file, uint8 * rbuffer, uint16 length)
{
File_infor tempfile;
Int ovilenth;
Uint16 offset = 0;
Ovilenth = (open_file-> offsetcursor + length );
Ovilenth-= nand_page_size; // The length that cannot be written
If (ovilenth> = 0) // the current page is insufficient.
{
Nand_readpage (nand_infor.filestartpage + open_file-> pagecursor, & rbuffer [offset], open_file-> offsetcursor + nand_spare_size, length-ovilenth );
Offset = length-ovilenth;
Open_file-> offsetcursor = 0;
Read_fileinfor (open_file-> pagecursor, & tempfile );
If (tempfile. nextpageaddr = 0 xffffffff) return te_nonextpage;
Else {
Open_file-> pagecursor = tempfile. nextpageaddr;
Length = ovilenth;
}
}
Nand_readpage (nand_infor.filestartpage + open_file-> pagecursor, & rbuffer [offset], open_file-> offsetcursor + nand_spare_size, length );
Open_file-> offsetcursor + = length;
If (ovilenth> = 0) return te_newpage;
Else return te_currentpage;
}
/*************************************** ****************************************
FUNC: write content to the specified page
Date: 2014-7-27
Note: returns the remaining amount written on this page.
**************************************** ***************************************/
Ts_s_error tqfs_writepagecontext (ts_s_open_file * open_file, uint8 * buffer, uint16 length)
{
File_infor tempfile;
Int ovilenth;
Uint16 offset = 0;
Ovilenth = (open_file-> offsetcursor + length );
Ovilenth-= nand_page_size; // The length that cannot be written
If (ovilenth> = 0) // the current page is insufficient.
{
Nand_writepage (nand_infor.filestartpage + open_file-> pagecursor, & buffer [offset], open_file-> offsetcursor + nand_spare_size, length-ovilenth );
Offset = length-ovilenth;
Open_file-> offsetcursor = 0;
Read_fileinfor (open_file-> pagecursor, & tempfile );
Tempfile. nextpageaddr = ts_s_findkeypagemark (ps_canuse, ps_beuse );
Tempfile. page_status = ps_usedfully; // The tag page is used up.
Write_fileinfor (open_file-> pagecursor, & tempfile); // write the following address to the current page.
Open_file-> pagecursor = tempfile. nextpageaddr;
Tempfile. page_status = ps_notfully;
Tempfile. previouspageaddr = open_file-> pagecursor;
Tempfile. nextpageaddr = 0 xffffffff;
Write_fileinfor (open_file-> pagecursor, & tempfile); // write the following address to the current page.
Length = ovilenth;
}
Nand_writepage (nand_infor.filestartpage + open_file-> pagecursor, & buffer [offset], open_file-> offsetcursor + nand_spare_size, length );
Open_file-> offsetcursor + = length;
If (ovilenth> = 0) return te_newpage;
Else return te_currentpage;
}
Void tqfs_rootdircreate (tqfs_open_file * open_file)
{
Uint32 I;
File_infor tempfile;
I = ts_s_findkeypagemark (ps_canuse, ps_beuse );
Tempfile. filetype = ft_rootdirhead;
Ts_s_fileroominfor (& tempfile, "rootdir", 0, readonly );
Ts_s_createfiloom (open_file, I, & tempfile );
}
Uint8 tqfs_filecreate (uint8 * path, uint8 state)
{
Return 0;
}
Ts_s_error tqfs_findfilebyname (uint8 * file, uint16 length)
{
File_infor temp_file;
Uint32 startpage = 0;
While (length --)
{
Read_fileinfor (startpage, & temp_file );
Startpage ++;
}
Return te_notfind;
}
# Include <include. h>
# Include <tos_device.h>
Deviceinfor _ tos_device_tab [_ tos_devicetotal];
/* Configure /*---------------------------------------------------------------------------------------------------------
FUNC: device information list Initialization
Time: 2014-5-22
Ver.: V1.0
Note;
Optional ---------------------------------------------------------------------------------------------------------*/
Void devicelist_init ()
{
Uint8 I;
For (I = 0; I <_ tos_devicetotal; I ++)
{
_ Tos_device_tab [I]. DeviceID = NULL;
_ Tos_device_tab [I]. init = NULL;
_ Tos_device_tab [I]. devicestate = ready;
_ Tos_device_tab [I]. Read = NULL;
_ Tos_device_tab [I]. Write = NULL;
_ Tos_device_tab [I]. Exit = NULL;
}
}
Bool tos_taskgetdev (uint8 ID, uint32 timeout)
{
Goget:
Switch (_ tos_device_tab [ID]. devicestate)
{
Case delete:
Return false;
Case busy:
If (_ tos_device_tab [ID]. deviceownerid = tos_taskcurrent) return true;
Else {tos_waitdev (ID); goto goget ;}
Case notbusy:
_ Tos_device_tab [ID]. devicestate = busy;
_ Tos_device_tab [ID]. deviceownerid = tos_taskcurrent;
Return true;
Case ready:
_ Tos_device_tab [ID]. devicestate = busy;
_ Tos_device_tab [ID]. deviceownerid = tos_taskcurrent;
Return true;
Default: Return false;
}
}
Bool tos_taskdropdev (uint8 ID)
{
If (_ tos_device_tab [ID]. devicestate = delete) return false;
If (_ tos_device_tab [ID]. deviceownerid! = Tos_taskcurrent) return false;
_ Tos_device_tab [ID]. devicestate = notbusy;
Return true;
}
/* Configure /*---------------------------------------------------------------------------------------------------------
FUNC: Device initialization and device registration
Time: 2014-5-22
Ver.: V1.0
Note: After registering a device, you must initialize the device in the device list.
Optional ---------------------------------------------------------------------------------------------------------*/
Void device_init ()
{
Uint8 I;
For (I = 0; I <_ tos_devicetotal; I ++)
{
If (_ tos_device_tab [I]. DeviceID! = NULL)
{
If (_ tos_device_tab [I]. init) _ tos_device_tab [I]. INIT ();
}
}
}
Void devicelist_printf ()
{
Uint8 I;
For (I = 0; I <_ tos_devicetotal; I ++)
{
If (_ tos_device_tab [I]. DeviceID! = NULL)
{
Debug ("% S % d % S % s", "device [", I, "] Name:", _ tos_device_tab [I]. devicename, infor_infor );
}
}
}
Bool deviceunmount (uint8 ID)
{
If (_ tos_device_tab [ID]. Exit) {_ tos_device_tab [ID]. Exit (); Return true ;}
Return false;
}
Example