From 8d1ce2529bc27a1f7de3bf6e9b47e9502045743d Mon Sep 17 00:00:00 2001 From: leandrofars Date: Thu, 30 May 2024 10:07:19 -0300 Subject: [PATCH] feat(cwmp): device answer timeout set via config --- backend/services/acs/cmd/acs/main.go | 1 + .../services/acs/internal/bridge/bridge.go | 14 +++++-- .../services/acs/internal/config/config.go | 37 ++++++++++--------- backend/services/acs/internal/cwmp/cwmp.go | 5 +++ 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/backend/services/acs/cmd/acs/main.go b/backend/services/acs/cmd/acs/main.go index d192f7b..228e66f 100644 --- a/backend/services/acs/cmd/acs/main.go +++ b/backend/services/acs/cmd/acs/main.go @@ -20,6 +20,7 @@ func main() { natsActions.Publish, natsActions.Subscribe, h, + &c.Acs, ) b.StartBridge() diff --git a/backend/services/acs/internal/bridge/bridge.go b/backend/services/acs/internal/bridge/bridge.go index 57fb7a1..aa67db8 100644 --- a/backend/services/acs/internal/bridge/bridge.go +++ b/backend/services/acs/internal/bridge/bridge.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "net/http" + "oktopUSP/backend/services/acs/internal/config" "oktopUSP/backend/services/acs/internal/server/handler" "strings" "time" @@ -17,6 +18,7 @@ type Bridge struct { sub func(string, func(*nats.Msg)) error cpes map[string]handler.CPE h *handler.Handler + conf *config.Acs } type msgAnswer struct { @@ -24,18 +26,18 @@ type msgAnswer struct { Msg any } -const DEVICE_ANSWER_TIMEOUT = 10 * time.Second - func NewBridge( pub func(string, []byte) error, sub func(string, func(*nats.Msg)) error, h *handler.Handler, + c *config.Acs, ) *Bridge { return &Bridge{ pub: pub, sub: sub, cpes: h.Cpes, h: h, + conf: c, } } @@ -68,6 +70,7 @@ func (b *Bridge) StartBridge() { Id: uuid.NewString(), CwmpMsg: msg.Data, Callback: deviceAnswer, + Time: time.Now(), }) err := b.h.ConnectionRequest(cpe) @@ -85,12 +88,15 @@ func (b *Bridge) StartBridge() { select { case response := <-deviceAnswer: - log.Println("Received response from device: ", string(response)) + if b.conf.DebugMode { + log.Printf("Received response from cpe: %s payload: %s ", cpe.SerialNumber, string(response)) + } respondMsg(msg.Respond, http.StatusOK, response) - case <-time.After(DEVICE_ANSWER_TIMEOUT): + case <-time.After(b.conf.DeviceAnswerTimeout): log.Println("Device response timed out") respondMsg(msg.Respond, http.StatusRequestTimeout, "Request timeout") } + }) } diff --git a/backend/services/acs/internal/config/config.go b/backend/services/acs/internal/config/config.go index 51815bf..53a3431 100644 --- a/backend/services/acs/internal/config/config.go +++ b/backend/services/acs/internal/config/config.go @@ -21,17 +21,18 @@ type Nats struct { } type Acs struct { - Port string - Tls bool - TlsPort bool - NoTls bool - KeepAliveInterval time.Duration - Username string - Password string - Route string - DebugMode bool - ConnReqUsername string - ConnReqPassword string + Port string + Tls bool + TlsPort bool + NoTls bool + KeepAliveInterval time.Duration + Username string + Password string + Route string + DebugMode bool + ConnReqUsername string + ConnReqPassword string + DeviceAnswerTimeout time.Duration } type Config struct { @@ -53,6 +54,7 @@ func NewConfig() *Config { connReqPasswd := flag.String("connrq_passwd", lookupEnvOrString("CONN_RQ_PASSWD", ""), "Connection Request Password") acsKeepAliveInterval := flag.Int("acs_keep_alive_interval", lookupEnvOrInt("KEEP_ALIVE_INTERVAL", 300), "keep alive interval in seconds for acs server") cwmpDebugMode := flag.Bool("debug_mode", lookupEnvOrBool("CWMP_DEBUG", false), "enable or disable cwmp logs in debug mode") + deviceAnswerTimeout := flag.Int("device_answer_timeout", lookupEnvOrInt("DEVICE_ANSWER_TIMEOUT", 10), "device answer timeout in seconds") flHelp := flag.Bool("help", false, "Help") /* @@ -79,12 +81,13 @@ func NewConfig() *Config { Ctx: ctx, }, Acs: Acs{ - Port: *acsPort, - Route: *acsRoute, - KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second, - DebugMode: *cwmpDebugMode, - ConnReqUsername: *connReqUser, - ConnReqPassword: *connReqPasswd, + Port: *acsPort, + Route: *acsRoute, + KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second, + DebugMode: *cwmpDebugMode, + ConnReqUsername: *connReqUser, + ConnReqPassword: *connReqPasswd, + DeviceAnswerTimeout: time.Duration(*deviceAnswerTimeout) * time.Second, }, } } diff --git a/backend/services/acs/internal/cwmp/cwmp.go b/backend/services/acs/internal/cwmp/cwmp.go index a0aaa06..8939317 100644 --- a/backend/services/acs/internal/cwmp/cwmp.go +++ b/backend/services/acs/internal/cwmp/cwmp.go @@ -63,6 +63,11 @@ type GetParameterNamesResponse struct { ParameterList []ParameterInfoStruct `xml:"Body>GetParameterNamesResponse>ParameterList>ParameterInfoStruct"` } +type Fault struct { + FaultCode uint + FaultString string +} + type CWMPInform struct { DeviceId DeviceID `xml:"Body>Inform>DeviceId"` Events []EventStruct `xml:"Body>Inform>Event>EventStruct"`