21
@THEORIS 2002-2008 Windows CE - Stream Driver 1 Cours Windows CE - Pilotes « Stream Driver » - © THEORIS 2002-2008 www.theoris.fr @THEORIS 2002-2008 Windows CE - Stream Driver 2 La Société Raison sociale Société de services Expertise Les systèmes temps réel, embarqués, et répartis Offre Aide au choix d’architectures Logiciels « clef en main » Formations et support personnalisés Coordonnées www.theoris.fr – [email protected]

Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

  • Upload
    others

  • View
    6

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 1

Cours Windows CE- Pilotes « Stream Driver » -

© THEORIS 2002-2008 www.theoris.fr

@THEORIS 2002-2008 Windows CE - Stream Driver 2

La Société� Raison sociale

� Société de services

� Expertise� Les systèmes temps réel, embarqués, et répartis

� Offre� Aide au choix d’architectures� Logiciels « clef en main »� Formations et support personnalisés

� Coordonnées� www.theoris.fr – [email protected]

Page 2: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 3

Le Formateur

� Fonction� Chef de Projet / Architecte Logiciel

� Domaines d’expertise� Conception et réalisation d’automatismes industriel s� Élaboration de systèmes temps réel� Formation au développement d’applications sous Wind ows

CE et à la réalisation de BSP sous Platform Builder� Formateur officiel Microsoft « Windows Embedded 6.0 »

� Coordonnées� [email protected]

YannickBALERE

@THEORIS 2002-2008 Windows CE - Stream Driver 4

Séances1- Introduction2- Système Windows CE3- Multitâche – Synchronisation4- Gestion mémoire5- Pilote « stream driver »6- SDK, développement applicatif7,8,9- C#, Compact Framework 2.010- Projets industriels

Page 3: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 5

����Séance 5� Cours

� Librairie dynamique� Modèle Stream� Interruption� Gestion de contextes driver

� Travaux dirigés� Exercices de chargement tardif de DLL

� Travaux pratiques� Ecrire un driver simplifié

@THEORIS 2002-2008 Windows CE - Stream Driver 6

Librairie dynamique

Page 4: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 7

Principe� Définitions

� Module exécutable non autonome (chargé et exécuté dans l’espace d’adressage d’un Process)

� Bibliothèque de fonctions exploitables via leur nomet prototype (API)

� Win32� Présente « naturellement »

sous forme de DLLs� Sous CE, la majeure partie se

trouve dans "CoreDll.dll"

ProcessProcess

@THEORIS 2002-2008 Windows CE - Stream Driver 8

Portable Executable� Format des fichiers :

� Exécutables (".EXE", ".DLL", ".OCX", …)� De compilation (".OBJ", ".LIB", ...)

� Successeur du COFF� Common Object File Format� Héritage VAX/VMS

� Multi cibles� Format natif des plates-formes Win32� De DOS à XP en passant par CE

Page 5: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 9

Édition de liens� Entrées :

• Les modules traduits (i.e. générés) séparément par le compilateur

� Actions :• Construction d’une table des modules• Affectation d’une adresse à chaque module• Gestions des translations d’adresse• Résolution les références externes

� Sortie :• Un module chargeable par le système (i.e.

exécutable)

@THEORIS 2002-2008 Windows CE - Stream Driver 10

Édition de liens –

Sections

LinkLink

� Construction d’un espace d’adressage unique

� Fusion des sections

Page 6: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 11

Édition de liens –

Statique

� Applications monolithiques

� Entièrement résolu àl’édition de liens

LinkLink

ProcedureProcedureImplementationImplementation

ProcedureProcedureCallCall

ExecutionExecution

CallCallImplementationImplementation

FooFoo(( ……););

@THEORIS 2002-2008 Windows CE - Stream Driver 12

Édition de liens –

Dynamique – Précoce

LinkLink

LinkLink

ProcedureProcedureImplementationImplementation

ProcedureProcedureExecutionExecution

ProcedureProcedureDefinitionDefinition

CallCallImplementationImplementation

CallCallExecutionExecution

FooFoo(( ……););

� Applications modulaires

� Partiellement résolu àl’édition de liens

Page 7: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 13

Édition de liens –

Dynamique – Tardif

� Applications modulaires

� Totalement résolu àl’exécution

LinkLink

CallCallImplementationImplementation

ProcedureProcedureExecutionExecution

CallCallExecutionExecution

typedeftypedef (( __stdcall__stdcall ** FooProcFooProc )()( ……););HINSTANCE HINSTANCE hLibhLib = = LoadLibraryLoadLibrary (( "" Foo.dllFoo.dll ");");FooProcFooProc MyFooMyFoo = (= ( FooProcFooProc ))

GetProcAdressGetProcAdress (( hLibhLib ,,"" FooFoo"" ););

MyFooMyFoo(( ……););FreeLibrary(hLibFreeLibrary(hLib ); );

@THEORIS 2002-2008 Windows CE - Stream Driver 14

Exportation� Désignation explicite

� Dans le code• __declspec(dllexport) Foo(…);

__declspec(dllexport) int MyVal;

� Dans le fichier ".def"• LIBRARY FOO

EXPORTSFoo @1OtherFoo @2

Page 8: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 15

Points d’entrée� Personnalisés

� Autant que de fonctions exportées

� Standards� Un seul accès

• BOOL WINAPI DllMain(HANDLE hinstDLL,DWORD dwReason,LPVOID lpvReserved);

� Plusieurs raisons• DLL_PROCESS_ATTACH/ DLL_PROCESS_DETACH

• DLL_THREAD_ATTACH/ DLL_THREAD_DETACH

@THEORIS 2002-2008 Windows CE - Stream Driver 16

Points d’entrée –

DllMain� Gestion des ressources

� Allocation/libération mémoire� Mise en œuvre des Thread Local Storage

� Restriction� Explicite

• BOOL DisableThreadLibraryCalls(HMODULE hLibModule);

� Implicite• TerminateThread , TerminateProcess

Page 9: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 17

Chargement� Allocation et mapping

� HINSTANCE LoadLibrary(LPCTSTR lpLibFileName);

� HMODULE GetModuleHandle(LPCTSTR lpModuleName);

� Récupération d’un point d’entrée� FARPROC GetProcAddress( HMODULE hModule,

LPCWSTR lpProcName);

� Libération� BOOL FreeLibrary(HMODULE hLibModule);

@THEORIS 2002-2008 Windows CE - Stream Driver 18

Modèle « Stream Driver »

Page 10: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 19

Classification des drivers� Native vs Stream

• Who loads the driver

� Monolithic vs Layered (MDD/PDD)• How the driver is architected

� Built In vs Dynamic/Installable• When the driver is loaded

� Driver Family• WAV/Miniport/Touch/Display/Serial etc

@THEORIS 2002-2008 19

@THEORIS 2002-2008 Windows CE - Stream Driver 20

« Stream » drivers� Normalized driver

� Prefix identification («COM1:», …)� Standard entry points� Identification with Registry keys

� Normalized Win32 API� No driver dependencies� Access to device through a kernel object

(handle)� File API: CreateFile , …

@THEORIS 2002-2008 20

Page 11: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 21

Hardware

OAL

Filesys.exe

Stream Driver Architecture

Application

File API

StreamStreamDriverDriver

Device.exeKernel

Interrupts

ASYNCHRONOUSASYNCHRONOUS

SYNCHRONOUS

SYNCHRONOUS

@THEORIS 2002-2008 21

@THEORIS 2002-2008 Windows CE - Stream Driver 22

Win32 API ���� Stream� Application

� ActivateDeviceEx

� DeactivateDevice� CreateFile

� CloseHandle

� SetFilePointer

� ReadFile

� WriteFile

� DeviceIoControl

� PowerOffSystem

� Driver� XXX_Init

� XXX_PreDeinit

� XXX_Deinit� XXX_Open

� XXX_PreClose

� XXX_Close

� XXX_Seek

� XXX_Read

� XXX_Write

� XXX_IOControl

� XXX_PowerUp� XXX_PowerDown

@THEORIS 2002-2008 22

Page 12: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 23

Rôles des fonctions� XXX_Init Prise en charge d’un périphérique� XXX_PreDeinit� XXX_Deinit

� XXX_Open Prise en charge d’un utilisateur� XXX_PreClose� XXX_Close

� XXX_Seek Utilisation (interface R/W)� XXX_Read� XXX_Write

� XXX_IOControl Utilisation (Générique)

� XXX_PowerUp Gestion d’alimentation� XXX_PowerDown

@THEORIS 2002-2008 Windows CE - Stream Driver 24

XXX_Init� Device manager calls this function at startup (legac y

mechanism) or as a result of a call to ActivateDeviceEx

� The first parameter is the registry path to the dev ice key instance ( HKEY_LOCAL_MACHINE\Drivers\Active)which gives access to the device specific parameter s

� The following list shows the tasks this function ca n perform: � Initialize the installed device to a default state. � Allocate global resources for the device driver. � Register an interrupt callback function for an interrupt-driven

device.

@THEORIS 2002-2008 24

Page 13: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 25

XXX_PreDeinit, XXX_Deinit� Device manager calls these functions as a result of a

call to DeactivateDeviceEx

� The XXX_PreDeinit function:� notifies the driver to mark the device instance as invalid and

wake up sleeping threads.� prevents from race conditions which can occur betwe en

threads : one call to XXX_Deinit may release resourc es during a call to XXX_Open, XXX_Read or XXX_Write.

� The XXX_Deinit function:� Un-initializes a device by releasing all the resource s (threads,

events, memory, interrupt handler, …)

@THEORIS 2002-2008 25

@THEORIS 2002-2008 Windows CE - Stream Driver 26

XXX_Open, XXX_PreClose, XXX_Close

� The XXX_Open function:� opens a device for reading, writing, or both. An app lication

indirectly invokes this function when it calls the Create Filefunction to open special device file named (« COM1: »).

� The XXX_PreClose function:� notifies the driver to mark the closing handle as inv alid and to

wake-up any sleeping threads.� prevents from race conditions which can occur betwe en

threads : one call to XXX_Close may release resource s during a call to XXX_Read or XXX_Write.

� The XXX_Close function:� releases the opened context

@THEORIS 2002-2008 26

Page 14: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 27

XXX_Seek, XXX_Read, XXX_Write

� The XXX_Seek function:� moves the data pointer inside the device.� Called as a result of a call to SetFilePointer on a valid device

handle.

� The XXX_Read function:� reads data to a buffer from a device identified by a valid

device handle� returns the number of bytes actually obtained.

� The XXX_Write function:� writes data from a buffer to a device identifies by a valid

device handle� returns the number of bytes actually written.

@THEORIS 2002-2008 27

@THEORIS 2002-2008 Windows CE - Stream Driver 28

XXX_IOControl� This functions sends a specific command to a device ide ntified

by a valid device handle.

� The dwCode parameter contains the input or output operation to perform; these codes are usually specific to each devic e driver and are exposed to application programmers through a h eader file (.h) prepared by the developer of the device driv er.

� An automatic call to an IOControl can be perform during system initialization by setting the registry key: HKLM\Drivers\BuiltIn\MyDevice\Ioctl

� The correct format for an IOCTL is (see CTL_CODE):

@THEORIS 2002-2008 28

Page 15: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 29

Stream driver registration

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\DeviceXXX]"Dll" = "driver.Dll" // driver dll file"Prefix"= "XXX" // driver prefix XXX1"index" = dword:1 // instance number XXX1"Order" = dword:2 // dependencies

// optional keys"friendly_name«"ioctl"…

// private keys…

@THEORIS 2002-2008 29

@THEORIS 2002-2008 Windows CE - Stream Driver 30

Stream driver load/unload� At initialization of Device Manager

� Launched by the kernel ("Launch20"="Device.exe")

� Read registry and launch all drivers found under registry key: [HKLM\Drivers\BuiltIn]

� At runtime by specific API� ActivateDeviceEx� DeactivateDeviceEx

@THEORIS 2002-2008 30

Page 16: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 31

DEVICE.exeDEVICE.exe

Stream driver load details

MyDrv.dll

DllMain()LoadLibrary( MyDrv.dll )GetProcAddress ( x 9 )

driver initialization1st device instance

driver initialization2nd device instance

DRV_Init()

DRV_Deinit()

DRV_Open()

HKLM/DEVICES/…/Dev1"DLL" = "MyDrv.dll"

"Prefix" = "DRV""Index" = dword:1"order" = dword:2

HKLM/DEVICES/…/Dev2"DLL" = "MyDrv.dll""Prefix" = "DRV""Index" = dword:2"order" = dword:2

….

BOOT

@THEORIS 2002-2008 31

@THEORIS 2002-2008 Windows CE - Stream Driver 32

H1H1=CreateFile("DRV1:"DRV1:"" …)Sz=ReadFile(H1,…)

H2H2=CreateFile("DRV1:"DRV1:" " …)Sz=ReadFile(H2H2,…)

MyApp.EXE

DEVICE.exeDEVICE.exe

Stream driver usage details

MyDrv.dll

Read stream2

Open stream1 DRV_Open()

Open stream2

DRV_Read()

DRV_Close()

@THEORIS 2002-2008 32

Page 17: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 33

Interruption

@THEORIS 2002-2008 Windows CE - Stream Driver 34

Hardware

Interrupts management

Kernel + OAL

Interrupt ServiceThread - IST

Interrupt ServiceRoutine - ISR

Code OAL

DEVICE.exeDEVICE.exe

NK.exeNK.exe

DRV_Init()

4

SetEvent

5

InterruptDone

1

Hardware interrupt

2

OAL Handler

3

SysIntr

@THEORIS 2002-2008 34

Page 18: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 35

ISR vs IST� Interrupt Service Routine

� When receiving an interrupt request, exception handler call the ISR

� All lower interrupts are disabled� ISR returns an interrupt logical identifier to the ker nel

� Interrupt Service Thread� IST is a standard system thread executing the interru pt

associated actions� Usally this thread has a high priority� When receiving the logical identifier from the ISR, the kernel

sets an event to signal the IST� System calls are available� Execution takes place in the driver context� Only current interrupt is disabled

@THEORIS 2002-2008 35

@THEORIS 2002-2008 Windows CE - Stream Driver 36

IST: initialization� Creation of the associated system event

(CreateEvent)� Creation of the thread in a suspended state

(CreateThread)� Set the thread priority (CeSetThreadPriority)� Get the logical identifier� Register association between event and

identifier to the kernel (InterruptInitialize)� Launch the thread (ResumeThread)

@THEORIS 2002-2008 36

Page 19: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 37

IST: un-initialization

� Unregister association between eventand identifier (InterruptDisable)

� Stop the thread (WaitForSingleObject)� Release resources: thread, event

(CloseHandle)

@THEORIS 2002-2008 37

@THEORIS 2002-2008 Windows CE - Stream Driver 38

IST: thread routine

� Infinite loop waiting for the event(WaitForSingleObject)

� At signal, execution of the interruptcode

� When finished, re-enable the interrrupt(InterruptDone)

@THEORIS 2002-2008 38

Page 20: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 39

Gestion de contexte

@THEORIS 2002-2008 Windows CE - Stream Driver 40

Contextes management� Stream driver model involves three context levels

� Driver context: manage allocation of other contexts� Device context: manage device resources� Open context: manage a current use of a device

� DLL driver loading� Initialization of driver context� Allocation of device contexts

� Driver initialization� Registration of a device� Allocation of device resources� Allocation of open contexts

� Device open� On CreateFile, Device.exe call XXX_Open� Registration of use context: access mode, share mod e� Check other current uses

� Optimization� Open and device contexts are managed at driver leve l as a pool of contexts� Link between a device context and an open context i s set dynamically at XXX_Open call

@THEORIS 2002-2008 40

Page 21: Cours Windows CEstreetpc.free.fr/pub/WinCE/Cours 05.pdfdevice handle returns the number of bytes actually obtained. The XXX_Write function: writes data from a buffer to a device identifies

© Theoris 2003-2006

@THEORIS 2002-2008 Windows CE - Stream Driver 41

HKLM/DEVICES/…/Dev1"DLL"="MyDrv.dll""Index"=dword:1"Prefix"="DRV"…

HKLM/DEVICES/…/Dev2DLL = « MyDrv.dll »….

DEVICE.exeDEVICE.exeBOOT

Contextes management

H1H1=CreateFile( "" DRV1:DRV1:"" …)Sz=ReadFile(H1,…)

H2H2=CreateFile( "" DRV1:DRV1:"" …)Sz=ReadFile(H2H2,…)

MyApplication.EXE

MyDrv.dll

Device Context• DCtx1• DCtx2

Open Context• OCtx1• OCtx2

hOpen1hOpen1--11

hOpen1hOpen1--22

XXX_InitXXX_Init((……))

hInst1hInst1

hInst2hInst2

XXX_OpenXXX_Open((hInst1hInst1))

XXX_OpenXXX_Open((hInst1hInst1))

XXX_ReadXXX_Read((hOpen1hOpen1--22) )

@THEORIS 2002-2008 41