//------------------------------------------------------------------ bool code_colorer::is_block_comment(const char_type* p, unsigned* len) const { if(m_block_comments.size() > 1) { if(str_is(p, m_block_comments[0])) { const char_type* start = p; p += m_block_comments[0].length(); int level = 1; while(p) { if(str_is(p, m_block_comments[0])) { ++level; p += m_block_comments[0].length(); } else if(str_is(p, m_block_comments[1])) { --level; p += m_block_comments[1].length(); if(level == 0) { *len = unsigned(p - start); return true; } } else { ++p; } } } } return false; }
//------------------------------------------------------------------ bool code_colorer::is_line_comment(const char_type* p, unsigned* len) const { if(m_line_comments.size()) { if(str_is(p, m_line_comments[0])) { const char_type* start = p; p += m_line_comments[0].length(); while(p) { if(is_lf(*p)) break; ++p; } *len = unsigned(p - start); return true; } } return false; }
int main(int argc, char **argv) { //Check if user == root if(geteuid() != 0) { puts("Please run this software as root!"); exit(EXIT_FAILURE); } // check for non-daemon mode for debugging for(int i = 1; i < argc; i++) { if (str_is(argv[i], "nodaemon")) { start_daemon = 0; break; } } if (start_daemon) { //Init daemon, can be replaced with daemon() call pid_t pid; pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } umask(0); //We are now running as the forked child process. openlog(argv[0],LOG_NOWAIT|LOG_PID,LOG_USER); pid_t sid; sid = setsid(); if (sid < 0) { syslog(LOG_ERR, "Could not create process group\n"); exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { syslog(LOG_ERR, "Could not change working directory to /\n"); exit(EXIT_FAILURE); } } else { // open syslog for non-daemon mode openlog(argv[0],LOG_NOWAIT|LOG_PID,LOG_USER); } //Read configuration file cfg_opt_t opts[] = { CFG_INT("i2cbus", 1, CFGF_NONE), CFG_INT("frequency", 99800, CFGF_NONE), CFG_BOOL("stereo", 1, CFGF_NONE), CFG_BOOL("rdsenable", 1, CFGF_NONE), CFG_BOOL("poweron", 1, CFGF_NONE), CFG_BOOL("tcpbindlocal", 1, CFGF_NONE), CFG_INT("tcpport", 42516, CFGF_NONE), CFG_INT("txpower", 3, CFGF_NONE), CFG_BOOL("gain", 0, CFGF_NONE), CFG_INT("volume", 3, CFGF_NONE), CFG_INT("rdspin", 17, CFGF_NONE), CFG_STR("rdsid", "", CFGF_NONE), CFG_STR("rdstext", "", CFGF_NONE), CFG_INT("ledpin", 27, CFGF_NONE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); if (cfg_parse(cfg, "/etc/fmberry.conf") == CFG_PARSE_ERROR) return 1; // get LED pin number int led = 1; // led state ledpin = cfg_getint(cfg, "ledpin"); rdsint = cfg_getint(cfg, "rdspin"); // Init I2C bus and transmitter with initial frequency and state if (ns741_init(cfg_getint(cfg, "i2cbus"), cfg_getint(cfg, "frequency")) == -1) { syslog(LOG_ERR, "Init failed! Double-check hardware and try again!\n"); exit(EXIT_FAILURE); } syslog(LOG_NOTICE, "Successfully initialized ns741 transmitter.\n"); int nfds; struct pollfd polls[2]; // open TCP listener socket, will exit() in case of error int lst = ListenTCP(cfg_getint(cfg, "tcpport")); polls[0].fd = lst; polls[0].events = POLLIN; nfds = 1; // initialize data structure for 'status' command bzero(&mmr70, sizeof(mmr70)); mmr70.frequency = cfg_getint(cfg, "frequency"); mmr70.power = cfg_getbool(cfg, "poweron"); mmr70.txpower = cfg_getint(cfg, "txpower"); mmr70.mute = 0; mmr70.gain = cfg_getbool(cfg, "gain"); mmr70.volume = cfg_getint(cfg, "volume"); mmr70.stereo = cfg_getbool(cfg, "stereo"); mmr70.rds = cfg_getbool(cfg, "rdsenable"); strncpy(mmr70.rdsid, cfg_getstr(cfg, "rdsid"), 8); strncpy(mmr70.rdstext, cfg_getstr(cfg, "rdstext"), 64); // apply configuration parameters ns741_txpwr(mmr70.txpower); ns741_mute(mmr70.mute); ns741_stereo(mmr70.stereo); ns741_rds_set_progname(mmr70.rdsid); ns741_rds_set_radiotext(mmr70.rdstext); ns741_power(mmr70.power); ns741_input_gain(mmr70.gain); ns741_volume(mmr70.volume); // Use RPI_REV1 for earlier versions of Raspberry Pi rpi_pin_init(RPI_REVISION); // Get file descriptor for RDS handler polls[1].revents = 0; if (mmr70.rds) { int rds = rpi_pin_poll_enable(rdsint, EDGE_FALLING); if (rds < 0) { printf("Couldn't enable RDS support\n"); run = 0; } polls[1].fd = rds; polls[1].events = POLLPRI; nfds = 2; if (ledpin > 0) { rpi_pin_export(ledpin, RPI_OUTPUT); rpi_pin_set(ledpin, led); } ns741_rds(1); ns741_rds_isr(); // send first two bytes } // main polling loop int ledcounter = 0; while(run) { if (poll(polls, nfds, -1) < 0) break; if (polls[1].revents) { rpi_pin_poll_clear(polls[1].fd); ns741_rds_isr(); // flash LED if enabled on every other RDS refresh cycle if (ledpin > 0) { ledcounter++; if (!(ledcounter % 80)) { led ^= 1; rpi_pin_set(ledpin, led); } } } if (polls[0].revents) ProcessTCP(lst, &mmr70); } // clean up at exit ns741_power(0); if (mmr70.rds) rpi_pin_unexport(rdsint); if (ledpin > 0) { rpi_pin_set(ledpin, 0); rpi_pin_unexport(ledpin); } close(lst); closelog(); return 0; }
int ProcessTCP(int sock, mmr70_data_t *pdata) { /* Puffer und Strukturen anlegen */ struct sockaddr_in clientaddr; socklen_t clen = sizeof(clientaddr); char buffer[512]; bzero(buffer, sizeof(buffer)); /* Auf Verbindung warten, bei Verbindung Connected-Socket erstellen */ int csd = accept(sock, (struct sockaddr *)&clientaddr, &clen); struct pollfd pol; pol.fd = csd; pol.events = POLLRDNORM; // just to be on a safe side check if data is available if (poll(&pol, 1, 1000) <= 0) { close(csd); return -1; } int len = recv(csd, buffer, sizeof(buffer) - 2, 0); buffer[len] = '\0'; char *end = buffer + len - 1; // remove any trailing spaces while((end != buffer) && (*end <= ' ')) { *end-- = '\0'; } do { const char *arg; if (str_is_arg(buffer, "set freq", &arg)) { int frequency = atoi(arg); if ((frequency >= 76000) && (frequency <= 108000)) { syslog(LOG_NOTICE, "Changing frequency...\n"); ns741_set_frequency(frequency); pdata->frequency = frequency; } else { syslog(LOG_NOTICE, "Bad frequency.\n"); } break; } if (str_is(buffer, "poweroff")) { ns741_power(0); pdata->power = 0; break; } if (str_is(buffer, "poweron")) { ns741_power(1); ns741_rds(1); ns741_rds_reset_radiotext(); pdata->power = 1; break; } if (str_is(buffer, "muteon")) { ns741_mute(1); pdata->mute = 1; break; } if (str_is(buffer, "muteoff")) { ns741_mute(0); pdata->mute = 0; break; } if (str_is(buffer, "gainlow")) { ns741_input_gain(1); pdata->gain = 1; break; } if (str_is(buffer, "gainoff")) { ns741_input_gain(0); pdata->gain = 0; break; } if (str_is_arg(buffer, "set volume", &arg)) { int volume = atoi(arg); if ((volume >= 0) && (volume <= 6)) { syslog(LOG_NOTICE, "Changing volume level...\n"); ns741_volume(volume); pdata->volume = volume; } else { syslog(LOG_NOTICE, "Bad volume level. Range 0-6\n"); } break; } if (str_is_arg(buffer, "set stereo", &arg)) { if (str_is(arg, "on")) { syslog(LOG_NOTICE, "Enabling stereo signal...\n"); ns741_stereo(1); pdata->stereo = 1; break; } if (str_is(arg, "off")) { syslog(LOG_NOTICE, "Disabling stereo signal...\n"); ns741_stereo(0); pdata->stereo = 0; } break; } if (str_is_arg(buffer, "set txpwr", &arg)) { int txpwr = atoi(arg); if ((txpwr >= 0) && (txpwr <= 3)) { syslog(LOG_NOTICE, "Changing transmit power...\n"); ns741_txpwr(txpwr); pdata->txpower = txpwr; } else { syslog(LOG_NOTICE, "Bad transmit power. Range 0-3\n"); } break; } if (str_is_arg(buffer, "set rdstext", &arg)) { strncpy(pdata->rdstext, arg, 64); ns741_rds_set_radiotext(pdata->rdstext); break; } if (str_is_arg(buffer, "set rdsid", &arg)) { bzero(pdata->rdsid, sizeof(pdata->rdsid)); strncpy(pdata->rdsid, arg, 8); // ns741_rds_set_progname() will pad rdsid with spaces if needed ns741_rds_set_progname(pdata->rdsid); ns741_rds_reset_radiotext(); break; } if (str_is(buffer, "die") || str_is(buffer, "stop")) { run = 0; syslog(LOG_NOTICE, "Shutting down.\n"); break; } if (str_is(buffer, "status")) { bzero(buffer, sizeof(buffer)); sprintf(buffer, "freq: %dKHz txpwr: %.2fmW power: '%s' mute: '%s' gain: '%s' volume: '%d' stereo: '%s' rds: '%s' rdsid: '%s' rdstext: '%s'\n", pdata->frequency, txpower[pdata->txpower], pdata->power ? "on" : "off", pdata->mute ? "on" : "off", pdata->gain ? "on" : "off", pdata->volume, pdata->stereo ? "on" : "off", pdata->rds ? "on" : "off", pdata->rdsid, pdata->rdstext); write(csd, buffer, strlen(buffer) + 1); break; } } while(0); close(csd); return 0; }
/* * Parses the argument given in the command line of the application, * calculates mask for used cores and initializes EAL with calculated core mask */ int app_parse_args(int argc, char **argv) { int opt, ret; int option_index; const char *optname; char *prgname = argv[0]; uint32_t i, nb_lcores; static struct option lgopts[] = { { "pfc", 1, 0, 0 }, { "mst", 1, 0, 0 }, { "rsz", 1, 0, 0 }, { "bsz", 1, 0, 0 }, { "msz", 1, 0, 0 }, { "rth", 1, 0, 0 }, { "tth", 1, 0, 0 }, { "cfg", 1, 0, 0 }, { NULL, 0, 0, 0 } }; /* initialize EAL first */ ret = rte_eal_init(argc, argv); if (ret < 0) return -1; argc -= ret; argv += ret; /* set en_US locale to print big numbers with ',' */ setlocale(LC_NUMERIC, "en_US.utf-8"); while ((opt = getopt_long(argc, argv, "i", lgopts, &option_index)) != EOF) { switch (opt) { case 'i': printf("Interactive-mode selected\n"); interactive = 1; break; /* long options */ case 0: optname = lgopts[option_index].name; if (str_is(optname, "pfc")) { ret = app_parse_flow_conf(optarg); if (ret) { RTE_LOG(ERR, APP, "Invalid pipe configuration %s\n", optarg); return -1; } break; } if (str_is(optname, "mst")) { app_master_core = (uint32_t)atoi(optarg); break; } if (str_is(optname, "rsz")) { ret = app_parse_ring_conf(optarg); if (ret) { RTE_LOG(ERR, APP, "Invalid ring configuration %s\n", optarg); return -1; } break; } if (str_is(optname, "bsz")) { ret = app_parse_burst_conf(optarg); if (ret) { RTE_LOG(ERR, APP, "Invalid burst configuration %s\n", optarg); return -1; } break; } if (str_is(optname, "msz")) { mp_size = atoi(optarg); if (mp_size <= 0) { RTE_LOG(ERR, APP, "Invalid mempool size %s\n", optarg); return -1; } break; } if (str_is(optname, "rth")) { ret = app_parse_rth_conf(optarg); if (ret) { RTE_LOG(ERR, APP, "Invalid RX threshold configuration %s\n", optarg); return -1; } break; } if (str_is(optname, "tth")) { ret = app_parse_tth_conf(optarg); if (ret) { RTE_LOG(ERR, APP, "Invalid TX threshold configuration %s\n", optarg); return -1; } break; } if (str_is(optname, "cfg")) { cfg_profile = optarg; break; } break; default: app_usage(prgname); return -1; } } /* check master core index validity */ for(i = 0; i <= app_master_core; i++) { if (app_used_core_mask & (1u << app_master_core)) { RTE_LOG(ERR, APP, "Master core index is not configured properly\n"); app_usage(prgname); return -1; } } app_used_core_mask |= 1u << app_master_core; if ((app_used_core_mask != app_eal_core_mask()) || (app_master_core != rte_get_master_lcore())) { RTE_LOG(ERR, APP, "EAL core mask not configured properly, must be %" PRIx64 " instead of %" PRIx64 "\n" , app_used_core_mask, app_eal_core_mask()); return -1; } if (nb_pfc == 0) { RTE_LOG(ERR, APP, "Packet flow not configured!\n"); app_usage(prgname); return -1; } /* sanity check for cores assignment */ nb_lcores = app_cpu_core_count(); for(i = 0; i < nb_pfc; i++) { if (qos_conf[i].rx_core >= nb_lcores) { RTE_LOG(ERR, APP, "pfc %u: invalid RX lcore index %u\n", i + 1, qos_conf[i].rx_core); return -1; } if (qos_conf[i].wt_core >= nb_lcores) { RTE_LOG(ERR, APP, "pfc %u: invalid WT lcore index %u\n", i + 1, qos_conf[i].wt_core); return -1; } uint32_t rx_sock = rte_lcore_to_socket_id(qos_conf[i].rx_core); uint32_t wt_sock = rte_lcore_to_socket_id(qos_conf[i].wt_core); if (rx_sock != wt_sock) { RTE_LOG(ERR, APP, "pfc %u: RX and WT must be on the same socket\n", i + 1); return -1; } app_numa_mask |= 1 << rte_lcore_to_socket_id(qos_conf[i].rx_core); } return 0; }
void test_classify(void) { icalcomponent *c, *match; int i = 0; int error_count = 0; /* Open up the two storage files, one for the incomming components, one for the calendar */ icalfileset_options options = { O_RDONLY, 0644, 0, NULL }; icalset *incoming = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/incoming.ics", &options); icalset *cal = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/calendar.ics", &options); icalset *f = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/classify.ics", &options); ok("opening file classify.ics", (f != 0)); ok("opening file calendar.ics", (cal != 0)); ok("opening file incoming.ics", (incoming != 0)); /* some basic tests.. */ if (f) { c = icalset_get_first_component(f); match = icalset_get_next_component(f); ok("test two vcalendars for SEQUENCE with icalclassify()", (icalclassify(c, match, "*****@*****.**") == ICAL_XLICCLASS_REQUESTRESCHEDULE)); icalset_free(f); } assert(incoming != 0); assert(cal != 0); /* Iterate through all of the incoming components */ for (c = icalset_get_first_component(incoming); c != 0; c = icalset_get_next_component(incoming)) { icalproperty_xlicclass class; icalcomponent *match = 0; const char *this_uid; const char *this_note = get_note(c); const char *expected_result = get_expect(c); const char *actual_result; char msg[128]; i++; /* Check this component against the restrictions imposed by iTIP. An errors will be inserted as X-LIC-ERROR properties in the component. The Parser will also insert errors if it cannot parse the component */ icalcomponent_check_restrictions(c); /* If there are any errors, print out the component */ error_count = icalcomponent_count_errors(c); snprintf(msg, sizeof(msg), "%s - parsing", this_note); int_is(msg, error_count, 0); if (error_count != 0) { if (VERBOSE) { printf("----- Component has errors ------- \n%s-----------------\n", icalcomponent_as_ical_string(c)); } } /* Use one of the icalcomponent convenience routines to get the UID. This routine will save you from having to use icalcomponent_get_inner(), icalcomponent_get_first_property(), checking the return value, and then calling icalproperty_get_uid. There are several other convenience routines for DTSTART, DTEND, DURATION, SUMMARY, METHOD, and COMMENT */ this_uid = icalcomponent_get_uid(c); if (this_uid != 0) { /* Look in the calendar for a component with the same UID as the incomming component. We should reall also be checking the RECURRENCE-ID. Another way to do this operation is to us icalset_find_match(), which does use the RECURRENCE-ID. */ match = icalset_fetch(cal, this_uid); } /* Classify the incoming component. The third argument is the calid of the user who owns the calendar. In a real program, you would probably switch() on the class. */ class = icalclassify(c, match, "*****@*****.**"); /** eventually test this too.. **/ (void)get_note(match); actual_result = icalproperty_enum_to_string(class); snprintf(msg, sizeof(msg), "expecting %s", expected_result); str_is(msg, expected_result, actual_result); if (VERBOSE) { printf("Test %d\n" "Incoming: %s\n" "Matched: %s\n" "Classification: %s\n\n", i, this_note, get_note(match), icalproperty_enum_to_string(class)); } } icalset_free(incoming); icalset_free(cal); }