Skip to content

Commit

Permalink
🍺 Extend Sample to RoutingActivation
Browse files Browse the repository at this point in the history
  • Loading branch information
langroodi committed Aug 20, 2023
1 parent 5acfd48 commit 579c273
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 24 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ if(build_samples)
add_executable(
doip_lib_sample
${sample_dir}/vehicle_id_message_handler.cpp
${sample_dir}/routing_activation_handler.cpp
${sample_dir}/message_handling_sample.cpp
)

Expand Down
124 changes: 100 additions & 24 deletions samples/message_handling_sample.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include <utility>
#include <iostream>
#include "./vehicle_id_message_handler.h"
#include "./routing_activation_handler.h"
#include "../include/doiplib/doip_controller.h"

void handleMessage(
constexpr std::size_t cIdSize{6};
const uint8_t cProtocolVersion{0x02};
const std::array<uint8_t, cIdSize> cEid{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
// Use EID as GID
const std::array<uint8_t, cIdSize> cGid{cEid};

bool handleMessage(
const DoipLib::DoipController &doipController,
const DoipLib::EidVehicleIdRequest &request)
const DoipLib::Message &request,
DoipLib::Message &response)
{
std::vector<uint8_t> _serializedRequest;
request.Serialize(_serializedRequest);
Expand All @@ -17,15 +25,13 @@ void handleMessage(

if (_isHandled)
{
DoipLib::VehicleIdResponse _response;
DoipLib::GenericNackType _nackCode;
bool _isDeserialized{
_response.TryDeserialize(_serializedResponse, _nackCode)};
response.TryDeserialize(_serializedResponse, _nackCode)};

if (_isDeserialized)
{
std::cout << "VIN: " << _response.GetVin() << std::endl
<< "LogicalAddress: " << _response.GetLogicalAddress() << std::endl;
return true;
}
else
{
Expand All @@ -36,44 +42,114 @@ void handleMessage(
{
std::cout << "Request is NOT handled correctly!\n";
}

return false;
}

int main()
void handleVehicleIdMessage(const DoipLib::DoipController &doipController)
{
const uint16_t cLogicalAddress{0x0001};
const std::size_t cIdSize{6};
const std::array<uint8_t, cIdSize> cValidEid{
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
// Use EID as GID
const std::array<uint8_t, cIdSize> cGid{cValidEid};
const std::array<uint8_t, cIdSize> cInvalidEid{
0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0};

DoipLib::VehicleIdResponse _response;

const DoipLib::EidVehicleIdRequest cValidRequest(
cProtocolVersion, cEid);

if (handleMessage(doipController, cValidRequest, _response))
{
std::cout << "VIN: " << _response.GetVin() << std::endl
<< "LogicalAddress: " << _response.GetLogicalAddress() << std::endl;
}

const DoipLib::EidVehicleIdRequest cInvalidRequest(
cProtocolVersion, cInvalidEid);

handleMessage(doipController, cInvalidRequest, _response);
}

void printResponseCode(DoipLib::RoutingActivationResponseType responseCode)
{
switch (responseCode)
{
case DoipLib::RoutingActivationResponseType::Successful:
std::cout << "Routing is activated successfully." << std::endl;
break;

case DoipLib::RoutingActivationResponseType::AlreadyRegisteredTester:
std::cout << "Tester is already registered." << std::endl;
break;

case DoipLib::RoutingActivationResponseType::UnsupportedActivationType:
std::cout << "Activation type is not supported." << std::endl;
break;

default:
std::cout << "Response code is out of range." << std::endl;
break;
}
}

void handleRoutingActivation(const DoipLib::DoipController &doipController)
{
const uint16_t cTesterAddress{0x0002};
const uint8_t cDefaultActivation{0x00};
const uint8_t cWwhObdActivation{0x01};

DoipLib::RoutingActivationResponse _response;

DoipLib::RoutingActivationRequest cDefaultRequest(
cProtocolVersion, cTesterAddress, cDefaultActivation);
if (handleMessage(doipController, cDefaultRequest, _response))
{
printResponseCode(_response.GetResponseCode());
}

if (handleMessage(doipController, cDefaultRequest, _response))
{
printResponseCode(_response.GetResponseCode());
}

DoipLib::RoutingActivationRequest cWwhObdRequest(
cProtocolVersion, cTesterAddress, cWwhObdActivation);
if (handleMessage(doipController, cWwhObdRequest, _response))
{
printResponseCode(_response.GetResponseCode());
}
}

int main()
{
const uint16_t cLogicalAddress{0x0001};

DoipLib::ControllerConfig _config;
_config.protocolVersion = 0x02;
_config.protocolVersion = cProtocolVersion;
_config.doipMaxRequestBytes = 64;
_config.doIPInitialVehicleAnnouncementTime = std::chrono::seconds(0);
_config.doIPVehicleAnnouncementCount = 3;
_config.doIPVehicleAnnouncementInterval = std::chrono::seconds(1);

DoipLib::VehicleIdMessageHandler _messageHandler(
DoipLib::DoipController _doipController(std::move(_config));

DoipLib::VehicleIdMessageHandler _vehicleIdMessageHandler(
_config.protocolVersion,
"ABCDEFGHIJKLMNOPQ",
cLogicalAddress,
cValidEid,
cEid,
cGid);

DoipLib::DoipController _doipController(std::move(_config));
_doipController.Register(
DoipLib::PayloadType::VehicleIdRequestWithEid, &_messageHandler);
DoipLib::PayloadType::VehicleIdRequestWithEid, &_vehicleIdMessageHandler);

DoipLib::EidVehicleIdRequest _validRequest(
_config.protocolVersion, cValidEid);
handleMessage(_doipController, _validRequest);
DoipLib::RoutingActivationHandler _routingActivationHandler(
_config.protocolVersion,
cLogicalAddress);

_doipController.Register(
DoipLib::PayloadType::RoutingActivationRequest, &_routingActivationHandler);

DoipLib::EidVehicleIdRequest _invalidRequest(
_config.protocolVersion, cInvalidEid);
handleMessage(_doipController, _invalidRequest);
handleVehicleIdMessage(_doipController);
handleRoutingActivation(_doipController);

return 0;
}
58 changes: 58 additions & 0 deletions samples/routing_activation_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <utility>
#include "./routing_activation_handler.h"

namespace DoipLib
{
const uint8_t RoutingActivationHandler::cSupportedActivationType;
const bool RoutingActivationHandler::cHasOemSpecificData;

RoutingActivationHandler::RoutingActivationHandler(
uint8_t protocolVersion,
uint16_t logicalAddress) : cProtocolVersion{protocolVersion}, cLogicalAddress{logicalAddress}
{
}

Message *RoutingActivationHandler::GetMessage()
{
return static_cast<Message *>(&mRequest);
}

bool RoutingActivationHandler::TryHandle(
const Message *request, std::vector<uint8_t> &response) const
{
auto _routingActivationRequest{
dynamic_cast<const RoutingActivationRequest *>(request)};

if (_routingActivationRequest == nullptr)
{
return false;
}

const uint16_t cTesterAddress{_routingActivationRequest->GetSourceAddress()};
RoutingActivationResponseType _responseCode;

if (_routingActivationRequest->GetActivationType() != cSupportedActivationType)
{
_responseCode = RoutingActivationResponseType::UnsupportedActivationType;
}
else if (mActiveTesters.find(cTesterAddress) != mActiveTesters.end())
{
_responseCode = RoutingActivationResponseType::AlreadyRegisteredTester;
}
else
{
mActiveTesters.emplace(cTesterAddress);
_responseCode = RoutingActivationResponseType::Successful;
}

RoutingActivationResponse _routingActivationResponse(
cProtocolVersion,
cTesterAddress,
cLogicalAddress,
_responseCode);

_routingActivationResponse.Serialize(response);

return true;
}
}
36 changes: 36 additions & 0 deletions samples/routing_activation_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef ROUTING_ACTIVATION_HANDLER_H
#define ROUTING_ACTIVATION_HANDLER_H

#include<set>

#include "../include/doiplib/message_handler.h"
#include "../include/doiplib/routing_activation_request.h"
#include "../include/doiplib/routing_activation_response.h"

namespace DoipLib
{
class RoutingActivationHandler : public MessageHandler
{
private:
static const uint8_t cSupportedActivationType{0x00U};
static const bool cHasOemSpecificData{false};

const uint8_t cProtocolVersion;
const uint16_t cLogicalAddress;

mutable std::set<uint16_t> mActiveTesters;
RoutingActivationRequest mRequest;

public:
RoutingActivationHandler(
uint8_t protocolVersion,
uint16_t logicalAddress);

Message *GetMessage() override;

bool TryHandle(
const Message *request, std::vector<uint8_t> &response) const override;
};
}

#endif

0 comments on commit 579c273

Please sign in to comment.