Files
kunlun/app/iot_cus_at_app/proto/app_proto.c

103 lines
3.0 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "app_proto.h"
#include "app_cus_task.h"
#include "app_proto_proc.h"
#include "app_proto_proc_cco.h"
uint8_t check_app_proto_frame(uint8_t * data, uint16_t data_length)
{
app_proto_frame_head *frame = (app_proto_frame_head *)data;
uint16_t frame_length;
uint16_t calc_crc;
uint8_t reason = 0;
uint8_t ret = true;
if ((data == NULL) || (data_length == 0)) {
reason = 1;
goto out;
}
frame_length = sizeof(app_proto_frame_head) + frame->length + PROTO_CRC_SIZE;
if (frame_length != data_length) {
reason = 2;
goto out;
}
if (frame->head != APP_FRAME_HEAD_BYTE) {
reason = 3;
goto out;
}
calc_crc = iot_getcrc16(data, frame_length, IOT_CRC16_TYPE_XMODEM);
if (calc_crc != 0) {
reason = 4;
goto out;
}
ret = false;
out:
if (ret) {
APP_PRINTF("[ERR] Check Frame Faild with reason[%d]", reason);
}
return ret;
}
static void app_proto_data_parse(uint8_t *buffer, uint32_t buffer_len, app_source_e source)
{
app_proto_frame_rx(buffer, buffer_len, source, NULL);
}
/*
* Process data from PLC
*/
static void app_proto_data_handle(uint16_t data_id, uint16_t data_src,
uint8_t * data, uint32_t data_len, void *info)
{
recv_info_t *recv_info = (recv_info_t*)info;
if (data_id == ID_PLC_DATA_TRANSMIT) {
/* send to uart */
app_proto_data_transmit(data, data_len, recv_info->mac);
} else if (data_id == ID_PLC_REMOTE_CMD) {
#if PLC_SUPPORT_STA_ROLE
app_proto_frame_rx(data, data_len, data_src, recv_info->mac);
#else
/* send to uart */
app_proto_remote_cmd(data, data_len, recv_info->mac);
#endif
} else if (data_id == ID_PLC_SYSCTRL_DATA) {
app_proto_sysctrl_data_process(data, data_len, recv_info->mac, app_get_mac_addr());
} else if (data_id == ID_PLC_DATA_TEST) {
/* TODO */
} else {
APP_PRINTF("mode not support data");
}
return;
}
void app_proto_init()
{
if (app_work_mode_register(APP_WORK_MODE_PROTO, app_proto_data_parse,
app_proto_data_handle, app_proto_onoffline_report)) {
}
return;
}