]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - server/Source/interface_srpcserver.c
add support for control bridge FW
[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 "zbSocCmd.h"
61 void SRPC_RxCB(int clientFd);
62 void SRPC_ConnectCB(int status);
64 static uint8_t SRPC_setDeviceState(uint8_t *pBuf, uint32_t clientFd);
65 static uint8_t SRPC_setDeviceLevel(uint8_t *pBuf, uint32_t clientFd);
66 static uint8_t SRPC_setDeviceColor(uint8_t *pBuf, uint32_t clientFd);
67 static uint8_t SRPC_getDeviceState(uint8_t *pBuf, uint32_t clientFd);
68 static uint8_t SRPC_getDeviceLevel(uint8_t *pBuf, uint32_t clientFd);
69 static uint8_t SRPC_getDeviceHue(uint8_t *pBuf, uint32_t clientFd);
70 static uint8_t SRPC_getDeviceSat(uint8_t *pBuf, uint32_t clientFd);
71 static uint8_t SRPC_bindDevices(uint8_t *pBuf, uint32_t clientFd);
72 static uint8_t SRPC_getGroups(uint8_t *pBuf, uint32_t clientFd);
73 static uint8_t SRPC_addGroup(uint8_t *pBuf, uint32_t clientFd);
74 static uint8_t SRPC_getScenes(uint8_t *pBuf, uint32_t clientFd);
75 static uint8_t SRPC_storeScene(uint8_t *pBuf, uint32_t clientFd);
76 static uint8_t SRPC_recallScene(uint8_t *pBuf, uint32_t clientFd);
77 static uint8_t SRPC_identifyDevice(uint8_t *pBuf, uint32_t clientFd);
78 static uint8_t SRPC_close(uint8_t *pBuf, uint32_t clientFd);
79 static uint8_t SRPC_getDevices(uint8_t *pBuf, uint32_t clientFd);
80 static uint8_t SRPC_permitJoin(uint8_t *pBuf, uint32_t clientFd);
81 static uint8_t SRPC_changeDeviceName(uint8_t *pBuf, uint32_t clientFd);
82 static uint8_t SRPC_notSupported(uint8_t *pBuf, uint32_t clientFd);
84 //RPSC ZLL Interface call back functions
85 static void SRPC_CallBack_addGroupRsp(uint16_t groupId, char *nameStr,
86                 uint32_t clientFd);
87 static void SRPC_CallBack_addSceneRsp(uint16_t groupId, uint8_t sceneId,
88                 char *nameStr, uint32_t clientFd);
90 static uint8_t* srpcParseEpInfo(epInfoExtended_t* epInfoEx);
92 typedef uint8_t (*srpcProcessMsg_t)(uint8_t *pBuf, uint32_t clientFd);
94 srpcProcessMsg_t rpcsProcessIncoming[] =
95 { SRPC_close, //SRPC_CLOSE
96                 SRPC_getDevices, //SRPC_GET_DEVICES
97                 SRPC_setDeviceState, //SRPC_SET_DEV_STATE
98                 SRPC_setDeviceLevel, //SRPC_SET_DEV_LEVEL
99                 SRPC_setDeviceColor, //SRPC_SET_DEV_COLOR
100                 SRPC_getDeviceState, //SRPC_GET_DEV_STATE
101                 SRPC_getDeviceLevel, //SRPC_GET_DEV_LEVEL
102                 SRPC_getDeviceHue, //SRPC_GET_DEV_HUE
103                 SRPC_getDeviceSat, //SRPC_GET_DEV_HUE
104                 SRPC_bindDevices, //SRPC_BIND_DEVICES
105                 SRPC_notSupported, //reserved
106                 SRPC_notSupported, //reserved
107                 SRPC_notSupported, //reserved
108                 SRPC_notSupported, //reserved
109                 SRPC_getGroups, //SRPC_GET_GROUPS
110                 SRPC_addGroup, //SRPC_ADD_GROUP
111                 SRPC_getScenes, //SRPC_GET_SCENES
112                 SRPC_storeScene, //SRPC_STORE_SCENE
113                 SRPC_recallScene, //SRPC_RECALL_SCENE
114                 SRPC_identifyDevice, //SRPC_IDENTIFY_DEVICE
115                 SRPC_changeDeviceName, //SRPC_CHANGE_DEVICE_NAME
116                 SRPC_notSupported, //SRPC_REMOVE_DEVICE
117                 SRPC_notSupported, //SRPC_RESERVED_10
118                 SRPC_notSupported, //SRPC_RESERVED_11
119                 SRPC_notSupported, //SRPC_RESERVED_12
120                 SRPC_notSupported, //SRPC_RESERVED_13
121                 SRPC_notSupported, //SRPC_RESERVED_14
122                 SRPC_notSupported, //SRPC_RESERVED_15
123                 SRPC_permitJoin, //SRPC_PERMIT_JOIN
124                 };
126 static void srpcSend(uint8_t* srpcMsg, int fdClient);
127 static void srpcSendAll(uint8_t* srpcMsg);
129 /***************************************************************************************************
130  * @fn      srpcParseEpInfp - Parse epInfo and prepare the SRPC message.
131  *
132  * @brief   Parse epInfo and prepare the SRPC message.
133  * @param   epInfo_t* epInfo
134  *
135  * @return  pSrpcMessage
136  ***************************************************************************************************/
137 static uint8_t* srpcParseEpInfo(epInfoExtended_t* epInfoEx)
139         uint8_t i;
140         uint8_t *pSrpcMessage, *pTmp, devNameLen = 0, pSrpcMessageLen;
142         //printf("srpcParseEpInfo++\n");
144         //RpcMessage contains function ID param Data Len and param data
145         if (epInfoEx->epInfo->deviceName)
146         {
147                 devNameLen = strlen(epInfoEx->epInfo->deviceName);
148         }
150         //sizre of EP infor - the name char* + num bytes of device name
151         //pSrpcMessageLen = sizeof(epInfo_t) - sizeof(char*) + devNameLen;
152         pSrpcMessageLen = 2 /* network address */
153         + 1 /* endpoint */
154         + 2 /* profile ID */
155         + 2 /* device ID */
156         + 1 /* version */
157         + 1 /* device name length */
158         + devNameLen /* device name */
159         + 1 /* status */
160         + 8 /* IEEE address */
161         + 1 /* type */
162         + 2 /* previous network address */
163         + 1; /* flags */
164         pSrpcMessage = malloc(pSrpcMessageLen + 2);
166         pTmp = pSrpcMessage;
168         if (pSrpcMessage)
169         {
170                 //Set func ID in SRPC buffer
171                 *pTmp++ = SRPC_NEW_DEVICE;
172                 //param size
173                 *pTmp++ = pSrpcMessageLen;
175                 *pTmp++ = LO_UINT16(epInfoEx->epInfo->nwkAddr);
176                 *pTmp++ = HI_UINT16(epInfoEx->epInfo->nwkAddr);
177                 *pTmp++ = epInfoEx->epInfo->endpoint;
178                 *pTmp++ = LO_UINT16(epInfoEx->epInfo->profileID);
179                 *pTmp++ = HI_UINT16(epInfoEx->epInfo->profileID);
180                 *pTmp++ = LO_UINT16(epInfoEx->epInfo->deviceID);
181                 *pTmp++ = HI_UINT16(epInfoEx->epInfo->deviceID);
182                 *pTmp++ = epInfoEx->epInfo->version;
184                 if (devNameLen > 0)
185                 {
186                         *pTmp++ = devNameLen;
187                         for (i = 0; i < devNameLen; i++)
188                         {
189                                 *pTmp++ = epInfoEx->epInfo->deviceName[i];
190                         }
191                         printf("srpcParseEpInfo: name:%s\n", epInfoEx->epInfo->deviceName);
192                 }
193                 else
194                 {
195                         *pTmp++ = 0;
196                 }
197                 *pTmp++ = epInfoEx->epInfo->status;
199                 for (i = 0; i < 8; i++)
200                 {
201                         //printf("srpcParseEpInfp: IEEEAddr[%d] = %x\n", i, epInfo->IEEEAddr[i]);
202                         *pTmp++ = epInfoEx->epInfo->IEEEAddr[i];
203                 }
205                 *pTmp++ = epInfoEx->type;
206                 *pTmp++ = LO_UINT16(epInfoEx->prevNwkAddr);
207                 *pTmp++ = HI_UINT16(epInfoEx->prevNwkAddr);
208                 *pTmp++ = epInfoEx->epInfo->flags; //bit 0 : start, bit 1: end
210         }
211         //printf("srpcParseEpInfp--\n");
213 //  printf("srpcParseEpInfo %0x:%0x\n", epInfo->nwkAddr, epInfo->endpoint);   
215         printf(
216                         "srpcParseEpInfo: %s device, nwkAddr=0x%04X, endpoint=0x%X, profileID=0x%04X, deviceID=0x%04X, flags=0x%02X",
217                         epInfoEx->type == EP_INFO_TYPE_EXISTING ? "EXISTING" :
218                         epInfoEx->type == EP_INFO_TYPE_NEW ? "NEW" : "UPDATED",
219                         epInfoEx->epInfo->nwkAddr, epInfoEx->epInfo->endpoint,
220                         epInfoEx->epInfo->profileID, epInfoEx->epInfo->deviceID,
221                         epInfoEx->epInfo->flags);
223         if (epInfoEx->type == EP_INFO_TYPE_UPDATED)
224         {
225                 printf(", prevNwkAddr=0x%04X\n", epInfoEx->prevNwkAddr);
226         }
228         printf("\n");
230         return pSrpcMessage;
233 /***************************************************************************************************
234  * @fn      srpcSend
235  *
236  * @brief   Send a message over SRPC to a clients.
237  * @param   uint8_t* srpcMsg - message to be sent
238  *
239  * @return  Status
240  ***************************************************************************************************/
241 static void srpcSend(uint8_t* srpcMsg, int fdClient)
243         int rtn;
245         rtn = socketSeverSend(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2), fdClient);
246         if (rtn < 0)
247         {
248                 printf("ERROR writing to socket\n");
249         }
251         return;
254 /***************************************************************************************************
255  * @fn      srpcSendAll
256  *
257  * @brief   Send a message over SRPC to all clients.
258  * @param   uint8_t* srpcMsg - message to be sent
259  *
260  * @return  Status
261  ***************************************************************************************************/
262 static void srpcSendAll(uint8_t* srpcMsg)
264         int rtn;
266         rtn = socketSeverSendAllclients(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2));
267         if (rtn < 0)
268         {
269                 printf("ERROR writing to socket\n");
270         }
272         return;
275 /*********************************************************************
276  * @fn          SRPC_ProcessIncoming
277  *
278  * @brief       This function processes incoming messages.
279  *
280  * @param       pBuf - incomin messages
281  *
282  * @return      afStatus_t
283  */
284 void SRPC_ProcessIncoming(uint8_t *pBuf, uint32_t clientFd)
286         srpcProcessMsg_t func;
288 //  printf("SRPC_ProcessIncoming++[%x]\n", pBuf[SRPC_FUNC_ID]);
289         /* look up and call processing function */
290         func = rpcsProcessIncoming[(pBuf[SRPC_FUNC_ID] & ~(0x80))];
291         if (func)
292         {
293                 (*func)(pBuf, clientFd);
294         }
295         else
296         {
297                 //printf("Error: no processing function for CMD 0x%x\n", pBuf[SRPC_FUNC_ID]);
298         }
300         //printf("SRPC_ProcessIncoming--\n");
303 /*********************************************************************
304  * @fn          SRPC_addGroup
305  *
306  * @brief       This function exposes an interface to add a devices to a group.
307  *
308  * @param       pBuf - incomin messages
309  *
310  * @return      afStatus_t
311  */
312 static uint8_t SRPC_addGroup(uint8_t *pBuf, uint32_t clientFd)
314         uint16_t dstAddr;
315         uint8_t addrMode;
316         uint8_t endpoint;
317         char *nameStr;
318         uint8_t nameLen;
319         uint16_t groupId;
321         //printf("SRPC_addGroup++\n");
323         //increment past SRPC header
324         pBuf += 2;
326         addrMode = (afAddrMode_t) *pBuf++;
327         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
328         pBuf += Z_EXTADDR_LEN;
329         endpoint = *pBuf++;
330         // index past panId
331         pBuf += 2;
333         nameLen = *pBuf++;
334         nameStr = malloc(nameLen + 1);
335         int i;
336         for (i = 0; i < nameLen; i++)
337         {
338                 nameStr[i] = *pBuf++;
339         }
340         nameStr[nameLen] = '\0';
342         printf("SRPC_addGroup++: %x:%x:%x name[%d] %s \n", dstAddr, addrMode,
343                         endpoint, nameLen, nameStr);
345         groupId = groupListAddDeviceToGroup(nameStr, dstAddr, endpoint);
346         zbSocAddGroup(groupId, dstAddr, endpoint, addrMode);
348         SRPC_CallBack_addGroupRsp(groupId, nameStr, clientFd);
350         free(nameStr);
352         //printf("SRPC_addGroup--\n");
354         return 0;
357 /*********************************************************************
358  * @fn          uint8_t SRPC_storeScene(uint8_t *pBuf, uint32_t clientFd)
359  *
360  * @brief       This function exposes an interface to store a scene.
361  *
362  * @param       pBuf - incomin messages
363  *
364  * @return      afStatus_t
365  */
366 static uint8_t SRPC_storeScene(uint8_t *pBuf, uint32_t clientFd)
368         uint16_t dstAddr;
369         uint8_t addrMode;
370         uint8_t endpoint;
371         char *nameStr;
372         uint8_t nameLen;
373         uint16_t groupId;
374         uint8_t sceneId;
376         //printf("SRPC_storeScene++\n");
378         //increment past SRPC header
379         pBuf += 2;
381         addrMode = (afAddrMode_t) *pBuf++;
382         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
383         pBuf += Z_EXTADDR_LEN;
384         endpoint = *pBuf++;
385         // index past panId
386         pBuf += 2;
388         groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
389         pBuf += 2;
391         nameLen = *pBuf++;
392         nameStr = malloc(nameLen + 1);
393         int i;
394         for (i = 0; i < nameLen; i++)
395         {
396                 nameStr[i] = *pBuf++;
397         }
398         nameStr[nameLen] = '\0';
400         printf("SRPC_storeScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
402         sceneId = sceneListAddScene(nameStr, groupId);
403         zbSocStoreScene(groupId, sceneId, dstAddr, endpoint, addrMode);
404         SRPC_CallBack_addSceneRsp(groupId, sceneId, nameStr, clientFd);
406         free(nameStr);
408         //printf("SRPC_storeScene--: id:%x\n", sceneId);
410         return 0;
413 /*********************************************************************
414  * @fn          uint8_t SRPC_recallScene(uint8_t *pBuf, uint32_t clientFd)
415  *
416  * @brief       This function exposes an interface to recall a scene.
417  *
418  * @param       pBuf - incomin messages
419  *
420  * @return      afStatus_t
421  */
422 static uint8_t SRPC_recallScene(uint8_t *pBuf, uint32_t clientFd)
424         uint16_t dstAddr;
425         uint8_t addrMode;
426         uint8_t endpoint;
427         char *nameStr;
428         uint8_t nameLen;
429         uint16_t groupId;
430         uint8_t sceneId;
432         //printf("SRPC_recallScene++\n");
434         //increment past SRPC header
435         pBuf += 2;
437         addrMode = (afAddrMode_t) *pBuf++;
438         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
439         pBuf += Z_EXTADDR_LEN;
440         endpoint = *pBuf++;
441         // index past panId
442         pBuf += 2;
444         groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
445         pBuf += 2;
447         nameLen = *pBuf++;
448         nameStr = malloc(nameLen + 1);
449         int i;
450         for (i = 0; i < nameLen; i++)
451         {
452                 nameStr[i] = *pBuf++;
453         }
454         nameStr[nameLen] = '\0';
456         printf("SRPC_recallScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
458         sceneId = sceneListGetSceneId(nameStr, groupId);
459         zbSocRecallScene(groupId, sceneId, dstAddr, endpoint, addrMode);
461         free(nameStr);
463         //printf("SRPC_recallScene--: id:%x\n", sceneId);
465         return 0;
468 /*********************************************************************
469  * @fn          uint8_t SRPC_identifyDevice(uint8_t *pBuf, uint32_t clientFd)
470  *
471  * @brief       This function exposes an interface to make a device identify.
472  *
473  * @param       pBuf - incomin messages
474  *
475  * @return      afStatus_t
476  */
477 static uint8_t SRPC_identifyDevice(uint8_t *pBuf, uint32_t clientFd)
479         uint8_t endpoint, addrMode;
480         uint16_t dstAddr;
481         uint16_t identifyTime;
483         //printf("SRPC_identifyDevice++\n");
485         //increment past SRPC header
486         pBuf += 2;
488         addrMode = (afAddrMode_t) *pBuf++;
489         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
490         pBuf += Z_EXTADDR_LEN;
491         endpoint = *pBuf++;
492         // index past panId
493         pBuf += 2;
495         identifyTime = BUILD_UINT16(pBuf[0], pBuf[1]);
496         pBuf += 2;
498         zbSocSendIdentify(identifyTime, dstAddr, endpoint, addrMode);
500         return 0;
503 /*********************************************************************
504  * @fn          SRPC_bindDevices
505  *
506  * @brief       This function exposes an interface to set a bind devices.
507  *
508  * @param       pBuf - incomin messages
509  *
510  * @return      afStatus_t
511  */
512 uint8_t SRPC_bindDevices(uint8_t *pBuf, uint32_t clientFd)
514         uint16_t srcNwkAddr;
515         uint8_t srcEndpoint;
516         uint8 srcIEEE[8];
517         uint8_t dstEndpoint;
518         uint8 dstIEEE[8];
519         uint16 clusterId;
521         //printf("SRPC_bindDevices++\n");
523         //increment past SRPC header
524         pBuf += 2;
526         /* Src Address */
527         srcNwkAddr = BUILD_UINT16( pBuf[0], pBuf[1] );
528         pBuf += 2;
530         srcEndpoint = *pBuf++;
532         memcpy(srcIEEE, pBuf, Z_EXTADDR_LEN);
533         pBuf += Z_EXTADDR_LEN;
535         dstEndpoint = *pBuf++;
537         memcpy(dstIEEE, pBuf, Z_EXTADDR_LEN);
538         pBuf += Z_EXTADDR_LEN;
540         clusterId = BUILD_UINT16(pBuf[0], pBuf[1]);
541         pBuf += 2;
543         zbSocBind(srcNwkAddr, srcEndpoint, srcIEEE, dstEndpoint, dstIEEE, clusterId);
545         return 0;
548 /*********************************************************************
549  * @fn          SRPC_setDeviceState
550  *
551  * @brief       This function exposes an interface to set a devices on/off attribute.
552  *
553  * @param       pBuf - incomin messages
554  *
555  * @return      afStatus_t
556  */
557 static uint8_t SRPC_setDeviceState(uint8_t *pBuf, uint32_t clientFd)
559         uint8_t endpoint, addrMode;
560         uint16_t dstAddr;
561         bool state;
563         //printf("SRPC_setDeviceState++\n");
565         //increment past SRPC header
566         pBuf += 2;
568         addrMode = (afAddrMode_t) *pBuf++;
569         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
570         pBuf += Z_EXTADDR_LEN;
571         endpoint = *pBuf++;
572         // index past panId
573         pBuf += 2;
575         state = (bool) *pBuf;
577         //printf("SRPC_setDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x, state=%x\n", dstAddr, endpoint, addrMode, state);
579         // Set light state on/off
580         zbSocSetState(state, dstAddr, endpoint, addrMode);
582         //printf("SRPC_setDeviceState--\n");
584         return 0;
587 /*********************************************************************
588  * @fn          SRPC_setDeviceLevel
589  *
590  * @brief       This function exposes an interface to set a devices level attribute.
591  *
592  * @param       pBuf - incomin messages
593  *
594  * @return      afStatus_t
595  */
596 static uint8_t SRPC_setDeviceLevel(uint8_t *pBuf, uint32_t clientFd)
598         uint8_t endpoint, addrMode;
599         uint16_t dstAddr;
600         uint8_t level;
601         uint16_t transitionTime;
603         //printf("SRPC_setDeviceLevel++\n");
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;
615         level = *pBuf++;
617         transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);
618         pBuf += 2;
620         //printf("SRPC_setDeviceLevel: dstAddr.addr.shortAddr=%x ,level=%x, tr=%x \n", dstAddr, level, transitionTime);
622         zbSocSetLevel(level, transitionTime, dstAddr, endpoint, addrMode);
624         //printf("SRPC_setDeviceLevel--\n");
626         return 0;
629 /*********************************************************************
630  * @fn          SRPC_setDeviceColor
631  *
632  * @brief       This function exposes an interface to set a devices hue and saturation attribute.
633  *
634  * @param       pBuf - incomin messages
635  *
636  * @return      afStatus_t
637  */
638 static uint8_t SRPC_setDeviceColor(uint8_t *pBuf, uint32_t clientFd)
640         uint8_t endpoint, addrMode;
641         uint16_t dstAddr;
642         uint8_t hue, saturation;
643         uint16_t transitionTime;
645         //printf("SRPC_setDeviceColor++\n");
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;
657         hue = *pBuf++;
659         saturation = *pBuf++;
661         transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);
662         pBuf += 2;
664 //  printf("SRPC_setDeviceColor: dstAddr=%x ,hue=%x, saturation=%x, tr=%x \n", dstAddr, hue, saturation, transitionTime); 
666         zbSocSetHueSat(hue, saturation, transitionTime, dstAddr, endpoint, addrMode);
668         //printf("SRPC_setDeviceColor--\n");
670         return 0;
673 /*********************************************************************
674  * @fn          SRPC_getDeviceState
675  *
676  * @brief       This function exposes an interface to get a devices on/off attribute.
677  *
678  * @param       pBuf - incomin messages
679  *
680  * @return      afStatus_t
681  */
682 static uint8_t SRPC_getDeviceState(uint8_t *pBuf, uint32_t clientFd)
684         uint8_t endpoint, addrMode;
685         uint16_t dstAddr;
687         //printf("SRPC_getDeviceState++\n");
689         //increment past SRPC header
690         pBuf += 2;
692         addrMode = (afAddrMode_t) *pBuf++;
693         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
694         pBuf += Z_EXTADDR_LEN;
695         endpoint = *pBuf++;
696         // index past panId
697         pBuf += 2;
699         //printf("SRPC_getDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x", dstAddr, endpoint, addrMode);
701         // Get light state on/off
702         zbSocGetState(dstAddr, endpoint, addrMode);
704         //printf("SRPC_getDeviceState--\n");
706         return 0;
709 /*********************************************************************
710  * @fn          SRPC_getDeviceLevel
711  *
712  * @brief       This function exposes an interface to get a devices level attribute.
713  *
714  * @param       pBuf - incomin messages
715  *
716  * @return      afStatus_t
717  */
718 static uint8_t SRPC_getDeviceLevel(uint8_t *pBuf, uint32_t clientFd)
720         uint8_t endpoint, addrMode;
721         uint16_t dstAddr;
723         //printf("SRPC_getDeviceLevel++\n");
725         //increment past SRPC header
726         pBuf += 2;
728         addrMode = (afAddrMode_t) *pBuf++;
729         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
730         pBuf += Z_EXTADDR_LEN;
731         endpoint = *pBuf++;
732         // index past panId
733         pBuf += 2;
735         //printf("SRPC_getDeviceLevel: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
737         // Get light level
738         zbSocGetLevel(dstAddr, endpoint, addrMode);
740         //printf("SRPC_getDeviceLevel--\n");
742         return 0;
745 /*********************************************************************
746  * @fn          SRPC_getDeviceHue
747  *
748  * @brief       This function exposes an interface to get a devices hue attribute.
749  *
750  * @param       pBuf - incomin messages
751  *
752  * @return      afStatus_t
753  */
754 static uint8_t SRPC_getDeviceHue(uint8_t *pBuf, uint32_t clientFd)
756         uint8_t endpoint, addrMode;
757         uint16_t dstAddr;
759         //printf("SRPC_getDeviceHue++\n");
761         //increment past SRPC header
762         pBuf += 2;
764         addrMode = (afAddrMode_t) *pBuf++;
765         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
766         pBuf += Z_EXTADDR_LEN;
767         endpoint = *pBuf++;
768         // index past panId
769         pBuf += 2;
771         //printf("SRPC_getDeviceHue: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
773         // Get light hue
774         zbSocGetHue(dstAddr, endpoint, addrMode);
776         //printf("SRPC_getDeviceHue--\n");
778         return 0;
781 /*********************************************************************
782  * @fn          SRPC_getDeviceSat
783  *
784  * @brief       This function exposes an interface to get a devices sat attribute.
785  *
786  * @param       pBuf - incomin messages
787  *
788  * @return      afStatus_t
789  */
790 static uint8_t SRPC_getDeviceSat(uint8_t *pBuf, uint32_t clientFd)
792         uint8_t endpoint, addrMode;
793         uint16_t dstAddr;
795         //printf("SRPC_getDeviceSat++\n");
797         //increment past SRPC header
798         pBuf += 2;
800         addrMode = (afAddrMode_t) *pBuf++;
801         dstAddr = BUILD_UINT16(pBuf[0], pBuf[1]);
802         pBuf += Z_EXTADDR_LEN;
803         endpoint = *pBuf++;
804         // index past panId
805         pBuf += 2;
807         //printf("SRPC_getDeviceSat: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
809         // Get light sat
810         zbSocGetSat(dstAddr, endpoint, addrMode);
812         //printf("SRPC_getDeviceSat--\n");
814         return 0;
817 /*********************************************************************
818  * @fn          SRPC_changeDeviceName
819  *
820  * @brief       This function exposes an interface to set a bind devices.
821  *
822  * @param       pBuf - incomin messages
823  *
824  * @return      afStatus_t
825  */
826 static uint8_t SRPC_changeDeviceName(uint8_t *pBuf, uint32_t clientFd)
828         uint16_t devNwkAddr;
829         uint8_t devEndpoint;
830         uint8_t nameLen;
831         epInfo_t * epInfo;
833         printf("RSPC_ZLL_changeDeviceName++\n");
835         //increment past SRPC header
836         pBuf += 2;
838         // Src Address
839         devNwkAddr = BUILD_UINT16( pBuf[0], pBuf[1] );
840         pBuf += 2;
842         devEndpoint = *pBuf++;
844         nameLen = *pBuf++;
845         if (nameLen > MAX_SUPPORTED_DEVICE_NAME_LENGTH)
846         {
847                 nameLen = MAX_SUPPORTED_DEVICE_NAME_LENGTH;
848         }
850         printf("RSPC_ZLL_changeDeviceName: renaming device %s, len=%d\n", pBuf,
851                         nameLen);
852         printf("RSPC_ZLL_changeDeviceName: devNwkAddr=%x, devEndpoint=%x\n",
853                         devNwkAddr, devEndpoint);
855         epInfo = devListRemoveDeviceByNaEp(devNwkAddr, devEndpoint);
856         if (epInfo != NULL)
857         {
858                 /*
859                  if(epInfo->deviceName != NULL)
860                  {
861                  free(epInfo->deviceName);
862                  }*/
863                 epInfo->deviceName = malloc(nameLen + 1);
864                 printf("RSPC_ZLL_changeDeviceName: renamed device %s, len=%d\n", pBuf,
865                                 nameLen);
866                 strncpy(epInfo->deviceName, (char *) pBuf, nameLen);
867                 epInfo->deviceName[nameLen] = '\0';
868                 devListAddDevice(epInfo);
869         }
871         return 0;
874 /*********************************************************************
875  * @fn          SRPC_close
876  *
877  * @brief       This function exposes an interface to allow an upper layer to close the interface.
878  *
879  * @param       pBuf - incomin messages
880  *
881  * @return      afStatus_t
882  */
883 uint8_t SRPC_close(uint8_t *pBuf, uint32_t clientFd)
885         uint16_t magicNumber;
887         //printf("SRPC_close++\n");
889         //increment past SRPC header
890         pBuf += 2;
892         magicNumber = BUILD_UINT16(pBuf[0], pBuf[1]);
894         if (magicNumber == CLOSE_AUTH_NUM)
895         {
896                 //Close the application
897                 socketSeverClose();
898                 //TODO: Need to create API's and close other fd's
900                 exit(EXIT_SUCCESS);
901         }
903         return 0;
906 /*********************************************************************
907  * @fn          uint8_t SRPC_permitJoin(uint8_t *pBuf, uint32_t clientFd)
908  *
909  * @brief       This function exposes an interface to permit other devices to join the network.
910  *
911  * @param       pBuf - incomin messages
912  *
913  * @return      none
914  */
915 static uint8_t SRPC_permitJoin(uint8_t *pBuf, uint32_t clientFd)
917         uint8_t duration;
918         uint16_t magicNumber;
920         //increment past SRPC header
921         pBuf += 2;
923         duration = *pBuf++;
924         magicNumber = BUILD_UINT16(pBuf[0], pBuf[1]);
926         if (magicNumber == JOIN_AUTH_NUM)
927         {
928                 //Open the network for joining
929                 zbSocOpenNwk(duration);
930         }
932         return 0;
935 /*********************************************************************
936  * @fn          uint8_t SRPC_getGroups(uint8_t *pBuf, uint32_t clientFd)
937  *
938  * @brief       This function exposes an interface to get the group list.
939  *
940  * @param       pBuf - incomin messages
941  *
942  * @return      none
943  */
944 static uint8_t SRPC_getGroups(uint8_t *pBuf, uint32_t clientFd)
946         uint32_t context = 0;
947         groupRecord_t *group = groupListGetNextGroup(&context);
948         uint8_t nameLen, nameIdx;
950         //printf("SRPC_getGroups++\n");
952         while (group != NULL)
953         {
954                 uint8_t *pSrpcMessage, *pBuf;
955                 //printf("SRPC_getGroups: group != null\n");
957                 //printf("SRPC_getGroups: malloc'ing %d bytes\n", (2 + (sizeof(uint16_t)) + ((uint8_t) (group->groupNameStr[0]))));
959                 //RpcMessage contains function ID param Data Len and param data
960                 //2 (SRPC header) + sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (GroupName Len) + group->groupNameStr[0] (string)
961                 pSrpcMessage = malloc(
962                                 2 + (sizeof(uint16_t)) + sizeof(uint8_t) + ((uint8_t)(group->name[0])));
964                 pBuf = pSrpcMessage;
966                 //Set func ID in RPCS buffer
967                 *pBuf++ = SRPC_GET_GROUP_RSP;
968                 //param size
969                 //sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (GroupName Len) + group->groupNameStr[0] (string)
970                 *pBuf++ = (sizeof(uint16_t) + sizeof(uint8_t) + group->name[0]);
972                 *pBuf++ = group->id & 0xFF;
973                 *pBuf++ = (group->id & 0xFF00) >> 8;
975                 nameLen = strlen(group->name);
976                 if (nameLen > 0)
977                 {
978                         *pBuf++ = nameLen;
979                         for (nameIdx = 0; nameIdx < nameLen; nameIdx++)
980                         {
981                                 *pBuf++ = group->name[nameIdx];
982                         }
983                         printf("SRPC_getGroups: name:%s\n", group->name);
984                 }
985                 else
986                 {
987                         *pBuf++ = 0;
988                 }
990                 //printf("SRPC_CallBack_addGroupRsp: groupName[%d] %s\n", group->groupNameStr[0], &(group->groupNameStr[1]));
991                 //Send SRPC
992                 srpcSend(pSrpcMessage, clientFd);
993                 free(pSrpcMessage);
994                 //get next group (NULL if all done)
995                 group = groupListGetNextGroup(&context);
996         }
997         //printf("SRPC_getGroups--\n");
999         return 0;
1002 /***************************************************************************************************
1003  * @fn      SRPC_CallBack_addGroupRsp
1004  *
1005  * @brief   Sends the groupId to the client after a groupAdd
1006  *
1007  * @return  Status
1008  ***************************************************************************************************/
1009 void SRPC_CallBack_addGroupRsp(uint16_t groupId, char *nameStr,
1010                 uint32_t clientFd)
1012         uint8_t *pSrpcMessage, *pBuf;
1014         //printf("SRPC_CallBack_addGroupRsp++\n");
1016         //printf("SRPC_CallBack_addGroupRsp: malloc'ing %d bytes\n", 2 + 3 + nameStr[0]);
1017         //RpcMessage contains function ID param Data Len and param data
1018         pSrpcMessage = malloc(2 + 3 + strlen(nameStr));
1020         pBuf = pSrpcMessage;
1022         //Set func ID in SRPC buffer
1023         *pBuf++ = SRPC_ADD_GROUP_RSP;
1024         //param size
1025         *pBuf++ = 3 + strlen(nameStr);
1027         *pBuf++ = groupId & 0xFF;
1028         *pBuf++ = (groupId & 0xFF00) >> 8;
1030         *pBuf++ = strlen(nameStr);
1031         int i;
1032         for (i = 0; i < strlen(nameStr); i++)
1033         {
1034                 *pBuf++ = nameStr[i];
1035         }
1037         printf("SRPC_CallBack_addGroupRsp: groupName[%d] %s\n", strlen(nameStr),
1038                         nameStr);
1039         //Send SRPC
1040         srpcSend(pSrpcMessage, clientFd);
1042         //printf("SRPC_CallBack_addGroupRsp--\n");
1044         return;
1047 /*********************************************************************
1048  * @fn          uint8_t SRPC_getScenes(uint8_t *pBuf, uint32_t clientFd)
1049  *
1050  * @brief       This function exposes an interface to get the scenes defined for a group.
1051  *
1052  * @param       pBuf - incomin messages
1053  *
1054  * @return      afStatus_t
1055  */
1056 static uint8_t SRPC_getScenes(uint8_t *pBuf, uint32_t clientFd)
1058         uint32_t context = 0;
1059         sceneRecord_t *scene = sceneListGetNextScene(&context);
1060         uint8_t nameLen, nameIdx;
1062         //printf("SRPC_getScenes++\n");
1064         while (scene != NULL)
1065         {
1066                 uint8_t *pSrpcMessage, *pBuf;
1067                 //printf("SRPC_getScenes: scene != null\n");
1069                 //printf("SRPC_getScenes: malloc'ing %d bytes\n", (2 + (sizeof(uint16_t)) + (sizeof(uint8_t)) + (sizeof(uint8_t)) + ((uint8_t) (scene->sceneNameStr[0]))));
1071                 //RpcMessage contains function ID param Data Len and param data
1072                 //2 (SRPC header) + sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (SceneName Len) + scene->sceneNameStr[0] (string)
1073                 pSrpcMessage = malloc(
1074                                 2 + (sizeof(uint16_t)) + (sizeof(uint8_t)) + (sizeof(uint8_t))
1075                                                 + ((uint8_t)(scene->name[0])));
1077                 pBuf = pSrpcMessage;
1079                 //Set func ID in RPCS buffer
1080                 *pBuf++ = SRPC_GET_SCENE_RSP;
1081                 //param size
1082                 //sizeof(uint16_t) (GroupId) + sizeof(uint8_t) (SceneId) + sizeof(uint8_t) (SceneName Len) + scene->sceneNameStr[0] (string)
1083                 *pBuf++ = (sizeof(uint16_t) + (sizeof(uint8_t)) + (sizeof(uint8_t))
1084                                 + scene->name[0]);
1086                 *pBuf++ = scene->groupId & 0xFF;
1087                 *pBuf++ = (scene->groupId & 0xFF00) >> 8;
1089                 *pBuf++ = scene->sceneId;
1091                 nameLen = strlen(scene->name);
1092                 if (nameLen > 0)
1093                 {
1094                         *pBuf++ = nameLen;
1095                         for (nameIdx = 0; nameIdx < nameLen; nameIdx++)
1096                         {
1097                                 *pBuf++ = scene->name[nameIdx];
1098                         }
1099                         printf("SRPC_getScenes: name:%s\n", scene->name);
1100                 }
1101                 else
1102                 {
1103                         *pBuf++ = 0;
1104                 }
1106                 //printf("SRPC_getScenes: sceneName[%d] %s, groupId %x\n", scene->sceneNameStr[0], &(scene->sceneNameStr[1]), scene->groupId);
1107                 //Send SRPC
1108                 srpcSend(pSrpcMessage, clientFd);
1109                 free(pSrpcMessage);
1110                 //get next scene (NULL if all done)
1111                 scene = sceneListGetNextScene(&context);
1112         }
1113         //printf("SRPC_getScenes--\n");
1115         return 0;
1118 /***************************************************************************************************
1119  * @fn      SRPC_CallBack_addSceneRsp
1120  *
1121  * @brief   Sends the sceneId to the client after a storeScene
1122  *
1123  * @return  Status
1124  ***************************************************************************************************/
1125 void SRPC_CallBack_addSceneRsp(uint16_t groupId, uint8_t sceneId, char *nameStr,
1126                 uint32_t clientFd)
1128         uint8_t *pSrpcMessage, *pBuf;
1130         //printf("SRPC_CallBack_addSceneRsp++\n");
1132         //printf("SRPC_CallBack_addSceneRsp: malloc'ing %d bytes\n", 2 + 4 + nameStr[0]);
1133         //RpcMessage contains function ID param Data Len and param data
1134         pSrpcMessage = malloc(2 + 4 + nameStr[0]);
1136         pBuf = pSrpcMessage;
1138         //Set func ID in SRPC buffer
1139         *pBuf++ = SRPC_ADD_SCENE_RSP;
1140         //param size
1141         *pBuf++ = 4 + nameStr[0];
1143         *pBuf++ = groupId & 0xFF;
1144         *pBuf++ = (groupId & 0xFF00) >> 8;
1146         *pBuf++ = sceneId & 0xFF;
1148         *pBuf++ = nameStr[0];
1149         int i;
1150         for (i = 0; i < nameStr[0]; i++)
1151         {
1152                 *pBuf++ = nameStr[i + 1];
1153         }
1155         //printf("SRPC_CallBack_addSceneRsp: groupName[%d] %s\n", nameStr[0], nameStr);
1156         //Send SRPC
1157         srpcSend(pSrpcMessage, clientFd);
1159         //printf("SRPC_CallBack_addSceneRsp--\n");
1161         return;
1164 /***************************************************************************************************
1165  * @fn      SRPC_CallBack_getStateRsp
1166  *
1167  * @brief   Sends the get State Rsp to the client that sent a get state
1168  *
1169  * @return  Status
1170  ***************************************************************************************************/
1171 void SRPC_CallBack_getStateRsp(uint8_t state, uint16_t srcAddr,
1172                 uint8_t endpoint, uint32_t clientFd)
1174         uint8_t * pBuf;
1175         uint8_t pSrpcMessage[2 + 4];
1177         //printf("SRPC_CallBack_getStateRsp++\n");
1179         //RpcMessage contains function ID param Data Len and param data
1181         pBuf = pSrpcMessage;
1183         //Set func ID in RPCS buffer
1184         *pBuf++ = SRPC_GET_DEV_STATE_RSP;
1185         //param size
1186         *pBuf++ = 4;
1188         *pBuf++ = srcAddr & 0xFF;
1189         *pBuf++ = (srcAddr & 0xFF00) >> 8;
1190         *pBuf++ = endpoint;
1191         *pBuf++ = state & 0xFF;
1193         //printf("SRPC_CallBack_getStateRsp: state=%x\n", state);
1195         //Store the device that sent the request, for now send to all clients
1196         srpcSendAll(pSrpcMessage);
1198         //printf("SRPC_CallBack_addSceneRsp--\n");
1200         return;
1203 /***************************************************************************************************
1204  * @fn      SRPC_CallBack_getLevelRsp
1205  *
1206  * @brief   Sends the get Level Rsp to the client that sent a get level
1207  *
1208  * @return  Status
1209  ***************************************************************************************************/
1210 void SRPC_CallBack_getLevelRsp(uint8_t level, uint16_t srcAddr,
1211                 uint8_t endpoint, uint32_t clientFd)
1213         uint8_t * pBuf;
1214         uint8_t pSrpcMessage[2 + 4];
1216         //printf("SRPC_CallBack_getLevelRsp++\n");
1218         //RpcMessage contains function ID param Data Len and param data
1220         pBuf = pSrpcMessage;
1222         //Set func ID in RPCS buffer
1223         *pBuf++ = SRPC_GET_DEV_LEVEL_RSP;
1224         //param size
1225         *pBuf++ = 4;
1227         *pBuf++ = srcAddr & 0xFF;
1228         *pBuf++ = (srcAddr & 0xFF00) >> 8;
1229         *pBuf++ = endpoint;
1230         *pBuf++ = level & 0xFF;
1232         //printf("SRPC_CallBack_getLevelRsp: level=%x\n", level);
1234         //Store the device that sent the request, for now send to all clients
1235         srpcSendAll(pSrpcMessage);
1237         //printf("SRPC_CallBack_getLevelRsp--\n");
1239         return;
1242 /***************************************************************************************************
1243  * @fn      SRPC_CallBack_getHueRsp
1244  *
1245  * @brief   Sends the get Hue Rsp to the client that sent a get hue
1246  *
1247  * @return  Status
1248  ***************************************************************************************************/
1249 void SRPC_CallBack_getHueRsp(uint8_t hue, uint16_t srcAddr, uint8_t endpoint,
1250                 uint32_t clientFd)
1252         uint8_t * pBuf;
1253         uint8_t pSrpcMessage[2 + 4];
1255         //printf("SRPC_CallBack_getLevelRsp++\n");
1257         //RpcMessage contains function ID param Data Len and param data
1259         pBuf = pSrpcMessage;
1261         //Set func ID in RPCS buffer
1262         *pBuf++ = SRPC_GET_DEV_HUE_RSP;
1263         //param size
1264         *pBuf++ = 4;
1266         *pBuf++ = srcAddr & 0xFF;
1267         *pBuf++ = (srcAddr & 0xFF00) >> 8;
1268         *pBuf++ = endpoint;
1269         *pBuf++ = hue & 0xFF;
1271         //printf("SRPC_CallBack_getHueRsp: hue=%x\n", hue);
1273         //Store the device that sent the request, for now send to all clients
1274         srpcSendAll(pSrpcMessage);
1276         //printf("SRPC_CallBack_getHueRsp--\n");
1278         return;
1281 /***************************************************************************************************
1282  * @fn      SRPC_CallBack_getSatRsp
1283  *
1284  * @brief   Sends the get Sat Rsp to the client that sent a get sat
1285  *
1286  * @return  Status
1287  ***************************************************************************************************/
1288 void SRPC_CallBack_getSatRsp(uint8_t sat, uint16_t srcAddr, uint8_t endpoint,
1289                 uint32_t clientFd)
1291         uint8_t * pBuf;
1292         uint8_t pSrpcMessage[2 + 4];
1294         //printf("SRPC_CallBack_getSatRsp++\n");
1296         //RpcMessage contains function ID param Data Len and param data
1298         pBuf = pSrpcMessage;
1300         //Set func ID in RPCS buffer
1301         *pBuf++ = SRPC_GET_DEV_SAT_RSP;
1302         //param size
1303         *pBuf++ = 4;
1305         *pBuf++ = srcAddr & 0xFF;
1306         *pBuf++ = (srcAddr & 0xFF00) >> 8;
1307         *pBuf++ = endpoint;
1308         *pBuf++ = sat & 0xFF;
1310         //printf("SRPC_CallBack_getSatRsp: sat=%x\n", sat);
1312         //Store the device that sent the request, for now send to all clients
1313         srpcSendAll(pSrpcMessage);
1315         //printf("SRPC_CallBack_getSatRsp--\n");
1317         return;
1320 void error(const char *msg)
1322         perror(msg);
1323         exit(1);
1326 /*********************************************************************
1327  * @fn          SRPC_getDevices
1328  *
1329  * @brief       This function exposes an interface to allow an upper layer to start device discovery.
1330  *
1331  * @param       pBuf - incomin messages
1332  *
1333  * @return      afStatus_t
1334  */
1335 static uint8_t SRPC_getDevices(uint8_t *pBuf, uint32_t clientFd)
1337         epInfoExtended_t epInfoEx;
1338         uint32_t context = 0;
1339         uint8_t *pSrpcMessage;
1341         //printf("SRPC_getDevices++ \n");
1343         while ((epInfoEx.epInfo = devListGetNextDev(&context)) != NULL)
1344         {
1345                 epInfoEx.type = EP_INFO_TYPE_EXISTING;
1346                 epInfoEx.prevNwkAddr = 0xFFFF;
1347                 epInfoEx.epInfo->flags = MT_NEW_DEVICE_FLAGS_NONE;
1349                 //Send epInfo
1350                 pSrpcMessage = srpcParseEpInfo(&epInfoEx);
1351                 printf("SRPC_getDevices: %x:%x:%x:%x:%x\n", epInfoEx.epInfo->nwkAddr, epInfoEx.epInfo->endpoint,
1352                                 epInfoEx.epInfo->profileID, epInfoEx.epInfo->deviceID, epInfoEx.type);
1353                 //Send SRPC
1354                 srpcSend(pSrpcMessage, clientFd);
1355                 free(pSrpcMessage);
1357                 usleep(1000);
1359                 //get next device (NULL if all done)
1360 //printf("--?--> 0x%04X, 0x%02X", (uint32_t)epInfoEx.epInfo->nwkAddr, (uint32_t)epInfoEx.epInfo->endpoint);
1361 //printf(" --!--> 0x%08X\n", (uint32_t)epInfoEx.epInfo);
1362         }
1364         return 0;
1367 /*********************************************************************
1368  * @fn          SRPC_notSupported
1369  *
1370  * @brief       This function is called for unsupported commands.
1371  *
1372  * @param       pBuf - incomin messages
1373  *
1374  * @return      afStatus_t
1375  */
1376 static uint8_t SRPC_notSupported(uint8_t *pBuf, uint32_t clientFd)
1378         return 0;
1381 /***************************************************************************************************
1382  * @fn      SRPC_Init
1383  *
1384  * @brief   initialises the RPC interface and waitsfor a client to connect.
1385  * @param   
1386  *
1387  * @return  Status
1388  ***************************************************************************************************/
1389 void SRPC_Init(void)
1391         if (socketSeverInit(SRPC_TCP_PORT) == -1)
1392         {
1393                 //exit if the server does not start
1394                 exit(-1);
1395         }
1397         serverSocketConfig(SRPC_RxCB, SRPC_ConnectCB);
1400 /*********************************************************************
1401  * @fn          SRPC_SendEpInfo
1402  *
1403  * @brief       This function exposes an interface to allow an upper layer to start send an ep indo to all devices.
1404  *
1405  * @param       epInfo - pointer to the epInfo to be sent
1406  *
1407  * @return      afStatus_t
1408  */
1409 uint8_t SRPC_SendEpInfo(epInfoExtended_t *epInfoEx)
1410
1411   uint8_t *pSrpcMessage = srpcParseEpInfo(epInfoEx);  
1412   
1413   printf("SRPC_SendEpInfo++ %x:%x:%x:%x\n", epInfoEx->epInfo->nwkAddr, epInfoEx->epInfo->endpoint, epInfoEx->epInfo->profileID, epInfoEx->epInfo->deviceID);
1414     
1415   //Send SRPC
1416   srpcSendAll(pSrpcMessage);  
1417   free(pSrpcMessage); 
1418   printf("RSPC_SendEpInfo--\n");
1419   
1420   return 0;  
1423 /***************************************************************************************************
1424  * @fn      SRPC_ConnectCB
1425  *
1426  * @brief   Callback for connecting SRPC clients.
1427  *
1428  * @return  Status
1429  ***************************************************************************************************/
1430 void SRPC_ConnectCB(int clientFd)
1432         //printf("SRPC_ConnectCB++ \n");
1433         /*
1434          epInfo = devListGetNextDev(0xFFFF, 0);
1436          while(epInfo)
1437          {
1438          printf("SRPC_ConnectCB: send epInfo\n");
1439          //Send device Annce
1440          uint8_t *pSrpcMessage = srpcParseEpInfo(epInfo);
1441          //Send SRPC
1442          srpcSend(pSrpcMessage, clientFd);
1443          free(pSrpcMessage);
1445          usleep(1000);
1447          //get next device (NULL if all done)
1448          epInfo = devListGetNextDev(epInfo->nwkAddr, epInfo->endpoint);
1449          }
1450          */
1451         //printf("SRPC_ConnectCB--\n");
1454 /***************************************************************************************************
1455  * @fn      SRPC_RxCB
1456  *
1457  * @brief   Callback for Rx'ing SRPC messages.
1458  *
1459  * @return  Status
1460  ***************************************************************************************************/
1461 void SRPC_RxCB(int clientFd)
1463         char buffer[256];
1464         int byteToRead;
1465         int byteRead;
1466         int rtn;
1468         //printf("SRPC_RxCB++[%x]\n", clientFd);
1470         rtn = ioctl(clientFd, FIONREAD, &byteToRead);
1472         if (rtn != 0)
1473         {
1474                 printf("SRPC_RxCB: Socket error\n");
1475         }
1477         while (byteToRead)
1478         {
1479                 bzero(buffer, 256);
1480                 byteRead = 0;
1481                 //Get the CMD-ID and Len
1482                 byteRead += read(clientFd, buffer, 2);
1483                 //Get the rest of the message
1484                 //printf("SRPC: reading %x byte message\n",buffer[SRPC_MSG_LEN]);
1485                 byteRead += read(clientFd, &buffer[2], buffer[SRPC_MSG_LEN]);
1486                 byteToRead -= byteRead;
1487                 if (byteRead < 0) error("SRPC ERROR: error reading from socket\n");
1488                 if (byteRead < buffer[SRPC_MSG_LEN])
1489                         error("SRPC ERROR: full message not read\n");
1490                 //printf("Read the message[%x]\n",byteRead);
1491                 SRPC_ProcessIncoming((uint8_t*) buffer, clientFd);
1492         }
1494         //printf("SRPC_RxCB--\n");
1496         return;
1499 /***************************************************************************************************
1500  * @fn      Closes the TCP port
1501  *
1502  * @brief   Send a message over SRPC.
1503  *
1504  * @return  Status
1505  ***************************************************************************************************/
1506 void SRPC_Close(void)
1508         socketSeverClose();