func mergeCheck()

in plc4go/tools/plc4xpcapanalyzer/internal/cbusanalyzer/analyzer.go [195:234]


func mergeCheck(currentPayload *[]byte, srcUip string, mergeCallback func(int), currentInboundPayloads map[string][]byte, lastPayload *[]byte) (isMergedMessage, shouldClearInboundPayload bool) {
	// Check if we have a merged message
	for i, b := range *currentPayload {
		if i == 0 {
			// we ignore the first byte as this is typical for reset etc... so maybe this is good or bad we will see
			continue
		}
		switch b {
		case 0x0D:
			if i+1 < len(*currentPayload) && (*currentPayload)[i+1] == 0x0A {
				// If we know the next is a newline we jump to that index...
				i++
			}
			// ... other than that the logic is the same
			fallthrough
		case 0x0A:
			// We have a merged message if we are not at the end
			if i < len(*currentPayload)-1 {
				headPayload := (*currentPayload)[:i+1]
				tailPayload := (*currentPayload)[i+1:]
				if reflect.DeepEqual(headPayload, *lastPayload) {
					// This means that we have a merge where the last payload is an echo. In that case we discard that here to not offset all numbers
					*currentPayload = tailPayload
					log.Debug().Bytes("headPayload", headPayload).Msg("We cut the echo message %s out of the response to keep numbering")
					return mergeCheck(currentPayload, srcUip, mergeCallback, currentInboundPayloads, lastPayload)
				} else {
					if mergeCallback != nil {
						mergeCallback(i)
					}
					// In this case we need to put the tail into our "buffer"
					currentInboundPayloads[srcUip] = tailPayload
					// and use the beginning as current payload
					*currentPayload = headPayload
					return true, false
				}
			}
		}
	}
	return false, true
}