summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/common/network_send_commands.c
blob: a2be7a3caac69de24584257db2f857c689aecfb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* network_send_commands.c */
/* generic messages to send. {{{
 *
 * Copyright (C) 2013 Florent Duchon
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

#include "network.h"

AppMessageBuffer_t zigbit_tx_buffer;


extern APS_RegisterEndpointReq_t endpointParams;
extern APS_DataReq_t network_config;
extern SimpleDescriptor_t simpleDescriptor;


/* This function applies the generic configuration requested before sending any packet */
void network_TX_prepare_configuration(void)
{
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		/* Configure the message structure */
		network_config.dstAddrMode = APS_SHORT_ADDRESS;					// Short addressing mode
		network_config.profileId = simpleDescriptor.AppProfileId;						// Profile ID
		network_config.dstEndpoint = simpleDescriptor.endpoint;						// Desctination endpoint
		network_config.clusterId = APP_CLUSTER_ID;						// Desctination cluster ID
		network_config.srcEndpoint = simpleDescriptor.endpoint;						// Source endpoint
		network_config.txOptions.acknowledgedTransmission = 0;				// Acknowledged transmission enabled
		network_config.txOptions.fragmentationPermitted = 0;
		network_config.radius = 0;										// Use maximal possible radius
		network_config.APS_DataConf = APS_DataConf;	
	}
}


/* This function must be used to send angle through zigbee network */
void network_send_angle(uint16_t address,uint16_t angle)
{
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		/* Configure network for tx */
		network_TX_prepare_configuration();
		
		/* Destination */
		network_config.dstAddress.shortAddress = address;
		
		/* Message Type */
		zigbit_tx_buffer.message.type = NETWORK_ANGLE_RAW;
		
		/* LSB Data */
		zigbit_tx_buffer.message.data[NETWORK_MSG_DATA_LSB_FIELD] = angle;
		
		/* MSB Data */
		zigbit_tx_buffer.message.data[NETWORK_MSG_DATA_MSB_FIELD] = angle >> 8;
		
		
		/* Bitcloud sending request */
		network_config.asdu = (uint8_t*) &zigbit_tx_buffer.message;					// application message pointer
		network_config.asduLength = 2 + sizeof(zigbit_tx_buffer.message.type);		// actual application message length
		
		network_start_transmission();
	}
}


/* This function must be used to send uart frame over zigbee network */
void network_send_buffer_over_zb(uint16_t address,uint8_t * buffer,uint8_t len)
{
	static uint8_t current_ASDUsize = 0;
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		if(network_get_transmission_state() == APP_DATA_TRANSMISSION_READY_STATE)
		{
			current_ASDUsize = 0;
		}
		
		/* Configure network for tx */
		network_TX_prepare_configuration();
		
		/* Destination */
		network_config.dstAddress.shortAddress = address;
		
		/* Message Type */
		zigbit_tx_buffer.message.type = NETWORK_UART_OVER_ZB;
		
		memcpy(zigbit_tx_buffer.message.data+current_ASDUsize,buffer,len);
		current_ASDUsize += len;
		
		/* Bitcloud sending request */
		network_config.asdu = (uint8_t*) &zigbit_tx_buffer.message;					// application message pointer
		network_config.asduLength = current_ASDUsize + sizeof(zigbit_tx_buffer.message.type);		// actual application message length
		
		network_start_transmission();
	}
}


/* This function must be used to send reset command through zigbee network */
void network_send_reset(uint16_t address)
{
	
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		/* Configure network for tx */
		network_TX_prepare_configuration();
		network_config.dstAddress.shortAddress = address;						// Destination address	
		network_config.asdu = (uint8_t*) &zigbit_tx_buffer.message;					// application message pointer
		
		/* Message type*/
		zigbit_tx_buffer.message.type = NETWORK_RESET;
		
		/* Bitcloud sending request */
		network_config.asduLength = sizeof(zigbit_tx_buffer.message.type);		// actual application message length
		
		network_start_transmission();
	}
}


/* This function must be used to send jack state through zigbee network */
void network_send_jack_state(uint16_t address,uint8_t state)
{
	
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		/* Configure network for tx */
		network_TX_prepare_configuration();
		network_config.dstAddress.shortAddress = address;						// Destination address	
		network_config.asdu = (uint8_t*) &zigbit_tx_buffer.message;					// application message pointer
		
		/* Message type*/
		zigbit_tx_buffer.message.type = NETWORK_JACK_STATE;
		
		/* Source address */
		zigbit_tx_buffer.message.data[NETWORK_MSG_DATA_MSB_FIELD] = state;
		
		/* Bitcloud sending request */
		network_config.asduLength = 1 + sizeof(zigbit_tx_buffer.message.type);		// actual application message length
		
		network_start_transmission();
	}
}

/* This function must be used to send calibration command through zigbee network */
void network_send_start_calibration()
{
	
	if(network_get_state() == APP_NETWORK_JOINED_STATE)
	{
		/* Configure network for tx */
		network_TX_prepare_configuration();
		network_config.dstAddress.shortAddress = 0xFFFF;						// Destination address	
		network_config.asdu = (uint8_t*) &zigbit_tx_buffer.message;					// application message pointer
		
		/* Message type*/
		zigbit_tx_buffer.message.type = NETWORK_START_CALIBRATION;
		
		/* Bitcloud sending request */
		network_config.asduLength = sizeof(zigbit_tx_buffer.message.type);		// actual application message length
		
		network_start_transmission();
	}
}