cli_commands.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "cli_commands.h"
  2. #include <api-hal.h>
  3. #include <rtc.h>
  4. void cli_command_help(string_t args, void* context) {
  5. (void)args;
  6. Cli* cli = context;
  7. printf("Commands we have:");
  8. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  9. CliCommandDict_it_t it;
  10. for(CliCommandDict_it(it, cli->commands); !CliCommandDict_end_p(it); CliCommandDict_next(it)) {
  11. CliCommandDict_itref_t* ref = CliCommandDict_ref(it);
  12. printf(" ");
  13. printf(string_get_cstr(ref->key));
  14. };
  15. furi_check(osMutexRelease(cli->mutex) == osOK);
  16. if(string_size(args) > 0) {
  17. cli_nl();
  18. printf("Also I have no clue what '");
  19. printf(string_get_cstr(args));
  20. printf("' is.");
  21. }
  22. }
  23. void cli_command_version(string_t args, void* context) {
  24. (void)args;
  25. (void)context;
  26. cli_print_version();
  27. }
  28. void cli_command_uuid(string_t args, void* context) {
  29. (void)args;
  30. (void)context;
  31. size_t uid_size = api_hal_uid_size();
  32. const uint8_t* uid = api_hal_uid();
  33. string_t byte_str;
  34. string_init(byte_str);
  35. string_cat_printf(byte_str, "UID:");
  36. for(size_t i = 0; i < uid_size; i++) {
  37. uint8_t uid_byte = uid[i];
  38. string_cat_printf(byte_str, "%02X", uid_byte);
  39. }
  40. printf(string_get_cstr(byte_str));
  41. }
  42. void cli_command_date(string_t args, void* context) {
  43. RTC_DateTypeDef date;
  44. RTC_TimeTypeDef time;
  45. // TODO add get_datetime to core, not use HAL here
  46. // READ ORDER MATTERS! Time then date.
  47. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  48. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  49. string_t datetime_str;
  50. string_init(datetime_str);
  51. string_cat_printf(datetime_str, "%.2d:%.2d:%.2d ", time.Hours, time.Minutes, time.Seconds);
  52. string_cat_printf(datetime_str, "%.2d-%.2d-%.2d", date.Month, date.Date, 2000 + date.Year);
  53. printf(string_get_cstr(datetime_str));
  54. string_clear(datetime_str);
  55. }
  56. void cli_command_log(string_t args, void* context) {
  57. Cli* cli = context;
  58. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  59. printf("Press any key to stop...\r\n");
  60. cli_getc(cli);
  61. furi_stdglue_set_global_stdout_callback(NULL);
  62. }
  63. void cli_command_vibro(string_t args, void* context) {
  64. if(!string_cmp(args, "0")) {
  65. api_hal_vibro_on(false);
  66. } else if(!string_cmp(args, "1")) {
  67. api_hal_vibro_on(true);
  68. } else {
  69. printf("Wrong input");
  70. }
  71. }
  72. void cli_command_led(string_t args, void* context) {
  73. // Get first word as light name
  74. Light light;
  75. string_t light_name;
  76. string_init(light_name);
  77. size_t ws = string_search_char(args, ' ');
  78. if(ws == STRING_FAILURE) {
  79. printf("Wrong input");
  80. string_clear(light_name);
  81. return;
  82. } else {
  83. string_set_n(light_name, args, 0, ws);
  84. string_right(args, ws);
  85. string_strim(args);
  86. }
  87. // Check light name
  88. if(!string_cmp(light_name, "r")) {
  89. light = LightRed;
  90. } else if(!string_cmp(light_name, "g")) {
  91. light = LightGreen;
  92. } else if(!string_cmp(light_name, "b")) {
  93. light = LightBlue;
  94. } else if(!string_cmp(light_name, "bl")) {
  95. light = LightBacklight;
  96. } else {
  97. printf("Wrong argument");
  98. string_clear(light_name);
  99. return;
  100. }
  101. string_clear(light_name);
  102. // Read light value from the rest of the string
  103. char* end_ptr;
  104. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  105. if(!(value < 256 && *end_ptr == '\0')) {
  106. printf("Wrong argument");
  107. return;
  108. }
  109. api_hal_light_set(light, value);
  110. }
  111. void cli_commands_init(Cli* cli) {
  112. cli_add_command(cli, "help", cli_command_help, cli);
  113. cli_add_command(cli, "?", cli_command_help, cli);
  114. cli_add_command(cli, "version", cli_command_version, cli);
  115. cli_add_command(cli, "!", cli_command_version, cli);
  116. cli_add_command(cli, "uid", cli_command_uuid, cli);
  117. cli_add_command(cli, "date", cli_command_date, cli);
  118. cli_add_command(cli, "log", cli_command_log, cli);
  119. cli_add_command(cli, "vibro", cli_command_vibro, cli);
  120. cli_add_command(cli, "led", cli_command_led, cli);
  121. }