quick_recipes (779892), страница 24
Текст из файла (страница 24)
Retrieving theaddress will work fine as long as it is indeed the email address thatwas stored as attendee data when it was created.What may go wrong when you do this: If you have gone throughthis section in order, the last calendar entry you modified is from the\PIM\AgendaRepeat code sample and it doesn’t have any attendees.Just run the \PIM\PopulateAgenda code sample again to find someattendees.4.3 NetworkingSymbian OS has comprehensive support for IP networking protocols fordeveloping applications that can run over a variety of IP-capable bearers,whether it is Wireless LAN or GPRS.The networking stack is implemented using client–server architecture, in the ESOCK component, just like many other framework serviceproviders in Symbian OS. The client side of ESOCK exposes a generic,protocol-independent API; the server side provides a plug-in-based interface to load and manage various protocols, each of which can be run ina separate thread and are accessible through the client APIs. Examples ofprotocols loaded by ESOCK include Bluetooth, IrDA and IP.The IP thread hosts the TCP/IP stack, which implements the popularTCP (RFC 793) and UDP (RFC 768) protocols running over IP (RFC791).
The TCP/IP stack in turn communicates with low-level bearersrepresenting link layer technologies such as WLAN and GPRS throughnetwork interfaces.CommsDat, which is a separate client library, is used to store andretrieve various settings relating to the networking subsystem and is usedby both the IP-subsystem and client applications for settings retrieval andstorage. See Figure 4.3.1.In order to keep them as simple as possible, the code samples inthese recipes use as few active objects as possible.
Most asynchronousmethod calls are simply followed by User::WaitForRequest(). ThisNETWORKING137ESOCKControlDataIP threadCommsDATConfiguration,Connection,Disconnectionetc.TCPUDPDNSICMPIPNETWORK INTERFACE (NIF)GPRS/WCDMAGGSNWIFIWIFI accesspointCircuitswitched callISPBluetoothBluetoothaccess pointFigure 4.3.1 Symbian OS Networking Subsystem and Bearer Technologiesis not how production-quality code should behave, and the code samplesprovided for download to accompany this book will illustrate how tocall asynchronous APIs correctly using active objects. You can also readmore about asynchronous calls and event handling on Symbian OS inChapter 3.4.3.1 Getting StartedThe three most important APIs for developing IP-enabled applicationsare RConnection, RSocket and RHostResolver – all of which aredeclared in es_sock.h.
The RConnection API is used for connectionselection/startup and management. The RSocket and RHostResolverAPIs implement socket and host resolver functionality respectively, similarto the BSD-style socket and host resolver APIs found on other operatingsystems.IP-enabled applications require NetworkServices capability to bepresent, since it is required for initiating a network connection andsending and receiving data over protocols such as TCP and UDP.On a mobile device, where multiple bearers are available and can beactive at the same time – for example, GPRS and WLAN – it is importantto have a mechanism that allows applications to choose over whichbearer IP data is sent and received; not only because of potential costissues involved (WLAN might be cheaper than using GPRS), but also forpotential routing issues.
The RConnection API facilitates an explicitway to start a particular network connection on a bearer to remove138SYMBIAN C++ RECIPESall ambiguity.1 Once the network connection has been initiated, thesocket and host resolver API can use that particular connection to sendand receive data. Besides selection and tearing down of a networkconnection, RConnection can be used to gather information about theactive network connection such as connection status, how much datawas sent and received at link layer, and so on.An Internet Access Point (IAP) is a collection of settings that enablesselection of a particular network connection on a particular bearer, whichis specified as a selection parameter when starting a connection using theRConnection API.The CommsDat API enables retrieval of configuration informationfrom the settings database, known as the CommsDat database.
Examplesof configuration information stored in CommsDat include the list ofavailable IAPs, default connection preferences and settings specific toan IAP, which could be the phone number in case of a dial-up ISP,the access point name (APN) in case of a GPRS network or the SSIDof a WLAN access point in case the IAP is configured to use WLAN asa bearer.In most cases, barring sensitive data like passwords, reading information from the CommsDat does not require any platform securitycapability.The common pattern for any IP-enabled application connecting to anIP network and using its services would normally be:Configure – select the correct IAP; programmatically, by asking the useror letting the system choose one.Connect – start connection using selected connection parameters.Use – send and receive data using sockets and finally.Disconnect – close socket connection and indicate that the applicationhas finished using the connection by closing it.We will use the above pattern throughout for example code.4.3.2 Setting Up the EnvironmentRunning your IP-enabled application under the emulator usually requiressome extra steps beyond installing the SDK.
In this subsection we willlook at some of the most common methods for enabling IP networkingconnectivity on the emulator, listed in Table 4.3.1.1How Symbian OS facilitates establishing simultaneous, independent, network connections to multiple bearers (or even the same bearer using different networks), known asmultihoming, is discussed in depth in chapter 6 of Symbian OS Communication Programming, 2nd Edition, by Iain Campbell.NETWORKING139Table 4.3.1 Common Methods for Enabling IP Network Connectivity4.3.2.1TechnologyRequirementWinPCapA wired Ethernet card on your PC connectedto a networkWinTAPAny IP connection from your PC and enablingInternet Connection Sharing (ICS) on your PCWinSockAny IP connection from your PCUsing the SDK Configuration ToolsIf you are using the S60 emulator, it already comes configured with theWinSock method, which should just work out of the box, since it is goingto simply use the Windows networking stack for connectivity.In the case of UIQ, the SDK installer will ask during installationto install WinPCap, a free packet capture library for Windows.
If youaccept, the WinPCap method will be available for the UIQ installer.After installation, you will need to finalize the WinPCap setup by goingto (from the Windows Start menu) UIQ 3 SDK -> SDKConfig ->Communications tab and click ‘Apply Ethernet’. Note that the UIQ 3.0SDK can be picky about the version of Perl installed and can prevent theSDKConfig application from running.
It is recommended you install theone that comes with the SDK installer (version 5.6.1.635).4.3.2.2Using WinPCapAs we saw earlier, the SDKConfig application can be used to configureWinPCap-based IP connectivity for the UIQ SDK. What the SDKConfigapplication really does is set up a virtual Ethernet driver for the emulator,which shares your PC’s Ethernet interface. The configuration option forthe virtual Ethernet driver is stored in the epoc.ini file (stored insdk_installation_path\epoc32\data), and looks like this:ETHER_NIF=\Device\NPF_{80FA5794-DAC8-4BAA-94C9-A0FAB2DE04D8}ETHER_MAC=020702b68886ETHER_SPEED=100MbpsIn case the SDKConfig application fails to update the epoc.inifile, or you would like to use Ethernet connectivity in the S60 SDK, youwill need to edit epoc.ini to add the lines above, with the appropriateparameters.ETHER_NIF specifies which Ethernet network card to use on your PC.The best way to get this is to install a free protocol analysis tool likeWireShark or Ethereal and use the ‘Capture Options’ dialog’s ‘Interface’140SYMBIAN C++ RECIPESdropdown list to select the appropriate adapter and copy the part startingwith ‘\Device.
. .’ and specify that in the ETHER_NIF option.ETHER_MAC specifies the MAC address of your virtual adapter. Thisis the same as the MAC address of the selected adapter in ETHER_NIF,with the top byte changed from 00 to 02 (that is, if the original MAC was000702b68886, it becomes 020702b68886), which means locallyadministered MAC according to IEEE. Certain networks, for examplecorporate LANs, sometimes use MAC address filtering to only allowpackets from registered LAN adapters. Make sure that this virtual MAC isregistered with the network (ask your network admin) if that is the case.Once you have finished making the changes to the epoc.ini file, youare ready to generate the CommsDat database that will use the settings.However, before you generate the new database, it is better that youback up the existing one, so that you can go back if the new settingsdon’t work.
Backing up of the existing database is achieved through running a tool called ceddump.exe from the command line, which wheninvoked creates a file called cedout.cfg in the emulator’s C: drive (i.e.,sdk_path\epoc32\winscw\c\ folder). To apply the Ethernet settings,you will need a CommsDat database that is already configured withappropriate Ethernet settings; fortunately, one already ships with the SDK,ethernetced.xml, again found in the emulator’s C: drive.
Now runced.exe from the command line, specifying the emulator path of ethernetced.xml as an argument (e.g., ced c:\ethernetced.xml).Once ced has finished running, check the emulator’s C: drive forced.log. If there was any problem generating the new database, youshould be able to spot it here; look for words like failure, failed and error.Once the database is generated, there should be multiple IAPs set up foruse.
For the vast majority, the one called ‘Ethernet with Daemon DynamicIP’ should work out of the box, since it is configured to use DHCP toallocate IP address and name server for the virtual Ethernet adapter. If youwould like to use a statically allocated IP address for some reason andknow other configuration information like name server address(s), youcan use the IAP ‘Ethernet No Daemon Static IP’, however you will haveto change the relevant fields in the service table in ethernetced.xmland regenerate CommsDat, using the above procedure.Note that on UIQ 3.0 SDK installation, the ethernetced.xml file ismissing. You can get a copy from the S60 SDK 3rd Edition SDK.Tip: ced.exe supports an additional commandline option -v thatreports all errors encountered while generating the CommsDat database from a configuration file at the end of the ced.log file, whichis quite helpful.
However, using the -v option will require using the-i option to specify the input configuration file (e.g., ced -v -ic:\ethernetced.xml).NETWORKING4.3.2.3141Using WinTAPWinTAP is a relatively new connectivity method available to SymbianOS emulators. Instructions on how to install and use it can be foundat: www.developer.symbian.com/main/tools/devtools. The main advantage of WinTAP is that it doesn’t require the elaborate setup involvingWinPCap and creates a virtual Ethernet adapter that is visible on aWindows PC too. The slight downside of this method is that it requiresenabling of Internet Connection Sharing on your PC, since the virtualadapter accesses the IP network through the physical adapter on whichICS was enabled; for example, the PC LAN card.