upnp_ctrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (C) 2012-2013 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <pro/uri.h>
  35. #include <pro/uxml.h>
  36. #include <pro/tcphost.h>
  37. #include <pro/upnp.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. /* Use for local debugging only. Do NOT include into NUTDEBUG.
  41. #define DEBUG_UPNPC
  42. */
  43. #ifdef DEBUG_UPNPC
  44. #include <stdio.h>
  45. #endif
  46. typedef struct _UPNP_OBSERVER UPNP_OBSERVER;
  47. struct _UPNP_OBSERVER {
  48. UPNP_OBSERVER *uobs_next;
  49. char *uobs_domain;
  50. char *uobs_type;
  51. SSDP_OBSERVER_FUNCTION uobs_cb;
  52. };
  53. static UPNP_OBSERVER *upnp_observer_root;
  54. /*!
  55. * \brief Call all our observers.
  56. *
  57. * \param ssvc Specifies the service.
  58. * \param removal 0 if a new service has been added or 1 if a service will
  59. * be removed.
  60. *
  61. * \return 0 on success or -1 on failure.
  62. */
  63. static int CallObservers(SSDP_SERVICE *ssvc, int_fast8_t removal)
  64. {
  65. int rc;
  66. UPNP_OBSERVER *uobs;
  67. for (rc = 0, uobs = upnp_observer_root; rc == 0 && uobs; uobs = uobs->uobs_next) {
  68. if (strcmp(ssvc->ssvc_domain, uobs->uobs_domain) == 0 && strcmp(ssvc->ssvc_type, uobs->uobs_type) == 0) {
  69. rc = (*uobs->uobs_cb)(ssvc, removal);
  70. }
  71. }
  72. return rc;
  73. }
  74. static const UXML_NODE *GetXmlNode(const UXML_NODE *list, const char *name)
  75. {
  76. const UXML_NODE *node;
  77. for (node = list; node; node = node->xmln_next) {
  78. if (strcasecmp(node->xmln_name, name) == 0) {
  79. break;
  80. }
  81. }
  82. return node;
  83. }
  84. static const UXML_NODE *GetDeviceChildrenNodes(const UXML_NODE *root)
  85. {
  86. root = GetXmlNode(root, "root");
  87. if (root) {
  88. root = GetXmlNode(root->xmln_child, "device");
  89. if (root) {
  90. root = root->xmln_child;
  91. }
  92. }
  93. return root;
  94. }
  95. static char *DuplicateXmlNodeContent(const UXML_NODE *node, const char *name)
  96. {
  97. node = GetXmlNode(node, name);
  98. if (node && node->xmln_content) {
  99. return strdup(node->xmln_content);
  100. }
  101. return NULL;
  102. }
  103. static int ParseServiceDeviceInfo(const UXML_NODE *root, SSDP_SERVICE *ssvc)
  104. {
  105. int rc = -1;
  106. const UXML_NODE *node;
  107. SSDP_DEVICE *sdev = ssvc->ssvc_dev;
  108. UPNP_DEVICE_INFO *udev = calloc(1, sizeof(*udev));
  109. root = GetDeviceChildrenNodes(root);
  110. if (udev) {
  111. sdev->sdev_info = udev;
  112. udev->udev_name = DuplicateXmlNodeContent(root, "friendlyName");
  113. }
  114. if (sdev->sdev_domain == NULL || sdev->sdev_type == NULL) {
  115. char *comp[5];
  116. SsdpSplitWords(DuplicateXmlNodeContent(root, "deviceType"), ':', comp, 5);
  117. if (sdev->sdev_domain == NULL) {
  118. sdev->sdev_domain = strdup(comp[1]);
  119. }
  120. if (sdev->sdev_type == NULL) {
  121. sdev->sdev_type = strdup(comp[3]);
  122. }
  123. }
  124. root = GetXmlNode(root, "serviceList");
  125. if (root) {
  126. for (root = root->xmln_child; root && root->xmln_child; root = root->xmln_next) {
  127. char *comp[5];
  128. const UXML_NODE *snode = root->xmln_child;
  129. node = GetXmlNode(snode, "serviceType");
  130. SsdpSplitWords(strdup(node->xmln_content), ':', comp, 5);
  131. if (strcasecmp(comp[1], ssvc->ssvc_domain) == 0 && strcasecmp(comp[3], ssvc->ssvc_type) == 0) {
  132. UPNP_SERVICE_INFO *usvc = calloc(1, sizeof(*usvc));
  133. if (usvc) {
  134. node = GetXmlNode(snode, "SCPDURL");
  135. if (node && node->xmln_content) {
  136. usvc->usvc_url_scpd = strdup(node->xmln_content);
  137. }
  138. node = GetXmlNode(snode, "controlURL");
  139. if (node && node->xmln_content) {
  140. usvc->usvc_url_ctrl = strdup(node->xmln_content);
  141. }
  142. node = GetXmlNode(snode, "eventSubURL");
  143. if (node && node->xmln_content) {
  144. usvc->usvc_url_event = strdup(node->xmln_content);
  145. }
  146. ssvc->ssvc_info = usvc;
  147. rc = 0;
  148. }
  149. }
  150. }
  151. }
  152. return rc;
  153. }
  154. static int ParseServiceInfo(const UXML_NODE *root, SSDP_SERVICE *ssvc)
  155. {
  156. int rc = -1;
  157. const UXML_NODE *node;
  158. const UXML_NODE *actlist = NULL;
  159. const UXML_NODE *varlist = NULL;
  160. root = GetXmlNode(root, "scpd");
  161. if (root) {
  162. actlist = GetXmlNode(root->xmln_child, "actionList");
  163. varlist = GetXmlNode(root->xmln_child, "serviceStateTable");
  164. }
  165. if (root == NULL || actlist == NULL || varlist == NULL) {
  166. return -1;
  167. }
  168. /*
  169. * Read state variables.
  170. */
  171. for (varlist = varlist->xmln_child; varlist && varlist->xmln_child; varlist = varlist->xmln_next) {
  172. UPNP_VARIABLE *var = calloc(1, sizeof(*var));
  173. if (var) {
  174. for (node = varlist->xmln_child; node; node = node->xmln_next) {
  175. if (node->xmln_content) {
  176. if (strcasecmp(node->xmln_name, "name") == 0) {
  177. var->ustv_name = strdup(node->xmln_content);
  178. }
  179. else if (strcasecmp(node->xmln_name, "dataType") == 0) {
  180. var->ustv_type = UpnpVarTypeIndex(node->xmln_content);
  181. }
  182. else if (strcasecmp(node->xmln_name, "defaultValue") == 0) {
  183. var->ustv_default = strdup(node->xmln_content);
  184. }
  185. else if (strcasecmp(node->xmln_name, "allowedValueList") == 0) {
  186. }
  187. }
  188. }
  189. if (var->ustv_name && var->ustv_type) {
  190. UPNP_SERVICE_INFO *usvc = ssvc->ssvc_info;
  191. var->ustv_next = usvc->usvc_stv;
  192. usvc->usvc_stv = var;
  193. } else {
  194. free(var->ustv_name);
  195. free(var->ustv_default);
  196. free(var);
  197. }
  198. }
  199. }
  200. /*
  201. * Read actions.
  202. */
  203. for (actlist = actlist->xmln_child; actlist && actlist->xmln_child; actlist = actlist->xmln_next) {
  204. SOAP_PROCEDURE *proc;
  205. const UXML_NODE *actnode = actlist->xmln_child;
  206. rc = -1;
  207. proc = calloc(1, sizeof(*proc));
  208. if (proc) {
  209. node = GetXmlNode(actnode, "name");
  210. if (node && node->xmln_content) {
  211. const UXML_NODE *argnode = GetXmlNode(actnode, "argumentList");
  212. proc->proc_name = strdup(node->xmln_content);
  213. if (argnode) {
  214. for (argnode = argnode->xmln_child; argnode && argnode->xmln_child; argnode = argnode->xmln_next) {
  215. SOAP_ARG *arg = calloc(1, sizeof(*arg));
  216. if (arg) {
  217. SOAP_ARG **argtyp = NULL;
  218. for (node = argnode->xmln_child; node; node = node->xmln_next) {
  219. if (node->xmln_content) {
  220. if (strcasecmp(node->xmln_name, "name") == 0) {
  221. arg->arg_name = strdup(node->xmln_content);
  222. }
  223. else if (strcasecmp(node->xmln_name, "direction") == 0) {
  224. if (*node->xmln_content == 'i') {
  225. argtyp = &proc->proc_argi;
  226. }
  227. else if (*node->xmln_content == 'o') {
  228. argtyp = &proc->proc_argo;
  229. }
  230. }
  231. else if (strcasecmp(node->xmln_name, "relatedStateVariable") == 0) {
  232. UPNP_VARIABLE *state;
  233. for (state = ((UPNP_SERVICE_INFO *)ssvc->ssvc_info)->usvc_stv; state; state = state->ustv_next) {
  234. if (strcasecmp(node->xmln_content, state->ustv_name) == 0) {
  235. arg->arg_info = state;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. if (arg->arg_name && argtyp && arg->arg_info) {
  243. arg->arg_next = *argtyp;
  244. *argtyp = arg;
  245. rc = 0;
  246. } else {
  247. free(arg->arg_name);
  248. free(arg);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. if (rc == 0) {
  255. UPNP_SERVICE_INFO *usvc = ssvc->ssvc_info;
  256. proc->proc_next = usvc->usvc_proc;
  257. usvc->usvc_proc = proc;
  258. }
  259. }
  260. }
  261. return rc;
  262. }
  263. static int SendHttpRequest(FILE *stream, const URI_SCHEME *schm)
  264. {
  265. fputs("GET ", stream);
  266. fprintf(stream, "/%s HTTP/1.1\r\n", schm->schm_path);
  267. fprintf(stream, "Host: %s\r\n", schm->schm_host);
  268. fputs("Connection: close\r\n\r\n", stream);
  269. fflush(stream);
  270. return 0;
  271. }
  272. /*!
  273. * \brief Request XML tree from a specified location.
  274. *
  275. * \param url HTTP URL.
  276. *
  277. * \return Pointer to the root node or NULL if an error occurred.
  278. */
  279. static UXML_NODE *RequestXmlTree(const char *url, uint32_t tmo)
  280. {
  281. UXML_NODE *tree = NULL;
  282. if (strncasecmp(url, "http://", 7) == 0) {
  283. URI_SCHEME *schm;
  284. url += 7;
  285. schm = UriSchemeSplit(url);
  286. if (schm) {
  287. TCPSOCKET *sock = NutTcpCreateSocket();
  288. if (sock) {
  289. FILE *stream = TcpHostConnectStream(sock, schm->schm_host, schm->schm_portnum, tmo);
  290. if (stream) {
  291. SendHttpRequest(stream, schm);
  292. tree = UxmlParseStream(stream, NULL, NULL);
  293. fclose(stream);
  294. }
  295. NutTcpCloseSocket(sock);
  296. }
  297. UriSchemeRelease(schm);
  298. }
  299. }
  300. return tree;
  301. }
  302. static int AddServiceDeviceInfo(SSDP_SERVICE *ssvc)
  303. {
  304. int rc = -1;
  305. UXML_NODE *tree;
  306. tree = RequestXmlTree(ssvc->ssvc_dev->sdev_url_desc, 2000);
  307. if (tree) {
  308. rc = ParseServiceDeviceInfo(tree, ssvc);
  309. UxmlTreeDestroy(tree);
  310. }
  311. return rc;
  312. }
  313. static int AddServiceInfo(SSDP_SERVICE *ssvc)
  314. {
  315. int rc = -1;
  316. UPNP_SERVICE_INFO *usvc = ssvc->ssvc_info;
  317. char *location;
  318. location = malloc(strlen(ssvc->ssvc_dev->sdev_url_desc) + strlen(usvc->usvc_url_scpd));
  319. if (location) {
  320. char *cp;
  321. strcpy(location, ssvc->ssvc_dev->sdev_url_desc);
  322. cp = strrchr(location, '/');
  323. if (cp) {
  324. UXML_NODE *tree;
  325. if (*usvc->usvc_url_scpd != '/') {
  326. cp++;
  327. }
  328. strcpy(cp, usvc->usvc_url_scpd);
  329. tree = RequestXmlTree(location, 2000);
  330. if (tree) {
  331. rc = ParseServiceInfo(tree, ssvc);
  332. UxmlTreeDestroy(tree);
  333. }
  334. }
  335. }
  336. return rc;
  337. }
  338. /*!
  339. * \brief Remove UPnP service information from
  340. */
  341. static void RemoveServiceInfo(SSDP_SERVICE *ssvc)
  342. {
  343. UPNP_SERVICE_INFO *usvc = ssvc->ssvc_info;
  344. if (usvc) {
  345. SOAP_PROCEDURE *proc;
  346. SOAP_PROCEDURE *proc_next = NULL;
  347. UPNP_VARIABLE *uvar;
  348. UPNP_VARIABLE *uvar_next = NULL;
  349. for (proc = usvc->usvc_proc; proc; proc = proc_next) {
  350. SOAP_ARG *arg;
  351. SOAP_ARG *arg_next = NULL;
  352. proc_next = proc->proc_next;
  353. for (arg = proc->proc_argi; arg; arg = arg_next) {
  354. arg_next = arg->arg_next;
  355. free(arg->arg_name);
  356. free(arg->arg_val);
  357. free(arg);
  358. }
  359. for (arg = proc->proc_argo; arg; arg = arg_next) {
  360. arg_next = arg->arg_next;
  361. free(arg->arg_name);
  362. free(arg->arg_val);
  363. free(arg);
  364. }
  365. free(proc->proc_name);
  366. free(proc);
  367. }
  368. for (uvar = usvc->usvc_stv; uvar; uvar = uvar_next) {
  369. uvar_next = uvar->ustv_next;
  370. free(uvar->ustv_name);
  371. free(uvar->ustv_default);
  372. free(uvar);
  373. }
  374. free(usvc->usvc_url_scpd);
  375. free(usvc->usvc_url_ctrl);
  376. free(usvc->usvc_url_event);
  377. free(usvc);
  378. }
  379. }
  380. /*!
  381. * \brief Service observer callback.
  382. *
  383. * This function is called by the SSDP receiver thread after a new service
  384. * has been added to the cache or before a service will be removed from the
  385. * cache.
  386. *
  387. * \param ssvc Specifies the service.
  388. * \param removal 0 if a new service has been added or 1 if a service will
  389. * be removed.
  390. *
  391. * \return 0 on success or -1 on failure.
  392. */
  393. static int ServiceObserver(SSDP_SERVICE *ssvc, int_fast8_t removal)
  394. {
  395. int rc = 0;
  396. if (removal) {
  397. CallObservers(ssvc, 1);
  398. RemoveServiceInfo(ssvc);
  399. } else {
  400. if (ssvc->ssvc_dev->sdev_info == NULL) {
  401. rc = AddServiceDeviceInfo(ssvc);
  402. }
  403. if (rc == 0) {
  404. rc = AddServiceInfo(ssvc);
  405. }
  406. if (rc == 0) {
  407. rc = CallObservers(ssvc, 0);
  408. }
  409. }
  410. return rc;
  411. }
  412. int UpnpRegisterServiceObserver(SSDP_OBSERVER_FUNCTION cb, const char *domain, const char *type, int_fast8_t mxwait)
  413. {
  414. int rc = -1;
  415. UPNP_OBSERVER *uobs;
  416. uobs = malloc(sizeof(*uobs));
  417. if (uobs) {
  418. uobs->uobs_domain = strdup(domain);
  419. uobs->uobs_type = strdup(type);
  420. if (uobs->uobs_domain && uobs->uobs_type) {
  421. uobs->uobs_cb = cb;
  422. uobs->uobs_next = upnp_observer_root;
  423. upnp_observer_root = uobs;
  424. rc = SsdpRegisterServiceObserver(ServiceObserver, domain, type, mxwait);
  425. }
  426. if (rc) {
  427. free(uobs->uobs_domain);
  428. free(uobs->uobs_type);
  429. free(uobs);
  430. }
  431. }
  432. return rc;
  433. }
  434. SOAP_PROCEDURE *UpnpServiceProcByName(const SSDP_SERVICE *ssvc, const char *name)
  435. {
  436. return SoapProcByName(((UPNP_SERVICE_INFO *) ssvc->ssvc_info)->usvc_proc, name);
  437. }
  438. int UpnpServiceProcCall(SSDP_SERVICE *ssvc, SOAP_PROCEDURE *proc, uint32_t tmo)
  439. {
  440. int rc = -1;
  441. char *urn;
  442. char *url;
  443. char *uri;
  444. UPNP_SERVICE_INFO *usvc = ssvc->ssvc_info;
  445. urn = malloc(strlen(ssvc->ssvc_domain) + strlen(ssvc->ssvc_type) + 12);
  446. url = malloc(strlen(ssvc->ssvc_dev->sdev_url_desc) + strlen(usvc->usvc_url_ctrl));
  447. if (urn && url) {
  448. sprintf(urn, "%s:service:%s:1", ssvc->ssvc_domain, ssvc->ssvc_type);
  449. strcpy(url, ssvc->ssvc_dev->sdev_url_desc);
  450. uri = strrchr(url, '/');
  451. if (uri) {
  452. if (*usvc->usvc_url_ctrl != '/') {
  453. uri++;
  454. }
  455. strcpy(uri, usvc->usvc_url_ctrl);
  456. rc = SoapProcCallResource(proc, url, uri, urn, tmo);
  457. }
  458. }
  459. free(urn);
  460. free(url);
  461. return rc;
  462. }