April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Palm OS Communications Primer: IR Library, Page 3

  • August 31, 2004
  • By Alex Gusev, Alex Gusev
  • Send Email »
  • More Articles »

Executing IAS queries

As we have noted earlier, you can use IAS queries to get information about devices within range. It's necessary to make a LAP connection before query execution. IAS is some kind of database where devices may publish info about themselves and their services. It accumulates records based on device class, protocol, parameter type, and so forth. The IR Library contains about a dozen IAS functions. The core one is IrIAS_Query. It enables you to retrieve requested information according to device class and parameter name. You will find some basical implementation of such queries in the article's sample project. If you need more details about IAS, you may find them at the IrDA site. This short article is just not a good place to discuss it.

Making a connection

After your application has gotten a remote LSAP value, it's possible to start a connection request. It is carried out by several steps. First, according to IrDA specifications, you should establish a LAP connection and then work over either a LMP or Tiny TP session. This last phase requires to know remote LSAP; thus, you should execute the IAS query after a successfull LAP connection. The following sample will help you to get the first glance on the whole process:

static void DoPrint()
{
   _pendingCommand = ConnectLapCommand;

   // _deviceInfo must be set by discovery command
   if (IrConnectIrLap(_IrLibraryRefNum, _deviceInfo.hDevice) !=
                      IR_STATUS_PENDING)
   {
      Log("Fail to establish LAP connection\n");
   return;
   }

   if ( !waitForResult() )
   {
      Log("Timeout is over for IrConnectIrLap\n");
      return;
   }

   if ( !connectIrLPT() )
   {
      Log("Fail to establish connection to printer\n");
      return;
   }

   // Do data transmission here
   ....

   if (IrDisconnectIrLap(_IrLibraryRefNum) != IR_STATUS_PENDING)
   {
      Log("Fail to disconnect via IrDisconnectIrLap\n");
      return;
   }
}

static bool connectIrLPT()
{
   Byte remoteLsap;
   IasQuery iasQuery(_IrLibraryRefNum);

   // Try connect to printer. LPT port uses LMP protocol and has
   // fixed IrLPT class
   if (iasQuery.getRemoteLsap(IrLptClass, IrLmpProtocol, remoteLsap))
   {
      StrPrintF(_szInfo,"remote LSAP is %u\n",remoteLsap);
      Log(_szInfo);

      if ( !connectLmp(remoteLsap) )
      {
         Log("Failed to connectLmp\n");
         return false;
      }
      return true;
   }

   Log("Fail to obtain remote LSAP\n");
   return false;
}

static bool connectLmp(Byte remoteLsap)
{
   _pendingCommand = ConnectLmCommand;

   IrSetConTypeLMP(&_IrConnection);

   _IrConnection.rLsap = remoteLsap;
   _packet.buff = NULL;
   _packet.len  = 0;

   if (IrConnectReq(_IrLibraryRefNum, &_IrConnection, &_packet, 0)
                    != IR_STATUS_PENDING)
   {
      Log("Fail to establish LMP connection\n");
      return false;
   }
   return waitForResult();
}

In fact, there is nothing too complicated here, except possibly the error handling, disconnection events, and so forth.

Transmitting the data

Because the connection is established, you can transmit data. That's quite a trivial task. The following code snippet shows all the story:

...
   char szTestData[] =
      "*******************\r\n"
      "IrLib Sample Packet\r\n"
      "*******************\r\n\r\n";

   _packet.buff = (Byte*)szTestData;
   _packet.len = StrLen(szTestData);

   _pendingCommand = SendDataCommand;
   if (IrDataReq(_IrLibraryRefNum, &_IrConnection, &_packet) !=
                 IR_STATUS_PENDING)
   {
      Log("Fail to send data\n");
      return;
   }

   if ( !waitForResult() )
   {
      Log("Timeout is over for sending data\n");
      return;
   }
...

Shutting down

All in all, you should unbind an existing connection from the IR protocol stack and close the IR Library:

static void AppStop(void)
{
   IrUnbind(_IrLibraryRefNum, &_IrConnection);
   IrClose(_IrLibraryRefNum);

   /* Close all the open forms. */
   FrmCloseAllForms();

}

Summary

As you might see, IR communications under Palm OS are a doable thing. A bit complicated from the very first touch, it's pretty usable later. I hope this article will help you to solve most of the common programming IR tasks.

Download

Download the accompanying code's zip file here.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. Now, he works at an international retail software company as a team leader of the Mobile R department, making programmers' lives in the mobile jungles a little bit simpler.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel