summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsc2000-12-04 15:28:01 +0000
committersc2000-12-04 15:28:01 +0000
commit97487c4585cbf2049c2b4a5ad86c8e15f829975d (patch)
tree896032460c6ca690868a19d7c3253e3a736cdbb5
parent4d39cd1dc34aacd1bef2fb0132d16fd85a4240d5 (diff)
downloadivy-league-97487c4585cbf2049c2b4a5ad86c8e15f829975d.zip
ivy-league-97487c4585cbf2049c2b4a5ad86c8e15f829975d.tar.gz
ivy-league-97487c4585cbf2049c2b4a5ad86c8e15f829975d.tar.bz2
ivy-league-97487c4585cbf2049c2b4a5ad86c8e15f829975d.tar.xz
Test app for Irda OBEX links
-rw-r--r--comm/testirda.cc157
1 files changed, 157 insertions, 0 deletions
diff --git a/comm/testirda.cc b/comm/testirda.cc
new file mode 100644
index 0000000..ffd13ba
--- /dev/null
+++ b/comm/testirda.cc
@@ -0,0 +1,157 @@
+#include "Scheduler.h"
+#include "TimeOut.h"
+#include "BusAccess.h"
+#include "IrdaAddress.h"
+#include "ObexStream.h"
+#include "ivl/Reaction.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <ostream.h>
+class IvlEvent;
+
+/*
+Global variables. This is because we use IvlCallbacks and functions
+instead of IvlReactions and objects. But this is only a test...
+*/
+IvlBusAccess* A;
+IvlCallback* C;
+
+/*
+This is an example of how to emit messages on the bus.
+Here, this function is called periodically so as to demonstrate
+the behaviour of the bus.
+*/
+void
+foo (Millisecond t)
+{
+ static int i;
+ static const char* cmds [] = {"free", "click", "lock"};
+ A->Emit ("tick %d", t);
+ if (i %10 == 0)
+ A->Emit ("knob %s", cmds[(i/10)%3]);
+ ++i;
+}
+
+/*
+This is an example of how to handle messages coming
+from the bus. Here, this function is associated to a callback,
+which in turn is associated to a regexp (see in main).
+*/
+void
+print_event (IvlEvent& ev)
+{
+ IvlBusEvent* be = dynamic_cast<IvlBusEvent*> (&ev);
+ if (!be)
+ return;
+
+ cout << "Event matches '" << be->Regexp << "'\n";
+ cout << "\t(" << be->NbMatches << " matches: ";
+ IvlListIterOf<IvlString> li = be->MatchList;
+ while (++li)
+ cout << "'" << **li << "' ";
+ cout << ")\n";
+}
+
+/*
+This is an example of how to handle a new agent connecting to
+the bus. Most applications are not interested in such events...
+*/
+void
+bus_agent_ready (IvlEvent& ev)
+{
+ IvlBusAgentEvent* ae = dynamic_cast<IvlBusAgentEvent*> (&ev);
+ if (!ae)
+ return;
+
+ IvlBusAgent* a = ae->GetAgent ();
+ cout << "Hello " << a->GetName () << "!\n";
+
+ C->SubscribeTo (a->Bye);
+}
+
+/*
+This is an example of how to handle an agent leaving
+the bus. Most applications are not interested in such events...
+*/
+void
+bus_agent_bye (IvlEvent& ev)
+{
+ IvlBusAgentEvent* ae = dynamic_cast<IvlBusAgentEvent*> (&ev);
+ if (!ae)
+ return;
+ IvlBusAgent* a = ae->GetAgent ();
+ cout << "BYE " << a->GetName () << "!\n";
+
+}
+
+void
+irda_agent_ready (IvlEvent& ev)
+{
+ IvlObexAgentEvent* ae = dynamic_cast<IvlObexAgentEvent*> (&ev);
+ if (!ae)
+ return;
+
+ IvlObexAgent* a = ae->GetAgent ();
+ A->Emit ("new IRDA Agent");
+
+// C->SubscribeTo (a->Bye);
+}
+
+#define MEMO_PAD_ID 0x6d656d6f /* "memo" *.txt */
+
+main ()
+{
+ /* initialize communication library */
+ IvlOpen ();
+
+ /* create bus access */
+ IvlBusAccess a ("Ivl test"); A = &a;
+
+ /* periodically send messages on the bus (see foo for timeout handling) */
+ IvlTimeOut t (1000, foo);
+
+ /* subscribe to a few event types (see print_event for event handling) */
+ /* Note that you can use any subclass of IvlBaseReaction instead
+ of IvlCallback */
+ IvlCallback c1 (print_event);
+ a.Subscribe (c1, "^(CLOCK) (.*)");
+ a.Subscribe (c1, "TRAFFIC Start");
+ a.Subscribe (c1, "(.*Tick.*)");
+ a.Subscribe (c1, "^AIRCRAFT:(.*) Moved (.*)");
+
+ /* react to new agents calling (see bus_agent_ready for event handling) */
+ IvlCallback c3 (bus_agent_ready);
+ c3.SubscribeTo (a.NewAgents);
+
+ /* reaction to agents leaving (see bus_agent_ready for subscription,
+ and bus_agent_bye for event handling) */
+ IvlCallback c4 (bus_agent_bye); C = &c4;
+
+
+
+ /*** IRDA test ***/
+
+ /* create IRDA OBEX access */
+ IvlObexStream irl (new IvlIrdaAddress);
+
+ /* find accessible devices */
+ IvlListOf<IvlIrdaAddress> la;
+ IvlIrdaAddress::DiscoverPeers (irl.GetFd (), la);
+
+ /* test connection to first available device */
+ IvlIrdaAddress* ira = la.First ();
+ if (ira) {
+ IvlObexAgent* ioa = new IvlObexAgent (ira, &irl);
+ IvlObexObject o;
+ o.SetClass (MEMO_PAD_ID);
+ o.SetName ("Essai");
+ o.SetBody ("Ceci est un mémo\nenvoyé depuis Linux vers\nle Palm\n");
+ ioa->SendObject (o);
+ cerr << "ok\n";
+ }
+
+ IvlCallback c5 (irda_agent_ready);
+ c5.SubscribeTo (irl.NewAgents);
+
+ IvlLoop ();
+}