cmd_server.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. static chip::CommonCaseDeviceServerInitParams initParams;
  51. (void) initParams.InitializeStaticResourcesBeforeServerInit();
  52. initParams.operationalServicePort = sServerPortOperational;
  53. initParams.userDirectedCommissioningPort = sServerPortCommissioning;
  54. chip::Server::GetInstance().Init(initParams);
  55. // Initialize device attestation config
  56. SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
  57. sServerEnabled = true;
  58. return CHIP_NO_ERROR;
  59. }
  60. static CHIP_ERROR CmdAppServerStop(int argc, char ** argv)
  61. {
  62. if (sServerEnabled == false)
  63. return CHIP_NO_ERROR;
  64. chip::Server::GetInstance().Shutdown();
  65. sServerEnabled = false;
  66. return CHIP_NO_ERROR;
  67. }
  68. static CHIP_ERROR CmdAppServerPort(int argc, char ** argv)
  69. {
  70. if (argc == 0)
  71. {
  72. streamer_printf(streamer_get(), "%d\r\n", sServerPortOperational);
  73. }
  74. else
  75. {
  76. bool success = ParseInt(argv[0], sServerPortOperational);
  77. if (!success)
  78. return CHIP_ERROR_INVALID_ARGUMENT;
  79. }
  80. return CHIP_NO_ERROR;
  81. }
  82. static CHIP_ERROR CmdAppServerUdcPort(int argc, char ** argv)
  83. {
  84. if (argc == 0)
  85. {
  86. streamer_printf(streamer_get(), "%d\r\n", sServerPortCommissioning);
  87. }
  88. else
  89. {
  90. bool success = ParseInt(argv[0], sServerPortCommissioning);
  91. if (!success)
  92. return CHIP_ERROR_INVALID_ARGUMENT;
  93. }
  94. return CHIP_NO_ERROR;
  95. }
  96. static bool PrintServerSession(void * context, SessionHandle & session)
  97. {
  98. switch (session->GetSessionType())
  99. {
  100. case Session::SessionType::kSecure: {
  101. SecureSession * secureSession = session->AsSecureSession();
  102. SecureSession::Type secureSessionType = secureSession->GetSecureSessionType();
  103. streamer_printf(
  104. streamer_get(), "session type=SECURE %s id=0x%04x peerSessionId=0x%04x peerNodeId=0x%016" PRIx64 " fabricIdx=%d\r\n",
  105. secureSessionType == SecureSession::Type::kCASE ? "CASE" : "PASE", secureSession->GetLocalSessionId(),
  106. secureSession->AsSecureSession()->GetPeerSessionId(), secureSession->GetPeerNodeId(), secureSession->GetFabricIndex());
  107. break;
  108. }
  109. case Session::SessionType::kUnauthenticated: {
  110. UnauthenticatedSession * unsecuredSession = session->AsUnauthenticatedSession();
  111. streamer_printf(streamer_get(), "session type=UNSECURED id=0x0000 peerNodeId=0x%016" PRIx64 "\r\n",
  112. unsecuredSession->GetPeerNodeId());
  113. break;
  114. }
  115. case Session::SessionType::kGroupIncoming: {
  116. IncomingGroupSession * groupSession = session->AsIncomingGroupSession();
  117. streamer_printf(streamer_get(), "session type=GROUP INCOMING id=0x%04x fabricIdx=%d\r\n", groupSession->GetGroupId(),
  118. groupSession->GetFabricIndex());
  119. break;
  120. }
  121. case Session::SessionType::kGroupOutgoing: {
  122. OutgoingGroupSession * groupSession = session->AsOutgoingGroupSession();
  123. streamer_printf(streamer_get(), "session type=GROUP OUTGOING id=0x%04x fabricIdx=%d\r\n", groupSession->GetGroupId(),
  124. groupSession->GetFabricIndex());
  125. break;
  126. }
  127. default:
  128. streamer_printf(streamer_get(), "session type=UNDEFINED\r\n");
  129. }
  130. return true;
  131. }
  132. static CHIP_ERROR CmdAppServerSessions(int argc, char ** argv)
  133. {
  134. Server::GetInstance().GetSecureSessionManager().ForEachSessionHandle(nullptr, PrintServerSession);
  135. return CHIP_NO_ERROR;
  136. }
  137. static CHIP_ERROR CmdAppServerExchanges(int argc, char ** argv)
  138. {
  139. // Messaging::ExchangeManager * exchangeMgr = &Server::GetInstance().GetExchangeManager();
  140. return CHIP_NO_ERROR;
  141. }
  142. static CHIP_ERROR CmdAppServerClusters(int argc, char ** argv)
  143. {
  144. bool server = true;
  145. for (uint16_t i = 0; i < emberAfEndpointCount(); i++)
  146. {
  147. EndpointId endpoint = emberAfEndpointFromIndex(i);
  148. uint8_t clusterCount = emberAfClusterCount(endpoint, server);
  149. streamer_printf(streamer_get(), "Endpoint %d:\r\n", endpoint);
  150. for (uint8_t clusterIndex = 0; clusterIndex < clusterCount; clusterIndex++)
  151. {
  152. const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
  153. streamer_printf(streamer_get(), " - Cluster 0x%04X\r\n", cluster->clusterId);
  154. }
  155. }
  156. return CHIP_NO_ERROR;
  157. }
  158. static CHIP_ERROR CmdAppServerEndpoints(int argc, char ** argv)
  159. {
  160. for (uint16_t i = 0; i < emberAfEndpointCount(); i++)
  161. {
  162. EndpointId endpoint = emberAfEndpointFromIndex(i);
  163. streamer_printf(streamer_get(), "Endpoint %d\r\n", endpoint);
  164. }
  165. return CHIP_NO_ERROR;
  166. }
  167. static CHIP_ERROR CmdAppServer(int argc, char ** argv)
  168. {
  169. switch (argc)
  170. {
  171. case 0:
  172. return CmdAppServerHelp(argc, argv);
  173. case 1:
  174. if ((strcmp(argv[0], "help") == 0) || (strcmp(argv[0], "-h") == 0))
  175. {
  176. return CmdAppServerHelp(argc, argv);
  177. }
  178. }
  179. return sShellServerSubcommands.ExecCommand(argc, argv);
  180. }
  181. static void CmdAppServerAtExit()
  182. {
  183. CmdAppServerStop(0, nullptr);
  184. }
  185. void cmd_app_server_init()
  186. {
  187. static const shell_command_t sServerComand = { &CmdAppServer, "server",
  188. "Manage the ZCL application server. Usage: server [help|start|stop]" };
  189. static const shell_command_t sServerSubCommands[] = {
  190. { &CmdAppServerHelp, "help", "Usage: server <subcommand>" },
  191. { &CmdAppServerStart, "start", "Start the ZCL application server." },
  192. { &CmdAppServerStop, "stop", "Stop the ZCL application server." },
  193. { &CmdAppServerPort, "port", "Get/Set operational port of server." },
  194. { &CmdAppServerUdcPort, "udcport", "Get/Set commissioning port of server." },
  195. { &CmdAppServerSessions, "sessions", "Manage active sessions on the server." },
  196. { &CmdAppServerExchanges, "exchanges", "Manage active exchanges on the server." },
  197. { &CmdAppServerClusters, "clusters", "Display clusters on the server." },
  198. { &CmdAppServerEndpoints, "endpoints", "Display endpoints on the server." },
  199. };
  200. std::atexit(CmdAppServerAtExit);
  201. // Register `server` subcommands with the local shell dispatcher.
  202. sShellServerSubcommands.RegisterCommands(sServerSubCommands, ArraySize(sServerSubCommands));
  203. // Register the root `server` command with the top-level shell.
  204. Engine::Root().RegisterCommands(&sServerComand, 1);
  205. }