Sensor application for transmitting humidity values
git clone https://git.8pit.net/feucht.git
1#include "9p.h" 2 3#include "net/af.h" 4#include "net/gnrc/ipv6.h" 5#include "net/gnrc/tcp.h" 6 7/** 8 * GNRC transmission control block. 9 */10static gnrc_tcp_tcb_t tcb;1112/**13 * 9P connection context.14 */15static _9pctx ctx;1617/**18 * Fid for the file containing the humidity values.19 */20static _9pfid *hfid;2122/**23 * File name of the file containing the humidity values.24 */25#define HUMIDITY_FN "humidity"2627/**28 * Function for reading from the GNRC tcp socket.29 *30 * @param buf Pointer to a buffer where received data should be stored.31 * @param count Maximum amount of bytes which should be read.32 * @return Amount of bytes received.33 */34static ssize_t35recvfn(void *buf, size_t count)36{37 return gnrc_tcp_recv(&tcb, buf,38 count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);39}4041/**42 * Function for writting to the GNRC tcp socket.43 *44 * @param buf Pointer to a buffer containing the data which should be written.45 * @param count Amount of bytes which should be written.46 * @return Amount of bytes written to the socket.47 */48static ssize_t49sendfn(void *buf, size_t count)50{51 return gnrc_tcp_send(&tcb, buf,52 count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);53}5455int56init_protocol(ipv6_addr_t *remote)57{58 int ret;59 _9pfid *rfid;6061 gnrc_tcp_tcb_init(&tcb);62 if ((ret = gnrc_tcp_open_active(&tcb, AF_INET6,63 (uint8_t *)remote, FEUCHT_PORT, 0)))64 return ret;6566 _9pinit(&ctx, recvfn, sendfn);67 if ((ret = _9pversion(&ctx)))68 return ret;6970 if ((ret = _9pattach(&ctx, &rfid, "glenda", NULL)))71 return ret;7273 if ((ret = _9pwalk(&ctx, &hfid, HUMIDITY_FN)) ||74 (ret = _9popen(&ctx, hfid, OWRITE)))75 return ret;7677 return 0;78}7980void81free_protocol(void)82{83 gnrc_tcp_close(&tcb);84}8586int87update_humidity(char *buf, size_t count)88{89 int ret;9091 if ((ret = _9pwrite(&ctx, hfid, buf, count)) < 0)92 return ret;9394 puts("Humidity has been updated!");95 return 0;96}