AGHAF  4.5.1
Annecy electronique Generic Hardware Access Framework
LIN_Sporadic/main.cpp

Sporadic example

Schedule table contains only one sporadic frame.
The sporadic frame is associatted with unconditional frames 0x22, 0x23 and 0x24.

#include <vector>
#include <iostream>
#include <thread>
#include <array>
#define AGHAF_DYNAMIC_LIBRARY_INIT
#include <aghaf.h>
#include <iostream>
#include <iomanip>
void onLinMasterTx(
uint64_t timestamp,
uint8_t fid,
uint8_t datalen,
uint8_t const* data,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "datalen: " << static_cast<int>(datalen) << std::endl;
std::cout << "fid: " << static_cast<int>(fid) << std::endl;
if (data != nullptr) {
std::cout << "0x";
for (uint8_t i = 0; i < datalen; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(data[i]);
}
std::cout << std::endl;
} else {
std::cout << "no data" << std::endl;
}
}
void onLinMasterRx(
uint64_t timestamp,
uint8_t fid,
uint8_t datalen,
uint8_t const* data,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "datalen: " << static_cast<int>(datalen) << std::endl;
std::cout << "fid: " << static_cast<int>(fid) << std::endl;
std::cout << "0x";
for (uint8_t i = 0; i < datalen; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(data[i]);
}
std::cout << std::endl;
}
void onLinMasterError(
uint64_t timestamp,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "error: " << static_cast<int>(error) << std::endl;
}
void onLinSlaveTx(
uint64_t timestamp,
uint8_t fid,
uint8_t datalen,
uint8_t const* data,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "datalen: " << static_cast<int>(datalen) << std::endl;
std::cout << "fid: " << static_cast<int>(fid) << std::endl;
if (data != nullptr) {
std::cout << "0x";
for (uint8_t i = 0; i < datalen; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(data[i]);
}
std::cout << std::endl;
} else {
std::cout << "no data" << std::endl;
}
}
void onLinSlaveRx(
uint64_t timestamp,
uint8_t fid,
uint8_t datalen,
uint8_t const* data,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "datalen: " << static_cast<int>(datalen) << std::endl;
std::cout << "fid: " << static_cast<int>(fid) << std::endl;
std::cout << "0x";
for (uint8_t i = 0; i < datalen; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(data[i]);
}
std::cout << std::endl;
}
void onLinSlaveError(
uint64_t timestamp,
void *userContext)
{
(void) timestamp;
(void) userContext;
std::cout << __FUNCTION__ << std::endl;
std::cout << "error: " << static_cast<int>(error) << std::endl;
}
int main()
{
#ifdef _WIN32
AGHAF_loadLibrary(AGHAF_DEFAULT_FILENAME);
#endif
uint32_t count = AGHAF_getDeviceCount();
if (count > 0) {
AGHAF_DeviceInfo *devicesInfo = nullptr;
uint32_t deviceInfoNumber = 0;
AGHAF_getDeviceList(&devicesInfo, &deviceInfoNumber);
if (deviceInfoNumber > 1) {
devicesInfo[0].productNo,
devicesInfo[0].serialNo);
devicesInfo[1].productNo,
devicesInfo[1].serialNo);
AGHAF_openDevice(deviceMaster);
AGHAF_openDevice(deviceSlave);
uint8_t linCount1 = AGHAF_LIN_getCount(deviceMaster);
uint8_t linCount2 = AGHAF_LIN_getCount(deviceSlave);
if (linCount1 > 0 && linCount2 > 0) {
AGHAF_LIN linMaster = AGHAF_LIN_get(deviceMaster, 0);
AGHAF_LIN_setOnMessageRxCallback(linMaster, &onLinMasterRx, nullptr);
AGHAF_LIN_setOnMessageTxCallback(linMaster, &onLinMasterTx, nullptr);
AGHAF_LIN_setOnErrorCallback(linMaster, &onLinMasterError, nullptr);
AGHAF_LIN_setParam(linMaster,
19200);
AGHAF_LIN_setParam(linMaster,
AGHAF_LIN linSlave = AGHAF_LIN_get(deviceSlave, 0);
AGHAF_LIN_setOnMessageRxCallback(linSlave, &onLinSlaveRx, nullptr);
AGHAF_LIN_setOnMessageTxCallback(linSlave, &onLinSlaveTx, nullptr);
AGHAF_LIN_setOnErrorCallback(linSlave, &onLinSlaveError, nullptr);
19200);
std::array<uint8_t, 3> spos = { 0x22, 0x23, 0x24 };
std::array<uint8_t, 4> spoFrame22 = { 0x00, 0x01, 0x02, 0x03 };
std::array<uint8_t, 5> spoFrame23 = { 0x00, 0x01, 0x02, 0x03, 0x04 };
std::array<uint8_t, 6> spoFrame24 = { 0x00, 0x01, 0x02, 0x03, 0x05, 0x06 };
AGHAF_LIN_SCHEDULE_addSporadic(linMaster, 0, 2, static_cast<uint8_t>(spos.size()), spos.data());
linMaster,
0x22,
0,
static_cast<uint8_t>(spoFrame22.size()),
spoFrame22.data());
linSlave,
0x22,
0,
static_cast<uint8_t>(spoFrame22.size()),
nullptr);
linMaster,
0x23,
0,
static_cast<uint8_t>(spoFrame23.size()),
spoFrame23.data());
linSlave,
0x23,
0,
static_cast<uint8_t>(spoFrame23.size()),
nullptr);
linMaster,
0x24,
0,
static_cast<uint8_t>(spoFrame24.size()),
spoFrame24.data());
linSlave,
0x24,
0,
static_cast<uint8_t>(spoFrame24.size()),
nullptr);
AGHAF_LIN_activate(linMaster);
AGHAF_LIN_activate(linSlave);
AGHAF_LIN_SCHEDULE_start(linMaster, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
spoFrame22[0] = 0x0A;
AGHAF_LIN_updateFrame(linMaster, 0x22, static_cast<uint8_t>(spoFrame22.size()), spoFrame22.data());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
spoFrame23[1] = 0x0B;
AGHAF_LIN_updateFrame(linMaster, 0x23, static_cast<uint8_t>(spoFrame23.size()), spoFrame23.data());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
spoFrame24[2] = 0x0C;
AGHAF_LIN_updateFrame(linMaster, 0x24, static_cast<uint8_t>(spoFrame24.size()), spoFrame24.data());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
spoFrame22[3] = 0x0D;
spoFrame23[3] = 0x0D;
spoFrame24[3] = 0x0D;
AGHAF_LIN_updateFrame(linMaster, 0x24, static_cast<uint8_t>(spoFrame24.size()), spoFrame24.data());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AGHAF_LIN_updateFrame(linMaster, 0x22, static_cast<uint8_t>(spoFrame22.size()), spoFrame22.data());
AGHAF_LIN_updateFrame(linMaster, 0x23, static_cast<uint8_t>(spoFrame23.size()), spoFrame23.data());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
AGHAF_closeDevice(deviceMaster);
AGHAF_closeDevice(deviceSlave);
}
AGHAF_freeDeviceList(devicesInfo);
}
#ifdef _WIN32
AGHAF_unloadLibrary();
#endif
return 0;
}
AGHAF_LIN_Error
enumerator to describe the error on the bus
Definition: aghaf_lin_enums.h:27
@ AGHAF_LIN_MODE_MASTER
LIN master.
Definition: aghaf_lin_enums.h:63
@ AGHAF_LIN_MODE_SLAVE
LIN slave.
Definition: aghaf_lin_enums.h:62
@ AGHAF_LIN_FRAME_KIND_SUBSCRIBE
Receive the response.
Definition: aghaf_lin_enums.h:70
@ AGHAF_LIN_FRAME_KIND_PUBLISH
Transmits the response.
Definition: aghaf_lin_enums.h:69
@ AGHAF_LIN_SCHEDULE_KIND_LOOP
Schedule table run in loop.
Definition: aghaf_lin_enums.h:76
@ AGHAF_LIN_PARAM_BAUDRATE
Definition: aghaf_lin_enums.h:100
@ AGHAF_LIN_PARAM_MODE
Definition: aghaf_lin_enums.h:94
void AGHAF_LIN_setOnMessageTxCallback(AGHAF_LIN lin, AGHAF_LIN_onMessageTx callback, void *userContext)
Set the callback called on tx event.
Definition: aghaf_lin.cpp:15
AGHAF_Status AGHAF_LIN_SCHEDULE_configure(AGHAF_LIN lin, uint8_t scheduleIndex, AGHAF_LIN_ScheduleKind kind)
AGHAF_LIN_SCHEDULE_configure.
Definition: aghaf_lin.cpp:310
void * AGHAF_LIN
handle on a LIN bus
Definition: aghaf_lin.h:21
AGHAF_Status AGHAF_LIN_activate(AGHAF_LIN lin)
Start the communication with the LIN.
Definition: aghaf_lin.cpp:108
AGHAF_Status AGHAF_LIN_deactivate(AGHAF_LIN lin)
Stop the communication with the LIN.
Definition: aghaf_lin.cpp:119
AGHAF_Status AGHAF_LIN_SCHEDULE_addSporadic(AGHAF_LIN lin, uint8_t scheduleIndex, uint8_t timeBaseCount, uint8_t nbSpo, uint8_t *idList)
Add a sporadic frame in a schedule.
Definition: aghaf_lin.cpp:269
AGHAF_Status AGHAF_LIN_SCHEDULE_stop(AGHAF_LIN lin)
Stop the schedule currently executed on the LIN.
Definition: aghaf_lin.cpp:334
AGHAF_Status AGHAF_LIN_configureFrame(AGHAF_LIN lin, uint8_t fid, AGHAF_LIN_FrameKind kind, uint8_t publishFlags, uint8_t dataLen, uint8_t const *initialData)
Configure the frame associated with the frame identifier.
Definition: aghaf_lin.cpp:211
void AGHAF_LIN_setOnMessageRxCallback(AGHAF_LIN lin, AGHAF_LIN_onMessageRx callback, void *userContext)
Set the callback called on rx event.
Definition: aghaf_lin.cpp:27
AGHAF_LIN AGHAF_LIN_get(AGHAF_Device device, uint8_t index)
Return the LIN bus at the index specified.
Definition: aghaf_lin.cpp:75
AGHAF_Status AGHAF_LIN_SCHEDULE_start(AGHAF_LIN lin, uint8_t scheduleIndex)
Start a schedule.
Definition: aghaf_lin.cpp:323
AGHAF_Status AGHAF_LIN_setParam(AGHAF_LIN lin, AGHAF_LIN_Param param, uint32_t value)
Set a parameter from a LIN.
Definition: aghaf_lin.cpp:160
AGHAF_Status AGHAF_LIN_updateFrame(AGHAF_LIN lin, uint8_t fid, uint8_t dataLen, uint8_t const *data)
Update the datas associated to a frame identifier.
Definition: aghaf_lin.cpp:190
void AGHAF_LIN_setOnErrorCallback(AGHAF_LIN lin, AGHAF_LIN_onError callback, void *userContext)
Set the callback called on an error event.
Definition: aghaf_lin.cpp:39
uint8_t AGHAF_LIN_getCount(AGHAF_Device device)
Return the number of LINs available on a device.
Definition: aghaf_lin.cpp:97
uint32_t AGHAF_getDeviceCount()
Return the number of devices onnected.
Definition: aghaf_global.cpp:111
AGHAF_Status AGHAF_getDeviceList(AGHAF_DeviceInfo **devices, uint32_t *size)
Provide the list of devices connected.
Definition: aghaf_global.cpp:123
AGHAF_Device AGHAF_getDeviceBySN(const char *productNumber, const char *serialNumber)
Return the handle on the specified device.
Definition: aghaf_global.cpp:156
void AGHAF_freeDeviceList(AGHAF_DeviceInfo *devices)
Free the memory allocated with AGHAF_getDeviceList.
Definition: aghaf_global.cpp:135
void * AGHAF_Device
handle on an exxotest device
Definition: aghaf_global.h:145
AGHAF_Status AGHAF_closeDevice(AGHAF_Device device)
close the device
Definition: aghaf_global.cpp:266
AGHAF_Status AGHAF_openDevice(AGHAF_Device device)
Open a device.
Definition: aghaf_global.cpp:255
structure containing the informtions about a device
Definition: aghaf_global.h:188