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.
17 #ifndef MESSAGE_COMMAND_C
18 #define MESSAGE_COMMAND_C
22 #include "messages/message_command.h"
23 #include "messages/macros.h"
25 static int _message_command_serialze_vcall(message_t *msg, writer_t *writer)
27 message_command_t *command_msg = container_of(msg, message_command_t, base);
28 return message_command_serialize(command_msg, writer);
31 static int _message_command_deserialize_vcall(message_t *msg, reader_t *reader)
33 message_command_t *command_msg = container_of(msg, message_command_t, base);
34 return message_command_deserialize(command_msg, reader);
37 static void _message_command_destroy_vcall(message_t *msg)
39 message_command_t *command_msg = container_of(msg, message_command_t, base);
40 message_command_destroy(command_msg);
43 void message_command_init(message_command_t *message)
45 message_base_init(&message->base);
47 message_set_type(&message->base, MESSAGE_COMMAND);
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;
53 memset(&message->command, 0x0, sizeof(command_s));
56 void message_command_destroy(message_command_t *message)
58 message_base_destroy(&message->base);
61 static int _camera_command_deserialize(command_s *cmd, reader_t *reader)
66 err |= reader_read_int32(reader, &speed);
67 err |= reader_read_int32(reader, &dir);
71 cmd->data.steering.speed = speed;
72 cmd->data.steering.direction = dir;
77 static int _drive_command_deserialize(command_s *cmd, reader_t *reader)
79 int32_t elevation, azimuth;
82 err |= reader_read_int32(reader, &elevation);
83 err |= reader_read_int32(reader, &azimuth);
87 cmd->data.camera_position.camera_elevation = elevation;
88 cmd->data.camera_position.camera_azimuth = azimuth;
93 static int _command_deserialize(command_s *cmd, reader_t *reader)
97 if (reader_read_int32(reader, &type)) {
104 case COMMAND_TYPE_DRIVE:
105 return _drive_command_deserialize(cmd, reader);
106 case COMMAND_TYPE_CAMERA:
107 return _camera_command_deserialize(cmd, reader);
109 case COMMAND_TYPE_NONE:
116 static int _camera_command_serialize(command_s *cmd, writer_t *writer)
118 int32_t speed = cmd->data.steering.speed;
119 int32_t dir = cmd->data.steering.direction;
122 err |= writer_write_int32(writer, speed);
123 err |= writer_write_int32(writer, dir);
128 static int _drive_command_serialize(command_s *cmd, writer_t *writer)
130 int32_t elevation = cmd->data.camera_position.camera_elevation;
131 int32_t azimuth = cmd->data.camera_position.camera_azimuth;
134 err |= writer_write_int32(writer, elevation);
135 err |= writer_write_int32(writer, azimuth);
140 static int _command_serialize(command_s *cmd, writer_t *writer)
142 int32_t type = cmd->type;
144 if (writer_write_int32(writer, type)) {
149 case COMMAND_TYPE_DRIVE:
150 return _drive_command_serialize(cmd, writer);
151 case COMMAND_TYPE_CAMERA:
152 return _camera_command_serialize(cmd, writer);
154 case COMMAND_TYPE_NONE:
161 int message_command_deserialize(message_command_t *message, reader_t *reader)
165 err |= message_base_deserialize(&message->base, reader);
166 err |= _command_deserialize(&message->command, reader);
171 int message_command_serialize(message_command_t *message, writer_t *writer)
175 err |= message_base_serialize(&message->base, writer);
176 err |= _command_serialize(&message->command, writer);
181 const command_s *message_command_get_command(message_command_t *message)
183 return &message->command;
186 void message_command_set_command(message_command_t *message, const command_s *cmd)
188 memcpy(&message->command, cmd, sizeof(command_s));
191 #endif /* end of include guard: MESSAGE_COMMAND_C */