1 
2 #include "rct_nxt_output.h"
3 
4 /*
5  *  General NXT USB stuff
6  */
7 
8 #define USB_ID_VENDOR_LEGO  0x0694
9 #define USB_ID_PRODUCT_NXT  0x0002
10 
11 #define USB_TIMEOUT         1000
12 
13 #define USB_STATUS_SUCCESS  0x00
14 
15 #define NXT_BUFF_LEN        100
16 #define NXT_RESPONSE_MAX    1024
17 #define NXT_NAME_LEN        15
18 #define NXT_FILENAME_MAX    19
19 
20 typedef enum
21 {
22     NXT_NOT_PRESENT = 1,
23     NXT_CONFIGURATION_ERROR,
24     NXT_IN_USE,
25     NXT_WRITE_ERROR,
26     NXT_READ_ERROR,
27     NXT_SAMBA_PROTOCOL_ERROR,
28     NXT_FILE_ERROR,
29     NXT_INVALID_FIRMWARE
30 }   nxt_status_t;
31 
32 typedef enum
33 {
34     NXT_NO_CONNECTION,
35     NXT_BLUETOOTH,
36     NXT_USB
37 }   nxt_connection_t;
38 
39 #define NXT_VENDOR(dev)     ((dev)->descriptor.idVendor)
40 #define NXT_PRODUCT(dev)    ((dev)->descriptor.idProduct)
41 
42 /* USB parameters for the NXT brick */
43 #define NXT_USB_INTERFACE       0
44 #define NXT_USB_OUT_ENDPOINT    0x01
45 #define NXT_USB_IN_ENDPOINT     0x82
46 
47 /* Command codes */
48 #define NXT_DIRECT_CMD      0x00
49 #define NXT_SYSTEM_CMD      0x01
50 #define NXT_RESPONSE        0x00
51 #define NXT_NO_RESPONSE     0x80
52 
53 // NXT constants
54 #define NXT_NoResponseMask   0x80
55 #define NXT_CmdReply         0x02
56 #define NXT_DirectCmdNoReply (NXT_DirectCmd | NXT_NoResponseMask)
57 #define NXT_SystemCmdNoReply (NXT_SystemCmd | NXT_NoResponseMask)
58 #define NXT_MaxBytes         64
59 #define NXT_NameMaxLen       15
60 
61 /* Direct commands */
62 #define NXT_DC_START_PROGRAM            0x00
63 #define NXT_DC_STOP_PROGRAM             0x01
64 #define NXT_DC_PLAY_SOUND_FILE          0x02
65 #define NXT_DC_PLAY_TONE                0x03
66 #define NXT_DC_SET_OUTPUT_STATE         0x04
67 #define NXT_DC_SET_INPUT_MODE           0x05
68 #define NXT_DC_GET_OUTPUT_STATE         0x06
69 #define NXT_DC_GET_INPUT_VALUES         0x07
70 #define NXT_DC_RESET_INPUT_SCALED_VALUE 0x08
71 #define NXT_DC_MESSAGE_WRITE            0x09
72 #define NXT_DC_RESET_MOTOR_POSITION     0x0A
73 #define NXT_DC_BATTERY_LEVEL            0x0B
74 #define NXT_DC_STOP_SOUND_PLAYBACK      0x0C
75 #define NXT_DC_KEEP_ALIVE               0x0D
76 #define NXT_DC_LS_GET_STATUS            0x0E
77 #define NXT_DC_LS_WRITE                 0x0F
78 #define NXT_DC_LS_READ                  0x10
79 #define NXT_DC_GET_CURRENT_PROGRAM_NAME 0x11
80 #define NXT_DC_GET_BUTTON_STATE         0x12
81 #define NXT_DC_MESSAGE_READ             0x13
82 
83 /* System commands */
84 #define NXT_SC_OPEN_READ            0x80
85 #define NXT_SC_OPEN_WRITE           0x81
86 #define NXT_SC_READ                 0x82
87 #define NXT_SC_WRITE                0x83
88 #define NXT_SC_CLOSE                0x84
89 #define NXT_SC_DELETE               0x85
90 #define NXT_SC_FIND_FIRST           0x86
91 #define NXT_SC_FIND_NEXT            0x87
92 #define NXT_SC_GET_VERSIONS         0x88
93 #define NXT_SC_OPEN_WRITE_LINEAR    0x89
94 #define NXT_SC_OPEN_READ_LINEAR     0x8A
95 #define NXT_SC_OPEN_WRITE_DATA      0x8B
96 #define NXT_SC_OPEN_APPEND_DATA     0x8C
97 #define NXT_SC_UNKNOWN1             0x8D
98 #define NXT_SC_UNKNOWN2             0x8E
99 #define NXT_SC_UNKNOWN3             0x8F
100 #define NXT_SC_FIND_FIRST_MODULE    0x90
101 #define NXT_SC_FIND_NEXT_MODULE     0x91
102 #define NXT_SC_CLOSE_MODULE_HANDLE  0x92
103 #define NXT_SC_UNKNOWN4             0x93
104 #define NXT_SC_IO_MAP_READ          0x94
105 #define NXT_SC_IO_MAP_WRITE         0x95
106 #define NXT_SC_UNKNOWN5             0x96
107 #define NXT_SC_BOOT_COMMAND         0x97
108 #define NXT_SC_SET_BRICK_NAME       0x98
109 #define NXT_SC_UNKNOWN6             0x99
110 #define NXT_SC_GET_BT_ADDRESS       0x9A
111 #define NXT_SC_GET_DEVICE_INFO      0x9B
112 #define NXT_SC_UNKNOWN7             0x9C
113 #define NXT_SC_UNKNOWN8             0x9D
114 #define NXT_SC_UNKNOWN9             0x9E
115 #define NXT_SC_UNKNOWN10            0x9F
116 #define NXT_SC_DELETE_USER_FLASH    0xA0
117 #define NXT_SC_POLL_COMMAND_LEN     0xA1
118 #define NXT_SC_POLL_COMMAND         0xA2
119 #define NXT_SC_RENAME_FILE          0xA3
120 #define NXT_SC_BT_FACTORY_RESET     0xA4
121 
122 // NXT status codes
123 #define NXT_STATUS_SUCCESS              0x00
124 #define NXT_STATUS_NO_MORE_HANDLE       0x81
125 #define NXT_STATUS_NO_SPACE             0x82
126 #define NXT_STATUS_NO_MORE_FILES        0x83
127 #define NXT_STATUS_EOF_EXPECTED         0x84
128 #define NXT_STATUS_EOF                  0x85
129 #define NXT_STATUS_NOT_LINEAR_FILE      0x86
130 #define NXT_STATUS_FILE_NOT_FOUND       0x87
131 #define NXT_STATUS_HANDLE_CLOSED        0x88
132 #define NXT_STATUS_NO_LINEAR_SPACE      0x89
133 #define NXT_STATUS_UNDEFINED_ERROR      0x8a
134 #define NXT_STATUS_FILE_IS_BUSY         0x8b
135 #define NXT_STATUS_NO_WRITE_BUFS        0x8c
136 #define NXT_STATUS_APPEND_NOT_POSS      0x8d
137 #define NXT_STATUS_FILE_IS_FULL         0x8e
138 #define NXT_STATUS_FILE_EXISTS          0x8f
139 #define NXT_STATUS_MODULE_NOT_FOUND     0x90
140 #define NXT_STATUS_OUT_OF_BOUNDARY      0x91
141 #define NXT_STATUS_ILLEGAL_FILENAME     0x92
142 #define NXT_STATUS_ILLEGAL_HANDLE       0x93
143 
144 /* NXT parameters */
145 typedef struct
146 {
147     char                    name[NXT_NAME_LEN+1];
148     unsigned long           free_flash;
149     int                     is_in_reset_mode;
150     unsigned int            firmware_major;
151     unsigned int            firmware_minor;
152     unsigned int            protocol_major;
153     unsigned int            protocol_minor;
154     unsigned int            battery_level;
155     unsigned int            response_mask;  /* 0x00 = respond, 0x80 = no */
156 
157     /* Used only for USB connections */
158     unsigned int            usb_bus;
159     unsigned int            usb_dev_num;
160     struct usb_device       *usb_dev;
161     struct usb_dev_handle   *usb_handle;
162 
163     /* Used only for bluetooth connections */
164     //char                    *bluetooth_passcode;
165     //char                    *bluetooth_pin;
166     int                     bluetooth_fd;
167     unsigned char           bluetooth_address[7];
168     unsigned long           bluetooth_signal_strength;
169 
170     nxt_output_state_t      port[3];
171 }   rct_nxt_t;
172 
173 
174 /* Get and set macros */
175 #define NXT_SET_USB_DEV(n,d)        ((n)->usb_dev = (d))
176 #define NXT_SET_BLUETOOTH_DEV(n,a,b,c,d,e,f) \
177 	(snprintf((n)->bluetooth_address,7,"%c%c%c%c%c%c", \
178 	(a),(b),(c),(d),(e),(f)))
179 
180 #define NXT_BLUETOOTH_IS_OPEN(nxt)  ((nxt)->bluetooth_fd != -1)
181 #define NXT_USB_IS_OPEN(nxt)        ((nxt)->usb_handle != NULL)
182 #define NXT_IS_OPEN(nxt) (\
183 	    NXT_BLUETOOTH_IS_OPEN((nxt)) || NXT_USB_IS_OPEN((nxt)) \
184 	    )
185 
186