From df67178a841a09eabf7407006d53c97074901a6b Mon Sep 17 00:00:00 2001
From: Rohit Gupta <rohit.gupta@eurecom.fr>
Date: Fri, 5 Feb 2016 18:27:08 +0100
Subject: [PATCH] Added bladeRF support + creation of common autotest library

---
 .../tools/configure_cots_bandrich_ue.py       | 29 ++----------
 cmake_targets/autotests/tools/lib_autotest.py | 33 +++++++++++++
 cmake_targets/tools/start_bladerf.py          | 46 +++++++++++++++++++
 3 files changed, 84 insertions(+), 24 deletions(-)
 create mode 100644 cmake_targets/autotests/tools/lib_autotest.py
 create mode 100644 cmake_targets/tools/start_bladerf.py

diff --git a/cmake_targets/autotests/tools/configure_cots_bandrich_ue.py b/cmake_targets/autotests/tools/configure_cots_bandrich_ue.py
index fdbbb5d6d00..6af37858657 100755
--- a/cmake_targets/autotests/tools/configure_cots_bandrich_ue.py
+++ b/cmake_targets/autotests/tools/configure_cots_bandrich_ue.py
@@ -21,6 +21,10 @@ if openair_dir == None:
   print "Error getting OPENAIR_DIR environment variable"
   sys.exit(1)
 
+sys.path.append(os.path.expandvars('$OPENAIR_DIR/cmake_targets/autotests/tools/'))
+
+from lib_autotest import *
+
 def find_open_port():
    global serial_port, ser
    max_ports=100
@@ -159,35 +163,12 @@ def reset_ue():
   VendorId=res[0][2]
   ProductId=res[0][3]
   usb_dir= find_usb_path(VendorId, ProductId)
-  print usb_dir
+  print "Bandrich 4G LTE Adapter found in..." + usb_dir
   cmd = "sudo sh -c \"echo 0 > " + usb_dir + "/authorized\""
   os.system(cmd + " ; sleep 15" )
   cmd = "sudo sh -c \"echo 1 > " + usb_dir + "/authorized\""
   os.system(cmd + " ; sleep 30" )
 
-def read_file(filename):
-  try:
-    file = open(filename, 'r')
-    return file.read()
-  except Exception, e:
-    #error = ' Filename ' + filename 
-    #error = error + ' In function: ' + sys._getframe().f_code.co_name + ': *** Caught exception: '  + str(e.__class__) + " : " + str( e)
-    #error = error + traceback.format_exc()
-    #print error
-    return ''
-
-
-def find_usb_path(idVendor, idProduct):
-  for root, dirs, files in os.walk("/sys/bus/usb/devices", topdown=False):
-    for name in dirs:
-        tmpdir= os.path.join(root, name)
-        tmpidVendor = read_file(tmpdir+'/idVendor').replace("\n","")
-        tmpidProduct = read_file(tmpdir+'/idProduct').replace("\n","")
-        #print "tmpdir = " + tmpdir + " tmpidVendor = " + tmpidVendor + " tmpidProduct = " + tmpidProduct
-        if tmpidVendor == idVendor and tmpidProduct == idProduct:
-            return tmpdir
-  return ''
-
 i=1
 gw='192.172.0.1'
 while i <  len(sys.argv):
diff --git a/cmake_targets/autotests/tools/lib_autotest.py b/cmake_targets/autotests/tools/lib_autotest.py
new file mode 100644
index 00000000000..67234de567e
--- /dev/null
+++ b/cmake_targets/autotests/tools/lib_autotest.py
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+
+import os
+from pyroute2 import IPRoute
+import sys
+import re
+import threading
+import signal
+import traceback
+import commands
+
+def read_file(filename):
+  try:
+    file = open(filename, 'r')
+    return file.read()
+  except Exception, e:
+    # WE just ignore the exception as some files are probably not present
+    #error = ' Filename ' + filename 
+    #error = error + ' In function: ' + sys._getframe().f_code.co_name + ': *** Caught exception: '  + str(e.__class__) + " : " + str( e)
+    #error = error + traceback.format_exc()
+    #print error
+    return ''
+
+def find_usb_path(idVendor, idProduct):
+  for root, dirs, files in os.walk("/sys/bus/usb/devices", topdown=False):
+    for name in dirs:
+        tmpdir= os.path.join(root, name)
+        tmpidVendor = read_file(tmpdir+'/idVendor').replace("\n","")
+        tmpidProduct = read_file(tmpdir+'/idProduct').replace("\n","")
+        if tmpidVendor == idVendor and tmpidProduct == idProduct:
+            return tmpdir
+  return ''
+
diff --git a/cmake_targets/tools/start_bladerf.py b/cmake_targets/tools/start_bladerf.py
new file mode 100644
index 00000000000..ad83d5d5179
--- /dev/null
+++ b/cmake_targets/tools/start_bladerf.py
@@ -0,0 +1,46 @@
+#!/usr/bin/python
+import time
+import serial
+import os
+from pyroute2 import IPRoute
+import sys
+import re
+import threading
+import signal
+import traceback
+import commands
+
+if os.environ.get('OPENAIR_DIR') == None:
+  print "Error getting OPENAIR_DIR environment variable"
+  sys.exit(1)
+
+sys.path.append(os.path.expandvars('$OPENAIR_DIR/cmake_targets/autotests/tools/'))
+
+from lib_autotest import *
+
+def reset_bladerf():
+  stringIdBladeRF='OpenMoko, Inc'
+  status, out = commands.getstatusoutput('lsusb | grep -i \'' + stringIdBladeRF + '\'')
+  if (out == '') :
+     print "BladeRF not found. Exiting now..."
+     sys.exit()
+  p=re.compile('Bus\s*(\w+)\s*Device\s*(\w+):\s*ID\s*(\w+):(\w+)')
+  res=p.findall(out)
+  BusId=res[0][0]
+  DeviceId=res[0][1]
+  VendorId=res[0][2]
+  ProductId=res[0][3]
+  usb_dir= find_usb_path(VendorId, ProductId)
+  print "BladeRF Found in directory..." + usb_dir
+  cmd = "sudo sh -c \"echo 0 > " + usb_dir + "/authorized\""
+  os.system(cmd + " ; sleep 5" )
+  cmd = "sudo sh -c \"echo 1 > " + usb_dir + "/authorized\""
+  os.system(cmd + " ; sleep 5" )
+
+os.system ('sudo -E bladeRF-cli --flash-firmware /usr/share/Nuand/bladeRF/bladeRF_fw.img')
+
+print "Resettting BladeRF..."
+
+reset_bladerf()
+
+os.system ("dmesg|tail")
-- 
GitLab