00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2012, Worcester Polytechnic Institute 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Worcester Polytechnic Institute nor the 00019 * names of its contributors may be used to endorse or promote 00020 * products derived from this software without specific prior 00021 * written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00026 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00027 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00028 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00029 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00033 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00034 * POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 *********************************************************************/ 00037 00048 #include <rovio_shared/rovio_http.h> 00049 00050 using namespace std; 00051 00052 rovio_http::rovio_http(string user, string pass) 00053 { 00054 // create the CURL handle 00055 curl = curl_easy_init(); 00056 if (curl == NULL) 00057 { 00058 ROS_ERROR("Curl was unable to initialize."); 00059 exit(-1); 00060 } 00061 00062 // set the username and password 00063 curl_easy_setopt(curl, CURLOPT_USERPWD, user.append(":").append(pass).c_str()); 00064 // set the pointer to the function which handles the responses 00065 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data); 00066 00067 // create the semaphore 00068 sem_init(&sem, 0, 1); 00069 } 00070 00071 rovio_http::~rovio_http() 00072 { 00073 // cleanup anything left by Curl 00074 curl_easy_cleanup(curl); 00075 // destroy the semaphore 00076 sem_destroy(&sem); 00077 } 00078 00079 rovio_response *rovio_http::send(const char *url) 00080 { 00081 // wait for the curl handle to be free 00082 sem_wait(&sem); 00083 00084 // create the response for the Rovio 00085 rovio_response *resp = (rovio_response *)malloc(sizeof(rovio_response)); 00086 resp->size = 0; 00087 resp->data = NULL; 00088 curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp); 00089 00090 //send the command to the Rovio 00091 curl_easy_setopt(curl, CURLOPT_URL, url); 00092 curl_easy_perform(curl); 00093 00094 sem_post(&sem); 00095 00096 return resp; 00097 } 00098 00099 size_t write_data(char *ptr, size_t size, size_t nmemb, rovio_response *buf) 00100 { 00101 // actual size of the new data 00102 size_t new_data = size * nmemb; 00103 00104 // see if there is any data 00105 if (new_data > 0) 00106 { 00107 00108 // check if the buffer already has data 00109 if (buf->data) 00110 // resize the buffer 00111 buf->data = (char *)realloc(buf->data, buf->size + new_data + 1); 00112 else 00113 // allocate the initial memory 00114 buf->data = (char *)malloc(new_data + 1); 00115 00116 // add the data to the buffer 00117 memcpy(&(buf->data[buf->size]), ptr, new_data); 00118 //update the size 00119 buf->size += new_data; 00120 // null terminate 00121 buf->data[buf->size] = '\0'; 00122 } 00123 00124 return new_data; 00125 } 00126 00127 void rovio_response_clean(rovio_response *resp) 00128 { 00129 // check if the response is null 00130 if (resp) 00131 { 00132 // free the data (if any) 00133 if (resp->data) 00134 free(resp->data); 00135 // free the struct 00136 free(resp); 00137 } 00138 }