1 /*
2  * This code is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This code is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12 
13 #ifndef __SPECTOOL_NET_CLIENT_H__
14 #define __SPECTOOL_NET_CLIENT_H__
15 
16 #include "config.h"
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <time.h>
21 #include <sys/file.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <unistd.h>
29 #include <netdb.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 
34 #include "spectool_container.h"
35 #include "spectool_net.h"
36 
37 #define CLI_BUF_SZ				16384
38 
39 #define SPECTOOL_NETCLI_URL_MAX	300
40 
41 /* Aux struct holding network device info.  In many ways a duplicate of the
42  * phydev dev spec, however this has to hold annotations for devies which
43  * are advertised but not activated */
44 typedef struct _spectool_net_dev {
45 	unsigned int device_version;
46 	unsigned int device_flags;
47 
48 	unsigned int device_id;
49 
50 	char device_name[256];
51 
52 	int amp_offset_mdbm;
53 	unsigned int amp_res_mdbm;
54 	unsigned int rssi_max;
55 
56 	unsigned int def_start_khz;
57 	unsigned int def_res_hz;
58 	unsigned int def_num_samples;
59 
60 	unsigned int start_khz;
61 	unsigned int res_hz;
62 	unsigned int num_samples;
63 
64 	/* Local attributes if we're an activated device */
65 	spectool_phy *phydev;
66 
67 	struct _spectool_net_dev *next;
68 } spectool_net_dev;
69 
70 /* Advertised servers we've seen & the info we have about them */
71 typedef struct _spectool_adv_server {
72 	unsigned int proto_version;
73 
74 	struct sockaddr addr;
75 	socklen_t addr_len;
76 
77 	unsigned short int port;
78 
79 	time_t last_advertised;
80 
81 	struct _spectool_adv_server *next;
82 } spectool_adv_server;
83 
84 /* Struct that handles tracking a server we've connected to */
85 typedef struct _spectool_server {
86 	int sock;
87 
88 	char *url;
89 
90 	char hostname[256];
91 	short unsigned int port;
92 
93 	unsigned int conaddr;
94 
95 	struct hostent *host;
96 
97 	int bufferwrite;
98 
99 	uint8_t wbuf[CLI_BUF_SZ];
100 	uint8_t rbuf[CLI_BUF_SZ];
101 
102 	int write_pos, read_pos, write_fill, read_fill;
103 
104 	int state;
105 
106 	spectool_net_dev *devlist;
107 } spectool_server;
108 
109 #define SPECTOOL_NET_STATE_NONE			0
110 #define SPECTOOL_NET_STATE_CONNECTED	1
111 #define SPECTOOL_NET_STATE_CONFIGURED	2
112 #define SPECTOOL_NET_STATE_ERROR		255
113 
114 /* Auxptr struct attached to a spectool_net phydev */
115 typedef struct _spectool_net_dev_aux {
116 	spectool_server *server;
117 	spectool_net_dev *netdev;
118 	spectool_sample_sweep *sweep;
119 	int new_sweep;
120 	int spipe[2];
121 } spectool_net_dev_aux;
122 
123 /* Server manipulation commands - one server can have many phydevs linked to it,
124  * and an app can conceivably have many servers. */
125 int spectool_netcli_init(spectool_server *sr, char *url, char *errstr);
126 int spectool_netcli_connect(spectool_server *sr, char *errstr);
127 int spectool_netcli_close(spectool_server *sr);
128 unsigned int spectool_netcli_getaddr(spectool_server *sr);
129 unsigned short int spectool_netcli_getport(spectool_server *sr);
130 char *spectool_netcli_geturl(spectool_server *sr);
131 int spectool_netcli_getstate(spectool_server *sr);
132 int spectool_netcli_getpollfd(spectool_server *sr);
133 void spectool_netcli_setbufferwrite(spectool_server *sr, int buf);
134 int spectool_netcli_getwritepend(spectool_server *sr);
135 int spectool_netcli_getwritefd(spectool_server *sr);
136 int spectool_netcli_poll(spectool_server *sr, char *errstr);
137 int spectool_netcli_writepoll(spectool_server *sr, char *errstr);
138 spectool_phy *spectool_netcli_enabledev(spectool_server *sr, unsigned int dev_id,
139 									 char *errstr);
140 int spectool_netcli_disabledev(spectool_server *sr, spectool_phy *dev);
141 
142 /* Initialize a broadcast listening socket, retval is the socket */
143 int spectool_netcli_initbroadcast(short int port, char *errstr);
144 /* Poll a listening socket, and return a host URL if we found one,
145  * expected to fit spectool_url_max */
146 int spectool_netcli_pollbroadcast(int sock, char *ret_url, char *errstr);
147 
148 /* Return mask from poll */
149 #define SPECTOOL_NETCLI_POLL_NONE			0
150 /* new devices have been detected */
151 #define SPECTOOL_NETCLI_POLL_NEWDEVS		1
152 /* additional data has been read, poll should be called again */
153 #define SPECTOOL_NETCLI_POLL_ADDITIONAL		2
154 /* sweep data has been read, devices should be checked */
155 #define SPECTOOL_NETCLI_POLL_NEWSWEEPS		4
156 
157 /* Parsers */
158 int spectool_netcli_block_netdev(spectool_server *sr, spectool_fr_header *header,
159 								 char *errstr);
160 int spectool_netcli_block_sweep(spectool_server *sr, spectool_fr_header *header,
161 								char *errstr);
162 /* Block management */
163 int spectool_netcli_append(spectool_server *sr, uint8_t *data,
164 						   int len, char *errstr);
165 
166 /* Phydev hooks */
167 void spectool_net_setcalibration(spectool_phy *phydev, int in_calib);
168 int spectool_net_poll(spectool_phy *phydev);
169 int spectool_net_getpollfd(spectool_phy *phydev);
170 int spectool_net_open(spectool_phy *phydev);
171 int spectool_net_close(spectool_phy *phydev);
172 spectool_sample_sweep *spectool_net_getsweep(spectool_phy *phydev);
173 int spectool_net_setposition(spectool_phy *phydev, int profilenum, int start_khz,
174 							 int res_hz);
175 spectool_sample_sweep *spectool_net_getsweep(spectool_phy *phydev);
176 
177 #endif
178 
179