tree: a4b05c63c4e197fb622e80fefcc4721459e7f5d9 [path history] [tgz]
  1. fingerprint/
  2. pwm/
  3. thermal/
  4. add_entropy_command.cc
  5. add_entropy_command.h
  6. add_entropy_command_test.cc
  7. BUILD.gn
  8. charge_control_set_command.h
  9. charge_control_set_command_test.cc
  10. charge_current_limit_set_command.cc
  11. charge_current_limit_set_command.h
  12. charge_current_limit_set_command_test.cc
  13. device_event_command.cc
  14. device_event_command.h
  15. device_event_command_test.cc
  16. display_soc_command.cc
  17. display_soc_command.h
  18. display_soc_command_test.cc
  19. ec_command.h
  20. ec_command_async.h
  21. ec_command_async_test.cc
  22. ec_command_factory.cc
  23. ec_command_factory.h
  24. ec_command_test.cc
  25. ec_panicinfo.cc
  26. ec_panicinfo.h
  27. ec_panicinfo_fuzzer.cc
  28. ec_panicinfo_test.cc
  29. ec_usb_device_monitor.cc
  30. ec_usb_device_monitor.h
  31. ec_usb_device_monitor_test.cc
  32. ec_usb_endpoint.cc
  33. ec_usb_endpoint.h
  34. ec_usb_endpoint_test.cc
  35. flash_info_command.cc
  36. flash_info_command.h
  37. flash_info_command_test.cc
  38. flash_info_params.cc
  39. flash_info_params.h
  40. flash_info_params_test.cc
  41. flash_protect_command.cc
  42. flash_protect_command.h
  43. flash_protect_command_factory.cc
  44. flash_protect_command_factory.h
  45. flash_protect_command_factory_test.cc
  46. flash_protect_command_test.cc
  47. flash_spi_info_command.cc
  48. flash_spi_info_command.h
  49. flash_spi_info_command_test.cc
  50. get_chip_info_command.cc
  51. get_chip_info_command.h
  52. get_chip_info_command_test.cc
  53. get_comms_status_command.cc
  54. get_comms_status_command.h
  55. get_comms_status_command_test.cc
  56. get_features_command.cc
  57. get_features_command.h
  58. get_features_command_test.cc
  59. get_mkbp_wake_mask_command.cc
  60. get_mkbp_wake_mask_command.h
  61. get_mkbp_wake_mask_command_test.cc
  62. get_protocol_info_command.cc
  63. get_protocol_info_command.h
  64. get_protocol_info_command_test.cc
  65. get_version_command.cc
  66. get_version_command.h
  67. get_version_command_test.cc
  68. hello_command.cc
  69. hello_command.h
  70. hello_command_test.cc
  71. i2c_passthru_command.cc
  72. i2c_passthru_command.h
  73. i2c_passthru_command_test.cc
  74. i2c_passthru_params.h
  75. i2c_passthru_params_test.cc
  76. i2c_read_command.cc
  77. i2c_read_command.h
  78. i2c_read_command_test.cc
  79. led_control_command.cc
  80. led_control_command.h
  81. led_control_command_test.cc
  82. libusb_wrapper.h
  83. mkbp_event.cc
  84. mkbp_event.h
  85. mock_ec_command_factory.h
  86. motion_sense_command.h
  87. motion_sense_command_lid_angle.cc
  88. motion_sense_command_lid_angle.h
  89. motion_sense_command_lid_angle_test.cc
  90. OWNERS
  91. pd_chip_info_command.h
  92. pd_chip_info_command_test.cc
  93. rand_num_command.cc
  94. rand_num_command.h
  95. rand_num_command_test.cc
  96. rand_num_params.h
  97. rand_num_params_test.cc
  98. read_memmap_command.h
  99. read_memmap_command_test.cc
  100. README.md
  101. reboot_command.h
  102. reboot_command_test.cc
  103. reboot_ec_command.cc
  104. reboot_ec_command.h
  105. reboot_ec_command_test.cc
  106. rgb_keyboard_command.h
  107. rgb_keyboard_command_test.cc
  108. rgb_keyboard_params.h
  109. rollback_info_command.cc
  110. rollback_info_command.h
  111. rollback_info_command_test.cc
  112. rwsig_action_command.h
  113. rwsig_action_command_test.cc
  114. set_force_lid_open_command.cc
  115. set_force_lid_open_command.h
  116. set_force_lid_open_command_test.cc
  117. set_mkbp_wake_mask_command.cc
  118. set_mkbp_wake_mask_command.h
  119. set_mkbp_wake_mask_command_test.cc
  120. smart_discharge_command.cc
  121. smart_discharge_command.h
  122. smart_discharge_command_test.cc
  123. versions_command.cc
  124. versions_command.h
  125. versions_command_test.cc
libec/README.md

libec

C++ library for interacting with the Chrome OS Embedded Controller.

Overview

Each EC command is represented with a C++ class that conforms to the EcCommandInterface interface. All EC commands are either synchronous or “asynchronous”. Asynchronous EC commands are commands that do not complete immediately and require polling to get the result. Most EC commands are synchronous.

Synchronous commands should inherit from the EcCommand class. Asynchronous commands should inherit from the EcCommandAsync class.

Command Versions

Each EC command has a version number associated with it, which allows introducing new versions of commands while maintaining backward compatibility with older ECs that cannot be changed.

In order to abstract this complexity for consumers of libec, libec provides a factory class called EcCommandFactory. EcCommandFactory can be used to automatically instantiate the correct version of the command based on the command version that the EC supports.

For example, EcCommandFactory::FpContextCommand uses FpContextCommandFactory::Create to instantiate version 0 or version 1 of the EC_CMD_FP_CONTEXT command, depending on what the EC supports.

Testing

All commands have a unit test with a name of <command>_test.cc. It's possible to mock the response from the EC by mocking the EcCommand::Resp() method. For a simple example, see the DisplayStateOfChargeCommandTest. For an example of more advanced testing you can do with mocking, see the FpFrameCommandTest.

Adding New Commands

To add a new command, create files for your command called <command>.cc and <command.h>, as well as a test file <command>_test.cc. Create a Command class that inherits from EcCommand or EcCommandAsync. The template arguments are the params and response structs for the command. For example, the EC_CMD_DEVICE_EVENT command uses struct ec_params_device_event for parameters and struct ec_response_device_event for the response, so the DeviceEventCommand inherits from EcCommand<struct ec_params_device_event, struct response_device_event>.

Commands that don't have a request or response use EmptyParam as the template parameter. See the DisplayStateOfChargeCommand for an example.

Commands that have variable length arrays in the params or response struct need to redefine the struct so that it has a well-defined size. See fp_template_params.h for an example. Please include a test to validate that the two structs do not get out of sync. See fp_template_params_test.cc for an example.