|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <math.h> |
|
21 |
|
22 #include "CRemoteInterface.h" |
|
23 #include "CTCPTransport.h" |
|
24 |
|
25 // Constant values |
|
26 const char KSpace[] = { " " }; |
|
27 const char KQuote[] = { "\"" }; |
|
28 const char KEquals[] = { "=" }; |
|
29 const char KRunCmd[] = { "runcmd" }; |
|
30 const char KServiceName[] = { "svcname" }; |
|
31 const char KCall[] = { "call" }; |
|
32 const char KMethodID[] = { "methodid" }; |
|
33 const char KStartServiceID[] = { "1" }; |
|
34 const char KStopServiceID[] = { "2" }; |
|
35 const char KDefaultMethodID[] = { "10" }; |
|
36 |
|
37 CRemoteInterface::CRemoteInterface( const string &aDestination ) |
|
38 : iTransport(NULL), iIsSetup(false), iDestination(aDestination), iConnected(false) |
|
39 { |
|
40 iRandomSeed = rand(); |
|
41 } |
|
42 |
|
43 CRemoteInterface::~CRemoteInterface() |
|
44 { |
|
45 if( iTransport ) |
|
46 { |
|
47 iTransport->Disconnect(); |
|
48 delete iTransport; |
|
49 iTransport = NULL; |
|
50 iConnected = false; |
|
51 } |
|
52 } |
|
53 |
|
54 void CRemoteInterface::SetupStartService( const string &aServiceName ) |
|
55 { |
|
56 // Setup the default start to the command line |
|
57 DefaultSetupCall( aServiceName, KStartServiceID ); |
|
58 |
|
59 // The command line is now setup |
|
60 iIsSetup = true; |
|
61 } |
|
62 |
|
63 void CRemoteInterface::SetupStopService( const string &aServiceName ) |
|
64 { |
|
65 // Setup the default start to the command line |
|
66 DefaultSetupCall( aServiceName, KStopServiceID ); |
|
67 |
|
68 // The command line is now setup |
|
69 iIsSetup = true; |
|
70 } |
|
71 |
|
72 void CRemoteInterface::SetupCall( const string &aServiceName, const string &aCall ) |
|
73 { |
|
74 // Setup the default start to the command line |
|
75 DefaultSetupCall( aServiceName, KDefaultMethodID ); |
|
76 |
|
77 // Add the call |
|
78 iCommandLine += KCall; |
|
79 iCommandLine += KEquals; |
|
80 iCommandLine += KQuote; |
|
81 iCommandLine += aCall; |
|
82 iCommandLine += KQuote; |
|
83 iCommandLine += KSpace; |
|
84 |
|
85 // The command line is now setup |
|
86 iIsSetup = true; |
|
87 } |
|
88 |
|
89 void CRemoteInterface::AddParam( const string &aName, const string &aValue ) |
|
90 { |
|
91 // If the command line is setup append the next parameter |
|
92 if( iIsSetup ) |
|
93 { |
|
94 iCommandLine += aName; |
|
95 iCommandLine += KEquals; |
|
96 iCommandLine += KQuote; |
|
97 iCommandLine += aValue; |
|
98 iCommandLine += KQuote; |
|
99 iCommandLine += KSpace; |
|
100 } |
|
101 } |
|
102 |
|
103 int CRemoteInterface::Send() |
|
104 { |
|
105 int ret = 0; |
|
106 |
|
107 if( !iTransport ) |
|
108 { |
|
109 iTransport = new CTCPTransport(); |
|
110 } |
|
111 |
|
112 // If the command line is setup then continue with the send |
|
113 if( iIsSetup && iTransport) |
|
114 { |
|
115 if( !iConnected ) |
|
116 { |
|
117 // Connect to the UCC |
|
118 ret = iTransport->Connect( iDestination ); |
|
119 iConnected = true; |
|
120 } |
|
121 if( !ret ) |
|
122 { |
|
123 if ( iCommandLine.size() > MAXCOMMANDLINELEN ) |
|
124 { |
|
125 ret = TRI_COMMANDLINETOOLONG; |
|
126 } |
|
127 else |
|
128 { |
|
129 TRunCommandRequest* run_req = new TRunCommandRequest(); |
|
130 if( run_req != NULL ) |
|
131 { |
|
132 memset( run_req->iCommandLine, 0x00, sizeof(run_req->iCommandLine) ); |
|
133 memcpy( run_req->iCommandLine, iCommandLine.c_str(), iCommandLine.size()); |
|
134 |
|
135 TPHeader header; |
|
136 header.iCmdID = CMD_REQ_RUNCOMMAND; |
|
137 header.iDataLen = sizeof(*run_req); |
|
138 header.iUid = (int)iRand_UID; |
|
139 |
|
140 // Send the header first |
|
141 ret = iTransport->RequestSend( (char*)&header, sizeof(header) ); |
|
142 ret?ret=TRI_ERRSENDING:ret=0; |
|
143 if( !ret ) |
|
144 { |
|
145 // Now send the rest of the data |
|
146 ret = iTransport->RequestSend( (char*)run_req, sizeof(*run_req) ); |
|
147 ret?ret=TRI_ERRSENDING:ret=0; |
|
148 } |
|
149 |
|
150 // Cleanup |
|
151 delete run_req; |
|
152 } |
|
153 else |
|
154 { |
|
155 ret = TRI_ERRINTIALISING; |
|
156 } |
|
157 } |
|
158 } |
|
159 if( !ret ) |
|
160 { |
|
161 // Retrieve the header |
|
162 TPHeader header; |
|
163 ret = iTransport->RequestReceive( (char*)&header, sizeof(header) ); |
|
164 |
|
165 if( !ret ) |
|
166 { |
|
167 TRunCommandReply run_rep; |
|
168 // Retrieve the result |
|
169 ret = iTransport->RequestReceive( (char*)&run_rep, sizeof(run_rep) ); |
|
170 ret?ret=TRI_ERRRECV:ret=run_rep.iResult; |
|
171 } |
|
172 } |
|
173 } |
|
174 |
|
175 return ret; |
|
176 } |
|
177 |
|
178 void CRemoteInterface::DefaultSetupCall( const string &aServiceName, const string &aMethodID ) |
|
179 { |
|
180 // New call started so clear out the command line buffer |
|
181 iCommandLine.erase(); |
|
182 |
|
183 // Add the runcmd |
|
184 iCommandLine += KRunCmd; |
|
185 iCommandLine += KSpace; |
|
186 |
|
187 // Add the service name |
|
188 iCommandLine += KServiceName; |
|
189 iCommandLine += KEquals; |
|
190 iCommandLine += KQuote; |
|
191 iCommandLine += aServiceName; |
|
192 iCommandLine += KQuote; |
|
193 iCommandLine += KSpace; |
|
194 |
|
195 // Append the methodid to the command line |
|
196 iCommandLine += KSpace; |
|
197 iCommandLine += KMethodID; |
|
198 iCommandLine += KEquals; |
|
199 iCommandLine += KQuote; |
|
200 iCommandLine += aMethodID; |
|
201 iCommandLine += KQuote; |
|
202 iCommandLine += KSpace; |
|
203 } |