Skip to content

Commit

Permalink
RDK-36410:RDKServices doc: Update JSON Definition
Browse files Browse the repository at this point in the history
  • Loading branch information
pkumbh631 committed Jun 13, 2022
1 parent addcaf7 commit 865b1cb
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 4 deletions.
143 changes: 143 additions & 0 deletions DeviceDiagnostics/DeviceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,56 @@

#include <curl/curl.h>
#include <time.h>
#include <fstream>

#include "UtilsJsonRpc.h"

#define DEVICE_DIAGNOSTICS_METHOD_NAME_GET_CONFIGURATION "getConfiguration"
#define DEVICE_DIAGNOSTICS_METHOD_GET_AV_DECODER_STATUS "getAVDecoderStatus"
#define DEVICE_DIAGNOSTICS_METHOD_GET_MILE_STONES "getMilestones"

#define DEVICE_DIAGNOSTICS_EVT_ON_AV_DECODER_STATUS_CHANGED "onAVDecoderStatusChanged"

#define MILESTONES_LOG_FILE "/opt/logs/rdk_milestones.log"

enum SysSrv_ErrorCode {
SysSrv_FileNotPresent,
SysSrv_FileAccessFailed
};

/**
* @brief : To map the error code with matching error message.
* @param1[in] : error code of type SysSrv_ErrorCode.
* @return : string; error message.
*/
std::string getErrorDescription(int errCode);

/***
* @brief : Used to read file contents into a vector
* @param1[in] : Complete file name with path
* @param2[in] : Destination vector buffer to be filled with file contents
* @return : <bool>; TRUE if operation success; else FALSE.
*/
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs);

/***
* @brief : Used to construct JSON response from Vector.
* @param1[in] : Destination JSON response buffer
* @param2[in] : JSON "Key"
* @param3[in] : Source Vector.
* @return : <bool>; TRUE if operation success; else FALSE.
*/
void setJSONResponseArray(JsonObject& response, const char* key,
const std::vector<string>& items);

/***
* @brief : Used to construct response with module error status.
* @param1[in] : Error Code
* @param2[out]: "response" JSON Object which is returned by the API
with updated module error status.
*/
void populateResponseWithError(int errorCode, JsonObject& response);

namespace WPEFramework
{
namespace Plugin
Expand Down Expand Up @@ -60,12 +102,14 @@ namespace WPEFramework

Register(DEVICE_DIAGNOSTICS_METHOD_NAME_GET_CONFIGURATION, &DeviceDiagnostics::getConfigurationWrapper, this);
Register(DEVICE_DIAGNOSTICS_METHOD_GET_AV_DECODER_STATUS, &DeviceDiagnostics::getAVDecoderStatus, this);
Register(DEVICE_DIAGNOSTICS_METHOD_GET_MILE_STONES, &DeviceDiagnostics::getMilestones, this);
}

DeviceDiagnostics::~DeviceDiagnostics()
{
Unregister(DEVICE_DIAGNOSTICS_METHOD_NAME_GET_CONFIGURATION);
Unregister(DEVICE_DIAGNOSTICS_METHOD_GET_AV_DECODER_STATUS);
Unregister(DEVICE_DIAGNOSTICS_METHOD_GET_MILE_STONES);
}

/* virtual */ const string DeviceDiagnostics::Initialize(PluginHost::IShell* service)
Expand Down Expand Up @@ -259,8 +303,107 @@ namespace WPEFramework
return result;
}

/***
* @brief : To fetch the list of milestones.
* @param1[in] : {params":{}}
* @param2[out] : "result":{"milestones":["<string>","<string>","<string>"],
* "success":<bool>}
* @return : Core::<StatusCode>
*/
uint32_t DeviceDiagnostics::getMilestones(const JsonObject& parameters,
JsonObject& response)
{
bool retAPIStatus = false;
std::vector<string> milestones;

if (Core::File(string(MILESTONES_LOG_FILE)).Exists()) {
retAPIStatus = getFileContent(MILESTONES_LOG_FILE, milestones);
if (retAPIStatus) {
setJSONResponseArray(response, "milestones", milestones);
} else {
populateResponseWithError(SysSrv_FileAccessFailed, response);
}
} else {
populateResponseWithError(SysSrv_FileNotPresent, response);
}
returnResponse(retAPIStatus);
}


} // namespace Plugin
} // namespace WPEFramework

std::map<int, std::string> ErrCodeMap = {
{SysSrv_FileNotPresent, "Expected file not found"},
{SysSrv_FileAccessFailed, "File access failed"}
};

std::string getErrorDescription(int errCode)
{
std::string errMsg = "Unexpected Error";

auto it = ErrCodeMap.find(errCode);
if (ErrCodeMap.end() != it) {
errMsg = it->second;
}
return errMsg;
}

/***
* @brief : Used to read file contents into a vector
* @param1[in] : Complete file name with path
* @param2[in] : Destination vector buffer to be filled with file contents
* @return : <bool>; TRUE if operation success; else FALSE.
*/
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{
bool retStatus = false;
std::ifstream inFile(fileName.c_str(), std::ios::in);

if (!inFile.is_open())
return retStatus;

std::string line;
retStatus = true;
while (std::getline(inFile, line)) {
if (line.size() > 0) {
vecOfStrs.push_back(line);
}
}
inFile.close();
return retStatus;
}

/***
* @brief : Used to construct JSON response from Vector.
* @param1[in] : Destination JSON response buffer
* @param2[in] : JSON "Key"
* @param3[in] : Source Vector.
* @return : <bool>; TRUE if operation success; else FALSE.
*/
void setJSONResponseArray(JsonObject& response, const char* key,
const std::vector<string>& items)
{
JsonArray arr;

for (auto& i : items) {
arr.Add(JsonValue(i));
}
response[key] = arr;
}

/***
* @brief : Used to construct response with module error status.
* @param1[in] : Error Code
* @param2[out]: "response" JSON Object which is returned by the API
with updated module error status.
*/
void populateResponseWithError(int errorCode, JsonObject& response)
{
if (errorCode) {
LOGWARN("Method %s failed; reason : %s\n", __FUNCTION__,
getErrorDescription(errorCode).c_str());
response["SysSrv_Status"] = static_cast<uint32_t>(errorCode);
response["errorMessage"] = getErrorDescription(errorCode);
}
}
3 changes: 1 addition & 2 deletions DeviceDiagnostics/DeviceDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace WPEFramework {

//Begin methods
uint32_t getConfigurationWrapper(const JsonObject& parameters, JsonObject& response);
uint32_t getMilestones(const JsonObject& parameters, JsonObject& response);
//End methods

int getConfiguration(const std::string& postData, JsonObject& response);
Expand Down Expand Up @@ -73,5 +74,3 @@ namespace WPEFramework {
};
} // namespace Plugin
} // namespace WPEFramework


23 changes: 23 additions & 0 deletions DeviceDiagnostics/DeviceDiagnostics.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@
]
}
},
"getMilestones": {
"summary": "Returns the list of milestones.\n \n### Events\n \n No Events.",
"result": {
"type": "object",
"properties": {
"milestones": {
"summary": "A string [] of milestones",
"type": "array",
"items": {
"type":"string",
"example": "2020 Jan 28 08:24:06.762355 arrisxi6 systemd[1]: Starting Log RDK Started Service..."
}
},
"success":{
"$ref": "#/definitions/success"
}
},
"required": [
"milestones",
"success"
]
}
},
"getAVDecoderStatus":{
"summary": "Gets the most active status of audio/video decoder/pipeline. This API doesn't track individual pipelines. It will aggregate and report the pipeline status, and the pipeline states are prioritized from High to Low (`ACTIVE`, `PAUSED`, and `IDLE`). Therefore, if any of the pipelines is in active state, then `getAVDecoderStatus` will return `ACTIVE`. If none of the pipelines are active but one is in a paused state, then `getAVDecoderStatus` will return `PAUSED`, and if all the pipelines are idle only then, `IDLE` will be returned.\n \n### Events \n \nNo events.",
"result":{
Expand Down
7 changes: 7 additions & 0 deletions SystemServices/System.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
},
"methods": {
"cacheContains": {
"deprecated" : true,
"summary": "Checks if a key is present in the cache.\n \n### Events\n \n No Events.",
"params": {
"type": "object",
Expand Down Expand Up @@ -380,6 +381,7 @@
}
},
"getCachedValue":{
"deprecated" : true,
"summary": "Gets the value of a key in the cache.\n \n### Events\n \n No Events.",
"params": {
"type": "object",
Expand Down Expand Up @@ -681,6 +683,7 @@
}
},
"getMilestones": {
"deprecated" : true,
"summary": "Returns the list of milestones.\n \n### Events\n \n No Events.",
"result": {
"type": "object",
Expand Down Expand Up @@ -1298,6 +1301,7 @@
}
},
"isGzEnabled":{
"deprecated" : true,
"summary": "Checks whether GZ is enabled.\n \n### Events\n \n No Events.",
"result": {
"type": "object",
Expand Down Expand Up @@ -1389,6 +1393,7 @@
}
},
"removeCacheKey":{
"deprecated" : true,
"summary": "Removes the cache key.\n \n### Events\n \n No Events.",
"params": {
"type":"object",
Expand Down Expand Up @@ -1426,6 +1431,7 @@
}
},
"setCachedValue": {
"deprecated" : true,
"summary": "Sets the value for a key in the cache.\n \n### Events\n \n No Events.",
"params": {
"type":"object",
Expand Down Expand Up @@ -1504,6 +1510,7 @@
}
},
"setGzEnabled": {
"deprecated" : true,
"summary": "Enables or disables GZ.\n \n### Events\n \n No Events.",
"params": {
"type":"object",
Expand Down
18 changes: 16 additions & 2 deletions SystemServices/SystemServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,7 @@ namespace WPEFramework {
JsonObject& response)
{
bool retStat = false;
bool deprecated = true;
if (parameters.HasLabel("key")) {
std::string key = parameters["key"].String();
LOGWARN("key: '%s'\n", key.c_str());
Expand All @@ -2398,6 +2399,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_MissingKeyValues, response);
}
response["deprecated"] = deprecated;
returnResponse(retStat);
}

Expand All @@ -2411,7 +2413,8 @@ namespace WPEFramework {
JsonObject& response)
{
bool retStat = false;

bool deprecated = true;

if (parameters.HasLabel("key") && parameters.HasLabel("value")) {
std::string key = parameters["key"].String();
std::string value = parameters["value"].String();
Expand All @@ -2429,6 +2432,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_MissingKeyValues, response);
}
response["deprecated"] = deprecated;
returnResponse(retStat);
}

Expand All @@ -2442,6 +2446,7 @@ namespace WPEFramework {
JsonObject& response)
{
bool retStat = false;
bool deprecated = true;
if (parameters.HasLabel("key")) {
std::string key = parameters["key"].String();
if (key.length()) {
Expand All @@ -2457,6 +2462,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_MissingKeyValues, response);
}
response["deprecated"] = deprecated;
returnResponse(retStat);
}

Expand All @@ -2470,6 +2476,7 @@ namespace WPEFramework {
JsonObject& response)
{
bool retStat = false;
bool deprecated = true;
if (parameters.HasLabel("key")) {
std::string key = parameters["key"].String();
if (key.length()) {
Expand All @@ -2485,6 +2492,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_MissingKeyValues, response);
}
response["deprecated"] = deprecated;
returnResponse(retStat);
}

Expand Down Expand Up @@ -2882,7 +2890,8 @@ namespace WPEFramework {
JsonObject& response)
{
bool retAPIStatus = false;
vector<string> milestones;
bool deprecated = true;
std::vector<string> milestones;

if (Utils::fileExists(MILESTONES_LOG_FILE)) {
retAPIStatus = getFileContent(MILESTONES_LOG_FILE, milestones);
Expand All @@ -2894,6 +2903,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_FileNotPresent, response);
}
response["deprecated"] = deprecated;
returnResponse(retAPIStatus);
}

Expand Down Expand Up @@ -3153,6 +3163,7 @@ namespace WPEFramework {
{
bool enabled = false;
bool result = false;
bool deprecated = true;
int32_t retVal = E_NOK;
if (parameters.HasLabel("enabled")) {
enabled = parameters["enabled"].Boolean();
Expand All @@ -3165,6 +3176,7 @@ namespace WPEFramework {
} else {
populateResponseWithError(SysSrv_MissingKeyValues, response);
}
response["deprecated"] = deprecated;
returnResponse(( E_OK == retVal)? true: false);
} //ent of SetGZEnabled

Expand All @@ -3179,9 +3191,11 @@ namespace WPEFramework {
JsonObject& response)
{
bool enabled = false;
bool deprecated = true;

isGzEnabledHelper(enabled);
response["enabled"] = enabled;
response["deprecated"] = deprecated;

returnResponse(true);
} //end of isGZEnbaled
Expand Down

0 comments on commit 865b1cb

Please sign in to comment.