def get_my_summary_vector(): efilespath=p.pathe+'/efiles'; myfilespath=p.pathe+'/myfiles'; mysummary1=c.write_read_cmd(['ls', efilespath,'-t']); mysummary1=mysummary1.splitlines(); mysummary2=c.write_read_cmd(['ls', myfilespath,'-t']); mysummary2=mysummary2.splitlines(); return mysummary1+mysummary2
def checkWDT(state,wdTimer,neighborID,eventLog_ref): if state!=p.TRANSPORT: currentTime=time.time(); if currentTime>=wdTimer+p.timeout: cmdout=c.write_read_cmd(['sudo','wpa_cli','p2p_flush']); cmdout=c.write_read_cmd(['sudo','wpa_cli','p2p_find']); if p.recordEvents and state==p.CONNECT: #Execute only in debug mode d.save_fail("Time-out",neighborID,eventLog_ref); return p.SEARCH,currentTime+random.random()*p.backoff; return state,wdTimer;
def clockSincronize(ser): isGPGGA=False; while not isGPGGA: nmeaCode = ser.readline(); isGPGGA=p.gnssInstruction in nmeaCode; line=nmeaCode.split(p.gnssDelimeter); time=str(int(line[1][0:2])+p.timeZone)+':'+line[1][2:4].decode('utf-8')+':'+line[1][4:].decode('utf-8'); cmdout=c.write_read_cmd(['sudo','timedatectl','set-ntp','false']); cmdtime=c.write_read_cmd(['sudo','date','--set='+time]); cmdout=c.write_read_cmd(['sudo','hwclock','--systohc']); print(cmdtime)
def start_protocol(wifiPort): cmdout = c.write_read_cmd(['sudo', 'ifconfig', wifiPort, 'up']) cmdout = c.write_read_cmd( ['sudo', 'rm', '/var/run/wpa_supplicant/"', 'wifiPort', '-f']) cmdout = c.write_read_cmd(['sudo', 'killall', 'wpa_supplicant']) time.sleep(p.sleepStart) cmdout = c.write_read_cmd([ 'sudo', 'wpa_supplicant', '-i', wifiPort, '-c', p.pathe + '/p2p.conf', '-Dnl80211', '-B', '-u' ]) print(cmdout) reboot()
def handle_goNegotiationRequest(devicepath,myID,wdTimer,eventLog_ref): mac=devicepath[-12:]; MAC=mac[0:2]+":"+mac[2:4]+":"+mac[4:6]+":"+mac[6:8]+":"+mac[8:10]+":"+mac[10:12]; #get mac address cmdout=c.write_read_cmd(['sudo', 'wpa_cli','p2p_peer',MAC]); neighborID=find_neighborID(cmdout); rssi=find_neighbor_RSSI(cmdout); if neighborID!=None: if (myID>neighborID): cmdout=c.write_read_cmd(['sudo','wpa_cli','p2p_connect',MAC,'pbc','go_intent=15']); if p.recordEvents: #Execute only in debug mode d.save_routing_events("Connection-accept",neighborID,rssi,eventLog_ref); return p.CONNECT,neighborID,MAC,time.time(); return p.SEARCH,None,None,wdTimer;
def GroupFinished(self, status): mutex.acquire() try: if self.state == p.SEARCH: cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find']) elif self.state == p.CONNECT: self.state = p.SEARCH cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find']) self.wdTimer = time.time() + random.random() * p.backoff if p.recordEvents: #Execute only in debug mode d.save_fail("Group-Finished", self.neighborID, self.eventLog_ref) finally: mutex.release()
def GONegotiationSuccess(self, status): mutex.acquire() try: if self.state == p.CONNECT: cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_stop_find']) finally: mutex.release()
def handle_GroupStarted(properties,myID,myAddress,neighborID,neighborMAC,neighborList,eventLog_ref): if "group_object" in properties: p2pInterface=get_direct_interface(); if p.recordEvents: #Run only in debug mode if neighborMAC!=None: cmdout=c.write_read_cmd(['sudo', 'wpa_cli','p2p_peer',neighborMAC]); intended_mac=find_neighbor_intended_addr(cmdout); else: intended_mac=None; if (myID<neighborID): role=p.CLIENT; else: role=p.SERVER; neighborAdress=build_ip(neighborID); cmdout=c.write_read_cmd(['sudo','ifconfig',p2pInterface,myAddress,'netmask','255.0.0.0','up']); t.transport_fsm(role,myID,neighborID,myAddress,neighborAdress,neighborList,intended_mac,p2pInterface,eventLog_ref);
def get_direct_interface(): out = c.write_read_cmd(['sudo', 'wpa_cli','interface']); if bytes("Selected interface",'utf8') in out: out=out.splitlines(); out=out[0].split(bytes([39])); out=out[1].decode('utf-8'); return out; return None;
def GroupStarted(self, properties): mutex.acquire() try: if self.state == p.CONNECT: self.state = p.TRANSPORT r.handle_GroupStarted(properties, self.myID, self.myAddress, self.neighborID, self.neighborMAC, self.neighborList, self.eventLog_ref) interface = r.get_direct_interface() if interface != None: cmdout = c.write_read_cmd( ['sudo', 'wpa_cli', 'p2p_group_remove', interface]) self.state = p.SEARCH cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find']) self.wdTimer = time.time() + random.random() * p.backoff finally: mutex.release()
def check_device(): cmdout = c.write_read_cmd( ['sudo', 'wpa_cli', 'p2p_peer', 'fe:c2:de:2a:86:c0']) if is_Parent(cmdout): rssi = find_neighbor_RSSI(cmdout) start_client() print('True') else: print('False')
def GONegotiationFailure(self, status): mutex.acquire() try: if self.state == p.SEARCH: cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find']) elif self.state == p.CONNECT: self.state = p.SEARCH cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_cancel']) interface = r.get_direct_interface() if interface != None: cmdout = c.write_read_cmd( ['sudo', 'wpa_cli', 'p2p_group_remove', interface]) cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find']) self.wdTimer = time.time() + p.backoff if p.recordEvents: #Execute only in debug mode d.save_fail("Go-Negotiation-Failure", self.neighborID, self.eventLog_ref) finally: mutex.release()
def handle_deviceFound(devicepath,myID,neighborList,wdTimer,eventLog_ref): mac=devicepath[-12:]; MAC=mac[0:2]+":"+mac[2:4]+":"+mac[4:6]+":"+mac[6:8]+":"+mac[8:10]+":"+mac[10:12]; #get mac address cmdout=c.write_read_cmd(['sudo', 'wpa_cli','p2p_peer',MAC]); neighborID=find_neighborID(cmdout); if neighborID != None: intended_mac=find_neighbor_intended_addr(cmdout); rssi=find_neighbor_RSSI(cmdout); if p.recordEvents: #Execute only in debug mode d.save_routing_events("Device-found",neighborID,rssi,eventLog_ref); if (myID<neighborID): if neighborID not in neighborList: #peer was not visted in a short time if intended_mac==p.FREEINTENDED and rssi!=0: if rssi>p.MINRSSI: cmdout=c.write_read_cmd(['sudo','wpa_cli','p2p_connect',MAC,'pbc','go_intent=1']); if p.recordEvents: #Execute only in debug mode d.save_routing_events("Connection-request",neighborID,rssi,eventLog_ref); return p.CONNECT,neighborID,MAC,time.time()+p.backoff; else: wdTimer=time.time()-p.timeout+p.retryTime; return p.SEARCH,None,None,wdTimer;
def save_rssi(neighborID,intended_mac,p2pInterface,eventLog_ref): if intended_mac!=None: cmdout=c.write_read_cmd(['sudo', 'iw','dev',p2pInterface,'station','get',intended_mac]); ind=cmdout.find(bytes('signal:','utf-8')); if ind!=-1: inst=cmdout[ind:]; ind=inst.find(bytes('\n','utf-8')); inst=inst[:ind].decode('utf-8'); else: inst=""; ind=cmdout.find(bytes('signal avg:','utf-8')); if ind!=-1: avg=cmdout[ind:]; ind=avg.find(bytes('\n','utf-8')); avg=avg[:ind].decode('utf-8'); else: avg=""; if inst!="" or avg!="": rssi="Time-stamp(seconds):"+str(time.time())+","+"RSSI,neighbor:"+str(neighborID)+","+inst+","+avg; eventLog_ref[0]=eventLog_ref[0]+rssi+'\n' print("RSSI,neighbor:"+str(neighborID)+","+inst+","+avg);
def reset_messages(): cmdout=c.write_read_cmd(['sudo','rm','-r',p.pathe+"/myfiles"]); cmdout=c.write_read_cmd(['sudo','rm','-r',p.pathe+"/efiles"]); cmdout=c.write_read_cmd(['sudo','cp','-r',p.pathe+"/Debug/initialFiles/myfiles",p.pathe]); cmdout=c.write_read_cmd(['sudo','cp','-r',p.pathe+"/Debug/initialFiles/efiles",p.pathe]);
def start_ip(): cmdout = c.write_read_cmd([ 'sudo', 'ifconfig', 'eth0', '192.168.1.11', 'netmask', '255.255.255.0' ]) print(cmdout)
def reboot(): cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_flush']) cmdout = c.write_read_cmd(['sudo', 'wpa_cli', 'p2p_find'])