From c4f2a53fadd4872d5ccf6d8e5100a354f9ca1c4a Mon Sep 17 00:00:00 2001 From: Krzysztof Wieclaw Date: Tue, 11 Sep 2018 18:14:17 +0200 Subject: Car Connection API for P2P on UDP Change-Id: I7474259840a066037ace2f7c05a9e9e84d7ae480 Signed-off-by: Krzysztof Wieclaw --- inc/car_connection_manager.h | 84 ++++++++++++++++++++++++++++++++++++++++++++ inc/command.h | 42 ++++++++++++++++++++++ inc/udp_connection.h | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 inc/car_connection_manager.h create mode 100644 inc/command.h create mode 100644 inc/udp_connection.h diff --git a/inc/car_connection_manager.h b/inc/car_connection_manager.h new file mode 100644 index 0000000..fbf68c3 --- /dev/null +++ b/inc/car_connection_manager.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INC_CAR_CONNECTION_MANAGER_H_ +#define INC_CAR_CONNECTION_MANAGER_H_ + +#include "command.h" + +/** + * @brief Describes state of connection. + */ +typedef enum car_connection_state { + CAR_CONNECTION_STATE_DISCONNECTED, /** There is no connection between controller and car. */ + CAR_CONNECTION_STATE_CONNECTED, /** There is established connection between controller and car. */ + CAR_CONNECTION_STATE_CONNECTING /** Controller attempts to connect to car, connection hasn't been established yet. */ +} car_connection_state_e; + +/** + * @brief Called whenever state of connection changes. + * @param[in] previous Previous state of connection. + * @param[in] current Current state of connection. + */ +typedef void (*connection_state_cb)(car_connection_state_e previous, car_connection_state_e current); + +/** + * @brief Inits communication manager on the given port. + * @param[in] port Port on which application will be listening. + * @return 0 on success, -1 otherwise. + * @remarks This function allocates resources that have to be freed with controller_car_manager_fini. + */ +int car_connection_manager_init(int port); + +/** + * @brief Gets currect connection state. + * @return Connection state. + */ +car_connection_state_e car_connection_manager_get_state(); + +/** + * @brief Sets callback function called whenever connection state changes. + * @param[in] callback Callback function to be set. + * @remarks Function is passed two arguments - previous state and actual one. + */ +void car_connection_manager_set_state_change_cb(connection_state_cb callback); + +/** + * @brief Connects to device with given address on the given port. + * @param[in] address IP address of the device. + * @param[in] port Port of target device on which connection should be established. + * @return 0 on success, 1 otherwise. + * @remarks When connection is no longer required, car_connection_manager_disconnect should be called. + */ +int car_connection_manager_connect(const char *address, int port); + +/** + * @brief Ends currently existing connection. + */ +void car_connection_manager_disconnect(); + +/** + * @brief Sends data about driving. + * @param[in] command command to be + */ +int car_connection_send_command(command_s command); + +/** + * @brief Destroys connection manager. + */ +void car_connection_manager_fini(); + +#endif /* INC_CAR_CONNECTION_MANAGER_H_ */ diff --git a/inc/command.h b/inc/command.h new file mode 100644 index 0000000..2d8ac3c --- /dev/null +++ b/inc/command.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @brief Types of commands used in communication. + */ +typedef enum command_type { + COMMAND_TYPE_NONE, /** Command doesn't carry any information */ + COMMAND_TYPE_DRIVE, /** Command carries information about steering included in data.steering. */ + COMMAND_TYPE_CAMERA /** Command carries information about camera position in data.camera_position. */ +} command_type_e; + +/** + * @brief Structure with info about speed and direction that should be set. + */ +typedef struct __command { + command_type_e type; /** Type of command. */ + union { + struct { + int speed; /** Speed to be set from range [-10000, 10000]. */ + int direction; /** Direction to be set from range [-10000, 10000]. */ + } steering; + struct { + int camera_elevation; /** Elevation of camera to be set from range [-10000, 10000]. */ + int camera_azimuth; /** Azimuth of camera to be set from range [-10000, 10000]. */ + } camera_position; + } data; +} command_s; diff --git a/inc/udp_connection.h b/inc/udp_connection.h new file mode 100644 index 0000000..e2d41dd --- /dev/null +++ b/inc/udp_connection.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INC_UDP_CONNECTION_H_ +#define INC_UDP_CONNECTION_H_ + +/** + * @brief Called whenever new UDP data arrives. + * @param[in] data Pointer to data that arrived. + * @param[in] size Size of data in bytes. + * @param[in] address Address of source of data. + * @param[in] port Port of data source. + */ +typedef void (*udp_receive_cb)(const char *data, unsigned int size, const char *address, int port); + +/** + * @brief Structure of data about udp_connection. + */ +typedef struct udp_connection udp_connection_t; + +/** + * @brief Creates UDP connection object. + * @param[in] port Local port on which creation should be stablished. + * @return new udp_connection object on success, NULL otherwise. + * @remarks Function allocates resources that have to be freed with udp_communication_destroy. + */ +udp_connection_t *udp_connection_create(int port); + +/** + * @brief Sets the receiver of sent data. + * @param[in] connection UDP connection object. + * @param[in] address IP address of receiver. + * @param[in] port Port of receiver to send data on. + * @return 0 on success, -1 otherwise. + */ +int udp_connection_set_receiver(udp_connection_t *connection, const char *address, int port); + +/** + * @brief Sends data to set receiver. + * @param[in] connection UDP connection object. + * @param[in] data Data to be sent. + * @param[in] size Size in bytes of data pointed by data pointer. + * @return 0 on success, -1 otherwise. + */ +int udp_connection_send(udp_connection_t *connection, const char *data, unsigned int size); + +/** + * @brief Sets callback for receiving data. + * @param[in] connection UDP connection object. + * @param[in] callback Callback to be set or NULL to unregister. + * @remarks Function is passed pointer to data and its size. + */ +void udp_connection_set_receive_cb(udp_connection_t *connection, udp_receive_cb callback); + +/** + * @brief Stops UDP communication and releases all resources allocated by udp_communication_create. + * @param[in] connection UDP connection object. + */ +void udp_connection_destroy(udp_connection_t *connection); + +#endif /* INC_UDP_CONNECTION_H_ */ -- cgit v1.2.3