cli_ps.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * \file cli_ps.c
  3. *
  4. * This File contains the "Persistent Storage" handlers for the CLI application,
  5. * to exercise various functionalities of the Mindtree Mesh stack.
  6. */
  7. /*
  8. * Copyright (C) 2017. Mindtree Ltd.
  9. * All rights reserved.
  10. */
  11. /* ------------------------------- Header File Inclusion */
  12. #include "cli_main.h"
  13. /* ------------------------------- Global Variables */
  14. /* Level - Persistent Storage */
  15. DECL_STATIC CLI_COMMAND cli_ps_cmd_list[] =
  16. {
  17. /* Help */
  18. { "help", "Help", cli_help },
  19. /* Get Device Key */
  20. { "getdevkey", "Get Device Key", cli_ps_get_device_key },
  21. /* Get App Key */
  22. { "getappkey", "Get App Key", cli_ps_get_app_key },
  23. /* Get Network Key */
  24. { "getnetkey", "Get Network Key", cli_ps_get_net_key },
  25. /* Get Primary Unicast Address */
  26. { "getprimunicastaddr", "Get Primary Unicast Address", cli_ps_get_primary_unicast_addr },
  27. /* Back */
  28. { "back", "One Level Up", cli_back },
  29. /* Root */
  30. { "root", "Back to Root", cli_root }
  31. };
  32. /* ------------------------------- Functions */
  33. /* Persistent Storage */
  34. API_RESULT cli_ps(UINT32 argc, UCHAR *argv[])
  35. {
  36. CONSOLE_OUT("In Persistent Storage\n");
  37. cli_cmd_stack_push(cli_ps_cmd_list, sizeof(cli_ps_cmd_list) / sizeof(CLI_COMMAND));
  38. cli_help(argc, argv);
  39. return API_SUCCESS;
  40. }
  41. /* Get Device Key */
  42. API_RESULT cli_ps_get_device_key(UINT32 argc, UCHAR *argv[])
  43. {
  44. UINT8 index;
  45. UINT8 * key;
  46. API_RESULT retval;
  47. CONSOLE_OUT("In PS Get Device Key\n");
  48. if (1 != argc)
  49. {
  50. CONSOLE_OUT("Usage: getdevkey <Device Key Index>\n");
  51. return API_FAILURE;
  52. }
  53. index = (UINT8)CLI_strtoi(argv[0], (UINT8)CLI_strlen(argv[0]), 16);
  54. retval = MS_access_cm_get_device_key
  55. (
  56. index,
  57. &key
  58. );
  59. /* Check Retval. Print Device Key */
  60. if (API_SUCCESS == retval)
  61. {
  62. CONSOLE_OUT("Device Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
  63. index, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
  64. key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);
  65. }
  66. else
  67. {
  68. CONSOLE_OUT("FAILED. Reason: 0x%04X\n", retval);
  69. }
  70. return API_SUCCESS;
  71. }
  72. /* Get App Key */
  73. API_RESULT cli_ps_get_app_key(UINT32 argc, UCHAR *argv[])
  74. {
  75. MS_APPKEY_HANDLE handle;
  76. UINT8 * key;
  77. UINT8 aid;
  78. API_RESULT retval;
  79. CONSOLE_OUT("In PS Get App Key\n");
  80. if (1 != argc)
  81. {
  82. CONSOLE_OUT("Usage: getappkey <App Key Handle>\n");
  83. return API_FAILURE;
  84. }
  85. handle = (MS_APPKEY_HANDLE)CLI_strtoi(argv[0], (UINT16)CLI_strlen(argv[0]), 16);
  86. retval = MS_access_cm_get_app_key
  87. (
  88. handle,
  89. &key,
  90. &aid
  91. );
  92. /* Check Retval. Print App Key */
  93. if (API_SUCCESS == retval)
  94. {
  95. CONSOLE_OUT("App Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
  96. handle, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
  97. key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);
  98. }
  99. else
  100. {
  101. CONSOLE_OUT("FAILED. Reason: 0x%04X\n", retval);
  102. }
  103. return API_SUCCESS;
  104. }
  105. /* Get Network Key */
  106. API_RESULT cli_ps_get_net_key(UINT32 argc, UCHAR *argv[])
  107. {
  108. MS_SUBNET_HANDLE handle;
  109. UINT8 key[16];
  110. API_RESULT retval;
  111. CONSOLE_OUT("In PS Get Network Key\n");
  112. if (1 != argc)
  113. {
  114. CONSOLE_OUT("Usage: getnetkey <Subnet Handle>\n");
  115. return API_FAILURE;
  116. }
  117. handle = (MS_SUBNET_HANDLE)CLI_strtoi(argv[0], (UINT16)CLI_strlen(argv[0]), 16);
  118. retval = MS_access_cm_get_netkey_at_offset
  119. (
  120. handle,
  121. 0,
  122. key
  123. );
  124. /* Check Retval. Print Network Key */
  125. if (API_SUCCESS == retval)
  126. {
  127. CONSOLE_OUT("Net Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
  128. handle, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
  129. key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);
  130. }
  131. else
  132. {
  133. CONSOLE_OUT("FAILED. Reason: 0x%04X\n", retval);
  134. }
  135. return API_SUCCESS;
  136. }
  137. /* Get Primary Unicast Address */
  138. API_RESULT cli_ps_get_primary_unicast_addr(UINT32 argc, UCHAR *argv[])
  139. {
  140. MS_NET_ADDR addr;
  141. API_RESULT retval;
  142. CONSOLE_OUT("In PS Get Primary Unicast Address\n");
  143. retval = MS_access_cm_get_primary_unicast_address(&addr);
  144. /* Check Retval. Print Primary Unicast Address */
  145. if (API_SUCCESS == retval)
  146. {
  147. CONSOLE_OUT("Primary Unicast Address: 0x%04X\n", addr);
  148. }
  149. else
  150. {
  151. CONSOLE_OUT("FAILED. Reason: 0x%04X\n", retval);
  152. }
  153. return API_SUCCESS;
  154. }