2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal.onvif;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.math.BigDecimal;
19 import java.net.HttpURLConnection;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.net.MalformedURLException;
23 import java.net.NetworkInterface;
24 import java.net.SocketException;
26 import java.net.UnknownHostException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.List;
31 import java.util.UUID;
32 import java.util.concurrent.TimeUnit;
34 import org.eclipse.jdt.annotation.NonNullByDefault;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.openhab.binding.ipcamera.internal.Helper;
37 import org.openhab.binding.ipcamera.internal.IpCameraDiscoveryService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import io.netty.bootstrap.Bootstrap;
42 import io.netty.buffer.ByteBuf;
43 import io.netty.buffer.Unpooled;
44 import io.netty.channel.ChannelFactory;
45 import io.netty.channel.ChannelHandlerContext;
46 import io.netty.channel.ChannelOption;
47 import io.netty.channel.SimpleChannelInboundHandler;
48 import io.netty.channel.group.ChannelGroup;
49 import io.netty.channel.group.DefaultChannelGroup;
50 import io.netty.channel.nio.NioEventLoopGroup;
51 import io.netty.channel.socket.DatagramChannel;
52 import io.netty.channel.socket.DatagramPacket;
53 import io.netty.channel.socket.InternetProtocolFamily;
54 import io.netty.channel.socket.nio.NioDatagramChannel;
55 import io.netty.util.CharsetUtil;
56 import io.netty.util.concurrent.GlobalEventExecutor;
59 * The {@link OnvifDiscovery} is responsible for finding cameras that are ONVIF using UDP multicast.
61 * @author Matthew Skinner - Initial contribution
65 public class OnvifDiscovery {
66 private IpCameraDiscoveryService ipCameraDiscoveryService;
67 private final Logger logger = LoggerFactory.getLogger(OnvifDiscovery.class);
68 public ArrayList<DatagramPacket> listOfReplys = new ArrayList<DatagramPacket>(10);
70 public OnvifDiscovery(IpCameraDiscoveryService ipCameraDiscoveryService) {
71 this.ipCameraDiscoveryService = ipCameraDiscoveryService;
74 public @Nullable List<NetworkInterface> getLocalNICs() {
75 List<NetworkInterface> results = new ArrayList<>(2);
77 for (Enumeration<NetworkInterface> enumNetworks = NetworkInterface.getNetworkInterfaces(); enumNetworks
78 .hasMoreElements();) {
79 NetworkInterface networkInterface = enumNetworks.nextElement();
80 for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr
81 .hasMoreElements();) {
82 InetAddress inetAddress = enumIpAddr.nextElement();
83 if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().toString().length() < 18
84 && inetAddress.isSiteLocalAddress()) {
85 results.add(networkInterface);
89 } catch (SocketException ex) {
94 void searchReply(String url, String xml) {
95 String ipAddress = "";
97 BigDecimal onvifPort = new BigDecimal(80);
99 logger.info("Camera found at xAddr:{}", url);
100 int endIndex = temp.indexOf(" ");// Some xAddr have two urls with a space in between.
102 temp = temp.substring(0, endIndex);// Use only the first url from now on.
105 int beginIndex = temp.indexOf(":") + 3;// add 3 to ignore the :// after http.
106 int secondIndex = temp.indexOf(":", beginIndex); // find second :
107 endIndex = temp.indexOf("/", beginIndex);
108 if (secondIndex > beginIndex && endIndex > secondIndex) {// http://192.168.0.1:8080/onvif/device_service
109 ipAddress = temp.substring(beginIndex, secondIndex);
110 onvifPort = new BigDecimal(temp.substring(secondIndex + 1, endIndex));
111 } else {// // http://192.168.0.1/onvif/device_service
112 ipAddress = temp.substring(beginIndex, endIndex);
114 String brand = checkForBrand(xml);
115 if ("onvif".equals(brand)) {
117 brand = getBrandFromLoginPage(ipAddress);
118 } catch (IOException e) {
122 ipCameraDiscoveryService.newCameraFound(brand, ipAddress, onvifPort.intValue());
125 void processCameraReplys() {
126 for (DatagramPacket packet : listOfReplys) {
127 String xml = packet.content().toString(CharsetUtil.UTF_8);
128 logger.trace("Device replied to discovery with:{}", xml);
129 String xAddr = Helper.fetchXML(xml, "", "d:XAddrs>");// Foscam <wsdd:XAddrs> and all other brands <d:XAddrs>
130 if (!xAddr.isEmpty()) {
131 searchReply(xAddr, xml);
132 } else if (xml.contains("onvif")) {
133 logger.info("Possible ONVIF camera found at:{}", packet.sender().getHostString());
134 ipCameraDiscoveryService.newCameraFound("onvif", packet.sender().getHostString(), 80);
139 String checkForBrand(String response) {
140 if (response.toLowerCase().contains("amcrest")) {
142 } else if (response.toLowerCase().contains("dahua")) {
144 } else if (response.toLowerCase().contains("foscam")) {
146 } else if (response.toLowerCase().contains("hikvision")) {
148 } else if (response.toLowerCase().contains("instar")) {
150 } else if (response.toLowerCase().contains("doorbird")) {
152 } else if (response.toLowerCase().contains("ipc-")) {
154 } else if (response.toLowerCase().contains("dh-sd")) {
160 public String getBrandFromLoginPage(String hostname) throws IOException {
161 URL url = new URL("http://" + hostname);
162 String brand = "onvif";
163 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
164 connection.setConnectTimeout(1000);
165 connection.setReadTimeout(2000);
166 connection.setInstanceFollowRedirects(true);
167 connection.setRequestMethod("GET");
169 connection.connect();
170 BufferedReader reply = new BufferedReader(new InputStreamReader(connection.getInputStream()));
171 String response = "";
173 while ((temp = reply.readLine()) != null) {
177 logger.trace("Cameras Login page is:{}", response);
178 brand = checkForBrand(response);
179 } catch (MalformedURLException e) {
181 connection.disconnect();
186 private DatagramPacket wsDiscovery() throws UnknownHostException {
187 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><e:Envelope xmlns:e=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:w=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dn=\"http://www.onvif.org/ver10/network/wsdl\"><e:Header><w:MessageID>uuid:"
189 + "</w:MessageID><w:To e:mustUnderstand=\"true\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To><w:Action a:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action></e:Header><e:Body><d:Probe><d:Types xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dp0=\"http://www.onvif.org/ver10/network/wsdl\">dp0:NetworkVideoTransmitter</d:Types></d:Probe></e:Body></e:Envelope>";
190 ByteBuf discoveryProbeMessage = Unpooled.copiedBuffer(xml, 0, xml.length(), StandardCharsets.UTF_8);
191 return new DatagramPacket(discoveryProbeMessage,
192 new InetSocketAddress(InetAddress.getByName("239.255.255.250"), 3702), new InetSocketAddress(0));
195 public void discoverCameras() throws UnknownHostException, InterruptedException {
196 List<NetworkInterface> nics = getLocalNICs();
197 if (nics == null || nics.isEmpty()) {
200 NetworkInterface networkInterface = nics.get(0);
201 Bootstrap bootstrap = new Bootstrap().group(new NioEventLoopGroup())
202 .channelFactory(new ChannelFactory<NioDatagramChannel>() {
204 public NioDatagramChannel newChannel() {
205 return new NioDatagramChannel(InternetProtocolFamily.IPv4);
207 }).handler(new SimpleChannelInboundHandler<DatagramPacket>() {
209 protected void channelRead0(@Nullable ChannelHandlerContext ctx, DatagramPacket msg)
212 listOfReplys.add(msg);
214 }).option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true)
215 .option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, false).option(ChannelOption.SO_RCVBUF, 2048)
216 .option(ChannelOption.IP_MULTICAST_TTL, 255).option(ChannelOption.IP_MULTICAST_IF, networkInterface);
217 ChannelGroup openChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
218 for (NetworkInterface nic : nics) {
219 DatagramChannel datagramChannel = (DatagramChannel) bootstrap.option(ChannelOption.IP_MULTICAST_IF, nic)
220 .bind(new InetSocketAddress(0)).sync().channel();
222 .joinGroup(new InetSocketAddress(InetAddress.getByName("239.255.255.250"), 3702), networkInterface)
224 openChannels.add(datagramChannel);
226 if (!openChannels.isEmpty()) {
227 openChannels.writeAndFlush(wsDiscovery());
228 TimeUnit.SECONDS.sleep(6);
229 openChannels.close();
230 processCameraReplys();
231 bootstrap.config().group().shutdownGracefully();