blob: ab2ac96b40f264c363bd70d0ac0987f336b92689 [file] [log] [blame]
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package packer
21
22import (
23 "fmt"
Anssi Mannila5c161a92020-01-15 15:40:57 +020024 "strings"
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020025)
26
27const cLogBufferMaxSize = 1024
28const cMsgBufferMaxSize = 2048
29
30//-----------------------------------------------------------------------------
31//
32//-----------------------------------------------------------------------------
33
34type PduPackerIf interface {
35 PduPack(logBuf []byte, data *PackedData) error
36}
37
38func PduPackerPack(entry PduPackerIf, trgBuf *PackedData) error {
39
40 var logBuffer []byte = make([]byte, cLogBufferMaxSize)
41 logBuffer[0] = 0
42
43 if trgBuf != nil {
44 trgBuf.Buf = make([]byte, cMsgBufferMaxSize)
45 }
46 err := entry.PduPack(logBuffer, trgBuf)
47 if err == nil {
48 return nil
49 }
Anssi Mannila5c161a92020-01-15 15:40:57 +020050 return fmt.Errorf("Pack failed: err: %s, logbuffer: %s", err.Error(), logBuffer[:strings.Index(string(logBuffer[:]), "\000")])
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020051}
52
53func PduPackerPackAllocTrg(entry PduPackerIf, trgBuf *PackedData) (error, *PackedData) {
54 dataPacked := trgBuf
55 if dataPacked == nil {
56 dataPacked = &PackedData{}
57 }
58 err := PduPackerPack(entry, dataPacked)
59 if err != nil {
60 return err, nil
61 }
62 return nil, dataPacked
63}
64
65//-----------------------------------------------------------------------------
66//
67//-----------------------------------------------------------------------------
68
69type PduUnPackerIf interface {
70 PduUnPack(logBuf []byte, data *PackedData) error
71}
72
73func PduPackerUnPack(entry PduUnPackerIf, data *PackedData) error {
74 var logBuffer []byte = make([]byte, cLogBufferMaxSize)
75
76 logBuffer[0] = 0
77 err := entry.PduUnPack(logBuffer, data)
78 if err == nil {
79 return nil
80 }
Anssi Mannila5c161a92020-01-15 15:40:57 +020081 return fmt.Errorf("Unpack failed: err: %s, logbuffer: %s", err.Error(), logBuffer[:strings.Index(string(logBuffer[:]), "\000")])
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020082}