Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: added separate 'log write' event type for OT nodes #162

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dispatcher/FailureCtrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type mockDispatcherCallback struct {
func (m mockDispatcherCallback) OnUartWrite(nodeid NodeId, data []byte) {
}

func (m mockDispatcherCallback) OnLogWrite(nodeid NodeId, data []byte) {
}

func (m mockDispatcherCallback) OnNextEventTime(nextTimeUs uint64) {
}

Expand Down
7 changes: 7 additions & 0 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type CallbackHandler interface {
// OnUartWrite Notifies that the node's UART was written with data.
OnUartWrite(nodeid NodeId, data []byte)

// OnLogWrite Notifies that a log item wsa written to the node's log.
OnLogWrite(nodeid NodeId, data []byte)

// OnNextEventTime Notifies that the Dispatcher simulation will move shortly to the next event time.
OnNextEventTime(nextTimeUs uint64)

Expand All @@ -80,6 +83,7 @@ type Dispatcher struct {
RadioEvents uint64
StatusPushEvents uint64
UartWriteEvents uint64
LogWriteEvents uint64
OtherEvents uint64
// Packet-related event dispatching counters
DispatchByExtAddrSucc uint64
Expand Down Expand Up @@ -407,6 +411,9 @@ func (d *Dispatcher) handleRecvEvent(evt *Event) {
case EventTypeUartWrite:
d.Counters.UartWriteEvents += 1
d.cbHandler.OnUartWrite(node.Id, evt.Data)
case EventTypeLogWrite:
d.Counters.LogWriteEvents += 1
d.cbHandler.OnLogWrite(node.Id, evt.Data)
case EventTypeExtAddr:
d.Counters.OtherEvents += 1
var extaddr = binary.BigEndian.Uint64(evt.Data[0:8])
Expand Down
3 changes: 2 additions & 1 deletion event/event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2023, The OTNS Authors.
// Copyright (c) 2020-2024, The OTNS Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -60,6 +60,7 @@ const (
EventTypeRadioRfSimParamGet EventType = 16
EventTypeRadioRfSimParamSet EventType = 17
EventTypeRadioRfSimParamRsp EventType = 18
EventTypeLogWrite EventType = 19
)

const (
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023, The OTNS Authors.
// Copyright (c) 2023-2024, The OTNS Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
13 changes: 13 additions & 0 deletions logger/nodelogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package logger
import (
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -156,6 +157,18 @@ func (nl *NodeLogger) IsLevelVisible(level Level) bool {
return nl.displayLevel >= level
}

// LogOt logs a complete OT format log string, which includes timestamp, log level character, and
// the log message. The right level to log is automatically determined.
func (nl *NodeLogger) LogOt(levelAndMsg string) {
isOtLogLine, level := ParseOtLogLine(levelAndMsg)
levelAndMsg = strings.TrimSpace(levelAndMsg)
if isOtLogLine {
NodeLogf(nl.Id, level, levelAndMsg)
} else {
NodeLogf(nl.Id, InfoLevel, levelAndMsg)
}
}

func (nl *NodeLogger) Log(level Level, msg string) {
NodeLogf(nl.Id, level, msg)
}
Expand Down
21 changes: 19 additions & 2 deletions logger/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@

package logger

import "fmt"
import (
"fmt"
"regexp"
)

const (
OffLevelString = "off"
NoneLevelString = "none"
DefaultLevelString = "default"
)

var (
logPattern = regexp.MustCompile(`\[(-|C|W|N|I|D|CRIT|WARN|NOTE|INFO|DEBG)]`)
)

func ParseLevelString(level string) (Level, error) {
switch level {
case "micro":
Expand All @@ -59,7 +66,7 @@ func ParseLevelString(level string) (Level, error) {
}
}

func ParseOtLevelChar(level byte) Level {
func parseOtLevelChar(level byte) Level {
switch level {
case 'T':
return TraceLevel
Expand All @@ -78,6 +85,16 @@ func ParseOtLevelChar(level byte) Level {
}
}

// ParseOtLogLine attempts to parse line as an OT generated log line with timestamp/level/message.
// Returns true if successful and also returns the determined log level of the log line.
func ParseOtLogLine(line string) (bool, Level) {
logIdx := logPattern.FindStringSubmatchIndex(line)
if logIdx == nil {
return false, 0
}
return true, parseOtLevelChar(line[logIdx[2]])
}

func GetLevelString(level Level) string {
switch level {
case MicroLevel:
Expand Down
12 changes: 12 additions & 0 deletions ot-rfsim/src/event-sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength) {
otSimSendEvent(&event);
}

void otSimSendLogWriteEvent(const uint8_t *aData, uint16_t aLength) {
OT_ASSERT(aLength <= OT_EVENT_DATA_MAX_SIZE);

struct Event event;
event.mEvent = OT_SIM_EVENT_LOG_WRITE;
event.mDelay = 0;
event.mDataLength = aLength;
memcpy(event.mData, aData, aLength);

otSimSendEvent(&event);
}

void otSimSendOtnsStatusPushEvent(const char *aStatus, uint16_t aLength) {
OT_ASSERT(aLength <= OT_EVENT_DATA_MAX_SIZE);

Expand Down
9 changes: 9 additions & 0 deletions ot-rfsim/src/event-sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum
OT_SIM_EVENT_RFSIM_PARAM_GET = 16,
OT_SIM_EVENT_RFSIM_PARAM_SET = 17,
OT_SIM_EVENT_RFSIM_PARAM_RSP = 18,
OT_SIM_EVENT_LOG_WRITE = 19,
};

#define OT_EVENT_DATA_MAX_SIZE 1024
Expand Down Expand Up @@ -171,6 +172,14 @@ void otSimSendRadioChanSampleEvent(struct RadioCommEventData *aChanData);
*/
void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength);

/**
* Send an OT log-write event to the simulator, containing a single log item.
*
* @param[in] aData A pointer to the UART data.
* @param[in] aLength Length of UART data.
*/
void otSimSendLogWriteEvent(const uint8_t *aData, uint16_t aLength);

/**
* Send status push data event to the OT-NS simulator.
*
Expand Down
2 changes: 1 addition & 1 deletion ot-rfsim/src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const
if (!gTerminate) {
logString[strLen] = '\n';
logString[strLen + 1] = '\0';
otSimSendUartWriteEvent((const uint8_t *) &logString[0], strLen + 1);
otSimSendLogWriteEvent((const uint8_t *) &logString[0], strLen + 1);
}
}

Expand Down
131 changes: 0 additions & 131 deletions otoutfilter/OTOutFilter.go

This file was deleted.

81 changes: 0 additions & 81 deletions otoutfilter/OTOutFilter_test.go

This file was deleted.

Loading
Loading