Esempio n. 1
0
        /// <summary>
        /// To build the byte array which represents this Gaia Packet over BLE.
        /// The bytes array is built according to the definition of a GAIA Packet sent over BLE:
        /// 0 bytes  1         2        3         4                 len+4
        /// +--------+---------+--------+--------+ +--------+--------+
        /// |    VENDOR ID     |   COMMAND ID    | | PAYLOAD   ...   |
        /// +--------+---------+--------+--------+ +--------+--------+
        /// </summary>
        /// <param name="_commandId">The command ID for the packet bytes to build:
        /// The original command ID of this packet.
        /// The acknowledgement version of the original command ID.</param>
        /// <param name="_payload">The payload to include in the packet bytes.</param>
        /// <returns>A new byte array built with the given information.</returns>
        public override byte[] BuildBytes(int _commandId, byte[] _payload)
        {
            int length = _payload.Length + OFFSET_PAYLOAD;

            byte[] data = new byte[length];

            try
            {
                GaiaUtils.CopyIntIntoByteArray(VendorId, data, OFFSET_VENDOR_ID, LENGTH_VENDOR_ID, false);
                GaiaUtils.CopyIntIntoByteArray(_commandId, data, OFFSET_COMMAND_ID, LENGTH_COMMAND_ID, false);
                Array.Copy(_payload, 0, data, OFFSET_PAYLOAD, _payload.Length);
            }
            catch (GaiaException)
            {
                throw new GaiaException(GaiaException.Type.PAYLOAD_LENGTH_TOO_LONG);
            }

            return(data);
        }
Esempio n. 2
0
        /// <summary>
        /// To build the byte array which represents this Gaia Packet over BR/EDR.
        /// The bytes array is built according to the definition of a GAIA Packet sent over BR/EDR:
        /// 0 bytes  1         2        3        4        5        6        7         8         9       len+8
        /// +--------+---------+--------+--------+--------+--------+--------+--------+ +--------+--------+ +--------+
        /// |   SOF  | VERSION | FLAGS  | LENGTH |    VENDOR ID    |   COMMAND ID    | | PAYLOAD   ...   | | CHECK  |
        /// +--------+---------+--------+--------+--------+--------+--------+--------+ +--------+--------+ +--------+
        /// </summary>
        /// <param name="_commandId">The command ID for the packet bytes to build:
        /// The original command ID of this packet.
        /// The acknowledgement version of the original command ID.</param>
        /// <param name="_payload">The payload to include in the packet bytes.</param>
        /// <returns>A new byte array built with the given information.</returns>
        public override byte[] BuildBytes(int _commandId, byte[] _payload)
        {
            // if the payload is bigger than the maximum size: packet won't be sent.
            if (_payload.Length > MAX_PAYLOAD)
            {
                throw new GaiaException(GaiaException.Type.PAYLOAD_LENGTH_TOO_LONG);
            }

            int length = _payload.Length + OFFSET_PAYLOAD + (mHasChecksum ? CHECK_LENGTH : 0);

            byte[] data = new byte[length];
            data[OFFSET_SOF]     = SOF;
            data[OFFSET_VERSION] = (byte)PROTOCOL_VERSION;
            data[OFFSET_FLAGS]   = 0x01;
            if (!mHasChecksum)
            {
                data[OFFSET_FLAGS] = 0x00;
            }
            data[OFFSET_LENGTH] = (byte)Payload.Length;

            GaiaUtils.CopyIntIntoByteArray(VendorId, data, OFFSET_VENDOR_ID, LENGTH_VENDOR_ID, false);
            GaiaUtils.CopyIntIntoByteArray(_commandId, data, OFFSET_COMMAND_ID, LENGTH_COMMAND_ID, false);

            Array.Copy(_payload, 0, data, OFFSET_PAYLOAD, _payload.Length);

            // if there is a checksum, calculating the checksum value
            if (mHasChecksum)
            {
                byte check = 0;
                for (int i = 0; i < length - 1; i++)
                {
                    check ^= data[i];
                }
                data[length - 1] = check;
            }

            return(data);
        }