coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter) { assert(pdu); assert(oi); memset(oi, 0, sizeof(coap_opt_iterator_t)); if (pdu->hdr->optcnt) { oi->optcnt = pdu->hdr->optcnt; oi->option = options_start(pdu); oi->type = COAP_OPT_DELTA(oi->option); memcpy(oi->filter, filter, sizeof(coap_opt_filter_t)); return oi; } return NULL; }
void for_each_option(coap_pdu_t *pdu, void (*f)(coap_opt_t *, unsigned char, unsigned int, const unsigned char *) ) { unsigned char cnt; coap_opt_t *opt; unsigned char opt_code = 0; if (! pdu ) return; opt = options_start( pdu ); for ( cnt = pdu->hdr->optcnt; cnt; --cnt ) { opt_code += COAP_OPT_DELTA(*opt); f ( opt, opt_code, COAP_OPT_LENGTH(*opt), COAP_OPT_VALUE(*opt) ); opt = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); } }
/* ** Use the options to set the baud rate and such. ** This is a parser and interpreter. ** ** The options string is in the form: ** speed=9600 parity=none bits=8 xonxoff=yes */ static void set_options(const char *printer_name, const char *printer_options, int portfd, struct termios *settings, struct OPTIONS *options) { struct OPTIONS_STATE o; char name[16]; char value[16]; int retval; struct termios readback; /* Set defaults. If changeable, default for online is DSR and CTS must be asserted. We don't normaly opt to detect hangups (which I believe means loss of CD). We don't normally send ^T's. */ #ifdef TIOCMGET options->online = TIOCM_DSR | TIOCM_CTS; #endif options->detect_hangups = FALSE; options->idle_status_interval = 0; /* If feedback is on and control-d handshaking is on, turn on the ^T stuff. */ if(int_cmdline.feedback && (int_cmdline.jobbreak == JOBBREAK_CONTROL_D || int_cmdline.jobbreak == JOBBREAK_PJL) ) options->idle_status_interval = 15; /* Parse the interface options. */ options_start(printer_options, &o); while((retval = options_get_one(&o, name, sizeof(name), value, sizeof(value))) > 0) { /* Intepret the keyword. */ if(strcmp(name, "speed") == 0) { speed_t speed; if((speed = speed_convert(value)) == B0 || cfsetispeed(settings, speed) == -1 || cfsetospeed(settings, speed) == -1) { o.error = N_("Illegal \"speed=\" value"); retval = -1; break; } } else if(strcmp(name, "xonxoff") == 0) { gu_boolean answer; if(gu_torf_setBOOL(&answer,value) == -1) { o.error = N_("Invalid boolean value"); retval = -1; break; } if(answer) /* on */ settings->c_iflag |= IXON | IXOFF; else /* off */ settings->c_iflag &= ~(IXON | IXOFF); } else if(strcmp(name, "rtscts") == 0) { gu_boolean answer; if(gu_torf_setBOOL(&answer,value) == -1) { o.error = N_("Invalid boolean value"); retval = -1; break; } #ifdef CRTSCTS #ifdef CRTSXOFF /* SunOS 5.x */ if(answer) settings->c_cflag |= (CRTSCTS | CRTSXOFF); else settings->c_cflag &= ~(CRTSCTS | CRTSXOFF); #else /* Linux */ if(answer) settings->c_cflag |= CRTSCTS; else settings->c_cflag &= ~CRTSCTS; #endif #else o.error = N_("No OS support for RTS/CTS handshaking"); retval = -1; break; #endif } else if(strcmp(name, "parity") == 0) { if(strcmp(value, "none") == 0) { settings->c_cflag &= ~PARENB; /* clear parity enable */ } else if(strcmp(value, "even") == 0) { settings->c_cflag &= ~PARODD; /* clear odd parity */ settings->c_cflag |= PARENB; /* enable parity */ } else if(strcmp(value, "odd") == 0) { settings->c_cflag |= PARODD; /* set odd parity */ settings->c_cflag |= PARENB; /* enable parity */ } else { o.error = N_("Only valid values are \"odd\", \"even\", and \"none\""); retval = -1; break; } } else if(strcmp(name, "bits") == 0) { int bits; if((bits = atoi(value)) != 7 && bits != 8) { o.error = N_("Only valid values are 7 and 8"); retval = -1; break; } settings->c_cflag &= ~CSIZE; /* clear old setting */ if(bits == 7) settings->c_cflag |= CS7; else settings->c_cflag |= CS8; } else if(strcmp(name, "online") == 0) { #ifdef TIOCMGET if(gu_strcasecmp(value, "dsr/cts") == 0) options->online = TIOCM_DSR | TIOCM_CTS; else if(gu_strcasecmp(value, "dsr") == 0) options->online = TIOCM_DSR; else if(gu_strcasecmp(value, "cts") == 0) options->online = TIOCM_CTS; else if(gu_strcasecmp(value, "none") == 0) options->online = 0; else { o.error = N_("Only valid values are \"DSR/CTS\", \"DSR\", \"CTS\", and \"none\""); retval = -1; break; } #else o.error = N_("No OS support for DSR or CTS state reporting"); retval = -1; break; #endif } else if(strcmp(name, "detect_hangups") == 0) { if(gu_torf_setBOOL(&options->detect_hangups,value) == -1) { o.error = N_("Invalid boolean value"); retval = -1; break; } } else if(strcmp(name, "hangup_on_close") == 0) { gu_boolean answer; if(gu_torf_setBOOL(&answer,value) == -1) { o.error = N_("Invalid boolean value"); retval = -1; break; } if(answer) settings->c_cflag |= HUPCL; else settings->c_cflag &= ~HUPCL; } else if(strcmp(name, "idle_status_interval") == 0) { if((options->idle_status_interval = atoi(value)) < 0) { o.error = N_("Negative value not allowed"); retval = -1; break; } } else { o.error = N_("unrecognized keyword"); retval = -1; break; } } /* end of while() */ /* See if final call to options_get_one() detected an error: */ if(retval == -1) { alert(int_cmdline.printer, TRUE, _("Option parsing error: %s"), gettext(o.error)); alert(int_cmdline.printer, FALSE, "%s", o.options); alert(int_cmdline.printer, FALSE, "%*s^ %s", o.index, "", _("right here")); int_exit(EXIT_PRNERR_NORETRY_BAD_SETTINGS); } /* We can't use control-T status updates if the job isn't PostScript, so override it in that case. */ if(int_cmdline.barbarlang[0]) options->idle_status_interval = 0; /* Make sure the codes setting is ok. */ if((settings->c_cflag & CSIZE) == CS7 && int_cmdline.codes != CODES_Clean7Bit && int_cmdline.codes != CODES_UNKNOWN) { alert(int_cmdline.printer, TRUE, _("%s interface: \"codes\" setting must be \"Clean7Bit\" if the option \"bits=7\" is set."), int_cmdline.int_basename); int_exit(EXIT_PRNERR_NORETRY_BAD_SETTINGS); } /* If detect_hangups was set to true, clear CLOCAL: */ if(options->detect_hangups) settings->c_cflag &= ~CLOCAL; /* Write the new port settings. */ if(tcsetattr(portfd, TCSANOW, settings) == -1) { alert(int_cmdline.printer, TRUE, "%s interface: tcsetattr() failed, errno=%d (%s)", int_cmdline.int_basename, errno, gu_strerror(errno)); int_exit(EXIT_PRNERR); } /* Make sure they were written correctly: */ if(tcgetattr(portfd, &readback) == -1) { alert(int_cmdline.printer, TRUE, "%s interface: tcgetattr() failed, errno=%d (%s)", int_cmdline.int_basename, errno, gu_strerror(errno)); int_exit(EXIT_PRNERR); } /* This code doesn't work. */ #if 0 if(memcmp(settings, &readback, sizeof(struct termios))) { alert(int_cmdline.printer, TRUE, _("%s interface: serial port driver does not support selected options"), int_cmdline.int_basename); int_exit(EXIT_PRNERR_NORETRY); } #endif } /* end of set_options() */