Integrated controller connection manager
[apps/native/gear-racing-car.git] / src / message.c
1 /*
2  * Copyright (c) 2017 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 #include <time.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <glib.h>
21
22 #include "log.h"
23 #include "message.h"
24
25 static unsigned long long int sequence_number = 0;
26 static GQueue inqueue = G_QUEUE_INIT;
27 static GQueue outqueue = G_QUEUE_INIT;
28
29 static unsigned long long int __message_get_monotonic_time(void)
30 {
31         unsigned long long int c_time = 0;
32         struct timespec ts;
33         int ret = 0;
34
35         ret = clock_gettime(CLOCK_MONOTONIC, &ts);
36         if (ret)
37                 _E("failed to get monotonic time");
38         else
39                 c_time = (((unsigned long long int)ts.tv_sec) * 1000000)
40                         + (ts.tv_nsec / 1000);
41
42         return c_time;
43 }
44
45 int message_new_to_send(message_cmd_e cmd,
46         int servo, int speed, message_s *new_msg)
47 {
48         retv_if(!new_msg, -1);
49
50         new_msg->seq_num = sequence_number++;
51         new_msg->cmd = cmd;
52         new_msg->servo = servo;
53         new_msg->speed = speed;
54         new_msg->time = __message_get_monotonic_time();
55
56         if (new_msg->seq_num >= ULLONG_MAX) {
57                 /* maybe never reach here */
58                 _W("seq number reachs max value, reset it to 0");
59                 sequence_number = 0;
60         }
61
62         return 0;
63 }
64
65 void message_reset_seq_num(void)
66 {
67         sequence_number = 0;
68
69         return;
70 }
71
72 int message_queue_new(void)
73 {
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.
77          */
78         return 0;
79 }
80
81 static void __queue_clear_cb(gpointer data, gpointer user_data)
82 {
83         free(data);
84         return;
85 }
86
87 void message_queue_clear(void)
88 {
89         g_queue_foreach(&inqueue, __queue_clear_cb, NULL);
90         g_queue_clear(&inqueue);
91
92         g_queue_foreach(&outqueue, __queue_clear_cb, NULL);
93         g_queue_clear(&outqueue);
94
95         return;
96 }
97
98 void message_push_to_inqueue(message_s *msg)
99 {
100         g_queue_push_tail(&inqueue, msg);
101         _D("seq[%llu] is pushed to in-queue", msg->seq_num);
102         return;
103 }
104
105 void message_push_to_outqueue(message_s *msg)
106 {
107         g_queue_push_tail(&outqueue, msg);
108         _D("seq[%llu] is pushed to out-queue", msg->seq_num);
109         return;
110 }
111
112 message_s *message_pop_from_inqueue(void)
113 {
114         return (message_s *)g_queue_pop_head(&inqueue);
115 }
116
117 message_s *message_pop_from_outqueue(void)
118 {
119         return (message_s *)g_queue_pop_head(&outqueue);
120 }