def setbuf(self, srcbuf, dtag): ''' srcbuf: holder of the data to be sent. dtag: dtag number. win_head: indicating the head of current window. it is held until the bitmap check is completed. pos: indicating the head of the data to be sent. win_head pos | | v v srcbuf |......................... ''' if type(srcbuf) == str: self.srcbuf = bytearray(srcbuf, "utf-8") elif type(srcbuf) in [bytearray, bytes]: self.srcbuf = bytearray(srcbuf) else: raise TypeError("srcbuf must be str, bytes or bytearray.") # self.win_head = 0 self.pos = 0 self.dtag = dtag self.mic, self.mic_size = self.R.C.mic_func.get_mic(self.srcbuf) self.win = 0 self.__init_window() self.state = sfs.fragment_state(STATE, logger=self.logger) self.state.set(STATE.INIT)
def __init__(self, fgh, logger=default_logger): self.logger = logger self.R = fgh.R self.win = fgh.win self.bitmap = 0 self.mic = None self.win_state = sfs.fragment_state(STATE, logger=self.logger) self.win_state.set(STATE.INIT) # fragement_list is a dict because FCN is not always sequentially decremented. self.fragment_list = {} # below list is used to hold the fragments in NO_ACK because FCN is always zero. self.fragment_list_no_ack = []
def setbuf(self, srcbuf, dtag): ''' srcbuf: holder of the data to be sent. dtag: dtag number. win_head: indicating the head position in bits of current window. it is held until the bitmap check is completed. pos: indicating the head of the data in bits to be sent. an L2 payload. |<----------- L2 payload size ---------->| a fragment except the last one: |...............|........................| |<------------->|<---------------------->| SCHC Frag. SCHC Frag. header payload the last fragment. |...............|.................|....| |<------------->|<--------------->|<-->| SCHC Frag. SCHC Frag. Padding as neede. header payload (for align to a byte, less than 8 bits) next win_head pos pos | | | v v v srcbuf |...............................| | | |----- total_payload_size ------| | -->|........|<-- max_payload_size -->|........|<-- max_payload_size payload_size -->|....|<-- padding -->|...|<-- byte alignment. ## MIC calculation: - not include the MIC field itself. - the implementation assumes that srcbuf is aligned to a byte. - the implementation assumes that the MIC size is multiple of 8-bits. - therefore, any padding are not needed to calculate the MIC. e.g. header size is 10 bits. MIC size is 32 bits. message size is 40 bits. 1. L2 size is 32 bits. 1st frag consists of 10 bits header + 22 bits stub of message. 2nd frag consists of 10 bits header + 18 bits rest of message. 3rd frag has only MIC of 32 bits. 2. L2 size is 120 bits. 1st frag consists of 10 bits header + 40 bits message + 32 bits MIC. ''' if type(srcbuf) == str: self.srcbuf = bytearray(srcbuf, "utf-8") elif type(srcbuf) in [bytearray, bytes]: self.srcbuf = bytearray(srcbuf) else: raise TypeError("srcbuf must be str, bytes or bytearray.") # self.win_head = 0 self.pos = 0 self.dtag = dtag self.mic_size = self.R.C.mic_func.get_mic_size() self.mic = None self.tx_payload_list = [] # payload holder to calculate MIC. self.win = 0 self.header_size = (self.R.C.rid_size + self.R.dtag_size + self.R.win_size + self.R.fcn_size) self.total_payload_size = len(self.srcbuf) * 8 self.rest_payload_size = self.total_payload_size self.__init_window() self.state = sfs.fragment_state(STATE, logger=self.logger) self.state.set(STATE.INIT)