This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChirpPlugin.cs
258 lines (224 loc) · 8.06 KB
/
ChirpPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public enum ChirpStateEnum {
NotInitialised,
Stopped,
Paused,
Running,
Sending,
Receiving,
}
public enum ChirpErrorsEnum {
CHIRP_CONNECT_OK = 0,
CHIRP_CONNECT_OUT_OF_MEMORY,
CHIRP_CONNECT_NOT_INITIALISED,
CHIRP_CONNECT_NOT_RUNNING,
CHIRP_CONNECT_ALREADY_RUNNING,
CHIRP_CONNECT_ALREADY_STOPPED,
CHIRP_CONNECT_INVALID_SAMPLE_RATE = 20,
CHIRP_CONNECT_NULL_BUFFER,
CHIRP_CONNECT_NULL_POINTER,
CHIRP_CONNECT_INVALID_CHANNEL,
CHIRP_CONNECT_INVALID_FREQUENCY_CORRECTION,
CHIRP_CONNECT_INVALID_KEY = 40,
CHIRP_CONNECT_INVALID_SECRET,
CHIRP_CONNECT_INVALID_CREDENTIALS,
CHIRP_CONNECT_MISSING_SIGNATURE,
CHIRP_CONNECT_INVALID_SIGNATURE,
CHIRP_CONNECT_MISSING_CONFIG,
CHIRP_CONNECT_INVALID_CONFIG,
CHIRP_CONNECT_EXPIRED_CONFIG,
CHIRP_CONNECT_INVALID_VERSION,
CHIRP_CONNECT_INVALID_PROJECT,
CHIRP_CONNECT_INVALID_CONFIG_CHARACTER,
CHIRP_CONNECT_PAYLOAD_EMPTY_MESSAGE = 80,
CHIRP_CONNECT_PAYLOAD_INVALID_MESSAGE,
CHIRP_CONNECT_PAYLOAD_UNKNOWN_SYMBOLS,
CHIRP_CONNECT_PAYLOAD_DECODE_FAILED,
CHIRP_CONNECT_PAYLOAD_TOO_LONG,
CHIRP_CONNECT_PAYLOAD_TOO_SHORT,
CHIRP_CONNECT_INVALID_VOLUME = 99,
CHIRP_CONNECT_UNKNOWN_ERROR = 100,
CHIRP_CONNECT_NETWORK_ERROR = 200,
CHIRP_CONNECT_NETWORK_NO_NETWORK,
CHIRP_CONNECT_NETWORK_PERMISSIONS_NOT_GRANTED,
CHIRP_CONNECT_ACCOUNT_DISABLED,
CHIRP_CONNECT_AUDIO_IO,
CHIRP_CONNECT_SEND_MODE_DISABLED,
CHIRP_CONNECT_RECIEVE_MODE_DISABLED,
CHIRP_CONNECT_DEVICE_MUTED,
CHIRP_PLUGIN_NOT_INIT = 404,
}
public class ChirpPlugin : MonoBehaviour {
#region iOS Plugin Import
delegate void MonoPStateChangeDelegate(ChirpStateEnum state);
delegate void MonoPDataDelegate(string data);
private static bool IsPluginInit;
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern int ChirpInitSDK(string key, string secret, string config);
[DllImport("__Internal")]
private static extern int ChirpStartSDK();
[DllImport("__Internal")]
private static extern int ChirpStopSDK();
[DllImport("__Internal")]
private static extern int ChirpSendData(int length, string payload);
[DllImport("__Internal")]
private static extern void RegisterPluginHandlers(MonoPStateChangeDelegate state, MonoPDataDelegate recieve ,MonoPDataDelegate sent);
#endif
#endregion
#region Android Plugin Import
#if UNITY_ANDROID && !UNITY_EDITOR
private static AndroidJavaObject PluginBridge;
public class ChirpPluginJavaMessageHandler : AndroidJavaProxy {
public ChirpPluginJavaMessageHandler() : base("com.alexanderkub.plugins.unitychirpio.ChirpPluginJavaMessageHandler") { }
public void OnChangeStateHandler(int state) {
if (Enum.IsDefined(typeof(ChirpStateEnum), state)) {
ChirpPlugin.OnChangeState((ChirpStateEnum)state);
}
}
public void OnReceiveDataHandler(string data) {
ChirpPlugin.OnReceiveData(data);
}
public void OnSentDataHandler(string data) {
ChirpPlugin.OnSentData(HexStringToString(data));
}
}
#endif
#endregion
#region Chirp SDK API
public static ChirpStateEnum ChirpState;
public static Action<ChirpStateEnum, ChirpStateEnum> OnChangeStateDataEvent;
public static Action<string> OnRecieveDataEvent;
public static Action<string> OnSentDataEvent;
public static void InitSDK(string key, string secret, string config) {
int errorCode = 0;
#if UNITY_EDITOR
Debug.Log("ChirpPlugin.InitSDK with\n" + key + "\n" + secret + "\n" + config);
#else
#if UNITY_IOS
errorCode = ChirpInitSDK(key, secret, config);
#elif UNITY_ANDROID
object[] parameters = new object[3];
parameters[0] = key;
parameters[1] = secret;
parameters[2] = config;
PluginBridge.Call("ChirpInitSDK", parameters);
#endif
#endif
HandleError(errorCode);
IsPluginInit = true;
}
public static void StartSDK() {
int errorCode = 0;
#if UNITY_EDITOR
Debug.Log("ChirpPlugin.StartSDK");
#else
#if UNITY_IOS
errorCode = ChirpStartSDK();
#elif UNITY_ANDROID
errorCode = PluginBridge.Call<int>("ChirpStartSDK");
#endif
#endif
HandleError(errorCode);
}
public static void StopSDK() {
if (!IsPluginInit) {
return;
}
int errorCode = 0;
#if UNITY_EDITOR
Debug.Log("ChirpPlugin.StopSDK");
#else
#if UNITY_IOS
errorCode = ChirpStopSDK();
#elif UNITY_ANDROID
errorCode = PluginBridge.Call<int>("ChirpStopSDK");
#endif
#endif
HandleError(errorCode);
}
public static void SendData(string payload) {
if (string.IsNullOrEmpty(payload)) {
Debug.Log("ChirpPlugin.SendData Error: NullOrEmpty payload");
return;
}
int errorCode = 0;
#if UNITY_EDITOR
Debug.Log("ChirpPlugin.SendData: " + payload);
#else
#if UNITY_IOS
errorCode = ChirpSendData(payload.Length, payload);
#elif UNITY_ANDROID
errorCode = PluginBridge.Call<int>("ChirpSendData", payload.Length, payload);
#endif
#endif
HandleError(errorCode);
}
#endregion
#region Native Callbacks
[AOT.MonoPInvokeCallback(typeof(MonoPStateChangeDelegate))]
private static void OnChangeState(ChirpStateEnum state) {
OnChangeStateDataEvent?.Invoke(ChirpState, state);
ChirpState = state;
}
[AOT.MonoPInvokeCallback(typeof(MonoPDataDelegate))]
private static void OnSentData(string data) {
if (OnSentDataEvent == null) {
return;
}
OnSentDataEvent(data);
}
[AOT.MonoPInvokeCallback(typeof(MonoPDataDelegate))]
private static void OnReceiveData(string data) {
if (OnRecieveDataEvent == null) {
return;
}
OnRecieveDataEvent(data);
}
[RuntimeInitializeOnLoadMethod]
private static void Initialize() {
#if UNITY_EDITOR
Debug.Log("ChirpPlugin.Initialize");
#else
#if UNITY_IOS
RegisterPluginHandlers(OnChangeState, OnReceiveData, OnSentData);
#elif UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
PluginBridge = new AndroidJavaObject("com.alexanderkub.plugins.unitychirpio.Bridge");
object[] parameters = new object[2];
parameters[0] = unityActivity;
parameters[1] = new ChirpPluginJavaMessageHandler();
PluginBridge.Call("registerPluginHandlers", parameters);
#endif
#endif
}
#endregion
private static string HexStringToString(string HexString) {
string stringValue = "";
for (int i = 0; i < HexString.Length / 2; i++)
{
string hexChar = HexString.Substring(i * 2, 2);
int hexValue = Convert.ToInt32(hexChar, 16);
stringValue += Char.ConvertFromUtf32(hexValue);
}
return stringValue;
}
private static void HandleError(int errorCode) {
if (errorCode == 0) {
return;
}
string errorString;
if (Enum.IsDefined(typeof(ChirpErrorsEnum), errorCode)) {
ChirpErrorsEnum error = (ChirpErrorsEnum)errorCode;
errorString = "ChirpPlugin error(" + errorCode + "): " + error.ToString();
Debug.LogError(errorString);
throw new Exception(errorString);
}
errorString = "ChirpPlugin error(" + errorCode + "): CHIRP_PLUGIN_UNKNOW_ERROR";
Debug.LogError(errorString);
throw new Exception(errorString);
}
}