soap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifndef _PRO_SOAP_H_
  2. #define _PRO_SOAP_H_
  3. /*
  4. * Copyright (C) 2012-2013 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file pro/soap.h
  38. * \brief SOAP remote call library.
  39. *
  40. * \verbatim File version $Id: soap.h 4919 2013-01-04 11:49:36Z haraldkipp $ \endverbatim
  41. */
  42. #include <cfg/http.h>
  43. #include <pro/uhttp/uhttpd.h>
  44. #include <stdint.h>
  45. #ifndef SOAP_MAX_TAG_SIZE
  46. #define SOAP_MAX_TAG_SIZE 128
  47. #endif
  48. #ifndef SOAP_MAX_TAG_ATTRIBUTES
  49. #define SOAP_MAX_TAG_ATTRIBUTES 8
  50. #endif
  51. /*! \brief Select input argument. */
  52. #define SOAP_ARG_IN 0
  53. /*! \brief Select output argument. */
  54. #define SOAP_ARG_OUT 1
  55. #define SOAPTYPE_EOTAG 0x40
  56. #define SOAPTYPE_ETAG 0x80
  57. typedef struct _SOAP_TAG SOAP_TAG;
  58. typedef struct _SOAP_NAME SOAP_NAME;
  59. typedef struct _SOAP_ATTRIBUTE SOAP_ATTRIBUTE;
  60. typedef struct _SOAP_ARG SOAP_ARG;
  61. typedef struct _SOAP_PROCEDURE SOAP_PROCEDURE;
  62. struct _SOAP_ARG {
  63. SOAP_ARG *arg_next;
  64. char *arg_name;
  65. char *arg_val;
  66. void *arg_info;
  67. };
  68. struct _SOAP_PROCEDURE {
  69. SOAP_PROCEDURE *proc_next;
  70. char *proc_name;
  71. SOAP_ARG *proc_argi;
  72. SOAP_ARG *proc_argo;
  73. };
  74. struct _SOAP_NAME {
  75. char *_namespace;
  76. char *_name;
  77. };
  78. struct _SOAP_ATTRIBUTE {
  79. SOAP_NAME attr_name;
  80. char *attr_value;
  81. };
  82. struct _SOAP_TAG {
  83. uint8_t soap_ttf;
  84. SOAP_NAME soap_name;
  85. SOAP_ATTRIBUTE soap_attr[SOAP_MAX_TAG_ATTRIBUTES];
  86. char soap_buff[SOAP_MAX_TAG_SIZE + 1];
  87. };
  88. /*!
  89. * \brief Get SOAP procedure by name.
  90. *
  91. * \param list Linked list of procedures.
  92. * \param name Procedure name.
  93. *
  94. * \return Pointer to the procedure or NULL if not found.
  95. */
  96. extern SOAP_PROCEDURE *SoapProcByName(SOAP_PROCEDURE *list, const char *name);
  97. /*!
  98. * \brief Get SOAP argument by name.
  99. *
  100. * \param proc Specifies the SOAP procedure.
  101. * \param name Name of the argument to find.
  102. * \param dir Must be \ref SOAP_ARG_IN for input or \ref SOAP_ARG_OUT
  103. * for output arguments.
  104. *
  105. * \return Pointer to the procedure or NULL if not found.
  106. */
  107. extern SOAP_ARG *SoapArgByName(SOAP_PROCEDURE *proc, const char *name, int dir);
  108. /*!
  109. * \brief Set SOAP procedure parameter.
  110. *
  111. * \param proc Specifies the SOAP procedure.
  112. * \param name Parameter name.
  113. * \param value Parameter value.
  114. * \param dir Must be \ref SOAP_ARG_IN for input or \ref SOAP_ARG_OUT
  115. * for output arguments.
  116. *
  117. * \return 0 on success or -1 on failure.
  118. */
  119. extern int SoapProcArgSet(SOAP_PROCEDURE *proc, const char *name, const char *value, int dir);
  120. /*!
  121. * \brief Set SOAP procedure integer parameter.
  122. *
  123. * \param proc Specifies the SOAP procedure.
  124. * \param name Parameter name.
  125. * \param value Parameter value.
  126. * \param dir Must be \ref SOAP_ARG_IN for input or \ref SOAP_ARG_OUT
  127. * for output arguments.
  128. *
  129. * \return 0 on success or -1 on failure.
  130. */
  131. extern int SoapProcArgSetInt(SOAP_PROCEDURE *proc, const char *name, int value, int dir);
  132. /*!
  133. * \brief Get SOAP procedure parameter.
  134. *
  135. * \param proc Specifies the SOAP procedure.
  136. * \param name Parameter name.
  137. * \param dir Must be \ref SOAP_ARG_IN for input or \ref SOAP_ARG_OUT
  138. * for output arguments.
  139. *
  140. * \return Pointer to the requested parameter value. If not available,
  141. * then NULL is returned.
  142. */
  143. extern const char *SoapProcArgGet(SOAP_PROCEDURE *proc, const char *name, int dir);
  144. /*!
  145. * \brief Get SOAP procedure integer parameter.
  146. *
  147. * \param proc Specifies the SOAP procedure.
  148. * \param name Parameter name.
  149. * \param dir Must be \ref SOAP_ARG_IN for input or \ref SOAP_ARG_OUT
  150. * for output arguments.
  151. *
  152. * \return Value of the requested parameter. If not available, then 0
  153. * is returned.
  154. */
  155. extern int SoapProcArgGetInt(SOAP_PROCEDURE *proc, const char *name, int dir);
  156. /*!
  157. * \brief Parse SOAP call request.
  158. *
  159. * \param stream uHTTP stream of the connection.
  160. * \param avail Number of bytes available in the request.
  161. * \param list Linked list of supported SOAP procedures.
  162. *
  163. * \return Pointer to the requested procedure. NULL is returned in case
  164. * of an error
  165. */
  166. extern SOAP_PROCEDURE *SoapParseCallRequest(HTTP_STREAM *stream, int avail, SOAP_PROCEDURE *list);
  167. /*!
  168. * \brief Send SOAP call response.
  169. *
  170. * Values of any output arguments must have be set by the caller in the
  171. * procedure structure.
  172. *
  173. * The following code fragment shows how to handle a SOAP remote call.
  174. * \code
  175. * #include <pro/uhttp/uhttpd.h>
  176. * #include <pro/uhttp/modules/mod_cgi_func.h>
  177. * #include <pro/uhttp/mediatypes.h>
  178. * #include <pro/soap.h>
  179. *
  180. * int status;
  181. *
  182. * int CgiSwitchLight(HTTPD_SESSION *hs)
  183. * {
  184. * const char *domain = "schemas-upnp-org";
  185. * const char *type = "BinaryLight";
  186. * HTTP_STREAM *stream = hs->s_stream;
  187. * int req_len = (int) hs->s_req.req_length;
  188. * SOAP_PROCEDURE *proc;
  189. *
  190. * proc = SoapParseCallRequest(stream, req_len, switchPowerProcs);
  191. * if (proc) {
  192. * if (strcmp(proc->proc_name, "GetStatus") == 0) {
  193. * SoapProcArgSetInt(proc, "ResultStatus", status, SOAP_ARG_OUT);
  194. * SoapSendCallResponse(hstream, proc, domain, type);
  195. * }
  196. * else if (strcmp(proc->proc_name, "SetTarget") == 0) {
  197. * status = SoapProcArgGetInt(proc, "newTargetValue", SOAP_ARG_IN);
  198. * SoapSendCallResponse(stream, proc, domain, type);
  199. * }
  200. * }
  201. * return 0;
  202. * }
  203. *
  204. * StreamInit();
  205. * MediaTypeInitDefaults();
  206. * HttpRegisterMediaType("cgi", NULL, NULL, HttpCgiFunctionHandler);
  207. * HttpRegisterCgiFunction("switchlight.cgi", CgiSwitchLight);
  208. * \endcode
  209. *
  210. * \param stream uHTTP stream of the connection.
  211. * \param proc Pointer to SOAP procedure structure.
  212. * \param domain Type domain of the service.
  213. * \param type Type name of the service.
  214. *
  215. * \return Pointer to the requested procedure. NULL is returned in case
  216. * of an error
  217. */
  218. extern int SoapSendCallResponse(HTTP_STREAM *stream, SOAP_PROCEDURE *proc, const char *domain, const char *type);
  219. /*!
  220. * \brief Call SOAP procedure resource.
  221. *
  222. * The following code fragment shows how to do a SOAP remote call.
  223. * \code
  224. * #include <pro/soap.h>
  225. *
  226. * char *url = "http://192.0.0.2/dlna/renderer";
  227. * char *uri = "renderer";
  228. * char *urn = "schemas-upnp-org:service:RenderingControl:1";
  229. * SOAP_PROCEDURE *proc;
  230. *
  231. * proc = SoapProcByName(tv_control, "GetVolume");
  232. * SoapProcArgSetInt(proc, "InstanceID", 0, SOAP_ARG_IN);
  233. * SoapProcArgSet(proc, "Channel", "Master", SOAP_ARG_IN);
  234. * if (SoapProcCallResource(proc, url, uri, urn, 2000) == 0) {
  235. * int vol = SoapProcArgGetInt(proc, "CurrentVolume", SOAP_ARG_OUT);
  236. * }
  237. * \endcode
  238. *
  239. * \param proc Specifies the SOAP procedure.
  240. * \param url HTTP resource locator.
  241. * \param uri HTTP resource identifier.
  242. * \param urn SOAP resource name.
  243. * \param tmo Maximum number of milliseconds to wait for response.
  244. *
  245. * \return 0 on success or -1 on failure.
  246. */
  247. extern int SoapProcCallResource(SOAP_PROCEDURE *proc, const char *url, const char *uri, const char *urn, uint32_t tmo);
  248. #endif