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 structures which is used in Service Adapter
42 #include "agent-framework/Utility/fw_log.h"
43 #include "ServiceAdapter/SA_Elements.h"
44 #include "ServiceAdapter/SA_Elements_Internal.h"
45 #include "ServiceAdapter/SA_Session_Internal.h"
46 #include "ServiceAdapter/SA_Command.h"
48 #define LOG_TAG "OMA_DS_SA"
50 SA_ErrorType create_anchor(char *last, char *next, Anchor **pAnchor)
54 SA_ErrorType errorType = SA_INTERNAL_OK;
57 errorType = SA_INTERNAL_NOT_DEFINED;
61 *pAnchor = (Anchor *)calloc(1, sizeof(Anchor));
62 if (*pAnchor == NULL) {
63 errorType = SA_INTERNAL_NO_MEMORY;
68 (*pAnchor)->lastAnchor = strdup(last);
70 (*pAnchor)->nextAnchor = strdup(next);
76 FW_LOGE("error :%d", errorType);
80 SA_ErrorType set_item_anchor(Item *item, Anchor *anchor)
84 SA_ErrorType errorType = SA_INTERNAL_OK;
87 errorType = SA_INTERNAL_NOT_DEFINED;
92 errorType = SA_INTERNAL_NOT_DEFINED;
97 free_anchor(item->anchor);
101 Anchor *pAnchor = NULL;
102 errorType = create_anchor(anchor->lastAnchor, anchor->nextAnchor, &pAnchor);
103 if (errorType != SA_INTERNAL_OK)
105 item->anchor = pAnchor;
111 FW_LOGE("error :%d", errorType);
115 SA_ErrorType set_last_anchor(Anchor *anchor, char *lastAnchor)
117 FW_LOGV("start with lastAnchor = %s\n", lastAnchor);
119 SA_ErrorType errorType = SA_INTERNAL_OK;
122 errorType = SA_INTERNAL_NOT_DEFINED;
127 errorType = SA_INTERNAL_NOT_DEFINED;
131 anchor->lastAnchor = lastAnchor;
137 FW_LOGE("error :%d", errorType);
141 SA_ErrorType set_next_anchor(Anchor *anchor, char *nextAnchor)
143 FW_LOGV("start with nextAnchor = %s\n", nextAnchor);
145 SA_ErrorType errorType = SA_INTERNAL_OK;
148 errorType = SA_INTERNAL_NOT_DEFINED;
153 errorType = SA_INTERNAL_NOT_DEFINED;
157 anchor->nextAnchor = nextAnchor;
163 FW_LOGE("error :%d", errorType);
167 void free_anchor(Anchor *pAnchor)
174 if (pAnchor->lastAnchor) {
175 free(pAnchor->lastAnchor);
176 pAnchor->lastAnchor = NULL;
179 if (pAnchor->nextAnchor) {
180 free(pAnchor->nextAnchor);
181 pAnchor->nextAnchor = NULL;
192 SA_ErrorType create_location(char *locURI, char *locName, Location **pLocation)
194 FW_LOGV("start with locURI = %s, locName = %s\n", locURI, locName);
196 SA_ErrorType errorType = SA_INTERNAL_OK;
199 errorType = SA_INTERNAL_NOT_DEFINED;
203 *pLocation = (Location *) calloc(1, sizeof(Location));
204 /* calloc된 *pLocation에 대한 null check로 변경
205 * 2011.09.28 seokgil kang
208 errorType = SA_INTERNAL_NO_MEMORY;
212 (*pLocation)->locURI = strdup(locURI);
215 (*pLocation)->locName = strdup(locName);
221 FW_LOGE("error :%d", errorType);
225 Location *dup_location(Location* pLocation)
229 SA_ErrorType errorType = SA_INTERNAL_OK;
232 errorType = SA_INTERNAL_NOT_DEFINED;
236 Location *location = NULL;
237 errorType = create_location(pLocation->locURI, pLocation->locName, &location);
238 if (errorType != SA_INTERNAL_OK)
246 FW_LOGE("error :%d", errorType);
251 char *get_location_locname(Location *location)
256 return location->locName;
259 char *get_location_locuri(Location *location)
264 return location->locURI;
267 void free_location(Location *loc)
274 FW_LOGV("loc->locURI = %s", loc->locURI);
278 FW_LOGV("loc->locName = %s", loc->locName);
290 SA_ErrorType create_cred(char *userName, char *pwd, AuthType authType, FormatType formatType , char *data, Cred **cred)
294 SA_ErrorType errorType = SA_INTERNAL_OK;
296 if (userName == NULL || !strlen(userName)) {
297 errorType = SA_INTERNAL_NOT_DEFINED;
300 if (pwd == NULL || !strlen(pwd)) {
301 errorType = SA_INTERNAL_NOT_DEFINED;
305 errorType = SA_INTERNAL_NOT_DEFINED;
309 *cred = (Cred *)calloc(1, sizeof(Cred));
311 errorType = SA_INTERNAL_NO_MEMORY;
315 (*cred)->type = authType;
316 (*cred)->format = formatType;
317 (*cred)->username = strdup(userName);
318 (*cred)->password = strdup(pwd);
320 (*cred)->data = strdup(data);
326 FW_LOGE("error :%d", errorType);
331 void free_cred(Cred *cred)
343 if (cred->username) {
344 free(cred->username);
345 cred->username = NULL;
348 if (cred->password) {
349 free(cred->password);
350 cred->password = NULL;
361 void free_chal(Chal *chal)
368 if (chal->nonce_plain) {
369 free(chal->nonce_plain);
370 chal->nonce_plain = NULL;
373 if (chal->nonce_b64) {
374 free(chal->nonce_b64);
375 chal->nonce_b64 = NULL;
385 Cred *create_cred_with_data(AuthType authType, char *data)
389 SA_ErrorType errorType = SA_INTERNAL_OK;
391 Cred *cred = (Cred *)calloc(1, sizeof(Cred));
393 errorType = SA_INTERNAL_NO_MEMORY;
397 cred->type = authType;
399 cred->data = strdup(data);
405 FW_LOGE("error :%d", errorType);
410 void set_cred_format_type(Cred *cred, FormatType formatType)
415 cred->format = formatType;
418 SA_ErrorType create_syncml(SyncHdr *syncHdr, GList *status, GList *commands, int isFinal , SyncML **pSyncML)
422 SA_ErrorType errorType = SA_INTERNAL_OK;
425 errorType = SA_INTERNAL_NOT_DEFINED;
429 *pSyncML = (SyncML *) calloc(1, sizeof(SyncML));
431 if (*pSyncML == NULL) {
432 errorType = SA_INTERNAL_NO_MEMORY;
436 (*pSyncML)->hdr = syncHdr ;
437 (*pSyncML)->status = status;
438 (*pSyncML)->commands = commands;
439 (*pSyncML)->final = isFinal;
445 FW_LOGE("error :%d", errorType);
450 void free_syncml(SyncML *syncML)
457 free_synchdr(syncML->hdr);
460 free_statuses(syncML->status);
461 syncML->status = NULL;
463 free_commands(syncML->commands);
464 syncML->commands = NULL;
473 SA_ErrorType create_synchdr(Session *session , SyncHdr **pSyncHdr)
477 SA_ErrorType errorType = SA_INTERNAL_OK;
479 if (!session->protocolVersion) {
480 errorType = SA_INTERNAL_NOT_DEFINED;
484 if (!session->protocolType) {
485 errorType = SA_INTERNAL_NOT_DEFINED;
489 if (!session->source) {
490 errorType = SA_INTERNAL_NOT_DEFINED;
494 if (!session->target) {
495 errorType = SA_INTERNAL_NOT_DEFINED;
499 *pSyncHdr = (SyncHdr *)calloc(1, sizeof(SyncHdr));
501 if (*pSyncHdr == NULL) {
502 errorType = SA_INTERNAL_NO_MEMORY;
506 (*pSyncHdr)->version = session->protocolVersion;
507 (*pSyncHdr)->protocol = session->protocolType;
508 (*pSyncHdr)->target = dup_location(session->target);
509 (*pSyncHdr)->source = dup_location(session->source);
512 (*pSyncHdr)->cred = dup_cred(session->cred);
514 if (session->sessionID)
515 (*pSyncHdr)->sessionID = strdup(session->sessionID); /*free*/
517 (*pSyncHdr)->messageID = ++session->msgID;
519 (*pSyncHdr)->maxmsgsize = session->sourceMaxMsgSize;
520 (*pSyncHdr)->maxobjsize = session->sourceMaxObjSize;
527 FW_LOGE("error :%d", errorType);
531 void free_synchdr(SyncHdr *syncHdr)
538 if (syncHdr->sessionID) {
539 free(syncHdr->sessionID);
540 syncHdr->sessionID = NULL;
543 if (syncHdr->responseURI) {
544 free(syncHdr->responseURI);
545 syncHdr->responseURI = NULL;
548 if (syncHdr->source) {
549 free_location(syncHdr->source);
550 syncHdr->source = NULL;
553 if (syncHdr->target) {
554 free_location(syncHdr->target);
555 syncHdr->target = NULL;
559 free_cred(syncHdr->cred);
560 syncHdr->cred = NULL;
574 SA_ErrorType errorType = SA_INTERNAL_OK;
576 Item *item = (Item *)calloc(1, sizeof(Item));
578 errorType = SA_INTERNAL_NO_MEMORY;
582 item->dataType = ITEM_UNKNOWN;
588 FW_LOGE("error :%d", errorType);
592 Item *create_item_for_data(const char *data, unsigned int size)
596 SA_ErrorType errorType = SA_INTERNAL_OK;
598 Item *item = create_item();
600 errorType = SA_INTERNAL_NO_MEMORY;
604 item->dataType = ITEM_DATA;
606 item->private.data = strdup(data);
614 FW_LOGE("error :%d", errorType);
618 Item *create_item_for_devinf(DevInf *devInf)
622 SA_ErrorType errorType = SA_INTERNAL_OK;
625 errorType = SA_INTERNAL_NOT_DEFINED;
629 Item *item = create_item();
631 errorType = SA_INTERNAL_NO_MEMORY;
635 item->dataType = ITEM_DEVINF;
636 item->private.devInf = devInf;
642 FW_LOGE("error :%d", errorType);
646 void set_item_target(Item *item, Location *target)
649 item->target = target;
652 void set_item_source(Item *item, Location *source)
655 item->source = source;
658 void free_item(Item *item)
666 free_location(item->source);
671 free_location(item->target);
676 free_anchor(item->anchor);
680 switch (item->dataType) {
682 free(item->private.data);
685 /*devinf is pointed from session. so doesnot need to free here*/
686 item->private.devInf = NULL;
693 if (item->contenttype) {
694 free(item->contenttype);
695 item->contenttype = NULL;
706 Chal *dup_chal(Chal *pChal)
711 FW_LOGV("pChal is null");
715 Chal *temp = (Chal *)calloc(1, sizeof(Chal));
718 temp->type = pChal->type;
721 temp->format = pChal->format;
723 if (pChal->nonce_b64)
724 temp->nonce_b64 = strdup(pChal->nonce_b64);
726 if (pChal->nonce_length)
727 temp->nonce_length = pChal->nonce_length;
729 if (pChal->nonce_plain)
730 temp->nonce_plain = strdup(pChal->nonce_plain);
737 Cred *dup_cred(Cred *pCred)
742 FW_LOGV("pCred is null");
746 Cred *temp = (Cred *)calloc(1, sizeof(Cred));
749 temp->type = pCred->type;
752 temp->format = pCred->format;
755 temp->username = strdup(pCred->username);
758 temp->password = strdup(pCred->password);
761 temp->data = strdup(pCred->data);
768 SA_ErrorType compare_cred(Cred *hdrCred, Cred *sessionCred)
772 SA_ErrorType errorType = SA_INTERNAL_OK;
775 errorType = SA_INTERNAL_NOT_DEFINED;
780 errorType = SA_INTERNAL_NOT_DEFINED;
784 if (strcmp(hdrCred->data, sessionCred->data) == 0)
785 errorType = SA_INTERNAL_OK;
787 errorType = SA_INTERNAL_AUTHENTICATION_ERROR;
791 FW_LOGE("error :%d", errorType);
795 AuthType convert_auth_type(char *authType)
798 return AUTH_TYPE_UNKNOWN;
800 if (!strcmp(authType, ELEMENT_AUTH_BASIC)) {
801 return AUTH_TYPE_BASIC;
802 } else if (!strcmp(authType, ELEMENT_AUTH_MD5)) {
803 return AUTH_TYPE_MD5;
806 return AUTH_TYPE_UNKNOWN;
809 FormatType convert_format_type(char *formatType)
812 return FORMAT_TYPE_UNKNOWN;
814 if (!strcmp(formatType, ELEMENT_FORMAT_BASE64)) {
815 return FORMAT_TYPE_BASE64;
818 return FORMAT_TYPE_UNKNOWN;