int main(void){ WDTCTL = WDTPW | WDTHOLD; //Disable WDT P1DIR = 0x41; //P1.6 (green led) and P1.0 (red LED) outputs //oscillator control DCOCTL |= DCO1 | DCO0; BCSCTL1 |= RSEL3 | RSEL2 | RSEL0; //UART io setup (RX: P1.1, TX: P1.2) P1SEL |= 0x03; P1SEL2 |= 0x03; init_auto(); //set global interrupt enable bit _BIS_SR(GIE); for(;;){ comm_loop(); loop_auto(); unsigned int i = 0xffff; while(--i); } }
int main (int argc, char** argv) { conn_t* conn = NULL; struct sigaction action; sigset_t block_mask; char *aux_string = NULL, path[PATH_MAX]; int next_option, ret; int daemonize = 0, len; int verbose = 0; struct module * netopeer_module = NULL, *server_module = NULL; /* initialize message system and set verbose and debug variables */ if ((aux_string = getenv (ENVIRONMENT_VERBOSE)) == NULL) { verbose = NC_VERB_ERROR; } else { verbose = atoi (aux_string); } aux_string = NULL; /* for sure to avoid unwanted changes in environment */ /* parse given options */ while ((next_option = getopt (argc, argv, OPTSTRING)) != -1) { switch (next_option) { case 'd': daemonize = 1; break; case 'h': print_usage (argv[0]); break; case 'v': verbose = atoi (optarg); break; case 'V': print_version (argv[0]); break; default: print_usage (argv[0]); break; } } /* set signal handler */ sigfillset (&block_mask); action.sa_handler = signal_handler; action.sa_mask = block_mask; action.sa_flags = 0; sigaction (SIGINT, &action, NULL); sigaction (SIGQUIT, &action, NULL); sigaction (SIGABRT, &action, NULL); sigaction (SIGTERM, &action, NULL); sigaction (SIGKILL, &action, NULL); sigaction (SIGHUP, &action, NULL); nc_callback_print (clb_print); /* normalize value if not from the enum */ if (verbose < NC_VERB_ERROR) { nc_verbosity (NC_VERB_ERROR); } else if (verbose > NC_VERB_DEBUG) { nc_verbosity (NC_VERB_DEBUG); } else { nc_verbosity (verbose); } /* go to the background as a daemon */ if (daemonize == 1) { if (daemon(0, 0) != 0) { nc_verb_error("Going to background failed (%s)", strerror(errno)); return (EXIT_FAILURE); } openlog("netopeer-server", LOG_PID, LOG_DAEMON); } else { openlog("netopeer-server", LOG_PID|LOG_PERROR, LOG_DAEMON); } /* make sure we were executed by root */ if (geteuid() != 0) { nc_verb_error("Failed to start, must have root privileges."); return (EXIT_FAILURE); } /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /* initialize library including internal datastores and maybee something more */ if ((ret = nc_init (NC_INIT_ALL | NC_INIT_MULTILAYER)) < 0) { nc_verb_error("Library initialization failed."); return (EXIT_FAILURE); } /* Initiate communication subsystem for communicate with agents */ conn = comm_init(ret & NC_INITRET_RECOVERY); if (conn == NULL) { nc_verb_error("Communication subsystem not initiated."); return (EXIT_FAILURE); } server_start = 1; restart: /* start NETCONF server module */ if ((server_module = calloc(1, sizeof(struct module))) == NULL) { nc_verb_error("Creating necessary NETCONF server plugin failed!"); comm_destroy(conn); return(EXIT_FAILURE); } server_module->name = strdup(NCSERVER_MODULE_NAME); if (module_enable(server_module, 0)) { nc_verb_error("Starting necessary NETCONF server plugin failed!"); free(server_module->name); free(server_module); comm_destroy(conn); return EXIT_FAILURE; } /* start netopeer device module - it will start all modules that are * in its configuration and in server configuration */ if ((netopeer_module = calloc(1, sizeof(struct module))) == NULL) { nc_verb_error("Creating necessary Netopeer plugin failed!"); module_disable(server_module, 1); comm_destroy(conn); return(EXIT_FAILURE); } netopeer_module->name = strdup(NETOPEER_MODULE_NAME); if (module_enable(netopeer_module, 0)) { nc_verb_error("Starting necessary Netopeer plugin failed!"); module_disable(server_module, 1); free(netopeer_module->name); free(netopeer_module); comm_destroy(conn); return EXIT_FAILURE; } server_start = 0; nc_verb_verbose("Netopeer server successfully initialized."); while (!done) { comm_loop(conn, 500); } /* unload Netopeer module -> unload all modules */ module_disable(server_module, 1); module_disable(netopeer_module, 1); /* main cleanup */ if (!restart_soft) { /* close connection and destroy all sessions only when shutting down or hard restarting the server */ comm_destroy(conn); server_sessions_destroy_all (); nc_close (); } /* *Free the global variables that may *have been allocated by the parser. */ xmlCleanupParser (); if (restart_soft) { nc_verb_verbose("Server is going to soft restart."); restart_soft = 0; done = 0; goto restart; } else if (restart_hard) { nc_verb_verbose("Server is going to hard restart."); len = readlink("/proc/self/exe", path, PATH_MAX); path[len] = 0; execv(path, argv); } return (EXIT_SUCCESS); }
int main(int argc, char **argv) { const char *optstring = "d:fhv:"; const struct option longopts[] = { {"db", required_argument, 0, 'd'}, {"foreground", no_argument, 0, 'f'}, {"help", no_argument, 0, 'h'}, {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0} }; int longindex, next_option; int verbose = 0; int retval = EXIT_SUCCESS, r; char *aux_string; struct sigaction action; sigset_t block_mask; struct { struct ncds_ds *server; ncds_id server_id; struct ncds_ds *ofc; ncds_id ofc_id; } ds = {NULL, -1, NULL, -1}; /* connection channel to agents */ comm_t *c = NULL; /* initialize message system and set verbose and debug variables */ if ((aux_string = getenv(ENVIRONMENT_VERBOSE)) == NULL) { /* default verbose level */ verbose = NC_VERB_ERROR; } else { verbose = atoi(aux_string); } /* parse given options */ while ((next_option = getopt_long(argc, argv, optstring, longopts, &longindex)) != -1) { switch (next_option) { case 'd': ovsdb_path = strdup(optarg); break; case 'f': ofc_daemonize = 0; break; case 'h': print_usage(argv[0]); break; case 'v': verbose = atoi(optarg); break; default: print_usage(argv[0]); break; } } /* set signal handler */ sigfillset(&block_mask); action.sa_handler = signal_handler; action.sa_mask = block_mask; action.sa_flags = 0; sigaction(SIGINT, &action, NULL); sigaction(SIGQUIT, &action, NULL); sigaction(SIGABRT, &action, NULL); sigaction(SIGTERM, &action, NULL); sigaction(SIGKILL, &action, NULL); /* set verbose message printer callback */ nc_callback_print(clb_print); /* normalize value if not from the enum */ if (verbose < NC_VERB_ERROR) { nc_verbosity(NC_VERB_ERROR); } else if (verbose > NC_VERB_DEBUG) { nc_verbosity(NC_VERB_DEBUG); } else { nc_verbosity(verbose); } /* go to the background as a daemon */ if (ofc_daemonize == 1) { if (daemon(0, 0) != 0) { nc_verb_error("Going to background failed (%s)", strerror(errno)); return (EXIT_FAILURE); } openlog("ofc-server", LOG_PID, LOG_DAEMON); } else { openlog("ofc-server", LOG_PID | LOG_PERROR, LOG_DAEMON); } /* make sure we have sufficient rights to communicate with OVSDB */ /* TODO */ /* init libnetconf for a multilayer server */ r = nc_init((NC_INIT_ALL & ~NC_INIT_NACM) | NC_INIT_MULTILAYER); if (r < 0) { nc_verb_error("libnetconf initialization failed."); return (EXIT_FAILURE); } /* Initiate communication subsystem for communication with agents */ if ((c = comm_init(r)) == NULL) { nc_verb_error("Communication subsystem not initiated."); return (EXIT_FAILURE); } /* prepare the ietf-netconf-server module */ ncds_add_model(OFC_DATADIR "/ietf-netconf-server/ietf-x509-cert-to-name.yin"); ds.server = ncds_new_transapi_static(NCDS_TYPE_FILE, OFC_DATADIR "/ietf-netconf-server/ietf-netconf-server.yin", &server_transapi); if (ds.server == NULL) { retval = EXIT_FAILURE; nc_verb_error("Creating ietf-netconf-server datastore failed."); goto cleanup; } ncds_file_set_path(ds.server, OFC_DATADIR "/ietf-netconf-server/datastore.xml"); ncds_feature_enable("ietf-netconf-server", "ssh"); ncds_feature_enable("ietf-netconf-server", "inbound-ssh"); if ((ds.server_id = ncds_init(ds.server)) < 0) { retval = EXIT_FAILURE; nc_verb_error ("Initiating ietf-netconf-server datastore failed (error code %d).", ds.ofc_id); goto cleanup; } /* prepare the of-config module */ ds.ofc = ncds_new_transapi_static(NCDS_TYPE_CUSTOM, OFC_DATADIR "/of-config/of-config.yin", &ofc_transapi); if (ds.ofc == NULL) { retval = EXIT_FAILURE; nc_verb_error("Creating of-config datastore failed."); goto cleanup; } ncds_custom_set_data(ds.ofc, NULL, &ofcds_funcs); if ((ds.ofc_id = ncds_init(ds.ofc)) < 0) { retval = EXIT_FAILURE; nc_verb_error("Initiating of-config datastore failed (error code %d).", ds.ofc_id); goto cleanup; } if (ncds_consolidate() != 0) { retval = EXIT_FAILURE; nc_verb_error("Consolidating data models failed."); goto cleanup; } if (ncds_device_init(&(ds.server_id), NULL, 1) != 0) { retval = EXIT_FAILURE; nc_verb_error("Initiating ietf-netconf-server module failed."); goto cleanup; } if (ncds_device_init(&(ds.ofc_id), NULL, 1) != 0) { retval = EXIT_FAILURE; nc_verb_error("Initiating of-config module failed."); goto cleanup; } nc_verb_verbose("OF-CONFIG server successfully initialized."); while (!mainloop) { comm_loop(c, 500); } cleanup: /* cleanup */ nc_close(); return (retval); }