linux下web端預覽SD卡的相關總結
最近在弄linux下web端的SD卡內容的預覽問題,研究了一下關於SD卡由前端web到下層呼叫的相關的程式碼。在此黏貼出來分享一下。
大概思路是這樣的:在web端的程式碼是建立對話方塊顯示SD卡的內容
web端事件在boa中通過雜湊表呼叫對應的響應函式。
在應用層有對應的事件的響應函式。對事件的處理。
整體思路是這樣的
web端顯示SD卡內容的函式
mkElem("sdleft",{src:IPNC.serverURL + "sdget.htm", "class":"ipnc-iframe"}),
boa中的雜湊表及對應的事件處理函式
{"/sdget.htm" ,uri_sdget_htm,AUTHORITY_VIEWER ,URI_FLAG_VIRTUAL_PAGE ,NULL },
int uri_sdget_htm(request * req)
{
char *addr, sdpath[128] = {0};
if (req->query_string && uri_decoding(req, req->query_string) < 0) {
send_r_bad_request(req);
return 0;
}
addr = (char *)malloc(MAX_SDLIST_LENGTH);
if (addr == NULL) {
send_r_error(req);
return 0;
}
req->filesize = http_sdget_cmd(req, req->cmd_arg, addr, MAX_SDLIST_LENGTH, sdpath);
if (req->filesize > 0) {
SQUASH_KA(req);
req->mem_flag |= MFLAG_IS_MEMORY;
req->data_mem = addr;
send_request_ok_sdget(req);
req->status = WRITE;
return 1;
}
free(addr);
if (req->pathname){
free(req->pathname);
}
strcat(sdpath, "/");
if(req->cmd_arg[0].value==NULL){
send_r_bad_request(req);
dbg("req->cmd_arg[0].value is NULL, no SD card\n");
return 0;
}
strcat(sdpath, req->cmd_arg[0].value);
req->pathname = strdup(sdpath);
if (!req->pathname) {
send_r_error(req);
WARN("unable to strdup buffer onto req->pathname");
return 0;
}
strncpy(req->request_uri, req->cmd_arg[0].value, MAX_HEADER_LENGTH);
return -1;
}
應用層中對事件處理
#define MMC_NODE "/dev/mmcblk0p1"
#define MMC_PATH "/mnt/mmc/ipnc/" //注意這個路徑是根據你自己的SD卡的路徑來設定的。不是固定的
int http_sdget_cmd(request *req, COMMAND_ARGUMENT *argm, char *addr, int max_size, char *sdpath)
{
int ret = 0;
if (argm->value != "\0")
{
if( CheckFileExist(MMC_PATH,argm->value) == 0 )
{
strcpy(sdpath, MMC_PATH);
return 0;
}
}
MEM_List_files_To_html( MMC_PATH, MMC_PATH, addr, max_size);
ret = strlen(addr);
return ret;
}
int MEM_List_files_To_html(char *pInputDir,char *pOutDisk, void *pOutMem, int MaxSize)
{
char http_ip_addr[100];
int count = 0;
int i = 0;
int error_code = 0;
FILE_INFO *pFilelist = NULL;
chartempbuff[100];
char recodingName[30];
int ret = 0;
int string_length = 0;
ret = ControlSystemData(SFIELD_GET_SD_FILE_NAME, (void *)recodingName, sizeof(recodingName));
if(ret >= 0 )
{
string_length = ret;
recodingName[string_length] = '\0';
}else{
string_length = 0;
}
GetIP_Addr(http_ip_addr);
fprintf(stderr,"para_netip %s \n",http_ip_addr);
if (chdir(pInputDir) != 0)
{
printf("Error in chdir \n");
error_code = -1;
goto QUIT_MEM_SAVE;
}
pFilelist = Get_File_List( pInputDir, &count );
if( pFilelist == NULL )
{
fprintf(stderr,"FileList Empty, No Files to Display \n");
error_code = -1;
goto QUIT_MEM_SAVE;
}
strncpy( pOutMem, html_sdget_header, MaxSize );
for (i=0;i<count;i++)
{
if( string_length > 0 )
{
if( strncmp(pFilelist[i].name, recodingName, sizeof(recodingName) ) == 0 )
{
continue;
}
}
sprintf(html_content,"A(\"%s\",\"%s\",\"%s\",\"%s\");",pFilelist[i].name,
pFilelist[i].date,
pFilelist[i].time,
pFilelist[i].size);
if( MaxSize > strlen(pOutMem) )
strncat(pOutMem, html_content, MaxSize-strlen(pOutMem));
}
if( MaxSize > strlen(pOutMem) )
strncat(pOutMem, "</SCRIPT>\n</table>\n</DIV>\n<HR size=1 color=#C0C0C0 width=620 align=left>\n", MaxSize-strlen(pOutMem));
else
fprintf(stderr,"MaxSize is not enough!\n");
sprintf(tempbuff, "%d file and %d KBytes free\n",count, (int)GetDiskfreeSpace(pOutDisk));
if( MaxSize > strlen(pOutMem) )
strncat(pOutMem, tempbuff, MaxSize-strlen(pOutMem));
else
fprintf(stderr,"MaxSize is not enough!\n");
QUIT_MEM_SAVE:
Clean_File_List();
chdir("/");
if( error_code < 0)
{
OuputMem_html_empty( pOutMem,MaxSize);
return error_code;
}
else
{
return count;
}
}
只黏貼出了和SD卡預覽相關的程式碼。給閱讀帶來不便的請諒解。
第一次寫博文,主要是對自己做過的工作的一個記錄。