def run(self): pkt = self.socket.recv(1000) arrived_packet = pickle.loads(pkt) ''' MAC layer checks whether there is a packet to transmit ''' if ("no_packet" == arrived_packet["HEADER"] ): # check whether the queue is empty print "Receive a query from MAC to check the buffer......" if self.cs.isEmpty() == True: print "The buffer is empty" x = plcp.create_packet("NO", "") plcp.send_to_mac(self.socket, x) #print "Send NO to MAC!!!!" else: self.cs.elements.reverse() if self.cs.read(0) == "[beacon packet]": x = plcp.create_packet("BEACON", "") print "Buffer has BEACON to send....." else: x = plcp.create_packet("YES", self.cs.read(0)) print "Buffer has a DATA to send......" self.cs.elements.reverse() plcp.send_to_mac(self.socket, x) print "Buffer sends DATA to MAC......" if ("remove" == arrived_packet["HEADER"] ): # Remove a packet from the buffer print "Upper buffer is set to remove a packet......" self.cs.elements.reverse() self.cs.pop() self.cs.elements.reverse() if ("copy" == arrived_packet["HEADER"] ): # A packet arrives from MAC layer self.cs2.push(arrived_packet["DATA"]) ''' Upper layer has a packet to send and stores it in the buffer ''' if (arrived_packet["HEADER"] == "PAYLOAD" ): # Payload received from Upper Layer Traffic generator self.cs.push(arrived_packet["DATA"]) #self.socket.close() elif (arrived_packet["HEADER"] == "BEACON"): # Beacon request arrival print "Buffer receives a BEACON from upper layer......" self.cs.push(arrived_packet["DATA"]) #self.socket.close() # Show the buffer state print "========== PACKETS TO TRANSMIT ============" print self.cs.elements print "===========================================" print "\n\n" print "======== RECEIVED PACKETS FROM MAC ========" print self.cs2.elements print "==========================================="
def run(self): pkt = self.socket.recv(1000) arrived_packet = pickle.loads(pkt) """ MAC layer checks whether there is a packet to transmit """ if "no_packet" == arrived_packet["HEADER"]: # check whether the queue is empty if self.cs.isEmpty() == True: x = plcp.create_packet("NO", "") plcp.send_to_mac(self.socket, x) else: self.cs.elements.reverse() if self.cs.read(0) == "[beacon packet]": x = plcp.create_packet("BEACON", "") else: x = plcp.create_packet("YES", self.cs.read(0)) self.cs.elements.reverse() plcp.send_to_mac(self.socket, x) if "remove" == arrived_packet["HEADER"]: # Remove a packet from the buffer self.cs.elements.reverse() self.cs.pop() self.cs.elements.reverse() if "copy" == arrived_packet["HEADER"]: # A packet arrives from MAC layer self.cs2.push(arrived_packet["DATA"]) """ Upper layer has a packet to send and stores it in the buffer """ if arrived_packet["HEADER"] == "PAYLOAD": # Payload received from Upper Layer Traffic generator self.cs.push(arrived_packet["DATA"]) self.socket.close() elif arrived_packet["HEADER"] == "BEACON": # Beacon request arrival self.cs.push(arrived_packet["DATA"]) self.socket.close() # Show the buffer state print "========== PACKETS TO TRANSMIT ============" print self.cs.elements print "===========================================" print "\n\n" print "======== RECEIVED PACKETS FROM MAC ========" print self.cs2.elements print "==========================================="
def main(): def send_pkt(puerto, main_regime, sub_regime, eof=False): return tb.txpath.send_pkt(puerto, main_regime, sub_regime, eof) # def test_attribute(puerto, eof=False): # return tb.txpath.test_attribute(puerto, eof) parser = OptionParser(option_class=eng_option, usage="%prog: [options]") 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=5.87e9, help="set USRP2 carrier frequency, [default=%default]", metavar="FREQ") parser.add_option("-w", "--window-size", type="int", default=48, help="set fir filter tap size [default=%default]") parser.add_option("-s", "--sync-length", type="int", default=256, help="sync length [default=%default]") parser.add_option("-W", "--bandwidth", type="eng_float", default=20e6, help="set symbol bandwidth [default=%default]\ 20 MHz -> 802.11a/g, OFDM-symbol duration=4us, 10 MHz -> 802.11p, OFDM-symbolduration=8us" ) parser.add_option("-g", "--gain", type="int", default=20, help="set USRP2 Rx GAIN in [dB] [default=%default]") parser.add_option("", "--regime", type="string", default="5", 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=20, help="set USRP2 Tx GAIN in [dB] [default=%default]") parser.add_option( "-n", "--norm", type="eng_float", default=0.4, help="set gain factor for complex baseband floats [default=%default]") parser.add_option("-N", "--Node", type="intx", default=1, help="USRP2 node [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=True, help="write debug-output of individual blocks to disk") parser.add_option("", "--PHYRXport", type="int", default=8513, help="Port used for PHY RX [default=%default] ") parser.add_option( "", "--PHYport", type="int", default=8013, help="Port used for MAC-->PHY communication [default=%default] ") parser.add_option("", "--real-time", action="store_true", default=True, help="Attempt to enable real-time scheduling") parser.add_option('-v', "--verbose", action="store_true", default=False, help="Print timing information, [default=%default]") parser.add_option("", "--nsym", type="int", default=1) parser.add_option("", "--modulation", type="string", default="bpsk") (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)) server.bind((socket.gethostname(), options.PHYport)) server.listen(1) # PHY is ready to attend MAC requests phy_rx_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) phy_rx_server.bind((socket.gethostname(), options.PHYRXport)) phy_rx_server.listen(1) print '\n', "-------------------------" print " PHY (TX) layer running ..." print " (Ctrl + C) to exit" print "-------------------------", '\n' my_mac = mac.usrp2_node(options.Node) # Assign the MAC address of the node rxclient = threading.Thread(target=RX_Client, args=(options, my_mac, phy_rx_server)) rxclient.start() # Initial values of variables used in time measurement N_sym_prev = 4 # OFDM symbols of the previous packet sent N_sensings = 0 # Number of power measurements N_packets = 0 # Number of packets sent time_socket = 0 # Instant time of socket communication t_socket_TOTAL = 0 # Total time of socket communication T_sense_USRP2 = 0 # Time of the USRP2 measuring the power T_sense_PHY = 0 # Time elapsed in PHY layer due to a carrier sensing request T_transmit_USRP2 = 0 # USRP2 TX time T_configure_USRP2 = 0 # Time due to USRP2 graph management T_transmit_PHY = 0 # Time elapsed in PHY layer due to a packet tx request main_regime = int(options.regime) sub_regime = 0 msgq = gr.msg_queue(1) first_time = True # Is the first time to transmit? #fg_cca = sense_block(options) # Set up the carrier sensing block for the first time. tb = wifi_rx(options, socket.gethostname(), msgq) tb.Start(True) while 1: socket_client, conn_addr = server.accept( ) # Waiting a request from the MAC layer arrived_packet = plcp.receive_from_mac( socket_client) # Packet received from MAC print "Receive a packet from MAC......" print "Arrived packet header: %s......" % arrived_packet["HEADER"] print "Arrived DATA: %s......" % arrived_packet["DATA"] if (arrived_packet["HEADER"] == "PKT"): print "PHY receives a DATA frame from MAC......" t_socket = time.time( ) - arrived_packet["DATA"]["INFO"]["timestamp"] t_socket_TOTAL = t_socket_TOTAL + t_socket #Update the time used in the socket communication. t_sendA = time.time() item = arrived_packet[ "DATA"] # Copy the packet to send from the MAC message info = item["INFO"] N_sym = info["N_sym"] # Read N_sym and add N_sym to options mod = info["modulation"] options.nsym = N_sym options.modulation = mod 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 #test_attribute(item,eof=False) t_sendB = time.time() send_pkt( item, main_regime, sub_regime, eof=False ) # 'item' is a dictionary which contents the packet to transmit and other information print "Complete send_pkt(item,eof=False)" send_pkt("", main_regime, sub_regime, eof=True) print "Complete send_pkt(" ",eof=True)" t_sendC = time.time() print "t_sendC" tb.wait() # Wait until USRP2 finishes the TX-graph print "PHY successfully transmit one frame!!!!!!!" t_sendD = time.time() if options.verbose: print "Time elapsed on graph configuration (TX Packet) = \t", ( t_sendB - t_2) T_transmit_USRP2 = T_transmit_USRP2 + t_sendC - t_sendB T_configure_USRP2 = T_configure_USRP2 + t_sendD - t_sendA - ( t_sendC - t_sendB) T_transmit_PHY = T_transmit_PHY + t_sendD - t_sendA # set values for the next packet tx request first_time = False N_sym_prev = N_sym # Keep a record of the OFDM symbols' Number N_packets += 1 # Carrier sensing request if (arrived_packet["HEADER"] == "CCA"): #print "PHY receives a CCA from MAC......" t_senseA = time.time() #print "Breakpoint 1......" msgq.flush() #print "Breakpoint 2......" t_reconfig = time.time() - t_senseA #print "Breakpoint 3......" m = msgq.delete_head() #print "Breakpoint 4......" t = m.to_string() #print "The head deleted is %s......" %t #print "Breakpoint 5......" msgdata = struct.unpack('%df' % (int(m.arg2()), ), t) #print "Breakpoint 6......" sensed_power = msgdata[0] #print "Breakpoint 7......" t_senseB = time.time() # PHY responds with the power measured to the MAC #print "PHY creates CCA......" packet = plcp.create_packet("CCA", sensed_power) #print "PHY sends CCA to MAC......" plcp.send_to_mac(socket_client, packet) #print "MAC finishes sending CCA to MAC......" t_senseC = time.time() T_sense_USRP2 = T_sense_USRP2 + (t_senseB - t_senseA) T_sense_PHY = T_sense_PHY + (t_senseC - t_senseA) N_sensings += 1 if options.verbose: print "Time elapsed on graph configuration (Carrier Sensing) = \t", t_reconfig if (arrived_packet["HEADER"] == "RATE"): header_pkt = arrived_packet["DATA"] print "The header of the pkt is %s......................" % header_pkt main_regime, sub_regime = v2r(int(header_pkt)) options.regime = str(main_regime) print "Now the rate is %s..............................." % options.regime # MAC requests an incoming packet to the PHY if (arrived_packet["HEADER"] == "TAIL"): header_pkt = arrived_packet["DATA"] len_data = data.length() len_ack = ack.length() len_rts = rts.length() len_cts = cts.length() if (header_pkt == "DATA") and len_data > 0: # There are Data packets? #print "Header is DATA......" data.elements.reverse() x = data.read(0) phy_pkt = plcp.create_packet("YES", x) data.pop() data.elements.reverse() elif header_pkt == "ACK" and len_ack > 0: # There are ACK packets? ack.elements.reverse() x = ack.read(0) phy_pkt = plcp.create_packet("YES", x) ack.pop() ack.elements.reverse() elif header_pkt == "RTS" and len_rts > 0: # There are RTS packets? rts.elements.reverse() x = rts.read(0) phy_pkt = plcp.create_packet("YES", x) rts.pop() rts.elements.reverse() elif header_pkt == "CTS" and len_cts > 0: # There are CTS packets? cts.elements.reverse() x = cts.read(0) phy_pkt = plcp.create_packet("YES", x) cts.pop() cts.elements.reverse() else: # There are not packets #print "There are no packets in PHY......" phy_pkt = plcp.create_packet("NO", []) #print "PHY sends notifications to MAC......" plcp.send_to_mac( socket_client, phy_pkt) # Send the result (PHY packet) to MAC layer #socket_client.close() if options.verbose: print "===================== Average statistics ====================" print "Number of Carrier Sensing Requests = ", N_sensings if N_sensings > 0: print "Time spent by USRP2 on sensing channel = \t", T_sense_USRP2 / N_sensings print "Time spent by PHY layer on sensing channel = \t", T_sense_PHY / N_sensings print "=============================================================" print "Number of packets transmitted = ", N_packets if N_packets > 1: print "Time spent by USRP2 on sending a packet = \t", T_transmit_USRP2 / N_packets print "Time spent by USRP2 on configuring the graphs\t", T_configure_USRP2 / N_packets print "Time spent by PHY on sending a packet = \t", T_transmit_PHY / N_packets print "Time spent on Socket Communication = \t", t_socket_TOTAL / N_packets print "=============================================================" tb.Wait()
def main(): def send_pkt(puerto, eof=False): return tb.txpath.send_pkt(puerto, eof) parser = OptionParser(option_class=eng_option, usage="%prog: [options]") 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 = 5.825e9, help="set USRP2 carrier frequency, [default=%default]", metavar="FREQ") parser.add_option("-w", "--window-size", type="int", default=48 , help = "set fir filter tap size [default=%default]") parser.add_option("-s", "--sync-length", type="int", default=256 , help = "sync length [default=%default]") parser.add_option("-W", "--bandwidth", type="eng_float", default=1e7, help="set symbol bandwidth [default=%default]\ 20 MHz -> 802.11a/g, OFDM-symbol duration=4us, 10 MHz -> 802.11p, OFDM-symbolduration=8us") parser.add_option("-g", "--gain", type="int", default=0 , help = "set USRP2 Rx GAIN in [dB] [default=%default]") 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("-n", "--norm", type="eng_float", default=0.3 , help="set gain factor for complex baseband floats [default=%default]") parser.add_option("-N", "--Node", type="intx", default=1, help="USRP2 node [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("", "--PHYRXport", type="int", default=8513 , help="Port used for PHY RX [default=%default] ") parser.add_option("", "--PHYport", type="int", default=8013 , help="Port used for MAC-->PHY communication [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 timing information, [default=%default]") parser.add_option("", "--nsym", type="int", default=1) parser.add_option("", "--modulation", type="string", default="bpsk") (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)) server.listen(1) # PHY is ready to attend MAC requests phy_rx_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) phy_rx_server.bind((socket.gethostname(), options.PHYRXport)) phy_rx_server.listen(1) print '\n',"-------------------------" print " PHY (TX) layer running ..." print " (Ctrl + C) to exit" print "-------------------------",'\n' my_mac = mac.usrp2_node(options.Node) # Assign the MAC address of the node rxclient = threading.Thread(target=RX_Client, args=(options,my_mac,phy_rx_server)) rxclient.start() # Initial values of variables used in time measurement N_sym_prev=4 # OFDM symbols of the previous packet sent N_sensings = 0 # Number of power measurements N_packets = 0 # Number of packets sent time_socket = 0 # Instant time of socket communication t_socket_TOTAL = 0 # Total time of socket communication T_sense_USRP2 = 0 # Time of the USRP2 measuring the power T_sense_PHY = 0 # Time elapsed in PHY layer due to a carrier sensing request T_transmit_USRP2 = 0 # USRP2 TX time T_configure_USRP2 = 0 # Time due to USRP2 graph management T_transmit_PHY = 0 # Time elapsed in PHY layer due to a packet tx request msgq = gr.msg_queue(1) first_time = True # Is the first time to transmit? #fg_cca = sense_block(options) # Set up the carrier sensing block for the first time. rb = receiver_block(options,socket.gethostname(),msgq) rb.start() while 1: socket_client, conn_addr = server.accept() # Waiting a request from the MAC layer arrived_packet=plcp.receive_from_mac(socket_client) # Packet received from MAC if (arrived_packet["HEADER"]=="PKT"): t_socket = time.time()-arrived_packet["DATA"]["INFO"]["timestamp"] t_socket_TOTAL =t_socket_TOTAL + t_socket #Update the time used in the socket communication. t_sendA = time.time() item=arrived_packet["DATA"] # Copy the packet to send from the MAC message info = item["INFO"] N_sym=info["N_sym"] # Read N_sym and add N_sym to options mod = info["modulation"] options.nsym = N_sym options.modulation=mod 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_sendB= time.time() send_pkt(item,eof=False) # 'item' is a dictionary which contents the packet to transmit and other information send_pkt("",eof=True) t_sendC = time.time() tb.wait() # Wait until USRP2 finishes the TX-graph t_sendD= time.time() if options.verbose: print "Time elapsed on graph configuration (TX Packet) = \t", (t_sendB - t_2) T_transmit_USRP2 = T_transmit_USRP2 + t_sendC-t_sendB T_configure_USRP2 = T_configure_USRP2 + t_sendD-t_sendA - (t_sendC-t_sendB) T_transmit_PHY = T_transmit_PHY + t_sendD-t_sendA # set values for the next packet tx request first_time=False N_sym_prev = N_sym # Keep a record of the OFDM symbols' Number N_packets += 1 # Carrier sensing request if (arrived_packet["HEADER"]=="CCA"): t_senseA = time.time() msgq.flush() t_reconfig = time.time()-t_senseA m=msgq.delete_head() t = m.to_string() msgdata = struct.unpack('%df' % (int(m.arg2()),), t) sensed_power=msgdata[0] t_senseB = time.time() # PHY responds with the power measured to the MAC packet=plcp.create_packet("CCA",sensed_power) plcp.send_to_mac(socket_client,packet) t_senseC = time.time() T_sense_USRP2 = T_sense_USRP2 + (t_senseB - t_senseA) T_sense_PHY = T_sense_PHY + (t_senseC - t_senseA) N_sensings +=1 if options.verbose: print "Time elapsed on graph configuration (Carrier Sensing) = \t", t_reconfig # MAC requests an incoming packet to the PHY if (arrived_packet["HEADER"]=="TAIL"): header_pkt = arrived_packet["DATA"] len_data = data.length() len_ack = ack.length() len_rts = rts.length() len_cts = cts.length() if (header_pkt == "DATA") and len_data>0: # There are Data packets? data.elements.reverse() x=data.read(0) phy_pkt = plcp.create_packet("YES",x) data.pop() data.elements.reverse() elif header_pkt == "ACK" and len_ack>0: # There are ACK packets? ack.elements.reverse() x=ack.read(0) phy_pkt = plcp.create_packet("YES",x) ack.pop() ack.elements.reverse() elif header_pkt == "RTS" and len_rts>0: # There are RTS packets? rts.elements.reverse() x=rts.read(0) phy_pkt = plcp.create_packet("YES",x) rts.pop() rts.elements.reverse() elif header_pkt == "CTS" and len_cts>0: # There are CTS packets? cts.elements.reverse() x=cts.read(0) phy_pkt = plcp.create_packet("YES",x) cts.pop() cts.elements.reverse() else: # There are not packets phy_pkt = plcp.create_packet("NO",[]) plcp.send_to_mac(socket_client,phy_pkt) # Send the result (PHY packet) to MAC layer socket_client.close() if options.verbose: print "===================== Average statistics ====================" print "Number of Carrier Sensing Requests = ",N_sensings if N_sensings>0: print "Time spent by USRP2 on sensing channel = \t",T_sense_USRP2/N_sensings print "Time spent by PHY layer on sensing channel = \t",T_sense_PHY/N_sensings print "=============================================================" print "Number of packets transmitted = ",N_packets if N_packets>1: print "Time spent by USRP2 on sending a packet = \t",T_transmit_USRP2/N_packets print "Time spent by USRP2 on configuring the graphs\t",T_configure_USRP2/N_packets print "Time spent by PHY on sending a packet = \t",T_transmit_PHY/N_packets print "Time spent on Socket Communication = \t", t_socket_TOTAL/N_packets print "=============================================================" rb.wait()