Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cellular Global Identity diagnostic cache #1820

Merged
merged 4 commits into from
Jun 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions system/src/system_network_diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <string.h>

#include "cellular_hal.h"
#include "check.h"
#include "spark_wiring_diagnostics.h"
#include "spark_wiring_fixed_point.h"
#include "spark_wiring_platform.h"
Expand Down Expand Up @@ -56,16 +57,16 @@ namespace
using namespace particle;
using namespace spark;

class SignalCache
class NetworkCache
{
public:
static const system_tick_t SIGNAL_INFO_CACHE_INTERVAL = 1000;
static const system_tick_t NETWORK_INFO_CACHE_INTERVAL = 1000;

const Signal* getSignal()
{
#if Wiring_WiFi || Wiring_Cellular
system_tick_t m = millis();
if (ts_ == 0 || (m - ts_) >= SIGNAL_INFO_CACHE_INTERVAL)
if (ts_ == 0 || (m - ts_) >= NETWORK_INFO_CACHE_INTERVAL)
{
#if Wiring_WiFi
sig_ = WiFi.RSSI();
Expand All @@ -80,18 +81,45 @@ class SignalCache
#endif
}

#if HAL_PLATFORM_CELLULAR
const cellular_result_t getCGI(CellularGlobalIdentity& cgi)
{
cellular_result_t result = SYSTEM_ERROR_NONE;
system_tick_t ms = millis();

if (cgi_ts_ == 0 || (ms - cgi_ts_) >= NETWORK_INFO_CACHE_INTERVAL)
{
// Update cache
cgi_.size = sizeof(CellularGlobalIdentity);
cgi_.version = CGI_VERSION_LATEST;
result = CHECK(cellular_global_identity(&cgi_, nullptr));
cgi_ts_ = millis();
}

// Return cached values
cgi = cgi_;
return result;
}
#endif

private:
#if Wiring_Cellular
CellularSignal sig_;
CellularGlobalIdentity cgi_{.size = sizeof(CellularGlobalIdentity),
.version = CGI_VERSION_LATEST};
#elif Wiring_WiFi
WiFiSignal sig_;
#elif Wiring_Mesh
MeshSignal sig_;
#endif
system_tick_t ts_ = 0;

#if HAL_PLATFORM_CELLULAR
system_tick_t cgi_ts_ = 0;
#endif
};

static SignalCache s_signalCache;
static NetworkCache s_networkCache;

class SignalStrengthDiagnosticData : public AbstractIntegerDiagnosticData
{
Expand All @@ -104,7 +132,7 @@ class SignalStrengthDiagnosticData : public AbstractIntegerDiagnosticData

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -133,7 +161,7 @@ class NetworkRssiDiagnosticData : public AbstractIntegerDiagnosticData

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -163,7 +191,7 @@ class SignalStrengthValueDiagnosticData : public AbstractIntegerDiagnosticData

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -193,7 +221,7 @@ class SignalQualityDiagnosticData : public AbstractIntegerDiagnosticData

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -223,7 +251,7 @@ class SignalQualityValueDiagnosticData : public AbstractIntegerDiagnosticData

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand Down Expand Up @@ -253,7 +281,7 @@ class NetworkAccessTechnologyDiagnosticData : public AbstractIntegerDiagnosticDa

virtual int get(IntType& val)
{
const Signal* sig = s_signalCache.getSignal();
const Signal* sig = s_networkCache.getSignal();
if (sig == nullptr)
{
return SYSTEM_ERROR_NOT_SUPPORTED;
Expand All @@ -279,11 +307,12 @@ class NetworkCellularCellGlobalIdentityMobileCountryCodeDiagnosticData

virtual int get(IntType& val)
{
CellularGlobalIdentity cgi{.size = sizeof(CellularGlobalIdentity), .version = CGI_VERSION_LATEST};
cellular_global_identity(&cgi, nullptr);
cellular_result_t result;
CellularGlobalIdentity cgi; // Intentionally left uninitialized
result = CHECK(s_networkCache.getCGI(cgi));
val = static_cast<IntType>(cgi.mobile_country_code);

return SYSTEM_ERROR_NONE;
return result;
}
} g_networkCellularCellGlobalIdentityMobileCountryCodeDiagnosticData;

Expand All @@ -300,8 +329,9 @@ class NetworkCellularCellGlobalIdentityMobileNetworkCodeDiagnosticData

virtual int get(IntType& val)
{
CellularGlobalIdentity cgi{.size = sizeof(CellularGlobalIdentity), .version = CGI_VERSION_LATEST};
cellular_global_identity(&cgi, nullptr);
cellular_result_t result;
CellularGlobalIdentity cgi; // Intentionally left uninitialized
result = CHECK(s_networkCache.getCGI(cgi));
if (CGI_FLAG_TWO_DIGIT_MNC & cgi.cgi_flags)
{
val = static_cast<IntType>(cgi.mobile_network_code * -1);
Expand All @@ -311,7 +341,7 @@ class NetworkCellularCellGlobalIdentityMobileNetworkCodeDiagnosticData
val = static_cast<IntType>(cgi.mobile_network_code);
}

return SYSTEM_ERROR_NONE;
return result;
}
} g_networkCellularCellGlobalIdentityMobileNetworkCodeDiagnosticData;

Expand All @@ -328,11 +358,12 @@ class NetworkCellularCellGlobalIdentityLocationAreaCodeDiagnosticData

virtual int get(IntType& val)
{
CellularGlobalIdentity cgi{.size = sizeof(CellularGlobalIdentity), .version = CGI_VERSION_LATEST};
cellular_global_identity(&cgi, nullptr);
cellular_result_t result;
CellularGlobalIdentity cgi; // Intentionally left uninitialized
result = CHECK(s_networkCache.getCGI(cgi));
val = static_cast<IntType>(cgi.location_area_code);

return SYSTEM_ERROR_NONE;
return result;
}
} g_networkCellularCellGlobalIdentityLocationAreaCodeDiagnosticData;

Expand All @@ -347,11 +378,12 @@ class NetworkCellularCellGlobalIdentityCellIdDiagnosticData : public AbstractInt

virtual int get(IntType& val)
{
CellularGlobalIdentity cgi{.size = sizeof(CellularGlobalIdentity), .version = CGI_VERSION_LATEST};
cellular_global_identity(&cgi, nullptr);
cellular_result_t result;
CellularGlobalIdentity cgi; // Intentionally left uninitialized
result = CHECK(s_networkCache.getCGI(cgi));
val = static_cast<IntType>(cgi.cell_id);

return SYSTEM_ERROR_NONE;
return result;
}
} g_networkCellularCellGlobalIdentityCellIdDiagnosticData;
#endif // HAL_PLATFORM_CELLULAR
Expand Down