summaryrefslogtreecommitdiffstats
path: root/nsframework/notification_persistent_service/server/src/ns_npp_state_notification.cpp
blob: ff8f054fb7adfefbd27dbcd32b1d49482f88ee0e (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * @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.
 */

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \defgroup <<Group Tag>> <<Group Name>>
/// \ingroup  tag_NS_NPPService
/// .
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NS_NPPService
/// \brief    This file contains implementation of class CStateNotification.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include "ns_npp_notificationpersistentservicelog.h"
#include "ns_npp_persistent_data.h"
#include "ns_npp_state_notification.h"



////////////////////////////////////////////////////////////////////////////////////////////////////
/// CStateNotification
/// Constructor of CStateNotification class
////////////////////////////////////////////////////////////////////////////////////////////////////
CStateNotification::CStateNotification(const std::string &f_cnotificationname,
                                       const UI_32 f_uimaxmsgsize):
  CNotification(f_cnotificationname, f_uimaxmsgsize),
  m_pData(NULL),
  m_pDefaultData(NULL),
  m_bWasPublished(FALSE) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  m_ePersistentType = eFrameworkunifiedStateVar;

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
}


////////////////////////////////////////////////////////////////////////////////////////////////////
/// ~CStateNotification
/// Destructor of CStateNotification class
////////////////////////////////////////////////////////////////////////////////////////////////////
CStateNotification::~CStateNotification() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (NULL != m_pData) {
    delete m_pData;   // LCOV_EXCL_BR_LINE 11: unexpected branch
    m_pData = NULL;
  }

  if (NULL != m_pDefaultData) {
    delete m_pDefaultData;
    m_pDefaultData = NULL;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// GetPersistentData
/// This function is used to get the persistent data pointer
////////////////////////////////////////////////////////////////////////////////////////////////////
const CPersistentData *CStateNotification::GetPersistentData() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");

  return m_pData;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetPersistentData
/// This function is used to set the data related to notification
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::SetPersistentData(PVOID f_pmessage,
                                                 const UI_32 f_msgsize) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (NULL == m_pData) {
    m_pData = new(std::nothrow) CPersistentData();  // LCOV_EXCL_BR_LINE 11: unexpected branch
  }

  l_estatus = SetData(m_pData, f_pmessage, f_msgsize);

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// GetDefaultPersistentData
/// This function get the default data(if any) related to notification
////////////////////////////////////////////////////////////////////////////////////////////////////
const CPersistentData *CStateNotification::GetDefaultPersistentData() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");

  return m_pDefaultData;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetDefaultPersistentData
/// This function is used to set the default data related to notification
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::SetDefaultPersistentData(PVOID f_pmessage,
                                                        const UI_32 f_msgsize) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (NULL == m_pDefaultData) {
    m_pDefaultData = new(std::nothrow) CPersistentData();  // LCOV_EXCL_BR_LINE 11: except,C++ STL
  }

  // set the default persistent data
  if (eFrameworkunifiedStatusOK == (l_estatus = SetData(m_pDefaultData, f_pmessage, f_msgsize))) {
    if (NULL == m_pData) {
      // set the default data as persistent data if notification is not yet published
      l_estatus = SetPersistentData(f_pmessage, f_msgsize);
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Setting Persistent data");
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Persistent data already set");
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error setting default persistent data for notification:: %s",
           m_cNotificationName.c_str());
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetData
/// This function is used to set the persistent data
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::SetData(CPersistentData *f_pdata,
                                       PVOID f_pmessage,
                                       const UI_32 f_msgsize) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (NULL != f_pdata) {  // LCOV_EXCL_BR_LINE 6: double check, f_pdata can't be NULL
    if (m_uiMaxMsgSize >= f_msgsize) {
      f_pdata->m_uiMsgSize = f_msgsize;

      if (NULL != f_pmessage && 0 != f_msgsize) {
        if (NULL == f_pdata->m_pMessage) {
          f_pdata->m_pMessage = new(std::nothrow) CHAR[m_uiMaxMsgSize];
        }

        if (NULL != f_pdata->m_pMessage) {  // LCOV_EXCL_BR_LINE 5: new error case
          std::memset(f_pdata->m_pMessage, 0, m_uiMaxMsgSize);
          std::memcpy(f_pdata->m_pMessage, f_pmessage, f_msgsize);
        } else {
          // LCOV_EXCL_START 5: new error case
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Memory allocation error, unable to set persistent data for notification: %s",
                 m_cNotificationName.c_str());
          l_estatus = eFrameworkunifiedStatusNullPointer;
          // LCOV_EXCL_STOP
        }
      } else {
        if (NULL != f_pdata->m_pMessage) {
          delete[](static_cast<PCHAR>(f_pdata->m_pMessage));
          f_pdata->m_pMessage = NULL;
        }
      }

      m_bWasPublished = TRUE;
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "Can't set notification data. Message data size (%d) is greater than maximum registered data size (%d)"
             " of notification (%s).", f_msgsize, m_uiMaxMsgSize, GetNotificationName().c_str());
      l_estatus = eFrameworkunifiedStatusFail;
    }
  } else {
    // LCOV_EXCL_START 6: double check, f_pdata can't be NULL
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Unable to set persistent data for notification: %s, persistent object is NULL",
           m_cNotificationName.c_str());
    l_estatus = eFrameworkunifiedStatusNullPointer;
    // LCOV_EXCL_STOP
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// AddEventReciever
/// This function adds the name of the application to receiver list of particular notification.
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::AddEventReciever(const std::string &f_csubscribername) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  // Message Queue Handle
  HANDLE l_hMsgQHandle = NULL;

  // iterator of CNotificationReceiver map
  NotifReceiver_Iterator_Type l_iterator;

  CNotificationReceiver *l_pNotificationReceiver = NULL;

  if (!f_csubscribername.empty()) {  // LCOV_EXCL_BR_LINE 6: double check, f_csubscribername can't be empty
    l_estatus = AddReceiverInMap(f_csubscribername);
  } else {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    l_estatus = eFrameworkunifiedStatusInvldParam;  // LCOV_EXCL_LINE 6: double check, f_csubscribername can't be empty
  }

  if (eFrameworkunifiedStatusOK == l_estatus) {  // LCOV_EXCL_BR_LINE 6: double check, l_estatus must be eFrameworkunifiedStatusOK
    if (m_bWasPublished) {
      FRAMEWORKUNIFIEDLOG(ZONE_NPP_INFO, __FUNCTION__, "Republish Notification to subscriber :: %s", f_csubscribername.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]

      if (NULL != m_pData) {
        l_iterator = m_pmSubscribersList->find(f_csubscribername);

        if (m_pmSubscribersList->end() != l_iterator) {
          l_pNotificationReceiver = (*l_iterator).second;

          if (NULL != l_pNotificationReceiver) {  // LCOV_EXCL_BR_LINE 6: double check, l_pNotificationReceiver can't be NULL  // NOLINT[whitespace/line_length]
            l_hMsgQHandle = l_pNotificationReceiver->m_MsgQHandle;

            if (NULL != l_hMsgQHandle) {  // LCOV_EXCL_BR_LINE 6: double check, l_hMsgQHandle can't be NULL
              if (eFrameworkunifiedStatusOK != (l_estatus = PublishData(l_pNotificationReceiver->m_MsgQHandle, m_pData->m_pMessage, m_pData->m_uiMsgSize))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case  // NOLINT[whitespace/line_length]
                // LCOV_EXCL_START 4: NSFW error case
                AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                       "Error Publishing notification %s to %s published by %s, error status: 0x%x",
                       m_cNotificationName.c_str(), l_iterator->first.c_str(), m_cServiceName.c_str(), l_estatus);
                // LCOV_EXCL_STOP
              }
            } else {
              // LCOV_EXCL_START 6: double check, l_hMsgQHandle can't be NULL
              AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
              FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "MsgQ Handle NULL");
              l_estatus = eFrameworkunifiedStatusNullPointer;
              // LCOV_EXCL_STOP
            }
          } else {
            // LCOV_EXCL_START 6: double check, l_pNotificationReceiver can't be NULL
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pNotificationReceiver is NULL");
            l_estatus = eFrameworkunifiedStatusNullPointer;
            // LCOV_EXCL_STOP
          }
        }
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_pData pointer is NULL");
        l_estatus = eFrameworkunifiedStatusNullPointer;
      }
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Subscribing Notfn, src=%s name=%s. Notification not published.",
             f_csubscribername.c_str(), m_cNotificationName.c_str());
    }
  } else {
    // LCOV_EXCL_START 6: double check, l_estatus must be eFrameworkunifiedStatusOK
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    l_estatus = eFrameworkunifiedStatusFail;
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Receiver Not Added in map");
    // LCOV_EXCL_STOP
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
/// Publish
/// This function publishes the notification to subscribed clients.
////////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::Publish(const std::string &f_cservicename,
                                       PVOID f_pmessage,
                                       const UI_32 f_uimsgsize) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  // Message Queue Handle
  HANDLE l_hMsgQHandle = NULL;

  // Pointer to class CNotificationReceiver
  CNotificationReceiver *l_pNotificationReceiver = NULL;

  // Iterator for Notification Receiver map
  NotifReceiver_Iterator_Type l_itrNotifReceiver;

  if (m_uiMaxMsgSize >= f_uimsgsize) {
    if (0 == m_cServiceName.compare(f_cservicename)) {
      l_estatus = SetPersistentData(f_pmessage,
                                    f_uimsgsize);

      if (eFrameworkunifiedStatusOK == l_estatus) {  // LCOV_EXCL_BR_LINE 6: l_estatus is always eFrameworkunifiedStatusOK
        for (l_itrNotifReceiver = m_pmSubscribersList->begin();
             l_itrNotifReceiver != m_pmSubscribersList->end();
             l_itrNotifReceiver++) {
          l_pNotificationReceiver = l_itrNotifReceiver->second;

          if (NULL != l_pNotificationReceiver) {  // LCOV_EXCL_BR_LINE 6:double check, l_pNotificationReceiver can not be null  // NOLINT[whitespace/line_length]
            l_hMsgQHandle = l_pNotificationReceiver->m_MsgQHandle;

            if (NULL != l_hMsgQHandle && NULL != m_pData) {  // LCOV_EXCL_BR_LINE 6:double check, l_hMsgQHandle and m_pData can not be null  // NOLINT[whitespace/line_length]
              if (eFrameworkunifiedStatusOK != (l_estatus = PublishData(l_hMsgQHandle, m_pData->m_pMessage, f_uimsgsize))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case  // NOLINT[whitespace/line_length]
                // LCOV_EXCL_START 4: NSFW error case
                AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                       "Error Publishing notification %s to %s published by %s, error status: 0x%x",
                       m_cNotificationName.c_str(),
                       l_itrNotifReceiver->first.c_str(),
                       f_cservicename.c_str(), l_estatus);
                // LCOV_EXCL_STOP
              }
            } else {
              // LCOV_EXCL_START 6: double check, l_hMsgQHandle and m_pData can not be null
              AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
              FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "MsgQ Handle NULL");
              l_estatus = eFrameworkunifiedStatusNullPointer;
              // LCOV_EXCL_STOP
            }
          } else {
            // LCOV_EXCL_START 6: double check, l_pNotificationReceiver can not be null
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pNotificationReceiver is NULL");
            l_estatus = eFrameworkunifiedStatusNullPointer;
            // LCOV_EXCL_STOP
          }
        }
      } else {
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Persistent Data Not Set");  // LCOV_EXCL_LINE 6: double check, l_estatus is always eFrameworkunifiedStatusOK  // NOLINT[whitespace/line_length]
      }
    } else {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Can't publish notification %s. Registered by %s and published by %s.",
             GetNotificationName().c_str(), m_cServiceName.c_str(), f_cservicename.c_str());
      l_estatus = eFrameworkunifiedStatusInvldParam;
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Can't publish notification %s. Message data size (%d) is greater than maximum registered data size (%d)",
           GetNotificationName().c_str(), f_uimsgsize, m_uiMaxMsgSize);
    l_estatus = eFrameworkunifiedStatusFail;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// IsPublished
/// This functions returns the published status of notification.
////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CStateNotification::IsPublished() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  return m_bWasPublished;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// ResetMaxMessageSize
/// This function reset the max size of data that can be published with notification.
/// Also deletes the old persistent data and default data.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CStateNotification::ResetMaxMessageSize(const UI_32 f_uilength) {
  EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  CNotification::ResetMaxMessageSize(f_uilength);

  // delete the old data as the size is changed the data may become invalid
  if (NULL != m_pData) {
    delete m_pData;
    m_pData = NULL;
  }

  if (NULL != m_pDefaultData) {
    delete m_pDefaultData;
    m_pDefaultData = NULL;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_estatus;
}