def radioChannel(): try: ips = get_radio_ip() except (ValueError, subprocess.CalledProcessError): return jsonify(status = 'error') if request.method == 'POST': coap.post('coap://[%s]/config?param=channel' % (ips['addrs'][0]), request.json['channel']) load_radio() return jsonify(status = 'ok') else: # GET return coap.get('coap://[%s]/config?param=channel' % (ips['addrs'][0]))
def load_radio(): subprocess.call(['killall', 'tunslip6']) radio = json.loads(db.get('conf/radio')) tunslip = json.loads(db.get('conf/tunslip')) try: subprocess.call(['systemctl', 'stop', '*****@*****.**']) except OSError: print "error disabling serial-getty ttyS0" try: subprocess.call(['uartsel', 'mc']) except OSError: print "error calling uartsel mc" subprocess.call(['killall', '-9', 'mc1322x-load']) time.sleep(.5) subprocess.call(['mc1322x-load', '-e', '-r', 'none', '-f', os.path.join(app.config['CACHE_ROOT'],'br.bin'), '-t', tunslip['device'], '-c', radio['resetcmd']]) devnull = open('/dev/null', 'w') now = time.time() result = None while result is None and time.time() - now < 5: try: result = subprocess.check_output(["ip", "-f", "inet6", "addr", "show", "tun", "scope", "global"], stderr=devnull) except subprocess.CalledProcessError: pass time.sleep(1) # save the radio ip addr radio['ips'] = get_radio_ip()['addrs'] print "Radio ips are %s" % (radio['ips']) if result is None: print "Using fallback address %s/64" % (tunslip['address']) os.system("tunslip6 -v3 -s %s %s > %s &" % (tunslip['device'], tunslip['address'], os.path.join(app.config['CACHE_ROOT'],'tunslip6.log'))) else: ipv6 = subprocess.check_output(["getbripv6.sh"]).rstrip(); print "Using tunnel address %s/64" % (ipv6) time.sleep(1) os.system("tunslip6 -v3 -s %s %s > %s &" % (tunslip['device'], ipv6 + '/64', os.path.join(app.config['CACHE_ROOT'],'tunslip6.log'))) os.system("for i in /proc/sys/net/ipv6/conf/*; do echo 1 > $i/forwarding; done") # get the current channel radio['channel'] = coap.get('coap://[%s]/config?param=channel' % (radio['ips'][0])).rstrip() print "Radio set to channel %s" % (radio['channel']) db.store('conf/radio', json.dumps(radio))
def get_radio_channel(): ip = get_radio_not_local_ip() channel = coap.get('coap://[%s]/config?param=channel' % (ip)).rstrip() # check if channel is a number return int(channel)