Пример #1
0
    def __init__(self, profile, header):
        rule_id = header.RULE_ID
        dtag = header.DTAG
        w = header.W

        header = Header(profile=profile,
                        rule_id=rule_id,
                        dtag=dtag,
                        w=w,
                        fcn='',
                        c='1')
        self.header = header
        padding = ''
        self.padding = padding
        # if the Header does not end at an L2 Word boundary,
        # append bits set to 1 as needed to reach the next L2 Word boundary.
        while len(header.string + padding) % profile.L2_WORD_SIZE != 0:
            padding += '1'

        # append exactly one more L2 Word with bits all set to ones.
        padding += '1' * profile.L2_WORD_SIZE

        #must check this method
        # sigfox ACK must be of downlink MTU size
        while len(header.string + padding) < profile.DOWNLINK_MTU:
            padding += '1'

        super().__init__(profile,
                         rule_id,
                         dtag,
                         w,
                         c='1',
                         bitmap='',
                         padding=padding)
Пример #2
0
    def __init__(self, profile, fragment):
        self.profile = profile

        self.header_length = profile.HEADER_LENGTH
        self.rule_id_size = profile.RULE_ID_SIZE
        self.t = profile.T
        self.n = profile.N
        self.m = profile.M

        header = zfill(
            str(bin(int.from_bytes(fragment[0], 'big')))[2:],
            self.header_length)
        payload = fragment[1]

        rule_id = str(header[:self.rule_id_size])
        dtag = str(header[self.rule_id_size:self.rule_id_size + self.t])
        window = str(header[self.rule_id_size + self.t:self.rule_id_size +
                            self.t + self.m])
        fcn = str(header[self.rule_id_size + self.t +
                         self.m:self.rule_id_size + self.t + self.m + self.n])
        c = ""

        self.header = Header(self.profile, rule_id, dtag, window, fcn, c)

        self.payload = payload
Пример #3
0
    def fragment(self):
        payload_max_length = int(
            (self.profile.MTU - self.profile.HEADER_LENGTH) / 8)
        message = self.schc_packet
        fragment_list = []
        n = self.profile.N
        m = self.profile.M
        number_of_fragments = int(
            ceil(float(len(message)) / payload_max_length))

        print("[FRGM] Fragmenting message into " + str(number_of_fragments) +
              " pieces...")

        for i in range(number_of_fragments):
            w = zfill(bin(int(floor((i / (2**n - 1) % (2**m)))))[2:], 2)
            fcn = zfill(bin((2**n - 2) - (i % (2**n - 1)))[2:], 3)

            fragment_payload = message[i * payload_max_length:(i + 1) *
                                       payload_max_length]

            if len(fragment_payload) < payload_max_length:
                header = Header(self.profile,
                                rule_id="00",
                                dtag="0",
                                w=w,
                                fcn="111",
                                c=0)

            else:
                header = Header(self.profile,
                                rule_id="00",
                                dtag="0",
                                w=w,
                                fcn=fcn,
                                c=0)

            fragment = [header.bytes, fragment_payload]
            # print("[" + header.string + "]" + str(fragment_payload))
            fragment_list.append(fragment)

        print("[FRGM] Fragmentation complete.")

        return fragment_list
Пример #4
0
    def __init__(self, profile, header):
        self.profile = profile
        rule_id = header.RULE_ID
        dtag = header.DTAG
        w = header.W
        fcn = "1" * profile.N
        self.header = Header(profile, rule_id, dtag, w, fcn)

        while len(self.header.string + self.padding) < profile.UPLINK_MTU:
            self.padding += '0'

        super().__init__(profile, [bitstring_to_bytes(rule_id + dtag + w + fcn), self.padding.encode()])
Пример #5
0
    def __init__(self, profile, rule_id, dtag, w):
        self.profile = profile

        self.header = Header(profile=profile,
                             rule_id=rule_id,
                             dtag=dtag,
                             w=w,
                             fcn="1" * profile.N,
                             c="")
        while len(self.header.string + self.padding) < profile.MTU:
            self.padding += '0'

        print(self.header.string + self.padding)
Пример #6
0
    def __init__(self, profile, rule_id, dtag, w):
        self.profile = profile

        self.header = Header(profile=profile,
                             rule_id=rule_id,
                             dtag=dtag,
                             w=w,
                             fcn='',
                             c='1')

        # if the Header does not end at an L2 Word boundary, append bits set to 1 as needed to reach the next L2 Word boundary.
        while len(self.header.string +
                  self.padding) % profile.L2_WORD_SIZE != 0:
            self.padding += '1'

        # append exactly one more L2 Word with bits all set to ones.
        self.padding += '1' * profile.L2_WORD_SIZE
Пример #7
0
    def fragment(self):
        payload_max_length = int(
            (self.profile.MTU - self.profile.HEADER_LENGTH) / 8)
        message = self.schc_packet
        fragment_list = []
        n = self.profile.N
        m = self.profile.M
        number_of_fragments = int(
            ceil(float(len(message)) / payload_max_length))
        if len(message) == 0:
            number_of_fragments = 1  #This is used to send 0-Payload SCHC Packets

        print("[FRGM] Fragmenting message into " + str(number_of_fragments) +
              " pieces...")

        #check if the packet size can be transmitted or not
        if len(fragment_list) > (2**self.profile.M) * self.profile.WINDOW_SIZE:
            print(len(fragment_list))
            print((2**self.profile.M) * self.profile.WINDOW_SIZE)
            print(
                "The SCHC packet cannot be fragmented in 2 ** M * WINDOW_SIZE fragments or less. A Rule ID cannot be selected."
            )
            # What does this mean?
            # Sending packet does not fit (should be tested in fragmentation)

        for i in range(number_of_fragments):
            w = zfill(
                bin(int(floor((i / (2**n - 1) % (2**m)))))[2:], self.profile.M)
            fcn = zfill(bin((2**n - 2) - (i % (2**n - 1)))[2:], self.profile.N)

            fragment_payload = message[i * payload_max_length:(i + 1) *
                                       payload_max_length]

            if len(self.schc_packet) <= 300:
                if len(fragment_payload) < payload_max_length or i == (
                        len(range(number_of_fragments)) - 1):
                    header = Header(self.profile,
                                    rule_id="000",
                                    dtag="",
                                    w=w,
                                    fcn="111",
                                    c=0)

                else:
                    header = Header(self.profile,
                                    rule_id="000",
                                    dtag="",
                                    w=w,
                                    fcn=fcn,
                                    c=0)
            else:
                if len(fragment_payload) < payload_max_length or i == (
                        len(range(number_of_fragments)) - 1):
                    header = Header(self.profile,
                                    rule_id="00100000",
                                    dtag="",
                                    w=w,
                                    fcn="11111",
                                    c=0)

                else:
                    header = Header(self.profile,
                                    rule_id="00100000",
                                    dtag="",
                                    w=w,
                                    fcn=fcn,
                                    c=0)

            fragment = [header.bytes, fragment_payload]
            # print("[" + header.string + "]" + str(fragment_payload))
            fragment_list.append(fragment)

        print("[FRGM] Fragmentation complete.")

        return fragment_list
Пример #8
0
# direction = input("DIRECTION: ")
# mode = input("MODE: ")
# mtu = input("MTU: ")
# if protocol_name == "SIGFOX":
# 	protocol = Sigfox(direction, mode)

profile = Sigfox("UPLINK", "ACK ON ERROR")
MTU = profile.MTU

with open("pollito_14x14.png", "rb") as data:
    f = data.read()
    payload = bytearray(f)

print("The payload to be transmitted is: " + str(payload))

test_header = Header(profile, rule_id="RR", dtag="D", w="WW", fcn="000", c=0)
test_header.test()

fragmenter = Fragmenter(profile, payload)
fragment_list = fragmenter.fragment()

print("Fragments:")
for fragment in fragment_list:
    print(fragment)

if Fragment(profile, fragment_list[-1]).is_all_1() is True:
    print("All-1 condition satisfied")
else:
    print("All-1 condition NOT satisfied")

# print("Rebuilding message...")