Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a4680b0
Make polygon type public
MedadRufus Jan 17, 2023
df262be
delete unneeded tests
MedadRufus Jan 17, 2023
b9cb792
Use new n_polygons
MedadRufus Jan 17, 2023
7a63410
Use n_polygons param
MedadRufus Jan 17, 2023
c00051d
Use correct number of polygons
MedadRufus Jan 17, 2023
cedb5b7
revert
MedadRufus Jan 17, 2023
acb72ad
revert
MedadRufus Jan 17, 2023
0916ec4
Simulate disabling tx over Ukraine
MedadRufus Jan 17, 2023
1f18377
allow Ukraine tx ok
MedadRufus Jan 17, 2023
6a6985b
use nicer polygon shape
MedadRufus Jan 17, 2023
88b0700
Enable over north korea test
MedadRufus Jan 17, 2023
4e289a2
comment
MedadRufus Jan 17, 2023
3c60cf0
use tx_permission_t type even in eeprom
MedadRufus Jan 17, 2023
7b498d9
use correct type
MedadRufus Jan 17, 2023
6729d17
Use correct type
MedadRufus Jan 17, 2023
86c01d0
use correct type
MedadRufus Jan 17, 2023
cb655fa
Use correct type and size
MedadRufus Jan 17, 2023
ed92336
fix return type
MedadRufus Jan 17, 2023
a4a322a
default over north korea
MedadRufus Jan 17, 2023
4bc5918
north korea
MedadRufus Jan 17, 2023
2a09b6d
Add a north korea polygon
MedadRufus Jan 18, 2023
ad6995c
add nk polygon
MedadRufus Jan 18, 2023
7bda8b8
Add north korea polygon
MedadRufus Jan 18, 2023
710f1fb
update tests
MedadRufus Jan 18, 2023
2245666
corret default
MedadRufus Jan 18, 2023
3e2ae44
dedicater polygon
MedadRufus Jan 18, 2023
d2bf406
use correct type
MedadRufus Jan 18, 2023
92b66d6
use sizeof
MedadRufus Jan 18, 2023
a9f11a8
Update comment
MedadRufus Jan 18, 2023
95461ec
Add bounds check
MedadRufus Jan 18, 2023
9dddcae
delete unneeded comment
MedadRufus Jan 18, 2023
b5d5602
correct type
MedadRufus Jan 18, 2023
19c0e1a
use default at root
MedadRufus Jan 18, 2023
1c666ad
remove cost
MedadRufus Jan 18, 2023
1f77026
Merge branch 'icspace-dev' into transmit-all-over-europe
MedadRufus Jan 19, 2023
f4b1f63
Merge branch 'icspace-dev' into transmit-all-over-europe
MedadRufus Jan 19, 2023
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
2 changes: 1 addition & 1 deletion src/peripherals/bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void BSP_sensor_Init(void)
IWDG_reset();

// Initialise the geofence permissions mask
bool values[N_POLYGONS];
tx_permission_t values[n_polygons];
read_geofence_settings_in_eeprom(values);

playback_hw_init();
Expand Down
52 changes: 35 additions & 17 deletions src/peripherals/eeprom_settings_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@
#include "bsp.h"
#include "eeprom_settings_manager.h"

static const tx_permission_t default_geofence_settings[n_polygons] = {
[AS920923_PHILIPPINES_polygon] = TX_OK,
[US902928_NAMERICA_polygon] = TX_OK,
[AS923925_BRUNEI_polygon] = TX_OK,
[AS923925_TAIWAN_polygon] = TX_OK,
[AS923925_INDONESIA_polygon] = TX_OK,
[AS923925_THAILAND_polygon] = TX_OK,
[US902928_ARGENTINA_polygon] = TX_OK,
[AU915928_BRAZIL_polygon] = TX_OK,
[AU915928_CHILE_polygon] = TX_OK,
[CN779787_CHINA_polygon] = TX_OK,
[IN865867_INDIA_polygon] = TX_OK,
[AS920923_JAPAN_polygon] = TX_OK,
[KR920923_SKOREA_polygon] = TX_OK,
[AS920923_MALAYSIASG_polygon] = TX_OK,
[AU915928_AUSTRALIA_polygon] = TX_OK,
[RU864870_RUSSIA_polygon] = TX_OK,
[EU863870_EUROPE_polygon] = TX_OK,
[US902928_GUAM_polygon] = TX_OK,
[US902928_ANTARTICA_polygon] = TX_OK,
[AS920923_ISRAEL_polygon] = TX_OK,
[US902928_MALDIVES_polygon] = TX_OK,
[EU863870_UKRAINE_polygon] = TX_OK,
[EU863870_NORTH_KOREA_polygon] = TX_NOT_OK,
[OUTSIDE_polygon] = TX_NOT_OK,
};

/**
* @brief Get the eeprom stored lorwan region
*
Expand Down Expand Up @@ -60,33 +87,24 @@ bool update_geofence_settings_in_eeprom(uint8_t *settings_bytes, uint16_t size)
/**
* @brief Reads and initialised the geofence module with the tx enable/disable settings
* from EEPROM. Fills passed buffer with the settings. Ensure buffer passed is
* of size N_POLYGONS * sizeof(bool)
* of size n_polygons * sizeof(tx_permission_t)
*
* @param values Pointer to buffer that will be filled with the bool enable/disable setting
* @param values Pointer to buffer that will be filled with the tx_permission_t setting
*/
void read_geofence_settings_in_eeprom(bool *values)
void read_geofence_settings_in_eeprom(tx_permission_t *values)
{
// todo: check non-null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check non null

// read settings stored in EEPROM
geofence_settings_t geofence_settings;
NvmmRead((uint8_t *)&geofence_settings, TX_PERMISSIONS_LEN, TX_PERMISSIONS_ADDR);
uint16_t sizeof_values = sizeof(geofence_settings.values);

// Use eeprom stored values only if CRC is correct. Else assume the default settings
if (is_crc_correct(sizeof(geofence_settings_t), (void *)&geofence_settings))
{
memcpy(values, geofence_settings.values, N_POLYGONS * sizeof(bool));
}
else
{

bool default_geofence_settings[N_POLYGONS] = {true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, true, true, false};

memcpy(values, default_geofence_settings, N_POLYGONS * sizeof(bool));
memcpy(values, geofence_settings.values, sizeof_values);
geofence_init_with_settings(values);
}

geofence_init_with_settings(values);
}

/**
Expand Down Expand Up @@ -220,7 +238,7 @@ uint8_t get_settings_crc()
}

// CRC of tx enable/disable settings for different regions.
bool values[N_POLYGONS];
tx_permission_t values[n_polygons];
read_geofence_settings_in_eeprom(values);
crc = Crc8Update(crc, (uint8_t *)values, sizeof(values));

Expand Down
7 changes: 3 additions & 4 deletions src/peripherals/eeprom_settings_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ extern "C"
#include "stdint.h"
#include "stdbool.h"
#include "LoRaWAN_config_switcher.h"
#include "geofence.h"

typedef struct
{
uint32_t tx_interval;
uint32_t Crc32;
} tx_interval_eeprom_t;

#define N_POLYGONS 23

typedef struct
{
bool values[N_POLYGONS];
tx_permission_t values[n_polygons];
uint32_t Crc32;
} geofence_settings_t;

bool update_device_tx_interval_in_eeprom(uint32_t address, uint32_t interval_ms);
uint32_t read_tx_interval_in_eeprom(uint32_t address, uint32_t default_value);
bool update_geofence_settings_in_eeprom(uint8_t *settings, uint16_t size);
void read_geofence_settings_in_eeprom(bool *values);
void read_geofence_settings_in_eeprom(tx_permission_t *values);
void retrieve_eeprom_stored_lorawan_region(void);
void set_eeprom_stored_lorwan_region(void);
bool update_device_credentials_to_eeprom(network_keys_t keys, registered_devices_t registered_device);
Expand Down
128 changes: 77 additions & 51 deletions src/peripherals/geofence.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,38 @@

/* Global variables definitions go here */

// GEOFENCE variables
/* The world is split into polygons e.g. EU863870_EUROPE_polygon.
* Multiple polygons can have the same LoRa region settings. E.g. LORAMAC_REGION_EU868.
* Keeps track of which polygon the tracker is in, and if it changes to another polygon,
* all LoRa settings are reinitialised when the balloon enters another polygon.
*
*/

/*!
* Polygon region enumeration
*/
typedef enum polygon_t
{
AS920923_PHILIPPINES_polygon,
US902928_NAMERICA_polygon,
AS923925_BRUNEI_polygon,
AS923925_TAIWAN_polygon,
AS923925_INDONESIA_polygon,
AS923925_THAILAND_polygon,
US902928_ARGENTINA_polygon,
AU915928_BRAZIL_polygon,
AU915928_CHILE_polygon,
CN779787_CHINA_polygon,
IN865867_INDIA_polygon,
AS920923_JAPAN_polygon,
KR920923_SKOREA_polygon,
AS920923_MALAYSIASG_polygon,
AU915928_AUSTRALIA_polygon,
RU864870_RUSSIA_polygon,
EU863870_EUROPE_polygon,
US902928_GUAM_polygon,
US902928_ANTARTICA_polygon,
AS920923_ISRAEL_polygon,
US902928_MALDIVES_polygon,
EU863870_UKRAINE_polygon,
OUTSIDE_polygon,
n_polygons,
} Polygon_t;

/**
* @brief Transmission enable/disable mask for each region. Each
* boolean value corresponds to each enum value in Polygon_t.
* By default, transmissions are disabled over regions
* outside of the geofence regions.
*/
bool tx_permissions_for_regions[n_polygons];
static tx_permission_t tx_permissions_for_regions[n_polygons] = {
[AS920923_PHILIPPINES_polygon] = TX_OK,
[US902928_NAMERICA_polygon] = TX_OK,
[AS923925_BRUNEI_polygon] = TX_OK,
[AS923925_TAIWAN_polygon] = TX_OK,
[AS923925_INDONESIA_polygon] = TX_OK,
[AS923925_THAILAND_polygon] = TX_OK,
[US902928_ARGENTINA_polygon] = TX_OK,
[AU915928_BRAZIL_polygon] = TX_OK,
[AU915928_CHILE_polygon] = TX_OK,
[CN779787_CHINA_polygon] = TX_OK,
[IN865867_INDIA_polygon] = TX_OK,
[AS920923_JAPAN_polygon] = TX_OK,
[KR920923_SKOREA_polygon] = TX_OK,
[AS920923_MALAYSIASG_polygon] = TX_OK,
[AU915928_AUSTRALIA_polygon] = TX_OK,
[RU864870_RUSSIA_polygon] = TX_OK,
[EU863870_EUROPE_polygon] = TX_OK,
[US902928_GUAM_polygon] = TX_OK,
[US902928_ANTARTICA_polygon] = TX_OK,
[AS920923_ISRAEL_polygon] = TX_OK,
[US902928_MALDIVES_polygon] = TX_OK,
[EU863870_UKRAINE_polygon] = TX_OK,
[EU863870_NORTH_KOREA_polygon] = TX_NOT_OK,
[OUTSIDE_polygon] = TX_NOT_OK,
};

geofence_status_t current_geofence_status =
{
Expand Down Expand Up @@ -425,7 +411,7 @@ static const float AS920923_JAPAN_F[32] = {

};

static const float KR920923_SKOREA_F[18] = {
static const float KR920923_SKOREA_F[22] = {
122.94545,
36.60046,
122.59912,
Expand All @@ -436,8 +422,12 @@ static const float KR920923_SKOREA_F[18] = {
35.18414,
131.42570,
38.21762,
131.67979,
41.62004,
131.84483,
41.61509,
130.90107,
42.36039,
129.65493,
40.70374,
127.96554,
39.81694,
128.33108,
Expand Down Expand Up @@ -807,6 +797,34 @@ static const float EU863870_UKRAINE_F[46] = {

};

static const float EU863870_NORTH_KOREA_F[24] = {
124.16708,
39.74409,
124.86613,
39.40398,
124.56884,
38.03067,
125.59527,
37.58797,
128.25518,
38.53998,
127.77692,
39.27210,
127.66038,
39.73660,
129.59331,
40.73516,
129.92897,
41.36654,
130.75795,
42.26509,
130.24571,
43.24406,
124.16708,
39.74409,

};

/* ==================================================================== */
/* ========================== private data ============================ */
/* ==================================================================== */
Expand Down Expand Up @@ -1025,6 +1043,11 @@ Polygon_t get_polygon(float latitude, float longitude)
current_poly = US902928_MALDIVES_polygon;
}

else if (pointInPolygonF(12, EU863870_NORTH_KOREA_F, latitude, longitude) == 1)
{
current_poly = EU863870_NORTH_KOREA_polygon;
}

return current_poly;
}

Expand All @@ -1037,11 +1060,12 @@ Polygon_t get_polygon(float latitude, float longitude)
*/
tx_permission_t get_permissions(Polygon_t currentpoly)
{
// then lookup value. Danger, there is no bounds validation for current poly. Can cause corruption.
bool can_transmit = tx_permissions_for_regions[currentpoly];

// then return permission
return can_transmit ? TX_OK : TX_NOT_OK;
tx_permission_t result = TX_OK; // failsafe by allowing TX
if (currentpoly < n_polygons)
{
result = tx_permissions_for_regions[currentpoly];
}
return result;
}

static void set_current_loramac_region_from_polygon(Polygon_t current_poly)
Expand Down Expand Up @@ -1127,7 +1151,9 @@ static void set_current_loramac_region_from_polygon(Polygon_t current_poly)
case EU863870_UKRAINE_polygon:
current_geofence_status.current_loramac_region = LORAMAC_REGION_EU868;
break;

case EU863870_NORTH_KOREA_polygon:
current_geofence_status.current_loramac_region = LORAMAC_REGION_EU868;
break;
case OUTSIDE_polygon:
current_geofence_status.current_loramac_region = LORAMAC_REGION_EU868;
break;
Expand All @@ -1153,7 +1179,7 @@ tx_permission_t get_current_tx_permission()
return current_geofence_status.tx_permission;
}

void geofence_init_with_settings(bool *settings)
void geofence_init_with_settings(tx_permission_t *settings)
{
memcpy(tx_permissions_for_regions, settings, n_polygons * sizeof(bool));
memcpy(tx_permissions_for_regions, settings, sizeof(tx_permissions_for_regions));
}
41 changes: 39 additions & 2 deletions src/peripherals/geofence.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern "C"

typedef enum
{
TX_OK = 0,
TX_OK = 0, // Default value is TX_OK. If this value is uninitialised, it will failsafe to TX_OK.
TX_NOT_OK
} tx_permission_t;

Expand All @@ -56,14 +56,51 @@ extern "C"

} geofence_status_t;

/* The world is split into polygons e.g. EU863870_EUROPE_polygon.
* Multiple polygons can have the same LoRa region settings. E.g. LORAMAC_REGION_EU868.
* Keeps track of which polygon the tracker is in, and if it changes to another polygon,
* all LoRa settings are reinitialised when the balloon enters another polygon.
*/
/*!
* Polygon region enumeration
*/
typedef enum polygon_t
{
AS920923_PHILIPPINES_polygon,
US902928_NAMERICA_polygon,
AS923925_BRUNEI_polygon,
AS923925_TAIWAN_polygon,
AS923925_INDONESIA_polygon,
AS923925_THAILAND_polygon,
US902928_ARGENTINA_polygon,
AU915928_BRAZIL_polygon,
AU915928_CHILE_polygon,
CN779787_CHINA_polygon,
IN865867_INDIA_polygon,
AS920923_JAPAN_polygon,
KR920923_SKOREA_polygon,
AS920923_MALAYSIASG_polygon,
AU915928_AUSTRALIA_polygon,
RU864870_RUSSIA_polygon,
EU863870_EUROPE_polygon,
US902928_GUAM_polygon,
US902928_ANTARTICA_polygon,
AS920923_ISRAEL_polygon,
US902928_MALDIVES_polygon,
EU863870_UKRAINE_polygon,
EU863870_NORTH_KOREA_polygon,
OUTSIDE_polygon,
n_polygons,
} Polygon_t;

/* ==================================================================== */
/* ======================= public functions =========================== */
/* ==================================================================== */

/* Function prototypes for public (external) functions go here */

void update_geofence_position(float latitude, float longitude);
void geofence_init_with_settings(bool *settings);
void geofence_init_with_settings(tx_permission_t *settings);
LoRaMacRegion_t get_current_loramac_region(void);
as923_subbands_t get_as923_subband(void);
void set_current_loramac_region(LoRaMacRegion_t region);
Expand Down
Loading