make it fit in allocated space

This commit is contained in:
philippe44
2023-03-27 17:09:27 -07:00
parent 7dfdd7b9e5
commit fc78b36c1f
24 changed files with 421 additions and 230 deletions

View File

@@ -41,13 +41,24 @@ void AccessKeyFetcher::getAccessKey(AccessKeyFetcher::Callback callback) {
if (res.fail) return;
char* accessKeyJson = (char*)res.parts[0].data();
auto accessJSON = std::string(accessKeyJson, strrchr(accessKeyJson, '}') - accessKeyJson + 1);
#ifdef BELL_ONLY_CJSON
cJSON* jsonBody = cJSON_Parse(accessJSON.c_str());
this->accessKey = cJSON_GetObjectItem(jsonBody, "accessToken")->valuestring;
int expiresIn = cJSON_GetObjectItem(jsonBody, "expiresIn")->valueint;
#else
auto jsonBody = nlohmann::json::parse(accessJSON);
this->accessKey = jsonBody["accessToken"];
int expiresIn = jsonBody["expiresIn"];
#endif
expiresIn = expiresIn / 2; // Refresh token before it expires
this->expiresAt =
timeProvider->getSyncedTimestamp() + (expiresIn * 1000);
#ifdef BELL_ONLY_CJSON
callback(cJSON_GetObjectItem(jsonBody, "accessToken")->valuestring);
cJSON_Delete(jsonBody);
#else
callback(jsonBody["accessToken"]);
#endif
});
}

View File

@@ -18,6 +18,13 @@ std::string ApResolve::fetchFirstApAddress()
std::string_view responseStr = request->body();
// parse json with nlohmann
#if BELL_ONLY_CJSON
cJSON* json = cJSON_Parse(responseStr.data());
auto ap_string = std::string(cJSON_GetArrayItem(cJSON_GetObjectItem(json, "ap_list"), 0)->valuestring);
cJSON_Delete(json);
return ap_string;
#else
auto json = nlohmann::json::parse(responseStr);
return json["ap_list"][0];
#endif
}

View File

@@ -38,8 +38,14 @@ void CDNTrackStream::fetchFile(const std::vector<uint8_t>& trackId,
std::string_view result = req->body();
#ifdef BELL_ONLY_CJSON
cJSON* jsonResult = cJSON_Parse(result.data());
std::string cdnUrl = cJSON_GetArrayItem(cJSON_GetObjectItem(jsonResult, "cdnurl"), 0)->valuestring;
cJSON_Delete(jsonResult);
#else
auto jsonResult = nlohmann::json::parse(result);
std::string cdnUrl = jsonResult["cdnurl"][0];
#endif
if (this->status != Status::FAILED) {
this->cdnUrl = cdnUrl;
@@ -61,7 +67,7 @@ void CDNTrackStream::seek(size_t newPos) {
this->position = newPos;
}
void CDNTrackStream::openStream() {
void CDNTrackStream::openStream() {
CSPOT_LOG(info, "Opening HTTP stream to %s", this->cdnUrl.c_str());
// Open connection, read first 128 bytes
@@ -96,7 +102,7 @@ void CDNTrackStream::openStream() {
this->isConnected = true;
}
size_t CDNTrackStream::readBytes(uint8_t* dst, size_t bytes) {
size_t CDNTrackStream::readBytes(uint8_t* dst, size_t bytes) {
size_t offsetPosition = position + SPOTIFY_OPUS_HEADER;
size_t actualFileSize = this->totalFileSize + SPOTIFY_OPUS_HEADER;

View File

@@ -1,6 +1,9 @@
#include "LoginBlob.h"
#include "ConstantParameters.h"
#include "Logger.h"
#ifdef BELL_ONLY_CJSON
#include "cJSON.h"
#endif
using namespace cspot;
@@ -125,21 +128,46 @@ void LoginBlob::loadUserPass(const std::string& username,
}
void LoginBlob::loadJson(const std::string& json) {
#ifdef BELL_ONLY_CJSON
cJSON* root = cJSON_Parse(json.c_str());
cJSON* item = cJSON_GetObjectItem(root, "authType");
this->authType = item->valueint;
item = cJSON_GetObjectItem(root, "username");
this->username = item->valuestring;
item = cJSON_GetObjectItem(root, "authData");
std::string authDataObject = item->valuestring;
cJSON_Delete(root);
#else
auto root = nlohmann::json::parse(json);
this->authType = root["authType"];
this->username = root["username"];
std::string authDataObject = root["authData"];
this->authData = crypto->base64Decode(authDataObject);
#endif
}
std::string LoginBlob::toJson() {
#ifdef BELL_ONLY_CJSON
cJSON* json_obj = cJSON_CreateObject();
cJSON_AddStringToObject(json_obj, "authData", crypto->base64Encode(authData).c_str());
cJSON_AddNumberToObject(json_obj, "authType", this->authType);
cJSON_AddStringToObject(json_obj, "username", this->username.c_str());
char *str = cJSON_PrintUnformatted(json_obj);
cJSON_Delete(json_obj);
std::string json_objStr(str);
free(str);
return json_objStr;
#else
nlohmann::json obj;
obj["authData"] = crypto->base64Encode(authData);
obj["authType"] = this->authType;
obj["username"] = this->username;
return obj.dump();
#endif
}
void LoginBlob::loadZeroconfQuery(
@@ -164,7 +192,35 @@ std::string LoginBlob::buildZeroconfInfo() {
// Encode publicKey into base64
auto encodedKey = crypto->base64Encode(crypto->publicKey);
#ifdef BELL_ONLY_CJSON
cJSON* json_obj = cJSON_CreateObject();
cJSON_AddNumberToObject(json_obj, "status", 101);
cJSON_AddStringToObject(json_obj, "statusString", "OK");
cJSON_AddStringToObject(json_obj, "version", cspot::protocolVersion);
cJSON_AddStringToObject(json_obj, "libraryVersion", cspot::swVersion);
cJSON_AddStringToObject(json_obj, "accountReq", "PREMIUM");
cJSON_AddStringToObject(json_obj, "brandDisplayName", cspot::brandName);
cJSON_AddStringToObject(json_obj, "modelDisplayName", name.c_str());
cJSON_AddStringToObject(json_obj, "voiceSupport", "NO");
cJSON_AddStringToObject(json_obj, "availability", this->username.c_str());
cJSON_AddNumberToObject(json_obj, "productID", 0);
cJSON_AddStringToObject(json_obj, "tokenType", "default");
cJSON_AddStringToObject(json_obj, "groupStatus", "NONE");
cJSON_AddStringToObject(json_obj, "resolverVersion", "0");
cJSON_AddStringToObject(json_obj, "scope", "streaming,client-authorization-universal");
cJSON_AddStringToObject(json_obj, "activeUser", "");
cJSON_AddStringToObject(json_obj, "deviceID", deviceId.c_str());
cJSON_AddStringToObject(json_obj, "remoteName", name.c_str());
cJSON_AddStringToObject(json_obj, "publicKey", encodedKey.c_str());
cJSON_AddStringToObject(json_obj, "deviceType", "deviceType");
char *str = cJSON_PrintUnformatted(json_obj);
cJSON_Delete(json_obj);
std::string json_objStr(str);
free(str);
return json_objStr;
#else
nlohmann::json obj;
obj["status"] = 101;
obj["statusString"] = "OK";
@@ -188,6 +244,7 @@ std::string LoginBlob::buildZeroconfInfo() {
obj["deviceType"] = "SPEAKER";
return obj.dump();
#endif
}
std::string LoginBlob::getDeviceId() {

View File

@@ -150,7 +150,7 @@ void TrackPlayer::runTask() {
dataCallback(pcmBuffer.data() + (ret - toWrite), toWrite,
this->currentTrackStream->trackInfo.trackId, this->sequence);
if (written == 0) {
BELL_SLEEP_MS(10);
BELL_SLEEP_MS(50);
}
toWrite -= written;
}