luat_lib_usbapp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /*
  22. @module usbapp
  23. @summary USB功能操作
  24. @version 1.0
  25. @date 2022.01.17
  26. */
  27. #include "luat_base.h"
  28. #include "luat_msgbus.h"
  29. #include "app_interface.h"
  30. static int l_usb_app_vhid_cb(lua_State *L, void* ptr) {
  31. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  32. lua_getglobal(L, "sys_pub");
  33. if (lua_isfunction(L, -1)) {
  34. lua_pushstring(L, "USB_HID_INC");
  35. lua_pushinteger(L, msg->arg1);
  36. lua_call(L, 2, 0);
  37. }
  38. return 0;
  39. }
  40. int32_t luat_usb_app_vhid_cb(void *pData, void *pParam)
  41. {
  42. //在这里把底层的HID消息转换为LUAT消息并上传,同时删除本注释
  43. rtos_msg_t msg;
  44. msg.handler = l_usb_app_vhid_cb;
  45. switch((uint32_t)pParam)
  46. {
  47. case USB_HID_NOT_READY:
  48. case USB_HID_READY:
  49. case USB_HID_SEND_DONE:
  50. msg.arg1 = (uint32_t)pParam;
  51. luat_msgbus_put(&msg, 0);
  52. break;
  53. }
  54. return 0;
  55. }
  56. /*
  57. 启动USB设备
  58. @api usbapp.start(id)
  59. @int 设备id,默认为0
  60. @return bool 成功返回true,否则返回false
  61. @usage
  62. -- 启动USB
  63. usbapp.start(0)
  64. */
  65. static int l_usb_start(lua_State* L) {
  66. luat_usb_app_start(USB_ID0);
  67. lua_pushboolean(L, 1);
  68. return 1;
  69. }
  70. /*
  71. 关闭USB设备
  72. @api usbapp.stop(id)
  73. @int 设备id,默认为0
  74. @return bool 成功返回true,否则返回false
  75. @usage
  76. -- 关闭USB
  77. usbapp.stop(0)
  78. */
  79. static int l_usb_stop(lua_State* L) {
  80. luat_usb_app_stop(USB_ID0);
  81. lua_pushboolean(L, 1);
  82. return 1;
  83. }
  84. /*
  85. USB HID设备上传数据
  86. @api usbapp.vhid_upload(id, data)
  87. @int 设备id,默认为0
  88. @string 数据. 注意, HID的可用字符是有限制的, 基本上只有可见字符是支持的, 不支持的字符会替换为空格.
  89. @return bool 成功返回true,否则返回false
  90. @usage
  91. -- HID上传数据
  92. usbapp.vhid_upload(0, "1234") -- usb hid会模拟敲出1234
  93. */
  94. static int l_usb_vhid_upload(lua_State* L) {
  95. size_t len;
  96. const char* data = luaL_checklstring(L, 2, &len);
  97. if (len > 0) {
  98. luat_usb_app_vhid_upload(USB_ID0, data, len);
  99. lua_pushboolean(L, 1);
  100. }
  101. else {
  102. lua_pushboolean(L, 0);
  103. }
  104. return 1;
  105. }
  106. /*
  107. USB HID设备取消上传数据
  108. @api usbapp.vhid_cancel_upload(id)
  109. @int 设备id,默认为0
  110. @return nil 无返回值
  111. @usage
  112. -- 取消上传数据,通常不需要
  113. usbapp.vhid_cancel_upload(0)
  114. */
  115. static int l_usb_vhid_cancel_upload(lua_State* L) {
  116. luat_usb_app_vhid_cancel_upload(USB_ID0);
  117. return 0;
  118. }
  119. #include "rotable.h"
  120. static const rotable_Reg reg_usbapp[] =
  121. {
  122. { "start", l_usb_start, 0},
  123. { "start", l_usb_stop, 0},
  124. { "vhid_upload", l_usb_vhid_upload, 0},
  125. { "vhid_cancel_upload", l_usb_vhid_cancel_upload, 0},
  126. { "HID_NOT_READY", NULL, USB_HID_NOT_READY},
  127. { "HID_READY", NULL, USB_HID_READY},
  128. { "HID_SEND_DONE", NULL, USB_HID_SEND_DONE},
  129. { NULL, NULL, 0}
  130. };
  131. LUAMOD_API int luaopen_usbapp( lua_State *L ) {
  132. luat_newlib(L, reg_usbapp);
  133. return 1;
  134. }