Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / otherservice / rpc_library / library / include / rpc_udp.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 #ifndef OTHERSERVICE_RPCUDP_H_  // NOLINT(build/header_guard)
18 #define OTHERSERVICE_RPCUDP_H_  // NOLINT(build/header_guard)
19
20 /** @addtogroup RPClib_in */
21 /** @{ */
22 /** Number of bytes of payload that can be stored in a UDP packet */
23 #define RPC_UDP_PAYLOAD RPC_MAX_API_ARG_TOTAL_SIZE
24
25 /** UDP packet header
26  *
27  * Packet type, source ID, sequence number, packet size, packet position
28  */
29 #define RPC_PACKET_HEADER "%-2d %-5x %-8x %-4u %1d "
30 /** Number of bytes in the UDP packet header
31  */
32 #define RPC_PACKET_HEADER_LEN (2+1 + 5+1 + 8+1 + 4+1 + 1+1)
33
34 /** Number of bytes to store in the UDP packet */
35 #define RPC_UDP_PACKET_SIZE (RPC_UDP_PAYLOAD + RPC_PACKET_HEADER_LEN)
36
37 #define RPC_COMMAND_LEN 3
38
39 /** Format string for UDP packet header analysis */
40 #define RPC_PACKET_HEADER_scanf "%d %x %x %d"
41
42 /** Header (API number) to be added to the API call packet */
43 #define RPC_APICALL_FORMAT "%-4d "
44 /** Number of bytes in the header of an API call packet */
45 #define RPC_APICALL_FORMAT_ARGS_START 5
46
47 #define RPC_MAX_RESPONSE_MESSAGE_SIZE 5
48
49 #define RPC_SEQ_NUM_START 1
50 #define RPC_SEQ_NUM_INVALID 0
51
52 /** Internal API call number for checking server existence */
53 #define RPC_API_NUM_RPC_ALIVE 0
54
55 /** Type of packet that the RPC library sends */
56 typedef enum {
57   RPC_PACKET_APICALL = 0,/**< API call request */
58
59   RPC_RESPONSE_APICALL,/**< Responding to API Call Requests */
60 #define RPC_RESPONSE_NONE 0x0000
61 #define RPC_RESPONSE_API_OK 0x0001
62 #define RPC_RESPONSE_API_BUSY 0x0002
63 #define RPC_RESPONSE_API_ERR 0x0004
64 #define RPC_RESPONSE_API_DEADLOCK 0x0005  /* Server deadlock */
65 #define RPC_RESPONSE_API_CERTIFY 0x0006    /* Authentication request */
66
67   RPC_PACKET_APIRETURN,/**< Result of the API call */
68
69   RPC_PACKET_NONE,
70 } RPC_packet_type;
71
72 /** Packet Location (Multi-Fragment Packet Support) */
73 typedef enum {
74   RPC_PACKET_POS_ONEANDONLY = 0,/**< Complete with one packet */
75 } rpc_packet_position;
76
77 /** Transmitting buffer */
78 typedef struct {
79   unsigned int bytes;
80   const void *buf;
81 } rpc_send_buf;
82
83 /** Valid bytes in the sockaddr_un struct specified for send */
84 #define RPC_SOCKET_NAME_LEN 6 /**< Name: 5 characters + first NULL bytes */
85 #define RPC_SOCKET_ADDR_LEN (sizeof(short) + RPC_SOCKET_NAME_LEN)  // NOLINT (readability/nolint)
86
87 enum {
88   RPC_SEND_TO_CLIENT,
89   RPC_SEND_TO_SERVER,
90   RPC_SEND_TO_SERVER_NO_RETRY,
91 };
92
93 struct RpcIdInfo;
94
95 int RpcSendUdp(struct RpcIdInfo *id, RPC_ID receiver, int direction,
96      RPC_packet_type type, const void *mesg, unsigned int bytes);
97 int RpcSendUdp2(struct RpcIdInfo *id, RPC_ID receiver, int direction,
98                   RPC_packet_type type, unsigned int num, rpc_send_buf *sendbuf);
99 RPC_Result RpcSendUdpResponse(struct RpcIdInfo *id, RPC_ID receiver,
100          int direction,
101          RPC_packet_type type,
102          UINT32 seq_num, char *mesg, UINT32 bytes);
103
104 int RpcReadUdpPacket(const struct RpcIdInfo *id, UINT8 *buf);
105
106 RPC_Result RpcParsePacketHeader(const char *str, RPC_packet_type *command,
107            RPC_ID_p id, UINT32 *seq_num, UINT32 *size);
108
109 RPC_Result RpcClientWaitResponse(struct RpcIdInfo *idinfo,
110             UINT32 seq_num,
111             UINT32 timeout_msec, UINT16 *response);
112 RPC_Result RpcClientWaitResult(struct RpcIdInfo *idinfo, RPC_ID srvr_id);
113
114 #if defined(RPC_USE_UNIX_AUTOBIND)
115 /* ===pathname of datagram sockets for API requests=== */
116 static inline void
117 RpcSetServerName(char *str, RPC_ID id) {
118   *str = '\0';
119   sprintf(str + 1, "S%04x", id);  // NOLINT (readability/nolint)
120 }
121
122 static inline void
123 RpcSetClientName(char *str, RPC_ID id) {
124   *str = '\0';
125   sprintf(str + 1, "%05x", id);  // NOLINT (readability/nolint)
126 }
127
128 static inline void
129 RpcGetClientName(const char *str, RPC_ID *id) {
130   char buf[6];
131   memcpy(buf, str + 1, 5);
132   buf[5] = '\0';
133   if (buf[1] == 'X') {
134     *id = (RPC_ID)(strtoul(buf + 1, NULL, 16));
135   } else {
136     *id = (RPC_ID)(strtoul(buf, NULL, 16));
137   }
138 }
139
140 /* ===pathname of stream-socket for authenticating=== */
141 static inline void
142 RpcSetServerSecureName(char *str, RPC_ID id) {
143   *str = '\0';
144   sprintf(str + 1, "secure_S%04x", id);  // NOLINT (readability/nolint)
145 }
146
147 static inline void
148 RpcSetClientSecureName(char *str, RPC_ID id) {
149   *str = '\0';
150   sprintf(str + 1, "secure_%05x", id);  // NOLINT (readability/nolint)
151 }
152
153 static inline void
154 RpcGetClientNameFromSock(const char *str, char *client_sock_name) {
155   memcpy(client_sock_name, str + 8, 5);
156   client_sock_name[5] = '\0';
157 }
158
159 #endif /* !AUTOBIND */
160
161 /** @} */
162 #endif  // OTHERSERVICE_RPCUDP_H_