1 //-----------------------------------------------------------------------------
2 //
3 //	NodeNaming.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_NODE_NAMING
6 //
7 //	Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
8 //
9 //	SOFTWARE NOTICE AND LICENSE
10 //
11 //	This file is part of OpenZWave.
12 //
13 //	OpenZWave is free software: you can redistribute it and/or modify
14 //	it under the terms of the GNU Lesser General Public License as published
15 //	by the Free Software Foundation, either version 3 of the License,
16 //	or (at your option) any later version.
17 //
18 //	OpenZWave is distributed in the hope that it will be useful,
19 //	but WITHOUT ANY WARRANTY; without even the implied warranty of
20 //	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 //	GNU Lesser General Public License for more details.
22 //
23 //	You should have received a copy of the GNU Lesser General Public License
24 //	along with OpenZWave.  If not, see <http://www.gnu.org/licenses/>.
25 //
26 //-----------------------------------------------------------------------------
27 
28 #include "command_classes/CommandClasses.h"
29 #include "command_classes/NodeNaming.h"
30 #include "command_classes/Association.h"
31 #include "Defs.h"
32 #include "Msg.h"
33 #include "Node.h"
34 #include "Driver.h"
35 #include "Notification.h"
36 #include "platform/Log.h"
37 
38 using namespace OpenZWave;
39 
40 enum NodeNamingCmd
41 {
42 	NodeNamingCmd_Set				= 0x01,
43 	NodeNamingCmd_Get				= 0x02,
44 	NodeNamingCmd_Report			= 0x03,
45 	NodeNamingCmd_LocationSet		= 0x04,
46 	NodeNamingCmd_LocationGet		= 0x05,
47 	NodeNamingCmd_LocationReport	= 0x06
48 };
49 
50 enum StringEncoding
51 {
52 	StringEncoding_ASCII = 0,
53 	StringEncoding_ExtendedASCII,
54 	StringEncoding_UTF16
55 };
56 
57 // Mapping of characters 0x80 and above to Unicode.
58 uint16 const c_extendedAsciiToUnicode[] =
59 {
60 	0x20ac,	    // 0x80 - Euro Sign
61 	0x0081,		// 0x81 -
62 	0x201a,	    // 0x82 - Single Low-9 Quotation Mark
63 	0x0192,	    // 0x83 - Latin Small Letter F With Hook
64 	0x201e,	    // 0x84 - Double Low-9 Quotation Mark
65 	0x2026,	    // 0x85 - Horizontal Ellipsis
66 	0x2020,	    // 0x86 - Dagger
67 	0x2021,	    // 0x87 - Double Dagger
68 	0x02c6,	    // 0x88 - Modifier Letter Circumflex Accent
69 	0x2030,	    // 0x89 - Per Mille Sign
70 	0x0160,	    // 0x8a - Latin Capital Letter S With Caron
71 	0x2039,	    // 0x8b - Single Left-Pointing Angle Quotation Mark
72 	0x0152,	    // 0x8c - Latin Capital Ligature Oe
73 	0x008d,		// 0x8d -
74 	0x017d,	    // 0x8e - Latin Capital Letter Z With Caron
75 	0x008f,		// 0x8f -
76 
77 	0x0090,		// 0x90 -
78 	0x2018,	    // 0x91 - Left Single Quotation Mark
79 	0x2019,	    // 0x92 - Right Single Quotation Mark
80 	0x201c,	    // 0x93 - Left Double Quotation Mark
81 	0x201d,	    // 0x94 - Right Double Quotation Mark
82 	0x2022,	    // 0x95 - Bullet
83 	0x2013,	    // 0x96 - En Dash
84 	0x2014,	    // 0x97 - Em Dash
85 	0x02dc,	    // 0x98 - Small Tilde
86 	0x2122,	    // 0x99 - Trade Mark Sign
87 	0x0161,	    // 0x9a - Latin Small Letter S With Caron
88 	0x203a,	    // 0x9b - Single Right-Pointing Angle Quotation Mark
89 	0x0153,	    // 0x9c - Latin Small Ligature Oe
90 	0x009d,		// 0x9d -
91 	0x017e,	    // 0x9e - Latin Small Letter Z With Caron
92 	0x0178,	    // 0x9f - Latin Capital Letter Y With Diaeresis
93 
94 	0x00a0,	    // 0xa0 - No-Break Space
95 	0x00a1,	    // 0xa1 - Inverted Exclamation Mark
96 	0x00a2,	    // 0xa2 - Cent Sign
97 	0x00a3,	    // 0xa3 - Pound Sign
98 	0x00a4,	    // 0xa4 - Currency Sign
99 	0x00a5,	    // 0xa5 - Yen Sign
100 	0x00a6,	    // 0xa6 - Broken Bar
101 	0x00a7,	    // 0xa7 - Section Sign
102 	0x00a8,	    // 0xa8 - Diaeresis
103 	0x00a9,	    // 0xa9 - Copyright Sign
104 	0x00aa,	    // 0xaa - Feminine Ordinal Indicator
105 	0x00ab,	    // 0xab - Left-Pointing Double Angle Quotation Mark
106 	0x00ac,	    // 0xac - Not Sign
107 	0x00ad,	    // 0xad - Soft Hyphen
108 	0x00ae,	    // 0xae - Registered Sign
109 	0x00af,	    // 0xaf - Macron
110 
111 	0x00b0,	    // 0xb0 - Degree Sign
112 	0x00b1,	    // 0xb1 - Plus-Minus Sign
113 	0x00b2,	    // 0xb2 - Superscript Two
114 	0x00b3,	    // 0xb3 - Superscript Three
115 	0x00b4,	    // 0xb4 - Acute Accent
116 	0x00b5,	    // 0xb5 - Micro Sign
117 	0x00b6,	    // 0xb6 - Pilcrow Sign
118 	0x00b7,	    // 0xb7 - Middle Dot
119 	0x00b8,	    // 0xb8 - Cedilla
120 	0x00b9,	    // 0xb9 - Superscript One
121 	0x00ba,	    // 0xba - Masculine Ordinal Indicator
122 	0x00bb,	    // 0xbb - Right-Pointing Double Angle Quotation Mark
123 	0x00bc,	    // 0xbc - Vulgar Fraction One Quarter
124 	0x00bd,	    // 0xbd - Vulgar Fraction One Half
125 	0x00be,	    // 0xbe - Vulgar Fraction Three Quarters
126 	0x00bf,	    // 0xbf - Inverted Question Mark
127 
128 	0x00c0,	    // 0xc0 - Latin Capital Letter A With Grave
129 	0x00c1,	    // 0xc1 - Latin Capital Letter A With Acute
130 	0x00c2,	    // 0xc2 - Latin Capital Letter A With Circumflex
131 	0x00c3,	    // 0xc3 - Latin Capital Letter A With Tilde
132 	0x00c4,	    // 0xc4 - Latin Capital Letter A With Diaeresis
133 	0x00c5,	    // 0xc5 - Latin Capital Letter A With Ring Above
134 	0x00c6,	    // 0xc6 - Latin Capital Ligature Ae
135 	0x00c7,	    // 0xc7 - Latin Capital Letter C With Cedilla
136 	0x00c8,	    // 0xc8 - Latin Capital Letter E With Grave
137 	0x00c9,	    // 0xc9 - Latin Capital Letter E With Acute
138 	0x00ca,	    // 0xca - Latin Capital Letter E With Circumflex
139 	0x00cb,	    // 0xcb - Latin Capital Letter E With Diaeresis
140 	0x00cc,	    // 0xcc - Latin Capital Letter I With Grave
141 	0x00cd,	    // 0xcd - Latin Capital Letter I With Acute
142 	0x00ce,	    // 0xce - Latin Capital Letter I With Circumflex
143 	0x00cf,	    // 0xcf - Latin Capital Letter I With Diaeresis
144 
145 	0x00d0,	    // 0xd0 - Latin Capital Letter Eth
146 	0x00d1,	    // 0xd1 - Latin Capital Letter N With Tilde
147 	0x00d2,	    // 0xd2 - Latin Capital Letter O With Grave
148 	0x00d3,	    // 0xd3 - Latin Capital Letter O With Acute
149 	0x00d4,	    // 0xd4 - Latin Capital Letter O With Circumflex
150 	0x00d5,	    // 0xd5 - Latin Capital Letter O With Tilde
151 	0x00d6,	    // 0xd6 - Latin Capital Letter O With Diaeresis
152 	0x00d7,	    // 0xd7 - Multiplication Sign
153 	0x00d8,	    // 0xd8 - Latin Capital Letter O With Stroke
154 	0x00d9,	    // 0xd9 - Latin Capital Letter U With Grave
155 	0x00da,	    // 0xda - Latin Capital Letter U With Acute
156 	0x00db,	    // 0xdb - Latin Capital Letter U With Circumflex
157 	0x00dc,	    // 0xdc - Latin Capital Letter U With Diaeresis
158 	0x00dd,	    // 0xdd - Latin Capital Letter Y With Acute
159 	0x00de,	    // 0xde - Latin Capital Letter Thorn
160 	0x00df,	    // 0xdf - Latin Small Letter Sharp S
161 
162 	0x00e0,	    // 0xe0 - Latin Small Letter A With Grave
163 	0x00e1,	    // 0xe1 - Latin Small Letter A With Acute
164 	0x00e2,	    // 0xe2 - Latin Small Letter A With Circumflex
165 	0x00e3,	    // 0xe3 - Latin Small Letter A With Tilde
166 	0x00e4,	    // 0xe4 - Latin Small Letter A With Diaeresis
167 	0x00e5,	    // 0xe5 - Latin Small Letter A With Ring Above
168 	0x00e6,	    // 0xe6 - Latin Small Ligature Ae
169 	0x00e7,	    // 0xe7 - Latin Small Letter C With Cedilla
170 	0x00e8,	    // 0xe8 - Latin Small Letter E With Grave
171 	0x00e9,	    // 0xe9 - Latin Small Letter E With Acute
172 	0x00ea,	    // 0xea - Latin Small Letter E With Circumflex
173 	0x00eb,	    // 0xeb - Latin Small Letter E With Diaeresis
174 	0x00ec,	    // 0xec - Latin Small Letter I With Grave
175 	0x00ed,	    // 0xed - Latin Small Letter I With Acute
176 	0x00ee,	    // 0xee - Latin Small Letter I With Circumflex
177 	0x00ef,	    // 0xef - Latin Small Letter I With Diaeresis
178 
179 	0x00f0,	    // 0xf0 - Latin Small Letter Eth
180 	0x00f1,	    // 0xf1 - Latin Small Letter N With Tilde
181 	0x00f2,	    // 0xf2 - Latin Small Letter O With Grave
182 	0x00f3,	    // 0xf3 - Latin Small Letter O With Acute
183 	0x00f4,	    // 0xf4 - Latin Small Letter O With Circumflex
184 	0x00f5,	    // 0xf5 - Latin Small Letter O With Tilde
185 	0x00f6,	    // 0xf6 - Latin Small Letter O With Diaeresis
186 	0x00f7,	    // 0xf7 - Division Sign
187 	0x00f8,	    // 0xf8 - Latin Small Letter O With Stroke
188 	0x00f9,	    // 0xf9 - Latin Small Letter U With Grave
189 	0x00fa,	    // 0xfa - Latin Small Letter U With Acute
190 	0x00fb,	    // 0xfb - Latin Small Letter U With Circumflex
191 	0x00fc,	    // 0xfc - Latin Small Letter U With Diaeresis
192 	0x00fd,	    // 0xfd - Latin Small Letter Y With Acute
193 	0x00fe,	    // 0xfe - Latin Small Letter Thorn
194 	0x00ff	    // 0xff - Latin Small Letter Y With Diaeresis
195 };
196 
197 //-----------------------------------------------------------------------------
198 // <NodeNaming::RequestState>
199 // Request current state from the device
200 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)201 bool NodeNaming::RequestState
202 (
203 	uint32 const _requestFlags,
204 	uint8 const _instance,
205 	Driver::MsgQueue const _queue
206 )
207 {
208 	bool res = false;
209 	if( _requestFlags & RequestFlag_Session )
210 	{
211 		if( Node* node = GetNodeUnsafe() )
212 		{
213 			if( node->m_nodeName == "" )
214 			{
215 				// If we don't already have a user-defined name, fetch it from the device
216 				res |= RequestValue( _requestFlags, NodeNamingCmd_Get, _instance, _queue );
217 			}
218 
219 			if( node->m_location == "" )
220 			{
221 				// If we don't already have a user-defined location, fetch it from the device
222 				res |= RequestValue( _requestFlags, NodeNamingCmd_LocationGet, _instance, _queue );
223 			}
224 		}
225 	}
226 
227 	return res;
228 }
229 
230 //-----------------------------------------------------------------------------
231 // <NodeNaming::RequestValue>
232 // Request current value from the device
233 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _getTypeEnum,uint8 const _instance,Driver::MsgQueue const _queue)234 bool NodeNaming::RequestValue
235 (
236 	uint32 const _requestFlags,
237 	uint8 const _getTypeEnum,
238 	uint8 const _instance,
239 	Driver::MsgQueue const _queue
240 )
241 {
242 	if( _instance != 1 )
243 	{
244 		// This command class doesn't work with multiple instances
245 		return false;
246 	}
247 
248 	Msg* msg;
249 	if( _getTypeEnum == NodeNamingCmd_Get )
250 	{
251 		if ( IsGetSupported() )
252 		{
253 			msg = new Msg( "NodeNamingCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
254 			msg->Append( GetNodeId() );
255 			msg->Append( 2 );
256 			msg->Append( GetCommandClassId() );
257 			msg->Append( NodeNamingCmd_Get );
258 			msg->Append( GetDriver()->GetTransmitOptions() );
259 			GetDriver()->SendMsg( msg, _queue );
260 			return true;
261 		} else {
262 			Log::Write(  LogLevel_Info, GetNodeId(), "NodeNamingCmd_Get Not Supported on this node");
263 		}
264 		return false;
265 	}
266 
267 	if( _getTypeEnum == NodeNamingCmd_LocationGet )
268 	{
269 		// If we don't already have a user-defined name, fetch it from the device
270 		msg = new Msg( "NodeNamingCmd_LocationGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
271 		msg->Append( GetNodeId() );
272 		msg->Append( 2 );
273 		msg->Append( GetCommandClassId() );
274 		msg->Append( NodeNamingCmd_LocationGet );
275 		msg->Append( GetDriver()->GetTransmitOptions() );
276 		GetDriver()->SendMsg( msg, _queue );
277 		return true;
278 	}
279 	return false;
280 }
281 
282 //-----------------------------------------------------------------------------
283 // <NodeNaming::HandleMsg>
284 // Handle a message from the Z-Wave network
285 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)286 bool NodeNaming::HandleMsg
287 (
288 	uint8 const* _data,
289 	uint32 const _length,
290 	uint32 const _instance	// = 1
291 )
292 {
293 	bool updated = false;
294 	if( Node* node = GetNodeUnsafe() )
295 	{
296 		if( NodeNamingCmd_Report == (NodeNamingCmd)_data[0] )
297 		{
298 			string name = ExtractString( _data, _length );
299 			if( node->m_nodeName == "" )
300 			{
301 				// We only overwrite the name if it is empty
302 				node->m_nodeName = name;
303 				Log::Write( LogLevel_Info, GetNodeId(), "Received the name: %s.", name.c_str() );
304 				updated = true;
305 			}
306 		}
307 		else if( NodeNamingCmd_LocationReport == (NodeNamingCmd)_data[0] )
308 		{
309 			string location = ExtractString( _data, _length );
310 			if( node->m_location == "" )
311 			{
312 				// We only overwrite the location if it is empty
313 				node->m_location = location;
314 				Log::Write( LogLevel_Info, GetNodeId(), "Received the location: %s.", location.c_str() );
315 				updated = true;
316 			}
317 		}
318  	}
319 
320 	if( updated )
321 	{
322 		Notification* notification = new Notification( Notification::Type_NodeNaming );
323 		notification->SetHomeAndNodeIds( GetHomeId(), GetNodeId() );
324 		GetDriver()->QueueNotification( notification );
325 	}
326 
327 	return true;
328 }
329 
330 //-----------------------------------------------------------------------------
331 // <NodeNaming::SetName>
332 // Set the name in the device
333 //-----------------------------------------------------------------------------
SetName(string const & _name)334 void NodeNaming::SetName
335 (
336 	string const& _name
337 )
338 {
339 	size_t length = _name.size();
340 	if( length > 16 )
341 	{
342 		length = 16;
343 	}
344 
345 	Log::Write( LogLevel_Info, GetNodeId(), "NodeNaming::Set - Naming to '%s'", _name.c_str() );
346 	Msg* msg = new Msg( "NodeNamingCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
347 	msg->Append( GetNodeId() );
348 	msg->Append( (uint8)(length + 3) );
349 	msg->Append( GetCommandClassId() );
350 	msg->Append( NodeNamingCmd_Set );
351 	msg->Append( (uint8)StringEncoding_ASCII );
352 
353 	for( uint32 i=0; i<length; ++i )
354 	{
355 		msg->Append( _name[i] );
356 	}
357 
358 	msg->Append( GetDriver()->GetTransmitOptions() );
359 	GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
360 }
361 
362 //-----------------------------------------------------------------------------
363 // <NodeNaming::SetLocation>
364 // Set the location in the device
365 //-----------------------------------------------------------------------------
SetLocation(string const & _location)366 void NodeNaming::SetLocation
367 (
368 	string const& _location
369 )
370 {
371 	size_t length = _location.size();
372 	if( length > 16 )
373 	{
374 		length = 16;
375 	}
376 
377 	Log::Write( LogLevel_Info, GetNodeId(), "NodeNaming::SetLocation - Setting location to '%s'", _location.c_str() );
378 	Msg* msg = new Msg( "NodeNamingCmd_LocationSet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
379 	msg->Append( GetNodeId() );
380 	msg->Append( (uint8)(length + 3) );
381 	msg->Append( GetCommandClassId() );
382 	msg->Append( NodeNamingCmd_LocationSet );
383 	msg->Append( (uint8)StringEncoding_ASCII );
384 
385 	for( uint32 i=0; i<length; ++i )
386 	{
387 		msg->Append( _location[i] );
388 	}
389 
390 	msg->Append( GetDriver()->GetTransmitOptions() );
391 	GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
392 }
393 
394 //-----------------------------------------------------------------------------
395 // <NodeNaming::ExtractString>
396 // Extract a string from the report data
397 //-----------------------------------------------------------------------------
ExtractString(uint8 const * _data,uint32 const _length)398 string NodeNaming::ExtractString
399 (
400 	uint8 const* _data,
401 	uint32 const _length
402 )
403 {
404 	uint8 i;
405 	char str[32];
406 	uint32 pos = 0;
407 
408 	str[0] = 0;
409 	StringEncoding encoding = (StringEncoding)( _data[1] & 0x07 );
410 
411 	if( _length >= 3 )
412 	{
413 		// Get the length of the string (maximum allowed is 16 bytes)
414 		uint8 numBytes = _length - 3;
415 		if( numBytes > 16 )
416 		{
417 			numBytes = 16;
418 		}
419 
420 		switch( encoding )
421 		{
422 			case StringEncoding_ASCII:
423 			{
424 				// Copy data as-is
425 				for( i=0; i<numBytes; ++i )
426 				{
427 					str[pos++] = _data[i+2];
428 				}
429 				break;
430 			}
431 			case StringEncoding_ExtendedASCII:
432 			{
433 				// Convert Extended ASCII characters to UTF-8
434 				for( i=0; i<numBytes; ++i )
435 				{
436 					uint8 ch = _data[i+2];
437 					if( ch >= 0x80 )
438 					{
439 						uint16 utf16 = c_extendedAsciiToUnicode[ch-0x80];
440 						pos = ConvertUFT16ToUTF8( utf16, str, pos );
441 					}
442 					else
443 					{
444 						str[pos++] = (char)ch;
445 					}
446 				}
447 				break;
448 			}
449 			case StringEncoding_UTF16:
450 			{
451 				// Convert UTF-16 characters to UTF-8
452 				for( i=0; i<numBytes; i+=2 )
453 				{
454 					uint16 utf16 = (((uint16)_data[i+2])<<8) | (uint16)_data[i+3];
455 					pos = ConvertUFT16ToUTF8( utf16, str, pos );
456 				}
457 				break;
458 			}
459 			default:
460 			{
461 			}
462 		}
463 
464 		str[pos] = 0;
465 	}
466 
467 	return string( str );
468 }
469 
470 //-----------------------------------------------------------------------------
471 // <NodeNaming::ConvertUFT16ToUTF8>
472 // Convert a UTF-16 string into UTF-8 encoding.
473 //-----------------------------------------------------------------------------
ConvertUFT16ToUTF8(uint16 _utf16,char * _buffer,uint32 pos)474 uint32 NodeNaming::ConvertUFT16ToUTF8
475 (
476 	uint16 _utf16,
477 	char* _buffer,
478 	uint32 pos
479 )
480 {
481 	static uint16 surrogate = 0;
482 
483 	if( ( surrogate != 0 ) && ( ( _utf16 & 0xdc00 ) == 0xdc00 ) )
484 	{
485 		// UTF-16 surrogate character pair converts to four UTF-8 characters.
486 		// We have the second member of the pair, so we can do the conversion now.
487 		_buffer[pos++] = (char)( ((surrogate>>7) & 0x0007) | 0x00f0 );
488 		_buffer[pos++] = (char)( ((surrogate>>1) & 0x0020) | ((surrogate>>2) & 0x000f) | 0x0090 );
489 		_buffer[pos++] = (char)( ((_utf16>>6) & 0x000f) | ((surrogate<<4) & 0x0030) | 0x0080 );
490 		_buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 );
491 	}
492 	else
493 	{
494 		surrogate = 0;
495 		if( _utf16 & 0xff80 )
496 		{
497 			if( _utf16 & 0xf800 )
498 			{
499 				if( ( _utf16 & 0xd800 ) == 0xd800 )
500 				{
501 					// UTF-16 surrogate character pair converts to four UTF-8 characters.
502 					// We have the first member of the pair, so we store it for next time.
503 					surrogate = _utf16;
504 				}
505 				else
506 				{
507 					// UTF-16 character can be represented by three UTF-8 characters.
508 					_buffer[pos++] = (char)( (_utf16>>12) | 0x00e0 );
509 					_buffer[pos++] = (char)( ((_utf16>>6) & 0x003f) | 0x0080 );
510 					_buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 );
511 				}
512 			}
513 			else
514 			{
515 				// UTF-16 character can be represented by two UTF-8 characters.
516 				_buffer[pos++] = (char)( (_utf16>>6) | 0x00c0 );
517 				_buffer[pos++] = (char)( (_utf16 & 0x003f) | 0x0080 );
518 			}
519 		}
520 		else
521 		{
522 			// UTF-16 character matches single ascii character.
523 			_buffer[pos++] = (char)_utf16;
524 		}
525 	}
526 
527 	return pos;
528 }
529 
530 
531 
532