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://www.apache.org/licenses/LICENSE-2.0
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 #include <sync_agent.h>
22 #include "common/dm_common.h"
23 #include "serviceadapter/sa_command.h"
24 #include "serviceadapter/sa_command_internal.h"
25 #include "serviceadapter/sa_elements.h"
26 #include "serviceadapter/sa_elements_internal.h"
27 #include "serviceadapter/sa_session.h"
28 #include "serviceadapter/sa_session_internal.h"
30 #ifndef OMADM_AGENT_LOG
32 #define LOG_TAG "OMA_DM_SA"
35 static Command *_create_Cmd(Session * pSession, CommandType type);
37 static Command *_create_Cmd(Session * pSession, CommandType type)
43 retvm_if((pSession) == NULL, NULL, "pSession is NULL!!");
44 retvm_if((type) == COMMAND_TYPE_UNKNOWN, NULL, "type is NULL!!");
46 _DEBUG_TRACE(" start type : %d msgID : %d cmdID : %d\n", type, pSession->msgID, pSession->cmdID);
48 Command *cmd = (Command *) calloc(1, sizeof(Command));
50 ret = COMMON_ERR_ALLOC;
55 cmd->msgID = pSession->msgID;
56 cmd->cmdID = pSession->cmdID;
59 PendingStatus *temp = NULL;
60 ret = create_prependingstatus(pSession->msgID, pSession->cmdID, &temp);
64 pSession->pendingStatus = g_list_append(pSession->pendingStatus, temp);
66 _DEBUG_INFO("session cmdId : %d", pSession->cmdID);
68 _DEBUG_TRACE(" end\n");
74 _DEBUG_TRACE(" error : %d\n", ret);
80 DM_ERROR duplicate_command(Command * source, Command ** target)
84 retvm_if((source) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
87 CommandType type = source->type;
90 case COMMAND_TYPE_RESULTS:
92 _DEBUG_INFO("dup result command");
93 *target = (Command *) calloc(1, sizeof(Command));
94 if (*target == NULL) {
95 ret = COMMON_ERR_ALLOC;
99 (*target)->type = type;
100 (*target)->msgID = source->msgID;
101 (*target)->cmdID = source->cmdID;
102 (*target)->refCount = 1;
103 if (source->source != NULL) {
104 _DEBUG_INFO("source location");
105 (*target)->source = dup_location(source->source);
107 if (source->target) {
108 _DEBUG_INFO("taget location");
109 (*target)->target = dup_location(source->target);
112 _DEBUG_INFO("result data cmdRef : %d, msgRef : %d", source->private.results.cmdRef, source->private.results.msgRef);
113 (*target)->private.results.cmdRef = source->private.results.cmdRef;
114 (*target)->private.results.msgRef = source->private.results.msgRef;
116 if (source->private.results.targetRef) {
117 _DEBUG_INFO("target ref");
118 (*target)->private.results.targetRef = dup_location(source->private.results.targetRef);
120 if (source->private.results.type) {
122 (*target)->private.results.type = strdup(source->private.results.type);
124 if (source->private.results.items) {
125 _DEBUG_INFO("result item list");
126 GList *iter_list = NULL;
129 for (iter_list = source->private.results.items; iter_list != NULL; iter_list = g_list_next(iter_list)) {
130 _DEBUG_INFO("result item");
131 temp = (Item *) iter_list->data;
132 Item *dup_item = NULL;
133 if (temp->source != NULL) {
134 if (temp->source->locURI != NULL) {
135 _DEBUG_INFO("loc uri : %s", temp->source->locURI);
137 ret = construct_Item(temp->source->locURI, temp->format, temp->contenttype, temp->private.data, temp->size, temp->moreData, &dup_item);
138 _DEBUG_INFO("construct item : %d", ret);
140 (*target)->private.results.items = g_list_append((*target)->private.results.items, dup_item);
142 _DEBUG_INFO("source is null");
148 case COMMAND_TYPE_ALERT:
149 case COMMAND_TYPE_HEADER:
150 case COMMAND_TYPE_ADD:
151 case COMMAND_TYPE_REPLACE:
152 case COMMAND_TYPE_DELETE:
153 case COMMAND_TYPE_GET:
154 case COMMAND_TYPE_EXEC:
155 case COMMAND_TYPE_SEQUENCE:
156 case COMMAND_TYPE_ATOMIC:
157 case COMMAND_TYPE_COPY:
158 case COMMAND_TYPE_UNKNOWN:
171 void free_commands(GList * commands)
175 if (commands == NULL) {
181 Command *pCommand = NULL;
182 for (iter = commands; iter != NULL;) {
183 pCommand = iter->data;
184 _DEBUG_INFO("command type is %d\n", pCommand->type);
186 iter = g_list_next(iter);
187 commands = g_list_remove(commands, pCommand);
188 if (pCommand->type != COMMAND_TYPE_UNKNOWN) {
189 free_command(pCommand);
190 _DEBUG_INFO("free command");
193 _DEBUG_INFO("free command list");
194 g_list_free(commands);
200 DM_ERROR set_correlator(Command ** pAlertCommand, char *pCorrelator)
204 DM_ERROR ret = DM_OK;
206 retvm_if((*pAlertCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pAlertCommand is NULL!!");
207 retvm_if((pCorrelator) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCorrelator is NULL!!");
209 (*pAlertCommand)->private.alert.Correlator = g_strdup(pCorrelator);
216 DM_ERROR create_alert_cmd(Session * session, AlertType syncType, Command ** pCommand)
220 DM_ERROR ret = DM_OK;
222 *pCommand = _create_Cmd(session, COMMAND_TYPE_ALERT);
223 if ((*pCommand) == NULL) {
224 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
228 (*pCommand)->private.alert.type = syncType;
234 _DEBUG_INFO(" error : %d\n", ret);
239 DM_ERROR create_replace_cmd(Session * session, Command ** pCommand)
243 DM_ERROR ret = DM_OK;
245 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
247 (*pCommand) = _create_Cmd(session, COMMAND_TYPE_REPLACE);
248 if ((*pCommand) == NULL) {
249 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
253 (*pCommand)->private.change.type = CHANGE_REPLACE;
258 _DEBUG_INFO(" error : %d\n", ret);
263 DM_ERROR create_results_cmd(Session * session, Command ** pCommand)
267 DM_ERROR ret = DM_OK;
269 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
271 *pCommand = _create_Cmd(session, COMMAND_TYPE_RESULTS);
272 if ((*pCommand) == NULL) {
273 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
280 _DEBUG_INFO(" error : %d\n", ret);
285 DM_ERROR create_add_cmd(Session * session, char *luid, const char *contenttype, char *data, unsigned int size, int moreData, Command ** pCommand)
289 DM_ERROR ret = DM_OK;
291 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
292 retvm_if((luid) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "luid is NULL!!");
293 retvm_if((contenttype) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "contenttype is NULL!!");
296 Location *pLocation = NULL;
298 *pCommand = _create_Cmd(session, COMMAND_TYPE_ADD);
299 if (*pCommand == NULL) {
300 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
304 ret = create_item_data(data, size, &temp);
306 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
310 (*pCommand)->private.change.type = CHANGE_ADD;
311 temp->contenttype = g_strdup(contenttype);
312 temp->moreData = moreData;
314 ret = create_location(luid, NULL, &pLocation);
318 set_itemsource(temp, pLocation);
319 (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
326 _DEBUG_INFO(" error : %d\n", ret);
331 DM_ERROR create_delete_cmd(Session * session, char *luid, const char *contenttype, Command ** pCommand)
335 DM_ERROR ret = DM_OK;
337 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
338 retvm_if((luid) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "luid is NULL!!");
339 retvm_if((contenttype) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "contenttype is NULL!!");
342 Location *pLocation = NULL;
344 *pCommand = _create_Cmd(session, COMMAND_TYPE_DELETE);
345 if (*pCommand == NULL) {
346 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
350 temp = create_Item();
352 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
356 (*pCommand)->private.change.type = CHANGE_DELETE;
358 ret = create_location(luid, NULL, &pLocation);
362 set_itemsource(temp, pLocation);
363 temp->contenttype = g_strdup(contenttype);
364 (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
371 _DEBUG_INFO(" error : %d\n", ret);
377 void free_command(Command * pCommand)
380 if (pCommand == NULL) {
386 _DEBUG_INFO(" Command type is %d", pCommand->type);
388 if (pCommand->refCount > 1) {
389 _DEBUG_INFO("Command's refCount is %d\n", pCommand->refCount);
390 /*decrease_command_refcount(pCommand); */
395 switch (pCommand->type) {
396 case COMMAND_TYPE_ALERT:
397 _DEBUG_INFO("alert type : %d", pCommand->private.alert.type);
398 if ((pCommand->private.alert.type >= DM_ALERT_DISPLAY && pCommand->private.alert.type <= DM_ALERT_MULTIPLE_CHOICE)
399 && pCommand->private.alert.items != NULL) {
400 for (iter = pCommand->private.alert.items; iter != NULL; iter = g_list_next(iter)) {
401 free_Item((Item *) iter->data);
404 _DEBUG_INFO("alert correlator");
405 if (pCommand->private.alert.Correlator != NULL) {
406 free(pCommand->private.alert.Correlator);
407 pCommand->private.alert.Correlator = NULL;
410 case COMMAND_TYPE_HEADER:
411 _DEBUG_INFO("header type");
412 //COMMAND_TYPE_HEADER doesnot come here
414 case COMMAND_TYPE_ADD:
415 case COMMAND_TYPE_REPLACE:
416 case COMMAND_TYPE_DELETE:
417 case COMMAND_TYPE_COPY:
418 _DEBUG_INFO("add repalce delete type");
419 if (pCommand->private.change.items != NULL) {
420 for (iter = pCommand->private.change.items; iter != NULL; iter = g_list_next(iter)) {
421 free_Item(iter->data);
425 case COMMAND_TYPE_GET:
426 _DEBUG_INFO("get type");
427 if (pCommand->private.access.type != NULL) {
428 free(pCommand->private.access.type);
429 pCommand->private.access.type = NULL;
432 if (pCommand->private.access.items != NULL) {
433 for (iter = pCommand->private.access.items; iter != NULL; iter = g_list_next(iter)) {
434 free_Item(iter->data);
438 case COMMAND_TYPE_RESULTS:
439 _DEBUG_INFO("result type");
440 if (pCommand->private.results.type != NULL) {
441 free(pCommand->private.results.type);
442 pCommand->private.results.type = NULL;
444 if (pCommand->private.results.items != NULL) {
445 for (iter = pCommand->private.results.items; iter != NULL; iter = g_list_next(iter)) {
446 free_Item(iter->data);
451 case COMMAND_TYPE_EXEC:
452 _DEBUG_INFO("exec type");
453 if (pCommand->private.exec.correlator != NULL) {
454 free(pCommand->private.exec.correlator);
455 pCommand->private.exec.correlator = NULL;
457 if (pCommand->private.exec.item != NULL) {
458 free_Item(pCommand->private.exec.item);
461 case COMMAND_TYPE_UNKNOWN:
462 _DEBUG_INFO("unknown type");
464 case COMMAND_TYPE_SEQUENCE:
465 case COMMAND_TYPE_ATOMIC:
466 _DEBUG_INFO("sequence atomic copy type");
467 if (pCommand->private.sequence_atomic.commands != NULL) {
468 for (iter = pCommand->private.sequence_atomic.commands; iter != NULL; iter = g_list_next(iter)) {
469 free_command(iter->data);
476 _DEBUG_INFO("command source");
477 if (pCommand->source != NULL) {
478 free_location(pCommand->source);
479 pCommand->source = NULL;
482 _DEBUG_INFO("command target");
483 if (pCommand->target != NULL) {
484 free_location(pCommand->target);
485 pCommand->target = NULL;
488 _DEBUG_INFO("free command ");
492 _DEBUG_INFO("free command end");
496 void free_statuses(GList * status)
501 _DEBUG_INFO(" List is null\n");
508 _DEBUG_INFO(" count : %d\n", g_list_length(status));
509 for (iter = status; iter != NULL; iter = g_list_next(iter)) {
510 _DEBUG_INFO(" count : %d\n", ++i);
511 free_status(iter->data);
519 void free_status(Status * pStatus)
523 if (pStatus == NULL) {
528 if (pStatus->data != NULL)
531 if (pStatus->sourceRef != NULL)
532 free_location(pStatus->sourceRef);
534 if (pStatus->targetRef != NULL)
535 free_location(pStatus->targetRef);
537 if(pStatus->res_chal)
538 free_chal(pStatus->res_chal);
541 free_cred(pStatus->cred);*/
543 //free_Item(pStatus->item);
545 for (iter = pStatus->items; iter != NULL; iter = g_list_next(iter))
546 free_Item(iter->data);
548 if (pStatus != NULL) {
556 DM_ERROR get_statuscode(Status * status)
560 retvm_if((status) == NULL, DM_ERR_FORBIDDEN, "status is NULL!!");
563 return atoi(status->data);
566 ChangeType converttochangetype(unsigned int type)
570 ChangeType changeType = CHANGE_UNKNOWN;
573 changeType = CHANGE_ADD;
576 changeType = CHANGE_REPLACE;
579 changeType = CHANGE_DELETE;
582 changeType = CHANGE_COPY;
590 unsigned int convertfromchangetype(ChangeType changeType)
594 unsigned int type = 0;
596 switch (changeType) {
619 CommandType converttocommandtype(char *type)
623 retvm_if((type) == NULL, COMMAND_TYPE_UNKNOWN, "type is NULL!!");
625 if (strcmp(type, "Alert") == 0) {
627 return COMMAND_TYPE_ALERT;
628 } else if (strcmp(type, "SyncHdr") == 0) {
630 return COMMAND_TYPE_HEADER;
631 } else if (strcmp(type, "Add") == 0) {
633 return COMMAND_TYPE_ADD;
634 } else if (strcmp(type, "Replace") == 0) {
636 return COMMAND_TYPE_REPLACE;
637 } else if (strcmp(type, "Delete") == 0) {
639 return COMMAND_TYPE_DELETE;
640 } else if (strcmp(type, "Results") == 0) {
642 return COMMAND_TYPE_RESULTS;
643 } else if (strcmp(type, "Get") == 0) {
645 return COMMAND_TYPE_GET;
646 } else if (strcmp(type, "Exec") == 0) {
648 return COMMAND_TYPE_EXEC;
649 } else if (strcmp(type, "Sequence") == 0) {
651 return COMMAND_TYPE_SEQUENCE;
652 } else if (strcmp(type, "Atomic") == 0) {
654 return COMMAND_TYPE_ATOMIC;
655 } else if (strcmp(type, "Copy") == 0) {
657 return COMMAND_TYPE_COPY;
660 return COMMAND_TYPE_UNKNOWN;
663 char *convertfromcommandtype(CommandType type)
667 char *commandType = NULL;
670 case COMMAND_TYPE_ALERT:
671 commandType = "Alert";
673 case COMMAND_TYPE_HEADER:
674 commandType = "SyncHdr";
676 case COMMAND_TYPE_ADD:
679 case COMMAND_TYPE_REPLACE:
680 commandType = "Replace";
682 case COMMAND_TYPE_DELETE:
683 commandType = "Delete";
685 case COMMAND_TYPE_RESULTS:
686 commandType = "Results";
688 case COMMAND_TYPE_GET:
691 case COMMAND_TYPE_EXEC:
692 commandType = "Exec";
694 case COMMAND_TYPE_ATOMIC:
695 commandType = "Atomic";
697 case COMMAND_TYPE_SEQUENCE:
698 commandType = "Sequence";
700 case COMMAND_TYPE_COPY:
701 commandType = "Copy";
704 commandType = "UNKNOWN";
710 ChangeType convertToChangeTypeFromCommandType(CommandType type)
714 ChangeType changeType = CHANGE_UNKNOWN;
717 case COMMAND_TYPE_ADD:
718 changeType = CHANGE_ADD;
720 case COMMAND_TYPE_REPLACE:
721 changeType = CHANGE_REPLACE;
723 case COMMAND_TYPE_DELETE:
724 changeType = CHANGE_DELETE;
726 case COMMAND_TYPE_COPY:
727 changeType = CHANGE_COPY;
729 case COMMAND_TYPE_UNKNOWN:
730 case COMMAND_TYPE_ALERT:
731 case COMMAND_TYPE_HEADER:
732 case COMMAND_TYPE_GET:
733 case COMMAND_TYPE_RESULTS:
734 case COMMAND_TYPE_EXEC:
735 case COMMAND_TYPE_SEQUENCE:
736 case COMMAND_TYPE_ATOMIC:
737 _DEBUG_INFO("not changetype command");
746 CommandType convertToCommandTypeFromChangeType(ChangeType type)
750 CommandType commandType = COMMAND_TYPE_UNKNOWN;
754 commandType = COMMAND_TYPE_UNKNOWN;
757 commandType = COMMAND_TYPE_ADD;
760 commandType = COMMAND_TYPE_REPLACE;
763 commandType = COMMAND_TYPE_DELETE;
766 commandType = COMMAND_TYPE_COPY;
775 DM_ERROR create_newstatuslocation(Session * session, DM_ERROR data, Command * command, Location * sourceref, Location * targetref, CommandType type, Status ** pStatus)
780 DM_ERROR ret = DM_OK;
782 retvm_if((session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
783 retvm_if((command) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
785 ret = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID, sourceref, targetref, NULL, type, pStatus);
790 _DEBUG_INFO("session cmdId : %d", session->cmdID);
792 _DEBUG_INFO(" end %d\n", data);
796 _DEBUG_INFO(" error : %d\n", ret);
801 DM_ERROR create_newstatus(Session * session, DM_ERROR data, Command * command, Location * sourceref, Location * targetref, CommandType type, Status ** pStatus)
805 DM_ERROR ret = DM_OK;
807 retvm_if((session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
808 retvm_if((command) == NULL, COMMON_ERR_IS_NULL, "command is NULL!!");
810 ret = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID, sourceref, targetref, NULL, type, pStatus);
815 _DEBUG_INFO("session cmdId : %d", session->cmdID);
820 _DEBUG_INFO(" error : %d\n", ret);
825 DM_ERROR create_status(DM_ERROR data, unsigned int cmdID, unsigned int msgref, unsigned int cmdref, Location * sourceref, Location * targetref, Chal * pChal, CommandType type, Status ** pStatus)
829 DM_ERROR ret = DM_OK;
831 _DEBUG_INFO("cmdref : %d msgref : %d", cmdref, msgref);
832 _DEBUG_INFO("create status : %d", data);
834 *pStatus = (Status *) calloc(1, sizeof(Status));
835 if (*pStatus == NULL) {
836 ret = COMMON_ERR_ALLOC;
840 (*pStatus)->cmdID = cmdID;
841 (*pStatus)->msgRef = msgref;
842 (*pStatus)->cmdRef = cmdref;
843 (*pStatus)->type = type;
845 (*pStatus)->data = g_strdup_printf("%d", data);
847 if (sourceref != NULL) {
848 (*pStatus)->sourceRef = dup_location(sourceref);
851 if (targetref != NULL) {
852 (*pStatus)->targetRef = dup_location(targetref);
853 if (targetref->locName != NULL) {
854 _DEBUG_INFO("targetref : %s", targetref->locName);
856 if (targetref->locURI != NULL) {
857 _DEBUG_INFO("targetref : %s", targetref->locURI);
862 (*pStatus)->chal = dup_chal(pChal);
868 _DEBUG_INFO(" error : %d\n", ret);
873 DM_ERROR set_status_data(Session * session, Command * pCommand, Item * item, DM_ERROR data)
877 DM_ERROR ret = DM_OK;
879 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
880 retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
881 retvm_if((item) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "item is NULL!!");
883 GList *status_iter = NULL;
885 char *target_url = get_location_locuri(item->target);
887 for (status_iter = session->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
888 temp = status_iter->data;
890 if (pCommand->cmdID == temp->cmdRef && strcmp(temp->targetRef->locURI, target_url) == 0) {
891 status_iter->data = g_strdup_printf("%d", data);
900 DM_ERROR set_resultscommand_msgref(Command * pCommand, unsigned int msgRef)
904 DM_ERROR ret = DM_OK;
906 retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
908 pCommand->private.results.msgRef = msgRef;
915 DM_ERROR set_resultscommand_cmdref(Command * pCommand, unsigned int cmdRef)
919 DM_ERROR ret = DM_OK;
921 retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
923 pCommand->private.results.cmdRef = cmdRef;
930 DM_ERROR set_resultscommand_targetref(Command * pCommand, Location * pLocation)
934 DM_ERROR ret = DM_OK;
936 retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pCommand is NULL!!");
937 retvm_if((pLocation) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pLocation is NULL!!");
939 pCommand->private.results.targetRef = dup_location(pLocation);