Пример #1
0
 def encode(self):
     parms = self.parent.get_attr('TCA_TBF_PARMS') or \
         self.parent.get_attr('TCA_HTB_PARMS') or \
         self.parent.get_attr('TCA_POLICE_TBF')
     if parms is not None:
         self.value = getattr(parms, self.__class__.__name__)
         self['value'] = struct.pack('I' * 256, *self.value)
     nla.encode(self)
Пример #2
0
 def encode(self):
     parms = self.parent.get_encoded('TCA_TBF_PARMS') or \
         self.parent.get_encoded('TCA_HTB_PARMS') or \
         self.parent.get_encoded('TCA_POLICE_TBF')
     if parms is not None:
         self.value = getattr(parms, self.__class__.__name__)
         self['value'] = struct.pack('I' * 256,
                                     *(int(x) for x in self.value))
     nla.encode(self)
Пример #3
0
 def encode(self):
     self.rtab = None
     self.ptab = None
     if self.get('rate', False):
         self.rtab = self.calc_rtab('rate')
     if self.get('peak', False):
         self.ptab = self.calc_rtab('peak')
     if self.get('ceil', False):
         self.ctab = self.calc_rtab('ceil')
     nla.encode(self)
Пример #4
0
 def encode(self):
     self.rtab = None
     self.ptab = None
     if self.get('rate', False):
         self.rtab = self.calc_rtab('rate')
     if self.get('peak', False):
         self.ptab = self.calc_rtab('peak')
     if self.get('ceil', False):
         self.ctab = self.calc_rtab('ceil')
     nla.encode(self)
Пример #5
0
            def encode(self):
                '''
                'keys': ['0x0006/0x00ff+8',
                         '0x0000/0xffc0+2',
                         '0x5/0xf+0',
                         '0x10/0xff+33']

                => 00060000/00ff0000 + 8
                   05000000/0f00ffc0 + 0
                   00100000/00ff0000 + 32

                '''

                def cut_field(key, separator):
                    '''
                    split a field from the end of the string
                    '''
                    field = '0'
                    pos = key.find(separator)
                    new_key = key
                    if pos > 0:
                        field = key[pos + 1:]
                        new_key = key[:pos]
                    return (new_key, field)

                # 'header' array to pack keys to
                header = [(0, 0) for i in range(256)]

                keys = []
                # iterate keys and pack them to the 'header'
                for key in self['keys']:
                    # TODO tags: filter
                    (key, nh) = cut_field(key, '@')  # FIXME: do not ignore nh
                    (key, offset) = cut_field(key, '+')
                    offset = int(offset, 0)
                    # a little trick: if you provide /00ff+8, that
                    # really means /ff+9, so we should take it into
                    # account
                    (key, mask) = cut_field(key, '/')
                    if mask[:2] == '0x':
                        mask = mask[2:]
                        while True:
                            if mask[:2] == '00':
                                offset += 1
                                mask = mask[2:]
                            else:
                                break
                        mask = '0x' + mask
                    mask = int(mask, 0)
                    value = int(key, 0)
                    bits = 24
                    if mask == 0 and value == 0:
                        key = self.u32_key(self.buf)
                        key['key_off'] = offset
                        key['key_mask'] = mask
                        key['key_val'] = value
                        keys.append(key)
                    for bmask in struct.unpack('4B', struct.pack('>I', mask)):
                        if bmask > 0:
                            bvalue = (value & (bmask << bits)) >> bits
                            header[offset] = (bvalue, bmask)
                            offset += 1
                        bits -= 8

                # recalculate keys from 'header'
                key = None
                value = 0
                mask = 0
                for offset in range(256):
                    (bvalue, bmask) = header[offset]
                    if bmask > 0 and key is None:
                        key = self.u32_key(self.buf)
                        key['key_off'] = offset
                        key['key_mask'] = 0
                        key['key_val'] = 0
                        bits = 24
                    if key is not None and bits >= 0:
                        key['key_mask'] |= bmask << bits
                        key['key_val'] |= bvalue << bits
                        bits -= 8
                        if (bits < 0 or offset == 255):
                            keys.append(key)
                            key = None

                assert keys
                self['nkeys'] = len(keys)
                # FIXME: do not hardcode flags :)
                self['flags'] = 1
                start = self.buf.tell()

                nla.encode(self)
                for key in keys:
                    key.encode()
                self.update_length(start)
Пример #6
0
 def encode(self):
     self["value"] = state_by_name[self.value]
     nla.encode(self)
Пример #7
0
 def encode(self):
     self['value'] = state_by_name[self.value]
     nla.encode(self)
Пример #8
0
            def encode(self):
                '''
                'keys': ['0x0006/0x00ff+8',
                         '0x0000/0xffc0+2',
                         '0x5/0xf+0',
                         '0x10/0xff+33']

                => 00060000/00ff0000 + 8
                   05000000/0f00ffc0 + 0
                   00100000/00ff0000 + 32

                '''
                def cut_field(key, separator):
                    '''
                    split a field from the end of the string
                    '''
                    field = '0'
                    pos = key.find(separator)
                    new_key = key
                    if pos > 0:
                        field = key[pos + 1:]
                        new_key = key[:pos]
                    return (new_key, field)

                # 'header' array to pack keys to
                header = [(0, 0) for i in range(256)]

                keys = []
                # iterate keys and pack them to the 'header'
                for key in self['keys']:
                    # TODO tags: filter
                    (key, nh) = cut_field(key, '@')  # FIXME: do not ignore nh
                    (key, offset) = cut_field(key, '+')
                    offset = int(offset, 0)
                    # a little trick: if you provide /00ff+8, that
                    # really means /ff+9, so we should take it into
                    # account
                    (key, mask) = cut_field(key, '/')
                    if mask[:2] == '0x':
                        mask = mask[2:]
                        while True:
                            if mask[:2] == '00':
                                offset += 1
                                mask = mask[2:]
                            else:
                                break
                        mask = '0x' + mask
                    mask = int(mask, 0)
                    value = int(key, 0)
                    bits = 24
                    if mask == 0 and value == 0:
                        key = self.u32_key(self.buf)
                        key['key_off'] = offset
                        key['key_mask'] = mask
                        key['key_val'] = value
                        keys.append(key)
                    for bmask in struct.unpack('4B', struct.pack('>I', mask)):
                        if bmask > 0:
                            bvalue = (value & (bmask << bits)) >> bits
                            header[offset] = (bvalue, bmask)
                            offset += 1
                        bits -= 8

                # recalculate keys from 'header'
                key = None
                value = 0
                mask = 0
                for offset in range(256):
                    (bvalue, bmask) = header[offset]
                    if bmask > 0 and key is None:
                        key = self.u32_key(self.buf)
                        key['key_off'] = offset
                        key['key_mask'] = 0
                        key['key_val'] = 0
                        bits = 24
                    if key is not None and bits >= 0:
                        key['key_mask'] |= bmask << bits
                        key['key_val'] |= bvalue << bits
                        bits -= 8
                        if (bits < 0 or offset == 255):
                            keys.append(key)
                            key = None

                assert keys
                self['nkeys'] = len(keys)
                # FIXME: do not hardcode flags :)
                self['flags'] = 1
                start = self.buf.tell()

                nla.encode(self)
                for key in keys:
                    key.encode()
                self.update_length(start)
Пример #9
0
 def encode(self):
     self['value'] = state_by_name[self.value]
     nla.encode(self)