Files
kunlun/app/smart_grid/protocol/proto_188.c
2024-09-28 14:24:04 +08:00

93 lines
2.6 KiB
C

/****************************************************************************
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.
****************************************************************************/
/* os shim includes */
#include "os_types_api.h"
#include "os_mem_api.h"
/* common includes */
#include "iot_utils_api.h"
#include "iot_errno_api.h"
/* smart grid internal includes */
#include "proto_188.h"
static const uint8_t proto_188_bcast_addr[PROTO_188_ADDR_LEN] =
{ 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
uint8_t proto_188_calc_cs(proto_188_header_t *hdr)
{
uint8_t i, len, ret = 0;
uint8_t *data = (uint8_t *)hdr;
len = sizeof(*hdr) + hdr->len;
for (i = 0; i < len; i++) {
ret += data[i];
}
return ret;
}
proto_188_header_t *proto_188_format_check(uint8_t *data,
uint32_t len, uint32_t dir)
{
uint32_t len_t, d_len, i = 0x0;
uint8_t *data_s = NULL;
proto_188_header_t *head;
proto_188_tailer_t *tail;
for (i = 0x0, len_t = len; i < len; i++) {
if (data[i] == PROTO_188_START_CHAR) {
data_s = data + i;
break;
}
len_t--;
}
if (data_s == NULL
|| len_t < sizeof(proto_188_header_t)) {
goto drop;
}
head = (proto_188_header_t *)data_s;
if (head->start_char != PROTO_188_START_CHAR) {
goto drop;
}
d_len = head->len;
if (len_t < (sizeof(proto_188_header_t) + d_len +
sizeof(proto_188_tailer_t))) {
goto drop;
}
tail = (proto_188_tailer_t*)(data_s + sizeof(*head) + d_len);
if (proto_188_calc_cs(head) != tail->cs) {
goto drop;
}
if (tail->end_char != PROTO_188_END_CHAR)
goto drop;
if (head->control.dir != dir) {
goto drop;
}
goto out;
drop:
data_s = NULL;
out:
return (proto_188_header_t *)data_s;
}
uint8_t proto_188_is_bcast(uint8_t* dst)
{
return os_mem_cmp(dst, proto_188_bcast_addr, PROTO_188_ADDR_LEN) ==
0 ? 1 : 0;
}