From 0ae2f2f2f8444c2967326e7ef3e95b3ab782dc4c Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Mon, 29 Jun 2020 11:08:41 +0530 Subject: [PATCH] samples: mgmt/osdp: Add new OSDP PD sample This commit adds a PD sample for drivers/osdp. It receives OSDP commands and prints a message to console. Signed-off-by: Siddharth Chandrasekaran --- samples/subsys/mgmt/osdp/CMakeLists.txt | 12 +++++ samples/subsys/mgmt/osdp/README.rst | 26 +++++++++ samples/subsys/mgmt/osdp/prj.conf | 11 ++++ samples/subsys/mgmt/osdp/src/main.c | 71 +++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 samples/subsys/mgmt/osdp/CMakeLists.txt create mode 100644 samples/subsys/mgmt/osdp/README.rst create mode 100644 samples/subsys/mgmt/osdp/prj.conf create mode 100644 samples/subsys/mgmt/osdp/src/main.c diff --git a/samples/subsys/mgmt/osdp/CMakeLists.txt b/samples/subsys/mgmt/osdp/CMakeLists.txt new file mode 100644 index 0000000000..de727deacf --- /dev/null +++ b/samples/subsys/mgmt/osdp/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2020 Siddharth Chandrasekaran +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) + +project(osdp_sample) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/subsys/mgmt/osdp/README.rst b/samples/subsys/mgmt/osdp/README.rst new file mode 100644 index 0000000000..81f326f8e9 --- /dev/null +++ b/samples/subsys/mgmt/osdp/README.rst @@ -0,0 +1,26 @@ +.. _osdp-sample: + +OSDP +#### + +OSDP describes the communication interface one or more Peripheral Devices (PD) +to a Control Panel (CP). The specification describes the protocol +implementation over a two-wire RS-485 multi-drop serial communication channel. +Nevertheless, this protocol can be used to transfer secure byte stream over any +physical channel in low memory embedded devices. + +Although OSDP is steered towards the Access and Security industries, it can be +used as a general communication protocol for devices in a secure way without +too much resource requirements. The security is not top-notch (AES-128) but it +is reasonable enough, given that the alternative is no security at all. + +OSDP Supports the control of the following components on a PD: + - LED + - Buzzer + - Keypad + - Output (GPIOs) + - Input Control (GPIOs) + - Displays + - Device status (tamper, power, etc.,) + - Card Reader + - Fingerprint Reader diff --git a/samples/subsys/mgmt/osdp/prj.conf b/samples/subsys/mgmt/osdp/prj.conf new file mode 100644 index 0000000000..5accf7859e --- /dev/null +++ b/samples/subsys/mgmt/osdp/prj.conf @@ -0,0 +1,11 @@ +# +# Copyright (c) 2020 Siddharth Chandrasekaran +# +# SPDX-License-Identifier: Apache-2.0 +# + +CONFIG_PRINTK=y +CONFIG_LOG=y + +# OSDP config +CONFIG_OSDP=y diff --git a/samples/subsys/mgmt/osdp/src/main.c b/samples/subsys/mgmt/osdp/src/main.c new file mode 100644 index 0000000000..5b0dcc2445 --- /dev/null +++ b/samples/subsys/mgmt/osdp/src/main.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2020 Siddharth Chandrasekaran + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +/* The devicetree node identifier for the "led0" alias. */ +#define LED0_NODE DT_ALIAS(led0) + +#if DT_NODE_HAS_STATUS(LED0_NODE, okay) +#define LED0 DT_GPIO_LABEL(LED0_NODE, gpios) +#define PIN DT_GPIO_PIN(LED0_NODE, gpios) +#if DT_PHA_HAS_CELL(LED0_NODE, gpios, flags) +#define FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios) +#endif +#else +#error "BOARD does not define a debug LED" +#define LED0 "" +#define PIN 0 +#endif + +#ifndef FLAGS +#define FLAGS 0 +#endif + +#define SLEEP_TIME_MS 10 + +int cmd_handler(struct osdp_cmd *p) +{ + printk("App received command %d\n", p->id); + return 0; +} + +void main(void) +{ + int ret, led_state; + uint32_t cnt = 0; + struct device *dev; + struct osdp_cmd cmd; + + dev = device_get_binding(LED0); + if (dev == NULL) { + printk("Failed to get LED0 binding\n"); + return; + } + + ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS); + if (ret < 0) { + printk("Failed to configure gpio pin\n"); + return; + } + + led_state = 0; + while (1) { + if ((cnt & 0x7f) == 0x7f) { + /* show a sign of life */ + led_state = !led_state; + } + if (osdp_pd_get_cmd(&cmd) == 0) { + cmd_handler(&cmd); + } + gpio_pin_set(dev, PIN, led_state); + k_msleep(SLEEP_TIME_MS); + cnt++; + } +}