Skip to content

Commit

Permalink
samples: cellular: gnss: add support for nRF91x1 factory almanac
Browse files Browse the repository at this point in the history
Added support for nRF91x1 factory almanac. The new almanac file
format also supports QZSS satellites.

Updated nRF9160 factory almanac.

Signed-off-by: Tommi Kangas <tommi.kangas@nordicsemi.no>
  • Loading branch information
tokangas authored and nordicjm committed Aug 15, 2023
1 parent fc7abc9 commit 51414dd
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ Cellular samples (renamed from nRF9160 samples)
* The sample now uses tickless operating mode from Zephyr's LwM2M engine which does not cause device wake-up in 500 ms interval anymore.
This allows the device to achieve 2 uA of current usage while in PSM sleep mode.

* :ref:`gnss_sample` sample:

* Added support for nRF91x1 factory almanac.
The new almanac file format also supports QZSS satellites.

Trusted Firmware-M (TF-M) samples
---------------------------------

Expand Down
53 changes: 45 additions & 8 deletions samples/cellular/gnss/src/assistance_minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#include <nrf_modem_gnss.h>

#include "assistance.h"
#include "factory_almanac.h"
#include "factory_almanac_v2.h"
#include "factory_almanac_v3.h"
#include "mcc_location_table.h"

LOG_MODULE_DECLARE(gnss_sample, CONFIG_GNSS_SAMPLE_LOG_LEVEL);
Expand All @@ -31,7 +32,12 @@ LOG_MODULE_DECLARE(gnss_sample, CONFIG_GNSS_SAMPLE_LOG_LEVEL);
#define DAYS_PER_WEEK (7UL)
#define PLMN_STR_MAX_LEN 8 /* MCC + MNC + quotes */

static char almanac_checksum[64];
enum almanac_version {
FACTORY_ALMANAC_V2 = 2,
FACTORY_ALMANAC_V3 = 3
};

static char current_alm_checksum[64];

static int set(const char *key, size_t len_rd, settings_read_cb read_cb, void *cb_arg)
{
Expand All @@ -46,8 +52,8 @@ static int set(const char *key, size_t len_rd, settings_read_cb read_cb, void *c
key_len = settings_name_next(key, &next);

if (!strncmp(key, "almanac_checksum", key_len)) {
len = read_cb(cb_arg, &almanac_checksum, sizeof(almanac_checksum));
if (len < sizeof(almanac_checksum)) {
len = read_cb(cb_arg, &current_alm_checksum, sizeof(current_alm_checksum));
if (len < sizeof(current_alm_checksum)) {
LOG_ERR("Failed to read almanac checksum from settings");
}

Expand All @@ -62,20 +68,51 @@ static struct settings_handler assistance_settings = {
.h_set = set,
};

static enum almanac_version factory_almanac_version_get(void)
{
char resp[32];

if (nrf_modem_at_cmd(resp, sizeof(resp), "AT+CGMM") == 0) {
/* nRF9160 uses factory almanac file format version 2, while nRF91x1 uses
* version 3.
*/
if (strstr(resp, "nRF9160") != NULL) {
return FACTORY_ALMANAC_V2;
}
}

return FACTORY_ALMANAC_V3;
}

static void factory_almanac_write(void)
{
int err;
enum almanac_version alm_version;
const char *alm_data;
const char *alm_checksum;

/* Get the supported factory almanac version. */
alm_version = factory_almanac_version_get();
LOG_DBG("Supported factory almanac version: %d", alm_version);

if (alm_version == 3) {
alm_data = FACTORY_ALMANAC_DATA_V3;
alm_checksum = FACTORY_ALMANAC_CHECKSUM_V3;
} else {
alm_data = FACTORY_ALMANAC_DATA_V2;
alm_checksum = FACTORY_ALMANAC_CHECKSUM_V2;
}

/* Check if the same almanac has already been written to prevent unnecessary writes
* to flash memory.
*/
if (!strncmp(almanac_checksum, FACTORY_ALMANAC_CHECKSUM, sizeof(almanac_checksum))) {
if (!strncmp(current_alm_checksum, alm_checksum, sizeof(current_alm_checksum))) {
LOG_INF("Factory almanac has already been written, skipping writing");
return;
}

err = nrf_modem_at_printf("AT%%XFILEWRITE=1,\"%s\",\"%s\"",
FACTORY_ALMANAC, FACTORY_ALMANAC_CHECKSUM);
alm_data, alm_checksum);
if (err != 0) {
LOG_ERR("Failed to write factory almanac");
return;
Expand All @@ -84,8 +121,8 @@ static void factory_almanac_write(void)
LOG_INF("Wrote factory almanac");

err = settings_save_one("assistance/almanac_checksum",
FACTORY_ALMANAC_CHECKSUM,
sizeof(almanac_checksum));
alm_checksum,
sizeof(current_alm_checksum));
if (err) {
LOG_ERR("Failed to write almanac checksum to settings, error %d", err);
}
Expand Down
52 changes: 0 additions & 52 deletions samples/cellular/gnss/src/factory_almanac.h

This file was deleted.

52 changes: 52 additions & 0 deletions samples/cellular/gnss/src/factory_almanac_v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef FACTORY_ALMANAC_V2_H_
#define FACTORY_ALMANAC_V2_H_

/* Factory almanac generated on 2023-08-04 18:20:55.
*
* Note, that the almanac gets more inaccurate with time and it should be updated periodically.
*/

#define FACTORY_ALMANAC_DATA_V2 \
"f0ea020031e2080000000031c2690fe208101f68fd3fce0ca10016802900fe5b" \
"28008181a1ffb200000031a8820fe208561055fd00050ca100386b25007f9bc8" \
"ff03cb0e00acfd010031bb290fe20865184dfd00940ca100e37a5300c3292c00" \
"cf0571002fff0500314f140fe208c00d48fd00ac0ca100e3807f00c42a85ff51" \
"44eaff7b00030031d4310fe208eb0f42fd00410da100478b51009ce730005383" \
"090070ff00003106160fe208891e67fd006c0da100242929004079e0ff43fba9" \
"ff5502ffff318b8f0fe2080c0551fd00760da100f5d4a8ff4b40a7ff132d86ff" \
"6c00fdff31d2450fe2080c0a36fd006f0da10038f0fcff820b0b00b130faff72" \
"ff00003120150fe208690940fd00170ca100fb2b7d003afb4e00433e0f009eff" \
"030031d8460fe20837184efd00190da100765f5300747e9cff929e4c00cdff00" \
"00314f080fe2080e0f53fd00420ca1004ec72a00ec4c8cff9159e3ffbcfefbff" \
"31ed460fe2085f0e3ffd001a0ca1001b46d6ff17933800a4ef6a0052feffff31" \
"cb3b0fe208421251fd00f40ca10097be83ff4d8825003963ebff480201003170" \
"1c0fe208800332fd00f70ca1001aa7d4ffb47387ff453d67009900030031be7c" \
"0fe20808fa27fd00970da100884678004adf3100cf1bd2ff4400010031076d0f" \
"e208260e3efd00ac0ca100e303d7ff11422000c248210011fe010031af6f0fe2" \
"08401448fd00130ca100e69b000065b0c6ffe6c3f8fff5020000312f1d0fe208" \
"91145afd00fb0ca100a67f290087ec7f00f678a8ff79fefcff31a84d0fe208a9" \
"134bfd00b70ca1009f6b020029ed600065dd4f00730101003121210fe2087e04" \
"35fd00500ca100c2804c0090e68aff0cb0c4ffb701ffff3176ca0fe2089e0c53" \
"fd005f0ca100753225002476e2ff79dff9ff9f00000000000000000000000000" \
"00000000000000000000000000000000000000000031511c0fe20809154bfd00" \
"f40ca100ff3f5200826281ff8dc67e0036000100311e740fe20868fa45fd00b5" \
"0ca1002516a5ff78f824003ac1aeffd9fefdff31b15a0fe2085a0636fd00220d" \
"a100b8e2d2ff6ce52a000c1b6200e301010031b5410fe208cef927fd005c0da1" \
"00f68fd0ff11e9120081514500f500000031ab5e0fe208de0f40fd00f20ca100" \
"cee9fdff59461e007541fcffeefffeff3131050fe208550c5afd00690da10035" \
"58a7ffae94420010bf4b005200ffff3191150fe208be154afd00750da1008330" \
"0100fb516600e1d8f8ff83fd00003106360fe2085afb43fd00d90da1005b09a9" \
"fff04098ff41eb82fff5fd010031df540fe208b10756fd00720ca1009ca8a9ff" \
"30ea16000ef5620018ff0000312a390fe208c00b45fd00c50da1005eb87d0001" \
"25a6ff9bda1300cefdfeff000000000000000000000000000000000000000000" \
"0000000000"
#define FACTORY_ALMANAC_CHECKSUM_V2 \
"48317f82f41d9d44a478f505a459b7c7ab58986b92a1160ee4a02e4cbc5e7073"

#endif /* FACTORY_ALMANAC_V2_H_ */
61 changes: 61 additions & 0 deletions samples/cellular/gnss/src/factory_almanac_v3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef FACTORY_ALMANAC_V3_H_
#define FACTORY_ALMANAC_V3_H_

/* Factory almanac generated on 2023-08-05 19:14:17.
*
* Note, that the almanac gets more inaccurate with time and it should be updated periodically.
*/

#define FACTORY_ALMANAC_DATA_V3 \
"f0ea030031e2080000000031bf6924e208111f65fd3fcc0ca10022792900635c" \
"280003b0a0ffb200000031b18224e208561051fd00010ca1000a6425001e9fc8" \
"ffd4fd0d00acfd010031c32924e208691858fd00980ca100b1735300f32f2c00" \
"9f30700030ff0500314d1424e208bf0d4bfd00a70ca100a1797f00d22685ff69" \
"78e9ff7c00030031d83124e208ef0f4dfd00470da100f483510002ed300079a8" \
"08006fff0000310c1624e2088a1e63fd006c0da1002f2229004d6ae0ff3333a9" \
"ff5502ffff318f8f24e2080c0552fd00710da100bccda8fff441a7ff335485ff" \
"6b00fdff31d14524e208090a35fd00720da100c8e8fcffb30c0b005558f9ff71" \
"ff000031251524e208680944fd00120ca100a1247d00cbf14e00627d0e009fff" \
"030031da4624e2083a185afd001f0da10044585300067c9cff03cd4b00cdff00" \
"0031570824e2080f0f50fd00410ca1001ec02a001d798cff8161e2ffbbfefbff" \
"31e94624e2085f0e48fd00200ca100c43ed6ff5f953800f0226a0052feffff31" \
"d63b24e208401252fd00ef0ca1006fb783ff7e8825007b90eaff480201003177" \
"1c24e20880033cfd00ff0ca1009a9fd4ff117987ff456566009a00030031c97c" \
"24e20808fa2dfd00950da100e63e780001e231008140d1ff4500010031076d24" \
"e208260e48fd00b50ca10089fcd6ffab4220004078200012fe010031b36f24e2" \
"083d1445fd00150ca100a39400008eb0c6ff7ff9f7fff502000031371d24e208" \
"911457fd00f90ca1008a78290056fc7f006b96a7ff78fefcff31ab4d24e208a5" \
"1348fd00b80ca1005e64020095f66000af034f007401010031212124e2088204" \
"40fd00570ca10045794c000ce98aff18e1c3ffb701ffff3179ca24e2089e0c4d" \
"fd005b0ca100372b25009379e2ff2b0ff9ff9e00000000000000000000000000" \
"00000000000000000000000000000000000000000031571c24e2080d1556fd00" \
"fa0ca100c1385200125f81ff66f77d0037000100312d7424e20867fa43fd00ae" \
"0ca100c10ea5ff47fb24006ceeadffd8fefdff31b25a24e2085b063ffd00290d" \
"a10044dbd2ffa0e52a009b466100e301010031bb4124e208cff932fd00640da1" \
"005288d0ffeae912004a7a4400f500000031a95e24e208db0f3efd00f30ca100" \
"76e2fdff94481e00bb6cfbffedfffeff31320524e208540c59fd00640da1001c" \
"51a7fffe814200fefa4a005200ffff31951524e208bb1547fd00740da1004729" \
"0100c4606600b7f2f7ff83fd000031063624e2085afb43fd00d30da100fb01a9" \
"ffca4198ff870f82fff5fd010031e45424e208b00755fd006e0ca10070a1a9ff" \
"cbef1600e521620018ff000031283924e208be0b48fd00c20da10010b17d0002" \
"27a6ff4afe1200cefdfeff000000000000000000000000000000000000000000" \
"000000000000000000000000000031577e15e2082bd3eefe00a9eaca00c741d8" \
"ff9ea6bfffde5eccfffeff000031f87615e2087dce2fff0047e8ca005e471e00" \
"c152bfffea6c84ffffff000031f28115e208cd91e9fe003ff0ca0066686400a3" \
"79c1ff1d6a360067000000000000000000000000000000000000000000000000" \
"0000000000000000000000000000000000000000000000000000000000000000" \
"00000000000000000031880115e208aa004a0000a9ecca00845b93ff85460100" \
"0029c5ff00000000000000000000000000000000000000000000000000000000" \
"0000000000000000000000000000000000000000000000000000000000000000" \
"0000000000000000000000000000000000000000000000000000000000000000" \
"00000000000000000000000000000000000000000000000000000000000000"
#define FACTORY_ALMANAC_CHECKSUM_V3 \
"27098d060a0b163d7f0eda8761ab94efc0f048a0994ea45178d5f61732a029c2"

#endif /* FACTORY_ALMANAC_V3_H_ */

0 comments on commit 51414dd

Please sign in to comment.