]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeSrpcClient.java
add support for control bridge FW
[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / Zigbee / ZigbeeSrpcClient.java
1 /**************************************************************************************************
2   Filename:       ZigBeeSrpcClient.java
3   Revised:        $$
4   Revision:       $$
6   Description:    ZigBee SRPC Client Class
8     Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions
13    are met:
15      Redistributions of source code must retain the above copyright
16      notice, this list of conditions and the following disclaimer.
18      Redistributions in binary form must reproduce the above copyright
19      notice, this list of conditions and the following disclaimer in the
20      documentation and/or other materials provided with the
21      distribution.
23      Neither the name of Texas Instruments Incorporated nor the names of
24      its contributors may be used to endorse or promote products derived
25      from this software without specific prior written permission.
27    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 **************************************************************************************************/
41 package com.lightingcontroller.Zigbee;
43 import java.io.File;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.OutputStream;
47 import java.math.BigInteger;
48 import java.net.InetSocketAddress;
49 import java.net.Socket;
50 import java.net.SocketAddress;
51 import java.net.UnknownHostException;
52 import java.nio.charset.Charset;
53 import java.util.ArrayList;
54 import java.util.Calendar;
55 import java.util.Date;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Timer;
59 import java.util.TimerTask;
60 import java.util.concurrent.TimeUnit;
62 import com.lightingcontroller.Zigbee.ZigbeeScene;
64 import android.app.Activity;
65 import android.app.ProgressDialog;
66 import android.content.SharedPreferences;
67 import android.content.SharedPreferences.Editor;
69 import android.graphics.Color;
70 import android.os.Bundle;
71 import android.os.Handler;
72 import android.os.Message;
73 import android.os.Parcelable;
74 import android.os.SystemClock;
75 import android.util.Log;
76 import android.widget.Toast;
78 public class ZigbeeSrpcClient {
79         
80         static Thread thread;
81         static Socket kkSocket;
82         static OutputStream outStream;
83         static InputStream inStream;
84         
85         private static String gatewayIp= "192.168.1.111";
86         static public String gatewayPort= "11235"; //"2be3";
88         //SRPC header bit positions
89         private static final int SRPC_CMD_ID_POS = 0;
90         private static final int SRPC_CMD_LEN_POS = 1;
91         
92         //SRPC CMD ID's 
93         //define the outgoing RPSC command ID's
94         private static final byte SRPC_NEW_DEVICE     = (byte) 0x0001;
95         private static final byte SRPC_RESERVED_1                 = (byte) 0x0002;
96         private static final byte SRPC_RESERVED_2             = (byte) 0x0003;
97         private static final byte SRPC_RESERVED_3         = (byte) 0x0004;
98         private static final byte SRPC_RESERVED_4         = (byte) 0x0005;
99         private static final byte SRPC_RESERVED_5         = (byte) 0x0006;
100         private static final byte SRPC_GET_DEV_STATE_RSP  = (byte) 0x0007;
101         private static final byte SRPC_GET_DEV_LEVEL_RSP  = (byte) 0x0008;
102         private static final byte SRPC_GET_DEV_HUE_RSP    = (byte) 0x0009;
103         private static final byte SRPC_GET_DEV_SAT_RSP    = (byte) 0x000a;
104         private static final byte SRPC_ADD_GROUP_RSP      = (byte) 0x000b;
105         private static final byte SRPC_GET_GROUP_RSP      = (byte) 0x000c;
106         private static final byte SRPC_ADD_SCENE_RSP      = (byte) 0x000d;
107         private static final byte SRPC_GET_SCENE_RSP      = (byte) 0x000e;
110         //define incoming SRPC command ID's
111         private static final byte SRPC_CLOSE              = (byte) 0x80;
112         private static final byte SRPC_GET_DEVICES        = (byte) 0x81;
113         private static final byte SRPC_SET_DEV_STATE      = (byte) 0x82;        
114         private static final byte SRPC_SET_DEV_LEVEL      = (byte) 0x83;        
115         private static final byte SRPC_SET_DEV_COLOR      = (byte) 0x84;
116         private static final byte SRPC_GET_DEV_STATE      = (byte) 0x85;        
117         private static final byte SRPC_GET_DEV_LEVEL      = (byte) 0x86;        
118         private static final byte SRPC_GET_DEV_HUE        = (byte) 0x87;
119         private static final byte SRPC_GET_DEV_SAT        = (byte) 0x88;
120         private static final byte SRPC_BIND_DEVICES       = (byte) 0x89;
121         private static final byte SRPC_RESERVED_6                 = (byte) 0x8a;
122         private static final byte SRPC_RESERVED_7         = (byte) 0x8b;
123         private static final byte SRPC_RESERVED_8         = (byte) 0x8c;
124         private static final byte SRPC_RESERVED_9         = (byte) 0x8d;
125         private static final byte SRPC_GET_GROUPS         = (byte) 0x8e;        
126         private static final byte SRPC_ADD_GROUP          = (byte) 0x8f;        
127         private static final byte SRPC_GET_SCENES         = (byte) 0x90;        
128         private static final byte SRPC_STORE_SCENE        = (byte) 0x91;        
129         private static final byte SRPC_RECALL_SCENE       = (byte) 0x92;        
130         private static final byte SRPC_IDENTIFY_DEVICE    = (byte) 0x93;        
131         private static final byte SRPC_CHANGE_DEVICE_NAME = (byte) 0x94;  
132         private static final byte SRPC_REMOVE_DEVICE      = (byte) 0x95; 
133         private static final byte SRPC_OPEN_NETWORK       = (byte) 0x9c; 
134         
135         private static final short JOIN_AUTH_NUM                  = (short) 0xebd4;
136         
137         //SRPC AfAddr Addr modes ID's   
138         public static final byte AddrNotPresent = 0;
139         public static final byte AddrGroup = 1;
140         public static final byte Addr16Bit = 2;
141         public static final byte Addr64Bit = 3;
142         public static final byte AddrBroadcast = 1;
143                           
144         public static final String PREFS_NAME = "MyPrefsFile";
145         private static Timer ResponseTimer;
146         private static Handler ResponseTimerHandler;
147         
148         static byte[] srpcResponse;             
149         
150         private static int rpcsProcessIncoming(byte[] msg, int msgPtr)
151         {       
152                 int msgLen;             
153                 
154                 switch (msg[msgPtr + SRPC_CMD_ID_POS])
155                 {
157                 case SRPC_NEW_DEVICE:
158                 {
159                         int profileId=0, deviceId=0, nwkAddr=0; 
160                         char endPoint;
161                         String deviceName = "";
162                         byte[] ieee = new byte[8];
163                         byte newDevFlag;
165                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
166                         //index passed len and cmd ID
167                         msgPtr+=2;
168                         
169                         //Get the NwkAddr
170             for (int i=0; i < 2; i++, msgPtr++) 
171             {   
172                 //java does not support unsigned so use a bigger container to avoid conversion issues
173                 int nwkAddrTemp = (msg[msgPtr] & 0xff);                  
174                 nwkAddr += (nwkAddrTemp << (8 * i));                    
175             }                   
176             
177             //Get the EndPoint
178             endPoint = (char) msg[msgPtr++];
179             
180             //Get the ProfileId
181             for (int i=0; i < 2; i++, msgPtr++) 
182             {   
183                 //java does not support unsigned so use a bigger container to avoid conversion issues
184                 int profileIdTemp = (msg[msgPtr] & 0xff);                
185                 profileId += (profileIdTemp << (8 * i));                
186             }   
187             
188             //Get the DeviceId
189             for (int i=0; i < 2; i++, msgPtr++) 
190             {   
191                 //java does not support unsigned so use a bigger container to avoid conversion issues
192                 int deviceIdTemp = (msg[msgPtr] & 0xff);                 
193                 deviceId += (deviceIdTemp << (8 * i));                  
194             }               
195                         
196             //index passed version
197                         msgPtr++;
199                         int nameSize = msg[msgPtr++];
200                         for(int i = 0; i < nameSize; i++)
201                         {
202                                 deviceName += (char) msg[msgPtr++];
203                         }                       
204                         
205             //index passed status
206                         msgPtr++;
207                         
208                         //copy IEEE Addr
209                         for(int i = 0; i < 8; i++)
210                         {
211                                 ieee[i] = msg[msgPtr++];
212                         }                       
213                         
214                         newDevFlag = (byte) msg[msgPtr++];
216                         ZigbeeAssistant.newDevice(profileId, deviceId, nwkAddr, endPoint, ieee, deviceName, newDevFlag);
217                         
218             break;
219                 }
220                 case SRPC_ADD_GROUP_RSP:
221                 {
222                         short groupId = 0;
223                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
224                           
225                         //index passed len and cmd ID
226                         msgPtr+=2;
227                         
228                         //Get the GroupId
229             for (int i=0; i < 2; i++, msgPtr++) 
230             {   
231                 //java does not support unsigned so use a bigger container to avoid conversion issues
232                 int groupIdTemp = (msg[msgPtr] & 0xff);                  
233                 groupId += (groupIdTemp << (8 * i));                    
234             }                   
235                         
236             String groupNameStr = new String(msg, msgPtr+1, msg[msgPtr], Charset.defaultCharset());
237                         
238             List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();
239             //find the group
240             for(int i=0; i < groupList.size(); i++)
241             {
242                 if(groupNameStr.equals(groupList.get(i).getGroupName()))
243                 {
244                         groupList.get(i).setGroupId(groupId);
245                         groupList.get(i).setStatus(ZigbeeGroup.groupStatusActive);
246                         break;
247                 }
248             }
249                         break;
250                 }
251                                                 
252                 case SRPC_GET_GROUP_RSP:
253                 {
254                         short groupId = 0;
255                         String groupName = "";
256                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
257                           
258                         //index passed len and cmd ID
259                         msgPtr+=2;
260                         
261                         //Get the groupId
262             for (int i=0; i < 2; i++, msgPtr++) 
263             {   
264                 //java does not support unsigned so use a bigger container to avoid conversion issues
265                 int groupIdTemp = (msg[msgPtr] & 0xff);                  
266                 groupId += (groupIdTemp << (8 * i));                    
267             }                   
268                                     
269                         int nameSize = msg[msgPtr++];
270                         for(int i = 0; i < nameSize; i++)
271                         {
272                                 groupName += (char) msg[msgPtr++];
273                         }
274                         
275             ZigbeeAssistant.newGroup(groupName, groupId, ZigbeeGroup.groupStatusActive);
276             
277                         break;
278                 }       
279                 
280                 case SRPC_ADD_SCENE_RSP:
281                 {
282                         short groupId = 0;
283                         byte sceneId = 0;
284                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
285                           
286                         //index passed len and cmd ID
287                         msgPtr+=2;
288                         
289                         //Get the GroupId
290             for (int i=0; i < 2; i++, msgPtr++) 
291             {   
292                 //java does not support unsigned so use a bigger container to avoid conversion issues
293                 int groupIdTemp = (msg[msgPtr] & 0xff);                  
294                 groupId += (groupIdTemp << (8 * i));                    
295             }           
296             
297             //Get the sceneId
298             sceneId = (byte) msg[msgPtr++];
299                         
300             String sceneNameStr = new String(msg, msgPtr+1, msg[msgPtr], Charset.defaultCharset());
301                         
302             List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();
303             //find the scene
304             for(int i=0; i < sceneList.size(); i++)
305             {
306                 if(sceneNameStr.equals(sceneList.get(i).getSceneName()) && (groupId == sceneList.get(i).getGroupId() ))
307                 {
308                         sceneList.get(i).setSceneId(sceneId);
309                         sceneList.get(i).setStatus(ZigbeeScene.sceneStatusActive);
310                         break;
311                 }
312             }
313                         break;
314                 }
315                                                 
316                 case SRPC_GET_SCENE_RSP:
317                 {
318                         short groupId = 0;
319                         byte sceneId = 0;
320                         String sceneName = "";
321                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
322                           
323                         //index passed len and cmd ID
324                         msgPtr+=2;
326                         //Get the groupId
327             for (int i=0; i < 2; i++, msgPtr++) 
328             {   
329                 //java does not support unsigned so use a bigger container to avoid conversion issues
330                 int groupIdTemp = (msg[msgPtr] & 0xff);                  
331                 groupId += (groupIdTemp << (8 * i));                    
332             }    
333             
334             //Get the sceneId
335             sceneId = (byte) msg[msgPtr++];            
336                                     
337                         int nameSize = msg[msgPtr++];
338                         for(int i = 0; i < nameSize; i++)
339                         {
340                                 sceneName += (char) msg[msgPtr++];
341                         }
342                         
343             ZigbeeAssistant.newScene(sceneName, groupId, sceneId, ZigbeeScene.sceneStatusActive);
344             
345                         break;
346                 }
347                 case SRPC_GET_DEV_STATE_RSP:
348                 {
349                         short nwkAddr = 0;
350                         byte endPoint = 0;
351                         byte state = 0;
352                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
353                           
354                         //index passed len and cmd ID
355                         msgPtr+=2;
357                         //Get the nwkAddr
358             for (int i=0; i < 2; i++, msgPtr++) 
359             {   
360                 //java does not support unsigned so use a bigger container to avoid conversion issues
361                 int nwkAddrTemp = (msg[msgPtr] & 0xff);                  
362                 nwkAddr += (nwkAddrTemp << (8 * i));                    
363             }    
364             
365             //Get the EP
366             endPoint = (byte) msg[msgPtr++]; 
368             //Get the state
369             state = (byte) msg[msgPtr++]; 
370             
371             
372             List<ZigbeeDevice> devList = ZigbeeAssistant.getDevices();            
373             //find the device
374             for (int i = 0 ; i < devList.size() ; i++)
375             {           
376                 if( ( ((short) devList.get(i).NetworkAddr) == nwkAddr) && (devList.get(i).EndPoint == endPoint) )
377                 {
378                         devList.get(i).setCurrentState(state);
379                         break;
380                 }               
381             }                                           
382                         break;
383                 }
384                 case SRPC_GET_DEV_LEVEL_RSP:
385                 {
386                         short nwkAddr = 0;
387                         byte endPoint = 0;
388                         byte level = 0;
389                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
390                           
391                         //index passed len and cmd ID
392                         msgPtr+=2;
394                         //Get the nwkAddr
395             for (int i=0; i < 2; i++, msgPtr++) 
396             {   
397                 //java does not support unsigned so use a bigger container to avoid conversion issues
398                 int nwkAddrTemp = (msg[msgPtr] & 0xff);                  
399                 nwkAddr += (nwkAddrTemp << (8 * i));                    
400             }    
401             
402             //Get the EP
403             endPoint = (byte) msg[msgPtr++]; 
405             //Get the state
406             level = (byte) msg[msgPtr++]; 
407             
408             
409             List<ZigbeeDevice> devList = ZigbeeAssistant.getDevices();            
410             //find the device
411             for (int i = 0 ; i < devList.size() ; i++)
412             {           
413                 if( ( ((short) devList.get(i).NetworkAddr) == nwkAddr) && (devList.get(i).EndPoint == endPoint) )
414                 {
415                         devList.get(i).setCurrentLevel(level);
416                         break;
417                 }
418             }                                           
419                         break;                  
420                 }               
421                 case SRPC_GET_DEV_HUE_RSP:
422                 {
423                         short nwkAddr = 0;
424                         byte endPoint = 0;
425                         byte hue = 0;
426                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
427                           
428                         //index passed len and cmd ID
429                         msgPtr+=2;
431                         //Get the nwkAddr
432             for (int i=0; i < 2; i++, msgPtr++) 
433             {   
434                 //java does not support unsigned so use a bigger container to avoid conversion issues
435                 int nwkAddrTemp = (msg[msgPtr] & 0xff);                  
436                 nwkAddr += (nwkAddrTemp << (8 * i));                    
437             }    
438             
439             //Get the EP
440             endPoint = (byte) msg[msgPtr++]; 
442             //Get the state
443             hue = (byte) msg[msgPtr++]; 
444             
445             
446             List<ZigbeeDevice> devList = ZigbeeAssistant.getDevices();            
447             //find the device
448             for (int i = 0 ; i < devList.size() ; i++)
449             {           
450                 if( ( ((short) devList.get(i).NetworkAddr) == nwkAddr) && (devList.get(i).EndPoint == endPoint) )
451                 {
452                         devList.get(i).setCurrentHue(hue);
453                         break;
454                 }
455             }                                           
456                         break;
457                 }
458                 case SRPC_GET_DEV_SAT_RSP:
459                 {
460                         short nwkAddr = 0;
461                         byte endPoint = 0;
462                         byte sat = 0;
463                         msgLen = msg[msgPtr + SRPC_CMD_LEN_POS] + 2;
464                           
465                         //index passed len and cmd ID
466                         msgPtr+=2;
468                         //Get the nwkAddr
469             for (int i=0; i < 2; i++, msgPtr++) 
470             {   
471                 //java does not support unsigned so use a bigger container to avoid conversion issues
472                 int nwkAddrTemp = (msg[msgPtr] & 0xff);                  
473                 nwkAddr += (nwkAddrTemp << (8 * i));                    
474             }    
475             
476             //Get the EP
477             endPoint = (byte) msg[msgPtr++]; 
479             //Get the state
480             sat = (byte) msg[msgPtr++]; 
481             
482             
483             List<ZigbeeDevice> devList = ZigbeeAssistant.getDevices();            
484             //find the device
485             for (int i = 0 ; i < devList.size() ; i++)
486             {           
487                 if( ( ((short) devList.get(i).NetworkAddr) == nwkAddr) && (devList.get(i).EndPoint == endPoint) )
488                 {
489                         devList.get(i).setCurrentSat(sat);
490                         break;
491                 }               
492             }                                           
493                         break;
494                 }
495                 
496                 default:
497                 {
498                         msgLen = 0;
499                 }               
500                 }
501                 
502                 return msgLen;
503         }
504         
505         public static void setDeviceState(short nwkAddr, byte AddrMode, char endPoint, boolean state)
506         {
507                 byte[] msg = new byte[15];
508                 byte  msgIdx;
509                  
510                 //set SRPC len and CMD ID
511                 msg[SRPC_CMD_ID_POS] = SRPC_SET_DEV_STATE;
512                 msg[SRPC_CMD_LEN_POS] = 13;
513                 
514                 //set ptr to point to data
515                 msgIdx=2;
516                 
517                 //dstAddr.addrMode
518                 msg[msgIdx++] = (byte) AddrMode;
519                 //set afAddrMode_t nwk address          
520                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
521                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
522                 //pad for an ieee addr size;
523                 msgIdx += 6;    
524                 //set Ep
525                 msg[msgIdx++] = (byte) endPoint;
526                 //pad out pan ID
527                 msgIdx+=2;
528                 
529                 //set State
530                 if(state)
531                 {
532                         msg[msgIdx++] = (byte) 1;
533                 }
534                 else
535                 {
536                         msg[msgIdx++] = (byte) 0;
537                 }               
538                 
539                 sendSrpc(msg);
540                 
541         }
543         public static void setDeviceLevel(short nwkAddr, byte AddrMode, char endPoint, char level, short transitionTime)
544         {
545                 byte[] msg = new byte[17];
546                 byte  msgIdx;
547                  
548                 //set SRPC len and CMD ID
549                 msg[SRPC_CMD_ID_POS] = SRPC_SET_DEV_LEVEL;
550                 msg[SRPC_CMD_LEN_POS] = 15;
551                 
552                 //set ptr to point to data
553                 msgIdx=2;
554                 
555                 //dstAddr.addrMode
556                 msg[msgIdx++] = (byte) AddrMode;
557                 //set afAddrMode_t nwk address          
558                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
559                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
560                 //pad for an ieee addr size;
561                 msgIdx += 6;    
562                 //set Ep
563                 msg[msgIdx++] = (byte) endPoint;        
564                 //pad out pan ID
565                 msgIdx+=2;
567                 //set level
568                 msg[msgIdx++] = (byte) level;   
570                 //set transitionTime
571                 msg[msgIdx++] = (byte) transitionTime;          
572                 
573                 sendSrpc(msg);          
574         }       
576         public static void setDeviceColor(short nwkAddr, byte AddrMode, char endPoint, char hue, char saturation, short transitionTime)
577         {
578                 byte[] msg = new byte[18];
579                 byte  msgIdx;
580                  
581                 //set SRPC len and CMD ID
582                 msg[SRPC_CMD_ID_POS] = SRPC_SET_DEV_COLOR;
583                 msg[SRPC_CMD_LEN_POS] = 16;
584                 
585                 //set ptr to point to data
586                 msgIdx=2;
587                 
588                 //dstAddr.addrMode
589                 msg[msgIdx++] = (byte) AddrMode;
590                 //set afAddrMode_t nwk address          
591                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
592                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
593                 //pad for an ieee addr size;
594                 msgIdx += 6;    
595                 //set Ep
596                 msg[msgIdx++] = (byte) endPoint;                
597                 //pad out pan ID
598                 msgIdx+=2;
600                 //set level
601                 msg[msgIdx++] = (byte) hue;     
603                 //set saturation
604                 msg[msgIdx++] = (byte) saturation;      
605                 
606                 //set transitionTime
607                 msg[msgIdx++] = (byte) transitionTime;          
608                 
609                 sendSrpc(msg);          
610         }       
612         public static void getDeviceState(short nwkAddr, byte AddrMode, char endPoint)
613         {
614                 byte[] msg = new byte[15];
615                 byte  msgIdx;
616                  
617                 //set SRPC len and CMD ID
618                 msg[SRPC_CMD_ID_POS] = SRPC_GET_DEV_STATE;
619                 msg[SRPC_CMD_LEN_POS] = 13;
620                 
621                 //set ptr to point to data
622                 msgIdx=2;
623                 
624                 //dstAddr.addrMode
625                 msg[msgIdx++] = (byte) AddrMode;
626                 //set afAddrMode_t nwk address          
627                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
628                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
629                 //pad for an ieee addr size;
630                 msgIdx += 6;    
631                 //set Ep
632                 msg[msgIdx++] = (byte) endPoint;
633                 //pad out pan ID
634                 msgIdx+=2;              
635                 
636                 sendSrpc(msg);          
637         }
639         public static void getDeviceLevel(short nwkAddr, byte AddrMode, char endPoint)
640         {
641                 byte[] msg = new byte[15];
642                 byte  msgIdx;
643                  
644                 //set SRPC len and CMD ID
645                 msg[SRPC_CMD_ID_POS] = SRPC_GET_DEV_LEVEL;
646                 msg[SRPC_CMD_LEN_POS] = 13;
647                 
648                 //set ptr to point to data
649                 msgIdx=2;
650                 
651                 //dstAddr.addrMode
652                 msg[msgIdx++] = (byte) AddrMode;
653                 //set afAddrMode_t nwk address          
654                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
655                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
656                 //pad for an ieee addr size;
657                 msgIdx += 6;    
658                 //set Ep
659                 msg[msgIdx++] = (byte) endPoint;
660                 //pad out pan ID
661                 msgIdx+=2;              
662                 
663                 sendSrpc(msg);          
664         }       
666         public static void getDeviceHue(short nwkAddr, byte AddrMode, char endPoint)
667         {
668                 byte[] msg = new byte[15];
669                 byte  msgIdx;
670                  
671                 //set SRPC len and CMD ID
672                 msg[SRPC_CMD_ID_POS] = SRPC_GET_DEV_HUE;
673                 msg[SRPC_CMD_LEN_POS] = 13;
674                 
675                 //set ptr to point to data
676                 msgIdx=2;
677                 
678                 //dstAddr.addrMode
679                 msg[msgIdx++] = (byte) AddrMode;
680                 //set afAddrMode_t nwk address          
681                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
682                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
683                 //pad for an ieee addr size;
684                 msgIdx += 6;    
685                 //set Ep
686                 msg[msgIdx++] = (byte) endPoint;
687                 //pad out pan ID
688                 msgIdx+=2;              
689                 
690                 sendSrpc(msg);          
691         }
693         public static void getDeviceSat(short nwkAddr, byte AddrMode, char endPoint)
694         {
695                 byte[] msg = new byte[15];
696                 byte  msgIdx;
697                  
698                 //set SRPC len and CMD ID
699                 msg[SRPC_CMD_ID_POS] = SRPC_GET_DEV_SAT;
700                 msg[SRPC_CMD_LEN_POS] = 13;
701                 
702                 //set ptr to point to data
703                 msgIdx=2;
704                 
705                 //dstAddr.addrMode
706                 msg[msgIdx++] = (byte) AddrMode;
707                 //set afAddrMode_t nwk address          
708                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
709                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
710                 //pad for an ieee addr size;
711                 msgIdx += 6;    
712                 //set Ep
713                 msg[msgIdx++] = (byte) endPoint;
714                 //pad out pan ID
715                 msgIdx+=2;              
716                 
717                 sendSrpc(msg);          
718         }
719         
720         public static boolean bindDevices(short network_a, char end_a, byte ieee_a[], short network_b, char end_b, byte ieee_b[], short clusterId)
721         {
722                 byte[] msg = new byte[24];
723                 byte  msgIdx;
725                 //set SRPC len and CMD ID
726                 msg[SRPC_CMD_ID_POS] = SRPC_BIND_DEVICES;
727                 msg[SRPC_CMD_LEN_POS] = 22;
728                 
729                 //set ptr to point to data
730                 msgIdx=2;
731                 
732                 //set src nwk address           
733                 msg[msgIdx++] = (byte) (network_a & 0xFF);
734                 msg[msgIdx++] = (byte) ((network_a & 0xFF00)>>8);               
736                 //set Ep
737                 msg[msgIdx++] = (byte) end_a;
739                 //set ieee of a
740                 msg[msgIdx++] = ieee_a[0];
741                 msg[msgIdx++] = ieee_a[1];
742                 msg[msgIdx++] = ieee_a[2];
743                 msg[msgIdx++] = ieee_a[3];
744                 msg[msgIdx++] = ieee_a[4];
745                 msg[msgIdx++] = ieee_a[5];
746                 msg[msgIdx++] = ieee_a[6];
747                 msg[msgIdx++] = ieee_a[7];
749                 //set Ep
750                 msg[msgIdx++] = (byte) end_b;
751                   
752                 //set ieee of b
753                 msg[msgIdx++] = ieee_b[0];
754                 msg[msgIdx++] = ieee_b[1];
755                 msg[msgIdx++] = ieee_b[2];
756                 msg[msgIdx++] = ieee_b[3];
757                 msg[msgIdx++] = ieee_b[4];
758                 msg[msgIdx++] = ieee_b[5];
759                 msg[msgIdx++] = ieee_b[6];
760                 msg[msgIdx++] = ieee_b[7];
762                 //set clusterId         
763                 msg[msgIdx++] = (byte) (clusterId & 0xFF);
764                 msg[msgIdx++] = (byte) ((clusterId & 0xFF00)>>8);       
765                 
766                 sendSrpc(msg);
767                 
768                 return true;
769         }                       
771         public static void getDevices()
772         {
773                 byte[] msg = new byte[2];
775                 //set SRPC len and CMD ID
776                 msg[SRPC_CMD_ID_POS] = SRPC_GET_DEVICES;
777                 msg[SRPC_CMD_LEN_POS] = 0;
778                 
779                 sendSrpc(msg);
780         }
781         
782         public static void discoverGroups()
783         {
784                 byte[] msg = new byte[2];
786                 //set SRPC len and CMD ID
787                 msg[SRPC_CMD_ID_POS] = SRPC_GET_GROUPS;
788                 msg[SRPC_CMD_LEN_POS] = 0;
789                 
790                 sendSrpc(msg);
791         }
793         public static void discoverScenes()
794         {
795                 byte[] msg = new byte[2];
797                 //set SRPC len and CMD ID
798                 msg[SRPC_CMD_ID_POS] = SRPC_GET_SCENES;
799                 msg[SRPC_CMD_LEN_POS] = 0;
800                 
801                 sendSrpc(msg);
802         }       
803         
804         public static void addGroup(short nwkAddr, char endPoint, String groupName)
805         {
806                 byte[] msg = new byte[14 + groupName.length() + 1];
807                 byte msgIdx;
809                 //set SRPC len and CMD ID
810                 msg[SRPC_CMD_ID_POS] = SRPC_ADD_GROUP;
811                 msg[SRPC_CMD_LEN_POS] = (byte) (12 + groupName.length() + 1);
812                 
813                 //set ptr to point to data
814                 msgIdx=2;
815                 
816                 //dstAddr.addrMode = Addr16Bit
817                 msg[msgIdx++] = (byte) Addr16Bit;
818                 //set afAddrMode_t nwk address          
819                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
820                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
821                 //pad for an ieee addr size;
822                 msgIdx += 6;    
823                 //set Ep
824                 msg[msgIdx++] = (byte) endPoint;                
825                 //pad out pan ID
826                 msgIdx+=2;
828                 msg[msgIdx++] = (byte) groupName.length();
829                 for(int i = 0; i < groupName.length(); i++)
830                 {
831                         msg[msgIdx++] = groupName.getBytes()[i];
832                 }
833                                 
834                 sendSrpc(msg);                          
835         }
836         
837         public static void storeScene(String sceneName, int groupId)
838         {
839                 byte[] msg = new byte[2 + 15 + sceneName.length()];
840                 byte msgIdx;
842                 //set SRPC len and CMD ID
843                 msg[SRPC_CMD_ID_POS] = SRPC_STORE_SCENE;
844                 msg[SRPC_CMD_LEN_POS] = (byte) (15 + sceneName.length());
845                 
846                 //set ptr to point to data
847                 msgIdx=2;               
849                 //dstAddr.addrMode = Addr16Bit
850                 msg[msgIdx++] = (byte) AddrGroup;
851                 //set afAddrMode_t nwk address          
852                 msg[msgIdx++] = (byte) (groupId & 0xFF);
853                 msg[msgIdx++] = (byte) ((groupId & 0xFF00)>>8);         
854                 //pad for an ieee addr size;
855                 msgIdx += 6;    
856                 //set Ep
857                 msg[msgIdx++] = (byte) 0xFF;            
858                 //pad out pan ID
859                 msgIdx+=2;
860                 
861                 msg[msgIdx++] = (byte) (groupId & 0xFF);
862                 msg[msgIdx++] = (byte) ((groupId & 0xFF00)>>8); 
864                 msg[msgIdx++] = (byte) sceneName.length();
865                 for(int i = 0; i < sceneName.length(); i++)
866                 {
867                         msg[msgIdx++] = sceneName.getBytes()[i];
868                 }
869                 
870                 sendSrpc(msg);                                  
871         }
873         public static void recallScene(String sceneName, int groupId)
874         {
875                 byte[] msg = new byte[2 + 15 + sceneName.length()];
876                 byte msgIdx;
878                 //set SRPC len and CMD ID
879                 msg[SRPC_CMD_ID_POS] = SRPC_RECALL_SCENE;
880                 msg[SRPC_CMD_LEN_POS] = (byte) (15 + sceneName.length());
881                 
882                 //set ptr to point to data
883                 msgIdx=2;               
885                 //dstAddr.addrMode = Addr16Bit
886                 msg[msgIdx++] = (byte) AddrGroup;
887                 //set afAddrMode_t nwk address          
888                 msg[msgIdx++] = (byte) (groupId & 0xFF);
889                 msg[msgIdx++] = (byte) ((groupId & 0xFF00)>>8);         
890                 //pad for an ieee addr size;
891                 msgIdx += 6;    
892                 //set Ep
893                 msg[msgIdx++] = (byte) 0xFF;            
894                 //pad out pan ID
895                 msgIdx+=2;
896                 
897                 //set groupId
898                 msg[msgIdx++] = (byte) (groupId & 0xFF);
899                 msg[msgIdx++] = (byte) ((groupId & 0xFF00)>>8); 
901                 msg[msgIdx++] = (byte) sceneName.length();
902                 for(int i = 0; i < sceneName.length(); i++)
903                 {
904                         msg[msgIdx++] = sceneName.getBytes()[i];
905                 }
906                 
907                 sendSrpc(msg);                                  
908         }
909         
911         public static void IdentifyDevice(short nwkAddr, byte AddrMode, char endPoint, short identifyTime)
912         {
913                 byte[] msg = new byte[16];
914                 byte  msgIdx;
915                  
916                 //set SRPC len and CMD ID
917                 msg[SRPC_CMD_ID_POS] = SRPC_IDENTIFY_DEVICE;
918                 msg[SRPC_CMD_LEN_POS] = 14;
919                 
920                 //set ptr to point to data
921                 msgIdx=2;
922                 
923                 //dstAddr.addrMode
924                 msg[msgIdx++] = (byte) AddrMode;
925                 //set afAddrMode_t nwk address          
926                 msg[msgIdx++] = (byte) (nwkAddr & 0xFF);
927                 msg[msgIdx++] = (byte) ((nwkAddr & 0xFF00)>>8);         
928                 //pad for an ieee addr size;
929                 msgIdx += 6;    
930                 //set Ep
931                 msg[msgIdx++] = (byte) endPoint;        
932                 //pad out pan ID
933                 msgIdx+=2;
935                 //set transitionTime            
936                 msg[msgIdx++] = (byte) (identifyTime & 0xFF);
937                 msg[msgIdx++] = (byte) ((identifyTime & 0xFF00)>>8);                    
938                 
939                 sendSrpc(msg);          
940         }       
941         
942         public static void changeDeviceName(ZigbeeDevice device, String deviceName)
943         {
944                 byte[] msg = new byte[6 + deviceName.length()];
945                 byte msgIdx;
947                 //set SRPC len and CMD ID
948                 msg[SRPC_CMD_ID_POS] = SRPC_CHANGE_DEVICE_NAME;
949                 msg[SRPC_CMD_LEN_POS] = (byte) (4 + deviceName.length());
950                 
951                 //set ptr to point to data
952                 msgIdx=2;               
954                 //set afAddrMode_t nwk address          
955                 msg[msgIdx++] = (byte) (device.NetworkAddr & 0xFF);
956                 msg[msgIdx++] = (byte) ((device.NetworkAddr & 0xFF00)>>8);      
957                 
958                 //set EP
959                 msg[msgIdx++] = (byte) (device.EndPoint);
961                 msg[msgIdx++] = (byte) deviceName.length();
962                 for(int i = 0; i < deviceName.length(); i++)
963                 {
964                         msg[msgIdx++] = deviceName.getBytes()[i];
965                 }
966                 
967                 sendSrpc(msg);                                  
968         }       
969         
970         
971         public static void openNetwork(byte duration)
972         {
973                 byte[] msg = new byte[5];
974                 byte msgIdx;
976                 //set SRPC len and CMD ID
977                 msg[SRPC_CMD_ID_POS] = SRPC_OPEN_NETWORK;
978                 msg[SRPC_CMD_LEN_POS] = (byte) (3);
979                 
980                 //set ptr to point to data
981                 msgIdx=2;               
983                 //set duration          
984                 msg[msgIdx++] = duration;
985                 
986                 //set join auth
987                 msg[msgIdx++] = (byte) (JOIN_AUTH_NUM & 0xFF);
988                 msg[msgIdx++] = (byte) ((JOIN_AUTH_NUM & 0xFF00)>>8);           
989                 
990                 sendSrpc(msg);                                  
991         }
992         
993         public static int clientConnect( )
994         {
995                 int Port;
996                 
997                 Port = Integer.parseInt(gatewayPort);                                   
998 /*              
999                 try {                                   
1000                         kkSocket = new Socket(gatewayIp, Port);
1001                         //kkSocket = new Socket(); 
1002             SocketAddress adr = new InetSocketAddress(gatewayIp,Port);
1003                         kkSocket.
1004             kkSocket.connect(adr, 5000);                        
1005                 } catch (UnknownHostException e) { 
1006                         e.printStackTrace();
1007                         return 1;
1008                         //errorMessage("Unknown host" + gatewayIp); 
1009                 } catch (IOException e) {
1010                         e.printStackTrace();
1011                         return 1;
1012                 //errorMessage("Couldn't get I/O for the connection to: " + gatewayIp); 
1013                 }*/
1014                 
1015         SocketAddress sockaddr = new InetSocketAddress(gatewayIp, Port);
1016         kkSocket = new Socket();
1017         try {
1018                         kkSocket.connect(sockaddr, 5000); //5 second connection timeout
1019                 } catch (IOException e1) {
1020                         return 1;
1021                 } 
1022         
1023         
1024         if (kkSocket.isConnected()) { 
1025                 
1026                         ZigbeeAssistant.gateWayConnected = true;
1027                         
1028                         try {
1029                                 outStream = kkSocket.getOutputStream();
1030                         } catch (IOException e) {
1031                                 e.printStackTrace();
1032                                 return 1;
1033                         }
1034                         
1035                         try {
1036                                 inStream = kkSocket.getInputStream();
1037                         } catch (IOException e) {
1038                                 e.printStackTrace();
1039                                 return 1;
1040                         }               
1041                         
1042                         //create Rx thread
1043                         new Thread(new Runnable() 
1044                         {    
1045                                 public void run() {   
1046                                         try {
1047                                                 Listen();
1048                                         } catch (IOException e) {
1049                                                 // TODO Auto-generated catch block
1050                                                 e.printStackTrace();
1051                                         }
1052                                 }  
1053                         }).start();
1054                         
1055                         return 0;
1056         }
1057         
1058         return 1;
1059         }
1060         
1061         public static void Disconnect()
1062         {
1063                 if(kkSocket != null)
1064                 {
1065                 try {
1066                                 kkSocket.close();
1067                         } catch (IOException e) {
1068                                 // TODO Auto-generated catch block
1069                                 e.printStackTrace();
1070                         }
1071                     ZigbeeAssistant.gateWayConnected = false;
1072                 }
1073         }
1074         
1076         public static void sendSrpc(byte[] msg)
1077         {
1078                 try{            
1079                         outStream.write( msg ); 
1080                 } catch (Exception e){}         
1081         }
1082         
1083         private static void Listen() throws IOException {
1084         //listen for Packets
1085                 while(true)
1086                 {       
1087                         byte[] RxBuffer = new byte[1024];
1088                         int bytesRead = ReadMessage(inStream, RxBuffer);
1089                         
1090                         Log.i("Process Message:", "[" + bytesRead + "]: RPC CMD: " + RxBuffer[0] + ", RPC LEN: " + RxBuffer[1]);
1091                         
1092                         if(bytesRead == -1)
1093                         {
1094                                 //error
1095                         }
1096                         else
1097                         {
1098                                 int bytesProcessed;
1099                                 bytesProcessed = rpcsProcessIncoming(RxBuffer, 0);
1100                                 if ( bytesRead > bytesProcessed )
1101                                 {
1102                                         //error did not process full buffer
1103                                 }
1104                         }
1105                 }
1106 /*                      
1107                 while(true)
1108                 {               
1109                         byte[] RxBuffer = new byte[1024];
1110                         try {
1111                                 int bytesRead = inStream.read(RxBuffer);
1112                                 int bytesProcessed = 0;
1113                                 while ( bytesRead > bytesProcessed )
1114                                 {
1115                                         bytesProcessed += rpcsProcessIncoming(RxBuffer, bytesProcessed);
1116                                 }
1117                         } catch (IOException e) {
1118                                 // TODO Auto-generated catch block
1119                                 e.printStackTrace();
1120                         }
1121                 }
1122 */              
1123         }
1124         
1125         private static int ReadMessage(InputStream is, byte[] bytes) throws IOException {
1126             byte[] rpcHeader = new byte[2];
1127             int offset, len;
1128             int read;
1129             
1130             read = is.read(rpcHeader, 0, 2);
1131             len = rpcHeader[1] + 2;
1132             
1133             bytes[0] = rpcHeader[0];
1134             bytes[1] = rpcHeader[1];
1135             
1136             if (read == -1) { return -1;}
1137             
1138             Log.i("ReadMessage", "RPC CMD: " + rpcHeader[0] + ", RPC LEN: " + rpcHeader[1]);
1139             
1140             offset = 2;
1141             while (offset < len) {
1142                 read = is.read(bytes, offset, len - offset);
1143                 if (read == -1) {
1144 /*                  if ( offset == 0 ) {
1145                         return -1;
1146                     } else {
1147                         return offset;
1148                     }*/
1149                 } else {
1150                     offset += read;
1151                 }
1152             }
1154             return len;
1155         }       
1157         public static String getGatewayIp() {
1158                 return gatewayIp;
1159         }
1161         public static void setGatewayIp(String gatewayIp) {
1162                 ZigbeeSrpcClient.gatewayIp = gatewayIp;
1163         }