diff --git a/platforms/posix/src/px4_daemon/client.cpp b/platforms/posix/src/px4_daemon/client.cpp index bbb3def133..c1db985887 100644 --- a/platforms/posix/src/px4_daemon/client.cpp +++ b/platforms/posix/src/px4_daemon/client.cpp @@ -165,7 +165,7 @@ Client::_send_cmds(const int argc, const char **argv) strcpy((char *)packet.payload.execute_msg.cmd, cmd_buf.c_str()); // The size is +1 because we want to include the null termination. - packet.header.payload_length = cmd_buf.size() + 1; + packet.header.payload_length = cmd_buf.size() + 1 + sizeof(packet.payload.execute_msg.is_atty); _client_send_pipe_fd = open(get_client_send_pipe_path(_instance_id).c_str(), O_WRONLY); @@ -214,7 +214,7 @@ Client::_listen() // Again, we only read as much as we need because otherwise we need // hold a buffer and parse it. - bytes_read = read(client_recv_pipe_fd, (char *)&packet_recv + bytes_read, payload_to_read); + bytes_read = read(client_recv_pipe_fd, ((uint8_t *)&packet_recv) + bytes_read, payload_to_read); if (bytes_read > 0) { @@ -293,7 +293,7 @@ Client::_stdout_msg_packet(const client_recv_packet_s &packet) return 0; } else { - PX4_ERR("payload size wrong"); + PX4_ERR("payload size wrong (%i > %i)", packet.header.payload_length, sizeof(packet.payload.stdout_msg.text)); return -1; } } diff --git a/platforms/posix/src/px4_daemon/pipe_protocol.cpp b/platforms/posix/src/px4_daemon/pipe_protocol.cpp index bbdc0e92a2..57bc95ebfb 100644 --- a/platforms/posix/src/px4_daemon/pipe_protocol.cpp +++ b/platforms/posix/src/px4_daemon/pipe_protocol.cpp @@ -53,7 +53,7 @@ namespace px4_daemon unsigned get_client_send_packet_length(const client_send_packet_s *packet) { - return sizeof(client_send_packet_s) - sizeof(packet->payload) + packet->header.payload_length + sizeof(uint8_t); + return sizeof(client_send_packet_s) - sizeof(packet->payload) + packet->header.payload_length; } unsigned get_client_recv_packet_length(const client_recv_packet_s *packet) diff --git a/platforms/posix/src/px4_daemon/pipe_protocol.h b/platforms/posix/src/px4_daemon/pipe_protocol.h index abc2b9b87a..0f8012cdfd 100644 --- a/platforms/posix/src/px4_daemon/pipe_protocol.h +++ b/platforms/posix/src/px4_daemon/pipe_protocol.h @@ -46,13 +46,16 @@ namespace px4_daemon static const unsigned RECV_PIPE_PATH_LEN = 64; +// The packet size is no more than 512 bytes, because that is the minimum guaranteed size +// for a pipe to avoid interleaving of messages when multiple clients write at the same time +// (atomic writes). struct client_send_packet_s { struct message_header_s { - enum class e_msg_id : uint8_t { + uint64_t client_uuid; + enum class e_msg_id : int { EXECUTE, KILL } msg_id; - uint64_t client_uuid; unsigned payload_length; } header; @@ -70,7 +73,7 @@ struct client_send_packet_s { // We have per client receiver a pipe with the uuid in its file path. struct client_recv_packet_s { struct message_header_s { - enum class e_msg_id : uint8_t { + enum class e_msg_id : int { RETVAL, STDOUT } msg_id; @@ -82,7 +85,7 @@ struct client_recv_packet_s { int retval; } retval_msg; struct stdout_msg_s { - uint8_t text[512 - sizeof(message_header_s)]; + uint8_t text[512 - sizeof(message_header_s)]; ///< null-terminated string (payload_length includes the null) } stdout_msg; } payload; }; diff --git a/platforms/posix/src/px4_daemon/pxh.cpp b/platforms/posix/src/px4_daemon/pxh.cpp index 60f840ee37..93e86daa44 100644 --- a/platforms/posix/src/px4_daemon/pxh.cpp +++ b/platforms/posix/src/px4_daemon/pxh.cpp @@ -239,8 +239,6 @@ void Pxh::run_pxh() } } - - return; } void Pxh::stop()