Пример #1
0
 public void Clear()
 {
     _msgId = FlMessageId.Unknown;
     //_deviceId = 0;
     _bufPos = 0;
     Array.Clear(_buf, 0, _buf.Length);
     _fullPacketLength = 0;
     Array.Clear(_fullPacket, 0, _fullPacket.Length);
     _receiveState = ReceiveState.MessageId;
     _arguments.Clear();
     _sb.Clear();
 }
Пример #2
0
        private unsafe bool MakeEventObject(FlBinMsgFullStruct fullMsg, ref IFlMessage message)
        {
            bool          ret           = false;
            ushort        receivedCrc   = 0;
            int           crcDataLen    = _bufPos - 4;
            int           crcStartIndex = _bufPos - 3;
            List <object> arguments     = null;
            FlMessageId   messageId     = (FlMessageId)fullMsg.header.message_id;

            switch (messageId)
            {
            case FlMessageId.ButtonEvent:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>()
                    {
                        fullMsg.payload[0],         // Button number
                        fullMsg.payload[1]          // Button value
                    };
                    ret = true;
                }
            }
            break;
            }

            if (ret == true)
            {
                FlBinMessageEvent evt = new FlBinMessageEvent()
                {
                    Arguments   = arguments,
                    ReceiveTime = DateTime.UtcNow
                };
                evt.Header.device_id  = fullMsg.header.device_id;
                evt.Header.length     = fullMsg.header.length;
                evt.Header.message_id = (FlMessageId)fullMsg.header.message_id;
                evt.Header.flag1      = fullMsg.header.flag1;
                evt.Header.flag2      = fullMsg.header.flag2;

                message = evt;
            }

            return(ret);
        }
Пример #3
0
        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);
        }
Пример #4
0
        private unsafe bool MakeResponseObject(FlBinMsgFullStruct fullMsg, ref IFlMessage message)
        {
            bool          ret           = false;
            ushort        receivedCrc   = 0;
            int           crcDataLen    = _bufPos - 4;
            int           crcStartIndex = _bufPos - 3;
            List <object> arguments     = null;
            FlMessageId   messageId     = (FlMessageId)fullMsg.header.message_id;

            // Payload size = header.length - 6;

            // stx header payload crc etx
            // crc : header payload,
            // crc data length = received bytes - stx(1) - crc(2) - etx(1)
            switch (messageId)
            {
            case FlMessageId.ReadHardwareVersion:
            case FlMessageId.ReadFirmwareVersion:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    string hwVerString = Encoding.ASCII.GetString(fullMsg.payload, fullMsg.header.length - 6);
                    arguments = new List <object>()
                    {
                        hwVerString
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.ReadGpio:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>()
                    {
                        fullMsg.payload[0],         // GPIO number
                        fullMsg.payload[1]          // GPIO value
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.WriteGpio:
            case FlMessageId.BootMode:
            case FlMessageId.Reset:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    ret = true;
                }
            }
            break;

            case FlMessageId.ReadTemperature:
            case FlMessageId.ReadHumidity:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>()
                    {
                        fullMsg.payload[0],                                       // Sensor number
                        BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double)) // Sensor value
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.ReadTempAndHum:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);

                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>()
                    {
                        fullMsg.payload[0],                                            // Sensor number
                        BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double) * 2), // Temperature
                        BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double))      // Humidity
                    };
                    ret = true;
                }
            }
            break;
            }

            if (ret == true)
            {
                FlBinMessageResponse response = new FlBinMessageResponse()
                {
                    Arguments   = arguments,
                    ReceiveTime = DateTime.UtcNow
                };
                response.Header.device_id  = fullMsg.header.device_id;
                response.Header.length     = fullMsg.header.length;
                response.Header.message_id = (FlMessageId)fullMsg.header.message_id;
                response.Header.flag1      = fullMsg.header.flag1;
                response.Header.flag2      = fullMsg.header.flag2;

                message = response;
            }

            return(ret);
        }
Пример #5
0
        private unsafe bool MakeCommandObject(ref IFlMessage message)
        {
            bool               ret           = false;
            int                crcDataLen    = _bufPos - 4;
            int                crcStartIndex = _bufPos - 3;
            List <object>      arguments     = null;
            FlMessageId        messageId     = FlMessageId.Unknown;
            ushort             receivedCrc   = 0;
            FlBinMsgFullStruct fullMsg       = Marshal.PtrToStructure <FlBinMsgFullStruct>(_bufPtr);

            messageId = (FlMessageId)fullMsg.header.message_id;
            switch (messageId)
            {
            case FlMessageId.ReadHardwareVersion:
            case FlMessageId.ReadFirmwareVersion:
            case FlMessageId.Reset:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);
                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    ret = true;
                }
            }
            break;

            case FlMessageId.ReadGpio:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);
                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>
                    {
                        fullMsg.payload[0]         // GPIO number
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.WriteGpio:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);
                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>
                    {
                        fullMsg.payload[0],          // GPIO number
                        fullMsg.payload[1]           // GPIO value
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.ReadTemperature:
            case FlMessageId.ReadHumidity:
            case FlMessageId.ReadTempAndHum:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);
                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>
                    {
                        fullMsg.payload[0]         // Sensor number
                    };
                    ret = true;
                }
            }
            break;

            case FlMessageId.BootMode:
            {
                _crc16      = FlUtil.CRC16(_buf, 1, crcDataLen);
                receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex);
                if ((CompareCrc(_crc16, receivedCrc) == true) &&
                    (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1]))
                {
                    arguments = new List <object>
                    {
                        fullMsg.payload[0]          // Boot mode
                    };
                    ret = true;
                }
            }
            break;
            }

            if (ret == true)
            {
                FlBinMessageCommand command = new FlBinMessageCommand
                {
                    Arguments = arguments
                };
                command.Header.device_id  = fullMsg.header.device_id;
                command.Header.length     = fullMsg.header.length;
                command.Header.message_id = (FlMessageId)fullMsg.header.message_id;
                command.Header.flag1      = fullMsg.header.flag1;
                command.Header.flag2      = fullMsg.header.flag2;

                message = command;
            }

            return(ret);
        }