public void TestReadTempemperatureAndHumidityCommandParse() { _message = new FlTxtMessageCommand() { MessageId = FlMessageId.ReadTempAndHum, Arguments = new List <object>() { "1", // DeviceID "3" // Sensor number } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseCommand(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.ReadTempAndHum, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(2, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); Assert.AreEqual("3", (string)_parsedMessage.Arguments[1]); }
public void TestReadTempemperatureAndHumidityResponseParse() { _message = new FlTxtMessageResponse() { MessageId = FlMessageId.ReadTempAndHum, Arguments = new List <object>() { 1, // DeviceID, 0, // Error 3, // Sensor number $"{23.4:0.00}", // Temperature value $"{56.7:0.00}" // Humidity value } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseResponseEvent(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.ReadTempAndHum, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(5, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); Assert.AreEqual("0", (string)_parsedMessage.Arguments[1]); Assert.AreEqual("3", (string)_parsedMessage.Arguments[2]); Assert.AreEqual("23.40", (string)_parsedMessage.Arguments[3]); Assert.AreEqual("56.70", (string)_parsedMessage.Arguments[4]); }
public void TestReadHumidityOkResponseMessageBuild() { _binMsg = new FlBinMessageResponse() { MessageId = FlMessageId.ReadHumidity, Arguments = new List <object>() { (byte)2, // Sensor number (double)23.4 // Humidity } }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 6; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Host; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)6, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.ReadHumidity, _parsedBinMsg.MessageId); Assert.AreEqual(2, _parsedBinMsg.Arguments.Count); Assert.AreEqual(2, (byte)_parsedBinMsg.Arguments[0]); Assert.AreEqual(23.4, (double)_parsedBinMsg.Arguments[1]); }
public void TestWriteGpioCommandParse() { _message = new FlTxtMessageCommand() { MessageId = FlMessageId.WriteGpio, Arguments = new List <object>() { "1", // DeviceID "3", // GPIO number "1" // GPIO value } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseCommand(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.WriteGpio, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(3, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); Assert.AreEqual("3", (string)_parsedMessage.Arguments[1]); Assert.AreEqual("1", (string)_parsedMessage.Arguments[2]); }
public void TestReadFirmwareVersionCommandParse() { _message = new FlTxtMessageCommand() { MessageId = FlMessageId.ReadFirmwareVersion, Arguments = new List <object>() { "1" // DeviceID } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseCommand(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.ReadFirmwareVersion, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(1, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); }
public void TestResetResponseParse() { _message = new FlTxtMessageResponse() { MessageId = FlMessageId.Reset, Arguments = new List <object>() { 1, // DeviceID, 0 // Error } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseResponseEvent(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.Reset, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(2, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); Assert.AreEqual("0", (string)_parsedMessage.Arguments[1]); }
public void TestReadHardwareVersionResponseParse() { _binMsg = new FlBinMessageResponse() { MessageId = FlMessageId.ReadHardwareVersion, Arguments = new List <object>() { "a.1.2.3" // Hardware version } }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 1; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Host; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)1, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.ReadHardwareVersion, _parsedBinMsg.MessageId); Assert.AreEqual(1, _parsedBinMsg.Arguments.Count); Assert.AreEqual("a.1.2.3", (string)_parsedBinMsg.Arguments[0]); }
public void TestReadFirmwareVersionCommandParse() { _binMsg = new FlBinMessageCommand() { MessageId = FlMessageId.ReadFirmwareVersion }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 2; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Device; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)2, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.ReadFirmwareVersion, _parsedBinMsg.MessageId); }
public unsafe void TestResetOkResponseMessageBuild() { _binMsg = new FlBinMessageResponse() { MessageId = FlMessageId.Reset, }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 2; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Host; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)2, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.Reset, _parsedBinMsg.MessageId); Assert.AreEqual((byte)0, _parsedBinMsg.Header.flag2.error); }
public void TestBootModeCommandMessageBuild() { _binMsg = new FlBinMessageCommand() { MessageId = FlMessageId.BootMode, Arguments = new List <object>() { (byte)1 // Boot mode(Bootloader) } }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 1; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Device; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)1, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.BootMode, _parsedBinMsg.MessageId); Assert.AreEqual(1, _parsedBinMsg.Arguments.Count); Assert.AreEqual(1, (byte)_parsedBinMsg.Arguments[0]); }
public void TestReadTemperatureHumidityCommandMessageBuild() { _binMsg = new FlBinMessageCommand() { MessageId = FlMessageId.ReadTempAndHum, Arguments = new List <object>() { (byte)3 // Sensor number } }; _binMsg.Header.device_id = 1; _binMsg.Header.flag1.sequence_num = 7; _message = _binMsg; FlBinPacketBuilder.BuildMessagePacket(ref _message); _parser.Role = FlParserRole.Device; for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.Parse(_message.Buffer[_i], out _parsedMessage); } _parsedBinMsg = (IFlBinMessage)_parsedMessage; Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Binary, _parsedBinMsg.MessageType); Assert.AreEqual(FlMessageCategory.Command, _parsedBinMsg.MessageCategory); Assert.AreEqual((uint)1, _parsedBinMsg.Header.device_id); Assert.AreEqual((byte)7, _parsedBinMsg.Header.flag1.sequence_num); Assert.AreEqual(FlMessageId.ReadTempAndHum, _parsedBinMsg.MessageId); Assert.AreEqual(1, _parsedBinMsg.Arguments.Count); Assert.AreEqual(3, (byte)_parsedBinMsg.Arguments[0]); }
public void TestReadHardwareVersionResponseParse() { _message = new FlTxtMessageResponse() { MessageId = FlMessageId.ReadHardwareVersion, Arguments = new List <object>() { 1, // DeviceID(string or uint32), 0, // Error(string or uint8) "a.1.2.3" // Hardware version } }; FlTxtPacketBuilder.BuildMessagePacket(ref _message); for (_i = 0; _i < _message.Buffer.Length; _i++) { _parseResult = _parser.ParseResponseEvent(_message.Buffer[_i], out _parsedMessage); } Assert.AreEqual(FlParseState.ParseOk, _parseResult); Assert.AreEqual(FlMessageType.Text, _parsedMessage.MessageType); Assert.AreEqual(FlMessageCategory.Response, _parsedMessage.MessageCategory); Assert.AreEqual(FlMessageId.ReadHardwareVersion, _parsedMessage.MessageId); Assert.IsNotNull(_parsedMessage.Arguments); Assert.AreEqual(3, _parsedMessage.Arguments.Count); Assert.AreEqual("1", (string)_parsedMessage.Arguments[0]); Assert.AreEqual("0", (string)_parsedMessage.Arguments[1]); Assert.AreEqual("a.1.2.3", (string)_parsedMessage.Arguments[2]); }
public void InitializeBeforeEveryTest() { _parseResult = FlParseState.Parsing; _parsedMessage = null; _parser.Clear(); }
private void ProcessAppTxtMessage(int bytesToRead) { for (int i = 0; i < bytesToRead; i++) { FlParseState ret = _appTxtParser.ParseResponseEvent(_rx_buf[i], out _response); if (ret == FlParseState.ParseOk) { ResponseReceived = true; switch (_response.MessageId) { case FlMessageId.ReadHardwareVersion: ProcessAppTxtHwVerResponse(_response); break; case FlMessageId.ReadFirmwareVersion: ProcessAppTxtFwVerResponse(_response); break; case FlMessageId.ReadGpio: ProcessAppTxtReadGpioResponse(_response); break; case FlMessageId.WriteGpio: ProcessAppTxtWriteGpioResponse(_response); break; case FlMessageId.ReadTemperature: ProcessAppTxtReadTemperatureResponse(_response); break; case FlMessageId.ReadHumidity: ProcessAppTxtReadHumidityResponse(_response); break; case FlMessageId.ReadTempAndHum: ProcessAppTxtReadTempAndHumResponse(_response); break; case FlMessageId.BootMode: ProcessAppTxtBootModeResponse(_response); break; case FlMessageId.Reset: ProcessAppTxtResetResponse(_response); break; case FlMessageId.ButtonEvent: ProcessAppTxtButtonEvent(_response); break; } } else if (ret == FlParseState.ParseFail) { Log.Debug("Application text response parser fail"); } } }
private void ProcessAppBinResponse(byte[] rxBuf, int bytesToRead) { for (int i = 0; i < bytesToRead; i++) { FlParseState ret = _appBinParser.Parse(rxBuf[i], out IFlMessage response); if (ret == FlParseState.ParseOk) { _responseQ.Enqueue(response); } else if (ret == FlParseState.ParseFail) { Log.Debug("Application binary response parser fail"); } } }
private void ProcessAppTxtResponse(byte[] rxBuf, int actualRead) { for (int i = 0; i < actualRead; i++) { FlParseState ret = _appTxtParser.ParseResponseEvent(rxBuf[i], out IFlMessage responseOrEvt); if (ret == FlParseState.ParseOk) { if (responseOrEvt.MessageCategory == FlMessageCategory.Response) { Log.Debug("Response received"); _responseQ.Enqueue(responseOrEvt); } else { _eventQ.Enqueue(responseOrEvt); } } else if (ret == FlParseState.ParseFail) { Log.Debug("Application binary response parser fail"); } } }
public FlParseState Parse(byte data, out IFlMessage message) { FlParseState ret = FlParseState.Parsing; message = null; switch (_receiveState) { case ReceiveState.Stx: if (data == FlConstant.FL_BIN_MSG_STX) { _buf[_bufPos++] = data; _receiveState = ReceiveState.Uid; _count = 0; } else { ret = FlParseState.ParseFail; } break; case ReceiveState.Uid: if (_count < FlConstant.FL_BIN_MSG_DEVICE_ID_LENGTH) { _buf[_bufPos++] = data; _count++; if (_count == FlConstant.FL_BIN_MSG_DEVICE_ID_LENGTH) { _count = 0; _receiveState = ReceiveState.Length; } } else { ret = FlParseState.ParseFail; } break; case ReceiveState.Length: _buf[_bufPos++] = data; _payloadLength = data; _receiveState = ReceiveState.Payload; break; case ReceiveState.Payload: // Flag1 ~ FL_BIN_MSG_ETX if (_count < _payloadLength) { // Check buffer overflow. if (_bufPos < _buf.Length) { _buf[_bufPos++] = data; _count++; if (_count == _payloadLength) { _count = 0; ret = FlParseState.ParseOk; } } else { ret = FlParseState.ParseFail; } } else { ret = FlParseState.ParseFail; } break; } if (ret != FlParseState.Parsing) { if (ret == FlParseState.ParseOk) { if (Role == FlParserRole.Host) { // Process response. //if (MakeResponseObject2(out message) != true) if (MakeResponseEventObject(ref message) != true) { ret = FlParseState.ParseFail; } } else if (Role == FlParserRole.Device) { // Process command. if (MakeCommandObject(ref message) != true) { ret = FlParseState.ParseFail; } } } // Clear parser data. Clear(); } return(ret); }
public FlParseState ParseCommand(byte data, out IFlMessage message) { FlParseState ret = FlParseState.Parsing; message = null; if (_fullPacketLength < FlConstant.FL_TXT_MSG_MAX_LENGTH) { _fullPacket[_fullPacketLength++] = data; } switch (_receiveState) { case ReceiveState.MessageId: if (IsMsgIdChar(data) == true) { _buf[_bufPos++] = data; if (_bufPos > FlConstant.TXT_MSG_ID_MAX_LEN) { ret = FlParseState.ParseFail; } } else if (data == FlConstant.FL_TXT_MSG_ID_DEVICE_ID_DELIMITER) { _msgId = GetMessageId(); if (_msgId != FlMessageId.Unknown) { _receiveState = ReceiveState.DeviceId; ClearReceiveBuffer(); } else { ret = FlParseState.ParseFail; } } else { ret = FlParseState.ParseFail; } break; case ReceiveState.DeviceId: if (IsDeviceIdChar(data) == true) { _buf[_bufPos++] = data; if (_bufPos > FlConstant.TXT_DEVICE_ID_MAX_LEN) { ret = FlParseState.ParseFail; } } else if (data == FlConstant.FL_TXT_MSG_ARG_DELIMITER) { if (ProcessCommandData() == true) { if (IsCommandWithArgument(_msgId) == true) { _receiveState = ReceiveState.Data; ClearReceiveBuffer(); } else { ret = FlParseState.ParseFail; } } else { ret = FlParseState.ParseFail; } } else if (IsTail(data) == true) { if (ProcessCommandData() == true) { _receiveState = ReceiveState.Tail; ret = FlParseState.ParseOk; } else { ret = FlParseState.ParseFail; } } else { ret = FlParseState.ParseFail; } break; case ReceiveState.Data: if (IsTail(data) != true) { if (data != FlConstant.FL_TXT_MSG_ARG_DELIMITER) { _buf[_bufPos++] = data; if (_bufPos >= FlConstant.FL_TXT_MSG_MAX_LENGTH) { ret = FlParseState.ParseFail; } } else { if (ProcessCommandData() == true) { ClearReceiveBuffer(); } else { ret = FlParseState.ParseFail; } } } else { if (ProcessCommandData() == true) { _receiveState = ReceiveState.Tail; ClearReceiveBuffer(); ret = FlParseState.ParseOk; } else { ret = FlParseState.ParseFail; } } break; default: ret = FlParseState.ParseFail; break; } if (ret != FlParseState.Parsing) { if (ret == FlParseState.ParseOk) { if (OnParseDone != null) { OnParseDone?.Invoke(this, null); } else { message = new FlTxtMessageCommand() { MessageId = _msgId, Arguments = new List <object>() }; while (_arguments.Count > 0) { message.Arguments.Add(_arguments[0]); _arguments.RemoveAt(0); } } } Clear(); } return(ret); }