d4a2d9665e2b70e39d99a84d9677e0e14940e944
[apps/native/gear-racing-car.git] / inc / messages / message.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 MESSAGE_H_
18 #define MESSAGE_H_
19
20 #include "messages/writer.h"
21 #include "messages/reader.h"
22
23 /**
24  * @brief message types
25  */
26 typedef enum message_type {
27         MESSAGE_NONE,
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_type_e;
36
37 #define IP_ADDRESS_LEN 15
38
39 typedef struct message message_t;
40
41 /**
42  * @brief message data
43  */
44 struct message {
45         int64_t serial;
46         int64_t timestamp;
47         int32_t type;
48         int sender_port;
49         int receiver_port;
50         char sender_ip[IP_ADDRESS_LEN+1];
51         char receiver_ip[IP_ADDRESS_LEN+1];
52         struct {
53                 int (*deserialize)(message_t *msg, reader_t *reader);
54                 int (*serialize)(message_t *msg, writer_t *writer);
55                 void (*destroy)(message_t *msg);
56         } vtable;
57 };
58
59 /**
60  * @brief Destroys message.
61  *
62  * @param[in] message message object.
63  *
64  * @note this function is implementing polymorphic behaviour of
65  * message_t object. The the destroy is made via vtable->destroy pointer.
66  */
67 void message_destroy(message_t *message);
68
69 /**
70  * @brief Serializes message into writer's buffer.
71  *
72  * @param[in] message message object.
73  * @param[in] writer writer object.
74  *
75  * @return 0 on success, other value on failure.
76  *
77  * @note this function is implementing polymorphic behaviour of
78  * message_t object. The serialization is made via vtable->serialize pointer.
79  */
80 int message_serialize(message_t *message, writer_t *writer);
81
82 /**
83  * @brief Deserializes message from reader's buffer.
84  *
85  * @param[in] message message object.
86  * @param[in] reader reader object.
87  *
88  * @return 0 on success, other value on failure.
89  *
90  * @note this function is implementing polymorphic behaviour of
91  * message_t object. The deserialization is made via vtable->serialize pointer.
92  */
93 int message_deserialize(message_t *message, reader_t *reader);
94
95 /**
96  * @brief Get reciever of the message
97  *
98  * @param[in] message message object.
99  * @param[out] ip address of reciever.
100  * @param[out] port port of the reciever.
101  *
102  * @note if reciever is not set the @ip will be empty string.
103  */
104 void message_get_receiver(message_t *messsage, const char **ip, int *port);
105
106 /**
107  * @brief Set reciever of the message.
108  *
109  * @param[in] message message object.
110  * @param[in] ip address of reciever.
111  * @param[in] port port of the reciever.
112  */
113 void message_set_receiver(message_t *messsage, const char *ip, int port);
114
115 /**
116  * @brief Get sender of the message.
117  *
118  * @param[in] message message object.
119  * @param[out] ip address of sender.
120  * @param[out] port port of the sender.
121  *
122  * @note if sender is not set the @ip will be empty string.
123  */
124 void message_get_sender(message_t *message, const char **ip, int *port);
125
126 /**
127  * @brief Set sender of the message.
128  *
129  * @param[in] message message object.
130  * @param[in] ip address of sender.
131  * @param[in] port port of the sender.
132  */
133 void message_set_sender(message_t *message, const char *ip, int port);
134
135 /**
136  * @brief Get timestamp of the message.
137  *
138  * @param[in] message message object.
139  *
140  * @return Unix timestamp.
141  */
142 time_t message_get_timestamp(message_t *messsage);
143
144 /**
145  * @brief Set timestamp of the message.
146  *
147  * @param[in] message message object.
148  * @param[in] time Unix timestamp.
149  */
150 void message_set_timestamp(message_t *messsage, time_t time);
151
152 /**
153  * @brief Get type of the message.
154  *
155  * @param[in] message message object.
156  *
157  * @return message type.
158  */
159 message_type_e message_get_type(message_t *message);
160
161 /**
162  * @brief Set type of the message.
163  *
164  * @param[in] message message object.
165  * @param[in] type message type.
166  */
167 void message_set_type(message_t *message, message_type_e type);
168
169 /**
170  * @brief Get serial number of the message. Serial numbers
171  * can be used to verify order of the messages.
172  *
173  * @param[in] message message object.
174  *
175  * @return message serial number.
176  */
177 int64_t message_get_serial(message_t *message);
178
179 /**
180  * @brief Initializes base message object. The main
181  * reponsibilities of this functions are:
182  * - zeroing memory.
183  * - assigning serial number.
184  *
185  * @param[in] message message object.
186  *
187  * @note the purpose of this function is to be used in
188  * derived classes.
189  */
190 void message_base_init(message_t *msg);
191
192 /**
193  * @brief Destroys base message object.
194  *
195  * @param[in] message message object.
196  *
197  * @note unlike @message_destroy, this function is performing
198  * real destroy on message_t object.
199  *
200  * @note the purpose of this function is to be used in
201  * derived classes.
202  */
203 void message_base_destroy(message_t *msg);
204
205 /**
206  * @brief Deserializes base message from reader's buffer.
207  *
208  * @param[in] message message object.
209  * @param[in] reader reader object.
210  *
211  * @note unlike @message_deserialize, this function is performing
212  * real deserialzation on message_t object.
213  *
214  * @note the purpose of this function is to be used in
215  * derived classes.
216  */
217 int message_base_deserialize(message_t *msg, reader_t *reader);
218
219 /**
220  * @brief Serializes base message into writer's buffer.
221  *
222  * @param[in] message message object.
223  * @param[in] writer writer object.
224  *
225  * @note unlike @message_serialize, this function is performing
226  * real serialzation on message_t object.
227  *
228  * @note the purpose of this function is to be used in
229  * derived classes.
230  */
231 int message_base_serialize(message_t *msg, writer_t *writer);
232
233 #endif /* MESSAGE_H_ */