gena.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  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 are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * - Neither name of Intel Corporation nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  26. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ******************************************************************************/
  31. #ifndef GENA_H
  32. #define GENA_H
  33. /*!
  34. * \file
  35. */
  36. #include "config.h"
  37. #include <string.h>
  38. #include <time.h>
  39. #include "client_table.h"
  40. #include "httpparser.h"
  41. #include "miniserver.h"
  42. #include "service_table.h"
  43. #include "sock.h"
  44. #include "ThreadPool.h"
  45. #include "upnp.h"
  46. #include "UpnpString.h"
  47. #include "uri.h"
  48. #ifdef __cplusplus
  49. #define EXTERN_C extern "C"
  50. #else /* __cplusplus */
  51. #ifndef EXTERN_C
  52. #define EXTERN_C
  53. #endif /* EXTERN_C */
  54. #endif /* __cplusplus */
  55. /*!
  56. * \brief XML version comment. Not used because it is not interopeable with
  57. * other UPnP vendors.
  58. */
  59. #define XML_VERSION "<?xml version='1.0' encoding='ISO-8859-1' ?>\n"
  60. #define XML_PROPERTYSET_HEADER \
  61. "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n"
  62. #define UNABLE_MEMORY "HTTP/1.1 500 Internal Server Error\r\n\r\n"
  63. #define UNABLE_SERVICE_UNKNOWN "HTTP/1.1 404 Not Found\r\n\r\n"
  64. #define UNABLE_SERVICE_NOT_ACCEPT "HTTP/1.1 503 Service Not Available\r\n\r\n"
  65. #define NOT_IMPLEMENTED "HTTP/1.1 501 Not Implemented\r\n\r\n"
  66. #define BAD_REQUEST "HTTP/1.1 400 Bad Request\r\n\r\n"
  67. #define INVALID_NT BAD_CALLBACK
  68. #define BAD_CALLBACK "HTTP/1.1 412 Precondition Failed\r\n\r\n"
  69. #define HTTP_OK_CRLF "HTTP/1.1 200 OK\r\n\r\n"
  70. #define HTTP_OK_STR "HTTP/1.1 200 OK\r\n"
  71. #define INVALID_SID BAD_CALLBACK
  72. #define MISSING_SID BAD_CALLBACK
  73. #define MAX_CONTENT_LENGTH 20
  74. #define MAX_SECONDS 10
  75. #define MAX_EVENTS 20
  76. #define MAX_PORT_SIZE 10
  77. #define GENA_E_BAD_RESPONSE UPNP_E_BAD_RESPONSE
  78. #define GENA_E_BAD_SERVICE UPNP_E_INVALID_SERVICE
  79. #define GENA_E_SUBSCRIPTION_UNACCEPTED UPNP_E_SUBSCRIBE_UNACCEPTED
  80. #define GENA_E_BAD_SID UPNP_E_INVALID_SID
  81. #define GENA_E_UNSUBSCRIBE_UNACCEPTED UPNP_E_UNSUBSCRIBE_UNACCEPTED
  82. #define GENA_E_NOTIFY_UNACCEPTED UPNP_E_NOTIFY_UNACCEPTED
  83. #define GENA_E_NOTIFY_UNACCEPTED_REMOVE_SUB -9
  84. #define GENA_E_BAD_HANDLE UPNP_E_INVALID_HANDLE
  85. #define XML_ERROR -5
  86. #define XML_SUCCESS UPNP_E_SUCCESS
  87. #define GENA_SUCCESS UPNP_E_SUCCESS
  88. #define CALLBACK_SUCCESS 0
  89. #define DEFAULT_TIMEOUT 1801
  90. extern ithread_mutex_t GlobalClientSubscribeMutex;
  91. /*!
  92. * \brief Locks the subscription.
  93. */
  94. #define SubscribeLock() \
  95. UpnpPrintf(UPNP_INFO, GENA, __FILE__, __LINE__, \
  96. "Trying Subscribe Lock"); \
  97. ithread_mutex_lock(&GlobalClientSubscribeMutex); \
  98. UpnpPrintf(UPNP_INFO, GENA, __FILE__, __LINE__, \
  99. "Subscribe Lock");
  100. /*!
  101. * \brief Unlocks the subscription.
  102. */
  103. #define SubscribeUnlock() \
  104. UpnpPrintf(UPNP_INFO, GENA, __FILE__, __LINE__, \
  105. "Trying Subscribe UnLock"); \
  106. ithread_mutex_unlock(&GlobalClientSubscribeMutex); \
  107. UpnpPrintf(UPNP_INFO, GENA, __FILE__, __LINE__, \
  108. "Subscribe UnLock");
  109. /*!
  110. * Structure to send NOTIFY message to all subscribed control points
  111. */
  112. typedef struct NOTIFY_THREAD_STRUCT {
  113. char *headers;
  114. DOMString propertySet;
  115. char *servId;
  116. char *UDN;
  117. Upnp_SID sid;
  118. time_t ctime;
  119. int *reference_count;
  120. UpnpDevice_Handle device_handle;
  121. } notify_thread_struct;
  122. /*!
  123. * \brief This is the callback function called by the miniserver to handle
  124. * incoming GENA requests.
  125. *
  126. * \return UPNP_E_SUCCESS if successful, otherwise appropriate error code.
  127. */
  128. EXTERN_C void genaCallback(
  129. /*! [in] represents the parse state of the request */
  130. http_parser_t *parser,
  131. /*! [in] HTTP message containing GENA request */
  132. http_message_t* request,
  133. /*! [in,out] Structure containing information about the socket */
  134. SOCKINFO *info);
  135. /*!
  136. * \brief This function subscribes to a PublisherURL (also mentioned as EventURL
  137. * in some places).
  138. *
  139. * It sends SUBSCRIBE http request to service processes request. Finally adds a
  140. * Subscription to the clients subscription list, if service responds with OK.
  141. *
  142. * \return UPNP_E_SUCCESS if service response is OK, otherwise returns the
  143. * appropriate error code
  144. */
  145. #ifdef INCLUDE_CLIENT_APIS
  146. EXTERN_C int genaSubscribe(
  147. /*! [in] The client handle. */
  148. UpnpClient_Handle client_handle,
  149. /*! [in] Of the form: "http://134.134.156.80:4000/RedBulb/Event */
  150. const UpnpString *PublisherURL,
  151. /*! [in,out] requested Duration:
  152. * \li if -1, then "infinite".
  153. * \li in the OUT case: actual Duration granted by Service,
  154. * -1 for infinite. */
  155. int *TimeOut,
  156. /*! [out] sid of subscription, memory passed in by caller. */
  157. UpnpString *out_sid);
  158. #endif /* INCLUDE_CLIENT_APIS */
  159. /*!
  160. * \brief Unsubscribes a SID.
  161. *
  162. * It first validates the SID and client_handle,copies the subscription, sends
  163. * UNSUBSCRIBE http request to service processes request and finally removes
  164. * the subscription.
  165. *
  166. * \return UPNP_E_SUCCESS if service response is OK, otherwise returns the
  167. * appropriate error code.
  168. */
  169. #ifdef INCLUDE_CLIENT_APIS
  170. EXTERN_C int genaUnSubscribe(
  171. /*! [in] UPnP client handle. */
  172. UpnpClient_Handle client_handle,
  173. /*! [in] The subscription ID. */
  174. const UpnpString *in_sid);
  175. #endif /* INCLUDE_CLIENT_APIS */
  176. /*!
  177. * \brief Unsubcribes all the outstanding subscriptions and cleans the
  178. * subscription list.
  179. *
  180. * This function is called when control point unregisters.
  181. *
  182. * \returns UPNP_E_SUCCESS if successful, otherwise returns the appropriate
  183. * error code.
  184. */
  185. #ifdef INCLUDE_CLIENT_APIS
  186. EXTERN_C int genaUnregisterClient(
  187. /*! [in] Handle containing all the control point related information. */
  188. UpnpClient_Handle client_handle);
  189. #endif /* INCLUDE_CLIENT_APIS */
  190. /*
  191. * DEVICE
  192. */
  193. /*!
  194. * \brief Cleans the service table of the device.
  195. *
  196. * \return UPNP_E_SUCCESS if successful, otherwise returns GENA_E_BAD_HANDLE
  197. */
  198. #ifdef INCLUDE_DEVICE_APIS
  199. EXTERN_C int genaUnregisterDevice(
  200. /*! [in] Handle of the root device */
  201. UpnpDevice_Handle device_handle);
  202. #endif /* INCLUDE_CLIENT_APIS */
  203. /*!
  204. * \brief Renews a SID.
  205. *
  206. * It first validates the SID and client_handle and copies the subscription.
  207. * It sends RENEW (modified SUBSCRIBE) http request to service and processes
  208. * the response.
  209. *
  210. * \return UPNP_E_SUCCESS if service response is OK, otherwise the
  211. * appropriate error code.
  212. */
  213. #ifdef INCLUDE_CLIENT_APIS
  214. EXTERN_C int genaRenewSubscription(
  215. /*! [in] Client handle. */
  216. UpnpClient_Handle client_handle,
  217. /*! [in] Subscription ID. */
  218. const UpnpString *in_sid,
  219. /*! [in,out] requested Duration, if -1, then "infinite". In the OUT case:
  220. * actual Duration granted by Service, -1 for infinite. */
  221. int *TimeOut);
  222. #endif /* INCLUDE_CLIENT_APIS */
  223. /*!
  224. * \brief Sends a notification to all the subscribed control points.
  225. *
  226. * \return int
  227. *
  228. * \note This function is similar to the genaNotifyAllExt. The only difference
  229. * is it takes event variable array instead of xml document.
  230. */
  231. #ifdef INCLUDE_DEVICE_APIS
  232. EXTERN_C int genaNotifyAll(
  233. /*! [in] Device handle. */
  234. UpnpDevice_Handle device_handle,
  235. /*! [in] Device udn. */
  236. char *UDN,
  237. /*! [in] Service ID. */
  238. char *servId,
  239. /*! [in] Array of varible names. */
  240. char **VarNames,
  241. /*! [in] Array of variable values. */
  242. char **VarValues,
  243. /*! [in] Number of variables. */
  244. int var_count);
  245. #endif /* INCLUDE_DEVICE_APIS */
  246. /*!
  247. * \brief Sends a notification to all the subscribed control points.
  248. *
  249. * \return int
  250. *
  251. * \note This function is similar to the genaNotifyAll. the only difference
  252. * is it takes the document instead of event variable array.
  253. */
  254. #ifdef INCLUDE_DEVICE_APIS
  255. EXTERN_C int genaNotifyAllExt(
  256. /*! [in] Device handle. */
  257. UpnpDevice_Handle device_handle,
  258. /*! [in] Device udn. */
  259. char *UDN,
  260. /*! [in] Service ID. */
  261. char *servId,
  262. /*! [in] XML document Event varible property set. */
  263. IXML_Document *PropSet);
  264. #endif /* INCLUDE_DEVICE_APIS */
  265. /*!
  266. * \brief Sends the intial state table dump to newly subscribed control point.
  267. *
  268. * \return GENA_E_SUCCESS if successful, otherwise the appropriate error code.
  269. *
  270. * \note No other event will be sent to this control point before the
  271. * intial state table dump.
  272. */
  273. #ifdef INCLUDE_DEVICE_APIS
  274. EXTERN_C int genaInitNotify(
  275. /*! [in] Device handle. */
  276. UpnpDevice_Handle device_handle,
  277. /*! [in] Device udn. */
  278. char *UDN,
  279. /*! [in] Service ID. */
  280. char *servId,
  281. /*! [in] Array of variable names. */
  282. char **VarNames,
  283. /*! [in] Array of variable values. */
  284. char **VarValues,
  285. /*! [in] Array size. */
  286. int var_count,
  287. /*! [in] Subscription ID. */
  288. const Upnp_SID sid);
  289. #endif /* INCLUDE_DEVICE_APIS */
  290. /*!
  291. * \brief Similar to the genaInitNofity. The only difference is that it
  292. * takes the xml document for the state table and sends the intial state
  293. * table dump to newly subscribed control point.
  294. *
  295. * \return GENA_E_SUCCESS if successful, otherwise the appropriate error code.
  296. *
  297. * \note No other event will be sent to this control point before the
  298. * intial state table dump.
  299. */
  300. #ifdef INCLUDE_DEVICE_APIS
  301. EXTERN_C int genaInitNotifyExt(
  302. /*! [in] Device handle. */
  303. UpnpDevice_Handle device_handle,
  304. /*! [in] Device udn. */
  305. char *UDN,
  306. /*! [in] Service ID. */
  307. char *servId,
  308. /*! [in] Document of the state table. */
  309. IXML_Document *PropSet,
  310. /*! [in] subscription ID. */
  311. const Upnp_SID sid);
  312. #endif /* INCLUDE_DEVICE_APIS */
  313. /*!
  314. * \brief Sends an error message to the control point in the case of incorrect
  315. * GENA requests.
  316. *
  317. * \return UPNP_E_SUCCESS if successful, otherwise appropriate error code.
  318. */
  319. void error_respond(
  320. /*! [in] Structure containing information about the socket. */
  321. SOCKINFO *info,
  322. /*! [in] error code that will be in the GENA response. */
  323. int error_code,
  324. /*! [in] GENA request Packet. */
  325. http_message_t* hmsg);
  326. #endif /* GENA_H */