Source: index.js

  1. /**
  2. * Media transfer protocol module for Node.js
  3. * @module mtp
  4. */
  5. /*jslint node:true */
  6. 'use strict';
  7. // Require
  8. var ffi = require('ffi');
  9. var ref = require('ref');
  10. var struct = require('./struct');
  11. var pointer = require('./pointer');
  12. var constants = require('./constants');
  13. var LIBMTP_raw_device_structPtr = ref.refType(struct.LIBMTP_raw_device_struct);
  14. var LIBMTP_raw_device_structPtrPtr = ref.refType(LIBMTP_raw_device_structPtr);
  15. /**
  16. * The callback type definition. Notice that a progress percentage ratio
  17. * is easy to calculate by dividing <code>sent</code> by
  18. * <code>total</code>.
  19. * @method
  20. * @param {uint64} sent the number of bytes sent so far
  21. * @param {uint64} total the total number of bytes to send
  22. * @param {voidPtr} data a user-defined dereferencable pointer
  23. * @return {int} if anything else than 0 is returned, the current transfer will be interrupted / cancelled.
  24. */
  25. var LIBMTP_progressfunc_t = ffi.Function('int', ['uint64', 'uint64', ref.refType('void')]);
  26. // libmtp definition
  27. var mtp = new ffi.Library('libmtp', {
  28. // 'Function name': [ 'return_type', [ 'param1', 'param2'] ]
  29. /**
  30. * LIBMTP_Init
  31. * @method
  32. * @returns {void}
  33. */
  34. 'LIBMTP_Init':
  35. ['void', []],
  36. /**
  37. * LIBMTP_Set_Debug
  38. * @method
  39. * @param {int}
  40. * @returns {void}
  41. */
  42. 'LIBMTP_Set_Debug':
  43. ['void', ['int']],
  44. /**
  45. * LIBMTP_Detect_Raw_Devices
  46. * @method
  47. * @param {LIBMTP_raw_device_structPtrPtr}
  48. * @param {intPtr}
  49. * @returns {int}
  50. */
  51. 'LIBMTP_Detect_Raw_Devices':
  52. ['int', [LIBMTP_raw_device_structPtrPtr, pointer.intPtr]],
  53. /**
  54. * LIBMTP_Open_Raw_Device
  55. * @method
  56. * @param {LIBMTP_raw_device_structPtr}
  57. * @returns {LIBMTP_mtpdevice_structPtr}
  58. */
  59. 'LIBMTP_Open_Raw_Device':
  60. [ref.refType(struct.LIBMTP_mtpdevice_struct), [LIBMTP_raw_device_structPtr]],
  61. /**
  62. * LIBMTP_Dump_Errorstack
  63. * @method
  64. * @param {LIBMTP_mtpdevice_structPtr}
  65. * @returns {void}
  66. */
  67. 'LIBMTP_Dump_Errorstack':
  68. ['void', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  69. /**
  70. * LIBMTP_Clear_Errorstack
  71. * @method
  72. * @param {LIBMTP_mtpdevice_structPtr}
  73. * @returns {void}
  74. */
  75. 'LIBMTP_Clear_Errorstack':
  76. ['void', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  77. /**
  78. * LIBMTP_Dump_Device_Info'
  79. * @method
  80. * @param {LIBMTP_mtpdevice_structPtr}
  81. * @returns {void}
  82. */
  83. 'LIBMTP_Dump_Device_Info':
  84. ['void', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  85. /**
  86. * LIBMTP_Get_Friendlyname
  87. * @method
  88. * @param {LIBMTP_mtpdevice_structPtr}
  89. * @returns {string}
  90. */
  91. 'LIBMTP_Get_Friendlyname':
  92. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  93. /**
  94. * LIBMTP_Get_Manufacturername
  95. * @method
  96. * @param {LIBMTP_mtpdevice_structPtr}
  97. * @returns {string}
  98. */
  99. 'LIBMTP_Get_Manufacturername':
  100. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  101. /**
  102. * LIBMTP_Get_Modelname
  103. * @method
  104. * @param {LIBMTP_mtpdevice_structPtr}
  105. * @returns {string}
  106. */
  107. 'LIBMTP_Get_Modelname':
  108. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  109. /**
  110. * LIBMTP_Get_Serialnumber
  111. * @method
  112. * @param {LIBMTP_mtpdevice_structPtr}
  113. * @returns {string}
  114. */
  115. 'LIBMTP_Get_Serialnumber':
  116. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  117. /**
  118. * LIBMTP_Get_Deviceversion
  119. * @method
  120. * @param {LIBMTP_mtpdevice_structPtr}
  121. * @returns {string}
  122. */
  123. 'LIBMTP_Get_Deviceversion':
  124. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  125. /**
  126. * LIBMTP_Get_Syncpartner
  127. * @method
  128. * @param {LIBMTP_mtpdevice_structPtr}
  129. * @returns {string}
  130. */
  131. 'LIBMTP_Get_Syncpartner':
  132. ['string', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  133. /**
  134. * LIBMTP_Get_Batterylevel
  135. * @method
  136. * @param {LIBMTP_mtpdevice_structPtr}
  137. * @param {uint8Ptr}
  138. * @param {uint8Ptr}
  139. * @returns {int}
  140. */
  141. 'LIBMTP_Get_Batterylevel':
  142. ['int', [ref.refType(struct.LIBMTP_mtpdevice_struct), pointer.uint8Ptr, pointer.uint8Ptr]],
  143. /**
  144. * LIBMTP_Get_Supported_Filetypes
  145. * @method
  146. * @param {LIBMTP_mtpdevice_structPtr}
  147. * @param {uint16PtrPtr}
  148. * @param {uint16Ptr}
  149. * @returns {int}
  150. */
  151. 'LIBMTP_Get_Supported_Filetypes':
  152. ['int', [ref.refType(struct.LIBMTP_mtpdevice_struct), pointer.uint16PtrPtr, pointer.uint16Ptr]],
  153. /**
  154. * LIBMTP_Get_Filetype_Description
  155. * @method
  156. * @param {int}
  157. * @returns {string}
  158. */
  159. 'LIBMTP_Get_Filetype_Description':
  160. ['string', [ 'int' ]],
  161. /**
  162. * LIBMTP_Get_Secure_Time
  163. * Crashes Samsung Galaxy S5.
  164. * @method
  165. * @param {LIBMTP_mtpdevice_structPtr}
  166. * @param {stringPtr}
  167. * @returns {int}
  168. */
  169. 'LIBMTP_Get_Secure_Time':
  170. ['int', [ref.refType(struct.LIBMTP_mtpdevice_struct), ref.refType('string') ]],
  171. /**
  172. * LIBMTP_Get_Device_Certificate
  173. * @method
  174. * @param {LIBMTP_mtpdevice_structPtr}
  175. * @param {stringPtr}
  176. * @returns {int}
  177. */
  178. 'LIBMTP_Get_Device_Certificate':
  179. ['int', [ref.refType(struct.LIBMTP_mtpdevice_struct), ref.refType('string')]],
  180. /**
  181. * LIBMTP_Release_Device
  182. * @method
  183. * @param {LIBMTP_mtpdevice_structPtr}
  184. * @returns {void}
  185. */
  186. 'LIBMTP_Release_Device':
  187. ['void', [ref.refType(struct.LIBMTP_mtpdevice_struct)]],
  188. /**
  189. * LIBMTP_Get_Filelisting_With_Callback
  190. * @method
  191. * @param {LIBMTP_mtpdevice_structPtr}
  192. * @param {LIBMTP_progressfunc_t}
  193. * @param {voidPtr}
  194. * @returns {LIBMTP_file_structPtr}
  195. */
  196. 'LIBMTP_Get_Filelisting_With_Callback':
  197. [ref.refType(struct.LIBMTP_file_struct),
  198. [ref.refType(struct.LIBMTP_mtpdevice_struct), LIBMTP_progressfunc_t, ref.refType('void')]],
  199. /**
  200. * LIBMTP_Get_Track_To_File_Descriptor
  201. * @method
  202. * @param {LIBMTP_mtpdevice_structPtr}
  203. * @param {uint32}
  204. * @param {int}
  205. * @param {LIBMTP_progressfunc_t}
  206. * @param {voidPtr}
  207. * @returns {int}
  208. */
  209. 'LIBMTP_Get_Track_To_File_Descriptor':
  210. ['int',
  211. [ref.refType(struct.LIBMTP_mtpdevice_struct), 'uint32', 'int', LIBMTP_progressfunc_t, ref.refType('void')]]
  212. });
  213. // Set structs
  214. Object.keys(struct).forEach(function (structName) {
  215. mtp[structName] = struct[structName];
  216. });
  217. // Set pointers
  218. Object.keys(pointer).forEach(function (pointerName) {
  219. mtp[pointerName] = pointer[pointerName];
  220. });
  221. mtp.LIBMTP_raw_device_structPtr = LIBMTP_raw_device_structPtr;
  222. mtp.LIBMTP_raw_device_structPtrPtr = LIBMTP_raw_device_structPtrPtr;
  223. // Set constants
  224. constants(mtp);
  225. module.exports = mtp;