2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////
18 /// This file supports templates for high use common tasks.
19 ///////////////////////////////////////////////////////////////////////////////
21 * @file ss_templates.h
24 /** @addtogroup BaseSystem
27 /** @addtogroup system_service
31 /** @addtogroup system_manager
32 * @ingroup system_service
35 #ifndef __SS_TEMPLATES_H__ // NOLINT (build/header_guard)
36 #define __SS_TEMPLATES_H__
40 #include <native_service/frameworkunified_types.h>
41 #include <native_service/ns_logger_if.h>
42 #include <native_service/frameworkunified_framework_if.h>
44 #include "system_service/ss_system_types.h"
45 #include "system_service/ss_string_maps.h"
48 #define ZONE_ERR ZONEMASK(31)
51 /////////////////////////////////////////////////////////////////////////////////////
53 /// \~english @par Summary
54 /// check the data information of msg.
55 /// \~english @param [in] hApp
56 /// HANDLE - HANDLE Application
57 /// \~english @param [in] Data
58 /// T - The reference to the Data memory location
59 /// \~english @param [in] f_eRetrieveMethod
60 /// ESMRetrieveTypes - The msg retrieval method ( release or retain )
63 /// \~english @retval eFrameworkunifiedStatusOK Success
64 /// \~english @retval eFrameworkunifiedStatusInvldHandle Invalid handle
65 /// \~english @retval eFrameworkunifiedStatusInvldParam Invalid parameter
66 /// \~english @par Preconditions
68 /// \~english @par Change of the internal state
69 /// - The internal state is not changed.
70 /// \~english @par Causes of failures
71 /// - System Manager handle for the session is NULL. [eFrameworkunifiedStatusInvldHandle]
72 /// - data'size is not corruct. [eFrameworkunifiedStatusInvldParam]
73 /// \~english @par Classification
75 /// \~english @par Type
77 /// \~english @par Detail
78 /// Check hApp ptr, msg size, msg reception, read msg if all ok
80 /// eFrameworkunifiedStatus:Result
81 /// - eFrameworkunifiedStatusOK:Success
82 /// - Except eFrameworkunifiedStatusOK:Fail
83 /// \~english @see None
84 ////////////////////////////////////////////////////////////////////////////////////
85 template <typename T> EFrameworkunifiedStatus ReadMsg(
87 T &Data, // NOLINT (runtime/references)
88 ESMRetrieveTypes f_eRetrieveMethod = eSMRRelease) {
89 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
91 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: NULL = hApp");
92 l_eStatus = eFrameworkunifiedStatusInvldHandle;
93 } else if (sizeof(Data) != FrameworkunifiedGetMsgLength(hApp)) {
94 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__
95 , " Error: message buffer sizes mismatch: expected %ld, received %d"
96 , sizeof(Data), FrameworkunifiedGetMsgLength(hApp));
97 l_eStatus = eFrameworkunifiedStatusInvldParam;
98 } else if ( eFrameworkunifiedStatusOK != (l_eStatus =
99 FrameworkunifiedGetMsgDataOfSize(hApp, (PVOID)&Data, sizeof(Data), f_eRetrieveMethod))) {
100 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__
101 , " Error: FrameworkunifiedGetMsgDataOfSize() errored: 0x%x", l_eStatus);
104 } // End of template <typename T> EFrameworkunifiedStatus ReadMsg ()
105 /* Copy & paste easy-to-use little 't' template
107 // Check hApp ptr, msg size, msg reception, read msg if all ok. *
108 // Report any errors found. *
110 if ( eFrameworkunifiedStatusOK != ( l_eStatus = ReadMsg<T>
114 LOG_ERROR("ReadMsg()");
119 //****************************************************************************
122 Log pStr and l_eStatus
124 #define LOG_ERROR(pStr) \
125 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__ \
126 , " Error: %s errored: %d/'%s'" \
129 , GetStr(static_cast<EFrameworkunifiedStatus>(l_eStatus)).c_str());
131 //****************************************************************************
134 Log pStr and l_eStatus
136 #define LOG_SUCCESS(pStr) \
137 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " %s successful", pStr);
139 //****************************************************************************
141 \def LOG_STATUS(l_eStatus,pStr)
142 Log pStr and success or failure. Include l_eStatus when not successful.
144 #define LOG_STATUS(l_eStatus, pStr) \
145 if ( eFrameworkunifiedStatusOK != l_eStatus ) \
152 } // End of #define LOG_STATUS(l_eStatus,pStr)
154 //****************************************************************************
156 \def CALL_AND_LOG_STATUS(fnc)
157 Call the function and log the returned EFrameworkunifiedStatus.
159 #define CALL_AND_LOG_STATUS(fnc) \
161 LOG_STATUS(l_eStatus, #fnc)
162 // End of #define CALL_AND_LOG_STATUS(fnc)
164 //****************************************************************************
166 \def LOG_STATUS_IF_ERRORED(l_eStatus, pStr)
167 Log pStr on failure, including EFrameworkunifiedStatus.
169 #define LOG_STATUS_IF_ERRORED(l_eStatus, pStr) \
170 if ( eFrameworkunifiedStatusOK != l_eStatus ) \
173 } // End of #define LOG_STATUS_IF_ERRORED(l_eStatus,pStr)
175 //****************************************************************************
177 \def CALL_AND_LOG_STATUS_IF_ERRORED(fnc)
178 Call the function and log the returned EFrameworkunifiedStatus on failure.
180 #define CALL_AND_LOG_STATUS_IF_ERRORED(fnc) \
182 LOG_STATUS_IF_ERRORED(l_eStatus, #fnc)
183 // End of #define CALL_AND_LOG_STATUS_IF_ERRORED(fnc)
185 //****************************************************************************
187 \def MAP_ENTRY( f_map, f_enum )
188 Simplify initializing string map entry.
190 Use to set a map entry's key and value to the specified enum and the enum's
191 literal text ( i.e., stringified ) equivalent.
193 #define MAP_ENTRY(f_map, f_enum) \
194 f_map[ f_enum ] = #f_enum
196 //****************************************************************************
198 \def INTERFACEUNIFIEDLOG_RECEIVED_FROM
199 Log whom we received message or notification from.
201 Use this macro to ensure that the string "Received from" is uniformly
202 logged; this string can be grepped on to find when/where callback functions
205 #define INTERFACEUNIFIEDLOG_RECEIVED_FROM(hApp) \
206 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Received from %s", FrameworkunifiedGetMsgSrc(hApp))
208 //****************************************************************************
210 \def INTERFACEUNIFIEDLOG_WHEN_COMPILED
211 Log when this file was compiled.
213 Useful when overlaying a delivered object file ( from an official build ) with
214 a debug-built obj file - verifies that the debug file did indeed get loaded.
216 #define INTERFACEUNIFIEDLOG_WHEN_COMPILED \
217 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " %s was compiled at %s @ %s", \
218 __FILE__, __DATE__, __TIME__);
221 #define SS_ASERT(x) \
223 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT"); \
226 #define SS_ASERT_ERRNO(x) \
228 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT %d:%s", errno, strerror(errno)); \
231 #define SS_ASERT_LOG(x, fmt, ...) \
233 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT " fmt, ## __VA_ARGS__); \
236 #define SS_STATIC_ASERT(expr) \
238 char STATIC_ASSERTION_FAILED[(expr) ? 1 : -1]; \
239 (void)STATIC_ASSERTION_FAILED; \
243 * @class EnumStringMap
244 * \~english @brief EnumStringMap
245 * \~english @par Brief Introduction
246 * Class to provide EnumStringMap template function
249 template <typename enumT, void(*fp)(std::map<enumT, SS_String> & m_strMap)> class EnumStringMap { // NOLINT (runtime/references)
251 /////////////////////////////////////////////////////////////////////////////////////
252 /// \ingroup EnumStringMap
253 /// \~english @par Summary
254 /// Default constructor of EnumStringMap class.
255 /// \~english @param None
256 /// \~english @retval None
257 /// \~english @par Preconditions
259 /// \~english @par Change of the internal state
260 /// - The internal state is not changed.
261 /// \~english @par Causes of failures
263 /// \~english @par Classification
265 /// \~english @par Type
267 /// \~english @par Detail
268 /// To generate a EnumStringMap class, and initialize member variables. \n
269 /// After the constructor, be sure to call the Initialize. \n
270 /// \~english @see ~EnumStringMap, Initialize
271 ////////////////////////////////////////////////////////////////////////////////////
275 /////////////////////////////////////////////////////////////////////////////////////
276 /// \ingroup ~EnumStringMap
277 /// \~english @par Summary
279 /// \~english @param None
280 /// \~english @retval None
281 /// \~english @par Preconditions
283 /// \~english @par Change of the internal state
284 /// - The internal state is not changed.
285 /// \~english @par Causes of failures
287 /// \~english @par Classification
289 /// \~english @par Type
291 /// \~english @par Detail
292 /// To delete a EnumStringMap object. \n
293 /// \~english @see EnumStringMap
294 ////////////////////////////////////////////////////////////////////////////////////
296 SS_String GetStr( enumT f_enum) { return m_strMap[ f_enum ];}
298 std::map<enumT, SS_String> m_strMap;
300 // End of template <typename enumT, void(*fp)(std::map<enumT, SS_String> & m_strMap)> class EnumStringMap
302 #endif /* __SS_TEMPLATES_H__ */ // NOLINT (build/header_guard)