/// <summary>
        /// Generic store function for all value types.
        /// Using BinaryFormatter.Serialize() to serialize data to bytes.
        /// </summary>
        /// <typeparam name="T">The type of data that is being retrieved.</typeparam>
        /// <param name="dataKey">The key string associated with the data.</param>
        /// <param name="value">The generic type value to store.</param>
        /// <returns>
        /// MLResult.Result will be<c> MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to internal invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// MLResult.Result will be <c>MLResult.Code.SecureStorageIOFailure</c> if an I/O failure occurred.
        /// </returns>
        public static MLResult StoreData <T>(string dataKey, T value) where T : struct
        {
            // TODO: use non-template version of StoreData after converting value to byte array
            try
            {
                MLResult result = CheckKey(dataKey);
                if (!result.IsOk)
                {
                    return(result);
                }

                byte[] valueByteArray = SerializeData(value);
                if (valueByteArray == null)
                {
                    return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "Data serialization failed"));
                }

                MLResult.Code resultCode = MLSecureStorageNativeBindings.MLSecureStoragePutBlob(dataKey, valueByteArray, (uint)valueByteArray.Length);
                result = MLResult.Create(resultCode);

                return(result);
            }
            catch (System.DllNotFoundException)
            {
                MLPluginLog.Error(DllNotFoundError);
                throw;
            }
        }
        /// <summary>
        /// Stores the specified data under the specified key. An existing key would be overwritten.
        /// </summary>
        /// <param name="dataKey">The key string associated with the data.</param>
        /// <param name="data">The data byte array to store.</param>
        /// <returns>
        /// MLResult.Result will be<c> MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to internal invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// MLResult.Result will be <c>MLResult.Code.SecureStorageIOFailure</c> if an I/O failure occurred.
        /// </returns>
        public static MLResult StoreData(string dataKey, byte[] data)
        {
            try
            {
                // Early exit if array is invalid.
                if (data == null)
                {
                    return(MLResult.Create(MLResult.Code.InvalidParam, "Data parameter was null"));
                }

                // Early exit if string key is invalid.
                MLResult result = CheckKey(dataKey);
                if (!result.IsOk)
                {
                    return(result);
                }

                MLResult.Code resultCode = MLSecureStorageNativeBindings.MLSecureStoragePutBlob(dataKey, data, (uint)data.Length);
                result = MLResult.Create(resultCode);

                return(result);
            }
            catch (System.DllNotFoundException)
            {
                MLPluginLog.Error(DllNotFoundError);
                throw;
            }
        }
        /// <summary>
        /// Retrieves the data associated with the specified key.
        /// </summary>
        /// <param name="dataKey">The key for the data that is being requested.</param>
        /// <param name="data">A valid array of bytes to store retrieved data.</param>
        /// <returns>
        /// MLResult.Result will be<c> MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to internal invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// MLResult.Result will be <c>MLResult.Code.SecureStorageBlobNotFound</c> if the dataKey was not found.
        /// MLResult.Result will be <c>MLResult.Code.SecureStorageIOFailure</c> if an I/O failure occurred.
        /// </returns>
        public static MLResult GetData(string dataKey, ref byte[] data)
        {
            try
            {
                MLResult result = CheckKey(dataKey);
                if (!result.IsOk)
                {
                    return(result);
                }

                IntPtr outputBytes = IntPtr.Zero;
                ulong  dataLength  = 0;

                MLResult.Code resultCode = MLSecureStorageNativeBindings.MLSecureStorageGetBlob(dataKey, ref outputBytes, ref dataLength);
                result = MLResult.Create(resultCode);

                // Are there situations where the result is Ok but no data is available?
                if (result.IsOk && dataLength > 0 && outputBytes != IntPtr.Zero)
                {
                    data = new byte[dataLength];
                    Marshal.Copy(outputBytes, data, 0, (int)dataLength);

                    MLSecureStorageNativeBindings.MLSecureStorageFreeBlobBuffer(outputBytes);
                }

                return(result);
            }
            catch (System.DllNotFoundException)
            {
                MLPluginLog.Error(DllNotFoundError);
                throw;
            }
        }
        /// <summary>
        /// Deletes the item associated with the specified key.
        /// </summary>
        /// <param name="dataKey">The key string of the item to delete.</param>
        /// <returns>
        /// MLResult.Result will be<c> MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if failed due to internal invalid input parameter.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error.
        /// MLResult.Result will be <c>MLResult.Code.SecureStorageIOFailure</c> if an I/O failure occurred.
        /// </returns>
        public static MLResult DeleteData(string dataKey)
        {
            try
            {
                MLResult result = CheckKey(dataKey);
                if (!result.IsOk)
                {
                    return(result);
                }

                return(MLResult.Create(MLSecureStorageNativeBindings.MLSecureStorageDeleteBlob(dataKey)));
            }
            catch (System.DllNotFoundException)
            {
                MLPluginLog.Error(DllNotFoundError);
                throw;
            }
        }
예제 #5
0
        /// <summary>
        /// Provides the string value for any MLResult.Code.
        /// </summary>
        /// <param name="resultCode">The code to convert into a string value.</param>
        /// <returns>The string value of the given MLResult.Code.</returns>
        public static string CodeToString(MLResult.Code resultCode)
        {
            string codeString = string.Empty;

            switch ((CodePrefix)((int)resultCode >> 16))
            {
            case CodePrefix.MLResultGlobal:
            case CodePrefix.MLSnapshotResult:
                codeString = Marshal.PtrToStringAnsi(MagicLeapNativeBindings.MLSnapshotGetResultString(resultCode));
                break;

            case CodePrefix.MLAudioResult:
                codeString = Marshal.PtrToStringAnsi(MLAudio.GetResultString(resultCode));
                break;

            case CodePrefix.MLMediaDRMResult:
            case CodePrefix.MLMediaGenericResult:
            case CodePrefix.MLMediaPlayerResult:
            case CodePrefix.MLMediaResult:
                codeString = Marshal.PtrToStringAnsi(MLMediaPlayer.GetResultString(resultCode));
                break;

            case CodePrefix.MLDispatchResult:
                codeString = Marshal.PtrToStringAnsi(MLDispatch.GetResultString(resultCode));
                break;

            case CodePrefix.MLIdentityResult:
                codeString = Marshal.PtrToStringAnsi(MLIdentity.GetResultString(resultCode));
                break;

            case CodePrefix.MLPassableWorldResult:
                codeString = Marshal.PtrToStringAnsi(MLPersistentCoordinateFrames.GetResultString(resultCode));
                break;

            case CodePrefix.MLTokenAgentResult:
                codeString = Marshal.PtrToStringAnsi(MLTokenAgent.GetResultString(resultCode));
                break;

            case CodePrefix.MLPrivilegesResult:
                codeString = Marshal.PtrToStringAnsi(MLPrivileges.GetResultString(resultCode));
                break;

            case CodePrefix.MLContactsResult:
                codeString = Marshal.PtrToStringAnsi(MLContacts.GetResultString(resultCode));
                break;

            case CodePrefix.MLLocationResult:
                codeString = Marshal.PtrToStringAnsi(MLLocation.GetResultString(resultCode));
                break;

            case CodePrefix.MLNetworkingResult:
                codeString = Marshal.PtrToStringAnsi(MLNetworkingNativeBindings.MLNetworkingGetResultString(resultCode));
                break;

            case CodePrefix.MLMovementResult:
                codeString = Marshal.PtrToStringAnsi(MLMovement.GetResultString(resultCode));
                break;

            case CodePrefix.MLConnectionsResult:
                codeString = Marshal.PtrToStringAnsi(MLConnections.GetResultString(resultCode));
                break;

            case CodePrefix.MLSecureStorageResult:
                codeString = Marshal.PtrToStringAnsi(MLSecureStorageNativeBindings.MLSecureStorageGetResultString(resultCode));
                break;

            case CodePrefix.MLAppConnect:
                codeString = Marshal.PtrToStringAnsi(MLAppConnectNativeBindings.MLAppConnectGetResultString(resultCode));
                break;

            case CodePrefix.MLWebRTC:
                codeString = Marshal.PtrToStringAnsi(MLWebRTC.NativeBindings.MLWebRTCGetResultString(resultCode));
                break;

            case CodePrefix.MLBluetoothGattResult:
                codeString = Marshal.PtrToStringAnsi(MLBluetoothLE.NativeBindings.MLBluetoothGattGetResultString(resultCode));
                break;

            default:
                // This will catch any unknown/invalid return values.
                codeString = MagicLeapNativeBindings.MLGetResultString(resultCode);
                break;
            }

            return(codeString);
        }
 /// <summary>
 /// Gets a readable version of the result code as an ASCII string.
 /// </summary>
 /// <param name="result">The MLResult that should be converted.</param>
 /// <returns>ASCII string containing a readable version of the result code.</returns>
 public static string GetResultString(MLResult.Code result)
 {
     return(Marshal.PtrToStringAnsi(MLSecureStorageNativeBindings.MLSecureStorageGetResultString(result)));
 }