forked from pfalcon/blutunode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btnode.c
174 lines (160 loc) · 5.06 KB
/
btnode.c
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
/*
* BluTuNode - Bluetooth sensor/actuator node software
* Copyright (c) 2011-2012 Paul Sokolovsky
*
* BtNode 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 3 of the License, or
* (at your option) any later version.
*
* This software 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 software. If not, see <http://www.gnu.org/licenses/>.
*/
#include "btnode.h"
void command_software_version(Task task);
static int input_echo = 1;
void sink_write(Sink sink, const char *buf, int size)
{
int offset = SinkClaim(sink, size);
uint8 *dest = SinkMap(sink);
memcpy(dest + offset, buf, size);
SinkFlush(sink, size);
}
void sink_write_str(Sink sink, const char *str)
{
sink_write(sink, str, strlen(str));
}
static void handle_input_data(BtNodeCommandTask *self, Source src)
{
int size;
while ((size = SourceSize(src)) > 0) {
const uint8 *p = SourceMap(src);
int i, processed_size = 0;
char c = 0;
for (i = size; i; i--) {
c = *p++;
processed_size++;
if (c == 0x08 || c == 0x7f) {
if (self->buf_ptr > self->input_buf) {
self->buf_ptr--;
/* Need to overwrite with a space for Linux terminal */
sink_write(StreamSinkFromSource(src), "\x08 \x08", 3);
}
continue;
}
if (input_echo) {
if (c == '\r') {
sink_write(StreamSinkFromSource(src), "\r\n", 2);
} else {
sink_write(StreamSinkFromSource(src), (char*)p - 1, 1);
}
}
*self->buf_ptr++ = c;
if (c == '\r') {
break;
}
}
SourceDrop(src, processed_size);
if (c == '\r') {
*self->buf_ptr = 0;
PRINT(("Received: %s\n", self->input_buf));
process_line(self, StreamSinkFromSource(src), self->input_buf);
self->buf_ptr = self->input_buf;
}
}
}
static void task_handler(Task task, MessageId msg_id, Message msg)
{
BtNodeCommandTask *self = (BtNodeCommandTask*)task;
#ifdef DEBUG
dump_message(msg_id, msg);
#endif
switch (msg_id) {
case CL_INIT_CFM:
ConnectionRfcommAllocateChannel(task);
break;
case CL_RFCOMM_REGISTER_CFM:
ConnectionWriteScanEnable(hci_scan_enable_inq_and_page);
break;
case CL_SM_PIN_CODE_IND:
{
CAST_TYPED_MSG(CL_SM_PIN_CODE_IND, tmsg);
ConnectionSmPinCodeResponse(&tmsg->bd_addr, 4, (uint8*)"1234");
}
break;
case CL_SM_AUTHORISE_IND:
{
CAST_TYPED_MSG(CL_SM_AUTHORISE_IND, tmsg);
ConnectionSmAuthoriseResponse(&tmsg->bd_addr, tmsg->protocol_id, tmsg->channel, tmsg->incoming, TRUE);
}
break;
case CL_RFCOMM_CONNECT_IND:
{
CAST_TYPED_MSG(CL_RFCOMM_CONNECT_IND, tmsg);
ConnectionRfcommConnectResponse(task, TRUE, &tmsg->bd_addr, tmsg->server_channel, NULL);
}
break;
case CL_RFCOMM_CONNECT_CFM:
{
CAST_TYPED_MSG(CL_RFCOMM_CONNECT_CFM, tmsg);
self->sink = tmsg->sink;
command_software_version(task);
}
break;
case MESSAGE_MORE_DATA:
{
MessageMoreData *tmsg = (MessageMoreData*)msg;
handle_input_data(self, tmsg->source);
/*parseSource(tmsg->source, task);*/
}
break;
case MESSAGE_ADC_RESULT:
{
MessageAdcResult *tmsg = (MessageAdcResult*)msg;
char buf[20];
sprintf(buf, "ADC%d=%d (%d)\r\n", tmsg->adc_source, tmsg->reading, tmsg->scaled_reading);
sink_write_str(self->sink, buf);
}
break;
case MESSAGE_PIO_CHANGED:
{
MessagePioChanged *tmsg = (MessagePioChanged*)msg;
char buf[20];
sprintf(buf, "GPIO=%u\r\n", tmsg->state);
sink_write_str(self->sink, buf);
}
break;
case APP_MESSAGE_POLL:
{
command_poll_handle(self);
}
break;
case CL_DM_READ_BT_VERSION_CFM:
{
CAST_TYPED_MSG(CL_DM_READ_BT_VERSION_CFM, tmsg);
command_bt_version_handle(self, tmsg);
}
break;
case CL_DM_LOCAL_VERSION_CFM:
{
CAST_TYPED_MSG(CL_DM_LOCAL_VERSION_CFM, tmsg);
command_local_version_handle(self, tmsg);
}
break;
}
}
int main(void)
{
static BtNodeCommandTask app;
app.task.handler = task_handler;
app.buf_ptr = app.input_buf;
MessagePioTask(&app.task);
ConnectionInit(&app.task);
MessageLoop();
return 0;
}