Commit | Line | Data |
---|---|---|
d4ee6ee2 JM |
1 | #include "CallbackStream.h" |
2 | #include "Kernel.h" | |
61134a65 | 3 | #include <stdio.h> |
d4ee6ee2 | 4 | |
8477814e JM |
5 | #include "SerialConsole.h" |
6 | #define DEBUG_PRINTF THEKERNEL->serial->printf | |
d4ee6ee2 JM |
7 | |
8 | CallbackStream::CallbackStream(cb_t cb, void *u) | |
9 | { | |
10 | DEBUG_PRINTF("Callbackstream ctor: %p\n", this); | |
11 | callback= cb; | |
12 | user= u; | |
13 | closed= false; | |
14 | use_count= 0; | |
15 | } | |
16 | ||
17 | CallbackStream::~CallbackStream() | |
18 | { | |
19 | DEBUG_PRINTF("Callbackstream dtor: %p\n", this); | |
20 | } | |
21 | ||
22 | int CallbackStream::puts(const char *s) | |
23 | { | |
24 | if(closed) return 0; | |
25 | ||
26 | if(s == NULL) return (*callback)(NULL, user); | |
27 | ||
28 | int len = strlen(s); | |
29 | int n; | |
30 | do { | |
31 | // call this streams result callback | |
32 | n= (*callback)(s, user); | |
33 | ||
34 | // if closed just pretend we sent it | |
35 | if(n == -1) { | |
36 | closed= true; | |
37 | return len; | |
38 | ||
39 | }else if(n == 0) { | |
40 | // if output queue is full | |
41 | // call idle until we can output more | |
42 | THEKERNEL->call_event(ON_IDLE); | |
43 | } | |
44 | } while(n == 0); | |
45 | ||
46 | return len; | |
47 | } | |
48 | ||
49 | void CallbackStream::mark_closed() | |
50 | { | |
51 | closed= true; | |
52 | if(use_count <= 0) delete this; | |
53 | } | |
54 | void CallbackStream::dec() | |
55 | { | |
56 | use_count--; | |
57 | if(closed && use_count <= 0) delete this; | |
58 | } | |
59 | ||
60 | extern "C" void *new_callback_stream(cb_t cb, void *u) | |
61 | { | |
62 | return new CallbackStream(cb, u); | |
63 | } | |
64 | ||
65 | extern "C" void delete_callback_stream(void *p) | |
66 | { | |
67 | // we don't delete it in case it is still on the command queue | |
68 | ((CallbackStream*)p)->mark_closed(); | |
69 | } |