Subversion Repositories public

Rev

Rev 230 | Rev 280 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 230 Rev 234
Line 28... Line 28...
28
#include <termios.h>
28
#include <termios.h>
29
#include <stdlib.h>
29
#include <stdlib.h>
30
#include <assert.h>
30
#include <assert.h>
31
#include <kmessagebox.h>
31
#include <kmessagebox.h>
32
#include <klocale.h>
32
#include <klocale.h>
-
 
33
#include <qfileinfo.h>
33
 
34
 
34
static char  G50_KEY[] = "A8A423B9F55E63C1"; // garmin network key
35
static char  G50_KEY[] = "A8A423B9F55E63C1"; // garmin network key
35
 
36
 
36
static char sendack1[] = "4402320499990000";
37
static char sendack1[] = "4402320499990000";
37
//                        44                message from pc?
38
//                        44                message from pc?
Line 76... Line 77...
76
	{
77
	{
77
	   device.close();
78
	   device.close();
78
	   qFile = false;
79
	   qFile = false;
79
	}
80
	}
80
 
81
 
81
	device.setName (fl.name ());
82
	device.setFileName (fl.fileName ());
82
 
83
 
83
	if (!device.exists ())
84
	if (!device.exists ())
84
	{
85
	{
85
	   KMessageBox::error (0, i18n("Device %1 does not exist!").arg(fl.name()));
86
	   KMessageBox::error (0, i18n("Device %1 does not exist!").arg(fl.fileName()));
86
	   return false;
87
	   return false;
87
	}
88
	}
88
 
89
 
89
	if (!device.open (IO_Raw | IO_ReadWrite))
90
	if (!device.open (QIODevice::ReadWrite))
90
	{
91
	{
91
	   KMessageBox::error (0, i18n("Error opening device %1.").arg(fl.name()),
92
	   KMessageBox::error (0, i18n("Error opening device %1.").arg(fl.fileName()),
92
	   	device.errorString());
93
	   	device.errorString());
93
	   return false;
94
	   return false;
94
	}
95
	}
95
 
96
 
96
	qFile = true;
97
	qFile = true;
Line 105... Line 106...
105
	{
106
	{
106
	   device.close ();
107
	   device.close ();
107
	   qFile = false;
108
	   qFile = false;
108
	}
109
	}
109
 
110
 
110
	f.setName (fl);
111
	f.setFileName (fl);
111
	return setDevice (f);
112
	return setDevice (f);
112
}
113
}
113
 
114
 
114
/*
115
/*
115
 * Send a message to the opened device.
116
 * Send a message to the opened device.
Line 136... Line 137...
136
	buf[3+i] = chk;
137
	buf[3+i] = chk;
137
	usleep (10 * 1000);
138
	usleep (10 * 1000);
138
 
139
 
139
	if ((4 + i) != (nw = write(device.handle(), buf, 4 + i)))
140
	if ((4 + i) != (nw = write(device.handle(), buf, 4 + i)))
140
	{
141
	{
141
	   KMessageBox::error (0, i18n("Error writing to device %1").arg(device.name()));
142
	   KMessageBox::error (0, i18n("Error writing to device %1").arg(device.fileName()));
142
	   return false;
143
	   return false;
143
	}
144
	}
144
 
145
 
145
	return true;
146
	return true;
146
}
147
}
Line 618... Line 619...
618
	peerdev = 0;
619
	peerdev = 0;
619
	myid = 0;
620
	myid = 0;
620
	state = 0;
621
	state = 0;
621
	authfd = false;
622
	authfd = false;
622
 
623
 
623
	faf.setName (af);
624
	faf.setFileName (af);
624
 
625
 
625
	if (!faf.open (IO_Raw | IO_ReadWrite))
626
	if (!faf.open (QIODevice::ReadWrite))
626
	{
627
	{
627
	   KMessageBox::error (0, i18n("Error opening device %1.").arg(af),
628
	   KMessageBox::error (0, i18n("Error opening device %1.").arg(af),
628
	   	faf.errorString());
629
	   	faf.errorString());
629
	}
630
	}
630
 
631
 
631
	authfile = af;
632
	authfile = af;
632
	authfd = true;
633
	authfd = true;
-
 
634
	tmpOut.setFileName(QString(tmpnam(NULL)));
633
}
635
}
634
 
636
 
635
gant::~gant ()
637
gant::~gant ()
636
{
638
{
637
	if (authfd)
639
	if (authfd)
638
	   faf.close();
640
	   faf.close();
-
 
641
 
-
 
642
	// if the file is still open, this function closes it before
-
 
643
	// it is removed.
-
 
644
	tmpOut.remove();
639
}
645
}
640
 
646
 
641
uint gant::randno ()
647
uint gant::randno ()
642
{
648
{
643
	return (uint)random ();
649
	return (uint)random ();
Line 683... Line 689...
683
		 else
689
		 else
684
		 {
690
		 {
685
		    // garmin moved to phase 1 in response to sendack1
691
		    // garmin moved to phase 1 in response to sendack1
686
		    int nr;
692
		    int nr;
687
//		    printf("reading auth data from %s\n", authfile);
693
//		    printf("reading auth data from %s\n", authfile);
688
		    authfd = open(authfile, O_RDONLY);
694
		    authfd = open(authfile.toAscii(), O_RDONLY);
689
 
695
 
690
		    if (!authfd || faf.size () < 32)
696
		    if (!authfd || faf.size () < 32)
691
		    {
697
		    {
692
		       KMessageBox::error(0, i18n("There is no authfile available or it has wrong size!"));
698
		       KMessageBox::error(0, i18n("There is no authfile available or it has wrong size!"));
693
		       return false;
699
		       return false;
Line 860... Line 866...
860
		 // in this state we're receiving the workout data
866
		 // in this state we're receiving the workout data
861
		 if (!once)
867
		 if (!once)
862
		 {
868
		 {
863
		    fprintf(stderr, "receiving\n");
869
		    fprintf(stderr, "receiving\n");
864
		    once = 1;
870
		    once = 1;
-
 
871
		    tmpOut.open (QIODevice::ReadWrite | QIODevice::Truncate);
865
		 }
872
		 }
866
 
873
 
-
 
874
		 // FIXME: Here we should fill our buffer, instead of writing
-
 
875
		 // everything into a file.
867
		 nw = write(faf.handle(), cbuf, 8);
876
		 nw = write(tmpOut.handle(), cbuf, 8);
868
 
877
 
869
		 if (nw != 8)
878
		 if (nw != 8)
870
		 {
879
		 {
871
		    KMessageBox::error(0, i18n ("Write to auth file %1 failed!").arg(authfile));
880
		    KMessageBox::error(0, i18n ("Failed to write to a temporary file!"));
872
		    return false;
881
		    return false;
873
		 }
882
		 }
874
 
883
 
875
		 if (last)
884
		 if (last)
876
		 {
885
		 {
877
		    state++;    // just to exit
886
		    state++;    // just to exit
878
				//state = 20; // to delete logs
887
				//state = 20; // to delete logs
-
 
888
		    tmpOut.close();
879
		 }
889
		 }
880
	      }
890
	      }
881
	      else if (state == 10)
891
	      else if (state == 10)
882
	      {
892
	      {
883
		 // receiving auth data
893
		 // receiving auth data
Line 972... Line 982...
972
		    ANT_AssignChannelEventFunction((uchar)chan, (CHANNEL_EVENT_FUNC)&gant::chevent, (uchar *)&cbuf);
982
		    ANT_AssignChannelEventFunction((uchar)chan, (CHANNEL_EVENT_FUNC)&gant::chevent, (uchar *)&cbuf);
973
		 break;
983
		 break;
974
 
984
 
975
		 case MESG_OPEN_CHANNEL_ID:
985
		 case MESG_OPEN_CHANNEL_ID:
976
		    state++;
986
		    state++;
977
		    fprintf(stderr, "channel open, waiting for broadcast\n", state);
987
		    fprintf(stderr, "channel open (%d), waiting for broadcast\n", state);
978
		 break;
988
		 break;
979
 
989
 
980
		 default:
990
		 default:
981
		    // ignore
991
		    // ignore
982
		    // printf("Message %02x NO_ERROR\n", ebuf[1]);
992
		    // printf("Message %02x NO_ERROR\n", ebuf[1]);
Line 1106... Line 1116...
1106
//	for(;;)
1116
//	for(;;)
1107
//		sleep(10);
1117
//		sleep(10);
1108
 
1118
 
1109
	return commfn ();
1119
	return commfn ();
1110
}
1120
}
-
 
1121
 
-
 
1122
uchar *gant::getBuffer (int *size)
-
 
1123
{
-
 
1124
QFileInfo fi;
-
 
1125
uchar *buffer;
-
 
1126
long sz;
-
 
1127
 
-
 
1128
	fi.setFile (tmpOut);
-
 
1129
	
-
 
1130
	if (!fi.exists() || fi.size() > 1000000)
-
 
1131
	   return 0;
-
 
1132
 
-
 
1133
	sz = fi.size();
-
 
1134
	*size = 0;
-
 
1135
 
-
 
1136
	if (!(buffer = new uchar[sz+1]))
-
 
1137
	{
-
 
1138
	   KMessageBox::error(0, i18n("Error allocating %1 bytes of memory!").arg(sz+1));
-
 
1139
	   return 0;
-
 
1140
	}
-
 
1141
 
-
 
1142
	if (tmpOut.handle() == -1)
-
 
1143
	{
-
 
1144
	   if (!tmpOut.open(QIODevice::ReadOnly))
-
 
1145
	   {
-
 
1146
	      KMessageBox::error(0, i18n("Error opening a temporary file for reading!"), tmpOut.errorString());
-
 
1147
	      delete buffer;
-
 
1148
	      return 0;
-
 
1149
	   }
-
 
1150
	}
-
 
1151
 
-
 
1152
	if (read (tmpOut.handle(), buffer, sz) < sz)
-
 
1153
	{
-
 
1154
	   KMessageBox::error(0, i18n("Error reading from a temporary file!"));
-
 
1155
	   tmpOut.close();
-
 
1156
	   delete buffer;
-
 
1157
	   return 0;
-
 
1158
	}
-
 
1159
 
-
 
1160
	*size = (int)sz;
-
 
1161
	return buffer;
-
 
1162
}