// A frame is received void Serial_RawMessageRecieved(byte[] buffer, int offset, int lenght) { byte source_address = buffer[4]; byte frame_type = buffer[2]; // If filter not set, send it to Wireshark if (((int)frame_type > 7) || (FiltreMessages[(int)frame_type] == false)) { Wireshark.SendToWireshark(buffer, offset, lenght); } // Update HMI and counters BeginInvoke(new Action <byte, byte>(MessageReceivedIndication), new object[] { frame_type, source_address }); }
static Action <byte[], int, int> WriteUDPAndWir(Socket socket, WiresharkSender sender) { byte[] readArray = new byte[ushort.MaxValue]; int index = WriteDes(readArray); return((buffer, offset, count) => { buffer.AsSpan(offset, count).CopyTo(readArray.AsSpan(index)); sender.SendToWireshark(readArray, 0, index + count); socket.Send(readArray, index, count, SocketFlags.None); }); }
static Func <byte[], int, int, int> ReadUDPAndWir(Socket socket, WiresharkSender sender) { byte[] readArray = new byte[ushort.MaxValue]; int index = WriteSource(readArray); return((buffer, offset, count) => { int n = socket.Receive(readArray, index, readArray.Length - index, SocketFlags.None); sender.SendToWireshark(readArray, 0, n + index); readArray.AsSpan(index, n).CopyTo(buffer.AsSpan(offset, count)); return n; }); }
static void Main(string[] args) { // parse command line options string serialPortName = SerialPort.GetPortNames().FirstOrDefault(); string pipeName = "conbee"; byte zigBeeChannel = 15; string wiresharkExe = ""; var options = new OptionSet(); options.Add(new OptionSet.Category("USAGE: DeConBee2Wireshark [options]")); options.Add("p|port=", "Serial Port (e.g. 'COM3', default is first available serial port)", x => serialPortName = x); options.Add("i|pipe=", String.Format(@"Pipe Name (defaults to '{0}' => '\\.\pipe\{0}')", pipeName), x => pipeName = x); options.Add("c|channel=", String.Format("ZigBee Channel to Listen on (defaults to {0})", zigBeeChannel), (byte x) => zigBeeChannel = x); options.Add("w|wireshark=", "Path to Wireshark.exe", x => wiresharkExe = x); options.Add("v|verbose", "Show Verbose Output", x => Verbose = true); options.Add("h|?|help", x => ShowHelpAndExit(options)); options.Add(new OptionSet.Category("")); options.Add(new OptionSet.Category("IMPORTANT: You need a ConBee USB stick from dresden elektronik (www.dresden-elektronik.de) WITH BITCATCHER FIRMWARE for this tool to work!")); try { var unprocessed = options.Parse(args); if (unprocessed.Count > 0) { throw new ArgumentException(string.Format("Unrecognized command line arguments {0}.", String.Join(" ", unprocessed.Select(x => String.Format("'{0}'", x))) )); } } catch (Exception e) { ShowHelpAndExit(options, e.Message); } // connect to ConBee USB stick try { if (string.IsNullOrWhiteSpace(serialPortName)) { throw new ApplicationException("No serial port found or provided"); } ComPort = new SerialPort(serialPortName, 38400, Parity.None, 8, StopBits.One); ComPort.Open(); } catch (Exception ex) { throw new ApplicationException("Error opening serial port", ex); } // open pipe and wait for wireshark to connect TextOutput.WriteLine("DeConBee2Wireshark started (port '{0}', pipe '{1}', zigbee channel {2})", serialPortName, pipeName, zigBeeChannel); TextOutput.WriteLine(); var wiresharkSender = new WiresharkSender(pipeName, 0xc3); bool wiresharkIsChild = !string.IsNullOrWhiteSpace(wiresharkExe); if (wiresharkIsChild) { try { Process.Start(wiresharkExe, String.Format(@"-i\\.\pipe\{0} -k", pipeName)); } catch (Exception ex) { throw new ApplicationException("Error starting Wireshark", ex); } } else { TextOutput.WriteLine(@"Now start Wireshark with command line arguments -i\\.\pipe\{0} -k", pipeName); TextOutput.WriteLine(); } TextOutput.Write(@"Waiting for Wireshark to connect to pipe \\.\pipe\{0}: ", pipeName); while (!wiresharkSender.isConnected) { if (!wiresharkIsChild) { TextOutput.Write("."); } Thread.Sleep(500); } if (!wiresharkIsChild) { TextOutput.WriteLine(); } TextOutput.WriteLine("Wireshark connected."); TextOutput.WriteLine(); // discard any old frames ComPort.DiscardOutBuffer(); ComPort.DiscardInBuffer(); // select channel ConBeeSendFrame(new byte[] { 0x09, zigBeeChannel, 0x00 }); // start sniffing try { ConBeeSendFrame(new byte[] { 0x0b }); ComPort.ReadTimeout = 1000; var frameAck = ConBeeReadFrame(); if (frameAck[0] != 0x0c) { throw new InvalidDataException(); } } catch (Exception ex) { throw new ApplicationException(String.Format("None or invalid data received from COM port. Are you sure a dresden elektronik ConBee USB stick WITH BITCATCHER FIRMWARE is connected to port {0}?", serialPortName), ex); } // read frames and forward to wireshark ComPort.ReadTimeout = SerialPort.InfiniteTimeout; while (true) { var payload = ConBeeReadFrame(); if (payload[0] == 0x50 && payload.Length > 9) { var result = wiresharkSender.SendToWireshark(payload, 9, payload.Length - 9); if (!result) { TextOutput.WriteLine(); TextOutput.WriteLine("Pipe broken, Wireshark might have exited - exiting as well."); break; } } } }