From a20458a9041ac354da77e8504e99c1e3b024cc50 Mon Sep 17 00:00:00 2001 From: Florian Thienel Date: Sun, 26 Sep 2021 15:27:14 +0200 Subject: [PATCH] implement the full Interface/Func pattern --- sds/command.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sds/command.go b/sds/command.go index 194453f..a94550a 100644 --- a/sds/command.go +++ b/sds/command.go @@ -20,8 +20,16 @@ func (f EncoderFunc) Encode() ([]byte, int) { return f() } +type Requester interface { + Request(context.Context, string) ([]string, error) +} + type RequesterFunc func(context.Context, string) ([]string, error) +func (f RequesterFunc) Request(ctx context.Context, request string) ([]string, error) { + return f(ctx, request) +} + const ( // CRLF line ending for AT commands CRLF = "\x0d\x0a" @@ -35,18 +43,18 @@ const ( ) // SendMessage according to [PEI] 6.13.2 -func SendMessage(destination tetra.Identity, sds Encoder) string { +func SendMessage(destination tetra.Identity, message Encoder) string { pdu := make([]byte, 0, 256) pduBits := 0 - pdu, pduBits = sds.Encode(pdu, pduBits) + pdu, pduBits = message.Encode(pdu, pduBits) return fmt.Sprintf("AT+CMGS=%s,%d"+CRLF+"%s"+CtrlZ, destination, pduBits, tetra.BinaryToHex(pdu)) } var sendMessageDescription = regexp.MustCompile(`^\+CMGS: .+\(\d*-(\d*)\)$`) // RequestMaxMessagePDUBits uses the given RequesterFunc to find out how many bits a message PDU may have (see [PEI] 6.13.2). -func RequestMaxMessagePDUBits(ctx context.Context, requester RequesterFunc) (int, error) { - responses, err := requester(ctx, "AT+CMGS=?") +func RequestMaxMessagePDUBits(ctx context.Context, requester Requester) (int, error) { + responses, err := requester.Request(ctx, "AT+CMGS=?") if err != nil { return 0, err }