require 'yaml' require 'peak/tnc_port' require 'apex/aprs_kiss' require 'apex/igate_tcp' require 'kiss' require 'aprs_is' module Peak def self.find_config(verbose, config_paths=[]) config_file = 'peak.conf' rc_file = '.peakrc' cur_path = config_file home_path = File.join(Dir.home, rc_file) etc_path = File.join('', 'etc', config_file) config_paths = [cur_path, home_path, etc_path] + config_paths if verbose puts 'Searching for configuration file in the following locations: ' + config_paths.inspect end config_paths.each do |config_path| if File.file?(config_path) return YAML::load_file(config_path) end end return nil end def self.config_lookup_enforce(config_map, key) unless config_map.key?(key) echo_colorized_error('Invalid configuration, could not find an ' + key + ' attribute in section') return false end return true end def self.init_port_map(config) igate_server = nil igate_callsign = nil igate_server_port = 14580 igate_password = nil igate_echo = 'none' config.each do |section_name, section_content| if section_name.eql?('IGATE') return nil unless config_lookup_enforce(section_content, 'server') igate_server = section_content['server'] return nil unless config_lookup_enforce(section_content, 'callsign') igate_callsign = section_content['callsign'] igate_server_port = section_content['server_port'] if section_content.key? 'server_port' igate_echo = section_content['echo'] if section_content.key? 'echo' return nil unless config_lookup_enforce(section_content, 'password') igate_password = section_content['password'] end end igate = nil port_map = {} unless igate_server.nil? igate = Apex::IGateTcp.new(igate_callsign, igate_password) igate.connect(igate_server, igate_server_port) port_map['IGATE'] = TncPort.new(igate: igate, echo_frames: igate_echo) end config.each do |section_name, section_content| if section_name.start_with?('TNC ') tnc_name = section_name.strip.split(' ')[1].strip if tnc_name == 'IGATE' echo_colorized_error('IGATE was used as the name for a TNC in the configuration, this name is reserved') return nil end kiss_tnc = nil if config_lookup_enforce(section_content, 'com_port') and config_lookup_enforce(section_content, 'baud') com_port = section_content['com_port'] baud = section_content['baud'] kiss_tnc = Apex::AprsKiss.new(Kiss::KissSerial.new(com_port, baud)) else return nil end if section_content.key?('kiss_init') kiss_init_string = section_content['kiss_init'] if kiss_init_string == 'MODE_INIT_W8DED' kiss_tnc.connect(Kiss::MODE_INIT_W8DED) elsif kiss_init_string == 'MODE_INIT_KENWOOD_D710' kiss_tnc.connect(Kiss::MODE_INIT_KENWOOD_D710) elsif kiss_init_string == 'NONE' kiss_tnc.connect else echo_colorized_error('Invalid configuration, value assigned to kiss_init was not recognized: ' + kiss_init_string) return nil end else kiss_tnc.connect end unless config_lookup_enforce(section_content, 'port_count') return nil end port_count = section_content['port_count'] (1..port_count).each do |port| port_name = tnc_name + '-' + port.to_s port_section_name = 'PORT ' + port_name unless config_lookup_enforce(config, port_section_name) return nil end port_section = config[port_section_name] unless config_lookup_enforce(port_section, 'identifier') return nil end port_identifier = port_section['identifier'] unless config_lookup_enforce(port_section, 'net') return nil end port_net = port_section['net'] unless config_lookup_enforce(port_section, 'tnc_port') return nil end tnc_port = port_section['tnc_port'] unless config_lookup_enforce(port_section, 'echo') return nil end echo_state = port_section['echo'] port_map[port_name] = TncPort.new(kiss_tnc, port_name, port_identifier, port_net, port = tnc_port, echo_frames: echo_state) end end end return port_map end end