Compare commits

..
18 Commits
Author SHA1 Message Date
skruecken 59584f3928 Dont remove build dir anymore
Build / Build (push) Successful in 1m20s
2025-05-19 21:51:32 +02:00
skruecken 77bcf116fb Removed creation of build dir 2025-05-19 21:51:07 +02:00
skruecken 41437f0f2f Embedded the Templates in the go binary
Build / Build (push) Successful in 1m26s
2025-05-19 21:46:38 +02:00
skruecken 1f05960ffc Reworked Gitea Actions
Build / Build (push) Successful in 1m52s
2025-05-19 21:24:23 +02:00
skruecken ae2879de41 Fixed Branchname
ProtoGen Action Runner / build (push) Failing after 56s
2025-05-19 21:21:36 +02:00
skruecken efd3916fb4 Reworked Actions Build 2025-05-19 21:20:37 +02:00
skruecken 52238f9aaf Added Workflow
ProtoGen Action Runner / build (push) Failing after 0s
2025-05-19 21:17:05 +02:00
skruecken a887d76dd9 Added build cli 2025-05-19 21:13:11 +02:00
skruecken 014186b65a Removed Default write to build dir 2025-05-19 21:11:36 +02:00
skruecken e6763a6d4b Modified Makefile with PHONY targets 2025-05-19 21:05:11 +02:00
skruecken e80cba9110 Added -i for Input File 2025-05-19 21:04:54 +02:00
skruecken ac5c660583 Added Build Script for Windows and linux 2025-05-19 21:01:26 +02:00
skruecken 853fda9814 Build Output File Select 2025-05-19 20:59:15 +02:00
skruecken aff454c277 Maintaince Utilty files 2025-05-19 20:59:01 +02:00
skruecken df2338b6f1 Fixed Spacing in Template 2025-04-27 15:31:09 +02:00
skruecken ca22cc501e Reorderd hook stubs in header file and fixed up some spacing 2025-04-27 15:07:54 +02:00
skruecken 49bfccfbd8 Added length to base messages 2025-04-27 12:58:50 +02:00
skruecken 73a7c8469a Added BaseMessages with messageid 2025-04-27 12:57:42 +02:00
10 changed files with 179 additions and 42 deletions
+28
View File
@@ -0,0 +1,28 @@
name: Build
on: [push]
jobs:
Build:
runs-on: ubuntu-latest
container:
image: golang:latest
options: --network host
steps:
- run: |
apt-get update
apt-get install -y nodejs npm
- name: Check out repository code
uses: actions/checkout@v4
- run: echo "The ${{ gitea.repository }} repository has been cloned to the runner."
- name: List files in the repository
run: |
ls ${{ gitea.workspace }}
- run: go mod tidy
- run: make build
- run: ls -lah ${{ gitea.workspace }}
- name: Upload Build Artifacts
uses: actions/upload-artifact@v3
with:
name: alox.protogen
path: ./alox.protogen
- run: echo "This job's status is ${{ job.status }}."
+2
View File
@@ -1 +1,3 @@
alox.protogen
alox.protogen.exe
build/
+10
View File
@@ -0,0 +1,10 @@
.PHONY: build build_win clean
build:
GOOS=linux go build .
build_win:
GOOS=windows go build .
clean:
rm -rf build/*
+47 -5
View File
@@ -1,7 +1,10 @@
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"log"
"os"
@@ -11,30 +14,69 @@ import (
"github.com/knadh/koanf/providers/file"
)
//go:embed templates/*
var embeddedTemplates embed.FS
var appConfig = koanf.New(".")
func main() {
if err := appConfig.Load(file.Provider("testdata/conf.yaml"), yaml.Parser()); err != nil {
confPath := flag.String("c", "testdata/conf.yaml", "Config File")
outFileName := flag.String("o", "", "Name of the Output File")
inputFile := flag.String("i", "testdata/prot1.json", "Name of the Input File")
flag.Parse()
if err := appConfig.Load(file.Provider(*confPath), yaml.Parser()); err != nil {
log.Printf("error loading config: %v", err)
} else {
log.Printf("CONF: %v", appConfig)
}
file, _ := os.ReadFile("testdata/prot1.json")
file, err := os.ReadFile(*inputFile)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
var cfg proto.Proto
json.Unmarshal(file, &cfg)
proto.InitTemplates()
// if outFileName is set use it, if is not set set it to default value
if *outFileName != "" {
cfg.FileName = *outFileName
} else {
*outFileName = "build/proto"
}
// If filename not set in json take it from cli or default
if cfg.FileName != "" {
cfg.FileName = *outFileName
}
proto.InitTemplates(embeddedTemplates, "templates/*.tmpl")
header, err := proto.CreateCStyleHeader(&cfg)
if err != nil {
log.Printf("ERROR: %v", err)
}
log.Printf("TEMPLATE HEADER: \n%v", header)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
err = os.WriteFile(fmt.Sprintf("%s.h", *outFileName), []byte(header), 0666)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
source, err := proto.CreateCStyleSource(&cfg)
if err != nil {
log.Printf("ERROR: %v", err)
}
log.Printf("TEMPLATE SOURCE: \n%v", source)
err = os.WriteFile(fmt.Sprintf("%s.c", *outFileName), []byte(source), 0666)
if err != nil {
log.Printf("ERROR: %v", err)
return
}
}
+8 -3
View File
@@ -3,6 +3,7 @@ package proto
import (
"bytes"
"encoding/json"
"io/fs"
"log"
"strconv"
"text/template"
@@ -27,13 +28,14 @@ type ProtoMessage struct {
type ProtoMessagePayload struct {
Name string `json:"name"`
DataType string `json:"type"`
ArrayLength int `json:"array,omitempty"`
ArrayLength int `json:"array,omitempty"` // optional falls kein array
}
type Proto struct {
ProtocolHeader ProtocolHeader `json:"protocol"`
ESP_TO_PC []ProtoMessage `json:"messages_esp_to_pc"`
PC_TO_ESP []ProtoMessage `json:"messages_pc_to_esp"`
FileName string `json:"filename,omitempty"`
}
func (h *HexByte) UnmarshalJSON(data []byte) error {
@@ -62,6 +64,7 @@ type PayloadStruct struct {
}
type CStyleData struct {
FileName string
CenumPCTOESP Cenum
CenumESPTOPC Cenum
PayloadStructs []PayloadStruct
@@ -71,13 +74,13 @@ type CStyleData struct {
var tmpl *template.Template
func InitTemplates() {
func InitTemplates(fsys fs.FS, pattern string) {
funcs := template.FuncMap{
"snake": toSnakeCase,
"isLast": isLast,
}
Ntmpl, err := template.New("all").Funcs(funcs).ParseGlob("templates/*.tmpl")
Ntmpl, err := template.New("all").Funcs(funcs).ParseFS(fsys, pattern)
if err != nil {
log.Fatalf("failed to parse templates: %v", err)
}
@@ -105,6 +108,7 @@ func CreateCStyleHeader(proto *Proto) (string, error) {
}
var data = CStyleData{
FileName: proto.FileName,
CenumPCTOESP: Cenum{
EnumName: "PC_TO_ESP_MESSAGE_IDS",
Entries: proto.PC_TO_ESP,
@@ -148,6 +152,7 @@ func CreateCStyleSource(proto *Proto) (string, error) {
}
var data = CStyleData{
FileName: proto.FileName,
CenumPCTOESP: Cenum{
EnumName: "PC_TO_ESP_MESSAGE_IDS",
Entries: proto.PC_TO_ESP,
+2 -2
View File
@@ -86,7 +86,7 @@ Each message is described as:
- [x] Hook-Funktionen
- [x] Message-Dispatcher (Switch-Case)
- [x] Send-Funktionen
- [ ] Generate Header and Source File
- [x] Generate Header and Source File
---
## To-Do
@@ -119,7 +119,7 @@ Each message is described as:
### Tooling
- [ ] CLI interface (`--input`, `--output`, `--language`, ...)
- [ ] Logging / verbose mode
- [ ] Build script / Makefile
- [x] Build script / Makefile
### Testing
- [ ] Unit tests for generator
+44 -9
View File
@@ -3,7 +3,7 @@ typedef enum {
{{- range .Entries }}
{{ .Name }} = {{ printf "0x%X" .ID }},
{{- end }}
}{{.EnumName}};
} {{.EnumName}};
{{end}}
{{define "payloads"}}
@@ -20,8 +20,8 @@ typedef struct {
{{- end}}
{{- end }}
} {{ .Name }}Payload;
{{ end }}
{{end}}
{{- end}}
{{define "union"}}
typedef union {
@@ -31,14 +31,30 @@ typedef union {
} PayloadUnion;
{{end}}
{{define "basemessage"}}
typedef struct {
uint8_t Version;
{{.CenumPCTOESP.EnumName}} MessageID;
uint8_t Length;
PayloadUnion Payload;
} PCTOESPBaseMessage;
typedef struct {
uint8_t Version;
{{.CenumESPTOPC.EnumName}} MessageID;
uint8_t Length;
PayloadUnion Payload;
} ESPTOPCBaseMessage;
{{end}}
{{define "handler"}}
{{- range . }}
void (*on_{{ .Name | snake }})({{ .Name }}Payload*) = NULL;
void (*on_{{ .Name | snake }})({{ .Name }}Payload*);
{{- end}}
{{end}}
{{define "dispatcher"}}
void dispatch_message(uint8_t msg_id, void* payload) {
void dispatch_message(uint8_t msg_id, void *payload) {
switch (msg_id) {
{{- range . }}
case {{ .Name }}:
@@ -52,19 +68,38 @@ void dispatch_message(uint8_t msg_id, void* payload) {
}
{{end}}
{{define "send_functions"}}
{{define "send_function_prototype"}}
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload);
{{end}}
{{define "send_function_source"}}
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload) {}
{{end}}
{{define "send_functions_prototype"}}
{{- range .}}
{{ $argLen := len .Payload }}
void send_{{.Name | snake}}({{- range $i, $p := .Payload}}{{$p.DataType}} {{$p.Name}} {{- if not (isLast $argLen $i) }}, {{ end}} {{- end}}) {
{{- $argLen := len .Payload }}
void send_{{.Name | snake}}({{- range $i, $p := .Payload}}{{$p.DataType}} {{ if .ArrayLength}}*{{end}}{{$p.Name}} {{- if not (isLast $argLen $i) }}, {{ end}} {{- end}});
{{- end }}
{{end}}
{{define "send_functions_source"}}
{{- range .}}
{{- $argLen := len .Payload }}
void send_{{.Name | snake}}({{- range $i, $p := .Payload}}{{$p.DataType}} {{ if .ArrayLength}}*{{end}}{{$p.Name}} {{ if not (isLast $argLen $i) }}, {{ end}} {{- end}}) {
{{.Name}}Payload payload;
// Payload-Daten zuweisen
{{- range .Payload}}
{{- if .ArrayLength }}
memcpy(payload.{{.Name}}, {{.Name}}, {{.ArrayLength}});
{{- else }}
payload.{{.Name}} = {{.Name}};
{{- end}}
{{- end}}
// Nachricht senden
send_message({{.Name}}, &payload);
send_message({{.Name}}, (PayloadUnion *)&payload);
}
{{- end }}
{{end}}
{{end}}
+21 -4
View File
@@ -4,12 +4,29 @@
#ifndef _PROTO_HEADER
#define _PROTO_HEADER
{{ block "cenum" .CenumPCTOESP }} {{ end }}
{{ block "cenum" .CenumESPTOPC }} {{ end }}
#include <stdint.h>
{{ block "payloads" .PayloadStructs }} {{ end }}
// MessageIDs
{{- block "cenum" .CenumPCTOESP }} {{- end}}
{{- block "cenum" .CenumESPTOPC }} {{- end}}
{{ block "union" .PayloadStructs }} {{ end }}
// Payloads for single Messages
{{- block "payloads" .PayloadStructs }} {{- end}}
// Union for all the Payloads
{{- block "union" .PayloadStructs }} {{- end}}
// Base Message that can hold all Payloads
{{- block "basemessage" . }} {{- end}}
// Generic Send Function Prototype
{{- block "send_function_prototype" .MessagesESPtoPC}} {{- end}}
// Spezific Send Functions Prototype
{{- block "send_functions_prototype" .MessagesESPtoPC}} {{- end}}
// Prototypes for Message Recieve Handler to be set in user code
{{- block "handler" .MessagesPCtoESP}} {{ end }}
#endif
{{- end -}}
+9 -3
View File
@@ -1,9 +1,15 @@
{{- define "cpartSource" -}}
// AUTO GENERATED DO NOT EDIT!!!
{{ block "handler" .MessagesPCtoESP}} {{ end }}
#include "{{.FileName}}.h"
#include <string.h>
{{ block "dispatcher" .MessagesPCtoESP}} {{ end }}
// Message Dispatcher
{{- block "dispatcher" .MessagesPCtoESP}} {{ end }}
{{ block "send_functions" .MessagesESPtoPC}} {{ end }}
//Generic Send Function
{{- block "send_function_source" .MessagesESPtoPC}} {{ end }}
// Sepzific Send Functions
{{- block "send_functions_source" .MessagesESPtoPC}} {{ end }}
{{- end -}}
-8
View File
@@ -1,8 +0,0 @@
# Current Work
Added ArrayLenght need to implement it everywhere in the template
in Payload struct it happend already
Need it in:
- Send Parameter
- [x] Also remove trailing "," in send parameter