import {Buffer} from "buffer"; import {OAD} from "./OAD"; import {ROAD} from "./ROAD"; import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; import {AxdrEnum} from "../asn1.axdr/AxdrEnum"; enum Choice { _ERR_NONE_SELECTED = -1, OAD = 0, ROAD = 1 } export class CSD implements AxdrType{ dataCode : Buffer | null = null; choice : Choice = Choice._ERR_NONE_SELECTED; oad : OAD | null = null; road : ROAD | null = null; constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } set_oad(oad : OAD){ this.resetChoices(); this.choice = Choice.OAD; this.oad = oad; } set_road(road : ROAD){ this.resetChoices(); this.choice = Choice.ROAD; this.road = road; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; let choosen = new AxdrEnum(); choosen.set_const(); codeLength += choosen.decode(input); this.resetChoices(); this.choice = choosen.getValue(); if (this.choice == Choice.OAD) { this.oad = new OAD(); codeLength += this.oad.decode(input); return codeLength; } if (this.choice == Choice.ROAD) { this.road = new ROAD(); codeLength += this.road.decode(input); return codeLength; } throw new Error("Error decoding AxdrChoice: Identifier matched to no item."); } encode(output: ReverseByteArrayOutputStream): number { if (this.dataCode != null) { for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } return this.dataCode.length; } if (this.choice == Choice._ERR_NONE_SELECTED) { throw new Error("Error encoding AxdrChoice: No item in choice was selected."); } let codeLength = 0; if (this.choice == Choice.ROAD) { codeLength += this.road.encode(output); let c = new AxdrEnum(); c.set_min_max_val(1) codeLength += c.encode(output); return codeLength; } if (this.choice == Choice.OAD) { codeLength += this.oad.encode(output); let c = new AxdrEnum(); c.set_min_max_val(0) codeLength += c.encode(output); return codeLength; } throw new Error("Error encoding AxdrChoice: No item in choice was encoded."); } getChoiceIndex() : Choice{ return this.choice; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } resetChoices() { this.choice = Choice._ERR_NONE_SELECTED; this.oad = null; this.road = null; } toString(): string { if (this.choice == Choice.OAD) { if (this.oad != null) { return "choice: {oad: " + this.oad + "}"; } else { return "choice is oad but oad is null"; } }else if(this.choice == Choice.ROAD){ if(this.road != null){ return "choice: {road: " + this.road + "}"; }else{ return "choice is road but road is null"; } } else { return "unknown"; } } }