def run(self): pkt = self.socket.recv(1000) paquete_llegada = pickle.loads(pkt) # MAC requests an incoming packet to the PHY if (paquete_llegada["TIPO"] == "COLA"): tipo_pkt = paquete_llegada["DATOS"] len_data = self.data.longitud() len_ack = self.ack.longitud() len_rts = self.rts.longitud() len_cts = self.cts.longitud() if (tipo_pkt == "DATA") and len_data > 0: # There are Data packets? self.data.elementos.reverse() x = self.data.read(0) phy_pkt = plcp.crear_paquete("SI", x) self.data.pop() self.data.elementos.reverse() elif tipo_pkt == "ACK" and len_ack > 0: # There are ACK packets? self.ack.elementos.reverse() x = self.ack.read(0) phy_pkt = plcp.crear_paquete("SI", x) self.ack.pop() self.ack.elementos.reverse() elif tipo_pkt == "RTS" and len_rts > 0: # There are RTS packets? self.rts.elementos.reverse() x = self.rts.read(0) phy_pkt = plcp.crear_paquete("SI", x) self.rts.pop() self.rts.elementos.reverse() elif tipo_pkt == "CTS" and len_cts > 0: # There are CTS packets? self.cts.elementos.reverse() x = self.cts.read(0) phy_pkt = plcp.crear_paquete("SI", x) self.cts.pop() self.cts.elementos.reverse() else: # There are not packets phy_pkt = plcp.crear_paquete("NO", []) plcp.enviar_a_mac( self.socket, phy_pkt) # Send the result (PHY packet) to MAC layer self.socket.close() # PHY packet generator script. It sends an 802.11 frame simulating its arrival from the wireless medium if (paquete_llegada["TIPO"] == "DATA" or paquete_llegada["TIPO"] == "DATA_FRAG"): if paquete_llegada["DATOS"]["INFO"][ "mac_add1"] == self.my_mac: # Is the data packet addressed to this node? self.data.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"] == "ACK"): if paquete_llegada["DATOS"]["INFO"][ "RX_add"] == self.my_mac: # Is the ACK addressed to this node? self.ack.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"] == "RTS"): #It is a RTS self.rts.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"] == "CTS"): #It is a CTS self.cts.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"] == "BEACON"): #It is a BEACON beacon = paquete_llegada["DATOS"] beacon = beacon["INFO"] msg = plcp.nuevo_beacon() msg["MAC"] = beacon["mac_add2"] msg["timestamp"] = beacon["timestamp"] msg["BI"] = beacon["BI"] msg["OFFSET"] = time.time() - beacon["timestamp"] x = self.bcn.longitud() actualizado = False # Update the beacon list for i in range(0, x): if msg["MAC"] == self.bcn.read(i)["MAC"]: self.bcn.quitar(i) self.bcn.insert(i, msg) actualizado = True if actualizado == False: self.bcn.insert(x + 1, msg) if (paquete_llegada["TIPO"] == "OTHER"): #print "No packet arrived" self.socket.close() #DEBUG print "=========== BUFFER STATUS ===========" print "DATA [%i]" % self.data.longitud() print "ACK [%i]" % self.ack.longitud() print "RTS [%i]" % self.rts.longitud() print "CTS [%i]" % self.cts.longitud() print "=====================================" print "\n\n" # Beacon list print "========= NEIGHBOR NODES INFORMATION ==========" for i in range(0, self.bcn.longitud()): leido = self.bcn.read(i) print "[MAC = %s]\t [Timestamp = %s]\t [Beacon Interval = %s]\t [OFFSET = %s]" % ( mac.which_dir(leido["MAC"]), leido["timestamp"], leido["BI"], leido["OFFSET"]) print "==============================================="
def run(self): pkt = self.socket.recv(1000) paquete_llegada = pickle.loads(pkt) # MAC requests an incoming packet to the PHY if (paquete_llegada["TIPO"]=="COLA"): tipo_pkt = paquete_llegada["DATOS"] len_data = self.data.longitud() len_ack = self.ack.longitud() len_rts = self.rts.longitud() len_cts = self.cts.longitud() if (tipo_pkt == "DATA") and len_data>0: # There are Data packets? self.data.elementos.reverse() x=self.data.read(0) phy_pkt = plcp.crear_paquete("SI",x) self.data.pop() self.data.elementos.reverse() elif tipo_pkt == "ACK" and len_ack>0: # There are ACK packets? self.ack.elementos.reverse() x=self.ack.read(0) phy_pkt = plcp.crear_paquete("SI",x) self.ack.pop() self.ack.elementos.reverse() elif tipo_pkt == "RTS" and len_rts>0: # There are RTS packets? self.rts.elementos.reverse() x=self.rts.read(0) phy_pkt = plcp.crear_paquete("SI",x) self.rts.pop() self.rts.elementos.reverse() elif tipo_pkt == "CTS" and len_cts>0: # There are CTS packets? self.cts.elementos.reverse() x=self.cts.read(0) phy_pkt = plcp.crear_paquete("SI",x) self.cts.pop() self.cts.elementos.reverse() else: # There are not packets phy_pkt = plcp.crear_paquete("NO",[]) plcp.enviar_a_mac(self.socket,phy_pkt) # Send the result (PHY packet) to MAC layer self.socket.close() # PHY packet generator script. It sends an 802.11 frame simulating its arrival from the wireless medium if (paquete_llegada["TIPO"]=="DATA" or paquete_llegada["TIPO"]=="DATA_FRAG"): if paquete_llegada["DATOS"]["INFO"]["mac_add1"] == self.my_mac: # Is the data packet addressed to this node? self.data.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"]=="ACK"): if paquete_llegada["DATOS"]["INFO"]["RX_add"] == self.my_mac:# Is the ACK addressed to this node? self.ack.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"]=="RTS"): #It is a RTS self.rts.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"]=="CTS"): #It is a CTS self.cts.push(paquete_llegada["DATOS"]) self.socket.close() if (paquete_llegada["TIPO"]=="BEACON"): #It is a BEACON beacon = paquete_llegada["DATOS"] beacon = beacon["INFO"] msg = plcp.nuevo_beacon() msg["MAC"]= beacon["mac_add2"] msg["timestamp"]=beacon["timestamp"] msg["BI"]=beacon["BI"] msg["OFFSET"]=time.time() - beacon["timestamp"] x = self.bcn.longitud() actualizado = False # Update the beacon list for i in range(0,x): if msg["MAC"]==self.bcn.read(i)["MAC"]: self.bcn.quitar(i) self.bcn.insert(i,msg) actualizado = True if actualizado == False: self.bcn.insert(x+1,msg) if (paquete_llegada["TIPO"]=="OTHER"): #print "No packet arrived" self.socket.close() #DEBUG print "=========== BUFFER STATUS ===========" print "DATA [%i]"%self.data.longitud() print "ACK [%i]"%self.ack.longitud() print "RTS [%i]"%self.rts.longitud() print "CTS [%i]"%self.cts.longitud() print "=====================================" print "\n\n" # Beacon list print "========= NEIGHBOR NODES INFORMATION ==========" for i in range (0,self.bcn.longitud()): leido = self.bcn.read(i) print "[MAC = %s]\t [Timestamp = %s]\t [Beacon Interval = %s]\t [OFFSET = %s]" %(mac.which_dir(leido["MAC"]),leido["timestamp"],leido["BI"],leido["OFFSET"]) print "==============================================="
def main(): def send_pkt(puerto, eof=False): return tb.txpath.send_pkt(puerto, eof) parser = OptionParser(option_class=eng_option, conflict_handler="resolve") parser.add_option("-a", "--args", type="string", default="", help="UHD device address args [default=%default]") parser.add_option("", "--spec", type="string", default=None, help="Subdevice of UHD device where appropriate") parser.add_option("-A", "--antenna", type="string", default=None, help="select Rx Antenna where appropriate") parser.add_option("-f", "--freq", type="eng_float", default = 2.412e9, help="set USRP2 carrier frequency, [default=%default]", metavar="FREQ") parser.add_option("-W", "--bandwidth", type="eng_float", default=10000000, help="set symbol bandwidth [default=%default]\ 20 MHz -> 802.11a/g, OFDM-symbolduration=4us, \ 10 MHz -> 802.11p, OFDM-symbolduration=8us") parser.add_option("", "--regime", type="string", default="1", help="set OFDM coderegime, [default=%default]\ 1 -> 6 (3) Mbit/s (BPSK r=0.5), \ 2 -> 9 (4.5) Mbit/s (BPSK r=0.75), \ 3 -> 12 (6) Mbit/s (QPSK r=0.5), \ 4 -> 18 (9) Mbit/s (QPSK r=0.75), \ 5 -> 24 (12) Mbit/s (QAM16 r=0.5), \ 6 -> 36 (18) Mbit/s (QAM16 r=0.75), \ 7 -> 48 (24) Mbit/s (QAM64 r=0.66), \ 8 -> 54 (27) Mbit/s (QAM64 r=0.75)") parser.add_option("-G", "--txgain", type="int", default=10 , help = "set USRP2 Tx GAIN in [dB] [default=%default]") parser.add_option("-g", "--gain", type="int", default=30 , help = "set USRP2 Rx GAIN in [dB] [default=%default]") parser.add_option("-n", "--norm", type="eng_float", default=0.3 , help="set gain factor for complex baseband floats [default=%default]") parser.add_option("-r", "--repetition", type="int", default=1 , help="set number of frame-copies to send, 0=infinite [default=%default] ") parser.add_option("-l", "--log", action="store_true", default=False, help="write debug-output of individual blocks to disk") parser.add_option("", "--PHYport", type="int", default=8000 , help="Port used for MAC-->PHY communication [default=%default] ") parser.add_option("", "--tune-delay", type="eng_float", default=1e-4, metavar="SECS", help="Carrier sensing parameter. Time to delay (in seconds) after changing frequency [default=%default]") parser.add_option("", "--dwell-delay", type="eng_float", default=1e-3, metavar="SECS", help="Carrier sensing parameter. Time to dwell (in seconds) at a given frequncy [default=%default]") parser.add_option("-F", "--fft-size", type="int", default=256, help="specify number of FFT bins [default=%default]") parser.add_option("-d", "--decim", type="intx", default=16, help="set the decimation value [default=%default]") parser.add_option("", "--real-time", action="store_true", default=False, help="Attempt to enable real-time scheduling") parser.add_option('-v', "--verbose", action="store_true", default=False, help="Print timming information, [default=%default]") (options, args) = parser.parse_args () # Stream sockets to ensure no packet loss during PHY<-->MAC communication server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((socket.gethostname(), options.PHYport)) s_cca = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.listen(1) # PHY is ready to attend MAC requests print '\n',"-------------------------" print " PHY (TX) layer running ..." print " (Ctrl + C) to exit" print "-------------------------",'\n' # Initial values of variables used in time measurement N_sym_ant=4 # OFDM symbols of the previous packet sent N_sensados = 0 # Number of power measurements N_paquetes = 0 # Number of packets sent tiempo_socket = 0 # Instant time of socket communication t_socket_TOTAL = 0 # Total time of socket communication T_sensado_USRP2 = 0 # Time of the USRP2 measuring the power T_sensado_PHY = 0 # Time elapsed in PHY layer due to a carrier sensing request T_transmitir_USRP2 = 0 # USRP2 TX time T_configurar_grafo = 0 # Time due to USRP2 graph management T_transmitir_PHY = 0 # Time elapsed in PHY layer due to a packet tx request first_time = True # Is the first time to transmit? fg_cca = sense_block(options) # Set up the carrier sensing block for the first time. while 1: socket_cliente, datos_cliente = server.accept() # Waiting a request from the MAC layer paquete_llegada=plcp.recibir_de_mac(socket_cliente) # Packet received from MAC if (paquete_llegada["TIPO"]=="PKT"): t_socket = time.time()-paquete_llegada["DATOS"]["INFO"]["timestamp"] t_socket_TOTAL =t_socket_TOTAL + t_socket #Update the time used in the socket communication. # If it is a 'send packet' request, it checks if the TX graph needs a reconfiguration if (paquete_llegada["TIPO"]=="PKT") and first_time == False and (N_sym_ant == paquete_llegada["DATOS"]["INFO"]["N_sym"]) == False: tb.stop() # It is necessary to switch the USRP2 functionality (802.11 transmitter or Carrier Sensing Function) # Carrier sensing request if (paquete_llegada["TIPO"]=="CCA"): t_sensadoA = time.time() fg_cca.start() t_reconfig = time.time()-t_sensadoA potencia_sensada=main_loop(fg_cca) # Power measurement fg_cca.stop() fg_cca.wait() # Wait until the USRP2 returns the Power measurement t_sensadoB = time.time() # PHY responds with the power measured to the MAC paquete=plcp.crear_paquete("CCA",potencia_sensada) plcp.enviar_a_mac(socket_cliente,paquete) t_sensadoC = time.time() T_sensado_USRP2 = T_sensado_USRP2 + (t_sensadoB - t_sensadoA) T_sensado_PHY = T_sensado_PHY + (t_sensadoC - t_sensadoA) N_sensados +=1 if options.verbose: print "Time elapsed on graph configuration (Carrier Sensing) = \t", t_reconfig # Send packet request if (paquete_llegada["TIPO"]=="PKT"): t_enviarA = time.time() leido=paquete_llegada["DATOS"] # Copy the packet to send from the MAC message info = leido["INFO"] N_sym=info["N_sym"] # Read N_sym and add N_sym to options mod = info["modulation"] # FIX ME! Another way to update N_sym and modulation values? parser.add_option("-T", "--nsym", type="int", default=N_sym) parser.add_option("", "--modulation", type="string", default=mod) (options, args) = parser.parse_args () # Check if the OFDM code regime or the amount of OFDM symbols to transmit differs from the last packet sent if first_time == True or (N_sym_ant == paquete_llegada["DATOS"]["INFO"]["N_sym"]) == False: #print "Re-setting USRP2 graph!" tb = transmitter_block(options) # If necessary, update the transmit flow-graph depending to the new OFDM parameters r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" t_2 = time.time() tb.start() # Send packet procedure starts t_enviarB= time.time() send_pkt(leido,eof=False) # 'leido' is a dictionary which contents the packet to transmit and other information send_pkt("",eof=True) t_enviarC = time.time() tb.wait() # Wait until USRP2 finishes the TX-graph t_enviarD= time.time() if options.verbose: print "Time elapsed on graph configuration (TX Packet) = \t", (t_enviarB - t_2) T_transmitir_USRP2 = T_transmitir_USRP2 + t_enviarC-t_enviarB T_configurar_grafo = T_configurar_grafo + t_enviarD-t_enviarA - (t_enviarC-t_enviarB) T_transmitir_PHY = T_transmitir_PHY + t_enviarD-t_enviarA # set values for the next packet tx request first_time=False N_sym_ant = N_sym # Keep a record of the OFDM symbols' Number N_paquetes += 1 if options.verbose: print "===================== Average statistics ====================" print "Number of Carrier Sensing Requests = ",N_sensados if N_sensados>0: print "Time spent by USRP2 on sensing channel = \t",T_sensado_USRP2/N_sensados print "Time spent by PHY layer on sensing channel = \t",T_sensado_PHY/N_sensados print "=============================================================" print "Number of packets transmitted = ",N_paquetes if N_paquetes>1: print "Time spent by USRP2 on sending a packet = \t",T_transmitir_USRP2/N_paquetes print "Time spent by USRP2 on configuring the graphs\t",T_configurar_grafo/N_paquetes print "Time spent by PHY on sending a packet = \t",T_transmitir_PHY/N_paquetes print "Time spent on Socket Communication = \t", t_socket_TOTAL/N_paquetes print "============================================================="