diff options
Diffstat (limited to 'service/other/rpc_library/library/include')
6 files changed, 1439 insertions, 0 deletions
diff --git a/service/other/rpc_library/library/include/other_service/rpc.h b/service/other/rpc_library/library/include/other_service/rpc.h new file mode 100755 index 0000000..f669331 --- /dev/null +++ b/service/other/rpc_library/library/include/other_service/rpc.h @@ -0,0 +1,765 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file rpc.h + * @brief \~english RPC library --API define header file + */ +/** @addtogroup BaseSystem + * @{ + */ +/** @addtogroup other_service + * @ingroup BaseSystem + * @{ + */ +/** @addtogroup rpc_library + * @ingroup other_service + * @{ + */ +#ifndef OTHERSERVICE_RPC_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPC_H_ // NOLINT(build/header_guard) + +#include <stdio.h> +#include <unistd.h> + +#include <other_service/rpc_id.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* @brief \~english RPC_ID max number +*/ +#define RPC_MAX_THREADS_IN_PROCESS 64 + +/** @brief \~english fd max number in one process. + * 0 = main/sub thread communication pipe fd. + * 1 = inotify() process listen fd. + * 2~129 = even num : API request data socket. + * odd num : secure stream socket. + * */ +#define RPC_MAX_FD_IN_PROCESS 130 + + +#define RPC_MAX_API_ARG_NUM 8 +///< \~english API call parameter max num + + +#define RPC_MAX_API_ARG_SIZE 1024 +///< \~english API call one parameter max byte + +#define RPC_MAX_API_ARG_TOTAL_SIZE 1500 +///< \~english API call all parameter max byte + +#define RPC_MAX_APICALL_QUEUE 16 +///< \~english API call max queue num + +#define RPC_NO_PORT 0 +///< \~english invalid port num + +/** @brief \~english RPC library function return value + */ +enum rpc_result { // NOLINT (readability/nolint) + RPC_OK = 0, ///< \~english ok + RPC_ERR_No_Response = 0x8fff0000, ///< \~english has no response + RPC_ERR_Timeout, ///< \~english timeout + RPC_ERR_Busy, ///< \~english busy + RPC_ERR_API_Error, ///< \~english API error + RPC_ERR_API_Fatal, ///< \~english API fatal + RPC_ERR_Fatal, ///< \~english fatal + RPC_ERR_Configuration, ///< \~english configuration + RPC_ERR_Server_DeadLock, ///< \~english server deadlock + RPC_ERR_Server_Finish, ///< \~english server finish + RPC_ERR_Reject_connect, ///< \~english reject connect +}; +/** @brief \~english RPC library function return type + * @see rpc_result + * */ +typedef INT32 RPC_Result; + +/** @brief \~english RPC ID + * + */ +typedef UINT32 RPC_ID; + +#define RPC_NO_ID RPC_NO_PORT +///< \~english The ID when destination is invalid + +#define RPC_SELF_ID RPC_NO_PORT +///< \~english The ID when destination is self + +/** @brief \~english The ID when destination is any + * */ +#define RPC_ANY_ID RPC_NO_PORT + + +typedef RPC_Result (*RPC_dispatch_func_t)(UINT16 api_num, + const char *args_string, + unsigned int args_size, + char **ret_string, + unsigned int *ret_bytes); + +#ifdef RPC_DISPATCH_FUNC +#error "Please include <rpc.h> before \"*_srvr.h\"." +#else /* !RPC_DISPATCH_FUNC */ +#define RPC_DISPATCH_FUNC NULL +#endif /* !RPC_DISPATCH_FUNC */ + +/* RPC Library Start Function Macro */ +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_START_SERVER +/// \~english @par Brief +/// server start(certification of the client by UID) +/// \~english @param [in] ID +/// RPC_ID - program's RPC_ID +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Case of an input parameter error +/// - Case of fail to information generation +/// - When RPC_start has already been issued +/// - Case of space reservation fails +/// - Case of socket generation fails +/// - Case of socket allocation fails +/// - Case of creating a pipe fails +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - Used when the server program of RPC-API performs RPC library use start processing +/// - ID specifies a symbol in the form of XXX_RPC_ID defined by rpc_id.h +/// - Only one server program is permitted per thread +/// - When the server program calls the API as a client of another server, +/// it only needs to call RPC_START_SERVER, there is no need to call RPC_START_CLIENT +/// \~english @see RPC_start, RPC_end +//////////////////////////////////////////////////////////////////////////////////// +#define RPC_START_SERVER(ID) RPC_start((ID), RPC_DISPATCH_FUNC, NULL, NO_SECURE_CHECK) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_START_SECURE_SERVER +/// \~english @par Brief +/// server start(certification of the client by UID) +/// \~english @param [in] ID +/// RPC_ID - program's RPC_ID +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Case of an input parameter error +/// - Case of fail to information generation +/// - When RPC_start has already been issued +/// - Case of space reservation fails +/// - Case of socket generation fails +/// - Case of socket allocation fails +/// - Case of creating a pipe fails +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - Used when the server program of RPC-API performs RPC library use start processing +/// - ID specifies a symbol in the form of XXX_RPC_ID defined by rpc_id.h +/// - Only one server program is permitted per thread +/// - When the server program calls the API as a client of another server, +/// it only needs to call RPC_START_SERVER, there is no need to call RPC_START_CLIENT +/// \~english @see RPC_start, RPC_end +//////////////////////////////////////////////////////////////////////////////////// +#define RPC_START_SECURE_SERVER(ID) RPC_start((ID), RPC_DISPATCH_FUNC, NULL, NEED_SECURE_CHECK) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_START_CLIENT +/// \~english @par Brief +/// client start +/// \~english @param [out] pID +/// RPC_ID * - auto allocated program's RPC_ID +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - input parameter error +/// - Unable to get the rpc lib info for the self-thread. +/// - Memory allocation fail for id info. +/// - Socket generation fail. +/// - Socket allocation fail. +/// - Unable to get the socket name. +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - This macro supports only client function of RPC library. +/// - Run the start processing by using this macro only when the APIs provided by the other program are called. +/// \~english @see RPC_start, RPC_end +//////////////////////////////////////////////////////////////////////////////////// +#define RPC_START_CLIENT(pID) RPC_start((UINT16)RPC_NO_ID, NULL, (pID), NO_SECURE_CHECK) +//!< \~english client start + +/* Client authentication by registering the server's UID list */ +#define NEED_SECURE_CHECK 1 ///< \~english has secure check +#define NO_SECURE_CHECK 0 ///< \~english has no secure check + +/* Setting whether to register UID list of server */ +#define REGISTERED 1 ///< \~english registered +#define NO_REGISTERED 0 ///< \~english not registered + +/* API Function Prototypes in the RPC Library */ +RPC_Result RPC_start(RPC_ID id, RPC_dispatch_func_t func, RPC_ID *pID, INT32 secure_check); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_get_fd +/// \~english @par Brief +/// Get the file descriptor to judge whether there is the request of RPC-API call or not. +/// \~english @param [in] id +/// RPC_ID - server program's RPC_ID +/// \~english @param [out] fd +/// int * - area for fd +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// RPC_START_SERVER or RPC_START_SECURE_SERVER is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Argument(id) is RPC_NO_ID. +/// - Argument(fd) is NULL. +/// - Unable to get the rpc lib info for the self-thread. +/// - RPC sub thread doesn't exist. +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - This function returns the fd(file descriptor) to judge whether there is the request to server. +/// - Return value(fd) is used only for poll function or select function parameter. +/// - Usage +/// - To get fd from this function. +/// - Wait for the data to use poll function or select function. +/// - After that, call RPC_process_API_request function. +/// - As a result, the API call request from client is done(that is, run the server API). +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_get_fd(RPC_ID id, int *fd); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_process_API_request +/// \~english @par Brief +/// Run the processing of RPC-API call +/// \~english @param [in] id +/// RPC_ID - server program's RPC_ID +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// RPC_START_SERVER or RPC_START_SECURE_SERVER is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Argument(id) is RPC_NO_ID. +/// - Unable to get the rpc lib info for the self-thread. +/// - RPC sub thread doesn't exist. +/// - fail to send UDP packet. +/// - API processing from the client returns the error. +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - Process all the queued API running requests from the client. +/// - API function of server program is called in this function. +/// - If there are no requests to run API, this function doesn't wait and return immediately. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_process_API_request(RPC_ID id); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_end +/// \~english @par Brief +/// End processing of RPC library. +/// \~english @param [in] id +/// RPC_ID - program's RPC_ID +/// \~english @retval None +/// \~english @par Prerequisite +/// RPC_START_SERVER or RPC_START_SECURE_SERVER or RPC_START_CLIENT is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - End processing(socket close and memory release etc) is done. +/// - When there are no more program that uses RPC_ID, RPC sub thread started by RPC_start will be ended. +/// - Need to call this function before the program is ended. +/// - Need to call this function from which the thread called RPC_start. +/// - Nothing is done when incorrect or no more used RPC_ID is designated. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +void RPC_end(RPC_ID id); // NOLINT (readability/nolint) + +//////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_end_all +/// \~english @par Brief +/// End processing of RPC library(forcing to clean up). +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// Delete the socket files created in process for communication. /n +/// The program which use RPC library must call RPC_end() to free resource, /n +/// This API is used for insurance processing when socket file's link lost. /n +/// 1,Before process ends, call this API. /n +/// When process is ending, auto running socket's close and memory free is not work. /n +/// 2,After called this API, all the RPC's process(both server and client) become to useless. /n +/// Even if after called this API, call RPC_START to repair it also can't make sure to active. /n +/// RPC library which use Unix autobind is nothing to do. /n +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////// +void RPC_end_all(void); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_set_API_timeout +/// \~english @par Brief +/// Set timeout time for server side API processing time +/// \~english @param [in] sec +/// INT32 - timeout time +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @retval RPC_ERR_Configuration Argument is out of range +/// \~english @par Prerequisite +/// RPC_START_SERVER or RPC_START_SECURE_SERVER is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Unable to get the rpc lib info for the self-thread. +/// - Server id info doesn't exist. +/// - Info for API call reception processing doesn't exist. +/// - RPC_ERR_Fatal +/// - The value of the argument(sec) is out of range. +/// - RPC_ERR_Configuration +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// None +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_set_API_timeout(INT32 sec); // NOLINT (readability/nolint) + + +//////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_is_server_ready +/// \~english @par Brief +/// Return RPC server's status +/// \~english @param [in] id +/// RPC_ID - server program's RPC_ID +/// +/// \~english @retval RPC_OK : Success +/// \~english @retval RPC_ERR_No_Response : No response +/// +/// \~english @par Prerequisite +/// - None +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// return the server's work status. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_is_server_ready(RPC_ID id); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_regist_credential +/// \~english @par Brief +/// Register UID and GID list +/// \~english @param [in] uid_num +/// int - UID number to regist(number of elements of UID list) +/// \~english @param [in] uid_list +/// uid_t* - head pointer of UID list +/// \~english @param [in] gid_num +/// int - GID number to regist(number of elements of GID list) +/// \~english @param [in] uid_list +/// gid_t* - head pointer of GID list +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @retval RPC_ERR_Configuration Argument is out of range +/// \~english @par Prerequisite +/// RPC_START_SECURE_SERVER is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Unable to get the rpc lib info for the self-thread. +/// - Server id info doesn't exist. +/// - Info for API call reception processing doesn't exist. +/// - Arguments(client_uid and client_gid) are NULL. +/// - Client authentication check is not necessary. +/// - Authentication info is already registered. +/// - Memory allocation fail for UID and GID lists. +/// - RPC_ERR_Fatal +/// - Values of Arguments(uid_num and gid_num) are out of range. +/// - RPC_ERR_Configuration +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - Register the UID and GID lists acceptable by the RPC server. +/// - If RPC_START_SECURE_SERVER is not called, this function returns the error. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_regist_credential(int uid_num, uid_t *uid_list, int gid_num, gid_t *gid_list); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_get_client_credential +/// \~english @par Brief +/// Get UID and GID of client that requested to run APIs processing +/// \~english @param [out] client_uid +/// uid_t* - UID of client that requested to run APIs processing +/// \~english @param [out] client_gid +/// gid_t* - GID of client that requested to run APIs processing +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal Error +/// \~english @par Prerequisite +/// RPC_START_SECURE_SERVER is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Server id info doesn't exist. +/// - Info for API call reception processing doesn't exist. +/// - Arguments(client_uid and client_gid) are NULL. +/// - Client authentication check is not necessary. +/// - There are no running clients. +/// - Info for the running client is not applicable. +/// - RPC_ERR_Fatal +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - This function is to get UID and GID of client that requested to run APIs processing. +/// - If RPC_START_SECURE_SERVER is not called, this function returns the error. +/// - If the running client doesn't exist, this function returns the error. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_get_client_credential(uid_t *client_uid, gid_t *client_gid); // NOLINT (readability/nolint) + +/* + * The following are only used in stub files + */ +#define RPC_RETCODE_LEN 9 + +#define RPC_MARSHALL_FLAG_BITS_CODE 12 +#define RPC_MARSHALL_FLAG_BITS_IS_VARARRAY 1 +#define RPC_MARSHALL_FLAG_BITS_IS_POINTER 1 +#define RPC_MARSHALL_FLAG_BITS_IN_OUT 2 + +/** @brief \~english APIcall property stored struct + * + */ +typedef union { + UINT32 all; ///< \~english all property + struct { + unsigned int code: RPC_MARSHALL_FLAG_BITS_CODE; ///< \~english value type + /** @brief \~english vararray or not + * + */ + unsigned int is_vararray: RPC_MARSHALL_FLAG_BITS_IS_VARARRAY; + /** @brief \~english pointer or not + * + */ + unsigned int is_pointer: RPC_MARSHALL_FLAG_BITS_IS_POINTER; + /** @brief \~english in/out parameter property + * + */ + unsigned int in_out: RPC_MARSHALL_FLAG_BITS_IN_OUT; + /** @brief \~english string or user define type byte num.other is 0 + * + */ + UINT16 bytes; + } bits; ///< \~english variable properties +} RPC_marshall_flag; + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_API_call +/// \~english @par brief +/// API call execution function (client) +/// \~english @param [in] id +/// RPC_ID - RPC_ID of server program +/// \~english @param [in] api_num +/// UINT16 - Request API number +/// \~english @param [in] args_string +/// const char* - Argument string +/// \~english @param [in] args_size +/// unsigned int - Argument string length +/// \~english @param [out] ret_string +/// char** - Process result string +/// \~english @param [out] ret_size +/// unsigned int* - Process result string length +/// \~english @retval RPC_OK Normal End +/// \~english @retval RPC_ERR_Fatal Fatal error +/// \~english @retval RPC_ERR_No_Response No response +/// \~english @retval RPC_ERR_Busy API call queue overflow +/// \~english @retval RPC_ERR_Server_DeadLock RPC server deadlock +/// \~english @retval RPC_ERR_Reject_connect The RPC server does not allow communication +/// \~english @par Prerequisite +/// The RPC server is running. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Acquisition of rpc library information for self thread fails +/// - Generation of self-thread rpc library information fails +/// - The RPC ID information does not exist +/// - Failed to receive data from the server at initial authentication +/// - Analysis of received data failed during initial authentication +/// - Commands not expected to be received during initial authentication +/// - Server response when initial authentication is API call error +/// - Failed to receive data from the server at API processing request +/// - Analysis of received data failed during API processing request +/// - Commands not expected to be received during API processing request +/// - Server response when API processing request is API call error +/// - Error in server response other than deadlock when processing results are received +/// - Server shut down +/// - RPC_ERR_Fatal +/// - UDP packet transmission fails at initial authentication +/// - A timeout occurred in response from the server during initial authentication +/// - UDP packet transmission failed at API processing request +/// - A timeout occurred in response from the server during API processing request +/// - RPC_ERR_No_Response +/// - Server response when initial authentication is API call queue overflow +/// - Server response when API processing request is API call queue overflow +/// - RPC_ERR_Busy +/// - When the server response is deadlocked on initial authentication +/// - When the server response is deadlocked on API processing request +/// - Server response is deadlocked when processing result is received +/// - RPC_ERR_Server_DeadLock +/// - Server response is not authenticated at initial authentication +/// - RPC_ERR_Reject_connect +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - Request execution of RPC-API from the client side. +/// - However, since this API is called from the automatically generated code, it is not used directly by the user. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +RPC_Result RPC_API_call(RPC_ID id, UINT16 api_num, // NOLINT (readability/nolint) + const char *args_string, unsigned int args_size, + char **ret_string, unsigned int *ret_size); + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_demarshall_arguments +/// \~english @par Brief +/// Confirm whether the input character string conforms to the prescribed format. +/// \~english @param [in] from +/// const char * - Input string +/// \~english @param [in] size +/// unsigned int - Byte length of the input string +/// \~english @param [in] need_alloc +/// int - Flag whether memory allocation is necessary or not. +/// \~english @param [in] num_args +/// int - Number of the variable length arguments +/// \~english @param [in] ... +/// void * - Variable length arguments +/// \~english @retval 0 Normal End +/// \~english @retval -1 Abnormal End +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - Byte length of the input string is 0 bytes or less or larger than 1024 bytes. +/// - The input character string doesn't conform to the prescribed format. +/// - -1 +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// This function is used in the stub file only and user must not call this function directly. +/// \~english @see RPC_marshall_arguments +//////////////////////////////////////////////////////////////////////////////////// +int RPC_demarshall_arguments(const char *from, unsigned int size, // NOLINT (readability/nolint) + int need_alloc, int num_args, ...); + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_marshall_arguments +/// \~english @par Brief +/// Collect variable arguments, convert them to a format that matches the receiver's specification, +/// and stuff them into the message buffer. +/// \~english @param [in] size +/// unsigned int * - data size after conversion +/// \~english @param [in] dont_marshall_out_args +/// int - input/output kind(when the argument is a pointer) +/// \~english @param [in] num_args +/// int - value of variable argument +/// \~english @param [in] ... +/// void * - variable argument +/// \~english @retval char * Message buffer after conversion +/// \~english @retval NULL Abnormal +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// - When marshall work securing work area fails +/// - When the specified type size is abnormal +/// - When securing the area of the message buffer after conversion fails +/// - NULL +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// This function is used in the stub file for client only and user must not call this function directly. +/// \~english @see RPC_demarshalol_arguments +//////////////////////////////////////////////////////////////////////////////////// +char *RPC_marshall_arguments(unsigned int *size, int dont_marshall_out_args, // NOLINT (readability/nolint) + int num_args, ...); + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_marshall_free +/// \~english @par Brief +/// Variadic function for memory release +/// \~english @param [in] num +/// int - number of memory to be released +/// \~english @param [in] ... +/// void * - allocated memory +/// \~english @retval None +/// \~english @par Prerequisite +/// None +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - This function is used in the stub file only and user must not call this function directly. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +void RPC_marshall_free(int num, ...); // NOLINT (readability/nolint) + +///////////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_free_return_string +/// \~english @par Brief +/// release memory allocated during running API call. +/// \~english @param [in] ptr +/// void * - allocated memory +/// \~english @retval None +/// \~english @par Prerequisite +/// RPC_API_call is already called. +/// \~english @par Change of internal state +/// None +/// \~english @par Conditions of processing failure +/// None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// - This function is used in the stub file for client only and user must not call this function directly. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////////// +void RPC_free_return_string(void *ptr); // NOLINT (readability/nolint) + +#ifdef DBG_ENABLE + +//////////////////////////////////////////////////////////////////////////////// +/// \ingroup RPC_record_dbg_log +/// \~english @par Brief +/// RPC-API call process's log +/// \~english @param [in] filename +/// const char - filename's point +/// \~english @param [in] funcname +/// const char - function name's point +/// \~english @param [in] line +/// int - file's line +/// \~english @param [in] apiname +/// const char - called API name's point +/// \~english @param [out] +/// +/// \~english @retval 0 : Success +/// +/// \~english @par Prerequisite +/// - DBG_ENABLE is defined. +/// \~english @par Change of internal state +/// - None +/// \~english @par Conditions of processing failure +/// - None +/// \~english @par Classification +/// Public +/// \~english @par Type +/// Sync Only +/// \~english @par Detail +/// Receipt and record the client's filename, function name, +/// line and called API's function name which called this API. +/// \~english @see None +//////////////////////////////////////////////////////////////////////////////// +int RPC_record_dbg_log(const char *filename, const char *funcname, int line, + const char *apiname); +#endif + +#define rpc_malloc malloc +#define rpc_free free + +/* + * RPC debug message control + * if set to non-null and some critical information is displayed. + */ +extern char *RPC_DEBUG; // NOLINT (readability/nolint) + +/** @}*/ // end of rpc_library +/** @}*/ // end of other_service +/** @}*/ // end of BaseSystem + +#ifdef __cplusplus +} +#endif + +#endif // OTHERSERVICE_RPC_H_ diff --git a/service/other/rpc_library/library/include/other_service/rpc_id.h b/service/other/rpc_library/library/include/other_service/rpc_id.h new file mode 100755 index 0000000..e43e03c --- /dev/null +++ b/service/other/rpc_library/library/include/other_service/rpc_id.h @@ -0,0 +1,96 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file rpc_id.h + * @brief \~english RPC library -- RPC_ID(port no) define + */ +/** @addtogroup BaseSystem + * @{ + */ +/** @addtogroup other_service + * @ingroup BaseSystem + * @{ + */ +/** @addtogroup rpc_library + * @ingroup other_service + * @{ + */ + +/** @brief \~english define the RPC_ID used by program + * like #define XXX_RPC_ID ID + * but RPC_START_CLIENT() is not necessary + * + * RPC_ID as the following + * - UI Application : 50xxx + * - Application Service : 51xxx + * + HMI : 511xx + * + AV : 512xx + * + Radio : 513xx + * + Connectivity : 514xx + * + Navigation/Location: 515xx + * + Vehicle : 516xx + * + Communication : 517xx + * + BasePF : 518xx + * + System : 519xx + * + Others : 510xx + * - Common Service : 53xxx + * - Extension Service : 55xxx + * - System/Device Driver : 56xxx + * + * - 49152~59999 are Reserved. + * @note \~english RPC_ID==port num , + * when not call from RPClibrary, and not well-known(~1023)/registered(1024~49151) + * use the fixed port num to IP communicate, use 6xxxx port number. + * @note \~english when RPC_START_CLIENT,and distribute automatically. + * use the linux kernel port distribute function + * 1024~4999 as the port num unused. + */ + +#ifndef OTHERSERVICE_RPCID_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPCID_H_ // NOLINT(build/header_guard) + + +#define test_RPC_ID 49999 +///< \~english sample(ID is used by RPClibrary test program) + +/* UI Aapplication : 50xxx */ + +/* Application Service : 51xxx */ +#define MODEMANAGER_RPC_ID 51100 /* ModeManager */ +#define ACTIVITYMANAGER_RPC_ID 51101 /* ActivityManager */ + +/* Common Service : 53xxx */ +#define TIMERENTRYDRV_RPC_ID 53000 /* TimerEntryDrv */ +#define TSKM_RPC_ID 53001 /* TaskManager */ +#define MSGBRK_RPC_ID 53002 /* MessageBroker */ +#define IPMANAGER_RPC_ID 53003 /* IPManager */ +#define IPMANAGER_RPC_ID 53003 /* IPManager */ +#define DEVICEMANAGER_RPC_ID 53004 /* DeviceManager */ +#define CAN_RPC_ID 53005 /* Communication(CAN) */ +#define _CWORD83__RPC_ID 53006 /* Communication(_CWORD83_) */ +#define SENSOR_RPC_ID 53007 /* Vehicle(Sensor) */ +#define GPS_RPC_ID 53008 /* Vehicle(GPS) */ +#define RESMGR_RPC_ID 53009 /* ResourceManager */ +#define GRAPHICS_RPC_ID 53010 /* Graphic */ + +/* Extension Service : 55xxx */ + +/* System/Device Driver : 56xxx */ +/** @}*/ // end of rpc_library +/** @}*/ // end of other_service +/** @}*/ // end of BaseSystem +#endif // OTHERSERVICE_RPCID_H_ diff --git a/service/other/rpc_library/library/include/other_service/rpc_library.h b/service/other/rpc_library/library/include/other_service/rpc_library.h new file mode 100755 index 0000000..298f741 --- /dev/null +++ b/service/other/rpc_library/library/include/other_service/rpc_library.h @@ -0,0 +1,41 @@ +// +// @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/** + * @file + * @brief \~english include all rpc_library head files + */ +/** @addtogroup BaseSystem + * @{ + */ +/** @addtogroup other_service + * @ingroup BaseSystem + * @{ + */ +/** @addtogroup rpc_library + * @ingroup other_service + * @{ + */ +#ifndef OTHERSERVICE_RPCLIBRARY_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPCLIBRARY_H_ // NOLINT(build/header_guard) + +#include <other_service/rpc.h> +#include <other_service/rpc_id.h> + +#endif // OTHERSERVICE_RPCLIBRARY_H_ +/** @}*/ +/** @}*/ +/** @}*/ diff --git a/service/other/rpc_library/library/include/rpc_internal.h b/service/other/rpc_library/library/include/rpc_internal.h new file mode 100755 index 0000000..2b293ca --- /dev/null +++ b/service/other/rpc_library/library/include/rpc_internal.h @@ -0,0 +1,135 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file rpc_internal.h + * @brief RPC Library Internal Implementation-Type, macro, and function definitions used internally + * + */ +/** @addtogroup RPClib_in + */ +#ifndef OTHERSERVICE_RPCINTERNAL_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPCINTERNAL_H_ // NOLINT(build/header_guard) + +#include <unistd.h> +#include <stdlib.h> + +#if defined(__linux__) +/* Using unix autobind Features That Are Only Available in linux */ +#define RPC_USE_UNIX_AUTOBIND +#endif /* __linux__ */ + +#define RPC_USE_SYSLOG + +typedef RPC_ID *RPC_ID_p; + +#include "rpc_udp.h" +#include "rpc_thread.h" + +RPC_Result RpcQueueAPIRequestBefore(RpcIdInfo *id, + UINT32 size, char **buff); +RPC_Result RpcQueueAPIRequestAfter(RpcIdInfo *id, RPC_ID client, + const char *mesg, UINT32 size, + char *args); +void RpcFreeAPIArgsString(char *args_string); +UINT16 RpcGetAPIRequest(RpcIdInfo *id, RPC_ID_p client, + char **args_string, unsigned int *args_size); +RPC_Result RpcSetAPIcallReturn(RpcIdInfo *id, + const char *mesg, UINT32 size); +void RpcDiscardAPIcallReturn(RpcIdInfo *id); + +RPC_Result RpcDeleteSrvrPid(RpcIdInfo *idinfo, RPC_ID srvr_rpc_id, int wd); + +#define StrEqual(a, b) (strcmp((a), (b)) == 0) +#define MemEqual(a, b, c) (memcmp((a), (b), (c)) == 0) + +#define RPC_IS_INVALID_ID(a) ((a) == RPC_NO_ID) +#define RPC_INVALIDATE_ID(a) ((a) = RPC_NO_ID) +#define RPC_ID_COPY(a, b) ((a) = (b)) +#define RPC_ID_Equal(a, b) ((a) == (b)) + +/* inotify Events read Sizes */ +#define EVENT_SIZE ( sizeof(struct inotify_event) ) /* One inotify event size */ +#define BUF_LEN (4 * EVENT_SIZE) /* Read sizes */ + +#define LESS_THAN < + +#define SET_NONBLOCK(fd) { \ + int flag; \ + flag = fcntl((fd), F_GETFL, 0); \ + fcntl((fd), F_SETFL, O_NONBLOCK|flag); \ + } + +#define SET_CLOSE_ON_EXEC(fd) { \ + int flag; \ + flag = fcntl((fd), F_GETFD, 0); \ + fcntl((fd), F_SETFD, FD_CLOEXEC|flag); \ + } + +#if defined(RPC_USE_SYSLOG) + +#include <syslog.h> + +#define RPC_LOG_CRIT(format, arg...) syslog(LOG_CRIT, format "\n", ##arg) +#define RPC_LOG_ERR(format, arg...) syslog(LOG_ERR, format "\n", ##arg) +#define RPC_LOG_STATE(format, arg...) syslog(LOG_INFO, format "\n", ##arg) +#define RPC_LOG_DEBUG(format, arg...) syslog(LOG_DEBUG, format "\n", ##arg) + +#else // defined(RPC_USE_SYSLOG) + +#define RPC_LOG_CRIT(format, arg...) fprintf(stderr, "[RPC:CRIT](%d): " format "\n", getpid(), ##arg) +#define RPC_LOG_ERR(format, arg...) fprintf(stderr, "[RPC:ERR](%d): " format "\n", getpid(), ##arg) +#define RPC_LOG_STATE(format, arg...) fprintf(stderr, "[RPC:STATE](%d): " format "\n", getpid(), ##arg) +#define RPC_LOG_DEBUG(format, arg...) + +#endif // defined(RPC_USE_SYSLOG) + +#include <sys/prctl.h> + +#define RPC_LOG_ERR_W_NAME(format, arg...) \ +do { \ + char name[16]; \ + prctl(PR_GET_NAME, name); \ + name[15] = '\0'; \ + RPC_LOG_ERR("(%s): " format, name, ##arg); \ +} while (0) + +#define RPC_LOG_PERROR(format, arg...) RPC_LOG_ERR("[%s:%d] %s : " format, __FILE__, __LINE__, strerror(errno), ##arg); + +#include <assert.h> +#define rpc_assert assert + +#define BUG_ASSERT(cond, x) \ + if (!(cond)) { \ + rpc_assert("BUG ASSERTION! " #x " @ " __FILE__ ==NULL); \ + } +#define CONFIG_ASSERT(s) rpc_assert((s) == NULL) + +#define RUNS_IN_CALLERS_THREAD +#define RUNS_IN_READING_THREAD + +#if !defined(RPC_USE_UNIX_AUTOBIND) + +#define RPC_SOCKET_NAME "/tmp/RPC/%05d" + +static inline void +rpc_set_socket_name(char *str, RPC_ID id) { + sprintf(str, RPC_SOCKET_NAME, id); // NOLINT (readability/nolint) +} + +#endif /* !AUTOBIND */ + +#endif // OTHERSERVICE_RPCINTERNAL_H_ diff --git a/service/other/rpc_library/library/include/rpc_thread.h b/service/other/rpc_library/library/include/rpc_thread.h new file mode 100755 index 0000000..819e5a6 --- /dev/null +++ b/service/other/rpc_library/library/include/rpc_thread.h @@ -0,0 +1,240 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OTHERSERVICE_RPCTHREAD_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPCTHREAD_H_ // NOLINT(build/header_guard) + +#include <pthread.h> + +/** @addtogroup RPClib_in + * @{ + */ + +#define RPC_NO_SOCKET -1 +#define RPC_NO_THREAD 0 + +#define RPC_MAIN_SUB_COMMAND "%-2d %-16lx" +#define RPC_MAIN_SUB_COMMAND_SIZE 20 +#define RPC_MAIN_SUB_COMMANDs "%d %lx" +/** Type of command sent from the main thread to the subthread */ +enum { + RPC_COMMAND_ADD_SERVER, /* Addition of RPC_ID */ + RPC_COMMAND_REMOVE_SERVER, /* Remove RPC_ID */ + RPC_COMMAND_EXIT, /* Sub-thread termination (when all RPC_ID are exhausted) */ +}; + +#define APICALL_TIMEOUT_DEFAULT -1 + +/** Periodic checking timeout (mSec) for detecting deadlocks */ +#define TIMEOUT_FOR_DEADLOCK_CHECK 1000 + +/** Length of the socket name + API request datagram socket = '\0' + "5-digit number associated with RPC_ID" + Authentication stream socket = '\0' + "secure_" + "5-digit number associated with RPC_ID" */ +#define SOCK_NAME_LEN (1+5) /* API request datagram socket */ +#define SECURE_SOCK_NAME_LEN (1+12) /* Authentication stream socket */ + +#define rpc_mutex_lock pthread_mutex_lock +#define rpc_mutex_unlock pthread_mutex_unlock + +/** Upper limit of UID and GID list registration */ +#define CREDENTIAL_LIST_NUM_MAX 32 + +/** Authentication result to be sent to the client */ +typedef struct { + UINT16 certify_res; /**< Client Authentication Result */ + pid_t srvr_pid; /**< Server PID */ +} RpcCertifyResult; + +#define CERTIFY_OK 0 +#define CERTIFY_NG 1 + +/** Received API call request */ +typedef struct { + RPC_ID client; /**< API ID of the requested program */ + UINT16 api_num; /**< Calling API number */ + char *args_string; /**< API Call Arguments */ + UINT32 args_size; /**< Number of bytes in the argument */ +} rpc_apicall_queue; + +/** Source Client Socket Name Management Table */ +typedef struct RpcClientSockNameInfoST RpcClientSockNameInfo; +struct RpcClientSockNameInfoST { + char client_sock_name[SOCK_NAME_LEN]; /**< Socket Name List for Source Client */ + pid_t pid; /**< Source pid */ + uid_t uid; /**< Source uid */ + gid_t gid; /**< Source gid */ + int wd; /**< Non-negative inotify monitored descriptors */ + RpcClientSockNameInfo *next; /**< Pointer to the next node */ +}; + +/** Communication server PID management table */ +typedef struct RpcSrvrPidInfoST RpcSrvrPidInfo; +struct RpcSrvrPidInfoST { + RPC_ID srvr_rpc_id; /**< Communication destination server RPC_ID */ + pid_t srvr_pid; /**< Communication destination server pid */ + int wd; /**< Non-negative inotify monitored descriptors */ + RpcSrvrPidInfo *next; /**< Pointer to the next node */ +}; + +/** Buffer structure for API call acceptance processing. + * This function is allocated only when the dispatch function is specified by RPC_start(). + */ +typedef struct { + RPC_dispatch_func_t dispatch_func;/**< Dispatch Functions to APIs (in stub) */ +#define RPC_apicall_dispatch_func(id) \ + ((id)->apicall ? (id)->apicall->dispatch_func : 0) + + INT32 pipe_sub_main[2];/**< Notification pipe from the subthread to the main thread */ +#define RPC_pipe_sub_main(th) ((th)->srvr_id->apicall->pipe_sub_main) + + UINT16 num_queue;/**< Number of queued API call requests */ +#define RPC_apicall_num_queue(id) \ + ((id)->apicall ? (id)->apicall->num_queue : 0) +#define RPC_apicall_num_queue_inc(id) ((id)->apicall->num_queue++) +#define RPC_apicall_num_queue_dec(id) ((id)->apicall->num_queue--) + + rpc_apicall_queue queue[RPC_MAX_APICALL_QUEUE];/**< Queuing API Call Requests (FIFO) */ +#define RPC_apicall_queue(id, i) ((id)->apicall->queue[(i)]) +#define RPC_apicall_queue_client(id, i) ((id)->apicall->queue[(i)].client) +#define RPC_apicall_queue_api_num(id, i) ((id)->apicall->queue[(i)].api_num) +#define RPC_apicall_queue_args(id, i) ((id)->apicall->queue[(i)].args_string) +#define RPC_apicall_queue_args_size(id, i) ((id)->apicall->queue[(i)].args_size) + + RPC_ID in_process_client;/**< Clients running on ID */ +#define RPC_apicall_in_process_client(id) ((id)->apicall->in_process_client) + + INT32 timeout_sec;/**< API processing timeout */ +#define RPC_apicall_api_timeout_sec(id) ((id)->apicall->timeout_sec) + + INT32 secure_check;/**< Client Authentication Check Enabled/Disabled */ +#define RPC_secure_check(id) ((id)->apicall->secure_check) + + INT32 regist_credential_info;/**< Registration of authentication information */ +#define RPC_regist_credential_info(id) ((id)->apicall->regist_credential_info) + + uid_t *uid_list;/**< List of UIDs that can communicate */ +#define RPC_uid_list(id, i) ((id)->apicall->uid_list[(i)]) + + INT32 uid_num;/**< Number of UID list elements that can communicate */ +#define RPC_uid_num(id) ((id)->apicall->uid_num) + + gid_t *gid_list;/**< GID list that can communicate */ +#define RPC_gid_list(id, i) ((id)->apicall->gid_list[(i)]) + + INT32 gid_num;/**< Number of GID list elements that can communicate */ +#define RPC_gid_num(id) ((id)->apicall->gid_num) + + RpcClientSockNameInfo *sock_info_head;/**< Leading node of the source client's socket information management table */ +#define RPC_sock_info_head(id) ((id)->apicall->sock_info_head) +#define RPC_client_sock_name(id) ((id)->apicall->sock_info_head->client_sock_name) +#define RPC_client_sock_pid(id) ((id)->apicall->sock_info_head->pid) +#define RPC_client_sock_next_node(id) ((id)->apicall->sock_info_head->next) + + INT32 client_sock_name_num;/**< Number of elements in the source client's socket name list */ +#define RPC_client_sock_name_num(id) ((id)->apicall->client_sock_name_num) +#define RPC_client_sock_name_num_inc(id) ((id)->apicall->client_sock_name_num++) +#define RPC_client_sock_name_num_dec(id) ((id)->apicall->client_sock_name_num--) +} RpcApicallInfo; + +struct RpcThreadInfo; + +/** Structure that holds information about each RPC_ID */ +typedef struct RpcIdInfo { + struct RpcThreadInfo *thread_info; + + RPC_ID port;/**< Port number (=ID) used by the RPC library */ +#define RPC_port(id) ((id)->port) +#define RPC_my_id(id) RPC_port(id) +#define rpc_get_port(id) (id) + + INT32 sock;/**< Sockets used by the RPC library */ +#define RPC_my_sock(id) ((id)->sock) + + INT32 secure_sock;/**< Authentication socket used by the RPC library */ +#define RPC_my_secure_sock(id) ((id)->secure_sock) + + int inotify_fd;/**< Server process monitoring inotify */ +#define RPC_clnt_inotify_fd(id) ((id)->inotify_fd) + + INT32 count;/**< Number of clients using the same RPC_ID */ +#define RPC_clnt_count(id) ((id)->count) +#define RPC_inc_clnt_count(id) ((id)->count++) +#define RPC_dec_clnt_count(id) ((id)->count--) + + RpcApicallInfo *apicall;/**< Information for API call acceptance processing */ +#define RPC_apicall_info(id) ((id)->apicall) + + RpcSrvrPidInfo *srvr_pid_head;/**< Communication destination server PID management table top node */ +#define RPC_srvr_pid_head(id) ((id)->srvr_pid_head) +#define RPC_srvr_rpc_id(id) ((id)->srvr_pid_head->srvr_rpc_id) +#define RPC_srvr_pid(id) ((id)->srvr_pid_head->srvr_pid) +#define RPC_srvr_pid_next_node(id) ((id)->srvr_pid_head->next) + + UINT32 return_str_len;/**<Number of bytes in the returned string as a result of an API call*/ + /* Including the terminating '\0' */ +#define RPC_apicall_return_str_len(id) ((id)->return_str_len) + + UINT8 *return_str;/**<String returned as a result of an API call*/ +#define RPC_apicall_return_str(id) ((id)->return_str) +} RpcIdInfo; + +/** Received response packet */ +typedef struct { + RPC_ID id;/**< ID of the thread that sent the response */ + UINT16 type;/**< Response type */ + UINT32 seq_num;/**< The packet number to which this was sent (the response to this) */ +} RpcResponse; + +/** Structure that stores the state of each thread that called the RPC library */ +typedef struct RpcThreadInfo { + pthread_mutex_t th_mtx;/**< Mutex for modifying this struct */ +#define RPC_THREAD_MUTEX_LOCK(th) (rpc_mutex_lock(&((th)->th_mtx))) +#define RPC_THREAD_MUTEX_UNLOCK(th) (rpc_mutex_unlock(&((th)->th_mtx))) + + pthread_t thread;/**< Thread calling the RPC library */ +#define RPC_main_thread(th) ((th)->thread) + + UINT32 sequence_number;/**< Sequence number given to the transmitted packet */ + /* Send 30 times per second from the beginning of the thread, then wrap around two years. + * It is not necessary to wrap around 0 because there is no comparison. + */ + UINT32 magic;/**< Magic number to detect corrupted memories */ + + RpcIdInfo *srvr_id;/**< Information by RPC_ID (server) */ +#define RPC_srvr_idinfo(th) ((th)->srvr_id) + + RpcIdInfo *clnt_id;/**< Info by RPC_ID (client) */ +#define RPC_clnt_idinfo(th) ((th)->clnt_id) +} RpcThreadInfo; + +extern pthread_t g_rpc_thread; +extern UINT32 g_rpc_thread_alive; + +#define PIPE_READ 0 +#define PIPE_WRITE 1 +extern int g_rpc_pipe_main_sub[2]; + +RpcThreadInfo *RpcMyThreadInfo(void); +RpcThreadInfo *RpcCreateThreadInfo(void); +int RpcCreateIdInfo(RpcThreadInfo *th, RPC_ID id, + RPC_dispatch_func_t dispatch, INT32 secure_check); +void RpcDestroyIdInfo(RpcThreadInfo *th, RpcIdInfo *idinfo); +void RpcDestroyThreadInfo(void); +void RpcUnlinkSocketFiles(void); + +/** @} */ +#endif // OTHERSERVICE_RPCTHREAD_H_ diff --git a/service/other/rpc_library/library/include/rpc_udp.h b/service/other/rpc_library/library/include/rpc_udp.h new file mode 100755 index 0000000..8c45ce6 --- /dev/null +++ b/service/other/rpc_library/library/include/rpc_udp.h @@ -0,0 +1,162 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OTHERSERVICE_RPCUDP_H_ // NOLINT(build/header_guard) +#define OTHERSERVICE_RPCUDP_H_ // NOLINT(build/header_guard) + +/** @addtogroup RPClib_in */ +/** @{ */ +/** Number of bytes of payload that can be stored in a UDP packet */ +#define RPC_UDP_PAYLOAD RPC_MAX_API_ARG_TOTAL_SIZE + +/** UDP packet header + * + * Packet type, source ID, sequence number, packet size, packet position + */ +#define RPC_PACKET_HEADER "%-2d %-5x %-8x %-4u %1d " +/** Number of bytes in the UDP packet header + */ +#define RPC_PACKET_HEADER_LEN (2+1 + 5+1 + 8+1 + 4+1 + 1+1) + +/** Number of bytes to store in the UDP packet */ +#define RPC_UDP_PACKET_SIZE (RPC_UDP_PAYLOAD + RPC_PACKET_HEADER_LEN) + +#define RPC_COMMAND_LEN 3 + +/** Format string for UDP packet header analysis */ +#define RPC_PACKET_HEADER_scanf "%d %x %x %d" + +/** Header (API number) to be added to the API call packet */ +#define RPC_APICALL_FORMAT "%-4d " +/** Number of bytes in the header of an API call packet */ +#define RPC_APICALL_FORMAT_ARGS_START 5 + +#define RPC_MAX_RESPONSE_MESSAGE_SIZE 5 + +#define RPC_SEQ_NUM_START 1 +#define RPC_SEQ_NUM_INVALID 0 + +/** Internal API call number for checking server existence */ +#define RPC_API_NUM_RPC_ALIVE 0 + +/** Type of packet that the RPC library sends */ +typedef enum { + RPC_PACKET_APICALL = 0,/**< API call request */ + + RPC_RESPONSE_APICALL,/**< Responding to API Call Requests */ +#define RPC_RESPONSE_NONE 0x0000 +#define RPC_RESPONSE_API_OK 0x0001 +#define RPC_RESPONSE_API_BUSY 0x0002 +#define RPC_RESPONSE_API_ERR 0x0004 +#define RPC_RESPONSE_API_DEADLOCK 0x0005 /* Server deadlock */ +#define RPC_RESPONSE_API_CERTIFY 0x0006 /* Authentication request */ + + RPC_PACKET_APIRETURN,/**< Result of the API call */ + + RPC_PACKET_NONE, +} RPC_packet_type; + +/** Packet Location (Multi-Fragment Packet Support) */ +typedef enum { + RPC_PACKET_POS_ONEANDONLY = 0,/**< Complete with one packet */ +} rpc_packet_position; + +/** Transmitting buffer */ +typedef struct { + unsigned int bytes; + const void *buf; +} rpc_send_buf; + +/** Valid bytes in the sockaddr_un struct specified for send */ +#define RPC_SOCKET_NAME_LEN 6 /**< Name: 5 characters + first NULL bytes */ +#define RPC_SOCKET_ADDR_LEN (sizeof(short) + RPC_SOCKET_NAME_LEN) // NOLINT (readability/nolint) + +enum { + RPC_SEND_TO_CLIENT, + RPC_SEND_TO_SERVER, + RPC_SEND_TO_SERVER_NO_RETRY, +}; + +struct RpcIdInfo; + +int RpcSendUdp(struct RpcIdInfo *id, RPC_ID receiver, int direction, + RPC_packet_type type, const void *mesg, unsigned int bytes); +int RpcSendUdp2(struct RpcIdInfo *id, RPC_ID receiver, int direction, + RPC_packet_type type, unsigned int num, rpc_send_buf *sendbuf); +RPC_Result RpcSendUdpResponse(struct RpcIdInfo *id, RPC_ID receiver, + int direction, + RPC_packet_type type, + UINT32 seq_num, char *mesg, UINT32 bytes); + +int RpcReadUdpPacket(const struct RpcIdInfo *id, UINT8 *buf); + +RPC_Result RpcParsePacketHeader(const char *str, RPC_packet_type *command, + RPC_ID_p id, UINT32 *seq_num, UINT32 *size); + +RPC_Result RpcClientWaitResponse(struct RpcIdInfo *idinfo, + UINT32 seq_num, + UINT32 timeout_msec, UINT16 *response); +RPC_Result RpcClientWaitResult(struct RpcIdInfo *idinfo, RPC_ID srvr_id); + +#if defined(RPC_USE_UNIX_AUTOBIND) +/* ===pathname of datagram sockets for API requests=== */ +static inline void +RpcSetServerName(char *str, RPC_ID id) { + *str = '\0'; + sprintf(str + 1, "S%04x", id); // NOLINT (readability/nolint) +} + +static inline void +RpcSetClientName(char *str, RPC_ID id) { + *str = '\0'; + sprintf(str + 1, "%05x", id); // NOLINT (readability/nolint) +} + +static inline void +RpcGetClientName(const char *str, RPC_ID *id) { + char buf[6]; + memcpy(buf, str + 1, 5); + buf[5] = '\0'; + if (buf[1] == 'X') { + *id = (RPC_ID)(strtoul(buf + 1, NULL, 16)); + } else { + *id = (RPC_ID)(strtoul(buf, NULL, 16)); + } +} + +/* ===pathname of stream-socket for authenticating=== */ +static inline void +RpcSetServerSecureName(char *str, RPC_ID id) { + *str = '\0'; + sprintf(str + 1, "secure_S%04x", id); // NOLINT (readability/nolint) +} + +static inline void +RpcSetClientSecureName(char *str, RPC_ID id) { + *str = '\0'; + sprintf(str + 1, "secure_%05x", id); // NOLINT (readability/nolint) +} + +static inline void +RpcGetClientNameFromSock(const char *str, char *client_sock_name) { + memcpy(client_sock_name, str + 8, 5); + client_sock_name[5] = '\0'; +} + +#endif /* !AUTOBIND */ + +/** @} */ +#endif // OTHERSERVICE_RPCUDP_H_ |