private void btnSendPkt_Click(object sender, EventArgs e) { if (_remote_data_endpoint == null) { MessageBox.Show("No EIB Client is connected. cannot sent packets.", "Error", MessageBoxButtons.OK); return; } if (this.tbValue.Text == null || this.tbValue.Text.Length == 0) { MessageBox.Show("No value was provided. you must enter the cEMI frame value to send", "Error", MessageBoxButtons.OK); return; } if (!hex_regex.IsMatch(this.tbValue.Text)) { MessageBox.Show("The cEMI frame value is not valid HEX format.", "Error", MessageBoxButtons.OK); return; } for (int i = 0; i < this.nudNumPkts.Value; ++i) { EIBAddress dst_addr; EIBAddress src_addr = new EIBAddress(this.tbSrcAddress.Text); if (this.checkBox1.Checked) { dst_addr = EIBAddress.GetRandomAddress(); } else { dst_addr = new EIBAddress(this.tbDestAddress.Text); } CemiFrame frame = new CemiFrame(src_addr, dst_addr, StringToByteArray(this.tbValue.Text), cbNeedAck.Checked); byte[] data = null; switch (_mode) { case DEVICE_MODE.MODE_TUNNELING: TunnelRequest req = new TunnelRequest(frame, _send_sequence); data = req.ToByteArray(); break; case DEVICE_MODE.MODE_ROUTING: RoutingIndication roi = new RoutingIndication(frame); data = roi.ToByteArray(); break; } _sock.SendTo(data, _remote_data_endpoint); LogSend(String.Format("Data packet. Dest addr: {0} Value 0x{1}", dst_addr.ToString(), this.tbValue.Text)); System.Threading.Thread.Sleep(10); } }
private bool SendReply(Stream s) { EIBHeader header = new EIBHeader(s); s.Position = 0; bool res = false; switch (header.mc) { case EIBMessages.EIB_MC_CONNECT_REQUEST: ConnectRequest creq = new ConnectRequest(s); _remote_data_endpoint = creq.data_endpoint.endpoint; LogReceive("Connect Request"); ConnectRepsonse cresp = new ConnectRepsonse(_local_endpoint); _sock.SendTo(cresp.ToByteArray(),creq.control_endpoint.endpoint); LogSend("Connect Response"); _connected = true; break; case EIBMessages.EIB_MC_CONNECTION_STATE_REQUEST: ConnectionStateRequest csreq = new ConnectionStateRequest(s); LogReceive("Connection State Request"); ConnectionStateResponse csresp = new ConnectionStateResponse(); _sock.SendTo(csresp.ToByteArray(), csreq.control_endpoint.endpoint); LogSend("Connection State Response"); break; case EIBMessages.EIB_MC_DISCONNECT_REQUEST: DisconnectRequest disreq = new DisconnectRequest(s); LogReceive("Disconnect Request"); DisconnectResponse disresp = new DisconnectResponse(); _sock.SendTo(disresp.ToByteArray(), disreq.control_endpoint.endpoint); LogSend("Dissconnect Response"); _restart_now = true; _connected = false; break; case EIBMessages.EIB_MC_SEARCH_REQUEST: LogReceive("Search Request"); SearchRequest sreq = new SearchRequest(s); SearchResponse sresp = new SearchResponse(_local_endpoint, _current_if.GetPhysicalAddress()); _sock.SendTo(sresp.ToByteArray(), sreq._discovery_endpoint.endpoint); LogSend("Search Response"); _connected = true; break; case EIBMessages.EIB_MC_TUNNEL_REQUEST: TunnelRequest req = new TunnelRequest(s); if (req._frame._apci == 0x0) { string log = String.Format("[Group Read Request] Dest: {0}", req._frame._dst.ToString()); LogReceive(log); } else { string log = String.Format("[Tunnel Request] Dest: {0}, Value: 0x{1}", req._frame._dst.ToString(), String.Format("{0:X}", req._frame._apci)); LogReceive(log); } if (req._frame.IsAckNeeded) { TunnelAck ack = new TunnelAck(_recv_sequence); _sock.SendTo(ack.ToByteArray(), _remote_data_endpoint); ++_recv_sequence; LogSend("Tunnel Ack"); } TunnelRequest tunnel_request = new TunnelRequest(req._frame, _send_sequence); tunnel_request._frame._mc = EIBMessages.L_DATA_CON; if (_cache != null) { lock (_sync) { foreach (var item in _cache.List) { if (tunnel_request._frame._dst.ToString() == item.Address && item.NumBytes > 1) { tunnel_request._frame._addil_data = new byte[item.NumBytes - 1]; tunnel_request._frame._apci_length = (byte)item.NumBytes; break; } } } } if (req._frame._apci == 0x0) { res = true; } LogSend(String.Format("Tunnel Data confirmation: {0}", tunnel_request._frame._dst.ToString())); _sock.SendTo(tunnel_request.ToByteArray(), _remote_data_endpoint); break; case EIBMessages.EIB_MC_TUNNEL_ACK: LogReceive("Ack"); ++_send_sequence; break; case EIBMessages.EIB_MC_ROUTING_INDICATION: LogReceive("routing indication"); break; default: LogReceive("Unknown message code!"); break; //throw new NotImplementedException(); } return res; }
void ListenerThread_DoWork(object sender, DoWorkEventArgs e) { EndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 0); TunnelRequest orig_req = null; byte[] buffer = new byte[256]; bool send_ind = false; int i = 0; while (!this.ListenerThread.CancellationPending) { if (!_sock.Poll(1000000, SelectMode.SelectRead)) { continue; } try { _sock.ReceiveFrom(buffer, ref endpoint); } catch (SocketException ex) { this.Log("Socket Exception", ex.Message); return; } MemoryStream stream = new MemoryStream(buffer); send_ind = SendReply(stream); if (send_ind || i > 0) { if (send_ind) { stream.Position = 0; orig_req = new TunnelRequest(stream); } i++; } if (_mode == DEVICE_MODE.MODE_TUNNELING && i == 2) { i = 0; TunnelRequest tunnel_request = new TunnelRequest(orig_req._frame, _send_sequence); tunnel_request._frame._mc = EIBMessages.MC_LDATA_IND; tunnel_request._frame._apci = 0x41; if (_cache != null) { lock (_sync) { foreach (var item in _cache.List) { if (tunnel_request._frame._dst.ToString() == item.Address && item.NumBytes > 1) { tunnel_request._frame._addil_data = new byte[item.NumBytes - 1]; tunnel_request._frame._apci_length = (byte)item.NumBytes; break; } } } } _sock.SendTo(tunnel_request.ToByteArray(), _remote_data_endpoint); LogSend(String.Format("Tunnel Data indication: {0} Value length: {1}", tunnel_request._frame._dst.ToString(), tunnel_request._frame._apci_length)); } if (!_connected && _mode != DEVICE_MODE.MODE_ROUTING) { break; } } }
private void btnSendPkt_Click(object sender, EventArgs e) { if (_remote_data_endpoint == null) { MessageBox.Show("No EIB Client is connected. cannot sent packets.", "Error", MessageBoxButtons.OK); return; } if (this.tbValue.Text == null || this.tbValue.Text.Length == 0) { MessageBox.Show("No value was provided. you must enter the cEMI frame value to send", "Error", MessageBoxButtons.OK); return; } if (!hex_regex.IsMatch(this.tbValue.Text)) { MessageBox.Show("The cEMI frame value is not valid HEX format.", "Error", MessageBoxButtons.OK); return; } for (int i = 0; i < this.nudNumPkts.Value; ++i) { EIBAddress dst_addr; EIBAddress src_addr = new EIBAddress(this.tbSrcAddress.Text); if (this.checkBox1.Checked) dst_addr = EIBAddress.GetRandomAddress(); else dst_addr = new EIBAddress(this.tbDestAddress.Text); CemiFrame frame = new CemiFrame(src_addr, dst_addr, StringToByteArray(this.tbValue.Text), cbNeedAck.Checked); byte[] data = null; switch (_mode) { case DEVICE_MODE.MODE_TUNNELING: TunnelRequest req = new TunnelRequest(frame, _send_sequence); data = req.ToByteArray(); break; case DEVICE_MODE.MODE_ROUTING: RoutingIndication roi = new RoutingIndication(frame); data = roi.ToByteArray(); break; } _sock.SendTo(data, _remote_data_endpoint); LogSend(String.Format("Data packet. Dest addr: {0} Value 0x{1}", dst_addr.ToString(), this.tbValue.Text)); System.Threading.Thread.Sleep(10); } }
private bool SendReply(Stream s) { EIBHeader header = new EIBHeader(s); s.Position = 0; bool res = false; switch (header.mc) { case EIBMessages.EIB_MC_CONNECT_REQUEST: ConnectRequest creq = new ConnectRequest(s); _remote_data_endpoint = creq.data_endpoint.endpoint; LogReceive("Connect Request"); ConnectRepsonse cresp = new ConnectRepsonse(_local_endpoint); _sock.SendTo(cresp.ToByteArray(), creq.control_endpoint.endpoint); LogSend("Connect Response"); _connected = true; break; case EIBMessages.EIB_MC_CONNECTION_STATE_REQUEST: ConnectionStateRequest csreq = new ConnectionStateRequest(s); LogReceive("Connection State Request"); ConnectionStateResponse csresp = new ConnectionStateResponse(); _sock.SendTo(csresp.ToByteArray(), csreq.control_endpoint.endpoint); LogSend("Connection State Response"); break; case EIBMessages.EIB_MC_DISCONNECT_REQUEST: DisconnectRequest disreq = new DisconnectRequest(s); LogReceive("Disconnect Request"); DisconnectResponse disresp = new DisconnectResponse(); _sock.SendTo(disresp.ToByteArray(), disreq.control_endpoint.endpoint); LogSend("Dissconnect Response"); _restart_now = true; _connected = false; break; case EIBMessages.EIB_MC_SEARCH_REQUEST: LogReceive("Search Request"); SearchRequest sreq = new SearchRequest(s); SearchResponse sresp = new SearchResponse(_local_endpoint, _current_if.GetPhysicalAddress()); _sock.SendTo(sresp.ToByteArray(), sreq._discovery_endpoint.endpoint); LogSend("Search Response"); _connected = true; break; case EIBMessages.EIB_MC_TUNNEL_REQUEST: TunnelRequest req = new TunnelRequest(s); if (req._frame._apci == 0x0) { string log = String.Format("[Group Read Request] Dest: {0}", req._frame._dst.ToString()); LogReceive(log); } else { string log = String.Format("[Tunnel Request] Dest: {0}, Value: 0x{1}", req._frame._dst.ToString(), String.Format("{0:X}", req._frame._apci)); LogReceive(log); } if (req._frame.IsAckNeeded) { TunnelAck ack = new TunnelAck(_recv_sequence); _sock.SendTo(ack.ToByteArray(), _remote_data_endpoint); ++_recv_sequence; LogSend("Tunnel Ack"); } TunnelRequest tunnel_request = new TunnelRequest(req._frame, _send_sequence); tunnel_request._frame._mc = EIBMessages.L_DATA_CON; if (_cache != null) { lock (_sync) { foreach (var item in _cache.List) { if (tunnel_request._frame._dst.ToString() == item.Address && item.NumBytes > 1) { tunnel_request._frame._addil_data = new byte[item.NumBytes - 1]; tunnel_request._frame._apci_length = (byte)item.NumBytes; break; } } } } if (req._frame._apci == 0x0) { res = true; } LogSend(String.Format("Tunnel Data confirmation: {0}", tunnel_request._frame._dst.ToString())); _sock.SendTo(tunnel_request.ToByteArray(), _remote_data_endpoint); break; case EIBMessages.EIB_MC_TUNNEL_ACK: LogReceive("Ack"); ++_send_sequence; break; case EIBMessages.EIB_MC_ROUTING_INDICATION: LogReceive("routing indication"); break; default: LogReceive("Unknown message code!"); break; //throw new NotImplementedException(); } return(res); }