1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 //******************************************************************
22 // COPYRIGHT AND PERMISSION NOTICE
24 // Copyright (c) 1996 - 2015, Daniel Stenberg, daniel@haxx.se.
26 // All rights reserved.
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.
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.
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 //******************************************************************
40 #include "HueConnector.h"
41 #include <curl/curl.h>
46 using namespace OIC::Service;
48 HueConnector::HueConnector()
53 HueConnector::~HueConnector()
58 void HueConnector::connect()
63 void HueConnector::disconnect()
68 std::string HueConnector::transmit(std::string target, std::string payload)
70 std::cout << "Transmitting to " << target << " " << payload << endl;
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");
78 const char *cstr = payload.c_str();
80 curl = curl_easy_init();
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");
88 /* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
90 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(cstr));
92 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
94 /* Perform the request, res will get the return code */
95 res = curl_easy_perform(curl);
96 /* Check for errors */
98 fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
101 curl_easy_cleanup(curl);
106 static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
108 buffer_in->append(data, size * nmemb);
112 std::string HueConnector::read(std::string target)
114 std::cout << "Reading from to " << target << endl;
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");
122 curl = curl_easy_init();
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");
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);
135 /* Perform the request, res will get the return code */
136 res = curl_easy_perform(curl);
137 /* Check for errors */
140 fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
144 cout << "Response is: " << response << endl;
148 curl_easy_cleanup(curl);