-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RTOS-299
- Loading branch information
HBuczynski
committed
Dec 15, 2022
1 parent
c47aaf1
commit 54c5764
Showing
6 changed files
with
257 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Phoenix-RTOS | ||
* | ||
* Operating system kernel | ||
* | ||
* Unnamed pipes | ||
* | ||
* Copyright 2022 Phoenix Systems | ||
* Author: Hubert Buczynski | ||
* | ||
* This file is part of Phoenix-RTOS. | ||
* | ||
* %LICENSE% | ||
*/ | ||
|
||
#include "pipe.h" | ||
|
||
#include "../usrv.h" | ||
#include "../lib/cbuffer.h" | ||
#include "../include/posix.h" | ||
|
||
|
||
#define SIZE_PIPE_BUFF (4 * SIZE_PAGE) | ||
|
||
typedef struct { | ||
rbnode_t linkage; | ||
oid_t oid; | ||
|
||
lock_t lock; | ||
void *data; | ||
cbuffer_t cbuff; | ||
} pipe_t; | ||
|
||
|
||
static struct { | ||
rbtree_t pipes; | ||
unsigned int cnt; | ||
lock_t lock; | ||
} pipe_common; | ||
|
||
|
||
static int pipe_cmp(rbnode_t *n1, rbnode_t *n2) | ||
{ | ||
pipe_t *p1 = lib_treeof(pipe_t, linkage, n1); | ||
pipe_t *p2 = lib_treeof(pipe_t, linkage, n2); | ||
int res; | ||
|
||
if (p1->oid.id < p2->oid.id) { | ||
res = -1; | ||
} | ||
else if (p1->oid.id > p2->oid.id) { | ||
res = 1; | ||
} | ||
else { | ||
res = 0; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
||
static pipe_t *pipe_getPipe(const oid_t *oid) | ||
{ | ||
pipe_t p; | ||
p.oid = *oid; | ||
|
||
return lib_treeof(pipe_t, linkage, lib_rbFind(&pipe_common.pipes, &p.linkage)); | ||
} | ||
|
||
|
||
static int pipe_destroy(oid_t oid) | ||
{ | ||
pipe_t *pipe = pipe_getPipe(&oid); | ||
if (pipe == NULL) { | ||
return -EINVAL; | ||
} | ||
|
||
proc_lockSet(&pipe_common.lock); | ||
lib_rbRemove(&pipe_common.pipes, &pipe->linkage); | ||
proc_lockClear(&pipe_common.lock); | ||
|
||
proc_lockSet(&pipe->lock); | ||
_cbuffer_free(&pipe->cbuff); | ||
vm_kfree(pipe->data); | ||
proc_lockClear(&pipe->lock); | ||
|
||
proc_lockDone(&pipe->lock); | ||
vm_kfree(pipe); | ||
|
||
return EOK; | ||
} | ||
|
||
|
||
static int pipe_create(oid_t *oid) | ||
{ | ||
int res; | ||
pipe_t *p; | ||
|
||
p = vm_kmalloc(sizeof(pipe_t)); | ||
if (p == NULL) { | ||
return -ENOMEM; | ||
} | ||
|
||
p->data = vm_kmalloc(SIZE_PIPE_BUFF); | ||
if (p->data == NULL) { | ||
vm_kfree(p); | ||
return -ENOMEM; | ||
} | ||
|
||
res = proc_lockInit(&p->lock); | ||
if (res < 0) { | ||
vm_kfree(p->data); | ||
vm_kfree(p); | ||
return res; | ||
} | ||
|
||
res = _cbuffer_init(&p->cbuff, p->data, SIZE_PIPE_BUFF); | ||
if (res < 0) { | ||
proc_lockClear(&p->lock); | ||
vm_kfree(p->data); | ||
vm_kfree(p); | ||
return res; | ||
} | ||
|
||
p->oid.port = USRV_PORT; | ||
p->oid.id = (id_t)(++pipe_common.cnt << USRV_ID_BITS) | USRV_ID_PIPES; | ||
|
||
proc_lockSet(&pipe_common.lock); | ||
lib_rbInsert(&pipe_common.pipes, &p->linkage); | ||
proc_lockClear(&pipe_common.lock); | ||
|
||
return EOK; | ||
} | ||
|
||
|
||
static int pipe_read(const oid_t *oid, void *buf, size_t sz, unsigned mode) | ||
{ | ||
pipe_t *pipe = pipe_getPipe(oid); | ||
|
||
if (pipe == NULL) { | ||
return -EINVAL; | ||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
||
|
||
static int pipe_write(const oid_t *oid, const void *buf, size_t sz, unsigned mode) | ||
{ | ||
pipe_t *pipe = pipe_getPipe(oid); | ||
|
||
if (pipe == NULL) { | ||
return -EINVAL; | ||
} | ||
|
||
|
||
if (mode & O_NONBLOCK) { | ||
|
||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
||
void pipe_msgHandler(msg_t *msg, oid_t oid, unsigned long int rid) | ||
{ | ||
switch (msg->type) { | ||
case mtOpen: | ||
/* TODO: increase refs in pipes */ | ||
break; | ||
|
||
case mtCreate: | ||
msg->o.create.err = pipe_create(&msg->o.create.oid); | ||
break; | ||
|
||
case mtRead: | ||
msg->o.io.err = pipe_read(&msg->i.io.oid, msg->o.data, msg->o.size, msg->i.io.mode); | ||
break; | ||
|
||
case mtWrite: | ||
msg->o.io.err = pipe_write(&msg->i.io.oid, msg->i.data, msg->i.size, msg->i.io.mode); | ||
break; | ||
|
||
case mtClose: | ||
/* TODO: manage refs ine pipes and destroy pipe */ | ||
break; | ||
|
||
case mtDevCtl: | ||
/* TODO: handle fcntl */ | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
proc_respond(oid.port, msg, rid); | ||
} | ||
|
||
|
||
void pipe_init(void) | ||
{ | ||
pipe_common.cnt = 0; | ||
|
||
proc_lockInit(&pipe_common.lock); | ||
lib_rbInit(&pipe_common.pipes, pipe_cmp, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Phoenix-RTOS | ||
* | ||
* Operating system kernel | ||
* | ||
* Unnamed pipes | ||
* | ||
* Copyright 2022 Phoenix Systems | ||
* Author: Hubert Buczynski | ||
* | ||
* This file is part of Phoenix-RTOS. | ||
* | ||
* %LICENSE% | ||
*/ | ||
|
||
#ifndef _POSIX_PIPE_H_ | ||
#define _POSIX_PIPE_H_ | ||
|
||
#include "../proc/msg.h" | ||
|
||
|
||
extern void pipe_msgHandler(msg_t *msg, oid_t oid, unsigned long int rid); | ||
|
||
|
||
extern void pipe_init(void); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters