bacnet/runtime/server.c
changeset 2191 b579e2155d02
parent 2187 c6321473fac1
child 2261 37ef7a3c6b8b
equal deleted inserted replaced
2190:b7d803fc44db 2191:b579e2155d02
   426     // success
   426     // success
   427     return 0;
   427     return 0;
   428 }
   428 }
   429 
   429 
   430 
   430 
       
   431 // This mutex blocks execution of __init_%(locstr)s() until initialization is done
       
   432 static int init_done = 0;
       
   433 static pthread_mutex_t init_done_lock = PTHREAD_MUTEX_INITIALIZER;
       
   434 static pthread_cond_t init_done_cond = PTHREAD_COND_INITIALIZER;
   431 
   435 
   432 /** Main function of server demo. **/
   436 /** Main function of server demo. **/
   433 int bn_server_run(server_node_t *server_node) {
   437 int bn_server_run(server_node_t *server_node) {
   434     int res = 0;
   438     int res = 0;
   435     BACNET_ADDRESS src = {0};  /* address where message came from */
   439     BACNET_ADDRESS src = {0};  /* address where message came from */
   448     address_init();
   452     address_init();
   449     /* load any entries in the BDT table from backup file */
   453     /* load any entries in the BDT table from backup file */
   450     bvlc_bdt_restore_local();
   454     bvlc_bdt_restore_local();
   451     /* Initiliaze the bacnet server 'device' */    
   455     /* Initiliaze the bacnet server 'device' */    
   452     Device_Init(server_node->device_name);
   456     Device_Init(server_node->device_name);
       
   457 
       
   458     pthread_mutex_lock(&init_done_lock);
       
   459     init_done = 1;
       
   460     pthread_cond_signal(&init_done_cond);
       
   461     pthread_mutex_unlock(&init_done_lock);
       
   462 
   453     /* Set the password (max 31 chars) for Device Communication Control request. */
   463     /* Set the password (max 31 chars) for Device Communication Control request. */
   454     /* Default in the BACnet stack is hardcoded as "filister" */
   464     /* Default in the BACnet stack is hardcoded as "filister" */
   455     /* (char *) cast is to remove the cast. The function is incorrectly declared/defined in the BACnet stack! */
   465     /* (char *) cast is to remove the cast. The function is incorrectly declared/defined in the BACnet stack! */
   456     /* BACnet stack needs to change demo/handler/h_dcc.c and include/handlers.h                               */
   466     /* BACnet stack needs to change demo/handler/h_dcc.c and include/handlers.h                               */
   457     handler_dcc_password_set((char *)server_node->comm_control_passwd);
   467     handler_dcc_password_set((char *)server_node->comm_control_passwd);
   547 int __cleanup_%(locstr)s ();
   557 int __cleanup_%(locstr)s ();
   548 
   558 
   549 int __init_%(locstr)s (int argc, char **argv){
   559 int __init_%(locstr)s (int argc, char **argv){
   550 	int index;
   560 	int index;
   551 
   561 
       
   562     init_done = 0;
   552 	/* init each local server */
   563 	/* init each local server */
   553 	/* NOTE: All server_nodes[].init_state are initialised to 0 in the code 
   564 	/* NOTE: All server_nodes[].init_state are initialised to 0 in the code 
   554 	 *       generated by the BACnet plugin 
   565 	 *       generated by the BACnet plugin 
   555 	 */
   566 	 */
   556 	/* create the BACnet server */
   567 	/* create the BACnet server */
   565 		if (res !=  0) {
   576 		if (res !=  0) {
   566 			fprintf(stderr, "BACnet plugin: Error starting bacnet server thread for node %%s\n", server_node.location);
   577 			fprintf(stderr, "BACnet plugin: Error starting bacnet server thread for node %%s\n", server_node.location);
   567 			goto error_exit;
   578 			goto error_exit;
   568 		}
   579 		}
   569 	}
   580 	}
       
   581 
       
   582     pthread_mutex_lock(&init_done_lock);
       
   583     while (!init_done) {
       
   584         pthread_cond_wait(&init_done_cond, &init_done_lock);
       
   585     }
       
   586     pthread_mutex_unlock(&init_done_lock);
       
   587 
   570 	server_node.init_state = 2; // we have created the node and thread
   588 	server_node.init_state = 2; // we have created the node and thread
   571 
   589 
   572 	return 0;
   590 	return 0;
   573 	
   591 	
   574 error_exit:
   592 error_exit: