libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
DoIPAddress.h
Go to the documentation of this file.
1 #ifndef DOIPADDRESS_H
2 #define DOIPADDRESS_H
3 
4 #include <stddef.h>
5 #include <stdint.h>
6 
7 #include <array>
8 #include <iomanip>
9 #include <iostream>
10 
11 #include "ByteArray.h"
12 
13 namespace doip {
14 
15 constexpr size_t DOIP_ADDRESS_SIZE = 2;
16 
17 /**
18  * @brief Represents a 16-bit DoIP address consisting of high and low significant bytes.
19  *
20  * This structure encapsulates a 16-bit address used in DoIP (Diagnostic over Internet Protocol)
21  * communication. The address is stored as two separate bytes (HSB and LSB) and provides
22  * convenient methods for construction, access, and manipulation.
23  *
24  * @note The address follows big-endian byte ordering (HSB first, then LSB).
25  */
26 using DoIPAddress = uint16_t;
27 
28 constexpr DoIPAddress ZERO_ADDRESS = 0x0000;
29 constexpr DoIPAddress MIN_SOURCE_ADDRESS = 0xE000;
30 constexpr DoIPAddress MAX_SOURCE_ADDRESS = 0xE3FF;
31 
32 /**
33  * @brief Check if source address is valid.
34  * @param data the data array containing the address
35  * @param offset the offset in the data array where the address starts
36  * @return true the source address is valid
37  * @return false the source address is NOT valid
38  */
39 inline bool isValidSourceAddress(const uint8_t *data, size_t offset = 0) {
40  uint16_t addr_value = (data[offset] << 8) | data[offset + 1];
41 
42  return MIN_SOURCE_ADDRESS <= addr_value && MAX_SOURCE_ADDRESS >= addr_value;
43 }
44 
45 /**
46  * @brief Try read the DoIP address from a byte array.
47  * @note No bounds checking
48  * @param data the pointer to the data array
49  * @param offset the offset in bytes
50  * @param address the address read
51  * @return true address was read successfully
52  * @return false invalid arguments
53  */
54 inline bool tryReadAddressFrom(const uint8_t *data, size_t offset, DoIPAddress &address) {
55  if (!data)
56  return false;
57 
58  address = static_cast<uint16_t>(data[offset] << 8 | data[offset + 1]);
59  return true;
60 }
61 
62 /**
63  * @brief Reads the DoIP address from a byte array.
64  * @note No bounds checking
65  * @param data the pointer to the data array
66  * @param offset the offset in bytes
67  * @return DoIPAddress the parsed DoIP address. If data is nullptr, then ZERO_ADDRESS is returned.
68  */
69 inline DoIPAddress readAddressFrom(const uint8_t *data, size_t offset = 0) {
70  if (!data)
71  return ZERO_ADDRESS;
72 
73  return static_cast<uint16_t>(data[offset] << 8 | data[offset + 1]);
74 }
75 
76 } // namespace doip
77 
78 #endif /* DOIPADDRESS_H */
Defines the ByteArray type and utility functions for byte manipulation.
Definition: AnsiColors.h:3
uint16_t DoIPAddress
Represents a 16-bit DoIP address consisting of high and low significant bytes.
Definition: DoIPAddress.h:26
bool isValidSourceAddress(const uint8_t *data, size_t offset=0)
Check if source address is valid.
Definition: DoIPAddress.h:39
constexpr DoIPAddress ZERO_ADDRESS
Definition: DoIPAddress.h:28
constexpr DoIPAddress MAX_SOURCE_ADDRESS
Definition: DoIPAddress.h:30
DoIPAddress readAddressFrom(const uint8_t *data, size_t offset=0)
Reads the DoIP address from a byte array.
Definition: DoIPAddress.h:69
bool tryReadAddressFrom(const uint8_t *data, size_t offset, DoIPAddress &address)
Try read the DoIP address from a byte array.
Definition: DoIPAddress.h:54
constexpr size_t DOIP_ADDRESS_SIZE
Definition: DoIPAddress.h:15
constexpr DoIPAddress MIN_SOURCE_ADDRESS
Definition: DoIPAddress.h:29