Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5263b1d75550a176c5b47bd176ce38c4ee7b411c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*******************************************************************************
* Copyright (c) 2014-2018 Orange.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    BAREAU Cyrille <cyrille.bareau@orange.com>
*    BONNARDEL Gregory <gbonnardel.ext@orange.com>
*    BORAWSKI Pawel <pawel.borawski@orange.com>
*    RATUSZEK Przemyslaw <przemyslaw.ratuszek@orange.com>
*    WIERZBOWSKI Jacek <jacek.wierzbowski@orange.com>
*******************************************************************************/
package org.eclipse.om2m.hue.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Deactivate;

/**
 * Implementation class of the OSGi component HueBasedriver. Registers
 * discovered Hue Bridge and Lights as OSGi services.
 */
public class HueBasedriver implements ManagedService {

	// ------------------------------------------------------------------------

	/** Logger */
	private static Log Logger = LogFactory.getLog(HueBasedriver.class);

	/*
	 * SSDP port number
	 */
	private static final int SSDP_PORT = 1900;

	private static final int MTIMER_PERIOD = 3 * 60 * 1000;

	/*
	 * Broadcast address for Upnp. Defined by normalization.
	 */
	private static final String UPNP_BROADCAST_IP = "239.255.255.250";

	/*
	 * UDP packet size
	 */
	private static final int UDP_PACKET_SIZE = 1024;

	/**
	 * ENABLE_SSDP_DISCOVERY is a boolean configAdmin property. If true, the hue
	 * base driver must perform SSDP discovery in order to discover the Hue
	 * Bridge(s).
	 */
	private static final String ENABLE_SSDP_DISCOVERY = "enable.ssdp.discovery";

	/**
	 * HUE_BRIDGE_DESCRIPTION_URL
	 */
	private static final String HUE_BRIDGE_DESCRIPTION_URL = "hue.bridge.description.url";

	/**
	 * HUE_BRIDGE_USER_ID
	 */
	private static final String HUE_BRIDGE_USER_ID = "hue.bridge.userid";
	
	private static final String SERVICE_ID = "service.pid";

	// ------------------------------------------------------------------------

	/*
	 * Timer for M-SEARCH
	 */
	private Timer mSearchTimer = new Timer();

	/*
	 * M-SEARCH thread
	 */
	private Thread mSearchThread;

	/*
	 * ALIVE thread
	 */
	private Thread aliveThread;
	
	/**
	 * Thread for registration process handling
	 */
	private Thread bridgeRegistrationThread;

	// ------------------------------------------------------------------------

	private MulticastSocket mSearchSocket;
	private MulticastSocket aliveSocket;

	private boolean mSearchInitialized;
	private boolean aliveInitialized;
	private boolean mSearchActivated;
	private boolean newMSearchRequired;

	private List<NetworkInterface> networkInterfaces;

	private HueBridgeDiscovery discovery;

	// ------------------------------------------------------------------------

	// ------------------------------------------------------------------------

	/**
	 * Constructor
	 * 
	 */
	public HueBasedriver() {
		networkInterfaces = new ArrayList<NetworkInterface>();
		mSearchInitialized = false;
		aliveInitialized = false;
		mSearchActivated = false;
		newMSearchRequired = true;
		Logger.info("HueBasedriver ctor");
	}

	// ------------------------------------------------------------------------
	// Declarative Service injection methods
	// ------------------------------------------------------------------------

	/**
	 * Activate method from Declarative Service
	 */
	@Activate
	protected void activate(final ComponentContext context) {
		Logger.info("Activating");
//		modified(properties);
		discovery = new HueBridgeDiscovery(context.getBundleContext());
		// initMSearchTimer();
		// discovery.registerBridge(new FakeBridge("xml"));
	}

	/**
	 * Deactivate method from Declarative Service
	 */
	@Deactivate
	protected void deactivate(ComponentContext cc) {
		Logger.info("Deactivating");
		closeSockets();
	}

	private void initMSearchTimer() {
		mSearchTimer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				if (mSearchInitialized) {
					newMSearchRequired = true;
					mSearchSocket.close();
				} else {
					mSearchActivated = true;
					searchHueBridge();
				}

				if (!aliveInitialized) {
					listenAliveHueBridge();
				}
			}
		}, 10, MTIMER_PERIOD); // send first M-SEARCH now
		// + new M-SEARCH every 3 min
	}

	private void cancelMSearchTimer() {
		mSearchTimer.cancel();
	}

	private void closeSockets() {
		if (discovery != null) {
			discovery.stopBackgroundProcesses();
		}
		if (bridgeRegistrationThread != null) {
			bridgeRegistrationThread.interrupt();
		}
		if (mSearchThread != null) {
			mSearchThread.interrupt();
		}
		if (aliveThread != null) {
			aliveThread.interrupt();
		}
		mSearchActivated = false;
		if (mSearchSocket != null) {
			mSearchSocket.close();
		}
		if (aliveSocket != null) {
			aliveSocket.close();
		}
	}

	// ------------------------------------------------------------------------
	// Private utility methods
	// ------------------------------------------------------------------------

	private void initSearch() {
		try {
			mSearchSocket = new MulticastSocket();
			mSearchSocket.setReuseAddress(true);
			// Keep a backlog of incoming datagrams if we are not fast enough
			mSearchSocket.setReceiveBufferSize(32768);

			InetSocketAddress multicastAddress = new InetSocketAddress(UPNP_BROADCAST_IP, SSDP_PORT);

			// discover network interface
			findNetworkInterfaces();

			for (NetworkInterface itf : networkInterfaces) {
				try {
					mSearchSocket.joinGroup(multicastAddress, itf);
				} catch (Exception ignored) {
				}
			}
		} catch (IOException e) {
			Logger.warn("Init Search IOException : " + e.getMessage());
		}
	}

	private void initAlive() {
		try {
			aliveSocket = new MulticastSocket(SSDP_PORT);
			aliveSocket.setReuseAddress(true);
			// Keep a backlog of incoming datagrams if we are not fast enough
			aliveSocket.setReceiveBufferSize(32768);

			InetSocketAddress multicastAddress = new InetSocketAddress(UPNP_BROADCAST_IP, SSDP_PORT);

			// discover network interface
			findNetworkInterfaces();

			for (NetworkInterface itf : networkInterfaces) {
				try {
					aliveSocket.joinGroup(multicastAddress, itf);
				} catch (Exception ignored) {
				}
			}
		} catch (IOException e) {
			Logger.warn("Init Alive IOException : " + e.getMessage());
		}
	}

	private void searchHueBridge() {
		mSearchThread = new Thread() {
			public void run() {
				while (mSearchActivated) {
					initSearch();

					mSearchInitialized = true;

					// -------------------------------------------------------
					// send M-SEARCh message
					discovery.clearDescriptions();
					sendMSearchMessage();
					newMSearchRequired = false;

					// -------------------------------------------------------
					// waiting for responses
					byte[] data = new byte[UDP_PACKET_SIZE];
					while (!newMSearchRequired) {
						DatagramPacket receivePacket = new DatagramPacket(data, data.length);
						try {
							mSearchSocket.receive(receivePacket);
						} catch (IOException e) {
							// socket closed to interrupt receive blocking and launch a new M-SEARCH
							break;
						}
						lookingForHueBridge(receivePacket);
					}
				}
			}
		};
		mSearchThread.start();
	}

	private void listenAliveHueBridge() {
		aliveThread = new Thread() {
			public void run() {
				initAlive();
				aliveInitialized = true;
				byte[] data = new byte[UDP_PACKET_SIZE];

				while (true) {
					DatagramPacket receivePacket = new DatagramPacket(data, data.length);
					try {
						aliveSocket.receive(receivePacket);
					} catch (IOException e) {
						// socket closed to interrupt receive blocking and launch a new M-SEARCH
					}
					lookingForHueBridge(receivePacket);
				}
			}
		};
		aliveThread.start();
	}

	private void lookingForHueBridge(final DatagramPacket receivePacket) {
		String message = new String(receivePacket.getData());
		try {
			String tag = "location:";
			int startIndex = message.toLowerCase().indexOf(tag) + tag.length();
			if (startIndex >= tag.length()) {
				int endIndex = message.indexOf("\r\n", startIndex);
				String location = message.substring(startIndex, endIndex).trim();
				discovery.processNewHueBridge(location);
			}
		} catch (IOException e) {
			Logger.warn("Looking for Hue Bridge IOException : ", e);
		}
	}

	@SuppressWarnings("rawtypes")
	private void findNetworkInterfaces() {
		try {
			for (Enumeration itfs = NetworkInterface.getNetworkInterfaces(); itfs.hasMoreElements();) {
				NetworkInterface itf = (NetworkInterface) itfs.nextElement();
				if (isUsableNetworkInterface(itf)) {
					synchronized (networkInterfaces) {
						networkInterfaces.add(itf);
					}
				}
			}
		} catch (Exception ignored) {
		}
	}

	private List<InetAddress> getInetAddresses(final NetworkInterface networkInterface) {
		return Collections.list(networkInterface.getInetAddresses());
	}

	private boolean isUsableNetworkInterface(final NetworkInterface iface) throws Exception {
		if (getInetAddresses(iface).isEmpty()) {
			return false;
		}
		if ((iface.getDisplayName() != null)
				&& (iface.getDisplayName().toLowerCase(Locale.ENGLISH).indexOf("vmnet") >= 0)) {
			return false;
		}
		String lower = iface.getName().toLowerCase(Locale.ENGLISH);
		if (lower.startsWith("vmnet") || lower.startsWith("vnic") || lower.startsWith("ppp") 
				|| lower.startsWith("lo") || (lower.indexOf("virtual") != -1)) {
			return false;
		}
		return true;
	}

	protected boolean isUsableAddress(final NetworkInterface networkInterface, 
			final InetAddress address) {
		return (address instanceof Inet4Address) || address.isLoopbackAddress();
	}

	private void sendMSearchMessage() {
		StringBuffer msearch = new StringBuffer();
		msearch.append("M-SEARCH * HTTP/1.1\r\n");
		msearch.append("Man: \"ssdp:discover\"\r\n");
		msearch.append("Mx: 3\r\n");
		msearch.append("Host: " + UPNP_BROADCAST_IP + ":" + SSDP_PORT + "\r\n");
		msearch.append("St: ssdp:all\r\n");
		msearch.append("\r\n");

		try {
			InetAddress group = InetAddress.getByName(UPNP_BROADCAST_IP);
			int msearch_length = msearch.length();
			byte[] msearch_bytes = msearch.toString().getBytes();
			DatagramPacket packet = new DatagramPacket(msearch_bytes, msearch_length, group, SSDP_PORT);
			mSearchSocket.send(packet);
		} catch (SocketException e) {
			Logger.warn("M-SEARCH SocketException : ", e);
		} catch (IOException e) {
			Logger.warn("M-SEARCH IOException : ", e);
		} catch (Exception e) {
			Logger.warn("M-SEARCH Exception : ", e);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void updated(final Dictionary properties) throws ConfigurationException {
		closeSockets();
		Logger.info("Updating Hue Bridge connection upon the config file");
		try {
			cancelMSearchTimer();
		} catch (Exception e) {
		}

		if (properties == null) {
			// enable SSDP discovery
			Logger.info("No found properties... Try SSDP discovery");
			initMSearchTimer();
		} else {
			try {
				if (Boolean.valueOf(properties.get(ENABLE_SSDP_DISCOVERY).toString())) {
					initMSearchTimer();
					return;
				}
			} catch (Exception ignored) {
			}
			final String descriptionUrl = (String) properties.get(HUE_BRIDGE_DESCRIPTION_URL);
			final String userId = (String) properties.get(HUE_BRIDGE_USER_ID);
			Logger.info("Config file description url: " + descriptionUrl 
					+ ", config file user id: " + userId);
			if (userId == null || userId == "") {
				Logger.warn("No userId for Hue Bridge update...");
			}
			if (descriptionUrl == null) {
				Logger.warn("No description...");
			} else {
				bridgeRegistrationThread = new Thread() {
					public void run() {
						try {
							String newUserId = discovery.processHueBridge(descriptionUrl, userId);
							if (newUserId != null && !newUserId.equals(userId)) {
								updatePropertiesFile(properties.get(SERVICE_ID) + ".properties",
										HUE_BRIDGE_USER_ID, newUserId);
							}
						} catch (IOException e) {
							Logger.warn("Error processing new bridge", e);
						}
					}
				};
				bridgeRegistrationThread.run();
			}	
		}
	}
	
	/**
	 * Set property and save in (config) file
	 * 
	 * @param filename: name of config file from configurations/services/ directory 
	 * @param key: name of the property to update
	 * @param value: value to update
	 */
	protected void updatePropertiesFile(String filename, String key, String value) {
        try {
            File file = new File("configurations/services/" + filename);
            Properties properties = new Properties();
            FileReader fr = new FileReader(file);
            properties.load(fr);
            
            Logger.info("Updating the: " + filename + " file, to save the: " + key + " property");
            properties.setProperty(key, value);
            properties.store(new FileOutputStream(file), "");
            
        }
        catch (IOException e) {
            Logger.error("Error while updating the: " + filename + " file, to save the: " + key + " property", e);
        }
    }

}

Back to the top