|
@@ -1,13 +1,17 @@
|
|
|
#include "luat_base.h"
|
|
#include "luat_base.h"
|
|
|
#include "stdlib.h"
|
|
#include "stdlib.h"
|
|
|
#include "luat_ymodem.h"
|
|
#include "luat_ymodem.h"
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
#include "luat_fs.h"
|
|
#include "luat_fs.h"
|
|
|
#include "luat_malloc.h"
|
|
#include "luat_malloc.h"
|
|
|
#include "luat_timer.h"
|
|
#include "luat_timer.h"
|
|
|
-
|
|
|
|
|
|
|
+#else
|
|
|
|
|
+#include "luat_malloc.h"
|
|
|
|
|
+#endif
|
|
|
#define LUAT_LOG_TAG "ymodem"
|
|
#define LUAT_LOG_TAG "ymodem"
|
|
|
#include "luat_log.h"
|
|
#include "luat_log.h"
|
|
|
|
|
|
|
|
|
|
+
|
|
|
#define XMODEM_FLAG 'C'
|
|
#define XMODEM_FLAG 'C'
|
|
|
#define XMODEM_SOH 0x01
|
|
#define XMODEM_SOH 0x01
|
|
|
#define XMODEM_STX 0x02
|
|
#define XMODEM_STX 0x02
|
|
@@ -21,9 +25,13 @@
|
|
|
|
|
|
|
|
typedef struct
|
|
typedef struct
|
|
|
{
|
|
{
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
char *save_path;
|
|
char *save_path;
|
|
|
const char *force_save_path;
|
|
const char *force_save_path;
|
|
|
FILE* fd;
|
|
FILE* fd;
|
|
|
|
|
+#else
|
|
|
|
|
+ luat_ymodem_callback cb;
|
|
|
|
|
+#endif
|
|
|
uint32_t file_size;
|
|
uint32_t file_size;
|
|
|
uint32_t write_size;
|
|
uint32_t write_size;
|
|
|
uint16_t data_pos;
|
|
uint16_t data_pos;
|
|
@@ -61,13 +69,17 @@ static uint16_t CRC16_Cal(void *Data, uint16_t Len, uint16_t CRC16Last)
|
|
|
}
|
|
}
|
|
|
return CRC16;
|
|
return CRC16;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
void *luat_ymodem_create_handler(const char *save_path, const char *force_save_path)
|
|
void *luat_ymodem_create_handler(const char *save_path, const char *force_save_path)
|
|
|
|
|
+#else
|
|
|
|
|
+void *luat_ymodem_create_handler(luat_ymodem_callback cb)
|
|
|
|
|
+#endif
|
|
|
{
|
|
{
|
|
|
ymodem_ctrlstruct *handler = luat_heap_malloc(sizeof(ymodem_ctrlstruct));
|
|
ymodem_ctrlstruct *handler = luat_heap_malloc(sizeof(ymodem_ctrlstruct));
|
|
|
if (handler)
|
|
if (handler)
|
|
|
{
|
|
{
|
|
|
memset(handler, 0, sizeof(ymodem_ctrlstruct));
|
|
memset(handler, 0, sizeof(ymodem_ctrlstruct));
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
if (save_path)
|
|
if (save_path)
|
|
|
{
|
|
{
|
|
|
if (save_path[strlen(save_path)-1] == '/'){
|
|
if (save_path[strlen(save_path)-1] == '/'){
|
|
@@ -86,7 +98,9 @@ void *luat_ymodem_create_handler(const char *save_path, const char *force_save_p
|
|
|
handler->force_save_path = luat_heap_malloc(strlen(force_save_path) + 1);
|
|
handler->force_save_path = luat_heap_malloc(strlen(force_save_path) + 1);
|
|
|
strcpy((char*)handler->force_save_path, force_save_path);
|
|
strcpy((char*)handler->force_save_path, force_save_path);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+#else
|
|
|
|
|
+ handler->cb = cb;
|
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
return handler;
|
|
return handler;
|
|
@@ -100,22 +114,13 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
char path[128];
|
|
char path[128];
|
|
|
*file_ok = 0;
|
|
*file_ok = 0;
|
|
|
*all_done = 0;
|
|
*all_done = 0;
|
|
|
|
|
+ *ack = 0;
|
|
|
*flag = 0;
|
|
*flag = 0;
|
|
|
- if (data)
|
|
|
|
|
- {
|
|
|
|
|
- if (data[0] == XMODEM_CAN)
|
|
|
|
|
- {
|
|
|
|
|
- luat_ymodem_reset(handler);
|
|
|
|
|
- *ack = XMODEM_ACK;
|
|
|
|
|
- *all_done = 1;
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
switch (ctrl->state)
|
|
switch (ctrl->state)
|
|
|
{
|
|
{
|
|
|
case 0:
|
|
case 0:
|
|
|
- if (!data)
|
|
|
|
|
|
|
+ if (!data || !len)
|
|
|
{
|
|
{
|
|
|
*ack = XMODEM_FLAG;
|
|
*ack = XMODEM_FLAG;
|
|
|
return 0;
|
|
return 0;
|
|
@@ -136,6 +141,11 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
case XMODEM_SOH:
|
|
case XMODEM_SOH:
|
|
|
ctrl->data_max = XMODEM_SOH_DATA_LEN;
|
|
ctrl->data_max = XMODEM_SOH_DATA_LEN;
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case XMODEM_CAN:
|
|
|
|
|
+ luat_ymodem_reset(handler);
|
|
|
|
|
+ *ack = XMODEM_ACK;
|
|
|
|
|
+ *all_done = 1;
|
|
|
|
|
+ return 0;
|
|
|
default:
|
|
default:
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
break;
|
|
break;
|
|
@@ -172,7 +182,7 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
*all_done = 1;
|
|
*all_done = 1;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
- NameEnd = NULL;
|
|
|
|
|
|
|
+ NameEnd = 0;
|
|
|
//for(i = XMODEM_DATA_POS; i < (XMODEM_SOH_DATA_LEN + 5); i++)
|
|
//for(i = XMODEM_DATA_POS; i < (XMODEM_SOH_DATA_LEN + 5); i++)
|
|
|
for(i = XMODEM_DATA_POS; i < (ctrl->data_max + 5); i++)
|
|
for(i = XMODEM_DATA_POS; i < (ctrl->data_max + 5); i++)
|
|
|
{
|
|
{
|
|
@@ -187,7 +197,7 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
LLOGD("name end");
|
|
LLOGD("name end");
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
}
|
|
}
|
|
|
- LenEnd = NULL;
|
|
|
|
|
|
|
+ LenEnd = 0;
|
|
|
//for(i = (NameEnd + 1); i < (XMODEM_SOH_DATA_LEN + 5); i++)
|
|
//for(i = (NameEnd + 1); i < (XMODEM_SOH_DATA_LEN + 5); i++)
|
|
|
for(i = (NameEnd + 1); i < (ctrl->data_max + 5); i++)
|
|
for(i = (NameEnd + 1); i < (ctrl->data_max + 5); i++)
|
|
|
{
|
|
{
|
|
@@ -205,6 +215,7 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
|
|
|
|
|
ctrl->file_size = strtol((const char*)&ctrl->packet_data[NameEnd + 1], NULL, 10);
|
|
ctrl->file_size = strtol((const char*)&ctrl->packet_data[NameEnd + 1], NULL, 10);
|
|
|
ctrl->write_size = 0;
|
|
ctrl->write_size = 0;
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
if (ctrl->force_save_path)
|
|
if (ctrl->force_save_path)
|
|
|
{
|
|
{
|
|
|
ctrl->fd = luat_fs_fopen(ctrl->force_save_path, "w");
|
|
ctrl->fd = luat_fs_fopen(ctrl->force_save_path, "w");
|
|
@@ -216,7 +227,10 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
ctrl->fd = luat_fs_fopen(path, "w");
|
|
ctrl->fd = luat_fs_fopen(path, "w");
|
|
|
LLOGD("%s,%u,%x", path, ctrl->file_size, ctrl->fd);
|
|
LLOGD("%s,%u,%x", path, ctrl->file_size, ctrl->fd);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+#else
|
|
|
|
|
+ ctrl->cb(&ctrl->packet_data[XMODEM_DATA_POS], 0);
|
|
|
|
|
+ ctrl->cb(NULL, ctrl->file_size);
|
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
ctrl->state++;
|
|
ctrl->state++;
|
|
@@ -234,6 +248,10 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
case 1:
|
|
case 1:
|
|
|
|
|
+ if (!data || !len)
|
|
|
|
|
+ {
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
if (!ctrl->data_pos)
|
|
if (!ctrl->data_pos)
|
|
|
{
|
|
{
|
|
|
switch(data[0])
|
|
switch(data[0])
|
|
@@ -244,7 +262,13 @@ int luat_ymodem_receive(void *handler, uint8_t *data, uint32_t len, uint8_t *ack
|
|
|
case XMODEM_SOH:
|
|
case XMODEM_SOH:
|
|
|
ctrl->data_max = (XMODEM_SOH_DATA_LEN + 5);
|
|
ctrl->data_max = (XMODEM_SOH_DATA_LEN + 5);
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case XMODEM_CAN:
|
|
|
|
|
+ luat_ymodem_reset(handler);
|
|
|
|
|
+ *ack = XMODEM_ACK;
|
|
|
|
|
+ *all_done = 1;
|
|
|
|
|
+ return 0;
|
|
|
default:
|
|
default:
|
|
|
|
|
+ LLOGD("%x", data[0]);
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -263,7 +287,7 @@ YMODEM_DATA_CHECK:
|
|
|
case XMODEM_SOH:
|
|
case XMODEM_SOH:
|
|
|
if (ctrl->packet_data[1] != ctrl->next_sn || ctrl->packet_data[2] != (255 - ctrl->next_sn))
|
|
if (ctrl->packet_data[1] != ctrl->next_sn || ctrl->packet_data[2] != (255 - ctrl->next_sn))
|
|
|
{
|
|
{
|
|
|
- LLOGD("head %x %x %x", ctrl->packet_data[0], ctrl->packet_data[1], ctrl->packet_data[2]);
|
|
|
|
|
|
|
+ LLOGD("head %x %x %x,%d", ctrl->packet_data[0], ctrl->packet_data[1], ctrl->packet_data[2],ctrl->next_sn);
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -276,7 +300,11 @@ YMODEM_DATA_CHECK:
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
}
|
|
}
|
|
|
LenEnd = ((ctrl->file_size - ctrl->write_size) > XMODEM_SOH_DATA_LEN)?XMODEM_SOH_DATA_LEN:(ctrl->file_size - ctrl->write_size);
|
|
LenEnd = ((ctrl->file_size - ctrl->write_size) > XMODEM_SOH_DATA_LEN)?XMODEM_SOH_DATA_LEN:(ctrl->file_size - ctrl->write_size);
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
luat_fs_fwrite(ctrl->packet_data+3, LenEnd, 1, ctrl->fd);
|
|
luat_fs_fwrite(ctrl->packet_data+3, LenEnd, 1, ctrl->fd);
|
|
|
|
|
+#else
|
|
|
|
|
+ ctrl->cb(ctrl->packet_data+3, LenEnd);
|
|
|
|
|
+#endif
|
|
|
ctrl->write_size += LenEnd;
|
|
ctrl->write_size += LenEnd;
|
|
|
goto DATA_RECIEVE_OK;
|
|
goto DATA_RECIEVE_OK;
|
|
|
break;
|
|
break;
|
|
@@ -297,7 +325,11 @@ YMODEM_DATA_CHECK:
|
|
|
}
|
|
}
|
|
|
//写入
|
|
//写入
|
|
|
LenEnd = ((ctrl->file_size - ctrl->write_size) > XMODEM_STX_DATA_LEN)?XMODEM_STX_DATA_LEN:(ctrl->file_size - ctrl->write_size);
|
|
LenEnd = ((ctrl->file_size - ctrl->write_size) > XMODEM_STX_DATA_LEN)?XMODEM_STX_DATA_LEN:(ctrl->file_size - ctrl->write_size);
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
luat_fs_fwrite(ctrl->packet_data+3, LenEnd, 1, ctrl->fd);
|
|
luat_fs_fwrite(ctrl->packet_data+3, LenEnd, 1, ctrl->fd);
|
|
|
|
|
+#else
|
|
|
|
|
+ ctrl->cb(ctrl->packet_data+3, LenEnd);
|
|
|
|
|
+#endif
|
|
|
ctrl->write_size += LenEnd;
|
|
ctrl->write_size += LenEnd;
|
|
|
goto DATA_RECIEVE_OK;
|
|
goto DATA_RECIEVE_OK;
|
|
|
break;
|
|
break;
|
|
@@ -318,17 +350,30 @@ YMODEM_DATA_CHECK:
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
case 2:
|
|
case 2:
|
|
|
- if (data[0] == XMODEM_EOT)
|
|
|
|
|
|
|
+ if (!data || !len)
|
|
|
{
|
|
{
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ switch(data[0])
|
|
|
|
|
+ {
|
|
|
|
|
+ case XMODEM_EOT:
|
|
|
ctrl->state++;
|
|
ctrl->state++;
|
|
|
ctrl->data_pos = 0;
|
|
ctrl->data_pos = 0;
|
|
|
*flag = 0;
|
|
*flag = 0;
|
|
|
*ack = XMODEM_NAK;
|
|
*ack = XMODEM_NAK;
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
if (ctrl->fd) luat_fs_fclose(ctrl->fd);
|
|
if (ctrl->fd) luat_fs_fclose(ctrl->fd);
|
|
|
ctrl->fd = NULL;
|
|
ctrl->fd = NULL;
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+#else
|
|
|
|
|
+ ctrl->cb(NULL, 0);
|
|
|
|
|
+#endif
|
|
|
|
|
+ break;
|
|
|
|
|
+ case XMODEM_CAN:
|
|
|
|
|
+ luat_ymodem_reset(handler);
|
|
|
|
|
+ *ack = XMODEM_ACK;
|
|
|
|
|
+ *all_done = 1;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ default:
|
|
|
goto DATA_RECIEVE_ERROR;
|
|
goto DATA_RECIEVE_ERROR;
|
|
|
}
|
|
}
|
|
|
return 0;
|
|
return 0;
|
|
@@ -362,8 +407,12 @@ DATA_RECIEVE_OK:
|
|
|
*ack = XMODEM_ACK;
|
|
*ack = XMODEM_ACK;
|
|
|
if (ctrl->file_size && (ctrl->write_size >= ctrl->file_size))
|
|
if (ctrl->file_size && (ctrl->write_size >= ctrl->file_size))
|
|
|
{
|
|
{
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
luat_fs_fclose(ctrl->fd);
|
|
luat_fs_fclose(ctrl->fd);
|
|
|
ctrl->fd = NULL;
|
|
ctrl->fd = NULL;
|
|
|
|
|
+#else
|
|
|
|
|
+
|
|
|
|
|
+#endif
|
|
|
ctrl->state = 2;
|
|
ctrl->state = 2;
|
|
|
*file_ok = 1;
|
|
*file_ok = 1;
|
|
|
}
|
|
}
|
|
@@ -376,14 +425,20 @@ void luat_ymodem_reset(void *handler)
|
|
|
ctrl->state = 0;
|
|
ctrl->state = 0;
|
|
|
ctrl->next_sn = 0;
|
|
ctrl->next_sn = 0;
|
|
|
ctrl->file_size = 0;
|
|
ctrl->file_size = 0;
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
if (ctrl->fd) luat_fs_fclose(ctrl->fd);
|
|
if (ctrl->fd) luat_fs_fclose(ctrl->fd);
|
|
|
ctrl->fd = NULL;
|
|
ctrl->fd = NULL;
|
|
|
|
|
+#else
|
|
|
|
|
+ ctrl->cb(NULL, 0);
|
|
|
|
|
+#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void luat_ymodem_release(void *handler)
|
|
void luat_ymodem_release(void *handler)
|
|
|
{
|
|
{
|
|
|
ymodem_ctrlstruct *ctrl = handler;
|
|
ymodem_ctrlstruct *ctrl = handler;
|
|
|
luat_ymodem_reset(handler);
|
|
luat_ymodem_reset(handler);
|
|
|
|
|
+#ifdef __LUATOS__
|
|
|
luat_heap_free(ctrl->save_path);
|
|
luat_heap_free(ctrl->save_path);
|
|
|
|
|
+#endif
|
|
|
luat_heap_free(handler);
|
|
luat_heap_free(handler);
|
|
|
}
|
|
}
|