input_hal branch
[staging/toyota.git] / src / input_util.cpp
1 /*
2  * @copyright Copyright (c) 2018-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include "input_util.h"
17
18 #include <native_service/ns_message_center_if.h>
19 #include <stdio.h>
20
21 #include "input_hal.h"
22 #include "input_hal_debug.h"
23
24 #define INPUT_UTIL_MESSAGE_SEND_RETRY    3
25 #define INPUT_UTIL_MESSAGE_SEND_WAIT     10000   /* RetryWait:10ms */
26 /*
27  * InputUtilListAdd
28  */
29 void InputUtilListAdd(struct InputUtilList *node_new, struct InputUtilList *node_head) {
30   node_new->prev = node_head;
31   node_new->next  = node_head->next;
32   node_head->next = node_new;
33   node_new->next->prev = node_new;
34 }
35
36 /*
37  * InputUtilListDelete
38  */
39 void InputUtilListDelete(struct InputUtilList *node) {
40   node->prev->next = node->next;
41   node->next->prev = node->prev;
42 }
43
44 /*
45  * InputUtilMCSend
46  */
47 int InputUtilMCSend(HANDLE h_message, PCSTR source, UI_32 cmd, UI_32 length, PCVOID data) {
48   int       i = 0;
49   EFrameworkunifiedStatus      e_status;                              /* return value */
50
51   do {
52     e_status = McSend(h_message, source, cmd, length, data);
53     if (eFrameworkunifiedStatusOK == e_status) {
54       break;
55     }
56     InputUtilSleep(INPUT_UTIL_MESSAGE_SEND_WAIT);
57   } while (i++ < INPUT_UTIL_MESSAGE_SEND_RETRY);
58
59   if (e_status != eFrameworkunifiedStatusOK) {
60     INPUT_ERROR_LOG("ERR: MessageSend=%d \n", e_status);
61   }
62
63   return e_status;
64 }
65
66 /*
67  * InputUtilSleep
68  */
69 int InputUtilSleep(int usec) {
70   struct timespec  req;
71
72   req.tv_sec = 0;
73   req.tv_nsec = usec * 1000;
74   nanosleep(&req, NULL);
75
76   return HAL_INPUT_RET_NORMAL;
77 }