### Start CAN Interface using libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Starts a specified CAN interface by setting its state to UP. This function relies on the kernel for initialization and assumes the interface is already configured with necessary parameters like bitrate. It returns an error if the interface cannot be started. ```c #include #include int main(void) { const char *ifname = "can0"; /* Start the CAN interface */ if (can_do_start(ifname) < 0) { fprintf(stderr, "Failed to start %s\n", ifname); return -1; } printf("Interface %s started successfully\n", ifname); return 0; } /* Expected output: Interface can0 started successfully */ ``` -------------------------------- ### Get CAN Hardware Timing Constants - C Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves hardware-dependent bit timing constraints for a CAN controller. These constants are crucial for calculating valid bit timing parameters that the hardware supports. Requires the 'libsocketcan' library. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_bittiming_const btc; if (can_get_bittiming_const(ifname, &btc) < 0) { fprintf(stderr, "Failed to get bit timing constants\n"); return -1; } printf("Bit timing constants for %s (controller: %s):\n", ifname, btc.name); printf(" TSEG1: %u - %u\n", btc.tseg1_min, btc.tseg1_max); printf(" TSEG2: %u - %u\n", btc.tseg2_min, btc.tseg2_max); printf(" SJW max: %u\n", btc.sjw_max); printf(" BRP: %u - %u (increment: %u)\n", btc.brp_min, btc.brp_max, btc.brp_inc); return 0; } ``` -------------------------------- ### Get CAN Auto-Restart Interval with libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the auto-restart interval setting for a CAN interface using `can_get_restart_ms`. A return value of 0 indicates that auto-restart is disabled. This function requires the `` header. ```c #include #include int main(void) { const char *ifname = "can0"; __u32 restart_ms; if (can_get_restart_ms(ifname, &restart_ms) < 0) { fprintf(stderr, "Failed to get restart interval\n"); return -1; } if (restart_ms == 0) { printf("Auto-restart is disabled on %s\n", ifname); } else { printf("Auto-restart interval: %u ms on %s\n", restart_ms, ifname); } return 0; } ``` -------------------------------- ### Get CAN Interface Bit Timing - C Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the current bit timing configuration for a CAN interface, including bitrate, sample point, and timing segments. This function is useful for understanding the current communication parameters. Requires the 'libsocketcan' library. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_bittiming bt; if (can_get_bittiming(ifname, &bt) < 0) { fprintf(stderr, "Failed to get bit timing\n"); return -1; } printf("Bit timing for %s:\n", ifname); printf(" Bitrate: %u bps\n", bt.bitrate); printf(" Sample point: %u.%u%%\n", bt.sample_point / 10, bt.sample_point % 10); printf(" TQ: %u ns\n", bt.tq); printf(" Prop seg: %u TQs\n", bt.prop_seg); printf(" Phase seg1: %u TQs\n", bt.phase_seg1); printf(" Phase seg2: %u TQs\n", bt.phase_seg2); printf(" SJW: %u TQs\n", bt.sjw); printf(" BRP: %u\n", bt.brp); return 0; } ``` -------------------------------- ### Get CAN FD Data Phase Bit Timing - C Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the bit timing configuration specifically for the data phase of CAN FD transmissions. This is essential for configuring higher data rates in flexible data-rate capable CAN controllers. Requires the 'libsocketcan' library. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_bittiming dbt; if (can_get_data_bittiming(ifname, &dbt) < 0) { fprintf(stderr, "Failed to get data bit timing (CAN FD may not be enabled)\n"); return -1; } printf("CAN FD data phase timing for %s:\n", ifname); printf(" Data bitrate: %u bps\n", dbt.bitrate); printf(" Data sample point: %u.%u%%\n", dbt.sample_point / 10, dbt.sample_point % 10); return 0; } ``` -------------------------------- ### Get CAN Device Statistics with libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves CAN-specific device statistics, such as bus errors, state changes, and restart counts, using the `can_get_device_stats` function. It requires `` and populates a `struct can_device_stats`. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_device_stats cds; if (can_get_device_stats(ifname, &cds) < 0) { fprintf(stderr, "Failed to get device stats\n"); return -1; } printf("CAN device statistics for %s:\n", ifname); printf(" Bus errors: %u\n", cds.bus_error); printf(" Error warnings: %u\n", cds.error_warning); printf(" Error passive: %u\n", cds.error_passive); printf(" Bus off: %u\n", cds.bus_off); printf(" Arbitration lost: %u\n", cds.arbitration_lost); printf(" Restarts: %u\n", cds.restarts); return 0; } ``` -------------------------------- ### Get CAN Interface State - C Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the current operational state of a CAN interface, indicating error levels based on TX/RX error counters. It maps integer states to human-readable strings. Requires the 'libsocketcan' library. ```c #include #include const char *state_to_string(int state) { switch (state) { case CAN_STATE_ERROR_ACTIVE: return "ERROR_ACTIVE"; case CAN_STATE_ERROR_WARNING: return "ERROR_WARNING"; case CAN_STATE_ERROR_PASSIVE: return "ERROR_PASSIVE"; case CAN_STATE_BUS_OFF: return "BUS_OFF"; case CAN_STATE_STOPPED: return "STOPPED"; case CAN_STATE_SLEEPING: return "SLEEPING"; default: return "UNKNOWN"; } } int main(void) { const char *ifname = "can0"; int state; if (can_get_state(ifname, &state) < 0) { fprintf(stderr, "Failed to get state of %s\n", ifname); return -1; } printf("Interface %s state: %s (%d)\n", ifname, state_to_string(state), state); return 0; } /* Expected output: Interface can0 state: ERROR_ACTIVE (0) */ ``` -------------------------------- ### Get CAN Bus Error Counters with libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the current transmit (TX) and receive (RX) error counters for a CAN interface using `can_get_berr_counter`. This function depends on `` and populates a `struct can_berr_counter`. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_berr_counter bc; if (can_get_berr_counter(ifname, &bc) < 0) { fprintf(stderr, "Failed to get error counters\n"); return -1; } printf("Error counters for %s:\n", ifname); printf(" TX errors: %u\n", bc.txerr); printf(" RX errors: %u\n", bc.rxerr); return 0; } /* Expected output: * Error counters for can0: * TX errors: 0 * RX errors: 0 */ ``` -------------------------------- ### Get CAN Controller Clock Frequency - C Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the system clock frequency of the CAN controller. This information is fundamental for accurate bit timing calculations and ensuring proper communication speed. Requires the 'libsocketcan' library. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_clock clock; if (can_get_clock(ifname, &clock) < 0) { fprintf(stderr, "Failed to get clock info\n"); return -1; } printf("CAN controller clock for %s: %u Hz (%.2f MHz)\n", ifname, clock.freq, clock.freq / 1000000.0); return 0; } /* Expected output: CAN controller clock for can0: 8000000 Hz (8.00 MHz) */ ``` -------------------------------- ### Get CAN Interface Control Mode with libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves the current control mode settings for a specified CAN interface using the `can_get_ctrlmode` function. It requires the `` header and outputs the status of various modes like loopback, listen-only, and CAN FD. ```c #include #include int main(void) { const char *ifname = "can0"; struct can_ctrlmode cm; if (can_get_ctrlmode(ifname, &cm) < 0) { fprintf(stderr, "Failed to get control mode\n"); return -1; } printf("Control modes for %s:\n", ifname); printf(" Loopback: %s\n", (cm.flags & CAN_CTRLMODE_LOOPBACK) ? "ON" : "OFF"); printf(" Listen-only: %s\n", (cm.flags & CAN_CTRLMODE_LISTENONLY) ? "ON" : "OFF"); printf(" Triple sample: %s\n", (cm.flags & CAN_CTRLMODE_3_SAMPLES) ? "ON" : "OFF"); printf(" One-shot: %s\n", (cm.flags & CAN_CTRLMODE_ONE_SHOT) ? "ON" : "OFF"); printf(" BERR reporting: %s\n", (cm.flags & CAN_CTRLMODE_BERR_REPORTING) ? "ON" : "OFF"); printf(" CAN FD: %s\n", (cm.flags & CAN_CTRLMODE_FD) ? "ON" : "OFF"); printf(" Presume ACK: %s\n", (cm.flags & CAN_CTRLMODE_PRESUME_ACK) ? "ON" : "OFF"); return 0; } ``` -------------------------------- ### Get CAN Network Link Statistics with libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Retrieves standard 64-bit network link statistics, including packet and byte counts for both RX and TX, using `can_get_link_stats`. This function requires `` and `` and populates a `struct rtnl_link_stats64`. ```c #include #include #include int main(void) { const char *ifname = "can0"; struct rtnl_link_stats64 rls; if (can_get_link_stats(ifname, &rls) < 0) { fprintf(stderr, "Failed to get link stats\n"); return -1; } printf("Link statistics for %s:\n", ifname); printf(" RX packets: %llu\n", (unsigned long long)rls.rx_packets); printf(" TX packets: %llu\n", (unsigned long long)rls.tx_packets); printf(" RX bytes: %llu\n", (unsigned long long)rls.rx_bytes); printf(" TX bytes: %llu\n", (unsigned long long)rls.tx_bytes); printf(" RX errors: %llu\n", (unsigned long long)rls.rx_errors); printf(" TX errors: %llu\n", (unsigned long long)rls.tx_errors); printf(" RX dropped: %llu\n", (unsigned long long)rls.rx_dropped); printf(" TX dropped: %llu\n", (unsigned long long)rls.tx_dropped); return 0; } ``` -------------------------------- ### Bitrate Configuration Functions Source: https://context7.com/linux-can/libsocketcan/llms.txt Functions for setting the bitrate and sample point of CAN interfaces. ```APIDOC ## can_set_bitrate - Set CAN Bus Bitrate ### Description Sets the bitrate for a CAN interface. The kernel automatically calculates the optimal bit timing parameters. Valid bitrates range from 1000 (1 kbit/s) to 1000000 (1 Mbit/s). Requires CONFIG_CAN_CALC_BITTIMING enabled in kernel. ### Method `can_set_bitrate` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include int main(void) { const char *ifname = "can0"; __u32 bitrate = 500000; /* 500 kbit/s */ /* Stop interface before configuration */ can_do_stop(ifname); /* Set the bitrate */ if (can_set_bitrate(ifname, bitrate) < 0) { fprintf(stderr, "Failed to set bitrate on %s\n", ifname); return -1; } /* Start interface with new configuration */ if (can_do_start(ifname) < 0) { fprintf(stderr, "Failed to start %s\n", ifname); return -1; } printf("Bitrate set to %u bps on %s\n", bitrate, ifname); return 0; } ``` ### Response #### Success Response (0) Returns 0 on success. #### Error Response (-1) Returns -1 on failure. #### Response Example `Bitrate set to 500000 bps on can0` ``` ```APIDOC ## can_set_bitrate_samplepoint - Set Bitrate with Custom Sample Point ### Description Sets the bitrate with a custom sample point value instead of using the CIA-recommended default. Sample point is specified in one-tenth of a percent (0-999). ### Method `can_set_bitrate_samplepoint` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include int main(void) { const char *ifname = "can0"; __u32 bitrate = 500000; /* 500 kbit/s */ __u32 sample_point = 875; /* 87.5% sample point */ can_do_stop(ifname); if (can_set_bitrate_samplepoint(ifname, bitrate, sample_point) < 0) { fprintf(stderr, "Failed to set bitrate with sample point\n"); return -1; } can_do_start(ifname); printf("Bitrate: %u bps, Sample point: %u.%u%%\n", bitrate, sample_point / 10, sample_point % 10); return 0; } ``` ### Response #### Success Response (0) Returns 0 on success. #### Error Response (-1) Returns -1 on failure. #### Response Example `Bitrate: 500000 bps, Sample point: 87.5%` ``` -------------------------------- ### Set CAN FD Bitrates with Sample Points (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt Configures CAN FD with custom sample points for both arbitration and data phases using `can_set_canfd_bitrates_samplepoint`. This function takes the interface name, arbitration bitrate and sample point, and data bitrate and sample point as arguments. It requires `` and ``. ```c #include #include int main(void) { const char *ifname = "can0"; __u32 bitrate = 500000; /* Arbitration: 500 kbit/s */ __u32 sample_point = 875; /* Arbitration sample point: 87.5% */ __u32 dbitrate = 2000000; /* Data: 2 Mbit/s */ __u32 dsample_point = 750; /* Data sample point: 75.0% */ can_do_stop(ifname); if (can_set_canfd_bitrates_samplepoint(ifname, bitrate, sample_point, dbitrate, dsample_point) < 0) { fprintf(stderr, "Failed to configure CAN FD\n"); return -1; } can_do_start(ifname); printf("CAN FD configured successfully\n"); return 0; } ``` -------------------------------- ### Set CAN Interface Bitrate and Sample Point using libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Sets the bitrate for a CAN interface along with a custom sample point, specified in tenths of a percent (0-999). This allows finer control over bit timing compared to the default calculation. The interface needs to be stopped before configuration and restarted afterwards. An error is returned if the operation fails. ```c #include #include int main(void) { const char *ifname = "can0"; __u32 bitrate = 500000; /* 500 kbit/s */ __u32 sample_point = 875; /* 87.5% sample point */ can_do_stop(ifname); if (can_set_bitrate_samplepoint(ifname, bitrate, sample_point) < 0) { fprintf(stderr, "Failed to set bitrate with sample point\n"); return -1; } can_do_start(ifname); printf("Bitrate: %u bps, Sample point: %u.%u%%\n", bitrate, sample_point / 10, sample_point % 10); return 0; } /* Expected output: Bitrate: 500000 bps, Sample point: 87.5% */ ``` -------------------------------- ### Set CAN Interface Bitrate using libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Configures the bitrate for a CAN interface, ranging from 1 kbit/s to 1 Mbit/s. The kernel calculates optimal bit timing parameters. This function requires the kernel to have CONFIG_CAN_CALC_BITTIMING enabled. The interface must be stopped before setting the bitrate and restarted afterward. ```c #include #include int main(void) { const char *ifname = "can0"; __u32 bitrate = 500000; /* 500 kbit/s */ /* Stop interface before configuration */ can_do_stop(ifname); /* Set the bitrate */ if (can_set_bitrate(ifname, bitrate) < 0) { fprintf(stderr, "Failed to set bitrate on %s\n", ifname); return -1; } /* Start interface with new configuration */ if (can_do_start(ifname) < 0) { fprintf(stderr, "Failed to start %s\n", ifname); return -1; } printf("Bitrate set to %u bps on %s\n", bitrate, ifname); return 0; } /* Expected output: Bitrate set to 500000 bps on can0 */ ``` -------------------------------- ### Set Manual Bit Timing Parameters (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt Sets bit timing parameters manually for expert users using the `can_set_bittiming` function. This provides precise control over timing values. It requires the `` header and takes the interface name and a `can_bittiming` struct as input. ```c #include #include #include int main(void) { const char *ifname = "can0"; struct can_bittiming bt; memset(&bt, 0, sizeof(bt)); bt.tq = 125; /* Time quanta in nanoseconds */ bt.prop_seg = 6; /* Propagation segment in TQs */ bt.phase_seg1 = 7; /* Phase segment 1 in TQs */ bt.phase_seg2 = 2; /* Phase segment 2 in TQs */ bt.sjw = 1; /* Synchronization jump width */ can_do_stop(ifname); if (can_set_bittiming(ifname, &bt) < 0) { fprintf(stderr, "Failed to set bit timing\n"); return -1; } can_do_start(ifname); printf("Custom bit timing configured on %s\n", ifname); return 0; } ``` -------------------------------- ### Configure CAN Interface with libsocketcan (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt This C function configures a specified CAN interface with a given bitrate, enables auto-restart, and sets up bus error reporting. It stops the interface, applies settings, and then restarts it. Dependencies include libsocketcan, string.h, and stdio.h. It returns 0 on success and -1 on failure. Requires root privileges for interface modification. ```c #include #include #include int configure_can_interface(const char *ifname, __u32 bitrate) { struct can_ctrlmode cm; int state; /* Stop interface first */ printf("Stopping %s...\n", ifname); if (can_do_stop(ifname) < 0) { fprintf(stderr, "Warning: Could not stop %s (may already be stopped)\n", ifname); } /* Set bitrate */ printf("Setting bitrate to %u bps...\n", bitrate); if (can_set_bitrate(ifname, bitrate) < 0) { fprintf(stderr, "Failed to set bitrate\n"); return -1; } /* Configure auto-restart (100ms) */ printf("Enabling auto-restart (100ms)..."); if (can_set_restart_ms(ifname, 100) < 0) { fprintf(stderr, "Failed to set auto-restart\n"); return -1; } /* Enable bus error reporting */ memset(&cm, 0, sizeof(cm)); cm.mask = CAN_CTRLMODE_BERR_REPORTING; cm.flags = CAN_CTRLMODE_BERR_REPORTING; printf("Enabling bus error reporting..."); if (can_set_ctrlmode(ifname, &cm) < 0) { fprintf(stderr, "Failed to set control mode\n"); return -1; } /* Start interface */ printf("Starting %s...\n", ifname); if (can_do_start(ifname) < 0) { fprintf(stderr, "Failed to start %s\n", ifname); return -1; } /* Verify state */ if (can_get_state(ifname, &state) == 0) { printf("Interface %s is now in state: %d\n", ifname, state); } printf("Configuration complete!\n"); return 0; } int main(void) { return configure_can_interface("can0", 500000); } ``` -------------------------------- ### Interface Control Functions Source: https://context7.com/linux-can/libsocketcan/llms.txt Functions for managing the state of CAN interfaces. ```APIDOC ## can_do_start - Start CAN Interface ### Description Brings up a CAN interface by changing its state to UP. All initialization is handled by the kernel. The interface must be properly configured (bitrate, etc.) before starting. ### Method `can_do_start` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include int main(void) { const char *ifname = "can0"; /* Start the CAN interface */ if (can_do_start(ifname) < 0) { fprintf(stderr, "Failed to start %s\n", ifname); return -1; } printf("Interface %s started successfully\n", ifname); return 0; } ``` ### Response #### Success Response (0) Returns 0 on success. #### Error Response (-1) Returns -1 on failure. #### Response Example `Interface can0 started successfully` ``` ```APIDOC ## can_do_stop - Stop CAN Interface ### Description Stops a running CAN interface by changing its state to DOWN. Any ongoing communication will be terminated. ### Method `can_do_stop` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include int main(void) { const char *ifname = "can0"; /* Stop the CAN interface */ if (can_do_stop(ifname) < 0) { fprintf(stderr, "Failed to stop %s\n", ifname); return -1; } printf("Interface %s stopped successfully\n", ifname); return 0; } ``` ### Response #### Success Response (0) Returns 0 on success. #### Error Response (-1) Returns -1 on failure. #### Response Example `Interface can0 stopped successfully` ``` ```APIDOC ## can_do_restart - Restart CAN Interface from Bus-Off ### Description Triggers a manual restart of a CAN interface that is in BUS_OFF state. This only works when auto-restart is disabled (restart_ms == 0) and the device is actually in BUS_OFF state. ### Method `can_do_restart` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include int main(void) { const char *ifname = "can0"; int state; /* Check current state first */ if (can_get_state(ifname, &state) < 0) { fprintf(stderr, "Failed to get state\n"); return -1; } if (state != CAN_STATE_BUS_OFF) { printf("Interface not in BUS_OFF state, no restart needed\n"); return 0; } /* Restart the interface */ if (can_do_restart(ifname) < 0) { fprintf(stderr, "Failed to restart %s\n", ifname); return -1; } printf("Interface %s restarted successfully\n", ifname); return 0; } ``` ### Response #### Success Response (0) Returns 0 on success. #### Error Response (-1) Returns -1 on failure. ``` -------------------------------- ### Set CAN FD Bit Timing (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt Configures bit timing for CAN FD mode using `can_set_canfd_bittiming`. It sets both arbitration and data phase timing parameters and automatically enables CAN FD control mode. Dependencies include ``, ``, and ``. ```c #include #include #include int main(void) { const char *ifname = "can0"; struct can_bittiming bt, dbt; /* Arbitration phase timing (500 kbit/s) */ memset(&bt, 0, sizeof(bt)); bt.bitrate = 500000; /* Data phase timing (2 Mbit/s) */ memset(&dbt, 0, sizeof(dbt)); dbt.bitrate = 2000000; can_do_stop(ifname); if (can_set_canfd_bittiming(ifname, &bt, &dbt) < 0) { fprintf(stderr, "Failed to set CAN FD bit timing\n"); return -1; } can_do_start(ifname); printf("CAN FD configured: Arbitration %u bps, Data %u bps\n", bt.bitrate, dbt.bitrate); return 0; } /* Expected output: CAN FD configured: Arbitration 500000 bps, Data 2000000 bps */ ``` -------------------------------- ### Set Controller Mode (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt Configures CAN controller operating modes such as loopback, listen-only, triple sampling, one-shot, bus-error reporting, CAN FD, and presume ACK modes using `can_set_ctrlmode`. It takes the interface name and a `can_ctrlmode` struct, which includes a mask and flags to specify the desired modes. Dependencies: ``, ``, ``. ```c #include #include #include int main(void) { const char *ifname = "can0"; struct can_ctrlmode cm; memset(&cm, 0, sizeof(cm)); /* Enable loopback and listen-only modes */ cm.mask = CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY; cm.flags = CAN_CTRLMODE_LOOPBACK; /* Enable loopback, disable listen-only */ can_do_stop(ifname); if (can_set_ctrlmode(ifname, &cm) < 0) { fprintf(stderr, "Failed to set control mode\n"); return -1; } can_do_start(ifname); printf("Loopback mode enabled on %s\n", ifname); return 0; } /* Available control modes: * CAN_CTRLMODE_LOOPBACK 0x01 - Loopback mode * CAN_CTRLMODE_LISTENONLY 0x02 - Listen-only mode * CAN_CTRLMODE_3_SAMPLES 0x04 - Triple sampling mode * CAN_CTRLMODE_ONE_SHOT 0x08 - One-Shot mode * CAN_CTRLMODE_BERR_REPORTING 0x10 - Bus-error reporting * CAN_CTRLMODE_FD 0x20 - CAN FD mode * CAN_CTRLMODE_PRESUME_ACK 0x40 - Ignore missing CAN ACKs */ ``` -------------------------------- ### Set Auto-Restart Interval (C) Source: https://context7.com/linux-can/libsocketcan/llms.txt Configures the automatic restart interval for bus-off recovery using `can_set_restart_ms`. A value of 0 disables auto-restart, requiring manual intervention. The function takes the interface name and the restart interval in milliseconds. Dependencies: ``, ``. ```c #include #include int main(void) { const char *ifname = "can0"; __u32 restart_ms = 100; /* Auto-restart after 100ms in bus-off */ if (can_set_restart_ms(ifname, restart_ms) < 0) { fprintf(stderr, "Failed to set restart interval\n"); return -1; } printf("Auto-restart interval set to %u ms on %s\n", restart_ms, ifname); /* Disable auto-restart */ if (can_set_restart_ms(ifname, 0) < 0) { fprintf(stderr, "Failed to disable auto-restart\n"); return -1; } printf("Auto-restart disabled on %s\n", ifname); return 0; } ``` -------------------------------- ### Restart BUS-OFF CAN Interface using libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Manually restarts a CAN interface that has entered the BUS_OFF state. This function is effective only when automatic restart is disabled and the device is confirmed to be in BUS_OFF. It returns an error if the restart operation fails. ```c #include #include int main(void) { const char *ifname = "can0"; int state; /* Check current state first */ if (can_get_state(ifname, &state) < 0) { fprintf(stderr, "Failed to get state\n"); return -1; } if (state != CAN_STATE_BUS_OFF) { printf("Interface not in BUS_OFF state, no restart needed\n"); return 0; } /* Restart the interface */ if (can_do_restart(ifname) < 0) { fprintf(stderr, "Failed to restart %s\n", ifname); return -1; } printf("Interface %s restarted successfully\n", ifname); return 0; } ``` -------------------------------- ### Stop CAN Interface using libsocketcan Source: https://context7.com/linux-can/libsocketcan/llms.txt Stops a running CAN interface by setting its state to DOWN. This action terminates any ongoing communication on the specified interface. An error is returned if the interface cannot be stopped. ```c #include #include int main(void) { const char *ifname = "can0"; /* Stop the CAN interface */ if (can_do_stop(ifname) < 0) { fprintf(stderr, "Failed to stop %s\n", ifname); return -1; } printf("Interface %s stopped successfully\n", ifname); return 0; } /* Expected output: Interface can0 stopped successfully */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.