connection.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _CONNECTION_H_
  6. #define _CONNECTION_H_
  7. #include "bi-inc/attr_container.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. struct _connection;
  12. typedef struct _connection connection_t;
  13. /* Connection event type */
  14. typedef enum {
  15. /* Data is received */
  16. CONN_EVENT_TYPE_DATA = 1,
  17. /* Connection is disconnected */
  18. CONN_EVENT_TYPE_DISCONNECT
  19. } conn_event_type_t;
  20. /*
  21. * @typedef on_connection_event_f
  22. *
  23. * @param conn the connection that the event belongs to
  24. * @param type event type
  25. * @param data the data received for CONN_EVENT_TYPE_DATA event
  26. * @param len length of the data in byte
  27. * @param user_data user data
  28. */
  29. typedef void (*on_connection_event_f)(connection_t *conn,
  30. conn_event_type_t type, const char *data,
  31. uint32 len, void *user_data);
  32. /*
  33. *****************
  34. * Connection API's
  35. *****************
  36. */
  37. /*
  38. * @brief Open a connection.
  39. *
  40. * @param name name of the connection, "TCP", "UDP" or "UART"
  41. * @param args connection arguments, such as: ip:127.0.0.1, port:8888
  42. * @param on_event callback function called when event occurs
  43. * @param user_data user data
  44. *
  45. * @return the connection or NULL means fail
  46. */
  47. connection_t *
  48. api_open_connection(const char *name, attr_container_t *args,
  49. on_connection_event_f on_event, void *user_data);
  50. /*
  51. * @brief Close a connection.
  52. *
  53. * @param conn connection
  54. */
  55. void
  56. api_close_connection(connection_t *conn);
  57. /*
  58. * Send data to the connection in non-blocking manner which returns immediately
  59. *
  60. * @param conn the connection
  61. * @param data data buffer to be sent
  62. * @param len length of the data in byte
  63. *
  64. * @return actual length sent, or -1 if fail(maybe underlying buffer is full)
  65. */
  66. int
  67. api_send_on_connection(connection_t *conn, const char *data, uint32 len);
  68. /*
  69. * @brief Configure connection.
  70. *
  71. * @param conn the connection
  72. * @param cfg configurations
  73. *
  74. * @return true if success, false otherwise
  75. */
  76. bool
  77. api_config_connection(connection_t *conn, attr_container_t *cfg);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif