-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotifyInterface.c
More file actions
385 lines (355 loc) · 16.8 KB
/
SpotifyInterface.c
File metadata and controls
385 lines (355 loc) · 16.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include "SpotifyTypedef.h"
#include "SpotifyInterface.h"
#include "SpotifyAPICall.h"
#include "SpotifyHttpLocalServer.h"
#include "SpiffsManger.h"
#include "JsonExtraction.h"
#include "SpotifyTypedef.h"
#include "rtc.h"
#include "JpegDecode.h"
// ****************************** Global Variables
SpotifyInterfaceHandler_t *InterfaceHandler;
SpotifyPrivateHandler_t PrivateHandler;
// ****************************** Local Variables
static const char *TAG = "SpotifyTask";
// ****************************** Local Functions
static void Spotify_MainTask(void *pvparameters);
static bool Spotify_TokenRenew(void);
static bool Spotify_IsTokenExpired();
/**
* @brief This function initiates the Spotify authorization process.
* @param SpotifyInterfaceHandler as the handler
* @return true if task run to the end
*/
bool Spotify_TaskInit(SpotifyInterfaceHandler_t *SpotifyInterfaceHandler)
{
InterfaceHandler = SpotifyInterfaceHandler;
InterfaceHandler->PlaybackInfo = (PlaybackInfo_t *)malloc(sizeof(PlaybackInfo_t));
InterfaceHandler->UserInfo = (UserInfo_t *)malloc(sizeof(UserInfo_t));
InterfaceHandler->CoverPhoto = (uint8_t *)malloc(COVER_PHOTO_SIZE * sizeof(uint8_t));
PrivateHandler.Status = INIT;
if (InterfaceHandler->ConfigAddressInSpiffs != NULL &&
InterfaceHandler->IsSpotifyAuthorizedSemaphore != NULL)
{
StaticTask_t *xTaskBuffer = (StaticTask_t *)malloc(sizeof(StaticTask_t));
StackType_t *xStack = (StackType_t *)malloc(SPOTIFY_TASK_STACK_SIZE * sizeof(StackType_t)); // Assuming a stack size of 400 words (adjust as needed)
if (xTaskBuffer == NULL || xStack == NULL)
{
ESP_LOGE(TAG, "Memory allocation failed!\n");
free(xTaskBuffer);
free(xStack);
return false; // Exit with an error code
}
xTaskCreateStatic(
Spotify_MainTask, // Task function
"Spotify_MainTask", // Task name (for debugging)
SPOTIFY_TASK_STACK_SIZE, // Stack size (in words)
NULL, // Task parameters (passed to the task function)
tskIDLE_PRIORITY + SPOTIFY_PRIORITY, // Task priority (adjust as needed)
xStack, // Stack buffer
xTaskBuffer // Task control block
);
// Allocate buffer and Initialize the Spotify API call
PrivateHandler.SpotifyBuffer.MessageBuffer = (char *)malloc(SUPER_BUF * sizeof(char));
PrivateHandler.SpotifyBuffer.SpotifyResponseReadyFlag = xSemaphoreCreateBinary();
SpotifyAPICallInit(&PrivateHandler.SpotifyBuffer);
ESP_LOGI(TAG, "Spotify app initiated successfully");
}
else
{
ESP_LOGE(TAG, "SpotifyIntefaceHandler is is missing some pointers, can not run the app");
return false;
}
return true;
}
/**
* @brief Run Http local service
*/
bool Spotify_HttpServerServiceInit()
{
SendCodeFromHttpToSpotifyTask = xQueueCreate(1, sizeof(char) * sizeof(char[MEDIUM_BUF]));
httpd_handle_t spotifyLocalServer = Spotify_StartWebServer();
if (spotifyLocalServer == NULL)
{
ESP_LOGE(TAG, "Creating Spotify local server failed!");
return false;
}
bool IsMdnsStarted = Spotify_StartMDNSService();
if (!IsMdnsStarted)
{
ESP_LOGE(TAG, "Running mDNS failed!");
return false;
};
ESP_LOGI(TAG, "** Spotify local server created, mDNS is running! **");
return true;
}
/**
* @brief Deinitiate the Spotify app
*/
void Spotify_TaskDeinit(SpotifyInterfaceHandler_t *SpotifyInterfaceHandler)
{
if (SpotifyInterfaceHandler->PlaybackInfo != NULL)
{
free(SpotifyInterfaceHandler->PlaybackInfo);
}
if (SpotifyInterfaceHandler->UserInfo != NULL)
{
free(SpotifyInterfaceHandler->UserInfo);
}
if (PrivateHandler.SpotifyBuffer.MessageBuffer != NULL)
{
free(PrivateHandler.SpotifyBuffer.MessageBuffer);
}
if (PrivateHandler.SpotifyBuffer.SpotifyResponseReadyFlag != NULL)
{
vSemaphoreDelete(PrivateHandler.SpotifyBuffer.SpotifyResponseReadyFlag);
}
if (SendCodeFromHttpToSpotifyTask != NULL)
{
vQueueDelete(SendCodeFromHttpToSpotifyTask);
}
ESP_LOGI(TAG, "Spotify app deinitiated successfully");
}
/**
* @brief This function is the entry point for handling HTTPS requests for Spotify authorization.
* @param[in] parameters because it is a Task!
*/
static void Spotify_MainTask(void *pvparameters)
{
while (1)
{
char receivedData[MEDIUM_BUF];
switch (PrivateHandler.Status)
{
case INIT:
{
bool IsServerInit = Spotify_HttpServerServiceInit(); // Init the webserver in initialization
if (!IsServerInit)
{
ESP_LOGE(TAG, "Authorization initialization failed!");
break;
}
bool IsSpiffExists = SpiffsExistenceCheck(InterfaceHandler->ConfigAddressInSpiffs); // Check if the refresh token exists in the spiffs
if (!IsSpiffExists)
{
ESP_LOGE(TAG, "Spotify autorization is needed");
PrivateHandler.Status = LOGIN; // If the refresh token does not exist, set the status to LOGIN
break;
}
ESP_LOGI(TAG, "RefreshToken found");
PrivateHandler.Status = EXPIRED; // If the refresh token exists, set the status to EXPIRED
break;
}
case LOGIN:
{
bool IsCodeReceived = xQueueReceive(SendCodeFromHttpToSpotifyTask, receivedData, pdMS_TO_TICKS(SEC)); // Waiting for Code to be recieved by queue
if (!IsCodeReceived)
{
// stay in this state until receive the Code
break;
}
Spotify_SendTokenRequest(receivedData); // send request for Token
PrivateHandler.Status = AUTHENTICATED; // Code received and checked, so update Status to AUTHENTICATED
break;
}
case AUTHENTICATED:
{
ESP_LOGI(TAG, "AUTHENTICATED");
bool IsSpotifyResponded = xSemaphoreTake(PrivateHandler.SpotifyBuffer.SpotifyResponseReadyFlag, pdMS_TO_TICKS(SEC)); // Waiting for Token to be recieved by queue
if (!IsSpotifyResponded)
{
PrivateHandler.Status = LOGIN; // if the response did not come within the expected time, set Status back to LOGIN
ESP_LOGW(TAG, "Timeout - Spotify did not respond within the expected time.!");
break;
}
bool IsTokenExtracted = ExtractAccessTokenParamsTokenFromJson(PrivateHandler.SpotifyBuffer.MessageBuffer,
&PrivateHandler.token
); // extract all keys from spotify server response
if (!IsTokenExtracted)
{
ESP_LOGW(TAG, "Token not found!");
PrivateHandler.Status = LOGIN; // the reponse did not include all needed keys, so set Status back to LOGIN
break;
}
ESP_LOGI(TAG, "Token found!");
PrivateHandler.TokenLastUpdate = xTaskGetTickCount(); // Save the time when the token was received
PrivateHandler.Status = AUTHORIZED; // Token recieved and checked, so update Status to AUTHORIZED
break;
}
case AUTHORIZED:
{
ESP_LOGI(TAG, "AUTHORIZED");
SpiffsRemoveFile(InterfaceHandler->ConfigAddressInSpiffs); // Delete old value at the directory
// so applicaiton can know the Spotify Module is authorized
// and ready for sending commands
SaveFileInSpiffsWithTxtFormat(InterfaceHandler->ConfigAddressInSpiffs, // Save new file in the directory
"refresh_token", PrivateHandler.token.RefreshToken,
NULL, NULL);
xSemaphoreGive((*InterfaceHandler->IsSpotifyAuthorizedSemaphore)); // give IsSpotifyAuthorizedSemaphore semaphore in the IntefaceHandler
PrivateHandler.Status = CHECK_TIME; // set Status to CHECK_TIME
break;
}
case CHECK_TIME:
{
bool IsTokenExpired = Spotify_IsTokenExpired(); // Check if the expiration time has elapsed since the last received token
if (!IsTokenExpired)
{
// keep state
break;
}
PrivateHandler.Status = EXPIRED; // set Status to EXPIRED if token expired
break;
}
case EXPIRED:
{
ESP_LOGW(TAG, "token is expired");
bool IsTokenRenewed = Spotify_TokenRenew();
if (!IsTokenRenewed) // Run function to renew the token
{
PrivateHandler.Status = LOGIN;
break; // set Status to ILDLE if token renewed unsuccessfully
}
xSemaphoreGive((*InterfaceHandler->IsSpotifyAuthorizedSemaphore)); // give IsSpotifyAuthorizedSemaphore semaphore in the IntefaceHandler
PrivateHandler.TokenLastUpdate = xTaskGetTickCount(); // Save the time when the token was received
PrivateHandler.Status = CHECK_TIME; // set Status to CHECK_TIME if token renewed successfully
break;
}
}
vTaskDelay(pdMS_TO_TICKS(100)); // Task delay at the end of while(1) loop
}
}
/**
* @brief This function check Time for
* @return true if token expired, false otherwise
* */
static bool Spotify_IsTokenExpired()
{
bool tokenExpired = false;
uint32_t elapsedTime = (xTaskGetTickCount() - PrivateHandler.TokenLastUpdate) * portTICK_PERIOD_MS; // Calculate the elapsed time since the last token was received
elapsedTime = elapsedTime / 1000; // Convert the elapsed time to seconds
if (elapsedTime > (HOUR - 300))
{
tokenExpired = true;
ESP_LOGW(TAG , "Spotify Token is Expired"); // If the elapsed time is greater than the expiration time, set tokenExpired to true
}
return tokenExpired;
}
/**
* @brief This function reads refresh token from spiffs and send request for new token
* @return True if token received and saved, false for otherwise
*/
static bool Spotify_TokenRenew(void)
{
char receivedData[LONG_BUF];
ReadTxtFileFromSpiffs(InterfaceHandler->ConfigAddressInSpiffs, "refresh_token", receivedData, NULL, NULL);
SendRequest_ExchangeTokenWithRefreshToken(receivedData);
memset(receivedData, 0x0, LONG_BUF);
bool IsResponseReady = xSemaphoreTake(PrivateHandler.SpotifyBuffer.SpotifyResponseReadyFlag, pdMS_TO_TICKS(SEC));
if (!IsResponseReady)
{
ESP_LOGW(TAG, "timeout - Spotify not responded!");
return false;
}
bool IsTokenExtracted = ExtractAccessTokenParamsTokenFromJson(PrivateHandler.SpotifyBuffer.MessageBuffer, &PrivateHandler.token);
if (!IsTokenExtracted)
{
ESP_LOGW(TAG, "new Token not found!");
return false;
}
ESP_LOGI(TAG, "new Token found!");
PrivateHandler.TokenLastUpdate = xTaskGetTickCount();
return true;
}
/**
* @brief Sends a command to control Spotify.
* This function sends various commands to control the Spotify application based on the given command value.
* @param command A pointer to the command to be sent to Spotify.
* @return true if the command is successfully sent, false otherwise.
*
* @note Possible command values include:
* - NO_COMMAND: Waits for a command.
* - PLAY: Sends the PLAY command to Spotify.
* - PAUSE: Sends the PAUSE command to Spotify.
* - PLAY_NEXT: Sends the PLAY_NEXT command to Spotify.
* - PLAY_PREV: Sends the PLAY_PREV command to Spotify.
* - GET_NOW_PLAYING: Sends the GET_NOW_PLAYING command to Spotify.
* - GET_USER_INFO: Sends the GET_USER_INFO command to Spotify.
* - GET_SONG_IMAGE_URL: Sends the GET_SONG_IMAGE_URL command to Spotify.
* - GET_ARTIST_IMAGE_URL: Sends the GET_ARTIST_IMAGE_URL command to Spotify.
*/
bool Spotify_SendCommand(SpotifyInterfaceHandler_t SpotifyInterfaceHandler, int Command)
{
bool retValue = false;
ESP_LOGI(TAG, "user Command is %d", Command);
if (PrivateHandler.Status == LOGIN || PrivateHandler.Status == AUTHENTICATED)
{
ESP_LOGE(TAG, "You are not authorized !");
return false;
}
bool IsSuccessfull = false;
switch (Command)
{
case Play:
case Pause:
case PlayNext:
case PlayPrev:
Spotify_ControlPlayback(Command, PrivateHandler.token.AccessToken);
IsSuccessfull = PrivateHandler.SpotifyBuffer.status == 204;
if(!IsSuccessfull)
{
ESP_LOGW(TAG, "Command is not sent successfully");
retValue = false;
break;
}
ESP_LOGI(TAG, "Command is sent successfully");
retValue = true;
break;
case GetNowPlaying:
Spotify_GetInfo(Command, PrivateHandler.token.AccessToken);
IsSuccessfull = PrivateHandler.SpotifyBuffer.status == 200;
if (!IsSuccessfull)
{
ESP_LOGW(TAG, "No song is found");
retValue = false;
break;
}
ExtractPlaybackInfoParamsfromJson(PrivateHandler.SpotifyBuffer.MessageBuffer, InterfaceHandler->PlaybackInfo);
retValue = true;
break;
case GetUserInfo:
Spotify_GetInfo(Command, PrivateHandler.token.AccessToken);
IsSuccessfull = PrivateHandler.SpotifyBuffer.status == 200;
if (!IsSuccessfull)
{
ESP_LOGW(TAG, "No user is found");
retValue = false;
break;
}
ExtractUserInfoParamsfromJson(PrivateHandler.SpotifyBuffer.MessageBuffer, InterfaceHandler->UserInfo);
retValue = true;
break;
case GetCoverPhoto:
Spotify_DownloadImage(InterfaceHandler->PlaybackInfo->SongImageURL, PrivateHandler.token.AccessToken);
IsSuccessfull = PrivateHandler.SpotifyBuffer.status == 200;
if (!IsSuccessfull)
{
ESP_LOGW(TAG, "No song is found");
retValue = false;
break;
}
bool convertedSuccesfully = convertJpeg(PrivateHandler.SpotifyBuffer.MessageBuffer, PrivateHandler.SpotifyBuffer.ContentLength, InterfaceHandler->CoverPhoto, 150 * 150 * 2);
if (!convertedSuccesfully)
{
ESP_LOGW(TAG, "Image is not converted successfully");
retValue = false;
break;
}
addAlphaPixeltoImage(InterfaceHandler->CoverPhoto, 150, 150, 0xFF);
retValue = true;
break;
default:
break;
}
return retValue;
}