Snapdragon® Telematics Application Framework (TelAF) Interface Specification
CAN Service

API Reference


A controller area network (CAN) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other. It is a high-integrity serial bus system for networking intelligent devices and has emerged as the standard in-vehicle network.

IPC interfaces binding

The functions of this API are provided by the tafCanSvc application service.

The following example illustrates how to bind to the CAN service.

bindings:
{
     clientExe.clientComponent.taf_can -> tafCanSvc.taf_can
}

CAN API

Create CAN interface and CAN frame

  • taf_can_CreateCanInf() – Creates a CAN interface by opening a CAN socket and binding it to the CAN interface using the CAN_RAW protocol and CAN interface name as a parameter. CAN_BCM protocol is not supported.
  • taf_can_CreateCanFrame() – Creates a CAN frame using the CAN interface reference and frame ID as parameters to send.
taf_can_InfProtocol_t canInfType = TAF_CAN_RAW_SOCK;
char* infNamePtr = "can0";
taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf(infNamePtr, canInfType);
if (canInfRef != NULL)
{
     uint32_t frameId = 0x123;
     taf_can_CanFrameRef_t frameRef = taf_can_CreateCanFrame(canInfRef, frameId);
     if(frameRef != NULL)
     {
         //Process after succesfully creating Can frame
     }
}

Set filter

Sets filter to receive CAN frame for the given frame ID. Applications can set filters with taf_can_SetFilter().

In the example code snippet, the filter is set to receive CAN frames for frame ID "0x123" in the requested CanInfRef.

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
uint32_t frameId = 0x123;
if(taf_can_SetFilter(canInfRef, frameId) == LE_OK)
{
  //Process after succesfully setting receive filter
}

Enable or disable loopback

If local loopback is enabled, the messages sent from the given interface (can0/can1/..) are looped back to the same CAN interface, so all applications bound with the given CAN interface can receive the message.

Applications can enable or disable loopback with the taf_can_EnableLoopback() or taf_can_DisableLoopback() functions. To meet multi-user needs, local loopback is enabled by default.

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
if(taf_can_EnableLoopback(canInfRef) == LE_OK)
{
  //Process after succesfully enabling the loopback
}

if(taf_can_DisableLoopback(canInfRef) == LE_OK)
{
  //Process after succesfully disabling the loopback
}

Enable or disable reception of own sent message

When receiving messages sent from the same CanInterface reference, Reception of the application's own sent CAN frame is usually unwanted, therefore it is disabled by default.

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
if(taf_can_EnableRcvOwnMsg(canInfRef) == LE_OK)
{
  //Process after succesfully enabling the reception of own sent CAN frame
}

if(taf_can_DisableRcvOwnMsg(canInfRef) == LE_OK)
{
  //Process after succesfully disabling the reception of own sent CAN frame
}

CAN FD frame

The new CAN FD capable CAN controllers support two different bitrates for the arbitration phase and the payload phase of the CAN FD frame and up to 64 bytes of payload.

  • taf_can_IsFdSupported() – Checks whether the device supports FD frames or not.
  • taf_can_EnableFdFrame() – If the device supports CAN FD frames then it needs to enable it because it is off by default. Once CAN-FD frame is enabled, the application can send both CAN frames and CAN FD frames.
  • taf_can_GetFdStatus() – Checks whether the CAN FD frame is enabled or not.
taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
if(taf_can_IsFdSupported(canInfRef) == true)
{
  //Process if device support FD Frame type.
}

if(taf_can_EnableFdFrame(canInfRef) == LE_OK)
{
  //Process after succesfully enabling the CAN FD frame
}

if(taf_can_GetFdStatus(canInfRef) == true)
{
  //Process successful if CAN FD frame is enabled
}

Receive CAN frame

The handler can be managed with the taf_can_AddCanEventHandler() and taf_can_RemoveCanEventHandler() functions.

To receive CAN frame when <received_can_id> & frIdMask == <frameId> & frIdMask, register a handler function using taf_can_AddCanEventHandler().

static void CanHandlerFunction
(
     taf_can_CanInterfaceRef_t canInfRef,
     bool isCanFdFrame,
     uint32_t frameId,
     const uint8_t* dataPtr,
     void* contextPtr
)
{
     // Process after succesfully receiving message
}

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
taf_can_CanEventHandlerRef_t handlerRef = NULL;
handlerRef = taf_can_AddCanEventHandler(canInfRef, frameId, frIdMask, CanHandlerFunction, NULL);

To remove the handler function, call taf_can_RemoveCanEventHandler().

taf_can_RemoveCanEventHandler(handlerRef);

Create and send CAN frame

taf_can_SetPayload() – Sets data up to 64 bytes in hexadecimal form.

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
uint32_t frameId = 0x123;
taf_can_CanFrameRef_t frameRef = taf_can_CreateCanFrame(canInfRef, frameId);
uint8_t dataPtr[TAF_CAN_DATA_MAX_LENGTH] = 0x1234ab34;
if(taf_can_SetPayload(frameRef, dataPtr) == LE_OK)
{
  //Process after succesfully setting the data
}

taf_can_SetFrameType() – Sets CAN frame type to send.

  • CAN_FRAME – Sets legacy CAN frame type to send; supports data up to 8 bytes.
  • CAN_FD_FRAME – Sets CAN-FD frame type to send; supports data up to 64 bytes.
  • AUTO_FRAME – Sets auto frame type to send with legacy CAN or CAN-FD frame type based on device capability.

The application sets CAN frame type with the taf_can_SetFrameType().

taf_can_FrameType_t frameType = TAF_CAN_CAN_FRAME;
if(taf_can_SetFrameType(frameRef, frameType) == LE_OK)
{
  //Process after succesfully setting frame type
}

taf_can_SendFrame() – Sends frame of any CAN frame type. Before sending CAN frame, sets the frame type using taf_can_SetFrameType() and payload using taf_can_SetPayload().

if(taf_can_SendFrame(frameRef) == LE_OK)
{
  //Process after succesfully sending CAN frame
}

Delete CAN interface

The application can close the opened socket and deallocate the allocated memory for a created CAN interface with the taf_can_DeleteCanInf() function.

taf_can_CanInterfaceRef_t canInfRef = taf_can_CreateCanInf("can0", TAF_CAN_RAW_SOCK);
if(taf_can_DeleteCanInf(canInfRef) == LE_OK)
{
  //Process after succesfully deleting the created CAN interface
}

Delete CAN frame

The application can deallocate the allocated memory for a created CAN frame using the taf_can_DeleteCanFrame() function.

if(taf_can_DeleteCanFrame(frameRef) == LE_OK)
{
  //Process after succesfully deleting the created CAN frame
}