Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / notification_persistent_service / server / src / ns_npp_state_notification.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup  tag_NS_NPPService
20 /// .
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup  tag_NS_NPPService
25 /// \brief    This file contains implementation of class CStateNotification.
26 ///
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
28
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
30 // Include Files
31 ////////////////////////////////////////////////////////////////////////////////////////////////////
32 #include <iostream>
33 #include <string>
34 #include "ns_npp_notificationpersistentservicelog.h"
35 #include "ns_npp_persistent_data.h"
36 #include "ns_npp_state_notification.h"
37
38
39
40 ////////////////////////////////////////////////////////////////////////////////////////////////////
41 /// CStateNotification
42 /// Constructor of CStateNotification class
43 ////////////////////////////////////////////////////////////////////////////////////////////////////
44 CStateNotification::CStateNotification(const std::string &f_cnotificationname,
45                                        const UI_32 f_uimaxmsgsize):
46   CNotification(f_cnotificationname, f_uimaxmsgsize),
47   m_pData(NULL),
48   m_pDefaultData(NULL),
49   m_bWasPublished(FALSE) {
50   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
51
52   m_ePersistentType = eFrameworkunifiedStateVar;
53
54   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
55 }
56
57
58 ////////////////////////////////////////////////////////////////////////////////////////////////////
59 /// ~CStateNotification
60 /// Destructor of CStateNotification class
61 ////////////////////////////////////////////////////////////////////////////////////////////////////
62 CStateNotification::~CStateNotification() {
63   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
64
65   if (NULL != m_pData) {
66     delete m_pData;   // LCOV_EXCL_BR_LINE 11: unexpected branch
67     m_pData = NULL;
68   }
69
70   if (NULL != m_pDefaultData) {
71     delete m_pDefaultData;
72     m_pDefaultData = NULL;
73   }
74
75   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
76 }
77 ////////////////////////////////////////////////////////////////////////////////////////////////////
78 /// GetPersistentData
79 /// This function is used to get the persistent data pointer
80 ////////////////////////////////////////////////////////////////////////////////////////////////////
81 const CPersistentData *CStateNotification::GetPersistentData() {
82   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
83
84   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
85
86   return m_pData;
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////////////////////////
90 /// SetPersistentData
91 /// This function is used to set the data related to notification
92 ////////////////////////////////////////////////////////////////////////////////////////////////////
93 EFrameworkunifiedStatus CStateNotification::SetPersistentData(PVOID f_pmessage,
94                                                  const UI_32 f_msgsize) {
95   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
96   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
97
98   if (NULL == m_pData) {
99     m_pData = new(std::nothrow) CPersistentData();  // LCOV_EXCL_BR_LINE 11: unexpected branch
100   }
101
102   l_estatus = SetData(m_pData, f_pmessage, f_msgsize);
103
104   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
105   return l_estatus;
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////////////////////////
109 /// GetDefaultPersistentData
110 /// This function get the default data(if any) related to notification
111 ////////////////////////////////////////////////////////////////////////////////////////////////////
112 const CPersistentData *CStateNotification::GetDefaultPersistentData() {
113   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
114
115   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
116
117   return m_pDefaultData;
118 }
119
120 ////////////////////////////////////////////////////////////////////////////////////////////////////
121 /// SetDefaultPersistentData
122 /// This function is used to set the default data related to notification
123 ////////////////////////////////////////////////////////////////////////////////////////////////////
124 EFrameworkunifiedStatus CStateNotification::SetDefaultPersistentData(PVOID f_pmessage,
125                                                         const UI_32 f_msgsize) {
126   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
127   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
128
129   if (NULL == m_pDefaultData) {
130     m_pDefaultData = new(std::nothrow) CPersistentData();  // LCOV_EXCL_BR_LINE 11: except,C++ STL
131   }
132
133   // set the default persistent data
134   if (eFrameworkunifiedStatusOK == (l_estatus = SetData(m_pDefaultData, f_pmessage, f_msgsize))) {
135     if (NULL == m_pData) {
136       // set the default data as persistent data if notification is not yet published
137       l_estatus = SetPersistentData(f_pmessage, f_msgsize);
138       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Setting Persistent data");
139     } else {
140       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Persistent data already set");
141     }
142   } else {
143     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error setting default persistent data for notification:: %s",
144            m_cNotificationName.c_str());
145   }
146
147   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
148   return l_estatus;
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////////////////////////
152 /// SetData
153 /// This function is used to set the persistent data
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 EFrameworkunifiedStatus CStateNotification::SetData(CPersistentData *f_pdata,
156                                        PVOID f_pmessage,
157                                        const UI_32 f_msgsize) {
158   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
159   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
160
161   if (NULL != f_pdata) {  // LCOV_EXCL_BR_LINE 6: double check, f_pdata can't be NULL
162     if (m_uiMaxMsgSize >= f_msgsize) {
163       f_pdata->m_uiMsgSize = f_msgsize;
164
165       if (NULL != f_pmessage && 0 != f_msgsize) {
166         if (NULL == f_pdata->m_pMessage) {
167           f_pdata->m_pMessage = new(std::nothrow) CHAR[m_uiMaxMsgSize];
168         }
169
170         if (NULL != f_pdata->m_pMessage) {  // LCOV_EXCL_BR_LINE 5: new error case
171           std::memset(f_pdata->m_pMessage, 0, m_uiMaxMsgSize);
172           std::memcpy(f_pdata->m_pMessage, f_pmessage, f_msgsize);
173         } else {
174           // LCOV_EXCL_START 5: new error case
175           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
176           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Memory allocation error, unable to set persistent data for notification: %s",
177                  m_cNotificationName.c_str());
178           l_estatus = eFrameworkunifiedStatusNullPointer;
179           // LCOV_EXCL_STOP
180         }
181       } else {
182         if (NULL != f_pdata->m_pMessage) {
183           delete[](static_cast<PCHAR>(f_pdata->m_pMessage));
184           f_pdata->m_pMessage = NULL;
185         }
186       }
187
188       m_bWasPublished = TRUE;
189     } else {
190       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
191              "Can't set notification data. Message data size (%d) is greater than maximum registered data size (%d)"
192              " of notification (%s).", f_msgsize, m_uiMaxMsgSize, GetNotificationName().c_str());
193       l_estatus = eFrameworkunifiedStatusFail;
194     }
195   } else {
196     // LCOV_EXCL_START 6: double check, f_pdata can't be NULL
197     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
198     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Unable to set persistent data for notification: %s, persistent object is NULL",
199            m_cNotificationName.c_str());
200     l_estatus = eFrameworkunifiedStatusNullPointer;
201     // LCOV_EXCL_STOP
202   }
203
204   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
205   return l_estatus;
206 }
207
208 ////////////////////////////////////////////////////////////////////////////////////////////////////
209 /// AddEventReciever
210 /// This function adds the name of the application to receiver list of particular notification.
211 ////////////////////////////////////////////////////////////////////////////////////////////////////
212 EFrameworkunifiedStatus CStateNotification::AddEventReciever(const std::string &f_csubscribername) {
213   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
214   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
215
216   // Message Queue Handle
217   HANDLE l_hMsgQHandle = NULL;
218
219   // iterator of CNotificationReceiver map
220   NotifReceiver_Iterator_Type l_iterator;
221
222   CNotificationReceiver *l_pNotificationReceiver = NULL;
223
224   if (!f_csubscribername.empty()) {  // LCOV_EXCL_BR_LINE 6: double check, f_csubscribername can't be empty
225     l_estatus = AddReceiverInMap(f_csubscribername);
226   } else {
227     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
228     l_estatus = eFrameworkunifiedStatusInvldParam;  // LCOV_EXCL_LINE 6: double check, f_csubscribername can't be empty
229   }
230
231   if (eFrameworkunifiedStatusOK == l_estatus) {  // LCOV_EXCL_BR_LINE 6: double check, l_estatus must be eFrameworkunifiedStatusOK
232     if (m_bWasPublished) {
233       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]
234
235       if (NULL != m_pData) {
236         l_iterator = m_pmSubscribersList->find(f_csubscribername);
237
238         if (m_pmSubscribersList->end() != l_iterator) {
239           l_pNotificationReceiver = (*l_iterator).second;
240
241           if (NULL != l_pNotificationReceiver) {  // LCOV_EXCL_BR_LINE 6: double check, l_pNotificationReceiver can't be NULL  // NOLINT[whitespace/line_length]
242             l_hMsgQHandle = l_pNotificationReceiver->m_MsgQHandle;
243
244             if (NULL != l_hMsgQHandle) {  // LCOV_EXCL_BR_LINE 6: double check, l_hMsgQHandle can't be NULL
245               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]
246                 // LCOV_EXCL_START 4: NSFW error case
247                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
248                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
249                        "Error Publishing notification %s to %s published by %s, error status: 0x%x",
250                        m_cNotificationName.c_str(), l_iterator->first.c_str(), m_cServiceName.c_str(), l_estatus);
251                 // LCOV_EXCL_STOP
252               }
253             } else {
254               // LCOV_EXCL_START 6: double check, l_hMsgQHandle can't be NULL
255               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
256               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "MsgQ Handle NULL");
257               l_estatus = eFrameworkunifiedStatusNullPointer;
258               // LCOV_EXCL_STOP
259             }
260           } else {
261             // LCOV_EXCL_START 6: double check, l_pNotificationReceiver can't be NULL
262             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
263             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pNotificationReceiver is NULL");
264             l_estatus = eFrameworkunifiedStatusNullPointer;
265             // LCOV_EXCL_STOP
266           }
267         }
268       } else {
269         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_pData pointer is NULL");
270         l_estatus = eFrameworkunifiedStatusNullPointer;
271       }
272     } else {
273       FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Subscribing Notfn, src=%s name=%s. Notification not published.",
274              f_csubscribername.c_str(), m_cNotificationName.c_str());
275     }
276   } else {
277     // LCOV_EXCL_START 6: double check, l_estatus must be eFrameworkunifiedStatusOK
278     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
279     l_estatus = eFrameworkunifiedStatusFail;
280     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Receiver Not Added in map");
281     // LCOV_EXCL_STOP
282   }
283
284   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
285   return l_estatus;
286 }
287
288
289 ////////////////////////////////////////////////////////////////////////////////////////////////////
290 /// Publish
291 /// This function publishes the notification to subscribed clients.
292 ////////////////////////////////////////////////////////////////////////////////////////////////////
293 EFrameworkunifiedStatus CStateNotification::Publish(const std::string &f_cservicename,
294                                        PVOID f_pmessage,
295                                        const UI_32 f_uimsgsize) {
296   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
297   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
298
299   // Message Queue Handle
300   HANDLE l_hMsgQHandle = NULL;
301
302   // Pointer to class CNotificationReceiver
303   CNotificationReceiver *l_pNotificationReceiver = NULL;
304
305   // Iterator for Notification Receiver map
306   NotifReceiver_Iterator_Type l_itrNotifReceiver;
307
308   if (m_uiMaxMsgSize >= f_uimsgsize) {
309     if (0 == m_cServiceName.compare(f_cservicename)) {
310       l_estatus = SetPersistentData(f_pmessage,
311                                     f_uimsgsize);
312
313       if (eFrameworkunifiedStatusOK == l_estatus) {  // LCOV_EXCL_BR_LINE 6: l_estatus is always eFrameworkunifiedStatusOK
314         for (l_itrNotifReceiver = m_pmSubscribersList->begin();
315              l_itrNotifReceiver != m_pmSubscribersList->end();
316              l_itrNotifReceiver++) {
317           l_pNotificationReceiver = l_itrNotifReceiver->second;
318
319           if (NULL != l_pNotificationReceiver) {  // LCOV_EXCL_BR_LINE 6:double check, l_pNotificationReceiver can not be null  // NOLINT[whitespace/line_length]
320             l_hMsgQHandle = l_pNotificationReceiver->m_MsgQHandle;
321
322             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]
323               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]
324                 // LCOV_EXCL_START 4: NSFW error case
325                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
326                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
327                        "Error Publishing notification %s to %s published by %s, error status: 0x%x",
328                        m_cNotificationName.c_str(),
329                        l_itrNotifReceiver->first.c_str(),
330                        f_cservicename.c_str(), l_estatus);
331                 // LCOV_EXCL_STOP
332               }
333             } else {
334               // LCOV_EXCL_START 6: double check, l_hMsgQHandle and m_pData can not be null
335               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
336               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "MsgQ Handle NULL");
337               l_estatus = eFrameworkunifiedStatusNullPointer;
338               // LCOV_EXCL_STOP
339             }
340           } else {
341             // LCOV_EXCL_START 6: double check, l_pNotificationReceiver can not be null
342             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
343             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pNotificationReceiver is NULL");
344             l_estatus = eFrameworkunifiedStatusNullPointer;
345             // LCOV_EXCL_STOP
346           }
347         }
348       } else {
349         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
350         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Persistent Data Not Set");  // LCOV_EXCL_LINE 6: double check, l_estatus is always eFrameworkunifiedStatusOK  // NOLINT[whitespace/line_length]
351       }
352     } else {
353       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Can't publish notification %s. Registered by %s and published by %s.",
354              GetNotificationName().c_str(), m_cServiceName.c_str(), f_cservicename.c_str());
355       l_estatus = eFrameworkunifiedStatusInvldParam;
356     }
357   } else {
358     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
359            "Can't publish notification %s. Message data size (%d) is greater than maximum registered data size (%d)",
360            GetNotificationName().c_str(), f_uimsgsize, m_uiMaxMsgSize);
361     l_estatus = eFrameworkunifiedStatusFail;
362   }
363
364   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
365   return l_estatus;
366 }
367
368 ////////////////////////////////////////////////////////////////////////////////////////////////////
369 /// IsPublished
370 /// This functions returns the published status of notification.
371 ////////////////////////////////////////////////////////////////////////////////////////////////////
372 BOOL CStateNotification::IsPublished() {
373   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
374
375   return m_bWasPublished;
376 }
377
378 ////////////////////////////////////////////////////////////////////////////////////////////////
379 /// ResetMaxMessageSize
380 /// This function reset the max size of data that can be published with notification.
381 /// Also deletes the old persistent data and default data.
382 ////////////////////////////////////////////////////////////////////////////////////////////////
383 EFrameworkunifiedStatus CStateNotification::ResetMaxMessageSize(const UI_32 f_uilength) {
384   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
385   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
386
387   CNotification::ResetMaxMessageSize(f_uilength);
388
389   // delete the old data as the size is changed the data may become invalid
390   if (NULL != m_pData) {
391     delete m_pData;
392     m_pData = NULL;
393   }
394
395   if (NULL != m_pDefaultData) {
396     delete m_pDefaultData;
397     m_pDefaultData = NULL;
398   }
399
400   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
401   return l_estatus;
402 }