]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - server/Source/interface_srpcserver.c
Initial checkin
[iot-gateway/lighting-gateway.git] / server / Source / interface_srpcserver.c
1 /**************************************************************************************************
2  * Filename:       interface_srpcserver.c
3  * Description:    Socket Remote Procedure Call Interface - sample device application.
4  *
5  *
6  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/ 
7  * 
8  * 
9  *  Redistribution and use in source and binary forms, with or without 
10  *  modification, are permitted provided that the following conditions 
11  *  are met:
12  *
13  *    Redistributions of source code must retain the above copyright 
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  *    Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the 
18  *    documentation and/or other materials provided with the   
19  *    distribution.
20  *
21  *    Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
26  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
27  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
29  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
30  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
31  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
34  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
35  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sys/types.h> 
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <fcntl.h>
47 #include <sys/signal.h>
48 #include <sys/ioctl.h>
49 #include <poll.h>
51 #include "interface_srpcserver.h"
52 #include "socket_server.h"
53 #include "interface_devicelist.h"
54 #include "interface_grouplist.h"
55 #include "interface_scenelist.h"
57 #include "hal_defs.h"
59 #include "zllSocCmd.h"
61 void SRPC_RxCB( int clientFd );
62 void SRPC_ConnectCB( int status ); 
64 static uint8_t RPCS_ZLL_setDeviceState(uint8_t *pBuf, uint32_t clientFd);
65 static uint8_t RPCS_ZLL_setDeviceLevel(uint8_t *pBuf, uint32_t clientFd);
66 static uint8_t RPCS_ZLL_setDeviceColor(uint8_t *pBuf, uint32_t clientFd);
67 static uint8_t RPCS_ZLL_getDeviceState(uint8_t *pBuf, uint32_t clientFd);
68 static uint8_t RPCS_ZLL_getDeviceLevel(uint8_t *pBuf, uint32_t clientFd);
69 static uint8_t RPCS_ZLL_getDeviceHue(uint8_t *pBuf, uint32_t clientFd);
70 static uint8_t RPCS_ZLL_getDeviceSat(uint8_t *pBuf, uint32_t clientFd);
71 static uint8_t RSPC_ZLL_bindDevices(uint8_t *pBuf, uint32 clientFd);
72 static uint8_t RPCS_ZLL_getGroups(uint8_t *pBuf, uint32 clientFd);
73 static uint8_t RPCS_ZLL_addGroup(uint8_t *pBuf, uint32 clientFd);
74 static uint8_t RPCS_ZLL_getScenes(uint8_t *pBuf, uint32 clientFd);
75 static uint8_t RPCS_ZLL_storeScene(uint8_t *pBuf, uint32 clientFd);
76 static uint8_t RPCS_ZLL_recallScene(uint8_t *pBuf, uint32 clientFd);
77 static uint8_t RPCS_ZLL_identifyDevice(uint8_t *pBuf, uint32_t clientFd);
78 static uint8_t RSPC_ZLL_close(uint8_t *pBuf, uint32_t clientFd);
79 static uint8_t RSPC_ZLL_getDevices(uint8_t *pBuf, uint32_t clientFd);
80 static uint8_t RPSC_ZLL_notSupported(uint8_t *pBuf, uint32_t clientFd);
82 //RPSC ZLL Interface call back functions
83 static void RPCS_ZLL_CallBack_addGroupRsp(uint16_t groupId, char *nameStr, uint32 clientFd);
84 static void RPCS_ZLL_CallBack_addSceneRsp(uint16_t groupId, uint8_t sceneId, char *nameStr, uint32 clientFd);
86 typedef uint8_t (*rpcsProcessMsg_t)(uint8_t *pBuf, uint32_t clientFd);
88 rpcsProcessMsg_t rpcsProcessIncoming[] =
89 {
90   RSPC_ZLL_close,           //RPCS_CLOSE
91   RSPC_ZLL_getDevices,      //RPCS_GET_DEVICES     
92   RPCS_ZLL_setDeviceState,  //RPCS_SET_DEV_STATE     
93   RPCS_ZLL_setDeviceLevel,  //RPCS_SET_DEV_LEVEL     
94   RPCS_ZLL_setDeviceColor,  //RPCS_SET_DEV_COLOR
95   RPCS_ZLL_getDeviceState,  //RPCS_GET_DEV_STATE     
96   RPCS_ZLL_getDeviceLevel,  //RPCS_GET_DEV_LEVEL     
97   RPCS_ZLL_getDeviceHue,    //RPCS_GET_DEV_HUE  
98   RPCS_ZLL_getDeviceSat,    //RPCS_GET_DEV_HUE        
99   RSPC_ZLL_bindDevices,     //RPCS_BIND_DEVICES      
100   RPSC_ZLL_notSupported,    //RPCS_GET_THERM_READING 
101   RPSC_ZLL_notSupported,    //RPCS_GET_POWER_READING 
102   RPSC_ZLL_notSupported,    //RPCS_DISCOVER_DEVICES  
103   RPSC_ZLL_notSupported,    //RPCS_SEND_ZCL          
104   RPCS_ZLL_getGroups,       //RPCS_GET_GROUPS    
105   RPCS_ZLL_addGroup,        //RPCS_ADD_GROUP     
106   RPCS_ZLL_getScenes,       //RPCS_GET_SCENES    
107   RPCS_ZLL_storeScene,       //RPCS_STORE_SCENE       
108   RPCS_ZLL_recallScene,     //RPCS_RECALL_SCENE      
109   RPCS_ZLL_identifyDevice,  //RPCS_IDENTIFY_DEVICE   
110   RPSC_ZLL_notSupported,    //RPCS_CHANGE_DEVICE_NAME  
111   RPSC_ZLL_notSupported,    //RPCS_REMOVE_DEVICE             
112 };
114 static void srpcSend(uint8_t* srpcMsg, int fdClient);
115 static void srpcSendAll(uint8_t* srpcMsg);
117 /***************************************************************************************************
118  * @fn      srpcParseEpInfp - Parse epInfo and prepare the SRPC message.
119  *
120  * @brief   Parse epInfo and prepare the SRPC message.
121  * @param   epInfo_t* epInfo
122  *
123  * @return  pSrpcMessage
124  ***************************************************************************************************/
125 static uint8_t* srpcParseEpInfo(epInfo_t* epInfo)
127   uint8_t i;
128   uint8_t *pSrpcMessage, *pTmp, devNameLen = 1, pSrpcMessageLen;  
130   //printf("srpcParseEpInfo++\n");   
131       
132   //RpcMessage contains function ID param Data Len and param data
133   if( epInfo->deviceName )
134   {
135     devNameLen = epInfo->deviceName[0];
136   }
137   
138   //sizre of EP infor - the name char* + num bytes of device name (byte 0 being len on name str)
139   pSrpcMessageLen = sizeof(epInfo_t) - sizeof(char*) + devNameLen;
140   pSrpcMessage = malloc(pSrpcMessageLen + 2);  
141   
142   pTmp = pSrpcMessage;
143   
144   if( pSrpcMessage )
145   {
146     //Set func ID in RPCS buffer
147     *pTmp++ = RPCS_NEW_ZLL_DEVICE;
148     //param size
149     *pTmp++ = pSrpcMessageLen;
150     
151     *pTmp++ = LO_UINT16(epInfo->nwkAddr);
152     *pTmp++ = HI_UINT16(epInfo->nwkAddr);
153     *pTmp++ = epInfo->endpoint;
154     *pTmp++ = LO_UINT16(epInfo->profileID);
155     *pTmp++ = HI_UINT16(epInfo->profileID);
156     *pTmp++ = LO_UINT16(epInfo->deviceID);
157     *pTmp++ = HI_UINT16(epInfo->deviceID);
158     *pTmp++ = epInfo->version;  
159     if( epInfo->deviceName )
160     {
161       for(i = 0; i < (epInfo->deviceName[0] + 1); i++)
162       {
163         *pTmp++ = epInfo->deviceName[i];
164       }
165     }
166     else
167     {
168       *pTmp++=0;
169     }
170     *pTmp++ = epInfo->status;    
171     
172     for(i = 0; i < 8; i++)
173     {
174       //printf("srpcParseEpInfp: IEEEAddr[%d] = %x\n", i, epInfo->IEEEAddr[i]);
175       *pTmp++ = epInfo->IEEEAddr[i];
176     }
177   }
178       
179   //printf("srpcParseEpInfp--\n");
181   return pSrpcMessage;
182 }    
184 /***************************************************************************************************
185  * @fn      srpcSend
186  *
187  * @brief   Send a message over SRPC to a clients.
188  * @param   uint8_t* srpcMsg - message to be sent
189  *
190  * @return  Status
191  ***************************************************************************************************/
192 static void srpcSend(uint8_t* srpcMsg, int fdClient)
193
194   int rtn;
195  
196   rtn = socketSeverSend(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2), fdClient);
197   if (rtn < 0) 
198   {
199     printf("ERROR writing to socket\n");
200   }
201     
202   return; 
205 /***************************************************************************************************
206  * @fn      srpcSendAll
207  *
208  * @brief   Send a message over SRPC to all clients.
209  * @param   uint8_t* srpcMsg - message to be sent
210  *
211  * @return  Status
212  ***************************************************************************************************/
213 static void srpcSendAll(uint8_t* srpcMsg)
214
215   int rtn;
216  
217   rtn = socketSeverSendAllclients(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2));
218   if (rtn < 0) 
219   {
220     printf("ERROR writing to socket\n");
221   }
222     
223   return; 
227 /*********************************************************************
228  * @fn          RPSC_ProcessIncoming
229  *
230  * @brief       This function processes incoming messages.
231  *
232  * @param       pBuf - incomin messages
233  *
234  * @return      afStatus_t
235  */
236 void RPSC_ProcessIncoming(uint8_t *pBuf, uint32_t clientFd)
238   rpcsProcessMsg_t func;
239   
240   //printf("RPSC_ProcessIncoming++[%x]\n", pBuf[SRPC_FUNC_ID]);
241   /* look up and call processing function */
242   func = rpcsProcessIncoming[(pBuf[SRPC_FUNC_ID] & ~(0x80))];
243   if (func)
244   {
245     (*func)(pBuf, clientFd);
246   }
247   else
248   {
249     //printf("Error: no processing function for CMD 0x%x\n", pBuf[SRPC_FUNC_ID]); 
250   }
251   
252   //printf("RPSC_ProcessIncoming--\n");
255 /*********************************************************************
256  * @fn          RPCS_ZLL_addGroup
257  *
258  * @brief       This function exposes an interface to add a devices to a group.
259  *
260  * @param       pBuf - incomin messages
261  *
262  * @return      afStatus_t
263  */
264 static uint8_t RPCS_ZLL_addGroup(uint8_t *pBuf, uint32 clientFd)
266   uint16_t dstAddr;
267   uint8_t addrMode;
268   uint8_t endpoint;
269   char *nameStr;
270   uint8_t nameLen;
271   uint16_t groupId;
272   
273   //printf("RPCS_ZLL_addGroup++\n");
274        
275   //increment past SRPC header
276   pBuf+=2;
278   addrMode = (afAddrMode_t)*pBuf++;   
279   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
280   pBuf += Z_EXTADDR_LEN;
281   endpoint = *pBuf++;
282   // index past panId
283   pBuf += 2;
284   
285   nameLen = *pBuf++;
286   nameStr = malloc(nameLen + 1);
287   nameStr[0] = nameLen;
288   int i;
289   for(i = 0; i < nameLen; i++)
290   {
291     nameStr[i+1] = *pBuf++;
292   } 
293   
294   //printf("RPCS_ZLL_addGroup++: %x:%x:%x name[%d] %s \n", dstAddr, addrMode, endpoint, nameLen, nameStr);
295           
296   groupId = groupListAddGroup( nameStr );        
297   zllSocAddGroup(groupId, dstAddr, endpoint, addrMode);
299   RPCS_ZLL_CallBack_addGroupRsp(groupId, nameStr, clientFd);
301   free(nameStr);
302   
303   //printf("RPCS_ZLL_addGroup--\n");
304   
305   return 0;
308 /*********************************************************************
309  * @fn          uint8_t RPCS_ZLL_storeScene(uint8_t *pBuf, uint32 clientFd)
310  *
311  * @brief       This function exposes an interface to store a scene.
312  *
313  * @param       pBuf - incomin messages
314  *
315  * @return      afStatus_t
316  */
317 static uint8_t RPCS_ZLL_storeScene(uint8_t *pBuf, uint32 clientFd)
319   uint16_t dstAddr;
320   uint8_t addrMode;
321   uint8_t endpoint;
322   char *nameStr;
323   uint8_t nameLen;
324   uint16_t groupId;
325   uint8_t sceneId;
326   
327   //printf("RPCS_ZLL_storeScene++\n");
328        
329   //increment past SRPC header
330   pBuf+=2;
332   addrMode = (afAddrMode_t)*pBuf++;   
333   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
334   pBuf += Z_EXTADDR_LEN;
335   endpoint = *pBuf++;
336   // index past panId
337   pBuf += 2;
338   
339   groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
340   pBuf += 2;
341   
342   nameLen = *pBuf++;
343   nameStr = malloc(nameLen + 1);
344   nameStr[0] = nameLen;
345   int i;
346   for(i = 0; i < nameLen; i++)
347   {
348     nameStr[i+1] = *pBuf++;
349   } 
350   
351   //printf("RPCS_ZLL_storeScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
352       
353   sceneId = sceneListAddScene( nameStr, groupId );
354   zllSocStoreScene(groupId, sceneId, dstAddr, endpoint, addrMode);
355   RPCS_ZLL_CallBack_addSceneRsp(groupId, sceneId, nameStr, clientFd);
357   free(nameStr);
358   
359   //printf("RPCS_ZLL_storeScene--\n");
360   
361   return 0;
364 /*********************************************************************
365  * @fn          uint8_t RPCS_ZLL_recallScene(uint8_t *pBuf, uint32 clientFd)
366  *
367  * @brief       This function exposes an interface to recall a scene.
368  *
369  * @param       pBuf - incomin messages
370  *
371  * @return      afStatus_t
372  */
373 static uint8_t RPCS_ZLL_recallScene(uint8_t *pBuf, uint32 clientFd)
375   uint16_t dstAddr;
376   uint8_t addrMode;
377   uint8_t endpoint;
378   char *nameStr;
379   uint8_t nameLen;
380   uint16_t groupId;
381   uint8_t sceneId;
382   
383   //printf("RPCS_ZLL_recallScene++\n");
384        
385   //increment past SRPC header
386   pBuf+=2;
388   addrMode = (afAddrMode_t)*pBuf++;   
389   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
390   pBuf += Z_EXTADDR_LEN;
391   endpoint = *pBuf++;
392   // index past panId
393   pBuf += 2;
394   
395   groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
396   pBuf += 2;
397     
398   nameLen = *pBuf++;
399   nameStr = malloc(nameLen + 1);
400   nameStr[0] = nameLen;
401   int i;
402   for(i = 0; i < nameLen; i++)
403   {
404     nameStr[i+1] = *pBuf++;
405   } 
406   
407   //printf("RPCS_ZLL_recallScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
408     
409   sceneId = sceneListGetSceneId( nameStr, groupId );
410   zllSocRecallScene(groupId, sceneId, dstAddr, endpoint, addrMode);
412   free(nameStr);
413   
414   //printf("RPCS_ZLL_recallScene--\n");
415   
416   return 0;
419 /*********************************************************************
420  * @fn          uint8_t RPCS_ZLL_identifyDevice(uint8_t *pBuf, uint32_t clientFd)
421  *
422  * @brief       This function exposes an interface to make a device identify.
423  *
424  * @param       pBuf - incomin messages
425  *
426  * @return      afStatus_t
427  */
428 static uint8_t RPCS_ZLL_identifyDevice(uint8_t *pBuf, uint32_t clientFd)
430   afAddrType_t dstAddr;
431   uint16_t identifyTime;
432   
433   //printf("RPCS_ZLL_identifyDevice++\n");
434        
435   //increment past SRPC header
436   pBuf+=2;
438   dstAddr.addrMode = (afAddrMode_t)*pBuf++;   
439   if (dstAddr.addrMode == afAddr64Bit)
440   {
441     memcpy(dstAddr.addr.extAddr, pBuf, Z_EXTADDR_LEN);
442   }
443   else
444   {
445     dstAddr.addr.shortAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
446   }
447   pBuf += Z_EXTADDR_LEN;
449   dstAddr.endPoint = *pBuf++;
450   dstAddr.panId = BUILD_UINT16(pBuf[0], pBuf[1]);
451   pBuf += 2;
452   
453   identifyTime = BUILD_UINT16(pBuf[0], pBuf[1]);
454   
455   //TODO: implement zllSocIdentify
456          
457                             
458   return 0;
461 /*********************************************************************
462  * @fn          RSPC_ZLL_bindDevices
463  *
464  * @brief       This function exposes an interface to set a bind devices.
465  *
466  * @param       pBuf - incomin messages
467  *
468  * @return      afStatus_t
469  */
470 uint8_t RSPC_ZLL_bindDevices(uint8_t *pBuf, uint32 clientFd)
471 {  
472   uint16_t srcNwkAddr;
473   uint8_t srcEndpoint;
474   uint8 srcIEEE[8];
475   uint8_t dstEndpoint;
476   uint8 dstIEEE[8];
477   uint16 clusterId;
478    
479   //printf("RSPC_ZLL_bindDevices++\n");   
480         
481   //increment past SRPC header
482   pBuf+=2;  
483   
484   /* Src Address */
485   srcNwkAddr = BUILD_UINT16( pBuf[0], pBuf[1] );
486   pBuf += 2;  
488   srcEndpoint = *pBuf++;
489   
490   memcpy(srcIEEE, pBuf, Z_EXTADDR_LEN);
491   pBuf += Z_EXTADDR_LEN;  
493   dstEndpoint = *pBuf++;
494     
495   memcpy(dstIEEE, pBuf, Z_EXTADDR_LEN);
496   pBuf += Z_EXTADDR_LEN;   
497   
498   clusterId = BUILD_UINT16(pBuf[0], pBuf[1]);
499   pBuf += 2;     
500   
501   zllSocBind(srcNwkAddr, srcEndpoint, srcIEEE, dstEndpoint, dstIEEE, clusterId);
503   return 0;
506 /*********************************************************************
507  * @fn          RPCS_ZLL_setDeviceState
508  *
509  * @brief       This function exposes an interface to set a devices on/off attribute.
510  *
511  * @param       pBuf - incomin messages
512  *
513  * @return      afStatus_t
514  */
515 static uint8_t RPCS_ZLL_setDeviceState(uint8_t *pBuf, uint32_t clientFd)
517   uint8_t endpoint, addrMode;
518   uint16_t dstAddr;
519   bool state;
520  
521   //printf("RPCS_ZLL_setDeviceState++\n");
522        
523   //increment past SRPC header
524   pBuf+=2;
526   addrMode = (afAddrMode_t)*pBuf++;   
527   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
528   pBuf += Z_EXTADDR_LEN;
529   endpoint = *pBuf++;
530   // index past panId
531   pBuf += 2;
532   
533   state = (bool)*pBuf;
534   
535   //printf("RPCS_ZLL_setDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x, state=%x\n", dstAddr, endpoint, addrMode, state); 
536     
537   // Set light state on/off
538   zllSocSetState(state, dstAddr, endpoint, addrMode);
540   //printf("RPCS_ZLL_setDeviceState--\n");
541   
542   return 0;
545 /*********************************************************************
546  * @fn          RPCS_ZLL_setDeviceLevel
547  *
548  * @brief       This function exposes an interface to set a devices level attribute.
549  *
550  * @param       pBuf - incomin messages
551  *
552  * @return      afStatus_t
553  */
554 static uint8_t RPCS_ZLL_setDeviceLevel(uint8_t *pBuf, uint32_t clientFd)
556   uint8_t endpoint, addrMode;
557   uint16_t dstAddr;
558   uint8_t level; 
559   uint16_t transitionTime;
560  
561   //printf("RPCS_ZLL_setDeviceLevel++\n");   
562       
563   //increment past SRPC header
564   pBuf+=2;
566   addrMode = (afAddrMode_t)*pBuf++;   
567   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
568   pBuf += Z_EXTADDR_LEN;
569   endpoint = *pBuf++;
570   // index past panId
571   pBuf += 2;
572   
573   level = *pBuf++;
574   
575   transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);  
576   pBuf += 2;
577   
578   //printf("RPCS_ZLL_setDeviceLevel: dstAddr.addr.shortAddr=%x ,level=%x, tr=%x \n", dstAddr, level, transitionTime); 
579     
580   zllSocSetLevel(level, transitionTime, dstAddr, endpoint, addrMode);
581   
582   //printf("RPCS_ZLL_setDeviceLevel--\n");
583   
584   return 0;
587 /*********************************************************************
588  * @fn          RPCS_ZLL_setDeviceColor
589  *
590  * @brief       This function exposes an interface to set a devices hue and saturation attribute.
591  *
592  * @param       pBuf - incomin messages
593  *
594  * @return      afStatus_t
595  */
596 static uint8_t RPCS_ZLL_setDeviceColor(uint8_t *pBuf, uint32_t clientFd)
598   uint8_t endpoint, addrMode;
599   uint16_t dstAddr;
600   uint8_t hue, saturation; 
601   uint16_t transitionTime;
603   //printf("RPCS_ZLL_setDeviceColor++\n");   
604       
605   //increment past SRPC header
606   pBuf+=2;
608   addrMode = (afAddrMode_t)*pBuf++;   
609   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
610   pBuf += Z_EXTADDR_LEN;
611   endpoint = *pBuf++;
612   // index past panId
613   pBuf += 2;
614   
615   hue = *pBuf++;
616   
617   saturation = *pBuf++;
618   
619   transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);  
620   pBuf += 2;
621   
622   //printf("RPCS_ZLL_setDeviceColor: dstAddr=%x ,hue=%x, saturation=%x, tr=%x \n", dstAddr, hue, saturation, transitionTime); 
623     
624   zllSocSetHueSat(hue, saturation, transitionTime, dstAddr, endpoint, addrMode);
625   
626   //printf("RPCS_ZLL_setDeviceColor--\n");
627   
628   return 0;
631 /*********************************************************************
632  * @fn          RPCS_ZLL_getDeviceState
633  *
634  * @brief       This function exposes an interface to get a devices on/off attribute.
635  *
636  * @param       pBuf - incomin messages
637  *
638  * @return      afStatus_t
639  */
640 static uint8_t RPCS_ZLL_getDeviceState(uint8_t *pBuf, uint32_t clientFd)
642   uint8_t endpoint, addrMode;
643   uint16_t dstAddr;
644  
645   //printf("RPCS_ZLL_getDeviceState++\n");
646        
647   //increment past SRPC header
648   pBuf+=2;
650   addrMode = (afAddrMode_t)*pBuf++;   
651   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
652   pBuf += Z_EXTADDR_LEN;
653   endpoint = *pBuf++;
654   // index past panId
655   pBuf += 2;
656   
657   //printf("RPCS_ZLL_getDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x", dstAddr, endpoint, addrMode); 
658     
659   // Get light state on/off
660   zllSocGetState(dstAddr, endpoint, addrMode);
662   //printf("RPCS_ZLL_getDeviceState--\n");
663   
664   return 0;
667 /*********************************************************************
668  * @fn          RPCS_ZLL_getDeviceLevel
669  *
670  * @brief       This function exposes an interface to get a devices level attribute.
671  *
672  * @param       pBuf - incomin messages
673  *
674  * @return      afStatus_t
675  */
676 static uint8_t RPCS_ZLL_getDeviceLevel(uint8_t *pBuf, uint32_t clientFd)
678   uint8_t endpoint, addrMode;
679   uint16_t dstAddr;
680  
681   //printf("RPCS_ZLL_getDeviceLevel++\n");
682        
683   //increment past SRPC header
684   pBuf+=2;
686   addrMode = (afAddrMode_t)*pBuf++;   
687   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
688   pBuf += Z_EXTADDR_LEN;
689   endpoint = *pBuf++;
690   // index past panId
691   pBuf += 2;
692   
693   //printf("RPCS_ZLL_getDeviceLevel: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode); 
694     
695   // Get light level
696   zllSocGetLevel(dstAddr, endpoint, addrMode);
698   //printf("RPCS_ZLL_getDeviceLevel--\n");
699   
700   return 0;
703 /*********************************************************************
704  * @fn          RPCS_ZLL_getDeviceHue
705  *
706  * @brief       This function exposes an interface to get a devices hue attribute.
707  *
708  * @param       pBuf - incomin messages
709  *
710  * @return      afStatus_t
711  */
712 static uint8_t RPCS_ZLL_getDeviceHue(uint8_t *pBuf, uint32_t clientFd)
714   uint8_t endpoint, addrMode;
715   uint16_t dstAddr;
716  
717   //printf("RPCS_ZLL_getDeviceHue++\n");
718        
719   //increment past SRPC header
720   pBuf+=2;
722   addrMode = (afAddrMode_t)*pBuf++;   
723   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
724   pBuf += Z_EXTADDR_LEN;
725   endpoint = *pBuf++;
726   // index past panId
727   pBuf += 2;
728   
729   //printf("RPCS_ZLL_getDeviceHue: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode); 
730     
731   // Get light hue
732   zllSocGetHue(dstAddr, endpoint, addrMode);
734   //printf("RPCS_ZLL_getDeviceHue--\n");
735   
736   return 0;
739 /*********************************************************************
740  * @fn          RPCS_ZLL_getDeviceSat
741  *
742  * @brief       This function exposes an interface to get a devices sat attribute.
743  *
744  * @param       pBuf - incomin messages
745  *
746  * @return      afStatus_t
747  */
748 static uint8_t RPCS_ZLL_getDeviceSat(uint8_t *pBuf, uint32_t clientFd)
750   uint8_t endpoint, addrMode;
751   uint16_t dstAddr;
752  
753   //printf("RPCS_ZLL_getDeviceSat++\n");
754        
755   //increment past SRPC header
756   pBuf+=2;
758   addrMode = (afAddrMode_t)*pBuf++;   
759   dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
760   pBuf += Z_EXTADDR_LEN;
761   endpoint = *pBuf++;
762   // index past panId
763   pBuf += 2;
764   
765   //printf("RPCS_ZLL_getDeviceSat: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode); 
766     
767   // Get light sat
768   zllSocGetSat(dstAddr, endpoint, addrMode);
770   //printf("RPCS_ZLL_getDeviceSat--\n");
771   
772   return 0;
775 /*********************************************************************
776  * @fn          RSPC_ZLL_close
777  *
778  * @brief       This function exposes an interface to allow an upper layer to close the interface.
779  *
780  * @param       pBuf - incomin messages
781  *
782  * @return      afStatus_t
783  */
784 uint8_t RSPC_ZLL_close(uint8_t *pBuf, uint32_t clientFd)
785
786   uint16_t magicNumber;
787     
788   //printf("RSPC_ZLL_close++\n");
790   //increment past SRPC header
791   pBuf+=2;
792   
793   magicNumber = BUILD_UINT16(pBuf[0], pBuf[1]);
794     
795   if(magicNumber == CLOSE_AUTH_NUM)
796   {
797     //Close the application  
798     socketSeverClose();
799     //TODO: Need to create API's and close other fd's
800     
801     exit(EXIT_SUCCESS );
802   }
803   
804   return 0; 
805
807 /*********************************************************************
808  * @fn          uint8_t RPCS_ZLL_getGroups(uint8_t *pBuf, uint32 clientFd)
809  *
810  * @brief       This function exposes an interface to get the group list.
811  *
812  * @param       pBuf - incomin messages
813  *
814  * @return      none
815  */
816 static uint8_t RPCS_ZLL_getGroups(uint8_t *pBuf, uint32 clientFd)
817 {  
818   groupListItem_t *group = groupListGetNextGroup(NULL);
819   
820   //printf("RPCS_ZLL_getGroups++\n");
821   
822   while(group != NULL)
823   {  
824     uint8_t *pSrpcMessage, *pBuf;            
825     //printf("RPCS_ZLL_getGroups: group != null\n");
826     
827     //printf("RPCS_ZLL_getGroups: malloc'ing %d bytes\n", (2 + (sizeof(uint16_t)) + ((uint8_t) (group->groupNameStr[0]))));
828     
829     //RpcMessage contains function ID param Data Len and param data
830     //2 (SRPC header) + sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (GroupName Len) + group->groupNameStr[0] (string)
831     pSrpcMessage = malloc(2 + (sizeof(uint16_t)) + sizeof(uint8_t) + ((uint8_t) (group->groupNameStr[0])));
832       
833      pBuf = pSrpcMessage;
834     
835     //Set func ID in RPCS buffer
836     *pBuf++ = RPCS_GET_GROUP_RSP;
837     //param size
838     //sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (GroupName Len) + group->groupNameStr[0] (string)
839     *pBuf++ = (sizeof(uint16_t) + sizeof(uint8_t) + group->groupNameStr[0]);
840           
841     *pBuf++ = group->groupId & 0xFF;
842     *pBuf++ = (group->groupId & 0xFF00) >> 8;
843     
844     *pBuf++ = group->groupNameStr[0];
845     
846     int i;
847     for(i = 0; i < group->groupNameStr[0]; i++)
848     {
849       *pBuf++ = group->groupNameStr[i+1];
850     }
851           
852     //printf("RPCS_ZLL_CallBack_addGroupRsp: groupName[%d] %s\n", group->groupNameStr[0], &(group->groupNameStr[1]));
853     //Send SRPC
854     srpcSend(pSrpcMessage, clientFd);  
855     free(pSrpcMessage);    
856     //get next group (NULL if all done)
857     group = groupListGetNextGroup(group->groupNameStr);
858   }
859   //printf("RPCS_ZLL_getGroups--\n");
860     
861   return 0;
864 /***************************************************************************************************
865  * @fn      RPCS_ZLL_CallBack_addGroupRsp
866  *
867  * @brief   Sends the groupId to the client after a groupAdd
868   *
869  * @return  Status
870  ***************************************************************************************************/
871 void RPCS_ZLL_CallBack_addGroupRsp(uint16_t groupId, char *nameStr, uint32 clientFd)
873   uint8_t *pSrpcMessage, *pBuf;  
874     
875   //printf("RPCS_ZLL_CallBack_addGroupRsp++\n");
876   
877   //printf("RPCS_ZLL_CallBack_addGroupRsp: malloc'ing %d bytes\n", 2 + 3 + nameStr[0]);
878   //RpcMessage contains function ID param Data Len and param data
879   pSrpcMessage = malloc(2 + 3 + nameStr[0]);
880   
881   pBuf = pSrpcMessage;
882   
883   //Set func ID in RPCS buffer
884   *pBuf++ = RPCS_ADD_GROUP_RSP;
885   //param size
886   *pBuf++ = 3 + nameStr[0];
887   
888   *pBuf++ = groupId & 0xFF;
889   *pBuf++ = (groupId & 0xFF00) >> 8;
890   
891   *pBuf++ = nameStr[0];
892   int i;
893   for(i = 0; i < nameStr[0]; i++)
894   {
895     *pBuf++ = nameStr[i+1];
896   }
897         
898   //printf("RPCS_ZLL_CallBack_addGroupRsp: groupName[%d] %s\n", nameStr[0], nameStr);
899   //Send SRPC
900   srpcSend(pSrpcMessage, clientFd);  
902   //printf("RPCS_ZLL_CallBack_addGroupRsp--\n");
903                     
904   return;              
907 /*********************************************************************
908  * @fn          uint8_t RPCS_ZLL_getScenes(uint8_t *pBuf, uint32 clientFd)
909  *
910  * @brief       This function exposes an interface to get the scenes defined for a group.
911  *
912  * @param       pBuf - incomin messages
913  *
914  * @return      afStatus_t
915  */
916 static uint8_t RPCS_ZLL_getScenes(uint8_t *pBuf, uint32 clientFd)
917 {  
918   sceneListItem_t *scene = sceneListGetNextScene(NULL, 0);
919   
920   //printf("RPCS_ZLL_getScenes++\n");
921   
922   while(scene != NULL)
923   {  
924     uint8_t *pSrpcMessage, *pBuf;            
925     //printf("RPCS_ZLL_getScenes: scene != null\n");
926     
927     //printf("RPCS_ZLL_getScenes: malloc'ing %d bytes\n", (2 + (sizeof(uint16_t)) + (sizeof(uint8_t)) + (sizeof(uint8_t)) + ((uint8_t) (scene->sceneNameStr[0]))));
928     
929     //RpcMessage contains function ID param Data Len and param data
930     //2 (SRPC header) + sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (SceneName Len) + scene->sceneNameStr[0] (string)
931     pSrpcMessage = malloc(2 + (sizeof(uint16_t)) + (sizeof(uint8_t)) + (sizeof(uint8_t)) + ((uint8_t) (scene->sceneNameStr[0])));
932       
933      pBuf = pSrpcMessage;
934     
935     //Set func ID in RPCS buffer
936     *pBuf++ = RPCS_GET_SCENE_RSP;
937     //param size
938     //sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (SceneName Len) + scene->sceneNameStr[0] (string)
939     *pBuf++ = (sizeof(uint16_t) + (sizeof(uint8_t)) + (sizeof(uint8_t)) + scene->sceneNameStr[0]);
940           
941     *pBuf++ = scene->groupId & 0xFF;
942     *pBuf++ = (scene->groupId & 0xFF00) >> 8;
943     
944     *pBuf++ = scene->sceneId;   
945     
946     *pBuf++ = scene->sceneNameStr[0];
947     int i;
948   for(i = 0; i < scene->sceneNameStr[0]; i++)
949     {
950       *pBuf++ = scene->sceneNameStr[i+1];
951     }
952           
953     //printf("RPCS_ZLL_getScenes: sceneName[%d] %s, groupId %x\n", scene->sceneNameStr[0], &(scene->sceneNameStr[1]), scene->groupId);
954     //Send SRPC
955     srpcSend(pSrpcMessage, clientFd);  
956     free(pSrpcMessage);    
957     //get next scene (NULL if all done)
958     scene = sceneListGetNextScene(scene->sceneNameStr, scene->groupId);
959   }
960   //printf("RPCS_ZLL_getScenes--\n");
961     
962   return 0;
965 /***************************************************************************************************
966  * @fn      RPCS_ZLL_CallBack_addSceneRsp
967  *
968  * @brief   Sends the sceneId to the client after a storeScene
969   *
970  * @return  Status
971  ***************************************************************************************************/
972 void RPCS_ZLL_CallBack_addSceneRsp(uint16_t groupId, uint8_t sceneId, char *nameStr, uint32 clientFd)
974   uint8_t *pSrpcMessage, *pBuf;  
975     
976   //printf("RPCS_ZLL_CallBack_addSceneRsp++\n");
977   
978   //printf("RPCS_ZLL_CallBack_addSceneRsp: malloc'ing %d bytes\n", 2 + 4 + nameStr[0]);
979   //RpcMessage contains function ID param Data Len and param data
980   pSrpcMessage = malloc(2 + 4 + nameStr[0]);
981   
982   pBuf = pSrpcMessage;
983   
984   //Set func ID in RPCS buffer
985   *pBuf++ = RPCS_ADD_SCENE_RSP;
986   //param size
987   *pBuf++ = 4 + nameStr[0];
988   
989   *pBuf++ = groupId & 0xFF;
990   *pBuf++ = (groupId & 0xFF00) >> 8;
991   
992   *pBuf++ = sceneId & 0xFF;  
993   
994   *pBuf++ = nameStr[0];
995   int i;
996   for(i = 0; i < nameStr[0]; i++)
997   {
998     *pBuf++ = nameStr[i+1];
999   }
1000         
1001   //printf("RPCS_ZLL_CallBack_addSceneRsp: groupName[%d] %s\n", nameStr[0], nameStr);
1002   //Send SRPC
1003   srpcSend(pSrpcMessage, clientFd);  
1005   //printf("RPCS_ZLL_CallBack_addSceneRsp--\n");
1006                     
1007   return;              
1010 /***************************************************************************************************
1011  * @fn      RPCS_ZLL_CallBack_getStateRsp
1012  *
1013  * @brief   Sends the get State Rsp to the client that sent a get state
1014   *
1015  * @return  Status
1016  ***************************************************************************************************/
1017 void RPCS_ZLL_CallBack_getStateRsp(uint8_t state, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd)
1019   uint8_t *pSrpcMessage, *pBuf;  
1020     
1021   //printf("RPCS_ZLL_CallBack_getStateRsp++\n");
1022   
1023   //printf("RPCS_ZLL_CallBack_getStateRsp: malloc'ing %d bytes\n", 2+ 4);
1024   //RpcMessage contains function ID param Data Len and param data
1025   pSrpcMessage = malloc(2+ 4);
1026   
1027   pBuf = pSrpcMessage;
1028   
1029   //Set func ID in RPCS buffer
1030   *pBuf++ = RPCS_GET_DEV_STATE_RSP;
1031   //param size
1032   *pBuf++ = 4;
1033     
1034   *pBuf++ = srcAddr & 0xFF;
1035   *pBuf++ = (srcAddr & 0xFF00) >> 8;
1036   *pBuf++ = endpoint; 
1037   *pBuf++ = state & 0xFF;  
1038         
1039   //printf("RPCS_ZLL_CallBack_getStateRsp: state=%x\n", state);
1040   
1041   //Store the device that sent the request, for now send to all clients
1042   srpcSendAll(pSrpcMessage);  
1044   //printf("RPCS_ZLL_CallBack_addSceneRsp--\n");
1045                     
1046   return;              
1049 /***************************************************************************************************
1050  * @fn      RPCS_ZLL_CallBack_getLevelRsp
1051  *
1052  * @brief   Sends the get Level Rsp to the client that sent a get level
1053   *
1054  * @return  Status
1055  ***************************************************************************************************/
1056 void RPCS_ZLL_CallBack_getLevelRsp(uint8_t level, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd)
1058   uint8_t *pSrpcMessage, *pBuf;  
1059     
1060   //printf("RPCS_ZLL_CallBack_getLevelRsp++\n");
1061   
1062   //printf("RPCS_ZLL_CallBack_getLevelRsp: malloc'ing %d bytes\n", 2 + 4);
1063   //RpcMessage contains function ID param Data Len and param data
1064   pSrpcMessage = malloc(2 + 4);
1065   
1066   pBuf = pSrpcMessage;
1067   
1068   //Set func ID in RPCS buffer
1069   *pBuf++ = RPCS_GET_DEV_LEVEL_RSP;
1070   //param size
1071   *pBuf++ = 4;
1072   
1073   *pBuf++ = srcAddr & 0xFF;
1074   *pBuf++ = (srcAddr & 0xFF00) >> 8;
1075   *pBuf++ = endpoint;   
1076   *pBuf++ = level & 0xFF;  
1077         
1078   //printf("RPCS_ZLL_CallBack_getLevelRsp: level=%x\n", level);
1079   
1080   //Store the device that sent the request, for now send to all clients
1081   srpcSendAll(pSrpcMessage);  
1083   //printf("RPCS_ZLL_CallBack_getLevelRsp--\n");
1084                     
1085   return;              
1088 /***************************************************************************************************
1089  * @fn      RPCS_ZLL_CallBack_getHueRsp
1090  *
1091  * @brief   Sends the get Hue Rsp to the client that sent a get hue
1092   *
1093  * @return  Status
1094  ***************************************************************************************************/
1095 void RPCS_ZLL_CallBack_getHueRsp(uint8_t hue, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd)
1097   uint8_t *pSrpcMessage, *pBuf;  
1098     
1099   //printf("RPCS_ZLL_CallBack_getLevelRsp++\n");
1100   
1101   //printf("RPCS_ZLL_CallBack_getHueRsp: malloc'ing %d bytes\n", 2 + 4);
1102   //RpcMessage contains function ID param Data Len and param data
1103   pSrpcMessage = malloc(2 + 4);
1104   
1105   pBuf = pSrpcMessage;
1106   
1107   //Set func ID in RPCS buffer
1108   *pBuf++ = RPCS_GET_DEV_HUE_RSP;
1109   //param size
1110   *pBuf++ = 4;
1111     
1112   *pBuf++ = srcAddr & 0xFF;
1113   *pBuf++ = (srcAddr & 0xFF00) >> 8;
1114   *pBuf++ = endpoint;   
1115   *pBuf++ = hue & 0xFF;  
1116         
1117   //printf("RPCS_ZLL_CallBack_getHueRsp: hue=%x\n", hue);
1118   
1119   //Store the device that sent the request, for now send to all clients
1120   srpcSendAll(pSrpcMessage);  
1122   //printf("RPCS_ZLL_CallBack_getHueRsp--\n");
1123                     
1124   return;              
1127 /***************************************************************************************************
1128  * @fn      RPCS_ZLL_CallBack_getSatRsp
1129  *
1130  * @brief   Sends the get Sat Rsp to the client that sent a get sat
1131   *
1132  * @return  Status
1133  ***************************************************************************************************/
1134 void RPCS_ZLL_CallBack_getSatRsp(uint8_t sat, uint16_t srcAddr, uint8_t endpoint, uint32_t clientFd)
1136   uint8_t *pSrpcMessage, *pBuf;  
1137     
1138   //printf("RPCS_ZLL_CallBack_getSatRsp++\n");
1139   
1140   //printf("RPCS_ZLL_CallBack_getSatRsp: malloc'ing %d bytes\n", 2 + 4);
1141   //RpcMessage contains function ID param Data Len and param data
1142   pSrpcMessage = malloc(2 + 4);
1143   
1144   pBuf = pSrpcMessage;
1145   
1146   //Set func ID in RPCS buffer
1147   *pBuf++ = RPCS_GET_DEV_SAT_RSP;
1148   //param size
1149   *pBuf++ = 4;
1150   
1151   *pBuf++ = srcAddr & 0xFF;
1152   *pBuf++ = (srcAddr & 0xFF00) >> 8;
1153   *pBuf++ = endpoint;   
1154   *pBuf++ = sat & 0xFF;  
1155         
1156   //printf("RPCS_ZLL_CallBack_getSatRsp: sat=%x\n", sat);
1157   
1158   //Store the device that sent the request, for now send to all clients
1159   srpcSendAll(pSrpcMessage);  
1161   //printf("RPCS_ZLL_CallBack_getSatRsp--\n");
1162                     
1163   return;              
1167 void error(const char *msg)
1169     perror(msg);
1170     exit(1);
1173 /*********************************************************************
1174  * @fn          RSPC_ZLL_getDevices
1175  *
1176  * @brief       This function exposes an interface to allow an upper layer to start device discovery.
1177  *
1178  * @param       pBuf - incomin messages
1179  *
1180  * @return      afStatus_t
1181  */
1182 static uint8_t RSPC_ZLL_getDevices(uint8_t *pBuf, uint32_t clientFd)
1183
1184   epInfo_t *epInfo;
1186   //printf("RSPC_ZLL_getDevices++ \n");
1188   epInfo = devListGetNextDev(0xFFFF, 0);
1189   
1190   while(epInfo)
1191   {  
1192     //Send epInfo
1193     uint8_t *pSrpcMessage = srpcParseEpInfo(epInfo);  
1194     //printf("RSPC_ZLL_getDevices: %x:%x:%x:%x\n", epInfo->nwkAddr, epInfo->endpoint, epInfo->profileID, epInfo->deviceID);
1195     //Send SRPC
1196     srpcSend(pSrpcMessage, clientFd);  
1197     free(pSrpcMessage); 
1198   
1199     usleep(1000);
1200     
1201     //get next device (NULL if all done)
1202     epInfo = devListGetNextDev(epInfo->nwkAddr, epInfo->endpoint);
1203   }
1204   
1205   return 0;  
1208 /*********************************************************************
1209  * @fn          RPSC_ZLL_notSupported
1210  *
1211  * @brief       This function is called for unsupported commands.
1212  *
1213  * @param       pBuf - incomin messages
1214  *
1215  * @return      afStatus_t
1216  */
1217 static uint8_t RPSC_ZLL_notSupported(uint8_t *pBuf, uint32_t clientFd)
1218 {   
1219   return 0;  
1222 /***************************************************************************************************
1223  * @fn      SRPC_Init
1224  *
1225  * @brief   initialises the RPC interface and waitsfor a client to connect.
1226  * @param   
1227  *
1228  * @return  Status
1229  ***************************************************************************************************/      
1230 void SRPC_Init( void )
1232   if(socketSeverInit(SRPC_TCP_PORT) == -1)
1233   {
1234     //exit if the server does not start
1235     exit(-1);
1236   }
1237   
1238   serverSocketConfig(SRPC_RxCB, SRPC_ConnectCB);  
1241 /*********************************************************************
1242  * @fn          RSPC_SendEpInfo
1243  *
1244  * @brief       This function exposes an interface to allow an upper layer to start send an ep indo to all devices.
1245  *
1246  * @param       epInfo - pointer to the epInfo to be sent
1247  *
1248  * @return      afStatus_t
1249  */
1250 uint8_t RSPC_SendEpInfo(epInfo_t *epInfo)
1251
1252   uint8_t *pSrpcMessage = srpcParseEpInfo(epInfo);  
1253   
1254   printf("RSPC_SendEpInfo++ %x:%x:%x:%x\n", epInfo->nwkAddr, epInfo->endpoint, epInfo->profileID, epInfo->deviceID);
1255     
1256   //Send SRPC
1257   srpcSendAll(pSrpcMessage);  
1258   free(pSrpcMessage); 
1259   
1260   printf("RSPC_SendEpInfo--\n");
1261   
1262   return 0;  
1265 /***************************************************************************************************
1266  * @fn      SRPC_ConnectCB
1267  *
1268  * @brief   Callback for connecting SRPC clients.
1269  *
1270  * @return  Status
1271  ***************************************************************************************************/
1272 void SRPC_ConnectCB( int clientFd )
1274   //printf("SRPC_ConnectCB++ \n");
1275 /*
1276   epInfo = devListGetNextDev(0xFFFF, 0);
1277   
1278   while(epInfo)
1279   {  
1280     printf("SRPC_ConnectCB: send epInfo\n");  
1281     //Send device Annce
1282     uint8_t *pSrpcMessage = srpcParseEpInfo(epInfo);  
1283     //Send SRPC
1284     srpcSend(pSrpcMessage, clientFd);  
1285     free(pSrpcMessage); 
1286   
1287     usleep(1000);
1288     
1289     //get next device (NULL if all done)
1290     epInfo = devListGetNextDev(epInfo->nwkAddr, epInfo->endpoint);
1291   }
1292 */     
1293   //printf("SRPC_ConnectCB--\n");
1295   
1296 /***************************************************************************************************
1297  * @fn      SRPC_RxCB
1298  *
1299  * @brief   Callback for Rx'ing SRPC messages.
1300  *
1301  * @return  Status
1302  ***************************************************************************************************/
1303 void SRPC_RxCB( int clientFd )
1305   char buffer[256]; 
1306   int byteToRead;
1307   int byteRead;
1308   int rtn;
1309       
1310   //printf("SRPC_RxCB++[%x]\n", clientFd);
1311     
1312   rtn = ioctl(clientFd, FIONREAD, &byteToRead);
1313   
1314   if(rtn !=0)
1315   {
1316     printf("SRPC_RxCB: Socket error\n");
1317   }      
1319   while(byteToRead)
1320   {
1321     bzero(buffer,256);
1322     byteRead = 0;
1323     //Get the CMD-ID and Len
1324     byteRead += read(clientFd,buffer,2);   
1325     //Get the rest of the message
1326     //printf("SRPC: reading %x byte message\n",buffer[SRPC_MSG_LEN]);
1327     byteRead += read(clientFd,&buffer[2],buffer[SRPC_MSG_LEN]);
1328     byteToRead -= byteRead;
1329     if (byteRead < 0) error("SRPC ERROR: error reading from socket\n");
1330     if (byteRead < buffer[SRPC_MSG_LEN]) error("SRPC ERROR: full message not read\n");         
1331     //printf("Read the message[%x]\n",byteRead);
1332     RPSC_ProcessIncoming((uint8_t*)buffer, clientFd);
1333   }
1334   
1335   //printf("SRPC_RxCB--\n");
1336     
1337   return; 
1342 /***************************************************************************************************
1343  * @fn      Closes the TCP port
1344  *
1345  * @brief   Send a message over SRPC.
1346  *
1347  * @return  Status
1348  ***************************************************************************************************/
1349 void SRPC_Close(void)
1351   socketSeverClose();