Ejemplo n.º 1
0
        public int Read(out Packet packet, out byte[] packet_data)
        {
            int packet_header_size = Marshal.SizeOf(typeof(Packet));
            int packet_size        = packet_header_size + 1024;

            byte[]    buffer = new byte[packet_size];
            ErrorCode error_code;
            int       bytes_read;

            error_code = Reader.Read(buffer, 0, buffer.Length, 100, out bytes_read);

            if (error_code != ErrorCode.None)
            {
                throw new Exception(UsbDevice.LastErrorString);
            }

            if (bytes_read == 0)
            {
                throw new Exception(UsbDevice.LastErrorString);
            }

            byte[] packet_header_bytes = new byte[packet_header_size];

            Array.Copy(buffer, packet_header_bytes, packet_header_size);

            packet = Protocol.ByteArrayToStructure <Packet>(packet_header_bytes);

            packet_data = new byte[packet.size];

            Array.Copy(buffer, packet_header_size, packet_data, 0, packet.size);

            return(bytes_read);
        }
Ejemplo n.º 2
0
 public static ProtocolDataType GetPacket(byte[] buffer)
 {
     return(Protocol.ByteArrayToStructure <ProtocolDataType>(buffer));
 }
Ejemplo n.º 3
0
        public static void GetCapability(GarminUnit garmin)
        {
            Packet p;
            bool   done = false;

            p = Packet.CreatePacket(PacketType.Application, (ushort)Protocol.PacketID.ProductRequest, 0);
            garmin.Write(Protocol.StructureToByteArray <Packet>(p));
            byte[] packet_data;
            while (!done && garmin.Read(out p, out packet_data) > 0)
            {
                switch (p.id)
                {
                case (ushort)Protocol.PacketID.ProductData:
                    byte[] pdt_array = new byte[4];
                    Array.Copy(packet_data, pdt_array, 4);
                    int     pdt_descr_size  = packet_data.Length - 4;
                    byte[]  pdt_descr_array = new byte[pdt_descr_size];
                    Product pdt             = Protocol.ByteArrayToStructure <Product>(pdt_array);
                    Array.Copy(packet_data, 4, pdt_descr_array, 0, pdt_descr_size);
                    garmin.SetProductType(pdt, System.Text.ASCIIEncoding.ASCII.GetString(pdt_descr_array));
                    break;

                case (ushort)Protocol.PacketID.ExtProductData:
                    var extended_data = System.Text.ASCIIEncoding.ASCII.GetString(packet_data);
                    garmin.SetExtendedDescription(extended_data);
                    break;

                case (ushort)Protocol.PacketID.ProtocolArray:
                    int size = packet_data.Length / 3;
                    ProtocolDataType[] data_types = new ProtocolDataType[size];
                    for (int i = 0; i < size; i++)
                    {
                        byte[] temp_buffer = new byte[3];
                        Array.Copy(packet_data, i * 3, temp_buffer, 0, 3);
                        data_types[i] = ProtocolDataType.GetPacket(temp_buffer);
                    }
                    int j = 0;
                    while (j < size)
                    {
                        switch (data_types[j].Tag)
                        {
                        case Tags.Phys_Prot_Id:
                            garmin.SetCapability("protocol.physical", data_types[j].data);
                            j++;
                            break;

                        case Tags.Link_Prot_Id:
                            garmin.SetCapability("protocol.link", data_types[j].data);
                            j++;
                            break;

                        case Tags.Appl_Prot_Id:
                            application_protocol   app  = data_types[j].GetApplicationProtocol();
                            List <garmin_datatype> phys = new  List <garmin_datatype>();
                            for (++j; j < size; j++)
                            {
                                if (data_types[j].Tag != Tags.Data_Type_Id)
                                {
                                    Protocol.AssignProtocols(garmin, app, phys);
                                    break;
                                }
                                phys.Add(data_types[j].GetDataType());
                            }
                            break;

                        case Tags.Data_Type_Id:
                            // taken care of in application protocol section
                            break;

                        default:
                            j++;
                            break;
                        }
                    }


                    done = true;
                    break;

                default:
                    break;
                }
            }
        }