11 Commits

Author SHA1 Message Date
oopuuu
8496e8cfd6 Update README.md
增加烧录说明引用
2025-05-08 17:02:34 +08:00
nhkefus
ade64139cb Merge branch 'dev_file_ota'
# Conflicts:
#	TC1/http_server/app_httpd.c
#	TC1/http_server/web/index.html
#	TC1/mqtt_server/user_mqtt_client.c
#	TC1/ota_server/ota_server.c
#	mico-os/libraries/daemons/http_server/httpd.c
2025-04-28 16:19:53 +08:00
oopuuu
e352b74e19 Update README.md 2025-04-19 15:36:15 +08:00
nhkefus
55a5f93148 调试用 2025-04-09 17:27:30 +08:00
nhkefus
86b78d8a4e 调试用 2025-04-09 17:06:49 +08:00
nhkefus
d8099ad499 调试用 2025-04-09 16:59:16 +08:00
nhkefus
4aca161327 调试用 2025-04-09 15:11:52 +08:00
nhkefus
67aa42fdd9 调试用 2025-04-09 14:54:46 +08:00
Your Name
6e3a48cba6 修复自定义名称过长可能导致系统崩溃的问题 2025-04-08 23:00:40 +08:00
nhkefus
fea21ab02a 更新了一些新版SDK中 ota和http相关的代码,不知道有没有用 2025-04-08 13:29:35 +08:00
nhkefus
49847dc77c 尝试增加https下载ota文件的功能,增加后台直接上传ota bin文件来升级的功能 2025-04-03 16:05:55 +08:00
8 changed files with 1416 additions and 1372 deletions

View File

@@ -60,7 +60,9 @@ TC1 排插硬件分 A1 A2 两个版本, 本固件仅支持 **A1 版本**. A1 A2
- [x] 后台可自定义设备名称此名称将会同至ha mqtt作为mqtt中插座名称的前缀 - [x] 后台可自定义设备名称此名称将会同至ha mqtt作为mqtt中插座名称的前缀
- [x] 后台可自定义按键连击,长按等功能 - [x] 后台可自定义按键连击,长按等功能
- [x] 定时开关现在支持更多操作项,与按键功能自定义一样 - [x] 定时开关现在支持更多操作项,与按键功能自定义一样
- [x] 现在可以上传本地文件来更新固件,同时可以在最新发布的固件说明中参考使用我搭建的文件服务来在线更新
# 固件烧录
- 参考原始固件代码提供者[a2633063](https://github.com/a2633063/zTC1/wiki/固件烧录)大佬提供的说明
# 编译固件 # 编译固件
- windows环境先安装python2.7,下载安装的时候添加环境变量到Path默认安装路径需要添加两个环境变量C:\Python27C:\Python27\Scripts - windows环境先安装python2.7,下载安装的时候添加环境变量到Path默认安装路径需要添加两个环境变量C:\Python27C:\Python27\Scripts

View File

@@ -32,7 +32,7 @@
#define __ota_server_H #define __ota_server_H
#define OTA_DEBUG (1) #define OTA_DEBUG (1)
#define OTA_USE_HTTPS (0) #define OTA_USE_HTTPS (1)
#define OTA_MD5_LENTH 32 #define OTA_MD5_LENTH 32
#define OTA_SEND_HEAD_SIZE 256 #define OTA_SEND_HEAD_SIZE 256
@@ -49,8 +49,8 @@ typedef enum _OTA_STATE_E{
}OTA_STATE_E; }OTA_STATE_E;
typedef enum _HTTP_SECURITY_E{ typedef enum _HTTP_SECURITY_E{
HTTP_SECURITY_HTTP, HTTP_SECURITY_HTTP,
HTTP_SECURITY_HTTPS HTTP_SECURITY_HTTPS
} HTTP_SECURITY_E; } HTTP_SECURITY_E;
typedef enum _OTA_CONTROL_E{ typedef enum _OTA_CONTROL_E{
@@ -87,11 +87,11 @@ typedef struct _ota_check_t{
typedef void (*ota_server_cb_fn) (OTA_STATE_E state, float progress); typedef void (*ota_server_cb_fn) (OTA_STATE_E state, float progress);
typedef struct _ota_server_context_t{ typedef struct _ota_server_context_t{
download_url_t download_url; download_url_t download_url;
download_state_t download_state; download_state_t download_state;
ota_check_t ota_check; ota_check_t ota_check;
OTA_CONTROL_E ota_control; OTA_CONTROL_E ota_control;
ota_server_cb_fn ota_server_cb; ota_server_cb_fn ota_server_cb;
} ota_server_context_t; } ota_server_context_t;
@@ -109,34 +109,34 @@ typedef struct _ota_server_context_t{
* @return kNoErr : on success. * @return kNoErr : on success.
* @return kGeneralErr : if an error occurred * @return kGeneralErr : if an error occurred
*/ */
OSStatus OtaServerStart(char *url, char *md5, ota_server_cb_fn call_back); OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back );
/** @brief Get OTA server state /** @brief Get OTA server state
* *
* @return OTA_CONTROL_E : state * @return OTA_CONTROL_E : state
*/ */
OTA_CONTROL_E OtaServerStateGet(void); OTA_CONTROL_E ota_server_state_get( void );
/** @brief Pause OTA server daemons /** @brief Pause OTA server daemons
* *
* @return No * @return No
*/ */
void OtaServerPause(void); void ota_server_pause( void );
/** @brief Continue OTA server daemons /** @brief Continue OTA server daemons
* *
* @return No * @return No
*/ */
void OtaServerContinue(void); void ota_server_continue( void );
/** @brief Stop OTA server daemons /** @brief Stop OTA server daemons
* *
* @return No * @return No
*/ */
void OtaServerStop(void); void ota_server_stop( void );
#endif #endif

View File

@@ -36,6 +36,6 @@ void UserOtaStart(char *url, char *md5)
{ {
ota_progress = 0; ota_progress = 0;
ota_log("ready to ota:%s",url); ota_log("ready to ota:%s",url);
OtaServerStart(url, md5, OtaServerStatusHandler); ota_server_start(url, md5, OtaServerStatusHandler);
} }

View File

@@ -58,173 +58,203 @@ static httpd_request_t httpd_req;
int httpd_send_chunk_begin(int conn, int size) int httpd_send_chunk_begin(int conn, int size)
{ {
int err; int err;
char buf[CHUNK_SIZE_DIGITS]; char buf[CHUNK_SIZE_DIGITS];
int i; int i;
int digit; int digit;
int begin = 1; int begin = 1;
for (i = CHUNK_SIZE_DIGITS - 1; i >= 0; i--) { for (i = CHUNK_SIZE_DIGITS - 1; i >= 0; i--) {
digit = size & 0xf; digit = size & 0xf;
if (!begin && !size) { if (!begin && !size) {
i++; i++;
break; break;
} }
buf[i] = (digit > 9) ? digit - 0xA + 'a' : digit + '0'; buf[i] = (digit > 9) ? digit - 0xA + 'a' : digit + '0';
size = size >> 4; size = size >> 4;
begin = 0; begin = 0;
} }
err = httpd_send(conn, &buf[i], CHUNK_SIZE_DIGITS - i); err = httpd_send(conn, &buf[i], CHUNK_SIZE_DIGITS - i);
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
err = httpd_send_crlf(conn); err = httpd_send_crlf(conn);
return err; return err;
} }
/* Send the last chunk which is simply an ascii "0\r\n\r\n" /* Send the last chunk which is simply an ascii "0\r\n\r\n"
*/ */
int httpd_send_last_chunk(int conn) int httpd_send_last_chunk(int conn)
{ {
int err; int err;
err = httpd_send(conn, http_last_chunk, err = httpd_send(conn, http_last_chunk,
sizeof(http_last_chunk) - 1); sizeof(http_last_chunk) - 1);
if (err != kNoErr) { if (err != kNoErr) {
httpd_d("Send last chunk failed"); httpd_d("Send last chunk failed");
} }
return err; return err;
} }
/* Send one chunk of data of a given size /* Send one chunk of data of a given size
*/ */
int httpd_send_chunk(int conn, const char *buf, int len) int httpd_send_chunk(int conn, const char *buf, int len)
{ {
int err; int err;
if (len) { if (len) {
/* Send chunk begin header */ /* Send chunk begin header */
err = httpd_send_chunk_begin(conn, len); err = httpd_send_chunk_begin(conn, len);
if (err != kNoErr) if (err != kNoErr)
return err; return err;
/* Send our data */ /* Send our data */
err = httpd_send(conn, buf, len); err = httpd_send(conn, buf, len);
if (err != kNoErr) if (err != kNoErr)
return err; return err;
/* Send chunk end indicator */ /* Send chunk end indicator */
err = httpd_send_crlf(conn); err = httpd_send_crlf(conn);
if (err != kNoErr) if (err != kNoErr)
return err; return err;
} else { } else {
/* Length is 0, last chunk */ /* Length is 0, last chunk */
err = httpd_send(conn, http_last_chunk, err = httpd_send(conn, http_last_chunk,
sizeof(http_last_chunk) - 1); sizeof(http_last_chunk) - 1);
if (err != kNoErr) if (err != kNoErr)
return err; return err;
} }
return err; return err;
} }
/*Helper function to send a buffer over a connection. /*Helper function to send a buffer over a connection.
*/ */
int httpd_send(int conn, const char *buf, int len) int httpd_send(int conn, const char *buf, int len)
{ {
int num; int num;
do { struct timeval t;
fd_set writefds;
t.tv_sec = 0;
t.tv_usec = 200*1000;
do {
#ifdef CONFIG_ENABLE_HTTPS #ifdef CONFIG_ENABLE_HTTPS
if (httpd_is_https_active()) if (httpd_is_https_active())
num = tls_send(httpd_tls_handle, buf, len); num = tls_send(httpd_tls_handle, buf, len);
else else
#endif /* ENABLE_HTTPS */ #endif /* ENABLE_HTTPS */
num = send(conn, buf, len, 0); FD_ZERO( &writefds );
if (num < 0) { FD_SET( conn, &writefds );
httpd_d("send() failed: %d" ,conn);
return -kInProgressErr; select( conn + 1, NULL, &writefds, NULL, &t );
} if( FD_ISSET( conn, &writefds) )
len -= num; {
buf += num; num = send(conn, buf, len, 0);
} while (len > 0); if (num < 0) {
httpd_d("send() failed: %d" ,conn);
return -kInProgressErr;
}
len -= num;
buf += num;
}
} while (len > 0);
return kNoErr; return kNoErr;
} }
int httpd_recv(int fd, void *buf, size_t n, int flags) int httpd_recv(int fd, void *buf, size_t n, int flags)
{ {
int len = 0;
struct timeval t;
fd_set readfds;
FD_ZERO( &readfds );
FD_SET( fd, &readfds );
t.tv_sec = 0;
t.tv_usec = 200*1000;
#ifdef CONFIG_ENABLE_HTTPS #ifdef CONFIG_ENABLE_HTTPS
if (httpd_is_https_active()) if (httpd_is_https_active())
return tls_recv(httpd_tls_handle, buf, n); return tls_recv(httpd_tls_handle, buf, n);
else else
#endif /* ENABLE_HTTPS */ #endif /* ENABLE_HTTPS */
return recv(fd, buf, n, flags); return recv(fd, buf, n, flags);
select( fd + 1, &readfds, NULL, NULL, &t );
if( FD_ISSET( fd, &readfds) )
{
len = recv( fd, buf, n, flags );
}
return len;
} }
int httpd_send_hdr_from_code(int sock, int stat_code, int httpd_send_hdr_from_code(int sock, int stat_code,
enum http_content_type content_type) enum http_content_type content_type)
{ {
const char *str; const char *str;
unsigned int str_len; unsigned int str_len;
/* /*
* Set the HTTP Response Status 200 OK, 404 NOT FOUND etc. * Set the HTTP Response Status 200 OK, 404 NOT FOUND etc.
* Also set the Transfer-Encoding to chunked. * Also set the Transfer-Encoding to chunked.
*/ */
switch (stat_code) { switch (stat_code) {
case -WM_E_HTTPD_HANDLER_404: case -WM_E_HTTPD_HANDLER_404:
str = http_header_404; str = http_header_404;
str_len = strlen(http_header_404); str_len = strlen(http_header_404);
break; break;
case -WM_E_HTTPD_HANDLER_400: case -WM_E_HTTPD_HANDLER_400:
str = http_header_400; str = http_header_400;
str_len = strlen(http_header_400); str_len = strlen(http_header_400);
break; break;
case -WM_E_HTTPD_HANDLER_500: case -WM_E_HTTPD_HANDLER_500:
str = http_header_500; str = http_header_500;
str_len = strlen(http_header_500); str_len = strlen(http_header_500);
break; break;
default: default:
/* The handler doesn't want an HTTP error code, but would return /* The handler doesn't want an HTTP error code, but would return
* 200 OK with an error in the response */ * 200 OK with an error in the response */
str = http_header_200; str = http_header_200;
str_len = strlen(http_header_200); str_len = strlen(http_header_200);
break; break;
} }
if (httpd_send(sock, str, str_len) != kNoErr) { if (httpd_send(sock, str, str_len) != kNoErr) {
return -kInProgressErr; return -kInProgressErr;
} }
/* Send the Content-Type */ /* Send the Content-Type */
switch (content_type) { switch (content_type) {
case HTTP_CONTENT_JSON: case HTTP_CONTENT_JSON:
str = http_content_type_json_nocache; str = http_content_type_json_nocache;
str_len = sizeof(http_content_type_json_nocache) - 1; str_len = sizeof(http_content_type_json_nocache) - 1;
break; break;
case HTTP_CONTENT_XML: case HTTP_CONTENT_XML:
str = http_content_type_xml_nocache; str = http_content_type_xml_nocache;
str_len = sizeof(http_content_type_xml_nocache) - 1; str_len = sizeof(http_content_type_xml_nocache) - 1;
break; break;
case HTTP_CONTENT_HTML: case HTTP_CONTENT_HTML:
str = http_content_type_html; str = http_content_type_html;
str_len = sizeof(http_content_type_html) - 1; str_len = sizeof(http_content_type_html) - 1;
break; break;
case HTTP_CONTENT_JPEG: case HTTP_CONTENT_JPEG:
str = http_content_type_jpg; str = http_content_type_jpg;
str_len = sizeof(http_content_type_jpg) - 1; str_len = sizeof(http_content_type_jpg) - 1;
break; break;
default: default:
str = http_content_type_plain; str = http_content_type_plain;
str_len = sizeof(http_content_type_plain) - 1; str_len = sizeof(http_content_type_plain) - 1;
break; break;
} }
if (httpd_send(sock, str, str_len) != kNoErr) { if (httpd_send(sock, str, str_len) != kNoErr) {
return -kInProgressErr; return -kInProgressErr;
} }
return kNoErr; return kNoErr;
} }
@@ -234,62 +264,62 @@ static char httpd_error[HTTPD_MAX_ERROR_STRING + 1];
void httpd_set_error(const char *fmt, ...) void httpd_set_error(const char *fmt, ...)
{ {
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
vsnprintf(httpd_error, HTTPD_MAX_ERROR_STRING + 1, fmt, argp); vsnprintf(httpd_error, HTTPD_MAX_ERROR_STRING + 1, fmt, argp);
va_end(argp); va_end(argp);
httpd_d("http set err"); httpd_d("http set err");
} }
/* Helper function to send an error. /* Helper function to send an error.
*/ */
int httpd_send_error(int conn, int http_error) int httpd_send_error(int conn, int http_error)
{ {
int err = 0; int err = 0;
switch (http_error) { switch (http_error) {
case HTTP_404: case HTTP_404:
err = httpd_send(conn, http_header_404, err = httpd_send(conn, http_header_404,
strlen(http_header_404)); strlen(http_header_404));
break; break;
case HTTP_500: case HTTP_500:
err = httpd_send(conn, http_header_500, err = httpd_send(conn, http_header_500,
strlen(http_header_500)); strlen(http_header_500));
break; break;
case HTTP_505: case HTTP_505:
err = httpd_send(conn, http_header_505, err = httpd_send(conn, http_header_505,
strlen(http_header_505)); strlen(http_header_505));
break; break;
} }
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
err = httpd_send(conn, http_header_type_chunked, err = httpd_send(conn, http_header_type_chunked,
strlen(http_header_type_chunked)); strlen(http_header_type_chunked));
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
err = httpd_send(conn, http_content_type_plain, err = httpd_send(conn, http_content_type_plain,
sizeof(http_content_type_plain) - 1); sizeof(http_content_type_plain) - 1);
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
err = httpd_send_crlf(conn); err = httpd_send_crlf(conn);
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
err = httpd_send_chunk(conn, httpd_error, strlen(httpd_error)); err = httpd_send_chunk(conn, httpd_error, strlen(httpd_error));
if (err != kNoErr) { if (err != kNoErr) {
return err; return err;
} }
return httpd_send_last_chunk(conn); return httpd_send_last_chunk(conn);
} }
@@ -299,28 +329,28 @@ int httpd_send_error(int conn, int http_error)
* data after that. * data after that.
*/ */
void httpd_purge_socket_data(httpd_request_t *req, char *msg_in, void httpd_purge_socket_data(httpd_request_t *req, char *msg_in,
int msg_in_len, int conn) int msg_in_len, int conn)
{ {
int status = httpd_parse_hdr_tags(req, conn, msg_in, msg_in_len); int status = httpd_parse_hdr_tags(req, conn, msg_in, msg_in_len);
if (status != kNoErr) { if (status != kNoErr) {
/* We were unsuccessful in purging the socket.*/ /* We were unsuccessful in purging the socket.*/
httpd_d("Unable to purge socket: %d", status); httpd_d("Unable to purge socket: %d", status);
return; return;
} }
unsigned data_remaining = req->body_nbytes; unsigned data_remaining = req->body_nbytes;
while (data_remaining) { while (data_remaining) {
unsigned to_read = msg_in_len >= data_remaining ? unsigned to_read = msg_in_len >= data_remaining ?
data_remaining : msg_in_len; data_remaining : msg_in_len;
int actually_read = httpd_recv(conn, msg_in, to_read, 0); int actually_read = httpd_recv(conn, msg_in, to_read, 0);
if (actually_read < 0) { if (actually_read < 0) {
httpd_d("Unable to read content." httpd_d("Unable to read content."
"Was purging socket data"); "Was purging socket data");
return; return;
} }
data_remaining -= actually_read; data_remaining -= actually_read;
} }
} }
/* Handle an incoming message (request) from the client. This is the /* Handle an incoming message (request) from the client. This is the
@@ -328,72 +358,72 @@ void httpd_purge_socket_data(httpd_request_t *req, char *msg_in,
*/ */
int httpd_handle_message(int conn) int httpd_handle_message(int conn)
{ {
int err; int err;
int req_line_len; int req_line_len;
char msg_in[128]; char msg_in[128];
/* clear out the httpd_req structure */ /* clear out the httpd_req structure */
memset(&httpd_req, 0x00, sizeof(httpd_req)); memset(&httpd_req, 0x00, sizeof(httpd_req));
httpd_req.sock = conn; httpd_req.sock = conn;
/* Read the first line of the HTTP header */ /* Read the first line of the HTTP header */
req_line_len = htsys_getln_soc(conn, msg_in, sizeof(msg_in)); req_line_len = htsys_getln_soc(conn, msg_in, sizeof(msg_in));
if (req_line_len == 0) if (req_line_len == 0)
return HTTPD_DONE; return HTTPD_DONE;
if (req_line_len < 0) { if (req_line_len < 0) {
httpd_d("Could not read from socket"); httpd_d("Could not read from socket");
return -kInProgressErr; return -kInProgressErr;
} }
/* Parse the first line of the header */ /* Parse the first line of the header */
err = httpd_parse_hdr_main(msg_in, &httpd_req); err = httpd_parse_hdr_main(msg_in, &httpd_req);
if (err == -WM_E_HTTPD_NOTSUPP) if (err == -WM_E_HTTPD_NOTSUPP)
/* Send 505 HTTP Version not supported */ /* Send 505 HTTP Version not supported */
return httpd_send_error(conn, HTTP_505); return httpd_send_error(conn, HTTP_505);
else if (err != kNoErr) { else if (err != kNoErr) {
/* Send 500 Internal Server Error */ /* Send 500 Internal Server Error */
return httpd_send_error(conn, HTTP_500); return httpd_send_error(conn, HTTP_500);
} }
/* set a generic error that can be overridden by the wsgi handling. */ /* set a generic error that can be overridden by the wsgi handling. */
httpd_d("Presetting"); httpd_d("Presetting");
/* Web Services Gateway Interface branch point: /* Web Services Gateway Interface branch point:
* At this point we have the request type (httpd_req.type) and the path * At this point we have the request type (httpd_req.type) and the path
* (httpd_req.filename) and all the headers waiting to be read from * (httpd_req.filename) and all the headers waiting to be read from
* the socket. * the socket.
* *
* The call bellow will iterate through all the url patterns and * The call bellow will iterate through all the url patterns and
* invoke the handlers that match the request type and pattern. If * invoke the handlers that match the request type and pattern. If
* request type and url patern match, invoke the handler. * request type and url patern match, invoke the handler.
*/ */
err = httpd_wsgi(&httpd_req); err = httpd_wsgi(&httpd_req);
if (err == HTTPD_DONE) { if (err == HTTPD_DONE) {
httpd_d("Done processing request."); httpd_d("Done processing request.");
return kNoErr; return kNoErr;
} else if (err == -WM_E_HTTPD_NO_HANDLER) { } else if (err == -WM_E_HTTPD_NO_HANDLER) {
httpd_d("No handler for the given URL %s was found", httpd_d("No handler for the given URL %s was found",
httpd_req.filename); httpd_req.filename);
/* /*
* We have not yet read the complete data from the current * We have not yet read the complete data from the current
* request, from the socket. We are in an error state and * request, from the socket. We are in an error state and
* we wish to cancel this HTTP transaction. We sent * we wish to cancel this HTTP transaction. We sent
* appropriate message to the client and read (flush) out * appropriate message to the client and read (flush) out
* all the pending data in the socket. We let the client * all the pending data in the socket. We let the client
* close the socket for us, if necessary. * close the socket for us, if necessary.
*/ */
httpd_purge_socket_data(&httpd_req, msg_in, httpd_purge_socket_data(&httpd_req, msg_in,
sizeof(msg_in), conn); sizeof(msg_in), conn);
httpd_set_error("File %s not_found", httpd_req.filename); httpd_set_error("File %s not_found", httpd_req.filename);
httpd_send_error(conn, HTTP_404); httpd_send_error(conn, HTTP_404);
return kNoErr; return kNoErr;
} else { } else {
httpd_d("WSGI handler failed."); httpd_d("WSGI handler failed.");
/* Send 500 Internal Server Error */ /* Send 500 Internal Server Error */
return httpd_send_error(conn, HTTP_500); return httpd_send_error(conn, HTTP_500);
} }
} }

View File

@@ -56,21 +56,21 @@ static OSStatus onReceivedData( struct _HTTPHeader_t * httpHeader,
static void hex2str(uint8_t *hex, int hex_len, char *str) static void hex2str(uint8_t *hex, int hex_len, char *str)
{ {
int i = 0; int i = 0;
for(i=0; i<hex_len; i++){ for(i=0; i<hex_len; i++){
sprintf(str+i*2, "%02x", hex[i]); sprintf(str+i*2, "%02x", hex[i]);
} }
} }
static void upper2lower(char *str, int len) static void upper2lower(char *str, int len)
{ {
int i = 0; int i = 0;
for(i=0; i<len; i++) for(i=0; i<len; i++)
{ {
if( (*(str+i) >= 'A') && (*(str+i) <= 'Z') ){ if( (*(str+i) >= 'A') && (*(str+i) <= 'Z') ){
*(str+i) += 32; *(str+i) += 32;
}
} }
}
} }
static int ota_server_send( uint8_t *data, int datalen ) static int ota_server_send( uint8_t *data, int datalen )
@@ -104,7 +104,7 @@ static OSStatus ota_server_connect( struct sockaddr_in *addr, socklen_t addrlen
} }
#endif #endif
exit: exit:
return err; return err;
} }
@@ -244,7 +244,7 @@ static void ota_server_thread( mico_thread_arg_t arg )
struct in_addr in_addr; struct in_addr in_addr;
mico_logic_partition_t* ota_partition = MicoFlashGetInfo( MICO_PARTITION_OTA_TEMP ); mico_logic_partition_t* ota_partition = MicoFlashGetInfo( MICO_PARTITION_OTA_TEMP );
ota_server_context->ota_control = OTA_CONTROL_START; ota_server_context->ota_control = OTA_CONTROL_START;
hostent_content = gethostbyname( ota_server_context->download_url.host ); hostent_content = gethostbyname( ota_server_context->download_url.host );
@@ -256,12 +256,12 @@ static void ota_server_thread( mico_thread_arg_t arg )
offset = 0; offset = 0;
MicoFlashErase( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length ); MicoFlashErase( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length );
CRC16_Init( &crc_context ); CRC16_Init( &crc_context );
if( ota_server_context->ota_check.is_md5 == true ){ if( ota_server_context->ota_check.is_md5 == true ){
InitMd5( &md5 ); InitMd5( &md5 );
} }
httpHeader = HTTPHeaderCreateWithCallback( 1024, onReceivedData, NULL, NULL ); httpHeader = HTTPHeaderCreateWithCallback( 1024, onReceivedData, NULL, NULL );
require_action( httpHeader, DELETE, ota_server_progress_set(OTA_FAIL) ); require_action( httpHeader, DELETE, ota_server_progress_set(OTA_FAIL) );
@@ -334,13 +334,13 @@ static void ota_server_thread( mico_thread_arg_t arg )
goto DELETE; goto DELETE;
} }
RECONNECTED: RECONNECTED:
ota_server_socket_close( ); ota_server_socket_close( );
mico_thread_sleep(2); mico_thread_sleep(2);
continue; continue;
} }
DELETE: DELETE:
HTTPHeaderDestory( &httpHeader ); HTTPHeaderDestory( &httpHeader );
ota_server_socket_close( ); ota_server_socket_close( );
if( ota_server_context != NULL ){ if( ota_server_context != NULL ){
@@ -421,7 +421,7 @@ static OSStatus ota_server_set_url( char *url )
strcpy( ota_server_context->download_url.url, pos ); strcpy( ota_server_context->download_url.url, pos );
} }
exit: exit:
url_free( url_t ); url_free( url_t );
return err; return err;
} }
@@ -461,7 +461,7 @@ OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
ota_server_context->ota_server_cb = call_back; ota_server_context->ota_server_cb = call_back;
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 ); err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 );
exit: exit:
return err; return err;
} }
@@ -483,4 +483,4 @@ void ota_server_stop( void )
OTA_CONTROL_E ota_server_get( void ) OTA_CONTROL_E ota_server_get( void )
{ {
return ota_server_context->ota_control; return ota_server_context->ota_control;
} }

View File

@@ -49,8 +49,8 @@ typedef enum _OTA_STATE_E{
}OTA_STATE_E; }OTA_STATE_E;
typedef enum _HTTP_SECURITY_E{ typedef enum _HTTP_SECURITY_E{
HTTP_SECURITY_HTTP, HTTP_SECURITY_HTTP,
HTTP_SECURITY_HTTPS HTTP_SECURITY_HTTPS
} HTTP_SECURITY_E; } HTTP_SECURITY_E;
typedef enum _OTA_CONTROL_E{ typedef enum _OTA_CONTROL_E{
@@ -87,11 +87,11 @@ typedef struct _ota_check_t{
typedef void (*ota_server_cb_fn) (OTA_STATE_E state, float progress); typedef void (*ota_server_cb_fn) (OTA_STATE_E state, float progress);
typedef struct _ota_server_context_t{ typedef struct _ota_server_context_t{
download_url_t download_url; download_url_t download_url;
download_state_t download_state; download_state_t download_state;
ota_check_t ota_check; ota_check_t ota_check;
OTA_CONTROL_E ota_control; OTA_CONTROL_E ota_control;
ota_server_cb_fn ota_server_cb; ota_server_cb_fn ota_server_cb;
} ota_server_context_t; } ota_server_context_t;
@@ -139,4 +139,4 @@ void ota_server_continue( void );
*/ */
void ota_server_stop( void ); void ota_server_stop( void );
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -40,33 +40,35 @@
#include "URLUtils.h" #include "URLUtils.h"
#include "stdbool.h" #include "stdbool.h"
#define Boolean bool
#define kHTTPPostMethod "POST" #define kHTTPPostMethod "POST"
// Status-Code = // Status-Code =
// "100" ; Section 10.1.1: Continue // "100" ; Section 10.1.1: Continue
// | "101" ; Section 10.1.2: Switching Protocols // | "101" ; Section 10.1.2: Switching Protocols
// | "200" ; Section 10.2.1: OK // | "200" ; Section 10.2.1: OK
// | "201" ; Section 10.2.2: Created // | "201" ; Section 10.2.2: Created
// | "202" ; Section 10.2.3: Accepted // | "202" ; Section 10.2.3: Accepted
// | "203" ; Section 10.2.4: Non-Authoritative Information // | "203" ; Section 10.2.4: Non-Authoritative Information
// | "204" ; Section 10.2.5: No Content // | "204" ; Section 10.2.5: No Content
// | "205" ; Section 10.2.6: Reset Content // | "205" ; Section 10.2.6: Reset Content
// | "206" ; Section 10.2.7: Partial Content // | "206" ; Section 10.2.7: Partial Content
// | "207" ; ??? : Multi-Status // | "207" ; ??? : Multi-Status
// | "300" ; Section 10.3.1: Multiple Choices // | "300" ; Section 10.3.1: Multiple Choices
// | "301" ; Section 10.3.2: Moved Permanently // | "301" ; Section 10.3.2: Moved Permanently
// | "302" ; Section 10.3.3: Found // | "302" ; Section 10.3.3: Found
// | "303" ; Section 10.3.4: See Other // | "303" ; Section 10.3.4: See Other
// | "304" ; Section 10.3.5: Not Modified // | "304" ; Section 10.3.5: Not Modified
// | "305" ; Section 10.3.6: Use Proxy // | "305" ; Section 10.3.6: Use Proxy
// | "307" ; Section 10.3.8: Temporary Redirect // | "307" ; Section 10.3.8: Temporary Redirect
// | "400" ; Section 10.4.1: Bad Request // | "400" ; Section 10.4.1: Bad Request
// | "401" ; Section 10.4.2: Unauthorized // | "401" ; Section 10.4.2: Unauthorized
// | "402" ; Section 10.4.3: Payment Required // | "402" ; Section 10.4.3: Payment Required
// | "403" ; Section 10.4.4: Forbidden // | "403" ; Section 10.4.4: Forbidden
// | "404" ; Section 10.4.5: Not Found // | "404" ; Section 10.4.5: Not Found
// | "405" ; Section 10.4.6: Method Not Allowed // | "405" ; Section 10.4.6: Method Not Allowed
// | "406" ; Section 10.4.7: Not Acceptable // | "406" ; Section 10.4.7: Not Acceptable
#define kStatusAccept 202 #define kStatusAccept 202
@@ -76,9 +78,9 @@
#define kStatusBadRequest 400 #define kStatusBadRequest 400
#define kStatusNotFound 404 #define kStatusNotFound 404
#define kStatusMethodNotAllowed 405 #define kStatusMethodNotAllowed 405
#define kStatusForbidden 403 #define kStatusForbidden 403
#define kStatusAuthenticationErr 470 #define kStatusAuthenticationErr 470
#define kStatusInternalServerErr 500 #define kStatusInternalServerErr 500
#define kMIMEType_Binary "application/octet-stream" #define kMIMEType_Binary "application/octet-stream"
#define kMIMEType_DMAP "application/x-dmap-tagged" #define kMIMEType_DMAP "application/x-dmap-tagged"
@@ -130,7 +132,7 @@ typedef struct _HTTPHeader_t
void * userContext; void * userContext;
bool isCallbackSupported; bool isCallbackSupported;
OSStatus (*onReceivedDataCallback) ( struct _HTTPHeader_t * , uint32_t, uint8_t *, size_t, void * ); OSStatus (*onReceivedDataCallback) ( struct _HTTPHeader_t * , uint32_t, uint8_t *, size_t, void * );
void (*onClearCallback) ( struct _HTTPHeader_t * httpHeader, void * userContext ); void (*onClearCallback) ( struct _HTTPHeader_t * httpHeader, void * userContext );
@@ -168,14 +170,14 @@ int HTTPHeaderMatchURL( HTTPHeader_t *inHeader, const char *url );
char* HTTPHeaderMatchPartialURL( HTTPHeader_t *inHeader, const char *url ); char* HTTPHeaderMatchPartialURL( HTTPHeader_t *inHeader, const char *url );
int HTTPGetHeaderField( const char *inHeaderPtr, int HTTPGetHeaderField( const char *inHeaderPtr,
size_t inHeaderLen, size_t inHeaderLen,
const char *inName, const char *inName,
const char **outNamePtr, const char **outNamePtr,
size_t *outNameLen, size_t *outNameLen,
const char **outValuePtr, const char **outValuePtr,
size_t *outValueLen, size_t *outValueLen,
const char **outNext ); const char **outNext );
HTTPHeader_t * HTTPHeaderCreate( size_t bufLen ); HTTPHeader_t * HTTPHeaderCreate( size_t bufLen );
@@ -196,10 +198,9 @@ OSStatus CreateHTTPRespondMessageNoCopy( int status, const char *contentType, si
OSStatus CreateHTTPMessage( const char *methold, const char *url, const char *contentType, uint8_t *inData, size_t inDataLen, uint8_t **outMessage, size_t *outMessageSize ); OSStatus CreateHTTPMessage( const char *methold, const char *url, const char *contentType, uint8_t *inData, size_t inDataLen, uint8_t **outMessage, size_t *outMessageSize );
OSStatus CreateHTTPMessageWithHost( const char *methold, const char *url, OSStatus CreateHTTPMessageWithHost( const char *methold, const char *url,
const char* host, uint16_t port, const char* host, uint16_t port,
const char *contentType, const char *contentType,
uint8_t *inData, size_t inDataLen, uint8_t *inData, size_t inDataLen,
uint8_t **outMessage, size_t *outMessageSize ); uint8_t **outMessage, size_t *outMessageSize );
#endif // __HTTPUtils_h__ #endif // __HTTPUtils_h__