axez
Version:
Simple AX.25 Implementation for Packet Radio
1 lines • 2.66 kB
JavaScript
function AXEZ(){return{readFrame:data=>{let frame={},result=Array.from(new Uint8Array(data));frame.destination=result.slice(2,8).map(val=>String.fromCharCode(val/2)).join("").trim(),frame.destinationSSID=result.slice(8)[0]-225,frame.destinationSSID=0<frame.destinationSSID?parseInt(frame.destinationSSID/2)+1:0,frame.source=result.slice(9,15).map(val=>String.fromCharCode(val/2)).join("").trim(),frame.sourceSSID=result.slice(15)[0]-224,frame.sourceSSID=0<frame.sourceSSID?parseInt(frame.sourceSSID/2):0;let repeaters=[],hasRepeaters=!1;result.slice(15)[0]/2===parseInt(result.slice(15)[0]/2)&&(hasRepeaters=!0);let position=18;if(hasRepeaters){let tail=result.slice(16,result.length-1),parts=[];for(let i=0;i<tail.length;i+=7)parts.push(tail.slice(i,i+7));for(let j=0;j<parts.length&&!(parts[j].length<7);j++){var callsign=parts[j].slice(0,6).map(val=>String.fromCharCode(val>>1&127)).join("").trim(),ssid=parts[j].slice(6)[0]>>1&15;if(repeaters.push({callsign:callsign,ssid:ssid}),1==(1&parts[j][6]))break}position+=7*repeaters.length}return frame.repeaters=repeaters,frame.message=result.slice(position,-1).map(val=>String.fromCharCode(val)).join(""),frame},createFrame:(frameData={})=>{let frame=[192,0],destination=(frameData.destination.toString().toLocaleUpperCase()+" ").split("").map(val=>2*val.charCodeAt(0));6<destination.length&&(destination=destination.slice(0,6));for(let i=0;i<destination.length;i++)frame.push(destination[i]);var sourceSSID=frameData.destinationSSID<16&&-1<frameData.destinationSSID?frameData.destinationSSID:0;frame.push(224+2*sourceSSID);let source=(frameData.source.toString().toLocaleUpperCase()+" ").split("").map(val=>2*val.charCodeAt(0));6<source.length&&(source=source.slice(0,6));for(let i=0;i<source.length;i++)frame.push(source[i]);sourceSSID=frameData.sourceSSID<16&&-1<frameData.sourceSSID?frameData.sourceSSID:0;frameData.repeaters&&frameData.repeaters.length?frame.push(224+2*sourceSSID):frame.push(224+2*sourceSSID+1);let repeaters=frameData.repeaters||[];if(0<repeaters.length)for(let i=0;i<repeaters.length;i++){var repeater=(repeaters[i].callsign.toLocaleUpperCase()+" ").split("").map(val=>2*val.charCodeAt(0)).slice(0,6);for(let j=0;j<repeater.length;j++)frame.push(repeater[j]);i===repeaters.length-1?frame.push(2*(repeaters[i].ssid||0)+1):frame.push(2*(repeaters[i].ssid||0))}frameData.aprs?frame.push(3):frame.push(0),frame.push(240);var content=frameData.message.split("").map(val=>val.charCodeAt(0));for(let i=0;i<content.length;i++)frame.push(content[i]);return frame.push(192),new Uint8Array(frame)}}}"undefined"!=typeof module&&module&&module.exports&&(module.exports=AXEZ);
;