Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / otherservice / rpc_library / library / include / rpc_internal.h
1 /*
2  * @copyright Copyright (c) 2016-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
17 /**
18  * @file rpc_internal.h
19  * @brief RPC Library Internal Implementation-Type, macro, and function definitions used internally
20  *
21  */
22 /** @addtogroup RPClib_in
23  */
24 #ifndef OTHERSERVICE_RPCINTERNAL_H_  // NOLINT(build/header_guard)
25 #define OTHERSERVICE_RPCINTERNAL_H_  // NOLINT(build/header_guard)
26
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #if defined(__linux__)
31 /* Using unix autobind Features That Are Only Available in linux */
32 #define RPC_USE_UNIX_AUTOBIND
33 #endif /* __linux__ */
34
35 #define RPC_USE_SYSLOG
36
37 typedef RPC_ID *RPC_ID_p;
38
39 #include "rpc_udp.h"
40 #include "rpc_thread.h"
41
42 RPC_Result RpcQueueAPIRequestBefore(RpcIdInfo *id,
43          UINT32 size, char **buff);
44 RPC_Result RpcQueueAPIRequestAfter(RpcIdInfo *id, RPC_ID client,
45          const char *mesg, UINT32 size,
46          char *args);
47 void RpcFreeAPIArgsString(char *args_string);
48 UINT16 RpcGetAPIRequest(RpcIdInfo *id, RPC_ID_p client,
49       char **args_string, unsigned int *args_size);
50 RPC_Result RpcSetAPIcallReturn(RpcIdInfo *id,
51           const char *mesg, UINT32 size);
52 void RpcDiscardAPIcallReturn(RpcIdInfo *id);
53
54 RPC_Result RpcDeleteSrvrPid(RpcIdInfo *idinfo, RPC_ID srvr_rpc_id, int wd);
55
56 #define StrEqual(a, b) (strcmp((a), (b)) == 0)
57 #define MemEqual(a, b, c) (memcmp((a), (b), (c)) == 0)
58
59 #define RPC_IS_INVALID_ID(a) ((a) == RPC_NO_ID)
60 #define RPC_INVALIDATE_ID(a) ((a) = RPC_NO_ID)
61 #define RPC_ID_COPY(a, b) ((a) = (b))
62 #define RPC_ID_Equal(a, b) ((a) == (b))
63
64 /* inotify Events read Sizes */
65 #define EVENT_SIZE  ( sizeof(struct inotify_event) )  /* One inotify event size */
66 #define BUF_LEN     (4 * EVENT_SIZE)  /* Read sizes */
67
68 #define LESS_THAN <
69
70 #define SET_NONBLOCK(fd) { \
71     int flag; \
72     flag = fcntl((fd), F_GETFL, 0); \
73     fcntl((fd), F_SETFL, O_NONBLOCK|flag); \
74   }
75
76 #define SET_CLOSE_ON_EXEC(fd) { \
77     int flag; \
78     flag = fcntl((fd), F_GETFD, 0); \
79     fcntl((fd), F_SETFD, FD_CLOEXEC|flag); \
80   }
81
82 #if defined(RPC_USE_SYSLOG)
83
84 #include <syslog.h>
85
86 #define RPC_LOG_CRIT(format, arg...)  syslog(LOG_CRIT, format "\n", ##arg)
87 #define RPC_LOG_ERR(format, arg...)   syslog(LOG_ERR, format "\n", ##arg)
88 #define RPC_LOG_STATE(format, arg...) syslog(LOG_INFO, format "\n", ##arg)
89 #define RPC_LOG_DEBUG(format, arg...) syslog(LOG_DEBUG, format "\n", ##arg)
90
91 #else  // defined(RPC_USE_SYSLOG)
92
93 #define RPC_LOG_CRIT(format, arg...)  fprintf(stderr, "[RPC:CRIT](%d): " format "\n", getpid(), ##arg)
94 #define RPC_LOG_ERR(format, arg...)   fprintf(stderr, "[RPC:ERR](%d): " format "\n", getpid(), ##arg)
95 #define RPC_LOG_STATE(format, arg...) fprintf(stderr, "[RPC:STATE](%d): " format "\n", getpid(), ##arg)
96 #define RPC_LOG_DEBUG(format, arg...)
97
98 #endif  // defined(RPC_USE_SYSLOG)
99
100 #include <sys/prctl.h>
101
102 #define RPC_LOG_ERR_W_NAME(format, arg...) \
103 do { \
104   char name[16]; \
105   prctl(PR_GET_NAME, name); \
106   name[15] = '\0'; \
107   RPC_LOG_ERR("(%s): " format, name, ##arg); \
108 } while (0)
109
110 #define RPC_LOG_PERROR(format, arg...) RPC_LOG_ERR("[%s:%d] %s : " format, __FILE__, __LINE__, strerror(errno), ##arg);
111
112 #include <assert.h>
113 #define rpc_assert assert
114
115 #define BUG_ASSERT(cond, x) \
116   if (!(cond)) { \
117     rpc_assert("BUG ASSERTION! " #x " @ " __FILE__ ==NULL); \
118   }
119 #define CONFIG_ASSERT(s) rpc_assert((s) == NULL)
120
121 #define RUNS_IN_CALLERS_THREAD
122 #define RUNS_IN_READING_THREAD
123
124 #if !defined(RPC_USE_UNIX_AUTOBIND)
125
126 #define RPC_SOCKET_NAME "/tmp/RPC/%05d"
127
128 static inline void
129 rpc_set_socket_name(char *str, RPC_ID id) {
130   sprintf(str, RPC_SOCKET_NAME, id);  // NOLINT (readability/nolint)
131 }
132
133 #endif /* !AUTOBIND */
134
135 #endif  // OTHERSERVICE_RPCINTERNAL_H_