Exercise+10

I denne øvelse skal vi lave et gennaralt program til at skrive og læse fra device drivers. underneden ses et eksempel til interface, dette er ikke fuldt hele vejen.



Constructor
code format="cpp" GenericDevice(char* fn) {   fileName = fn; fd = open(fileName, O_RDWR); if (fd < 0) cout << "Could NOT open '" << fileName << "' !!!\n"; }; code

Desctructor
code format="cpp" ~GenericDevice {   if (fd < 0) return; close(fd); } code

getValue
code format="cpp" int getValue {   if (fd < 0) {cout << "Could NOT read from '" << fileName << "' !!!\n"; return 0;} char* c = ""; read(fd, c, 12); return atoi (c); } code

setValue
code format="cpp" void setValue(char* input) {       size_t length = strlen(input); cout << "length: " << length << endl; write(fileDescriptor, input, length+1); } code

Dette er testet og ingen fejl fundet code format="cpp" int main { GenericDevice gd("/dev/test");

cout << "Write 10 to node\n"; gd.setValue(10);

cout << "Read '" << gd.getValue << endl;

return 0; } code