Skip to content

Commit

Permalink
Zephyr module update (#9)
Browse files Browse the repository at this point in the history
* Update Zephyr Module setup and include UDP interface

* Update README.md
  • Loading branch information
Mtalat committed Mar 6, 2023
1 parent d6bfa11 commit e509c92
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 9 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,36 @@ Given a version number MAJOR.MINOR.PATCH, increment the:
* MINOR version when you add functionality in a backward-compatible manner,
* PATCH version when you make backward-compatible bug fixes.

## Getting started
## Quick starting with Nordic nRF9160 Development Kit & Zephyr RTOS
1NCE IoT C SDK can be imported as a Zephyr module which is integrated with [nRF Connect SDK](https://www.nordicsemi.com/Products/Development-software/nrf-connect-sdk).

The following commands will import 1NCE IoT C SDK, Nordic nRF Connect SDK and [1NCE Zephyr blueprint](https://github.com/1NCE-GmbH/blueprint-zephyr) which includes CoAP, UDP & LwM2M Demos.
```
west init -m https://github.com/1NCE-GmbH/1nce-iot-c-sdk
west update
```
Then, using the quickstart command, you can directly build and flash the demos to a connected [nRF9160 Development Kit](https://www.nordicsemi.com/Products/Development-hardware/nrf9160-dk).

The Default demo establishes a secure connection to 1NCE endpoint via CoAP after receiving DTLS credentials from the Device Authenticator.
```
west quickstart
```
The command can also be used to run udp and lwm2m demos and to enable energy saving.

```
usage: west quickstart [-h] [-p PROTOCOL] [-e ENERGYSAVING]
Build and flash CoAP (With DTLS), LwM2M or UDP Demos, the payload reduction (using 1NCE Energy saver) can also be enabled.
options:
-h, --help show this help message and exit
-p PROTOCOL, --protocol PROTOCOL
Select the protocol: coap (Default), lwm2m or udp
-e ENERGYSAVING, --energysaving ENERGYSAVING
Set to "y" to enable energy saver
```

## Generic Getting started guide

**This section shows you:**

Expand Down
1 change: 1 addition & 0 deletions lexicon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ recv
recvbytes
repo
sdk
september
sni
ssh
stdlib
Expand Down
17 changes: 11 additions & 6 deletions zephyr/CMakeLists.txt → ports/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
# 1NCE IoT C SDK (ZEPHYR OS Module)
#

set(NCE_SDK_DIR ${ZEPHYR_CURRENT_MODULE_DIR})

set(NCE_SDK_ROOT ../..)

# set(NCE_SDK_DIR ${ZEPHYR_CURRENT_MODULE_DIR})

zephyr_library()
zephyr_include_directories(${NCE_SDK_DIR}/source/include)
zephyr_include_directories(${NCE_SDK_DIR}/source/interface)
zephyr_include_directories(${NCE_SDK_ROOT}/source/include)
zephyr_include_directories(${NCE_SDK_ROOT}/source/interface)
zephyr_library_sources(
${NCE_SDK_DIR}/source/nce_iot_c_sdk.c
${NCE_SDK_ROOT}/source/nce_iot_c_sdk.c
)

zephyr_compile_definitions_ifdef(CONFIG_NCE_DEVICE_AUTHENTICATOR NCE_DEVICE_AUTHENTICATOR)
Expand All @@ -17,6 +21,7 @@ zephyr_compile_definitions(
NCE_SDK_ATTEMPTS=${CONFIG_NCE_SDK_ATTEMPTS})
zephyr_compile_definitions(
NCE_SDK_MAX_STRING_SIZE=${CONFIG_NCE_SDK_MAX_STRING_SIZE})




zephyr_library_sources_ifdef(CONFIG_NCE_SDK_UDP_INTERFACE udp_interface_zephyr.c)
zephyr_include_directories(include)
6 changes: 6 additions & 0 deletions zephyr/Kconfig → ports/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ config NCE_DEVICE_AUTHENTICATOR
help
use 1nce device authenticator to retrieve DTLS Security credentials.

config NCE_SDK_UDP_INTERFACE
bool "Define a UDP interface for communication with 1NCE Endpoint"
default y if NCE_DEVICE_AUTHENTICATOR
help
Build and link common code that is shared across Golioth samples.

config NCE_ENERGY_SAVER
bool "Enable 1NCE Energy Saver"
default n
Expand Down
29 changes: 29 additions & 0 deletions ports/zephyr/include/udp_interface_zephyr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file udp_interface_zephyr.h
* @brief UDP interface definitions to send and receive data over the
* network via UDP in ZEPHYR OS.
*/


#include "udp_interface.h"

/**
* @typedef OSNetwork_t
*/
struct OSNetwork
{
int os_socket;
};

int nce_os_udp_connect( OSNetwork_t osnetwork,
OSEndPoint_t nce_oboarding );

int nce_os_udp_send( OSNetwork_t osnetwork,
void * pBuffer,
size_t bytesToSend );

int nce_os_udp_recv( OSNetwork_t osnetwork,
void * pBuffer,
size_t bytesToRecv );

int nce_os_udp_disconnect( OSNetwork_t osnetwork );
93 changes: 93 additions & 0 deletions ports/zephyr/udp_interface_zephyr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @file udp_interface_zephyr.c
* @brief Implements a UDP interface for ZEPHYR OS.
* @author Hatim Jamali & Mohamed Abdelmaksoud
* @date 01 September 2022
*/


#ifndef NCE_SDK_H_
#include <nce_iot_c_sdk.h>
#endif

#include <zephyr/kernel.h>
#include <stdio.h>
#include <modem/lte_lc.h>
#include <zephyr/net/socket.h>
#include "log_interface.h"
#include <udp_interface_zephyr.h>


LOG_MODULE_DECLARE( NCE_SDK, CONFIG_NCE_SDK_LOG_LEVEL );

/* Sample Network definitions */
struct OSNetwork xOSNetwork = { .os_socket = 0 };

int nce_os_udp_connect( OSNetwork_t osnetwork,
OSEndPoint_t nce_oboarding )
{
int socket = zsock_socket( AF_INET, SOCK_DGRAM,
IPPROTO_UDP );
int err;
struct zsock_addrinfo * addr;
struct zsock_addrinfo hints =
{
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM
};

err = getaddrinfo( nce_oboarding.host,
NULL, &hints, &addr );

NceOSLogDebug( "getaddrinfo status: %d\n", err );

osnetwork->os_socket = socket;

( ( struct sockaddr_in * ) addr->ai_addr )->sin_port = htons( nce_oboarding.port );

size_t peer_addr_size = sizeof( struct sockaddr_in );

err = zsock_connect( socket, addr->ai_addr,
peer_addr_size );
NceOSLogDebug( "UDP Socket Connect: %d\n", err );


return err;
}

int nce_os_udp_send( OSNetwork_t osnetwork,
void * pBuffer,
size_t bytesToSend )
{
int flags = 0;
int ret;

ret = zsock_send( osnetwork->os_socket, pBuffer, bytesToSend, flags );
NceOSLogDebug( "UDP Socket Send: %d\n", ret );

return ret;
}


int nce_os_udp_recv( OSNetwork_t osnetwork,
void * pBuffer,
size_t bytesToRecv )
{
int flags = 0;
int ret;

ret = zsock_recv( osnetwork->os_socket, pBuffer, bytesToRecv, flags );
NceOSLogDebug( "UDP Socket Receive: %d\n", ret );

return ret;
}

int nce_os_udp_disconnect( OSNetwork_t osnetwork )
{
int err;

err = zsock_close( osnetwork->os_socket );
NceOSLogDebug( "UDP Socket Disconnect: %d\n", err );

return err;
}
6 changes: 6 additions & 0 deletions scripts/west-commands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
west-commands:
- file: scripts/west_commands/quickstart.py
commands:
- name: quickstart
class: QuickStart
help: Build a 1NCE OS Demo and flash it to a connected nrf9160 DK
50 changes: 50 additions & 0 deletions scripts/west_commands/quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

from textwrap import dedent
from west.commands import WestCommand
from west import log
import subprocess

class QuickStart(WestCommand):

def __init__(self):
super().__init__(
'quickstart',
'Build a 1NCE OS Demo and flash it to a connected nrf9160 DK',
dedent(''' Build and flash CoAP (With DTLS), LwM2M or UDP Demos, the payload reduction (using 1NCE Energy saver) can also be enabled.
'''))

def do_add_parser(self, parser_adder):
parser = parser_adder.add_parser(self.name,
help=self.help,
description=self.description)
parser.add_argument('-p', '--protocol', help='Select the protocol: coap (Default), lwm2m or udp')
parser.add_argument('-e', '--energysaving', help='Set to "y" to enable energy saver')

return parser

def do_run(self, args, unknown_args):
cmd = "west build -b nrf9160dk_nrf9160_ns "
if (args.protocol is None or (args.protocol is not None and args.protocol.lower() == "coap") ):
log.inf('Building 1NCE CoAP Demo')
cmd = cmd + "nce-blueprint/nce_coap_demo --build-dir build_coap_demo "
if( (args.energysaving is not None and args.energysaving.lower() == "y") ):
cmd = cmd + "-DCONFIG_NCE_ENERGY_SAVER=y"
log.inf('1NCE Energy Saver is Enabled')
subprocess.run(cmd,shell=True)
subprocess.run("west flash",shell=True)
elif ( (args.protocol is not None and args.protocol.lower() == "udp") ):
log.inf('Building 1NCE UDP Demo')
cmd = cmd + "nce-blueprint/nce_udp_demo --build-dir build_udp_demo "
if((args.energysaving is not None and args.energysaving.lower() == "y")):
cmd = cmd + "-DCONFIG_NCE_ENERGY_SAVER=y"
log.inf('1NCE Energy Saver is Enabled')
subprocess.run(cmd,shell=True)
subprocess.run("west flash",shell=True)
elif ( (args.protocol is not None and args.protocol.lower() == "lwm2m") ):
log.inf('Building 1NCE LwM2M Demo')
cmd = cmd + "nce-blueprint/nce_lwm2m_demo --build-dir build_lwm2m_demo "
subprocess.run(cmd,shell=True)
subprocess.run("west flash",shell=True)
else :
log.err('Unsupported argument')

26 changes: 26 additions & 0 deletions west.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2021 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

manifest:
self:
path: modules/lib/nce-sdk
west-commands: scripts/west-commands.yml

remotes:
- name: ncs
url-base: https://github.com/nrfconnect
- name: nce
url-base: https://github.com/1NCE-GmbH

projects:
- name: nrf
remote: ncs
repo-path: sdk-nrf
revision: v2.2.0
import: true
- name: nce-blueprint
remote: nce
repo-path: blueprint-zephyr
revision: main


4 changes: 2 additions & 2 deletions zephyr/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name: nce-sdk

build:
cmake: zephyr
kconfig: zephyr/Kconfig
cmake: ports/zephyr
kconfig: ports/zephyr/Kconfig
depends:
- nrfxlib

0 comments on commit e509c92

Please sign in to comment.