Added new class in command.h file
[apps/native/gear-racing-car.git] / src / messages / message_command.c
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_COMMAND_C
18 #define MESSAGE_COMMAND_C
19
20 #include <string.h>
21
22 #include "messages/message_command.h"
23 #include "messages/macros.h"
24
25 static int _message_command_serialze_vcall(message_t *msg, writer_t *writer)
26 {
27         message_command_t *command_msg = container_of(msg, message_command_t, base);
28         return message_command_serialize(command_msg, writer);
29 }
30
31 static int _message_command_deserialize_vcall(message_t *msg, reader_t *reader)
32 {
33         message_command_t *command_msg = container_of(msg, message_command_t, base);
34         return message_command_deserialize(command_msg, reader);
35 }
36
37 static void _message_command_destroy_vcall(message_t *msg)
38 {
39         message_command_t *command_msg = container_of(msg, message_command_t, base);
40         message_command_destroy(command_msg);
41 }
42
43 void message_command_init(message_command_t *message)
44 {
45         message_base_init(&message->base);
46
47         message_set_type(&message->base, MESSAGE_COMMAND);
48
49         message->base.vtable.serialize = _message_command_serialze_vcall;
50         message->base.vtable.deserialize = _message_command_deserialize_vcall;
51         message->base.vtable.destroy = _message_command_destroy_vcall;
52
53         memset(&message->command, 0x0, sizeof(command_s));
54 }
55
56 void message_command_destroy(message_command_t *message)
57 {
58         message_base_destroy(&message->base);
59 }
60
61 static int _camera_command_deserialize(command_s *cmd, reader_t *reader)
62 {
63         int32_t elevation, azimuth;
64         int err = 0;
65
66         err |= reader_read_int32(reader, &elevation);
67         err |= reader_read_int32(reader, &azimuth);
68
69         if (err) return err;
70
71         cmd->data.camera_position.camera_elevation = elevation;
72         cmd->data.camera_position.camera_azimuth = azimuth;
73
74         return 0;
75 }
76
77 static int _drive_command_deserialize(command_s *cmd, reader_t *reader)
78 {
79
80         int32_t speed, dir;
81         int err = 0;
82
83         err |= reader_read_int32(reader, &speed);
84         err |= reader_read_int32(reader, &dir);
85
86         if (err) return err;
87
88         cmd->data.steering.speed = speed;
89         cmd->data.steering.direction = dir;
90
91         return 0;
92 }
93
94 static int _drive_and_camera_command_deserialize(command_s *cmd, reader_t *reader)
95 {
96
97         int32_t speed, dir;
98         int32_t elevation, azimuth;
99         int err = 0;
100
101         err |= reader_read_int32(reader, &speed);
102         err |= reader_read_int32(reader, &dir);
103         err |= reader_read_int32(reader, &elevation);
104         err |= reader_read_int32(reader, &azimuth);
105
106         if (err) return err;
107
108         cmd->data.steering_and_camera.speed = speed;
109         cmd->data.steering_and_camera.direction = dir;
110         cmd->data.steering_and_camera.camera_elevation = elevation;
111         cmd->data.steering_and_camera.camera_azimuth = azimuth;
112
113         return 0;
114 }
115
116 static int _command_deserialize(command_s *cmd, reader_t *reader)
117 {
118         int32_t type;
119
120         if (reader_read_int32(reader, &type)) {
121                 return -1;
122         }
123
124         cmd->type = type;
125
126         switch (type) {
127                 case COMMAND_TYPE_DRIVE:
128                         return _drive_command_deserialize(cmd, reader);
129                 case COMMAND_TYPE_CAMERA:
130                         return _camera_command_deserialize(cmd, reader);
131                         break;
132                 case COMMAND_TYPE_DRIVE_AND_CAMERA:
133                         return _drive_and_camera_command_deserialize(cmd, reader);
134                         break;
135                 case COMMAND_TYPE_NONE:
136                         return 0;
137                 default:
138                         return -1;
139         }
140 }
141
142 static int _camera_command_serialize(command_s *cmd, writer_t *writer)
143 {
144         int32_t elevation = cmd->data.camera_position.camera_elevation;
145         int32_t azimuth = cmd->data.camera_position.camera_azimuth;
146         int err = 0;
147
148         err |= writer_write_int32(writer, elevation);
149         err |= writer_write_int32(writer, azimuth);
150
151         return err;
152 }
153
154 static int _drive_command_serialize(command_s *cmd, writer_t *writer)
155 {
156         int32_t speed = cmd->data.steering.speed;
157         int32_t dir = cmd->data.steering.direction;
158         int err = 0;
159
160         err |= writer_write_int32(writer, speed);
161         err |= writer_write_int32(writer, dir);
162
163         return err;
164 }
165
166 static int _drive_and_camera_command_serialize(command_s *cmd, writer_t *writer)
167 {
168         int32_t speed = cmd->data.steering_and_camera.speed;
169         int32_t dir = cmd->data.steering_and_camera.direction;
170         int32_t elevation = cmd->data.steering_and_camera.camera_elevation;
171         int32_t azimuth = cmd->data.steering_and_camera.camera_azimuth;
172         int err = 0;
173
174         err |= writer_write_int32(writer, speed);
175         err |= writer_write_int32(writer, dir);
176         err |= writer_write_int32(writer, elevation);
177         err |= writer_write_int32(writer, azimuth);
178
179         return err;
180 }
181
182 static int _command_serialize(command_s *cmd, writer_t *writer)
183 {
184         int32_t type = cmd->type;
185
186         if (writer_write_int32(writer, type)) {
187                 return -1;
188         }
189
190         switch (cmd->type) {
191                 case COMMAND_TYPE_DRIVE:
192                         return _drive_command_serialize(cmd, writer);
193                 case COMMAND_TYPE_CAMERA:
194                         return _camera_command_serialize(cmd, writer);
195                         break;
196                 case COMMAND_TYPE_DRIVE_AND_CAMERA:
197                         return _drive_and_camera_command_serialize(cmd, writer);
198                         break;
199                 case COMMAND_TYPE_NONE:
200                         return 0;
201                 default:
202                         return -1;
203         }
204 }
205
206 int message_command_deserialize(message_command_t *message, reader_t *reader)
207 {
208         int err = 0;
209
210         err |= message_base_deserialize(&message->base, reader);
211         err |= _command_deserialize(&message->command, reader);
212
213         return err;
214 }
215
216 int message_command_serialize(message_command_t *message, writer_t *writer)
217 {
218         int err = 0;
219
220         err |= message_base_serialize(&message->base, writer);
221         err |= _command_serialize(&message->command, writer);
222
223         return err;
224 }
225
226 const command_s *message_command_get_command(message_command_t *message)
227 {
228         return &message->command;
229 }
230
231 void message_command_set_command(message_command_t *message, const command_s *cmd)
232 {
233         memcpy(&message->command, cmd, sizeof(command_s));
234 }
235
236 #endif /* end of include guard: MESSAGE_COMMAND_C */