Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package packer |
| 21 | |
| 22 | import ( |
| 23 | "fmt" |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame^] | 24 | "strings" |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const cLogBufferMaxSize = 1024 |
| 28 | const cMsgBufferMaxSize = 2048 |
| 29 | |
| 30 | //----------------------------------------------------------------------------- |
| 31 | // |
| 32 | //----------------------------------------------------------------------------- |
| 33 | |
| 34 | type PduPackerIf interface { |
| 35 | PduPack(logBuf []byte, data *PackedData) error |
| 36 | } |
| 37 | |
| 38 | func 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 Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame^] | 50 | return fmt.Errorf("Pack failed: err: %s, logbuffer: %s", err.Error(), logBuffer[:strings.Index(string(logBuffer[:]), "\000")]) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | func 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 | |
| 69 | type PduUnPackerIf interface { |
| 70 | PduUnPack(logBuf []byte, data *PackedData) error |
| 71 | } |
| 72 | |
| 73 | func 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 Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame^] | 81 | return fmt.Errorf("Unpack failed: err: %s, logbuffer: %s", err.Error(), logBuffer[:strings.Index(string(logBuffer[:]), "\000")]) |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 82 | } |