Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Phonebook (PB) to modem.cpp. It also stores PB in the config file #426

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
This file is automatically modified by the build_version.py script
to update the BUILD and BUILD_DATE values based on the
current Git commit ID and current UTC date/time.

FN_VERSION_MAJOR and FN_VERSION_MINOR should be manually updated
as needed.
*/

#define FN_VERSION_MAJOR 0
#define FN_VERSION_MINOR 5

#define FN_VERSION_BUILD "a673ceb2"

#define FN_VERSION_DATE "2021-01-18 19:37:39"

#define FN_VERSION_FULL "0.5.a673ceb2"
/*
This file is automatically modified by the build_version.py script
to update the BUILD and BUILD_DATE values based on the
current Git commit ID and current UTC date/time.
FN_VERSION_MAJOR and FN_VERSION_MINOR should be manually updated
as needed.
*/
#define FN_VERSION_MAJOR 0
#define FN_VERSION_MINOR 5
#define FN_VERSION_BUILD "bec03534"
#define FN_VERSION_DATE "2021-01-18 22:12:42"
#define FN_VERSION_FULL "0.5.bec03534"
150 changes: 150 additions & 0 deletions lib/config/fnConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#define CONFIG_DEFAULT_SNTPSERVER "pool.ntp.org"

#define PHONEBOOK_CHAR_WIDTH 12

fnConfig Config;

// Initialize some defaults
Expand Down Expand Up @@ -250,6 +252,97 @@ void fnConfig::clear_mount(uint8_t num, mount_type_t mounttype)
}
}

//The next section its to handle the phonebook entries
std::string fnConfig::get_pb_host_name(const char *pbnum)
{
int i=0;
while ( i<MAX_PB_SLOTS && ( _phonebook_slots[i].phnumber.compare(pbnum)!=0 ) )
i++;

//Number found
if (i<MAX_PB_SLOTS)
return _phonebook_slots[i].hostname;

//Return empty (not found)
return std::string();
}
std::string fnConfig::get_pb_host_port(const char *pbnum)
{
int i=0;
while ( i<MAX_PB_SLOTS && ( _phonebook_slots[i].phnumber.compare(pbnum)!=0 ) )
i++;

//Number found
if (i<MAX_PB_SLOTS)
return _phonebook_slots[i].port;

//Return empty (not found)
return std::string();
}
bool fnConfig::add_pb_number(const char *pbnum, const char *pbhost, const char *pbport)
{
//Check maximum lenght of phone number
if ( strlen(pbnum)>PHONEBOOK_CHAR_WIDTH )
return false;

int i=0;
while (i<MAX_PB_SLOTS && !(_phonebook_slots[i].phnumber.empty()) )
i++;

//Empty found
if (i<MAX_PB_SLOTS)
{
_phonebook_slots[i].phnumber = pbnum;
_phonebook_slots[i].hostname = pbhost;
_phonebook_slots[i].port = pbport;
_dirty = true;
save();
return true;
}
return false;

}
bool fnConfig::del_pb_number(const char *pbnum)
{
int i=0;
while ( i<MAX_PB_SLOTS && ( _phonebook_slots[i].phnumber.compare(pbnum)!=0 ) )
i++;

//Number found
if (i<MAX_PB_SLOTS)
{
_phonebook_slots[i].phnumber.clear();
_phonebook_slots[i].hostname.clear();
_phonebook_slots[i].port.clear();
_dirty = true;
save();
return true;
}
//Not found
return false;
}
void fnConfig::clear_pb(void)
{
for (int i=0; i<MAX_PB_SLOTS; i++)
{
_phonebook_slots[i].phnumber.clear();
_phonebook_slots[i].hostname.clear();
_phonebook_slots[i].port.clear();
}
save();
_dirty = true;
}
std::string fnConfig::get_pb_entry(uint8_t n)
{
if (_phonebook_slots[n].phnumber.empty())
return std::string();
std::string numberPadded = _phonebook_slots[n].phnumber;
numberPadded.append(PHONEBOOK_CHAR_WIDTH + 1 - numberPadded.length(), ' ');
std::string pbentry = numberPadded + _phonebook_slots[n].hostname + ":" + _phonebook_slots[n].port;
return pbentry;
}
////End of phonebook handling

// Returns printer type stored in configuration for printer slot
sioPrinter::printer_type fnConfig::get_printer_type(uint8_t num)
{
Expand Down Expand Up @@ -427,6 +520,18 @@ void fnConfig::save()
ss << LINETERM << "[Modem]" << LINETERM;
ss << "sniffer_enabled=" << _modem.sniffer_enabled << LINETERM;

//PHONEBOOK
for (i = 0; i < MAX_PB_SLOTS; i++)
{
if (!_phonebook_slots[i].phnumber.empty())
{
ss << LINETERM << "[Phonebook" << (i+1) << "]" LINETERM;
ss << "number=" << _phonebook_slots[i].phnumber << LINETERM;
ss << "host=" << _phonebook_slots[i].hostname << LINETERM;
ss << "port=" << _phonebook_slots[i].port << LINETERM;
}
}

// CASSETTE
ss << LINETERM << "[Cassette]" << LINETERM;
ss << "play_record=" << ((_cassette.button) ? "1 Record" : "0 Play") << LINETERM;
Expand Down Expand Up @@ -581,6 +686,9 @@ New behavior: copy from SD first if available, then read SPIFFS.
case SECTION_CASSETTE: //Jeff put this here to handle tape drive configuration (pulldown and play/record)
_read_section_cassette(ss);
break;
case SECTION_PHONEBOOK: //Mauricio put this here to handle the phonebook
_read_section_phonebook(ss, index);
break;
case SECTION_UNKNOWN:
break;
}
Expand Down Expand Up @@ -864,6 +972,37 @@ void fnConfig::_read_section_cassette(std::stringstream &ss)
}
}

void fnConfig::_read_section_phonebook(std::stringstream &ss, int index)
{
// Throw out any existing data for this index
_phonebook_slots[index].phnumber.clear();
_phonebook_slots[index].hostname.clear();
_phonebook_slots[index].port.clear();

std::string line;
// Read lines until one starts with '[' which indicates a new section
while (_read_line(ss, line, '[') >= 0)
{
std::string name;
std::string value;
if (_split_name_value(line, name, value))
{
if (strcasecmp(name.c_str(), "number") == 0)
{
_phonebook_slots[index].phnumber = value;
}
else if (strcasecmp(name.c_str(), "host") == 0)
{
_phonebook_slots[index].hostname = value;
}
else if (strcasecmp(name.c_str(), "port") == 0)
{
_phonebook_slots[index].port = value;
}
}
}
}

/*
Looks for [SectionNameX] where X is an integer
Returns which SectionName was found and sets index to X if X is an integer
Expand Down Expand Up @@ -948,6 +1087,17 @@ fnConfig::section_match fnConfig::_find_section_in_line(std::string &line, int &
{
return SECTION_CASSETTE;
}
else if (strncasecmp("Phonebook", s1.c_str(), 9) == 0)
{
index = atoi((const char *)(s1.c_str() + 9)) - 1;
if (index < 0 || index >= MAX_PB_SLOTS)
{
Debug_println("Invalid index value - discarding");
return SECTION_UNKNOWN;
}
//Debug_printf("Found Phonebook Entry %d\n", index);
return SECTION_PHONEBOOK;
}
}
}
return SECTION_UNKNOWN;
Expand Down
20 changes: 20 additions & 0 deletions lib/config/fnConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define MAX_MOUNT_SLOTS 8
#define MAX_PRINTER_SLOTS 4
#define MAX_TAPE_SLOTS 1
#define MAX_PB_SLOTS 16

#define BASE_TAPE_SLOT 0x1A

Expand Down Expand Up @@ -79,6 +80,14 @@ class fnConfig
void store_host(uint8_t num, const char *hostname, host_type_t type);
void clear_host(uint8_t num);

// PHONEBOOK SLOTS
std::string get_pb_host_name(const char *pbnum);
std::string get_pb_host_port(const char *pbnum);
std::string get_pb_entry(uint8_t n);
bool add_pb_number(const char *pbnum, const char *pbhost, const char *pbport);
bool del_pb_number(const char *pbnum);
void clear_pb(void);

// MOUNTS
std::string get_mount_path(uint8_t num, mount_type_t mounttype = mount_type_t::MOUNTTYPE_DISK);
mount_mode_t get_mount_mode(uint8_t num, mount_type_t mounttype = mount_type_t::MOUNTTYPE_DISK);
Expand Down Expand Up @@ -122,6 +131,7 @@ class fnConfig
void _read_section_tape(std::stringstream &ss, int index);
void _read_section_modem(std::stringstream &ss);
void _read_section_cassette(std::stringstream &ss);
void _read_section_phonebook(std::stringstream &ss, int index);

enum section_match
{
Expand All @@ -135,6 +145,7 @@ class fnConfig
SECTION_TAPE,
SECTION_MODEM,
SECTION_CASSETTE,
SECTION_PHONEBOOK,
SECTION_UNKNOWN
};
section_match _find_section_in_line(std::string &line, int &index);
Expand Down Expand Up @@ -219,6 +230,13 @@ class fnConfig
bool button = false;
};

struct phbook_info
{
std::string phnumber;
std::string hostname;
std::string port;
};

host_info _host_slots[MAX_HOST_SLOTS];
mount_info _mount_slots[MAX_MOUNT_SLOTS];
printer_info _printer_slots[MAX_PRINTER_SLOTS];
Expand All @@ -230,6 +248,8 @@ class fnConfig
general_info _general;
modem_info _modem;
cassette_info _cassette;

phbook_info _phonebook_slots[MAX_PB_SLOTS];
};

extern fnConfig Config;
Expand Down
Loading