You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

134 lines
4.0 KiB

  1. /** @page primercommand Command Development Primer
  2. This page provides a primer for writing commands by introducing @c hello
  3. module. The full source code used in this example can be found in
  4. hello.c, and the @ref primercmdcode section shows how to use it.
  5. A summary of this information can be found in @ref helpercommand .
  6. @section primercmdhandler Command Handlers
  7. Defining new commands and their helpers is easy. The following code
  8. defines a simple command handler that delegates its argument parsing:
  9. @code
  10. COMMAND_HANDLER(handle_hello_command)
  11. {
  12. const char *sep, *name;
  13. int retval = CALL_COMMAND_HANDLER(handle_hello_args);
  14. if (ERROR_OK == retval)
  15. command_print(CMD_CTX, "Greetings%s%s!", sep, name);
  16. return retval;
  17. }
  18. @endcode
  19. Here, the @c COMMAND_HANDLER macro establishes the function signature,
  20. see in command.h by the @c __COMMAND_HANDLER macro.
  21. The COMMAND_HELPER macro function allows defining functions with an
  22. extended version of the base signature. These helper functions can be
  23. called (with the appropriate parameters), the @c CALL_COMMAND_HANDLER
  24. macro to pass any e as parameters to the following helper function:
  25. The subsequent blocks of code are a normal C function that can do
  26. anything, so only complex commands deserve should use comamnd helper
  27. functions. In this respect, this example uses one to demonstrate how --
  28. not when -- they should be used.
  29. @code
  30. static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
  31. {
  32. if (argc > 1)
  33. {
  34. LOG_ERROR("%s: too many arguments", CMD_NAME);
  35. return ERROR_COMMAND_SYNTAX_ERROR;
  36. }
  37. if (1 == CMD_ARGC)
  38. {
  39. *sep = ", ";
  40. *name = CMD_ARGV[0];
  41. }
  42. else
  43. *sep = *name = "";
  44. return ERROR_OK;
  45. }
  46. @endcode
  47. Of course, you may also call other macros or functions, but that extends
  48. beyond the scope of this tutorial on writing commands.
  49. @section primercmdreg Command Registration
  50. Before this new function can be used, it must be registered somehow.
  51. For a new module, registering should be done in a new function for
  52. the purpose, which must be called from @c openocd.c:
  53. @code
  54. static const struct command_registration hello_command_handlers[] = {
  55. {
  56. .name = "hello",
  57. .mode = COMMAND_ANY,
  58. .handler = &handle_hello_command,
  59. .help = "print a warm greetings",
  60. .usage = "[<name>]",
  61. },
  62. {
  63. .chain = foo_command_handlers,
  64. }
  65. COMMAND_REGISTRATION_DONE
  66. };
  67. int hello_register_commands(struct command_context_s *cmd_ctx)
  68. {
  69. return register_commands(cmd_ctx, NULL, handle_command_handlers);
  70. }
  71. @endcode
  72. That's it! The command should now be registered and avaiable to scripts.
  73. @section primercmdchain Command Chaining
  74. This example also shows how to chain command handler registration, so
  75. your modules can "inherit" commands provided by other (sub)modules.
  76. Here, the hello module includes the foo commands in the same context
  77. that the 'hello' command will be registered.
  78. If the @c chain field had been put in the 'hello' command, then the
  79. @c foo module commands would be registered under it. Indeed, that
  80. technique is used to define the 'foo bar' and 'foo baz' commands,
  81. as well as for the example drivers that use these modules.
  82. The code for the 'foo' command handlers can be found in @c hello.c.
  83. @section primercmdcode Trying These Example Commands
  84. These commands have been inherited by the dummy interface, faux flash,
  85. and testee target drivers. The easiest way to test these is by using the
  86. dummy interface.
  87. Once OpenOCD has been built with this example code, the following command
  88. demonstrates the abilities that the @c hello module provides:
  89. @code
  90. openocd -c 'interface dummy' \
  91. -c 'dummy hello' \
  92. -c 'dummy hello World' \
  93. -c 'dummy hello {John Doe}' \
  94. -c 'dummy hello John Doe' # error: too many arguments
  95. @endcode
  96. If saved in @c hello.cfg, then running <code>openocd -f hello.cfg</code>
  97. should produce the following output before displaying the help text and
  98. exiting:
  99. @code
  100. Greetings!
  101. Greetings, World!
  102. Greetings, John Doe!
  103. Error: hello: too many arguments
  104. Runtime error, file "openocd.cfg", line 14:
  105. hello: too many arguments
  106. dummy hello [<name>]
  107. prints a warm welcome
  108. @endcode
  109. */