diff --git a/canutils/canlib/Kconfig b/canutils/canlib/Kconfig new file mode 100644 index 000000000..0eab8a07f --- /dev/null +++ b/canutils/canlib/Kconfig @@ -0,0 +1,17 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config CANUTILS_CANLIB + bool "CAN utility library" + default n + depends on CAN && LIB_BOARDCTL + select BOARDCTL_CANINIT + ---help--- + Enable the CAN utility library + +if CANUTILS_CANLIB + +endif + diff --git a/canutils/canlib/Make.defs b/canutils/canlib/Make.defs new file mode 100644 index 000000000..f94a1d21e --- /dev/null +++ b/canutils/canlib/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/canutils/canlib/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2016 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +ifeq ($(CONFIG_CANUTILS_CANLIB),y) +CONFIGURED_APPS += canutils/canlib +endif diff --git a/canutils/canlib/Makefile b/canutils/canlib/Makefile new file mode 100644 index 000000000..ada335b1b --- /dev/null +++ b/canutils/canlib/Makefile @@ -0,0 +1,45 @@ +############################################################################ +# apps/canutils/canlib/Makefile +# +# Copyright (C) 2016 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# CAN utility library + +ASRCS = +CSRCS = canlib_getbaud.c canlib_setbaud.c +CSRCS+= canlib_getloopback.c canlib_setloopback.c +CSRCS+= canlib_getsilent.c canlib_setsilent.c + +include $(APPDIR)/Application.mk diff --git a/canutils/canlib/canlib_getbaud.c b/canutils/canlib/canlib_getbaud.c new file mode 100644 index 000000000..59cdc0d38 --- /dev/null +++ b/canutils/canlib/canlib_getbaud.c @@ -0,0 +1,82 @@ +/**************************************************************************** + * canutils/canlib/canlib_getbaud.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_getbaud + * + * Description: + * Wrapper for CANIOC_GET_BITTIMING + * + * Input Parameter: + * fd - file descriptor of an opened can device + * baud - pointer to a buffer to store the current baud rate + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_getbaud(int fd, FAR int *bauds) +{ + int ret; + struct canioc_bittiming_s timings; + + ret = ioctl(fd, CANIOC_GET_BITTIMING, (unsigned long)&timings); + if (ret!=OK) + { + canerr("CANIOC_GET_BITTIMING failed, errno=%d\n", errno); + return 0; + } + + *bauds = timings.bt_baud; + + return ret; +} diff --git a/canutils/canlib/canlib_getloopback.c b/canutils/canlib/canlib_getloopback.c new file mode 100644 index 000000000..7fa29f95c --- /dev/null +++ b/canutils/canlib/canlib_getloopback.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * canutils/canlib/canlib_getloopback.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_getloopback + * + * Description: + * Wrapper for CANIOC_GET_CONNMODES. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - pointer to a buffer to store the current loopback mode state. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_getloopback(int fd, FAR bool *loopback) +{ + int ret; + struct canioc_connmodes_s connmodes; + + ret = ioctl(fd, CANIOC_GET_CONNMODES, (unsigned long)&connmodes); + if (ret!=OK) + { + canerr("CANIOC_GET_CONNMODES failed, errno=%d\n", errno); + return FALSE; + } + + *loopback = (bool)connmodes.bm_loopback; + + return ret; +} diff --git a/canutils/canlib/canlib_getsilent.c b/canutils/canlib/canlib_getsilent.c new file mode 100644 index 000000000..1e855e739 --- /dev/null +++ b/canutils/canlib/canlib_getsilent.c @@ -0,0 +1,84 @@ +/**************************************************************************** + * canutils/canlib/canlib_getsilent.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_getsilent + * + * Description: + * Wrapper for CANIOC_GET_CONNMODES. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - pointer to a buffer to store the current silent mode state. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_getsilent(int fd, FAR bool *silent) +{ + int ret; + struct canioc_connmodes_s connmodes; + + ret = ioctl(fd, CANIOC_GET_CONNMODES, (unsigned long)&connmodes); + if (ret!=OK) + { + canerr("CANIOC_GET_CONNMODES failed, errno=%d\n", errno); + return FALSE; + } + + *silent = (bool)connmodes.bm_silent; + + return ret; +} + diff --git a/canutils/canlib/canlib_setbaud.c b/canutils/canlib/canlib_setbaud.c new file mode 100644 index 000000000..88998edd8 --- /dev/null +++ b/canutils/canlib/canlib_setbaud.c @@ -0,0 +1,87 @@ +/**************************************************************************** + * canutils/canlib/canlib_setbaud.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_setbaud + * + * Description: + * Wrapper for CANIOC_SET_BITTIMING + * + * Input Parameter: + * fd - file descriptor of an opened can device + * baud - baud rate to use on the CAN bus + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setbaud(int fd, int bauds) +{ + int ret; + struct canioc_bittiming_s timings; + + ret = ioctl(fd, CANIOC_GET_BITTIMING, (unsigned long)&timings); + if (ret!=OK) + { + canerr("CANIOC_GET_BITTIMING failed, errno=%d\n", errno); + return ret; + } + + timings.bt_baud = bauds; + ret = ioctl(fd, CANIOC_SET_BITTIMING, (unsigned long)&timings); + + if (ret!=OK) + { + canerr("CANIOC_SET_BITTIMING failed, errno=%d\n", errno); + } + return ret; +} diff --git a/canutils/canlib/canlib_setloopback.c b/canutils/canlib/canlib_setloopback.c new file mode 100644 index 000000000..717568d8d --- /dev/null +++ b/canutils/canlib/canlib_setloopback.c @@ -0,0 +1,89 @@ +/**************************************************************************** + * canutils/canlib/canlib_setloopback.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_setloopback + * + * Description: + * Wrapper for CANIOC_SET_CONNMODES. When loopback mode is enabled, the CAN + * peripheral transmits on the bus, but only receives its own sent messages. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - wether to use loopback mode. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setloopback(int fd, bool loopback) +{ + int ret; + struct canioc_connmodes_s connmodes; + + ret = ioctl(fd, CANIOC_GET_CONNMODES, (unsigned long)&connmodes); + if (ret!=OK) + { + canerr("CANIOC_GET_CONNMODES failed, errno=%d\n", errno); + return ret; + } + + connmodes.bm_loopback = !!loopback; + ret = ioctl(fd, CANIOC_SET_CONNMODES, (unsigned long)&connmodes); + + if (ret!=OK) + { + canerr("CANIOC_SET_CONNMODES failed, errno=%d\n", errno); + } + return ret; +} diff --git a/canutils/canlib/canlib_setsilent.c b/canutils/canlib/canlib_setsilent.c new file mode 100644 index 000000000..4f0376481 --- /dev/null +++ b/canutils/canlib/canlib_setsilent.c @@ -0,0 +1,89 @@ +/**************************************************************************** + * canutils/canlib/canlib_setsilent.c + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Name: canlib_setsilent + * + * Description: + * Wrapper for CANIOC_SET_CONNMODES. When silent mode is enabled, the CAN + * peripheral never transmits on the bus, but receives all bus traffic. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - wether to use loopback mode. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setsilent(int fd, bool silent) +{ + int ret; + struct canioc_connmodes_s connmodes; + + ret = ioctl(fd, CANIOC_GET_CONNMODES, (unsigned long)&connmodes); + if (ret!=OK) + { + canerr("CANIOC_GET_CONNMODES failed, errno=%d\n", errno); + return ret; + } + + connmodes.bm_silent = !!silent; + ret = ioctl(fd, CANIOC_SET_CONNMODES, (unsigned long)&connmodes); + + if (ret!=OK) + { + canerr("CANIOC_SET_CONNMODES failed, errno=%d\n", errno); + } + return ret; +} diff --git a/include/canutils/canlib.h b/include/canutils/canlib.h new file mode 100644 index 000000000..59befd8b3 --- /dev/null +++ b/include/canutils/canlib.h @@ -0,0 +1,190 @@ +/**************************************************************************** + * apps/include/canutils/canlib.h + * Various non-standard APIs to support canutils. All non-standard and + * intended only for internal use. + * + * Copyright (C) 2016 Sebastien Lorquet Nutt. All rights reserved. + * Author: Sebastien Lorquet + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __APPS_INCLUDE_CANUTILS_CANLIB_H +#define __APPS_INCLUDE_CANUTILS_CANLIB_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: canlib_setbaud + * + * Description: + * Wrapper for CANIOC_SET_BITTIMING + * + * Input Parameter: + * fd - file descriptor of an opened can device + * baud - baud rate to use on the CAN bus + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setbaud(int fd, int bauds); + +/**************************************************************************** + * Name: canlib_getbaud + * + * Description: + * Wrapper for CANIOC_GET_BITTIMING + * + * Input Parameter: + * fd - file descriptor of an opened can device + * baud - pointer to a buffer to store the current baud rate + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_getbaud(int fd, FAR int *bauds); + +/**************************************************************************** + * Name: canlib_setloopback + * + * Description: + * Wrapper for CANIOC_SET_CONNMODES. When loopback mode is enabled, the CAN + * peripheral transmits on the bus, but only receives its own sent messages. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - wether to use loopback mode. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +/**************************************************************************** + * Name: canlib_getloopback + * + * Description: + * Wrapper for CANIOC_GET_CONNMODES. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - pointer to a buffer to store the current loopback mode state. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setloopback(int fd, bool loop); + +int canlib_getloopback(int fd, FAR bool *loop); + +/**************************************************************************** + * Name: canlib_setsilent + * + * Description: + * Wrapper for CANIOC_SET_CONNMODES. When silent mode is enabled, the CAN + * peripheral never transmits on the bus, but receives all bus traffic. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - wether to use loopback mode. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_setsilent(int fd, bool silent); + +/**************************************************************************** + * Name: canlib_getsilent + * + * Description: + * Wrapper for CANIOC_GET_CONNMODES. + * + * Input Parameter: + * fd - file descriptor of an opened can device + * loopback - pointer to a buffer to store the current silent mode state. + * + * Returned Value: + * Zero (OK) is returned on success. Otherwise -1 (ERROR) + * is returned with the errno variable set to indicate the + * nature of the error. + * + ****************************************************************************/ + +int canlib_getsilent(int fd, FAR bool *silent); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __APPS_INCLUDE_CANUTILS_CANLIB_H */