Embedded FreeBSD Cookbook A Volume in the Embedded Technology™ Series Embedded FreeBSD Cookbook by Paul Cevoli An imprint of Elsevier Science Amsterdam Boston London New York Oxford Paris San Diego San Francisco Singapore Sydney Tokyo iv Newnes is an imprint of Elsevier Science. Copyright © 2002, Elsevier Science (USA). All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Recognizing the importance of preserving what has been written, Elsevier Science prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data ISBN: 1-5899-5004-6 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. The publisher offers special discounts on bulk orders for this book. For information, please contact: Manager of Special Sales Elsevier Science 200 Wheeler Road Burlington, MA 01803 For information on all Newnes publications available, contact our World Wide Web home page at http://www.newnespress.com 10 9 8 7 6 5 4 3 2 1 Printed in the United States of America Preface .................................................................................. vii Prerequisites and Other Resources .................................................... vii 1 Getting Started .................................................................. 1 Overview ............................................................................................. 1 Embedded Systems ............................................................................ 1 Internet Appliances ............................................................................. 2 Compatibility of upgrades ........................................................................ 3 Time to market ......................................................................................... 4 Labor pool ................................................................................................ 4 Licensing.................................................................................................. 4 The DIO Server Appliance .................................................................. 4 FreeBSD .................................................................................................. 5 GNU Development Tools ......................................................................... 6 Java ......................................................................................................... 7 Server ...................................................................................................... 7 Data Acquisition ....................................................................................... 7 Boot Device.............................................................................................. 7 Summary............................................................................................. 8 2 Systems Programming ..................................................... 9 Overview ............................................................................................. 9 Process ............................................................................................... 9 The ........................................................................................................... 10 System Call.............................................................................................. 10 The ........................................................................................................... 11 System Call.............................................................................................. 11 The ........................................................................................................... 12 System Call.............................................................................................. 12 The ........................................................................................................... 12 System Call.............................................................................................. 12 An Example.............................................................................................. 12 Process IDs.............................................................................................. 13 Security .................................................................................................... 14 An Example.............................................................................................. 15 The ........................................................................................................... 16 and ........................................................................................................... 16 System Calls ............................................................................................ 16 File Descriptors ........................................................................................ 17 Permissions ............................................................................................. 17 Current Working Directory ....................................................................... 17 The ........................................................................................................... 18 and ........................................................................................................... 18 System Calls ............................................................................................ 18 The ........................................................................................................... 18 System Call.............................................................................................. 18 Priority...................................................................................................... 19 State......................................................................................................... 20 The ........................................................................................................... 20 Function ................................................................................................... 20 The ........................................................................................................... 21 System Call.............................................................................................. 21 Daemons............................................................................................. 21 Fork.......................................................................................................... 21 Create a new session .............................................................................. 22 Close file descriptors................................................................................ 22 Change the current working directory ...................................................... 22 Set the file mode creation mask............................................................... 22 Handle Child Exit Status .......................................................................... 22 The ........................................................................................................... 22 Function ................................................................................................... 22 The ........................................................................................................... 23 Function ................................................................................................... 23 Summary............................................................................................. 25 3 System Calls...................................................................... 27 Overview ............................................................................................. 27 Library Functions and System Calls.................................................... 27 Software Interrupt .................................................................................... 29 Passing Data............................................................................................ 29 An Example.............................................................................................. 30 Creating a System Call ....................................................................... 32 Load Handler ........................................................................................... 32 System Call Arguments ........................................................................... 34 The ........................................................................................................... 34 System Call.............................................................................................. 34 The ........................................................................................................... 36 Structure .................................................................................................. 36 The System Call Number ......................................................................... 36 The SYSCALL_MODULE Macro ............................................................. 38 Command Definitions............................................................................... 39 Command Table ...................................................................................... 40 The ........................................................................................................... 40 Function ................................................................................................... 40 Command Function Handlers .................................................................. 42 The ........................................................................................................... 45 Program ................................................................................................... 45 An example .............................................................................................. 47 Summary............................................................................................. 48 4 Device Driver ..................................................................... 49 Overview ............................................................................................. 49 Driver Environment ............................................................................. 49 Driver Structure ................................................................................... 51 The ........................................................................................................... 52 Structure .................................................................................................. 52 The ........................................................................................................... 53 Structure .................................................................................................. 53 The ........................................................................................................... 55 Structure .................................................................................................. 55 The ........................................................................................................... 57 Structure .................................................................................................. 57 The ........................................................................................................... 57 Structure .................................................................................................. 57 The ........................................................................................................... 57 Macro ....................................................................................................... 57 The ........................................................................................................... 58 Structure .................................................................................................. 58 The DIO24 Device Driver.................................................................... 59 The ........................................................................................................... 61 Function ................................................................................................... 61 The ........................................................................................................... 62 Function ................................................................................................... 62 The ........................................................................................................... 63 Function ................................................................................................... 63 The ........................................................................................................... 64 Function ................................................................................................... 64 The ........................................................................................................... 66 Function ................................................................................................... 66 The ........................................................................................................... 66 Function ................................................................................................... 66 The ........................................................................................................... 67 Function ................................................................................................... 67 The ........................................................................................................... 68 Function ................................................................................................... 68 The ........................................................................................................... 69 Function ................................................................................................... 69 The ........................................................................................................... 69 Function ................................................................................................... 69 Register Shadowing ................................................................................. 71 The ........................................................................................................... 73 Function ................................................................................................... 73 Summary............................................................................................. 76 5 Midlevel Interface Library................................................. 77 Overview ............................................................................................. 77 Shared Libraries .................................................................................. 77 Accessing the Device Driver ............................................................... 79 The ........................................................................................................... 80 System Call.............................................................................................. 80 The ........................................................................................................... 81 System Call.............................................................................................. 81 The ........................................................................................................... 81 System Call.............................................................................................. 81 The ........................................................................................................... 82 System Call.............................................................................................. 82 The ........................................................................................................... 82 System Call.............................................................................................. 82 PCI-DIO24 Hardware Registers.......................................................... 82 The DIO24 Application Interface Library............................................. 87 The ........................................................................................................... 89 and ........................................................................................................... 90 Functions ................................................................................................. 90 The ........................................................................................................... 92 and ........................................................................................................... 92 Functions ................................................................................................. 92 The ........................................................................................................... 94 and ........................................................................................................... 94 Functions ................................................................................................. 94 The diopolarity ......................................................................................... 95 The ........................................................................................................... 95 and ........................................................................................................... 95 functions................................................................................................... 95 The ........................................................................................................... 97 and ........................................................................................................... 97 Functions ................................................................................................. 97 The ........................................................................................................... 98 Function ................................................................................................... 98 Summary............................................................................................. 101 6 Daemons............................................................................ 103 Overview ............................................................................................. 103 Introduction to TCP/IP ......................................................................... 103 IP Addressing........................................................................................... 105 ARP.......................................................................................................... 105 RARP ....................................................................................................... 106 ICMP ........................................................................................................ 106 Ports......................................................................................................... 106 UDP ......................................................................................................... 106 TCP.......................................................................................................... 107 Socket System Calls ........................................................................... 107 Connection Initiation ................................................................................ 109 The ........................................................................................................... 111 System Call.............................................................................................. 111 The ........................................................................................................... 112 System Call.............................................................................................. 112 The ........................................................................................................... 112 System Call.............................................................................................. 112 The ........................................................................................................... 113 System Call.............................................................................................. 113 The ........................................................................................................... 113 System Call.............................................................................................. 113 The ........................................................................................................... 114 System Call.............................................................................................. 114 The ........................................................................................................... 115 System Call.............................................................................................. 115 The DIO Daemon ................................................................................ 115 The ........................................................................................................... 115 Structure .................................................................................................. 116 The ........................................................................................................... 117 Function ................................................................................................... 117 The ........................................................................................................... 119 Function ................................................................................................... 119 The ........................................................................................................... 120 Function ................................................................................................... 120 Summary............................................................................................. 122 7 Remote Management ........................................................ 123 Overview ............................................................................................. 123 Using Secure Shell (SSH)................................................................... 123 The DIOShell....................................................................................... 130 The ........................................................................................................... 132 Function ................................................................................................... 132 The ........................................................................................................... 133 Function ................................................................................................... 133 The ........................................................................................................... 134 Function ................................................................................................... 134 The ........................................................................................................... 135 Function ................................................................................................... 135 The ........................................................................................................... 136 Function ................................................................................................... 136 The ........................................................................................................... 137 Function ................................................................................................... 137 The ........................................................................................................... 138 Function ................................................................................................... 138 The ........................................................................................................... 139 Function ................................................................................................... 139 The ........................................................................................................... 139 Function ................................................................................................... 139 The ........................................................................................................... 140 Function ................................................................................................... 140 The ........................................................................................................... 141 Utility ........................................................................................................ 141 Summary............................................................................................. 142 8 JNI Layer............................................................................ 143 Overview ............................................................................................. 143 The JDK .............................................................................................. 143 Creating the JNI Layer ........................................................................ 145 The DIOLineState Class .......................................................................... 147 The DIOLineNumber Class...................................................................... 148 The DIOLineDirection Class .................................................................... 149 The DIOIfJNI Class .................................................................................. 150 The ........................................................................................................... 154 Function ................................................................................................... 154 The ........................................................................................................... 154 Function ................................................................................................... 154 The ........................................................................................................... 155 Function ................................................................................................... 155 The Makefile ............................................................................................ 155 Summary............................................................................................. 156 9 Web Access Using Tomcat .............................................. 157 Overview ............................................................................................. 157 Tomcat ................................................................................................ 157 bin ............................................................................................................ 158 JSP Overview...................................................................................... 160 Comments................................................................................................ 161 Directives ................................................................................................. 161 Declarations ............................................................................................. 162 Expressions ............................................................................................. 162 Code Scriptlets......................................................................................... 162 The DIO JSP Page.............................................................................. 162 Summary............................................................................................. 165 10 Building the Kernel ......................................................... 167 Overview ............................................................................................. 167 The ........................................................................................................... 168 Keyword ................................................................................................... 168 The ........................................................................................................... 168 Keyword ................................................................................................... 168 The ........................................................................................................... 168 Keyword ................................................................................................... 168 The ........................................................................................................... 168
Description: