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