| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2008 Adriano Monteiro Marques. |
|---|
| 5 | # |
|---|
| 6 | # Author: Bartosz SKOWRON <getxsick at gmail dot com> |
|---|
| 7 | # |
|---|
| 8 | # This library is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU Lesser General Public License as published |
|---|
| 10 | # by the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This library is distributed in the hope that it will be useful, but |
|---|
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 15 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
|---|
| 16 | # License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU Lesser General Public License |
|---|
| 19 | # along with this library; if not, write to the Free Software Foundation, |
|---|
| 20 | # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | import struct |
|---|
| 23 | |
|---|
| 24 | import umpa.utils.bits |
|---|
| 25 | from umpa.protocols._consts import BYTE |
|---|
| 26 | |
|---|
| 27 | class Packet(object): |
|---|
| 28 | """You have to use this class to build a completely packets. |
|---|
| 29 | An instance of the class should contains protocols which |
|---|
| 30 | you want to send.""" |
|---|
| 31 | |
|---|
| 32 | def __init__(self, *protos): |
|---|
| 33 | """You can include some protocols objects in construtor |
|---|
| 34 | or use include() method later. |
|---|
| 35 | """ |
|---|
| 36 | self.protos = [] |
|---|
| 37 | self._add_new_protocols(protos) |
|---|
| 38 | self.raw = None |
|---|
| 39 | self.bits = 0 |
|---|
| 40 | |
|---|
| 41 | def __str__(self): |
|---|
| 42 | """Print in human-readable style a content of the packet.""" |
|---|
| 43 | print "Not implemented yet." |
|---|
| 44 | |
|---|
| 45 | def print_protocols(self): |
|---|
| 46 | """Print all included protocols into the packet.""" |
|---|
| 47 | for p in self.protos: |
|---|
| 48 | print p |
|---|
| 49 | |
|---|
| 50 | def include(self, *protos): |
|---|
| 51 | """Add protocols into packet. |
|---|
| 52 | """ |
|---|
| 53 | self._add_new_protocols(protos) |
|---|
| 54 | |
|---|
| 55 | def _add_new_protocols(self, protos): |
|---|
| 56 | for p in protos: |
|---|
| 57 | self.protos.append(p) |
|---|
| 58 | |
|---|
| 59 | def get_raw(self): |
|---|
| 60 | """Return raw packet, in bit-mode (big-endian).""" |
|---|
| 61 | |
|---|
| 62 | self.raw = 0 |
|---|
| 63 | self.bits = 0 |
|---|
| 64 | proto_id = 0 |
|---|
| 65 | for proto in reversed(self.protos): |
|---|
| 66 | raw_proto, bit_proto = proto.get_raw(tuple(self.protos), self.bits) |
|---|
| 67 | self.raw |= raw_proto << self.bits |
|---|
| 68 | self.bits += bit_proto |
|---|
| 69 | # split into chunks |
|---|
| 70 | # we make it because we need string for socket object |
|---|
| 71 | # so after that we pack it by struct module.pack() |
|---|
| 72 | byte_chunks = umpa.utils.bits.split_into_chunks(self.raw, self.bits) |
|---|
| 73 | return struct.pack('!' + 'B'*(self.bits/BYTE), *byte_chunks) |
|---|