Handle a newly arrived packet

def _new_packet_cb(self, packet):
        """Handle a newly arrived packet"""
        chan = packet.channel
        if (chan != 0):
            return
        payload = packet.data[1:]

        if (self.state == GET_TOC_INFO):
            if self._useV2:
                [self.nbr_of_items, self._crc] = struct.unpack(
                    '<HI', payload[:6])
            else:
                [self.nbr_of_items, self._crc] = struct.unpack(
                    '<BI', payload[:5])
            logger.debug('[%d]: Got TOC CRC, %d items and crc=0x%08X',
                         self.port, self.nbr_of_items, self._crc)

            cache_data = self._toc_cache.fetch(self._crc)
            if (cache_data):
                self.toc.toc = cache_data
                logger.info('TOC for port [%s] found in cache' % self.port)
                self._toc_fetch_finished()
            else:
                self.state = GET_TOC_ELEMENT
                self.requested_index = 0
                self._request_toc_element(self.requested_index)

        elif (self.state == GET_TOC_ELEMENT):
            # Always add new element, but only request new if it's not the
            # last one.
            if self._useV2:
                ident = struct.unpack('<H', payload[:2])[0]
            else:
                ident = payload[0]

            if ident != self.requested_index:
                return
            if self._useV2:
                self.toc.add_element(self.element_class(ident, payload[2:]))
            else:
                self.toc.add_element(self.element_class(ident, payload[1:]))
            logger.debug('Added element [%s]', ident)
            if (self.requested_index < (self.nbr_of_items - 1)):
                logger.debug('[%d]: More variables, requesting index %d',
                             self.port, self.requested_index + 1)
                self.requested_index = self.requested_index + 1
                self._request_toc_element(self.requested_index)
            else:  # No more variables in TOC
                self._toc_cache.insert(self._crc, self.toc.toc)
                self._toc_fetch_finished()