Parser.java
Go to the documentation of this file.
00001 /* AUTO-GENERATED FILE.  DO NOT MODIFY.
00002  *
00003  * This class was automatically generated by the
00004  * java mavlink generator tool. It should not be modified by hand.
00005  */
00006 
00007 package com.MAVLink;
00008 
00009 import com.MAVLink.MAVLinkPacket;
00010 import com.MAVLink.Messages.MAVLinkStats;
00011 
00012 public class Parser {
00013 
00017     enum MAV_states {
00018         MAVLINK_PARSE_STATE_UNINIT, MAVLINK_PARSE_STATE_IDLE, MAVLINK_PARSE_STATE_GOT_STX, MAVLINK_PARSE_STATE_GOT_LENGTH, MAVLINK_PARSE_STATE_GOT_SEQ, MAVLINK_PARSE_STATE_GOT_SYSID, MAVLINK_PARSE_STATE_GOT_COMPID, MAVLINK_PARSE_STATE_GOT_MSGID, MAVLINK_PARSE_STATE_GOT_CRC1, MAVLINK_PARSE_STATE_GOT_PAYLOAD
00019     }
00020 
00021     MAV_states state = MAV_states.MAVLINK_PARSE_STATE_UNINIT;
00022 
00023     static boolean msg_received;
00024 
00025     public MAVLinkStats stats = new MAVLinkStats();
00026     private MAVLinkPacket m;
00027 
00037     public MAVLinkPacket mavlink_parse_char(int c) {
00038         msg_received = false;
00039 
00040         switch (state) {
00041         case MAVLINK_PARSE_STATE_UNINIT:
00042         case MAVLINK_PARSE_STATE_IDLE:
00043 
00044             if (c == MAVLinkPacket.MAVLINK_STX) {
00045                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_STX;
00046             }
00047             break;
00048 
00049         case MAVLINK_PARSE_STATE_GOT_STX:
00050             if (msg_received) {
00051                 msg_received = false;
00052                 state = MAV_states.MAVLINK_PARSE_STATE_IDLE;
00053             } else {
00054                 m = new MAVLinkPacket(c);
00055                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_LENGTH;
00056             }
00057             break;
00058 
00059         case MAVLINK_PARSE_STATE_GOT_LENGTH:
00060             m.seq = c;
00061             state = MAV_states.MAVLINK_PARSE_STATE_GOT_SEQ;
00062             break;
00063 
00064         case MAVLINK_PARSE_STATE_GOT_SEQ:
00065             m.sysid = c;
00066             state = MAV_states.MAVLINK_PARSE_STATE_GOT_SYSID;
00067             break;
00068 
00069         case MAVLINK_PARSE_STATE_GOT_SYSID:
00070             m.compid = c;
00071             state = MAV_states.MAVLINK_PARSE_STATE_GOT_COMPID;
00072             break;
00073 
00074         case MAVLINK_PARSE_STATE_GOT_COMPID:
00075             m.msgid = c;
00076             if (m.len == 0) {
00077                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_PAYLOAD;
00078             } else {
00079                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_MSGID;
00080             }
00081             break;
00082 
00083         case MAVLINK_PARSE_STATE_GOT_MSGID:
00084             m.payload.add((byte) c);
00085             if (m.payloadIsFilled()) {
00086                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_PAYLOAD;
00087             }
00088             break;
00089 
00090         case MAVLINK_PARSE_STATE_GOT_PAYLOAD:
00091             m.generateCRC();
00092             // Check first checksum byte
00093             if (c != m.crc.getLSB()) {
00094                 msg_received = false;
00095                 state = MAV_states.MAVLINK_PARSE_STATE_IDLE;
00096                 if (c == MAVLinkPacket.MAVLINK_STX) {
00097                     state = MAV_states.MAVLINK_PARSE_STATE_GOT_STX;
00098                     m.crc.start_checksum();
00099                 }
00100                 stats.crcError();
00101             } else {
00102                 state = MAV_states.MAVLINK_PARSE_STATE_GOT_CRC1;
00103             }
00104             break;
00105 
00106         case MAVLINK_PARSE_STATE_GOT_CRC1:
00107             // Check second checksum byte
00108             if (c != m.crc.getMSB()) {
00109                 msg_received = false;
00110                 state = MAV_states.MAVLINK_PARSE_STATE_IDLE;
00111                 if (c == MAVLinkPacket.MAVLINK_STX) {
00112                     state = MAV_states.MAVLINK_PARSE_STATE_GOT_STX;
00113                     m.crc.start_checksum();
00114                 }
00115                 stats.crcError();
00116             } else { // Successfully received the message
00117                 stats.newPacket(m);
00118                 msg_received = true;
00119                 state = MAV_states.MAVLINK_PARSE_STATE_IDLE;
00120             }
00121 
00122             break;
00123 
00124         }
00125         if (msg_received) {
00126             return m;
00127         } else {
00128             return null;
00129         }
00130     }
00131 
00132 }


mavlink
Author(s): Lorenz Meier
autogenerated on Sun May 22 2016 04:05:43