#!/usr/bin/python

import sys, getopt
sys.path.append('/usr/local/bin/thrift-ice-py')
 
from ice import Ice
from ice.ttypes import *

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
def main(argv):
  try:
    opts, args = getopt.getopt(sys.argv[1:],"hsiwrat", ["help", "signal", "id", "wan", "roaming", "all", "test"])
  except getopt.GetoptError as err:
    print str(err)
    Usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-s", "--signal"):
       CellSignal()
       sys.exit()
    elif opt in ("-i", "--id"):
       CellID()
       sys.exit()
    elif opt in ("-w", "--wan"):
       CellWANStatus()
       sys.exit()
    elif opt in ("-r", "--roaming"):
      CellRoaming()
      sys.exit()
    elif opt in ("-a", "--all"):
      CellInfo()
      sys.exit()
    elif opt in ("-t", "--test"):
       CellSetModemTestInfo()
       sys.exit()
    else:
      Usage()
      sys.exit(2)

def CellSetModemTestInfo():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    sInfo = stCellularInfo()
    sInfo.model = "ModemModel"
    sInfo.modemid = "ModemId"
    sInfo.rssi = "-97"

    #local link ipv6
    #sInfo.ip = "fe80::240:9dff:fe7c:876b"

    #full ipv6
    sInfo.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"

    #ipv4
    #sInfo.ip = "100.101.102.103"

    sInfo.roaming = 0
    sInfo.phone_number = "PhoneNumber"
    sInfo.network_type = "4G LTE"
    sInfo.apn = "ModemAPN"
    sInfo.sim_id = "ModemSIMid"
    sInfo.gps_mode = 6
    sInfo.current_bands = "CurrentBands"
 
    print client.SetModemInfo(sInfo)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def CellSignal():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.getSignalStrength()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def CellID():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.getModemId()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def CellWANStatus():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.getWanStatus()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def CellRoaming():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.getRoamingStatus()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def CellInfo():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.GetModemInfo()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def Usage():
	print 'ice-cell [option]'
	print '   --all         Shows all modem info'
	print '   --signal      Shows RSSI'
	print '   --id          Prints ModemID'
	print '   --wan         Wan IP address valid'
	print '   --roaming     Roaming state'
        print '   --test        Load Test Modem Info'

if __name__ == "__main__":
   main(sys.argv[1:])
