Method: OpenC3::BinaryAccessor.read_array
- Defined in:
- lib/openc3/accessors/binary_accessor.rb
.read_array(bit_offset, bit_size, data_type, array_size, buffer, endianness) ⇒ Array
Reads an array of binary data of any data type from a buffer
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 |
# File 'lib/openc3/accessors/binary_accessor.rb', line 721 def self.read_array(bit_offset, bit_size, data_type, array_size, buffer, endianness) # Save given values of bit offset, bit size, and array_size given_bit_offset = bit_offset given_bit_size = bit_size given_array_size = array_size # Handle negative and zero bit sizes raise ArgumentError, "bit_size #{given_bit_size} must be positive for arrays" if bit_size <= 0 # Handle negative bit offsets if bit_offset < 0 bit_offset = ((buffer.length * 8) + bit_offset) raise_buffer_error(:read, buffer, data_type, given_bit_offset, given_bit_size) if bit_offset < 0 end # Handle negative and zero array sizes if array_size <= 0 if given_bit_offset < 0 raise ArgumentError, "negative or zero array_size (#{given_array_size}) cannot be given with negative bit_offset (#{given_bit_offset})" else array_size = ((buffer.length * 8) - bit_offset + array_size) if array_size == 0 return [] elsif array_size < 0 raise_buffer_error(:read, buffer, data_type, given_bit_offset, given_bit_size) end end end # Calculate number of items in the array # If there is a remainder then we have a problem raise ArgumentError, "array_size #{given_array_size} not a multiple of bit_size #{given_bit_size}" if array_size % bit_size != 0 num_items = array_size / bit_size # Define bounds of string to access this item lower_bound = bit_offset / 8 upper_bound = (bit_offset + array_size - 1) / 8 # Check for byte alignment byte_aligned = ((bit_offset % 8) == 0) case data_type when :STRING, :BLOCK ####################################### # Handle :STRING and :BLOCK data types ####################################### if byte_aligned value = [] num_items.times do value << self.read(bit_offset, bit_size, data_type, buffer, endianness) bit_offset += bit_size end else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end when :INT, :UINT ################################### # Handle :INT and :UINT data types ################################### if byte_aligned and (bit_size == 8 or bit_size == 16 or bit_size == 32 or bit_size == 64) ########################################################### # Handle byte-aligned 8, 16, 32, and 64 bit :INT and :UINT ########################################################### case bit_size when 8 if data_type == :INT value = buffer[lower_bound..upper_bound].unpack(PACK_8_BIT_INT_ARRAY) else # data_type == :UINT value = buffer[lower_bound..upper_bound].unpack(PACK_8_BIT_UINT_ARRAY) end when 16 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_16_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 2) value = temp.to_s.unpack(PACK_NATIVE_16_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_16_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_16_BIT_UINT_ARRAY) end end when 32 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_32_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 4) value = temp.to_s.unpack(PACK_NATIVE_32_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_32_BIT_UINT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_32_BIT_UINT_ARRAY) end end when 64 if data_type == :INT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_64_BIT_INT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 8) value = temp.to_s.unpack(PACK_NATIVE_64_BIT_INT_ARRAY) end else # data_type == :UINT if endianness == HOST_ENDIANNESS value = buffer[lower_bound..upper_bound].unpack(PACK_NATIVE_64_BIT_UINT_ARRAY) else # endianness != HOST_ENDIANNESS temp = self.byte_swap_buffer(buffer[lower_bound..upper_bound], 8) value = temp.to_s.unpack(PACK_NATIVE_64_BIT_UINT_ARRAY) end end end else ################################## # Handle :INT and :UINT Bitfields ################################## raise ArgumentError, "read_array does not support little endian bit fields with bit_size greater than 1-bit" if endianness == :LITTLE_ENDIAN and bit_size > 1 value = [] num_items.times do value << self.read(bit_offset, bit_size, data_type, buffer, endianness) bit_offset += bit_size end end when :FLOAT ########################## # Handle :FLOAT data type ########################## if byte_aligned case bit_size when 32 if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_32_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_32_BIT_FLOAT_ARRAY) end when 64 if endianness == :BIG_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_BIG_ENDIAN_64_BIT_FLOAT_ARRAY) else # endianness == :LITTLE_ENDIAN value = buffer[lower_bound..upper_bound].unpack(PACK_LITTLE_ENDIAN_64_BIT_FLOAT_ARRAY) end else raise ArgumentError, "bit_size is #{given_bit_size} but must be 32 or 64 for data_type #{data_type}" end else raise ArgumentError, "bit_offset #{given_bit_offset} is not byte aligned for data_type #{data_type}" end else ############################ # Handle Unknown data types ############################ raise ArgumentError, "data_type #{data_type} is not recognized" end value end |