Skip to content

Commit

Permalink
Implemented Device::getIdentifier() and modified DeviceServer to use it.
Browse files Browse the repository at this point in the history
This guarantees that devices are always visible in /dev with a unique path.
  • Loading branch information
nieklinnenbank committed Nov 4, 2015
1 parent fc6b262 commit 0d99398
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 41 deletions.
15 changes: 15 additions & 0 deletions server/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Device
{
public:

/**
* Get unique device identifier.
*
* @return String object
*/
virtual String & getIdentifier()
{
return m_identifier;
}

/**
* @brief Perform device specific initialization.
* @return Error result code.
Expand Down Expand Up @@ -66,6 +76,11 @@ class Device
{
return ESUCCESS;
}

protected:

/** Unique identifier for this Device. */
String m_identifier;
};

#endif /* __DEVICE_H */
39 changes: 10 additions & 29 deletions server/DeviceServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ class DeviceServer : public IPCServer<DeviceServer, FileSystemMessage>
*
* Responsible for registering IPC handlers.
*
* @param prefix Used to to form the filename of the device files.
* @param type FileType of the device files to create.
* @param mode Access permissions on the device files.
*/
DeviceServer(const char *prefix, FileType type, FileMode mode = OwnerRW)
DeviceServer(FileType type, FileMode mode = OwnerRW)
: IPCServer<DeviceServer, FileSystemMessage>(this), devices(DEVICE_MAX)
{
/* Initialize local member variables. */
this->prefix = prefix;
this->type = type;
this->mode = mode;
this->interrupts.fill(ZERO);
Expand Down Expand Up @@ -127,25 +125,17 @@ class DeviceServer : public IPCServer<DeviceServer, FileSystemMessage>
// Create device node files
for (Size i = 0; i < devices.count(); i++)
{
/* Attempt to create the device file. */
for (Size i = 0; i < 1000; i++)
{
/* For a path using the supplied prefix. */
snprintf(path, sizeof(path), "/dev/%s%u",
prefix, i);
// Attempt to create the device file. */
snprintf(path, sizeof(path), "/dev/%s",
*devices[i]->getIdentifier());

/*
* Use our ProcessID to redirect FileSystemMessages to us.
*/
id.major = pid;
id.minor = i;
// Use our ProcessID to redirect FileSystemMessages to us.
id.major = pid;
id.minor = i;

/* Create the special device file. */
if (mknod(path, (type << FILEMODE_BITS) | mode, id) == 0)
{
break;
}
}
// Create the special device file
if (mknod(path, (type << FILEMODE_BITS) | mode, id) != 0)
ERROR("failed to create device at: " << path << ": " << errno);
}
return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -377,15 +367,6 @@ class DeviceServer : public IPCServer<DeviceServer, FileSystemMessage>
*/
List<FileSystemMessage *> requests;

/**
* @brief Prefix string used to create device files in /tmp.
*
* If prefix contains e.g. "foobar", then add() will
* attempt to create "/dev/foobar0". If that fails it tries
* "/dev/foobar1", "/dev/foobar2", and so on.
*/
const char *prefix;

/** The type of device file to create. */
FileType type;

Expand Down
3 changes: 2 additions & 1 deletion server/ata/ATAController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

int main(int argc, char **argv)
{
DeviceServer server("ata", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

/*
* Start serving requests.
Expand All @@ -34,6 +34,7 @@ int main(int argc, char **argv)

ATAController::ATAController()
{
m_identifier << "ata0";
}

Error ATAController::initialize()
Expand Down
1 change: 1 addition & 0 deletions server/i2c/grovepi/AnalogPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AnalogPort::AnalogPort(I2C *i2c, uint port)
{
m_i2c = i2c;
m_port = port;
m_identifier << "groveAnalog" << port;
}

Error AnalogPort::initialize()
Expand Down
1 change: 1 addition & 0 deletions server/i2c/grovepi/DigitalPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DigitalPort::DigitalPort(I2C *i2c, uint port)
{
m_i2c = i2c;
m_port = port;
m_identifier << "groveDigi" << port;
}

Error DigitalPort::initialize()
Expand Down
4 changes: 1 addition & 3 deletions server/i2c/grovepi/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ void setRGB(BroadcomI2C *i2c, uint r, uint g, uint b)

int main(int argc, char **argv)
{
#warning modify Device to provide a name.
#warning ensure that devices each have a fixed reliable name, based on e.g. its hardware address.
DeviceServer server("groveDigi", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

// Setup logging
Log *log = new KernelLog();
Expand Down
1 change: 1 addition & 0 deletions server/input/keyboard/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const char Keyboard::keymap[0x3a][2] =

Keyboard::Keyboard() : shiftState(ZERO)
{
m_identifier << "keyboard0";
}

Error Keyboard::initialize()
Expand Down
6 changes: 3 additions & 3 deletions server/input/keyboard/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

int main(int argc, char **argv)
{
DeviceServer server("keyboard", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

/* Create a new keyboard object. */
Keyboard *kb = new Keyboard;

/* Register it with the DeviceServer. */
server.add(kb);
server.interrupt(kb, PS2_IRQ);

/*
* Start processing requests.
*/
Expand Down
2 changes: 1 addition & 1 deletion server/serial/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ uarts[] =

int main(int argc, char **argv)
{
DeviceServer server("serial", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

/* Open the logging facilities. */
Log *log = new KernelLog();
Expand Down
1 change: 1 addition & 0 deletions server/serial/PL011.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PL011::PL011(u32 irq)
{
m_irq = irq;
m_identifier << "serial0";
}

Error PL011::initialize()
Expand Down
1 change: 1 addition & 0 deletions server/serial/i8250.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
i8250::i8250(u16 b, u16 q)
: base(b), irq(q)
{
m_identifier << "serial0";
}

Error i8250::initialize()
Expand Down
2 changes: 1 addition & 1 deletion server/terminal/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

int main(int argc, char **argv)
{
DeviceServer server("tty", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

/*
* Start serving requests.
Expand Down
3 changes: 2 additions & 1 deletion server/terminal/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ u8 tekenToVGA[] =
};

Terminal::Terminal(const char *in, const char *out,
Size w, Size h)
Size w, Size h)
: inputFile(in), outputFile(out), width(w), height(h)
{
m_identifier << "tty0";
buffer = new u16[width * height];
}

Expand Down
2 changes: 1 addition & 1 deletion server/time/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

int main(int argc, char **argv)
{
DeviceServer server("time", CharacterDeviceFile);
DeviceServer server(CharacterDeviceFile);

/*
* Start serving requests
Expand Down
1 change: 1 addition & 0 deletions server/time/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

Time::Time()
{
m_identifier << "time0";
}

Error Time::initialize()
Expand Down
2 changes: 1 addition & 1 deletion server/video/vga/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

int main(int argc, char **argv)
{
DeviceServer server("vga", BlockDeviceFile);
DeviceServer server(BlockDeviceFile);

/*
* Start serving requests.
Expand Down
1 change: 1 addition & 0 deletions server/video/vga/VGA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

VGA::VGA(Size w, Size h) : width(w), height(h)
{
m_identifier << "vga0";
}

Error VGA::initialize()
Expand Down

0 comments on commit 0d99398

Please sign in to comment.