annotate zbmux.py @ 12:a3ec3f2988ac

Initial commit of zbmux. This program demultiplexes packets from remote ZB modules to feed into a log file. Also allows user(s) to telnet to a port to talk to a particular module. Needs more work. eg - TCP connections should be in raw mode, not line. - There is no way to access statistics for each module.
author darius@Inchoate
date Tue, 13 Jan 2009 12:17:02 +1030
parents
children ac60a9244bdf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
1 #
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
2 # Mux the ZB module to TCP ports
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
3 #
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
4 # Copyright (c) 2009
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
5 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
6 #
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
7 # Redistribution and use in source and binary forms, with or without
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
8 # modification, are permitted provided that the following conditions
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
9 # are met:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
10 # 1. Redistributions of source code must retain the above copyright
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
11 # notice, this list of conditions and the following disclaimer.
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
12 # 2. Redistributions in binary form must reproduce the above copyright
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
13 # notice, this list of conditions and the following disclaimer in the
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
14 # documentation and/or other materials provided with the distribution.
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
15 #
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
16 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
19 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
26 # SUCH DAMAGE.
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
27 #
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
28
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
29 from twisted.internet.serialport import SerialPort
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
30 from twisted.internet.protocol import Protocol
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
31 from twisted.protocols import basic
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
32 from twisted.internet import protocol, reactor
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
33 from twisted.python import log
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
34 import sys, zb, logging, logging.handlers
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
35
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
36 portname = '/dev/cuaU0'
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
37 baudrate = 38400
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
38 lognamebase = '/tmp/zbmux-%d.log'
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
39 baseport = 1080
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
40 zbids = [1, 2]
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
41
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
42 class ZBClient(basic.LineReceiver):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
43 """Client for the TCP connection"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
44
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
45 def connectionMade(self):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
46 log.msg("Got new client")
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
47 self.factory.clients.append(self)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
48 if self.factory.lastline != None:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
49 self.message(self.factory.lastline)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
50
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
51 def connectionLost(self, reason):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
52 log.msg("Lost a client")
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
53 self.factory.clients.remove(self)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
54
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
55 def lineReceived(self, line):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
56 """Got a line - send it to our ZB module"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
57 #log.msg("Got line " + line)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
58 self.factory.proto.sendData(self.factory.zbid, line + '\n')
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
59
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
60 def message(self, message):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
61 """Called to write a mesage to our client"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
62 self.transport.write(message)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
63
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
64 class ZBFactory(protocol.ServerFactory):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
65 """Factory for a ZB module
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
66
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
67 Represents a remote ZB module and has zero or more clients and a log file.
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
68 """
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
69 protocol = ZBClient
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
70
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
71 def __init__(self, zbid, proto, lognamebase):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
72 self.zbid = zbid
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
73 self.proto = proto
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
74 self.clients = []
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
75 self.tmpline = ""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
76 self.lastline = None
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
77
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
78 # Open logger
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
79 self.logger = logging.getLogger('Zigbee-%d' % (zbid))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
80 self.logger.setLevel(logging.DEBUG)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
81
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
82 # Add the log message handler to the logger
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
83 handler = logging.handlers.RotatingFileHandler(
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
84 lognamebase % (zbid), maxBytes = 20 * 1024, backupCount = 5)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
85
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
86 self.logger.addHandler(handler)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
87
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
88 def message(self, zbid, message):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
89 """Called when we get a message, check it's for us - if it is log it and write to our clients"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
90 if zbid != self.zbid:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
91 return
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
92
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
93 for c in self.clients:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
94 c.message(message)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
95
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
96 # Logger is line oriented, convert from packet oriented here
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
97 self.tmpline = self.tmpline + message
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
98 tmp = self.tmpline.split('\n')
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
99 for l in tmp[0:-1]:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
100 self.lastline = l # Stores last seen line for new clients
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
101 self.logger.debug(l.replace('\n', ''))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
102 self.tmpline = tmp[-1]
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
103
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
104 class ZBProto(Protocol):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
105 """Protocol to handle packets from the ZB module on the serial port"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
106 def __init__(self):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
107 self.pkts = zb.Packets()
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
108 self.factories = []
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
109
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
110 def dataReceived(self, data):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
111 """Parses data from ZB module into packets, calls each factory if a RX packet is received"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
112 #log.msg("Read data " + data)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
113 if self.pkts.processstr(data) > 0:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
114 while len(self.pkts.pktq) > 0:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
115 a = self.pkts.pktq.pop(0)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
116 #log.msg("type is " + str(type(a)))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
117 if type(a) == type(zb.RX_16_Bit()):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
118 #log.msg("Rx'd from %d => %s" % (a.sender, a.payloadstr))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
119 for f in self.factories:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
120 f.message(a.sender, a.payloadstr)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
121
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
122 def sendData(self, zbid, data):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
123 """Sends a chunk of data to our ZB module"""
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
124 #log.msg("%d <= %s" % (zbid, data))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
125
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
126 # Chop up data into pieces the module can handle
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
127 maxsz = 10
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
128 #maxsz = zb.TX_16_Bit.PKT_MAX_PAYLOAD
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
129 for i, j in zip(range(0, len(data), maxsz), range(maxsz, len(data) + maxsz, maxsz)):
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
130 p = zb.TX_16_Bit(zbid, data[i:j])
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
131 self.transport.write(p.Pack())
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
132 #log.msg("sent " + str(p))
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
133
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
134 if __name__ == '__main__':
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
135 logFile = sys.stdout
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
136 log.startLogging(logFile)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
137
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
138 # ZigBee serial protocol handler
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
139 zbproto = ZBProto()
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
140 SerialPort(zbproto, portname, reactor, baudrate = 38400)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
141
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
142 # Per-module TCP listener
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
143 for id in zbids:
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
144 zbfactory = ZBFactory(id, zbproto, lognamebase)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
145 zbproto.factories.append(zbfactory)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
146 reactor.listenTCP(baseport + id, zbfactory)
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
147
a3ec3f2988ac Initial commit of zbmux.
darius@Inchoate
parents:
diff changeset
148 reactor.run()