예제 #1
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();
        }
예제 #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
        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);
        }
예제 #4
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);
        }