// Simplest technic static byte SetReplyPackets(string PcapFile, int NumberofFrames, int NPDU_offset) { // Read into the Wireshark or tcpdump export file // retrieve the service call InvokeId if any // assume NPDU_offset is the same in all the consecutive response frames, normaly it does PcapDevice pcap = new CaptureFileReaderDevice(PcapFile); RawCapture raw; byte InvokeId = 0; debug_transport.MsgToSendBack = new byte[NumberofFrames][]; for (int i = 0; i < NumberofFrames; i++) { raw = pcap.GetNextPacket(); debug_transport.MsgToSendBack[i] = new byte[raw.Data.Length - NPDU_offset]; Array.Copy(raw.Data, NPDU_offset, debug_transport.MsgToSendBack[i], 0, raw.Data.Length - NPDU_offset); if (i == 0) { BacnetNpduControls npdu_function; BacnetAddress destination, source; byte hop_count; BacnetNetworkMessageTypes nmt; ushort vendor_id; // Find the InvokeId in the first frame // skip the NPDU int offset = NPDU.Decode(debug_transport.MsgToSendBack[0], 0, out npdu_function, out destination, out source, out hop_count, out nmt, out vendor_id); // if -1 we don't care InvokeId = (byte)APDU.GetDecodedInvokeId(debug_transport.MsgToSendBack[0], offset); } } return(InvokeId); // invokeId }
private T ReadPacket <T>(USBPipe pipe, UInt16 choice = 0, int maxDiscardedPackets = 0) { T result = default(T); lock (pipe) { byte[] rawHeaderLE = new byte[4]; usbInterface.InPipe.Read(rawHeaderLE); GCHandle handle = GCHandle.Alloc(rawHeaderLE, GCHandleType.Pinned); APDU apdu = (APDU)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(APDU)); handle.Free(); if (apdu.Length > 0) { byte[] rawBodyLE = new byte[apdu.Length]; usbInterface.InPipe.Read(rawBodyLE); handle = GCHandle.Alloc(rawBodyLE, GCHandleType.Pinned); result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); handle.Free(); } if ((choice != 0) && (apdu.Choice != choice) && (maxDiscardedPackets > 0)) { // this is not the packet we are looking for return(ReadPacket <T>(pipe, choice, --maxDiscardedPackets)); } } return(result); }
/// <summary> /// 保护定值选择 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DownloadProtectSetSelect_Click(object sender, RoutedEventArgs e) { try { var observale = (ObservableCollection <ProtectSetPoint>)protectSetPoint; if ((observale != null) && (observale.Count > 0)) { var qos = new QualifyCommandSet(ActionDescrible.Select); var protectsetAPDU = new APDU(appMessageManager.TransmitSequenceNumber, appMessageManager.RealReceiveSequenceNumber, TypeIdentification.C_SE_NC_1, true, (byte)observale.Count, CauseOfTransmissionList.Activation, appMessageManager.ASDUADdress, ProtectSetPoint.BasicAddress, qos); foreach (var m in observale) { if (m.InternalID <= observale.Count) { var sf = new ShortFloating((float)m.ParameterValue); protectsetAPDU.AddInformationObject(sf.GetDataArray(), (byte)sf.GetDataArray().Length, (byte)(m.InternalID - 1)); } else { throw new Exception("序号不在顺序范围之内,无法使用序列化方法,请检查InternalID是否连续"); } } //BeginInvokeUpdateHistory(fram.GetAPDUDataArray(), fram.FrameArray.Length, "测试"); SendTypeIMessage(TypeIdentification.P_ME_NC_1, protectsetAPDU); MakeLogMessage(sender, "定值选择" + protectsetAPDU.ToString(), LogType.ProtectSetpoint); } } catch (Exception ex) { MessageBox.Show(ex.Message, "DownloadProtectSetSelect_Click"); } }
/// <summary> /// 更新电能脉冲参数 /// </summary> private void UpdateElectricEnergy(APDU apdu) { try { var list = apdu.GetObjectListObject(); string str = ""; if (apdu.ASDU.IsSequence) { var addr = ElementTool.CombinationByte(apdu.ASDU.InformationObject[0], apdu.ASDU.InformationObject[1], apdu.ASDU.InformationObject[2]); foreach (var m in list) { var sf = new ShortFloating(m); var q = new QualityDescription(m[5]); str += "电能召唤:" + addr.ToString("X2") + " 品质描述:" + q.QDS.ToString("X2") + " 值:" + sf.ToString(); } } //txtTest.Text = ""; //txtTest.Text = str; } catch (Exception ex) { MessageBox.Show(ex.Message, " UpdateElectricEnergy"); } }
public static byte[] Request(byte[] derivation, byte[] messageChunk, bool firstBlock = true) { return(APDU.InputData(EthConstants.CLA, EthConstants.INS_SIGN_PERSONAL_MESSAGE, firstBlock ? EthConstants.P1_FIRST_BLOCK : EthConstants.P1_SUBSEQUENT_BLOCK, EthConstants.EMPTY, firstBlock ? derivation.Concat(messageChunk).ToArray() : messageChunk)); }
public static byte[] Request(byte[] derivation, bool display = false, bool useChainCode = false) { return(APDU.InputData(EthConstants.CLA, EthConstants.INS_GET_PUBLIC_ADDRESS, display ? EthConstants.P1_CONFIRM : EthConstants.P1_NON_CONFIRM, useChainCode ? EthConstants.P2_CHAINCODE : EthConstants.P2_NO_CHAINCODE, derivation)); }
public static byte[] Request(byte[] derivation, byte[] rlpTransactionChunk, bool firstBlock = true) { return(APDU.InputData(EthConstants.CLA, EthConstants.INS_SIGN_TRANSACTION, firstBlock ? EthConstants.P1_FIRST_BLOCK : EthConstants.P1_SUBSEQUENT_BLOCK, EthConstants.EMPTY, firstBlock ? derivation.Concat(rlpTransactionChunk).ToArray() : rlpTransactionChunk)); }
private void sendValueButton_Click(object sender, RoutedEventArgs e) { if (metf_tosend == null) { addValueButton_Click(null, null); } node.SendAPDU(metf_tosend); metf_tosend = null; }
public void parseEnvelope(string val) { TLV mainTLV = APDU.Parse(val).First <TLV>(); this.Tag = mainTLV.Tag; this.Length = mainTLV.Length; this.Value = mainTLV.Value; this.Description = "Envelope"; Contents = APDU.Parse(mainTLV.Value); }
private void button1_Click(object sender, EventArgs e) { treeViewColumns1.TreeView.Nodes.Clear(); TreeNode[] result = DataConverter.fromMultipleTLV(APDU.ParseMultipleTLV(SourceTextBox.Text)); if (result[0] != null) { treeViewColumns1.TreeView.Nodes.AddRange(result); } }
static void reply(APDU apdu, sbyte a0, sbyte a1, sbyte a2, sbyte a3) { var buffer = apdu.getBuffer(); buffer[0] = a0; buffer[1] = a1; buffer[2] = a2; buffer[3] = a3; apdu.setOutgoingAndSend(0, 4); }
/// <summary> /// 获取遥控 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> public void GetTelecotrolCommand(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); TelecontrolCommandArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取电能脉冲 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> private void GetElectricEnergy(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); ElectricEnergyArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); } catch (Exception ex) { throw ex; } }
public override void process(APDU apdu) { if (this.selectingApplet()) { return; } ProvideStorage(apdu); //ProvideFileSystem(apdu); //ProvideCrypto(apdu); //reply(apdu, 4, 3, 2, 1); }
/// <summary> /// 更新遥测数据 /// </summary> private void UpdateTelemetering(APDU apdu) { try { var list = apdu.GetInformationList(); var m = (ObservableCollection <Telemetering>)telemetering; switch ((TypeIdentification)apdu.ASDU.TypeId) { case TypeIdentification.M_ME_NA_1: //测量值,归一化值 { break; } case TypeIdentification.M_ME_NC_1: //测量值,短浮点数 { foreach (var ele in list) { for (int k = 0; k < m.Count; k++) { var t = m[k]; if ((t.InternalID + Telemetering.BasicAddress - 1) == ele.Item1) { t.TelemeteringValue = (float)ele.Item2; t.TelemeteringID = (int)(t.InternalID + Telemetering.BasicAddress - 1); } } } UpdateCalbrationData(list); break; } case TypeIdentification.M_ME_TD_1: //带CP56Time2a时标的测量值,归一化值 case TypeIdentification.M_ME_TF_1: //带CP56Time2a时标的测量值,短浮点数 { break; } default: { throw new ArgumentOutOfRangeException("不能识别既定的遥信ID"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "UpdateTelesignalisation"); } }
/// <summary> /// 主站发送TypeI-测量值参数/参数激活 /// </summary> /// <param name="id"></param> /// <param name="apdu"></param> private void SendTypeIMessage(TypeIdentification id, APDU apdu) { try { eventTypeIDManager.AddEventProcess(new EventProperty(id)); var array = apdu.GetAPDUDataArray(); MainTypeIProcess(array, array.Length, appMessageManager.WaitTime, id); appMessageManager.UpdateTransmitSequenceNumber(); } catch (Exception ex) { MessageBox.Show(ex.Message, "SendTypeIMessage主站遥控命令"); } }
private void ProvideStorage(APDU apdu) { // /send 802000000104 // /send 8030000004CA22FE33 // /send 8040000004 var buffer = apdu.getBuffer(); var CLA = buffer[ISO7816Constants.OFFSET_CLA]; var INS = buffer[ISO7816Constants.OFFSET_INS]; var P1 = buffer[ISO7816Constants.OFFSET_P1]; var P2 = buffer[ISO7816Constants.OFFSET_P2]; var LC = buffer[ISO7816Constants.OFFSET_LC]; unchecked { if (CLA == (sbyte)0x10) { #region Add File if (INS == (sbyte)0x1) { var n = new File { NextFile = FirstFile }; apdu.setIncomingAndReceive(); n.Name = new sbyte[LC]; Util.arrayCopyNonAtomic(buffer, (short)(ISO7816Constants.OFFSET_LC + 1), n.Name, 0, LC); this.FirstFile = n; reply(apdu, 6, 6, 6, Count); } #endregion #region Count if (INS == (sbyte)0x2) { reply(apdu, 6, 6, 6, Count); } #endregion //reply(apdu, 6, 6, 1, 1); } } }
public short GetAnswerFromOtherApplet(APDU apdu) { short counter = 0; var buffer = apdu.getBuffer(); var CLA = buffer[ISO7816Constants.OFFSET_CLA]; var INS = buffer[ISO7816Constants.OFFSET_INS]; var P1 = buffer[ISO7816Constants.OFFSET_P1]; var P2 = buffer[ISO7816Constants.OFFSET_P2]; var LC = buffer[ISO7816Constants.OFFSET_LC]; //buffer = ret; - need to use array copy!! var temp = (byte)0xB2; if (CLA == (sbyte)0x00 && INS == (sbyte)temp) { //last name if (P1 == (sbyte)0x01 && P2 == (sbyte)0x04) { sbyte[] ret = new sbyte[6]; ret[0] = (sbyte)0x6b; ret[1] = (sbyte)0x69; ret[2] = (sbyte)0x69; ret[3] = (sbyte)0x76; ret[4] = (sbyte)0x65; ret[5] = (sbyte)0x72; javacard.framework.Util.arrayCopy(ret, 0, buffer, 0, (short)ret.Length); counter = (short)ret.Length; } //first name else if (P1 == (sbyte)0x02 && P2 == (sbyte)0x04) { sbyte[] ret = new sbyte[5]; ret[0] = (sbyte)0x6a; ret[1] = (sbyte)0x6f; ret[2] = (sbyte)0x6e; ret[3] = (sbyte)0x61; ret[4] = (sbyte)0x73; javacard.framework.Util.arrayCopy(ret, 0, buffer, 0, (short)ret.Length); counter = (short)ret.Length; } } return(counter); }
/// <summary> /// 获取保护定值设置信息 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> private void GetProtectsetPointMessage(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); switch (id) { case TypeIdentification.C_SE_NC_1: // 保护定值 { ProtectSetMessageArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); break; } } } catch (Exception ex) { throw ex; } }
public override void process(APDU apdu) { if (this.selectingApplet()) { return; } //var b = apdu.getIncomingLength(); //apdu.setOutgoingAndSend(0, (short)buff.Length); //ProvideStorage(apdu); //ProvideFileSystem(apdu); //ProvideCrypto(apdu); //reply(apdu, 4, 3, 2, 1); response(apdu); }
/// <summary> /// 获取校准信息 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> private void GetCalibrationMessage(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); switch (id) { case TypeIdentification.P_AC_NA_1: // 参数激活 case TypeIdentification.P_ME_NC_1: // 测量值参数,短浮点数 { CalibrationMessageArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); break; } } } catch (Exception ex) { throw ex; } }
private void addValueButton_Click(object sender, RoutedEventArgs e) { var f = (M_ME_TF_1)node.FormatterManager.GetInstance(typeof(M_ME_TF_1)); if (metf_tosend == null) { metf_tosend = f.CreateAPDU(1); } try { var t = DateTime.Now; if (UseNowCheckbox.IsChecked != true && DateToSendPicker.SelectedDate.HasValue) { t = DateToSendPicker.SelectedDate.Value; t = new DateTime(t.Year, t.Month, t.Day, int.Parse(HourTextbox.Text), int.Parse(MinuteTextbox.Text), int.Parse(SecondTextbox.Text)); } f.PutData(metf_tosend, Convert.ToSingle(valTextbox.Text), t, Convert.ToUInt32(MsgAddrTextbox.Text)); } catch (Exception) { } }
private void WritePacket <T>(USBPipe pipe, UInt16 choice, T dataToSend) { List <byte> rawData = new List <byte>(); rawData.AddRange(ObjectToBinaryData <T>(dataToSend)); // APDU must be converted to little endian so we need some trickery here APDU apdu = new APDU() { Choice = choice, Length = (ushort)rawData.Count }; byte[] apduBE = ObjectToBinaryData <APDU>(apdu); rawData.InsertRange(0, new byte[] { apduBE[1], apduBE[0], apduBE[3], apduBE[2] }); lock (pipe) { usbInterface.OutPipe.Write(rawData.ToArray()); } }
/// <summary> /// 获取遥信信息 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> public void GetTelesignalisationMessage(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); switch (id) { //遥信信息 case TypeIdentification.M_SP_NA_1: //单点信息 case TypeIdentification.M_DP_NA_1: //双点信息 case TypeIdentification.M_SP_TB_1: //带CP56Time2a时标的单点信息 case TypeIdentification.M_DP_TB_1: //带CP56Time2a时标的双点信息 { TelesignalisationMessageArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); break; } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取遥测信息 /// </summary> /// <param name="id">类型ID</param> /// <param name="dataArray">原始字节数组</param> public void GetTelemeteringMessage(TypeIdentification id, byte[] dataArray) { try { var message = new APDU(dataArray); switch (id) { //遥信信息 case TypeIdentification.M_ME_NA_1: //测量值,归一化值 case TypeIdentification.M_ME_NC_1: //测量值,短浮点数 case TypeIdentification.M_ME_TD_1: //带CP56Time2a时标的测量值,归一化值 case TypeIdentification.M_ME_TF_1: //带CP56Time2a时标的测量值,短浮点数 { TelemeteringMessageArrived(this, new TransmitEventArgs <TypeIdentification, APDU>(id, message)); break; } } } catch (Exception ex) { throw ex; } }
static void Main(string[] args) { APDU myApdu = new APDU(); TPDU myTpdu = new TPDU(); List <byte> dnpPkt = new List <byte>(); //params should be in the following order //confirm, unsolicited, function, group, variation, prefixQualifier, [range] OR [start index, stop index] myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, 0x02, 0x0A, 0x02, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01); myTpdu.buildTPDU(ref dnpPkt); DPDU myDpdu = new DPDU(); //List<byte> dpdu = new List<byte>(); /*dpdu.Add(0x05); * dpdu.Add(0x64); * dpdu.Add(0x05); * dpdu.Add(0xF2); * dpdu.Add(0x01); * dpdu.Add(0x00); * dpdu.Add(0xEF); * dpdu.Add(0xFF);*/ myDpdu.buildDPDU(ref dnpPkt, 0xC4, 1, 65519); //dst=1, src=65519 foreach (byte element in dnpPkt) { Console.Write(element.ToString("X2") + " "); } Console.ReadLine(); }
private void ProvideFileSystem(APDU apdu) { // /send 802000000104 // /send 8030000004CA22FE33 // /send 8040000004 var buffer = apdu.getBuffer(); var CLA = buffer[ISO7816Constants.OFFSET_CLA]; var INS = buffer[ISO7816Constants.OFFSET_INS]; var P1 = buffer[ISO7816Constants.OFFSET_P1]; var P2 = buffer[ISO7816Constants.OFFSET_P2]; var LC = buffer[ISO7816Constants.OFFSET_LC]; unchecked { if (CLA == (sbyte)0x80) { if (INS == 0) { buffer[0] = (sbyte)0xDE; buffer[1] = (sbyte)0xAD; buffer[2] = (sbyte)0xBE; buffer[3] = (sbyte)0xEF; apdu.setOutgoingAndSend(0, 4); } else if (INS == (sbyte)0x20) { /* * CREATE FILE: creates the file with the size given in the data field * APDU: CLA 0x80, INS: 0x20, Lc=0x01, Data=size of the file (max 0xFF=255 bytes) */ //apdu.setIncomingAndReceive(); var Size = (short)P1; this.FileBytes = new sbyte[Size]; } else if (INS == (sbyte)0x30) { /* * WRITE FILE: writes the data bytes from the APDU to the file * APDU: CLA 0x80, INS: 0x30, Lc=0xXX (number of bytes to write), Data=bytes to write to the file */ apdu.setIncomingAndReceive(); var length = (short)LC; if (length > this.FileBytes.Length) { length = (short)this.FileBytes.Length; } Util.arrayCopyNonAtomic(buffer, (short)(ISO7816Constants.OFFSET_LC + 2), this.FileBytes, 0, length); } else if (INS == (byte)0x40) { /* * * READ FILE: sends some the data bytes from the file to the terminal * APDU: CLA 0x80, INS: 0x40, Le=0xXX (number of bytes to read) */ Util.arrayCopyNonAtomic(this.FileBytes, (short)0, buffer, (short)0, P1); apdu.setOutgoingAndSend((short)0, (short)P1); } } } }
/// <summary> /// 更新遥控状态,更新相应的按钮状态。 /// Todo:地址更新需要同步,这限制了灵活性 /// </summary> /// <param name="apdu">APDU帧</param> private void UpdateTelecontrolState(APDU apdu) { try { UInt32 objectAddr = ElementTool.CombinationByte(apdu.ASDU.InformationObject[0], apdu.ASDU.InformationObject[1], apdu.ASDU.InformationObject[2]); var dc = new DoubleCommand(apdu.ASDU.InformationObject[3]); switch (objectAddr) { //开关控制 case Telecontrol.BasicAddress: { if ((dc.DCS == DCOState.On) && (dc.SE == SelectExecuteOption.Select)) { //合闸 选择 controlReayActionState.ActionCloseEnabled = true; controlReayActionState.ReadyCloseEnabled = false; MakeLogMessage(apdu, "", "合闸预制-确认", LogType.Telecontrol); } else if ((dc.DCS == DCOState.On) && (dc.SE == SelectExecuteOption.Execute)) { //合闸 执行 controlReayActionState.ActionCloseEnabled = false; controlReayActionState.ReadyCloseEnabled = true; MakeLogMessage(apdu, "", "合闸执行-确认", LogType.Telecontrol); } if ((dc.DCS == DCOState.Off) && (dc.SE == SelectExecuteOption.Select)) { //分闸 选择 controlReayActionState.ActionOpenEnabled = true; controlReayActionState.ReadyOpenEnabled = false; MakeLogMessage(apdu, "", "分闸预制-确认", LogType.Telecontrol); } else if ((dc.DCS == DCOState.Off) && (dc.SE == SelectExecuteOption.Execute)) { //分闸 执行 controlReayActionState.ActionOpenEnabled = false; controlReayActionState.ReadyOpenEnabled = true; MakeLogMessage(apdu, "", "分闸执行-确认", LogType.Telecontrol); } break; } //电池活化 case (Telecontrol.BasicAddress + 1): { break; } //复归 case (Telecontrol.BasicAddress + 2): { break; } default: { break; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "更新遥控状态"); } }
public void InitTest() { APDU = new APDU(); expected = new List <TLV>(); }
public void MyTestMethod() { List <TLV> result = APDU.ParseMultipleTLV("A00233440203112233A0 05 01 03 A00133"); }