├── trivium_cmd
├── make.sh
├── trivium.h
├── trivium.c
└── main.c
├── snesnet_firmware
├── monitor.sh
├── monitor9600.sh
├── config.mk
├── src
│ ├── Makefile
│ ├── trivium.h
│ ├── io.h
│ ├── trivium.c
│ ├── io.c
│ └── main.c
└── Makefile
├── vsnesnet
├── socat.sh
├── socat_revert.sh
├── vsnesnet
├── Makefile
├── vsnesnet.conf
├── trivium.h
├── vsnesnet.h
├── axbtnmap.h
├── axbtnmap.c
├── login.c
└── vsnesnet.c
├── ControllerInput
├── Mode1.pnps
├── pvsneslib.bmp
├── pvsneslibfont.bmp
├── Mode1.pnproj
├── snesnet.h
├── data.asm
├── snesnet.c
├── hdr.asm
├── Makefile
└── Mode1.c
├── snesnet_lib
├── snesnet.h
└── snesnet.c
├── snesnet_server
├── mod
│ ├── DummyModule.java
│ ├── TestModule.java
│ ├── Module.java
│ └── DemoModule.java
├── net
│ ├── Session.java
│ └── MySQLAccess.java
└── SnesNETsrv.java
└── bsnes_patch
└── bsnes_plus_vsnesnet.patch
/trivium_cmd/make.sh:
--------------------------------------------------------------------------------
1 | gcc -o trivium main.c trivium.c
2 |
--------------------------------------------------------------------------------
/snesnet_firmware/monitor.sh:
--------------------------------------------------------------------------------
1 | minicom -D /dev/ttyUSB0 -b 57600UL
2 |
--------------------------------------------------------------------------------
/vsnesnet/socat.sh:
--------------------------------------------------------------------------------
1 | socat -v tcp-listen:13376 TCP:localhost:13375
2 |
--------------------------------------------------------------------------------
/snesnet_firmware/monitor9600.sh:
--------------------------------------------------------------------------------
1 | minicom -D /dev/ttyUSB0 -b 9600UL
2 |
--------------------------------------------------------------------------------
/vsnesnet/socat_revert.sh:
--------------------------------------------------------------------------------
1 | socat -v tcp-listen:13375 TCP:localhost:13376
2 |
--------------------------------------------------------------------------------
/ControllerInput/Mode1.pnps:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/vsnesnet/vsnesnet:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/saturnu/snesnet/HEAD/vsnesnet/vsnesnet
--------------------------------------------------------------------------------
/ControllerInput/pvsneslib.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/saturnu/snesnet/HEAD/ControllerInput/pvsneslib.bmp
--------------------------------------------------------------------------------
/ControllerInput/pvsneslibfont.bmp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/saturnu/snesnet/HEAD/ControllerInput/pvsneslibfont.bmp
--------------------------------------------------------------------------------
/ControllerInput/Mode1.pnproj:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/vsnesnet/Makefile:
--------------------------------------------------------------------------------
1 | # -*-makefile-*-
2 | .PHONY: all clean
3 |
4 |
5 | all:
6 | gcc -g -O2 -Wall \
7 | -lconfig `mysql_config --cflags --libs` \
8 | -o ./vsnesnet \
9 | axbtnmap.c vsnesnet.c login.c \
10 | -lpthread
11 |
12 |
13 | clean:
14 | rm ./vsnesnet
15 |
16 |
--------------------------------------------------------------------------------
/snesnet_firmware/config.mk:
--------------------------------------------------------------------------------
1 | # -*-makefile-*-
2 |
3 |
4 | # Basic configuration.
5 | BAUD = 9600UL
6 | PORT = /dev/ttyUSB0
7 |
8 |
9 | # Nothing below this line need to be changed.
10 | F_CPU = 16000000UL
11 | MCU = atmega168
12 | OBJ = src/io.o \
13 | src/trivium.o \
14 | src/main.o
15 | PRG = firmware
16 |
--------------------------------------------------------------------------------
/snesnet_firmware/src/Makefile:
--------------------------------------------------------------------------------
1 | # -*-makefile-*-
2 | .PHONY: all clean flash
3 |
4 |
5 | include ../config.mk
6 |
7 |
8 | all:
9 | make -C ../
10 |
11 |
12 | clean:
13 | rm *.o ../*.elf ../*.hex
14 |
15 |
16 | flash: all
17 | fboot -d $(PORT) -b 57600 -H -p ../$(PRG).hex
18 | # fboot -d $(PORT) -b 57600 -H -v -p ../$(PRG).hex
19 |
--------------------------------------------------------------------------------
/snesnet_lib/snesnet.h:
--------------------------------------------------------------------------------
1 | #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
2 | #define SET_BIT(var,pos) ((var) |= 1 << pos)
3 | #define CLEAR_BIT(var,pos) ((var) &= ~(1 << pos))
4 | #define TOGGLE_BIT(var,pos) ((var) ^= 1 << pos)
5 |
6 | #define REG_WRIO (*(vuint8*) 0x4201)
7 | #define REG_RDIO (*(vuint8*) 0x4213)
8 |
9 | void __sendBit(unsigned char type);
10 | void sendByte(unsigned char byte);
11 | unsigned short recvByte(void);
12 |
--------------------------------------------------------------------------------
/ControllerInput/snesnet.h:
--------------------------------------------------------------------------------
1 | #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
2 | #define SET_BIT(var,pos) ((var) |= 1 << pos)
3 | #define CLEAR_BIT(var,pos) ((var) &= ~(1 << pos))
4 | #define TOGGLE_BIT(var,pos) ((var) ^= 1 << pos)
5 |
6 | #define REG_WRIO (*(vuint8*) 0x4201)
7 | #define REG_RDIO (*(vuint8*) 0x4213)
8 |
9 | void __sendBit(unsigned char type);
10 | void sendByte(unsigned char byte);
11 | unsigned short recvByte(void);
12 |
--------------------------------------------------------------------------------
/ControllerInput/data.asm:
--------------------------------------------------------------------------------
1 | .include "hdr.asm"
2 |
3 | .section ".rodata1" superfree
4 |
5 | patterns:
6 | .incbin "pvsneslib.pic"
7 | patterns_end:
8 |
9 | .ends
10 |
11 | .section ".rodata2" superfree
12 | map:
13 | .incbin "pvsneslib.map"
14 | map_end:
15 |
16 | palette:
17 | .incbin "pvsneslib.pal"
18 | palette_end:
19 |
20 | .ends
21 |
22 | .section ".rodata3" superfree
23 |
24 | snesfont:
25 | .incbin "pvsneslibfont.pic"
26 |
27 | .ends
28 |
--------------------------------------------------------------------------------
/snesnet_server/mod/DummyModule.java:
--------------------------------------------------------------------------------
1 | package mod;
2 |
3 | import java.io.DataOutputStream;
4 |
5 | public class DummyModule extends Module {
6 |
7 |
8 |
9 | public DummyModule(DataOutputStream dos) {
10 | super(dos);
11 |
12 | }
13 |
14 | @Override
15 | public void setStatus(String string) {
16 | // TODO Auto-generated method stub
17 |
18 | }
19 |
20 | @Override
21 | public void setCommand(String cmd) {
22 | // TODO Auto-generated method stub
23 |
24 | }
25 |
26 | @Override
27 | public byte getMagicByte() {
28 | return 0x00;
29 | }
30 |
31 | @Override
32 | public String getStatus() {
33 | // TODO Auto-generated method stub
34 | return null;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/snesnet_firmware/Makefile:
--------------------------------------------------------------------------------
1 | # -*-makefile-*-
2 | .PHONY: all clean flash
3 |
4 | include config.mk
5 |
6 | all: $(PRG).hex
7 |
8 | $(PRG).hex: $(PRG).elf
9 | avr-objcopy -j .text -j .data -O ihex $(PRG).elf $(PRG).hex
10 | avr-size --target=ihex $(PRG).hex
11 |
12 | $(PRG).elf: $(OBJ)
13 | avr-gcc $+ -o $(PRG).elf -mmcu=$(MCU)
14 | avr-size -A $(PRG).elf
15 |
16 | %.o: %.c
17 | avr-gcc $< \
18 | -Os \
19 | -Wno-deprecated-declarations \
20 | -D__PROG_TYPES_COMPAT__ \
21 | -fno-tree-scev-cprop \
22 | -std=c99 \
23 | -DF_CPU=$(F_CPU) \
24 | -g -mmcu=$(MCU) -c -o $@
25 |
26 | clean:
27 | rm src/*.o *.elf *.hex
28 |
29 | flash: $(PRG).hex
30 | fboot -d $(PORT) -b $(BAUD) -H -v -p $(PRG).hex
31 |
--------------------------------------------------------------------------------
/snesnet_lib/snesnet.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "snesnet.h"
3 |
4 |
5 | unsigned short recvByte(void){
6 |
7 | scanPads();
8 |
9 | return padsCurrent(1);
10 | }
11 |
12 | void sendByte(unsigned char byte){
13 |
14 | int i;
15 | for(i=0; i<8; i++){
16 |
17 | if(CHECK_BIT(byte,i))
18 | __sendBit(1);
19 | else
20 | __sendBit(0);
21 | }
22 |
23 | }
24 |
25 | void __sendBit(unsigned char type){
26 |
27 | if(type==1){
28 | REG_WRIO=0x40; //01
29 |
30 | WaitForVBlank();
31 | REG_WRIO=0xC0; //11
32 |
33 | WaitForVBlank();
34 | REG_WRIO=0x40; //01
35 | }
36 | else{
37 |
38 | REG_WRIO=0x00; //00
39 |
40 | WaitForVBlank();
41 | REG_WRIO=0x80; //10
42 |
43 | WaitForVBlank();
44 | REG_WRIO=0x00; //00
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/ControllerInput/snesnet.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "snesnet.h"
3 |
4 |
5 | unsigned short recvByte(void){
6 |
7 | scanPads();
8 |
9 | return padsCurrent(1);
10 | }
11 |
12 | void sendByte(unsigned char byte){
13 |
14 | int i;
15 | for(i=0; i<8; i++){
16 |
17 | if(CHECK_BIT(byte,i))
18 | __sendBit(1);
19 | else
20 | __sendBit(0);
21 | }
22 |
23 | }
24 |
25 | void __sendBit(unsigned char type){
26 |
27 | if(type==1){
28 | REG_WRIO=0x40; //01
29 |
30 | WaitForVBlank();
31 | REG_WRIO=0xC0; //11
32 |
33 | WaitForVBlank();
34 | REG_WRIO=0x40; //01
35 | }
36 | else{
37 |
38 | REG_WRIO=0x00; //00
39 |
40 | WaitForVBlank();
41 | REG_WRIO=0x80; //10
42 |
43 | WaitForVBlank();
44 | REG_WRIO=0x00; //00
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/vsnesnet/vsnesnet.conf:
--------------------------------------------------------------------------------
1 | // vSNESnet configuration.
2 |
3 | //login data
4 | username = "client1"
5 | password = "pSKw1QXngF5et4DC0vWxhC"
6 | key = "AiD8Ofh57K"
7 |
8 | //login server data
9 | loginserver = "192.168.123.222"
10 | server_port = 13375
11 |
12 | //input game pad device
13 | //test with 'jstest --event /dev/input/js0'
14 | js_dev = "/dev/input/js0"
15 | js_axis_type = 2
16 | js_button_type = 1
17 |
18 | js_axis_up_val = -32767
19 | js_axis_down_val = 32767
20 | js_axis_left_val = -32767
21 | js_axis_right_val = 32767
22 |
23 | js_axis_y_nr = 1
24 | js_axis_x_nr = 0
25 |
26 | js_button_a_nr = 1
27 | js_button_b_nr = 2
28 | js_button_x_nr = 0
29 | js_button_y_nr = 3
30 | js_button_l_nr = 4
31 | js_button_r_nr = 5
32 | js_button_st_nr = 9
33 | js_button_se_nr = 8
34 |
35 | //debug settings
36 | //disable remapping js0->js1
37 | disable_p1 = 1
38 | //select+x to exit
39 | select_x = 1
40 | //mapping mode
41 | mapping_mode = 0
42 | //debug messages
43 | debug = 0
44 |
45 |
--------------------------------------------------------------------------------
/snesnet_server/mod/TestModule.java:
--------------------------------------------------------------------------------
1 | package mod;
2 |
3 | import java.io.DataOutputStream;
4 |
5 | public class TestModule extends Module{
6 |
7 |
8 | public TestModule(DataOutputStream dos) {
9 | super(dos);
10 | }
11 |
12 | @Override
13 | public byte getMagicByte() {
14 | return 0x01;
15 | }
16 |
17 | @Override
18 | public void setCommand(String cmd) {
19 |
20 |
21 | if(cmd.equals("74")){
22 |
23 |
24 | //0b1111 11 111111111
25 | //0=bt pressed, 1=bt released
26 | //FFFE = B
27 | byte[] data = new byte[2];
28 | data[0]=(byte) 0xFF;
29 | data[1]=(byte) 0xFE; //B
30 |
31 | sendBytes(data);
32 |
33 |
34 | try {
35 | Thread.sleep(35);
36 | } catch (InterruptedException e) {
37 | // TODO Auto-generated catch block
38 | e.printStackTrace();
39 | }
40 |
41 |
42 | data[0]=(byte) 0xFF;
43 | data[1]=(byte) 0xFF;
44 |
45 | sendBytes(data);
46 |
47 | }
48 |
49 | }
50 |
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/snesnet_server/net/Session.java:
--------------------------------------------------------------------------------
1 | package net;
2 |
3 | import java.io.DataOutputStream;
4 |
5 | import mod.Module;
6 |
7 | public class Session {
8 |
9 | private int id;
10 | private String username;
11 | private Module module0;
12 | private long logout_time;
13 |
14 |
15 |
16 | public Session(int id_num, String name){
17 |
18 | id=id_num;
19 | setUsername(name);
20 | setLogout_time(0);
21 | }
22 |
23 |
24 | public int getId() {
25 | return id;
26 | }
27 | public void setId(int id) {
28 | this.id = id;
29 | }
30 |
31 |
32 | public String getUsername() {
33 | return username;
34 | }
35 |
36 |
37 | public void setUsername(String username) {
38 | this.username = username;
39 | }
40 |
41 |
42 | public Module getModule() {
43 | return module0;
44 | }
45 |
46 |
47 | public void setModule(Module module0) {
48 | this.module0 = module0;
49 | }
50 |
51 |
52 | public long getLogout_time() {
53 | return logout_time;
54 | }
55 |
56 |
57 | public void setLogout_time(long logout_time) {
58 | this.logout_time = logout_time;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/vsnesnet/trivium.h:
--------------------------------------------------------------------------------
1 | /* trivium.h */
2 | /*
3 | This file is part of the AVR-Crypto-Lib.
4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | #ifndef TRIVIUM_H_
20 | #define TRIVIUM_H_
21 |
22 | typedef uint8_t trivium_ctx_t[36]; /* 288bit */
23 |
24 | uint8_t trivium_enc(trivium_ctx_t* ctx);
25 | uint8_t trivium_getbyte(trivium_ctx_t* ctx);
26 | void trivium_init(const void* key, uint16_t keysize_b,
27 | const void* iv, uint16_t ivsize_b,
28 | trivium_ctx_t* ctx);
29 |
30 | #endif /*TRIVIUM_H_*/
31 |
--------------------------------------------------------------------------------
/trivium_cmd/trivium.h:
--------------------------------------------------------------------------------
1 | /* trivium.h */
2 | /*
3 | This file is part of the AVR-Crypto-Lib.
4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | #ifndef TRIVIUM_H_
20 | #define TRIVIUM_H_
21 |
22 | typedef uint8_t trivium_ctx_t[36]; /* 288bit */
23 |
24 | uint8_t trivium_enc(trivium_ctx_t* ctx);
25 | uint8_t trivium_getbyte(trivium_ctx_t* ctx);
26 | void trivium_init(const void* key, uint16_t keysize_b,
27 | const void* iv, uint16_t ivsize_b,
28 | trivium_ctx_t* ctx);
29 |
30 | #endif /*TRIVIUM_H_*/
31 |
--------------------------------------------------------------------------------
/snesnet_firmware/src/trivium.h:
--------------------------------------------------------------------------------
1 | /* trivium.h */
2 | /*
3 | This file is part of the AVR-Crypto-Lib.
4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #ifndef TRIVIUM_H_
21 | #define TRIVIUM_H_
22 |
23 | typedef uint8_t trivium_ctx_t[36]; /* 288bit */
24 |
25 | uint8_t trivium_enc(trivium_ctx_t *ctx);
26 | uint8_t trivium_getbyte(trivium_ctx_t *ctx);
27 | void trivium_init(const void *key, uint16_t keysize_b,
28 | const void *iv, uint16_t ivsize_b,
29 | trivium_ctx_t *ctx);
30 |
31 | #endif
32 |
33 | /*TRIVIUM_H_*/
34 |
--------------------------------------------------------------------------------
/snesnet_server/mod/Module.java:
--------------------------------------------------------------------------------
1 | package mod;
2 |
3 | import java.io.DataOutputStream;
4 | import java.io.IOException;
5 |
6 |
7 | public class Module {
8 |
9 | String status;
10 | DataOutputStream dos;
11 |
12 |
13 |
14 |
15 | public void setDos(DataOutputStream dos) {
16 | this.dos = dos;
17 | }
18 |
19 |
20 | public Module(DataOutputStream dos) {
21 | this.dos=dos;
22 | status=null;
23 | }
24 |
25 |
26 | public void setStatus(String string) {
27 | status=string;
28 | }
29 |
30 |
31 | public void setCommand(String cmd) {
32 | }
33 |
34 | public byte getMagicByte () {
35 | return 0x00;
36 | }
37 |
38 | public String getStatus() {
39 | return status;
40 | }
41 |
42 |
43 | public int sendBytes(byte data[]){
44 | try {
45 |
46 | if(dos==null)
47 | System.out.println("dos is null");
48 | else
49 | dos.write(data, 0, 2);
50 | } catch (IOException e) {
51 | // TODO Auto-generated catch block
52 | e.printStackTrace();
53 | } //.print(a+""+b);
54 |
55 | try {
56 | if(dos==null)
57 | System.out.println("dos is null");
58 | else
59 | dos.flush();
60 | } catch (IOException e) {
61 | // TODO Auto-generated catch block
62 | e.printStackTrace();
63 | }
64 | // System.out.println("data: "+data);
65 | return 0;
66 | }
67 |
68 |
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/snesnet_server/mod/DemoModule.java:
--------------------------------------------------------------------------------
1 |
2 | package mod;
3 |
4 | import java.io.DataOutputStream;
5 |
6 | public class DemoModule extends Module{
7 |
8 | private long str_length;
9 | private String string0;
10 |
11 |
12 | public DemoModule(DataOutputStream dos) {
13 | super(dos);
14 | str_length=0;
15 | string0="";
16 | }
17 |
18 | @Override
19 | public byte getMagicByte() {
20 | return 0x02;
21 | }
22 |
23 | @Override
24 | public void setCommand(String cmd) {
25 |
26 | if(str_length==0){
27 | str_length = Long.parseLong(cmd, 16);
28 | System.out.println("str_length="+str_length);
29 | }else{
30 |
31 |
32 | if(str_length>0){
33 | string0=string0+((char)Long.parseLong(cmd, 16));
34 |
35 | }
36 |
37 | if(string0.length()==str_length && str_length!=0){
38 | System.out.println("received string: "+string0);
39 | str_length=0;
40 | string0="";
41 | }
42 | }
43 | /*
44 | if(cmd.equals("74")){
45 |
46 |
47 | //0b1111 11 111111111
48 | //0=bt pressed, 1=bt released
49 | //FFFE = B
50 | byte[] data = new byte[2];
51 | data[0]=(byte) 0xFF;
52 | data[1]=(byte) 0xFE; //B
53 |
54 | sendBytes(data);
55 |
56 |
57 | try {
58 | Thread.sleep(35);
59 | } catch (InterruptedException e) {
60 | // TODO Auto-generated catch block
61 | e.printStackTrace();
62 | }
63 |
64 |
65 | data[0]=(byte) 0xFF;
66 | data[1]=(byte) 0xFF;
67 |
68 | sendBytes(data);
69 |
70 | }
71 | */
72 |
73 | }
74 |
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/bsnes_patch/bsnes_plus_vsnesnet.patch:
--------------------------------------------------------------------------------
1 | diff -rupN bsnes-plus-master/bsnes/snes/cpu/mmio/mmio.cpp bsnes-plus-master_patch/bsnes/snes/cpu/mmio/mmio.cpp
2 | --- bsnes-plus-master/bsnes/snes/cpu/mmio/mmio.cpp 2015-04-26 01:13:32.000000000 +0200
3 | +++ bsnes-plus-master_patch/bsnes/snes/cpu/mmio/mmio.cpp 2015-05-07 23:03:23.309643855 +0200
4 | @@ -1,3 +1,5 @@
5 | +#include
6 | +
7 | #ifdef CPU_CPP
8 |
9 | uint8 CPU::pio() { return status.pio; }
10 | @@ -66,8 +68,49 @@ void CPU::mmio_w4200(uint8 data) {
11 |
12 | //WRIO
13 | void CPU::mmio_w4201(uint8 data) {
14 | +
15 | + //is only called when register updates
16 | + static char init=0;
17 | + static int out_fd;
18 | + static uint8 last_state;
19 | +
20 | if((status.pio & 0x80) && !(data & 0x80)) ppu.latch_counters();
21 | status.pio = data;
22 | +
23 | + //last_state=data;
24 | + if(init==0){
25 | + last_state=0x00;
26 | + //open file descriptor
27 | + out_fd=open("/tmp/snes_wrio.fifo", O_WRONLY);
28 | +
29 | + if (out_fd==-1) {
30 | + perror("WRIO: file open error");
31 | + init=-1;
32 | + }else{
33 | + init=1;
34 | + }
35 | + }
36 | +
37 | +
38 | + if(init && data!=0xff){
39 | +
40 | + char output[2];
41 | + output[1]='\0';
42 | +
43 | + if((data==0xC0 || data==0x80) && (last_state==0x00 || last_state==0x40)){
44 | +
45 | + if(data==0xC0) output[0]=0x31; else output[0]=0x30;
46 | +
47 | + if (write(out_fd, output, sizeof(output))==-1) {
48 | +
49 | + perror("WRIO: write error");
50 | + init=-1;
51 | + }
52 | + }
53 | +
54 | + last_state=data;
55 | + }
56 | +
57 | }
58 |
59 | //WRMPYA
60 |
--------------------------------------------------------------------------------
/ControllerInput/hdr.asm:
--------------------------------------------------------------------------------
1 |
2 | .MEMORYMAP ; Begin describing the system architecture.
3 | SLOTSIZE $8000 ; The slot is $8000 bytes in size. More details on slots later.
4 | DEFAULTSLOT 0 ; There's only 1 slot in SNES, there are more in other consoles.
5 | SLOT 0 $8000 ; Defines Slot 0's starting address.
6 | SLOT 1 $0 $2000
7 | SLOT 2 $2000 $E000
8 | SLOT 3 $0 $10000
9 | .ENDME ; End MemoryMap definition
10 |
11 | .ROMBANKSIZE $8000 ; Every ROM bank is 32 KBytes in size
12 | .ROMBANKS 8 ; 2 Mbits - Tell WLA we want to use 8 ROM Banks
13 |
14 | .SNESHEADER
15 | ID "SNES" ; 1-4 letter string, just leave it as "SNES"
16 |
17 | NAME "LIBSNES MODE1 DEMO " ; Program Title - can't be over 21 bytes,
18 | ; "123456789012345678901" ; use spaces for unused bytes of the name.
19 |
20 | SLOWROM
21 | LOROM
22 |
23 | CARTRIDGETYPE $02 ; $02 = ROM+SRAM, see WLA documentation for others
24 | ROMSIZE $08 ; $08 = 2 Mbits, see WLA doc for more..
25 | SRAMSIZE $01 ; $01 = 16 kbits, see WLA doc for more..
26 | COUNTRY $01 ; $01 = U.S. $00 = Japan, that's all I know
27 | LICENSEECODE $00 ; Just use $00
28 | VERSION $00 ; $00 = 1.00, $01 = 1.01, etc.
29 | .ENDSNES
30 |
31 | .SNESNATIVEVECTOR ; Define Native Mode interrupt vector table
32 | COP EmptyHandler
33 | BRK EmptyHandler
34 | ABORT EmptyHandler
35 | NMI VBlank
36 | IRQ EmptyHandler
37 | .ENDNATIVEVECTOR
38 |
39 | .SNESEMUVECTOR ; Define Emulation Mode interrupt vector table
40 | COP EmptyHandler
41 | ABORT EmptyHandler
42 | NMI EmptyHandler
43 | RESET tcc__start ; where execution starts
44 | IRQBRK EmptyHandler
45 | .ENDEMUVECTOR
46 |
--------------------------------------------------------------------------------
/vsnesnet/vsnesnet.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 |
9 | #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
10 | #define SET_BIT(var,pos) ((var) |= 1 << pos)
11 | #define CLEAR_BIT(var,pos) ((var) &= ~(1 << pos))
12 | #define TOGGLE_BIT(var,pos) ((var) ^= 1 << pos)
13 |
14 |
15 | #define MAX_USERNAME_SIZE 33
16 | #define KEY_LENGTH 11
17 | #define IV_LENGTH 11
18 | #define MAX_PASSWORD_LENGTH 256
19 | #define MAX_MSG_SIZE 1024
20 | #define FIFO_FILE "/tmp/snes_wrio.fifo"
21 |
22 | #define TRUE 1
23 | #define FALSE 0
24 |
25 | int init_login(char *confFile);
26 | void stop_snesnet();
27 | int process_incoming(uint8_t a, uint8_t b);
28 | const char *byte_to_binary(int x);
29 |
30 | typedef uint8_t trivium_ctx_t[36]; /* 288bit */
31 | extern int sock; /* Socket descriptor */
32 |
33 | uint8_t trivium_enc(trivium_ctx_t* ctx);
34 | uint8_t trivium_getbyte(trivium_ctx_t* ctx);
35 | void trivium_init(const void* key, uint16_t keysize_b, const void* iv, uint16_t ivsize_b, trivium_ctx_t* ctx);
36 |
37 |
38 | int _SNESnet_master;
39 | int _SNESnet_server_port;
40 |
41 | char _SNESnet_server[128];
42 | char _SNESnet_username[128];
43 | char _SNESnet_password[128];
44 | char _SNESnet_key[128];
45 |
46 | char _SNESnet_master_addr[128];
47 | int _SNESnet_master_port;
48 | int _SNESnet_socket_tcp;
49 |
50 | struct sockaddr_in _SNESnet_server_addr;
51 | struct sockaddr_in _SNESnet_client_addr;
52 |
53 | const char *js_dev;
54 | int debug;
55 | int ready;
56 | int fdx;
57 |
58 | //cheap controller mapping
59 | int js_axis_type;
60 | int js_button_type;
61 |
62 | int js_axis_up_val;
63 | int js_axis_down_val;
64 | int js_axis_left_val;
65 | int js_axis_right_val;
66 |
67 | int js_axis_y_nr;
68 | int js_axis_x_nr;
69 |
70 | int js_button_a_nr;
71 | int js_button_b_nr;
72 | int js_button_x_nr;
73 | int js_button_y_nr;
74 | int js_button_l_nr;
75 | int js_button_r_nr;
76 | int js_button_st_nr;
77 | int js_button_se_nr;
78 |
79 | int disable_p1;
80 | int select_x;
81 |
82 | int mapping_mode;
83 |
84 |
--------------------------------------------------------------------------------
/snesnet_firmware/src/io.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #define LEDgreenDDR DDRC
5 | #define LEDgreenPORT PORTC
6 | #define LEDgreen PC0
7 |
8 | #define LEDyellowDDR DDRC
9 | #define LEDyellowPORT PORTC
10 | #define LEDyellow PC1
11 |
12 |
13 | #define InputClockDDR DDRD
14 | #define InputClockPORT PORTD
15 | #define InputClock PD5
16 |
17 | #define InputLatchDDR DDRD
18 | #define InputLatchPORT PORTD
19 | #define InputLatch PD6
20 |
21 | #define InputDataDDR DDRD
22 | #define InputDataPORT PORTD
23 | #define InputDataPIN PIND
24 | #define InputData PD7
25 |
26 |
27 | #define Port0ClockDDR DDRC
28 | #define Port0ClockPORT PORTC
29 | #define Port0ClockPIN PINC
30 | #define Port0Clock PC2
31 |
32 | #define Port0LatchDDR DDRC
33 | #define Port0LatchPORT PORTC
34 | #define Port0LatchPIN PINC
35 | #define Port0Latch PC3
36 |
37 | #define Port0DataDDR DDRC
38 | #define Port0DataPORT PORTC
39 | #define Port0Data PC4
40 |
41 |
42 | #define Port1ClockDDR DDRD
43 | #define Port1ClockPORT PORTD
44 | #define Port1ClockPIN PIND
45 | #define Port1Clock PD4
46 |
47 | #define Port1LatchDDR DDRD
48 | #define Port1LatchPORT PORTD
49 | #define Port1LatchPIN PIND
50 | #define Port1Latch PD3 //int1
51 |
52 | #define Port1DataDDR DDRC //PORTD
53 | #define Port1DataPORT PORTC //PORTD
54 | #define Port1Data PC5 //PD2 freed for int0 -> wiznet interrupt new (PC5)
55 |
56 | // IO BIT
57 | #define Port0IODDR DDRB
58 | #define Port0IOPORT PORTB
59 | #define Port0IOData PB0
60 |
61 | #define Port1IODDR DDRB
62 | #define Port1IOPORT PORTB
63 | #define Port1IOData PB1
64 | //IO BIT END
65 |
66 | #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
67 | #define SET_BIT(var,pos) ((var) |= 1 << pos)
68 | #define CLEAR_BIT(var,pos) ((var) &= ~(1 << pos))
69 | #define TOGGLE_BIT(var,pos) ((var) ^= 1 << pos)
70 |
71 |
72 | void initInput(void);
73 | void initOutput(void);
74 | uint8_t recvGPIO(uint8_t port);
75 | uint16_t recvInput(void);
76 | void initIO(void);
77 | //void sendPorts(uint16_t port0, uint16_t port1);
78 | void sendPort_0(uint16_t port0);
79 | void sendPort_1(uint16_t port1);
80 |
81 |
82 | void initLed(void);
83 | void ledOff(void);
84 | void ledOnGreen(void);
85 | void ledOnYellow(void);
86 | void ledToggleGreen(void);
87 | void ledToggleYellow(void);
88 | void ledSignal(uint8_t times);
89 |
--------------------------------------------------------------------------------
/vsnesnet/axbtnmap.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Axis and button map support functions.
3 | * Copyright © 2009 Stephen Kitt
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #ifndef __AXBTNMAP_H__
21 | #define __AXBTNMAP_H__
22 |
23 | #include
24 | #include
25 |
26 | /* The following values come from include/input.h in the kernel
27 | source; the small variant is used up to version 2.6.27, the large
28 | one from 2.6.28 onwards. We need to handle both values because the
29 | kernel doesn't; it only expects one of the values, and we need to
30 | determine which one at run-time. */
31 | #define KEY_MAX_LARGE 0x2FF
32 | #define KEY_MAX_SMALL 0x1FF
33 |
34 | /* Axis map size. */
35 | #define AXMAP_SIZE (ABS_MAX + 1)
36 |
37 | /* Button map size. */
38 | #define BTNMAP_SIZE (KEY_MAX_LARGE - BTN_MISC + 1)
39 |
40 | /* Retrieves the current axis map in the given array, which must
41 | contain at least AXMAP_SIZE elements. Returns the result of the
42 | ioctl(): negative in case of an error, 0 otherwise for kernels up
43 | to 2.6.30, the length of the array actually copied for later
44 | kernels. */
45 | int getaxmap(int fd, uint8_t *axmap);
46 |
47 | /* Uses the given array as the axis map. The array must contain at
48 | least AXMAP_SIZE elements. Returns the result of the ioctl():
49 | negative in case of an error, 0 otherwise. */
50 | int setaxmap(int fd, uint8_t *axmap);
51 |
52 | /* Retrieves the current button map in the given array, which must
53 | contain at least BTNMAP_SIZE elements. Returns the result of the
54 | ioctl(): negative in case of an error, 0 otherwise for kernels up
55 | to 2.6.30, the length of the array actually copied for later
56 | kernels. */
57 | int getbtnmap(int fd, uint16_t *btnmap);
58 |
59 | /* Uses the given array as the button map. The array must contain at
60 | least BTNMAP_SIZE elements. Returns the result of the ioctl():
61 | negative in case of an error, 0 otherwise. */
62 | int setbtnmap(int fd, uint16_t *btnmap);
63 |
64 | #endif
65 |
66 |
--------------------------------------------------------------------------------
/vsnesnet/axbtnmap.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Axis and button map support functions.
3 | * Copyright © 2009 Stephen Kitt
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along
16 | * with this program; if not, write to the Free Software Foundation, Inc.,
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 | */
19 |
20 | #include
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 | #include
28 |
29 | #include "axbtnmap.h"
30 |
31 | /* The following values come from include/joystick.h in the kernel source. */
32 | #define JSIOCSBTNMAP_LARGE _IOW('j', 0x33, __u16[KEY_MAX_LARGE - BTN_MISC + 1])
33 | #define JSIOCSBTNMAP_SMALL _IOW('j', 0x33, __u16[KEY_MAX_SMALL - BTN_MISC + 1])
34 | #define JSIOCGBTNMAP_LARGE _IOR('j', 0x34, __u16[KEY_MAX_LARGE - BTN_MISC + 1])
35 | #define JSIOCGBTNMAP_SMALL _IOR('j', 0x34, __u16[KEY_MAX_SMALL - BTN_MISC + 1])
36 |
37 | int determine_ioctl(int fd, int *ioctls, int *ioctl_used, void *argp)
38 | {
39 | int i, retval = 0;
40 |
41 | /* Try each ioctl in turn. */
42 | for (i = 0; ioctls[i]; i++) {
43 | if ((retval = ioctl(fd, ioctls[i], argp)) >= 0) {
44 | /* The ioctl did something. */
45 | *ioctl_used = ioctls[i];
46 | return retval;
47 | } else if (errno != -EINVAL) {
48 | /* Some other error occurred. */
49 | return retval;
50 | }
51 | }
52 | return retval;
53 | }
54 |
55 | int getbtnmap(int fd, uint16_t *btnmap)
56 | {
57 | static int jsiocgbtnmap = 0;
58 | int ioctls[] = { JSIOCGBTNMAP, JSIOCGBTNMAP_LARGE, JSIOCGBTNMAP_SMALL, 0 };
59 |
60 | if (jsiocgbtnmap != 0) {
61 | /* We already know which ioctl to use. */
62 | return ioctl(fd, jsiocgbtnmap, btnmap);
63 | } else {
64 | return determine_ioctl(fd, ioctls, &jsiocgbtnmap, btnmap);
65 | }
66 | }
67 |
68 | int setbtnmap(int fd, uint16_t *btnmap)
69 | {
70 | static int jsiocsbtnmap = 0;
71 | int ioctls[] = { JSIOCSBTNMAP, JSIOCSBTNMAP_LARGE, JSIOCSBTNMAP_SMALL, 0 };
72 |
73 | if (jsiocsbtnmap != 0) {
74 | /* We already know which ioctl to use. */
75 | return ioctl(fd, jsiocsbtnmap, btnmap);
76 | } else {
77 | return determine_ioctl(fd, ioctls, &jsiocsbtnmap, btnmap);
78 | }
79 | }
80 |
81 | int getaxmap(int fd, uint8_t *axmap)
82 | {
83 | return ioctl(fd, JSIOCGAXMAP, axmap);
84 | }
85 |
86 | int setaxmap(int fd, uint8_t *axmap)
87 | {
88 | return ioctl(fd, JSIOCSAXMAP, axmap);
89 | }
90 |
--------------------------------------------------------------------------------
/ControllerInput/Makefile:
--------------------------------------------------------------------------------
1 | # path to snesdev root directory (for emulators, devkitsnes, libsnes)
2 | export DEVKITSNES := /home/tt/snes/pvsneslib-read-only/
3 |
4 | # path to devkitsnes root directory for compiler
5 | export DEVKIT65XX := /home/tt/snes/pvsneslib-read-only/devkitsnes
6 |
7 | #---------------------------------------------------------------------------------
8 | .SUFFIXES:
9 | #---------------------------------------------------------------------------------
10 |
11 | ifeq ($(strip $(DEVKIT65XX)),)
12 | $(error "Please set DEVKIT65XX in your environment. export DEVKIT65XX=devkit65XX")
13 | endif
14 |
15 | include $(DEVKIT65XX)/snes_rules
16 |
17 | #---------------------------------------------------------------------------------
18 | # TARGET is the name of the output
19 | # BUILD is the directory where object files & intermediate files will be placed
20 | # SOURCES is a list of directories containing source code
21 | # INCLUDES is a list of directories containing extra header files
22 | #---------------------------------------------------------------------------------
23 | TARGET := $(shell basename $(CURDIR))
24 | SOURCES := .
25 |
26 | #---------------------------------------------------------------------------------
27 | # options for code generation
28 | #---------------------------------------------------------------------------------
29 | CFLAGS += $(INCLUDE)
30 |
31 | #---------------------------------------------------------------------------------
32 | # list of directories containing libraries, this must be the top level containing
33 | # include and lib
34 | #---------------------------------------------------------------------------------
35 | LIBDIRS := $(PVSNESLIB)
36 | #LIBOBJS +:=
37 |
38 | export OUTPUT := $(CURDIR)/$(TARGET)
39 |
40 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
41 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.asm)))
42 |
43 | #---------------------------------------------------------------------------------
44 |
45 | export OFILES := $(BINFILES:.bin=.obj) $(CFILES:.c=.obj) $(SFILES:.asm=.obj)
46 |
47 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
48 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
49 | -I$(CURDIR)/$(BUILD)
50 |
51 | GTITLE := -ht"$(TARGET)"
52 |
53 | .PHONY: bitmaps all
54 |
55 | #---------------------------------------------------------------------------------
56 | all : bitmaps $(OUTPUT).sfc
57 | $(SNTOOLS) -hi! $(GTITLE) $(TARGET).sfc
58 |
59 | clean:
60 | @echo clean ...
61 | @rm -f $(OFILES) $(TARGET).sfc $(TARGET).sym *.pic *.map *.pal
62 |
63 |
64 | pvsneslibfont.pic: pvsneslibfont.bmp
65 | @echo convert font with no tile reduction ... $(notdir $@)
66 | $(GFXCONV) -n -gs8 -po2 -pc16 -pe1 -mR! -m! -p! $<
67 |
68 |
69 | #---------------------------------------------------------------------------------
70 | bitmaps : pvsneslibfont.pic pvsneslib.pic
71 |
72 | #---------------------------------------------------------------------------------
73 | $(OUTPUT).sfc : $(OFILES)
74 |
--------------------------------------------------------------------------------
/snesnet_firmware/src/trivium.c:
--------------------------------------------------------------------------------
1 | /* trivium.c */
2 | /*
3 | This file is part of the AVR-Crypto-Lib.
4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | /**
20 | *
21 | * author: Daniel Otte
22 | * email: daniel.otte@rub.de
23 | * license: GPLv3
24 | *
25 | */
26 |
27 |
28 | #include
29 | #include
30 | #include "trivium.h"
31 | #include
32 |
33 | #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
34 | #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
35 | uint8_t trivium_enc(trivium_ctx_t *ctx){
36 | uint8_t t1,t2,t3,z;
37 |
38 | t1 = G(65) ^ G(92);
39 | t2 = G(161) ^ G(176);
40 | t3 = G(242) ^ G(287);
41 | z = t1^t2^t3;
42 | t1 ^= (G(90) & G(91)) ^ G(170);
43 | t2 ^= (G(174) & G(175)) ^ G(263);
44 | t3 ^= (G(285) & G(286)) ^ G(68);
45 |
46 | /* shift whole state and insert ts later */
47 | uint8_t i,c1=0,c2;
48 | for(i=0; i<36; ++i){
49 | c2=(((*ctx)[i])>>7);
50 | (*ctx)[i] = (((*ctx)[i])<<1)|c1;
51 | c1=c2;
52 | }
53 | /* insert ts */
54 | S(0, t3);
55 | S(93, t1);
56 | S(177, t2);
57 |
58 | return z?0x080:0x00;
59 | }
60 |
61 | uint8_t trivium_getbyte(trivium_ctx_t *ctx){
62 | uint8_t r=0, i=0;
63 | do{
64 | r>>=1;
65 | r |= trivium_enc(ctx);
66 | }while(++i<8);
67 | return r;
68 | }
69 |
70 | #define KEYSIZE_B ((keysize_b+7)/8)
71 | #define IVSIZE_B ((ivsize_b +7)/8)
72 |
73 | static const uint8_t rev_table[16] PROGMEM = {
74 | 0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
75 | 0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
76 | 0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
77 | 0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
78 | };
79 |
80 | void trivium_init(const void *key, uint16_t keysize_b,
81 | const void *iv, uint16_t ivsize_b,
82 | trivium_ctx_t *ctx){
83 | uint16_t i;
84 | uint8_t c1,c2;
85 | uint8_t t1,t2;
86 | memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
87 | c2=0;
88 | c1=KEYSIZE_B;
89 | do{
90 | t1 = ((uint8_t*)key)[--c1];
91 | t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
92 | (*ctx)[c2++] = t2;
93 | }while(c1!=0);
94 |
95 | c2=12;
96 | c1=IVSIZE_B;
97 | do{
98 | t1 = ((uint8_t*)iv)[--c1];
99 | t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
100 | (*ctx)[c2++] = t2;
101 | }while(c1!=0);
102 |
103 | for(i=12+IVSIZE_B; i>10; --i){
104 | c2=(((*ctx)[i])<<5);
105 | (*ctx)[i] = (((*ctx)[i])>>3)|c1;
106 | c1=c2;
107 | }
108 |
109 | (*ctx)[35] = 0xE0;
110 |
111 | for(i=0; i<4*288; ++i){
112 | trivium_enc(ctx);
113 | }
114 | }
115 |
116 |
117 |
--------------------------------------------------------------------------------
/trivium_cmd/trivium.c:
--------------------------------------------------------------------------------
1 | /* trivium.c */
2 | /*
3 | This file is part of the AVR-Crypto-Lib.
4 | Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | /**
20 | *
21 | * author: Daniel Otte
22 | * email: daniel.otte@rub.de
23 | * license: GPLv3
24 | *
25 | */
26 |
27 |
28 | #include
29 | #include
30 | #include "trivium.h"
31 | //#include
32 |
33 | #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
34 | #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
35 | uint8_t trivium_enc(trivium_ctx_t* ctx){
36 | uint8_t t1,t2,t3,z;
37 |
38 | t1 = G(65) ^ G(92);
39 | t2 = G(161) ^ G(176);
40 | t3 = G(242) ^ G(287);
41 | z = t1^t2^t3;
42 | t1 ^= (G(90) & G(91)) ^ G(170);
43 | t2 ^= (G(174) & G(175)) ^ G(263);
44 | t3 ^= (G(285) & G(286)) ^ G(68);
45 |
46 | /* shift whole state and insert ts later */
47 | uint8_t i,c1=0,c2;
48 | for(i=0; i<36; ++i){
49 | c2=(((*ctx)[i])>>7);
50 | (*ctx)[i] = (((*ctx)[i])<<1)|c1;
51 | c1=c2;
52 | }
53 | /* insert ts */
54 | S(0, t3);
55 | S(93, t1);
56 | S(177, t2);
57 |
58 | return z?0x080:0x00;
59 | }
60 |
61 | uint8_t trivium_getbyte(trivium_ctx_t *ctx){
62 | uint8_t r=0, i=0;
63 | do{
64 | r>>=1;
65 | r |= trivium_enc(ctx);
66 | }while(++i<8);
67 | return r;
68 | }
69 |
70 | #define KEYSIZE_B ((keysize_b+7)/8)
71 | #define IVSIZE_B ((ivsize_b +7)/8)
72 |
73 | //static const uint8_t rev_table[16] PROGMEM = {
74 | static const uint8_t rev_table[16] = {
75 | 0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
76 | 0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
77 | 0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
78 | 0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
79 | };
80 |
81 | void trivium_init(const void* key, uint16_t keysize_b,
82 | const void* iv, uint16_t ivsize_b,
83 | trivium_ctx_t* ctx){
84 | uint16_t i;
85 | uint8_t c1,c2;
86 | uint8_t t1,t2;
87 | memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
88 | c2=0;
89 | c1=KEYSIZE_B;
90 | do{
91 | t1 = ((uint8_t*)key)[--c1];
92 | //t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
93 | t2 = (rev_table[t1&0x0f] << 4)|(rev_table[t1>>4]);
94 | (*ctx)[c2++] = t2;
95 | }while(c1!=0);
96 |
97 | c2=12;
98 | c1=IVSIZE_B;
99 | do{
100 | t1 = ((uint8_t*)iv)[--c1];
101 | //t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
102 | t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
103 | (*ctx)[c2++] = t2;
104 | }while(c1!=0);
105 |
106 | for(i=12+IVSIZE_B; i>10; --i){
107 | c2=(((*ctx)[i])<<5);
108 | (*ctx)[i] = (((*ctx)[i])>>3)|c1;
109 | c1=c2;
110 | }
111 |
112 | (*ctx)[35] = 0xE0;
113 |
114 | for(i=0; i<4*288; ++i){
115 | trivium_enc(ctx);
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/trivium_cmd/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include "trivium.h"
6 |
7 | #define PASSWORD_MAX 30
8 | #define PASSWORD_MIN 10
9 |
10 | int main(int argc, char *argv[]){
11 |
12 |
13 | //printf("%s\n\n",argv[1]);
14 | //1 = key
15 | //2 = iv
16 | //3 = passwd
17 |
18 | // Now comes the trivium part
19 | //char key[] = "0AnL]d`50m";
20 |
21 | int real_length_password=0;
22 | int real_length_key=0;
23 | int real_length_iv=0;
24 | int h=0;
25 |
26 | //hex pw to string
27 | char passwd_hex[128]; //<- argv 3
28 |
29 | //password
30 | char passwd[PASSWORD_MAX]; //-> pw string
31 |
32 | if(strlen(argv[3]) >= 5 + PASSWORD_MIN * 2){ // min 10chr passwd
33 |
34 | if(strlen(argv[3]) <= 5 + PASSWORD_MAX * 2){ //max pw len
35 |
36 | //crypt passwd from client
37 | snprintf ( passwd_hex, 128, "%s", argv[3]); //PASS XXXX [5]
38 | real_length_password=strlen(passwd_hex)/2;
39 |
40 | //char
41 | *passwd = (char*) malloc(strlen(passwd_hex)*2);
42 |
43 |
44 | h=0;
45 | for(h=0;h
3 |
4 | void initLed() {
5 | LEDgreenDDR |= (1 << LEDgreen);
6 | LEDyellowDDR |= (1 << LEDyellow);
7 | }
8 |
9 |
10 | void ledOff() {
11 | LEDgreenPORT &= ~(1 << LEDgreen);
12 | LEDyellowPORT &= ~(1 << LEDyellow);
13 | }
14 |
15 |
16 | void ledOnGreen() {
17 | LEDyellowPORT &= ~(1 << LEDyellow);
18 | LEDgreenPORT |= (1 << LEDgreen);
19 | }
20 |
21 |
22 | void ledOnYellow() {
23 | LEDgreenPORT &= ~(1 << LEDgreen);
24 | LEDyellowPORT |= (1 << LEDyellow);
25 | }
26 |
27 |
28 | void ledToggleGreen() {
29 | LEDyellowPORT &= ~(1 << LEDyellow);
30 | LEDgreenPORT ^= (1 << LEDgreen);
31 | }
32 |
33 |
34 | void ledToggleYellow() {
35 | LEDgreenPORT &= ~(1 << LEDgreen);
36 | LEDyellowPORT ^= (1 << LEDyellow);
37 | }
38 |
39 |
40 | void ledSignal(uint8_t times) {
41 | ledOff();
42 |
43 | while (times > 0) {
44 | ledToggleGreen();
45 | _delay_ms(100);
46 | ledToggleGreen();
47 | ledToggleYellow();
48 | _delay_ms(100);
49 | ledToggleYellow();
50 | times--;
51 | }
52 |
53 | ledOff();
54 | }
55 |
56 |
57 |
58 | uint8_t recvGPIO(uint8_t port){
59 |
60 | if(port) return (PINB & (1<
3 | #include "snesnet.h"
4 |
5 | extern char patterns, patterns_end;
6 | extern char palette, palette_end;
7 | extern char map, map_end;
8 |
9 | extern char snesfont;
10 |
11 | int pos=0;
12 |
13 | char str[26];
14 |
15 | //---------------------------------------------------------------------------------
16 |
17 |
18 | void clean(void){
19 | consoleDrawText(20,14," ");
20 | consoleDrawText(22,19," ");
21 | consoleDrawText(25,21," ");
22 | consoleDrawText(22,23," ");
23 | consoleDrawText(20,21," ");
24 | consoleDrawText(12,20," ");
25 | consoleDrawText(15,20," ");
26 | }
27 |
28 | void addChar(int set_id, int key_id, char chr){
29 | int pad0 = padsCurrent(0);
30 |
31 |
32 |
33 | str[pos]=chr;
34 | str[pos+1]=0x00;
35 | consoleDrawText(3+pos,9,"%c",chr);
36 |
37 |
38 |
39 | if(pos<25) pos++;
40 |
41 |
42 | while((pad0&BIT(set_id))&&(pad0&BIT(key_id))){
43 | scanPads();
44 | pad0 = padsCurrent(0);
45 | }
46 | }
47 |
48 |
49 | void sendString(void){
50 |
51 | consoleDrawText(7, 5, "sending...");
52 |
53 | int length = strlen(str);
54 |
55 | sendByte(length);
56 |
57 | int i;
58 | for(i=0;i
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 |
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 |
20 | #ifdef __SVR4
21 | #include
22 | #endif
23 |
24 |
25 | #include
26 | #include
27 | #include
28 |
29 | #include
30 | #include "trivium.h"
31 | #include "vsnesnet.h"
32 |
33 | #define RCVBUFSIZE 128 /* Size of receive buffer */
34 | //#define MAX_MSG_SIZE 257 // We will never send more than 256 bytes (+\0)
35 |
36 | #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
37 | #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
38 | int sock;
39 | struct sockaddr_in snesnetServAddr;
40 |
41 |
42 | //port2 io
43 | void addIOBit(uint8_t bit)
44 | {
45 | static uint8_t in_byte=0;
46 | static uint8_t in_byte_cnt=0;
47 |
48 | if(bit){ //port1 io
49 | SET_BIT(in_byte,in_byte_cnt);
50 | }else{
51 | CLEAR_BIT(in_byte,in_byte_cnt);
52 | }
53 | /*
54 | #if _DEBUG_MODE
55 | printf(": in_byte_cnt: %d\n",in_byte_cnt);
56 | #endif
57 | */
58 |
59 | if(in_byte_cnt==7){
60 | char sbuff[5];
61 | sbuff[0]='C';
62 | snprintf(sbuff+1, 3,"%02x", (char)in_byte);
63 | //sbuff[1]=(char)in_byte; //bug 0b00000000 -> endline
64 | sbuff[3]='\n';
65 | sbuff[4]='\0';
66 |
67 |
68 |
69 |
70 | //if(login_state==3)
71 | if (send(sock, sbuff, strlen((char *)sbuff), 0) <= 0){
72 | //error
73 | }else{
74 | /*
75 | #if _DEBUG_MODE
76 | printf(": sent\n");
77 | #endif
78 | */
79 | }
80 | in_byte_cnt=0;
81 | }else{
82 |
83 | in_byte_cnt++;
84 | }
85 |
86 | }
87 |
88 |
89 | void *recv_server_data(void* val) {
90 |
91 | uint8_t inc_data[512];
92 |
93 | process_incoming(0xFF, 0xFF);
94 |
95 | usleep(10);
96 | while(1){
97 |
98 |
99 | uint16_t recv_size = recv(sock, inc_data, sizeof(uint8_t)*32, 0);
100 | if (recv_size <= 0)
101 | {
102 | printf("recv() failed or connection closed prematurely");
103 | break;
104 | }
105 |
106 |
107 | int ptr=0;
108 |
109 | //security cutoff
110 | if(recv_size%2 != 0) recv_size--;
111 |
112 | while(ptr> 8);
115 | uint8_t byte1 = inc_data[ptr+1];//(uint8_t)(inc_data[0] & 0x00FF);
116 |
117 | process_incoming(byte0, byte1);
118 |
119 | ptr+=2;
120 | usleep(10);
121 | //uint16_t bt2 = byte0 + (byte1 << 8) ;
122 | //printf("bt hack: [%04x]\n",bt2);
123 | }
124 |
125 |
126 |
127 | }
128 |
129 | return NULL;
130 |
131 |
132 | }
133 |
134 |
135 | void *readout_fifo(void* val) {
136 |
137 | int in_fd;
138 | mkfifo(FIFO_FILE, 0666); //0667 is not working (masked?) :>
139 |
140 | //root process change permission to others +w
141 | int mode=0x00;
142 | mode |= S_IWOTH;
143 | mode |= S_IRUSR;
144 | mode |= S_IWUSR;
145 |
146 | if(chmod(FIFO_FILE, mode))
147 | printf("error: chmod /tmp/snes_wrio.fifo\n");
148 |
149 | char in[RCVBUFSIZE];
150 | in_fd=open(FIFO_FILE, O_RDWR);
151 |
152 |
153 | if (in_fd==-1) {
154 | perror("error: fifo - file open()");
155 | exit(-1);
156 | }
157 |
158 | while(1){
159 |
160 | while (read(in_fd, in, RCVBUFSIZE)>0) {
161 | //printf("%s", in);
162 |
163 | if(in[0]==0x31)
164 | addIOBit(1);
165 | else
166 | addIOBit(0);
167 |
168 | }
169 |
170 |
171 |
172 |
173 | /*
174 | //incomming controller data
175 | if(!_SNESnet_master){ // i'm slave
176 | socklen_t len;
177 | unsigned char remote_mesg[8];
178 |
179 | len = sizeof(_SNESnet_client_addr);
180 | recvfrom(_SNESnet_socket_tcp, remote_mesg,32,0,(struct sockaddr *)&_SNESnet_server_addr,&len);
181 |
182 | unsigned char b0[2];
183 | unsigned char b1[2];
184 | b0[0] = remote_mesg[0];
185 | b0[1] = remote_mesg[1];
186 | b1[0] = remote_mesg[2];
187 | b1[1] = remote_mesg[3];
188 |
189 | unsigned char byte0 = strtol(b0, NULL, 16);
190 | unsigned char byte1 = strtol(b1, NULL, 16);
191 |
192 | process_incoming(byte0, byte1);
193 |
194 | //uint16_t bt = byte0 + (byte1 << 8) ;
195 | //printf("bt hack: [%d]\n",bt);
196 | }
197 | */
198 | }
199 |
200 | return NULL;
201 | }
202 |
203 |
204 |
205 | void stop_snesnet()
206 | {
207 | close(sock);
208 | }
209 |
210 | int init_login(char *confFile)
211 | {
212 |
213 | const char *loginserver;
214 | const char *username;
215 | const char *password;
216 | const char *key;
217 | debug=0;
218 | /*
219 | int *server_port;
220 | int *tcp_min_port;
221 | int *tcp_max_port;
222 | */
223 |
224 |
225 | // Initialise and read configuration file.
226 | config_t conf;
227 | //config_setting_t *setting;
228 | config_init(&conf);
229 |
230 |
231 | if (! config_read_file(&conf, confFile)) {
232 | config_destroy(&conf);
233 | printf("... %s: wrong file format or file does not exist.\n", confFile);
234 | return -1;
235 | }
236 | if ( (! config_lookup_string(&conf, "loginserver", &loginserver) ) || (strlen(loginserver) == 0) ) {
237 | printf("... %s: loginserver is not set\n", confFile);
238 | return -1;
239 | }
240 | if ( (! config_lookup_string(&conf, "username", &username) ) || (strlen(username) == 0) ) {
241 | printf("... %s: username is not set\n", confFile);
242 | return -1;
243 | }
244 | if ( (! config_lookup_string(&conf, "password", &password) ) || (strlen(password) == 0) ) {
245 | printf("... %s: password is not set\n", confFile);
246 | return -1;
247 | }
248 | if ( (! config_lookup_string(&conf, "key", &key) ) || (strlen(key) == 0) ) {
249 | printf("... %s: key is not set\n", confFile);
250 | return -1;
251 | }
252 | if ( (! config_lookup_string(&conf, "js_dev", &js_dev) ) || (strlen(js_dev) == 0) ) {
253 | printf("... %s: js_dev is not set\n", confFile);
254 | return -1;
255 | }
256 | if ( (! config_lookup_int(&conf, "server_port", &_SNESnet_server_port) ) || _SNESnet_server_port == 0) {
257 | printf("... %s: server_port is not set\n", confFile);
258 | return -1;
259 | }
260 | if ( ! config_lookup_int(&conf, "js_axis_type", &js_axis_type)) {
261 | printf("... %s: js_axis_type is not set\n", confFile);
262 | return -1;
263 | }
264 | if ( ! config_lookup_int(&conf, "js_button_type", &js_button_type)) {
265 | printf("... %s: js_button_type is not set\n", confFile);
266 | return -1;
267 | }
268 | if ( ! config_lookup_int(&conf, "js_axis_up_val", &js_axis_up_val)) {
269 | printf("... %s: js_axis_up_val is not set\n", confFile);
270 | return -1;
271 | }
272 | if ( ! config_lookup_int(&conf, "js_axis_down_val", &js_axis_down_val)) {
273 | printf("... %s: js_axis_down_val is not set\n", confFile);
274 | return -1;
275 | }
276 | if ( ! config_lookup_int(&conf, "js_axis_left_val", &js_axis_left_val)) {
277 | printf("... %s: js_axis_left_val is not set\n", confFile);
278 | return -1;
279 | }
280 | if ( ! config_lookup_int(&conf, "js_axis_right_val", &js_axis_right_val)) {
281 | printf("... %s: js_axis_right_val is not set\n", confFile);
282 | return -1;
283 | }
284 | if ( ! config_lookup_int(&conf, "js_axis_y_nr", &js_axis_y_nr)) {
285 | printf("... %s: js_axis_y_nr is not set\n", confFile);
286 | return -1;
287 | }
288 | if ( ! config_lookup_int(&conf, "js_axis_x_nr", &js_axis_x_nr)) {
289 | printf("... %s: js_axis_x_nr is not set\n", confFile);
290 | return -1;
291 | }
292 | if ( ! config_lookup_int(&conf, "js_button_a_nr", &js_button_a_nr)) {
293 | printf("... %s: js_button_a_nr is not set\n", confFile);
294 | return -1;
295 | }
296 | if ( ! config_lookup_int(&conf, "js_button_b_nr", &js_button_b_nr)) {
297 | printf("... %s: js_button_b_nr is not set\n", confFile);
298 | return -1;
299 | }
300 | if ( ! config_lookup_int(&conf, "js_button_x_nr", &js_button_x_nr)) {
301 | printf("... %s: js_button_x_nr is not set\n", confFile);
302 | return -1;
303 | }
304 | if ( ! config_lookup_int(&conf, "js_button_y_nr", &js_button_y_nr)) {
305 | printf("... %s: js_button_y_nr is not set\n", confFile);
306 | return -1;
307 | }
308 | if ( ! config_lookup_int(&conf, "js_button_l_nr", &js_button_l_nr)) {
309 | printf("... %s: js_button_l_nr is not set\n", confFile);
310 | return -1;
311 | }
312 | if ( ! config_lookup_int(&conf, "js_button_r_nr", &js_button_r_nr)) {
313 | printf("... %s: js_button_r_nr is not set\n", confFile);
314 | return -1;
315 | }
316 | if ( ! config_lookup_int(&conf, "js_button_st_nr", &js_button_st_nr)) {
317 | printf("... %s: js_button_st_nr is not set\n", confFile);
318 | return -1;
319 | }
320 | if ( ! config_lookup_int(&conf, "js_button_se_nr", &js_button_se_nr)) {
321 | printf("... %s: js_button_se_nr is not set\n", confFile);
322 | return -1;
323 | }
324 | if ( ! config_lookup_int(&conf, "disable_p1", &disable_p1)) {
325 | printf("... %s: disable_p1 is not set\n", confFile);
326 | return -1;
327 | }
328 | if ( ! config_lookup_int(&conf, "select_x", &select_x)) {
329 | printf("... %s: select_x is not set\n", confFile);
330 | return -1;
331 | }
332 | if ( ! config_lookup_int(&conf, "mapping_mode", &mapping_mode)) {
333 | printf("... %s: mapping_mode is not set\n", confFile);
334 | return -1;
335 | }
336 | if (! config_lookup_int(&conf, "debug", &debug) ) {
337 | printf("... %s: debug is not set\n", confFile);
338 | printf("... default value set\n");
339 | }
340 |
341 |
342 |
343 | _SNESnet_master=0;
344 | snprintf(_SNESnet_server, 128, loginserver);
345 | snprintf(_SNESnet_username, 128, username);
346 | snprintf(_SNESnet_password, 128, password);
347 | snprintf(_SNESnet_key, 128, key);
348 |
349 |
350 | // struct sockaddr_in snesnetServAddr; /* sockaddr_in for snesnet server address */
351 | // unsigned int messageStringLen; /* Length of string to echo */
352 | int totalBytesRcvd, bytesRcvd; // (Total) Amount of data received from the server
353 | char messageBuffer[RCVBUFSIZE]; /* Buffer for message string */
354 | char sendMessage[MAX_MSG_SIZE]; // Buffer for outgoing messages
355 | pthread_t t_fifo_inc;
356 | pthread_t t_server_inc;
357 |
358 |
359 | printf("%s\n","Initialize _SNESnet...");
360 | if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
361 | {
362 | printf("Could notcreate TCP/IP Socket!");
363 | return FALSE;
364 | }
365 |
366 | memset(&snesnetServAddr,0,sizeof(struct sockaddr_in));
367 | snesnetServAddr.sin_family = AF_INET; /* Internet address family */
368 | snesnetServAddr.sin_addr.s_addr = inet_addr(_SNESnet_server); /* Server IP address */
369 | snesnetServAddr.sin_port = htons(_SNESnet_server_port); /* Server port */
370 |
371 | if(debug){
372 | fprintf(stdout, "-> SNESnet key [%s]\n", _SNESnet_key);
373 | fprintf(stdout, "-> SNESnet username [%s]\n", _SNESnet_username);
374 | fprintf(stdout, "-> SNESnet password [%s]\n", _SNESnet_password);
375 | fprintf(stdout, "-> SNESnet server [%s]\n", _SNESnet_server);
376 | fprintf(stdout, "-> SNESnet server_port [%d]\n", _SNESnet_server_port);
377 | }
378 |
379 | if (connect(sock, (struct sockaddr *) &snesnetServAddr, sizeof(snesnetServAddr)) < 0)
380 | {
381 | printf("Connectection to SNESnet server failed\n");
382 | return FALSE;
383 | }
384 |
385 | printf("Connection to _SNESnet server established\n");
386 |
387 |
388 | totalBytesRcvd = 0;
389 |
390 | //stage two USER LOGIN
391 | snprintf(sendMessage,MAX_MSG_SIZE,"U%s\n",_SNESnet_username);
392 |
393 | printf("Try to login as %s\n",sendMessage);
394 |
395 | if (send(sock, sendMessage, strlen(sendMessage), 0) != strlen(sendMessage))
396 | {
397 | printf("Could not send user request to the server...\n");
398 | return FALSE;
399 | }
400 |
401 | totalBytesRcvd = 0;
402 | do
403 | {
404 | if ((bytesRcvd = recv(sock, messageBuffer, RCVBUFSIZE - 1, 0)) <= 0)
405 | {
406 | printf("recv() failed or connection closed prematurely");
407 | return FALSE;
408 | }
409 |
410 | totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
411 | messageBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
412 | }while (messageBuffer[totalBytesRcvd-1] != '\n');
413 |
414 | if (messageBuffer[0]!='I' && messageBuffer[1]!='V' && messageBuffer[2]!=' ')
415 | {
416 | printf("IV request failed");
417 | return FALSE;
418 | }
419 |
420 | // Now comes the trivium part
421 |
422 | //password
423 | char passwd[MAX_PASSWORD_LENGTH];
424 |
425 | //passwd="pass1";
426 | snprintf(passwd,MAX_PASSWORD_LENGTH,_SNESnet_password);
427 |
428 | trivium_ctx_t ctx;
429 |
430 | trivium_init(_SNESnet_key, 80, messageBuffer+3, 80, &ctx);
431 |
432 | trivium_enc(&ctx);
433 |
434 | int g=0;
435 | unsigned char passwd_[strlen(passwd)];
436 | for(g=0; g>7);
528 | (*ctx)[i] = (((*ctx)[i])<<1)|c1;
529 | c1=c2;
530 | }
531 | /* insert ts */
532 | S(0, t3);
533 | S(93, t1);
534 | S(177, t2);
535 |
536 | return z?0x080:0x00;
537 | }
538 |
539 | uint8_t trivium_getbyte(trivium_ctx_t *ctx){
540 | uint8_t r=0, i=0;
541 | do{
542 | r>>=1;
543 | r |= trivium_enc(ctx);
544 | }while(++i<8);
545 | return r;
546 | }
547 |
548 | #define KEYSIZE_B ((keysize_b+7)/8)
549 | #define IVSIZE_B ((ivsize_b +7)/8)
550 |
551 | //static const uint8_t rev_table[16] PROGMEM = {
552 | static const uint8_t rev_table[16] = {
553 | 0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
554 | 0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
555 | 0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
556 | 0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
557 | };
558 |
559 | void trivium_init(const void* key, uint16_t keysize_b,
560 | const void* iv, uint16_t ivsize_b,
561 | trivium_ctx_t* ctx){
562 | uint16_t i;
563 | uint8_t c1,c2;
564 | uint8_t t1,t2;
565 | memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
566 | c2=0;
567 | c1=KEYSIZE_B;
568 | do{
569 | t1 = ((uint8_t*)key)[--c1];
570 | t2 = (rev_table[t1&0x0f] << 4)|(rev_table[t1>>4]);
571 | (*ctx)[c2++] = t2;
572 | }while(c1!=0);
573 |
574 | c2=12;
575 | c1=IVSIZE_B;
576 | do{
577 | t1 = ((uint8_t*)iv)[--c1];
578 | t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
579 | (*ctx)[c2++] = t2;
580 | }while(c1!=0);
581 |
582 | for(i=12+IVSIZE_B; i>10; --i){
583 | c2=(((*ctx)[i])<<5);
584 | (*ctx)[i] = (((*ctx)[i])>>3)|c1;
585 | c1=c2;
586 | }
587 |
588 | (*ctx)[35] = 0xE0;
589 |
590 | for(i=0; i<4*288; ++i){
591 | trivium_enc(ctx);
592 | }
593 | }
594 |
595 |
596 |
--------------------------------------------------------------------------------
/vsnesnet/vsnesnet.c:
--------------------------------------------------------------------------------
1 | /*
2 | * virtual snesnet userspace device v0.1 beta
3 | * by saturnu 2015
4 | *
5 | * If uhid is not available as /dev/uhid, then you can pass a different path as
6 | * first argument.
7 | * If is not installed in /usr, then compile this with:
8 | * gcc -o ./vsnesnet -Wall -I./include ./vsnesnet.c
9 | * And ignore the warning about kernel headers. However, it is recommended to
10 | * use the installed uhid.h if available.
11 | */
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 |
24 | #include
25 | #include
26 | #include
27 |
28 | #include
29 | #include
30 | #include
31 |
32 | #include "axbtnmap.h"
33 | #include "vsnesnet.h"
34 |
35 | char *confFile = "vsnesnet.conf";
36 |
37 |
38 | #define NAME_LENGTH 128
39 |
40 | int read_joystick(int fd);
41 |
42 | char *axis_names[ABS_MAX + 1] = {
43 | "X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder",
44 | "Wheel", "Gas", "Brake", "?", "?", "?", "?", "?",
45 | "Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat3Y",
46 | "?", "?", "?", "?", "?", "?", "?",
47 | };
48 |
49 | char *button_names[KEY_MAX - BTN_MISC + 1] = {
50 | "Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7", "Btn8", "Btn9", "?", "?", "?", "?", "?", "?",
51 | "LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn", "TaskBtn", "?", "?", "?", "?", "?", "?", "?", "?",
52 | "Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn", "BaseBtn", "BaseBtn2", "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6", "BtnDead",
53 | "BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR", "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode", "BtnThumbL", "BtnThumbR", "?",
54 | "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
55 | "WheelBtn", "Gear up",
56 | };
57 |
58 |
59 | //Game Pad usb-desc
60 | static unsigned char rdesc[] = {
61 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
62 | 0x09, 0x05, // USAGE (Game Pad)
63 | 0xa1, 0x01, // COLLECTION (Application)
64 | 0xa1, 0x00, // COLLECTION (Physical)
65 | 0x05, 0x09, // USAGE_PAGE (Button)
66 | 0x19, 0x01, // USAGE_MINIMUM (Button 1)
67 | 0x29, 0x18, // USAGE_MAXIMUM (Button 24)
68 | 0x15, 0x00, // LOGICAL_MINIMUM (0)
69 | 0x25, 0x01, // LOGICAL_MAXIMUM (1)
70 | 0x95, 0x18, // REPORT_COUNT (24)
71 | 0x75, 0x01, // REPORT_SIZE (1)
72 | 0x81, 0x02, // INPUT (Data,Var,Abs)
73 | 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
74 | 0x09, 0x30, // USAGE (X)
75 | 0x09, 0x31, // USAGE (Y)
76 | 0x09, 0x32, // USAGE (Z)
77 | 0x09, 0x33, // USAGE (Rx)
78 | 0x15, 0x81, // LOGICAL_MINIMUM (-127)
79 | 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
80 | 0x75, 0x08, // REPORT_SIZE (8)
81 | 0x95, 0x04, // REPORT_COUNT (4)
82 | 0x81, 0x02, // INPUT (Data,Var,Abs)
83 | 0xc0, // END_COLLECTION
84 | 0xc0 // END_COLLECTION
85 | };
86 |
87 |
88 | static unsigned char b0;
89 | static unsigned char b1;
90 |
91 |
92 | static bool btn1_down;
93 | static bool btn2_down;
94 | static bool btn3_down;
95 | static bool btn4_down;
96 | static bool btn5_down;
97 | static bool btn6_down;
98 | static bool btn7_down;
99 | static bool btn8_down;
100 | static bool btn9_down;
101 | static bool btn10_down;
102 | static bool btn11_down;
103 | static bool btn12_down;
104 | static bool btn13_down;
105 | static bool btn14_down;
106 | static bool btn15_down;
107 | static bool btn16_down;
108 | static bool btn17_down;
109 | static bool btn18_down;
110 | static bool btn19_down;
111 | static bool btn20_down;
112 | static bool btn21_down;
113 | static bool btn22_down;
114 | static bool btn23_down;
115 | static bool btn24_down;
116 |
117 |
118 |
119 | static int uhid_write(int fd, const struct uhid_event *ev)
120 | {
121 | ssize_t ret;
122 |
123 | ret = write(fd, ev, sizeof(*ev));
124 | if (ret < 0) {
125 | fprintf(stderr, "Cannot write to uhid: %m\n");
126 | return -errno;
127 | } else if (ret != sizeof(*ev)) {
128 | fprintf(stderr, "Wrong size written to uhid: %ld != %lu\n",
129 | ret, sizeof(ev));
130 | return -EFAULT;
131 | } else {
132 | return 0;
133 | }
134 | }
135 |
136 | static int create(int fd)
137 | {
138 | struct uhid_event ev;
139 |
140 | memset(&ev, 0, sizeof(ev));
141 | ev.type = UHID_CREATE;
142 | strcpy((char*)ev.u.create.name, "vsnesnet-gamepad");
143 | ev.u.create.rd_data = rdesc;
144 | ev.u.create.rd_size = sizeof(rdesc);
145 | ev.u.create.bus = BUS_USB;
146 | ev.u.create.vendor = 0x057E; //Nintendo
147 | ev.u.create.product = 0x0815;
148 | ev.u.create.version = 0;
149 | ev.u.create.country = 0;
150 |
151 | return uhid_write(fd, &ev);
152 | }
153 |
154 | static void destroy(int fd)
155 | {
156 | struct uhid_event ev;
157 |
158 | memset(&ev, 0, sizeof(ev));
159 | ev.type = UHID_DESTROY;
160 |
161 | uhid_write(fd, &ev);
162 | }
163 |
164 |
165 | static int send_event(int fd)
166 | {
167 | struct uhid_event ev;
168 |
169 | memset(&ev, 0, sizeof(ev));
170 | ev.type = UHID_INPUT;
171 | ev.u.input.size = 24;
172 |
173 | if(!disable_p1){
174 |
175 | //player 1
176 | if (btn1_down)
177 | ev.u.input.data[0] |= 1 << 0;
178 | if (btn2_down)
179 | ev.u.input.data[0] |= 1 << 1;
180 | if (btn3_down)
181 | ev.u.input.data[0] |= 1 << 2;
182 | if (btn4_down)
183 | ev.u.input.data[0] |= 1 << 3;
184 | if (btn5_down)
185 | ev.u.input.data[0] |= 1 << 4;
186 | if (btn6_down)
187 | ev.u.input.data[0] |= 1 << 5;
188 | if (btn7_down)
189 | ev.u.input.data[0] |= 1 << 6;
190 | if (btn8_down)
191 | ev.u.input.data[0] |= 1 << 7;
192 | if (btn9_down)
193 | ev.u.input.data[1] |= 1 << 0;
194 | if (btn10_down)
195 | ev.u.input.data[1] |= 1 << 1;
196 | if (btn11_down)
197 | ev.u.input.data[1] |= 1 << 2;
198 | if (btn12_down)
199 | ev.u.input.data[1] |= 1 << 3;
200 | }
201 |
202 | //player 2
203 | if (btn13_down)
204 | ev.u.input.data[1] |= 1 << 4;
205 | if (btn14_down)
206 | ev.u.input.data[1] |= 1 << 5;
207 | if (btn15_down)
208 | ev.u.input.data[1] |= 1 << 6;
209 | if (btn16_down)
210 | ev.u.input.data[1] |= 1 << 7;
211 | if (btn17_down)
212 | ev.u.input.data[2] |= 1 << 0;
213 | if (btn18_down)
214 | ev.u.input.data[2] |= 1 << 1;
215 | if (btn19_down)
216 | ev.u.input.data[2] |= 1 << 2;
217 | if (btn20_down)
218 | ev.u.input.data[2] |= 1 << 3;
219 | if (btn21_down)
220 | ev.u.input.data[2] |= 1 << 4;
221 | if (btn22_down)
222 | ev.u.input.data[2] |= 1 << 5;
223 | if (btn23_down)
224 | ev.u.input.data[2] |= 1 << 6;
225 | if (btn24_down)
226 | ev.u.input.data[2] |= 1 << 7;
227 |
228 | return uhid_write(fd, &ev);
229 | }
230 |
231 |
232 |
233 | static int button_mapping(int fd)
234 | {
235 | char buf[128];
236 | ssize_t ret, i;
237 |
238 | ret = read(STDIN_FILENO, buf, sizeof(buf));
239 | if (ret == 0) {
240 | fprintf(stderr, "Read HUP on stdin\n");
241 | return -EFAULT;
242 | } else if (ret < 0) {
243 | fprintf(stderr, "Cannot read stdin: %m\n");
244 | return -errno;
245 | }
246 |
247 | int s=3;
248 |
249 | for (i = 0; i < ret; ++i) {
250 | switch (buf[i]) {
251 | case 'r':
252 | sleep(s);
253 | btn13_down = !btn13_down;
254 | send_event(fd);
255 | sleep(1);
256 | btn13_down = !btn13_down;
257 | send_event(fd);
258 |
259 | break;
260 | case 'l':
261 | sleep(s);
262 | btn14_down = !btn14_down;
263 | send_event(fd);
264 | sleep(1);
265 | btn14_down = !btn14_down;
266 | send_event(fd);
267 | break;
268 | case 'd':
269 | sleep(s);
270 | btn15_down = !btn15_down;
271 | send_event(fd);
272 | sleep(1);
273 | btn15_down = !btn15_down;
274 | send_event(fd);
275 | break;
276 | case 'u':
277 | sleep(s);
278 | btn16_down = !btn16_down;
279 | send_event(fd);
280 | sleep(1);
281 | btn16_down = !btn16_down;
282 | send_event(fd);
283 | break;
284 | case 's':
285 | sleep(s);
286 | btn17_down = !btn17_down;
287 | send_event(fd);
288 | sleep(1);
289 | btn17_down = !btn17_down;
290 | send_event(fd);
291 | break;
292 | case 'e':
293 | sleep(s);
294 | btn18_down = !btn18_down;
295 | send_event(fd);
296 | sleep(1);
297 | btn18_down = !btn18_down;
298 | send_event(fd);
299 | break;
300 | case 'y':
301 | sleep(s);
302 | btn19_down = !btn19_down;
303 | send_event(fd);
304 | sleep(1);
305 | btn19_down = !btn19_down;
306 | send_event(fd);
307 | break;
308 | case 'b':
309 | sleep(s);
310 | btn20_down = !btn20_down;
311 | send_event(fd);
312 | sleep(1);
313 | btn20_down = !btn20_down;
314 | send_event(fd);
315 | break;
316 | case 'a':
317 | sleep(s);
318 | btn21_down = !btn21_down;
319 | send_event(fd);
320 | sleep(1);
321 | btn21_down = !btn21_down;
322 | send_event(fd);
323 | break;
324 | case 'x':
325 | sleep(s);
326 | btn22_down = !btn22_down;
327 | send_event(fd);
328 | sleep(1);
329 | btn22_down = !btn22_down;
330 | send_event(fd);
331 | break;
332 | case '1':
333 | sleep(s);
334 | btn23_down = !btn23_down;
335 | send_event(fd);
336 | sleep(1);
337 | btn23_down = !btn23_down;
338 | send_event(fd);
339 | break;
340 | case '2':
341 | sleep(s);
342 | btn24_down = !btn24_down;
343 | send_event(fd);
344 | sleep(1);
345 | btn24_down = !btn24_down;
346 | send_event(fd);
347 | break;
348 |
349 |
350 |
351 |
352 | case 'q':
353 | return -ECANCELED;
354 | default:
355 | fprintf(stderr, "Invalid input: %c\n", buf[i]);
356 | }
357 | }
358 |
359 | return 0;
360 | }
361 |
362 |
363 | int main(int argc, char **argv)
364 | {
365 | int fd;
366 | const char *path = "/dev/uhid";
367 | struct pollfd pfds[2];
368 | int ret;
369 | struct termios state;
370 |
371 | ret = tcgetattr(STDIN_FILENO, &state);
372 | if (ret) {
373 | fprintf(stderr, "Cannot get tty state\n");
374 | } else {
375 | state.c_lflag &= ~ICANON;
376 | state.c_cc[VMIN] = 1;
377 | ret = tcsetattr(STDIN_FILENO, TCSANOW, &state);
378 | if (ret)
379 | fprintf(stderr, "Cannot set tty state\n");
380 | }
381 |
382 | if (argc >= 2) {
383 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
384 | puts("");
385 | fprintf(stderr, "Usage: %s [%s]\n", argv[0], path);
386 |
387 | return EXIT_SUCCESS;
388 | } else {
389 | path = argv[1];
390 | }
391 | }
392 |
393 | if(debug)
394 | fprintf(stderr, "Open uhid-cdev %s\n", path);
395 |
396 | fd = open(path, O_RDWR | O_CLOEXEC);
397 | if (fd < 0) {
398 | fprintf(stderr, "Cannot open uhid-cdev %s: %m\n", path);
399 | return EXIT_FAILURE;
400 | }
401 | fdx=fd;
402 |
403 | if(debug)
404 | fprintf(stderr, "Create uhid device\n");
405 |
406 | ret = create(fd);
407 | if (ret) {
408 | close(fd);
409 | return EXIT_FAILURE;
410 | }
411 |
412 | init_login(confFile);
413 |
414 |
415 | //start joystick reading
416 | if(!mapping_mode)
417 | read_joystick(fd);
418 | else{
419 |
420 |
421 |
422 |
423 | pfds[0].fd = STDIN_FILENO;
424 | pfds[0].events = POLLIN;
425 | pfds[1].fd = fd;
426 | pfds[1].events = POLLIN;
427 |
428 | fprintf(stderr, "mapping mode:\nbutton pressed 3sec after input\ndpad:abxy buttons:udlr 1:L 2:R s:start e:select\nPress 'q' to quit...\n");
429 | while (1) {
430 | ret = poll(pfds, 2, -1);
431 | if (ret < 0) {
432 | fprintf(stderr, "Cannot poll for fds: %m\n");
433 | break;
434 | }
435 | if (pfds[0].revents & POLLHUP) {
436 | fprintf(stderr, "Received HUP on stdin\n");
437 | break;
438 | }
439 | if (pfds[1].revents & POLLHUP) {
440 | fprintf(stderr, "Received HUP on uhid-cdev\n");
441 | break;
442 | }
443 |
444 | if (pfds[0].revents & POLLIN) {
445 | ret = button_mapping(fd);
446 | if (ret)
447 | break;
448 | }
449 | }
450 | }
451 |
452 |
453 | //never reached ^^
454 | fprintf(stderr, "\nDestroy uhid device\n");
455 | destroy(fd);
456 | return EXIT_SUCCESS;
457 | }
458 |
459 |
460 | int read_joystick (int fd0)
461 | {
462 |
463 | int fd, i;
464 | unsigned char axes = 2;
465 | unsigned char buttons = 2;
466 | int version = 0x000800;
467 | char name[NAME_LENGTH] = "Unknown";
468 | uint16_t btnmap[BTNMAP_SIZE];
469 | uint8_t axmap[AXMAP_SIZE];
470 | int btnmapok = 1;
471 |
472 |
473 | if ((fd = open(js_dev, O_RDONLY)) < 0) {
474 | perror("error: could not open device");
475 | return 1;
476 | }
477 |
478 | ioctl(fd, JSIOCGVERSION, &version);
479 | ioctl(fd, JSIOCGAXES, &axes);
480 | ioctl(fd, JSIOCGBUTTONS, &buttons);
481 | ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);
482 |
483 | getaxmap(fd, axmap);
484 | getbtnmap(fd, btnmap);
485 |
486 | if(debug)
487 | printf("Driver version is %d.%d.%d.\n", version >> 16, (version >> 8) & 0xff, version & 0xff);
488 |
489 | /* Determine whether the button map is usable. */
490 | for (i = 0; btnmapok && i < buttons; i++) {
491 | if (btnmap[i] < BTN_MISC || btnmap[i] > KEY_MAX) {
492 | btnmapok = 0;
493 | break;
494 | }
495 | }
496 | if (!btnmapok) {
497 | /* btnmap out of range for names. Don't print any. */
498 | puts("vsnesnet is not fully compatible with your kernel. Unable to retrieve button map!");
499 | printf("Joystick (%s) has %d axes ", name, axes);
500 | printf("and %d buttons.\n", buttons);
501 | } else {
502 | printf("Joystick (%s) has %d axes (", name, axes);
503 | for (i = 0; i < axes; i++)
504 | printf("%s%s", i > 0 ? ", " : "", axis_names[axmap[i]]);
505 | puts(")");
506 |
507 | printf("and %d buttons (", buttons);
508 | for (i = 0; i < buttons; i++) {
509 | printf("%s%s", i > 0 ? ", " : "", button_names[btnmap[i] - BTN_MISC]);
510 | }
511 | puts(").");
512 | }
513 |
514 | printf("(interrupt to exit)\n");
515 |
516 | struct js_event js;
517 |
518 | while (1) {
519 | if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
520 | perror("\nvsnesnet: error reading");
521 | return 1;
522 | }
523 |
524 | if(debug){
525 | if(js.type!=129 && js.type!=130) //init values?
526 | printf("Event: type %d, time %d, number %d, value %d\n", js.type, js.time, js.number, js.value);
527 | }
528 |
529 | //AXIS
530 | if(js.type==js_axis_type || js.type==js_button_type){
531 |
532 | if(js.type==js_axis_type){
533 |
534 | if(js.number==js_axis_y_nr){
535 |
536 | if(js.value==js_axis_up_val) {
537 | btn1_down=1; b1 |= 1 << 3;
538 | }
539 |
540 | if(js.value==js_axis_down_val) {
541 | btn2_down=1; b1 |= 1 << 2;
542 | }
543 |
544 | if(js.value==0) {
545 | btn1_down = 0;
546 | btn2_down = 0;
547 | b1 &= ~(1 << 3);
548 | b1 &= ~(1 << 2);
549 | }
550 |
551 |
552 | }
553 | else if(js.number==js_axis_x_nr){
554 |
555 | if(js.value==js_axis_left_val) {
556 | btn3_down=1; b1 |= 1 << 1;
557 | }
558 |
559 | if(js.value==js_axis_right_val) {
560 | btn4_down=1; b1 |= 1 << 0;
561 | }
562 |
563 | if(js.value==0) {
564 | btn3_down=0;
565 | btn4_down=0;
566 | b1 &= ~(1 << 1);
567 | b1 &= ~(1 << 0);
568 | }
569 | }
570 | }
571 | //BUTTONS
572 | else if(js.type==js_button_type){
573 |
574 | if(js.number==js_button_a_nr ) { btn5_down=js.value; if(js.value) b0 |= 1 << 7; else b0 &= ~(1 << 7); }
575 | if(js.number==js_button_b_nr ) { btn6_down=js.value; if(js.value) b1 |= 1 << 7; else b1 &= ~(1 << 7); }
576 | if(js.number==js_button_x_nr ) { btn7_down=js.value; if(js.value) b0 |= 1 << 6; else b0 &= ~(1 << 6); }
577 | if(js.number==js_button_y_nr ) { btn8_down=js.value; if(js.value) b1 |= 1 << 6; else b1 &= ~(1 << 6); }
578 | if(js.number==js_button_l_nr ) { btn9_down=js.value; if(js.value) b0 |= 1 << 5; else b0 &= ~(1 << 5); }
579 | if(js.number==js_button_r_nr ) { btn10_down=js.value; if(js.value) b0 |= 1 << 4; else b0 &= ~(1 << 4); }
580 | if(js.number==js_button_st_nr ) { btn11_down=js.value; if(js.value) b1 |= 1 << 4; else b1 &= ~(1 << 4); }
581 | if(js.number==js_button_se_nr ) { btn12_down=js.value; if(js.value) b1 |= 1 << 5; else b1 &= ~(1 << 5); }
582 | }
583 |
584 | //exit combination
585 | if(select_x)
586 | if(btn7_down && btn12_down){
587 | return 0;
588 | fflush(stdout);
589 | }
590 |
591 | if(!disable_p1)
592 | send_event(fd0); //send button to virtual game pad device
593 |
594 | char buffer[6];
595 |
596 | if(ready){
597 |
598 | if(debug){
599 | //printf("send [%02X][%02X]\n",b1,b0);
600 | printf("send b1 %s\n", byte_to_binary(b1));
601 | printf("send b0 %s\n", byte_to_binary(b0));
602 | }
603 |
604 | snprintf(buffer,12,"%02X%02X\n",b1,b0);
605 | //send button to snesnet opponent
606 | sendto(_SNESnet_socket_tcp,buffer,strlen(buffer),0,
607 | (struct sockaddr *)&_SNESnet_server_addr,sizeof(struct sockaddr_in) );
608 | //only slave mode
609 | //TODO master mode
610 |
611 | }//end connection ok
612 |
613 | }//end axis or button
614 |
615 |
616 | fflush(stdout);
617 | } //break while to exit
618 | return -1;
619 | }
620 |
621 | const char *byte_to_binary(int x)
622 | {
623 | static char b[9];
624 | b[0] = '\0';
625 |
626 | int z;
627 | for (z = 128; z > 0; z >>= 1)
628 | {
629 | strcat(b, ((x & z) == z) ? "1" : "0");
630 | }
631 |
632 | return b;
633 | }
634 |
635 |
636 | //called by incoming thread
637 | int process_incoming(uint8_t a, uint8_t b){
638 |
639 | //process network data
640 | if(debug){
641 | //printf("recv from echo server: %02x %02x\n", a, b);
642 | printf("recv b1 %s\n", byte_to_binary(a));
643 | printf("recv b0 %s\n", byte_to_binary(b));
644 | }
645 |
646 | //a b
647 | //11110000 00000000
648 |
649 | //16 bit snes controller
650 | //111111 bitpos (msb <- lsb)
651 | //54321098 76543210
652 | //76543210 76543210
653 | //00000000 00000000 16bit low(0) = enable
654 | //zzzzRLXA RLDUSSBY mapping
655 | //4321 ieoptl
656 |
657 | // Software reset: L + R + Select + Start.
658 | // 0b 11110011 11110011
659 |
660 |
661 |
662 |
663 | btn13_down = !(b & (1 << 0)); //B 12
664 | btn14_down = !(b & (1 << 1)); //Y 13
665 | btn15_down = !(b & (1 << 2)); //Sl 14
666 | btn16_down = !(b & (1 << 3)); //St 15
667 | btn17_down = !(b & (1 << 4)); //Up 16
668 | btn18_down = !(b & (1 << 5)); //Do 17
669 | btn19_down = !(b & (1 << 6)); //Le 18
670 | btn20_down = !(b & (1 << 7)); //Ri 19
671 |
672 | btn21_down = !(a & (1 << 0)); //A 20
673 | btn22_down = !(a & (1 << 1)); //X 21
674 | btn23_down = !(a & (1 << 2)); //L 22
675 | btn24_down = !(a & (1 << 3)); //R 23
676 |
677 | send_event(fdx);
678 |
679 | return 0;
680 | }
681 |
682 |
683 |
684 |
685 |
686 |
--------------------------------------------------------------------------------
/snesnet_server/SnesNETsrv.java:
--------------------------------------------------------------------------------
1 | import java.io.BufferedReader;
2 | import java.io.DataOutputStream;
3 | import java.io.IOException;
4 | import java.io.InputStreamReader;
5 | import java.io.OutputStream;
6 | import java.io.PrintWriter;
7 | import java.net.ServerSocket;
8 | import java.net.Socket;
9 | import java.util.LinkedList;
10 | import java.util.Random;
11 |
12 | import mod.DemoModule;
13 | import mod.DummyModule;
14 | import mod.TestModule;
15 | import net.MySQLAccess;
16 | import net.Session;
17 |
18 | public class SnesNETsrv {
19 |
20 | // the socket used by the server
21 | private ServerSocket serverSocket;
22 | private LinkedList session_list;
23 | final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
24 |
25 | //debug;
26 | long startTime=0;
27 | long pkt_cnt=0;
28 |
29 |
30 | // server constructor
31 | SnesNETsrv (int port) {
32 |
33 | session_list = new LinkedList();
34 | // modulcontainerlist = new LinkedList();
35 | /* create socket server and wait for connection requests */
36 | try
37 | {
38 | serverSocket = new ServerSocket(port);
39 | System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
40 |
41 | while(true)
42 | {
43 | Socket socket = serverSocket.accept(); // accept connection
44 | // System.out.println("New client asked for a connection");
45 |
46 | TcpThread t = new TcpThread(socket); // make a thread of it
47 | System.out.println("Starting a thread for a new Client");
48 | t.start();
49 | }
50 | }
51 | catch (IOException e) {
52 | System.out.println("Exception on new ServerSocket: " + e);
53 | }
54 | }
55 |
56 | // you must "run" server to have the server run as a console application
57 | public static void main(String[] arg) {
58 | // start server on port 1500
59 | new SnesNETsrv(13375);
60 | }
61 |
62 | /*
63 | public int sendBytes(DataOutputStream output, byte data[]){
64 | try {
65 | output.write(data, 0, 2);
66 | } catch (IOException e) {
67 | // TODO Auto-generated catch block
68 | e.printStackTrace();
69 | } //.print(a+""+b);
70 |
71 | try {
72 | output.flush();
73 | } catch (IOException e) {
74 | // TODO Auto-generated catch block
75 | e.printStackTrace();
76 | }
77 | // System.out.println("data: "+data);
78 | return 0;
79 | }
80 | */
81 |
82 | private int addSession(String user, int id){
83 |
84 | int remove_flag=-1;
85 |
86 | for(int i=0;i 3000){
94 | System.out.println("session of user "+id+" expired");
95 | remove_flag=i;
96 | }else
97 | return -1; //active session - reauth needed
98 |
99 | }
100 | }
101 |
102 | //delete old session
103 | if(remove_flag!=-1)
104 | session_list.remove(remove_flag);
105 |
106 | Session s0 = new Session(id, user); //not on list... add new session
107 | // s0.setModul(new DummyModule(null));
108 | session_list.add(s0);
109 |
110 | return 0;
111 | }
112 |
113 | private Session getSession(int id){
114 |
115 | for(int i=0;i>> 4];
128 | hexChars[j * 2 + 1] = hexArray[v & 0x0F];
129 | }
130 | return new String(hexChars);
131 | }
132 |
133 | /*
134 | private void clearList(PrintWriter Soutput, BufferedReader Sinput){
135 |
136 |
137 | for(int i=0;i
155 | }
156 | }
157 |
158 | }
159 | */
160 |
161 | private void login(PrintWriter o, BufferedReader i){
162 | System.out.println("login");
163 | }
164 |
165 |
166 | //10 random bit for trivium
167 | //only harmless chars used for the lulz
168 | private String generateIV(){
169 | String iv = "";
170 |
171 | Random randomGenerator = new Random();
172 | for (int idx = 1; idx <= 10; ++idx){
173 | int randomInt = randomGenerator.nextInt(72);
174 | if(randomInt == '\\') randomInt='c';
175 | iv = iv + "" + (char)(0x30+randomInt);
176 | }
177 |
178 | return iv;
179 | }
180 |
181 |
182 | /** One instance of this thread will run for each client */
183 | class TcpThread extends Thread {
184 | // the socket where to listen/talk
185 | Socket socket;
186 | BufferedReader input;
187 | PrintWriter output;
188 |
189 | OutputStream out_stream;
190 | DataOutputStream dos;
191 |
192 | MySQLAccess dao = new MySQLAccess("jdbc:mysql://10.0.0.1/snesnet", "snesnet", "asdasd");
193 |
194 | String username;
195 | String password;
196 | Session session0;
197 | int auth_id=0;
198 | int reauth=0;
199 | int id;
200 | String iv="";
201 |
202 | TcpThread(Socket socket) {
203 | this.socket = socket;
204 | }
205 | public void run() {
206 | /* Creating both Data Stream */
207 | System.out.println("Thread trying to create Object Input/Output Streams");
208 | try
209 | {
210 | out_stream = socket.getOutputStream();
211 | dos = new DataOutputStream(out_stream);
212 | output = new PrintWriter(socket.getOutputStream(), true);
213 |
214 | input = new BufferedReader( new InputStreamReader(socket.getInputStream()));
215 |
216 |
217 | }
218 | catch (IOException e) {
219 | System.out.println("Exception creating new Input/output Streams: " + e);
220 | return;
221 | }
222 | System.out.println("Thread waiting for a String from the Client");
223 |
224 |
225 | boolean active = true;
226 |
227 | //loop connection
228 | do{
229 | String cmd = null;
230 |
231 | if(socket.isConnected())
232 | try {
233 | //line based input
234 | cmd = input.readLine();
235 | } catch (IOException e) {
236 | auth_id=0;
237 | output.close();
238 |
239 | if(session0!=null)
240 | session0.setLogout_time(System.currentTimeMillis());
241 |
242 | try {
243 | input.close();
244 | } catch (IOException e1) {
245 | // TODO Auto-generated catch block
246 | if(session0!=null)
247 | session0.setLogout_time(System.currentTimeMillis());
248 | e1.printStackTrace();
249 | }
250 | // Handle the error
251 | }
252 |
253 |
254 |
255 | if(cmd != null){
256 | // System.out.println("*"+cmd+"*");
257 | switch(cmd.charAt(0)){
258 | /*
259 | username = client0
260 | password = oisjoseoisiusde
261 | key = 0AnL]d`50m
262 | */
263 |
264 |
265 | case 'U' : username = cmd.substring(1);
266 | System.out.println("#"+username+"#");
267 | //1. check if valid user in db
268 |
269 | //todo:
270 | //some security and length checks here
271 |
272 | String statement = "select * from snesnet.user where username like '"+ username +"'";
273 | int db_userid=0;
274 | try {
275 | db_userid = dao.getIntFromqueryDB(statement, "userid");
276 | } catch (Exception e1) {
277 | // TODO Auto-generated catch block
278 | e1.printStackTrace();
279 | }
280 |
281 | if(db_userid!=0){
282 | if(addSession(username,db_userid)==0){ //2. is valid username - add session
283 | System.out.println("new session added: auth needed");
284 | }else{
285 | System.out.println("old session: reauth needed");
286 | reauth=1;
287 | }
288 | id=db_userid;
289 |
290 | // reference Session
291 | session0 = getSession(db_userid);
292 |
293 | //send iv
294 | iv = generateIV();
295 | System.out.println("iv test: "+iv);
296 | output.println("IV "+iv);
297 |
298 | }else{
299 | //send wrong username
300 | output.println("ERROR");
301 | }
302 |
303 |
304 | //maybe add with unique db-id instead 1-333
305 |
306 |
307 | break;
308 | case 'P' : password = cmd.substring(1);
309 | System.out.println("#"+password+"#");
310 |
311 |
312 |
313 | String key="";
314 | // System.out.println("id: "+id);
315 | statement = "select * from snesnet.user where userid = '"+ id +"'";
316 |
317 | try {
318 |
319 | key = dao.getStringFromqueryDB(statement, "key");
320 | } catch (Exception e1) {
321 | // TODO Auto-generated catch block
322 | e1.printStackTrace();
323 | }
324 |
325 | String db_pw="";
326 | // System.out.println("id: "+id);
327 | statement = "select * from snesnet.user where userid = '"+ id +"'";
328 |
329 | try {
330 |
331 | db_pw = dao.getStringFromqueryDB(statement, "password");
332 | } catch (Exception e1) {
333 | // TODO Auto-generated catch block
334 | e1.printStackTrace();
335 | }
336 |
337 |
338 |
339 | String iv_hex = bytesToHex(iv.getBytes());
340 | String key_hex = bytesToHex(key.getBytes());
341 | String pass_hex_crypt = password; //bytesToHex(password.getBytes());
342 |
343 |
344 |
345 |
346 |
347 | // using the Runtime exec method:
348 | String exec = "/home/tt/snesnet/trivium_cmd/trivium "+key_hex+" "+iv_hex+" "+pass_hex_crypt;
349 | Process p = null;
350 | try {
351 | p = Runtime.getRuntime().exec(exec);
352 | } catch (IOException e1) {
353 | // TODO Auto-generated catch block
354 | e1.printStackTrace();
355 | }
356 |
357 | BufferedReader stdInput = new BufferedReader(new
358 | InputStreamReader(p.getInputStream()));
359 |
360 | // read the output from the command
361 |
362 | String hex_pw = "";
363 | try {
364 | String tmp = null;
365 | while ((tmp = stdInput.readLine()) != null) {
366 | hex_pw = tmp.substring(5);
367 | }
368 | } catch (IOException e1) {
369 | // TODO Auto-generated catch block
370 | e1.printStackTrace();
371 | }
372 |
373 |
374 | // System.out.println("enc2: "+hex_pw); //pw in hex
375 | StringBuilder decoded_pw = new StringBuilder();
376 | for (int i = 0; i < hex_pw.length(); i+=2) {
377 | String str = hex_pw.substring(i, i+2);
378 | decoded_pw.append((char)Integer.parseInt(str, 16));
379 | }
380 | System.out.println( "decoded: "+ decoded_pw);
381 |
382 | //sqlquery :> if plain pw is the right one
383 |
384 | String user_esc = username; //todo escape me first
385 | String pass_esc = decoded_pw.toString(); //todo escape me first
386 |
387 | String auth_query = "SELECT userid FROM snesnet.user WHERE username = '"+user_esc+"' AND password = (SHA1(CONCAT_WS(':','"+user_esc+"','"+pass_esc+"')))";
388 |
389 |
390 |
391 | try {
392 | auth_id = dao.getIntFromqueryDB(auth_query, "userid");
393 | //not found -> null -> 0
394 | } catch (Exception e1) {
395 | // TODO Auto-generated catch block
396 | e1.printStackTrace();
397 | }
398 | /*
399 | try {
400 | sleep(1000);
401 | } catch (InterruptedException e1) {
402 | // TODO Auto-generated catch block
403 | e1.printStackTrace();
404 | }
405 | */
406 | if(auth_id!=0){
407 | System.out.println( "auth: access granted");
408 | output.println("OK");
409 | }else{
410 | System.out.println( "auth: access denied");
411 | output.println("ERROR");
412 | }
413 |
414 |
415 | break;
416 |
417 |
418 | case 'C' : //command
419 | if(auth_id!=0){
420 | String value=cmd.substring(1);
421 | System.out.println("cmd: ["+value+"]");
422 |
423 | //check if reauth
424 | if(reauth==0){
425 | //first byte is module id
426 | System.out.println("setting module:"+value);
427 | switch(value){
428 | case "01": session0.setModule(new TestModule(dos));
429 | break;
430 | case "02": session0.setModule(new DemoModule(dos));
431 | break;
432 |
433 | default: session0.setModule(new DummyModule(dos));
434 | }
435 |
436 | //setting in reauth mode not to add the next cmd as module
437 | reauth=2;
438 |
439 | }else{
440 | if(reauth==1){ //real_reauth mode - set new dos
441 | System.out.println("setting new dos to module");
442 | session0.getModule().setDos(dos);
443 | reauth=2;
444 | }
445 |
446 | //std module command
447 | session0.getModule().setCommand(value);
448 | }
449 |
450 |
451 | /*if(value.equals("ff")){
452 | startTime = System.currentTimeMillis();
453 | }else if (value.equals("00")){
454 | long stopTime = System.currentTimeMillis();
455 | long elapsedTime = stopTime - startTime;
456 | System.out.println("benchmark - 100 bytes: "+elapsedTime+" ms");
457 | }else{
458 | System.out.println("pkt counter: "+ (++pkt_cnt));
459 | }
460 | */
461 |
462 |
463 | }
464 | //send command to modul inside session
465 | break;
466 | case 'Q' : //quit
467 | //log out
468 | try {
469 | auth_id=0;
470 | session0.setLogout_time(System.currentTimeMillis());
471 | active=false;
472 | output.close();
473 | input.close();
474 | System.out.println("Client quit");
475 | }
476 | catch (Exception e) {
477 | }
478 |
479 | break;
480 | case 'X' : //clearList(output, input);
481 | break; //clear finished stopped
482 | default : break;
483 | }
484 | }else{
485 | //nullstring -> quit client
486 | try {
487 | auth_id=0;
488 | session0.setLogout_time(System.currentTimeMillis());
489 | active=false;
490 | output.close();
491 | input.close();
492 | System.out.println("Client quit");
493 | }
494 | catch (Exception e) {
495 | }
496 | }
497 |
498 | //still active?
499 | }while(active);
500 |
501 |
502 | }
503 | }
504 | }
505 |
506 |
--------------------------------------------------------------------------------
/snesnet_firmware/src/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * SNESNET v0.1 by saturnu 2015
3 | * snes network client firmware
4 | Kuroneko!
5 |
6 | :\ /; _
7 | ; \___/ ; ; ;
8 | ,:-"' `"-:. / ;
9 | _ /,---. ,---.\ _ _; /
10 | _:>(( | ) ( | ))<:_ ,-""_,"
11 | \````` `````/""""",-""
12 | '-.._ v _..-' )
13 | / ___ ____,.. \
14 | / / | | | ( \. \
15 | ctr / / | | | | \ \
16 | `" `" `" `"
17 |
18 | nyannyannyannyannyannyannyannyannyannyannyannyannyannyannyannyannyan
19 | */
20 |
21 | #include
22 | #include //strtol
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 |
30 | #include "io.h"
31 | #include "trivium.h" //encryption
32 |
33 |
34 | // Mega168 SPI I/O
35 | #define SPI_PORT PORTB
36 | #define SPI_DDR DDRB
37 | #define SPI_CS PORTB2
38 | // Wiznet W3150a+ Op Code
39 |
40 | #define WIZNET_WRITE_OPCODE 0xF0
41 | #define WIZNET_READ_OPCODE 0x0F
42 |
43 | // Wiznet W3510a+ Register Addresses
44 | #define MR 0x0000 // Mode Register
45 | #define GAR 0x0001 // Gateway Address: 0x0001 to 0x0004
46 | #define SUBR 0x0005 // Subnet mask Address: 0x0005 to 0x0008
47 | #define SAR 0x0009 // Source Hardware Address (MAC): 0x0009 to 0x000E
48 | #define SIPR 0x000F // Source IP Address: 0x000F to 0x0012
49 | #define RMSR 0x001A // RX Memory Size Register
50 | #define TMSR 0x001B // TX Memory Size Register
51 | #define S0_MR 0x0400 // Socket 0: Mode Register Address
52 | #define S0_CR 0x0401 // Socket 0: Command Register Address
53 | #define S0_IR 0x0402 // Socket 0: Interrupt Register Address
54 | #define S0_SR 0x0403 // Socket 0: Status Register Address
55 | #define S0_PORT 0x0404 // Socket 0: Source Port: 0x0404 to 0x0405
56 | //add saturnu
57 | #define S0_DPORT 0x0410 // Socket 0: deest Port: 0x0410 to 0x0411
58 | #define S0_DIPR 0x040C // dest IP Address: 0x040C to 0x040F
59 | //
60 | #define SO_TX_FSR 0x0420 // Socket 0: Tx Free Size Register: 0x0420 to 0x0421
61 | #define S0_TX_RD 0x0422 // Socket 0: Tx Read Pointer Register: 0x0422 to 0x0423
62 | #define S0_TX_WR 0x0424 // Socket 0: Tx Write Pointer Register: 0x0424 to 0x0425
63 | #define S0_RX_RSR 0x0426 // Socket 0: Rx Received Size Pointer Register: 0x0425 to 0x0427
64 | #define S0_RX_RD 0x0428 // Socket 0: Rx Read Pointer: 0x0428 to 0x0429
65 | #define TXBUFADDR 0x4000 // W3510a+ Send Buffer Base Address
66 | #define RXBUFADDR 0x6000 // W3510a+ Read Buffer Base Address
67 |
68 | // S0_MR values
69 | #define MR_CLOSE 0x00 // Unused socket
70 | #define MR_TCP 0x01 // TCP
71 | #define MR_UDP 0x02 // UDP
72 | #define MR_IPRAW 0x03 // IP LAYER RAW SOCK
73 | #define MR_MACRAW 0x04 // MAC LAYER RAW SOCK
74 | #define MR_PPPOE 0x05 // PPPoE
75 | #define MR_ND 0x20 // No Delayed Ack(TCP) flag
76 | #define MR_MULTI 0x80 // support multicating
77 |
78 | // S0_CR values
79 | #define CR_OPEN 0x01 // Initialize or open socket
80 | #define CR_LISTEN 0x02 // Wait connection request in tcp mode(Server mode)
81 | #define CR_CONNECT 0x04 // Send connection request in tcp mode(Client mode)
82 | #define CR_DISCON 0x08 // Send closing reqeuset in tcp mode
83 | #define CR_CLOSE 0x10 // Close socket
84 | #define CR_SEND 0x20 // Update Tx memory pointer and send data
85 | #define CR_SEND_MAC 0x21 // Send data with MAC address, so without ARP process
86 | #define CR_SEND_KEEP 0x22 // Send keep alive message
87 | #define CR_RECV 0x40 // Update Rx memory buffer pointer and receive data
88 |
89 | // S0_SR values
90 | #define SOCK_CLOSED 0x00 // Closed
91 | #define SOCK_INIT 0x13 // Init state
92 | #define SOCK_LISTEN 0x14 // Listen state
93 | #define SOCK_SYNSENT 0x15 // Connection state
94 | #define SOCK_SYNRECV 0x16 // Connection state
95 | #define SOCK_ESTABLISHED 0x17 // Success to connect
96 | #define SOCK_FIN_WAIT 0x18 // Closing state
97 | #define SOCK_CLOSING 0x1A // Closing state
98 | #define SOCK_TIME_WAIT 0x1B // Closing state
99 | #define SOCK_CLOSE_WAIT 0x1C // Closing state
100 | #define SOCK_LAST_ACK 0x1D // Closing state
101 | #define SOCK_UDP 0x22 // UDP socket
102 | #define SOCK_IPRAW 0x32 // IP raw mode socket
103 | #define SOCK_MACRAW 0x42 // MAC raw mode socket
104 | #define SOCK_PPPOE 0x5F // PPPOE socket
105 | #define TX_BUF_MASK 0x07FF // Tx 2K Buffer Mask:
106 | #define RX_BUF_MASK 0x07FF // Rx 2K Buffer Mask:
107 | #define NET_MEMALLOC 0x05 // Use 2K of Tx/Rx Buffer
108 | #define TCP_PORT 0x03E4 // TCP/IP src Port 996
109 | #define TCP_DPORT 0x343F // TCP/IP dest Port 13375
110 |
111 |
112 | // Debugging Mode, 0 - Debug OFF, 1 - Debug ON
113 | #define _DEBUG_MODE 0
114 | #if _DEBUG_MODE
115 | #define BAUD_RATE 9600
116 | #endif
117 |
118 |
119 | // Define W3510a+ Socket Register and Variables Used
120 | uint8_t sockreg;
121 | #define MAX_BUF 512
122 | uint8_t buf[MAX_BUF];
123 |
124 | uint16_t local_port=100;
125 | uint8_t login_state=0;
126 |
127 | uint16_t port_0;
128 | uint16_t port_1;
129 |
130 |
131 |
132 | #if _DEBUG_MODE
133 | void uart_init(void)
134 | {
135 | UBRR0H = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
136 | UBRR0L = (((F_CPU/BAUD_RATE)/16)-1);
137 | UCSR0B = (1<> 8;
200 | // Wait for transmission complete
201 | while(!(SPSR & (1<> 8;
226 | // Wait for transmission complete
227 | while(!(SPSR & (1<> 8));
317 | SPI_Write(S0_PORT + 1,(uint8_t)(port & 0x00ff));
318 | } else {
319 | local_port++; // if don't set the source port, set local_port number.
320 | SPI_Write(S0_PORT,(uint8_t)((local_port & 0xff00) >> 8));
321 | SPI_Write(S0_PORT + 1,(uint8_t)(local_port & 0x00ff));
322 | }
323 | SPI_Write(S0_CR,CR_OPEN); // run sockinit Sn_CR
324 | /* +200804[woong]:wait to process the command... */
325 | while(SPI_Read(S0_CR));
326 | /* ------- */
327 | ret = 1;
328 |
329 | return ret;
330 | }
331 |
332 | /* deactivated for codeshrink
333 | uint8_t listen(uint8_t sock)
334 | {
335 | uint8_t retval = 0;
336 | if (sock != 0) return retval;
337 | if (SPI_Read(S0_SR) == SOCK_INIT) {
338 | // Send the LISTEN Command
339 | SPI_Write(S0_CR,CR_LISTEN);
340 |
341 | // Wait for Listening Process
342 | while(SPI_Read(S0_CR));
343 |
344 | // Check for Listen Status
345 | if (SPI_Read(S0_SR) == SOCK_LISTEN)
346 | retval=1;
347 | else
348 | close(sock);
349 | }
350 | return retval;
351 | }
352 | */
353 |
354 | uint8_t connect(uint8_t * addr, uint16_t port)
355 | {
356 | uint8_t ret;
357 | /*
358 | #ifdef _DEBUG_MODE
359 | printf("connect()\r\n");
360 | #endif
361 | */
362 | if
363 | (
364 | ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
365 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
366 | (port == 0x00)
367 | )
368 | {
369 | ret = 0;
370 | #ifdef _DEBUG_MODE
371 | printf("Fail[invalid ip,port]\r\n");
372 | #endif
373 | }
374 | else
375 | {
376 |
377 | ret = 1;
378 | // set destination IP
379 | SPI_Write( (S0_DIPR + 0), addr[0] );
380 | SPI_Write( (S0_DIPR + 1), addr[1] );
381 | SPI_Write( (S0_DIPR + 2), addr[2] );
382 | SPI_Write( (S0_DIPR + 3), addr[3] );
383 |
384 | SPI_Write( S0_DPORT + 0, (uint8_t)((port & 0xff00) >> 8));
385 | SPI_Write( S0_DPORT + 1, (uint8_t)(port & 0x00ff));
386 |
387 | SPI_Write( S0_CR, CR_CONNECT );
388 |
389 | // wait for completion
390 | while(SPI_Read(S0_CR))
391 | {
392 |
393 | if (SPI_Read(S0_SR) == SOCK_CLOSED)
394 | {
395 | /*
396 | #ifdef _DEBUG_MODE
397 | printf("SOCK_CLOSED.\r\n");
398 | #endif
399 | */
400 | ret = 0; break;
401 | }
402 | }
403 | SPI_Write(SUBR + 0, 0x00);
404 | SPI_Write(SUBR + 1, 0x00);
405 | SPI_Write(SUBR + 2, 0x00);
406 | SPI_Write(SUBR + 3, 0x00);
407 |
408 | }
409 |
410 | return ret;
411 | }
412 |
413 |
414 | uint16_t send(uint8_t sock,const uint8_t *buf,uint16_t buflen)
415 | {
416 | uint16_t ptr,offaddr,realaddr,txsize,timeout;
417 |
418 | if (buflen <= 0 || sock != 0) return 0;
419 | /*
420 | #if _DEBUG_MODE
421 | printf("Send Size: %d\n",buflen);
422 | #endif
423 | */
424 | // Make sure the TX Free Size Register is available
425 | txsize=SPI_Read(SO_TX_FSR);
426 | txsize=(((txsize & 0x00FF) << 8 ) + SPI_Read(SO_TX_FSR + 1));
427 |
428 | /*
429 | #if _DEBUG_MODE
430 | printf("TX Free Size: %d\n",txsize);
431 | #endif
432 | */
433 | timeout=0;
434 | while (txsize < buflen) {
435 | _delay_ms(1);
436 | txsize=SPI_Read(SO_TX_FSR);
437 | txsize=(((txsize & 0x00FF) << 8 ) + SPI_Read(SO_TX_FSR + 1));
438 | // Timeout for approx 1000 ms
439 | if (timeout++ > 1000) {
440 | /*
441 | #if _DEBUG_MODE
442 | printf("TX Free Size Error!\n");
443 | #endif
444 | * */
445 | // Disconnect the connection
446 | disconnect(sock);
447 | return 0;
448 | }
449 | }
450 |
451 | // Read the Tx Write Pointer
452 | ptr = SPI_Read(S0_TX_WR);
453 | offaddr = (((ptr & 0x00FF) << 8 ) + SPI_Read(S0_TX_WR + 1));
454 | /*
455 | #if _DEBUG_MODE
456 | printf("TX Buffer: %x\n",offaddr);
457 | #endif
458 | */
459 |
460 | while(buflen) {
461 | buflen--;
462 | // Calculate the real W3510a+ physical Tx Buffer Address
463 | realaddr = TXBUFADDR + (offaddr & TX_BUF_MASK);
464 | // Copy the application data to the W5100 Tx Buffer
465 | SPI_Write(realaddr,*buf);
466 | offaddr++;
467 | buf++;
468 | }
469 |
470 | // Increase the S0_TX_WR value, so it point to the next transmit
471 | SPI_Write(S0_TX_WR,(offaddr & 0xFF00) >> 8 );
472 | SPI_Write(S0_TX_WR + 1,(offaddr & 0x00FF));
473 |
474 | // Now Send the SEND command
475 | SPI_Write(S0_CR,CR_SEND);
476 |
477 | // Wait for Sending Process
478 | while(SPI_Read(S0_CR));
479 |
480 | return 1;
481 | }
482 |
483 |
484 | uint16_t recv(uint8_t sock,uint8_t *buf,uint16_t buflen)
485 | {
486 | uint16_t ptr,offaddr,realaddr;
487 |
488 | if (buflen <= 0 || sock != 0) return 1;
489 |
490 | // If the request size > MAX_BUF,just truncate it
491 | if (buflen > MAX_BUF)
492 | buflen=MAX_BUF - 2;
493 | // Read the Rx Read Pointer
494 | ptr = SPI_Read(S0_RX_RD);
495 | offaddr = (((ptr & 0x00FF) << 8 ) + SPI_Read(S0_RX_RD + 1));
496 | /*
497 | #if _DEBUG_MODE
498 | printf("RX Buffer: %x\n",offaddr);
499 | #endif
500 | */
501 |
502 | while(buflen) {
503 | buflen--;
504 | realaddr=RXBUFADDR + (offaddr & RX_BUF_MASK);
505 | *buf = SPI_Read(realaddr);
506 | offaddr++;
507 | buf++;
508 | }
509 | *buf='\0'; // String terminated character
510 |
511 | // Increase the S0_RX_RD value, so it point to the next receive
512 | SPI_Write(S0_RX_RD,(offaddr & 0xFF00) >> 8 );
513 | SPI_Write(S0_RX_RD + 1,(offaddr & 0x00FF));
514 |
515 | // Now Send the RECV command
516 | SPI_Write(S0_CR,CR_RECV);
517 | _delay_us(5); // Wait for Receive Process
518 |
519 | return 1;
520 | }
521 |
522 |
523 | uint16_t recv_size(void)
524 | {
525 | return ((SPI_Read(S0_RX_RSR) & 0x00FF) << 8 ) + SPI_Read(S0_RX_RSR + 1);
526 | }
527 |
528 | /*
529 | int strindex(char *s,char *t)
530 | {
531 | uint16_t i,n;
532 |
533 | n=strlen(t);
534 | for(i=0;*(s+i); i++) {
535 | if (strncmp(s+i,t,n) == 0)
536 | return i;
537 | }
538 | return -1;
539 | }
540 | */
541 |
542 | //called by incoming thread
543 | int process_incoming(void* rec_buffer, int size){
544 |
545 | uint8_t* state = (uint8_t*)rec_buffer;
546 |
547 | int ptr=0;
548 |
549 | //printf("pi size %d\n", size);
550 |
551 | //security cutoff
552 | //if(size%2 != 0) size--;
553 |
554 | while(ptrspecial command and no controller input
571 | /*
572 | if(CHECK_BIT(state[ptr],4) == 0){
573 |
574 | char sbuff[2];
575 | sbuff[1]='\0';
576 |
577 | //do something special here
578 | if(recvGPIO(0)){ //test only: default 0 bit 6
579 | sbuff[0]='B';
580 | }else{
581 | sbuff[0]='A';
582 | }
583 |
584 | //sending...
585 | if (send(sockreg, sbuff, strlen((char *)sbuff)) <= 0){
586 | //error
587 | }
588 | */
589 |
590 | //}else{
591 | //just controller data
592 | port_1=0xFFFF;
593 | port_1 = (state[ptr] << 8) + state[ptr+1];
594 |
595 | // printf("size %d value %04x\n", size, port_1);
596 | //_delay_ms(20);
597 | //}
598 |
599 | ptr+=2;
600 |
601 | _delay_us(10);
602 |
603 | }
604 |
605 | return 0;
606 | }
607 |
608 |
609 | //controller_1 latch
610 | ISR(INT1_vect)
611 | {
612 | //start shiftig controller bits out
613 | sendPort_1(port_1);
614 | }
615 |
616 | //port2 io
617 | ISR(INT0_vect)
618 | {
619 | static uint8_t in_byte=0;
620 | static uint8_t in_byte_cnt=0;
621 |
622 | if(recvGPIO(0)){ //port1 io
623 | SET_BIT(in_byte,in_byte_cnt);
624 | }else{
625 | CLEAR_BIT(in_byte,in_byte_cnt);
626 | }
627 | /*
628 | #if _DEBUG_MODE
629 | printf(": in_byte_cnt: %d\n",in_byte_cnt);
630 | #endif
631 | */
632 |
633 | if(in_byte_cnt==7){
634 | char sbuff[5];
635 | sbuff[0]='C';
636 | snprintf(sbuff+1, 3,"%02x", (char)in_byte);
637 | //sbuff[1]=(char)in_byte; //bug 0b00000000 -> endline
638 | sbuff[3]='\n';
639 | sbuff[4]='\0';
640 |
641 | if(login_state==3)
642 | if (send(sockreg, sbuff, strlen((char *)sbuff)) <= 0){
643 | //error
644 | }else{
645 | /*
646 | #if _DEBUG_MODE
647 | printf(": sent\n");
648 | #endif
649 | */
650 | }
651 | in_byte_cnt=0;
652 | }else{
653 |
654 | in_byte_cnt++;
655 | }
656 |
657 | }
658 |
659 |
660 | #if _DEBUG_MODE
661 | // Assign I/O stream to UART
662 | FILE uart_str = FDEV_SETUP_STREAM(uart_putch, uart_getch, _FDEV_SETUP_RW);
663 | #endif
664 |
665 |
666 | int main(void){
667 | uint8_t sockstat;
668 | uint16_t rsize;
669 | // char radiostat0[10],radiostat1[10],temp[4];
670 | //int getidx,postidx;
671 | port_0=0xFFFF;
672 | port_1=0xFFFF;
673 |
674 | //TODO set in EEPROM
675 |
676 | //server ip
677 | uint8_t dest_ip_addr[] = {192,168,123,222};
678 |
679 | //login data
680 | char username[] = "Uclient0\n"; //Uxxx\n
681 | char password[] = "oisjoseoisiusde"; //Pxxx\n
682 | char key[] = "0AnL]d`50m";
683 |
684 | // Reset Port D
685 | //DDRD = 0xFF; // Set PORTD as Output
686 | DDRD = 0b11110011;
687 | PORTD = 0x00;
688 |
689 | #if _DEBUG_MODE
690 | // Define Output/Input Stream
691 | stdout = stdin = &uart_str;
692 | // Initial UART Peripheral
693 | uart_init();
694 | // Clear Screen
695 | ansi_me();
696 | ansi_cl();
697 | ansi_me();
698 | ansi_cl();
699 | uart_flush();
700 | #endif
701 |
702 | // Initial the AVR ATMega168 SPI Peripheral
703 | // Set MOSI (PORTB3),SCK (PORTB5) and PORTB2 (SS) as output, others as input
704 | SPI_DDR = (1< 0) {
779 | _delay_ms(1); //bugfix delay,
780 | if (recv(sockreg,buf,rsize) <= 0){
781 | }else //read tcp data to buffer , u8 buffer
782 | process_incoming(buf, rsize);
783 |
784 |
785 | // Disconnect the socket
786 | //disconnect(sockreg);
787 |
788 |
789 | } else{
790 | _delay_us(10); // Wait for request
791 |
792 | }
793 | }//ls3
794 | else
795 | if(login_state==0){
796 | char sbuff[4];
797 |
798 | //send username plain - non hex
799 | if (send(sockreg, username, strlen((char *)username)) <= 0){
800 | //sending error signal
801 | ledSignal(20);
802 | ledOnYellow();
803 | }
804 | login_state=1;
805 | }//ls0
806 | else
807 | if(login_state==1){
808 |
809 | //wait for iv or error
810 | // rsize=recv_size();
811 | if (rsize > 0) {
812 | if (recv(sockreg,buf,rsize) <= 0){
813 | //no data in rx buffer
814 | }else{ //data in rx buffer
815 | //data found check for errors
816 | //secure strcmp workaround
817 | if(rsize >= 5)
818 | if(buf[0]=='E' && buf[1]=='R' && buf[2]=='R' && buf[3]=='O' && buf[4]=='R') {
819 | //signal error disconnect
820 | ledSignal(20);
821 | ledOnYellow();
822 | }else{
823 | if(buf[0]=='I' && buf[1]=='V') {
824 | //should now be iv
825 | //crypt passwort
826 |
827 | trivium_ctx_t ctx;
828 | //key = key
829 | //buf+3 = iv bits
830 | trivium_init(key, 80, buf+3, 80, &ctx);
831 | trivium_enc(&ctx);
832 |
833 | unsigned char passwd_[strlen(password)];
834 |
835 | for(int g=0; g 0) {
891 | if (recv(sockreg,buf,rsize) <= 0){
892 | //no data in rx buffer
893 | // #if _DEBUG_MODE
894 | // printf("auth: 0\n");
895 | // #endif
896 | }else{
897 | //'OK' or 'ERROR'
898 | if(rsize >= 5){
899 | if(buf[0]=='E' && buf[1]=='R' && buf[2]=='R' && buf[3]=='O' && buf[4]=='R') {
900 | ledSignal(20);
901 | ledOnYellow();
902 | #if _DEBUG_MODE
903 | printf("auth: error\n");
904 | #endif
905 |
906 | }
907 | }else if(rsize >= 3){
908 | if(buf[0]=='O' && buf[1]=='K'){
909 | //auth ok?
910 |
911 | #if _DEBUG_MODE
912 | printf("auth: ok\n");
913 | #endif
914 |
915 | login_state=3;
916 | }
917 | }
918 |
919 | }
920 | }
921 | _delay_ms(100);
922 | /*
923 | if (send(sockreg, password, strlen((char *)password)) <= 0){
924 | //error
925 | }
926 | */
927 |
928 | }//ls2
929 |
930 |
931 | break;
932 | case SOCK_CLOSE_WAIT: // If the client request to close
933 |
934 | /*
935 | #if _DEBUG_MODE
936 | printf(": CLOSE_WAIT");
937 | #endif
938 | */
939 | disconnect(0);
940 | sock_state = 0;
941 | break;
942 |
943 | //0x00
944 | case SOCK_CLOSED: // if a socket is closed
945 |
946 | ledOnYellow();
947 |
948 | if(!sock_state)
949 | {
950 | /*
951 | #if _DEBUG_MODE
952 | printf(": Loop-Back TCP Client Started.");
953 | #endif
954 | */
955 |
956 | sock_state = 1;
957 | }
958 |
959 | if(socket(MR_TCP,local_port++,0x00) == 0) // reinitialize the socket
960 | {
961 | /*
962 | #if _DEBUG_MODE
963 | printf("\a: Fail to create socket.");
964 | #endif
965 | */
966 | sock_state = 0;
967 | }
968 | else{
969 | _delay_ms(1);
970 | connect(dest_ip_addr,TCP_DPORT);
971 | }
972 | break;
973 |
974 | case SOCK_FIN_WAIT:
975 | case SOCK_CLOSING:
976 | case SOCK_TIME_WAIT:
977 | case SOCK_LAST_ACK:
978 | // Force to close the socket
979 | close(sockreg);
980 | ledOnYellow();
981 |
982 | #if _DEBUG_MODE
983 | printf("Socket Close!\n");
984 | #endif
985 |
986 | }
987 |
988 | }
989 | return 0;
990 | }
991 |
--------------------------------------------------------------------------------