Skip to content

Commit

Permalink
perf, donot need malloc commpacket
Browse files Browse the repository at this point in the history
  • Loading branch information
junlon2006 committed Apr 23, 2020
1 parent f10697e commit 00cb5c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion inc/uni_communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 9 additions & 18 deletions src/uni_communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 00cb5c7

Please sign in to comment.