diff --git a/.gitignore b/.gitignore index 83658ec527..a0f0e5d886 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc *.log +.idea/ diff --git a/code/software/__init__.py b/code/software/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/code/software/app/__init__.py b/code/software/app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/code/software/app/mercatorRunExperiment.py b/code/software/app/mercatorRunExperiment.py index fa63543f97..f9df7e74e7 100644 --- a/code/software/app/mercatorRunExperiment.py +++ b/code/software/app/mercatorRunExperiment.py @@ -1,13 +1,5 @@ #!/usr/bin/python -# =========================== adjust path ===================================== - -import os -import sys -if __name__ == '__main__': - here = sys.path[0] - sys.path.insert(0, os.path.join(here, '..', 'lib')) - # =========================== imports ========================================= import argparse @@ -19,12 +11,12 @@ import socket # Mercator -import MoteHandler -import MercatorDefines as d +from ..lib import MoteHandler +from ..lib import MercatorDefines as d # IoT-lab import iotlabcli as iotlab -from iotlabcli import experiment +from iotlabcli import experiment, node # =========================== logging ========================================= @@ -41,7 +33,6 @@ # =========================== body ============================================ - class MercatorRunExperiment(object): FREQUENCIES = [n+11 for n in range(16)] # frequencies to measure on, in IEEE notation @@ -62,20 +53,33 @@ def __init__(self, args, serialports, site="local"): self.nbpackets = args.nbpackets self.txpksize = args.txpksize self.txpower = args.txpower + self.experiment_id = args.expid + + # use the file created by auth-cli command + usr, pwd = iotlab.get_user_credentials() + + # authenticate through the REST interface + self.api = iotlab.rest.Api(usr, pwd) # connect to motes for s in serialports: logfile.debug("connected to %s", s) - self.motes[s] = MoteHandler.MoteHandler(s, self._cb) + self.motes[s] = MoteHandler.MoteHandler(s, self._cb, reset_cb=self._reset_cb) if not self.motes[s].isActive: - logconsole.info("DELETED %s", s) - del self.motes[s] + raise Exception("Mote {0} is not responding.".format(s)) + # get current datetime now = datetime.datetime.now().strftime("%Y.%m.%d-%H.%M.%S") - self.file = gzip.open('{0}{1}-{2}_raw.csv.gz'.format(DATASET_PATH, - self.site, - now), - 'wb') + + # open file + self.file = gzip.open( + '{0}{1}-{2}_raw.csv.gz'.format( + DATASET_PATH, + self.site, + now + ), + 'wb' + ) # write settings settings = { @@ -85,7 +89,7 @@ def __init__(self, args, serialports, site="local"): "tx_count": self.nbpackets, "transaction_count": self.nbtrans, "node_count": len(self.motes), - "site": self.site, + "location": self.site, "channel_count": len(self.FREQUENCIES), "start_date": now, "txpower": self.txpower @@ -94,7 +98,7 @@ def __init__(self, args, serialports, site="local"): self.file.write('\n') # write csv header - self.file.write('datetime,src,dst,frequency,rssi,crc,expected,' + + self.file.write('datetime,src,dst,channel,rssi,crc,expected,' + 'transaction_id,pkctr\n') try: @@ -236,6 +240,11 @@ def _cb(self, serialport, notif): logfile.debug("Node %s restarted", d.format_mac(self.motes[serialport].get_mac())) + def _reset_cb(self, mote): + logfile.debug('restarting mote {0}'.format(mote.serialport)) + node.node_command(self.api, 'reset', self.experiment_id, [mote.serialport]) + logfile.debug('mote {0} restarted'.format(mote.serialport)) + @staticmethod def _quit_callback(): print "quitting!" diff --git a/code/software/lib/MoteHandler.py b/code/software/lib/MoteHandler.py index ac6332c900..29e79e14de 100644 --- a/code/software/lib/MoteHandler.py +++ b/code/software/lib/MoteHandler.py @@ -9,20 +9,21 @@ import Hdlc import MercatorDefines as d +BAUDRATE = 500000 +TIMEOUT_RESPONSE = 3 +MAX_TIMEOUTS = 3 -class MoteHandler(threading.Thread): - - _BAUDRATE = 500000 - TIMEOUT_RESPONSE = 3 +STAT_UARTNUMRXCRCOK = 'uartNumRxCrcOk' +STAT_UARTNUMRXCRCWRONG = 'uartNumRxCrcWrong' +STAT_UARTNUMTX = 'uartNumTx' - STAT_UARTNUMRXCRCOK = 'uartNumRxCrcOk' - STAT_UARTNUMRXCRCWRONG = 'uartNumRxCrcWrong' - STAT_UARTNUMTX = 'uartNumTx' +class MoteHandler(threading.Thread): - def __init__(self, serialport, cb=None): + def __init__(self, serialport, cb=None, reset_cb=None): self.serialport = serialport self.cb = cb + self.reset_cb = reset_cb self.serialLock = threading.Lock() self.dataLock = threading.RLock() self.mac = None @@ -36,13 +37,15 @@ def __init__(self, serialport, cb=None): self.isActive = True self.response = None self._iotlab = False + self.timeouts = 0 self._reset_stats() + try: if self.iotlab: self.serial = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.serial.connect((serialport, 20000)) else: - self.serial = serial.Serial(self.serialport, self._BAUDRATE) + self.serial = serial.Serial(self.serialport, BAUDRATE) except Exception as err: msg = 'could not connect to {0}, reason: {1}'.format(serialport, err) print msg @@ -133,12 +136,17 @@ def send_REQ_ST(self): ) ) - self.waitResponseEvent.wait(self.TIMEOUT_RESPONSE) + self.waitResponseEvent.wait(TIMEOUT_RESPONSE) if not self.waitResponseEvent.isSet(): print "-----------timeout--------------" + self.serialport self.isActive = False + self.timeouts += 1 + if self.timeouts > MAX_TIMEOUTS: + self.reset_cb(self) return + else: + self.timeouts = 0 with self.dataLock: self.waitResponse = False diff --git a/code/software/lib/__init__.py b/code/software/lib/__init__.py new file mode 100644 index 0000000000..e69de29bb2