/*
* Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "sys/time.h"
#include "kernel/os/os_time.h"
#include "common/framework/net_ctrl.h"
#include "net/HTTPClient/HTTPCUsr_api.h"
#include "mbedtls/mbedtls.h"
#include "mbedtls/base64.h"
#include "httpc_post.h"
#include "fs/fatfs/ff.h"
#include "efpg/efpg.h"
#include "common/framework/sysinfo.h"
#include "cjson/cJSON.h"
#define RECORDER_THREAD_STACK_SIZE (1024 * 4)
#define HTTPC_RECODER_URL "http://49.232.112.247:9227/api/pc/audio/uploadData"
#define FORM_DATA_BOUND_ "wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T"
#define FORM_DATA_BOUND "--"FORM_DATA_BOUND_
#define FORM_DATA_TAIL FORM_DATA_BOUND"--"
FIL fp;
static OS_Thread_t httpc_thread;
static OS_Queue_t httpc_queue;
struct send_record_msg {
char record_name[100];
int record_end;
uint32_t timeout;
};
static unsigned char data_checksum;
static int cmd_data_process_init(void)
{
data_checksum = 0;
return 0;
}
static int cmd_data_process(void *buffer, int length)
{
unsigned char *cal = (unsigned char *)buffer;
while (length != 0) {
data_checksum += cal[--length];
}
return 0;
}
static int cmd_data_process_deinit(void)
{
printf("[httpc cmd test]:checksum = %#x\n", data_checksum);
data_checksum = 0;
return 0;
}
void *HTTP_get_heads(void)
{
static char *headers = "Accept: */*&Connection: keep-alive&Authorization: Basic cmw6cmwxMjM0NTY=&Content-type: multipart/form-data; boundary=wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T";
return headers;
}
int FillFormData(char *file_name, char* postbuffer)
{
int ret = 0;
FRESULT result;
char* pt = postbuffer;
unsigned int act_read, file_size = 0, post_size = 0;
char* file_buf;
unsigned int buf_size = 4*1024;
printf("file_name:%s\n", file_name);
strcpy(pt,FORM_DATA_BOUND"\r\n");
//printf("FillFormData buffer postbuffer4:%s\n", postbuffer);
sprintf(pt+strlen(pt),"Content-Disposition: form-data; name=\"file\";filename=\"%s\"\r\n", file_name);
strcat(pt+strlen(pt),"Content-Type: audio/wav\r\n");
strcat(pt+strlen(pt),"\r\n");//file data 前空一行
post_size = strlen(pt);
result = f_open(&fp, file_name, FA_READ);
if (result != FR_OK) {
ret = -1;
printf("FillFormData f_open file failed\n");
return ret;
}
file_buf = malloc((buf_size + 1) * sizeof(char));
if (file_buf == NULL) {
ret = -1;
printf("FillFormData malloc failed\n");
return ret;
}
printf("FillFormData pt=%d\n", strlen(postbuffer));
pt += strlen(pt);
#if 1
while (1) {
result = f_read(&fp, file_buf, buf_size, &act_read);
if (result != FR_OK) {
printf("FillFormData read fail.\n");
return -1;
//break;
}
file_size += act_read;
#if 1
memcpy(pt, file_buf, act_read);
pt += act_read;
#endif
if (act_read != buf_size) {
printf("FillFormData reach file end\n");
break;
}
}
post_size += file_size;
printf("FillFormData file_size=%d, pt=%d\n", file_size, strlen(postbuffer));
#else
buf_size = 5*512*1024;
result = f_read(&fp, pt+strlen(pt), buf_size, &act_read);
if (result != FR_OK) {
printf("FillFormData read fail.\n");
return -1;
}
post_size += act_read;
printf("FillFormData act_read=%d, file_size=%d\n", file_size, strlen(pt));
#endif
//strcat(pt+strlen(pt),"\r\n");
//strcat(pt+strlen(pt),FORM_DATA_TAIL"\r\n");
pt = &postbuffer[0];
memcpy(pt+post_size, "\0", 1);
memcpy(pt+post_size+1, "\r\n", 2);
post_size += 3;
memcpy(pt+post_size, FORM_DATA_TAIL"\r\n", strlen(FORM_DATA_TAIL"\r\n"));
post_size += strlen(FORM_DATA_TAIL"\r\n");
//printf("FillFormData buffer postbuffer:\r\n%s\n", postbuffer);
return post_size;
}
static int httpc_post_server(char *file)
{
int ret = 0;
char *Postbuf = NULL;
char *Readbuf = NULL;
HTTP_CLIENT httpClient;
UINT32 toReadLength = 512;
UINT32 Received = 0;
HTTPParameters *clientParams = NULL;
#ifdef __CONFIG_PSRAM
UINT32 toPostLength = 3*1024*1024;
#else
UINT32 toPostLength = 1024*65;
#endif
unsigned long totalsize = 0;
char url[]="http://120.78.144.168:8000/";
printf("httpc_post start\n");
clientParams = malloc(sizeof(HTTPParameters));
if (clientParams == NULL) {
goto exit0;
}
memset(clientParams, 0, sizeof(HTTPParameters));
#ifdef __CONFIG_PSRAM
Postbuf = psram_malloc(toPostLength);
#else
Postbuf = malloc(toPostLength);
#endif
if (Postbuf == NULL) {
printf("httpc_post_start Postbuf malloc failed\n");
ret = -1;
}
memset(Postbuf, 0, toPostLength);
Readbuf = malloc(toReadLength);
if (Readbuf == NULL) {
goto exit0;
}
memset(Readbuf, 0, toReadLength);
memset(&httpClient, 0, sizeof(httpClient));
clientParams->HttpVerb = VerbPost;
clientParams->pData = Postbuf;
memcpy(clientParams->Uri, url, strlen(url));
totalsize = FillFormData(file, Postbuf);
//totalsize = strlen(Postbuf);
printf("FillFormData totalsize:%ld\n", totalsize);
if (totalsize <0){
goto exit0;
}
clientParams->pLength = totalsize;
//printf("Postbuf:\r\n%s\n", Postbuf);
//memcpy(Postbuf, recoder_data, strlen(recoder_data));
request:
ret = HTTPC_open(clientParams);
if (ret != 0) {
printf("http open err..\n");
goto exit0;
}
ret = HTTPC_request(clientParams, HTTP_get_heads);
if (ret != 0) {
printf("http request err..\n");
goto exit1;
}
ret = HTTPC_get_request_info(clientParams, &httpClient);
if (ret != 0) {
printf("http get request info err..\n");
goto exit1;
}
if (httpClient.HTTPStatusCode != HTTP_STATUS_OK) {
if ((httpClient.HTTPStatusCode == HTTP_STATUS_OBJECT_MOVED) ||
(httpClient.HTTPStatusCode == HTTP_STATUS_OBJECT_MOVED_PERMANENTLY)) {
printf("Redirect url..\n");
HTTPC_close(clientParams);
memset(clientParams, 0, sizeof(*clientParams));
clientParams->HttpVerb = VerbGet;
if (httpClient.RedirectUrl->nLength < sizeof(clientParams->Uri)) {
strncpy(clientParams->Uri, httpClient.RedirectUrl->pParam,
httpClient.RedirectUrl->nLength);
} else {
goto exit0;
}
printf("go to request.\n");
goto request;
} else {
ret = -1;
printf("get result not correct. HTTPStatusCode=%lu\n", httpClient.HTTPStatusCode);
goto exit1;
}
}
cmd_data_process_init();
if (httpClient.TotalResponseBodyLength != 0
|| (httpClient.HttpFlags & HTTP_CLIENT_FLAG_CHUNKED)) {
do {
ret = HTTPC_read(clientParams, Readbuf, toReadLength,
(void *)&Received);
if ((ret != HTTP_CLIENT_SUCCESS