FreeNOS
DeviceServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "LogLevelFile.h"
19 #include "DeviceServer.h"
20 
21 DeviceServer::DeviceServer(const char *path)
22  : FileSystemServer(new Directory(1), path)
23 {
24 }
25 
27 {
28 }
29 
31 {
32  // Initialize all devices
33  for (Size i = 0; i < m_devices.count(); i++)
34  {
35  Device *dev = m_devices[i];
36  if (dev != ZERO)
37  {
38  const FileSystem::Result result = dev->initialize();
39  if (result != FileSystem::Success)
40  {
41  ERROR("failed to initialize device " << (*dev->getIdentifier()) <<
42  ": result = " << (int)result);
43  return result;
44  }
45  }
46  }
47 
48  // Add log level pseudo file
49  const FileSystem::Result logResult = registerFile(new LogLevelFile(getNextInode()), "loglevel");
50  if (logResult != FileSystem::Success)
51  {
52  ERROR("failed to register LogLevelFile: result = " << (int) logResult);
53  return logResult;
54  }
55 
56  // Mount on the root file system
57  const FileSystem::Result result = mount();
58  if (result != FileSystem::Success)
59  {
60  ERROR("failed to mount to path " << m_mountPath << ": result = " << (int)result);
61  return result;
62  }
63 
64  return FileSystem::Success;
65 }
66 
67 void DeviceServer::registerDevice(Device *dev, const char *path)
68 {
70 
71  // Add to the list of Devices
72  const bool result = m_devices.insert(dev);
73  if (!result)
74  {
75  FATAL("failed to register device on path: " << path);
76  }
77 }
78 
80 {
81  if (!m_interrupts.get(vector))
82  {
84  }
85  m_interrupts[vector]->append(dev);
86 
87  // Register to kernel
88  const API::Result watchResult = ProcessCtl(SELF, WatchIRQ, vector);
89  if (watchResult != API::Success)
90  {
91  ERROR("failed to register IRQ handler using WatchIRQ: result = " << (int) watchResult);
92  return;
93  }
94 
95  const API::Result enableResult = ProcessCtl(SELF, EnableIRQ, vector);
96  if (enableResult != API::Success)
97  {
98  ERROR("failed to enable IRQ handler using EnableIRQ: result = " << (int) enableResult);
99  return;
100  }
101 
102  // Register interrupt handler
104 }
105 
107 {
108  List<Device *> *lst = m_interrupts.get(vector);
109 
110  // Do we have any Devices with this interrupt vector?
111  if (lst)
112  {
113  // Loop all Devices of interest. Invoke callback.
114  for (ListIterator<Device *> i(lst); i.hasCurrent(); i++)
115  {
116  i.current()->interrupt(vector);
117  }
118  }
119 
120  // Keep retrying any pending requests, if any
122 }
Index::count
virtual Size count() const
Item count in the Index.
Definition: Index.h:225
DeviceServer.h
Index::insertAt
virtual bool insertAt(const Size position, T *item)
Inserts the given item at the given position.
Definition: Index.h:113
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
FileSystemServer::getNextInode
u32 getNextInode()
Get next unused inode.
Definition: FileSystemServer.cpp:66
EnableIRQ
@ EnableIRQ
Definition: ProcessCtl.h:44
WatchIRQ
@ WatchIRQ
Definition: ProcessCtl.h:43
Device::initialize
virtual FileSystem::Result initialize()
Initialize the device.
Definition: Device.cpp:35
Directory
Directory File functionality.
Definition: Directory.h:59
LogLevelFile.h
DeviceServer::registerInterrupt
void registerInterrupt(Device *dev, Size vector)
Register an interrupt vector for the given device.
Definition: DeviceServer.cpp:79
ChannelServer< FileSystemServer, FileSystemMessage >::addIRQHandler
void addIRQHandler(const Size slot, IRQHandlerFunction h)
Register a new IRQ message vector handler.
Definition: ChannelServer.h:214
Device
Abstract device class interface.
Definition: Device.h:35
ChannelServer< FileSystemServer, FileSystemMessage >::IRQHandlerFunction
void(FileSystemServer ::* IRQHandlerFunction)(Size)
Member function pointer inside Base, to handle interrupts.
Definition: ChannelServer.h:92
FileSystemServer
Abstract filesystem class.
Definition: FileSystemServer.h:44
FileSystem::Success
@ Success
Definition: FileSystem.h:54
ProcessCtl
API::Result ProcessCtl(const ProcessID proc, const ProcessOperation op, const Address addr=0, const Address output=0)
Prototype for user applications.
Definition: ProcessCtl.h:93
SELF
#define SELF
Definition: ProcessID.h:35
DeviceServer::registerDevice
void registerDevice(Device *dev, const char *path)
Add a Device.
Definition: DeviceServer.cpp:67
FATAL
#define FATAL(msg)
Output a critical message and terminate program immediatly.
Definition: Log.h:50
LogLevelFile
Provides a File abstraction of the current Log::Level.
Definition: LogLevelFile.h:35
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
FileSystemServer::m_mountPath
const char * m_mountPath
Mount point path.
Definition: FileSystemServer.h:311
DeviceServer::interruptHandler
virtual void interruptHandler(Size vector)
Interrupt request handler.
Definition: DeviceServer.cpp:106
DeviceServer::DeviceServer
DeviceServer(const char *path)
Constructor.
Definition: DeviceServer.cpp:21
DeviceServer::m_devices
Index< Device, MaximumDevices > m_devices
Contains all Devices served by this DeviceServer.
Definition: DeviceServer.h:112
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Index::get
virtual T * get(const Size position) const
Returns the item at the given position.
Definition: Index.h:187
DeviceServer::initialize
virtual FileSystem::Result initialize()
Initialize DeviceServer.
Definition: DeviceServer.cpp:30
ChannelServer< FileSystemServer, FileSystemMessage >::retryAllRequests
void retryAllRequests()
Keep retrying requests until all served.
Definition: ChannelServer.h:251
FileSystemServer::mount
FileSystem::Result mount()
Mount the FileSystem.
Definition: FileSystemServer.cpp:77
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
Device::getIdentifier
virtual const String & getIdentifier() const
Get unique device identifier.
Definition: Device.cpp:30
API::Success
@ Success
Definition: API.h:70
DeviceServer::~DeviceServer
virtual ~DeviceServer()
Destructor.
Definition: DeviceServer.cpp:26
List< Device * >
DeviceServer::m_interrupts
Index< List< Device * >, MaximumInterrupts > m_interrupts
Registers Devices using interrupts.
Definition: DeviceServer.h:124
Index::insert
virtual bool insert(Size &position, T *item)
Adds the given item, if possible.
Definition: Index.h:60
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
ListIterator
Iterate through a List.
Definition: ListIterator.h:37
FileSystemServer::registerFile
FileSystem::Result registerFile(File *file, const char *path)
Register a new File.
Definition: FileSystemServer.cpp:115