1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #define _POSIX_C_SOURCE 200112L
23 #include "srmutility.h"
24 #include "srmresourcestrings.h"
26 #include "oic_malloc.h"
29 #define TAG "OIC_SRM_UTILITY"
31 void ParseQueryIterInit(const unsigned char * query, OicParseQueryIter_t * parseIter)
33 OIC_LOG(INFO, TAG, "Initializing coap iterator");
34 if ((NULL == query) || (NULL == parseIter))
39 parseIter->attrPos = NULL;
40 parseIter->attrLen = 0;
41 parseIter->valPos = NULL;
42 parseIter->valLen = 0;
43 coap_parse_iterator_init((unsigned char *)query, strlen((char *)query),
44 (unsigned char *)OIC_SEC_REST_QUERY_SEPARATOR,
45 (unsigned char *) "", 0, &parseIter->pi);
48 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter)
50 OIC_LOG(INFO, TAG, "Getting Next Query");
51 if (NULL == parseIter)
56 unsigned char * qrySeg = NULL;
59 // Get the next query. Querys are separated by OIC_SEC_REST_QUERY_SEPARATOR.
60 qrySeg = coap_parse_next(&parseIter->pi);
64 delimPos = strchr((char *)qrySeg, OIC_SEC_REST_QUERY_DELIMETER);
67 parseIter->attrPos = parseIter->pi.pos;
68 parseIter->attrLen = (unsigned char *)delimPos - parseIter->pi.pos;
69 parseIter->valPos = (unsigned char *)delimPos + 1;
70 parseIter->valLen = &qrySeg[parseIter->pi.segment_length] - parseIter->valPos;
78 * Function to getting string of ownership transfer method
80 * @prarm oxmType ownership transfer method
82 * @return string value of ownership transfer method
84 const char* GetOxmString(OicSecOxm_t oxmType)
89 return OXM_JUST_WORKS;
90 case OIC_RANDOM_DEVICE_PIN:
91 return OXM_RANDOM_DEVICE_PIN;
92 case OIC_MANUFACTURER_CERTIFICATE:
93 return OXM_MANUFACTURER_CERTIFICATE;
94 #ifdef _ENABLE_MULTIPLE_OWNER_
95 case OIC_PRECONFIG_PIN:
96 return OXM_PRECONF_PIN;
97 #endif //_ENABLE_MULTIPLE_OWNER_
103 OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid)
105 if(NULL == uuid || NULL == strUuid || NULL != *strUuid)
107 OIC_LOG(ERROR, TAG, "ConvertUuidToStr : Invalid param");
108 return OC_STACK_INVALID_PARAM;
113 const size_t urnBufSize = (UUID_LENGTH * 2) + 4 + 1;
114 char* convertedUrn = (char*)OICCalloc(urnBufSize, sizeof(char));
115 VERIFY_NON_NULL(TAG, convertedUrn, ERROR);
117 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH && urnIdx < urnBufSize; uuidIdx++, urnIdx+=2)
119 // canonical format for UUID has '8-4-4-4-12'
120 if(uuidIdx==4 || uuidIdx==6 || uuidIdx==8 || uuidIdx==10)
122 snprintf(convertedUrn + urnIdx, 2, "%c", '-');
125 snprintf(convertedUrn + urnIdx, 3, "%02x", (uint8_t)(uuid->id[uuidIdx]));
127 convertedUrn[urnBufSize - 1] = '\0';
129 *strUuid = convertedUrn;
133 return OC_STACK_NO_MEMORY;
136 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
138 if(NULL == strUuid || NULL == uuid)
140 OIC_LOG(ERROR, TAG, "ConvertStrToUuid : Invalid param");
141 return OC_STACK_INVALID_PARAM;
146 size_t strUuidLen = 0;
147 char convertedUuid[UUID_LENGTH * 2] = {0};
149 strUuidLen = strlen(strUuid);
152 OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\
153 "\"00000000-0000-0000-0000-000000000000\"");
155 else if(UUID_LENGTH * 2 + 4 == strUuidLen)
157 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
159 if(*(strUuid + urnIdx) == '-')
163 sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
168 OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format");
169 OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)");
170 OIC_LOG(ERROR, TAG, "e.g) \"\"");
172 return OC_STACK_INVALID_PARAM;
175 memcpy(uuid->id, convertedUuid, UUID_LENGTH);