Release 4.0.0-preview1-00279
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.SttEngine / Interop / Interop.SttEngine.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Tizen.Uix.SttEngine;
21 using static Tizen.Uix.SttEngine.Engine;
22
23 /// <summary>
24 /// Partial Interop Class
25 /// </summary>
26 internal static partial class Interop
27 {
28     /// <summary>
29     /// SttEngine Interop Class
30     /// </summary>
31     internal static class SttEngine
32     {
33         internal static string LogTag = "Tizen.Uix.SttEngine";
34
35         private const int ErrorStt = -0x02F00000;
36
37         public enum ErrorCode
38         {
39             None = Tizen.Internals.Errors.ErrorCode.None,                           /* Successful */
40             OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,             /* Out of Memory */
41             IoError = Tizen.Internals.Errors.ErrorCode.IoError,                     /* I/O error */
42             InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,   /* Invalid parameter */
43             NetworkDown = Tizen.Internals.Errors.ErrorCode.Networkdown,             /* Network down(Out of network) */
44             InvalidState = ErrorStt | 0x01,                                         /* Invalid state */
45             InvalidLanguage = ErrorStt | 0x02,                                      /* Invalid language */
46             OperationFailed = ErrorStt | 0x04,                                      /* Operation failed  */
47             NotSupportedFeature = ErrorStt | 0x05,                                  /* Not supported feature of current engine */
48             NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,           /* Device or resource busy */
49             PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,   /* Permission denied */
50             RecordingTimedOut = ErrorStt | 0x06,                                    /* Recording timed out */
51         };
52
53         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
54         internal delegate Error InitializeCb();
55
56         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
57         internal delegate Error DeinitializeCb();
58
59         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
60         internal delegate Error ForEachSupportedLangsCb(SupportedLanguages cb, IntPtr userData);
61
62         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
63         internal delegate Error IsValidLanguageCb(IntPtr language, IntPtr isValid);
64
65         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
66         internal delegate bool SupportSilenceDetectionCb();
67
68         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
69         internal delegate Error SupportRecognitionTypeCb(string type, out bool isSupported);
70
71         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
72         internal delegate Error GetRecordingFormatCb(out AudioType types, out int rate, out int channels);
73
74         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
75         internal delegate Error SetSilenceDetectionCb(bool isSet);
76
77         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
78         internal delegate Error CheckAppAgreedCb(string appid, out bool isAgreed);
79
80         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
81         internal delegate bool NeedAppCredentialCb();
82
83         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
84         internal delegate Error ForEachResultTimeCb(IntPtr timeInfo, ResultTime callback, IntPtr userData);
85
86         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
87         internal delegate Error StartCb(IntPtr language, IntPtr type, IntPtr appid, IntPtr credential, IntPtr userData);
88
89         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
90         internal delegate Error SetRecordingDataCb(string data, uint length);
91
92         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
93         internal delegate Error StopCb();
94
95         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
96         internal delegate Error CancelCb();
97
98         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
99         internal delegate Error GetInfoCb(out IntPtr engineUuid, out IntPtr engineName, out IntPtr engineSetting, out IntPtr useNetwork);
100
101         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
102         internal delegate Error PrivateDataSetCb(string key, string data);
103
104         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
105         internal delegate Error PrivateDataRequestedCb(string key, out string data);
106
107         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
108         internal struct RequestCallbackStruct
109         {
110             internal int version;
111             [MarshalAs(UnmanagedType.FunctionPtr)]
112             internal GetInfoCb getInfo;
113             [MarshalAs(UnmanagedType.FunctionPtr)]
114             internal InitializeCb initialize;
115             [MarshalAs(UnmanagedType.FunctionPtr)]
116             internal DeinitializeCb deinitialize;
117             [MarshalAs(UnmanagedType.FunctionPtr)]
118             internal ForEachSupportedLangsCb supportedLanaguge;
119             [MarshalAs(UnmanagedType.FunctionPtr)]
120             internal IsValidLanguageCb validLanaguage;
121             [MarshalAs(UnmanagedType.FunctionPtr)]
122             internal SupportSilenceDetectionCb silence;
123             [MarshalAs(UnmanagedType.FunctionPtr)]
124             internal SupportRecognitionTypeCb recognitionType;
125             [MarshalAs(UnmanagedType.FunctionPtr)]
126             internal GetRecordingFormatCb recordingFormat;
127             [MarshalAs(UnmanagedType.FunctionPtr)]
128             internal ForEachResultTimeCb resultTime;
129             [MarshalAs(UnmanagedType.FunctionPtr)]
130             internal SetSilenceDetectionCb silenceDetection;
131             [MarshalAs(UnmanagedType.FunctionPtr)]
132             internal StartCb start;
133             [MarshalAs(UnmanagedType.FunctionPtr)]
134             internal SetRecordingDataCb recordingData;
135             [MarshalAs(UnmanagedType.FunctionPtr)]
136             internal StopCb stop;
137             [MarshalAs(UnmanagedType.FunctionPtr)]
138             internal CancelCb cancel;
139             [MarshalAs(UnmanagedType.FunctionPtr)]
140             internal CheckAppAgreedCb checkAppAgreed;
141             [MarshalAs(UnmanagedType.FunctionPtr)]
142             internal NeedAppCredentialCb needAppCredential;
143         };
144
145         internal sealed class CallbackStructGCHandle : IDisposable
146         {
147             internal RequestCallbackStruct CallbackStruct;
148             internal GCHandle CallbackStructHandle;
149             public CallbackStructGCHandle()
150             {
151                 CallbackStruct = new RequestCallbackStruct();
152                 CallbackStructHandle = GCHandle.Alloc(CallbackStruct);
153             }
154
155             #region IDisposable Support
156             private bool disposedValue = false;
157
158             void Dispose(bool disposing)
159             {
160                 Tizen.Log.Info(LogTag, "In Dispose");
161                 if (!disposedValue)
162                 {
163                     if (disposing)
164                     {
165                         Tizen.Log.Info(LogTag, "In Dispose free called");
166                         CallbackStructHandle.Free();
167                     }
168
169                     disposedValue = true;
170                 }
171             }
172
173             public void Dispose()
174             {
175                 Dispose(true);
176             }
177             #endregion
178         }
179
180         [DllImport(Libraries.SttEngine, EntryPoint = "stte_main")]
181         internal static extern Error STTEMain(int argc, string[] argv, IntPtr callback);
182
183         [DllImport(Libraries.SttEngine, EntryPoint = "stte_send_result")]
184         internal static extern Error STTESendResult(ResultEvent resultEvent, string type, string[] result, int resultCount, string msg, IntPtr timeInfo, IntPtr userData);
185
186         [DllImport(Libraries.SttEngine, EntryPoint = "stte_send_error")]
187         internal static extern Error STTESendError(Error error, string msg);
188
189         [DllImport(Libraries.SttEngine, EntryPoint = "stte_send_speech_status")]
190         internal static extern Error STTESendSpeechStatus(SpeechStatus status, IntPtr userData);
191
192         [DllImport(Libraries.SttEngine, EntryPoint = "stte_set_private_data_set_cb")]
193         internal static extern Error STTESetPrivateDataSetCb(PrivateDataSetCb callbackFunc);
194
195         [DllImport(Libraries.SttEngine, EntryPoint = "stte_set_private_data_requested_cb")]
196         internal static extern Error STTESetPrivateDataRequestedCb(PrivateDataRequestedCb callbackFunc);
197     }
198 }