Net Streams
Posted: Fri Aug 09, 2013 1:05 pm
Hi,
The following code has been contributed via email by W.S.
It looks pretty useful: lwIP sockets wrapped in ChibiOS streams.
Giovanni
netstream.c
netstream.h
Giovanni
The following code has been contributed via email by W.S.
It looks pretty useful: lwIP sockets wrapped in ChibiOS streams.
Giovanni
netstream.c
Code: Select all
#include "netstream.h"
#include "lwip/sockets.h"
static size_t writes(void *ip, const uint8_t *bp, size_t n) {
NetStream *sp = ip;
return lwip_write(sp->socket, bp, n);
}
static size_t reads(void *ip, uint8_t *bp, size_t n) {
NetStream *sp = ip;
return lwip_read(sp->socket, bp, n);
}
static msg_t put(void *ip, uint8_t b) {
return (writes(ip, &b, 1) == 1 ? Q_OK : Q_RESET);
}
static msg_t get(void *ip) {
uint8_t b;
return (reads(ip, &b, 1) == 1 ? b : Q_RESET);
}
static const struct NetStreamVMT vmt = {writes, reads, put, get};
void nsObjectInit(NetStream *sp, int socket) {
sp->vmt = &vmt;
sp->socket = socket;
}
netstream.h
Code: Select all
#ifndef NETSTREAM_H_
#define NETSTREAM_H_
#include <stddef.h>
#include <stdint.h>
#include <chtypes.h>
#include <chstreams.h>
#define _net_stream_data \
_base_sequential_stream_data \
int socket;
struct NetStreamVMT {
_base_sequential_stream_methods
};
/**
* @extends BaseSequentialStream
*
* @brief Memory stream object.
*/
typedef struct {
const struct NetStreamVMT *vmt;
_net_stream_data
} NetStream;
#ifdef __cplusplus
extern "C" {
#endif
void nsObjectInit(NetStream *sp, int socket);
#ifdef __cplusplus
}
#endif
#endif /* NETSTREAM_H_ */
Giovanni