Skip to content

File syn_j1939.h

FileList > proto > syn_j1939.h

Go to the source code of this file

SAE J1939 Heavy-Duty Vehicle CAN Protocol Stack. More...

  • #include "../common/syn_defs.h"
  • #include "../drivers/syn_can.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_J1939_DTC
J1939 Diagnostic Trouble Code (DTC) Structure (J1939-73).
struct SYN_J1939_DTCLog
struct SYN_J1939_Header
Parsed J1939 29-bit CAN ID Header.
struct SYN_J1939_Name
64-bit J1939 Device NAME Structure (J1939-81).
struct SYN_J1939_Node
J1939 Node Control Instance.
struct SYN_J1939_TpRxSession
J1939 Transport Protocol Rx Re-assembly Buffer.

Public Types

Type Name
enum SYN_J1939_AddressState
J1939 Address Claiming States.

Public Functions

Type Name
SYN_Status syn_j1939_build_address_claim (const SYN_J1939_Node * node, SYN_CAN_Frame * frame)
Build an Address Claim CAN frame for the node.
SYN_Status syn_j1939_build_request (uint8_t sa, uint8_t da, uint32_t requested_pgn, SYN_CAN_Frame * frame)
Build a Request PGN CAN frame (PGN 59904).
SYN_Status syn_j1939_build_tp_bam (uint8_t sa, uint32_t pgn, uint16_t total_bytes, SYN_CAN_Frame * frame)
Build a Transport Protocol Broadcast Announce Message (TP.CM_BAM).
SYN_Status syn_j1939_build_tp_dt (uint8_t sa, uint8_t sequence, const uint8_t * payload, size_t payload_len, SYN_CAN_Frame * frame)
Build a Transport Protocol Data Transfer packet (TP.DT).
uint32_t syn_j1939_calc_pgn (uint8_t dp, uint8_t pf, uint8_t ps)
Compute 24-bit Parameter Group Number from Data Page, PF, and PS.
SYN_Status syn_j1939_dtc_add_active (SYN_J1939_DTCLog * log, uint32_t spn, uint8_t fmi)
Add an active Diagnostic Trouble Code (DM1).
SYN_Status syn_j1939_dtc_clear_active (SYN_J1939_DTCLog * log, uint32_t spn, uint8_t fmi)
Clear an active DTC and move it to previously active log (DM2).
void syn_j1939_dtc_clear_dm11 (SYN_J1939_DTCLog * log)
Clear all active DTCs (DM11 Request handling).
void syn_j1939_dtc_clear_dm3 (SYN_J1939_DTCLog * log)
Clear all previously active DTCs (DM3 Request handling).
void syn_j1939_dtc_log_init (SYN_J1939_DTCLog * log)
Initialize a J1939 Diagnostic Trouble Code logger.
size_t syn_j1939_encode_dm1 (uint8_t * buf, size_t buf_size, const SYN_J1939_DTC * dtc_list, size_t dtc_count, uint8_t mil_lamp_status)
Encode Active Diagnostic Trouble Codes into a DM1 buffer (PGN 65226).
size_t syn_j1939_encode_dm2 (uint8_t * buf, size_t buf_size, const SYN_J1939_DTC * dtc_list, size_t dtc_count, uint8_t mil_lamp_status)
Encode a J1939 DM2 payload (Previously Active Diagnostic Trouble Codes).
uint32_t syn_j1939_id_pack (uint8_t priority, uint32_t pgn, uint8_t sa, uint8_t da)
Pack J1939 bitfields into a 29-bit CAN Identifier.
SYN_Status syn_j1939_id_unpack (uint32_t can_id, SYN_J1939_Header * header)
Unpack a 29-bit CAN Identifier into a J1939 Header structure.
void syn_j1939_name_decode (const uint8_t buf, SYN_J1939_Name * name)
Decode an 8-byte buffer into a 64-bit J1939 NAME structure.
void syn_j1939_name_encode (const SYN_J1939_Name * name, uint8_t buf)
Encode a 64-bit J1939 NAME into an 8-byte buffer.
SYN_Status syn_j1939_node_init (SYN_J1939_Node * node, uint8_t sa, const SYN_J1939_Name * name)
Initialize a J1939 Node instance.
SYN_Status syn_j1939_process_frame (SYN_J1939_Node * node, const SYN_CAN_Frame * frame, uint32_t * out_pgn, const uint8_t ** out_data, size_t * out_len)
Process an incoming CAN frame in the J1939 node state machine.

Macros

Type Name
define SYN_J1939_MAX_LOGGED_DTCS 16

Detailed Description

Implements J1939 29-bit CAN ID parsing/packing, PGN definitions, Transport Protocol (TP.CM BAM & RTS/CTS), Address Claiming (J1939-81), and Active Diagnostic Trouble Codes (DM1 J1939-73).

Public Types Documentation

enum SYN_J1939_AddressState

J1939 Address Claiming States.

enum SYN_J1939_AddressState {
    SYN_J1939_STATE_UNCLAIMED = 0,
    SYN_J1939_STATE_CLAIMING = 1,
    SYN_J1939_STATE_CLAIMED = 2,
    SYN_J1939_STATE_CANNOT_CLAIM = 3
};


Public Functions Documentation

function syn_j1939_build_address_claim

Build an Address Claim CAN frame for the node.

SYN_Status syn_j1939_build_address_claim (
    const SYN_J1939_Node * node,
    SYN_CAN_Frame * frame
) 

Parameters:

  • node Pointer to J1939 node instance.
  • frame Output CAN frame.

Returns:

SYN_OK on success.


function syn_j1939_build_request

Build a Request PGN CAN frame (PGN 59904).

SYN_Status syn_j1939_build_request (
    uint8_t sa,
    uint8_t da,
    uint32_t requested_pgn,
    SYN_CAN_Frame * frame
) 

Parameters:

  • sa Source address sending request.
  • da Destination address requested (0xFF for broadcast).
  • requested_pgn PGN being requested.
  • frame Output CAN frame.

Returns:

SYN_OK on success.


function syn_j1939_build_tp_bam

Build a Transport Protocol Broadcast Announce Message (TP.CM_BAM).

SYN_Status syn_j1939_build_tp_bam (
    uint8_t sa,
    uint32_t pgn,
    uint16_t total_bytes,
    SYN_CAN_Frame * frame
) 

Parameters:

  • sa Source address.
  • pgn PGN of payload.
  • total_bytes Total payload length in bytes (9 to 1785).
  • frame Output CAN frame.

Returns:

SYN_OK on success.


function syn_j1939_build_tp_dt

Build a Transport Protocol Data Transfer packet (TP.DT).

SYN_Status syn_j1939_build_tp_dt (
    uint8_t sa,
    uint8_t sequence,
    const uint8_t * payload,
    size_t payload_len,
    SYN_CAN_Frame * frame
) 

Parameters:

  • sa Source address.
  • sequence Packet sequence number (1 to 255).
  • payload Pointer to payload chunk (up to 7 bytes).
  • payload_len Length of chunk (1 to 7 bytes).
  • frame Output CAN frame.

Returns:

SYN_OK on success.


function syn_j1939_calc_pgn

Compute 24-bit Parameter Group Number from Data Page, PF, and PS.

uint32_t syn_j1939_calc_pgn (
    uint8_t dp,
    uint8_t pf,
    uint8_t ps
) 

Parameters:

  • dp Data Page (0 or 1).
  • pf PDU Format (0-255).
  • ps PDU Specific (0-255).

Returns:

24-bit PGN value.


function syn_j1939_dtc_add_active

Add an active Diagnostic Trouble Code (DM1).

SYN_Status syn_j1939_dtc_add_active (
    SYN_J1939_DTCLog * log,
    uint32_t spn,
    uint8_t fmi
) 


function syn_j1939_dtc_clear_active

Clear an active DTC and move it to previously active log (DM2).

SYN_Status syn_j1939_dtc_clear_active (
    SYN_J1939_DTCLog * log,
    uint32_t spn,
    uint8_t fmi
) 


function syn_j1939_dtc_clear_dm11

Clear all active DTCs (DM11 Request handling).

void syn_j1939_dtc_clear_dm11 (
    SYN_J1939_DTCLog * log
) 


function syn_j1939_dtc_clear_dm3

Clear all previously active DTCs (DM3 Request handling).

void syn_j1939_dtc_clear_dm3 (
    SYN_J1939_DTCLog * log
) 


function syn_j1939_dtc_log_init

Initialize a J1939 Diagnostic Trouble Code logger.

void syn_j1939_dtc_log_init (
    SYN_J1939_DTCLog * log
) 


function syn_j1939_encode_dm1

Encode Active Diagnostic Trouble Codes into a DM1 buffer (PGN 65226).

size_t syn_j1939_encode_dm1 (
    uint8_t * buf,
    size_t buf_size,
    const SYN_J1939_DTC * dtc_list,
    size_t dtc_count,
    uint8_t mil_lamp_status
) 

Parameters:

  • buf Output buffer.
  • buf_size Buffer capacity.
  • dtc_list Array of DTC structures.
  • dtc_count Number of DTCs in list.
  • mil_lamp_status 2-bit MIL status.

Returns:

Total bytes written to buffer.


function syn_j1939_encode_dm2

Encode a J1939 DM2 payload (Previously Active Diagnostic Trouble Codes).

size_t syn_j1939_encode_dm2 (
    uint8_t * buf,
    size_t buf_size,
    const SYN_J1939_DTC * dtc_list,
    size_t dtc_count,
    uint8_t mil_lamp_status
) 


function syn_j1939_id_pack

Pack J1939 bitfields into a 29-bit CAN Identifier.

uint32_t syn_j1939_id_pack (
    uint8_t priority,
    uint32_t pgn,
    uint8_t sa,
    uint8_t da
) 

Parameters:

  • priority Priority (0-7, typically 6).
  • pgn Parameter Group Number (18-bit).
  • sa Source Address (0-255).
  • da Destination Address (used if PGN is PDU1 format).

Returns:

29-bit CAN ID.


function syn_j1939_id_unpack

Unpack a 29-bit CAN Identifier into a J1939 Header structure.

SYN_Status syn_j1939_id_unpack (
    uint32_t can_id,
    SYN_J1939_Header * header
) 

Parameters:

  • can_id 29-bit Extended CAN Identifier.
  • header Output structure receiving parsed fields.

Returns:

SYN_OK on success, or SYN_INVALID_PARAM if header is NULL.


function syn_j1939_name_decode

Decode an 8-byte buffer into a 64-bit J1939 NAME structure.

void syn_j1939_name_decode (
    const uint8_t buf,
    SYN_J1939_Name * name
) 

Parameters:

  • buf Source 8-byte buffer.
  • name Output NAME structure.

function syn_j1939_name_encode

Encode a 64-bit J1939 NAME into an 8-byte buffer.

void syn_j1939_name_encode (
    const SYN_J1939_Name * name,
    uint8_t buf
) 

Parameters:

  • name Source NAME structure.
  • buf Output 8-byte buffer.

function syn_j1939_node_init

Initialize a J1939 Node instance.

SYN_Status syn_j1939_node_init (
    SYN_J1939_Node * node,
    uint8_t sa,
    const SYN_J1939_Name * name
) 

Parameters:

  • node Pointer to node instance.
  • sa Preferred Source Address (0-253).
  • name Device 64-bit NAME configuration.

Returns:

SYN_OK on success.


function syn_j1939_process_frame

Process an incoming CAN frame in the J1939 node state machine.

SYN_Status syn_j1939_process_frame (
    SYN_J1939_Node * node,
    const SYN_CAN_Frame * frame,
    uint32_t * out_pgn,
    const uint8_t ** out_data,
    size_t * out_len
) 

Parameters:

  • node Pointer to node.
  • frame Incoming CAN frame.
  • out_pgn Output received PGN.
  • out_data Pointer to receiving reassembled or single-frame payload.
  • out_len Output payload length.

Returns:

SYN_OK if a complete J1939 message is ready, or SYN_BUSY if multi-packet assembly is in progress.


Macro Definition Documentation

define SYN_J1939_MAX_LOGGED_DTCS

#define SYN_J1939_MAX_LOGGED_DTCS `16`


The documentation for this class was generated from the following file src/syntropic/proto/syn_j1939.h