Skip to content

Commit

Permalink
change NodeAddr to NodeNumber for more consistent API
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfrej committed Oct 24, 2024
1 parent 89ae691 commit 22f7870
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 86 deletions.
3 changes: 1 addition & 2 deletions modules/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
set(SOURCES)
add_subdirectory(src/can)
add_subdirectory(src/node)
add_subdirectory(src/opcode)

add_library(vlcb_common ${CMAKE_CURRENT_LIST_DIR})
target_sources(vlcb_common PRIVATE ${SOURCES})
Expand All @@ -14,4 +13,4 @@ install(TARGETS vlcb_common
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY inc/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY inc/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
10 changes: 6 additions & 4 deletions modules/common/inc/vlcb/common/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
/**
* VLCB node address (11 bit wide)
*/
typedef uint16_t VlcbNodeAddr;
typedef uint16_t VlcbNodeNumber;

#define VLCB_NODE_ADDR_UNINITIALIZED (VlcbNodeAddr)0

VlcbNodeAddr vlcb_defs_NewNodeAddrFromBytesUnchecked(uint8_t hi, uint8_t lo);
int vlcb_defs_NewNodeAddr(uint16_t val, VlcbNodeAddr *const addr);
int vlcb_defs_NewNodeAddrFromBytes(uint8_t hi, uint8_t lo, VlcbNodeAddr *const addr);
VlcbNodeNumber vlcb_defs_NewNodeNumberFromBytesUnchecked(uint8_t hi,
uint8_t lo);
int vlcb_defs_NewNodeNumber(uint16_t val, VlcbNodeNumber *const addr);
int vlcb_defs_NewNodeNumberFromBytes(uint8_t hi, uint8_t lo,
VlcbNodeNumber *const addr);
8 changes: 4 additions & 4 deletions modules/common/src/node/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
set(
SOURCES
${SOURCES}
${CMAKE_CURRENT_LIST_DIR}/node_new_addr.c
${CMAKE_CURRENT_LIST_DIR}/node_new_addr_from_bytes.c
${CMAKE_CURRENT_LIST_DIR}/node_new_addr_unchecked.c
${CMAKE_CURRENT_LIST_DIR}/node_new_number.c
${CMAKE_CURRENT_LIST_DIR}/node_new_number_from_bytes.c
${CMAKE_CURRENT_LIST_DIR}/node_new_number_unchecked.c
PARENT_SCOPE
)
)
26 changes: 0 additions & 26 deletions modules/common/src/node/node_new_addr.c

This file was deleted.

8 changes: 0 additions & 8 deletions modules/common/src/node/node_new_addr_from_bytes.c

This file was deleted.

6 changes: 0 additions & 6 deletions modules/common/src/node/node_new_addr_unchecked.c

This file was deleted.

28 changes: 28 additions & 0 deletions modules/common/src/node/node_new_number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "vlcb/common/node.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

bool IsNodeNumberValid(const VlcbNodeNumber addr) { return addr > 0; }

int vlcb_defs_NewNodeNumber(const uint16_t val, VlcbNodeNumber *const addr) {
assert(addr != NULL);
const VlcbNodeNumber _addr = val;
if (IsNodeNumberValid(_addr)) {
*addr = _addr;
return 0;
}
return 1;
}

int vlcb_defs_NewNodeNumberFromBytes(const uint8_t hi, const uint8_t lo,
VlcbNodeNumber *const addr) {
const VlcbNodeNumber _addr =
vlcb_defs_NewNodeNumberFromBytesUnchecked(hi, lo);
if (IsNodeNumberValid(_addr)) {
*addr = _addr;
return 0;
}
return 1;
}
9 changes: 9 additions & 0 deletions modules/common/src/node/node_new_number_from_bytes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "vlcb/common/node.h"

#include <stdint.h>

int vlcb_defs_NewNodeNumberFromBytes(const uint8_t hi, const uint8_t lo,
VlcbNodeNumber *const addr) {
const uint16_t _addr = vlcb_defs_NewNodeNumberFromBytesUnchecked(hi, lo);
return vlcb_defs_NewNodeNumber(_addr, addr);
}
7 changes: 7 additions & 0 deletions modules/common/src/node/node_new_number_unchecked.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "vlcb/common/node.h"

VlcbNodeNumber vlcb_defs_NewNodeNumberFromBytesUnchecked(const uint8_t hi,
const uint8_t lo) {
const VlcbNodeNumber addr = (hi << 8) | lo;
return addr;
}
6 changes: 0 additions & 6 deletions modules/common/src/opcode/CMakeLists.txt

This file was deleted.

12 changes: 0 additions & 12 deletions modules/common/src/opcode/opcode_from_byte.c

This file was deleted.

4 changes: 3 additions & 1 deletion modules/module/inc/vlcb/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ typedef struct {
VlcbModuleOpFlags operationFlags;
VlcbModulePersistedState state;
VlcbNetHwAddr hwAddr;
VlcbNodeAddr nodeAddr;
VlcbNodeNumber nodeAddr;
} VlcbModuleConfig;

typedef struct {
clock_t lastHeartbeat;
uint8_t heartbeatCount;
VlcbNetIface *const iface;
VlcbNetSocketDatagram *const socket;
VlcbModuleUi ui;
Expand Down
5 changes: 4 additions & 1 deletion modules/module/inc/vlcb/module/state.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "vlcb/common/node.h"
#include "vlcb/platform/time.h"
#include <stdint.h>
#include <time.h>

typedef uint8_t VlcbModuleState;
enum VlcbModuleState {
Expand All @@ -12,7 +14,8 @@ enum VlcbModuleState {

typedef struct {
VlcbModuleState prevState;
VlcbNodeAddr nodeAddr;
VlcbNodeNumber nodeAddr;
clock_t startedAt;
} VlcbModuleStateMachineSetupData;

typedef union {
Expand Down
121 changes: 119 additions & 2 deletions modules/module/src/module.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,126 @@
#include "module.h"

#include <assert.h>
#include <time.h>

#include "state_machine.h"
#include "vlcb/common/vlcb_defs.h"
#include "vlcb/module.h"
#include "vlcb/module/state.h"
#include "vlcb/net/packet/datagram.h"
#include "vlcb/platform/interface.h"
#include "vlcb/platform/time.h"

#include "state_machine.h"
#ifndef VLCB_MODULE_HEARTBEAT_MS
#define VLCB_MODULE_HEARTBEAT_MS 5000
#endif /* ifndef VLCB_MODULE_HEARTBEAT_MS */

static inline void HandleHeartbeat(VlcbModule *const self, const clock_t now) {
if (self->sm.state == VLCB_MODULE_STATE_NORMAL &&
self->config.operationFlags & VLCB_MODULE_FLAGS_HEARTBEAT &&
(self->lastHeartbeat == 0 ||
vlcb_platform_time_DiffInMs(self->lastHeartbeat, now) >=
VLCB_MODULE_HEARTBEAT_MS)) {
// TODO: send the packet
self->lastHeartbeat = now;
self->heartbeatCount++;
}
}

static inline void HandleMnsMessages(VlcbModule *const self,
const VlcbPacketDatagram *const packet) {
switch (packet->opc) {
case VLCB_OPC_QUERY_NODE_PARAMETERS:
// 10 - RQNP message - request for node parameters -- does not contain a NN
// or EN, so only respond if we are in transition to Normal
handleRequestNodeParameters();
break;

case VLCB_OPC_QUERY_NODE_PARAMETER_BY_INDEX:
// 73 - RQNPN message -- request parameter by index number
// index 0 = number of params available followed by each parameter as a
// seperate PARAN respond with PARAN
handleRequestNodeParameter(msg, nn);
break;

case VLCB_OPC_SET_NODE_NUMBER:
// 42 - received SNN - set node number
handleSetNodeNumber(msg, nn);
break;

case VLCB_OPC_REQUEST_NEW_NODE_NUMBER:
// 50 - Another module has entered setup.
// If we are in setup, abort (MNS Spec 3.2.1)

if (instantMode == MODE_SETUP) {
instantMode = module_config->currentMode;
controller->indicateMode(module_config->currentMode);
}
break;

case VLCB_OPC_QUERY_NODE_INFO:
// 0D - this is probably a config recreate -- respond with PNN if we have a
// node number DEBUG_SERIAL << F("> QNN received, my node number = ") <<
// module_config->nodeNum << endl;

if (module_config->nodeNum > 0) {
// DEBUG_SERIAL << ("> responding with PNN message") << endl;
controller->sendMessageWithNN(OPC_PNN, controller->getParam(PAR_MANU),
controller->getParam(PAR_MTYP),
controller->getParam(PAR_FLAGS));
}
break;

case VLCB_OPC_QUERY_MODULE_NAME:
// 11 - request for node module name, excluding "CAN" prefix
// sent during module transition, so no node number check
// DEBUG_SERIAL << F("> RQMN received") << endl;

// only respond if in transition to Normal, i.e. Setup mode, or in learn
// mode.

if (instantMode == MODE_SETUP ||
(controller->getParam(PAR_FLAGS) & PF_LRN)) {
// respond with NAME
VlcbMessage response;
response.len = 8;
response.data[0] = OPC_NAME;
memcpy(response.data + 1, controller->getModuleName(), 7);
controller->sendMessage(&response);
}
break;

case OPC_RQSD:
// 78 - Request Service Definitions.
handleRequestServiceDefinitions(msg, nn);
break;

case OPC_RDGN:
// 87 - Request Diagnostic Data
handleRequestDiagnostics(msg, nn);
break;

case OPC_MODE:
// 76 - Set Operating Mode
handleModeMessage(msg, nn);
break;

case OPC_NNRSM:
// 4F - reset to manufacturer's defaults
if (nn == module_config->nodeNum) {
controller->sendMessageWithNN(OPC_NNREL); // release node number first
module_config->resetModule();
}
break;

case OPC_NNRST:
// 5E - software reset
if (nn == module_config->nodeNum) {
module_config->reboot();
}
break;
}
}

VlcbModule vlcb_module_New(VlcbNetIface *const iface,
VlcbNetSocketDatagram *const socket,
Expand All @@ -31,7 +146,7 @@ VlcbModule vlcb_module_New(VlcbNetIface *const iface,
void vlcb_module_Init(VlcbModule *const module, const clock_t now) {
assert(module != NULL);
// todo: load config from persistent storage
state_Dispatch(module, (ModuleStateEvent){.sig = MSE_INIT});
state_Dispatch(module, (ModuleStateEvent){.sig = MSE_INIT}, now);
_INTERFACE_CALL(module->ui, Poll, now);
}

Expand All @@ -57,5 +172,7 @@ void vlcb_module_Poll(VlcbModule *const module, const clock_t now) {
}
}

HandleHeartbeat(module, now);

_INTERFACE_CALL(module->ui, Poll, now);
}
Loading

0 comments on commit 22f7870

Please sign in to comment.