|
1 // Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Symbian Foundation License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Trace Core |
|
15 // |
|
16 #include <kern_priv.h> |
|
17 #include "TraceCoreRouter.h" |
|
18 #include "TraceCoreSubscriber.h" |
|
19 #include "TraceCoreDebug.h" |
|
20 #include "OstTraceDefinitions.h" |
|
21 |
|
22 #ifdef OST_TRACE_COMPILER_IN_USE |
|
23 #include "TraceCoreRouterTraces.h" |
|
24 #endif |
|
25 |
|
26 // Constants |
|
27 |
|
28 /** |
|
29 * Constructor |
|
30 */ |
|
31 DTraceCoreRouter::DTraceCoreRouter() |
|
32 : iMessageSender( NULL ) |
|
33 { |
|
34 } |
|
35 |
|
36 |
|
37 /** |
|
38 * Destructor |
|
39 */ |
|
40 DTraceCoreRouter::~DTraceCoreRouter() |
|
41 { |
|
42 Kern::MutexWait(*iLock); |
|
43 iRoutingItems.Reset(); |
|
44 Kern::MutexSignal(*iLock); |
|
45 iLock->Close(NULL); |
|
46 } |
|
47 |
|
48 |
|
49 /** |
|
50 * Initializes this router |
|
51 * |
|
52 * @param aMessageSender The message sender interface |
|
53 */ |
|
54 TInt DTraceCoreRouter::Init( MTraceCoreMessageSender& aMessageSender ) |
|
55 { |
|
56 iMessageSender = &aMessageSender; |
|
57 _LIT(KTraceCoreRouterLock, "DTraceCoreRouter_Lock"); |
|
58 TInt err = Kern::MutexCreate(iLock, KTraceCoreRouterLock, KMutexOrdGeneral0); |
|
59 return err; |
|
60 } |
|
61 |
|
62 |
|
63 /** |
|
64 * Callback for incoming messages |
|
65 * |
|
66 * @param aMsg The message |
|
67 * @return KErrNotFound if the message id was not found (not subscribed), KErrNone if found |
|
68 */ |
|
69 TInt DTraceCoreRouter::MessageReceived( TTraceMessage &aMsg ) |
|
70 { |
|
71 OstTraceExt1( TRACE_FLOW, DTRACECOREROUTER_MESSAGERECEIVED_ENTRY, "> DTraceCoreRouter::MessageReceived. MsgId:0x%hhx", aMsg.iMessageId ); |
|
72 |
|
73 TInt ret( KErrNotFound ); |
|
74 TUint32 messageId = aMsg.iMessageId; |
|
75 TUint32 msgFormat = aMsg.iMsgFormat; |
|
76 |
|
77 Kern::MutexWait(*iLock); |
|
78 for ( TInt i = 0; i < iRoutingItems.Count(); i++ ) |
|
79 { |
|
80 if ( iRoutingItems[ i ].iMessageID == messageId && iRoutingItems[ i ].iMsgFormat == msgFormat ) |
|
81 { |
|
82 iRoutingItems[ i ].iSubscriber->MessageReceived( aMsg ); |
|
83 ret = KErrNone; |
|
84 } |
|
85 } |
|
86 Kern::MutexSignal(*iLock); |
|
87 |
|
88 OstTrace1( TRACE_FLOW, DTRACECOREROUTER_MESSAGERECEIVED_EXIT, "< DTraceCoreRouter::MessageReceived. Ret:%d", ret ); |
|
89 return ret; |
|
90 } |
|
91 |
|
92 |
|
93 /** |
|
94 * Subscribes to a message |
|
95 * |
|
96 * @param aRoutingItem The subscription properties. |
|
97 */ |
|
98 TInt DTraceCoreRouter::Subscribe( TRoutingItem& aRoutingItem ) |
|
99 { |
|
100 OstTrace1( TRACE_FLOW, DTRACECOREROUTER_SUBSCRIBE_ENTRY, "> DTraceCoreRouter::Subscribe 0x%x", ( TUint )&( aRoutingItem ) ); |
|
101 |
|
102 TInt err = KErrNone; |
|
103 if ( iMessageSender != NULL ) |
|
104 { |
|
105 if ( aRoutingItem.iSubscriber != NULL ) |
|
106 { |
|
107 Kern::MutexWait(*iLock); |
|
108 // Add to routingItems array |
|
109 err = iRoutingItems.Append( aRoutingItem ); |
|
110 Kern::MutexSignal(*iLock); |
|
111 if ( err == KErrNone ) |
|
112 { |
|
113 // Set message sender to routing item |
|
114 aRoutingItem.iSubscriber->SetMessageSender( *iMessageSender ); |
|
115 OstTraceExt2( TRACE_NORMAL, DTRACECOREROUTER_SUBSCRIBE_SUBSCRIBED_TO_MESSAGE, "DTraceCoreRouter::Subscribe - Subscribed to message. Subscriber:0x%x MsgId:0x%x", (TUint)aRoutingItem.iSubscriber, (TUint)aRoutingItem.iMessageID ); |
|
116 } |
|
117 } |
|
118 // Subscriber was NULL |
|
119 else |
|
120 { |
|
121 err = KErrArgument; |
|
122 } |
|
123 } |
|
124 // Message sender not set |
|
125 else |
|
126 { |
|
127 err = KErrGeneral; |
|
128 } |
|
129 OstTrace1( TRACE_FLOW, DTRACECOREROUTER_SUBSCRIBE_EXIT, "< DTraceCoreRouter::Subscribe. Err:%d", err ); |
|
130 return err; |
|
131 } |
|
132 |
|
133 |
|
134 /** |
|
135 * Unsubscribes from a message |
|
136 * |
|
137 * @param aRoutingItem The subscription properties |
|
138 */ |
|
139 void DTraceCoreRouter::Unsubscribe( TRoutingItem& aRoutingItem ) |
|
140 { |
|
141 // Make sure not to delete item while going through items somewhere else |
|
142 Kern::MutexWait(*iLock); |
|
143 for ( TInt i = 0; i < iRoutingItems.Count(); i++ ) |
|
144 { |
|
145 if ( iRoutingItems[ i ].iMessageID == aRoutingItem.iMessageID |
|
146 && iRoutingItems[ i ].iSubscriber == aRoutingItem.iSubscriber ) |
|
147 { |
|
148 OstTraceExt2( TRACE_NORMAL, DTRACECOREROUTER_UNSUBSCRIBE_UNSUBSCRIBED, "DTraceCoreRouter::Unsubscribe - Unsubscribed 0x%x MsgID:0x%x", (TUint)aRoutingItem.iSubscriber, (TUint)aRoutingItem.iMessageID ); |
|
149 iRoutingItems.Remove( i ); |
|
150 i--; |
|
151 } |
|
152 } |
|
153 Kern::MutexSignal(*iLock); |
|
154 } |
|
155 |
|
156 |
|
157 /** |
|
158 * Unsubscribes from all messages of given subscriber |
|
159 * |
|
160 * @param aSubscriber The subscriber to be unregistered |
|
161 */ |
|
162 void DTraceCoreRouter::Unsubscribe( DTraceCoreSubscriber& aSubscriber ) |
|
163 { |
|
164 OstTrace1( TRACE_FLOW, DTRACECOREROUTER_UNSUBSCRIBEALL_ENTRY, "> DTraceCoreRouter::UnsubscribeAll 0x%x", ( TUint )&( aSubscriber ) ); |
|
165 |
|
166 Kern::MutexWait(*iLock); |
|
167 for ( TInt i = 0; i < iRoutingItems.Count(); i++ ) |
|
168 { |
|
169 if ( iRoutingItems[ i ].iSubscriber == &aSubscriber ) |
|
170 { |
|
171 OstTraceExt2( TRACE_NORMAL, DTRACECOREROUTER_UNSUBSCRIBEALL_UNSUBSCRIBED, "DTraceCoreRouter::UnsubscribeAll - Unsubscribed 0x%x MsgID:%d", (TUint)&aSubscriber, (TInt)iRoutingItems[ i ].iMessageID ); |
|
172 iRoutingItems.Remove( i ); |
|
173 i--; |
|
174 } |
|
175 } |
|
176 Kern::MutexSignal(*iLock); |
|
177 } |
|
178 |
|
179 // End of File |