Skip to content

Commit

Permalink
feat: initial commit of SEP2 module
Browse files Browse the repository at this point in the history
  • Loading branch information
activeshadow committed Mar 11, 2024
1 parent 014bab7 commit 63f93a4
Show file tree
Hide file tree
Showing 15 changed files with 10,825 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"dockerfile": "Dockerfile",
"context": "..",
"args": {
"GOLANG_VERSION": "1.21.1"
"GOLANG_VERSION": "1.22.0"
}
},

Expand Down
15 changes: 15 additions & 0 deletions config/sep2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<ot-sim>
<message-bus>
<pull-endpoint>tcp://127.0.0.1:1234</pull-endpoint>
<pub-endpoint>tcp://127.0.0.1:5678</pub-endpoint>
</message-bus>
<cpu>
<module name="backplane">ot-sim-message-bus {{config_file}}</module>
<module name="2030.5">ot-sim-sep2-module {{config_file}}</module>
</cpu>
<sep2>
<endpoint>:9234</endpoint>
<device>1</device>
</sep2>
</ot-sim>
53 changes: 49 additions & 4 deletions src/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,48 @@ prefix := /usr/local
# DATE := $(shell date -u)
# VERSION := $(VER) (commit $(COMMIT)) $(DATE)

MSGBUS_SOURCES := $(shell find msgbus \( -name '*.go' \))
THISFILE := $(lastword $(MAKEFILE_LIST))
THISDIR := $(shell dirname $(realpath $(THISFILE)))
GOBIN := $(THISDIR)/bin

# Prepend this repo's bin directory to our path since we'll want to
# install some build tools there for use during the build process.
PATH := $(GOBIN):$(PATH)

# Export GOBIN env variable so `go install` picks it up correctly.
export GOBIN

.PHONY: install-build-deps
install-build-deps: bin/xsdgen

.PHONY: remove-build-deps
remove-build-deps:
$(RM) bin/xsdgen

bin/xsdgen:
go install actshad.dev/go-xml/cmd/xsdgen@xmlname-field

all: bin/ot-sim-cpu-module bin/ot-sim-logic-module bin/ot-sim-modbus-module \
bin/ot-sim-mqtt-module bin/ot-sim-node-red-module bin/ot-sim-sep2-module \
bin/ot-sim-tailscale-module bin/ot-sim-telnet-module

clean:
$(RM) bin/*
$(RM) bin/ot-sim-cpu-module
$(RM) bin/ot-sim-logic-module
$(RM) bin/ot-sim-modbus-module
$(RM) bin/ot-sim-mqtt-module
$(RM) bin/ot-sim-node-red-module
$(RM) bin/ot-sim-sep2-module
$(RM) bin/ot-sim-tailscale-module
$(RM) bin/ot-sim-telnet-module

all: bin/ot-sim-cpu-module bin/ot-sim-logic-module bin/ot-sim-modbus-module bin/ot-sim-mqtt-module bin/ot-sim-node-red-module bin/ot-sim-tailscale-module bin/ot-sim-telnet-module
.PHONY: generate-sep-types
generate-sep: api/config/bindata.go tmpl/bindata.go web/bindata.go

sep2/schema/types.go: sep2/schema/SEP2.xsd bin/xsdgen
$(GOBIN)/xsdgen -f -i "ref,offset" -ns "http://ieee.org/2030.5" -o sep2/schema/types.go -pkg schema sep2/schema/SEP2.xsd

MSGBUS_SOURCES := $(shell find msgbus \( -name '*.go' \))

CPU_SOURCES := $(shell find cpu \( -name '*.go' \))

Expand Down Expand Up @@ -46,6 +82,12 @@ bin/ot-sim-node-red-module: $(NODERED_SOURCES)
mkdir -p bin
GOOS=linux go build -a -ldflags="-s -w" -trimpath -o bin/ot-sim-node-red-module cmd/ot-sim-node-red-module/main.go

SEP2_SOURCES := $(shell find sep2 \( -name '*.go' \))

bin/ot-sim-sep2-module: $(SEP2_SOURCES) sep2/schema/types.go
mkdir -p bin
GOOS=linux go build -a -ldflags="-s -w" -trimpath -o bin/ot-sim-sep2-module cmd/ot-sim-sep2-module/main.go

TAILSCALE_SOURCES := $(shell find tailscale \( -name '*.go' \))

bin/ot-sim-tailscale-module: $(TAILSCALE_SOURCES) $(MSGBUS_SOURCES)
Expand All @@ -59,11 +101,14 @@ bin/ot-sim-telnet-module: $(TELNET_SOURCES) $(MSGBUS_SOURCES)
GOOS=linux go build -a -ldflags="-s -w" -trimpath -o bin/ot-sim-telnet-module cmd/ot-sim-telnet-module/main.go

.PHONY: install
install: bin/ot-sim-cpu-module bin/ot-sim-logic-module bin/ot-sim-modbus-module bin/ot-sim-mqtt-module bin/ot-sim-node-red-module bin/ot-sim-tailscale-module bin/ot-sim-telnet-module
install: bin/ot-sim-cpu-module bin/ot-sim-logic-module bin/ot-sim-modbus-module \
bin/ot-sim-mqtt-module bin/ot-sim-node-red-module bin/ot-sim-sep2-module \
bin/ot-sim-tailscale-module bin/ot-sim-telnet-module
cp bin/ot-sim-cpu-module $(prefix)/bin/ot-sim-cpu-module
cp bin/ot-sim-logic-module $(prefix)/bin/ot-sim-logic-module
cp bin/ot-sim-modbus-module $(prefix)/bin/ot-sim-modbus-module
cp bin/ot-sim-mqtt-module $(prefix)/bin/ot-sim-mqtt-module
cp bin/ot-sim-node-red-module $(prefix)/bin/ot-sim-node-red-module
cp bin/ot-sim-sep2-module $(prefix)/bin/ot-sim-sep2-module
cp bin/ot-sim-tailscale-module $(prefix)/bin/ot-sim-tailscale-module
cp bin/ot-sim-telnet-module $(prefix)/bin/ot-sim-telnet-module
53 changes: 53 additions & 0 deletions src/go/cmd/ot-sim-sep2-module/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"errors"
"fmt"
"os"

otsim "github.com/patsec/ot-sim"
"github.com/patsec/ot-sim/util"
"github.com/patsec/ot-sim/util/sigterm"

// This will cause the SEP2 module to register itself with the otsim package
// so it gets run by the otsim.Start function below.
_ "github.com/patsec/ot-sim/sep2"
)

func main() {
if len(os.Args) != 2 {
panic("path to config file not provided")
}

if err := otsim.ParseConfigFile(os.Args[1]); err != nil {
fmt.Printf("Error parsing config file: %v\n", err)
os.Exit(util.ExitNoRestart)
}

ctx := sigterm.CancelContext(context.Background())

if err := otsim.Start(ctx); err != nil {
fmt.Printf("Error starting SEP2 (2030.5) module: %v\n", err)

var exitErr util.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode)
}

os.Exit(1)
}

<-ctx.Done()

if err := ctx.Err(); err != nil && !errors.Is(err, context.Canceled) {
fmt.Printf("Error running SEP2 (2030.5) module: %v\n", err)

var exitErr util.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode)
}

os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions src/go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/patsec/ot-sim

go 1.18
go 1.22

require (
actshad.dev/mbserver v0.3.1
Expand All @@ -16,7 +16,6 @@ require (
github.com/prometheus/client_golang v1.12.2
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand All @@ -25,6 +24,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/goburrow/serial v0.1.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down
3 changes: 1 addition & 2 deletions src/go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -247,8 +248,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
29 changes: 29 additions & 0 deletions src/go/sep2/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import (
"context"

"github.com/beevik/etree"
)

type SEP2Client struct {
name string
}

func New(name string) *SEP2Client {
return &SEP2Client{
name: name,
}
}

func (this SEP2Client) Name() string {
return this.name
}

func (this *SEP2Client) Configure(e *etree.Element) error {
return nil
}

func (this *SEP2Client) Run(ctx context.Context, pubEndpoint, pullEndpoint string) error {
return nil
}
Loading

0 comments on commit 63f93a4

Please sign in to comment.