/// <summary> /// Disposes the port. /// </summary> public void Dispose() { this.port.Dispose(); this.message = PowerLineModemMessage.Message.UserResetDetected; this.port = null; this.readData = 0; this.receivedAck.Dispose(); this.receivedAck = null; this.sending = null; this.sendingResponse = PowerLineModemMessage.MessageResponse.Unknown; this.state = ReadState.START; this.data = null; this.depth = 0; }
/// <summary> /// Creates the message and fills in the data. /// </summary> /// <param name="message"></param> /// <param name="data"></param> /// <returns></returns> public static PowerLineModemMessage GetReceivedMessage(PowerLineModemMessage.Message message, byte[] data) { switch (message) { case PowerLineModemMessage.Message.StandardMessageReceived: return(new StandardMessage(message, data)); case PowerLineModemMessage.Message.ExtendedMessageReceived: return(new ExtendedMessage(message, data)); case PowerLineModemMessage.Message.AllLinkRecordResponse: return(new AllLinkRecordResponse(message, data)); } return(null); }
/// <summary> /// Figures out how big this message needs to be. /// </summary> /// <param name="message"></param> /// <returns></returns> public static int SizeOfMessage(PowerLineModemMessage.Message message, PowerLineModemMessage sending) { switch (message) { case PowerLineModemMessage.Message.StandardMessageReceived: return(9); case PowerLineModemMessage.Message.ExtendedMessageReceived: return(23); case PowerLineModemMessage.Message.GetImConfiguration: return(GetConfiguration.ResponseSize); case PowerLineModemMessage.Message.GetImInfo: return(GetInfo.ResponseSize); case PowerLineModemMessage.Message.AllLinkRecordResponse: return(AllLinkRecordResponse.ResponseSize); case PowerLineModemMessage.Message.SendInsteonMessage: if (sending != null) { StandardMessage standard = (StandardMessage)sending; if (standard.Packet.Flags.ExtendedMessage) { return(ExtendedMessage.ResponseSize); } return(StandardMessage.ResponseSize); } return(0); default: return(BasicMessage.ResponseSize); } return(0); }
public ExtendedMessage(PowerLineModemMessage.Message message, byte[] data) : base(message, data) { }
private void ReadMessages(byte[] input, int size) { List <PowerLineModemMessage> receivedMessages = new List <PowerLineModemMessage>(); lock (port) { string strDebug = "Input: ["; // First we look for a 0x2, which they all start with. for (int i = 0; i < size; i++) { int data = input[i]; strDebug += String.Format("{0:x2}-", data, readData, (this.data == null ? 0 :this.data.Length), state.ToString()); switch (state) { case ReadState.START: if (data == START_OF_MESSAGE) { state = ReadState.MESSAGE_TYPE; } break; case ReadState.MESSAGE_TYPE: // This next one is the message type. message = (PowerLineModemMessage.Message)data; state = ReadState.MESSAGE_DATA; this.data = new byte[MessageFactory.SizeOfMessage(message, sending)]; readData = 0; break; case ReadState.MESSAGE_DATA: if (readData < this.data.Length) { this.data[readData++] = (byte)data; } if (readData >= this.data.Length) { // If this is a response, update the request with the response data. if (sending != null && message == sending.MessageType) { sendingResponse = sending.VerifyResponse(this.data); sending = null; receivedAck.Set(); } else { // Create the received message. PowerLineModemMessage packet = MessageFactory.GetReceivedMessage(message, this.data); if (packet != null) { receivedMessages.Add(packet); } } state = ReadState.START; } break; } } log.Debug(strDebug + "]"); } if (ReceivedMessage != null) { // Switch context so we don't end up deadlocked. ThreadPool.QueueUserWorkItem(delegate { foreach (PowerLineModemMessage mess in receivedMessages) { ReceivedMessage(this, new RecievedMessageEventArgs(mess)); } }); } }
public BasicMessage(PowerLineModemMessage.Message message) : base(message) { }