/src/opendnp3/cpp/lib/include/opendnp3/app/OctetData.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2013-2022 Step Function I/O, LLC |
3 | | * |
4 | | * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O |
5 | | * LLC (https://stepfunc.io) under one or more contributor license agreements. |
6 | | * See the NOTICE file distributed with this work for additional information |
7 | | * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license |
8 | | * this file to you under the Apache License, Version 2.0 (the "License"); you |
9 | | * may not use this file except in compliance with the License. You may obtain |
10 | | * a copy of the License at: |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #ifndef OPENDNP3_OCTETDATA_H |
21 | | #define OPENDNP3_OCTETDATA_H |
22 | | |
23 | | #include "opendnp3/util/Buffer.h" |
24 | | |
25 | | #include <array> |
26 | | #include <cstdint> |
27 | | |
28 | | namespace opendnp3 |
29 | | { |
30 | | |
31 | | /** |
32 | | * A base-class for bitstrings containing up to 255 bytes |
33 | | */ |
34 | | class OctetData |
35 | | { |
36 | | public: |
37 | | const static uint8_t MAX_SIZE = 255; |
38 | | |
39 | | /** |
40 | | * Construct with a default value of [0x00] (length == 1) |
41 | | */ |
42 | | OctetData(); |
43 | | |
44 | | /** |
45 | | * Construct from a c-style string |
46 | | * |
47 | | * strlen() is used internally to determine the length |
48 | | * |
49 | | * If the length is 0, the default value of [0x00] is assigned |
50 | | * If the length is > 255, only the first 255 bytes are copied. |
51 | | * |
52 | | * The null terminator is NOT copied as part of buffer |
53 | | */ |
54 | | OctetData(const char* input); |
55 | | |
56 | | /** |
57 | | * Construct from read-only buffer slice |
58 | | * |
59 | | * |
60 | | * If the length is 0, the default value of [0x00] is assigned |
61 | | * If the length is > 255, only the first 255 bytes are copied. |
62 | | * |
63 | | * The null terminator is NOT copied as part of buffer |
64 | | */ |
65 | | OctetData(const Buffer& input); |
66 | | |
67 | | inline uint8_t Size() const |
68 | 6.13k | { |
69 | 6.13k | return size; |
70 | 6.13k | } |
71 | | |
72 | | /** |
73 | | * Set the octet data to the input buffer |
74 | | * |
75 | | * If the length is 0, the default value of [0x00] is assigned |
76 | | * If the length is > 255, only the first 255 bytes are copied |
77 | | * |
78 | | * @param input the input data to copy into this object |
79 | | * |
80 | | * @return true if the input meets the length requirements, false otherwise |
81 | | */ |
82 | | bool Set(const Buffer& input); |
83 | | |
84 | | /** |
85 | | * Set the buffer equal to the supplied c-string |
86 | | * |
87 | | * If the length is 0, the default value of [0x00] is assigned |
88 | | * If the length is > 255, only the first 255 bytes are copied |
89 | | * |
90 | | * @param input c-style string to copy into this object |
91 | | * |
92 | | * @return true if the input meets the length requirements, false otherwise |
93 | | */ |
94 | | bool Set(const char* input); |
95 | | |
96 | | /** |
97 | | * @return a view of the current data |
98 | | */ |
99 | | const Buffer ToBuffer() const; |
100 | | |
101 | | private: |
102 | | static const Buffer ToSlice(const char* input); |
103 | | |
104 | | std::array<uint8_t, MAX_SIZE> buffer = {{0x00}}; |
105 | | uint8_t size; |
106 | | }; |
107 | | |
108 | | } // namespace opendnp3 |
109 | | |
110 | | #endif |