Subversion Repositories HomeAutomation

Rev

Rev 612 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #!/usr/bin/python
  2.  
  3. import termios
  4. import os
  5.  
  6. import can
  7. import uncen
  8.  
  9. class Job( uncen.UncenJob ):
  10.     name    = "Socket serial"
  11.     version = "0.01"
  12.  
  13.     cid     = None
  14.     listen  = True
  15.  
  16.     buffer  = ""
  17.  
  18.     MAGIC_START = "\xfd"
  19.     MAGIC_END   = "\xfa"
  20.     PKT_LEN     = 15
  21.  
  22.  
  23.     def getstatus( self ):
  24.         return "%s @ %d 8N1" % (self.path, self.baudrate)
  25.  
  26.     def __init__( self, core, attr ):
  27.         self.job_init( core )
  28.  
  29.         self.path = attr[0]
  30.         self.baudrate = 38400
  31.  
  32.         self.fd = os.open( attr[0], os.O_RDWR )
  33.         termios.tcsetattr( self.fd, termios.TCSANOW, [ \
  34.             termios.IGNPAR, \
  35.             0, \
  36.             termios.CS8 | termios.CLOCAL | termios.CREAD, \
  37.             0, \
  38.             termios.B38400, \
  39.             termios.B38400, \
  40.             [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ] )
  41.  
  42.         self.trigger_add( self.fd )
  43.  
  44.     def close( self ):
  45.         os.close( self.fd )
  46.         self.job_close()
  47.  
  48.     def trigger( self, fd, eventmask ):
  49.         self.buffer += os.read( self.fd, 8192 )
  50.  
  51.         if self.cid == None:
  52.             self.buffer = ""
  53.             return
  54.  
  55.         while len( self.buffer ) >= self.PKT_LEN + 2:
  56.             if self.buffer[0] == self.MAGIC_START and self.buffer[self.PKT_LEN+1] == self.MAGIC_END:
  57.                 pktdata = self.buffer[1:self.PKT_LEN+1]
  58.                 self.buffer=self.buffer[self.PKT_LEN+2:]
  59.  
  60.                 datalen = ord( pktdata[6] )
  61.                 canp = can.CanPacket( \
  62.                     ( ord( pktdata[0] ) ) | \
  63.                     ( ord( pktdata[1] ) << 8 ) | \
  64.                     ( ord( pktdata[2] ) << 16 ) | \
  65.                     ( ord( pktdata[3] ) << 24 ), \
  66.                     [ ord( val ) for val in pktdata[7:7+datalen] ], \
  67.                     pktdata[4] != "\x00", \
  68.                     pktdata[5] != "\x00" )
  69.  
  70.                 self.can_send( self.cid, canp )
  71.  
  72.             while   len( self.buffer )          >  self.PKT_LEN + 2 \
  73.                 and self.buffer[0]              != self.MAGIC_START \
  74.                 and self.buffer[self.PKT_LEN+1] != self.MAGIC_END:
  75.                 idxstart = self.buffer.find( self.MAGIC_START )
  76.                 if idxstart < 0:
  77.                     self.buffer = ""
  78.                     return
  79.                 self.buffer = self.buffer[idxstart:]
  80.  
  81.  
  82.     def can_recieve( self, cid, canp ):
  83.         pktdata = self.MAGIC_START
  84.         pktdata += chr( ( canp.id & 0x000000FF ) >>  0 )
  85.         pktdata += chr( ( canp.id & 0x0000FF00 ) >>  8 )
  86.         pktdata += chr( ( canp.id & 0x00FF0000 ) >> 16 )
  87.         pktdata += chr( ( canp.id & 0xFF000000 ) >> 24 )
  88.         pktdata += {True: "\x01", False: "\x00"}[canp.extended]
  89.         pktdata += {True: "\x01", False: "\x00"}[canp.remote]
  90.         pktdata += chr( len( canp.data ) )
  91.         for val in canp.data:
  92.             pktdata += chr( val )
  93.         for val in range( 8-len( canp.data ) ):
  94.             pktdata += "\x00"
  95.         pktdata += self.MAGIC_END
  96.         os.write( self.fd, pktdata )
  97.  
  98.     def can_accept( self, sjid, cid ):
  99.         self.listen = False
  100.         self.cid = cid
  101.         return True
  102.  
  103.     def can_close( self, cid ):
  104.         self.cid = None
  105.         self.listen = True
  106.