@aircast-4g/mavlink
Version:
TypeScript type generator for MAVLink dialects
2 lines (1 loc) • 1.72 kB
JavaScript
import{MAVLinkCRC as t}from"./crc.js";const e=254,n=253,s=6,i=10,l=2,r=13;function c(e,s){if(e.length<8)return{bytesConsumed:0};let i=0;for(;i<e.length&&254!==e[i]&&e[i]!==n;)i++;if(i===e.length)return{bytesConsumed:e.length};const l=e[i],r=l===n;if(e.length-i<(r?12:8))return{bytesConsumed:i};let c=i;const o={magic:l};c++,o.length=e[c++],r&&(o.incompatible_flags=e[c++],o.compatible_flags=e[c++]),o.sequence=e[c++],o.system_id=e[c++],o.component_id=e[c++],o.message_id=e[c++],r&&e.length-c>=2&&(o.message_id|=e[c++]<<8,o.message_id|=e[c++]<<16);const a=c-i+o.length+2;if(e.length-i<a)return{bytesConsumed:i};o.payload=e.slice(c,c+o.length),c+=o.length,o.checksum=e[c]|e[c+1]<<8,c+=2,r&&o.incompatible_flags&&1&o.incompatible_flags&&e.length-c>=13&&(o.signature=e.slice(c,c+13),c+=13);const g=e.slice(i+1,i+c-i-2);return o.crc_ok=t.validateWithTable(g,o.message_id,o.checksum,s),o.protocol_version=r?2:1,{frame:o,bytesConsumed:c-i}}function o(e,s,i,l,r,c,o){const a=2===(o??(e>255?2:1)),g=a?n:254,m=(a?10:6)+s.length+2,h=new ArrayBuffer(m),u=new DataView(h);let U=0;u.setUint8(U++,g),u.setUint8(U++,s.length),a?(u.setUint8(U++,0),u.setUint8(U++,0),u.setUint8(U++,r),u.setUint8(U++,i),u.setUint8(U++,l),u.setUint8(U++,255&e),u.setUint8(U++,e>>8&255),u.setUint8(U++,e>>16&255)):(u.setUint8(U++,r),u.setUint8(U++,i),u.setUint8(U++,l),u.setUint8(U++,255&e)),new Uint8Array(h,U,s.length).set(s),U+=s.length;const f=new Uint8Array(h,1,U-1),d=t.calculate(f,c);return u.setUint8(U++,255&d),u.setUint8(U++,d>>8&255),new Uint8Array(h)}export{l as MAVLINK_CHECKSUM_SIZE,r as MAVLINK_SIGNATURE_SIZE,s as MAVLINK_V1_HEADER_SIZE,e as MAVLINK_V1_MAGIC,i as MAVLINK_V2_HEADER_SIZE,n as MAVLINK_V2_MAGIC,o as createFrame,c as parseFrame};