cmd_server.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <inttypes.h>
  18. #include <lib/core/CHIPCore.h>
  19. #include <lib/shell/Commands.h>
  20. #include <lib/shell/Engine.h>
  21. #include <lib/shell/commands/Help.h>
  22. #include <lib/support/CHIPArgParser.hpp>
  23. #include <lib/support/CHIPMem.h>
  24. #include <lib/support/CodeUtils.h>
  25. #include <app/server/Server.h>
  26. #include <app/util/af.h>
  27. #include <app/util/attribute-storage.h>
  28. #include <credentials/examples/DeviceAttestationCredsExample.h>
  29. #include <transport/Session.h>
  30. using namespace chip;
  31. using namespace chip::Shell;
  32. using namespace chip::Credentials;
  33. using namespace chip::ArgParser;
  34. using namespace chip::Transport;
  35. // Anonymous namespace for file-scoped, static variables.
  36. namespace {
  37. chip::Shell::Engine sShellServerSubcommands;
  38. uint16_t sServerPortOperational = CHIP_PORT;
  39. uint16_t sServerPortCommissioning = CHIP_UDC_PORT;
  40. bool sServerEnabled = false;
  41. } // namespace
  42. static CHIP_ERROR CmdAppServerHelp(int argc, char ** argv)
  43. {
  44. sShellServerSubcommands.ForEachCommand(PrintCommandHelp, nullptr);
  45. return CHIP_NO_ERROR;
  46. }
  47. static CHIP_ERROR CmdAppServerStart(int argc, char ** argv)
  48. {
  49. // Init ZCL Data Model and CHIP App Server
  50. chip::Server::GetInstance().Init(nullptr, sServerPortOperational, sServerPortCommissioning);
  51. // Initialize device attestation config
  52. SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
  53. sServerEnabled = true;
  54. return CHIP_NO_ERROR;
  55. }
  56. static CHIP_ERROR CmdAppServerStop(int argc, char ** argv)
  57. {
  58. if (sServerEnabled == false)
  59. return CHIP_NO_ERROR;
  60. chip::Server::GetInstance().Shutdown();
  61. sServerEnabled = false;
  62. return CHIP_NO_ERROR;
  63. }
  64. static CHIP_ERROR CmdAppServerPort(int argc, char ** argv)
  65. {
  66. if (argc == 0)
  67. {
  68. streamer_printf(streamer_get(), "%d\r\n", sServerPortOperational);
  69. }
  70. else
  71. {
  72. bool success = ParseInt(argv[0], sServerPortOperational);
  73. if (!success)
  74. return CHIP_ERROR_INVALID_ARGUMENT;
  75. }
  76. return CHIP_NO_ERROR;
  77. }
  78. static CHIP_ERROR CmdAppServerUdcPort(int argc, char ** argv)
  79. {
  80. if (argc == 0)
  81. {
  82. streamer_printf(streamer_get(), "%d\r\n", sServerPortCommissioning);
  83. }
  84. else
  85. {
  86. bool success = ParseInt(argv[0], sServerPortCommissioning);
  87. if (!success)
  88. return CHIP_ERROR_INVALID_ARGUMENT;
  89. }
  90. return CHIP_NO_ERROR;
  91. }
  92. static bool PrintServerSession(void * context, SessionHandle & session)
  93. {
  94. switch (session->GetSessionType())
  95. {
  96. case Session::SessionType::kSecure: {
  97. SecureSession * secureSession = session->AsSecureSession();
  98. SecureSession::Type secureSessionType = secureSession->GetSecureSessionType();
  99. streamer_printf(
  100. streamer_get(), "session type=SECURE %s id=0x%04x peerSessionId=0x%04x peerNodeId=0x%016" PRIx64 " fabricIdx=%d\r\n",
  101. secureSessionType == SecureSession::Type::kCASE ? "CASE" : "PASE", secureSession->GetLocalSessionId(),
  102. secureSession->AsSecureSession()->GetPeerSessionId(), secureSession->GetPeerNodeId(), secureSession->GetFabricIndex());
  103. break;
  104. }
  105. case Session::SessionType::kUnauthenticated: {
  106. UnauthenticatedSession * unsecuredSession = session->AsUnauthenticatedSession();
  107. streamer_printf(streamer_get(), "session type=UNSECURED id=0x0000 peerNodeId=0x%016\r\n",
  108. unsecuredSession->GetPeerNodeId());
  109. break;
  110. }
  111. case Session::SessionType::kGroup: {
  112. GroupSession * groupSession = session->AsGroupSession();
  113. streamer_printf(streamer_get(), "session type=GROUP id=0x%04x fabricIdx=%d\r\n", groupSession->GetGroupId(),
  114. groupSession->GetFabricIndex());
  115. break;
  116. }
  117. default:
  118. streamer_printf(streamer_get(), "session type=UNDEFINED\r\n");
  119. }
  120. return true;
  121. }
  122. static CHIP_ERROR CmdAppServerSessions(int argc, char ** argv)
  123. {
  124. Server::GetInstance().GetSecureSessionManager().ForEachSessionHandle(nullptr, PrintServerSession);
  125. return CHIP_NO_ERROR;
  126. }
  127. static CHIP_ERROR CmdAppServerExchanges(int argc, char ** argv)
  128. {
  129. // Messaging::ExchangeManager * exchangeMgr = &Server::GetInstance().GetExchangeManager();
  130. return CHIP_NO_ERROR;
  131. }
  132. static CHIP_ERROR CmdAppServerClusters(int argc, char ** argv)
  133. {
  134. bool server = true;
  135. for (int i = 0; i < emberAfEndpointCount(); i++)
  136. {
  137. EndpointId endpoint = emberAfEndpointFromIndex(i);
  138. uint16_t clusterCount = emberAfClusterCount(endpoint, server);
  139. streamer_printf(streamer_get(), "Endpoint %d:\r\n", endpoint);
  140. for (uint8_t clusterIndex = 0; clusterIndex < clusterCount; clusterIndex++)
  141. {
  142. const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
  143. streamer_printf(streamer_get(), " - Cluster 0x%04X\r\n", cluster->clusterId);
  144. }
  145. }
  146. return CHIP_NO_ERROR;
  147. }
  148. static CHIP_ERROR CmdAppServerEndpoints(int argc, char ** argv)
  149. {
  150. for (int i = 0; i < emberAfEndpointCount(); i++)
  151. {
  152. EndpointId endpoint = emberAfEndpointFromIndex(i);
  153. streamer_printf(streamer_get(), "Endpoint %d\r\n", endpoint);
  154. }
  155. return CHIP_NO_ERROR;
  156. }
  157. static CHIP_ERROR CmdAppServer(int argc, char ** argv)
  158. {
  159. switch (argc)
  160. {
  161. case 0:
  162. return CmdAppServerHelp(argc, argv);
  163. case 1:
  164. if ((strcmp(argv[0], "help") == 0) || (strcmp(argv[0], "-h") == 0))
  165. {
  166. return CmdAppServerHelp(argc, argv);
  167. }
  168. }
  169. return sShellServerSubcommands.ExecCommand(argc, argv);
  170. }
  171. static void CmdAppServerAtExit()
  172. {
  173. CmdAppServerStop(0, nullptr);
  174. }
  175. void cmd_app_server_init()
  176. {
  177. static const shell_command_t sServerComand = { &CmdAppServer, "server",
  178. "Manage the ZCL application server. Usage: server [help|start|stop]" };
  179. static const shell_command_t sServerSubCommands[] = {
  180. { &CmdAppServerHelp, "help", "Usage: server <subcommand>" },
  181. { &CmdAppServerStart, "start", "Start the ZCL application server." },
  182. { &CmdAppServerStop, "stop", "Stop the ZCL application server." },
  183. { &CmdAppServerPort, "port", "Get/Set operational port of server." },
  184. { &CmdAppServerUdcPort, "udcport", "Get/Set commissioning port of server." },
  185. { &CmdAppServerSessions, "sessions", "Manage active sessions on the server." },
  186. { &CmdAppServerExchanges, "exchanges", "Manage active exchanges on the server." },
  187. { &CmdAppServerClusters, "clusters", "Display clusters on the server." },
  188. { &CmdAppServerEndpoints, "endpoints", "Display endpoints on the server." },
  189. };
  190. std::atexit(CmdAppServerAtExit);
  191. // Register `server` subcommands with the local shell dispatcher.
  192. sShellServerSubcommands.RegisterCommands(sServerSubCommands, ArraySize(sServerSubCommands));
  193. // Register the root `server` command with the top-level shell.
  194. Engine::Root().RegisterCommands(&sServerComand, 1);
  195. }