Source code of Windows XP (NT5)
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.

240 lines
5.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: U C P . C P P
  7. //
  8. // Contents: Functions dealing with UPnP User Control Points
  9. //
  10. // Notes:
  11. //
  12. // Author: danielwe 28 Oct 1999
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ncbase.h"
  18. #include "updiagp.h"
  19. BOOL DoNewUcp(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  20. {
  21. Assert(g_ctx.ectx == CTX_ROOT);
  22. if (cArgs == 2)
  23. {
  24. if (g_params.cUcp < MAX_UCP)
  25. {
  26. UPNPUCP * pucp = new UPNPUCP;
  27. ZeroMemory(pucp, sizeof(UPNPUCP));
  28. lstrcpy(pucp->szName, rgArgs[1]);
  29. g_params.rgUcp[g_params.cUcp++] = pucp;
  30. g_ctx.pucpCur = pucp;
  31. g_ctx.ectx = CTX_UCP;
  32. }
  33. else
  34. {
  35. _tprintf(TEXT("Exceeded number of UCPs allowed!\n\n"));
  36. }
  37. }
  38. else
  39. {
  40. Usage(iCmd);
  41. }
  42. return FALSE;
  43. }
  44. BOOL DoDelUcp(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  45. {
  46. Assert(g_ctx.ectx == CTX_ROOT);
  47. if (cArgs == 2)
  48. {
  49. DWORD iucp;
  50. UPNPUCP * pucp;
  51. iucp = _tcstoul(rgArgs[1], NULL, 10);
  52. pucp = g_params.rgUcp[iucp - 1];
  53. if (iucp &&
  54. iucp <= g_params.cUcp &&
  55. pucp)
  56. {
  57. _tprintf(TEXT("Deleted control point %s.\n"), pucp->szName);
  58. // Move last item into gap and decrement the count
  59. g_params.rgUcp[iucp - 1] = g_params.rgUcp[g_params.cUcp - 1];
  60. CleanupUcp(pucp);
  61. g_params.cUcp--;
  62. }
  63. else
  64. {
  65. _tprintf(TEXT("%d is not a valid UCP index!\n"), iucp);
  66. }
  67. }
  68. else
  69. {
  70. Usage(iCmd);
  71. }
  72. return FALSE;
  73. }
  74. BOOL DoSwitchUcp(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  75. {
  76. Assert(g_ctx.ectx == CTX_ROOT);
  77. if (cArgs == 2)
  78. {
  79. DWORD iucp;
  80. iucp = _tcstoul(rgArgs[1], NULL, 10);
  81. if (iucp &&
  82. iucp <= g_params.cUcp &&
  83. g_params.rgUcp[iucp - 1])
  84. {
  85. g_ctx.pucpCur = g_params.rgUcp[iucp - 1];
  86. g_ctx.ectx = CTX_UCP;
  87. }
  88. else
  89. {
  90. _tprintf(TEXT("%d is not a valid UCP index!\n"), iucp);
  91. }
  92. }
  93. else
  94. {
  95. Usage(iCmd);
  96. }
  97. return FALSE;
  98. }
  99. VOID CleanupUcp(UPNPUCP *pucp)
  100. {
  101. DWORD i;
  102. for (i = 0; i < pucp->cResults; i++)
  103. {
  104. CleanupResult(pucp->rgResults[i]);
  105. }
  106. delete pucp;
  107. }
  108. BOOL DoListUcp(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  109. {
  110. if (g_ctx.ectx == CTX_ROOT)
  111. {
  112. DWORD iucp;
  113. _tprintf(TEXT("Listing all Control Points\n"));
  114. _tprintf(TEXT("------------------------------\n"));
  115. for (iucp = 0; iucp < g_params.cUcp; iucp++)
  116. {
  117. _tprintf(TEXT("%d) %s\n"), iucp + 1, g_params.rgUcp[iucp]->szName);
  118. }
  119. _tprintf(TEXT("------------------------------\n\n"));
  120. }
  121. return FALSE;
  122. }
  123. BOOL DoSubscribe(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  124. {
  125. HANDLE hSubs;
  126. Assert(g_ctx.ectx & (CTX_UCP | CTX_DEVICE));
  127. if (g_ctx.pucpCur->cResults >= MAX_RESULT)
  128. {
  129. _tprintf(TEXT("Exceeded maximum number of subscriptions!\n"));
  130. return FALSE;
  131. }
  132. if (cArgs == 2)
  133. {
  134. UPNPRESULT * psubs;
  135. psubs = new UPNPRESULT;
  136. ZeroMemory(psubs, sizeof(UPNPRESULT));
  137. lstrcpy(psubs->szType, rgArgs[1]);
  138. psubs->resType = RES_SUBS;
  139. if (g_ctx.ectx == CTX_UCP)
  140. {
  141. Assert(rgArgs[1]);
  142. LPSTR pszType = SzFromTsz(rgArgs[1]);
  143. if (pszType)
  144. {
  145. psubs->hResult = RegisterNotification(NOTIFY_PROP_CHANGE, NULL,
  146. pszType, NotifyCallback,
  147. (LPVOID) psubs);
  148. if (psubs->hResult && psubs->hResult != INVALID_HANDLE_VALUE)
  149. {
  150. g_ctx.pucpCur->rgResults[g_ctx.pucpCur->cResults++] = psubs;
  151. _tprintf(TEXT("Subscribed to event URL : %s\n"), rgArgs[1]);
  152. }
  153. else
  154. {
  155. _tprintf(TEXT("RegisterNotification failed with error %d.\n"),
  156. GetLastError());
  157. delete psubs;
  158. }
  159. delete [] pszType;
  160. }
  161. else
  162. {
  163. _tprintf(TEXT("DoSubscribe: SzFromTsz failed\n"));
  164. }
  165. }
  166. }
  167. else
  168. {
  169. Usage(iCmd);
  170. }
  171. return FALSE;
  172. }
  173. BOOL DoUnsubscribe(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  174. {
  175. Assert(g_ctx.ectx == CTX_UCP);
  176. if (cArgs == 2)
  177. {
  178. DWORD isrch;
  179. isrch = _tcstoul(rgArgs[1], NULL, 10);
  180. if (isrch <= g_ctx.pucpCur->cResults &&
  181. g_ctx.pucpCur->rgResults[isrch - 1])
  182. {
  183. UPNPRESULT * psubs = g_ctx.pucpCur->rgResults[isrch - 1];
  184. _tprintf(TEXT("Deleted subscription %s.\n"),
  185. psubs->szType);
  186. // Move last item into gap and decrement the count
  187. g_ctx.pucpCur->rgResults[isrch - 1] = g_ctx.pucpCur->rgResults[g_ctx.pucpCur->cResults - 1];
  188. CleanupResult(psubs);
  189. g_ctx.pucpCur->cResults--;
  190. }
  191. else
  192. {
  193. _tprintf(TEXT("%d is not a valid subscription index!\n"), isrch);
  194. }
  195. }
  196. else
  197. {
  198. Usage(iCmd);
  199. }
  200. return FALSE;
  201. }