#!/usr/bin/env python3
# Copyright (c) 2008 Arastra, Inc.  All rights reserved.
# Arastra, Inc. Confidential and Proprietary.

# pylint: disable=consider-using-f-string

import sys, optparse, Sol # pylint: disable=deprecated-module

usage = """usage: solid [--chip <chipid>] <command>
       solid [--smbus /scd/<accelId>/<busId>/<deviceId>]
             [--reset <chipid>|Fixed] <command>

  <command> is 'reset', 'version', 'chip', 'info', or 'list'.
  <chipid> is usually the sliceid.  Use the 'list' command to get a list. """

def main():
   parser = optparse.OptionParser( usage=usage )
   parser.add_option( "--bootloader", action="store",
                      help="jump back to bootloader" )
   parser.add_option( "--chip", action="store",
                      help="which solchip to access" )
   parser.add_option( "--sysname", action="store", default="ar",
                      help="Sysdb sysname" )
   parser.add_option( "--magic", action="store_true", default=False,
                      help="find --chip using magic decoder ring" )
   parser.add_option( "--verbose", action="store_true", default=False,
                      help="verbose (debugging)" )

   parser.add_option( "--smbus", action="store",
                      help="smbus address of chip (don't get from Sysdb)" )
   parser.add_option( "--reset", action="store",
                      help="reset mechanism" )

   ( opt, args ) = parser.parse_args()

   if len(args) != 1:
      parser.error( "missing <command>" )

   if opt.chip and (opt.smbus or opt.reset):
      parser.error( "<chip> and <smbus>/<reset> are mutually exclusive" )

   if args[0] == 'list' :
      chipList = Sol.chipList( opt.sysname )
      print( "Available sol chips:", end=' ' )
      for chip in chipList:
         print( chip, end=' ' )
      print( "" )
      return

   if opt.smbus or opt.reset:
      print( "creating smbus sol device" )
      handle = Sol.chipFromCmdLine( opt.smbus, opt.reset )
   elif opt.magic:
      handle = Sol.chipFromMagic( opt.chip )
   else:
      handle = Sol.setup( opt.chip, opt.sysname )
   if handle is None:
      print( "ERROR: no chip found" )
      sys.exit( 1 )

   if args[0] == 'reset' :
      Sol.resetChip( handle, opt.verbose )
      return
      
   if opt.bootloader:
      Sol.call( handle, 0x08, data=int(opt.bootloader) )

   if args[0] == 'version':
      # pylint: disable-next=multiple-statements
      if Sol.call( handle, Sol.SolCmdM2Info ):          sys.exit( 1 )
      buffer = Sol.get( handle, 18 )
      sys.stdout.write( "%s" % Sol.dump_m2info( buffer ) )
      Sol.call( handle, Sol.SolCmdSWVersion )
      buffer = Sol.get( handle, 8 )
      sys.stdout.write( " / %s\n" % Sol.dump_swinfo( buffer, str="Software" ) )
   elif args[0] == 'info':
      Sol.call( handle, Sol.SolCmdInfo, data=1 )
      buffer = Sol.get( handle, 64 )
      for x in buffer:
         sys.stdout.write( "%c" % x )
      sys.stdout.write( "\n" )
   elif args[0] == 'chip':
      def atoi( a ): # array to int
         i = 0
         for x in a:
            i = i * 256 + x
         return i
      Sol.call( handle, Sol.SolCmdInfo, data=2 )
      buffer = Sol.get( handle, 64 )
      print( "date: %02x" % atoi( buffer[ 0:2 ] ) )
      print( "lot:  %02x" % atoi( buffer[ 2:4 ] ) )
      print( "die:  %08x" % atoi( buffer[ 4:8 ] ) )
      print( "rom:  %c%x%x" % ( buffer[ 17 ],
                               ( buffer[ 18 ] & 0xf0 ) >> 4, buffer[ 18 ] & 0xf ) )
      print( "lot:  %06x" % atoi( buffer[ 20:23 ] ) )
      print( "waf:  %02x" % atoi( buffer[ 23:24 ] ) )
      print( "die:  %04x" % atoi( buffer[ 24:26 ] ) )
      print( "year: %04x" % atoi( buffer[ 26:28 ] ) )
   else:
      parser.error( "unknown command" )

   Sol.cleanup( handle )

if __name__ == "__main__":
   main()

sys.exit( 0 )
