netlink_notifier.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /****************************************************************************
  2. * net/netlink/netlink_notifier.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <assert.h>
  26. #include <nuttx/wqueue.h>
  27. #include "netlink/netlink.h"
  28. /****************************************************************************
  29. * Public Functions
  30. ****************************************************************************/
  31. /****************************************************************************
  32. * Name: netlink_notifier_setup
  33. *
  34. * Description:
  35. * Set up to perform a callback to the worker function the Netlink
  36. * response data is received. The worker function will execute on the low
  37. * priority worker thread.
  38. *
  39. * Input Parameters:
  40. * worker - The worker function to execute on the low priority work
  41. * queue when Netlink response data is available.
  42. * conn - The Netlink connection where the response is expected.
  43. * arg - A user-defined argument that will be available to the worker
  44. * function when it runs.
  45. *
  46. * Returned Value:
  47. * Zero (OK) is returned if the notification was successfully set up.
  48. * A negated error value is returned if an unexpected error occurred
  49. * and no notification will occur.
  50. *
  51. ****************************************************************************/
  52. int netlink_notifier_setup(worker_t worker, FAR struct netlink_conn_s *conn,
  53. FAR void *arg)
  54. {
  55. struct work_notifier_s info;
  56. DEBUGASSERT(worker != NULL && conn != NULL);
  57. if (conn->key > 0)
  58. {
  59. return -EBUSY;
  60. }
  61. /* This is just a simple wrapper around work_notifer_setup(). */
  62. info.evtype = WORK_NETLINK_RESPONSE;
  63. info.qid = LPWORK;
  64. info.qualifier = conn;
  65. info.arg = arg;
  66. info.worker = worker;
  67. conn->key = work_notifier_setup(&info);
  68. return conn->key;
  69. }
  70. /****************************************************************************
  71. * Name: netlink_notifier_teardown
  72. *
  73. * Description:
  74. * Eliminate a Netlink response notification previously setup by
  75. * netlink_notifier_setup(). This function should only be called if the
  76. * notification should be aborted prior to the notification. The
  77. * notification will automatically be torn down after the notification.
  78. *
  79. * Input Parameters:
  80. * conn - Teardown the notification for this Netlink connection.
  81. *
  82. * Returned Value:
  83. * Zero (OK) is returned on success; a negated errno value is returned on
  84. * any failure.
  85. *
  86. ****************************************************************************/
  87. int netlink_notifier_teardown(FAR struct netlink_conn_s *conn)
  88. {
  89. DEBUGASSERT(conn != NULL);
  90. int ret = OK;
  91. /* This is just a simple wrapper around work_notifier_teardown(). */
  92. if (conn->key > 0)
  93. {
  94. ret = work_notifier_teardown(conn->key);
  95. conn->key = 0;
  96. }
  97. return ret;
  98. }
  99. /****************************************************************************
  100. * Name: netlink_notifier_signal
  101. *
  102. * Description:
  103. * New Netlink response data is available. Execute worker thread
  104. * functions for all threads that wait for response data.
  105. *
  106. * Input Parameters:
  107. * conn - The Netlink connection where the response was just buffered.
  108. *
  109. * Returned Value:
  110. * None.
  111. *
  112. ****************************************************************************/
  113. void netlink_notifier_signal(FAR struct netlink_conn_s *conn)
  114. {
  115. /* This is just a simple wrapper around work_notifier_signal(). */
  116. work_notifier_signal(WORK_NETLINK_RESPONSE, conn);
  117. }