hid_host.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * hid_host.c
  3. *
  4. * Created on: Dec 20, 2012
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2013, hathach (tinyusb.org)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tiny usb stack.
  35. */
  36. #include "tusb_option.h"
  37. #if (MODE_HOST_SUPPORTED && defined HOST_CLASS_HID)
  38. #define _TINY_USB_SOURCE_FILE_
  39. //--------------------------------------------------------------------+
  40. // INCLUDE
  41. //--------------------------------------------------------------------+
  42. #include "common/common.h"
  43. #include "hid_host.h"
  44. #if TUSB_CFG_HOST_HID_KEYBOARD
  45. //--------------------------------------------------------------------+
  46. // MACRO CONSTANT TYPEDEF
  47. //--------------------------------------------------------------------+
  48. //--------------------------------------------------------------------+
  49. // INTERNAL OBJECT & FUNCTION DECLARATION
  50. //--------------------------------------------------------------------+
  51. STATIC_ hidh_keyboard_info_t keyboard_data[TUSB_CFG_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1
  52. //--------------------------------------------------------------------+
  53. // PUBLIC API (Parameter Verification is required)
  54. //--------------------------------------------------------------------+
  55. tusb_error_t tusbh_hid_keyboard_get(uint8_t const dev_addr, uint8_t instance_num, tusb_keyboard_report_t * const report)
  56. {
  57. //------------- parameters validation -------------//
  58. ASSERT_INT(TUSB_DEVICE_STATE_CONFIGURED, tusbh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
  59. ASSERT_PTR(report, TUSB_ERROR_INVALID_PARA);
  60. ASSERT(instance_num < TUSB_CFG_HOST_HID_KEYBOARD_NO_INSTANCES_PER_DEVICE, TUSB_ERROR_INVALID_PARA);
  61. keyboard_interface_t *p_kbd;
  62. p_kbd = &keyboard_data[dev_addr-1].instance[instance_num];
  63. // TODO abtract class support for device
  64. ASSERT(0 != p_kbd->pipe_in.dev_addr, TUSB_ERROR_CLASS_DEVICE_DONT_SUPPORT);
  65. // TODO abtract to use hidh service
  66. ASSERT_STATUS( hcd_pipe_xfer(p_kbd->pipe_in, report, p_kbd->report_size, 1) ) ;
  67. return TUSB_ERROR_NONE;
  68. }
  69. uint8_t tusbh_hid_keyboard_no_instances(uint8_t const dev_addr)
  70. {
  71. ASSERT(tusbh_device_is_configured(dev_addr), 0);
  72. return keyboard_data[dev_addr-1].instance_count;
  73. }
  74. //--------------------------------------------------------------------+
  75. // CLASS-USBD API (don't require to verify parameters)
  76. //--------------------------------------------------------------------+
  77. void hidh_init(void)
  78. {
  79. #if TUSB_CFG_HOST_HID_KEYBOARD
  80. hidh_keyboard_init();
  81. #endif
  82. }
  83. void hidh_keyboard_init(void)
  84. {
  85. memclr_(&keyboard_data, sizeof(hidh_keyboard_info_t)*TUSB_CFG_HOST_DEVICE_MAX);
  86. }
  87. tusb_error_t hidh_keyboard_install(uint8_t const dev_addr, uint8_t const *descriptor)
  88. {
  89. keyboard_data[dev_addr-1].instance_count++;
  90. return TUSB_ERROR_NONE;
  91. }
  92. #endif
  93. #endif