xiaoxiang781216 commented on code in PR #3512: URL: https://github.com/apache/nuttx-apps/pull/3512#discussion_r3329652702
########## examples/microros_pub/Kconfig: ########## @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_MICROROS_PUB + tristate "micro-ROS int32 publisher example" + default n + depends on SYSTEM_MICROROS + ---help--- + Minimal micro-ROS publisher that creates a node and publishes + an std_msgs/Int32 every second on the topic /nuttx_pub. Used + to validate the NuttX-native transport layer end-to-end with + a micro-ROS agent. + +if EXAMPLES_MICROROS_PUB + +config EXAMPLES_MICROROS_PUB_PROGNAME + string "Program name" + default "microros_pub" + +config EXAMPLES_MICROROS_PUB_PRIORITY + int "micro-ROS pub task priority" + default 100 + +config EXAMPLES_MICROROS_PUB_STACKSIZE + int "micro-ROS pub stack size" + default 16384 Review Comment: do we really need so big stack ########## examples/microros_pub/Makefile: ########## @@ -0,0 +1,32 @@ +############################################################################ +# apps/examples/microros_pub/Makefile Review Comment: add cmake build file ########## system/microros/transport/microros_transport_serial.c: ########## @@ -0,0 +1,206 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_serial.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <fcntl.h> +#include <poll.h> +#include <termios.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_serial_fd = -1; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int set_baud(struct termios *tio, int baud) +{ + speed_t s; + + switch (baud) + { + case 9600: + s = B9600; + break; + + case 19200: + s = B19200; + break; + + case 38400: + s = B38400; + break; + + case 57600: + s = B57600; + break; + + case 115200: + s = B115200; + break; + + case 230400: + s = B230400; + break; + + case 460800: + s = B460800; + break; + + case 921600: + s = B921600; + break; + + default: + return -1; + } + + cfsetispeed(tio, s); + cfsetospeed(tio, s); + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_serial_open(void *transport) +{ + struct termios tio; + int fd; + + fd = open(CONFIG_MICROROS_SERIAL_DEVICE, O_RDWR | O_NOCTTY); Review Comment: add O_CLOEXEC ########## system/microros/transport/microros_transport.c: ########## @@ -0,0 +1,60 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> + +#include <rmw_microros/rmw_microros.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int microros_transport_init(void) +{ +#if defined(CONFIG_MICROROS_TRANSPORT_UDP) + rmw_uros_set_custom_transport(false, + NULL, + (open_custom_func)microros_udp_open, + (close_custom_func)microros_udp_close, + (write_custom_func)microros_udp_write, + (read_custom_func)microros_udp_read); + return 0; +#elif defined(CONFIG_MICROROS_TRANSPORT_SERIAL) Review Comment: can/should we support the multiple transport at the same time ########## system/microros/transport/microros_transport_udp.c: ########## @@ -0,0 +1,145 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_udp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <poll.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_udp_fd = -1; +static struct sockaddr_in g_agent_addr; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_udp_open(void *transport) +{ + int fd; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + return false; + } + + memset(&g_agent_addr, 0, sizeof(g_agent_addr)); + g_agent_addr.sin_family = AF_INET; + g_agent_addr.sin_port = htons(CONFIG_MICROROS_AGENT_PORT); + + if (inet_pton(AF_INET, CONFIG_MICROROS_AGENT_IP, + &g_agent_addr.sin_addr) != 1) + { + close(fd); + return false; + } + + g_udp_fd = fd; + return true; +} + +bool microros_udp_close(void *transport) +{ + if (g_udp_fd >= 0) + { + close(g_udp_fd); + g_udp_fd = -1; + } + + return true; +} + +size_t microros_udp_write(void *transport, + const uint8_t *buf, size_t len, uint8_t *err) +{ + ssize_t n; + + if (g_udp_fd < 0) + { + *err = 1; + return 0; + } + + n = sendto(g_udp_fd, buf, len, 0, Review Comment: call send ########## system/microros/transport/microros_transport_udp.c: ########## @@ -0,0 +1,145 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_udp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <poll.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_udp_fd = -1; +static struct sockaddr_in g_agent_addr; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_udp_open(void *transport) +{ + int fd; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + return false; + } + + memset(&g_agent_addr, 0, sizeof(g_agent_addr)); + g_agent_addr.sin_family = AF_INET; + g_agent_addr.sin_port = htons(CONFIG_MICROROS_AGENT_PORT); + + if (inet_pton(AF_INET, CONFIG_MICROROS_AGENT_IP, + &g_agent_addr.sin_addr) != 1) + { + close(fd); + return false; + } + + g_udp_fd = fd; Review Comment: save to tranport and remove g_udp_fd ########## system/microros/transport/microros_transport_udp.c: ########## @@ -0,0 +1,145 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_udp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <poll.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_udp_fd = -1; +static struct sockaddr_in g_agent_addr; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_udp_open(void *transport) +{ + int fd; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + return false; + } + + memset(&g_agent_addr, 0, sizeof(g_agent_addr)); + g_agent_addr.sin_family = AF_INET; + g_agent_addr.sin_port = htons(CONFIG_MICROROS_AGENT_PORT); + + if (inet_pton(AF_INET, CONFIG_MICROROS_AGENT_IP, + &g_agent_addr.sin_addr) != 1) + { + close(fd); + return false; + } + + g_udp_fd = fd; + return true; +} + +bool microros_udp_close(void *transport) +{ + if (g_udp_fd >= 0) + { + close(g_udp_fd); + g_udp_fd = -1; + } + + return true; +} + +size_t microros_udp_write(void *transport, + const uint8_t *buf, size_t len, uint8_t *err) +{ + ssize_t n; + + if (g_udp_fd < 0) + { + *err = 1; + return 0; + } + + n = sendto(g_udp_fd, buf, len, 0, + (struct sockaddr *)&g_agent_addr, sizeof(g_agent_addr)); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; +} + +size_t microros_udp_read(void *transport, + uint8_t *buf, size_t len, + int timeout_ms, uint8_t *err) +{ + struct pollfd pfd; + ssize_t n; + int r; + + if (g_udp_fd < 0) + { + *err = 1; + return 0; + } + + pfd.fd = g_udp_fd; + pfd.events = POLLIN; + + r = poll(&pfd, 1, timeout_ms); + if (r < 0) + { + *err = 1; + return 0; + } + + if (r == 0) + { + return 0; + } + + n = recv(g_udp_fd, buf, len, 0); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; Review Comment: remove too ########## examples/microros_pub/microros_pub_main.c: ########## @@ -0,0 +1,114 @@ +/**************************************************************************** + * apps/examples/microros_pub/microros_pub_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdio.h> +#include <unistd.h> + +#include <rcl/rcl.h> +#include <rcl/error_handling.h> +#include <rclc/rclc.h> +#include <rclc/executor.h> +#include <std_msgs/msg/int32.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static rcl_publisher_t g_publisher; +static std_msgs__msg__Int32 g_msg; Review Comment: change to local variables ########## system/microros/transport/microros_transport_serial.c: ########## @@ -0,0 +1,206 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_serial.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <fcntl.h> +#include <poll.h> +#include <termios.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_serial_fd = -1; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int set_baud(struct termios *tio, int baud) +{ + speed_t s; + + switch (baud) + { + case 9600: + s = B9600; + break; + + case 19200: + s = B19200; + break; + + case 38400: + s = B38400; + break; + + case 57600: + s = B57600; + break; + + case 115200: + s = B115200; + break; + + case 230400: + s = B230400; + break; + + case 460800: + s = B460800; + break; + + case 921600: + s = B921600; + break; + + default: + return -1; + } + + cfsetispeed(tio, s); + cfsetospeed(tio, s); + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_serial_open(void *transport) +{ + struct termios tio; + int fd; + + fd = open(CONFIG_MICROROS_SERIAL_DEVICE, O_RDWR | O_NOCTTY); + if (fd < 0) + { + return false; + } + + if (tcgetattr(fd, &tio) < 0) + { + close(fd); + return false; + } + + cfmakeraw(&tio); + tio.c_cflag |= (CLOCAL | CREAD); + tio.c_cflag &= ~CRTSCTS; + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 0; + + if (set_baud(&tio, CONFIG_MICROROS_SERIAL_BAUD) < 0) + { + close(fd); + return false; + } + + if (tcsetattr(fd, TCSANOW, &tio) < 0) + { + close(fd); + return false; + } + + tcflush(fd, TCIOFLUSH); + g_serial_fd = fd; Review Comment: could we save fd to transport and remove g_serial_fd ########## system/microros/transport/microros_transport.c: ########## @@ -0,0 +1,60 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> + +#include <rmw_microros/rmw_microros.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int microros_transport_init(void) +{ +#if defined(CONFIG_MICROROS_TRANSPORT_UDP) + rmw_uros_set_custom_transport(false, + NULL, + (open_custom_func)microros_udp_open, Review Comment: remove ALL cast ########## system/microros/transport/microros_transport_serial.c: ########## @@ -0,0 +1,206 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_serial.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <fcntl.h> +#include <poll.h> +#include <termios.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_serial_fd = -1; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int set_baud(struct termios *tio, int baud) +{ + speed_t s; + + switch (baud) + { + case 9600: + s = B9600; + break; + + case 19200: + s = B19200; + break; + + case 38400: + s = B38400; + break; + + case 57600: + s = B57600; + break; + + case 115200: + s = B115200; + break; + + case 230400: + s = B230400; + break; + + case 460800: + s = B460800; + break; + + case 921600: + s = B921600; + break; + + default: + return -1; + } + + cfsetispeed(tio, s); + cfsetospeed(tio, s); + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_serial_open(void *transport) +{ + struct termios tio; + int fd; + + fd = open(CONFIG_MICROROS_SERIAL_DEVICE, O_RDWR | O_NOCTTY); + if (fd < 0) + { + return false; + } + + if (tcgetattr(fd, &tio) < 0) + { + close(fd); + return false; + } + + cfmakeraw(&tio); + tio.c_cflag |= (CLOCAL | CREAD); + tio.c_cflag &= ~CRTSCTS; + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 0; + + if (set_baud(&tio, CONFIG_MICROROS_SERIAL_BAUD) < 0) + { + close(fd); + return false; + } + + if (tcsetattr(fd, TCSANOW, &tio) < 0) + { + close(fd); + return false; + } + + tcflush(fd, TCIOFLUSH); + g_serial_fd = fd; + return true; +} + +bool microros_serial_close(void *transport) +{ + if (g_serial_fd >= 0) + { + close(g_serial_fd); + g_serial_fd = -1; + } + + return true; +} + +size_t microros_serial_write(void *transport, + const uint8_t *buf, size_t len, uint8_t *err) +{ + ssize_t n; + + if (g_serial_fd < 0) + { + *err = 1; + return 0; + } + + n = write(g_serial_fd, buf, len); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; Review Comment: remove the cast ########## system/microros/transport/microros_transport_udp.c: ########## @@ -0,0 +1,145 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_udp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <poll.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_udp_fd = -1; +static struct sockaddr_in g_agent_addr; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_udp_open(void *transport) +{ + int fd; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + return false; + } + + memset(&g_agent_addr, 0, sizeof(g_agent_addr)); Review Comment: change g_agent_addr to local variable and call connect ########## system/microros/Make.defs: ########## @@ -23,8 +23,12 @@ ifneq ($(CONFIG_SYSTEM_MICROROS),) CONFIGURED_APPS += $(APPDIR)/system/microros -CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include -CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include +MICROROS_INC_DIRS := $(APPDIR)/system/microros/include \ + $(wildcard $(APPDIR)/system/microros/include/*) +CFLAGS += $(foreach d,$(MICROROS_INC_DIRS),${INCDIR_PREFIX}$(d)) +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/transport +CXXFLAGS += $(foreach d,$(MICROROS_INC_DIRS),${INCDIR_PREFIX}$(d)) +CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/transport Review Comment: ditto ########## system/microros/Make.defs: ########## @@ -23,8 +23,12 @@ ifneq ($(CONFIG_SYSTEM_MICROROS),) CONFIGURED_APPS += $(APPDIR)/system/microros -CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include -CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include +MICROROS_INC_DIRS := $(APPDIR)/system/microros/include \ + $(wildcard $(APPDIR)/system/microros/include/*) +CFLAGS += $(foreach d,$(MICROROS_INC_DIRS),${INCDIR_PREFIX}$(d)) +CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/transport Review Comment: move to Makefile to avoid expose $(APPDIR)/system/microros/transport to public search path ########## system/microros/transport/microros_transport_serial.c: ########## @@ -0,0 +1,206 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_serial.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <fcntl.h> +#include <poll.h> +#include <termios.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_serial_fd = -1; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int set_baud(struct termios *tio, int baud) +{ + speed_t s; + + switch (baud) + { + case 9600: + s = B9600; + break; + + case 19200: + s = B19200; + break; + + case 38400: + s = B38400; + break; + + case 57600: + s = B57600; + break; + + case 115200: + s = B115200; + break; + + case 230400: + s = B230400; + break; + + case 460800: + s = B460800; + break; + + case 921600: + s = B921600; + break; + + default: + return -1; + } + + cfsetispeed(tio, s); + cfsetospeed(tio, s); + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_serial_open(void *transport) +{ + struct termios tio; + int fd; + + fd = open(CONFIG_MICROROS_SERIAL_DEVICE, O_RDWR | O_NOCTTY); + if (fd < 0) + { + return false; + } + + if (tcgetattr(fd, &tio) < 0) + { + close(fd); + return false; + } + + cfmakeraw(&tio); + tio.c_cflag |= (CLOCAL | CREAD); + tio.c_cflag &= ~CRTSCTS; + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 0; + + if (set_baud(&tio, CONFIG_MICROROS_SERIAL_BAUD) < 0) + { + close(fd); + return false; + } + + if (tcsetattr(fd, TCSANOW, &tio) < 0) + { + close(fd); + return false; + } + + tcflush(fd, TCIOFLUSH); + g_serial_fd = fd; + return true; +} + +bool microros_serial_close(void *transport) +{ + if (g_serial_fd >= 0) + { + close(g_serial_fd); + g_serial_fd = -1; + } + + return true; +} + +size_t microros_serial_write(void *transport, + const uint8_t *buf, size_t len, uint8_t *err) +{ + ssize_t n; + + if (g_serial_fd < 0) + { + *err = 1; + return 0; + } + + n = write(g_serial_fd, buf, len); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; +} + +size_t microros_serial_read(void *transport, + uint8_t *buf, size_t len, + int timeout_ms, uint8_t *err) +{ + struct pollfd pfd; + ssize_t n; + int r; + + if (g_serial_fd < 0) + { + *err = 1; + return 0; + } + + pfd.fd = g_serial_fd; + pfd.events = POLLIN; + + r = poll(&pfd, 1, timeout_ms); + if (r < 0) + { + *err = 1; + return 0; + } + + if (r == 0) + { + return 0; + } + + n = read(g_serial_fd, buf, len); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; Review Comment: remove the cast too ########## system/microros/transport/microros_transport_udp.c: ########## @@ -0,0 +1,145 @@ +/**************************************************************************** + * apps/system/microros/transport/microros_transport_udp.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <poll.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "microros_transport.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static int g_udp_fd = -1; +static struct sockaddr_in g_agent_addr; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool microros_udp_open(void *transport) +{ + int fd; + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) + { + return false; + } + + memset(&g_agent_addr, 0, sizeof(g_agent_addr)); + g_agent_addr.sin_family = AF_INET; + g_agent_addr.sin_port = htons(CONFIG_MICROROS_AGENT_PORT); + + if (inet_pton(AF_INET, CONFIG_MICROROS_AGENT_IP, + &g_agent_addr.sin_addr) != 1) + { + close(fd); + return false; + } + + g_udp_fd = fd; + return true; +} + +bool microros_udp_close(void *transport) +{ + if (g_udp_fd >= 0) + { + close(g_udp_fd); + g_udp_fd = -1; + } + + return true; +} + +size_t microros_udp_write(void *transport, + const uint8_t *buf, size_t len, uint8_t *err) +{ + ssize_t n; + + if (g_udp_fd < 0) + { + *err = 1; + return 0; + } + + n = sendto(g_udp_fd, buf, len, 0, + (struct sockaddr *)&g_agent_addr, sizeof(g_agent_addr)); + if (n < 0) + { + *err = 1; + return 0; + } + + return (size_t)n; Review Comment: remove the cast ########## system/microros/Make.defs: ########## @@ -23,8 +23,12 @@ ifneq ($(CONFIG_SYSTEM_MICROROS),) CONFIGURED_APPS += $(APPDIR)/system/microros -CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include -CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/system/microros/include +MICROROS_INC_DIRS := $(APPDIR)/system/microros/include \ Review Comment: merge to previous patch -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
