-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
2,303 additions
and
1,862 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright (c) 2020, Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include "message.hpp" | ||
#include <modm/processing.hpp> | ||
|
||
namespace modm::amnb | ||
{ | ||
|
||
/// @ingroup modm_communication_amnb | ||
enum class Error : uint8_t | ||
{ | ||
Ok = 0, | ||
|
||
RequestTimeout, | ||
WrongArgumentSize, | ||
NoAction, | ||
}; | ||
|
||
/// @ingroup modm_communication_amnb | ||
struct Listener | ||
{ | ||
inline | ||
Listener(int command, void(*listener)(uint8_t)) | ||
: command(command), callback((void*)listener), | ||
redirect([](const Message &msg, void *cb) | ||
{ reinterpret_cast<decltype(listener)>(cb)(msg.address()); }) | ||
{} | ||
|
||
template<typename T> | ||
Listener(int command, void(*listener)(uint8_t, const T&)) | ||
: command(command), callback((void*)listener), | ||
redirect([](const Message &msg, void* cb) | ||
{ | ||
if (msg.length() == sizeof(T)) | ||
reinterpret_cast<decltype(listener)>(cb)(msg.address(), *msg.get<T>()); | ||
}) | ||
{} | ||
|
||
protected: | ||
const uint8_t command; | ||
void *const callback; | ||
void (*const redirect)(const Message &msg, void *cb); | ||
|
||
inline | ||
void call(const Message &msg) const | ||
{ | ||
redirect(msg, callback); | ||
} | ||
|
||
template< class, size_t > friend class Node; | ||
}; | ||
|
||
/// @ingroup modm_communication_amnb | ||
struct Response | ||
{ | ||
inline | ||
Response() | ||
: msg(0, 0, 0, Type::Response) {} | ||
|
||
template<typename T> | ||
Response(T retval) | ||
: msg(0, 0, sizeof(T), Type::Response) | ||
{ *msg.get<T>() = retval; } | ||
|
||
Message msg; | ||
}; | ||
|
||
/// @ingroup modm_communication_amnb | ||
template<typename T> | ||
inline Response | ||
ErrorResponse(T error) | ||
{ | ||
Response res(error); | ||
res.msg.setType(Type::UserError); | ||
return res; | ||
} | ||
|
||
/// @ingroup modm_communication_amnb | ||
struct Action | ||
{ | ||
inline | ||
Action(int command, Response(*action)()) | ||
: command(command), callback((void*)action), | ||
redirect([](const Message &msg, void *cb) -> Message | ||
{ | ||
if (msg.length() == 0) | ||
return reinterpret_cast<decltype(action)>(cb)().msg; | ||
return Response(Error::WrongArgumentSize).msg; | ||
}) | ||
{} | ||
|
||
template<typename T> | ||
Action(int command, Response(*action)(const T&)) | ||
: command(command), callback((void*)action), | ||
redirect([](const Message &msg, void* cb) -> Message | ||
{ | ||
if (msg.length() == sizeof(T)) | ||
return reinterpret_cast<decltype(action)>(cb)(*msg.get<T>()).msg; | ||
return Response(Error::WrongArgumentSize).msg; | ||
}) | ||
{} | ||
|
||
inline | ||
Action(int command, void(*action)()) | ||
: command(command), callback((void*)action), | ||
redirect([](const Message &msg, void *cb) -> Message | ||
{ | ||
if (msg.length() == 0) { | ||
reinterpret_cast<decltype(action)>(cb)(); | ||
return Response().msg; | ||
} | ||
return Response(Error::WrongArgumentSize).msg; | ||
}) | ||
{} | ||
|
||
template<typename T> | ||
Action(int command, void(*action)(const T&)) | ||
: command(command), callback((void*)action), | ||
redirect([](const Message &msg, void* cb) -> Message | ||
{ | ||
if (msg.length() == sizeof(T)) { | ||
reinterpret_cast<decltype(action)>(cb)(*msg.get<T>()); | ||
return Response().msg; | ||
} | ||
return Response(Error::WrongArgumentSize).msg; | ||
}) | ||
{} | ||
|
||
protected: | ||
const uint8_t command; | ||
void *const callback; | ||
Message (*const redirect)(const Message &msg, void *cb); | ||
|
||
inline | ||
Message call(const Message &msg) const | ||
{ | ||
return redirect(msg, callback); | ||
} | ||
|
||
template< class, size_t > friend class Node; | ||
}; | ||
|
||
} | ||
|
||
%% if with_io | ||
#include <modm/io/iostream.hpp> | ||
|
||
inline modm::IOStream& | ||
operator << (modm::IOStream& s, const modm::amnb::Error error) | ||
{ | ||
using namespace modm::amnb; | ||
switch(error) | ||
{ | ||
case Error::Ok: s << "Ok"; break; | ||
case Error::NoAction: s << "NoAction"; break; | ||
case Error::RequestTimeout: s << "RequestTimeout"; break; | ||
case Error::WrongArgumentSize: s << "WrongArgumentSize"; break; | ||
} | ||
return s; | ||
} | ||
%% endif | ||
|
Oops, something went wrong.