Adding a gateway project to demonstrate routing to a virtual network.

The work is not complete yet; this is an interim checkin.  Ultimately the device.c file will be merged with the regular one or the deltas will be separated into some other file.
This commit is contained in:
tbrennan3
2010-09-29 21:04:00 +00:00
parent 9f4b8e5f19
commit cf0f01c5a1
7 changed files with 2513 additions and 2 deletions
+230
View File
@@ -0,0 +1,230 @@
/**************************************************************************
*
* 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.
*
*********************************************************************/
/**
* Code for this project began with code from the demo/server project and
* Paul Chapman's vmac project.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include "config.h"
#include "gateway.h"
#include "address.h"
#include "bacdef.h"
#include "handlers.h"
#include "client.h"
#include "dlenv.h"
#include "bacdcode.h"
#include "npdu.h"
#include "apdu.h"
#include "iam.h"
#include "tsm.h"
#include "device.h"
#include "bacfile.h"
#include "datalink.h"
#include "dcc.h"
#include "net.h"
#include "txbuf.h"
#include "lc.h"
#include "debug.h"
#include "version.h"
/* include the device object */
#include "device.h"
//#include "vmac.h"
/** @file gateway/main.c Example virtual gateway application using the BACnet Stack. */
/* Prototypes */
extern void routing_npdu_handler( BACNET_ADDRESS * src, uint8_t * pdu, uint16_t pdu_len);
/* (Doxygen note: The next two lines pull all the following Javadoc
* into the GatewayDemo module.) */
/** @addtogroup GatewayDemo */
/*@{*/
/** Buffer used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/** The list of DNETs that our router can reach.
* Only one entry since we don't support downstream routers.
*/
int DNET_list[2] = {
VIRTUAL_DNET, -1 /* Need -1 terminator */
};
/** Initialize the handlers we will utilize.
* @see Device_Init, apdu_set_unconfirmed_handler, apdu_set_confirmed_handler
*/
static void Init_Service_Handlers(
uint32_t first_object_instance )
{
Devices_Init( first_object_instance );
/* 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);
/* 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);
#if defined(BACFILE)
apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file);
apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_WRITE_FILE,
handler_atomic_write_file);
#endif
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);
}
/** Handler registered with atexit() inside main function to, well, cleanup.
* Especially if we don't end normally.
* @see datalink_cleanup
*/
static void cleanup(
void)
{
datalink_cleanup();
}
/** Main function of server demo.
*
* @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
* datalink_receive, npdu_handler,
* dcc_timer_seconds, bvlc_maintenance_timer,
* Load_Control_State_Machine_Handler, 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 = 1000; /* milliseconds */
time_t last_seconds = 0;
time_t current_seconds = 0;
uint32_t elapsed_seconds = 0;
uint32_t elapsed_milliseconds = 0;
uint32_t first_object_instance = FIRST_DEVICE_NUMBER;
/* Router data */
// BACNET_DEVICE_PROFILE* device;
// BACNET_VMAC_ADDRESS adr;
/* allow the device ID to be set */
if (argc > 1) {
first_object_instance = strtol(argv[1], NULL, 0);
if ( ( first_object_instance == 0 ) ||
( first_object_instance >= BACNET_MAX_INSTANCE ) ) {
printf( "Error: Invalid Object Instance %s \n", argv[1] );
printf( "Provide a number from 1 to %ul \n", BACNET_MAX_INSTANCE -1 );
exit(1);
}
}
printf("BACnet Router Demo\n" "BACnet Stack Version %s\n"
"BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
first_object_instance, MAX_APDU);
Init_Service_Handlers( first_object_instance );
dlenv_init();
atexit(cleanup);
Initialize_Device_Addresses( );
// /* initialize vmac table and router device */
// device = vmac_initialize(99, 2001);
//
// debug_printf(device->name, "ROUTER:%u", vmac_get_subnet());
/* configure the timeout values */
last_seconds = time(NULL);
/* broadcast an I-Am on startup */
Send_I_Am(&Handler_Transmit_Buffer[0]);
/* broadcast an I-am-router-to-network on startup */
printf( "Remote Network DNET Number %d \n", DNET_list[0] );
Send_I_Am_Router_To_Network( DNET_list );
/* 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) {
routing_npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
elapsed_seconds = current_seconds - last_seconds;
if (elapsed_seconds) {
last_seconds = current_seconds;
dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
bvlc_maintenance_timer(elapsed_seconds);
#endif
dlenv_maintenance_timer(elapsed_seconds);
Load_Control_State_Machine_Handler();
elapsed_milliseconds = elapsed_seconds * 1000;
handler_cov_task(elapsed_seconds);
tsm_timer_milliseconds(elapsed_milliseconds);
}
/* output */
/* blink LEDs, Turn on or off outputs, etc */
}
}
/* @} */
/* End group GatewayDemo */