2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
20 #include "messages/writer.h"
21 #include "messages/reader.h"
24 * @brief message types
26 typedef enum message_type {
28 MESSAGE_CONNECT, /** Connection request */
29 MESSAGE_CONNECT_ACCEPTED, /** Connection accepted reply */
30 MESSAGE_CONNECT_REFUSED, /** Connection refused reply */
31 MESSAGE_KEEP_ALIVE, /** Keep alive request */
32 MESSAGE_ACK, /** Message delivery confirmation */
33 MESSAGE_COMMAND, /** Message with command data */
34 MESSAGE_BYE /** Connection end request */
37 #define IP_ADDRESS_LEN 15
39 typedef struct message message_t;
50 char sender_ip[IP_ADDRESS_LEN+1];
51 char receiver_ip[IP_ADDRESS_LEN+1];
53 int (*deserialize)(message_t *msg, reader_t *reader);
54 int (*serialize)(message_t *msg, writer_t *writer);
55 void (*destroy)(message_t *msg);
60 * @brief Destroys message.
62 * @param[in] message message object.
64 * @note this function is implementing polymorphic behaviour of
65 * message_t object. The the destroy is made via vtable->destroy pointer.
67 void message_destroy(message_t *message);
70 * @brief Serializes message into writer's buffer.
72 * @param[in] message message object.
73 * @param[in] writer writer object.
75 * @return 0 on success, other value on failure.
77 * @note this function is implementing polymorphic behaviour of
78 * message_t object. The serialization is made via vtable->serialize pointer.
80 int message_serialize(message_t *message, writer_t *writer);
83 * @brief Deserializes message from reader's buffer.
85 * @param[in] message message object.
86 * @param[in] reader reader object.
88 * @return 0 on success, other value on failure.
90 * @note this function is implementing polymorphic behaviour of
91 * message_t object. The deserialization is made via vtable->serialize pointer.
93 int message_deserialize(message_t *message, reader_t *reader);
96 * @brief Get reciever of the message
98 * @param[in] message message object.
99 * @param[out] ip address of reciever.
100 * @param[out] port port of the reciever.
102 * @note if reciever is not set the @ip will be empty string.
104 void message_get_receiver(message_t *messsage, const char **ip, int *port);
107 * @brief Set reciever of the message.
109 * @param[in] message message object.
110 * @param[in] ip address of reciever.
111 * @param[in] port port of the reciever.
113 void message_set_receiver(message_t *messsage, const char *ip, int port);
116 * @brief Get sender of the message.
118 * @param[in] message message object.
119 * @param[out] ip address of sender.
120 * @param[out] port port of the sender.
122 * @note if sender is not set the @ip will be empty string.
124 void message_get_sender(message_t *message, const char **ip, int *port);
127 * @brief Set sender of the message.
129 * @param[in] message message object.
130 * @param[in] ip address of sender.
131 * @param[in] port port of the sender.
133 void message_set_sender(message_t *message, const char *ip, int port);
136 * @brief Get timestamp of the message.
138 * @param[in] message message object.
140 * @return Unix timestamp.
142 time_t message_get_timestamp(message_t *messsage);
145 * @brief Set timestamp of the message.
147 * @param[in] message message object.
148 * @param[in] time Unix timestamp.
150 void message_set_timestamp(message_t *messsage, time_t time);
153 * @brief Get type of the message.
155 * @param[in] message message object.
157 * @return message type.
159 message_type_e message_get_type(message_t *message);
162 * @brief Set type of the message.
164 * @param[in] message message object.
165 * @param[in] type message type.
167 void message_set_type(message_t *message, message_type_e type);
170 * @brief Get serial number of the message. Serial numbers
171 * can be used to verify order of the messages.
173 * @param[in] message message object.
175 * @return message serial number.
177 int64_t message_get_serial(message_t *message);
180 * @brief Initializes base message object. The main
181 * reponsibilities of this functions are:
183 * - assigning serial number.
185 * @param[in] message message object.
187 * @note the purpose of this function is to be used in
190 void message_base_init(message_t *msg);
193 * @brief Destroys base message object.
195 * @param[in] message message object.
197 * @note unlike @message_destroy, this function is performing
198 * real destroy on message_t object.
200 * @note the purpose of this function is to be used in
203 void message_base_destroy(message_t *msg);
206 * @brief Deserializes base message from reader's buffer.
208 * @param[in] message message object.
209 * @param[in] reader reader object.
211 * @note unlike @message_deserialize, this function is performing
212 * real deserialzation on message_t object.
214 * @note the purpose of this function is to be used in
217 int message_base_deserialize(message_t *msg, reader_t *reader);
220 * @brief Serializes base message into writer's buffer.
222 * @param[in] message message object.
223 * @param[in] writer writer object.
225 * @note unlike @message_serialize, this function is performing
226 * real serialzation on message_t object.
228 * @note the purpose of this function is to be used in
231 int message_base_serialize(message_t *msg, writer_t *writer);
233 #endif /* MESSAGE_H_ */