1 /** @addtogroup MCD_MCDIMPL_DAEMON_DEV 2 * @{ 3 * @file 4 * 5 * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 --> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote 16 * products derived from this software without specific prior 17 * written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "TrustletSession.h" 33 #include <cstdlib> 34 35 #include "log.h" 36 37 using namespace std; 38 39 //------------------------------------------------------------------------------ 40 TrustletSession::TrustletSession(Connection *deviceConnection, uint32_t sessionId) 41 { 42 this->deviceConnection = deviceConnection; 43 this->notificationConnection = NULL; 44 this->sessionId = sessionId; 45 sessionMagic = rand(); 46 } 47 48 49 //------------------------------------------------------------------------------ 50 TrustletSession::~TrustletSession(void) 51 { 52 map<uint32_t, CWsm_ptr>::iterator it; 53 delete notificationConnection; 54 55 if (!buffers.empty()) { 56 LOG_W("%s: Mapped buffers still available %u", __func__, buffers.size()); 57 } 58 for ( it = buffers.begin() ; it != buffers.end(); it++ ) 59 delete (*it).second; 60 61 buffers.clear(); 62 } 63 64 //------------------------------------------------------------------------------ 65 void TrustletSession::queueNotification(notification_t *notification) 66 { 67 notifications.push(*notification); 68 } 69 70 //------------------------------------------------------------------------------ 71 void TrustletSession::processQueuedNotifications(void) 72 { 73 // Nothing to do here! 74 if (notificationConnection == NULL) 75 return; 76 77 while (!notifications.empty()) { 78 // Forward session ID and additional payload of 79 // notification to the just established connection 80 notificationConnection->writeData((void *)¬ifications.front(), 81 sizeof(notification_t)); 82 notifications.pop(); 83 } 84 } 85 86 //------------------------------------------------------------------------------ 87 bool TrustletSession::addBulkBuff(CWsm_ptr pWsm) 88 { 89 if (!pWsm) 90 return false; 91 if (buffers.find(pWsm->handle) != buffers.end()) { 92 delete pWsm; 93 return false; 94 } 95 buffers[pWsm->handle] = pWsm; 96 return true; 97 } 98 99 //------------------------------------------------------------------------------ 100 bool TrustletSession::removeBulkBuff(uint32_t handle) 101 { 102 if (buffers.find(handle) == buffers.end()) { 103 return false; 104 } 105 CWsm_ptr pWsm = buffers[handle]; 106 delete pWsm; 107 buffers.erase(handle); 108 return true; 109 } 110 111 112 CWsm_ptr TrustletSession::popBulkBuff() 113 { 114 if (buffers.empty()) { 115 return NULL; 116 } 117 118 CWsm_ptr pWsm = buffers.begin()->second; 119 // Remove it from the map 120 buffers.erase(pWsm->handle); 121 return pWsm; 122 } 123 124 /** @} */ 125