2 * Copyright (c) 2017 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.
25 static unsigned long long int sequence_number = 0;
26 static GQueue inqueue = G_QUEUE_INIT;
27 static GQueue outqueue = G_QUEUE_INIT;
29 static unsigned long long int __message_get_monotonic_time(void)
31 unsigned long long int c_time = 0;
35 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
37 _E("failed to get monotonic time");
39 c_time = (((unsigned long long int)ts.tv_sec) * 1000000)
40 + (ts.tv_nsec / 1000);
45 int message_new_to_send(message_cmd_e cmd,
46 int servo, int speed, message_s *new_msg)
48 retv_if(!new_msg, -1);
50 new_msg->seq_num = sequence_number++;
52 new_msg->servo = servo;
53 new_msg->speed = speed;
54 new_msg->time = __message_get_monotonic_time();
56 if (new_msg->seq_num >= ULLONG_MAX) {
57 /* maybe never reach here */
58 _W("seq number reachs max value, reset it to 0");
65 void message_reset_seq_num(void)
72 int message_queue_new(void)
74 /* Do nothing because we use static queue
75 * if we use multiple thread to handling messages,
76 * message queue should be changed to thread-safe one.
81 static void __queue_clear_cb(gpointer data, gpointer user_data)
87 void message_queue_clear(void)
89 g_queue_foreach(&inqueue, __queue_clear_cb, NULL);
90 g_queue_clear(&inqueue);
92 g_queue_foreach(&outqueue, __queue_clear_cb, NULL);
93 g_queue_clear(&outqueue);
98 void message_push_to_inqueue(message_s *msg)
100 g_queue_push_tail(&inqueue, msg);
101 _D("seq[%llu] is pushed to in-queue", msg->seq_num);
105 void message_push_to_outqueue(message_s *msg)
107 g_queue_push_tail(&outqueue, msg);
108 _D("seq[%llu] is pushed to out-queue", msg->seq_num);
112 message_s *message_pop_from_inqueue(void)
114 return (message_s *)g_queue_pop_head(&inqueue);
117 message_s *message_pop_from_outqueue(void)
119 return (message_s *)g_queue_pop_head(&outqueue);