Skip to content

Commit

Permalink
Merge branch 'master' into bakerstu-ble-basics
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/utils/DataBuffer.cxxtest
#	src/utils/DataBuffer.hxx
  • Loading branch information
bakerstu committed Oct 19, 2024
2 parents 109a67d + 5ad34b1 commit f4ee32f
Show file tree
Hide file tree
Showing 28 changed files with 607 additions and 123 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ArduinoBuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
sketch-names: ESP32CanLoadTest.ino,ESP32IOBoard.ino,ESP32SerialBridge.ino,ESP32WifiCanBridge.ino
debug-compile: true
required-libraries: OpenMRNLite
arduino-platform: esp32:[email protected]
if: ${{ matrix.target == 'esp32' }}

- name: Compile ESP32-C3 examples
Expand All @@ -74,6 +75,7 @@ jobs:
sketch-names: ESP32C3CanLoadTest.ino,ESP32C3IOBoard.ino
debug-compile: true
required-libraries: OpenMRNLite
arduino-platform: esp32:[email protected]
if: ${{ matrix.target == 'esp32c3' }}

- name: Compile ESP32-S2 examples
Expand All @@ -84,4 +86,5 @@ jobs:
sketch-names: ESP32S2CanLoadTest.ino,ESP32S2IOBoard.ino
debug-compile: true
required-libraries: OpenMRNLite
arduino-platform: esp32:[email protected]
if: ${{ matrix.target == 'esp32s2' }}
17 changes: 17 additions & 0 deletions applications/io_board/targets/linux.x86/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ openlcb::ConfiguredProducer producer_sw2(
openlcb::RefreshLoop loop(
stack.node(), {&consumer_pulse1, &consumer_pulse2});

class FactoryResetHelper : public DefaultConfigUpdateListener
{
public:
UpdateAction apply_configuration(
int fd, bool initial_load, BarrierNotifiable *done) OVERRIDE
{
AutoNotify n(done);
return UPDATED;
}

void factory_reset(int fd) override
{
cfg.userinfo().name().write(fd, "IO Board");
cfg.userinfo().description().write(fd, "User description");
}
} reset_helper;

/** Entry point to application.
* @param argc number of command line arguments
* @param argv array of command line arguments
Expand Down
21 changes: 21 additions & 0 deletions src/executor/Dispatcher.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ TEST_F(DispatcherTest, TestMultiplehandlers)
wait();
}

TEST_F(DispatcherTest, TestFallbackHandler)
{
StrictMock<MockCanMessageHandler> h1;
f_.register_handler(&h1, 1, 0xFFUL);
StrictMock<MockCanMessageHandler> h2;
f_.register_handler(&h2, 257, 0x1FFFFFFFUL);
StrictMock<MockCanMessageHandler> hfb;
f_.register_fallback_handler(&hfb);

EXPECT_CALL(h1, handle_message(257, _));
EXPECT_CALL(h1, handle_message(1, _));
EXPECT_CALL(h2, handle_message(257, _));
send_message(257);
send_message(1);
wait();

EXPECT_CALL(hfb, handle_message(2, _));
send_message(2);
wait();
}

/*TEST_F(DispatcherTest, TestAsync)
{
StrictMock<MockCanMessageHandler> h1;
Expand Down
32 changes: 30 additions & 2 deletions src/executor/Dispatcher.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ protected:
/// is the handler to unregister from all instances.
void unregister_handler_all(UntypedHandler *handler);

/// Sets one handler to receive all messages that no other handler has
/// matched. May be called only once in the lifetime of a dispatcher
/// object. @param handler is the handler pointer for the fallback handler.
void register_fallback_handler(UntypedHandler *handler)
{
HASSERT(!fallbackHandler_);
fallbackHandler_ = handler;
}

/// Returns the current message's ID.
virtual ID get_message_id() = 0;

Expand Down Expand Up @@ -180,7 +189,10 @@ private:

protected:
/// If non-NULL we still need to call this handler.
UntypedHandler *lastHandlerToCall_;
UntypedHandler *lastHandlerToCall_{nullptr};
/// Handler to give all messages that were not matched by any other handler
/// registration.
UntypedHandler *fallbackHandler_{nullptr};
private:
/// Protects handler add / remove against iteration.
OSMutex lock_;
Expand Down Expand Up @@ -256,10 +268,19 @@ public:
}

/// Removes all instances of a handler from this dispatcher.
void unregister_handler_all(HandlerType *handler) {
void unregister_handler_all(HandlerType *handler)
{
Base::unregister_handler_all(handler);
}

/// Sets one handler to receive all messages that no other handler has
/// matched. May be called only once in the lifetime of a dispatcher
/// object. @param handler is the handler pointer for the fallback handler.
void register_fallback_handler(HandlerType *handler)
{
Base::register_fallback_handler(handler);
}

protected:
/// @return the identifier bits of the current message.
typename Base::ID get_message_id() OVERRIDE {
Expand Down Expand Up @@ -460,6 +481,13 @@ StateFlowBase::Action DispatchFlowBase<NUM_PRIO>::iteration_done()
{
send_transfer();
}
else if (fallbackHandler_)
{
// Nothing handled this message, and we have a fallbac handler
// registered. Gives the message to the fallback handler.
lastHandlerToCall_ = fallbackHandler_;
send_transfer();
}
return release_and_exit();
}

Expand Down
25 changes: 14 additions & 11 deletions src/freertos_drivers/net_cc32xx/CC32xxWiFi.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -805,18 +805,18 @@ void CC32xxWiFi::wlan_wps_pbc_initiate()
void CC32xxWiFi::wlan_setup_ap(const char *ssid, const char *security_key,
SecurityType security_type)
{
HASSERT(strlen(ssid) <= 32);
HASSERT(strlen(security_key) <= 64);

uint8_t sec_type = security_type_to_simplelink(security_type);

sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_SSID, strlen(ssid),
(uint8_t*)ssid);
if (wlanRole == WlanRole::AP)
if (ssid)
{
str_populate(this->ssid, ssid);
HASSERT(strlen(ssid) <= 32);
sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_SSID, strlen(ssid),
(uint8_t *)ssid);
if (wlanRole == WlanRole::AP)
{
str_populate(this->ssid, ssid);
}
}


uint8_t sec_type = security_type_to_simplelink(security_type);
sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_SECURITY_TYPE, 1,
(uint8_t*)&sec_type);

Expand All @@ -826,6 +826,7 @@ void CC32xxWiFi::wlan_setup_ap(const char *ssid, const char *security_key,
return;
}

HASSERT(strlen(security_key) <= 64);
sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_PASSWORD,
strlen(security_key), (uint8_t*)security_key);
}
Expand All @@ -849,7 +850,9 @@ void CC32xxWiFi::wlan_get_ap_config(string *ssid, SecurityType *security_type)
{
uint16_t len = sizeof(*security_type);
uint16_t config_opt = SL_WLAN_AP_OPT_SECURITY_TYPE;
sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt, &len, (_u8*) security_type);
uint8_t sl_sec_type = 0;
sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt, &len, &sl_sec_type);
*security_type = security_type_from_simplelink(sl_sec_type);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/freertos_drivers/net_cc32xx/CC32xxWiFi.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ protected:
{
}

/** Setup access point role credentials.
* @param ssid access point ssid
* @param security_key access point security key
* @param security_type specifies security type
/** Setup access point role credentials. It is OK to leave ssid as nullptr
* or password as nullptr, in which case those properties will not be
* changed.
* @param ssid access point ssid (name)
* @param security_key access point security key (password)
* @param security_type specifies security type. Required.
*/
virtual void wlan_setup_ap(const char *ssid, const char *security_key,
SecurityType security_type) = 0;
Expand Down
17 changes: 16 additions & 1 deletion src/openlcb/AliasAllocator.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,24 @@ TEST_F(AsyncAliasAllocatorTest, AllocateMultiple)
expect_packet(":X14003AAAN;");
expect_packet(":X10700AAAN;");

run_x([this]() {
EXPECT_EQ(
0x02010D000005u, ifCan_->local_aliases()->lookup(NodeAlias(0x555)));
EXPECT_EQ(
0x555u, ifCan_->local_aliases()->lookup(NodeID(0x02010D000005u)));
});
/* Conflicts with the previous alias to be tested. That's not a problem at
* this point however, because that alias has already left the
* allocator. */
* allocator. The conflict will generate an AMR frame. */
expect_packet(":X10703555N02010D000005;");
send_packet(":X10700555N;");
twait();
run_x([this]() {
// This mapping should be removed.
EXPECT_EQ(0U, ifCan_->local_aliases()->lookup(NodeAlias(0x555)));
EXPECT_EQ(0U, ifCan_->local_aliases()->lookup(NodeID(0x02010D000005u)));
});


get_next_alias();
EXPECT_EQ(0xAAAU, b_->data()->alias);
Expand Down Expand Up @@ -297,6 +311,7 @@ TEST_F(AsyncAliasAllocatorTest, AllocationConflict)
ifCan_->local_aliases()->lookup(NodeAlias(0xAA5)));
// This one should be unknown.
EXPECT_EQ(0U, ifCan_->local_aliases()->lookup(NodeAlias(0x555)));
EXPECT_EQ(0U, ifCan_->local_aliases()->lookup(NodeID(0x02010D000005u)));
});

get_next_alias();
Expand Down
10 changes: 8 additions & 2 deletions src/openlcb/AliasCache.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -311,25 +311,27 @@ void AliasCache::add(NodeID id, NodeAlias alias)
{
/* we already have a mapping for this alias, so lets remove it */
insert = it->deref(this);
auto nid = insert->get_node_id();
remove(insert->alias_);

if (removeCallback)
{
/* tell the interface layer that we removed this mapping */
(*removeCallback)(insert->get_node_id(), insert->alias_, context);
(*removeCallback)(nid, insert->alias_, context);
}
}
auto nit = idMap.find(id);
if (nit != idMap.end())
{
/* we already have a mapping for this id, so lets remove it */
insert = nit->deref(this);
auto nid = insert->get_node_id();
remove(insert->alias_);

if (removeCallback)
{
/* tell the interface layer that we removed this mapping */
(*removeCallback)(insert->get_node_id(), insert->alias_, context);
(*removeCallback)(nid, insert->alias_, context);
}
}

Expand Down Expand Up @@ -420,7 +422,10 @@ void AliasCache::remove(NodeAlias alias)
Metadata *metadata = it->deref(this);
aliasMap.erase(it);
idMap.erase(idMap.find(metadata->get_node_id()));
// Ensures that the AME query handler does not find this metadata.
metadata->set_node_id(0);

// Removes metadata from the linked lists.
if (!metadata->newer_.empty())
{
metadata->newer_.deref(this)->older_ = metadata->older_;
Expand All @@ -438,6 +443,7 @@ void AliasCache::remove(NodeAlias alias)
oldest = metadata->newer_;
}

// Adds metadata to the freelist.
metadata->older_ = freeList;
freeList.idx_ = metadata - pool;
}
Expand Down
4 changes: 2 additions & 2 deletions src/openlcb/AliasCache.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ static void remove_callback(NodeID node_id, NodeAlias alias, void *context)
{
EXPECT_TRUE(context == (void*)0xABCD0123);

EXPECT_TRUE(10 == alias);
EXPECT_TRUE(101 == node_id);
EXPECT_EQ(10u, alias);
EXPECT_EQ(101u, node_id);
}

TEST(AliasCacheTest, kick_out_duplicate_alias_callback)
Expand Down
6 changes: 6 additions & 0 deletions src/openlcb/Convert.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ extern void append_error_to_buffer(uint16_t error_code, Payload *p);
extern void buffer_to_error(const Payload &payload, uint16_t *error_code,
uint16_t *mti, string *error_message);

/// Generates the payload for an OIR or TDE message.
/// @param error_code the 16-bit ErrorCodes value.
/// @param incoming_mti the MTI of the message that this error should refer
/// to.
extern Payload error_payload(uint16_t error_code, Defs::MTI incoming_mti);

/** A global class / variable for empty or not-yet-initialized payloads. */
extern string EMPTY_PAYLOAD;

Expand Down
7 changes: 5 additions & 2 deletions src/openlcb/DatagramCan.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,13 @@ TEST_F(AsyncDatagramTest, DoubleReply)
wait();
wait();
send_packet(":X19A2877CN022A00;"); // Received OK
send_packet(":X19A2877CN022A00;"); // Received OK dup
wait(); // will unregister handlers
send_packet_and_expect_response(
":X19A2877CN022A00;", ":X1906822AN077C10400A28;"); // Received OK dup
wait_for_notification();
// Releases client.
send_packet(":X19A2877CN022A00;"); // Received OK dup
send_packet_and_expect_response(
":X19A2877CN022A00;", ":X1906822AN077C10400A28;"); // Received OK dup
wait();
datagram_support_.client_allocator()->insert(c);
}
Expand Down
6 changes: 6 additions & 0 deletions src/openlcb/DatagramTcp.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ class TcpDatagramTestBase : public MultiTcpIfTest
protected:
TcpDatagramTestBase()
{
LOG(INFO, "Add client 0");
add_client(REMOTE_NODE_ID + 0);
LOG(INFO, "Add client 1");
add_client(REMOTE_NODE_ID + 1);
LOG(INFO, "Add node nc");
create_new_node(&nc_, TEST_NODE_ID, &ifTcp_);
LOG(INFO, "Add node nc2");
create_new_node(&nc2_, TEST_NODE_ID + 1, &ifTcp_);
LOG(INFO, "Add node n0");
create_new_node(&n0_, REMOTE_NODE_ID + 0, &clients_[0]->ifTcp_);
LOG(INFO, "Add node n1");
create_new_node(&n1_, REMOTE_NODE_ID + 1, &clients_[1]->ifTcp_);
}

Expand Down
9 changes: 9 additions & 0 deletions src/openlcb/If.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/

#include "openlcb/If.hxx"
#include "openlcb/Convert.hxx"

/// Ensures that the largest bucket in the main buffer pool is exactly the size
/// of a GenMessage.
Expand Down Expand Up @@ -131,6 +132,14 @@ void buffer_to_error(const Payload &payload, uint16_t *error_code,
}
}

Payload error_payload(uint16_t error_code, Defs::MTI incoming_mti)
{
Payload p(4, 0);
error_to_data(error_code, &p[0]);
error_to_data(incoming_mti, &p[2]);
return p;
}

void send_event(Node* src_node, uint64_t event_id)
{
auto *b = src_node->iface()->global_message_write_flow()->alloc();
Expand Down
Loading

0 comments on commit f4ee32f

Please sign in to comment.