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 */
35 MESSAGE_CONFIG_USER_NAME /** User name to be set */
38 #define IP_ADDRESS_LEN 15
40 typedef struct message message_t;
51 char sender_ip[IP_ADDRESS_LEN+1];
52 char receiver_ip[IP_ADDRESS_LEN+1];
54 int (*deserialize)(message_t *msg, reader_t *reader);
55 int (*serialize)(message_t *msg, writer_t *writer);
56 void (*destroy)(message_t *msg);
61 * @brief Destroys message.
63 * @param[in] message message object.
65 * @note this function is implementing polymorphic behaviour of
66 * message_t object. The the destroy is made via vtable->destroy pointer.
68 void message_destroy(message_t *message);
71 * @brief Serializes message into writer's buffer.
73 * @param[in] message message object.
74 * @param[in] writer writer object.
76 * @return 0 on success, other value on failure.
78 * @note this function is implementing polymorphic behaviour of
79 * message_t object. The serialization is made via vtable->serialize pointer.
81 int message_serialize(message_t *message, writer_t *writer);
84 * @brief Deserializes message from reader's buffer.
86 * @param[in] message message object.
87 * @param[in] reader reader object.
89 * @return 0 on success, other value on failure.
91 * @note this function is implementing polymorphic behaviour of
92 * message_t object. The deserialization is made via vtable->serialize pointer.
94 int message_deserialize(message_t *message, reader_t *reader);
97 * @brief Get reciever of the message
99 * @param[in] message message object.
100 * @param[out] ip address of reciever.
101 * @param[out] port port of the reciever.
103 * @note if reciever is not set the @ip will be empty string.
105 void message_get_receiver(message_t *messsage, const char **ip, int *port);
108 * @brief Set reciever of the message.
110 * @param[in] message message object.
111 * @param[in] ip address of reciever.
112 * @param[in] port port of the reciever.
114 void message_set_receiver(message_t *messsage, const char *ip, int port);
117 * @brief Get sender of the message.
119 * @param[in] message message object.
120 * @param[out] ip address of sender.
121 * @param[out] port port of the sender.
123 * @note if sender is not set the @ip will be empty string.
125 void message_get_sender(message_t *message, const char **ip, int *port);
128 * @brief Set sender of the message.
130 * @param[in] message message object.
131 * @param[in] ip address of sender.
132 * @param[in] port port of the sender.
134 void message_set_sender(message_t *message, const char *ip, int port);
137 * @brief Get timestamp of the message.
139 * @param[in] message message object.
141 * @return Unix timestamp.
143 time_t message_get_timestamp(message_t *messsage);
146 * @brief Set timestamp of the message.
148 * @param[in] message message object.
149 * @param[in] time Unix timestamp.
151 void message_set_timestamp(message_t *messsage, time_t time);
154 * @brief Get type of the message.
156 * @param[in] message message object.
158 * @return message type.
160 message_type_e message_get_type(message_t *message);
163 * @brief Set type of the message.
165 * @param[in] message message object.
166 * @param[in] type message type.
168 void message_set_type(message_t *message, message_type_e type);
171 * @brief Get serial number of the message. Serial numbers
172 * can be used to verify order of the messages.
174 * @param[in] message message object.
176 * @return message serial number.
178 int64_t message_get_serial(message_t *message);
181 * @brief Initializes base message object. The main
182 * reponsibilities of this functions are:
184 * - assigning serial number.
186 * @param[in] message message object.
188 * @note the purpose of this function is to be used in
191 void message_base_init(message_t *msg);
194 * @brief Destroys base message object.
196 * @param[in] message message object.
198 * @note unlike @message_destroy, this function is performing
199 * real destroy on message_t object.
201 * @note the purpose of this function is to be used in
204 void message_base_destroy(message_t *msg);
207 * @brief Deserializes base message from reader's buffer.
209 * @param[in] message message object.
210 * @param[in] reader reader object.
212 * @note unlike @message_deserialize, this function is performing
213 * real deserialzation on message_t object.
215 * @note the purpose of this function is to be used in
218 int message_base_deserialize(message_t *msg, reader_t *reader);
221 * @brief Serializes base message into writer's buffer.
223 * @param[in] message message object.
224 * @param[in] writer writer object.
226 * @note unlike @message_serialize, this function is performing
227 * real serialzation on message_t object.
229 * @note the purpose of this function is to be used in
232 int message_base_serialize(message_t *msg, writer_t *writer);
234 #endif /* MESSAGE_H_ */