4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JuHak Park <juhaki.park@samsung.com>,
7 * JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8 * SunBong Ha <sunbong.ha@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
28 * For any sort of issue you concern as to this software,
29 * you may use following point of contact.
30 * All resources contributed on this software
31 * are orinigally written by S-Core Inc., a member of Samsung Group.
33 * SeongWon Shim <seongwon.shim@samsung.com>
39 * @brief This file is the source file of implementation of functions for command structure which is used in Service Adapter
43 #include "agent-framework/Utility/fw_log.h"
44 #include "ServiceAdapter/SA_Command.h"
45 #include "ServiceAdapter/SA_Command_Internal.h"
46 #include "ServiceAdapter/SA_Elements.h"
47 #include "ServiceAdapter/SA_Elements_Internal.h"
48 #include "ServiceAdapter/SA_Session.h"
49 #include "ServiceAdapter/SA_Session_Internal.h"
51 #define LOG_TAG "OMA_DS_SA"
53 static Command *__create_command(Session *session, CommandType type);
56 static Command *__create_command(Session *session, CommandType type)
59 SA_ErrorType errorType = SA_INTERNAL_OK;
62 errorType = SA_INTERNAL_NOT_DEFINED;
67 errorType = SA_INTERNAL_NOT_DEFINED;
71 Command *cmd = (Command *) calloc(1, sizeof(Command));
73 errorType = SA_INTERNAL_NO_MEMORY;
79 cmd->msgID = session->msgID;
80 cmd->cmdID = session->cmdID;
90 FW_LOGE("error : %d", errorType);
94 void free_commands(GList *commands)
96 FW_LOGV("start list length is %d", g_list_length(commands));
99 FW_LOGV("List is NULL");
105 Command *pCommand = NULL;
106 for (iter = commands; iter != NULL;) {
107 pCommand = iter->data;
108 FW_LOGV("command list length is %d command type is %d\n", g_list_length(commands), pCommand->type);
110 iter = g_list_next(iter);
111 if (pCommand->type != COMMAND_TYPE_UNKNOWN) {
112 free_command(pCommand);
113 commands = g_list_remove(commands, pCommand);
118 g_list_free(commands);
125 SA_ErrorType create_alert_command(Session *session, AlertType syncType, Location *source, Location *target,
126 char *lastAnchor, char *nextAnchor, Cred *pCred, Command **pCommand)
128 FW_LOGV("start with syncType =%d", syncType);
130 SA_ErrorType errorType = SA_INTERNAL_OK;
131 Anchor *pAnchor = NULL;
134 errorType = SA_INTERNAL_NOT_DEFINED;
139 errorType = SA_INTERNAL_NOT_DEFINED;
143 *pCommand = __create_command(session, COMMAND_TYPE_ALERT);
144 if (*pCommand == NULL) {
145 errorType = SA_INTERNAL_NO_MEMORY;
149 (*pCommand)->target = target;
150 (*pCommand)->source = source;
152 /*TODO check that sync Type is ALERT_NEXT_MESSAGE(does not need last and next anchor)*/
153 /*TODO check that sync Type is ALERT_SLOW_SYNC(does not need last anchor)*/
156 errorType = create_anchor(lastAnchor, nextAnchor , &pAnchor);
157 if (errorType != SA_INTERNAL_OK)
160 (*pCommand)->private.alert.anchor = pAnchor;
165 (*pCommand)->cred = dup_cred(pCred);
167 (*pCommand)->private.alert.type = syncType;
176 free_anchor(pAnchor);
178 FW_LOGE("error : %d", errorType);
182 SA_ErrorType create_get_command(Session *session, Location* target, const char *contenttype, Command **pCommand)
184 FW_LOGV("start with content type = %s", contenttype);
186 SA_ErrorType errorType = SA_INTERNAL_OK;
189 errorType = SA_INTERNAL_NOT_DEFINED;
193 *pCommand = __create_command(session, COMMAND_TYPE_GET);
194 if (*pCommand == NULL) {
195 errorType = SA_INTERNAL_NO_MEMORY;
199 (*pCommand)->private.access.item = create_item();
200 if ((*pCommand)->private.access.item == NULL) {
201 errorType = SA_INTERNAL_NO_MEMORY;
205 set_item_target((*pCommand)->private.access.item, target);
207 if (contenttype != NULL)
208 (*pCommand)->private.access.item->contenttype = strdup(contenttype);
215 FW_LOGE("error : %d", errorType);
220 SA_ErrorType create_put_command(Session *session, Location *source, const char *contenttype,
221 DevInf *devInf , Command **pCommand)
223 FW_LOGV("start with content type = %s", contenttype);
225 SA_ErrorType errorType = SA_INTERNAL_OK;
228 errorType = SA_INTERNAL_NOT_DEFINED;
232 *pCommand = __create_command(session, COMMAND_TYPE_PUT);
233 if (*pCommand == NULL) {
234 errorType = SA_INTERNAL_NO_MEMORY;
238 (*pCommand)->private.access.item = create_item_for_devinf(devInf);
240 if ((*pCommand)->private.access.item == NULL) {
241 errorType = SA_INTERNAL_NO_MEMORY;
245 set_item_source((*pCommand)->private.access.item, source);
247 if (contenttype != NULL)
248 (*pCommand)->private.access.item->contenttype = strdup(contenttype);
255 FW_LOGE("error : %d", errorType);
260 SA_ErrorType create_results_command(Session *session, Location *source, const char *contenttype,
261 DevInf *devInf , Command **pCommand)
263 FW_LOGV("start with content type = %s", contenttype);
265 SA_ErrorType errorType = SA_INTERNAL_OK;
268 errorType = SA_INTERNAL_NOT_DEFINED;
272 *pCommand = __create_command(session, COMMAND_TYPE_RESULTS);
273 if (*pCommand == NULL) {
274 errorType = SA_INTERNAL_NO_MEMORY;
278 (*pCommand)->private.results.item = create_item_for_devinf(devInf);
280 if ((*pCommand)->private.results.item == NULL) {
281 errorType = SA_INTERNAL_NO_MEMORY;
285 set_item_source((*pCommand)->private.results.item, source);
287 if (contenttype != NULL)
288 (*pCommand)->private.results.item->contenttype = strdup(contenttype);
295 FW_LOGE("error : %d", errorType);
299 SA_ErrorType create_sync_start_command(Session *session, Location *source, Location *target, Command **pCommand)
303 SA_ErrorType errorType = SA_INTERNAL_OK;
306 errorType = SA_INTERNAL_NOT_DEFINED;
311 errorType = SA_INTERNAL_NOT_DEFINED;
315 *pCommand = __create_command(session, COMMAND_TYPE_SYNC_START);
316 if (*pCommand == NULL) {
317 errorType = SA_INTERNAL_NO_MEMORY;
321 (*pCommand)->source = source;
322 (*pCommand)->target = target;
329 FW_LOGE("error : %d", errorType);
333 SA_ErrorType set_sync_start_command_numberofchanges(Command *pCommand, unsigned int numberOfChanges)
336 SA_ErrorType errorType = SA_INTERNAL_OK;
338 if (pCommand == NULL) {
339 errorType = SA_INTERNAL_NOT_DEFINED;
343 pCommand->private.sync.hasNumChanged = 1;
344 pCommand->private.sync.numChanged = numberOfChanges;
350 FW_LOGE("errorType = %d", errorType);
354 SA_ErrorType create_sync_end_command(Command **pCommand)
358 SA_ErrorType errorType = SA_INTERNAL_OK;
360 *pCommand = (Command *) calloc(1, sizeof(Command));
361 if (*pCommand == NULL) {
362 errorType = SA_INTERNAL_NO_MEMORY;
366 (*pCommand)->type = COMMAND_TYPE_SYNC_END;
372 FW_LOGE("errorType = %d", errorType);
377 SA_ErrorType create_add_command(Session *session, ChangeType type, char *luid, const char *contenttype,
378 char *data, unsigned int size , int moreData, Command **pCommand)
380 FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
382 SA_ErrorType errorType = SA_INTERNAL_OK;
384 Location *pLocation = NULL;
386 *pCommand = __create_command(session, COMMAND_TYPE_ADD);
387 if (*pCommand == NULL) {
388 errorType = SA_INTERNAL_NO_MEMORY;
392 temp = create_item_for_data(data, size);
394 errorType = SA_INTERNAL_NO_MEMORY;
398 (*pCommand)->private.change.type = type;
399 if (contenttype != NULL)
400 temp->contenttype = strdup(contenttype);
401 temp->moreData = moreData;
403 errorType = create_location(luid, NULL, &pLocation);
404 if (errorType != SA_INTERNAL_OK)
407 set_item_source(temp, pLocation);
408 (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
414 FW_LOGE("errorType = %d", errorType);
418 SA_ErrorType create_replace_command(Session *session, ChangeType type, char *luid, const char *contenttype,
419 const char *data, unsigned int size, int moreData, Command **pCommand)
421 FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
423 SA_ErrorType errorType = SA_INTERNAL_OK;
425 Location *pLocation = NULL;
427 *pCommand = __create_command(session, COMMAND_TYPE_REPLACE);
428 if (*pCommand == NULL) {
429 errorType = SA_INTERNAL_NO_MEMORY;
433 temp = create_item_for_data(data, size);
435 errorType = SA_INTERNAL_NO_MEMORY;
439 (*pCommand)->private.change.type = type;
440 if (contenttype != NULL)
441 temp->contenttype = strdup(contenttype);
442 temp->moreData = moreData;
444 errorType = create_location(luid, NULL, &pLocation);
445 if (errorType != SA_INTERNAL_OK)
448 set_item_source(temp, pLocation);
449 (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
455 FW_LOGE("errorType = %d", errorType);
459 SA_ErrorType create_delete_command(Session *session, ChangeType type, char *luid, const char *contenttype, Command **pCommand)
461 FW_LOGV("start with ChangeType = %d content type = %s", type, contenttype);
463 SA_ErrorType errorType = SA_INTERNAL_OK;
465 Location *pLocation = NULL;
467 *pCommand = __create_command(session, COMMAND_TYPE_DELETE);
468 if (*pCommand == NULL) {
469 errorType = SA_INTERNAL_NO_MEMORY;
473 temp = create_item();
475 errorType = SA_INTERNAL_NO_MEMORY;
479 (*pCommand)->private.change.type = type;
481 errorType = create_location(luid, NULL, &pLocation);
482 if (errorType != SA_INTERNAL_OK)
485 set_item_source(temp, pLocation);
486 if (contenttype != NULL)
487 temp->contenttype = strdup(contenttype);
488 (*pCommand)->private.change.items = g_list_append((*pCommand)->private.change.items, temp);
494 FW_LOGE("errorType = %d", errorType);
499 SA_ErrorType create_map_command(Session *session, Location *source, Location *target, Command **pCommand)
503 SA_ErrorType errorType = SA_INTERNAL_OK;
506 errorType = SA_INTERNAL_NOT_DEFINED;
511 errorType = SA_INTERNAL_NOT_DEFINED;
515 *pCommand = __create_command(session, COMMAND_TYPE_MAP);
516 if (*pCommand == NULL) {
517 errorType = SA_INTERNAL_NO_MEMORY;
521 (*pCommand)->source = source;
522 (*pCommand)->target = target;
528 FW_LOGE("errorType = %d", errorType);
532 SA_ErrorType set_map_command_item(Command *mapCommand, Item *temp)
536 SA_ErrorType errorType = SA_INTERNAL_OK;
539 errorType = SA_INTERNAL_NOT_DEFINED;
543 mapCommand->private.map.items = g_list_append(mapCommand->private.map.items, temp);
549 FW_LOGE("errorType = %d", errorType);
553 SA_ErrorType increase_command_refcount(Command *pCommand)
556 SA_ErrorType errorType = SA_INTERNAL_OK;
559 errorType = SA_INTERNAL_NOT_DEFINED;
563 pCommand->refCount++;
569 FW_LOGE("errorType = %d", errorType);
573 SA_ErrorType decrease_command_refcount(Command *pCommand)
576 SA_ErrorType errorType = SA_INTERNAL_OK;
579 errorType = SA_INTERNAL_NOT_DEFINED;
583 pCommand->refCount--;
589 FW_LOGE("errorType = %d", errorType);
593 SA_ErrorType set_results_command_msgref(Command *pCommand, unsigned int msgRef)
596 SA_ErrorType errorType = SA_INTERNAL_OK;
599 errorType = SA_INTERNAL_NOT_DEFINED;
603 pCommand->private.results.msgRef = msgRef;
609 FW_LOGE("errorType = %d", errorType);
613 SA_ErrorType set_results_command_cmdref(Command *pCommand, unsigned int cmdRef)
616 SA_ErrorType errorType = SA_INTERNAL_OK;
619 errorType = SA_INTERNAL_NOT_DEFINED;
623 pCommand->private.results.cmdRef = cmdRef;
629 FW_LOGE("errorType = %d", errorType);
633 SA_ErrorType set_results_command_targetref(Command *pCommand, Location *pLocation)
635 SA_ErrorType errorType = SA_INTERNAL_OK;
638 errorType = SA_INTERNAL_NOT_DEFINED;
642 FW_LOGV("start with Command Type =%d\n", pCommand->type);
644 pCommand->private.results.targetRef = dup_location(pLocation);
650 FW_LOGE("errorType = %d", errorType);
654 void free_command(Command *pCommand)
659 FW_LOGV("start with Command type is %d", pCommand->type);
663 if (pCommand->refCount > 1) {
664 FW_LOGV("Command's refCount is %d", pCommand->refCount);
665 decrease_command_refcount(pCommand);
670 switch (pCommand->type) {
671 case COMMAND_TYPE_ALERT:
672 if (pCommand->private.alert.anchor) {
673 free_anchor(pCommand->private.alert.anchor);
674 pCommand->private.alert.anchor = NULL;
677 if (pCommand->private.alert.contentType) {
678 free(pCommand->private.alert.contentType);
679 pCommand->private.alert.contentType = NULL;
682 case COMMAND_TYPE_SYNC_START:
685 case COMMAND_TYPE_SYNC_END:
688 case COMMAND_TYPE_PUT:
689 if (pCommand->private.access.type) {
690 free(pCommand->private.access.type);
691 pCommand->private.access.type = NULL;
694 if (pCommand->private.access.item) {
695 free_item(pCommand->private.access.item);
696 pCommand->private.access.item = NULL;
699 case COMMAND_TYPE_HEADER:
700 /*COMMAND_TYPE_HEADER doesnot come here*/
702 case COMMAND_TYPE_ADD:
703 case COMMAND_TYPE_REPLACE:
704 case COMMAND_TYPE_DELETE:
705 for (iter = pCommand->private.change.items; iter != NULL; iter = g_list_next(iter))
706 free_item(iter->data);
708 case COMMAND_TYPE_MAP:
709 for (iter = pCommand->private.map.items; iter != NULL; iter = g_list_next(iter))
710 free_item(iter->data);
712 case COMMAND_TYPE_GET:
713 if (pCommand->private.access.type) {
714 free(pCommand->private.access.type);
715 pCommand->private.access.type = NULL;
718 if (pCommand->private.access.item) {
719 free_item(pCommand->private.access.item);
720 pCommand->private.access.item = NULL;
723 case COMMAND_TYPE_RESULTS:
724 if (pCommand->private.results.type) {
725 free(pCommand->private.results.type);
726 pCommand->private.results.type = NULL;
729 if (pCommand->private.results.item) {
730 free_item(pCommand->private.results.item);
731 pCommand->private.results.item = NULL;
734 if (pCommand->private.results.targetRef) {
735 free_location(pCommand->private.results.targetRef);
736 pCommand->private.results.targetRef = NULL;
740 case COMMAND_TYPE_UNKNOWN:
744 if (pCommand->source) {
745 free_location(pCommand->source);
746 pCommand->source = NULL;
749 if (pCommand->target) {
750 free_location(pCommand->target);
751 pCommand->target = NULL;
754 if (pCommand->cred) {
755 free_cred(pCommand->cred);
756 pCommand->cred = NULL;
764 SA_ErrorType create_new_status_location(Session *session, OMA_StatusType data, Command *command,
765 Location *sourceref, Location *targetref, CommandType type, Status **pStatus)
767 FW_LOGV("start Errortype %d", data);
769 SA_ErrorType errorType = SA_INTERNAL_OK;
771 errorType = create_status(data, session->cmdID, session->lastRecievedMsgID,
772 command->cmdID, sourceref, targetref, type, pStatus);
773 if (errorType != SA_INTERNAL_OK)
782 FW_LOGE("error : %d", errorType);
786 SA_ErrorType create_new_status(Session *session, OMA_StatusType data, Command *command,
787 CommandType type, Status **pStatus)
789 FW_LOGV("start Errortype %d", data);
791 SA_ErrorType errorType = SA_INTERNAL_OK;
793 errorType = create_status(data, session->cmdID, session->lastRecievedMsgID, command->cmdID,
794 command->source, command->target, type, pStatus);
795 if (errorType != SA_INTERNAL_OK)
804 FW_LOGE("error : %d", errorType);
808 SA_ErrorType create_status(OMA_StatusType data, unsigned int cmdID, unsigned int msgref , unsigned int cmdref,
809 Location *sourceref, Location *targetref, CommandType type, Status **pStatus)
811 FW_LOGV("start Errortype %d", data);
813 SA_ErrorType errorType = SA_INTERNAL_OK;
815 *pStatus = (Status *)calloc(1, sizeof(Status));
816 if (*pStatus == NULL) {
817 errorType = SA_INTERNAL_NO_MEMORY;
821 (*pStatus)->cmdID = cmdID;
822 (*pStatus)->msgRef = msgref;
823 (*pStatus)->cmdRef = cmdref;
824 (*pStatus)->type = type;
827 (*pStatus)->data = g_strdup_printf("%i", data);
830 (*pStatus)->sourceRef = dup_location(sourceref);
833 (*pStatus)->targetRef = dup_location(targetref);
840 FW_LOGE("error : %d", errorType);
844 void free_statuses(GList *status)
849 FW_LOGV("List is null");
854 FW_LOGV("count : %d", g_list_length(status));
855 for (iter = status; iter != NULL; iter = g_list_next(iter))
856 free_status(iter->data);
863 void free_status(Status *pStatus)
873 if (pStatus->sourceRef)
874 free_location(pStatus->sourceRef);
876 if (pStatus->targetRef)
877 free_location(pStatus->targetRef);
880 free_cred(pStatus->cred);
883 free_chal(pStatus->chal);
885 free_item(pStatus->item);
893 OMA_StatusType get_status_code(Status *status)
895 return atoi(status->data);
898 CommandType convert_command_type(char *type)
900 CommandType commandType = COMMAND_TYPE_UNKNOWN;
904 if (!strcmp(type, "Alert")) {
905 commandType = COMMAND_TYPE_ALERT;
906 } else if (!strcmp(type, "Sync")) {
907 commandType = COMMAND_TYPE_SYNC_START;
908 } else if (!strcmp(type, "Put")) {
909 commandType = COMMAND_TYPE_PUT;
910 } else if (!strcmp(type, "SyncHdr")) {
911 commandType = COMMAND_TYPE_HEADER;
912 } else if (!strcmp(type, "Add")) {
913 commandType = COMMAND_TYPE_ADD;
914 } else if (!strcmp(type, "Replace")) {
915 commandType = COMMAND_TYPE_REPLACE;
916 } else if (!strcmp(type, "Map")) {
917 commandType = COMMAND_TYPE_MAP;
918 } else if (!strcmp(type, "Delete")) {
919 commandType = COMMAND_TYPE_DELETE;
920 } else if (!strcmp(type, "Results")) {
921 commandType = COMMAND_TYPE_RESULTS;
922 } else if (!strcmp(type, "Get")) {
923 commandType = COMMAND_TYPE_GET;
929 ChangeType convert_change_type_command_type(CommandType type)
931 ChangeType changeType = CHANGE_UNKNOWN;
933 case COMMAND_TYPE_UNKNOWN:
934 case COMMAND_TYPE_ALERT:
935 case COMMAND_TYPE_SYNC_START:
936 case COMMAND_TYPE_SYNC_END:
937 case COMMAND_TYPE_PUT:
938 case COMMAND_TYPE_HEADER:
939 case COMMAND_TYPE_MAP:
940 case COMMAND_TYPE_GET:
941 case COMMAND_TYPE_RESULTS:
942 /*never comes these commands*/
944 case COMMAND_TYPE_ADD:
945 changeType = CHANGE_ADD;
947 case COMMAND_TYPE_REPLACE:
948 changeType = CHANGE_REPLACE;
950 case COMMAND_TYPE_DELETE:
951 changeType = CHANGE_DELETE;
958 CommandType convert_command_type_change_type(ChangeType type)
960 CommandType commandType = COMMAND_TYPE_UNKNOWN;
964 commandType = COMMAND_TYPE_UNKNOWN;
967 commandType = COMMAND_TYPE_ADD;
970 commandType = COMMAND_TYPE_REPLACE;
973 commandType = COMMAND_TYPE_DELETE;
981 ChangeType convertToChangeType(unsigned int type) {
983 ChangeType changeType=CHANGE_UNKNOWN;
986 changeType = CHANGE_ADD;
989 changeType = CHANGE_REPLACE;
992 changeType = CHANGE_DELETE;
1000 char *convertFromCommandType(CommandType type) {
1001 char *commandType=NULL;
1004 case COMMAND_TYPE_ALERT:
1005 commandType = "Alert";
1007 case COMMAND_TYPE_SYNC_START:
1008 case COMMAND_TYPE_SYNC_END:
1009 commandType = "Sync";
1011 case COMMAND_TYPE_PUT:
1012 commandType = "Put";
1014 case COMMAND_TYPE_HEADER:
1015 commandType = "SyncHdr";
1017 case COMMAND_TYPE_ADD:
1018 commandType = "Add";
1020 case COMMAND_TYPE_REPLACE:
1021 commandType = "Replace";
1023 case COMMAND_TYPE_MAP:
1024 commandType = "Map";
1026 case COMMAND_TYPE_DELETE:
1027 commandType = "Delete";
1029 case COMMAND_TYPE_RESULTS:
1030 commandType = "Results";
1032 case COMMAND_TYPE_GET:
1033 commandType = "Get";
1036 commandType="UNKNOWN";