2 * flash-manager - Tizen kernel-level image flashing solution
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
23 #include <sys/types.h>
25 #include <arpa/inet.h>
29 #include "interface.h"
31 #define DEFAULT_PORT 23456
33 static ssize_t net_rx_data(int fd, void *buf, ssize_t len)
35 return recv(fd, buf, len, MSG_WAITALL);
38 static ssize_t net_tx_data(int fd, void *buf, ssize_t len)
40 return send(fd, buf, len, 0);
43 static int net_connect(struct tfm_interface *intf)
45 struct sockaddr_in servaddr;
46 int listener, ret = 0;
49 listener = socket(AF_INET, SOCK_STREAM, 0);
51 fprintf(stderr, "Failed to create socket\n");
55 port = (unsigned long) intf->priv;
59 fprintf(stdout, "%s using port: %d\n", intf->name, port);
61 memset(&servaddr, 0, sizeof(servaddr));
62 servaddr.sin_family = AF_INET;
63 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
64 servaddr.sin_port = htons(port);
66 ret = bind(listener, (struct sockaddr *) &servaddr, sizeof(servaddr));
68 fprintf(stderr, "Failed to bind socket\n");
72 ret = listen(listener, 1024);
74 fprintf(stderr, "Failed to call listen\n");
78 ret = accept(listener, NULL, NULL);
80 fprintf(stderr, "Failed to accept connection\n");
84 intf->rxd = intf->txd = ret;
95 static int net_disconnect(struct tfm_interface *intf)
97 /* since rxd == txd, we close only rxd */
98 return close(intf->rxd);
101 static int net_available(void)
103 struct ifaddrs *ifs, *cur;
106 ret = getifaddrs(&ifs);
112 * On RPI-family targets it works well checking network availabilty
113 * via presence of NIC 'eth0' or 'wlan0', but it looks quite hack
114 * anyway. So, it should be changed if there is more generic way.
116 for (cur = ifs; cur != NULL; cur = cur->ifa_next) {
117 if (!strcmp(cur->ifa_name, "eth0"))
120 if (!strcmp(cur->ifa_name, "wlan0"))
127 static struct tfm_interface_driver network = {
130 .connect = net_connect,
131 .disconnect = net_disconnect,
132 .rx_data = net_rx_data,
133 .tx_data = net_tx_data,
134 .available = net_available,
137 INTERFACE_REGISTER(network)