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)
126 {
127 uint8_t i;
128 uint8_t *pSrpcMessage, *pTmp, devNameLen = 1, pSrpcMessageLen;
130 //printf("srpcParseEpInfo++\n");
132 //RpcMessage contains function ID param Data Len and param data
133 if( epInfo->deviceName )
134 {
135 devNameLen = epInfo->deviceName[0];
136 }
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);
142 pTmp = pSrpcMessage;
144 if( pSrpcMessage )
145 {
146 //Set func ID in RPCS buffer
147 *pTmp++ = RPCS_NEW_ZLL_DEVICE;
148 //param size
149 *pTmp++ = pSrpcMessageLen;
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;
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 }
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;
196 rtn = socketSeverSend(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2), fdClient);
197 if (rtn < 0)
198 {
199 printf("ERROR writing to socket\n");
200 }
202 return;
203 }
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;
217 rtn = socketSeverSendAllclients(srpcMsg, (srpcMsg[SRPC_MSG_LEN] + 2));
218 if (rtn < 0)
219 {
220 printf("ERROR writing to socket\n");
221 }
223 return;
224 }
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)
237 {
238 rpcsProcessMsg_t func;
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 }
252 //printf("RPSC_ProcessIncoming--\n");
253 }
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)
265 {
266 uint16_t dstAddr;
267 uint8_t addrMode;
268 uint8_t endpoint;
269 char *nameStr;
270 uint8_t nameLen;
271 uint16_t groupId;
273 //printf("RPCS_ZLL_addGroup++\n");
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;
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 }
294 //printf("RPCS_ZLL_addGroup++: %x:%x:%x name[%d] %s \n", dstAddr, addrMode, endpoint, nameLen, nameStr);
296 groupId = groupListAddGroup( nameStr );
297 zllSocAddGroup(groupId, dstAddr, endpoint, addrMode);
299 RPCS_ZLL_CallBack_addGroupRsp(groupId, nameStr, clientFd);
301 free(nameStr);
303 //printf("RPCS_ZLL_addGroup--\n");
305 return 0;
306 }
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)
318 {
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;
327 //printf("RPCS_ZLL_storeScene++\n");
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;
339 groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
340 pBuf += 2;
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 }
351 //printf("RPCS_ZLL_storeScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
353 sceneId = sceneListAddScene( nameStr, groupId );
354 zllSocStoreScene(groupId, sceneId, dstAddr, endpoint, addrMode);
355 RPCS_ZLL_CallBack_addSceneRsp(groupId, sceneId, nameStr, clientFd);
357 free(nameStr);
359 //printf("RPCS_ZLL_storeScene--\n");
361 return 0;
362 }
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)
374 {
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;
383 //printf("RPCS_ZLL_recallScene++\n");
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;
395 groupId = BUILD_UINT16(pBuf[0], pBuf[1]);
396 pBuf += 2;
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 }
407 //printf("RPCS_ZLL_recallScene++: name[%d] %s, group %d \n", nameLen, nameStr, groupId);
409 sceneId = sceneListGetSceneId( nameStr, groupId );
410 zllSocRecallScene(groupId, sceneId, dstAddr, endpoint, addrMode);
412 free(nameStr);
414 //printf("RPCS_ZLL_recallScene--\n");
416 return 0;
417 }
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)
429 {
430 afAddrType_t dstAddr;
431 uint16_t identifyTime;
433 //printf("RPCS_ZLL_identifyDevice++\n");
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;
453 identifyTime = BUILD_UINT16(pBuf[0], pBuf[1]);
455 //TODO: implement zllSocIdentify
458 return 0;
459 }
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;
479 //printf("RSPC_ZLL_bindDevices++\n");
481 //increment past SRPC header
482 pBuf+=2;
484 /* Src Address */
485 srcNwkAddr = BUILD_UINT16( pBuf[0], pBuf[1] );
486 pBuf += 2;
488 srcEndpoint = *pBuf++;
490 memcpy(srcIEEE, pBuf, Z_EXTADDR_LEN);
491 pBuf += Z_EXTADDR_LEN;
493 dstEndpoint = *pBuf++;
495 memcpy(dstIEEE, pBuf, Z_EXTADDR_LEN);
496 pBuf += Z_EXTADDR_LEN;
498 clusterId = BUILD_UINT16(pBuf[0], pBuf[1]);
499 pBuf += 2;
501 zllSocBind(srcNwkAddr, srcEndpoint, srcIEEE, dstEndpoint, dstIEEE, clusterId);
503 return 0;
504 }
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)
516 {
517 uint8_t endpoint, addrMode;
518 uint16_t dstAddr;
519 bool state;
521 //printf("RPCS_ZLL_setDeviceState++\n");
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;
533 state = (bool)*pBuf;
535 //printf("RPCS_ZLL_setDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x, state=%x\n", dstAddr, endpoint, addrMode, state);
537 // Set light state on/off
538 zllSocSetState(state, dstAddr, endpoint, addrMode);
540 //printf("RPCS_ZLL_setDeviceState--\n");
542 return 0;
543 }
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)
555 {
556 uint8_t endpoint, addrMode;
557 uint16_t dstAddr;
558 uint8_t level;
559 uint16_t transitionTime;
561 //printf("RPCS_ZLL_setDeviceLevel++\n");
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;
573 level = *pBuf++;
575 transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);
576 pBuf += 2;
578 //printf("RPCS_ZLL_setDeviceLevel: dstAddr.addr.shortAddr=%x ,level=%x, tr=%x \n", dstAddr, level, transitionTime);
580 zllSocSetLevel(level, transitionTime, dstAddr, endpoint, addrMode);
582 //printf("RPCS_ZLL_setDeviceLevel--\n");
584 return 0;
585 }
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)
597 {
598 uint8_t endpoint, addrMode;
599 uint16_t dstAddr;
600 uint8_t hue, saturation;
601 uint16_t transitionTime;
603 //printf("RPCS_ZLL_setDeviceColor++\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 hue = *pBuf++;
617 saturation = *pBuf++;
619 transitionTime = BUILD_UINT16(pBuf[0], pBuf[1]);
620 pBuf += 2;
622 //printf("RPCS_ZLL_setDeviceColor: dstAddr=%x ,hue=%x, saturation=%x, tr=%x \n", dstAddr, hue, saturation, transitionTime);
624 zllSocSetHueSat(hue, saturation, transitionTime, dstAddr, endpoint, addrMode);
626 //printf("RPCS_ZLL_setDeviceColor--\n");
628 return 0;
629 }
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)
641 {
642 uint8_t endpoint, addrMode;
643 uint16_t dstAddr;
645 //printf("RPCS_ZLL_getDeviceState++\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 //printf("RPCS_ZLL_getDeviceState: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x", dstAddr, endpoint, addrMode);
659 // Get light state on/off
660 zllSocGetState(dstAddr, endpoint, addrMode);
662 //printf("RPCS_ZLL_getDeviceState--\n");
664 return 0;
665 }
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)
677 {
678 uint8_t endpoint, addrMode;
679 uint16_t dstAddr;
681 //printf("RPCS_ZLL_getDeviceLevel++\n");
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;
693 //printf("RPCS_ZLL_getDeviceLevel: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
695 // Get light level
696 zllSocGetLevel(dstAddr, endpoint, addrMode);
698 //printf("RPCS_ZLL_getDeviceLevel--\n");
700 return 0;
701 }
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)
713 {
714 uint8_t endpoint, addrMode;
715 uint16_t dstAddr;
717 //printf("RPCS_ZLL_getDeviceHue++\n");
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;
729 //printf("RPCS_ZLL_getDeviceHue: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
731 // Get light hue
732 zllSocGetHue(dstAddr, endpoint, addrMode);
734 //printf("RPCS_ZLL_getDeviceHue--\n");
736 return 0;
737 }
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)
749 {
750 uint8_t endpoint, addrMode;
751 uint16_t dstAddr;
753 //printf("RPCS_ZLL_getDeviceSat++\n");
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;
765 //printf("RPCS_ZLL_getDeviceSat: dstAddr.addr.shortAddr=%x, endpoint=%x dstAddr.mode=%x\n", dstAddr, endpoint, addrMode);
767 // Get light sat
768 zllSocGetSat(dstAddr, endpoint, addrMode);
770 //printf("RPCS_ZLL_getDeviceSat--\n");
772 return 0;
773 }
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;
788 //printf("RSPC_ZLL_close++\n");
790 //increment past SRPC header
791 pBuf+=2;
793 magicNumber = BUILD_UINT16(pBuf[0], pBuf[1]);
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
801 exit(EXIT_SUCCESS );
802 }
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);
820 //printf("RPCS_ZLL_getGroups++\n");
822 while(group != NULL)
823 {
824 uint8_t *pSrpcMessage, *pBuf;
825 //printf("RPCS_ZLL_getGroups: group != null\n");
827 //printf("RPCS_ZLL_getGroups: malloc'ing %d bytes\n", (2 + (sizeof(uint16_t)) + ((uint8_t) (group->groupNameStr[0]))));
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])));
833 pBuf = pSrpcMessage;
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]);
841 *pBuf++ = group->groupId & 0xFF;
842 *pBuf++ = (group->groupId & 0xFF00) >> 8;
844 *pBuf++ = group->groupNameStr[0];
846 int i;
847 for(i = 0; i < group->groupNameStr[0]; i++)
848 {
849 *pBuf++ = group->groupNameStr[i+1];
850 }
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");
861 return 0;
862 }
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)
872 {
873 uint8_t *pSrpcMessage, *pBuf;
875 //printf("RPCS_ZLL_CallBack_addGroupRsp++\n");
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]);
881 pBuf = pSrpcMessage;
883 //Set func ID in RPCS buffer
884 *pBuf++ = RPCS_ADD_GROUP_RSP;
885 //param size
886 *pBuf++ = 3 + nameStr[0];
888 *pBuf++ = groupId & 0xFF;
889 *pBuf++ = (groupId & 0xFF00) >> 8;
891 *pBuf++ = nameStr[0];
892 int i;
893 for(i = 0; i < nameStr[0]; i++)
894 {
895 *pBuf++ = nameStr[i+1];
896 }
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");
904 return;
905 }
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);
920 //printf("RPCS_ZLL_getScenes++\n");
922 while(scene != NULL)
923 {
924 uint8_t *pSrpcMessage, *pBuf;
925 //printf("RPCS_ZLL_getScenes: scene != null\n");
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]))));
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])));
933 pBuf = pSrpcMessage;
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]);
941 *pBuf++ = scene->groupId & 0xFF;
942 *pBuf++ = (scene->groupId & 0xFF00) >> 8;
944 *pBuf++ = scene->sceneId;
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 }
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");
962 return 0;
963 }
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)
973 {
974 uint8_t *pSrpcMessage, *pBuf;
976 //printf("RPCS_ZLL_CallBack_addSceneRsp++\n");
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]);
982 pBuf = pSrpcMessage;
984 //Set func ID in RPCS buffer
985 *pBuf++ = RPCS_ADD_SCENE_RSP;
986 //param size
987 *pBuf++ = 4 + nameStr[0];
989 *pBuf++ = groupId & 0xFF;
990 *pBuf++ = (groupId & 0xFF00) >> 8;
992 *pBuf++ = sceneId & 0xFF;
994 *pBuf++ = nameStr[0];
995 int i;
996 for(i = 0; i < nameStr[0]; i++)
997 {
998 *pBuf++ = nameStr[i+1];
999 }
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");
1007 return;
1008 }
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)
1018 {
1019 uint8_t *pSrpcMessage, *pBuf;
1021 //printf("RPCS_ZLL_CallBack_getStateRsp++\n");
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);
1027 pBuf = pSrpcMessage;
1029 //Set func ID in RPCS buffer
1030 *pBuf++ = RPCS_GET_DEV_STATE_RSP;
1031 //param size
1032 *pBuf++ = 4;
1034 *pBuf++ = srcAddr & 0xFF;
1035 *pBuf++ = (srcAddr & 0xFF00) >> 8;
1036 *pBuf++ = endpoint;
1037 *pBuf++ = state & 0xFF;
1039 //printf("RPCS_ZLL_CallBack_getStateRsp: state=%x\n", state);
1041 //Store the device that sent the request, for now send to all clients
1042 srpcSendAll(pSrpcMessage);
1044 //printf("RPCS_ZLL_CallBack_addSceneRsp--\n");
1046 return;
1047 }
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)
1057 {
1058 uint8_t *pSrpcMessage, *pBuf;
1060 //printf("RPCS_ZLL_CallBack_getLevelRsp++\n");
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);
1066 pBuf = pSrpcMessage;
1068 //Set func ID in RPCS buffer
1069 *pBuf++ = RPCS_GET_DEV_LEVEL_RSP;
1070 //param size
1071 *pBuf++ = 4;
1073 *pBuf++ = srcAddr & 0xFF;
1074 *pBuf++ = (srcAddr & 0xFF00) >> 8;
1075 *pBuf++ = endpoint;
1076 *pBuf++ = level & 0xFF;
1078 //printf("RPCS_ZLL_CallBack_getLevelRsp: level=%x\n", level);
1080 //Store the device that sent the request, for now send to all clients
1081 srpcSendAll(pSrpcMessage);
1083 //printf("RPCS_ZLL_CallBack_getLevelRsp--\n");
1085 return;
1086 }
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)
1096 {
1097 uint8_t *pSrpcMessage, *pBuf;
1099 //printf("RPCS_ZLL_CallBack_getLevelRsp++\n");
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);
1105 pBuf = pSrpcMessage;
1107 //Set func ID in RPCS buffer
1108 *pBuf++ = RPCS_GET_DEV_HUE_RSP;
1109 //param size
1110 *pBuf++ = 4;
1112 *pBuf++ = srcAddr & 0xFF;
1113 *pBuf++ = (srcAddr & 0xFF00) >> 8;
1114 *pBuf++ = endpoint;
1115 *pBuf++ = hue & 0xFF;
1117 //printf("RPCS_ZLL_CallBack_getHueRsp: hue=%x\n", hue);
1119 //Store the device that sent the request, for now send to all clients
1120 srpcSendAll(pSrpcMessage);
1122 //printf("RPCS_ZLL_CallBack_getHueRsp--\n");
1124 return;
1125 }
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)
1135 {
1136 uint8_t *pSrpcMessage, *pBuf;
1138 //printf("RPCS_ZLL_CallBack_getSatRsp++\n");
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);
1144 pBuf = pSrpcMessage;
1146 //Set func ID in RPCS buffer
1147 *pBuf++ = RPCS_GET_DEV_SAT_RSP;
1148 //param size
1149 *pBuf++ = 4;
1151 *pBuf++ = srcAddr & 0xFF;
1152 *pBuf++ = (srcAddr & 0xFF00) >> 8;
1153 *pBuf++ = endpoint;
1154 *pBuf++ = sat & 0xFF;
1156 //printf("RPCS_ZLL_CallBack_getSatRsp: sat=%x\n", sat);
1158 //Store the device that sent the request, for now send to all clients
1159 srpcSendAll(pSrpcMessage);
1161 //printf("RPCS_ZLL_CallBack_getSatRsp--\n");
1163 return;
1164 }
1167 void error(const char *msg)
1168 {
1169 perror(msg);
1170 exit(1);
1171 }
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);
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);
1199 usleep(1000);
1201 //get next device (NULL if all done)
1202 epInfo = devListGetNextDev(epInfo->nwkAddr, epInfo->endpoint);
1203 }
1205 return 0;
1206 }
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;
1220 }
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 )
1231 {
1232 if(socketSeverInit(SRPC_TCP_PORT) == -1)
1233 {
1234 //exit if the server does not start
1235 exit(-1);
1236 }
1238 serverSocketConfig(SRPC_RxCB, SRPC_ConnectCB);
1239 }
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);
1254 printf("RSPC_SendEpInfo++ %x:%x:%x:%x\n", epInfo->nwkAddr, epInfo->endpoint, epInfo->profileID, epInfo->deviceID);
1256 //Send SRPC
1257 srpcSendAll(pSrpcMessage);
1258 free(pSrpcMessage);
1260 printf("RSPC_SendEpInfo--\n");
1262 return 0;
1263 }
1265 /***************************************************************************************************
1266 * @fn SRPC_ConnectCB
1267 *
1268 * @brief Callback for connecting SRPC clients.
1269 *
1270 * @return Status
1271 ***************************************************************************************************/
1272 void SRPC_ConnectCB( int clientFd )
1273 {
1274 //printf("SRPC_ConnectCB++ \n");
1275 /*
1276 epInfo = devListGetNextDev(0xFFFF, 0);
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);
1287 usleep(1000);
1289 //get next device (NULL if all done)
1290 epInfo = devListGetNextDev(epInfo->nwkAddr, epInfo->endpoint);
1291 }
1292 */
1293 //printf("SRPC_ConnectCB--\n");
1294 }
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 )
1304 {
1305 char buffer[256];
1306 int byteToRead;
1307 int byteRead;
1308 int rtn;
1310 //printf("SRPC_RxCB++[%x]\n", clientFd);
1312 rtn = ioctl(clientFd, FIONREAD, &byteToRead);
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 }
1335 //printf("SRPC_RxCB--\n");
1337 return;
1338 }
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)
1350 {
1351 socketSeverClose();
1352 }