-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpianosteps.cpp
More file actions
276 lines (192 loc) · 7.26 KB
/
Copy pathpianosteps.cpp
File metadata and controls
276 lines (192 loc) · 7.26 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "pianosteps.h"
/**
* Clears the request object from its allocation
*/
inline void clearRequest(PianoRequest_t request){
if(request.responseData != NULL){
free(request.responseData);
}
PianoDestroyRequest(&request);
}
PianoSteps::PianoSteps()
{
//Default constructor
}
void PianoSteps::PianoInitialize(PianoHandle_t* pianoHandle, WaitressHandle_t* waitressHandle)
{
//Initialize the gnutls library, otherwise wierd things start to happen
gnutls_global_init();
//Constants for Piano Init
const char *user = "android";
const char *password = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7";
const char *device = "android-generic";
const char *in = "R=U!LH$O2B#";
const char *out = "6#26FRL$ZWD";
//Initialize PianoHandle
//TODO: DOCUMENT METHOD IN LIBPIANOBAR
PianoInit(pianoHandle, user, password, device, in, out);
//Initialize WaitressHandle
//TODO: DOCUMENT METHOD IN LIBWAITRESS
WaitressInit(waitressHandle);
waitressHandle->url.host = PIANO_RPC_HOST;
waitressHandle->tlsFingerprint = TLS_FINGERPRINT;
}
//TODO: Allow for dynamic username and passwords
bool PianoSteps::PianoLogin(PianoHandle_t* pianoHandle, WaitressHandle_t* waitressHandle, char* username, char* password)
{
PianoReturn_t pianoRet;
WaitressReturn_t waitRet;
//Store the username and password in so
//they can be reused for timeout
lastKnownPassword = password;
lastKnownUsername = username;
PianoRequestDataLogin_t loginReq;
loginReq.user = username;
loginReq.password = password;
loginReq.step = 0;
//ui.c:160
PianoRequest_t request;
memset(&request, 0, sizeof(request));
//This is in a loop because for logging into the system you need to go twice
do{
request.data = (void *)&loginReq;
//Send the login request to libpiano
pianoRet = PianoRequest (pianoHandle, &request, PIANO_REQUEST_LOGIN);
qDebug() << "LOGIN: Pianobar Request sent";
if(pianoRet != PIANO_RET_OK){
qDebug() << "Problem Encountered, piano returned not ok";
}else{
qDebug() << "Piano Returned OK, moving on";
}
//Send the login request to libwaitress
waitRet = BarPianoHttpRequest(waitressHandle, &request);
qDebug() << "LOGIN: Libwaitress Request sent";
if(waitRet != WAITRESS_RET_OK){
qDebug() << "Problem Encountered, waitress returned not ok" ;
}else{
qDebug() << "Waitress Returned OK, moving on";
}
//Analyze the response from libpiano
pianoRet = PianoResponse(pianoHandle, &request);
qDebug() << "LOGIN: Reponse sent to libpiano";
if(pianoRet != PIANO_RET_CONTINUE_REQUEST){
if(pianoRet == PIANO_RET_P_INVALID_AUTH_TOKEN){
qDebug() << "Invalid Auth Token";
}else if(pianoRet == PIANO_RET_OK){
qDebug() << "Everything whent ok";
}
}
clearRequest(request);
} while(pianoRet == PIANO_RET_CONTINUE_REQUEST);
if(pianoRet == PIANO_RET_P_INVALID_PARTNER_LOGIN){
qDebug() << "Invalid user credentials";
return false;
}else if(pianoRet == PIANO_RET_OK){
qDebug() << "Valid credentials";
return true;
}
qDebug() << "Done with login";
return false;
}
void PianoSteps::PianoGetStations(PianoHandle_t* pianoHandle, WaitressHandle_t* waitressHandle)
{
PianoReturn_t pianoRet;
WaitressReturn_t waitRet;
PianoRequest_t request;
memset(&request, 0, sizeof(request));
do{
pianoRet = PianoRequest (pianoHandle, &request, PIANO_REQUEST_GET_STATIONS);
if(pianoRet != PIANO_RET_OK){
qDebug() << "Problem Encountered, piano returned not ok";
}else{
qDebug() << "Piano Returned OK, moving on";
}
waitRet = BarPianoHttpRequest(waitressHandle, &request);
if(waitRet != WAITRESS_RET_OK){
qDebug() << "Problem Encountered, waitress returned not ok";
}else{
qDebug() << "Waitress Returned OK, moving on";
}
pianoRet = PianoResponse(pianoHandle, &request);
if(pianoRet == PIANO_RET_P_INVALID_PARTNER_LOGIN){
PianoLogin(pianoHandle, waitressHandle, lastKnownUsername, lastKnownPassword);
pianoRet = PIANO_RET_CONTINUE_REQUEST;
}
clearRequest(request);
} while(pianoRet == PIANO_RET_CONTINUE_REQUEST);
qDebug() << "Done getting stations";
}
//Code from ui.c:175
WaitressReturn_t PianoSteps::BarPianoHttpRequest(WaitressHandle_t* waitressHandle, PianoRequest_t* request)
{
waitressHandle->extraHeaders = "Content-Type: text/plain\r\n";
waitressHandle->postData = request->postData;
waitressHandle->method = WAITRESS_METHOD_POST;
waitressHandle->url.path = request->urlPath;
waitressHandle->url.tls = request->secure;
return WaitressFetchBuf(waitressHandle, &request->responseData);
}
PianoSong_t* PianoSteps::PianoGetPlaylist(PianoHandle_t* pianoHandle, WaitressHandle_t* waitressHandle, PianoStation_t* station)
{
PianoReturn_t pianoRet;
WaitressReturn_t waitRet;
PianoRequestDataGetPlaylist_t playlistReq;
playlistReq.station = station;
//TODO: Make this adjustable by some sort of settings
playlistReq.format = PIANO_AF_MP3;
PianoRequest_t request;
memset(&request, 0, sizeof(request));
do{
request.data = (void *)&playlistReq;
pianoRet = PianoRequest (pianoHandle, &request, PIANO_REQUEST_GET_PLAYLIST);
if(pianoRet != PIANO_RET_OK){
qDebug() << "Problem Encountered, piano returned not ok";
}else{
qDebug() << "Piano Returned OK, moving on";
}
waitRet = BarPianoHttpRequest(waitressHandle, &request);
if(waitRet != WAITRESS_RET_OK){
qDebug() << "Problem Encountered, waitress returned not ok";
}else{
qDebug() << "Waitress Returned OK, moving on";
}
pianoRet = PianoResponse(pianoHandle, &request);
if(pianoRet == PIANO_RET_P_INVALID_PARTNER_LOGIN){
PianoLogin(pianoHandle, waitressHandle, lastKnownUsername, lastKnownPassword);
pianoRet = PIANO_RET_CONTINUE_REQUEST;
}
clearRequest(request);
} while(pianoRet == PIANO_RET_CONTINUE_REQUEST);
return playlistReq.retPlaylist;
}
void PianoSteps::PianoRateSong(PianoHandle_t* pianoHandle, WaitressHandle_t* waitressHandle, PianoSong_t* song, PianoSongRating_t rating)
{
PianoReturn_t pianoRet;
WaitressReturn_t waitRet;
PianoRequestDataRateSong_t songRateReq;
songRateReq.song = song;
songRateReq.rating = rating;
PianoRequest_t request;
memset(&request, 0, sizeof(request));
do{
request.data = (void *)&songRateReq;
pianoRet = PianoRequest (pianoHandle, &request, PIANO_REQUEST_GET_PLAYLIST);
if(pianoRet != PIANO_RET_OK){
qDebug() << "Problem Encountered, piano returned not ok";
}else{
qDebug() << "Piano Returned OK, moving on";
}
waitRet = BarPianoHttpRequest(waitressHandle, &request);
if(waitRet != WAITRESS_RET_OK){
qDebug() << "Problem Encountered, waitress returned not ok";
}else{
qDebug() << "Waitress Returned OK, moving on";
}
pianoRet = PianoResponse(pianoHandle, &request);
if(pianoRet == PIANO_RET_P_INVALID_PARTNER_LOGIN){
PianoLogin(pianoHandle, waitressHandle, lastKnownUsername, lastKnownPassword);
pianoRet = PIANO_RET_CONTINUE_REQUEST;
}
clearRequest(request);
} while(pianoRet == PIANO_RET_CONTINUE_REQUEST);
}