2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file AccessoryConditionHandler.cpp
20 * @brief This is the implementation file of the AccessoryConditionHandler class.
24 #include <unique_ptr.h>
28 #include <FApp_AppManagerImpl.h>
29 #include <FApp_AppLaunchConditionHandlerBase.h>
30 #include <FBase_StringConverter.h>
31 #include "AccessoryConditionHandler.h"
32 #include "AccessoryMonitor.h"
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
40 const static char TIZEN_REQUEST[] = {"tizen.request"};
41 const static char TIZEN_RESPONSE[] = {"tizen.response"};
42 const static char TIZEN_LAUNCH_BY_CONDITION[] = {"tizen.launch_by_condition"};
43 const static char TIZEN_INITIALIZE[] = {"tizen.initialize"};
44 const static char TIZEN_SUCCESS[] = {"tizen.success"};
45 const static char TIZEN_FAILURE[] = {"tizen.failure"};
46 const static char TIZEN_PING[] = {"tizen.ping"};
48 const static char ACCESSORY_SERIAL_COMMAND_MESSAGE[] = {"Osp:Msg"};
49 const static char ACCESSORY_SERIAL_COMMAND_REQUEST[] = {"Osp:Req"};
50 const static char ACCESSORY_SERIAL_COMMAND_RESPONSE[] = {"Osp:Res"};
51 const static char ACCESSORY_SERIAL_COMMAND_HELLO[] = {"Hello"};
52 const static char ACCESSORY_SERIAL_COMMAND_OK[] = {"OK"};
53 const static char ACCESSORY_SERIAL_COMMAND_SUCCESS[] = {"Susccess"};
54 const static char ACCESSORY_SERIAL_COMMAND_FAIL[] = {"Fail"};
56 AccessoryConditionHandler::AccessoryConditionHandler()
57 :__pAccessoryMonitor(null)
60 SysLog(NID_SYS, "AccessoryConditionHandler is initialized");
62 __pAccessoryMonitor = AccessoryMonitor::GetInstance();
63 r = __pAccessoryMonitor->Construct(this);
67 SysLogException(NID_SYS, r, "Failed to initialize AccessoryMonitor");
71 r = __appConditionList.Construct();
75 SysLogException(NID_SYS, r, "Failed to initialize Condition List");
79 AccessoryConditionHandler::~AccessoryConditionHandler()
81 if (__pAccessoryMonitor != null)
83 __pAccessoryMonitor->ReleaseInstance();
88 AccessoryConditionHandler::Register(_AppLaunchCondition& operation)
90 String condition = operation.GetConditionString();
91 SysLog(NID_SYS, "Request to register accessory process, [%S][%S]", operation.GetAppId().GetPointer(), operation.GetConditionString().GetPointer());
93 SysTryReturnResult(NID_SYS, condition.StartsWith(L"Serial='", 0) == true, E_INVALID_FORMAT, "It is not compatible format.");
94 SysTryReturnResult(NID_SYS, condition.EndsWith(L"'") == true, E_INVALID_FORMAT, "It is not compatible format.");
96 condition.SubString(8, condition.GetLength() - 9, condition);
97 SysLog(NID_SYS, "Requested String is %ls", condition.GetPointer());
99 unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
100 SysTryReturnResult(NID_SYS, pEnum != null, E_SYSTEM, "Enumerator is not created.");
102 while(pEnum->MoveNext() == E_SUCCESS)
104 _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*> (pEnum->GetCurrent());
105 if(pCondition->GetAppId() == operation.GetAppId())
107 SysTryReturnResult(NID_SYS, pCondition->GetConditionString() != condition, E_OBJ_ALREADY_EXIST, "The specified condition(%ls) is already registered.", condition.GetPointer());
110 __appConditionList.Add(operation);
116 AccessoryConditionHandler::Unregister(_AppLaunchCondition& operation)
118 SysLog(NID_SYS, "Request to unregister accessory process");
120 unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
121 SysTryReturnResult(NID_SYS, pEnum != null, E_SYSTEM, "Enumerator is not created.");
123 while(pEnum->MoveNext() == E_SUCCESS)
125 _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*> (pEnum->GetCurrent());
126 if(pCondition->GetAppId() == operation.GetAppId())
128 if(pCondition->GetConditionString() == operation.GetConditionString())
130 __appConditionList.Remove(*pCondition);
135 return E_OBJ_NOT_FOUND;
139 AccessoryConditionHandler::GetValueFromCommandN(const char* command, int length)
141 if (command == null || length <= 0)
147 char* indexStart = strstr(const_cast < char* > (command), "'");
149 if (indexStart == null)
154 char* indexEnd = strstr(indexStart, "'");
156 if (indexEnd == null)
159 valueLength = indexEnd - indexStart;
161 value = new (std::nothrow) char[valueLength + 1];
162 memset(value, 0, valueLength + 1);
164 strncpy(value, indexStart, valueLength);
170 AccessoryConditionHandler::MessageCommandHandle(const char* command, int length)
172 result r = E_SUCCESS;
173 bool resultValue = true;
174 char* commandValue = GetValueFromCommandN(command, length);
175 char* response = null;
177 SysTryCatch(NID_IO, commandValue != null, resultValue = false, E_SYSTEM, "[E_SYSTEM] there is no message command");
179 SysLog(NID_IO, "Message command is [%s]", commandValue);
181 if (strcmp(commandValue, ACCESSORY_SERIAL_COMMAND_HELLO) == 0)
183 SysLog(NID_IO, "This is hello request");
185 int responseLength = strlen(ACCESSORY_SERIAL_COMMAND_MESSAGE) + strlen(ACCESSORY_SERIAL_COMMAND_OK) + 5;
186 response = new (std::nothrow) char[responseLength];
187 memset(response, 0, responseLength);
189 response = strcat(response, ACCESSORY_SERIAL_COMMAND_MESSAGE);
190 response = strcat(response, "='");
191 response = strcat(response, ACCESSORY_SERIAL_COMMAND_OK);
192 response = strcat(response, "'\r\n");
194 r = __pAccessoryMonitor->SendData(response, strlen(response));
196 SysTryCatch(NID_IO, r == E_SUCCESS, resultValue = true, E_SYSTEM, "[%s] Fail to send data");
200 SysLog(NID_IO, "[%s] is not supported command", commandValue);
204 delete [] commandValue;
211 AccessoryConditionHandler::TizenRequestCommand(const char* command, int length)
214 char request[16] = {0,};
215 char value[32] = {0, };
216 char condition[512] = {0, };
217 char* response = null;
218 char* responseMessage = null;
219 result r = E_SUCCESS;
221 SysTryCatch(NID_SYS, command != null && length > 0, ,E_SYSTEM, "Condition is not available.");
223 ret = sscanf(command, "%13s='%s %s'", request, value, condition);
227 value[strlen(value) -1 ] = 0;
231 condition[strlen(condition) -1 ] = 0;
235 responseMessage = const_cast <char*> (TIZEN_FAILURE);
238 SysLog(NID_SYS, "Request: %s, %s, %s, %d", request, value, condition, ret);
240 if(strcmp(request, TIZEN_REQUEST) == 0 && (ret == 2 || ret ==3))
242 SysLog(NID_SYS, "Command is request.");
243 if(strcmp(value, TIZEN_LAUNCH_BY_CONDITION) == 0)
245 SysLog(NID_SYS, "Command is launch request.");
247 unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
248 SysTryCatch(NID_IO, pEnum != null, , E_SYSTEM, "Enumerator is not created.");
250 while(pEnum->MoveNext() == E_SUCCESS)
252 _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*>(pEnum->GetCurrent());
253 SysTryCatch(NID_SYS, pCondition != null, ,E_SYSTEM, "Condition is not available.");
254 String registered = pCondition->GetConditionString();
255 String requested(condition);
257 registered.SubString(8, registered.GetLength() - 9, registered);
258 if(registered == requested)
260 SysLog(NID_SYS, "Application[%ls] launch is required.", pCondition->GetAppId().GetPointer());
262 responseMessage = const_cast <char*> (TIZEN_SUCCESS);
267 else if(strcmp(value, TIZEN_PING) == 0)
269 SysLog(NID_SYS, "Command is launch request.");
270 responseMessage = const_cast <char*> (TIZEN_PING);
272 else if(strcmp(value, TIZEN_INITIALIZE) == 0)
274 SysLog(NID_SYS, "Command is initialize.");
275 responseMessage = const_cast <char*> (TIZEN_SUCCESS);
280 if(responseMessage == null)
282 responseMessage = const_cast <char*> (TIZEN_FAILURE);
285 int responseLength = strlen(TIZEN_RESPONSE) + strlen(responseMessage) + 6;
286 response = new (std::nothrow) char[responseLength];
289 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to allocate memory");
293 memset(response, 0, responseLength);
294 response = strcat(response, TIZEN_RESPONSE);
295 response = strcat(response, "='");
296 response = strcat(response, responseMessage);
297 response = strcat(response, "'\r\n");
299 r = __pAccessoryMonitor->SendData(response, strlen(response)+1);
310 AccessoryConditionHandler::RequestCommandHandle(const char* command, int length)
313 result r = E_SUCCESS;
314 char* response = null;
315 char* resultValue = null;
316 char condition[512] = {0, };
317 int responseLength = 0;
319 SysTryCatch(NID_SYS, command != null && length > 0, ,E_SYSTEM, "Condition is not available.");
321 ret = sscanf(command, "Osp:Req='%s'", condition);
322 SysTryCatch(NID_SYS, ret == 1, ,E_SYSTEM, "Condition is not available.(%d, %s/%s)", ret, condition, command);
324 condition[strlen(condition) -1 ] = 0;
328 unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
329 SysTryCatch(NID_IO, pEnum != null, , E_SYSTEM, "Enumerator is not created.");
331 while(pEnum->MoveNext() == E_SUCCESS)
333 _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*>(pEnum->GetCurrent());
334 SysTryCatch(NID_SYS, pCondition != null, ,E_SYSTEM, "Condition is not available.");
335 String registered = pCondition->GetConditionString();
336 String requested(condition);
338 registered.SubString(8, registered.GetLength() - 9, registered);
339 if(registered == requested)
341 SysLog(NID_SYS, "Application[%ls] launch is required.", pCondition->GetAppId().GetPointer());
343 resultValue = const_cast<char*>(ACCESSORY_SERIAL_COMMAND_SUCCESS);
350 if(resultValue == null)
352 resultValue = const_cast<char*>(ACCESSORY_SERIAL_COMMAND_FAIL);
355 responseLength = strlen(ACCESSORY_SERIAL_COMMAND_RESPONSE) + strlen(resultValue) + 6;
356 response = new (std::nothrow) char[responseLength];
359 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to allocate memory");
363 memset(response, 0, responseLength);
364 response = strcat(response, ACCESSORY_SERIAL_COMMAND_RESPONSE);
365 response = strcat(response, "='");
366 response = strcat(response, resultValue);
367 response = strcat(response, "'\r\n");
369 r = __pAccessoryMonitor->SendData(response, strlen(response)+1);
381 AccessoryConditionHandler::ProcessPrimitiveCommand(const char* command, int length)
383 bool resultValue = false;
385 SysLog(NID_IO, "serial data is forwarded to ProcessPrimitiveCommand [%s]", command);
386 SysTryCatch(NID_IO, command != null && length > 0, resultValue = false, E_INVALID_ARG, "[E_INVALID_ARG] There is no available data");
388 if (strncmp(command, ACCESSORY_SERIAL_COMMAND_MESSAGE, strlen(ACCESSORY_SERIAL_COMMAND_MESSAGE)) == 0)
390 resultValue = MessageCommandHandle(command, length);
391 SysLog(NID_SYS, "Message result: %d", resultValue);
393 else if (strncmp(command, ACCESSORY_SERIAL_COMMAND_REQUEST, strlen(ACCESSORY_SERIAL_COMMAND_REQUEST)) == 0)
395 resultValue = RequestCommandHandle(command, length);
396 SysLog(NID_SYS, "Request result: %d", resultValue);
398 else if (strncmp(command, TIZEN_REQUEST, strlen(TIZEN_REQUEST)) == 0
399 || strncmp(command, TIZEN_PING, strlen(TIZEN_PING)) == 0)
401 resultValue = TizenRequestCommand(command, length);
402 SysLog(NID_SYS, "Request result: %d", resultValue);
406 SysLog(NID_SYS, "result: %d", resultValue);
411 /////////////////////////////////////////////////////////////////////////
412 // All plugins must provide both a creation and a destruction function
413 /////////////////////////////////////////////////////////////////////////
416 _OSP_EXPORT_ _AppLaunchConditionHandlerBase*
419 SysLog(NID_IO, "CreatePlugin is called");
420 return new (std::nothrow) AccessoryConditionHandler;
424 DestroyPlugin(_AppLaunchConditionHandlerBase* p)