1 /**************************************************************************************************
2 Filename: flashled.c
3 Revised: $Date: 2012-03-21 17:37:33 -0700 (Wed, 21 Mar 2012) $
4 Revision: $Revision: 246 $
6 Description: This file contains an example client for the zbGateway sever
8 Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
15 Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
18 Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the
21 distribution.
23 Neither the name of Texas Instruments Incorporated nor the names of
24 its contributors may be used to endorse or promote products derived
25 from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 **************************************************************************************************/
40 #include <fcntl.h>
41 #include <termios.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <netdb.h>
48 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <sys/socket.h>
51 #include <poll.h>
53 #include "socket_client.h"
54 #include "interface_srpcserver.h"
55 #include "hal_defs.h"
57 int keyFd;
59 #define CONSOLEDEVICE "/dev/console"
61 void sendLightState(uint16_t addr, uint16_t addrMode, uint16_t ep, uint8_t state);
62 void socketClientCb( msgData_t *msg );
64 typedef uint8_t (*srpcProcessMsg_t)(msgData_t *msg);
66 srpcProcessMsg_t rpcsProcessSeIncoming[] =
67 {
68 };
70 void keyInit(void)
71 {
72 keyFd = open(CONSOLEDEVICE, O_RDONLY | O_NOCTTY | O_NONBLOCK );
73 tcflush(keyFd, TCIFLUSH);
74 }
76 int main(int argc, char *argv[])
77 {
78 uint16_t addr, period, loops;
79 uint8_t addrMode, ep, cnt=0;
80 uint32 start_with = 0;
82 if(argc < 6)
83 {
84 printf("Expected 4 and got %d params Usage: %s <device/group addr> <addr mode> <ep> <periodms> <loops>\n", argc, argv[0] );
85 printf("Example - Unicast command of nwk addr 0xb85a ep 0xb every 1s: %s 0xb85a 2 0xb 1000 0\n", argv[0] );
86 exit(0);
87 }
88 else
89 {
90 uint32_t tmpInt;
91 sscanf(argv[1], "0x%x", &tmpInt);
92 addr = (uint16_t) tmpInt;
94 sscanf(argv[2], "%d", &tmpInt);
95 addrMode = (uint8_t) tmpInt;
97 sscanf(argv[3], "0x%x", &tmpInt);
98 ep = (uint8_t) tmpInt;
100 sscanf(argv[4], "%d", &tmpInt);
101 period = (uint16_t) tmpInt;
103 sscanf(argv[5], "%d", &tmpInt);
104 loops = (uint16_t) tmpInt;
106 if (argc > 6)
107 {
108 start_with = atoi(argv[6]);
109 }
111 }
113 socketClientInit("127.0.0.1:11235", socketClientCb);
115 if(loops != 0)
116 loops+=2;
118 //loop for ever if loops = 0
119 while(loops != 2)
120 {
121 printf("Toggling Light %x:%x - %d\n", addr, ep, cnt++ );
122 if((loops % 2) == start_with)
123 {
124 sendLightState(addr, addrMode, ep, 1);
125 }
126 else
127 {
128 sendLightState(addr, addrMode, ep, 0);
129 }
131 usleep(period * 1000);
133 if(loops > 1)
134 loops--;
135 else
136 loops^=1;
137 }
139 socketClientClose();
141 return 0;
142 }
144 //Process the message from HA-Interface
145 void socketClientCb( msgData_t *msg )
146 {
147 //for now we are not interested in messages from HA server
148 }
150 void sendLightState(uint16_t addr, uint16_t addrMode, uint16_t ep, uint8_t state)
151 {
152 msgData_t msg;
153 uint8_t* pRpcCmd = msg.pData;
155 msg.cmdId = SRPC_SET_DEV_STATE;
156 msg.len = 15;
157 //Addr Mode
158 *pRpcCmd++ = (afAddrMode_t)addrMode;
159 //Addr
160 *pRpcCmd++ = addr & 0xFF;
161 *pRpcCmd++ = (addr & 0xFF00) >> 8;
162 //index past 8byte addr
163 pRpcCmd+=6;
164 //Ep
165 *pRpcCmd++ = ep;
166 //Pad out Pan ID
167 pRpcCmd+=2;
168 //State
169 *pRpcCmd++ = state;
171 socketClientSendData (&msg);
172 }