Project

General

Profile

Revision 221

merged in manualtron fixes

View differences:

tooltron.py
76 76

  
77 77

  
78 78
TT_GET_KEY = 'k'
79
TT_SEND_KEY = 's'
79 80
TT_ACK     = 'a'
80 81
TT_NACK    = 'n'
81 82
TT_TO      = 'f'
......
243 244
       print "seding ACK to",toolNum
244 245
       sendMessage(toolNum, TT_NACK, "")
245 246

  
246
    #returns key if the key_send packet is recived from tool ID 2
247
    # otherwise returns 0
248
    def readKey():
249
       startDelim = bus.read(1)
250
       if startDelim == '^':
247
    # This function eats the rest of the packet where data is what we have so far
248
    def flushPacket(data):
249
       while len(data) < 6:
250
          data += bus.read(1)
251

  
252
       plen = data[4]
253
       if plen > 0:
254
          bus.read(ord(plen))
255
    
256

  
257
    # Returns [src, dest, cmd, data]
258
    def readMessage():
259
       if bus.read(1) == '^':
251 260
          src = bus.read(1)
252
          if src == 0:
253
             return 0
261
          if src == chr(1): #reflection
262
             print "(reflection)"
263
             flushPacket('^' + src)
264
             return readMessage()
265

  
254 266
          dest = bus.read(1)
255
          if dest == 0:
256
             return 0
257
          packetType = bus.read(1)
258
          if packetType == 0:
259
             return 0
260
          payloadLen = bus.read(1)
261
          if payloadLen != 1:
262
             print "ERROR: payload length wrong! Should be 1, was",payloadLen
263
             return 0
264
          key = bus.read(1)
265
          if key == 0:
266
             return 0
267
          crc = bus.read(1)
267
          cmd = bus.read(1)
268
          plen = bus.read(1)
269
          data = bus.read(ord(plen))
270
          x = bus.read(1)
268 271

  
269 272
          print "got packet"
270
          if src != 2 or dest != 1 or packetType != TT_KEY_SEND:
271
             print "Bad packet! ^",src,dest,packetType,key,crc
273
          #printMsg('^' + src + dest + cmd + plen + data + x)
272 274

  
273
          if chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)) == crc:
274
             return key
275
          crc = ord(src) ^ ord(dest) ^ ord(cmd) ^ ord(plen)
276
          for d in data:
277
             crc ^= ord(d)
278

  
279
          if crc == ord(x):
280
             return [src, dest, cmd, data]
275 281
          else:
276
             print "xor fail. got", crc, "should have been got",(chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)))
282
             print "xor fail. got", str(ord(x)), "should have been got",crc
283
             return None
277 284

  
285
    def readKey():
286
       m = readMessage()
287
       if m == None:
278 288
          return 0
279

  
289
       [src, dest, cmd, data] = m
290
       if cmd == TT_SEND_KEY:
291
          return data[0]
280 292
       else:
281
          print "did not get start delim!: ", ord(startDelim)
282 293
          return 0
283
       
294

  
284 295
    #returns [src, dest, command] or [] on error
296
    # because of reflection, this will quietly ignore packets send by the server
285 297
    def readTool():
286
        ret = [0,0,0]
298
       m = readMessage()
299
       if m == None:
300
          return []
301
       print m
302
       [src, dest, cmd, data] = m
303
       return [src, dest, cmd]
287 304

  
288
        if bus.read(1) == '^':
289
            ret[0] = bus.read(1)
290
            ret[1] = bus.read(1)
291
            ret[2] = bus.read(1)
292
            plen = bus.read(1)
293
            if plen != chr(0):
294
               print "ERROR: tool responded with payload len",str(ord(plen))
295
               bus.flushInput()
296
            x = bus.read(1)
297

  
298
            print "got packet",ret
299

  
300
            if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x:
301
                return ret
302
            else:
303
                print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2]))
304

  
305
        return []
306

  
307 305
    def checkAck(t):
308 306
        tn = keypad2toolID(t)
309 307
        m = readTool()

Also available in: Unified diff