XDevicePlugin.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. //#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_WIN || UNITY_MAC
  5. #if true
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using XDebug = UnityEngine.Debug;
  9. #else
  10. using XDebug = System.Diagnostics.Debug;
  11. #endif // UNITY_EDITOR
  12. using NativeHandle = System.Int64;
  13. using NativeExHandle = System.Int64;
  14. using AOT;
  15. using Unity.Collections;
  16. using System.IO;
  17. using System.Linq;
  18. using Ximmerse.XR.Collections;
  19. namespace Ximmerse.XR
  20. {
  21. /// <summary>
  22. /// Tracking summary, reported per frame
  23. /// </summary>
  24. internal struct TrackingSummary : IDisposable
  25. {
  26. /// <summary>
  27. /// The tracked marker count
  28. /// </summary>
  29. public int TrackedCount;
  30. /// <summary>
  31. /// The untracked marker count
  32. /// </summary>
  33. public int UntrackedCount;
  34. /// <summary>
  35. /// The lost tracked marker count
  36. /// </summary>
  37. public int LostTrackedCount;
  38. /// <summary>
  39. /// The new tracked marker count
  40. /// </summary>
  41. public int NewTrackedCount;
  42. /// <summary>
  43. /// All results
  44. /// </summary>
  45. public NativeArray<TrackingResult> allResults;
  46. /// <summary>
  47. /// Currently tracked result.
  48. /// </summary>
  49. public NativeArray<TrackingResult> tracked;
  50. /// <summary>
  51. /// Untracked result.
  52. /// </summary>
  53. public NativeArray<TrackingResult> unTracked;
  54. /// <summary>
  55. /// Added result;
  56. /// </summary>
  57. public NativeArray<TrackingResult> newTracked;
  58. /// <summary>
  59. /// Lost tracking result.
  60. /// </summary>
  61. public NativeArray<TrackingResult> lostTracked;
  62. public void Dispose()
  63. {
  64. allResults.Dispose();
  65. tracked.Dispose();
  66. unTracked.Dispose();
  67. newTracked.Dispose();
  68. lostTracked.Dispose();
  69. }
  70. }
  71. public partial class XDevicePlugin
  72. {
  73. public const string pluginName = "xdevice_client";
  74. internal static List<MarkerDescriptor> loadedMarkers = new List<MarkerDescriptor>();
  75. /// <summary>
  76. /// Cache the previous frames tracking results.
  77. /// </summary>
  78. static Dictionary<int, TrackingResult> prevFrameTrackingResults = new Dictionary<int, TrackingResult>();
  79. #region api wrapper
  80. public static bool ConnectController(int index, string mac, bool force)
  81. {
  82. int ret = xdevc_ctrl_connect_to_address(xdevc_get_controller(index), mac, force);
  83. if (ret < 0)
  84. {
  85. return false;
  86. }
  87. return true;
  88. }
  89. ///////////////////////////////
  90. /// \brief Reset tracking marker settings.
  91. /// \returns
  92. public static XErrorCodes ResetTrackingMarkerSettings()
  93. {
  94. XErrorCodes c = (XErrorCodes)xdevc_vpu_clear_marker_calibration_settings(xdevc_get_vpu());
  95. if (c == XErrorCodes.kErrNoError)
  96. {
  97. loadedMarkers.Clear();
  98. }
  99. return c;
  100. }
  101. ///////////////////////////////
  102. /// \brief Load tracking algorithm marker settings.
  103. /// \param settings_file File to load.
  104. /// \param markerIds [out] Output int array for marker IDs defined in specified file.
  105. /// \returns Return count of marker IDs, or a negactive value for error.
  106. [System.Obsolete("Deprecated , usee another one with NativeArray")]
  107. public static int LoadTrackingMarkerSettingsFile(string settings_file, out int[] markerIds, int n)
  108. {
  109. int[] tmp = new int[n];
  110. IntPtr buffer = Marshal.AllocCoTaskMem(Buffer.ByteLength(tmp));
  111. int ret = xdevc_vpu_load_marker_calibration_from_file(xdevc_get_vpu(), settings_file, n, buffer);
  112. Marshal.Copy(tmp, 0, buffer, n);
  113. Marshal.FreeCoTaskMem(buffer);
  114. if (ret > 0)
  115. {
  116. markerIds = new int[ret];
  117. Array.Copy(tmp, markerIds, ret);
  118. }
  119. else
  120. {
  121. markerIds = new int[0];
  122. }
  123. return ret;
  124. }
  125. ///////////////////////////////
  126. /// \brief Load tracking algorithm marker settings.
  127. /// \param settings_file File to load.
  128. /// \param markerIds [out] Output int array for marker IDs defined in specified file.
  129. /// \returns Return count of marker IDs, or a negactive value for error.
  130. public static int LoadTrackingMarkerSettingsFile(string trackingJsonFile, ref NativeArray<int> markerIds, int n)
  131. {
  132. int resultCode = -1;
  133. int[] tmp = new int[n];
  134. IntPtr buffer = Marshal.AllocCoTaskMem(tmp.Length);
  135. string fullPath_External = Path.Combine(SDKVariants.kTrackingDataDir_External, trackingJsonFile);
  136. string fullPath_Internal = Path.Combine(SDKVariants.kTrackingDataDir_Internal, trackingJsonFile);
  137. //try to load from external, then internal:
  138. string _path = File.Exists(fullPath_External) ? fullPath_External : (File.Exists(fullPath_Internal) ? fullPath_Internal : string.Empty);
  139. if (string.IsNullOrEmpty(_path))
  140. {
  141. Debug.LogErrorFormat("Tracking json : {0} not found", trackingJsonFile);
  142. return resultCode;
  143. }
  144. resultCode = xdevc_vpu_load_marker_calibration_from_file(xdevc_get_vpu(), _path, n, buffer);
  145. Marshal.Copy(tmp, 0, buffer, tmp.Length);
  146. Marshal.FreeCoTaskMem(buffer);
  147. if (resultCode > 0)
  148. {
  149. for (int i = 0; i < tmp.Length; i++)
  150. {
  151. markerIds[i] = tmp[i];
  152. }
  153. //Add to trace list:
  154. TrackedObjectJson trackObj = (TrackedObjectJson)JsonUtility.FromJson(File.ReadAllText(_path), typeof(TrackedObjectJson));
  155. if (trackObj.IsCardGroup)
  156. {
  157. if (loadedMarkers.Count(x => x.id == trackObj.CARD_GROUP.GroupID) == 0)
  158. {
  159. loadedMarkers.Add(new MarkerDescriptor()
  160. {
  161. id = trackObj.CARD_GROUP.GroupID,
  162. isGroup = true,
  163. size = trackObj.CARD_GROUP.MarkersSize[0],
  164. });
  165. }
  166. }
  167. else
  168. {
  169. for (int i = 0; i < trackObj.CARD_SINGLE.Markers.Length; i++)
  170. {
  171. int cardID = trackObj.CARD_SINGLE.Markers[i];
  172. float _size = trackObj.CARD_SINGLE.MarkersSize[i];
  173. if (loadedMarkers.Count(x => x.id == cardID) == 0)
  174. {
  175. loadedMarkers.Add(new MarkerDescriptor()
  176. {
  177. id = cardID,
  178. isGroup = false,
  179. size = _size,
  180. });
  181. }
  182. }
  183. }
  184. }
  185. Debug.LogFormat("Tracking json profile : {0} loaded.", _path);
  186. return resultCode;
  187. }
  188. /// <summary>
  189. /// Called per frame to get hardware tracking result.
  190. /// </summary>
  191. /// <param name="centerEyePose"></param>
  192. /// <param name="trackingSummary"></param>
  193. internal static void UpdateTracking(
  194. Matrix4x4 centerEyePose, out TrackingSummary trackingSummary)
  195. {
  196. trackingSummary = new TrackingSummary();
  197. NativeArray<TrackingResult> _allResults = new NativeArray<TrackingResult>(loadedMarkers.Count, Allocator.TempJob);
  198. xNativeList<TrackingResult> _tracked = xNativeList<TrackingResult>.Create(32);
  199. xNativeList<TrackingResult> _unTracked = xNativeList<TrackingResult>.Create(32);
  200. xNativeList<TrackingResult> _newTracked = xNativeList<TrackingResult>.Create(32);
  201. xNativeList<TrackingResult> _lostTracked = xNativeList<TrackingResult>.Create(32);
  202. for (int i = 0; i < loadedMarkers.Count; i++)
  203. {
  204. MarkerDescriptor loadedMarker = loadedMarkers[i];
  205. TrackingResult r = new TrackingResult()
  206. {
  207. id = loadedMarker.id,
  208. isGroup = loadedMarker.isGroup,
  209. tracked = false,
  210. size = loadedMarker.size,
  211. };
  212. int id = loadedMarker.id;
  213. int index = 0;
  214. long timestamp = 0;
  215. int state = 0;
  216. float posX = 0;
  217. float posY = 0;
  218. float posZ = 0;
  219. float rotX = 0;
  220. float rotY = 0;
  221. float rotZ = 0;
  222. float rotW = 0;
  223. float confidence = 0;
  224. float marker_distance = 0;
  225. long predTimestampNano = 0;
  226. if (NativePluginApi.Unity_getTagTracking(id, predTimestampNano,
  227. ref index, ref timestamp, ref state, ref posX, ref posY, ref posZ,
  228. ref rotX, ref rotY, ref rotZ, ref rotW,
  229. ref confidence, ref marker_distance))
  230. {
  231. if (state != 0)
  232. {
  233. r.tracked = true;
  234. r.trackedConfidence = confidence;
  235. r.trackedDistance = marker_distance;
  236. var world = centerEyePose * Matrix4x4.TRS(new Vector3(posX, posY, posZ), new Quaternion(rotX, rotY, rotZ, rotW), Vector3.one);
  237. r.worldPose = new Pose(world.GetColumn(3),
  238. world.rotation);
  239. trackingSummary.TrackedCount++;
  240. _tracked.Add(r);
  241. //compare to prev frame:
  242. bool prevFrameTracked = prevFrameTrackingResults.ContainsKey(id) && prevFrameTrackingResults[id].tracked;
  243. if (!prevFrameTracked)//new tracked
  244. {
  245. trackingSummary.NewTrackedCount++;
  246. _newTracked.Add(r);
  247. }
  248. //Debug.LogFormat("ID: {0} is tracked, world position = {1}, rotation = {2}", id, r.worldPose.position.ToString("F3"), r.worldPose.rotation.ToString("F3"));
  249. }
  250. //not tracked
  251. else
  252. {
  253. trackingSummary.UntrackedCount++;
  254. _unTracked.Add(r);
  255. //compare to prev frame:
  256. bool prevFrameTracked = prevFrameTrackingResults.ContainsKey(id) && prevFrameTrackingResults[id].tracked;
  257. if (prevFrameTracked)//lost track
  258. {
  259. trackingSummary.LostTrackedCount++;
  260. _lostTracked.Add(r);
  261. }
  262. }
  263. }
  264. _allResults[i] = r;
  265. }
  266. //Cache this frame's result:
  267. prevFrameTrackingResults.Clear();
  268. foreach (var ret in _allResults)
  269. {
  270. prevFrameTrackingResults.Add(ret.id, ret);
  271. }
  272. trackingSummary.allResults = _allResults;
  273. trackingSummary.tracked = _tracked.ToArray(Allocator.TempJob);
  274. trackingSummary.newTracked = _newTracked.ToArray(Allocator.TempJob);
  275. trackingSummary.unTracked = _unTracked.ToArray(Allocator.TempJob);
  276. trackingSummary.lostTracked = _lostTracked.ToArray(Allocator.TempJob);
  277. _tracked.Dispose();
  278. _newTracked.Dispose();
  279. _unTracked.Dispose();
  280. _lostTracked.Dispose();
  281. }
  282. public static bool GetControllerState(int index, Int64 predTimestampNano, ref XAttrTrackingInfo info)
  283. {
  284. System.IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(XAttrTrackingInfo)));
  285. Marshal.StructureToPtr(info, ptr, false);
  286. int ret = xdevc_ctrl_get_fusion(xdevc_get_controller(index), predTimestampNano, ptr);
  287. info = (XAttrTrackingInfo)Marshal.PtrToStructure(ptr, typeof(XAttrTrackingInfo));
  288. Marshal.FreeHGlobal(ptr);
  289. if (ret < 0)
  290. {
  291. return false;
  292. }
  293. return true;
  294. }
  295. ///////////////////////////
  296. /// \brief Get pose data of tracking object.
  297. /// \param track_id Tracking ID.
  298. /// \returns Tracking pose data. \see XAttrTrackingInfo.
  299. public static bool GetTracking(int track_id, Int64 predTimestampNano, ref XAttrTrackingInfo trackingInfo)
  300. {
  301. System.IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(XAttrTrackingInfo)));
  302. Marshal.StructureToPtr(trackingInfo, ptr, false);
  303. int ret = xdevc_vpu_get_predict_tracking(xdevc_get_vpu(), track_id, predTimestampNano, ptr);
  304. trackingInfo = (XAttrTrackingInfo)Marshal.PtrToStructure(ptr, typeof(XAttrTrackingInfo));
  305. Marshal.FreeHGlobal(ptr);
  306. if (ret < 0)
  307. {
  308. return false;
  309. }
  310. return true;
  311. }
  312. #endregion api wrapper
  313. public enum XEvent
  314. {
  315. kXEvtNormalEventsBegin = 1000,
  316. kXEvtInitSuccess = kXEvtNormalEventsBegin,
  317. kXEvtInitFailed,
  318. kXEvtConnectionStateChange,
  319. kXEvtDeviceBatteryStateChange,
  320. kXEvtVpuInfoUpdated,
  321. kXEvtVpuConnectionStateChange,
  322. kXEvtControllerInfoUpdate,
  323. kXEvtControllerStateChange,
  324. kXEvtControllerConnectionStateChange,
  325. kXEvtButtonStateChange,
  326. kXEvtControllerPaired,
  327. kXEvtControllerUnpaired,
  328. kXEvtMax,
  329. };
  330. #region Common
  331. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  332. public static extern int xdevc_init();
  333. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  334. public static extern void xdevc_exit();
  335. // private delegate void xdevc_events_handle_func_t(XEvent evt, IntPtr arg, IntPtr ud);
  336. public delegate void XDeviceClientEventDelegate(XEvent evt, IntPtr handle, IntPtr ud);
  337. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  338. public static extern void xdevc_set_events_cb(XDeviceClientEventDelegate cb);
  339. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  340. public static extern IntPtr xdevc_get_event_name(XEvent evt);
  341. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  342. public static extern IntPtr xdevc_get_error_name(int err);
  343. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  344. public static extern NativeHandle xdevc_get_xcontext();
  345. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  346. public static extern NativeHandle xdevc_get_vpu();
  347. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  348. public static extern NativeHandle xdevc_get_controller(int index);
  349. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  350. public static extern NativeHandle xdevc_get_number_of_controllers();
  351. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  352. public static extern int xdevc_get_property_memory_id(Int64 object_id, int prop_id);
  353. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  354. public static extern int xdevc_do_test();
  355. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  356. public static extern IntPtr xdevc_get_client_version();
  357. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  358. public static extern int xdevc_get_client_build_number();
  359. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  360. public static extern IntPtr xdevc_get_client_build_information();
  361. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  362. public static extern int xdevc_get_client_alg_version();
  363. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  364. public static extern IntPtr xdevc_get_server_version();
  365. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  366. public static extern int xdevc_get_server_build_number();
  367. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  368. public static extern IntPtr xdevc_get_server_build_information();
  369. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  370. public static extern IntPtr xdevc_get_server_alg_version();
  371. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  372. public static extern int xdevc_sdk_autorize();
  373. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  374. public static extern int xdevc_get_authorization_available_time();
  375. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  376. public static extern int xdevc_tx_message(NativeExHandle device_handl, int size, string data);
  377. #endregion Common
  378. #region VPU API
  379. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  380. public static extern bool xdevc_vpu_is_connected(NativeExHandle vpu_handle);
  381. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  382. public static extern int xdevc_vpu_start_pairing_by_type(NativeExHandle vpu_handle, XControllerTypes type);
  383. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  384. public static extern int xdevc_vpu_stop_pairing(NativeExHandle vpu_handle);
  385. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  386. public static extern int xdevc_vpu_unpair_and_disconnect_all_controllers(NativeExHandle vpu_handle);
  387. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  388. public static extern IntPtr xdevc_vpu_get_firmware_version(NativeExHandle vpu_handle);
  389. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  390. public static extern IntPtr xdevc_vpu_get_hardware_version(NativeExHandle vpu_handle);
  391. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  392. public static extern IntPtr xdevc_vpu_get_sn(NativeExHandle vpu_handle);
  393. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  394. public static extern IntPtr xdevc_vpu_get_device_name(NativeExHandle vpu_handle);
  395. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  396. public static extern IntPtr xdevc_vpu_get_model_name(NativeExHandle vpu_handle);
  397. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  398. public static extern IntPtr xdevc_vpu_get_fpga_version(NativeExHandle vpu_handle);
  399. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  400. public static extern IntPtr xdevc_vpu_get_ble_firmware_version(NativeExHandle vpu_handle);
  401. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  402. public static extern IntPtr xdevc_vpu_get_imu_alg_version(NativeExHandle vpu_handle);
  403. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  404. public static extern int xdevc_vpu_get_fpga_model_number(NativeExHandle vpu_handle);
  405. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  406. public static extern int xdevc_vpu_get_mcu_model_number(NativeExHandle vpu_handle);
  407. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  408. public static extern int xdevc_vpu_get_ble_model_number(NativeExHandle vpu_handle);
  409. public delegate void XDeviceFirmwareUpgradeEventsDelegateFn_t(int evt, int int_arg, IntPtr ud);
  410. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  411. public static extern int xdevc_vpu_start_upgrading(
  412. NativeExHandle vpu_handle,
  413. int proj_type,
  414. int fw_type,
  415. string fw_path,
  416. XDeviceFirmwareUpgradeEventsDelegateFn_t event_delegate,
  417. IntPtr ud,
  418. int wait_ms);
  419. #region VPU Tracking Alg
  420. /**
  421. * @brief load marker calibration configure data from file.
  422. *
  423. * @param vpu_handle
  424. * @param file the path of file which must can be read by xdeviceservice application.
  425. * @param nof_output_elements size of output IDs buffer, how many id can be write in output_ids parameter.
  426. * @param output_ids
  427. * @return negative value for error, positive value for number of output tracking IDs.
  428. */
  429. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  430. public static extern int xdevc_vpu_load_marker_calibration_from_file(NativeExHandle vpu_handle, string file, int nof_output_elements, IntPtr output_ids);
  431. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  432. public static extern int xdevc_vpu_clear_marker_calibration_settings(NativeExHandle vpu_handle);
  433. /**
  434. * @brief
  435. *
  436. * @param track_id
  437. * @param timestamp positive value for boot time in nanosecond to predict.
  438. * 0 for no predition, -1 for now timestamp,
  439. * @param out
  440. * @return XIM_API
  441. */
  442. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  443. public static extern int xdevc_vpu_get_predict_tracking(NativeExHandle vpu_handle, int track_id, Int64 timestamp, IntPtr out_tracking_info);
  444. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  445. public static extern int xdevc_vpu_get_tracking(NativeExHandle vpu_handle, int track_id, IntPtr out_tracking_info);
  446. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  447. public static extern int xdevc_vpu_get_tracked(NativeExHandle vpu_handle, Int64 timestamp, int nof_buf, IntPtr out_tracking_info);
  448. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  449. public static extern int xdevc_vpu_get_multiple_trackings(NativeExHandle vpu_handle, Int64 timestamp, int nof_ids, IntPtr ids, int nof_buf, IntPtr[] out_tracking_info);
  450. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  451. public static extern int xdevc_vpu_tracking_predict(Int64 timestamp_ns);
  452. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  453. public static extern int xdevc_vpu_tracking_get_predicted(int tag_id, IntPtr out_tracking_info);
  454. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  455. public static extern int xdevc_vpu_tracking_get_predicted2(
  456. int tag_id,
  457. ref float px, ref float py, ref float pz,
  458. ref float qx, ref float qy, ref float qz, ref float qw,
  459. ref Int64 timestamp,
  460. ref double confidence,
  461. ref double marker_distance);
  462. #endregion VPU Tracking Alg
  463. #endregion VPU API
  464. #region Controller API
  465. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  466. public static extern int xdevc_ctrl_get_connect_state(NativeExHandle controller_handle);
  467. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  468. public static extern bool xdevc_ctrl_is_paired(NativeExHandle controller_handle);
  469. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  470. public static extern int xdevc_ctrl_get_imu_fps(NativeExHandle controller_handle);
  471. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  472. public static extern int xdevc_ctrl_get_battery_level(NativeExHandle controller_handle);
  473. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  474. public static extern int xdevc_ctrl_get_button_state_bitmask(NativeExHandle controller_handle);
  475. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  476. public static extern int xdevc_ctrl_get_trigger(NativeExHandle controller_handle);
  477. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  478. public static extern int xdevc_ctrl_get_imu(NativeExHandle controller_handle, IntPtr imu);
  479. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  480. public static extern int xdevc_ctrl_get_fusion(NativeExHandle controller_handle, Int64 timestamp_ns, IntPtr track_info);
  481. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  482. public static extern int xdevc_ctrl_get_touchpad_state(NativeExHandle controller_handle, IntPtr state);
  483. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  484. public static extern int xdevc_ctrl_get_controller_state(NativeExHandle controller_handle, IntPtr state);
  485. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  486. public static extern int xdevc_ctrl_get_track_id(NativeExHandle controller_handle);
  487. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  488. public static extern int xdevc_ctrl_get_device_type(NativeExHandle controller_handle);
  489. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  490. public static extern string xdevc_ctrl_get_device_address(NativeExHandle controller_handle);
  491. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  492. public static extern string xdevc_ctrl_get_device_name(NativeExHandle controller_handle);
  493. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  494. public static extern string xdevc_ctrl_get_software_revision(NativeExHandle controller_handle);
  495. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  496. public static extern string xdevc_ctrl_get_hardware_revision(NativeExHandle controller_handle);
  497. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  498. public static extern string xdevc_ctrl_get_serial_number(NativeExHandle controller_handle);
  499. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  500. public static extern string xdevc_ctrl_get_model_name(NativeExHandle controller_handle);
  501. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  502. public static extern string xdevc_ctrl_get_manufacture_name(NativeExHandle controller_handle);
  503. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  504. public static extern int xdevc_ctrl_connect(NativeExHandle controller_handle);
  505. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  506. public static extern int xdevc_ctrl_connect_to_type(NativeExHandle controller_handle, int type, bool force);
  507. /**
  508. * @brief
  509. *
  510. * @param controller_handle
  511. * @param mac MAC string of controller, e.g. 01:09:0B:09:0E:39
  512. * @param force
  513. * @return XIM_API
  514. */
  515. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  516. public static extern int xdevc_ctrl_connect_to_address(NativeExHandle controller_handle, string mac, bool force);
  517. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  518. public static extern int xdevc_ctrl_connect_to_rfid(NativeExHandle controller_handle, int rfid, bool force);
  519. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  520. public static extern int xdevc_ctrl_connect_to_rfid_pattern(NativeExHandle controller_handle, int rfid_pattern, bool force);
  521. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  522. public static extern int xdevc_ctrl_disconnect(NativeExHandle controller_handle);
  523. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  524. public static extern int xdevc_ctrl_confirm_pair(NativeExHandle controller_handle);
  525. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  526. public static extern int xdevc_ctrl_hold_connection(NativeExHandle controller_handle, int hold_time_sec);
  527. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  528. public static extern int xdevc_ctrl_unbind(NativeExHandle controller_handle);
  529. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  530. public static extern int xdevc_ctrl_vibrate(NativeExHandle controller_handle, int strengh_percentage, int dur_ms);
  531. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  532. public static extern int xdevc_ctrl_tx_message(NativeExHandle controller_handle, int uuid, int msg_size, IntPtr message);
  533. #endregion Controller API
  534. #region Others
  535. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  536. public static extern int xdevc_start_vio_service();
  537. [DllImport(pluginName, CallingConvention = CallingConvention.Cdecl)]
  538. public static extern int xdevc_stop_vio_service();
  539. #endregion Others
  540. public static XDeviceClientEventDelegate _xDeviceClientEventDelegate = null;
  541. public void RegisterXEventCallbackDelegate(XDeviceClientEventDelegate dlg)
  542. {
  543. _xDeviceClientEventDelegate = dlg;
  544. xdevc_set_events_cb(dlg);
  545. }
  546. public void UnregisterXEventCallbackDelegate()
  547. {
  548. xdevc_set_events_cb(null);
  549. _xDeviceClientEventDelegate = null;
  550. }
  551. }
  552. }