Support HighQoS and Cancel Observe in CA and RI
[platform/upstream/iotivity.git] / resource / src / OCUtilities.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <OCApi.h>
22
23 #include <OCUtilities.h>
24
25 #include <boost/algorithm/string.hpp>
26
27 #include <sstream>
28 #include <iterator>
29 #include <algorithm>
30
31 OC::Utilities::QueryParamsKeyVal OC::Utilities::getQueryParams(const std::string& uri)
32 {
33     OC::Utilities::QueryParamsKeyVal qp;
34     if(uri.empty())
35     {
36         return qp;
37     }
38
39     std::vector<std::string> queryparams;
40     boost::split(queryparams, uri, [](const char c){return c=='&';},
41     boost::token_compress_on);
42
43     for(std::string& it: queryparams)
44     {
45         auto index = it.find('=');
46
47         if(index == std::string::npos)
48         {
49             qp[it] = "";
50         }
51         else
52         {
53             qp[it.substr(0, index)] = it.substr(index + 1);
54         }
55     }
56         return qp;
57     }
58
59 namespace OC {
60
61 OCStackResult result_guard(const OCStackResult r)
62 {
63  std::ostringstream os;
64
65  switch(r)
66  {
67     default:
68         os << "result_guard(): unhandled exception: " << OCException::reason(r);
69         throw OCException(os.str(), r);
70
71     /* Exceptional conditions: */
72     case OC_STACK_NO_MEMORY:
73     case OC_STACK_COMM_ERROR:
74     case OC_STACK_NOTIMPL:
75     case OC_STACK_INVALID_URI:
76     case OC_STACK_INVALID_QUERY:
77     case OC_STACK_INVALID_IP:
78     case OC_STACK_INVALID_PORT:
79     case OC_STACK_INVALID_CALLBACK:
80     case OC_STACK_INVALID_METHOD:
81     case OC_STACK_INVALID_PARAM:
82     case OC_STACK_INVALID_OBSERVE_PARAM:
83         os << "result_guard(): " << r << ": " << OCException::reason(r);
84         throw OCException(os.str(), r);
85
86     /* Non-exceptional failures or success: */
87     case OC_STACK_OK:
88     case OC_STACK_NO_RESOURCE:
89     case OC_STACK_RESOURCE_ERROR:
90     case OC_STACK_SLOW_RESOURCE:
91     case OC_STACK_NO_OBSERVERS:
92     case OC_STACK_OBSERVER_NOT_FOUND:
93 #ifdef WITH_PRESENCE
94     case OC_STACK_PRESENCE_STOPPED:
95     case OC_STACK_PRESENCE_TIMEOUT:
96     case OC_STACK_PRESENCE_DO_NOT_HANDLE:
97 #endif
98     break;
99  }
100
101  return r;
102 }
103
104 } // namespace OC
105