summaryrefslogtreecommitdiffstats
path: root/systemservice/interface_unified/library/include/system_service/ss_templates.h
blob: 581e616a4e7c60621fccbdd7abc85baf0680c457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * @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.
 */

///////////////////////////////////////////////////////////////////////////////
/// This file supports templates for high use common tasks.
///////////////////////////////////////////////////////////////////////////////
/**
 * @file ss_templates.h
 */

/** @addtogroup BaseSystem
 *  @{
 */
/** @addtogroup system_service
 *  @ingroup BaseSystem
 *  @{
 */
/** @addtogroup system_manager
 *  @ingroup system_service
 *  @{
 */
#ifndef __SS_TEMPLATES_H__  // NOLINT (build/header_guard)
#define __SS_TEMPLATES_H__

#include <stdio.h>
#include <errno.h>
#include <native_service/frameworkunified_types.h>
#include <native_service/ns_logger_if.h>
#include <native_service/frameworkunified_framework_if.h>
#include <map>
#include "system_service/ss_system_types.h"
#include "system_service/ss_string_maps.h"

#ifndef ZONE_ERR
#define ZONE_ERR     ZONEMASK(31)
#endif

/////////////////////////////////////////////////////////////////////////////////////
/// \ingroup ReadMsg
/// \~english @par Summary
///       check the data information of msg.
/// \~english @param [in] hApp
///       HANDLE - HANDLE Application
/// \~english @param [in] Data
///       T - The reference to the Data memory location
/// \~english @param [in] f_eRetrieveMethod
///       ESMRetrieveTypes - The msg retrieval method ( release or retain )
/// \~english @par
///            T template type
/// \~english @retval eFrameworkunifiedStatusOK Success
/// \~english @retval eFrameworkunifiedStatusInvldHandle Invalid handle
/// \~english @retval eFrameworkunifiedStatusInvldParam Invalid parameter
/// \~english @par Preconditions
///       -
/// \~english @par Change of the internal state
///       - The internal state is not changed.
/// \~english @par Causes of failures
///       - System Manager handle for the session is NULL. [eFrameworkunifiedStatusInvldHandle]
///       - data'size is not corruct. [eFrameworkunifiedStatusInvldParam]
/// \~english @par Classification
///       Public
/// \~english @par Type
///       Method only
/// \~english @par Detail
///       Check hApp ptr, msg size, msg reception, read msg if all ok
/// \~english @par
///             eFrameworkunifiedStatus:Result
///                - eFrameworkunifiedStatusOK:Success
///                - Except eFrameworkunifiedStatusOK:Fail
/// \~english @see  None
////////////////////////////////////////////////////////////////////////////////////
template <typename T> EFrameworkunifiedStatus ReadMsg(
                     HANDLE hApp,
                     T &Data,  // NOLINT (runtime/references)
                     ESMRetrieveTypes f_eRetrieveMethod = eSMRRelease) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  if ( NULL == hApp ) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: NULL = hApp");
    l_eStatus = eFrameworkunifiedStatusInvldHandle;
  } else if (sizeof(Data) != FrameworkunifiedGetMsgLength(hApp)) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__
       , " Error: message buffer sizes mismatch: expected %ld, received %d"
       , sizeof(Data), FrameworkunifiedGetMsgLength(hApp));
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  } else if ( eFrameworkunifiedStatusOK != (l_eStatus =
                 FrameworkunifiedGetMsgDataOfSize(hApp, (PVOID)&Data, sizeof(Data), f_eRetrieveMethod))) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__
       , " Error: FrameworkunifiedGetMsgDataOfSize() errored: 0x%x", l_eStatus);
  }
  return l_eStatus;
}  // End of template <typename T> EFrameworkunifiedStatus ReadMsg ()
  /* Copy & paste easy-to-use little 't' template
  // ReadMsg():                                                        *
  //     Check hApp ptr, msg size, msg reception, read msg if all ok.  *
  //     Report any errors found.                                      *
  //                                                                   *
  if ( eFrameworkunifiedStatusOK != ( l_eStatus = ReadMsg<T>
                                            ( hApp
                                            , Data )))
  {
    LOG_ERROR("ReadMsg()");
  }
  else
  */

//****************************************************************************
/*!
  \def LOG_ERROR(pStr)
   Log pStr and l_eStatus
 */
#define  LOG_ERROR(pStr)                     \
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__            \
           , " Error: %s errored: %d/'%s'"   \
           , pStr                            \
           , l_eStatus                       \
           , GetStr(static_cast<EFrameworkunifiedStatus>(l_eStatus)).c_str());

//****************************************************************************
/*!
  \def LOG_ERROR(pStr)
   Log pStr and l_eStatus
 */
#define  LOG_SUCCESS(pStr) \
  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " %s successful", pStr);

//****************************************************************************
/*!
  \def LOG_STATUS(l_eStatus,pStr)
   Log pStr and success or failure. Include l_eStatus when not successful.
 */
#define LOG_STATUS(l_eStatus, pStr)  \
    if ( eFrameworkunifiedStatusOK != l_eStatus ) \
    {                                \
        LOG_ERROR(pStr);             \
    }                                \
    else                             \
    {                                \
        LOG_SUCCESS(pStr);           \
    }  // End of #define LOG_STATUS(l_eStatus,pStr)

//****************************************************************************
/*!
  \def CALL_AND_LOG_STATUS(fnc)
     Call the function and log the returned EFrameworkunifiedStatus.
 */
#define CALL_AND_LOG_STATUS(fnc)           \
    l_eStatus = (fnc);                     \
    LOG_STATUS(l_eStatus, #fnc)
    // End of #define CALL_AND_LOG_STATUS(fnc)

//****************************************************************************
/*!
  \def LOG_STATUS_IF_ERRORED(l_eStatus, pStr)
   Log pStr on failure, including EFrameworkunifiedStatus.
 */
#define LOG_STATUS_IF_ERRORED(l_eStatus, pStr)         \
    if ( eFrameworkunifiedStatusOK != l_eStatus )                   \
    {                                                  \
        LOG_ERROR(pStr);                               \
    }  // End of #define LOG_STATUS_IF_ERRORED(l_eStatus,pStr)

//****************************************************************************
/*!
  \def CALL_AND_LOG_STATUS_IF_ERRORED(fnc)
   Call the function and log the returned EFrameworkunifiedStatus on failure.
 */
#define CALL_AND_LOG_STATUS_IF_ERRORED(fnc) \
    l_eStatus = (fnc);                      \
    LOG_STATUS_IF_ERRORED(l_eStatus, #fnc)
    // End of #define CALL_AND_LOG_STATUS_IF_ERRORED(fnc)

//****************************************************************************
/*!
  \def MAP_ENTRY( f_map, f_enum )
  Simplify initializing string map entry.

  Use to set a map entry's key and value to the specified enum and the enum's
  literal text ( i.e., stringified ) equivalent.
 */
#define MAP_ENTRY(f_map, f_enum) \
  f_map[ f_enum ] = #f_enum

//****************************************************************************
/*!
  \def INTERFACEUNIFIEDLOG_RECEIVED_FROM
   Log whom we received message or notification from.

   Use this macro to ensure that the string "Received from" is uniformly
   logged; this string can be grepped on to find when/where callback functions
   were invoked.
 */
#define INTERFACEUNIFIEDLOG_RECEIVED_FROM(hApp) \
  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Received from %s", FrameworkunifiedGetMsgSrc(hApp))

//****************************************************************************
/*!
  \def INTERFACEUNIFIEDLOG_WHEN_COMPILED
  Log when this file was compiled.

  Useful when overlaying a delivered object file ( from an official build ) with
  a debug-built obj file - verifies that the debug file did indeed get loaded.
  */
#define INTERFACEUNIFIEDLOG_WHEN_COMPILED \
  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " %s was compiled at %s @ %s", \
    __FILE__, __DATE__, __TIME__);


#define SS_ASERT(x) \
  if (!(x)) { \
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT"); \
  }

#define SS_ASERT_ERRNO(x) \
  if (!(x)) { \
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT %d:%s", errno, strerror(errno)); \
  }

#define SS_ASERT_LOG(x, fmt, ...) \
  if (!(x)) { \
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "SS_ASSERT " fmt, ## __VA_ARGS__); \
  }

#define SS_STATIC_ASERT(expr) \
  { \
    char STATIC_ASSERTION_FAILED[(expr) ? 1 : -1]; \
    (void)STATIC_ASSERTION_FAILED; \
  }

/**
 * @class EnumStringMap
 * \~english @brief EnumStringMap
 * \~english @par   Brief Introduction
 *        Class to provide EnumStringMap template function
 *
 */
template <typename enumT, void(*fp)(std::map<enumT, SS_String> & m_strMap)> class EnumStringMap {  // NOLINT (runtime/references)
public:
  /////////////////////////////////////////////////////////////////////////////////////
  /// \ingroup EnumStringMap
  /// \~english @par Summary
  ///       Default constructor of EnumStringMap class.
  /// \~english @param None
  /// \~english @retval None
  /// \~english @par Preconditions
  ///       - None.
  /// \~english @par Change of the internal state
  ///       - The internal state is not changed.
  /// \~english @par Causes of failures
  ///       None
  /// \~english @par Classification
  ///       Public
  /// \~english @par Type
  ///       None
  /// \~english @par Detail
  ///       To generate a EnumStringMap class, and initialize member variables. \n
  ///       After the constructor, be sure to call the Initialize. \n
  /// \~english @see  ~EnumStringMap, Initialize
  ////////////////////////////////////////////////////////////////////////////////////
  EnumStringMap() {
    (*fp)(m_strMap);
  }
  /////////////////////////////////////////////////////////////////////////////////////
  /// \ingroup ~EnumStringMap
  /// \~english @par Summary
  ///
  /// \~english @param None
  /// \~english @retval None
  /// \~english @par Preconditions
  ///       - None.
  /// \~english @par Change of the internal state
  ///       - The internal state is not changed.
  /// \~english @par Causes of failures
  ///       None
  /// \~english @par Classification
  ///       Public
  /// \~english @par Type
  ///       None
  /// \~english @par Detail
  ///       To delete a EnumStringMap object. \n
  /// \~english @see  EnumStringMap
  ////////////////////////////////////////////////////////////////////////////////////
  ~EnumStringMap() {}
  SS_String GetStr( enumT f_enum) { return m_strMap[ f_enum ];}
private:
  std::map<enumT, SS_String> m_strMap;
};
// End of template <typename enumT, void(*fp)(std::map<enumT, SS_String> & m_strMap)> class EnumStringMap

#endif /* __SS_TEMPLATES_H__ */  // NOLINT (build/header_guard)