Skip to content

Commit

Permalink
examples: zephyr: location: add simple cellular location demo
Browse files Browse the repository at this point in the history
Add a sample with cellular network cell towers collected over the city
trip. This is a playback with configurable speed (1x by default) to quickly
see results over time during testing (e.g. on a Grafana map plugin).

Signed-off-by: Marcin Niestroj <[email protected]>
  • Loading branch information
mniestroj committed Jan 2, 2025
1 parent c6f3fe9 commit 20c2ffb
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/zephyr/location/cellular/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(location_cellular)

target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_GOLIOTH_CELLULAR_PLAYBACK app PRIVATE src/cellular_playback.c)
25 changes: 25 additions & 0 deletions examples/zephyr/location/cellular/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2021-2024 Golioth, Inc.
# SPDX-License-Identifier: Apache-2.0

source "${ZEPHYR_GOLIOTH_FIRMWARE_SDK_MODULE_DIR}/examples/zephyr/common/Kconfig.defconfig"

config GOLIOTH_CELLULAR_PLAYBACK
bool
default y
depends on DT_HAS_GOLIOTH_CELLULAR_PLAYBACK_ENABLED

if GOLIOTH_CELLULAR_PLAYBACK

config GOLIOTH_CELLULAR_PLAYBACK_SPEED_FACTOR
int "Speed factor"
default 1000
help
Speed of playback.

1000 means 1x (normal) speed.
3000 means 3x speed.
500 means 0.5x speed.

endif # GOLIOTH_CELLULAR_PLAYBACK

source "Kconfig.zephyr"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Golioth, Inc.
# SPDX-License-Identifier: Apache-2.0

description: Cellular driver with playback of scanned cellular networks

compatible: "golioth,cellular-playback"

include: base.yaml
15 changes: 15 additions & 0 deletions examples/zephyr/location/cellular/dts/bindings/vendor-prefixes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Device tree binding vendor prefix registry. Keep this list in
# alphabetical order.
#
# This isn't an exhaustive list, but you should add new prefixes to it
# before using them to avoid name-space collisions.
#
# The contents of this file are parsed during documentation generation.
# Anything that starts with a '#' is treated as a comment and ignored.
# Non-empty lines should be in this format:
#
# <vendor-prefix><TAB><Full name of vendor>

# zephyr-keep-sorted-start
golioth Golioth, Inc.
# zephyr-keep-sorted-stop
9 changes: 9 additions & 0 deletions examples/zephyr/location/cellular/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Application
CONFIG_GOLIOTH_SAMPLE_COMMON=y
CONFIG_GOLIOTH_SAMPLE_HARDCODED_CREDENTIALS=y

# Golioth Firmware SDK with all dependencies
CONFIG_GOLIOTH_FIRMWARE_SDK=y

CONFIG_GOLIOTH_LOCATION=y
CONFIG_GOLIOTH_LOCATION_CELLULAR=y
15 changes: 15 additions & 0 deletions examples/zephyr/location/cellular/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sample:
description: Location Cellular
name: location cellular
common:
build_only: true
tags:
- golioth
- location
- socket
tests:
sample.golioth.location_cellular:
platform_allow:
- native_sim
- native_sim/native/64
- nrf9160dk/nrf9160/ns
19 changes: 19 additions & 0 deletions examples/zephyr/location/cellular/socs/native.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Use offloaded sockets using host BSD sockets
CONFIG_ETH_DRIVER=n
CONFIG_ETH_NATIVE_POSIX=n
CONFIG_NET_DRIVERS=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y
CONFIG_HEAP_MEM_POOL_SIZE=1024

# Use embedded libc to use Zephyr's eventfd instead of host eventfd
CONFIG_PICOLIBC=y

# Static networking
CONFIG_NET_CONFIG_LOG_LEVEL_DBG=y
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
6 changes: 6 additions & 0 deletions examples/zephyr/location/cellular/socs/native.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/ {
cellular-playback {
compatible = "golioth,cellular-playback";
status = "okay";
};
};
19 changes: 19 additions & 0 deletions examples/zephyr/location/cellular/socs/native_64.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Use offloaded sockets using host BSD sockets
CONFIG_ETH_DRIVER=n
CONFIG_ETH_NATIVE_POSIX=n
CONFIG_NET_DRIVERS=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y
CONFIG_HEAP_MEM_POOL_SIZE=1024

# Use embedded libc to use Zephyr's eventfd instead of host eventfd
CONFIG_PICOLIBC=y

# Static networking
CONFIG_NET_CONFIG_LOG_LEVEL_DBG=y
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
1 change: 1 addition & 0 deletions examples/zephyr/location/cellular/socs/native_64.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "native.overlay"
16 changes: 16 additions & 0 deletions examples/zephyr/location/cellular/src/cellular.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 Golioth, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __CELLULAR_H__
#define __CELLULAR_H__

#include <golioth/location/cellular.h>

int cellular_info_get(struct golioth_cellular_info *infos,
size_t num_max_infos,
size_t *num_returned_infos);

#endif /* __CELLULAR_H__ */
73 changes: 73 additions & 0 deletions examples/zephyr/location/cellular/src/cellular_playback.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 Golioth, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(cellular_playback, LOG_LEVEL_DBG);

#include <zephyr/net/net_offload.h>

#include "cellular.h"

struct cellular_playback_info
{
int64_t ts;
const struct golioth_cellular_info *infos;
size_t num_infos;
};

struct cellular_playback_config
{
const struct cellular_playback_info *infos;
size_t num_infos;
};

struct cellular_playback_data
{
const struct cellular_playback_config *config;
size_t info_idx;
};

static const struct cellular_playback_config cellular_playback_config_0;
static struct cellular_playback_data cellular_playback_data_0;

int cellular_info_get(struct golioth_cellular_info *infos,
size_t num_max_infos,
size_t *num_returned_infos)
{
struct cellular_playback_data *data = &cellular_playback_data_0;
const struct cellular_playback_config *config = data->config;
const struct cellular_playback_info *playback_info = &config->infos[data->info_idx];
size_t num_copied_infos = MIN(playback_info->num_infos, num_max_infos);

k_sleep(
K_TIMEOUT_ABS_MS(playback_info->ts * 1000 / CONFIG_GOLIOTH_CELLULAR_PLAYBACK_SPEED_FACTOR));

memcpy(infos, playback_info->infos, num_copied_infos * sizeof(*infos));
*num_returned_infos = num_copied_infos;

data->info_idx++;

return 0;
}

static int cellular_playback_init(void)
{
const struct cellular_playback_config *config = &cellular_playback_config_0;
struct cellular_playback_data *data = &cellular_playback_data_0;

data->config = config;

return 0;
}

#include "cellular_playback_input.c"

static const struct cellular_playback_config cellular_playback_config_0 = {
.infos = cellular_infos,
.num_infos = ARRAY_SIZE(cellular_infos),
};

SYS_INIT(cellular_playback_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
Loading

0 comments on commit 20c2ffb

Please sign in to comment.