private void ZWave_NodeUpdated(object sender, NodeEvent eventData) { ZWaveNode node = (ZWaveNode)sender; if (eventData.Parameter == EventParameter.SecurityDecriptedMessage && eventData.Value is byte[]) { node.ApplicationCommandHandler((byte[])eventData.Value); return; } else if (eventData.Parameter == EventParameter.SecurityNodeInformationFrame) { node.SecuredNodeInformationFrame = (byte[])eventData.Value; // we take them one a a time to make sure we keep the list with unique elements foreach (byte nodeInfo in node.SecuredNodeInformationFrame) { // if we found the COMMAND_CLASS_MARK we get out of the for loop if (nodeInfo == (byte)0xEF) { break; } node.NodeInformationFrame = Utility.AppendByteToArray(node.NodeInformationFrame, nodeInfo); } // we just send other events and save the node data NodeInformationFrameDone(node); } // Route node event OnNodeUpdated(new NodeUpdatedEventArgs(eventData.Node.Id, eventData)); }
private void SetupNetworkKey(ZWaveNode node) { byte[] initialNetworkKey = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] EncryptPassword = new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; byte[] AuthPassword = new byte[] { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }; byte[] NetworkKey; if (adding_node && !networkKeySet) { NetworkKey = initialNetworkKey; Utility.logMessage("In SetupNetworkKey - in node inclusion mode."); } else { NetworkKey = PrivateNetworkKey; } encryptKey = enc.ECB_EncryptMessage(NetworkKey, EncryptPassword); authKey = enc.ECB_EncryptMessage(NetworkKey, AuthPassword); // Utility.logMessage("In SetupNetworkKey - Network key - " + Utility.ByteArrayToString(NetworkKey)); // Utility.logMessage("In SetupNetworkKey - Encrypt key - " + Utility.ByteArrayToString(encryptKey)); // Utility.logMessage("In SetupNetworkKey - Authent key - " + Utility.ByteArrayToString(authKey)); }
private void gotNodeUpdateInformation(ZWaveNode znode) { // once we get the security command classes we'll issue the same events and call SaveNodesConfig(); RaiseUpdateParameterEvent(new ZWaveEvent(znode, EventParameter.NodeInfo, Utility.ByteArrayToString(znode.NodeInformationFrame), 0)); RaiseUpdateParameterEvent(new ZWaveEvent(znode, EventParameter.WakeUpNotify, "1", 0)); SaveNodesConfig(); }
public ZWaveEvent(ZWaveNode node, EventParameter eventType, object eventValue, int instance) { this.Node = node; this.Parameter = eventType; this.Value = eventValue; this.Instance = instance; }
private void CreateDevices(byte[] receivedMessage) { var nodeList = ExtractNodesFromBitMask(receivedMessage); foreach (byte i in nodeList) { var node = devices.Find(n => n.Id == i); if (node == null) { //Console.WriteLine("Z-Wave Adding node " + i + " Class[ Basic=" + receivedMessage[7].ToString("X2") + " Generic=" + ((GenericType)receivedMessage[8]).ToString() + " Specific=" + receivedMessage[9].ToString("X2") + " ]"); devices.Add(CreateDevice(i, 0x00)); } else { OnControllerEvent(new ControllerEventArgs(i, ControllerStatus.NodeUpdated)); } } while (nodeList.Count > 0) { ZWaveNode nextNode = devices.Find(zn => zn.BasicClass == 0x00); if (nextNode != null) { GetNodeCapabilities(nextNode.Id); } else { OnControllerEvent(new ControllerEventArgs(0x00, ControllerStatus.DiscoveryEnd)); break; } } }
private void CreateDevices(byte[] receivedMessage) { var nodeList = ExtractNodesFromBitMask(receivedMessage); foreach (byte i in nodeList) { var node = devices.Find(n => n.Id == i); if (node == null) { devices.Add(CreateDevice(i, 0x00)); } else { OnControllerEvent(new ControllerEventArgs(i, ControllerStatus.NodeUpdated)); } } while (nodeList.Count > 0) { ZWaveNode nextNode = devices.Find(zn => zn.BasicClass == 0x00); if (nextNode != null) { GetNodeCapabilities(nextNode.Id); } else { OnControllerEvent(new ControllerEventArgs(0x00, ControllerStatus.DiscoveryEnd)); break; } } }
public NodeEvent(ZWaveNode node, EventParameter eventType, object eventValue, int instance) { Node = node; Parameter = eventType; Value = eventValue; Instance = instance; Timestamp = DateTime.UtcNow; }
private void NodeInformationFrameDone(ZWaveNode znode) { znode.UpdateCommandClassList(); // once we get the security command classes we'll issue the same events and call SaveNodesConfig(); OnNodeUpdated(new NodeUpdatedEventArgs(znode.Id, new NodeEvent(znode, EventParameter.NodeInfo, BitConverter.ToString(znode.NodeInformationFrame).Replace("-", " "), 0))); // TODO: deprecate the WakeUpNotify event? OnNodeUpdated(new NodeUpdatedEventArgs(znode.Id, new NodeEvent(znode, EventParameter.WakeUpNotify, "1", 0))); }
private void init(ZWaveNode node) { //ZWaveNode node = null; if (enc == null) { enc = new AES_work(); } SetupNetworkKey(node); }
public void sendSupportedGet(ZWaveNode node) { var message = ZWaveMessage.CreateRequest(node.Id, new byte[] { (byte)CommandClass.Security, (byte)SecurityCommand.SupportedGet }); encryptAndSend(node, message); }
public void getScheme(ZWaveNode node) { if (adding_node) { schemeRequestSent = true; node.SendRequest(new byte[] { (byte)CommandClass.Security, (byte)SecurityCommand.SchemeGet, 0 }); } }
public void RequestNeighborsUpdate(ZWaveNode node) { node.SendMessage(new byte[] { (byte)MessageHeader.SOF, 0x05, /* packet length */ (byte)MessageType.Request, /* Type of message */ (byte)Function.RequestNodeNeighborsUpdate, node.Id, 0x00, 0x00 }); }
public void SendNonceReport(ZWaveNode node) { byte[] message = new byte[10]; message[0] = (byte)CommandClass.Security; message[1] = (byte)SecurityCommand.NonceReport; Array.Copy(c_currentNonce, 0, message, 2, 8); node.SendRequest(message); c_nonceTimer.Reset(); }
internal void RaiseUpdateParameterEvent(ZWaveNode node, int pid, EventParameter peventtype, object value) { if (UpdateNodeParameter != null) { UpdateNodeParameter(node, new UpdateNodeParameterEventArgs() { NodeId = (int)node.NodeId, ParameterId = pid, ParameterName = peventtype, Value = value }); } }
public void GetNeighborsRoutingInfo(ZWaveNode node) { node.SendMessage(new byte[] { (byte)MessageHeader.SOF, 0x07, /* packet length */ (byte)MessageType.Request, /* Type of message */ (byte)Function.GetRoutingInfo, node.Id, 0x00, 0x00, 0x03, 0x00 }); }
public void ProcessNonceReport(ZWaveNode node, byte[] message, int start) { d_nonceTimer.Restart(); ProcessNonceReport(message, start); EncryptMessage(node, message); waitingForNonce = false; // if we still have items in the queue request a new nonce if (secure_payload.Count > 0) { sendRequestNonce(node); } }
private void QueuePayload(ZWaveNode node, SecutiryPayload payload) { lock (secure_payload) { secure_payload.Add(payload); if (d_nonceTimer.ElapsedMilliseconds > 10000) { waitingForNonce = false; } if (!waitingForNonce) { sendRequestNonce(node); } } }
private void sendMsg(ZWaveNode node, byte[] message) { Utility.logMessage("In sendMsg - SecurityHandler"); if (message.Length < 7) { Utility.logMessage("Message too short"); } if (message[3] != 0x13) { Utility.logMessage("Invalid Message type"); } int length = message[5]; if (length > 28) { SecutiryPayload t_payload = new SecutiryPayload(); t_payload.length = 28; t_payload.part = 1; byte[] t_message = new byte[t_payload.length]; System.Array.Copy(message, 6, t_message, 0, t_payload.length); t_payload.message = t_message; QueuePayload(node, t_payload); SecutiryPayload t_payload2 = new SecutiryPayload(); t_payload2.length = length - 28; t_payload2.part = 2; byte[] t_message2 = new byte[t_payload.length]; System.Array.Copy(message, 34, t_message2, 0, t_payload2.length); t_payload2.message = t_message2; QueuePayload(node, t_payload2); } else { SecutiryPayload t_payload = new SecutiryPayload(); t_payload.length = length; t_payload.part = 0; byte[] t_message = new byte[t_payload.length]; System.Array.Copy(message, 6, t_message, 0, t_payload.length); t_payload.message = t_message; QueuePayload(node, t_payload); } }
private bool sendRequestNonce(ZWaveNode node) { Utility.logMessage("In sendRequestNonce - SecurityHandler"); if (waitingForNonce) { return(false); } Utility.logMessage("In sendRequestNonce - not waiting for Nonce - SecurityHandler"); waitingForNonce = true; node.SendRequest(new byte[] { (byte)CommandClass.Security, (byte)SecurityCommand.NonceGet }); return(true); }
private ZWaveNode AddNode(byte nodeId, byte genericClass) { ZWaveNode node; switch (genericClass) { case (byte)GenericType.StaticController: // TODO: what should be done here?... node = null; break; default: // generic node node = new ZWaveNode(this, nodeId, genericClass); break; } node.NodeUpdated += ZWave_NodeUpdated; UpdateOperationProgress(nodeId, NodeQueryStatus.NodeAdded); return(node); }
private ZWaveNode CreateDevice(byte nodeId, byte genericClass) { ZWaveNode node; switch (genericClass) { case (byte)GenericType.StaticController: // TODO: this is very untested... node = (ZWaveNode) new Controller(zwavePort); break; default: // generic node node = new ZWaveNode(nodeId, zwavePort, genericClass); break; } node.ParameterChanged += znode_ParameterChanged; node.ManufacturerSpecificResponse += znode_ManufacturerSpecificResponse; // OnControllerEvent(new ControllerEventArgs(nodeId, ControllerStatus.NodeAdded)); // return(node); }
private void znode_ParameterChanged(object sender, ZWaveEvent eventData) { if (sender is ZWaveNode) { ZWaveNode node = (ZWaveNode)sender; if (eventData.Parameter == EventParameter.SecurityDecriptedMessage && eventData.Value is byte[]) { node.MessageRequestHandler((byte[])eventData.Value); return; } else if (eventData.Parameter == EventParameter.SecurityGeneratedKey && eventData.Value is int) { SaveNodesConfig(); return; } else if (eventData.Parameter == EventParameter.SecurityNodeInformationFrame) { node.SecuredNodeInformationFrame = (byte[])eventData.Value; // we take them one a a time to make sure we keep the list with unique elements foreach (byte nodeInfo in node.SecuredNodeInformationFrame) { // if we found the COMMAND_CLASS_MARK we get out of the for loop if (nodeInfo == (byte)0xEF) { break; } node.NodeInformationFrame = Utility.AppendByteToArray(node.NodeInformationFrame, nodeInfo); } // we just send other events and save the node data gotNodeUpdateInformation(node); } } // Route node event RaiseUpdateParameterEvent(eventData); }
private void SetNodeLevel(ZWaveNode node, int level) { node.UpdateData("Level", level); if (level > 0) node.UpdateData("LastLevel", level); }
private int GetNodeLevel(ZWaveNode node) { return (int)node.GetData("Level", 0).Value; }
public void GetNodeCcsVersion(ZWaveNode zn) { // If node support version command class, query each one for its version. if (zn.SupportCommandClass(CommandClass.Version)) { foreach (var cmdClass in zn.CommandClasses) { if (controllerStatus != ControllerStatus.Ready || !serialPort.IsConnected) break; // if not cached query the node. if (cmdClass.CommandClass != CommandClass.NotSet && cmdClass.Version == -1) ZWaveLib.CommandClasses.Version.Get(zn, cmdClass.CommandClass).Wait(); } } }
// use with care public bool encryptAndSend(ZWaveNode node, byte[] message) { Utility.logMessage("In encryptAndSend - SecurityHandler - " + Utility.ByteArrayToString(message)); sendMsg(node, message); return(true); }
public bool DecryptMessage(ZWaveNode node, byte[] message, int start) { if (enc == null) { init(node); } Utility.logMessage("In DecryptMessage - SecurityHandler"); if (c_nonceTimer.ElapsedMilliseconds > 10000) { Utility.logMessage("Received the nonce too late'" + c_nonceTimer.ElapsedMilliseconds + "' > 10000"); return(false); } // Utility.logMessage("Message to be decrypted: " + Utility.ByteArrayToString(message)); byte[] iv = getVIFromPacket_inbound(message, start + 1); int _length = message.Length; int encryptedpackagesize = _length - 11 - 8; //19 + 11 + 8 byte[] encryptedpacket = new byte[encryptedpackagesize]; Array.Copy(message, 8 + start + 1, encryptedpacket, 0, encryptedpackagesize); byte[] decryptedpacket = enc.OFB_EncryptMessage(encryptKey, iv, encryptedpacket); // Utility.logMessage("Message " + Utility.ByteArrayToString(message)); // Utility.logMessage("IV " + Utility.ByteArrayToString(iv)); // Utility.logMessage("Encrypted Packet " + Utility.ByteArrayToString(encryptedpacket)); Utility.logMessage("Decrypted Packet " + Utility.ByteArrayToString(decryptedpacket)); byte[] mac = GenerateAuthentication(message, start, _length, node.Id, 0x01, iv, enc); byte[] e_mac = new byte[8]; Array.Copy(message, start + 8 + encryptedpackagesize + 2, e_mac, 0, 8); if (!Enumerable.SequenceEqual(mac, e_mac)) { Utility.logMessage("Computed mac " + Utility.ByteArrayToString(mac) + " does not match the provider mac " + Utility.ByteArrayToString(e_mac) + ". Dropping."); if (secure_payload.Count > 1) { sendRequestNonce(node); } return(false); } if (decryptedpacket[1] == (byte)CommandClass.Security && 1 == 0) { byte[] msg = new byte[decryptedpacket.Length - 1]; Array.Copy(decryptedpacket, 1, msg, 0, msg.Length); Utility.logMessage("Processing Internally: " + Utility.ByteArrayToString(msg)); } else { byte[] msg = new byte[decryptedpacket.Length - 2 + 8]; Array.Clear(msg, 0, 7); Array.Copy(decryptedpacket, 1, msg, 7, msg.Length - 7); msg[6] = (byte)(msg.Length - 7); Utility.logMessage("Forwarding: " + Utility.ByteArrayToString(msg)); /* send to the Command Class for Proecssing */ Utility.logMessage("Received External Command Class: " + Utility.ByteArrayToString(new byte[] { decryptedpacket[1] })); node.MessageRequestHandler(node.pController, msg); } Utility.logMessage("In DecryptMessage - Finished"); return(true); }
public ZWaveEvent(ZWaveNode node, EventParameter eventType, object eventValue, int instance) { this.Node = node; this.Event = eventType; this.Value = eventValue; this.Instance = instance; }
private ZWaveNode CreateNode(byte nodeId, byte genericType) { ZWaveNode node; switch (genericType) { case (byte) GenericType.StaticController: // TODO: what should be done here?... node = null; break; default: // generic node node = new ZWaveNode(this, nodeId, genericType); break; } node.NodeUpdated += ZWave_NodeUpdated; UpdateOperationProgress(nodeId, NodeQueryStatus.NodeAdded); return node; }
private ZWaveNode CreateDevice(byte nodeId, byte genericClass) { ZWaveNode node; switch (genericClass) { case (byte) GenericType.StaticController: // TODO: this is very untested... node = (ZWaveNode) new Controller(zwavePort); break; default: // generic node node = new ZWaveNode(nodeId, zwavePort, genericClass); break; } node.ParameterChanged += znode_ParameterChanged; node.ManufacturerSpecificResponse += znode_ManufacturerSpecificResponse; // OnControllerEvent(new ControllerEventArgs(nodeId, ControllerStatus.NodeAdded)); // return node; }
public void GetNodeCcsVersion(ZWaveNode zn) { // If node support version command class, query each one for its version. if (zn.SupportCommandClass(CommandClass.Version)) { foreach (var cmdClass in zn.CommandClasses) { // if not cached query the node. if (cmdClass.Version == 0) ZWaveLib.CommandClasses.Version.Get(zn, cmdClass.CommandClass).Wait(); } } }
private void SetNodeLevel(ZWaveNode node, int level) { if (!node.Data.ContainsKey("Level")) { node.Data.Add("Level", level); } else { node.Data["Level"] = level; } }
private int GetNodeLevel(ZWaveNode node) { int level = 0; if (node.Data.ContainsKey("Level")) { level = (int)node.Data["Level"]; } return level; }
// IN the mesage to be Encrypted // OUT - true - message processed and sent - proceed to next one // - false - we need to wait for the nonce report to come private bool EncryptMessage(ZWaveNode node, byte[] message) { if (enc == null) { init(node); } Utility.logMessage("In EncryptMessage - secure_payload [" + secure_payload.Count + "] - " + d_nonceTimer.ElapsedMilliseconds); // if we get true we need to wait for the new Nonce // if we get false we need to proceed // if (sendRequestNonce()) // return false; if (d_nonceTimer.ElapsedMilliseconds > 10000) { return(false); } SecutiryPayload payload = null; lock (secure_payload) { if (secure_payload.Count > 0) { payload = secure_payload.First(); secure_payload.Remove(payload); } } if (payload != null) { int len = payload.length + 20; byte[] t_message = new byte[len]; int i = 0; t_message[i] = (byte)CommandClass.Security; i++; t_message[i] = (byte)SecurityCommand.MessageEncap; byte[] initializationVector = new byte[16]; for (int a = 0; a < 8; a++) { initializationVector[a] = (byte)0xAA; i++; t_message[i] = initializationVector[a]; } Array.Copy(d_currentNonce, 0, initializationVector, 8, 8); int sequence = 0; if (payload.part == 1) { ++m_sequenceCounter; sequence = m_sequenceCounter & (byte)0x0f; sequence |= (byte)0x10; } else if (payload.part == 2) { ++m_sequenceCounter; sequence = m_sequenceCounter & (byte)0x0f; sequence |= (byte)0x30; } byte[] plaintextmsg = new byte[payload.length + 1]; plaintextmsg[0] = (byte)sequence; for (int a = 0; a < payload.length; a++) { plaintextmsg[a + 1] = payload.message[a]; } byte[] encryptedPayload = new byte[30]; encryptedPayload = enc.OFB_EncryptMessage(encryptKey, initializationVector, plaintextmsg); // Utility.logMessage("authKey " + Utility.ByteArrayToString(authKey)); // Utility.logMessage("EncryptKey " + Utility.ByteArrayToString(encryptKey)); Utility.logMessage("Input Packet: " + Utility.ByteArrayToString(plaintextmsg)); // Utility.logMessage("IV " + Utility.ByteArrayToString(initializationVector)); // Utility.logMessage("encryptedPayload " + Utility.ByteArrayToString(encryptedPayload)); for (int a = 0; a < payload.length + 1; ++a) { i++; t_message[i] = encryptedPayload[a]; } i++; t_message[i] = d_currentNonce[0]; //GenerateAuthentication int start = 1; byte[] mac = GenerateAuthentication(t_message, start, t_message.Length + 2 - start - 1, 0x01, node.Id, initializationVector, enc); for (int a = 0; a < 8; ++a) { i++; t_message[i] = mac[a]; } node.SendRequest(t_message); Utility.logMessage("In EncryptMessage - message sent"); if ((networkKeySet == false) && payload.message[0] == 0x98 && payload.message[1] == 0x06) { networkKeySet = true; adding_node = false; SetupNetworkKey(node); } return(true); } return(true); }
private void NodeInformationFrameDone(ZWaveNode znode) { znode.UpdateCommandClassList(); // once we get the security command classes we'll issue the same events and call SaveNodesConfig(); OnNodeUpdated(new NodeUpdatedEventArgs(znode.Id, new NodeEvent(znode, EventParameter.NodeInfo, BitConverter.ToString(znode.NodeInformationFrame).Replace("-", " "), 0))); }