/******************************************************************
 * change baudrate
 ******************************************************************/
static int writeBaudrate(
    int   action,
    u_char *var_val,
    u_char var_val_type,
    size_t var_val_len,
    u_char *statP,
    oid    *name,
    size_t name_len
)
{
    unsigned long value = *(unsigned long *)var_val;
    DEBUGMSGTL(("aj_serial", "%s()\n", __FUNCTION__));

    switch(action) {
    case RESERVE1:
        if(var_val_type != ASN_INTEGER) {
            DEBUGMSGTL(("aj_serial", "%x net inter type", var_val_type));
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len != sizeof(long)) {
            DEBUGMSGTL(("aj_serial", "wrong length %x", var_val_len));
            return SNMP_ERR_WRONGLENGTH;
        }
        if(!check_baudrate(value)) {
            return SNMP_ERR_WRONGVALUE;
        }
        break;

    case ACTION:
        if(g_serial_setting.baudrate != value) {
            g_serial_setting.baudrate = value;
            g_save_flag = 1;
        }
        break;

    case COMMIT:
        if(g_save_flag) {
            save_serial_config();
            g_save_flag = 0;
        }
        break;
    default:
        break;
    }

    return SNMP_ERR_NOERROR;
}
Beispiel #2
0
/* open serial port
 * returns the file descriptor on success or -1 on error */
int open_port(const char *pPort, int pBaud)
{
	/* serial port file descriptor */
	int fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
	
	if(fd == -1) {
		msg_Err("Unable to open serial-port");
		msg_Info("Retrying in 5 seconds...");
		sleep(5);
		fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
	}
	
	if (fd == -1) {
		die("Unable to open serial-port");
	} else {
		fcntl(fd, F_SETFL, 0);
		
		struct termios options;
		
		/* get the current options for the port */
		tcgetattr(fd, &options);
		
		check_baudrate(pBaud);
		
		/* set baudrate */
		cfsetispeed(&options, pBaud);
		cfsetospeed(&options, pBaud);
		
		/* enable the receiver and set local mode */
		options.c_cflag |= (CLOCAL | CREAD);
		
		/* set the new options for the port */
		tcsetattr(fd, TCSANOW, &options);
		
		msg_Dbg("BAUDRATE SET TO %i",pBaud);
	}
	return (fd);
}
/******************************************************************
 * load serial setting
 *****************************************************************/
static void load_serial_setting(void)
{
    char buff[256];
    FILE* fp = fopen(SERIAL_CONFIG_FILE, "r");
    if(!fp) {
        DEBUGMSGTL(("aj_serial", "init_serial_seting()\n"));
        return;
    }

    /* get value from config file */
    while(fgets(buff, sizeof(buff), fp)) {
        const char* fmt = "%s %u";
        char key[64];
        unsigned long value;

        if(sscanf(buff, fmt, key, &value) != 2) {
            DEBUGMSGTL(("aj_serial", "format invalid %s\n", buff));
            continue;
        }

        if(!strcmp(key, SOCK_PROTO_KEY_STR)) {
            if(check_sock_proto(value)) {
                g_sock_proto = value;
            }
        }
        else if(!strcmp(key, CONNECT_ADDR_KEY_STR)) {
            g_connect_addr = value;
        }
        else if(!strcmp(key, PORTNO_KEY_STR)) {
            if(check_portno(value)) {
                g_port_no = value;
            } else {
                g_port_no = DEFAULT_PORT_NO;
            }
        }
        else if(!strcmp(key, BAUDRATE_KEY_STR)) {
            if(check_baudrate(value)) {
                g_serial_setting.baudrate = value;
            }
        }
        else if(!strcmp(key, STOPBIT_KEY_STR)) {
            if(check_stop_bit(value)) {
                g_serial_setting.stop_bit = (stop_bit_e)value;
            }
        }
        else if(!strcmp(key, PARITY_KEY_STR)) {
            if(check_parity(value)) {
                g_serial_setting.parity = (parity_e)value;
            }
        }
        else if(!strcmp(key, FLOWCTRL_KEY_STR)) {
            if(check_flow_ctrl(value)) {
                g_serial_setting.flow_ctrl = (flow_ctrl_e)value;
            }
        }
        else {
            DEBUGMSGTL(("aj_serial", "invalid key %s\n", key));
        }
    }

    fclose(fp);
    return;
}