Skip to content

Commit

Permalink
* Upgrade to use either CSV or MySQL database backend
Browse files Browse the repository at this point in the history
* Ability to store copy of log in database as well as flatfile.
  • Loading branch information
jethrocarr committed Feb 28, 2010
1 parent aa626ef commit 3d0b945
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
72 changes: 56 additions & 16 deletions app/o4send-srv.pl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# By default, o4send will only ever send a file once, regardless whether the phone
# accepts it or not, that way if a user refuses, they won't keep getting nagged.
#
# It is possible to configure it to send messages to a particular phone multiple times
# though for testing purposes, by defining an exception.
#
#
# Copyright (C) 2010 Amberdms Ltd
#
Expand Down Expand Up @@ -53,18 +56,33 @@

# configuration
my $location_file = "test.txt"; # full path to file to send to bluetooth phone
my $location_csv = "db/"; # location to store CSV database of MAC addresses
my $location_logfile = "/dev/tty12"; # location of log file

my $debug = 1; # enable/disable debugging


# database configuration
my $db_type = "mysql"; # either "mysql" or "csv"
my $db_log = 1; # set to 1 to enable logging into the database as well as the log file.

my $db_mysql_db = "o4send"; # mysql database name
my $db_mysql_user = "root"; # mysql database user
my $db_mysql_pwd = ""; # mysql database password

my $location_csv = "db/"; # location to store CSV database of MAC addresses (if CSV database is enabled)


# application paths
my $app_hcitool = "/usr/bin/hcitool"; # hcitool for scanning for wifi
my $app_obexftp = "/usr/bin/obexftp"; # tool for sending files to phone
my $app_sdptool = "/usr/bin/sdptool"; # tool for fetching capabilities from phone



# pre-delare some key variables
my $dbh;



#########################################################################
# #
Expand Down Expand Up @@ -303,14 +321,20 @@ ($$)
}
}

# if set, print copy of message to log file
# if enabled, print copy of message to log file
if ($location_logfile ne "")
{
open(LOG,">>$location_logfile") || print "Warning: Unable to write to log file!\n";
print LOG "[$log_category] $log_entry\n";
close(LOG);
}

# if enabled, record log message in database
if ($db_log && $dbh)
{
$dbh->do("INSERT INTO app_log (timestamp, category, message) VALUES (". time() .", '$log_category', '$log_entry')");
}

print "[$log_category] $log_entry\n";

} # end of log_add
Expand Down Expand Up @@ -356,25 +380,39 @@ ($)
#########################################################################


# start of application
log_add("info", "Started openbluedistribute_srv");


# 1. Checksum the file.
my $checksum = file_checksum($location_file);
# 1. Connect to the phone database (and seed the DB if needed)
if ($db_type eq "mysql")
{
# Use MySQL database
# 'DBI:mysql:databasename', 'username', 'password'
$dbh = DBI->connect("DBI:mysql:$db_mysql_db", $db_mysql_user, $db_mysql_pwd) || die("Unable to connect to MySQL database in $location_csv");
}
else
{
# Use CSV files for database
mkdir ($location_csv);
$dbh = DBI->connect("DBI:CSV:f_dir=$location_csv") || die("Unable to connect to CSV database in $location_csv");

# if the CSV file doesn't exist, we need to create the table structure
if (! -e "$location_csv/phones_seen")
{
$dbh->do("CREATE TABLE phones_seen (timestamp INTEGER, bt_phone_mac CHAR(17), transfer_filemd5sum CHAR(32), transfer_status CHAR(7))");
$dbh->do("CREATE TABLE app_log (timestamp INTEGER, category CHAR(20), message CHAR(255)");
}
}


# 2. Connect to the phone database (and seed the DB if needed)
# start of application
log_add("info", "Started openbluedistribute_srv");



# 2. Checksum the file.
my $checksum = file_checksum($location_file);

mkdir ($location_csv);
my $dbh = DBI->connect("DBI:CSV:f_dir=$location_csv") || die("Unable to connect to CSV database in $location_csv");

# if the CSV file doesn't exist, we need to create the table structure
if (! -e "$location_csv/phones_seen")
{
$dbh->do("CREATE TABLE phones_seen (timestamp INTEGER, bt_phone_mac CHAR(17), transfer_filemd5sum CHAR(32), transfer_status CHAR(7))");
}



Expand Down Expand Up @@ -420,11 +458,13 @@ ($)
}


# disconnect from DB
$dbh->disconnect();

# end of application loop
log_add("info", "Closing down openbluedistribute_srv");

# disconnect from DB
$dbh->disconnect();


exit 0;

25 changes: 25 additions & 0 deletions schema/o4send_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--
-- o4send template database schema
--

CREATE DATABASE `o4send` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `o4send`;


CREATE TABLE IF NOT EXISTS `app_log` (
`id` int(11) NOT NULL auto_increment,
`timestamp` bigint(20) NOT NULL,
`category` char(20) NOT NULL,
`message` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `phones_seen` (
`id` int(11) NOT NULL auto_increment,
`timestamp` bigint(20) NOT NULL,
`bt_phone_mac` char(17) NOT NULL,
`transfer_filemd5sum` char(32) NOT NULL,
`transfer_status` char(7) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

0 comments on commit 3d0b945

Please sign in to comment.