Files
banban/ql-application/threadx/interface/custom_app/http_downfile.c
T

321 lines
7.7 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ql_rtos.h"
#include "ql_application.h"
#include "ql_audio.h"
#include "ql_iic.h"
#include "ql_gpio.h"
#include "ql_fs.h"
#include "webclient.h"
#include <netdb.h>
#include "sockets.h"
#include "layer3.h"
#include "ql_uart.h"
const char *mp3_file[] ={
"test.mp3",// 0
"network_connect_zh.mp3",//1 系统启动中,正在等待网络连接
"network_check.mp3",//2 网络不好 请检查网络
"upgrading.mp3",//3
"volume20.mp3",//4
"volume40.mp3",//5
"volume60.mp3",//6
"volume80.mp3",//7
"volume100.mp3",//8
"tts_error.mp3",//9
"have_rest_zh.mp3",//10 小憩一下,待会儿见
"network_ok_zh.mp3",//11 已连上网络可以按键或刷卡跟我对话喔
"device_sleep_mode_zh.mp3",//12 进入休眠模式
"device_wake_up_zh.mp3",//13 设备已唤醒
"low_power_zh.mp3",//14 电量低
"low_power_off_zh.mp3",//15 电量严重不足,马上关机
"volume_up_zh.mp3",//16
"volume_down_zh.mp3",//17
"ai_active_zh.mp3",//18
"barge_in_cute.mp3",//19 对话打断提示音
};
//http://banban.api.talkingq.com:8080/assets/roles/banban/tts_error.mp3
//#define PCM_FILE "http://47.113.99.138:7050/output_16k.pcm"
int http_file_download(const char *uri)
{
int res=0, length,total_length = 0;
int resp_status = (-1);
QFILE * fp = NULL;
char fd_buff[128]={0x00};
u8 *buffer_read = NULL;
//从url中分离文件名字
char *filename = strrchr(uri, '/');
if (filename==NULL) return -1;
filename++;//remove '/'
sprintf(fd_buff, "U:/%s", filename);
printf("http_file_download:%s\n", fd_buff);
ql_remove(fd_buff);
fp = ql_fopen(fd_buff, "wb+");
if(fp==NULL) {
printf("open file error\n");
res = -1;
goto __exit;
}
struct webclient_session *session = NULL;
session = webclient_session_create(1024);
if (!session)
{
printf("open uri failed.");
res = -1;
goto __exit;
}
resp_status = webclient_get(session, uri);
if (resp_status == 304)
{
printf("file download failed! Server http response : 304 not modified!");
res = -1;
goto __exit;
}
if (resp_status != 200)
{
printf("file download failed! Server http response : %d !", resp_status);
res = -1;
goto __exit;
}
if (session->content_length == 0)
{
printf("Request file size is 0!");
res = -1;
goto __exit;
}
// LOG_I("file size is (%d)", session->content_length);
buffer_read = web_malloc(4096);
if ((buffer_read == NULL))
{
printf("No memory for http ota!");
res = -1;
goto __exit;
}
printf("download start:%d\n", session->content_length);
do
{
u32 nw;
u32 page_pos;
nw = 4096;
if ((nw + total_length) > session->content_length)
{
nw = session->content_length - total_length;
//log_i("last page, content_length:%d, total_length:%d, nw:%d\n", session->content_length, total_length, nw);
}
/* read */
page_pos = 0;
do
{
length = webclient_read(session, buffer_read + page_pos, nw - page_pos);
if (length <= 0)
{
printf("read error: %d, content_pos:%d, page_pos:%d\n", length, total_length, page_pos);
break;
}
//LOG_I("webclient_read %d\n", length);
page_pos += length;
}
while (page_pos < nw); /* read */
if (page_pos != nw)
{
res = -1;
printf("page_pos error, %d:%d\n", page_pos, nw);
goto __exit;
}
ql_fwrite(buffer_read, nw, 1, fp);
total_length += nw;
//print_progress(total_length, session->content_length);
}
while (total_length != session->content_length);
printf("download end:%d\n", total_length);
__exit:
if(res==-1){
ql_remove(fd_buff);
}
if (session != NULL)
webclient_close(session);
if (buffer_read != NULL)
web_free(buffer_read);
if(fp!=NULL){
ql_fclose(fp);
fp = NULL;
}
return res;
}
void app_file_check_download(void)
{
int index = 1;
char url_buff[128]={0x00};
char filename_buff[64]={0x00};
int ret = 0;
for(index=1;index<20;index++){
int cnt = 0;
if(index==4||index==5||index==6||index==7||index==8){
continue;
}
if(strlen(mp3_file[index])>0){
sprintf(filename_buff, "U:/%s", mp3_file[index]);
if(ql_access(filename_buff, 0)!=0){
printf("downing mp3 file %d,%s\r\n", index,filename_buff);
down_loop:
sprintf(url_buff, "http://banban.api.talkingq.com:8080/assets/roles/banban/%s", mp3_file[index]);
ret = http_file_download(url_buff);
if(ret!=0&&cnt++<3) goto down_loop;
}
}
}
}
void app_file_check_del(void)
{
int index = 1;
char filename_buff[64]={0x00};
for(index=1;index<20;index++){
if(index==4||index==5||index==6||index==7||index==8){
continue;
}
if(strlen(mp3_file[index])>0){
sprintf(filename_buff, "U:/%s", mp3_file[index]);
if(ql_access(filename_buff, 0)==0){
ql_remove(filename_buff);
ql_rtos_task_sleep_ms(5);
}
}
}
}
#if 0
//注意目前文件系统大小默认为256k
uint8_t g_mp3_test1_buff[1024*60];
int32 g_mp3_test1_buff_len = 0;
u16 pcm_buff[2*SHINE_MAX_SAMPLES];
u16 *pbuff=pcm_buff;
void app_file_download(void)
{
QFILE *pcm_file_point=NULL;
uint8_t* data_out=NULL;
int count = 0;
int nread = 0;
struct lfs_info pcm_file;
ql_rtos_task_sleep_ms(10000);
pcm_file_point = ql_fopen("U:/output_16k.pcm","rb");
if(pcm_file_point==NULL) {
printf("ql_fopen output_16k.pcm error\n");
http_file_download(PCM_FILE);
pcm_file_point = ql_fopen("U:/output_16k.pcm","rb");
if(pcm_file_point==NULL) return;
}
ql_stat("U:/output_16k.pcm", &pcm_file);
printf("pcm_file.size:%d\n", pcm_file.size);
printf("pcm_file.name:%s\n", pcm_file.name);
shine_t sh_shine;
shine_config_t sh_config;
//--------初始化config.mpeg结构 ------
shine_set_config_mpeg_defaults(&(sh_config.mpeg)); //--init sh_config struct
//-------- 配置 config 结构 -------------
sh_config.wave.channels=PCM_MONO; //PCM_STEREO;//PCM_MONO;
sh_config.wave.samplerate=16000;//valid samplerates: 44100,48000,32000,22050,24000,16000,11025,12000,8000
sh_config.mpeg.copyright=1;
sh_config.mpeg.original=1;
sh_config.mpeg.mode=MONO;//STEREO;//MONO;
sh_config.mpeg.bitr=16; //64kbps for 16k //32kbps for 8k samplerate
sh_shine=shine_initialise(&sh_config);
do
{
nread=ql_fread(pcm_buff,sizeof(u16),576,pcm_file_point);
printf("nread:%d\n", nread);
if(nread<=0){
break;
}
else if(nread < 576){ //---小于喂给采样数时,剩余用0填充
printf("nread < samples_per_pass! nread = %d\n",nread);
memset(pcm_buff+nread,0,(576-nread)*sizeof(u16));
}else{
data_out = shine_encode_buffer(sh_shine,&pbuff,&count);
memcpy(g_mp3_test1_buff+g_mp3_test1_buff_len, data_out, count);
g_mp3_test1_buff_len+=count;
}
}while(1);
data_out=shine_flush(sh_shine,&count);
memcpy(g_mp3_test1_buff+g_mp3_test1_buff_len, data_out, count);
g_mp3_test1_buff_len+=count;
printf("app_file_download:%d\n", g_mp3_test1_buff_len);
if(pcm_file_point!=NULL){
ql_fclose(pcm_file_point);
pcm_file_point = NULL;
}
if(sh_shine!=NULL){
shine_close(sh_shine);
sh_shine = NULL;
}
unsigned char value = 0;
for(count=0;count<g_mp3_test1_buff_len;count++){
value = g_mp3_test1_buff[count];
ql_uart_write(QL_USB_CDC_PORT, &value, 1);
}
g_mp3_test1_buff_len = 0;
}
#endif