-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone.cpp
More file actions
415 lines (334 loc) · 11.7 KB
/
phone.cpp
File metadata and controls
415 lines (334 loc) · 11.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "phone.h"
/*
void CPhone::newCMD(CMsg &msg){
writeconsoleln("..................................... Phone Adding Command Queue .....................................");
commandQueue.push(msg);
}
*/
void CPhone::callCustomFunctions(CMsg &msg){
std::string act=msg.getACT();
writeconsoleln(act);
if(act.size()>1) commandQueue.push(msg);
}
CPhone::CPhone(CMsg &msg){_cmsg=msg;init();}
CPhone::CPhone(){init();}
void CPhone::init(){
Name("PHONE");
_INTERVAL=20;
}
void CPhone::on(){
writeconsoleln("..................................... SwitchtoOn - Add Power Logic Here .....................................");
setup();
}
void CPhone::off(){
//writeconsoleln("..................................... SwitchtoOff - Add Power Logic Here .....................................");
}
void CPhone::setup() {
// Serial1.begin(115200);
// Serial1.setTimeout(TIMEOUT);
writeconsoleln("..................................... Setup .....................................");
#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
Serial1.begin(PHONE_BAUD_RATE);
#else
//Serial1.begin(PHONE_BAUD_RATE, SERIAL_8N1, PHONE_TX, PHONE_RX);
// Serial1.begin(115200,SERIAL_8N1,13,14);
//Serial1.begin(115200,SERIAL_8N1,35,15);
Serial1.begin(PHONE_BAUD_RATE,SERIAL_8N1, PHONE_TX, PHONE_RX);
// Serial1.begin(115200, SERIAL_8N1, 23,4);
#endif
sendSerial("INIT"); //to synchronise Arduino and app
//setState("READY");
setState("PLAY");
}
void CPhone::ready(){
writeconsoleln("..................................... StartPlay .....................................");
ReceivedPacket(); //Should hit onInit whch should set state to PLAY
}
bool CPhone::waitForBytes(int count) { //Waits for data from serial port up to TIMEOUT secs If it got the number of bytes, return true.
long start = getTime();
while (Serial1.available() < count) { //Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer
if (getTime() - start > TIMEOUT) {
return false;
}
}
return true;
}
bool CPhone::readUntil(char terminator, unsigned char* buffer) { //Read from Phone
for (int i = 0; i < BUFFER_LENGTH; i++) {
bool ok = waitForBytes(1);
if (!ok) return false;
unsigned char b = Serial1.read();
if (b == terminator) {
buffer[i] = 0;
return true;
}
else {
if (i == BUFFER_LENGTH - 1) {
return false;
}
else {
buffer[i] = b;
}
}
}
return false;
}
void CPhone::sendSerial(const char* cmd) { //Send to Phone
Serial1.print(id);
Serial1.flush();
Serial1.print(",");
Serial1.flush();
Serial1.println(cmd);
Serial1.flush();
delayMicroseconds(500);
id++;
writeconsoleln();
writeconsole("Sending to phone => ");
writeconsoleln(cmd);
}
/*
void push_stream(long id, byte* fileName, int block, int len, bool hasMore, byte* data) {
message m;
m.data[0] = id >> 8; //writing the parameters passed to the message structure
m.data[1] = id;
m.data[2] = 'S';
m.data[3] = block >> 8;
m.data[4] = block;
m.data[5] = len;
m.data[6] = hasMore;
strcpy(m.data+7, (char*) fileName);
int fnLen = strlen((char*) fileName);
m.data[7 + fnLen] = 0;
memcpy(m.data + fnLen + 8, data, len);
m.length = len + fnLen + 8;
if (queue.size() < 400) {
// queue.push(m);
}
else {
writeconsoleln("Queue full!!!");
}
}
*/
void CPhone::push_stream(long id, unsigned char* fileName, int block, int len, bool hasMore, unsigned char* data) { //Gets Filename and offset sent to comms, id is id of the phone
char type = 'S';
std::string str;
//MAYBE USE CID AS ID
//X as first character denotes stream
//Capital B means no more
//std::string str = std::string("fileid:")+std::to_string(id)+std::string("~filename:")+std::string((char *)fileName)+std::string("~len:")+ std::to_string(int(len))+std::string("~more:")+ std::to_string(int(hasMore));
//if(hasMore) str= std::string("cid:")+std::to_string(id)+std::string("~b:")+std::to_string(block);
//else str = std::string("X")+CID()+std::string("B")+std::to_string(block);
if(hasMore) str= std::string("X")+CID()+std::string("b")+tostring(block);
else str = std::string("X")+CID()+std::string("B")+tostring(block);
while (str.length()<STREAMHEADERLEN) str=str+" ";
writeconsoleln();
writeconsole("---------------------------Stream Length:");
writeconsole(len+STREAMHEADERLEN);
writeconsoleln(str.c_str());
unsigned char buffer[256];
int bufcount=0,count=0;
for (count=0;count<str.length();count++){
buffer[bufcount]=str[count];
bufcount++;
}
if(bufcount>STREAMHEADERLEN){
writeconsole("------------------------- ERROR STREAM HEADER OVERFLOW ----------");
writeconsole(bufcount);
}
while(bufcount<STREAMHEADERLEN){
buffer[bufcount]=' ';
bufcount++;
}
for (count=0;count<len;count++){
buffer[bufcount]=data[count];
bufcount++;
}
//memcpy(buffer, str.c_str(), 10);
//memcpy(buffer+10, data, len);
CMsg m(str);
std::string strfn((const char *)fileName);
m.Parameters["FILE"]=strfn;
m.Parameters["BLK"]=tostring(block);
m.initArray(buffer,bufcount); ///////////////////////////////////////////////////////////////////////////////////////////////////////////// FIX THIS
addDataList(m);
writeconsoleln("..................................... Adding Picture Data to DataList .....................................");
writeconsoleln(m.serialize());
}
void CPhone::onInitAvailable(int id) { //Read from Phone add to queue
writeconsoleln("..................................... On Init Available .....................................");
unsigned char buff[BUFFER_LENGTH];
bool ok = readUntil('\n', buff);
if (!ok) return;
CMsg m;
m.Parameters["table"]="timephone";
m.Parameters["init"]="OK";
addTransmitList(m);
setState("PLAY");
}
void CPhone::onTimeAvailable(int id) { //Read from Phone add to queue
unsigned char timeStr[BUFFER_LENGTH];
bool ok = readUntil('\n', timeStr);
if (!ok) return;
std::string str1=(char *)timeStr;
CMsg m;
m.Parameters["table"]="timephone";
m.Parameters["time"]=str1;
addTransmitList(m);
}
void CPhone::onGpsAvailable(int id) { //Read from Phone add to queue
unsigned char gpsStr[BUFFER_LENGTH];
bool ok = readUntil('\n', gpsStr);
if (!ok) return;
std::string str;
str=String((char *)gpsStr).c_str();
int x=str.find(',');
if (x+4<=str.size()){
CMsg m;
m.Parameters["table"]="gps";
m.Parameters["lat"]=str.substr(0,x);
m.Parameters["lon"]=str.substr(x+1,20);
addTransmitList(m);
}
}
void CPhone::onPhotoAvailable(int id) { //Read from Phone add to filename queue to go to comms
unsigned char fileName[BUFFER_LENGTH];
unsigned char fileSizeStr[BUFFER_LENGTH];
long fileSize;
bool ok = readUntil(',', fileName);
if (!ok) return;
ok = readUntil('\n', fileSizeStr);
if (!ok) return;
sscanf((char*)fileSizeStr, "%ld", &fileSize);
//char msg[BUFFER_LENGTH];
//sprintf(msg, "%s,%d", fileName, fileSize);
std::string strfn((const char *)fileName);
std::string strfs((const char *)fileSizeStr);
CMsg m;
m.Parameters["table"]="photos";
m.Parameters["filename"]=strfn;
m.Parameters["filesize"]=strfs;
addTransmitList(m);
/*char cmd[BUFFER_LENGTH];
sprintf(cmd, "STREAM(%s,%d)", fileName, 0);
sendSerial(cmd);*/
//Done();
// setState("COMPLETE");
}
void CPhone::onStreamAvailable(int id) { //Read from Phone add to streamqueue
unsigned char fileName[BUFFER_LENGTH];
bool ok = readUntil(',', fileName);
if (!ok) return;
ok = waitForBytes(4);
if (!ok) return;
long block = (Serial1.read() << 8) + Serial1.read();
char len = Serial1.read();
unsigned char hasMore = Serial1.read();
unsigned char data[len];
for (int i = 0; i < len; i++) {
ok = waitForBytes(1);
if (!ok) {
return;
}
data[i] = Serial1.read();
}
push_stream(id, fileName, block, len, hasMore, data); //This is the output
/*
if (hasMore == 3) { //This is the queue to get more data from the phone
char cmd[50];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "STREAM(%s,%ld+)", fileName, block+1); //Seems to be stream command
CMsg m(cmd);
streamQueue.push(m);
}
if (streamQueue.size() > 0) { //Sends next request to phone
sendSerial(streamQueue.front().Data().c_str());
streamQueue.pop();
}
*/
}
void CPhone::ProcessCommandQueue(){ //This is where the New Command is procressed and sent to the phone
if (commandQueue.size() > 0) {
writeconsoleln("........................................ Sending Command to Phone .........................................");
CMsg cmsg =commandQueue.front();
commandQueue.pop();
// std::string bytes=cmsg.getMODE(); //Temporaty not using any params just seeing if CTIME works
std::string bytes=cmsg.getACT(); //Temporaty not using any params just seeing if CTIME works
writeconsoleln(cmsg.Data().c_str());
writeconsoleln(bytes.c_str());
received = false;
if (bytes[0] == 'C') { //command
std::string tmpstr=bytes.substr(1,200); //Sends it to the Phone Needs to start with 'C'
sendSerial((char*)tmpstr.c_str()); //Sends it to the Phone
}
else if (bytes[0] == 'A') { //acknowledgment from receiver
waitingForAck = 0;
/*
char filename[16];
for (int i = 0; i < 16; i++) {
filename[i] = bytes[i+1];
}
bool sent = false;
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 8; j++) {
if ((bytes[i+1+16] & (1 << (7-j))) == 0) {
char cmd[BUFFER_LENGTH];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "STREAM(%s,%ld)", filename, i*8 + j);
if (!sent) {
sendSerial(cmd); //Sends it to the Phone
sent = true;
}
else {
std::string s=cmd;
CMsg m(s);
streamQueue.push(m);
}
}
}
}
*/
}
}
//Play();
setState("PLAY");
}
void CPhone::ReceivedPacket() { //Any Comments to Serial Port go right back to the PHONE!!!!! Screws it up
//writeconsoleln(F("o"));
if (Serial1.available() > 0) { //Reads data from phone. Takes ID and MessageType and uses that to call the appropriate function to read from phone and if result is good adds it to the "queue"
writeconsoleln(" ------------------------------- Phone loop Reading Data ----------------------------------------------------------------------------------------------------------------");
unsigned char msgIdStr[BUFFER_LENGTH];
int msgId;
char msgType;
bool ok = readUntil(',', msgIdStr);
if (!ok) return;
sscanf((char*)msgIdStr, "%d", &msgId);
ok = waitForBytes(1);
if (!ok) return;
msgType = Serial1.read();
switch (msgType) {
case 'I':
onInitAvailable(msgId);
break;
case 'T':
onTimeAvailable(msgId);
break;
case 'G':
onGpsAvailable(msgId);
break;
case 'P':
onPhotoAvailable(msgId);
break;
case 'S':
onStreamAvailable(msgId);
break;
}
}
}
void CPhone::SetPhoneReceive(){}
void CPhone::TransmitPacket(CMsg &m){
commandQueue.push(m);
}
void CPhone::loop() {
ReceivedPacket();
ProcessCommandQueue();
}