3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <net/socket.h>
27 #define IPS_PROTO_CONNECT "/#ips#c"
28 #define IPS_PROTO_CONNECTED "/#ips#k"
29 #define IPS_PROTO_RECV "/#ips#r"
30 #define IPS_PROTO_EOF "/#ips#e"
31 #define IPS_PROTO_CLOSE "/#ips#q"
32 #define IPS_PROTO_SOCKET "/#ips#s"
33 #define IPS_PROTO_BIND "/#ips#b"
34 #define IPS_PROTO_LISTEN "/#ips#l"
35 #define IPS_PROTO_ACCEPT "/#ips#a"
36 #define IPS_PROTO_FCNTL "/#ips#f"
38 /* Mutex for socket function */
39 MUTEX_CREATE (mutex_ips
);
41 /** Internet Protocol over Serial - IPS **
42 * For easy connection to network
50 char cmd
[] = IPS_PROTO_SOCKET
;
51 ips_send (fd
, cmd
, 7);
53 mutex_lock (&mutex_ips
);
56 num
[0] = rs232_read ();
57 num
[1] = rs232_read ();
58 num
[2] = rs232_read ();
59 num
[3] = rs232_read ();
61 fd
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
63 // printf ("ips_socket (): %d\n", fd);
65 mutex_unlock (&mutex_ips
);
70 int ips_connect (int fd
, sockaddr_in
*addr
)
77 if (!net_proto_ip_convert2 (addr
->sin_addr
, ip
))
80 sprintf (data
, IPS_PROTO_CONNECT
" %s %d", ip
, addr
->sin_port
);
82 ips_send (fd
, data
, strlen (data
));
84 mutex_lock (&mutex_ips
);
86 /* receive return value */
88 num
[0] = rs232_read ();
89 num
[1] = rs232_read ();
90 num
[2] = rs232_read ();
91 num
[3] = rs232_read ();
93 len
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
95 mutex_unlock (&mutex_ips
);
100 int ips_send (int fd
, char *msg
, size_t size
)
104 mutex_lock (&mutex_ips
);
106 rs232_write ((unsigned char) size
);
107 rs232_write ((unsigned char) (size
>> 8));
108 rs232_write ((unsigned char) (size
>> 16));
109 rs232_write ((unsigned char) (size
>> 24));
111 rs232_write ((unsigned char) fd
);
112 rs232_write ((unsigned char) (fd
>> 8));
113 rs232_write ((unsigned char) (fd
>> 16));
114 rs232_write ((unsigned char) (fd
>> 24));
116 while (len
!= size
) {
117 rs232_write (msg
[len
]);
121 // printf ("send (%d): %d, '%s', %d\n", len, fd, msg, size);
123 mutex_unlock (&mutex_ips
);
128 int ips_recv (int fd
, char *msg
, size_t size
)
134 sprintf (data
, IPS_PROTO_RECV
" %d", size
);
135 ips_send (fd
, data
, strlen (data
));
137 mutex_lock (&mutex_ips
);
140 num
[0] = rs232_read ();
141 num
[1] = rs232_read ();
142 num
[2] = rs232_read ();
143 num
[3] = rs232_read ();
145 len
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
148 mutex_unlock (&mutex_ips
);
153 mutex_unlock (&mutex_ips
);
159 msg
[l
] = rs232_read ();
163 mutex_unlock (&mutex_ips
);
168 int ips_bind (int fd
, sockaddr_in
*addr
)
174 sprintf (data
, IPS_PROTO_BIND
" 0 %d", /*addr->sin_addr,*/ addr
->sin_port
);
175 ips_send (fd
, data
, strlen (data
));
177 mutex_lock (&mutex_ips
);
180 num
[0] = rs232_read ();
181 num
[1] = rs232_read ();
182 num
[2] = rs232_read ();
183 num
[3] = rs232_read ();
185 ret
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
187 mutex_unlock (&mutex_ips
);
192 int ips_listen (int fd
, int backlog
)
198 sprintf (data
, IPS_PROTO_LISTEN
" %d", backlog
);
199 ips_send (fd
, data
, strlen (data
));
201 mutex_lock (&mutex_ips
);
204 num
[0] = rs232_read ();
205 num
[1] = rs232_read ();
206 num
[2] = rs232_read ();
207 num
[3] = rs232_read ();
209 ret
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
211 mutex_unlock (&mutex_ips
);
216 int ips_accept (int fd
, sockaddr_in
*addr
, socklen_t
*addrlen
)
220 char cmd
[] = IPS_PROTO_ACCEPT
;
221 ips_send (fd
, cmd
, 7);
223 mutex_lock (&mutex_ips
);
226 num
[0] = rs232_read ();
227 num
[1] = rs232_read ();
228 num
[2] = rs232_read ();
229 num
[3] = rs232_read ();
231 ret
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
233 mutex_unlock (&mutex_ips
);
238 int ips_close (int fd
)
240 // mutex_lock (&mutex_ips);
242 char cmd
[] = IPS_PROTO_CLOSE
;
243 ips_send (fd
, cmd
, 7);
245 // printf ("ips_close (%d)\n", fd);
247 // mutex_unlock (&mutex_ips);
252 int ips_fcntl (int fd
, int cmd
, long arg
)
258 sprintf (data
, IPS_PROTO_FCNTL
" %d %ld", cmd
, arg
);
259 ips_send (fd
, data
, strlen (data
));
261 mutex_lock (&mutex_ips
);
264 num
[0] = rs232_read ();
265 num
[1] = rs232_read ();
266 num
[2] = rs232_read ();
267 num
[3] = rs232_read ();
269 ret
= num
[0] | (num
[1] << 8) | (num
[2] << 16) | (num
[3] << 24);
271 mutex_unlock (&mutex_ips
);