Project

General

Profile

Revision 213

fixed cardbox code for new board, updated tooltron.py for new cardbox

View differences:

tooltron.py
44 44
import getpass
45 45
from time import *
46 46

  
47
keypad = []
48 47
keypadTimeout = 11 #in seconds
49 48

  
50 49
#this table maps which keypad button to press for each tool. This
......
189 188
    return
190 189

  
191 190

  
192
if len(sys.argv) < 3:
193
    print "usage: tooltron.py /path/to/keypad/device /path/to/tool/bus/device"
191
if len(sys.argv) < 2:
192
    print "usage: tooltron.py /path/to/bus/device"
194 193

  
195 194
else:
196 195

  
......
206 205

  
207 206
    qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
208 207

  
209
    # user has 10 seconds to press a key, after that the keypad device
210
    # should send a timeout packet. This is a non-blocking serial
211
    # device so we can re-transmit the key request when the user
212
    # swipes a card instead of being stuck on a blocking read call
213
    keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 0) 
214
    keypad.flushInput()
215
    print keypad
216

  
217
    bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 2)
208
    bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 2)
218 209
    bus.flushInput()
219 210
    print bus
220 211
    
......
226 217
        bus.write(msg)
227 218
        return
228 219

  
220
    def sendKeyRequest():
221
       tn = 2 #2 is the ID for the cardbox
222
       msg = '^' + chr(1) + chr(tn) + TT_GET_KEY + chr(1 ^ tn ^ ord(TT_GET_KEY))
223
       print "seding key request"
224
       bus.write(msg)
225
       return
226

  
227
    def sendAck(toolNum):
228
       msg = '^' + chr(1) + chr(toolNum) + TT_ACK + chr(1 ^ tn ^ ord(TT_ACK))
229
       print "seding ACK to",toolNum
230
       bus.write(msg)
231
       return
232

  
233
    def sendNack(toolNum):
234
       msg = '^' + chr(1) + chr(toolNum) + TT_NACK + chr(1 ^ tn ^ ord(TT_NACK))
235
       print "seding ACK to",toolNum
236
       bus.write(msg)
237
       return
238

  
239

  
240
    #returns key if the key_send packet is recived from tool ID 2
241
    # otherwise returns 0
242
    def readKey():
243
       startDelim = bus.read(1)
244
       if startDelim == '^':
245
          src = bus.read(1)
246
          if src == 0:
247
             return 0
248
          dest = bus.read(1)
249
          if dest == 0:
250
             return 0
251
          packetType = bus.read(1)
252
          if packetType == 0:
253
             return 0
254
          key = bus.read(1)
255
          if key == 0:
256
             return 0
257
          crc = bus.read(1)
258

  
259
          print "got packet"
260
          if src != 2 or dest != 1 or packetType != TT_KEY_SEND:
261
             print "Bad packet! ^",src,dest,packetType,key,crc
262

  
263
          if chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)) == crc:
264
             return key
265
          else:
266
             print "xor fail. got", crc, "should have been got",(chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)))
267

  
268
          return 0
269

  
270
       else:
271
          print "did not get start delim!: ", ord(startDelim)
272
          return 0
273
       
229 274
    #returns [src, data] or [] on error
230 275
    def readTool():
231 276
        ret = [0,0,0]
......
263 308
        if id != None:
264 309
            print "\n-----------\nid# ", id.group(1)
265 310

  
311
            sendKeyRequest()
312
            startTime = time.time()
266 313

  
267
            print "sending key request"
268
            keypad.flushInput()
269
            keypad.write(TT_GET_KEY)
270

  
271 314
            cursor.execute(qry + id.group(1))
272 315

  
273 316
            result = cursor.fetchall()
......
318 361

  
319 362
            print user
320 363

  
321
            startTime = time.time();
322
            resp = ""
323
            while resp=="":
324
               resp = keypad.read(1)
364

  
365
            resp = 0
366
            while resp==0:
367
               resp = readKey()
368

  
325 369
               #no lock since we are just reading and we can afford to
326 370
               #miss a loop
327 371
               if idready == True:
......
334 378
                  logMessage("keypad timed out")
335 379
                  break
336 380

  
337
            #if we have a vaid response
381
            #if we have a valid response
338 382
            #sometimes the FTDI chip seems to give a zero as a string when power is reset
339 383
            if resp != "" and ord(resp) != 0 and resp != TT_TIMEOUT:
340 384

  
......
343 387
               print "request:",resp,"(",ord(resp),") ",toolNameLong
344 388

  
345 389
               if acl.count(resp) > 0:
346
                  keypad.write(TT_ACK)
390
                  sendAck(2)
347 391
                  sendTool(resp)
348 392
                  print "ACCESS GRANTED"
349 393
                  logMessage(user+" ACCESSED tool "+toolNameLong)
......
361 405
                                   str(MAX_TOOL_FAILS)+" times in a row.")
362 406

  
363 407
               else:
364
                  keypad.write(TT_NACK)
408
                  sendNack(2)
365 409
                  print "ACCESS DENIED!!"
366 410
                  logMessage(user + " DENIED on tool "+toolNameLong)
367 411

  
368 412
               clear_id()
369 413

  
370 414
        elif resp == 0 or ord(resp) == 0: #if we get noise, send timeout to reset and sync states
371
           keypad.write(TT_TIMEOUT);
415
           keypad.write(TT_TIMEOUT)
372 416
           print "ERROR: got strange byte, timing out"
373 417
           logMessage("ERROR: got 0 as a byte, sending timeout (cardbox power issue?)")
374 418
        else:

Also available in: Unified diff