Feature/raspberry pi blinkt color objects (#503)
* fixed BACnetXYcolor and BACnetColorCommand encode and decoding and improved unit test coverage. Refactored BACnetXYcolor to/from ascii into lighting module. * added to the color, color temperature, and lighting output objects a fade/ramp/step engine. Added color and color command coercion into the channel object and enabled color temperature object coercion. Added CreateObject and DeleteObject service handling to the color, color temperature, lighting output, and channel objects. * added blinkt demo app for Raspberry Pi [WIP] * updated gitignore to simplify handling of apps folder contents * fixed piface demo build * added RaspiOS to pipeline for piface and blinkt! demo builds * added device object timer function for child object types into example Device object. Refactored device object to increment database revision for create or delete object services. Refactored example app/server to use mstimer library and device child object timers. --------- Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
+10
-2
@@ -215,14 +215,14 @@ endif
|
||||
#####
|
||||
|
||||
# AFL
|
||||
fuzz-afl: CC=afl-gcc
|
||||
fuzz-afl: CC=afl-gcc
|
||||
fuzz-afl: FUZZ_FLAGS=-DFUZZING=1
|
||||
fuzz-afl: LFLAGS += $(FUZZ_FLAGS)
|
||||
fuzz-afl: CFLAGS += $(FUZZ_FLAGS)
|
||||
fuzz-afl: export AFL_USE_ASAN=1
|
||||
|
||||
# LIBFUZZER
|
||||
fuzz-libfuzzer: CC=clang
|
||||
fuzz-libfuzzer: CC=clang
|
||||
fuzz-libfuzzer: FUZZ_FLAGS=-DFUZZING=1 -fsanitize=fuzzer,address -g3 -Og -fno-optimize-sibling-calls -fno-omit-frame-pointer
|
||||
fuzz-libfuzzer: LFLAGS += $(FUZZ_FLAGS)
|
||||
fuzz-libfuzzer: CFLAGS += $(FUZZ_FLAGS)
|
||||
@@ -263,6 +263,10 @@ ack-alarm: $(BACNET_LIB_TARGET)
|
||||
add-list-element: $(BACNET_LIB_TARGET)
|
||||
$(MAKE) -B -C $@ clean all
|
||||
|
||||
.PHONY: blinkt
|
||||
blinkt:
|
||||
$(MAKE) -C $@
|
||||
|
||||
.PHONY: dcc
|
||||
dcc: $(BACNET_LIB_TARGET)
|
||||
$(MAKE) -B -C $@
|
||||
@@ -311,6 +315,10 @@ mstpcap:
|
||||
mstpcrc:
|
||||
$(MAKE) -B -C $@
|
||||
|
||||
.PHONY: piface
|
||||
piface:
|
||||
$(MAKE) -B -C $@
|
||||
|
||||
.PHONY: ptransfer
|
||||
ptransfer: $(BACNET_LIB_TARGET)
|
||||
$(MAKE) -B -C $@
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 132 KiB |
@@ -0,0 +1,56 @@
|
||||
###############################################################################
|
||||
# Makefile for Project - BACnet Blinkt!
|
||||
# sudo apt install libpigpio-dev libpigpiod-if-dev pigpiod
|
||||
###############################################################################
|
||||
|
||||
## General Flags
|
||||
TARGET = bacblinkt
|
||||
# BACnet objects that are used with this app
|
||||
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
|
||||
SRC = main.c \
|
||||
blinkt.c \
|
||||
device.c \
|
||||
$(BACNET_OBJECT_DIR)/netport.c \
|
||||
$(BACNET_OBJECT_DIR)/channel.c \
|
||||
$(BACNET_OBJECT_DIR)/color_object.c \
|
||||
$(BACNET_OBJECT_DIR)/color_temperature.c \
|
||||
$(BACNET_OBJECT_DIR)/lc.c \
|
||||
$(BACNET_OBJECT_DIR)/lo.c
|
||||
|
||||
CFLAGS += -DMAX_TSM_TRANSACTIONS=1
|
||||
|
||||
# TARGET_EXT is defined in apps/Makefile as .exe or nothing
|
||||
TARGET_BIN = ${TARGET}$(TARGET_EXT)
|
||||
|
||||
# note: link to pigpio daemon so that our app can run without root
|
||||
# start pigio daemon with the command 'sudo pigpiod'
|
||||
LD_PIGPIO = -Wl,-pthread,-lpigpiod_if2
|
||||
LFLAGS += $(LD_PIGPIO)
|
||||
|
||||
OBJS += ${SRC:.c=.o}
|
||||
|
||||
all: ${BACNET_LIB_TARGET} Makefile ${TARGET_BIN}
|
||||
|
||||
${TARGET_BIN}: ${OBJS} Makefile ${BACNET_LIB_TARGET}
|
||||
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
|
||||
size $@
|
||||
cp $@ ../../bin
|
||||
|
||||
${BACNET_LIB_TARGET}:
|
||||
( cd ${BACNET_LIB_DIR} ; $(MAKE) clean ; $(MAKE) -s )
|
||||
|
||||
.c.o:
|
||||
${CC} -c ${CFLAGS} $*.c -o $@
|
||||
|
||||
.PHONY: depend
|
||||
depend:
|
||||
rm -f .depend
|
||||
${CC} -MM ${CFLAGS} *.c >> .depend
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f core ${TARGET_BIN} ${OBJS} $(TARGET).map ${BACNET_LIB_TARGET}
|
||||
|
||||
.PHONY: include
|
||||
include: .depend
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Raspberry Pi connected to a Blinkt! RGB card demo
|
||||
|
||||
This demo includes a BACnet server with Color objects and service handlers
|
||||
for the eight RGB (red-green-blue) LEDs attached to the Blinkt! card.
|
||||
|
||||
## Installation
|
||||
|
||||
The demo uses pigpiod (Pi GPIO Daemon) and developer library. To install
|
||||
and run the daemon at powerup (and immediately):
|
||||
|
||||
$ sudo apt install libpigpio-dev libpigpiod-if-dev pigpiod
|
||||
$ sudo systemctl enable pigpiod
|
||||
$ sudo systemctl start pigpiod
|
||||
|
||||
## Building
|
||||
|
||||
Build from the root folder:
|
||||
|
||||
$ make blinkt
|
||||
|
||||
## Running
|
||||
|
||||
Run from the bin/ folder:
|
||||
|
||||
$ ./bin/bacblinkt 9009
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief API for Blinkt! daughter board for Raspberry Pi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <pigpiod_if2.h>
|
||||
#include "blinkt.h"
|
||||
|
||||
#define BLINKT_DEFAULT_BRIGHTNESS 7
|
||||
#define BLINKT_NUM_LEDS 8
|
||||
|
||||
/* GPIO pin numbers */
|
||||
#define BLINKT_MOSI 23
|
||||
#define BLINKT_SCLK 24
|
||||
|
||||
/* RGBb data for each LED */
|
||||
static uint32_t Blinkt_LED[BLINKT_NUM_LEDS];
|
||||
/* handle to the pigpiod */
|
||||
static int Blinkt_Pi;
|
||||
|
||||
/**
|
||||
* @brief Get the number of LEDS
|
||||
* @return Number of LEDs
|
||||
*/
|
||||
uint8_t blinkt_led_count(void)
|
||||
{
|
||||
return BLINKT_NUM_LEDS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set all the LEDS to default brightness (7/31)
|
||||
*/
|
||||
void blinkt_clear(void)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < BLINKT_NUM_LEDS; x++) {
|
||||
Blinkt_LED[x] = BLINKT_DEFAULT_BRIGHTNESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set one LED to specific RGB color
|
||||
* @param led index 0..#BLINKT_NUM_LEDS
|
||||
* @param r color red from 0..255
|
||||
* @param g color green from 0..255
|
||||
* @param b color blue from 0..255
|
||||
*/
|
||||
void blinkt_set_pixel(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
if (led >= BLINKT_NUM_LEDS)
|
||||
return;
|
||||
|
||||
Blinkt_LED[led] = blinkt_rgbb(r, g, b, Blinkt_LED[led] & 0x1F);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set one LED to specific intensity
|
||||
* @param led index 0..#BLINKT_NUM_LEDS
|
||||
* @param brightness intensity from 0..31, 0=OFF, 1=dimmest, 31=brightest
|
||||
*/
|
||||
void blinkt_set_pixel_brightness(uint8_t led, uint8_t brightness)
|
||||
{
|
||||
if (led >= BLINKT_NUM_LEDS)
|
||||
return;
|
||||
|
||||
Blinkt_LED[led] = (Blinkt_LED[led] & 0xFFFFFF00) | (brightness & 0x1F);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set one LED to RGB color and brightness
|
||||
* @param led index 0..#BLINKT_NUM_LEDS
|
||||
* @param color encoded as 32-bit RGBb (red | green | blue | brightness)
|
||||
*/
|
||||
void blinkt_set_pixel_uint32(uint8_t led, uint32_t color)
|
||||
{
|
||||
if (led >= BLINKT_NUM_LEDS)
|
||||
return;
|
||||
|
||||
Blinkt_LED[led] = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode RGB color and brightness into 32-bit RGBb
|
||||
* @param r color red from 0..255
|
||||
* @param g color green from 0..255
|
||||
* @param b color blue from 0..255
|
||||
* @param brightness intensity from 0..31, 0=OFF, 1=dimmest, 31=brightest
|
||||
* @return color encoded as 32-bit RGBb (red | green | blue | brightness)
|
||||
*/
|
||||
uint32_t blinkt_rgbb(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
result = (brightness & 0x1F);
|
||||
result |= ((uint32_t)r << 24);
|
||||
result |= ((uint32_t)g << 16);
|
||||
result |= ((uint16_t)b << 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode RGB color at default brightness into 32-bit RGBb
|
||||
* @param r color red from 0..255
|
||||
* @param g color green from 0..255
|
||||
* @param b color blue from 0..255
|
||||
* @return color encoded as 32-bit RGBb (red | green | blue | brightness)
|
||||
*/
|
||||
uint32_t blinkt_rgb(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
return blinkt_rgbb(r, g, b, BLINKT_DEFAULT_BRIGHTNESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write a byte via shift register to LEDs
|
||||
* @param byte to be written
|
||||
*/
|
||||
inline static void write_byte(uint8_t byte)
|
||||
{
|
||||
int n;
|
||||
for (n = 0; n < 8; n++) {
|
||||
gpio_write(Blinkt_Pi, BLINKT_MOSI, (byte & (1 << (7 - n))) > 0);
|
||||
gpio_write(Blinkt_Pi, BLINKT_SCLK, 1);
|
||||
gpio_write(Blinkt_Pi, BLINKT_SCLK, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Shift LED values to the actual LEDs via shift registers
|
||||
*/
|
||||
void blinkt_show(void)
|
||||
{
|
||||
int x;
|
||||
|
||||
write_byte(0);
|
||||
write_byte(0);
|
||||
write_byte(0);
|
||||
write_byte(0);
|
||||
for (x = 0; x < BLINKT_NUM_LEDS; x++) {
|
||||
write_byte(0xE0 | (Blinkt_LED[x] & 0x1F));
|
||||
write_byte((Blinkt_LED[x] >> 8) & 0xFF);
|
||||
write_byte((Blinkt_LED[x] >> 16) & 0xFF);
|
||||
write_byte((Blinkt_LED[x] >> 24) & 0xFF);
|
||||
}
|
||||
write_byte(0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disable the GPIO hardware to the Blinkt! board
|
||||
*/
|
||||
void blinkt_stop(void)
|
||||
{
|
||||
/* Disconnect from local Pi. */
|
||||
pigpio_stop(Blinkt_Pi);
|
||||
printf("PiGPIO stopped\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the GPIO hardware for the Blinkt! board
|
||||
*/
|
||||
int blinkt_init(void)
|
||||
{
|
||||
char *optHost = NULL;
|
||||
char *optPort = NULL;
|
||||
|
||||
/* Connect to local Pi. */
|
||||
Blinkt_Pi = pigpio_start(optHost, optPort);
|
||||
if (Blinkt_Pi < 0) {
|
||||
perror("PiGPIO failed to start!");
|
||||
return -1;
|
||||
}
|
||||
set_mode(Blinkt_Pi, BLINKT_MOSI, PI_OUTPUT);
|
||||
gpio_write(Blinkt_Pi, BLINKT_MOSI, 0);
|
||||
set_mode(Blinkt_Pi, BLINKT_SCLK, PI_OUTPUT);
|
||||
gpio_write(Blinkt_Pi, BLINKT_SCLK, 0);
|
||||
blinkt_clear();
|
||||
printf("PiGPIO started\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_column = 0;
|
||||
static int test_z, test_y;
|
||||
/**
|
||||
* @brief Test the Blinkt! board with a simple changing pattern
|
||||
*/
|
||||
void blinkt_test_task(void)
|
||||
{
|
||||
for (test_z = 0; test_z < BLINKT_NUM_LEDS; test_z++) {
|
||||
switch (test_column) {
|
||||
case 0:
|
||||
blinkt_set_pixel_uint32(test_z, blinkt_rgb(test_y, 0, 0));
|
||||
break;
|
||||
case 1:
|
||||
blinkt_set_pixel_uint32(test_z, blinkt_rgb(0, test_y, 0));
|
||||
break;
|
||||
case 2:
|
||||
blinkt_set_pixel_uint32(test_z, blinkt_rgb(0, 0, test_y));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
blinkt_show();
|
||||
test_y += 1;
|
||||
if (test_y > 254)
|
||||
test_column++;
|
||||
test_column %= 3;
|
||||
test_y %= 255;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief API for Blinkt! daughter board for Raspberry Pi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#ifndef BLINKT_H
|
||||
#define BLINKT_H
|
||||
|
||||
void blinkt_clear(void);
|
||||
void blinkt_set_pixel_uint32(uint8_t led, uint32_t color);
|
||||
void blinkt_set_pixel(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
|
||||
void blinkt_set_pixel_brightness(uint8_t led, uint8_t brightness);
|
||||
uint32_t blinkt_rgbb(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness);
|
||||
uint32_t blinkt_rgb(uint8_t r, uint8_t g, uint8_t b);
|
||||
void blinkt_stop(void);
|
||||
void blinkt_show(void);
|
||||
uint8_t blinkt_led_count(void);
|
||||
|
||||
int blinkt_init(void);
|
||||
void blinkt_test_task(void);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,465 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include "bacnet/config.h"
|
||||
#include "bacnet/basic/binding/address.h"
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/basic/services.h"
|
||||
#include "bacnet/basic/services.h"
|
||||
#include "bacnet/datalink/dlenv.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
#include "bacnet/npdu.h"
|
||||
#include "bacnet/apdu.h"
|
||||
#include "bacnet/iam.h"
|
||||
#include "bacnet/basic/tsm/tsm.h"
|
||||
#include "bacnet/basic/object/device.h"
|
||||
#include "bacnet/basic/object/bacfile.h"
|
||||
#include "bacnet/datalink/datalink.h"
|
||||
#include "bacnet/dcc.h"
|
||||
#include "bacnet/getevent.h"
|
||||
#include "bacnet/lighting.h"
|
||||
#include "bacport.h"
|
||||
#include "bacnet/basic/sys/mstimer.h"
|
||||
#include "bacnet/basic/sys/color_rgb.h"
|
||||
#include "bacnet/basic/sys/filename.h"
|
||||
#include "bacnet/basic/sys/linear.h"
|
||||
#include "bacnet/basic/tsm/tsm.h"
|
||||
#include "bacnet/version.h"
|
||||
/* include the device object */
|
||||
#include "bacnet/basic/object/device.h"
|
||||
#include "bacnet/basic/object/lo.h"
|
||||
#include "bacnet/basic/object/channel.h"
|
||||
#include "bacnet/basic/object/color_object.h"
|
||||
#include "bacnet/basic/object/color_temperature.h"
|
||||
#include "blinkt.h"
|
||||
|
||||
/** @file blinkt/main.c Example application using the BACnet Stack. */
|
||||
|
||||
/* (Doxygen note: The next two lines pull all the following Javadoc
|
||||
* into the ServerDemo module.) */
|
||||
/** @addtogroup ServerDemo */
|
||||
/*@{*/
|
||||
|
||||
/** Buffer used for receiving */
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
|
||||
/* current version of the BACnet stack */
|
||||
static const char *BACnet_Version = BACNET_VERSION_TEXT;
|
||||
/* task timer for various BACnet timeouts */
|
||||
static struct mstimer BACnet_Task_Timer;
|
||||
/* task timer for TSM timeouts */
|
||||
static struct mstimer BACnet_TSM_Timer;
|
||||
/* task timer for address binding timeouts */
|
||||
static struct mstimer BACnet_Address_Timer;
|
||||
/* task timer for object functionality */
|
||||
static struct mstimer BACnet_Object_Timer;
|
||||
|
||||
/** Initialize the handlers we will utilize.
|
||||
* @see Device_Init, apdu_set_unconfirmed_handler, apdu_set_confirmed_handler
|
||||
*/
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
Device_Init(NULL);
|
||||
/* we need to handle who-is to support dynamic device binding */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has);
|
||||
/* handle i-am to support binding to other devices */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind);
|
||||
/* set the handler for all the services we don't implement */
|
||||
/* It is required to send the proper reject message... */
|
||||
apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service);
|
||||
/* Set the handlers for any confirmed services that we support. */
|
||||
/* We must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_WRITE_PROP_MULTIPLE, handler_write_property_multiple);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_RANGE, handler_read_range);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device);
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc);
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_SUBSCRIBE_COV, handler_cov_subscribe);
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_COV_NOTIFICATION, handler_ucov_notification);
|
||||
/* handle communication so we can shutup when asked */
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
|
||||
handler_device_communication_control);
|
||||
/* handle the data coming back from private requests */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_PRIVATE_TRANSFER,
|
||||
handler_unconfirmed_private_transfer);
|
||||
/* configure the cyclic timers */
|
||||
mstimer_set(&BACnet_Task_Timer, 1000UL);
|
||||
mstimer_set(&BACnet_TSM_Timer, 50UL);
|
||||
mstimer_set(&BACnet_Address_Timer, 60UL * 1000UL);
|
||||
mstimer_set(&BACnet_Object_Timer, 100UL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the Blinkt! interface
|
||||
*/
|
||||
static void blinkt_cleanup(void)
|
||||
{
|
||||
blinkt_stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for tracking value
|
||||
* @param object_instance - object-instance number of the object
|
||||
* @param old_value - Color temperature value prior to write
|
||||
* @param value - Color temperature value of the write
|
||||
*/
|
||||
static void Lighting_Output_Write_Value_Handler(
|
||||
uint32_t object_instance, float old_value, float value)
|
||||
{
|
||||
uint8_t index = 255;
|
||||
uint8_t brightness;
|
||||
|
||||
(void)old_value;
|
||||
if (object_instance > 0) {
|
||||
index = object_instance - 1;
|
||||
}
|
||||
if (index < blinkt_led_count()) {
|
||||
/* brightness intensity from 0..31, 0=OFF, 1=dimmest, 31=brightest */
|
||||
if (isgreaterequal(value, 1.0)) {
|
||||
brightness = linear_interpolate(1.0, value, 100.0, 1, 31);
|
||||
} else {
|
||||
brightness = 0;
|
||||
}
|
||||
blinkt_set_pixel_brightness(index, brightness);
|
||||
printf("LED[%u]=%.1f%% (%u)\n", (unsigned)index, value,
|
||||
(unsigned)brightness);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for tracking value
|
||||
* @param object_instance - object-instance number of the object
|
||||
* @param old_value - Color temperature value prior to write
|
||||
* @param value - Color temperature value of the write
|
||||
*/
|
||||
static void Color_Temperature_Write_Value_Handler(
|
||||
uint32_t object_instance, uint32_t old_value, uint32_t value)
|
||||
{
|
||||
uint8_t red, green, blue;
|
||||
uint8_t index = 255;
|
||||
|
||||
(void)old_value;
|
||||
if (object_instance > 0) {
|
||||
index = object_instance - 1;
|
||||
}
|
||||
if (index < blinkt_led_count()) {
|
||||
color_rgb_from_temperature(value, &red, &green, &blue);
|
||||
blinkt_set_pixel(index, red, green, blue);
|
||||
printf("%u Kelvin RGB[%u]=%u,%u,%u\n", (unsigned)value, (unsigned)index,
|
||||
(unsigned)red, (unsigned)green, (unsigned)blue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for tracking value
|
||||
* @param object_instance - object-instance number of the object
|
||||
* @param old_value - BACnetXYColor value prior to write
|
||||
* @param value - BACnetXYColor value of the write
|
||||
*/
|
||||
static void Color_Write_Value_Handler(uint32_t object_instance,
|
||||
BACNET_XY_COLOR *old_value,
|
||||
BACNET_XY_COLOR *value)
|
||||
{
|
||||
uint8_t red, green, blue;
|
||||
float brightness_percent = 100.0;
|
||||
uint8_t index = 255;
|
||||
|
||||
(void)old_value;
|
||||
if (object_instance > 0) {
|
||||
index = object_instance - 1;
|
||||
}
|
||||
if (index < blinkt_led_count()) {
|
||||
color_rgb_from_xy(&red, &green, &blue, value->x_coordinate,
|
||||
value->y_coordinate, brightness_percent);
|
||||
blinkt_set_pixel(index, red, green, blue);
|
||||
printf("x,y=%0.2f,%0.2f(%.1f%%) RGB[%u]=%u,%u,%u\n",
|
||||
value->x_coordinate, value->y_coordinate, brightness_percent,
|
||||
(unsigned)index, (unsigned)red, (unsigned)green, (unsigned)blue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create the objects and configure the callbacks for BACnet objects
|
||||
*/
|
||||
static void bacnet_output_init(void)
|
||||
{
|
||||
unsigned i = 0;
|
||||
uint8_t led_max;
|
||||
uint32_t object_instance = 1;
|
||||
BACNET_COLOR_COMMAND command = { 0 };
|
||||
BACNET_OBJECT_ID object_id;
|
||||
uint32_t light_channel_instance = 1;
|
||||
uint32_t color_channel_instance = 2;
|
||||
uint32_t temp_channel_instance = 3;
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE member;
|
||||
|
||||
Channel_Create(light_channel_instance);
|
||||
Channel_Name_Set(light_channel_instance, "Lights");
|
||||
Channel_Create(color_channel_instance);
|
||||
Channel_Name_Set(color_channel_instance, "Colors");
|
||||
Channel_Create(temp_channel_instance);
|
||||
Channel_Name_Set(temp_channel_instance, "Color-Temperatures");
|
||||
led_max = blinkt_led_count();
|
||||
for (i = 0; i < led_max; i++) {
|
||||
/* color */
|
||||
Color_Create(object_instance);
|
||||
Color_Write_Enable(object_instance);
|
||||
/* fade to black */
|
||||
Color_Command(object_instance, &command);
|
||||
command.operation = BACNET_COLOR_OPERATION_FADE_TO_COLOR;
|
||||
command.target.color.x_coordinate = 0.0;
|
||||
command.target.color.y_coordinate = 0.0;
|
||||
command.transit.fade_time = 0;
|
||||
Color_Command_Set(object_instance, &command);
|
||||
|
||||
/* configure channel members */
|
||||
member.objectIdentifier.type = OBJECT_COLOR;
|
||||
member.objectIdentifier.instance = object_instance;
|
||||
member.propertyIdentifier = PROP_PRESENT_VALUE;
|
||||
member.arrayIndex = BACNET_ARRAY_ALL;
|
||||
member.deviceIdentifier.type = OBJECT_DEVICE;
|
||||
member.deviceIdentifier.instance = Device_Object_Instance_Number();
|
||||
Channel_Reference_List_Member_Element_Set(
|
||||
color_channel_instance, 1 + i, &member);
|
||||
|
||||
/* color temperature */
|
||||
Color_Temperature_Create(object_instance);
|
||||
Color_Temperature_Write_Enable(object_instance);
|
||||
/* stop the color temperature */
|
||||
Color_Temperature_Command(object_instance, &command);
|
||||
command.operation = BACNET_COLOR_OPERATION_STOP;
|
||||
Color_Temperature_Command_Set(object_instance, &command);
|
||||
|
||||
/* configure channel members */
|
||||
member.objectIdentifier.type = OBJECT_COLOR_TEMPERATURE;
|
||||
member.objectIdentifier.instance = object_instance;
|
||||
member.propertyIdentifier = PROP_PRESENT_VALUE;
|
||||
member.arrayIndex = BACNET_ARRAY_ALL;
|
||||
member.deviceIdentifier.type = OBJECT_DEVICE;
|
||||
member.deviceIdentifier.instance = Device_Object_Instance_Number();
|
||||
Channel_Reference_List_Member_Element_Set(
|
||||
temp_channel_instance, 1 + i, &member);
|
||||
|
||||
/* lighting output */
|
||||
Lighting_Output_Create(object_instance);
|
||||
|
||||
/* configure references */
|
||||
object_id.type = OBJECT_COLOR;
|
||||
object_id.instance = object_instance;
|
||||
Lighting_Output_Color_Reference_Set(object_instance, &object_id);
|
||||
|
||||
/* configure channel members */
|
||||
member.objectIdentifier.type = OBJECT_LIGHTING_OUTPUT;
|
||||
member.objectIdentifier.instance = object_instance;
|
||||
member.propertyIdentifier = PROP_PRESENT_VALUE;
|
||||
member.arrayIndex = BACNET_ARRAY_ALL;
|
||||
member.deviceIdentifier.type = OBJECT_DEVICE;
|
||||
member.deviceIdentifier.instance = Device_Object_Instance_Number();
|
||||
Channel_Reference_List_Member_Element_Set(
|
||||
light_channel_instance, 1 + i, &member);
|
||||
|
||||
object_instance++;
|
||||
}
|
||||
Color_Write_Present_Value_Callback_Set(Color_Write_Value_Handler);
|
||||
Color_Temperature_Write_Present_Value_Callback_Set(
|
||||
Color_Temperature_Write_Value_Handler);
|
||||
Lighting_Output_Write_Present_Value_Callback_Set(
|
||||
Lighting_Output_Write_Value_Handler);
|
||||
Channel_Write_Property_Internal_Callback_Set(Device_Write_Property);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print the terse usage info
|
||||
* @param filename - this application file name
|
||||
*/
|
||||
static void print_usage(const char *filename)
|
||||
{
|
||||
printf("Usage: %s [device-instance]\n", filename);
|
||||
printf(" [--device N][--test]\n");
|
||||
printf(" [--version][--help]\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print the verbose usage info
|
||||
* @param filename - this application file name
|
||||
*/
|
||||
static void print_help(const char *filename)
|
||||
{
|
||||
printf("BACnet Blinkt! server device.\n");
|
||||
printf("device-instance:\n"
|
||||
"--device N:\n"
|
||||
"BACnet Device Object Instance number of this device.\n"
|
||||
"This number will be used when other devices\n"
|
||||
"try and bind with this device using Who-Is and\n"
|
||||
"I-Am services.\n");
|
||||
printf("\n");
|
||||
printf("--test:\n"
|
||||
"Test the Blinkt! RGB LEDs with a cycling pattern.\n");
|
||||
printf("\n");
|
||||
printf("Example:\n"
|
||||
"%s 9009\n",
|
||||
filename);
|
||||
}
|
||||
|
||||
/** Main function of server demo.
|
||||
*
|
||||
* @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
|
||||
* datalink_receive, npdu_handler,
|
||||
* dcc_timer_seconds, datalink_maintenance_timer,
|
||||
* handler_cov_task,
|
||||
* tsm_timer_milliseconds
|
||||
*
|
||||
* @param argc [in] Arg count.
|
||||
* @param argv [in] Takes one argument: the Device Instance #.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = { 0 }; /* address where message came from */
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout_ms = 1;
|
||||
unsigned long seconds = 0;
|
||||
unsigned long milliseconds;
|
||||
bool blinkt_test = false;
|
||||
unsigned int target_args = 0;
|
||||
uint32_t device_id = BACNET_MAX_INSTANCE;
|
||||
int argi = 0;
|
||||
char *filename = NULL;
|
||||
|
||||
filename = filename_remove_path(argv[0]);
|
||||
for (argi = 1; argi < argc; argi++) {
|
||||
if (strcmp(argv[argi], "--help") == 0) {
|
||||
print_usage(filename);
|
||||
print_help(filename);
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[argi], "--version") == 0) {
|
||||
printf("%s %s\n", filename, BACNET_VERSION_TEXT);
|
||||
printf("Copyright (C) 2023 by Steve Karg and others.\n"
|
||||
"This is free software; see the source for copying "
|
||||
"conditions.\n"
|
||||
"There is NO warranty; not even for MERCHANTABILITY or\n"
|
||||
"FITNESS FOR A PARTICULAR PURPOSE.\n");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[argi], "--device") == 0) {
|
||||
if (++argi < argc) {
|
||||
device_id = strtol(argv[argi], NULL, 0);
|
||||
}
|
||||
} else if (strcmp(argv[argi], "--test") == 0) {
|
||||
blinkt_test = true;
|
||||
} else {
|
||||
if (target_args == 0) {
|
||||
device_id = strtol(argv[argi], NULL, 0);
|
||||
target_args++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (device_id > BACNET_MAX_INSTANCE) {
|
||||
fprintf(stderr, "device=%u - it must be less than %u\n", device_id,
|
||||
BACNET_MAX_INSTANCE);
|
||||
return 1;
|
||||
}
|
||||
Device_Set_Object_Instance_Number(device_id);
|
||||
printf("BACnet Raspberry Pi Blinkt! Demo\n"
|
||||
"BACnet Stack Version %s\n"
|
||||
"BACnet Device ID: %u\n"
|
||||
"Max APDU: %d\n",
|
||||
BACnet_Version, Device_Object_Instance_Number(), MAX_APDU);
|
||||
/* load any static address bindings to show up
|
||||
in our device bindings list */
|
||||
address_init();
|
||||
Init_Service_Handlers();
|
||||
dlenv_init();
|
||||
atexit(datalink_cleanup);
|
||||
blinkt_init();
|
||||
atexit(blinkt_cleanup);
|
||||
bacnet_output_init();
|
||||
/* configure the timeout values */
|
||||
/* broadcast an I-Am on startup */
|
||||
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
||||
/* loop forever */
|
||||
for (;;) {
|
||||
/* input */
|
||||
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout_ms);
|
||||
/* process */
|
||||
if (pdu_len) {
|
||||
npdu_handler(&src, &Rx_Buf[0], pdu_len);
|
||||
}
|
||||
if (mstimer_expired(&BACnet_Task_Timer)) {
|
||||
mstimer_reset(&BACnet_Task_Timer);
|
||||
/* 1 second tasks */
|
||||
dcc_timer_seconds(1);
|
||||
datalink_maintenance_timer(1);
|
||||
dlenv_maintenance_timer(1);
|
||||
handler_cov_timer_seconds(1);
|
||||
}
|
||||
if (mstimer_expired(&BACnet_TSM_Timer)) {
|
||||
mstimer_reset(&BACnet_TSM_Timer);
|
||||
tsm_timer_milliseconds(mstimer_interval(&BACnet_TSM_Timer));
|
||||
}
|
||||
handler_cov_task();
|
||||
if (mstimer_expired(&BACnet_Address_Timer)) {
|
||||
mstimer_reset(&BACnet_Address_Timer);
|
||||
/* address cache */
|
||||
seconds = mstimer_interval(&BACnet_Address_Timer) / 1000;
|
||||
address_cache_timer(seconds);
|
||||
}
|
||||
/* output/input */
|
||||
if (blinkt_test) {
|
||||
blinkt_test_task();
|
||||
} else {
|
||||
if (mstimer_expired(&BACnet_Object_Timer)) {
|
||||
mstimer_reset(&BACnet_Object_Timer);
|
||||
milliseconds = mstimer_interval(&BACnet_Object_Timer);
|
||||
Device_Timer(milliseconds);
|
||||
blinkt_show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* @} */
|
||||
|
||||
/* End group ServerDemo */
|
||||
@@ -7,7 +7,7 @@ TARGET = bacpiface
|
||||
# BACnet objects that are used with this app
|
||||
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
|
||||
SRC = main.c \
|
||||
$(BACNET_OBJECT_DIR)/device.c \
|
||||
device.c \
|
||||
$(BACNET_OBJECT_DIR)/netport.c \
|
||||
$(BACNET_OBJECT_DIR)/bi.c \
|
||||
$(BACNET_OBJECT_DIR)/bo.c
|
||||
@@ -24,6 +24,8 @@ PIFACE_INCLUDE = libpifacedigital/src
|
||||
PIFACE_LIB = libpifacedigital
|
||||
MCP23S17_LIB = libmcp23s17
|
||||
|
||||
CFLAGS += -I$(PIFACE_INCLUDE)
|
||||
|
||||
LDPIFACE = -Wl,-L$(PIFACE_LIB),-lpifacedigital,-L$(MCP23S17_LIB),-lmcp23s17
|
||||
LFLAGS += $(LDPIFACE)
|
||||
|
||||
|
||||
+468
-154
@@ -1,6 +1,6 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2014 Steve Karg <skarg@users.sourceforge.net>
|
||||
* Copyright (C) 2005,2006,2009 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@@ -22,15 +22,19 @@
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/** @file device.c Base "class" for handling all BACnet objects belonging
|
||||
* to a BACnet device, as well as Device-specific properties. */
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h> /* for memmove */
|
||||
#include <time.h> /* for timezone, localtime */
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
#include "bacnet/bacenum.h"
|
||||
#include "bacnet/bacapp.h"
|
||||
#include "bacnet/config.h" /* the custom stuff */
|
||||
#include "bacnet/datetime.h"
|
||||
#include "bacnet/apdu.h"
|
||||
#include "bacnet/wp.h" /* WriteProperty handling */
|
||||
#include "bacnet/rp.h" /* ReadProperty handling */
|
||||
@@ -41,8 +45,6 @@
|
||||
#include "bacnet/datalink/datalink.h"
|
||||
#include "bacnet/basic/binding/address.h"
|
||||
/* include the OS specific */
|
||||
#include "bacnet/basic/sys/mstimer.h"
|
||||
/* include the device object */
|
||||
#include "bacnet/basic/object/device.h"
|
||||
#include "bacnet/basic/object/bi.h"
|
||||
#include "bacnet/basic/object/bo.h"
|
||||
@@ -67,14 +69,18 @@ static object_functions_t My_Object_Table[] = {
|
||||
Device_Read_Property_Local, Device_Write_Property_Local,
|
||||
Device_Property_Lists, DeviceGetRRInfo, NULL /* Iterator */,
|
||||
NULL /* Value_Lists */, NULL /* COV */, NULL /* COV Clear */,
|
||||
NULL /* Intrinsic Reporting */ },
|
||||
NULL /* Intrinsic Reporting */, NULL /* Add_List_Element */,
|
||||
NULL /* Remove_List_Element */,
|
||||
NULL /* Create */, NULL /* Delete */, NULL /* Timer */ },
|
||||
#if (BACNET_PROTOCOL_REVISION >= 17)
|
||||
{ OBJECT_NETWORK_PORT, Network_Port_Init, Network_Port_Count,
|
||||
Network_Port_Index_To_Instance, Network_Port_Valid_Instance,
|
||||
Network_Port_Object_Name, Network_Port_Read_Property,
|
||||
Network_Port_Write_Property, Network_Port_Property_Lists,
|
||||
NULL /* ReadRangeInfo */, NULL /* Iterator */, NULL /* Value_Lists */,
|
||||
NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */ },
|
||||
NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */,
|
||||
NULL /* Add_List_Element */, NULL /* Remove_List_Element */,
|
||||
NULL /* Create */, NULL /* Delete */, NULL /* Timer */ },
|
||||
#endif
|
||||
{ OBJECT_BINARY_INPUT, Binary_Input_Init, Binary_Input_Count,
|
||||
Binary_Input_Index_To_Instance, Binary_Input_Valid_Instance,
|
||||
@@ -82,20 +88,25 @@ static object_functions_t My_Object_Table[] = {
|
||||
Binary_Input_Write_Property, Binary_Input_Property_Lists,
|
||||
NULL /* ReadRangeInfo */, NULL /* Iterator */,
|
||||
Binary_Input_Encode_Value_List, Binary_Input_Change_Of_Value,
|
||||
Binary_Input_Change_Of_Value_Clear, NULL /* Intrinsic Reporting */ },
|
||||
Binary_Input_Change_Of_Value_Clear, NULL /* Intrinsic Reporting */,
|
||||
NULL /* Add_List_Element */, NULL /* Remove_List_Element */,
|
||||
NULL /* Create */, NULL /* Delete */, NULL /* Timer */ },
|
||||
{ OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count,
|
||||
Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance,
|
||||
Binary_Output_Object_Name, Binary_Output_Read_Property,
|
||||
Binary_Output_Write_Property, Binary_Output_Property_Lists,
|
||||
NULL /* ReadRangeInfo */, NULL /* Iterator */, NULL /* Value_Lists */,
|
||||
NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */ },
|
||||
NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */,
|
||||
NULL /* Add_List_Element */, NULL /* Remove_List_Element */,
|
||||
Binary_Output_Create, Binary_Output_Delete, NULL /* Timer */},
|
||||
{ MAX_BACNET_OBJECT_TYPE, NULL /* Init */, NULL /* Count */,
|
||||
NULL /* Index_To_Instance */, NULL /* Valid_Instance */,
|
||||
NULL /* Object_Name */, NULL /* Read_Property */,
|
||||
NULL /* Write_Property */, NULL /* Property_Lists */,
|
||||
NULL /* ReadRangeInfo */, NULL /* Iterator */, NULL /* Value_Lists */,
|
||||
NULL /* COV */, NULL /* COV Clear */,
|
||||
NULL /* Intrinsic Reporting */ }
|
||||
NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */,
|
||||
NULL /* Add_List_Element */, NULL /* Remove_List_Element */,
|
||||
NULL /* Create */, NULL /* Delete */ , NULL /* Timer */}
|
||||
};
|
||||
|
||||
/** Glue function to let the Device object, when called by a handler,
|
||||
@@ -208,6 +219,10 @@ static const int Device_Properties_Optional[] = {
|
||||
#endif
|
||||
PROP_DESCRIPTION, PROP_LOCAL_TIME, PROP_UTC_OFFSET, PROP_LOCAL_DATE,
|
||||
PROP_DAYLIGHT_SAVINGS_STATUS, PROP_LOCATION, PROP_ACTIVE_COV_SUBSCRIPTIONS,
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
PROP_TIME_SYNCHRONIZATION_RECIPIENTS, PROP_TIME_SYNCHRONIZATION_INTERVAL,
|
||||
PROP_ALIGN_INTERVALS, PROP_INTERVAL_OFFSET,
|
||||
#endif
|
||||
-1
|
||||
};
|
||||
|
||||
@@ -259,10 +274,15 @@ static BACNET_DATE Local_Date; /* rely on OS, if there is one */
|
||||
If your UTC offset is -5hours of GMT,
|
||||
then BACnet UTC offset is +5hours.
|
||||
BACnet UTC offset is expressed in minutes. */
|
||||
static int32_t UTC_Offset = 5 * 60;
|
||||
static int16_t UTC_Offset = 5 * 60;
|
||||
static bool Daylight_Savings_Status = false; /* rely on OS */
|
||||
/* List_Of_Session_Keys */
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
static bool Align_Intervals;
|
||||
static uint32_t Interval_Minutes;
|
||||
static uint32_t Interval_Offset_Minutes;
|
||||
/* Time_Synchronization_Recipients */
|
||||
#endif
|
||||
/* List_Of_Session_Keys */
|
||||
/* Max_Master - rely on MS/TP subsystem, if there is one */
|
||||
/* Max_Info_Frames - rely on MS/TP subsystem, if there is one */
|
||||
/* Device_Address_Binding - required, but relies on binding cache */
|
||||
@@ -296,8 +316,18 @@ bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data)
|
||||
{
|
||||
bool status = false;
|
||||
|
||||
/* Note: you could use a mix of state and password to multiple things */
|
||||
if (characterstring_ansi_same(&rd_data->password, Reinit_Password)) {
|
||||
/* From 16.4.1.1.2 Password
|
||||
This optional parameter shall be a CharacterString of up to
|
||||
20 characters. For those devices that require the password as a
|
||||
protection, the service request shall be denied if the parameter
|
||||
is absent or if the password is incorrect. For those devices that
|
||||
do not require a password, this parameter shall be ignored.*/
|
||||
if (characterstring_length(&rd_data->password) > 20) {
|
||||
rd_data->error_class = ERROR_CLASS_SERVICES;
|
||||
rd_data->error_code = ERROR_CODE_PARAMETER_OUT_OF_RANGE;
|
||||
} else if (characterstring_ansi_same(&rd_data->password, Reinit_Password)) {
|
||||
/* Note: you could use a mix of state and password to
|
||||
accomplish multiple things before restarting */
|
||||
switch (rd_data->state) {
|
||||
case BACNET_REINIT_COLDSTART:
|
||||
case BACNET_REINIT_WARMSTART:
|
||||
@@ -347,7 +377,7 @@ unsigned Device_Count(void)
|
||||
|
||||
uint32_t Device_Index_To_Instance(unsigned index)
|
||||
{
|
||||
index = index;
|
||||
(void)index;
|
||||
return Object_Instance_Number;
|
||||
}
|
||||
|
||||
@@ -361,11 +391,7 @@ uint32_t Device_Index_To_Instance(unsigned index)
|
||||
*/
|
||||
uint32_t Device_Object_Instance_Number(void)
|
||||
{
|
||||
#ifdef BAC_ROUTING
|
||||
return Routed_Device_Object_Instance_Number();
|
||||
#else
|
||||
return Object_Instance_Number;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Device_Set_Object_Instance_Number(uint32_t object_id)
|
||||
@@ -413,6 +439,11 @@ bool Device_Set_Object_Name(BACNET_CHARACTER_STRING *object_name)
|
||||
return status;
|
||||
}
|
||||
|
||||
bool Device_Object_Name_ANSI_Init(const char *value)
|
||||
{
|
||||
return characterstring_init_ansi(&My_Object_Name, value);
|
||||
}
|
||||
|
||||
BACNET_DEVICE_STATUS Device_System_Status(void)
|
||||
{
|
||||
return System_Status;
|
||||
@@ -813,57 +844,8 @@ bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type,
|
||||
|
||||
static void Update_Current_Time(void)
|
||||
{
|
||||
struct tm *tblock = NULL;
|
||||
#if defined(_MSC_VER)
|
||||
time_t tTemp;
|
||||
#else
|
||||
struct timeval tv;
|
||||
#endif
|
||||
/*
|
||||
struct tm
|
||||
|
||||
int tm_sec Seconds [0,60].
|
||||
int tm_min Minutes [0,59].
|
||||
int tm_hour Hour [0,23].
|
||||
int tm_mday Day of month [1,31].
|
||||
int tm_mon Month of year [0,11].
|
||||
int tm_year Years since 1900.
|
||||
int tm_wday Day of week [0,6] (Sunday =0).
|
||||
int tm_yday Day of year [0,365].
|
||||
int tm_isdst Daylight Savings flag.
|
||||
*/
|
||||
#if defined(_MSC_VER)
|
||||
time(&tTemp);
|
||||
tblock = (struct tm *)localtime(&tTemp);
|
||||
#else
|
||||
if (gettimeofday(&tv, NULL) == 0) {
|
||||
tblock = (struct tm *)localtime((const time_t *)&tv.tv_sec);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (tblock) {
|
||||
datetime_set_date(&Local_Date, (uint16_t)tblock->tm_year + 1900,
|
||||
(uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday);
|
||||
#if !defined(_MSC_VER)
|
||||
datetime_set_time(&Local_Time, (uint8_t)tblock->tm_hour,
|
||||
(uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec,
|
||||
(uint8_t)(tv.tv_usec / 10000));
|
||||
#else
|
||||
datetime_set_time(&Local_Time, (uint8_t)tblock->tm_hour,
|
||||
(uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, 0);
|
||||
#endif
|
||||
if (tblock->tm_isdst) {
|
||||
Daylight_Savings_Status = true;
|
||||
} else {
|
||||
Daylight_Savings_Status = false;
|
||||
}
|
||||
/* note: timezone is declared in <time.h> stdlib. */
|
||||
UTC_Offset = timezone / 60;
|
||||
} else {
|
||||
datetime_date_wildcard_set(&Local_Date);
|
||||
datetime_time_wildcard_set(&Local_Time);
|
||||
Daylight_Savings_Status = false;
|
||||
}
|
||||
datetime_local(
|
||||
&Local_Date, &Local_Time, &UTC_Offset, &Daylight_Savings_Status);
|
||||
}
|
||||
|
||||
void Device_getCurrentDateTime(BACNET_DATE_TIME *DateTime)
|
||||
@@ -881,11 +863,90 @@ int32_t Device_UTC_Offset(void)
|
||||
return UTC_Offset;
|
||||
}
|
||||
|
||||
void Device_UTC_Offset_Set(int16_t offset)
|
||||
{
|
||||
UTC_Offset = offset;
|
||||
}
|
||||
|
||||
bool Device_Daylight_Savings_Status(void)
|
||||
{
|
||||
return Daylight_Savings_Status;
|
||||
}
|
||||
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
/**
|
||||
* Sets the time sync interval in minutes
|
||||
*
|
||||
* @param flag
|
||||
* This property, of type BOOLEAN, specifies whether (TRUE)
|
||||
* or not (FALSE) clock-aligned periodic time synchronization is
|
||||
* enabled. If periodic time synchronization is enabled and the
|
||||
* time synchronization interval is a factor of (divides without
|
||||
* remainder) an hour or day, then the beginning of the period
|
||||
* specified for time synchronization shall be aligned to the hour or
|
||||
* day, respectively. If this property is present, it shall be writable.
|
||||
*/
|
||||
bool Device_Align_Intervals_Set(bool flag)
|
||||
{
|
||||
Align_Intervals = flag;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Device_Align_Intervals(void)
|
||||
{
|
||||
return Align_Intervals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time sync interval in minutes
|
||||
*
|
||||
* @param minutes
|
||||
* This property, of type Unsigned, specifies the periodic
|
||||
* interval in minutes at which TimeSynchronization and
|
||||
* UTCTimeSynchronization requests shall be sent. If this
|
||||
* property has a value of zero, then periodic time synchronization is
|
||||
* disabled. If this property is present, it shall be writable.
|
||||
*/
|
||||
bool Device_Time_Sync_Interval_Set(uint32_t minutes)
|
||||
{
|
||||
Interval_Minutes = minutes;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t Device_Time_Sync_Interval(void)
|
||||
{
|
||||
return Interval_Minutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time sync interval offset value.
|
||||
*
|
||||
* @param minutes
|
||||
* This property, of type Unsigned, specifies the offset in
|
||||
* minutes from the beginning of the period specified for time
|
||||
* synchronization until the actual time synchronization requests
|
||||
* are sent. The offset used shall be the value of Interval_Offset
|
||||
* modulo the value of Time_Synchronization_Interval;
|
||||
* e.g., if Interval_Offset has the value 31 and
|
||||
* Time_Synchronization_Interval is 30, the offset used shall be 1.
|
||||
* Interval_Offset shall have no effect if Align_Intervals is
|
||||
* FALSE. If this property is present, it shall be writable.
|
||||
*/
|
||||
bool Device_Interval_Offset_Set(uint32_t minutes)
|
||||
{
|
||||
Interval_Offset_Minutes = minutes;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t Device_Interval_Offset(void)
|
||||
{
|
||||
return Interval_Offset_Minutes;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* return the length of the apdu encoded or BACNET_STATUS_ERROR for error or
|
||||
BACNET_STATUS_ABORT for abort message */
|
||||
int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
@@ -1002,7 +1063,8 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
pObject = Object_Table;
|
||||
while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) {
|
||||
if ((pObject->Object_Count) && (pObject->Object_Count() > 0)) {
|
||||
bitstring_set_bit(&bit_string, pObject->Object_Type, true);
|
||||
bitstring_set_bit(
|
||||
&bit_string, (uint8_t)pObject->Object_Type, true);
|
||||
}
|
||||
pObject++;
|
||||
}
|
||||
@@ -1050,6 +1112,28 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
apdu_len =
|
||||
encode_application_unsigned(&apdu[0], dlmstp_max_master());
|
||||
break;
|
||||
#endif
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
case PROP_TIME_SYNCHRONIZATION_RECIPIENTS:
|
||||
apdu_len = handler_timesync_encode_recipients(&apdu[0], MAX_APDU);
|
||||
if (apdu_len < 0) {
|
||||
rpdata->error_code =
|
||||
ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||
apdu_len = BACNET_STATUS_ABORT;
|
||||
}
|
||||
break;
|
||||
case PROP_TIME_SYNCHRONIZATION_INTERVAL:
|
||||
apdu_len = encode_application_unsigned(
|
||||
&apdu[0], Device_Time_Sync_Interval());
|
||||
break;
|
||||
case PROP_ALIGN_INTERVALS:
|
||||
apdu_len =
|
||||
encode_application_boolean(&apdu[0], Device_Align_Intervals());
|
||||
break;
|
||||
case PROP_INTERVAL_OFFSET:
|
||||
apdu_len =
|
||||
encode_application_unsigned(&apdu[0], Device_Interval_Offset());
|
||||
break;
|
||||
#endif
|
||||
case PROP_ACTIVE_COV_SUBSCRIPTIONS:
|
||||
apdu_len = handler_cov_encode_subscriptions(&apdu[0], apdu_max);
|
||||
@@ -1071,6 +1155,59 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/** Looks up the common Object and Property, and encodes its Value in an
|
||||
* APDU. Sets the error class and code if request is not appropriate.
|
||||
* @param pObject - object table
|
||||
* @param rpdata [in,out] Structure with the requested Object & Property info
|
||||
* on entry, and APDU message on return.
|
||||
* @return The length of the APDU on success, else BACNET_STATUS_ERROR
|
||||
*/
|
||||
static int Read_Property_Common(
|
||||
struct object_functions *pObject, BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
{
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
BACNET_CHARACTER_STRING char_string;
|
||||
uint8_t *apdu = NULL;
|
||||
#if (BACNET_PROTOCOL_REVISION >= 14)
|
||||
struct special_property_list_t property_list;
|
||||
#endif
|
||||
|
||||
if ((rpdata->application_data == NULL) ||
|
||||
(rpdata->application_data_len == 0)) {
|
||||
return 0;
|
||||
}
|
||||
apdu = rpdata->application_data;
|
||||
if (property_list_common(rpdata->object_property)) {
|
||||
apdu_len = property_list_common_encode(rpdata, Object_Instance_Number);
|
||||
} else if (rpdata->object_property == PROP_OBJECT_NAME) {
|
||||
/* only array properties can have array options */
|
||||
if (rpdata->array_index != BACNET_ARRAY_ALL) {
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
|
||||
apdu_len = BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
characterstring_init_ansi(&char_string, "");
|
||||
if (pObject->Object_Name) {
|
||||
(void)pObject->Object_Name(
|
||||
rpdata->object_instance, &char_string);
|
||||
}
|
||||
apdu_len =
|
||||
encode_application_character_string(&apdu[0], &char_string);
|
||||
}
|
||||
#if (BACNET_PROTOCOL_REVISION >= 14)
|
||||
} else if (rpdata->object_property == PROP_PROPERTY_LIST) {
|
||||
Device_Objects_Property_List(
|
||||
rpdata->object_type, rpdata->object_instance, &property_list);
|
||||
apdu_len = property_list_encode(rpdata, property_list.Required.pList,
|
||||
property_list.Optional.pList, property_list.Proprietary.pList);
|
||||
#endif
|
||||
} else if (pObject->Object_Read_Property) {
|
||||
apdu_len = pObject->Object_Read_Property(rpdata);
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/** Looks up the requested Object and Property, and encodes its Value in an
|
||||
* APDU.
|
||||
* @ingroup ObjIntf
|
||||
@@ -1092,10 +1229,14 @@ int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
if (pObject != NULL) {
|
||||
if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(rpdata->object_instance)) {
|
||||
if (pObject->Object_Read_Property) {
|
||||
apdu_len = pObject->Object_Read_Property(rpdata);
|
||||
apdu_len = Read_Property_Common(pObject, rpdata);
|
||||
} else {
|
||||
rpdata->error_class = ERROR_CLASS_OBJECT;
|
||||
rpdata->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rpdata->error_class = ERROR_CLASS_OBJECT;
|
||||
rpdata->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
@@ -1109,7 +1250,10 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
BACNET_APPLICATION_DATA_VALUE value;
|
||||
BACNET_OBJECT_TYPE object_type = OBJECT_NONE;
|
||||
uint32_t object_instance = 0;
|
||||
int temp;
|
||||
int result = 0;
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
uint32_t minutes = 0;
|
||||
#endif
|
||||
|
||||
/* decode the some of the request */
|
||||
len = bacapp_decode_application_data(
|
||||
@@ -1173,12 +1317,13 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_ENUMERATED);
|
||||
if (status) {
|
||||
temp = Device_Set_System_Status(
|
||||
result = Device_Set_System_Status(
|
||||
(BACNET_DEVICE_STATUS)value.type.Enumerated, false);
|
||||
if (temp != 0) {
|
||||
if (result != 0) {
|
||||
/* result: - 0 = ok, -1 = bad value, -2 = not allowed */
|
||||
status = false;
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
if (temp == -1) {
|
||||
if (result == -1) {
|
||||
wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
|
||||
} else {
|
||||
wp_data->error_code =
|
||||
@@ -1236,9 +1381,67 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
characterstring_length(&value.type.Character_String));
|
||||
}
|
||||
break;
|
||||
|
||||
case PROP_MAX_INFO_FRAMES:
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
case PROP_TIME_SYNCHRONIZATION_INTERVAL:
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_UNSIGNED_INT);
|
||||
if (status) {
|
||||
if (value.type.Unsigned_Int < 65535) {
|
||||
minutes = value.type.Unsigned_Int;
|
||||
Device_Time_Sync_Interval_Set(minutes);
|
||||
status = true;
|
||||
} else {
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PROP_ALIGN_INTERVALS:
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_BOOLEAN);
|
||||
if (status) {
|
||||
Device_Align_Intervals_Set(value.type.Boolean);
|
||||
status = true;
|
||||
}
|
||||
break;
|
||||
case PROP_INTERVAL_OFFSET:
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_UNSIGNED_INT);
|
||||
if (status) {
|
||||
if (value.type.Unsigned_Int < 65535) {
|
||||
minutes = value.type.Unsigned_Int;
|
||||
Device_Interval_Offset_Set(minutes);
|
||||
status = true;
|
||||
} else {
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
#else
|
||||
case PROP_TIME_SYNCHRONIZATION_INTERVAL:
|
||||
case PROP_ALIGN_INTERVALS:
|
||||
case PROP_INTERVAL_OFFSET:
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
|
||||
break;
|
||||
#endif
|
||||
case PROP_UTC_OFFSET:
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_SIGNED_INT);
|
||||
if (status) {
|
||||
if ((value.type.Signed_Int < (12 * 60)) &&
|
||||
(value.type.Signed_Int > (-12 * 60))) {
|
||||
Device_UTC_Offset_Set(value.type.Signed_Int);
|
||||
status = true;
|
||||
} else {
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
#if defined(BACDL_MSTP)
|
||||
case PROP_MAX_INFO_FRAMES:
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_UNSIGNED_INT);
|
||||
if (status) {
|
||||
@@ -1252,9 +1455,7 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case PROP_MAX_MASTER:
|
||||
#if defined(BACDL_MSTP)
|
||||
status = write_property_type_valid(
|
||||
wp_data, &value, BACNET_APPLICATION_TAG_UNSIGNED_INT);
|
||||
if (status) {
|
||||
@@ -1268,13 +1469,18 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
}
|
||||
}
|
||||
break;
|
||||
#else
|
||||
case PROP_MAX_INFO_FRAMES:
|
||||
case PROP_MAX_MASTER:
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
|
||||
break;
|
||||
#endif
|
||||
case PROP_OBJECT_TYPE:
|
||||
case PROP_VENDOR_NAME:
|
||||
case PROP_FIRMWARE_REVISION:
|
||||
case PROP_APPLICATION_SOFTWARE_VERSION:
|
||||
case PROP_LOCAL_TIME:
|
||||
case PROP_UTC_OFFSET:
|
||||
case PROP_LOCAL_DATE:
|
||||
case PROP_DAYLIGHT_SAVINGS_STATUS:
|
||||
case PROP_PROTOCOL_VERSION:
|
||||
@@ -1287,6 +1493,9 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
case PROP_DEVICE_ADDRESS_BINDING:
|
||||
case PROP_DATABASE_REVISION:
|
||||
case PROP_ACTIVE_COV_SUBSCRIPTIONS:
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
case PROP_TIME_SYNCHRONIZATION_RECIPIENTS:
|
||||
#endif
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
break;
|
||||
@@ -1321,7 +1530,15 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(wp_data->object_instance)) {
|
||||
if (pObject->Object_Write_Property) {
|
||||
#if (BACNET_PROTOCOL_REVISION >= 14)
|
||||
if (wp_data->object_property == PROP_PROPERTY_LIST) {
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
status = pObject->Object_Write_Property(wp_data);
|
||||
}
|
||||
} else {
|
||||
wp_data->error_class = ERROR_CLASS_PROPERTY;
|
||||
wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
@@ -1338,6 +1555,76 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data)
|
||||
return (status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AddListElement from an object list property
|
||||
* @param list_element [in] Pointer to the BACnet_List_Element_Data structure,
|
||||
* which is packed with the information from the request.
|
||||
* @return The length of the apdu encoded or #BACNET_STATUS_ERROR or
|
||||
* #BACNET_STATUS_ABORT or #BACNET_STATUS_REJECT.
|
||||
*/
|
||||
int Device_Add_List_Element(
|
||||
BACNET_LIST_ELEMENT_DATA * list_element)
|
||||
{
|
||||
int status = BACNET_STATUS_ERROR;
|
||||
struct object_functions *pObject = NULL;
|
||||
|
||||
pObject = Device_Objects_Find_Functions(list_element->object_type);
|
||||
if (pObject != NULL) {
|
||||
if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(list_element->object_instance)) {
|
||||
if (pObject->Object_Add_List_Element) {
|
||||
status = pObject->Object_Add_List_Element(list_element);
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_PROPERTY;
|
||||
list_element->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
}
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_OBJECT;
|
||||
list_element->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_OBJECT;
|
||||
list_element->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RemoveListElement from an object list property
|
||||
* @param list_element [in] Pointer to the BACnet_List_Element_Data structure,
|
||||
* which is packed with the information from the request.
|
||||
* @return The length of the apdu encoded or #BACNET_STATUS_ERROR or
|
||||
* #BACNET_STATUS_ABORT or #BACNET_STATUS_REJECT.
|
||||
*/
|
||||
int Device_Remove_List_Element(
|
||||
BACNET_LIST_ELEMENT_DATA * list_element)
|
||||
{
|
||||
int status = BACNET_STATUS_ERROR;
|
||||
struct object_functions *pObject = NULL;
|
||||
|
||||
pObject = Device_Objects_Find_Functions(list_element->object_type);
|
||||
if (pObject != NULL) {
|
||||
if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(list_element->object_instance)) {
|
||||
if (pObject->Object_Remove_List_Element) {
|
||||
status = pObject->Object_Remove_List_Element(list_element);
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_PROPERTY;
|
||||
list_element->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
}
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_OBJECT;
|
||||
list_element->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
} else {
|
||||
list_element->error_class = ERROR_CLASS_OBJECT;
|
||||
list_element->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/** Looks up the requested Object, and fills the Property Value list.
|
||||
* If the Object or Property can't be found, returns false.
|
||||
* @ingroup ObjHelpers
|
||||
@@ -1411,33 +1698,110 @@ void Device_COV_Clear(BACNET_OBJECT_TYPE object_type, uint32_t object_instance)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
void Device_local_reporting(void)
|
||||
/**
|
||||
* @brief Creates a child object, if supported
|
||||
* @ingroup ObjHelpers
|
||||
* @param data - CreateObject data, including error codes if failures
|
||||
* @return true if object has been created
|
||||
*/
|
||||
bool Device_Create_Object(
|
||||
BACNET_CREATE_OBJECT_DATA *data)
|
||||
{
|
||||
struct object_functions *pObject;
|
||||
uint32_t objects_count;
|
||||
bool status = false;
|
||||
struct object_functions *pObject = NULL;
|
||||
uint32_t object_instance;
|
||||
BACNET_OBJECT_TYPE object_type;
|
||||
uint32_t idx;
|
||||
|
||||
objects_count = Device_Object_List_Count();
|
||||
|
||||
/* loop for all objects */
|
||||
for (idx = 1; idx < objects_count; idx++) {
|
||||
Device_Object_List_Identifier(idx, &object_type, &object_instance);
|
||||
|
||||
pObject = Device_Objects_Find_Functions(object_type);
|
||||
if (pObject != NULL) {
|
||||
if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(object_instance)) {
|
||||
if (pObject->Object_Intrinsic_Reporting) {
|
||||
pObject->Object_Intrinsic_Reporting(object_instance);
|
||||
pObject = Device_Objects_Find_Functions(data->object_type);
|
||||
if (pObject != NULL) {
|
||||
if (!pObject->Object_Create) {
|
||||
/* The device supports the object type and may have
|
||||
sufficient space, but does not support the creation of the
|
||||
object for some other reason.*/
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_DYNAMIC_CREATION_NOT_SUPPORTED;
|
||||
} else if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(data->object_instance)) {
|
||||
/* The object being created already exists */
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_OBJECT_IDENTIFIER_ALREADY_EXISTS;
|
||||
} else {
|
||||
if (data->list_of_initial_values) {
|
||||
/* FIXME: add support for writing to list of initial values */
|
||||
/* A property specified by the Property_Identifier in the
|
||||
List of Initial Values does not support initialization
|
||||
during the CreateObject service. */
|
||||
data->first_failed_element_number = 1;
|
||||
data->error_class = ERROR_CLASS_PROPERTY;
|
||||
data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
|
||||
/* and the object shall not be created */
|
||||
} else {
|
||||
object_instance = pObject->Object_Create(data->object_instance);
|
||||
if (object_instance == BACNET_MAX_INSTANCE) {
|
||||
/* The device cannot allocate the space needed
|
||||
for the new object.*/
|
||||
data->error_class = ERROR_CLASS_RESOURCES;
|
||||
data->error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
|
||||
} else {
|
||||
/* required by ACK */
|
||||
data->object_instance = object_instance;
|
||||
Device_Inc_Database_Revision();
|
||||
status = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* The device does not support the specified object type. */
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_UNSUPPORTED_OBJECT_TYPE;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deletes a child object, if supported
|
||||
* @ingroup ObjHelpers
|
||||
* @param data - DeleteObject data, including error codes if failures
|
||||
* @return true if object has been deleted
|
||||
*/
|
||||
bool Device_Delete_Object(
|
||||
BACNET_DELETE_OBJECT_DATA *data)
|
||||
{
|
||||
bool status = false;
|
||||
struct object_functions *pObject = NULL;
|
||||
|
||||
pObject = Device_Objects_Find_Functions(data->object_type);
|
||||
if (pObject != NULL) {
|
||||
if (!pObject->Object_Delete) {
|
||||
/* The device supports the object type
|
||||
but does not support the deletion of the
|
||||
object for some reason.*/
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_OBJECT_DELETION_NOT_PERMITTED;
|
||||
} else if (pObject->Object_Valid_Instance &&
|
||||
pObject->Object_Valid_Instance(data->object_instance)) {
|
||||
/* The object being deleted must already exist */
|
||||
status = pObject->Object_Delete(data->object_instance);
|
||||
if (status) {
|
||||
Device_Inc_Database_Revision();
|
||||
} else {
|
||||
/* The object exists but cannot be deleted. */
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_OBJECT_DELETION_NOT_PERMITTED;
|
||||
}
|
||||
} else {
|
||||
/* The object to be deleted does not exist. */
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_UNKNOWN_OBJECT;
|
||||
}
|
||||
} else {
|
||||
/* The device does not support the specified object type. */
|
||||
data->error_class = ERROR_CLASS_OBJECT;
|
||||
data->error_code = ERROR_CODE_UNSUPPORTED_OBJECT_TYPE;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
#endif
|
||||
|
||||
/** Looks up the requested Object to see if the functionality is supported.
|
||||
* @ingroup ObjHelpers
|
||||
@@ -1470,24 +1834,8 @@ bool Device_Value_List_Supported(BACNET_OBJECT_TYPE object_type)
|
||||
void Device_Init(object_functions_t *object_table)
|
||||
{
|
||||
struct object_functions *pObject = NULL;
|
||||
#if defined(BAC_UCI)
|
||||
const char *uciname;
|
||||
struct uci_context *ctx;
|
||||
fprintf(stderr, "Device_Init\n");
|
||||
ctx = ucix_init("bacnet_dev");
|
||||
if (!ctx)
|
||||
fprintf(stderr, "Failed to load config file bacnet_dev\n");
|
||||
uciname = ucix_get_option(ctx, "bacnet_dev", "0", "Name");
|
||||
if (uciname != 0) {
|
||||
characterstring_init_ansi(&My_Object_Name, uciname);
|
||||
} else {
|
||||
#endif /* defined(BAC_UCI) */
|
||||
characterstring_init_ansi(&My_Object_Name, "PiFace Digital Demo");
|
||||
#if defined(BAC_UCI)
|
||||
}
|
||||
ucix_cleanup(ctx);
|
||||
#endif /* defined(BAC_UCI) */
|
||||
|
||||
characterstring_init_ansi(&My_Object_Name, "PiFace");
|
||||
datetime_init();
|
||||
if (object_table) {
|
||||
Object_Table = object_table;
|
||||
} else {
|
||||
@@ -1540,37 +1888,3 @@ bool DeviceGetRRInfo(BACNET_READ_RANGE_DATA *pRequest, /* Info on the request */
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
#ifdef BAC_ROUTING
|
||||
/****************************************************************************
|
||||
************* BACnet Routing Functionality (Optional) **********************
|
||||
****************************************************************************
|
||||
* The supporting functions are located in gw_device.c, except for those
|
||||
* that need access to local data in this file.
|
||||
****************************************************************************/
|
||||
|
||||
/** Initialize the first of our array of Devices with the main Device's
|
||||
* information, and then swap out some of the Device object functions and
|
||||
* replace with ones appropriate for routing.
|
||||
* @ingroup ObjIntf
|
||||
* @param first_object_instance Set the first (gateway) Device to this
|
||||
instance number.
|
||||
*/
|
||||
void Routing_Device_Init(uint32_t first_object_instance)
|
||||
{
|
||||
struct object_functions *pDevObject = NULL;
|
||||
|
||||
/* Initialize with our preset strings */
|
||||
Add_Routed_Device(first_object_instance, &My_Object_Name, Description);
|
||||
|
||||
/* Now substitute our routed versions of the main object functions. */
|
||||
pDevObject = Object_Table;
|
||||
pDevObject->Object_Index_To_Instance = Routed_Device_Index_To_Instance;
|
||||
pDevObject->Object_Valid_Instance =
|
||||
Routed_Device_Valid_Object_Instance_Number;
|
||||
pDevObject->Object_Name = Routed_Device_Name;
|
||||
pDevObject->Object_Read_Property = Routed_Device_Read_Property_Local;
|
||||
pDevObject->Object_Write_Property = Routed_Device_Write_Property_Local;
|
||||
}
|
||||
|
||||
#endif /* BAC_ROUTING */
|
||||
|
||||
+54
-33
@@ -28,7 +28,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "bacnet/config.h"
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
@@ -41,12 +40,21 @@
|
||||
#include "bacnet/basic/services.h"
|
||||
#include "bacnet/datalink/dlenv.h"
|
||||
#include "bacnet/basic/sys/filename.h"
|
||||
#include "bacnet/basic/sys/mstimer.h"
|
||||
#include "bacnet/basic/tsm/tsm.h"
|
||||
#include "bacnet/basic/tsm/tsm.h"
|
||||
#include "bacnet/datalink/datalink.h"
|
||||
#include "bacnet/basic/binding/address.h"
|
||||
/* include the device object */
|
||||
#include "bacnet/basic/object/device.h"
|
||||
/* objects that have tasks inside them */
|
||||
#if (BACNET_PROTOCOL_REVISION >= 14)
|
||||
#include "bacnet/basic/object/lo.h"
|
||||
#endif
|
||||
#if (BACNET_PROTOCOL_REVISION >= 24)
|
||||
#include "bacnet/basic/object/color_object.h"
|
||||
#include "bacnet/basic/object/color_temperature.h"
|
||||
#endif
|
||||
#include "bacnet/basic/object/lc.h"
|
||||
#include "bacnet/basic/object/trendlog.h"
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
@@ -68,7 +76,18 @@
|
||||
|
||||
/* current version of the BACnet stack */
|
||||
static const char *BACnet_Version = BACNET_VERSION_TEXT;
|
||||
|
||||
/* task timer for various BACnet timeouts */
|
||||
static struct mstimer BACnet_Task_Timer;
|
||||
/* task timer for TSM timeouts */
|
||||
static struct mstimer BACnet_TSM_Timer;
|
||||
/* task timer for address binding timeouts */
|
||||
static struct mstimer BACnet_Address_Timer;
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
/* task timer for notification recipient timeouts */
|
||||
static struct mstimer BACnet_Notification_Timer;
|
||||
#endif
|
||||
/* task timer for objects */
|
||||
static struct mstimer BACnet_Object_Timer;
|
||||
/** Buffer used for receiving */
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
|
||||
|
||||
@@ -148,6 +167,14 @@ static void Init_Service_Handlers(void)
|
||||
SERVICE_CONFIRMED_CREATE_OBJECT, handler_create_object);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_DELETE_OBJECT, handler_delete_object);
|
||||
/* configure the cyclic timers */
|
||||
mstimer_set(&BACnet_Task_Timer, 1000UL);
|
||||
mstimer_set(&BACnet_TSM_Timer, 50UL);
|
||||
mstimer_set(&BACnet_Address_Timer, 60UL*1000UL);
|
||||
mstimer_set(&BACnet_Object_Timer, 100UL);
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
mstimer_set(&BACnet_Notification_Timer, NC_RESCAN_RECIPIENTS_SECS*1000UL);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void print_usage(const char *filename)
|
||||
@@ -190,15 +217,9 @@ int main(int argc, char *argv[])
|
||||
BACNET_ADDRESS src = { 0 }; /* address where message came from */
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 1; /* milliseconds */
|
||||
time_t last_seconds = 0;
|
||||
time_t current_seconds = 0;
|
||||
uint32_t elapsed_seconds = 0;
|
||||
uint32_t elapsed_milliseconds = 0;
|
||||
uint32_t address_binding_tmr = 0;
|
||||
uint32_t elapsed_seconds = 0;
|
||||
BACNET_CHARACTER_STRING DeviceName;
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
uint32_t recipient_scan_tmr = 0;
|
||||
#endif
|
||||
#if defined(BACNET_TIME_MASTER)
|
||||
BACNET_DATE_TIME bdatetime;
|
||||
#endif
|
||||
@@ -277,33 +298,27 @@ int main(int argc, char *argv[])
|
||||
|
||||
dlenv_init();
|
||||
atexit(datalink_cleanup);
|
||||
/* configure the timeout values */
|
||||
last_seconds = time(NULL);
|
||||
/* broadcast an I-Am on startup */
|
||||
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
||||
/* loop forever */
|
||||
for (;;) {
|
||||
/* input */
|
||||
current_seconds = time(NULL);
|
||||
|
||||
/* returns 0 bytes on timeout */
|
||||
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
|
||||
|
||||
/* process */
|
||||
if (pdu_len) {
|
||||
npdu_handler(&src, &Rx_Buf[0], pdu_len);
|
||||
}
|
||||
/* at least one second has passed */
|
||||
elapsed_seconds = (uint32_t)(current_seconds - last_seconds);
|
||||
if (elapsed_seconds) {
|
||||
last_seconds = current_seconds;
|
||||
if (mstimer_expired(&BACnet_Task_Timer)) {
|
||||
mstimer_reset(&BACnet_Task_Timer);
|
||||
elapsed_milliseconds = mstimer_interval(&BACnet_Task_Timer);
|
||||
elapsed_seconds = elapsed_milliseconds/1000;
|
||||
/* 1 second tasks */
|
||||
dcc_timer_seconds(elapsed_seconds);
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
dlenv_maintenance_timer(elapsed_seconds);
|
||||
Load_Control_State_Machine_Handler();
|
||||
elapsed_milliseconds = elapsed_seconds * 1000;
|
||||
handler_cov_timer_seconds(elapsed_seconds);
|
||||
tsm_timer_milliseconds(elapsed_milliseconds);
|
||||
Load_Control_State_Machine_Handler();
|
||||
trend_log_timer(elapsed_seconds);
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
Device_local_reporting();
|
||||
@@ -313,24 +328,30 @@ int main(int argc, char *argv[])
|
||||
handler_timesync_task(&bdatetime);
|
||||
#endif
|
||||
}
|
||||
handler_cov_task();
|
||||
/* scan cache address */
|
||||
address_binding_tmr += elapsed_seconds;
|
||||
if (address_binding_tmr >= 60) {
|
||||
address_cache_timer(address_binding_tmr);
|
||||
address_binding_tmr = 0;
|
||||
if (mstimer_expired(&BACnet_TSM_Timer)) {
|
||||
mstimer_reset(&BACnet_TSM_Timer);
|
||||
elapsed_milliseconds = mstimer_interval(&BACnet_TSM_Timer);
|
||||
tsm_timer_milliseconds(elapsed_milliseconds);
|
||||
}
|
||||
if (mstimer_expired(&BACnet_Address_Timer)) {
|
||||
mstimer_reset(&BACnet_Address_Timer);
|
||||
elapsed_milliseconds = mstimer_interval(&BACnet_Address_Timer);
|
||||
elapsed_seconds = elapsed_milliseconds/1000;
|
||||
address_cache_timer(elapsed_seconds);
|
||||
}
|
||||
handler_cov_task();
|
||||
#if defined(INTRINSIC_REPORTING)
|
||||
/* try to find addresses of recipients */
|
||||
recipient_scan_tmr += elapsed_seconds;
|
||||
if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
|
||||
if (mstimer_expired(&BACnet_Notification_Timer)) {
|
||||
mstimer_reset(&BACnet_Notification_Timer);
|
||||
Notification_Class_find_recipient();
|
||||
recipient_scan_tmr = 0;
|
||||
}
|
||||
#endif
|
||||
/* output */
|
||||
|
||||
/* blink LEDs, Turn on or off outputs, etc */
|
||||
if (mstimer_expired(&BACnet_Object_Timer)) {
|
||||
mstimer_reset(&BACnet_Object_Timer);
|
||||
elapsed_milliseconds = mstimer_interval(&BACnet_Object_Timer);
|
||||
Device_Timer(elapsed_milliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
# Executable file name
|
||||
TARGET = bacwp
|
||||
|
||||
CFLAGS += -DBACAPP_COLOR_RGB_CONVERSION_ENABLED=1
|
||||
|
||||
# BACnet objects that are used with this app
|
||||
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
|
||||
SRC = main.c \
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
# Executable file name
|
||||
TARGET = bacwpm
|
||||
|
||||
CFLAGS += -DBACAPP_COLOR_RGB_CONVERSION_ENABLED=1
|
||||
|
||||
# BACnet objects that are used with this app
|
||||
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
|
||||
SRC = main.c \
|
||||
|
||||
Reference in New Issue
Block a user