bacnet/runtime/server.c
changeset 2649 db68cb0e6bdc
parent 2268 e6c9a52d3877
equal deleted inserted replaced
2648:e4ab768170f9 2649:db68cb0e6bdc
    48 #include "getevent.h"
    48 #include "getevent.h"
    49 #include "net.h"
    49 #include "net.h"
    50 #include "txbuf.h"
    50 #include "txbuf.h"
    51 #include "version.h"
    51 #include "version.h"
    52 #include "timesync.h"
    52 #include "timesync.h"
       
    53 
       
    54 
       
    55 
       
    56 
    53 
    57 
    54 
    58 
    55 /* A utility function used by most (all?) implementations of BACnet Objects */
    59 /* A utility function used by most (all?) implementations of BACnet Objects */
    56 /* Adds to Prop_List all entries in Prop_List_XX that are not
    60 /* Adds to Prop_List all entries in Prop_List_XX that are not
    57  * PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PROPERTY_LIST
    61  * PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PROPERTY_LIST
   452     address_init();
   456     address_init();
   453     /* load any entries in the BDT table from backup file */
   457     /* load any entries in the BDT table from backup file */
   454     bvlc_bdt_restore_local();
   458     bvlc_bdt_restore_local();
   455     /* Initiliaze the bacnet server 'device' */    
   459     /* Initiliaze the bacnet server 'device' */    
   456     Device_Init(server_node->device_name);
   460     Device_Init(server_node->device_name);
   457 
   461     
   458     pthread_mutex_lock(&init_done_lock);
   462     /* Although the default values for the following properties are hardcoded into
   459     init_done = 1;
   463      * the respective variable definition+initialization in the C code,
   460     pthread_cond_signal(&init_done_cond);
   464      * these values may be potentially changed after compilation but before 
   461     pthread_mutex_unlock(&init_done_lock);
   465      * code startup. This is done by the web interface(1), directly loading the .so file, 
   462 
   466      * and changing the values in the server_node_t variable.
       
   467      * We must therefore honor those values when we start running the BACnet device server
       
   468      * 
       
   469      * (1) Web interface is implemented in runtime/BACnet_config.py
       
   470      *     which works as an extension of the web server in runtime/NevowServer.py
       
   471      *     which in turn is initialised/run by the Beremiz_service.py deamon
       
   472      */
       
   473     Device_Set_Location                    (server_node->device_location,        strlen(server_node->device_location));
       
   474     Device_Set_Description                 (server_node->device_description,     strlen(server_node->device_description));
       
   475     Device_Set_Application_Software_Version(server_node->device_appsoftware_ver, strlen(server_node->device_appsoftware_ver));
   463     /* Set the password (max 31 chars) for Device Communication Control request. */
   476     /* Set the password (max 31 chars) for Device Communication Control request. */
   464     /* Default in the BACnet stack is hardcoded as "filister" */
   477     /* Default in the BACnet stack is hardcoded as "filister" */
   465     /* (char *) cast is to remove the cast. The function is incorrectly declared/defined in the BACnet stack! */
   478     /* (char *) cast is to remove the cast. The function is incorrectly declared/defined in the BACnet stack! */
   466     /* BACnet stack needs to change demo/handler/h_dcc.c and include/handlers.h                               */
   479     /* BACnet stack needs to change demo/handler/h_dcc.c and include/handlers.h                               */
   467     handler_dcc_password_set((char *)server_node->comm_control_passwd);
   480     handler_dcc_password_set((char *)server_node->comm_control_passwd);
       
   481 
       
   482     
       
   483     pthread_mutex_lock(&init_done_lock);
       
   484     init_done = 1;
       
   485     pthread_cond_signal(&init_done_cond);
       
   486     pthread_mutex_unlock(&init_done_lock);
       
   487 
   468     /* Set callbacks and configure network interface */
   488     /* Set callbacks and configure network interface */
   469     res = Init_Service_Handlers();
   489     res = Init_Service_Handlers();
   470     if (res < 0) exit(1);
   490     if (res < 0) exit(1);
   471     res = Init_Network_Interface(
   491     res = Init_Network_Interface(
   472             server_node->network_interface, // interface    (NULL => use default (eth0))
   492             server_node->network_interface, // interface    (NULL => use default (eth0))
   659 	// Nothing to do ???
   679 	// Nothing to do ???
   660 
   680 
   661 	return res;
   681 	return res;
   662 }
   682 }
   663 
   683 
       
   684 
       
   685 
       
   686 
       
   687 
       
   688 /**********************************************/
       
   689 /** Functions for Beremiz web interface.     **/
       
   690 /**********************************************/
       
   691 
       
   692 /*
       
   693  * Beremiz has a program to run on the PLC (Beremiz_service.py)
       
   694  * to handle downloading of compiled programs, start/stop of PLC, etc.
       
   695  * (see runtime/PLCObject.py for start/stop, loading, ...)
       
   696  * 
       
   697  * This service also includes a web server to access PLC state (start/stop)
       
   698  * and to change some basic confiuration parameters.
       
   699  * (see runtime/NevowServer.py for the web server)
       
   700  * 
       
   701  * The web server allows for extensions, where additional configuration
       
   702  * parameters may be changed on the running/downloaded PLC.
       
   703  * BACnet plugin also comes with an extension to the web server, through
       
   704  * which the basic BACnet plugin configuration parameters may be changed
       
   705  * (basically, only the parameters in server_node_t may be changed)
       
   706  * 
       
   707  * The following functions are never called from other C code. They are 
       
   708  * called instead from the python code in runtime/BACnet_config.py, that
       
   709  * implements the web server extension for configuring BACnet parameters.
       
   710  */
       
   711 
       
   712 
       
   713 /* The location (in the Config. Node Tree of Beremiz IDE)
       
   714  * of the BACnet plugin.
       
   715  * 
       
   716  * This variable is also used by the BACnet web config code to determine 
       
   717  * whether the current loaded PLC includes the BACnet plugin
       
   718  * (so it should make the BACnet parameter web interface visible to the user).
       
   719  */
       
   720 const char * __bacnet_plugin_location = "%(locstr)s";
       
   721 
       
   722 
       
   723 /* NOTE: We could have the python code in runtime/BACnet_config.py
       
   724  *       directly access the server_node_t structure, however
       
   725  *       this would create a tight coupling between these two
       
   726  *       disjoint pieces of code.
       
   727  *       Any change to the server_node_t structure would require the 
       
   728  *       python code to be changed accordingly. I have therefore opted
       
   729  *       to cretae get/set functions, one for each parameter.
       
   730  *       
       
   731  * NOTE: since the BACnet plugin can only support/allow at most one instance 
       
   732  *       of the BACnet plugin in Beremiz (2 or more are not allowed due to 
       
   733  *       limitations of the underlying BACnet protocol stack being used),
       
   734  *       a single generic version of each of the following functions would work.
       
   735  *       However, simply for the sake of keeping things general, we create
       
   736  *       a diferent function for each plugin instance (which, as I said,
       
   737  *       will never occur for now).
       
   738  * 
       
   739  *      The functions being declared are therefoe named:
       
   740  *          __bacnet_0_get_ConfigParam_location
       
   741  *          __bacnet_0_get_ConfigParam_device_name
       
   742  *        etc...
       
   743  *      where the 0 will be replaced by the location of the BACnet plugin
       
   744  *      in the Beremiz configuration tree (will change depending on where
       
   745  *      the user inserted the BACnet plugin into their project) 
       
   746  */
       
   747 
       
   748 /* macro works for all data types */
       
   749 #define __bacnet_get_ConfigParam(param_type,param_name)                 \
       
   750 param_type __bacnet_%(locstr)s_get_ConfigParam_##param_name(void) {     \
       
   751     return server_node.param_name;                                      \
       
   752 }
       
   753 
       
   754 /* macro only works for char * data types */
       
   755 /* Note that the storage space (max string size) reserved for each parameter
       
   756  * (this storage space is reserved in device.h)
       
   757  * is set to a minimum of 
       
   758  *     %(BACnet_Param_String_Size)d 
       
   759  * which is set to the value of
       
   760  *     BACNET_PARAM_STRING_SIZE
       
   761  * in bacnet.py
       
   762  */
       
   763 #define __bacnet_set_ConfigParam(param_type,param_name)                 \
       
   764 void __bacnet_%(locstr)s_set_ConfigParam_##param_name(param_type val) { \
       
   765     strncpy(server_node.param_name, val, %(BACnet_Param_String_Size)d); \
       
   766     server_node.param_name[%(BACnet_Param_String_Size)d - 1] = '\0';    \
       
   767 }
       
   768 
       
   769 
       
   770 #define __bacnet_ConfigParam_str(param_name)        \
       
   771 __bacnet_get_ConfigParam(const char*,param_name)    \
       
   772 __bacnet_set_ConfigParam(const char*,param_name)     
       
   773 
       
   774 
       
   775 __bacnet_ConfigParam_str(location)
       
   776 __bacnet_ConfigParam_str(network_interface)
       
   777 __bacnet_ConfigParam_str(port_number)
       
   778 __bacnet_ConfigParam_str(device_name)
       
   779 __bacnet_ConfigParam_str(device_location)
       
   780 __bacnet_ConfigParam_str(device_description)
       
   781 __bacnet_ConfigParam_str(device_appsoftware_ver)
       
   782 __bacnet_ConfigParam_str(comm_control_passwd)
       
   783 
       
   784 __bacnet_get_ConfigParam(uint32_t,device_id)
       
   785 void __bacnet_%(locstr)s_set_ConfigParam_device_id(uint32_t val) {
       
   786     server_node.device_id = val;
       
   787 }
       
   788