| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | // clang-format off |
| 10 | #ifndef _MSC_VER |
| 11 | # include <stdint.h> |
| 12 | #else |
| 13 | # include "stdint.h" |
| 14 | #endif |
| 15 | |
| 16 | #include <string.h> |
| 17 | |
| 18 | #if defined(_WIN32) |
| 19 | # include <winsock2.h> |
| 20 | #else |
| 21 | # include <arpa/inet.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <config.h> |
| 25 | |
| 26 | #include "modbus.h" |
| 27 | |
| 28 | // clang-format on |
| 29 | |
| 30 | /* Sets many bits from a single byte value (all 8 bits of the byte value are |
| 31 | set) */ |
| 32 | void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value) |
| 33 | { |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < 8; i++) { |
| 37 | dest[idx + i] = (value & (1 << i)) ? 1 : 0; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* Sets many bits from a table of bytes (only the bits between idx and |
| 42 | idx + nb_bits are set) */ |
| 43 | void modbus_set_bits_from_bytes(uint8_t *dest, |
| 44 | int idx, |
| 45 | unsigned int nb_bits, |
| 46 | const uint8_t *tab_byte) |
| 47 | { |
| 48 | unsigned int i; |
| 49 | int shift = 0; |
| 50 | |
| 51 | for (i = idx; i < idx + nb_bits; i++) { |
| 52 | dest[i] = tab_byte[(i - idx) / 8] & (1 << shift) ? 1 : 0; |
| 53 | /* gcc doesn't like: shift = (++shift) % 8; */ |
| 54 | shift++; |
| 55 | shift %= 8; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* Gets the byte value from many bits. |
| 60 | To obtain a full byte, set nb_bits to 8. */ |
| 61 | uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits) |
| 62 | { |
| 63 | unsigned int i; |
| 64 | uint8_t value = 0; |
| 65 | |
| 66 | /* A byte contains 8 bits at most */ |
| 67 | if (nb_bits > 8) { |
| 68 | nb_bits = 8; |
| 69 | } |
| 70 | |
| 71 | for (i = 0; i < nb_bits; i++) { |
| 72 | value |= (src[idx + i] << i); |
| 73 | } |
| 74 | |
| 75 | return value; |
| 76 | } |
| 77 | |
| 78 | /* Get a float from 4 bytes (Modbus) without any conversion (ABCD) */ |
| 79 | float modbus_get_float_abcd(const uint16_t *src) |
| 80 | { |
| 81 | float f; |
| 82 | uint32_t i; |
| 83 | uint8_t a, b, c, d; |
| 84 | |
| 85 | // Mind: src contains 16-bit numbers in processor-endianness, hence |
| 86 | // we use shift operations and do not access memory directly |
| 87 | a = (src[0] >> 8) & 0xFF; // high byte of first word |
| 88 | b = (src[0] >> 0) & 0xFF; // low byte of first word |
| 89 | c = (src[1] >> 8) & 0xFF; // high byte of second word |
| 90 | d = (src[1] >> 0) & 0xFF; // low byte of second word |
| 91 | |
| 92 | // we assemble 32bit integer always in abcd order via shift operations |
| 93 | // Cast to uint32_t before shifting: a..d promote to signed int, and |
| 94 | // "a << 24" with a >= 0x80 would overflow a 32-bit signed int (undefined |
| 95 | // behavior). Assembling in unsigned width keeps it well-defined. The same |
| 96 | // applies to the identical assembly in the dcba/badc/cdab variants below. |
| 97 | i = ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d; |
| 98 | memcpy(&f, &i, 4); |
| 99 | |
| 100 | return f; |
| 101 | } |
| 102 | |
| 103 | /* Get a float from 4 bytes (Modbus) in inversed format (DCBA) */ |
| 104 | float modbus_get_float_dcba(const uint16_t *src) |
| 105 | { |
| 106 | float f; |
| 107 | uint32_t i; |
| 108 | uint8_t a, b, c, d; |
| 109 | |
| 110 | // byte order is defined when reading from src: dcba |
| 111 | d = (src[0] >> 8) & 0xFF; |
| 112 | c = (src[0] >> 0) & 0xFF; |
| 113 | b = (src[1] >> 8) & 0xFF; |
| 114 | a = (src[1] >> 0) & 0xFF; |
| 115 | |
| 116 | // we assemble 32bit integer always in abcd order via shift operations |
| 117 | i = ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d; |
| 118 | memcpy(&f, &i, 4); |
| 119 | |
| 120 | return f; |
| 121 | } |
| 122 | |
| 123 | /* Get a float from 4 bytes (Modbus) with swapped bytes (BADC) */ |
| 124 | float modbus_get_float_badc(const uint16_t *src) |
| 125 | { |
| 126 | float f; |
| 127 | uint32_t i; |
| 128 | uint8_t a, b, c, d; |
| 129 | |
| 130 | // byte order is defined when reading from src: badc |
| 131 | b = (src[0] >> 8) & 0xFF; |
| 132 | a = (src[0] >> 0) & 0xFF; |
| 133 | d = (src[1] >> 8) & 0xFF; |
| 134 | c = (src[1] >> 0) & 0xFF; |
| 135 | |
| 136 | // we assemble 32bit integer always in abcd order via shift operations |
| 137 | i = ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d; |
| 138 | memcpy(&f, &i, 4); |
| 139 | |
| 140 | return f; |
| 141 | } |
| 142 | |
| 143 | /* Get a float from 4 bytes (Modbus) with swapped words (CDAB) */ |
| 144 | float modbus_get_float_cdab(const uint16_t *src) |
| 145 | { |
| 146 | float f; |
| 147 | uint32_t i; |
| 148 | uint8_t a, b, c, d; |
| 149 | |
| 150 | // byte order is defined when reading from src: cdab |
| 151 | c = (src[0] >> 8) & 0xFF; |
| 152 | d = (src[0] >> 0) & 0xFF; |
| 153 | a = (src[1] >> 8) & 0xFF; |
| 154 | b = (src[1] >> 0) & 0xFF; |
| 155 | |
| 156 | // we assemble 32bit integer always in abcd order via shift operations |
| 157 | i = ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d; |
| 158 | memcpy(&f, &i, 4); |
| 159 | |
| 160 | return f; |
| 161 | } |
| 162 | |
| 163 | /* DEPRECATED - Get a float from 4 bytes in sort of Modbus format */ |
| 164 | float modbus_get_float(const uint16_t *src) |
| 165 | { |
| 166 | return modbus_get_float_cdab(src); |
| 167 | } |
| 168 | |
| 169 | /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */ |
| 170 | void modbus_set_float_abcd(float f, uint16_t *dest) |
| 171 | { |
| 172 | // Use memcpy for the type conversion: accessing a float through a uint32_t |
| 173 | // lvalue (the old *(uint32_t*)&f trick) violates strict aliasing and is |
| 174 | // undefined behavior. This mirrors the modbus_get_float_* functions and |
| 175 | // compiles to the same load. Same applies to the dcba/badc/cdab variants. |
| 176 | uint32_t i; |
| 177 | uint8_t a, b, c, d; |
| 178 | |
| 179 | memcpy(&i, &f, 4); |
| 180 | |
| 181 | a = (i >> 24) & 0xFF; |
| 182 | b = (i >> 16) & 0xFF; |
| 183 | c = (i >> 8) & 0xFF; |
| 184 | d = (i >> 0) & 0xFF; |
| 185 | |
| 186 | dest[0] = (a << 8) | b; |
| 187 | dest[1] = (c << 8) | d; |
| 188 | } |
| 189 | |
| 190 | /* Set a float to 4 bytes for Modbus with byte and word swap conversion (DCBA) */ |
| 191 | void modbus_set_float_dcba(float f, uint16_t *dest) |
| 192 | { |
| 193 | uint32_t i; |
| 194 | uint8_t a, b, c, d; |
| 195 | |
| 196 | memcpy(&i, &f, 4); |
| 197 | |
| 198 | a = (i >> 24) & 0xFF; |
| 199 | b = (i >> 16) & 0xFF; |
| 200 | c = (i >> 8) & 0xFF; |
| 201 | d = (i >> 0) & 0xFF; |
| 202 | |
| 203 | dest[0] = (d << 8) | c; |
| 204 | dest[1] = (b << 8) | a; |
| 205 | } |
| 206 | |
| 207 | /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */ |
| 208 | void modbus_set_float_badc(float f, uint16_t *dest) |
| 209 | { |
| 210 | uint32_t i; |
| 211 | uint8_t a, b, c, d; |
| 212 | |
| 213 | memcpy(&i, &f, 4); |
| 214 | |
| 215 | a = (i >> 24) & 0xFF; |
| 216 | b = (i >> 16) & 0xFF; |
| 217 | c = (i >> 8) & 0xFF; |
| 218 | d = (i >> 0) & 0xFF; |
| 219 | |
| 220 | dest[0] = (b << 8) | a; |
| 221 | dest[1] = (d << 8) | c; |
| 222 | } |
| 223 | |
| 224 | /* Set a float to 4 bytes for Modbus with word swap conversion (CDAB) */ |
| 225 | void modbus_set_float_cdab(float f, uint16_t *dest) |
| 226 | { |
| 227 | uint32_t i; |
| 228 | uint8_t a, b, c, d; |
| 229 | |
| 230 | memcpy(&i, &f, 4); |
| 231 | |
| 232 | a = (i >> 24) & 0xFF; |
| 233 | b = (i >> 16) & 0xFF; |
| 234 | c = (i >> 8) & 0xFF; |
| 235 | d = (i >> 0) & 0xFF; |
| 236 | |
| 237 | dest[0] = (c << 8) | d; |
| 238 | dest[1] = (a << 8) | b; |
| 239 | } |
| 240 | |
| 241 | /* DEPRECATED - Set a float to 4 bytes in a sort of Modbus format! */ |
| 242 | void modbus_set_float(float f, uint16_t *dest) |
| 243 | { |
| 244 | modbus_set_float_cdab(f, dest); |
| 245 | } |