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 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup tag_NS_NPPService
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup tag_NS_NPPService
25 /// \brief This file contains implementation of class CStateNotification.
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
31 ////////////////////////////////////////////////////////////////////////////////////////////////////
34 #include "ns_npp_notificationpersistentservicelog.h"
35 #include "ns_npp_persistent_data.h"
36 #include "ns_npp_state_notification.h"
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),
49 m_bWasPublished(FALSE) {
50 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
52 m_ePersistentType = eFrameworkunifiedStateVar;
54 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
58 ////////////////////////////////////////////////////////////////////////////////////////////////////
59 /// ~CStateNotification
60 /// Destructor of CStateNotification class
61 ////////////////////////////////////////////////////////////////////////////////////////////////////
62 CStateNotification::~CStateNotification() {
63 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
65 if (NULL != m_pData) {
66 delete m_pData; // LCOV_EXCL_BR_LINE 11: unexpected branch
70 if (NULL != m_pDefaultData) {
71 delete m_pDefaultData;
72 m_pDefaultData = NULL;
75 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
77 ////////////////////////////////////////////////////////////////////////////////////////////////////
79 /// This function is used to get the persistent data pointer
80 ////////////////////////////////////////////////////////////////////////////////////////////////////
81 const CPersistentData *CStateNotification::GetPersistentData() {
82 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
84 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
89 ////////////////////////////////////////////////////////////////////////////////////////////////////
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__, "+");
98 if (NULL == m_pData) {
99 m_pData = new(std::nothrow) CPersistentData(); // LCOV_EXCL_BR_LINE 11: unexpected branch
102 l_estatus = SetData(m_pData, f_pmessage, f_msgsize);
104 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
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__, "+");
115 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
117 return m_pDefaultData;
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__, "+");
129 if (NULL == m_pDefaultData) {
130 m_pDefaultData = new(std::nothrow) CPersistentData(); // LCOV_EXCL_BR_LINE 11: except,C++ STL
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");
140 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Persistent data already set");
143 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error setting default persistent data for notification:: %s",
144 m_cNotificationName.c_str());
147 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
151 ////////////////////////////////////////////////////////////////////////////////////////////////////
153 /// This function is used to set the persistent data
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 EFrameworkunifiedStatus CStateNotification::SetData(CPersistentData *f_pdata,
157 const UI_32 f_msgsize) {
158 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
159 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
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;
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];
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);
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;
182 if (NULL != f_pdata->m_pMessage) {
183 delete[](static_cast<PCHAR>(f_pdata->m_pMessage));
184 f_pdata->m_pMessage = NULL;
188 m_bWasPublished = TRUE;
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;
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;
204 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
208 ////////////////////////////////////////////////////////////////////////////////////////////////////
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__, "+");
216 // Message Queue Handle
217 HANDLE l_hMsgQHandle = NULL;
219 // iterator of CNotificationReceiver map
220 NotifReceiver_Iterator_Type l_iterator;
222 CNotificationReceiver *l_pNotificationReceiver = NULL;
224 if (!f_csubscribername.empty()) { // LCOV_EXCL_BR_LINE 6: double check, f_csubscribername can't be empty
225 l_estatus = AddReceiverInMap(f_csubscribername);
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
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]
235 if (NULL != m_pData) {
236 l_iterator = m_pmSubscribersList->find(f_csubscribername);
238 if (m_pmSubscribersList->end() != l_iterator) {
239 l_pNotificationReceiver = (*l_iterator).second;
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;
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);
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;
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;
269 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "m_pData pointer is NULL");
270 l_estatus = eFrameworkunifiedStatusNullPointer;
273 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Subscribing Notfn, src=%s name=%s. Notification not published.",
274 f_csubscribername.c_str(), m_cNotificationName.c_str());
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");
284 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
289 ////////////////////////////////////////////////////////////////////////////////////////////////////
291 /// This function publishes the notification to subscribed clients.
292 ////////////////////////////////////////////////////////////////////////////////////////////////////
293 EFrameworkunifiedStatus CStateNotification::Publish(const std::string &f_cservicename,
295 const UI_32 f_uimsgsize) {
296 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
297 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
299 // Message Queue Handle
300 HANDLE l_hMsgQHandle = NULL;
302 // Pointer to class CNotificationReceiver
303 CNotificationReceiver *l_pNotificationReceiver = NULL;
305 // Iterator for Notification Receiver map
306 NotifReceiver_Iterator_Type l_itrNotifReceiver;
308 if (m_uiMaxMsgSize >= f_uimsgsize) {
309 if (0 == m_cServiceName.compare(f_cservicename)) {
310 l_estatus = SetPersistentData(f_pmessage,
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;
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;
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);
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;
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;
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]
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;
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;
364 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
368 ////////////////////////////////////////////////////////////////////////////////////////////////////
370 /// This functions returns the published status of notification.
371 ////////////////////////////////////////////////////////////////////////////////////////////////////
372 BOOL CStateNotification::IsPublished() {
373 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
375 return m_bWasPublished;
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__, "+");
387 CNotification::ResetMaxMessageSize(f_uilength);
389 // delete the old data as the size is changed the data may become invalid
390 if (NULL != m_pData) {
395 if (NULL != m_pDefaultData) {
396 delete m_pDefaultData;
397 m_pDefaultData = NULL;
400 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");