Added implementation of UDP connection
[apps/native/gear-racing-car.git] / inc / udp_connection.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef INC_UDP_CONNECTION_H_
18 #define INC_UDP_CONNECTION_H_
19
20 /**
21  * @brief Called whenever new UDP data arrives.
22  * @param[in] data Pointer to data that arrived.
23  * @param[in] size Size of data in bytes.
24  * @param[in] address Address of source of data.
25  * @param[in] port Port of data source.
26  */
27 typedef void (*udp_receive_cb)(const char *data, unsigned int size, const char *address, int port);
28
29 /**
30  * @brief Structure of data about udp_connection.
31  */
32 typedef struct udp_connection udp_connection_t;
33
34 /**
35  * @brief Creates UDP connection object.
36  * @param[in] port Local port on which creation should be stablished.
37  * @return new udp_connection object on success, NULL otherwise.
38  * @remarks Function allocates resources that have to be freed with udp_communication_destroy.
39  */
40 udp_connection_t *udp_connection_create(int port);
41
42 /**
43  * @brief Sends data on given address.
44  * @param[in] connection UDP connection object.
45  * @param[in] data Data to be sent.
46  * @param[in] size Size in bytes of data pointed by data pointer.
47  * @param[in] address IP address of receiver.
48  * @param[in] port Port of receiver.
49  * @return 0 on success, -1 otherwise.
50  */
51 int udp_connection_send(udp_connection_t *connection, const char *data, unsigned short int size, const char *address, int port);
52
53 /**
54  * @brief Sets callback for receiving data.
55  * @param[in] connection UDP connection object.
56  * @param[in] callback Callback to be set or NULL to unregister.
57  * @remarks Function is passed pointer to data and its size.
58  */
59 void udp_connection_set_receive_cb(udp_connection_t *connection, udp_receive_cb callback);
60
61 /**
62  * @brief Stops UDP communication and releases all resources allocated by udp_communication_create.
63  * @param[in] connection UDP connection object.
64  */
65 void udp_connection_destroy(udp_connection_t *connection);
66
67 #endif /* INC_UDP_CONNECTION_H_ */