/// <summary> /// Write a new OOB data element to the provided data stream. Takes care of the correct /// format, including measuring the data length. /// </summary> /// <param name="oobData">Target stream which should then contain the OOB data block.</param> /// <param name="type">Data type of the OOB element.</param> /// <param name="data">Payload of the OOB element to write (does not include its /// type or length - both properties will be prepended by this method!)</param> private static void AddOobDataElement(Stream oobData, OobDataTypes type, byte[] data) { if (data == null) return; var dataLength = 1 + data.Length; // 1 Byte: data length oobData.WriteByte((byte)dataLength); // 2 Byte: Type oobData.WriteByte((byte)type); // xx bytes: OOB Data oobData.Write(data, 0, data.Length); }
/// <summary> /// Convert the specified service class enum value to a byte array, based on the specified OOB datatype /// which influences the bit length. /// </summary> /// <param name="newSc">Service class enum to convert to a byte array.</param> /// <param name="dataType">Data type to use for the conversion - defines the byte length of the resulting /// byte array (2 / 4 / 16 bytes)</param> /// <returns>The service class enum value converted to a byte array with the specified length.</returns> private static byte[] ConvertServiceClassToByte(ServiceClassTypes newSc, OobDataTypes dataType) { return ConvertServiceClassToByte(newSc, BytesPerServiceClass(dataType)); }
/// <summary> /// Parse the supplied service class information and internalize it into the /// properties of this class instance - the DataType and the ServiceClasses list. /// </summary> /// <param name="scType">Specifies the service class type, which has to be /// defined in the OobDataTypes enum and defines the number of bits used /// for each service class UUID (16 / 32 / 128 bits)</param> /// <param name="scData">Byte array continaing one or more service class UUIDs, /// each in the bit length specified by the scType parameter.</param> private void ParseServiceClass(byte scType, byte[] scData) { // Check service class type if (!Enum.IsDefined(typeof (OobDataTypes), scType)) { throw new NdefException(NdefExceptionMessages.ExBtNoValidServiceClassId); } var scTypeConv = (OobDataTypes) scType; var bytesPerClass = BytesPerServiceClass(scTypeConv); if (bytesPerClass <= 0) { throw new NdefException(NdefExceptionMessages.ExBtNoValidServiceClassId); } DataType = scTypeConv; // Convert values var numClasses = scData.Length / bytesPerClass; ServiceClasses = new List<byte[]>(numClasses); for (var i = 0; i < scData.Length; i += bytesPerClass) { var curSc = new byte[bytesPerClass]; Array.Copy(scData, i, curSc, 0, bytesPerClass); ServiceClasses.Add(curSc); } }
/// <summary> /// Determine the number of bytes per service class, according to the service class type /// supplied as a parameter. /// </summary> /// <param name="serviceClassType">Service class type to return the number of bytes for.</param> /// <returns>Number of bytes that each service class needs to have to correspond to the /// specified service class type.</returns> private static int BytesPerServiceClass(OobDataTypes serviceClassType) { if (serviceClassType == OobDataTypes.CompleteList16BitServiceClassUuids || serviceClassType == OobDataTypes.IncompleteList16BitServiceClassUuids) return 2; if (serviceClassType == OobDataTypes.CompleteList32BitServiceClassUuids || serviceClassType == OobDataTypes.IncompleteList32BitServiceClassUuids) return 4; if (serviceClassType == OobDataTypes.CompleteList128BitServiceClassUuids || serviceClassType == OobDataTypes.IncompleteList128BitServiceClassUuids) return 16; return -1; }