-
Notifications
You must be signed in to change notification settings - Fork 14
/
servercomm.c
223 lines (204 loc) · 7.32 KB
/
servercomm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// servercomm.c
// SqueezeButtonPi
//
// Send CLI commands to the server
//
// Created by Jörg Schwieder on 02.02.17.
//
//
// Copyright (c) 2017, Joerg Schwieder, PenguinLovesMusic.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of ickStream nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include "servercomm.h"
#include "sbpd.h"
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
//
// lock for asynchronous sending of commands - we don't do this right now
//
/*#include <pthread.h>
static volatile bool commLock;
static pthread_mutex_t lock;*/
static CURL *curl;
static char * MAC = NULL;
static struct curl_slist * headerList = NULL;
#define JSON_CALL_MASK "{\"id\":%ld,\"method\":\"slim.request\",\"params\":[\"%s\",%s]}"
#define SERVER_ADDRESS_TEMPLATE "http://localhost/jsonrpc.js"
//
//
// Send CLI command fragment to Logitech Media Server/Squeezebox Server
// This command blocks (synchronously communicates".
// In timing critical situations this should be called from a separate thread
//
// Parameters:
// server: the server information structure defining host, port etc.
// frament: the command fragment to be sent as JSON array
// e.g. "[\"mixer\”,\"volume\",\"+2\"]"
// optionally: some CLI commands can take parameter hashes as "params:{}"
// Returns: success flag
//
//
bool send_command(struct sbpd_server * server, int command, char * fragment) {
loginfo("Send Command:%d, Fragment:%s", command, fragment);
if ( command == LMS ) {
if (!curl)
return false;
//
// Sending commands asynchronously? Secure with a mutex
// But right now we are actually not doing that, so comment out
//
/*pthread_mutex_lock(&lock);
if (commLock) {
pthread_mutex_unlock(&lock);
return false;
}
commLock = true;
pthread_mutex_unlock(&lock);*/
//
// target setup. We call an IPv4 ip so we need to replace a default host
//
struct curl_slist * targetList = NULL;
curl_easy_setopt(curl, CURLOPT_URL, SERVER_ADDRESS_TEMPLATE);
char target[100];
snprintf(target, sizeof(target), "::%s:%d", server->host, server->port);
//logdebug("Command Target: %s", target);
targetList = curl_slist_append(targetList, target);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_CONNECT_TO, targetList);
// Setup an error buffer to log errors
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
errbuf[0] = 0;
//
// username/password?
//
char secret[255];
if (server->user && server->password) {
snprintf(secret, sizeof(secret), "%s:%s", server->user, server->password);
curl_easy_setopt(curl, CURLOPT_USERPWD, secret);
}
//
// setup payload (JSON/RPC CLI command) for POST command
//
char jsonFragment[256];
snprintf(jsonFragment, sizeof(jsonFragment), JSON_CALL_MASK, 1l, MAC, fragment);
logdebug("Server %s command: %s", target, jsonFragment);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonFragment);
if (headerList)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList);
//
// Send command and clean up
// Note: one could retrieve a result here since all communication is synchronous!
//
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
size_t len = strlen(errbuf);
loginfo("Curl Error: (%d) ", res);
if(len)
loginfo( "%s%s", errbuf,((errbuf[len - 1] != '\n') ? "\n" : ""));
else
loginfo( "%s\n", curl_easy_strerror(res));
}
curl_slist_free_all(targetList);
targetList = NULL;
//commLock = false;
return true;
} else if ( command == SCRIPT ) {
char * cmdline = (char *) malloc(strlen(fragment));
int err;
strcpy( cmdline, fragment);
loginfo("Sending commandline: %s\n", cmdline);
if ((err = system(cmdline)) != 0){
loginfo ("%s exit status = %d\n", cmdline, err);
return false;
}
free (cmdline);
}
return true;
}
//
// Curl reply callback
// Replies from the server go here.
// We could handle return values here but we just log.
//
size_t write_data(char *buffer, size_t size, size_t nmemb, void *userp) {
if (size)
logdebug("Server reply %s", buffer);
return size * nmemb;
}
//
//
// Initialize CURL for server communication and set MAC address
//
//
int init_comm(char * use_mac) {
loginfo("Initializing CURL");
MAC = use_mac;
//
// Setup mutex lock for comm - unused
//
/*if (pthread_mutex_init(&lock, NULL) != 0) {
logerr("comm lock mutex init failed");
return -1;
}*/
//
// Initialize curl comm
//
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
curl_global_cleanup();
return -1;
}
//
// Set verbose mode for communication debugging
//
if (loglevel() == LOG_DEBUG)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
headerList = curl_slist_append(headerList, "Content-Type: application/json");
char userAgent[50];
snprintf(userAgent, sizeof(userAgent), "User-Agent: %s/%s)", USER_AGENT, VERSION);
headerList = curl_slist_append(headerList, userAgent);
//
// Add session-ID? Only needed for MySB which is not supported
//
//headerList = curl_slist_append(headerList, "x-sdi-squeezenetwork-session: ...")
return 0;
}
//
//
// Shutdown CURL
//
//
void shutdown_comm() {
curl_slist_free_all(headerList);
curl_easy_cleanup(curl);
curl_global_cleanup();
}