From 6a596f1087be7b6a0fd5b90a425a82e37ca19f27 Mon Sep 17 00:00:00 2001 From: DJ2LS Date: Tue, 19 Dec 2023 14:08:16 +0100 Subject: [PATCH] helper functions for bitwise flag setting --- modem/arq_session.py | 11 ++++++++++- modem/helpers.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/modem/arq_session.py b/modem/arq_session.py index ac59d776..0fbc3696 100644 --- a/modem/arq_session.py +++ b/modem/arq_session.py @@ -25,6 +25,14 @@ class ARQSession(): } + """ + helpers.set_flag(byte, 'DATA-ACK-NACK', True, FLAG_POSITIONS) + helpers.get_flag(byte, 'DATA-ACK-NACK', FLAG_POSITIONS) + """ + FLAG_POSITIONS = { + 'DATA-ACK-NACK': 0, # Bit position for DATA-ACK-NACK + } + def __init__(self, config: dict, modem, dxcall: str): self.logger = structlog.get_logger(type(self).__name__) self.config = config @@ -83,4 +91,5 @@ class ARQSession(): return self.log(f"Ignoring unknow transition from state {self.state} with frame {frame['frame_type']}") - \ No newline at end of file + + diff --git a/modem/helpers.py b/modem/helpers.py index cee713cd..eb2d1bcd 100644 --- a/modem/helpers.py +++ b/modem/helpers.py @@ -667,4 +667,45 @@ def check_if_file_exists(path): log.warning( "[Modem] [FILE] Lookup failed", e=e, path=path, ) - return False \ No newline at end of file + return False + + +def set_bit(byte, position, value): + """Set the bit at 'position' to 'value' in the given byte.""" + if not 0 <= position <= 7: + raise ValueError("Position must be between 0 and 7") + + if value: + return byte | (1 << position) + else: + return byte & ~(1 << position) + +def get_bit(byte, position): + """Get the boolean value of the bit at 'position' in the given byte.""" + if not 0 <= position <= 7: + raise ValueError("Position must be between 0 and 7") + + return (byte & (1 << position)) != 0 + +def set_flag(byte, flag_name, value, flag_dict): + """Set the flag in the byte according to the flag dictionary. + + # Define a dictionary mapping flag names to their bit positions + flag_dict = { + 'FLAG1': 0, # Bit position for FLAG1 + 'FLAG2': 1, # Bit position for FLAG2, etc. + 'FLAG3': 2 + } + + """ + if flag_name not in flag_dict: + raise ValueError(f"Unknown flag name: {flag_name}") + position = flag_dict[flag_name] + return set_bit(byte, position, value) + +def get_flag(byte, flag_name, flag_dict): + """Get the value of the flag from the byte according to the flag dictionary.""" + if flag_name not in flag_dict: + raise ValueError(f"Unknown flag name: {flag_name}") + position = flag_dict[flag_name] + return get_bit(byte, position)