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.
21 * @brief This file is the source file of implementation of functions for command structure which is used in Service Adapter
26 #include <sync_agent.h>
28 #include "service-adapter/sa_command.h"
29 #include "service-adapter/sa_command_internal.h"
30 #include "service-adapter/sa_elements.h"
31 #include "service-adapter/sa_elements_internal.h"
32 #include "service-adapter/sa_session.h"
33 #include "service-adapter/sa_session_internal.h"
35 #ifndef OMADS_AGENT_LOG
37 #define LOG_TAG "OMA_DS_SA"
40 static command_s *_create_command(session_s * session, command_type_e type);
42 static command_s *_create_command(session_s * session, command_type_e type)
45 sa_error_type_e errorType = SA_INTERNAL_OK;
47 if (session == NULL) {
48 errorType = SA_INTERNAL_NOT_DEFINED;
53 errorType = SA_INTERNAL_NOT_DEFINED;
57 command_s *cmd = (command_s *) calloc(1, sizeof(command_s));
59 errorType = SA_INTERNAL_NO_MEMORY;
64 cmd->msg_id = session->msg_id;
65 cmd->cmd_id = session->cmd_id;
78 void free_commands(GList * commands)
82 retm_if(commands == NULL, "List is NULL");
84 _DEBUG_INFO("start list length is %d", g_list_length(commands));
87 command_s *pCommand = NULL;
88 for (iter = commands; iter != NULL;) {
89 pCommand = iter->data;
90 _DEBUG_INFO("command list length is %d command type is %d\n", g_list_length(commands), pCommand->type);
92 iter = g_list_next(iter);
93 if (pCommand->type != COMMAND_TYPE_UNKNOWN) {
94 commands = g_list_remove(commands, pCommand);
95 free_command(pCommand);
99 g_list_free(commands);
105 sa_error_type_e create_alert_command(session_s * session, alert_type_e sync_type, location_s * source, location_s * target, char *last_anchor, char *next_anchor, cred_s * cred, command_s ** command)
108 _DEBUG_INFO("start with syncType =%d", sync_type);
110 sa_error_type_e errorType = SA_INTERNAL_OK;
111 anchor_s *pAnchor = NULL;
113 if (source == NULL) {
114 errorType = SA_INTERNAL_NOT_DEFINED;
118 if (target == NULL) {
119 errorType = SA_INTERNAL_NOT_DEFINED;
123 *command = _create_command(session, COMMAND_TYPE_ALERT);
124 if (*command == NULL) {
125 errorType = SA_INTERNAL_NO_MEMORY;
129 (*command)->target = target;
130 (*command)->source = source;
132 /*TODO check that sync Type is ALERT_NEXT_MESSAGE(does not need last and next anchor) */
133 /*TODO check that sync Type is ALERT_SLOW_SYNC(does not need last anchor) */
135 if (next_anchor != NULL) {
136 errorType = create_anchor(last_anchor, next_anchor, &pAnchor);
137 if (errorType != SA_INTERNAL_OK)
140 (*command)->private.alert.anchor = pAnchor;
145 (*command)->cred = dup_cred(cred);
147 (*command)->private.alert.type = sync_type;
155 free_anchor(pAnchor);
161 sa_error_type_e create_get_command(session_s * session, location_s * target, const char *content_type, command_s ** command)
164 _DEBUG_INFO("start with content type = %s", content_type);
166 sa_error_type_e errorType = SA_INTERNAL_OK;
168 if (target == NULL) {
169 errorType = SA_INTERNAL_NOT_DEFINED;
173 *command = _create_command(session, COMMAND_TYPE_GET);
174 if (*command == NULL) {
175 errorType = SA_INTERNAL_NO_MEMORY;
179 (*command)->private.access.item = create_item();
180 if ((*command)->private.access.item == NULL) {
181 errorType = SA_INTERNAL_NO_MEMORY;
185 set_item_target((*command)->private.access.item, target);
187 if (content_type != NULL)
188 (*command)->private.access.item->content_type = strdup(content_type);
196 sa_error_type_e create_put_command(session_s * session, location_s * source, const char *content_type, devinf_s * devinf, command_s ** command)
199 _DEBUG_INFO("start with content type = %s", content_type);
201 sa_error_type_e errorType = SA_INTERNAL_OK;
203 if (source == NULL) {
204 errorType = SA_INTERNAL_NOT_DEFINED;
208 *command = _create_command(session, COMMAND_TYPE_PUT);
209 if (*command == NULL) {
210 errorType = SA_INTERNAL_NO_MEMORY;
214 (*command)->private.access.item = create_item_for_devinf(devinf);
216 if ((*command)->private.access.item == NULL) {
217 errorType = SA_INTERNAL_NO_MEMORY;
221 set_item_source((*command)->private.access.item, source);
223 if (content_type != NULL)
224 (*command)->private.access.item->content_type = strdup(content_type);
232 sa_error_type_e create_results_command(session_s * session, location_s * source, const char *content_type, devinf_s * devinf, command_s ** command)
235 _DEBUG_INFO("start with content type = %s", content_type);
237 sa_error_type_e errorType = SA_INTERNAL_OK;
239 if (source == NULL) {
240 errorType = SA_INTERNAL_NOT_DEFINED;
244 *command = _create_command(session, COMMAND_TYPE_RESULTS);
245 if (*command == NULL) {
246 errorType = SA_INTERNAL_NO_MEMORY;
250 (*command)->private.results.item = create_item_for_devinf(devinf);
252 if ((*command)->private.results.item == NULL) {
253 errorType = SA_INTERNAL_NO_MEMORY;
257 set_item_source((*command)->private.results.item, source);
259 if (content_type != NULL)
260 (*command)->private.results.item->content_type = strdup(content_type);
268 sa_error_type_e create_sync_start_command(session_s * session, location_s * source, location_s * target, command_s ** command)
272 sa_error_type_e errorType = SA_INTERNAL_OK;
274 if (source == NULL) {
275 errorType = SA_INTERNAL_NOT_DEFINED;
279 if (target == NULL) {
280 errorType = SA_INTERNAL_NOT_DEFINED;
284 *command = _create_command(session, COMMAND_TYPE_SYNC_START);
285 if (*command == NULL) {
286 errorType = SA_INTERNAL_NO_MEMORY;
290 (*command)->source = source;
291 (*command)->target = target;
299 sa_error_type_e set_sync_start_command_number_of_changes(command_s * command, unsigned int number_of_changes)
303 sa_error_type_e errorType = SA_INTERNAL_OK;
305 if (command == NULL) {
306 errorType = SA_INTERNAL_NOT_DEFINED;
310 command->private.sync.has_num_changed = 1;
311 command->private.sync.num_changed = number_of_changes;
319 sa_error_type_e set_sync_start_command_mem(command_s * command, mem_s * mem)
323 sa_error_type_e errorType = SA_INTERNAL_OK;
325 if (command == NULL) {
326 errorType = SA_INTERNAL_NOT_DEFINED;
330 command->private.sync.mem = mem;
339 sa_error_type_e create_sync_end_command(command_s ** command)
343 sa_error_type_e errorType = SA_INTERNAL_OK;
345 *command = (command_s *) calloc(1, sizeof(command_s));
346 if (*command == NULL) {
347 errorType = SA_INTERNAL_NO_MEMORY;
351 (*command)->type = COMMAND_TYPE_SYNC_END;
359 sa_error_type_e create_add_command(session_s * session, change_type_e type, char *luid, const char *content_type, char *data, unsigned int size, int more_data, command_s ** command)
362 _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type);
364 sa_error_type_e errorType = SA_INTERNAL_OK;
366 location_s *pLocation = NULL;
368 *command = _create_command(session, COMMAND_TYPE_ADD);
369 if (*command == NULL) {
370 errorType = SA_INTERNAL_NO_MEMORY;
374 temp = create_item_for_data(data, size);
376 errorType = SA_INTERNAL_NO_MEMORY;
380 (*command)->private.change.type = type;
381 if (content_type != NULL)
382 temp->content_type = strdup(content_type);
383 temp->more_data = more_data;
385 errorType = create_location(luid, NULL, &pLocation);
386 if (errorType != SA_INTERNAL_OK)
389 set_item_source(temp, pLocation);
390 (*command)->private.change.items = g_list_append((*command)->private.change.items, temp);
402 sa_error_type_e create_replace_command(session_s * session, change_type_e type, char *luid, const char *content_type, const char *data, unsigned int size, int more_data, command_s ** command)
405 _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type);
407 sa_error_type_e errorType = SA_INTERNAL_OK;
409 location_s *pLocation = NULL;
411 *command = _create_command(session, COMMAND_TYPE_REPLACE);
412 if (*command == NULL) {
413 errorType = SA_INTERNAL_NO_MEMORY;
417 temp = create_item_for_data(data, size);
419 errorType = SA_INTERNAL_NO_MEMORY;
423 (*command)->private.change.type = type;
424 if (content_type != NULL)
425 temp->content_type = strdup(content_type);
426 temp->more_data = more_data;
428 errorType = create_location(luid, NULL, &pLocation);
429 if (errorType != SA_INTERNAL_OK)
432 set_item_source(temp, pLocation);
433 (*command)->private.change.items = g_list_append((*command)->private.change.items, temp);
445 sa_error_type_e create_delete_command(session_s * session, change_type_e type, char *luid, const char *content_type, command_s ** command)
448 _DEBUG_INFO("start with ChangeType = %d content type = %s", type, content_type);
450 sa_error_type_e errorType = SA_INTERNAL_OK;
452 location_s *pLocation = NULL;
454 *command = _create_command(session, COMMAND_TYPE_DELETE);
455 if (*command == NULL) {
456 errorType = SA_INTERNAL_NO_MEMORY;
460 temp = create_item();
462 errorType = SA_INTERNAL_NO_MEMORY;
466 (*command)->private.change.type = type;
468 errorType = create_location(luid, NULL, &pLocation);
469 if (errorType != SA_INTERNAL_OK)
472 set_item_source(temp, pLocation);
473 if (content_type != NULL)
474 temp->content_type = strdup(content_type);
475 (*command)->private.change.items = g_list_append((*command)->private.change.items, temp);
488 sa_error_type_e create_map_command(session_s * session, location_s * source, location_s * target, command_s ** command)
492 sa_error_type_e errorType = SA_INTERNAL_OK;
494 if (source == NULL) {
495 errorType = SA_INTERNAL_NOT_DEFINED;
499 if (target == NULL) {
500 errorType = SA_INTERNAL_NOT_DEFINED;
504 *command = _create_command(session, COMMAND_TYPE_MAP);
505 if (*command == NULL) {
506 errorType = SA_INTERNAL_NO_MEMORY;
510 (*command)->source = source;
511 (*command)->target = target;
519 sa_error_type_e set_map_command_item(command_s * map_command, item_s * temp)
523 sa_error_type_e errorType = SA_INTERNAL_OK;
525 if (map_command == NULL) {
526 errorType = SA_INTERNAL_NOT_DEFINED;
530 map_command->private.map.items = g_list_append(map_command->private.map.items, temp);
538 sa_error_type_e increase_command_ref_count(command_s * command)
541 sa_error_type_e errorType = SA_INTERNAL_OK;
543 if (command == NULL) {
544 errorType = SA_INTERNAL_NOT_DEFINED;
548 command->ref_count++;
556 sa_error_type_e decrease_command_ref_count(command_s * command)
559 sa_error_type_e errorType = SA_INTERNAL_OK;
561 if (command == NULL) {
562 errorType = SA_INTERNAL_NOT_DEFINED;
566 command->ref_count--;
574 sa_error_type_e set_results_command_msg_ref(command_s * command, unsigned int msg_ref)
577 sa_error_type_e errorType = SA_INTERNAL_OK;
579 if (command == NULL) {
580 errorType = SA_INTERNAL_NOT_DEFINED;
584 command->private.results.msg_ref = msg_ref;
592 sa_error_type_e set_results_command_cmd_ref(command_s * command, unsigned int cmd_ref)
595 sa_error_type_e errorType = SA_INTERNAL_OK;
597 if (command == NULL) {
598 errorType = SA_INTERNAL_NOT_DEFINED;
602 command->private.results.cmd_ref = cmd_ref;
610 sa_error_type_e set_results_command_target_ref(command_s * command, location_s * location)
613 sa_error_type_e errorType = SA_INTERNAL_OK;
615 if (command == NULL) {
616 errorType = SA_INTERNAL_NOT_DEFINED;
620 _DEBUG_INFO("start with Command Type =%d\n", command->type);
622 command->private.results.target_ref = dup_location(location);
630 void free_command(command_s * command)
634 retm_if(command == NULL, "pCommand is NULL");
636 _DEBUG_INFO("start with Command type is %d", command->type);
640 if (command->ref_count > 1) {
641 _DEBUG_INFO("Command's refCount is %d", command->ref_count);
642 decrease_command_ref_count(command);
646 switch (command->type) {
647 case COMMAND_TYPE_ALERT:
648 if (command->private.alert.anchor != NULL) {
649 free_anchor(command->private.alert.anchor);
650 command->private.alert.anchor = NULL;
653 if (command->private.alert.content_type != NULL) {
654 free(command->private.alert.content_type);
655 command->private.alert.content_type = NULL;
658 case COMMAND_TYPE_SYNC_START:
661 case COMMAND_TYPE_SYNC_END:
664 case COMMAND_TYPE_PUT:
665 if (command->private.access.type != NULL) {
666 free(command->private.access.type);
667 command->private.access.type = NULL;
670 if (command->private.access.item != NULL) {
671 free_item(command->private.access.item);
672 command->private.access.item = NULL;
675 case COMMAND_TYPE_HEADER:
676 /*COMMAND_TYPE_HEADER doesnot come here */
678 case COMMAND_TYPE_ADD:
679 case COMMAND_TYPE_REPLACE:
680 case COMMAND_TYPE_DELETE:
681 for (iter = command->private.change.items; iter != NULL; iter = g_list_next(iter))
682 free_item(iter->data);
684 case COMMAND_TYPE_MAP:
685 for (iter = command->private.map.items; iter != NULL; iter = g_list_next(iter))
686 free_item(iter->data);
688 case COMMAND_TYPE_GET:
689 if (command->private.access.type != NULL) {
690 free(command->private.access.type);
691 command->private.access.type = NULL;
694 if (command->private.access.item != NULL) {
695 free_item(command->private.access.item);
696 command->private.access.item = NULL;
699 case COMMAND_TYPE_RESULTS:
700 if (command->private.results.type != NULL) {
701 free(command->private.results.type);
702 command->private.results.type = NULL;
705 if (command->private.results.item != NULL) {
706 free_item(command->private.results.item);
707 command->private.results.item = NULL;
710 if (command->private.results.target_ref != NULL) {
711 free_location(command->private.results.target_ref);
712 command->private.results.target_ref = NULL;
716 case COMMAND_TYPE_UNKNOWN:
720 if (command->source != NULL) {
721 free_location(command->source);
722 command->source = NULL;
725 if (command->target != NULL) {
726 free_location(command->target);
727 command->target = NULL;
730 if (command->cred != NULL) {
731 free_cred(command->cred);
732 command->cred = NULL;
740 sa_error_type_e create_new_status_location(session_s * session, oma_status_type_e data, command_s * command, location_s * source_ref, location_s * target_ref, command_type_e type, status_s ** status)
743 _DEBUG_INFO("start Errortype %d", data);
745 sa_error_type_e errorType = SA_INTERNAL_OK;
747 errorType = create_status(data, session->cmd_id, session->last_recieved_msg_id, command->cmd_id, source_ref, target_ref, type, status);
748 if (errorType != SA_INTERNAL_OK)
759 sa_error_type_e create_new_status(session_s * session, oma_status_type_e data, command_s * command, command_type_e type, status_s ** status)
762 _DEBUG_INFO("start Errortype %d", data);
764 sa_error_type_e errorType = SA_INTERNAL_OK;
766 errorType = create_status(data, session->cmd_id, session->last_recieved_msg_id, command->cmd_id, command->source, command->target, type, status);
767 if (errorType != SA_INTERNAL_OK)
778 sa_error_type_e create_status(oma_status_type_e data, unsigned int cmd_id, unsigned int msg_ref, unsigned int cmd_ref, location_s * source_ref, location_s * target_ref, command_type_e type, status_s ** status)
781 _DEBUG_INFO("start Errortype %d", data);
783 sa_error_type_e errorType = SA_INTERNAL_OK;
785 *status = (status_s *) calloc(1, sizeof(status_s));
786 if (*status == NULL) {
787 errorType = SA_INTERNAL_NO_MEMORY;
791 (*status)->cmd_id = cmd_id;
792 (*status)->msg_ref = msg_ref;
793 (*status)->cmd_ref = cmd_ref;
794 (*status)->type = type;
795 (*status)->data = g_strdup_printf("%d", data);
797 if (source_ref != NULL)
798 (*status)->source_ref = dup_location(source_ref);
800 if (target_ref != NULL)
801 (*status)->target_ref = dup_location(target_ref);
809 void free_statuses(GList * statuses)
813 retm_if(statuses == NULL, "List is NULL");
816 status_s *status = NULL;
817 _DEBUG_INFO("count : %d", g_list_length(statuses));
818 for (iter = statuses; iter != NULL; iter = g_list_next(iter)) {
819 status = (status_s *) iter->data;
823 g_list_free(statuses);
828 void free_status(status_s * status)
832 retm_if(status == NULL, "pStatus is NULL");
834 if (status->data != NULL)
837 if (status->source_ref != NULL)
838 free_location(status->source_ref);
840 if (status->target_ref != NULL)
841 free_location(status->target_ref);
843 if (status->cred != NULL)
844 free_cred(status->cred);
846 if (status->chal != NULL)
847 free_chal(status->chal);
849 free_item(status->item);
857 oma_status_type_e get_status_code(status_s * status)
863 return atoi(status->data);
866 command_type_e convert_command_type(char *type)
870 command_type_e commandType = COMMAND_TYPE_UNKNOWN;
872 retvm_if(type == NULL, commandType, "type is NULL");
874 if (!strcmp(type, "Alert")) {
875 commandType = COMMAND_TYPE_ALERT;
876 } else if (!strcmp(type, "Sync")) {
877 commandType = COMMAND_TYPE_SYNC_START;
878 } else if (!strcmp(type, "Put")) {
879 commandType = COMMAND_TYPE_PUT;
880 } else if (!strcmp(type, "SyncHdr")) {
881 commandType = COMMAND_TYPE_HEADER;
882 } else if (!strcmp(type, "Add")) {
883 commandType = COMMAND_TYPE_ADD;
884 } else if (!strcmp(type, "Replace")) {
885 commandType = COMMAND_TYPE_REPLACE;
886 } else if (!strcmp(type, "Map")) {
887 commandType = COMMAND_TYPE_MAP;
888 } else if (!strcmp(type, "Delete")) {
889 commandType = COMMAND_TYPE_DELETE;
890 } else if (!strcmp(type, "Results")) {
891 commandType = COMMAND_TYPE_RESULTS;
892 } else if (!strcmp(type, "Get")) {
893 commandType = COMMAND_TYPE_GET;
900 change_type_e convert_change_type_command_type(command_type_e type)
904 change_type_e changeType = CHANGE_UNKNOWN;
906 case COMMAND_TYPE_UNKNOWN:
907 case COMMAND_TYPE_ALERT:
908 case COMMAND_TYPE_SYNC_START:
909 case COMMAND_TYPE_SYNC_END:
910 case COMMAND_TYPE_PUT:
911 case COMMAND_TYPE_HEADER:
912 case COMMAND_TYPE_MAP:
913 case COMMAND_TYPE_GET:
914 case COMMAND_TYPE_RESULTS:
915 /*never comes these commands */
917 case COMMAND_TYPE_ADD:
918 changeType = CHANGE_ADD;
920 case COMMAND_TYPE_REPLACE:
921 changeType = CHANGE_REPLACE;
923 case COMMAND_TYPE_DELETE:
924 changeType = CHANGE_DELETE;
932 command_type_e convert_command_type_change_type(change_type_e type)
936 command_type_e commandType = COMMAND_TYPE_UNKNOWN;
940 commandType = COMMAND_TYPE_UNKNOWN;
943 commandType = COMMAND_TYPE_ADD;
946 commandType = COMMAND_TYPE_REPLACE;
949 commandType = COMMAND_TYPE_DELETE;
958 ChangeType convertToChangeType(unsigned int type) {
960 ChangeType changeType=CHANGE_UNKNOWN;
963 changeType = CHANGE_ADD;
966 changeType = CHANGE_REPLACE;
969 changeType = CHANGE_DELETE;
976 char *convertFromCommandType(CommandType type) {
977 char *commandType=NULL;
980 case COMMAND_TYPE_ALERT:
981 commandType = "Alert";
983 case COMMAND_TYPE_SYNC_START:
984 case COMMAND_TYPE_SYNC_END:
985 commandType = "Sync";
987 case COMMAND_TYPE_PUT:
990 case COMMAND_TYPE_HEADER:
991 commandType = "SyncHdr";
993 case COMMAND_TYPE_ADD:
996 case COMMAND_TYPE_REPLACE:
997 commandType = "Replace";
999 case COMMAND_TYPE_MAP:
1000 commandType = "Map";
1002 case COMMAND_TYPE_DELETE:
1003 commandType = "Delete";
1005 case COMMAND_TYPE_RESULTS:
1006 commandType = "Results";
1008 case COMMAND_TYPE_GET:
1009 commandType = "Get";
1012 commandType="UNKNOWN";