diff --git a/inc/uni_communication.h b/inc/uni_communication.h index c584f3d..5d90d62 100644 --- a/inc/uni_communication.h +++ b/inc/uni_communication.h @@ -41,7 +41,7 @@ typedef int (*CommWriteHandler)(char *buf, int len); typedef struct { CommCmd cmd; /* air condition ctrl cmd such as power_on, power_off */ CommPayloadLen payload_len; /* parameter length of command */ - char payload[0]; /* parameter of command */ + char* payload; /* parameter of command */ } PACKED CommPacket; typedef struct { diff --git a/src/uni_communication.c b/src/uni_communication.c index 7f74e7b..a48942b 100644 --- a/src/uni_communication.c +++ b/src/uni_communication.c @@ -414,25 +414,18 @@ int CommProtocolPacketAssembleAndSend(CommCmd cmd, char *payload, return ret; } -static CommPacket* _packet_disassemble(CommProtocolPacket *protocol_packet) { - CommPacket *packet; +static int _packet_disassemble(CommProtocolPacket *protocol_packet, + CommPacket *packet) { if (!_checksum_valid(protocol_packet)) { LOGD(UART_COMM_TAG, "checksum failed"); - return NULL; + return -1; } - packet = (CommPacket *)uni_malloc(sizeof(CommPacket) + - _payload_len_get(protocol_packet)); - if (NULL == packet) { - LOGE(UART_COMM_TAG, "alloc memory failed"); - return NULL; - } - - packet->cmd = _byte2_big_endian_2_u16(protocol_packet->cmd); + packet->cmd = _byte2_big_endian_2_u16(protocol_packet->cmd); packet->payload_len = _payload_len_get(protocol_packet); - memcpy(packet->payload, _payload_get(protocol_packet), _payload_len_get(protocol_packet)); + packet->payload = _payload_get(protocol_packet); - return packet; + return 0; } static void _enlarge_protocol_buffer(char **orginal, CommPayloadLen *orginal_len) { @@ -544,8 +537,8 @@ static void _one_protocol_frame_process(char *protocol_buffer) { } /* disassemble protocol buffer */ - CommPacket* packet = _packet_disassemble(protocol_packet); - if (NULL == packet) { + CommPacket packet; + if (0 != _packet_disassemble(protocol_packet, &packet)) { _send_nack_frame(protocol_packet->sequence); LOGW(UART_COMM_TAG, "disassemble packet failed"); return; @@ -556,10 +549,8 @@ static void _one_protocol_frame_process(char *protocol_buffer) { /* notify application when not ack frame nor duplicate frame */ if (!_is_duplicate_frame(protocol_packet)) { - g_comm_protocol_business.on_recv_frame(packet); + g_comm_protocol_business.on_recv_frame(&packet); } - - uni_free(packet); } static long _get_clock_time_ms(void) {