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
22 : FileSystemServer(new Directory(1), path)
23{
24}
25
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
65}
66
67void 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}
Result
Enumeration of generic kernel API result codes.
Definition API.h:69
@ Success
Definition API.h:70
void retryAllRequests()
Keep retrying requests until all served.
void(Base::* IRQHandlerFunction)(Size)
Member function pointer inside Base, to handle interrupts.
void addIRQHandler(const Size slot, IRQHandlerFunction h)
Register a new IRQ message vector handler.
DeviceServer(const char *path)
Constructor.
virtual FileSystem::Result initialize()
Initialize DeviceServer.
virtual void interruptHandler(Size vector)
Interrupt request handler.
void registerDevice(Device *dev, const char *path)
Add a Device.
void registerInterrupt(Device *dev, Size vector)
Register an interrupt vector for the given device.
Index< List< Device * >, MaximumInterrupts > m_interrupts
Registers Devices using interrupts.
virtual ~DeviceServer()
Destructor.
Index< Device, MaximumDevices > m_devices
Contains all Devices served by this DeviceServer.
Abstract device class interface.
Definition Device.h:36
virtual FileSystem::Result initialize()
Initialize the device.
Definition Device.cpp:35
virtual const String & getIdentifier() const
Get unique device identifier.
Definition Device.cpp:30
Directory File functionality.
Definition Directory.h:60
Abstract filesystem class.
FileSystem::Result mount()
Mount the FileSystem.
u32 getNextInode()
Get next unused inode.
FileSystem::Result registerFile(File *file, const char *path)
Register a new File.
const char * m_mountPath
Mount point path.
virtual Size count() const
Item count in the Index.
Definition Index.h:225
virtual T * get(const Size position) const
Returns the item at the given position.
Definition Index.h:187
virtual bool insertAt(const Size position, T *item)
Inserts the given item at the given position.
Definition Index.h:113
virtual bool insert(Size &position, T *item)
Adds the given item, if possible.
Definition Index.h:60
Iterate through a List.
virtual bool hasCurrent() const
Check if there is a current item on the List.
Simple linked list template class.
Definition List.h:37
Provides a File abstraction of the current Log::Level.
#define SELF
Definition ProcessID.h:35
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
@ WatchIRQ
Definition ProcessCtl.h:43
@ EnableIRQ
Definition ProcessCtl.h:44
#define ERROR(msg)
Output an error message.
Definition Log.h:61
#define FATAL(msg)
Output a critical message and terminate program immediatly.
Definition Log.h:50
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
Result
Result code for filesystem Actions.
Definition FileSystem.h:53