Imported Upstream version 1.2.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         {
99             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
100         }
101
102         /* always cleanup */
103         curl_easy_cleanup(curl);
104     }
105     return "";
106 }
107
108 static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
109 {
110     buffer_in->append(data, size * nmemb);
111     return size * nmemb;
112 }
113
114 std::string HueConnector::read(std::string target)
115 {
116     std::cout << "Reading from to " << target << endl;
117     CURL *curl;
118     CURLcode res;
119     struct curl_slist *headers = NULL; /* http headers to send with request */
120     /* set content type */
121     headers = curl_slist_append(headers, "Accept: application/json");
122     headers = curl_slist_append(headers, "Content-Type: application/json");
123
124     curl = curl_easy_init();
125
126     if (curl)
127     {
128         curl_easy_setopt(curl, CURLOPT_URL, target.c_str());
129         curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
130         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
131
132         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
133         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
134         std::string response;
135         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
136
137         /* Perform the request, res will get the return code */
138         res = curl_easy_perform(curl);
139         /* Check for errors */
140         if (res != CURLE_OK)
141         {
142             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
143         }
144         else
145         {
146             cout << "Response is: " << response << endl;
147         }
148
149         /* always cleanup */
150         curl_easy_cleanup(curl);
151     }
152     return "";
153 }
154