Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / examples / HueSampleBundle / src / HueConnector.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 //******************************************************************
22 // COPYRIGHT AND PERMISSION NOTICE
23 //
24 // Copyright (c) 1996 - 2015, Daniel Stenberg, daniel@haxx.se.
25 //
26 // All rights reserved.
27 //
28 // Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted,
29 // provided that the above copyright notice and this permission notice appear in all copies.
30 //
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
32 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS
33 // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36 // Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the
37 // sale, use or other dealings in this Software without prior written authorization of the copyright holder.
38 //******************************************************************
39
40 #include "HueConnector.h"
41 #include <curl/curl.h>
42 #include <string.h>
43 #include <iostream>
44
45 using namespace std;
46 using namespace OIC::Service;
47
48 HueConnector::HueConnector()
49 {
50
51 }
52
53 HueConnector::~HueConnector()
54 {
55
56 }
57
58 void HueConnector::connect()
59 {
60
61 }
62
63 void HueConnector::disconnect()
64 {
65
66 }
67
68 std::string HueConnector::transmit(std::string target, std::string payload)
69 {
70     std::cout << "Transmitting to " << target << " " << payload << endl;
71     CURL *curl;
72     CURLcode res;
73     struct curl_slist *headers = NULL; /* http headers to send with request */
74     /* set content type */
75     headers = curl_slist_append(headers, "Accept: application/json");
76     headers = curl_slist_append(headers, "Content-Type: application/json");
77
78     const char *cstr = payload.c_str();
79
80     curl = curl_easy_init();
81
82     if (curl)
83     {
84         curl_easy_setopt(curl, CURLOPT_URL, target.c_str());
85         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cstr);
86         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
87
88         /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
89          itself */
90         curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(cstr));
91
92         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
93
94         /* Perform the request, res will get the return code */
95         res = curl_easy_perform(curl);
96         /* Check for errors */
97         if (res != CURLE_OK)
98             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
99
100         /* always cleanup */
101         curl_easy_cleanup(curl);
102     }
103     return "";
104 }
105
106 static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
107 {
108     buffer_in->append(data, size * nmemb);
109     return size * nmemb;
110 }
111
112 std::string HueConnector::read(std::string target)
113 {
114     std::cout << "Reading from to " << target << endl;
115     CURL *curl;
116     CURLcode res;
117     struct curl_slist *headers = NULL; /* http headers to send with request */
118     /* set content type */
119     headers = curl_slist_append(headers, "Accept: application/json");
120     headers = curl_slist_append(headers, "Content-Type: application/json");
121
122     curl = curl_easy_init();
123
124     if (curl)
125     {
126         curl_easy_setopt(curl, CURLOPT_URL, target.c_str());
127         curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
128         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
129
130         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
131         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
132         std::string response;
133         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
134
135         /* Perform the request, res will get the return code */
136         res = curl_easy_perform(curl);
137         /* Check for errors */
138         if (res != CURLE_OK)
139         {
140             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
141         }
142         else
143         {
144             cout << "Response is: " << response << endl;
145         }
146
147         /* always cleanup */
148         curl_easy_cleanup(curl);
149     }
150     return "";
151 }
152