-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.c
1261 lines (1070 loc) · 41.4 KB
/
commands.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "commands.h"
#include <string.h>
#include "mobile_data.h"
#include "mobile_inet.h"
#include "util.h"
#include "compat.h"
#ifdef MOBILE_LIBCONF_USE
#include <mobile_config.h>
#endif
// Accessible area of the mobile config by the game boy
#define MOBILE_CONFIG_SIZE_REAL 0x100
static_assert(MOBILE_CONFIG_SIZE >= MOBILE_CONFIG_SIZE_REAL,
"MOBILE_CONFIG_SIZE isn't big enough!");
// UNKERR is used for errors of which we don't really know if they exist, and
// if so what error code they return, but have been implemented just in case.
// NEWERR is used to indicate an error code that we made up ourselves to
// indicate something that couldn't happen with the real adapter.
// Connection number to use for p2p comms
static const int p2p_conn = 0;
// Static keys
static const char nintendo[] PROGMEM = {
'N', 'I', 'N', 'T', 'E', 'N', 'D', 'O'
};
static const char happy[] PROGMEM = {
0x45, 0x56, 0x45, 0x52, 0x59, 0x4f, 0x4e, 0x45,
0x20, 0x48, 0x41, 0x50, 0x50, 0x59, 0x20, 0x4d,
0x4f, 0x42, 0x49, 0x4c, 0x45, 0x20, 0x43, 0x4f,
0x4e, 0x4e, 0x45, 0x43, 0x54, 0x49, 0x4f, 0x4e
};
static const char isp_number_pdc_isp[] PROGMEM = "#9677";
static const char isp_number_pdc_serv[] PROGMEM = "#9477";
static const char isp_number_ddi_isp[] PROGMEM = "0077487751";
static const char isp_number_ddi_serv[] PROGMEM = "0077487752";
static const char isp_number_test[] PROGMEM = "0755311973";
static const char *const isp_numbers[] PROGMEM = {
isp_number_pdc_isp, // DION PDC/CDMAONE - ISP login
isp_number_pdc_serv, // DION PDC/CDMAONE - Service/configuration number
isp_number_ddi_isp, // DION DDI-POCKET - ISP login
isp_number_ddi_serv, // DION DDI-POCKET - Service/configuration number
isp_number_test, // NINTENDO TEST
NULL
};
void mobile_commands_init(struct mobile_adapter *adapter)
{
adapter->commands.session_started = false;
adapter->commands.mode_32bit = false;
}
static struct mobile_packet *error_packet(struct mobile_packet *packet, unsigned char error)
{
enum mobile_command command = packet->command;
packet->command = MOBILE_COMMAND_ERROR;
packet->data[0] = command;
packet->data[1] = error;
packet->length = 2;
return packet;
}
static int connection_new(struct mobile_adapter *adapter)
{
struct mobile_adapter_commands *s = &adapter->commands;
// Find a free connection slot
unsigned char conn;
for (conn = 0; conn < MOBILE_MAX_CONNECTIONS; conn++) {
if (!s->connections[conn]) break;
}
if (conn >= MOBILE_MAX_CONNECTIONS) return -1;
return conn;
}
static bool do_ppp_disconnect(struct mobile_adapter *adapter)
{
struct mobile_adapter_commands *s = &adapter->commands;
// Clean up internet connections if connected to the internet
if (s->state != MOBILE_CONNECTION_INTERNET) return false;
for (unsigned char conn = 0; conn < MOBILE_MAX_CONNECTIONS; conn++) {
if (s->connections[conn]) {
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
}
}
s->state = MOBILE_CONNECTION_CALL_ISP;
return true;
}
static bool do_offline(struct mobile_adapter *adapter)
{
do_ppp_disconnect(adapter);
struct mobile_adapter_commands *s = &adapter->commands;
// Can't hang up if not in a call
if (s->state != MOBILE_CONNECTION_CALL &&
s->state != MOBILE_CONNECTION_CALL_RECV &&
s->state != MOBILE_CONNECTION_CALL_ISP) {
return false;
}
mobile_cb_update_number(adapter, MOBILE_NUMBER_PEER, NULL);
// Clean up p2p connections if in a call
if (s->connections[p2p_conn]) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
}
s->state = MOBILE_CONNECTION_DISCONNECTED;
return true;
}
static void do_end_session(struct mobile_adapter *adapter)
{
do_offline(adapter);
struct mobile_adapter_commands *s = &adapter->commands;
// Clean up a possibly residual connection that wasn't established by
// the command_wait_call function
if (s->connections[p2p_conn]) mobile_cb_sock_close(adapter, p2p_conn);
s->session_started = false;
s->mode_32bit = false;
}
static void do_start_session(struct mobile_adapter *adapter)
{
struct mobile_adapter_commands *s = &adapter->commands;
s->session_started = true;
s->state = MOBILE_CONNECTION_DISCONNECTED;
memset(s->connections, false, sizeof(s->connections));
mobile_number_fetch_cancel(adapter);
}
void mobile_commands_reset(struct mobile_adapter *adapter)
{
struct mobile_adapter_commands *s = &adapter->commands;
if (s->session_started) do_end_session(adapter);
}
// Errors:
// 1 - Invalid use (Already begun a session)
// 2 - Invalid contents
static struct mobile_packet *command_start(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
if (s->session_started) return error_packet(packet, 1);
if (packet->length == sizeof(happy) &&
memcmp_P(packet->data, happy, sizeof(happy)) == 0) {
do_start_session(adapter);
return packet;
}
if (adapter->serial.device != MOBILE_ADAPTER_RED) {
if (packet->length != sizeof(nintendo)) return error_packet(packet, 2);
} else {
if (packet->length < sizeof(nintendo)) return error_packet(packet, 2);
packet->length = sizeof(nintendo);
}
if (memcmp_P(packet->data, nintendo, sizeof(nintendo)) != 0) {
return error_packet(packet, 2);
}
do_start_session(adapter);
return packet;
}
// Errors:
// 2 - Still connected/failed to disconnect(?)
static struct mobile_packet *command_end(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
// TODO: Reset mode_32bit here? Verify on hardware.
// Currently reset in MOBILE_ACTION_RESET_SERIAL
do_end_session(adapter);
packet->length = 0;
return packet;
}
enum process_tel {
PROCESS_TEL_BEGIN,
PROCESS_TEL_IP,
PROCESS_TEL_RELAY
};
static struct mobile_packet *command_tel_begin(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
if (s->state != MOBILE_CONNECTION_DISCONNECTED &&
s->state != MOBILE_CONNECTION_WAIT &&
s->state != MOBILE_CONNECTION_WAIT_RELAY &&
s->state != MOBILE_CONNECTION_WAIT_TIMEOUT) {
return error_packet(packet, 1);
}
if (packet->length < 1) return error_packet(packet, 2);
// Close any connection created by command_wait_call
if (s->connections[p2p_conn]) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
}
s->state = MOBILE_CONNECTION_DISCONNECTED;
// Validate the first byte (unknown purpose...)
switch (adapter->serial.device) {
case MOBILE_ADAPTER_BLUE:
if (packet->data[0] != 0) return error_packet(packet, 2);
break;
case MOBILE_ADAPTER_GREEN:
// Never released (games send value 1)
case MOBILE_ADAPTER_RED:
if (packet->data[0] != 1 && packet->data[0] != 9) {
return error_packet(packet, 2);
}
break;
default:
case MOBILE_ADAPTER_YELLOW:
// Not parsed (games send value 2)
break;
}
// Filter acceptable characters out of the string
unsigned char *w = packet->data + 1;
for (int i = 0; i < packet->length - 1 && i < 0x20; i++) {
unsigned char c = packet->data[i + 1];
if (('0' <= c && c <= '9') || c == '#' || c == '*') *w++ = c;
}
*w = '\0';
packet->length = (unsigned)(w - packet->data);
// If we're calling an ISP number, simulate being connected
for (const char *const *ptr = isp_numbers; pgm_read_ptr(ptr); ptr++) {
const char *number = pgm_read_ptr(ptr);
if ((unsigned)packet->length - 1 != strlen_P(number)) continue;
if (memcmp_P(packet->data + 1, number, packet->length - 1) == 0) {
// Report this number to the implementation
if (packet->length - 1 <= MOBILE_MAX_NUMBER_SIZE) {
packet->data[packet->length] = '\0';
mobile_cb_update_number(adapter, MOBILE_NUMBER_PEER,
(char *)packet->data + 1);
}
s->state = MOBILE_CONNECTION_CALL_ISP;
packet->length = 0;
return packet;
}
}
// If the relay is enabled, start the connection
if (adapter->config.relay.type != MOBILE_ADDRTYPE_NONE) {
mobile_addr_copy(&b->processing_addr, &adapter->config.relay);
mobile_relay_init(adapter);
if (!mobile_cb_sock_open(adapter, p2p_conn, MOBILE_SOCKTYPE_TCP,
b->processing_addr.type, 0)) {
return error_packet(packet, 3);
}
s->connections[p2p_conn] = true;
b->processing = PROCESS_TEL_RELAY;
return NULL;
}
// Interpret the number as an IP and connect to someone
if (packet->length == 1 + 3 * 4) {
// Convert the numerical phone "ip address" into a real ipv4 address
struct mobile_addr4 *addr = (struct mobile_addr4 *)&b->processing_addr;
if (!mobile_parse_phoneaddr(addr->host, (char *)packet->data + 1)) {
return error_packet(packet, 3);
}
addr->type = MOBILE_ADDRTYPE_IPV4;
addr->port = adapter->config.p2p_port;
if (!mobile_cb_sock_open(adapter, p2p_conn, MOBILE_SOCKTYPE_TCP,
b->processing_addr.type, 0)) {
return error_packet(packet, 3);
}
s->connections[p2p_conn] = true;
b->processing = PROCESS_TEL_IP;
return NULL;
}
return error_packet(packet, 3);
}
static struct mobile_packet *command_tel_ip(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
// Check if we're connected until it either errors or succeeds
int rc = mobile_cb_sock_connect(adapter, p2p_conn, &b->processing_addr);
if (rc == 0) return NULL;
if (rc < 0) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
return error_packet(packet, 3);
}
// Report the called number to the implementation
if (packet->length - 1 <= MOBILE_MAX_NUMBER_SIZE) {
packet->data[packet->length] = '\0';
mobile_cb_update_number(adapter, MOBILE_NUMBER_PEER,
(char *)packet->data + 1);
}
s->state = MOBILE_CONNECTION_CALL;
s->call_packets_sent = 0;
packet->length = 0;
return packet;
}
static struct mobile_packet *command_tel_relay(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
int rc = mobile_relay_proc_call(adapter, p2p_conn, &b->processing_addr,
(char *)packet->data + 1, packet->length - 1);
if (rc == 0) return NULL;
if (rc < 0) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
return error_packet(packet, 3);
}
// Interpret the result
int errcode;
switch (rc) {
case MOBILE_RELAY_CALL_RESULT_ACCEPTED: errcode = -1; break;
case MOBILE_RELAY_CALL_RESULT_INTERNAL: errcode = 3; break;
case MOBILE_RELAY_CALL_RESULT_BUSY: errcode = 0; break;
case MOBILE_RELAY_CALL_RESULT_UNAVAILABLE: errcode = 0; break;
default: errcode = 3; break;
}
if (errcode != -1) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
return error_packet(packet, errcode);
}
s->state = MOBILE_CONNECTION_CALL;
s->call_packets_sent = 0;
packet->length = 0;
return packet;
}
// Errors:
// 0 - "BUSY" (the called number is busy)
// 1 - Invalid use (already connected)
// 2 - Invalid contents (first byte isn't correct)
// 3 - "NO CARRIER"/"ERROR"/timeout (internal phone/communication error)
// 4 - "REDIAL ERROR"
static struct mobile_packet *command_tel(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
switch (b->processing) {
case PROCESS_TEL_BEGIN:
mobile_cb_time_latch(adapter, MOBILE_TIMER_COMMAND);
return command_tel_begin(adapter, packet);
case PROCESS_TEL_IP:
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 60000)) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
return error_packet(packet, 3);
}
return command_tel_ip(adapter, packet);
case PROCESS_TEL_RELAY:
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 60000)) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
return error_packet(packet, 3);
}
return command_tel_relay(adapter, packet);
default:
return error_packet(packet, 3);
}
}
// Errors:
// 1 - Invalid use (already hung up/phone not connected)
static struct mobile_packet *command_offline(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
if (!do_offline(adapter)) return error_packet(packet, 1);
packet->length = 0;
return packet;
}
enum process_wait_call {
PROCESS_WAIT_CALL_INIT,
PROCESS_WAIT_CALL_INIT_DONE
};
static struct mobile_packet *command_wait_call_begin(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
// Time out if anything fails
s->state = MOBILE_CONNECTION_WAIT_TIMEOUT;
if (adapter->config.relay.type != MOBILE_ADDRTYPE_NONE) {
mobile_addr_copy(&b->processing_addr, &adapter->config.relay);
mobile_relay_init(adapter);
// Open the relay connection
if (!mobile_cb_sock_open(adapter, p2p_conn, MOBILE_SOCKTYPE_TCP,
b->processing_addr.type, 0)) {
return error_packet(packet, 0);
}
s->connections[p2p_conn] = true;
s->state = MOBILE_CONNECTION_WAIT_RELAY;
return NULL;
}
// Open the connection and start listening
if (!mobile_cb_sock_open(adapter, p2p_conn, MOBILE_SOCKTYPE_TCP,
MOBILE_ADDRTYPE_IPV4, adapter->config.p2p_port)) {
return error_packet(packet, 0);
}
if (!mobile_cb_sock_listen(adapter, p2p_conn)) {
mobile_cb_sock_close(adapter, p2p_conn);
return error_packet(packet, 0);
}
s->connections[p2p_conn] = true;
s->state = MOBILE_CONNECTION_WAIT;
return NULL;
}
static struct mobile_packet *command_wait_call_ip(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
// Check if we've received any connection
if (!mobile_cb_sock_accept(adapter, 0)) return NULL;
s->state = MOBILE_CONNECTION_CALL_RECV;
s->call_packets_sent = 0;
packet->length = 0;
return packet;
}
static struct mobile_packet *command_wait_call_relay(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
// Connect to the server and wait for a call
int rc = mobile_relay_proc_wait(adapter, p2p_conn, &b->processing_addr);
if (rc == 0) return NULL;
if (rc < 0) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
s->state = MOBILE_CONNECTION_WAIT_TIMEOUT;
return error_packet(packet, 3);
}
// Interpret the result
int errcode;
switch (rc) {
case MOBILE_RELAY_WAIT_RESULT_ACCEPTED: errcode = -1; break;
case MOBILE_RELAY_WAIT_RESULT_INTERNAL: errcode = 3; break;
default: errcode = 3; break;
}
if (errcode != -1) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
s->state = MOBILE_CONNECTION_WAIT_TIMEOUT;
return error_packet(packet, errcode);
}
s->state = MOBILE_CONNECTION_CALL_RECV;
s->call_packets_sent = 0;
packet->length = 0;
return packet;
}
// Errors:
// 0 - No call received/phone not connected
// 1 - Invalid use (already calling)
// 3 - Internal error (ringing but picking up fails)
static struct mobile_packet *command_wait_call(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
if (s->state != MOBILE_CONNECTION_DISCONNECTED &&
s->state != MOBILE_CONNECTION_WAIT &&
s->state != MOBILE_CONNECTION_WAIT_RELAY &&
s->state != MOBILE_CONNECTION_WAIT_TIMEOUT) {
return error_packet(packet, 1);
}
if (b->processing == PROCESS_WAIT_CALL_INIT) {
// If a previous timeout is in effect, wait it out
if (s->state == MOBILE_CONNECTION_WAIT_TIMEOUT) {
if (!mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND,
1000)) {
return NULL;
}
s->state = MOBILE_CONNECTION_DISCONNECTED;
}
mobile_cb_time_latch(adapter, MOBILE_TIMER_COMMAND);
b->processing = PROCESS_WAIT_CALL_INIT_DONE;
}
// The s->state variable is preserved across multiple commands,
// allowing us to resume a started command.
switch (s->state) {
default:
case MOBILE_CONNECTION_DISCONNECTED:
return command_wait_call_begin(adapter, packet);
case MOBILE_CONNECTION_WAIT:
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 1000)) {
return error_packet(packet, 0);
}
return command_wait_call_ip(adapter, packet);
case MOBILE_CONNECTION_WAIT_RELAY:
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 1000)) {
// If not done connecting to the server, the connection is hanging
// Treat it as if the connection failed
if (adapter->relay.state != MOBILE_RELAY_RECV_WAIT) {
mobile_cb_sock_close(adapter, p2p_conn);
s->connections[p2p_conn] = false;
s->state = MOBILE_CONNECTION_DISCONNECTED;
return error_packet(packet, 3);
}
return error_packet(packet, 0);
}
return command_wait_call_relay(adapter, packet);
}
}
enum process_data {
PROCESS_DATA_INIT,
PROCESS_DATA_INIT_DONE
};
enum procdata_data {
PROCDATA_DATA_SENT_SIZE
};
// Errors:
// 0 - Invalid connection/communication failed
// 1 - Invalid use (Call was ended/never made)
static struct mobile_packet *command_data(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
if (s->state != MOBILE_CONNECTION_CALL &&
s->state != MOBILE_CONNECTION_CALL_RECV &&
s->state != MOBILE_CONNECTION_INTERNET) {
return error_packet(packet, 1);
}
if (packet->length < 1) return error_packet(packet, 0);
const bool internet = s->state == MOBILE_CONNECTION_INTERNET;
unsigned char conn = packet->data[0];
// P2P connections use ID 0xff, but the adapter ignores this
if (!internet) conn = p2p_conn;
if (conn >= MOBILE_MAX_CONNECTIONS || !s->connections[conn]) {
return error_packet(packet, 0);
}
if (b->processing == PROCESS_DATA_INIT) {
b->processing_data[PROCDATA_DATA_SENT_SIZE] = 0;
mobile_cb_time_latch(adapter, MOBILE_TIMER_COMMAND);
b->processing = PROCESS_DATA_INIT_DONE;
}
unsigned sent_size = b->processing_data[PROCDATA_DATA_SENT_SIZE];
unsigned char *data = packet->data + 1;
unsigned send_size = packet->length - 1;
if (send_size > sent_size) {
int rc = mobile_cb_sock_send(adapter, conn, data + sent_size,
send_size - sent_size, NULL);
if (rc < 0) return error_packet(packet, 0);
sent_size += rc;
b->processing_data[PROCDATA_DATA_SENT_SIZE] = sent_size;
// Attempt to send again while not everything has been sent
if (send_size > sent_size) {
// TODO: Verify the timeout with a game
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND,
10000)) {
return error_packet(packet, 0);
}
return NULL;
}
// Pokémon Crystal expects communications to be "synchronized".
// For this, we only try to receive packets when we've sent one.
// TODO: Check other games with peer to peer functionality.
if (!internet) {
if (s->call_packets_sent < 0xFF) s->call_packets_sent++;
}
}
int recv_size = 0;
if (internet || s->call_packets_sent) {
recv_size = mobile_cb_sock_recv(adapter, conn, data,
MOBILE_MAX_TRANSFER_SIZE, NULL);
}
if (!internet && recv_size > 0) {
if (s->call_packets_sent > 0) s->call_packets_sent--;
}
// If connected to the internet, and a disconnect is received, we should
// inform the game about a remote disconnect.
if (internet && recv_size == -2) {
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
packet->command = MOBILE_COMMAND_DATA_END;
packet->length = 1;
return packet;
}
// Allow echoing this packet
if (recv_size == -10) return packet;
// Any other errors should raise a proper error
if (recv_size < 0) return error_packet(packet, 0);
// If nothing was sent, try to receive for at least one second
// TODO: Don't delay for UDP connections
if (internet && !send_size && !recv_size &&
!mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 1000)) {
return NULL;
}
packet->length = recv_size + 1;
return packet;
}
// Errors:
// 2 - Still connected/failed to disconnect(?)
static struct mobile_packet *command_reinit(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
// Reset everything without ending the session
do_end_session(adapter);
do_start_session(adapter);
packet->length = 0;
return packet;
}
static struct mobile_packet *command_check_status(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
switch (s->state) {
// 0xff if phone is disconnected
default:
packet->data[0] = 0;
break;
//case ???:
//packet->data[0] = 1; // Incoming ringing call
//break;
case MOBILE_CONNECTION_CALL:
case MOBILE_CONNECTION_CALL_ISP:
case MOBILE_CONNECTION_INTERNET:
packet->data[0] = 4; // Outgoing established call
break;
case MOBILE_CONNECTION_CALL_RECV:
packet->data[0] = 5; // Incoming established call
break;
}
switch (adapter->serial.device) {
default:
case MOBILE_ADAPTER_BLUE:
packet->data[1] = 0x4D;
break;
case MOBILE_ADAPTER_RED:
case MOBILE_ADAPTER_YELLOW:
packet->data[1] = 0x48;
break;
}
packet->data[2] = adapter->serial.device_unmetered ? 0xF0 : 0x00;
packet->length = 3;
return packet;
}
// Errors:
// 2 - Invalid contents (first byte not either 1 or 0)
static struct mobile_packet *command_change_clock(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
if (packet->length < 1) return error_packet(packet, 2);
if (packet->data[0] != 0 && packet->data[0] != 1) {
return error_packet(packet, 2);
}
s->mode_32bit = packet->data[0] == 1;
#ifdef MOBILE_ENABLE_NO32BIT
// Replying with a different command in the header tricks the official GBA
// library into never changing its serial mode. Using the REINIT command
// forces it to set its serial mode to 8-bit.
packet->command = MOBILE_COMMAND_REINIT;
s->mode_32bit = false;
#endif
packet->length = 0;
return packet;
}
// Errors:
// 0 - Internal error (Failed to read config)
// 2 - Read outside of config area/too big a chunk
static struct mobile_packet *command_eeprom_read(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
if (packet->length < 2) return error_packet(packet, 2);
unsigned offset = packet->data[0];
unsigned size = packet->data[1];
if (size > 0x80) return error_packet(packet, 2);
if (offset + size > MOBILE_CONFIG_SIZE_REAL) {
return error_packet(packet, 2);
}
packet->length = size + 1; // Preserve offset byte
if (size && !mobile_cb_config_read(adapter, packet->data + 1, offset,
size)) {
return error_packet(packet, 0);
}
return packet;
}
// Errors:
// 0 - Internal error (Failed to write config)
// 2 - Invalid use (Tried to write outside of configuration area)
static struct mobile_packet *command_eeprom_write(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
if (packet->length < 1) return error_packet(packet, 2);
unsigned offset = packet->data[0];
unsigned size = packet->length - 1;
if (size > 0x80) return error_packet(packet, 2);
if (offset + size > MOBILE_CONFIG_SIZE_REAL) {
return error_packet(packet, 2);
}
if (size && !mobile_cb_config_write(adapter, packet->data + 1, offset,
size)) {
return error_packet(packet, 0);
}
packet->length = 2;
packet->data[1] = size;
return packet;
}
// Errors:
// 1 - Invalid use (Not in a call)
// 2 - Invalid contents
// 3 - Unknown error (internal error)
static struct mobile_packet *command_ppp_connect(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
// TODO: The original adapter allows logging in multiple times without
// logging out first, but I'm unsure if that's a bug.
if (s->state != MOBILE_CONNECTION_CALL_ISP) {
return error_packet(packet, 1);
}
// Make sure we aren't connected to an actual phone
if (s->connections[p2p_conn]) return error_packet(packet, 3);
const unsigned char *data = packet->data;
if (packet->data + packet->length < data + 1) {
return error_packet(packet, 2);
}
unsigned id_size = *data++;
if (id_size > 0x20) id_size = 0x20;
if (packet->data + packet->length < data + id_size + 1) {
return error_packet(packet, 2);
}
// const unsigned char *id = data;
data += id_size;
unsigned pass_size = *data++;
if (pass_size > 0x20) pass_size = 0x20;
if (packet->data + packet->length < data + pass_size + 8) {
return error_packet(packet, 2);
}
// const unsigned char *pass = data;
data += pass_size;
const unsigned char *dns1 = data + 0;
const unsigned char *dns2 = data + 4;
// Check if the DNS addresses are empty
static const unsigned char dns0[MOBILE_HOSTLEN_IPV4] = {0};
bool dns1_empty = memcmp(dns1, dns0, MOBILE_HOSTLEN_IPV4) == 0;
bool dns2_empty = memcmp(dns2, dns0, MOBILE_HOSTLEN_IPV4) == 0;
// Initialize the DNS address structures with whatever data we've got
s->dns1.type = dns1_empty ? MOBILE_ADDRTYPE_NONE : MOBILE_ADDRTYPE_IPV4;
s->dns1.port = MOBILE_DNS_PORT;
memcpy(s->dns1.host, dns1, MOBILE_HOSTLEN_IPV4);
s->dns2.type = dns2_empty ? MOBILE_ADDRTYPE_NONE : MOBILE_ADDRTYPE_IPV4;
s->dns2.port = MOBILE_DNS_PORT;
memcpy(s->dns2.host, dns2, MOBILE_HOSTLEN_IPV4);
// If either DNS is empty, return the address in the config if available
const unsigned char *dns1_ret = dns0;
const unsigned char *dns2_ret = dns0;
if (dns1_empty && adapter->config.dns1.type == MOBILE_ADDRTYPE_IPV4) {
struct mobile_addr4 *c_dns1 =
(struct mobile_addr4 *)&adapter->config.dns1;
dns1_ret = c_dns1->host;
}
if (dns2_empty && adapter->config.dns2.type == MOBILE_ADDRTYPE_IPV4) {
struct mobile_addr4 *c_dns2 =
(struct mobile_addr4 *)&adapter->config.dns2;
dns2_ret = c_dns2->host;
}
s->dns2_use = 0;
s->state = MOBILE_CONNECTION_INTERNET;
// Return 3 IP addresses, the phone's IP, and the chosen DNS servers.
static const unsigned char ip_local[] = {127, 0, 0, 1};
memcpy(packet->data + 0, ip_local, MOBILE_HOSTLEN_IPV4); // Phone's IP
memcpy(packet->data + 4, dns1_ret, MOBILE_HOSTLEN_IPV4); // Chosen DNS1
memcpy(packet->data + 8, dns2_ret, MOBILE_HOSTLEN_IPV4); // Chosen DNS2
packet->length = 4 * 3;
return packet;
}
// Errors:
// 0 - Invalid use (Not logged in)
// 1 - Invalid use (Not in a call)
// 2 - Unknown error (some kind of timeout?)
static struct mobile_packet *command_ppp_disconnect(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
if (!do_ppp_disconnect(adapter)) return error_packet(packet, 1);
packet->length = 0;
return packet;
}
enum process_tcp_connect {
PROCESS_TCP_CONNECT_BEGIN,
PROCESS_TCP_CONNECT_CONNECTING
};
enum procdata_tcp_connect {
PROCDATA_TCP_CONNECT_CONN
};
static struct mobile_packet *command_tcp_connect_begin(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
if (s->state != MOBILE_CONNECTION_INTERNET) {
return error_packet(packet, 1);
}
if (packet->length < 6) return error_packet(packet, 3);
int conn = connection_new(adapter);
if (conn < 0) return error_packet(packet, 0);
if (!mobile_cb_sock_open(adapter, conn, MOBILE_SOCKTYPE_TCP,
MOBILE_ADDRTYPE_IPV4, 0)) {
return error_packet(packet, 3);
}
s->connections[conn] = true;
b->processing_data[PROCDATA_TCP_CONNECT_CONN] = conn;
b->processing = PROCESS_TCP_CONNECT_CONNECTING;
return NULL;
}
static struct mobile_packet *command_tcp_connect_connecting(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
unsigned char conn = b->processing_data[PROCDATA_TCP_CONNECT_CONN];
struct mobile_addr4 addr = {
.type = MOBILE_ADDRTYPE_IPV4,
.port = packet->data[4] << 8 | packet->data[5],
};
memcpy(addr.host, packet->data, 4);
int rc = mobile_cb_sock_connect(adapter, conn,
(struct mobile_addr *)&addr);
if (rc == 0) return NULL;
if (rc < 0) {
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
return error_packet(packet, 3);
}
packet->data[0] = conn;
packet->length = 1;
return packet;
}
// Errors:
// 0 - Too many connections
// 1 - Invalid use (Not logged in)
// 3 - Connection failed
static struct mobile_packet *command_tcp_connect(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
struct mobile_buffer_commands *b = &adapter->buffer.commands;
switch (b->processing) {
case PROCESS_TCP_CONNECT_BEGIN:
mobile_cb_time_latch(adapter, MOBILE_TIMER_COMMAND);
return command_tcp_connect_begin(adapter, packet);
case PROCESS_TCP_CONNECT_CONNECTING:
// TODO: Verify this timeout with a game
if (mobile_cb_time_check_ms(adapter, MOBILE_TIMER_COMMAND, 60000)) {
unsigned char conn =
b->processing_data[PROCDATA_TCP_CONNECT_CONN];
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
return error_packet(packet, 3);
}
return command_tcp_connect_connecting(adapter, packet);
default:
return error_packet(packet, 3);
}
}
// Errors:
// 0 - Invalid connection (Not connected)
// 1 - Invalid use (Not logged in)
// 2 - Unknown error (???)
static struct mobile_packet *command_tcp_disconnect(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;
if (s->state != MOBILE_CONNECTION_INTERNET) {
return error_packet(packet, 1);
}
if (packet->length < 1) {
return error_packet(packet, 0);
}
unsigned char conn = packet->data[0];
if (conn >= MOBILE_MAX_CONNECTIONS || !s->connections[conn]) {
return error_packet(packet, 0); // UNKERR
}
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
packet->length = 1;
return packet;
}
// Errors:
// 0 - Too many connections
// 1 - Invalid use (Not logged in)
// 2 - Connection failed (though this can't happen)
static struct mobile_packet *command_udp_connect(struct mobile_adapter *adapter, struct mobile_packet *packet)
{