duggabe

pkt_xmt_epy_block_0.py

Dec 10th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. """
  2. Embedded Python Block: File Source to Tagged Stream
  3. """
  4.  
  5. import numpy as np
  6. from gnuradio import gr
  7. import time
  8. import pmt
  9. import os.path
  10. import sys
  11. import base64
  12.  
  13. """
  14. State definitions
  15.    0   idle
  16.    1   send preamble
  17.    2   send file data
  18.    3   send file name
  19.    4   send post filler
  20. """
  21.  
  22. class blk(gr.sync_block):
  23.     def __init__(self, FileName='None', Pkt_len=52, Num_of_preambles=64, Num_of_postambles=16):
  24.         gr.sync_block.__init__(
  25.             self,
  26.             name='EPB: File Source to Tagged Stream',
  27.             in_sig=None,
  28.             out_sig=[np.uint8])
  29.         self.FileName = FileName
  30.         self.Pkt_len = Pkt_len
  31.         self.Num_of_preambles = Num_of_preambles
  32.         self.Num_of_postambles = Num_of_postambles
  33.         self.state = 0      # idle state
  34.         self.pre_count = 0
  35.         self.indx = 0
  36.         self._debug = 0     # debug
  37.         self.data = ""
  38.  
  39.         if (os.path.exists(self.FileName)):
  40.             # open input file
  41.             self.f_in = open (self.FileName, 'rb')
  42.             self._eof = False
  43.             if (self._debug):
  44.                 print ("File name:", self.FileName)
  45.             self.state = 1
  46.         else:
  47.             print(self.FileName, 'does not exist')
  48.             self._eof = True
  49.             self.state = 0
  50.  
  51.         self.char_list = [37,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, 85,85,85,93]
  52.         self.c_len = len (self.char_list)
  53.         # print (self.c_len)
  54.         self.filler = [37,85,85,85, 35,69,79,70, 85,85,85,85,85,85,85,85, 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, 85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, 85,85,85,93]
  55.         self.f_len = len (self.filler)
  56.  
  57.     def work(self, input_items, output_items):
  58.  
  59.         if (self.state == 0):
  60.             # idle
  61.             return (0)
  62.  
  63.         elif (self.state == 1):
  64.             # send preamble
  65.             if (self._debug):
  66.                 print ("state = 1", self.pre_count)
  67.             key1 = pmt.intern("packet_len")
  68.             val1 = pmt.from_long(self.c_len)
  69.             self.add_item_tag(0, # Write to output port 0
  70.                 self.indx,   # Index of the tag
  71.                 key1,   # Key of the tag
  72.                 val1    # Value of the tag
  73.                 )
  74.             self.indx += self.c_len
  75.             i = 0
  76.             while (i < self.c_len):
  77.                 output_items[0][i] = self.char_list[i]
  78.                 i += 1
  79.             self.pre_count += 1
  80.             if (self.pre_count > self.Num_of_preambles):    # number of preamble packets
  81.                 self.pre_count = 0
  82.                 self.state = 2      # send msg
  83.             return (self.c_len)
  84.  
  85.         elif (self.state == 2):
  86.             while (not (self._eof)):
  87.                 buff = self.f_in.read (self.Pkt_len)
  88.                 b_len = len(buff)
  89.                 if b_len == 0:
  90.                     print ('End of file')
  91.                     self._eof = True
  92.                     self.f_in.close()
  93.                     self.state = 3      # send file name
  94.                     self.pre_count = 0
  95.                     break
  96.                 # convert to Base64
  97.                 encoded = base64.b64encode (buff)
  98.                 e_len = len(encoded)
  99.                 if (self._debug):
  100.                     print ('b64 length =', e_len)
  101.                 key0 = pmt.intern("packet_len")
  102.                 val0 = pmt.from_long(e_len)
  103.                 self.add_item_tag(0, # Write to output port 0
  104.                     self.indx,   # Index of the tag
  105.                     key0,   # Key of the tag
  106.                     val0    # Value of the tag
  107.                     )
  108.                 self.indx += e_len
  109.                 i = 0
  110.                 while (i < e_len):
  111.                     output_items[0][i] = encoded[i]
  112.                     i += 1
  113.                 return (e_len)
  114.  
  115.         elif (self.state == 3):
  116.             # send file name
  117.             fn_len = len (self.FileName)
  118.             key1 = pmt.intern("packet_len")
  119.             val1 = pmt.from_long(fn_len+8)
  120.             self.add_item_tag(0, # Write to output port 0
  121.                 self.indx,   # Index of the tag
  122.                 key1,   # Key of the tag
  123.                 val1    # Value of the tag
  124.                 )
  125.             self.indx += (fn_len+8)
  126.             i = 0
  127.             while (i < 8):
  128.                 output_items[0][i] = self.filler[i]
  129.                 i += 1
  130.             j = 0
  131.             while (i < (fn_len+8)):
  132.                 output_items[0][i] = ord(self.FileName[j])
  133.                 i += 1
  134.                 j += 1
  135.             self.state = 4
  136.             return (fn_len+8)
  137.  
  138.         elif (self.state == 4):
  139.             # send post filler
  140.             if (self._debug):
  141.                 print ("state = 4", self.pre_count)
  142.             key1 = pmt.intern("packet_len")
  143.             val1 = pmt.from_long(self.f_len)
  144.             self.add_item_tag(0, # Write to output port 0
  145.                 self.indx,   # Index of the tag
  146.                 key1,   # Key of the tag
  147.                 val1    # Value of the tag
  148.                 )
  149.             self.indx += self.f_len
  150.             i = 0
  151.             while (i < self.f_len):
  152.                 output_items[0][i] = self.filler[i]
  153.                 i += 1
  154.             self.pre_count += 1
  155.             if (self.pre_count > self.Num_of_postambles):   # number of postamble packets
  156.                 self.pre_count = 0
  157.                 self.state = 0      # idle
  158.             return (self.f_len)
  159.  
  160.         return (0)
  161.  
  162.  
Add Comment
Please, Sign In to add comment