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 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 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 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]);
        }
Example #7
0
        private void ProcessAppBinMessage(int bytesToRead)
        {
            for (int i = 0; i < bytesToRead; i++)
            {
                FlParseState ret = _appBinParser.Parse(_rx_buf[i], out _response);
                if (ret == FlParseState.ParseOk)
                {
                    ResponseReceived = true;
                    IFlBinMessage message = (IFlBinMessage)_response;
                    switch (message.MessageId)
                    {
                    case FlMessageId.ReadHardwareVersion:
                        ProcessAppBinHwVerResponse(message);
                        break;

                    case FlMessageId.ReadFirmwareVersion:
                        ProcessAppBinFwVerResponse(message);
                        break;

                    case FlMessageId.ReadGpio:
                        ProcessAppBinReadGpioResponse(message);
                        break;

                    case FlMessageId.WriteGpio:
                        ProcessAppBinWriteGpioResponse(message);
                        break;

                    case FlMessageId.ReadTemperature:
                        ProcessAppBinReadTemperatureResponse(message);
                        break;

                    case FlMessageId.ReadHumidity:
                        ProcessAppBinReadHumidityResponse(message);
                        break;

                    case FlMessageId.ReadTempAndHum:
                        ProcessAppBinReadTempAndHumResponse(message);
                        break;

                    case FlMessageId.BootMode:
                        ProcessAppBinBootModeResponse(message);
                        break;

                    case FlMessageId.Reset:
                        ProcessAppBinResetResponse(message);
                        break;

                    case FlMessageId.ButtonEvent:
                        ProcessAppBinButtonEvent(message);
                        break;
                    }
                }
                else if (ret == FlParseState.ParseFail)
                {
                    Log.Debug("Application binary response parser fail");
                }
            }
        }
Example #8
0
        public static void BuildMessagePacket(ref IFlMessage binMessage)
        {
            List <byte>   packet = new List <byte>();
            Type          type;
            byte          length = BASE_LENGTH;
            UInt16        crc;
            IFlBinMessage binMsg = (IFlBinMessage)binMessage;

            // TODO : Check message type(text).
            // TODO : Check message category.
            // TODO : Check DeviceID

            // STX.
            packet.Add(FlConstant.FL_BIN_MSG_STX);

            // Device ID.
            type = binMsg.Header.device_id.GetType();
            packet.AddRange(InternalProtoUtil.BuildBinPacketForPrimitiveArgument(binMsg.Header.device_id, Type.GetTypeCode(type)));

            // Length
            packet.Add(length);

            // Message ID.
            packet.Add((byte)binMsg.Header.message_id);

            // Flag1
            packet.Add(binMsg.Header.flag1.BitField1);

            // Flag2
            packet.Add(binMsg.Header.flag2.BitField1);

            // Arguments
            if (binMessage.Arguments?.Count > 0)
            {
                foreach (object arg in binMessage.Arguments)
                {
                    type = arg.GetType();
                    packet.AddRange(InternalProtoUtil.BuildBinPacketForPrimitiveArgument(arg, Type.GetTypeCode(type)));
                }
            }

            // Length value =   ( current packet length + CRC(2 byte) + ETX(1 byte) )
            //                - ( STX(1 byte) + Device ID(4 byte) + Length(1 byte))
            // => current packet length - 3
            packet[LENGTH_FIELD_INDEX] = (byte)(packet.Count - 3);

            // CRC16
            crc = FlUtil.CRC16(packet, 1, packet.Count - 1);
            packet.AddRange(InternalProtoUtil.BuildBinPacketForPrimitiveArgument(crc, crc.GetTypeCode()));

            // ETX
            packet.Add(FlConstant.FL_BIN_MSG_ETX);

            binMsg.Buffer = packet.ToArray();
        }
Example #9
0
 private void ProcessAppBinButtonEvent(IFlBinMessage evt)
 {
     if (evt.Arguments?.Count == 2)
     {
         Log.Information($"Button : {evt.Arguments[0]}, State : {evt.Arguments[1]}");
     }
     else
     {
         Log.Warning("Invalid button event");
     }
 }
Example #10
0
        private void ProcessAppBinWriteGpioResponse(IFlBinMessage response)
        {
            if (response.Header.flag2.error == FlConstant.FL_OK)
            {
                Log.Information("Write GPIO OK");
            }
            else
            {
                Log.Warning("Write GPIO fail");
            }

            ResponseReceived = true;
        }
Example #11
0
        private void ProcessAppBinBootModeResponse(IFlBinMessage response)
        {
            if (response.Header.flag2.error == FlConstant.FL_OK)
            {
                Log.Information("Boot mode OK");
            }
            else
            {
                Log.Warning("Boot mode fail");
            }

            ResponseReceived = true;
        }
Example #12
0
        private void ProcessAppBinHwVerResponse(IFlBinMessage response)
        {
            if (response.Header.flag2.error == FlConstant.FL_OK)
            {
                if (response.Arguments?.Count == 1)
                {
                    Log.Information($"Hw version : {(string)response.Arguments[0]}");
                }
            }
            else
            {
                Log.Warning("Hardware version read fail");
            }

            ResponseReceived = true;
        }
Example #13
0
        private void ProcessAppBinReadGpioResponse(IFlBinMessage response)
        {
            if (response.Header.flag2.error == FlConstant.FL_OK)
            {
                if (response.Arguments?.Count == 2)
                {
                    Log.Information($"GPIO number : {(byte)response.Arguments[0]}, GPIO value : {(byte)response.Arguments[1]}");
                }
            }
            else
            {
                Log.Warning("Read GPIO fail");
            }

            ResponseReceived = true;
        }
Example #14
0
        private void ProcessAppBinReadTemperatureResponse(IFlBinMessage response)
        {
            if (response.Header.flag2.error == FlConstant.FL_OK)
            {
                if (response.Arguments?.Count == 2)
                {
                    Log.Information($"Sensor number : {(byte)response.Arguments[0]}, Temperature : {(double)response.Arguments[1]:0.##}");
                }
            }
            else
            {
                Log.Warning("Read temperature fail");
            }

            ResponseReceived = true;
        }