feucht

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;
11
12/**
13 * 9P connection context.
14 */
15static _9pctx ctx;
16
17/**
18 * Fid for the file containing the humidity values.
19 */
20static _9pfid *hfid;
21
22/**
23 * File name of the file containing the humidity values.
24 */
25#define HUMIDITY_FN "humidity"
26
27/**
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_t
35recvfn(void *buf, size_t count)
36{
37	return gnrc_tcp_recv(&tcb, buf,
38		count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);
39}
40
41/**
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_t
49sendfn(void *buf, size_t count)
50{
51	return gnrc_tcp_send(&tcb, buf,
52		count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);
53}
54
55int
56init_protocol(ipv6_addr_t *remote)
57{
58	int ret;
59	_9pfid *rfid;
60
61	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;
65
66	_9pinit(&ctx, recvfn, sendfn);
67	if ((ret = _9pversion(&ctx)))
68		return ret;
69
70	if ((ret = _9pattach(&ctx, &rfid, "glenda", NULL)))
71		return ret;
72
73	if ((ret = _9pwalk(&ctx, &hfid, HUMIDITY_FN)) ||
74			(ret = _9popen(&ctx, hfid, OWRITE)))
75		return ret;
76
77	return 0;
78}
79
80void
81free_protocol(void)
82{
83	gnrc_tcp_close(&tcb);
84}
85
86int
87update_humidity(char *buf, size_t count)
88{
89	int ret;
90	
91	if ((ret = _9pwrite(&ctx, hfid, buf, count)) < 0)
92		return ret;
93
94	puts("Humidity has been updated!");
95	return 0;
96}