//Note, the preq needs to be able to set the EN flag void process_PReq(uintptr_t rx_buffer_address, chanend c_can_open){ frame * frm = (frame *)rx_buffer_address; PReq_t * p = &(frm->pl_frame.preq); if(p->RD==0) return; //read from 0x1600:0 unsigned char number_of_entries; unsigned no_of_bytes; co_error_code_t err = co_read(c_can_open, 0x1600, 0, &number_of_entries, &no_of_bytes); //TODO check the err for(unsigned short subindex=1;subindex<=number_of_entries;subindex++){ uint16_t rx_index; uint8_t rx_subindex; uint16_t rx_offset; uint16_t rx_length; co_read_PDO_entry(c_can_open, 0x1600, subindex, &rx_index, &rx_subindex, &rx_length, &rx_offset); uint8_t data[C_DLL_ISOCHR_MAX_PAYL]; unsigned number_of_bytes = rx_length>>3; co_error_code_t e = co_write(c_can_open, rx_index, rx_subindex, p->payload, number_of_bytes); } //TODO deal with the error flags in much the same way as soa //if(error_signalling_data_present) // async_state->EN = 1 - p->EA; }
static void main_thread(co_context_t *co, void *user) { co_file_t *peer; const char *request = "GET / HTTP/1.0\r\n" "Host: google.com\r\n" "Connection: close\r\n" "User-Agent: libco/0.1\r\n" "\r\n"; char buf; ssize_t rsize; printf("connecting...\n"); peer = co_connect_tcp(co, "google.com", 80); if (peer == NULL) { printf("connect failed :(\n"); return; } printf("connected! sending request...\n"); co_write(co, peer, request, strlen(request), NULL); printf("request sent! reading response, a byte at a time...\n"); do { buf = 0; co_read(co, peer, &buf, 1, &rsize); putchar(buf); } while (rsize); printf("end of response. bye!\n"); }
void co_listen(void *arg) { int i, n, fd, state; char buf[1024]; state = 0; fd = (long)arg; while (state != DONE && (n = co_tcp_read(fd, buf, 1024)) > 0) { for (i = 0; i < n; ++i) { switch(state) { case IGN: if (buf[i] == '\r') { state = CR; } break; case CR: if (buf[i] == '\n') { state = ENDLINE; } else { state = IGN; } break; case ENDLINE: if (buf[i] == '\r') { state = ALMOST; } else { state = IGN; } break; case ALMOST: if (buf[i] == '\n') { state = DONE; } else { state = IGN; } break; case DONE: default: break; } } } if (state == DONE) { (void) co_write(fd, out, strlen(out)); (void) close(fd); } return ; }
static void connector_thread(co_context_t *co, void *_conn) { co_file_t *peer; struct connector *conn = _conn; const char *request_fmt = "GET / HTTP/1.0\r\n" "Host: %s\r\n" "User-Agent: libco/0.0\r\n" "Accept: */*\r\n" "Connection: close\r\n" "\r\n"; char buf[BUFSIZE]; ssize_t rsize; snprintf(buf, BUFSIZE, request_fmt, conn->host); printf("%s: connecting to %s\n", conn->name, conn->host); peer = co_connect_tcp(co, conn->host, 80); if (peer == NULL) { printf("%s: connect failed :(\n", conn->name); return; } printf("%s: connected! sending request...\n", conn->name); co_write(co, peer, buf, strlen(buf), NULL); printf("%s: request sent!\n", conn->name); printf("%s: reading response...\n", conn->name); do { memset(buf, 0, sizeof(buf)); co_read(co, peer, buf, BUFSIZE, &rsize); printf("%s: got %d bytes\n", conn->name, rsize); } while (rsize); printf("%s: bye!\n", conn->name); free(conn); }