| 1 | /* $NetBSD: can.h,v 1.3 2017/05/30 13:30:51 bouyer Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Robert Swindells and Manuel Bouyer |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef _NETCAN_CAN_H |
| 33 | #define _NETCAN_CAN_H |
| 34 | |
| 35 | #include <sys/featuretest.h> |
| 36 | #include <sys/types.h> |
| 37 | |
| 38 | |
| 39 | /* Definitions compatible (as much as possible) with socketCAN */ |
| 40 | |
| 41 | /* |
| 42 | * CAN id structure |
| 43 | * bits 0-28 : CAN identifier (11/29 bits, see bit 31) |
| 44 | * bit2 29-31 : see below |
| 45 | */ |
| 46 | |
| 47 | typedef uint32_t canid_t; |
| 48 | typedef uint32_t can_err_mask_t; |
| 49 | |
| 50 | /* canid_t bits 29-31 descriptions */ |
| 51 | #define CAN_EFF_FLAG 0x80000000U /* extended frame format */ |
| 52 | #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */ |
| 53 | #define CAN_ERR_FLAG 0x20000000U /* error message frame */ |
| 54 | |
| 55 | /* valid bits in CAN ID for frame formats */ |
| 56 | #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ |
| 57 | #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ |
| 58 | #define CAN_ERR_MASK 0x1FFFFFFFU /* error frame format */ |
| 59 | |
| 60 | /* CAN payload length and DLC definitions according to ISO 11898-1 */ |
| 61 | #define CAN_MAX_DLC 8 |
| 62 | #define CAN_MAX_DLEN 8 |
| 63 | |
| 64 | /* CAN frame */ |
| 65 | struct can_frame { |
| 66 | canid_t can_id; /* ID + EFF/RTR/ERR flags */ |
| 67 | uint8_t can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */ |
| 68 | uint8_t __pad; |
| 69 | uint8_t __res0; |
| 70 | uint8_t __res1; |
| 71 | uint8_t data[CAN_MAX_DLEN] __aligned(8); |
| 72 | }; |
| 73 | |
| 74 | #define CAN_MTU (sizeof(struct can_frame)) |
| 75 | |
| 76 | /* protocols */ |
| 77 | #define CAN_RAW 1 /* RAW sockets */ |
| 78 | #define CAN_NPROTO 2 |
| 79 | |
| 80 | /* |
| 81 | * Socket address, CAN style |
| 82 | */ |
| 83 | struct sockaddr_can { |
| 84 | u_int8_t can_len; |
| 85 | sa_family_t can_family; |
| 86 | int can_ifindex; |
| 87 | union { |
| 88 | /* transport protocol class address information (e.g. ISOTP) */ |
| 89 | struct { canid_t rx_id, tx_id; } tp; |
| 90 | /* reserved for future CAN protocols address information */ |
| 91 | } can_addr; |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Options for use with [gs]etsockopt for raw sockets |
| 96 | * First word of comment is data type; bool is stored in int. |
| 97 | */ |
| 98 | #define SOL_CAN_RAW CAN_RAW |
| 99 | |
| 100 | #define CAN_RAW_FILTER 1 /* struct can_filter: set filter */ |
| 101 | #define CAN_RAW_LOOPBACK 4 /* bool: loopback to local sockets (default:on) */ |
| 102 | #define CAN_RAW_RECV_OWN_MSGS 5 /* bool: receive my own msgs (default:off) */ |
| 103 | |
| 104 | /* |
| 105 | * CAN ID based filter |
| 106 | * checks received can_id & can_filter.can_mask against |
| 107 | * can_filter.can_id & can_filter.can_mask |
| 108 | * valid flags for can_id: |
| 109 | * CAN_INV_FILTER: invert filter |
| 110 | * valid flags for can_mask: |
| 111 | * CAN_ERR_FLAG: filter for error message frames |
| 112 | */ |
| 113 | struct can_filter { |
| 114 | canid_t can_id; |
| 115 | canid_t can_mask; |
| 116 | }; |
| 117 | |
| 118 | #define CAN_INV_FILTER 0x20000000U |
| 119 | |
| 120 | #ifdef _NETBSD_SOURCE |
| 121 | #ifdef _KERNEL |
| 122 | |
| 123 | #define satoscan(sa) ((struct sockaddr_can *)(sa)) |
| 124 | #define scantosa(scan) ((struct sockaddr *)(scan)) |
| 125 | |
| 126 | #endif /* _KERNEL */ |
| 127 | #endif /* _NETBSD_SOURCE */ |
| 128 | #endif /* _NETCAN_CAN_H */ |
| 129 | |