Skip to content

Commit

Permalink
Fix Endian of Parsing (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy-van-Jerry committed Aug 30, 2023
1 parent 5ca3cfe commit a8296df
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/pkt_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Pkt_Parse {
private:
uint32_t bytesToUInt32(const std::array<std::byte, 4>& bytes);

uint32_t strToUInt32(const std::string& str);
uint32_t strToUInt32(const std::string& str, bool swap = true);

} data;
};
Expand Down
8 changes: 6 additions & 2 deletions reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ def print_table(df):
CIR_I = []
CIR_Q = []
for i in range(0, 256):
CIR_I.append(np.int16(CIR[i] >> 16))
CIR_Q.append(np.int16(CIR[i] & 0xffff))
# Extract the 16-bit signed integer I and Q components from the 32-bit value
Q_component = np.int16(CIR[i] & 0xffff) # Mask the lower 16 bits
I_component = np.int16(CIR[i] >> 16) # Shift and mask the higher 16 bits

CIR_I.append(I_component)
CIR_Q.append(Q_component)

# plot
plt.figure(figsize=(10, 5))
Expand Down
14 changes: 7 additions & 7 deletions src/pkt_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ bool Pkt_Parse::parse() {

Pkt_Parse::Data::Data(const Pkt_Payload& payload) { // payload is an std::array of std::byte.
// timestamp is the bytes 2-5
timestamp = strToUInt32(payload.substr(2, 4));
timestamp = strToUInt32(payload.substr(2, 4), false);
// noise is the bytes 6-7
noise = strToUInt32(std::string(2, '\0').append(payload.substr(6, 2)));
for (size_t i = 0; i != CIR_LENGTH; ++i) { CIR[i] = strToUInt32(payload.substr(CIR_BEGIN_BYTE_N + i, 4)); }
for (size_t i = 0; i != CIR_LENGTH; ++i) { CIR[i] = strToUInt32(payload.substr(CIR_BEGIN_BYTE_N + i * 4, 4)); }
// std::cout << timestamp << std::endl;
}

Expand All @@ -79,10 +79,10 @@ uint32_t Pkt_Parse::Data::bytesToUInt32(const std::array<std::byte, 4>& bytes) {
return value;
}

uint32_t Pkt_Parse::Data::strToUInt32(const std::string& str) {
uint32_t Pkt_Parse::Data::strToUInt32(const std::string& str, bool swap) {
assert(str.size() == 4); // str must be length of 4
return (static_cast<uint32_t>(static_cast<uint8_t>(str[0])) << 24) |
(static_cast<uint32_t>(static_cast<uint8_t>(str[1])) << 16) |
(static_cast<uint32_t>(static_cast<uint8_t>(str[2])) << 8) |
static_cast<uint32_t>(static_cast<uint8_t>(str[3]));
return (static_cast<uint32_t>(static_cast<uint8_t>(str[swap ? 1 : 0])) << 24) |
(static_cast<uint32_t>(static_cast<uint8_t>(str[swap ? 0 : 1])) << 16) |
(static_cast<uint32_t>(static_cast<uint8_t>(str[swap ? 3 : 2])) << 8) |
static_cast<uint32_t>(static_cast<uint8_t>(str[swap ? 2 : 3]));
}
73 changes: 70 additions & 3 deletions tests/udp_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,74 @@
import time
import argparse

message = b"\x31\xd4\x91\xb2\x34\x01\xd2\x04" + b"\x00" * 1032

print(len(message))
# message = b"\x31\xd4\x91\xb2\x34\x01\xd2\x04" + b"\x00" * 1032
message = \
b"\x31\xd4\x91\xb2\x34\x01" \
b"\xd2\x04\x31\xd4\x91\xb2\x34\x01\xd2\x04\x02\x00\xfa\xff\x02\x00" \
b"\xfb\xff\xfc\xff\x09\x00\x05\x00\x0f\x00\xff\xff\x8e\xff\x07\x00" \
b"\x08\x00\xfd\xff\xad\xff\x03\x00\x05\x00\xfe\xff\xcf\xff\x01\x00" \
b"\xff\xff\xfc\xff\x73\x00\xfd\xff\x05\x00\xfa\xff\xf1\xff\x0d\x00" \
b"\xff\xff\xfa\xff\xdd\xff\xf8\xff\xfa\xff\xff\xff\xca\xff\xfe\xff" \
b"\xf8\xff\xfb\xff\xbf\xff\xf5\xff\xf1\xff\xfd\xff\x73\x00\x03\x00" \
b"\xef\xff\xfd\xff\x7f\x00\xee\xff\x04\x00\xff\xff\x48\x00\xff\xff" \
b"\x05\x00\xfa\xff\xf0\xff\x01\x00\xfa\xff\xff\xff\x3e\x00\x0a\x00" \
b"\xfc\xff\xfb\xff\x24\x00\x01\x00\xfb\xff\xff\xff\xe1\xff\xfc\xff" \
b"\x01\x00\xfe\xff\xc1\xff\xfe\xff\xfd\xff\xf8\xff\x30\x00\xfb\xff" \
b"\xfc\xff\xfe\xff\x2b\x00\x04\x00\x03\x00\x01\x00\x38\x00\xfb\xff" \
b"\x03\x00\xf9\xff\xf3\xff\xfa\xff\xff\xff\xfe\xff\x15\x00\x00\x00" \
b"\xff\xff\xfc\xff\x22\x00\xff\xff\x00\x00\xf9\xff\x0f\x00\xfe\xff" \
b"\x00\x00\xfa\xff\x00\x00\xfc\xff\x01\x00\xfd\xff\x03\x00\xfc\xff" \
b"\xff\xff\xfc\xff\x00\x00\xfe\xff\x03\x00\xff\xff\x10\x00\xfd\xff" \
b"\xff\xff\xfb\xff\xfe\xff\xfd\xff\xff\xff\x00\x00\x03\x00\xfc\xff" \
b"\xfe\xff\xfa\xff\xff\xff\xfe\xff\xff\xff\xfe\xff\x03\x00\xfc\xff" \
b"\xfe\xff\xfd\xff\xfe\xff\xfb\xff\xfd\xff\x00\x00\x02\x00\xfc\xff" \
b"\xff\xff\xf8\xff\x00\x00\xfc\xff\x00\x00\xfc\xff\x00\x00\xfd\xff" \
b"\x01\x00\xfa\xff\xfe\xff\xfd\xff\xfe\xff\xff\xff\xfe\xff\xff\xff" \
b"\xfe\xff\xfd\xff\x00\x00\xfc\xff\xfe\xff\xfc\xff\x00\x00\xfc\xff" \
b"\xfd\xff\xfd\xff\xfb\xff\xff\xff\x00\x00\x00\x00\xff\xff\x00\x00" \
b"\xfd\xff\xf8\xff\x00\x00\xfb\xff\xfe\xff\xfe\xff\x03\x00\xfe\xff" \
b"\xff\xff\xfb\xff\xfe\xff\xfb\xff\x01\x00\xfe\xff\xff\xff\xfd\xff" \
b"\x01\x00\xfb\xff\xff\xff\xfb\xff\xfe\xff\xfd\xff\x02\x00\xfe\xff" \
b"\xfb\xff\xfa\xff\xfd\xff\xfa\xff\xfe\xff\xfe\xff\x00\x00\xfd\xff" \
b"\x01\x00\xf6\xff\x09\x00\xf6\xff\xfc\xff\xfc\xff\x00\x00\xfe\xff" \
b"\xfd\xff\xff\xff\xf4\xff\x04\x00\xff\xff\x02\x00\xff\xff\xf5\xff" \
b"\xfc\xff\x16\x00\xec\xff\xc0\xff\xfc\xff\xfd\xff\x02\x00\x01\x00" \
b"\xf9\xff\xf2\xff\xe6\xff\xc2\xff\xfe\xff\x17\x00\x04\x00\xf3\xff" \
b"\xfd\xff\x02\x00\x08\x00\xff\xff\xff\xff\xdf\x00\x00\x00\x09\xf7" \
b"\xfe\xff\xf5\xff\xf8\xff\xe8\xff\x03\x00\xff\xff\x02\x00\xf8\xff" \
b"\xfd\xff\xef\xff\x01\x00\x07\x00\x00\x00\xf3\xff\x02\x00\xfb\xff" \
b"\xfe\xff\xf6\xff\xff\xff\xf7\xff\xff\xff\xf6\xff\xff\xff\xfc\xff" \
b"\xfd\xff\xf8\xff\x00\x00\xf9\xff\x04\x00\xe2\xff\x01\x00\xf8\xff" \
b"\x03\x00\xfa\xff\xfc\xff\xfd\xff\xff\xff\xfc\xff\x03\x00\xff\xff" \
b"\xff\xff\xfa\xff\x01\x00\xfb\xff\x00\x00\xf9\xff\x04\x00\xf9\xff" \
b"\xfe\xff\xfd\xff\xfc\xff\xfe\xff\x02\x00\xff\xff\x01\x00\x00\x00" \
b"\xff\xff\xfa\xff\xff\xff\xfe\xff\x02\x00\x00\x00\x00\x00\xfc\xff" \
b"\xfd\xff\xfd\xff\xff\xff\xff\xff\x01\x00\xff\xff\x01\x00\xff\xff" \
b"\xfe\xff\xfb\xff\xfe\xff\xff\xff\x01\x00\x03\x00\xff\xff\xfd\xff" \
b"\x02\x00\xff\xff\x01\x00\xfe\xff\x01\x00\x01\x00\x02\x00\x01\x00" \
b"\xff\xff\xfe\xff\x00\x00\xfe\xff\x01\x00\x02\x00\x00\x00\x00\x00" \
b"\x00\x00\x02\x00\xfe\xff\xfd\xff\x00\x00\x02\x00\xfd\xff\x03\x00" \
b"\x00\x00\x01\x00\x00\x00\x02\x00\x04\x00\x02\x00\x05\x00\x03\x00" \
b"\x00\x00\x02\x00\xed\xff\x04\x00\x04\x00\x02\x00\xd9\xff\x09\x00" \
b"\xfd\xff\x03\x00\xf3\xff\xfc\xff\xfe\xff\x03\x00\x00\x00\x03\x00" \
b"\xff\xff\xfc\xff\xef\xff\x06\x00\x02\x00\x01\x00\xe2\xff\xfc\xff" \
b"\x05\x00\x04\x00\xb8\xff\x03\x00\xfb\xff\xfd\xff\x0c\x00\x02\x00" \
b"\xff\xff\xff\xff\xb9\xff\x03\x00\x03\x00\x03\x00\xd7\xff\x15\x00" \
b"\x02\x00\x00\x00\xc1\xff\x09\x00\xff\xff\x02\x00\x31\x00\xfe\xff" \
b"\x04\x00\xfe\xff\x23\x00\xf2\xff\x10\x00\xff\xff\x7a\xff\x06\x00" \
b"\x07\x00\xfe\xff\x94\xff\xfa\xff\xfc\xff\x03\x00\x09\x00\x00\x00" \
b"\xfb\xff\xff\xff\xa5\xff\xf9\xff\xfe\xff\x01\x00\x25\x00\x12\x00" \
b"\xfe\xff\x01\x00\xff\xff\x03\x00\x0a\x00\x03\x00\x32\x00\x02\x00" \
b"\x07\x00\xff\xff\x44\x00\xfe\xff\xf9\xff\xfc\xff\x5d\x00\x08\x00" \
b"\xf3\xff\xfd\xff\x6b\x00\x08\x00\xfe\xff\xff\xff\xa1\xff\x03\x00" \
b"\xf6\xff\xfd\xff\x3e\x00\xfe\xff\x08\x00\x03\x00\xd2\xff\x07\x00" \
b"\x03\x00\x00\x00\xd4\xff\xf8\xff\x03\x00\xff\xff\xfc\xff\x05\x00" \
b"\xf2\xff\xff\xff\x09\x00\x02\x00\x07\x00\x01\x00\xf0\xff\xfc\xff" \
b"\x03\x00\xfd\xff\xdb\xff\xff\xff\x02\x00\x03\x00\xfa\xff\x00\x00" \
b"\x01\x00\xff\xff\x02\x00\xfa\xff\xfe\xff\x00\x00\x17\x00\xfd\xff" \
b"\xff\xff\xfd\xff\x07\x00\x01\x00\xff\xff\x00\x00\xf6\xff\x01\x00" \
b"\xfd\xff\xff\xff\x05\x00\xfa\xff\xff\xff\xff\xff\x12\x00\x05\x00" \
b"\x00\x00\xfe\xff\x25\x00\xfc\xff\x06\x00"

def send_udp_packet(host, port, data, quiet=False):
try:
Expand All @@ -31,6 +96,8 @@ def main():
parser.add_argument("-i", "--interval", type=float, default=0.1, help="Packet sending interval in seconds")
args = parser.parse_args()

print(len(message))

try:
while True:
send_udp_packet(args.host, args.port, message, args.quiet)
Expand Down

0 comments on commit a8296df

Please sign in to comment.