Update accessory-condition-handler
authorJoohyun Kim <joohyune.kim@samsung.com>
Wed, 23 Jan 2013 11:35:28 +0000 (20:35 +0900)
committerJoohyun Kim <joohyune.kim@samsung.com>
Wed, 23 Jan 2013 11:35:28 +0000 (20:35 +0900)
Change-Id: If39ffca88f022cc373301ff74464b68b6bf1dbc2
Signed-off-by: Joohyun Kim <joohyune.kim@samsung.com>
plugins/accessory-condition-handler/AccessoryConditionHandler.cpp
plugins/accessory-condition-handler/AccessoryConditionHandler.h

index 54d3be9..b128048 100644 (file)
@@ -21,6 +21,7 @@
  *
  */
 
+#include <unique_ptr.h>
 #include <new>
 #include <string.h>
 
@@ -30,6 +31,8 @@
 #include "AccessoryConditionHandler.h"
 #include "AccessoryMonitor.h"
 
+using namespace std;
+
 using namespace Tizen::App;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
@@ -37,6 +40,7 @@ using namespace Tizen::Base::Collection;
 const static char TIZEN_REQUEST[] = {"tizen.request"};
 const static char TIZEN_RESPONSE[] = {"tizen.response"};
 const static char TIZEN_LAUNCH_BY_CONDITION[] = {"tizen.launch_by_condition"};
+const static char TIZEN_INITIALIZE[] = {"tizen.initialize"};
 const static char TIZEN_SUCCESS[] = {"tizen.success"};
 const static char TIZEN_FAILURE[] = {"tizen.failure"};
 const static char TIZEN_PING[] = {"tizen.ping"};
@@ -83,14 +87,27 @@ AccessoryConditionHandler::~AccessoryConditionHandler()
 result
 AccessoryConditionHandler::Register(_AppLaunchCondition& operation)
 {
+       String condition = operation.GetConditionString();
        SysLog(NID_SYS, "Request to register accessory process, [%S][%S]", operation.GetAppId().GetPointer(), operation.GetConditionString().GetPointer());
 
-       AppId* pAppId = new (std::nothrow) AppId(operation.GetAppId());
-       String* pCondition = new (std::nothrow) String(operation.GetConditionString());
+       SysTryReturnResult(NID_SYS, condition.StartsWith(L"Serial='", 0) == true, E_INVALID_FORMAT, "It is not compatible format.");
+       SysTryReturnResult(NID_SYS, condition.EndsWith(L"'") == true, E_INVALID_FORMAT, "It is not compatible format.");
+
+       condition.SubString(8, condition.GetLength() - 9, condition);
+       SysLog(NID_SYS, "Requested String is %ls", condition.GetPointer());
 
-       __appConditionList.Remove(operation.GetAppId(), true);
-       __appConditionList.Add(*pAppId, *pCondition);
-       //__appConditionList.Add(operation.GetAppId(), operation.GetConditionString());
+       unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
+       SysTryReturnResult(NID_SYS, pEnum != null, E_SYSTEM, "Enumerator is not created.");
+
+       while(pEnum->MoveNext() == E_SUCCESS)
+       {
+               _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*> (pEnum->GetCurrent());
+               if(pCondition->GetAppId() == operation.GetAppId())
+               {
+                       SysTryReturnResult(NID_SYS, pCondition->GetConditionString() != condition, E_OBJ_ALREADY_EXIST, "The specified condition(%ls) is already registered.", condition.GetPointer());
+               }
+       }
+       __appConditionList.Add(operation);
 
        return E_SUCCESS;
 }
@@ -99,8 +116,23 @@ result
 AccessoryConditionHandler::Unregister(_AppLaunchCondition& operation)
 {
        SysLog(NID_SYS, "Request to unregister accessory process");
-       __appConditionList.Remove(operation.GetAppId(), true);
-       return E_SUCCESS;
+
+       unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
+       SysTryReturnResult(NID_SYS, pEnum != null, E_SYSTEM, "Enumerator is not created.");
+
+       while(pEnum->MoveNext() == E_SUCCESS)
+       {
+               _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*> (pEnum->GetCurrent());
+               if(pCondition->GetAppId() == operation.GetAppId())
+               {
+                       if(pCondition->GetConditionString() == operation.GetConditionString())
+                       {
+                               __appConditionList.Remove(*pCondition);
+                               return E_SUCCESS;
+                       }
+               }
+       }
+       return E_OBJ_NOT_FOUND;
 }
 
 char*
@@ -178,224 +210,171 @@ CATCH:
 bool
 AccessoryConditionHandler::TizenRequestCommand(const char* command, int length)
 {
+       int ret = 0;
        char request[16] = {0,};
        char value[32] = {0, };
        char condition[512] = {0, };
        char* response = null;
-       bool executed = false;
-       char* registered = null;
-       char* executionCondition = null;
-       IMapEnumerator* pMapEnum = null;
-
+       char* responseMessage = null;
        result r = E_SUCCESS;
 
-       sscanf(command, "%13s='%s %s'", request, value, condition);
+       SysTryCatch(NID_SYS, command != null && length > 0, ,E_SYSTEM, "Condition is not available.");
 
-       if(strcmp(request, TIZEN_REQUEST) == 0)
+       ret = sscanf(command, "%13s='%s %s'", request, value, condition);
+       
+       if(ret == 2)
+       {
+               value[strlen(value) -1 ] = 0;
+       }
+       else if(ret == 3)
+       {
+               condition[strlen(condition) -1 ] = 0;
+       }
+       else
        {
+               responseMessage = const_cast <char*> (TIZEN_FAILURE);
+       }
+
+       SysLog(NID_SYS, "Request: %s, %s, %s, %d", request, value, condition, ret);
+
+       if(strcmp(request, TIZEN_REQUEST) == 0 && (ret == 2 || ret ==3))
+       {
+               SysLog(NID_SYS, "Command is request.");
                if(strcmp(value, TIZEN_LAUNCH_BY_CONDITION) == 0)
                {
-                       AppId* pAppId = NULL;
-                       String* pCondition = NULL;
-       
-                       pMapEnum = __appConditionList.GetMapEnumeratorN();
+                       SysLog(NID_SYS, "Command is launch request.");
 
-                       while(pMapEnum->MoveNext() == E_SUCCESS)
-                       {
-                               pAppId = static_cast< AppId* >(pMapEnum->GetKey());
-                               pCondition = static_cast< String* >(pMapEnum->GetValue());
-
-                               registered = _StringConverter::CopyToCharArrayN(*pCondition);
+                       unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
+                       SysTryCatch(NID_IO, pEnum != null, , E_SYSTEM, "Enumerator is not created.");
 
-                               executionCondition = GetValueFromCommandN(registered, strlen(registered));
+                       while(pEnum->MoveNext() == E_SUCCESS)
+                       {
+                               _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*>(pEnum->GetCurrent());
+                               SysTryCatch(NID_SYS, pCondition != null, ,E_SYSTEM, "Condition is not available.");
+                               String registered = pCondition->GetConditionString();
+                               String requested(condition);
 
-                               if (strcmp(condition, executionCondition) == 0)
+                               registered.SubString(8, registered.GetLength() - 9, registered);
+                               if(registered == requested)
                                {
-                                       //Launch applicaiton.
-                                       _AppManagerImpl* pAppManagerImpl = _AppManagerImpl::GetInstance();
-
-                                       if(pAppManagerImpl == null)
-                                       {
-                                               SysLog(NID_IO, "Not found AppManager instance");
-                                               executed = false;
-                                               break;
-                                       }
-                                       r = pAppManagerImpl->LaunchApplication(*pAppId, null, AppManager::LAUNCH_OPTION_DEFAULT);
-                                       SysLog(NID_IO, "Application[%S] launch is requested.", pAppId->GetPointer());
-
-                                       if (r != E_SUCCESS)
-                                       {
-                                               executed = false;
-                                       }
-                                       else
-                                       {
-                                               executed = true;
-                                       }
+                                       SysLog(NID_SYS, "Application[%ls] launch is required.", pCondition->GetAppId().GetPointer());
+                                       Fire(*pCondition);
+                                       responseMessage = const_cast <char*> (TIZEN_SUCCESS);
                                        break;
                                }
-
-                               delete [] executionCondition;
-                               delete [] registered;
-                               executionCondition = null;
-                               registered = null;
                        }
-
+               }
+               else if(strcmp(value, TIZEN_PING) == 0)
+               {
+                       SysLog(NID_SYS, "Command is launch request.");
+                       responseMessage = const_cast <char*> (TIZEN_PING);
+               }
+               else if(strcmp(value, TIZEN_INITIALIZE) == 0)
+               {
+                       SysLog(NID_SYS, "Command is initialize.");
+                       responseMessage = const_cast <char*> (TIZEN_SUCCESS);
                }
        }
-       else if(strcmp(request, TIZEN_PING) == 0)
+
+CATCH:
+       if(responseMessage == null)
        {
-               executed = true;
+               responseMessage = const_cast <char*> (TIZEN_FAILURE);
        }
 
-       if (executed == true)
+       int responseLength = strlen(TIZEN_RESPONSE) + strlen(responseMessage) + 6;
+       response = new (std::nothrow) char[responseLength];
+       if(response == null)
        {
-
-               int responseLength = strlen(TIZEN_RESPONSE) + strlen(TIZEN_SUCCESS) + 6;
-               response = new (std::nothrow) char[responseLength];
-               memset(response, 0, responseLength);
-
-               response = strcat(response, TIZEN_RESPONSE);
-               response = strcat(response, "='");
-               response = strcat(response, TIZEN_SUCCESS);
-               response = strcat(response, "'\r\n");
+               SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to allocate memory");
+               return false;
        }
-       else
-       {
-               int responseLength = strlen(TIZEN_RESPONSE) + strlen(TIZEN_FAILURE) + 6;
-               response = new (std::nothrow) char[responseLength];
-               memset(response, 0, responseLength);
 
-               response = strcat(response, TIZEN_RESPONSE);
-               response = strcat(response, "='");
-               response = strcat(response, TIZEN_FAILURE);
-               response = strcat(response, "'\r\n");
-       }
+       memset(response, 0, responseLength);
+       response = strcat(response, TIZEN_RESPONSE);
+       response = strcat(response, "='");
+       response = strcat(response, responseMessage);
+       response = strcat(response, "'\r\n");
 
-       SysTryCatch(NID_IO, response != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failure.");
        r = __pAccessoryMonitor->SendData(response, strlen(response)+1);
-       SysTryCatch(NID_IO, r == E_SUCCESS, executed = false, E_SYSTEM, "[%s] Fail to send data");
-
-CATCH:
-       delete [] registered;
-       delete [] executionCondition;
        delete [] response;
-       delete pMapEnum;
 
-       return executed;
+       if(r != E_SUCCESS)
+       {
+               return false;
+       }
+
+       return true;
 }
 bool
 AccessoryConditionHandler::RequestCommandHandle(const char* command, int length)
 {
+       int ret = 0;
        result r = E_SUCCESS;
-       bool resultValue = true;
-       bool executed = false;
-       char* commandValue = GetValueFromCommandN(command, length);
-       char* registered = null;
-       char* executionCondition = null;
        char* response = null;
+       char* resultValue = null;
+       char condition[512] = {0, };
+       int responseLength = 0;
 
-       IMapEnumerator* pMapEnum = null;
-
-       AppId* pAppId = null;
-       String* pCondition = null;
-
-       SysTryCatch(NID_IO, commandValue != null, resultValue = false, E_SYSTEM, "[E_SYSTEM] there is no message command");
+       SysTryCatch(NID_SYS, command != null && length > 0, ,E_SYSTEM, "Condition is not available.");
 
-       pMapEnum = __appConditionList.GetMapEnumeratorN();
+       ret = sscanf(command, "Osp:Req='%s'", condition);
+       SysTryCatch(NID_SYS, ret == 1, ,E_SYSTEM, "Condition is not available.(%d, %s/%s)", ret,  condition, command);
 
-       SysTryCatch(NID_IO, pMapEnum != null, resultValue = false, E_SYSTEM, "[E_SYSTEM] There is wrong app list");
+       condition[strlen(condition) -1 ] = 0;
 
-       //if conditional popup is required, it can be implemented here.
-       while(pMapEnum->MoveNext() == E_SUCCESS)
+       if(ret == 1)
        {
-               pAppId = static_cast< AppId* >(pMapEnum->GetKey());
-               pCondition = static_cast< String* >(pMapEnum->GetValue());
-
-               registered = _StringConverter::CopyToCharArrayN(*pCondition);
+               unique_ptr<IEnumerator> pEnum (__appConditionList.GetEnumeratorN());
+               SysTryCatch(NID_IO, pEnum != null, , E_SYSTEM, "Enumerator is not created.");
 
-               executionCondition = GetValueFromCommandN(registered, strlen(registered));
-
-               if (strcmp(commandValue, executionCondition) == 0)
+               while(pEnum->MoveNext() == E_SUCCESS)
                {
-                       //Launch applicaiton.
-                       _AppManagerImpl* pAppManagerImpl = _AppManagerImpl::GetInstance();
+                       _AppLaunchCondition* pCondition = static_cast<_AppLaunchCondition*>(pEnum->GetCurrent());
+                       SysTryCatch(NID_SYS, pCondition != null, ,E_SYSTEM, "Condition is not available.");
+                       String registered = pCondition->GetConditionString();
+                       String requested(condition);
 
-                       if(pAppManagerImpl == null)
+                       registered.SubString(8, registered.GetLength() - 9, registered);
+                       if(registered == requested)
                        {
-                               SysLog(NID_IO, "Not found AppManager instance");
-                               executed = false;
+                               SysLog(NID_SYS, "Application[%ls] launch is required.", pCondition->GetAppId().GetPointer());
+                               Fire(*pCondition);
+                               resultValue = const_cast<char*>(ACCESSORY_SERIAL_COMMAND_SUCCESS);
                                break;
                        }
-                       r = pAppManagerImpl->LaunchApplication(*pAppId, null, AppManager::LAUNCH_OPTION_DEFAULT);
-                       SysLog(NID_IO, "Application[%S] launch is requested.", pAppId->GetPointer());
-
-                       if (r != E_SUCCESS)
-                       {
-                               executed = false;
-                       }
-                       else
-                       {
-                               executed = true;
-                       }
-                       break;
                }
-
-               delete [] executionCondition;
-               delete [] registered;
-               executionCondition = null;
-               registered = null;
        }
 
-       if (executed == true)
+CATCH:
+       if(resultValue == null)
        {
-
-               int responseLength = strlen(ACCESSORY_SERIAL_COMMAND_RESPONSE) + strlen(ACCESSORY_SERIAL_COMMAND_SUCCESS) + 6;
-               response = new (std::nothrow) char[responseLength];
-               memset(response, 0, responseLength);
-
-               response = strcat(response, ACCESSORY_SERIAL_COMMAND_RESPONSE);
-               response = strcat(response, "='");
-               response = strcat(response, ACCESSORY_SERIAL_COMMAND_SUCCESS);
-               response = strcat(response, "'\r\n");
+               resultValue = const_cast<char*>(ACCESSORY_SERIAL_COMMAND_FAIL);
        }
-       else
-       {
-               int responseLength = strlen(ACCESSORY_SERIAL_COMMAND_RESPONSE) + strlen(ACCESSORY_SERIAL_COMMAND_FAIL) + 6;
-               response = new (std::nothrow) char[responseLength];
-               memset(response, 0, responseLength);
 
-               response = strcat(response, ACCESSORY_SERIAL_COMMAND_RESPONSE);
-               response = strcat(response, "='");
-               response = strcat(response, ACCESSORY_SERIAL_COMMAND_FAIL);
-               response = strcat(response, "'\r\n");
+       responseLength = strlen(ACCESSORY_SERIAL_COMMAND_RESPONSE) + strlen(resultValue) + 6;
+       response = new (std::nothrow) char[responseLength];
+       if(response == null)
+       {
+               SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to allocate memory");
+               return false;
        }
 
+       memset(response, 0, responseLength);
+       response = strcat(response, ACCESSORY_SERIAL_COMMAND_RESPONSE);
+       response = strcat(response, "='");
+       response = strcat(response, resultValue);
+       response = strcat(response, "'\r\n");
+
        r = __pAccessoryMonitor->SendData(response, strlen(response)+1);
-       SysTryCatch(NID_IO, r == E_SUCCESS, resultValue = true, E_SYSTEM, "[%s] Fail to send data");
+       delete [] response;
 
-CATCH:
-       if(commandValue != null)
-       {
-               delete [] commandValue;
-       }
-       if(registered != null)
-       {
-               delete [] registered;
-       }
-       if(executionCondition != null)
-       {
-               delete [] executionCondition;
-       }
-       if(response != null)
+       if(r != E_SUCCESS)
        {
-               delete [] response;
-       }
-       if(pMapEnum != null)
-       {
-               delete pMapEnum;
+               return false;
        }
 
-       return resultValue;
+       return true;
 }
 
 bool
index fed9d90..ee8f8cd 100644 (file)
@@ -45,7 +45,7 @@ private:
        bool RequestCommandHandle(const char* command, int length);
        bool TizenRequestCommand(const char* command, int length);
        AccessoryMonitor* __pAccessoryMonitor;
-       Tizen::Base::Collection::HashMap __appConditionList;
+       Tizen::Base::Collection::ArrayList __appConditionList;
 };
 
 #endif //_ACCESSORY_CONDITION_HANDLER_H_