1
+ #ifdef HAVE_CONFIG_H
2
+ #include "config.h"
3
+ #endif
4
+ #include "php.h"
5
+ #include "php_ini.h"
6
+
7
+ #include <ftdi.h>
8
+ #include "php_ftdi.h"
9
+
10
+ /* resource list entry */
11
+ static int le_php_ftdi_context ;
12
+ /* exception class entry */
13
+ static zend_class_entry * php_ftdi_exception_sc_entry ;
14
+
15
+ /* resource destructor */
16
+ static void php_ftdi_context_dtor (zend_rsrc_list_entry * rsrc TSRMLS_DC )
17
+ {
18
+ struct ftdi_context * context = (struct ftdi_context * ) rsrc -> ptr ;
19
+
20
+ if (context ) {
21
+ ftdi_usb_close (context );
22
+ ftdi_free (context );
23
+ }
24
+ }
25
+
26
+ ZEND_MINIT_FUNCTION (php_ftdi ) {
27
+ le_php_ftdi_context = zend_register_list_destructors_ex (
28
+ php_ftdi_context_dtor , NULL , PHP_FTDI_CONTEXT_RES_NAME , module_number
29
+ );
30
+
31
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_BITBANG" , BITMODE_BITBANG , CONST_CS | CONST_PERSISTENT );
32
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_CBUS" , BITMODE_CBUS , CONST_CS | CONST_PERSISTENT );
33
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_MCU" , BITMODE_MCU , CONST_CS | CONST_PERSISTENT );
34
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_MPSSE" , BITMODE_MPSSE , CONST_CS | CONST_PERSISTENT );
35
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_OPTO" , BITMODE_OPTO , CONST_CS | CONST_PERSISTENT );
36
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_RESET" , BITMODE_RESET , CONST_CS | CONST_PERSISTENT );
37
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_SYNCBB" , BITMODE_SYNCBB , CONST_CS | CONST_PERSISTENT );
38
+ REGISTER_LONG_CONSTANT ("FTDI_BITMODE_SYNCFF" , BITMODE_SYNCFF , CONST_CS | CONST_PERSISTENT );
39
+
40
+ return SUCCESS ;
41
+ }
42
+
43
+ ZEND_NAMED_FUNCTION (php_ftdi_init )
44
+ {
45
+ struct ftdi_context * context = ftdi_new ();
46
+ int resource_id ;
47
+
48
+ ftdi_init (context );
49
+
50
+ resource_id = ZEND_REGISTER_RESOURCE (return_value , context , le_php_ftdi_context );
51
+
52
+ unsigned char data = 64 ;
53
+
54
+ RETURN_RESOURCE (resource_id );
55
+ }
56
+
57
+ ZEND_NAMED_FUNCTION (php_ftdi_usb_open )
58
+ {
59
+ struct ftdi_context * context ;
60
+ long vendor_id = 0 ;
61
+ long product_id = 0 ;
62
+ zval * zcontext ;
63
+ int ret = -1 ;
64
+
65
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "rll" , & zcontext , & vendor_id , & product_id ) == FAILURE ) {
66
+ return ;
67
+ }
68
+
69
+ ZEND_FETCH_RESOURCE (context , struct ftdi_context * , & zcontext , -1 , PHP_FTDI_CONTEXT_RES_NAME , le_php_ftdi_context );
70
+
71
+ ret = ftdi_usb_open (context , vendor_id , product_id );
72
+
73
+ if (ret != 0 ) {
74
+ PHP_FTDI_THROW (context , ret );
75
+ }
76
+
77
+ RETURN_LONG ((long ) ret );
78
+ }
79
+
80
+ ZEND_NAMED_FUNCTION (php_ftdi_usb_close )
81
+ {
82
+ struct ftdi_context * context ;
83
+ zval * zcontext ;
84
+ int ret = -1 ;
85
+
86
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "r" , & zcontext ) == FAILURE ) {
87
+ return ;
88
+ }
89
+
90
+ ZEND_FETCH_RESOURCE (context , struct ftdi_context * , & zcontext , -1 , PHP_FTDI_CONTEXT_RES_NAME , le_php_ftdi_context );
91
+
92
+ ret = ftdi_usb_close (context );
93
+
94
+ if (ret != 0 ) {
95
+ PHP_FTDI_THROW (context , ret );
96
+ }
97
+
98
+ RETURN_LONG ((long ) ret );
99
+ }
100
+
101
+ ZEND_NAMED_FUNCTION (php_ftdi_set_bitmode )
102
+ {
103
+ struct ftdi_context * context ;
104
+ long bitmask ;
105
+ long mode ;
106
+ zval * zcontext ;
107
+ int ret = -1 ;
108
+
109
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "rll" , & zcontext , & bitmask , & mode ) == FAILURE ) {
110
+ return ;
111
+ }
112
+
113
+ ZEND_FETCH_RESOURCE (context , struct ftdi_context * , & zcontext , -1 , PHP_FTDI_CONTEXT_RES_NAME , le_php_ftdi_context );
114
+
115
+ ret = ftdi_set_bitmode (context , bitmask , mode );
116
+
117
+ if (ret != 0 ) {
118
+ PHP_FTDI_THROW (context , ret );
119
+ }
120
+
121
+ RETURN_LONG ((long ) ret );
122
+ }
123
+
124
+ ZEND_NAMED_FUNCTION (php_ftdi_free )
125
+ {
126
+ struct ftdi_context * context ;
127
+ zval * zcontext ;
128
+
129
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "rll" , & zcontext ) == FAILURE ) {
130
+ return ;
131
+ }
132
+
133
+ ZEND_FETCH_RESOURCE (context , struct ftdi_context * , & zcontext , -1 , PHP_FTDI_CONTEXT_RES_NAME , le_php_ftdi_context );
134
+
135
+ ftdi_free (context );
136
+ }
137
+
138
+ ZEND_NAMED_FUNCTION (php_ftdi_write_data )
139
+ {
140
+ struct ftdi_context * context ;
141
+ long * buf = NULL ;
142
+ int ret = -1 ;
143
+ zval * zcontext ;
144
+ unsigned char data ;
145
+
146
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "rl" , & zcontext , & buf ) == FAILURE ) {
147
+ return ;
148
+ }
149
+
150
+ data = (unsigned char ) (long ) buf ;
151
+
152
+ ZEND_FETCH_RESOURCE (context , struct ftdi_context * , & zcontext , -1 , PHP_FTDI_CONTEXT_RES_NAME , le_php_ftdi_context );
153
+
154
+ /* -666 device unavailable, <0 error code from usb_bulk_write, >0 number of bytes written */
155
+ ret = ftdi_write_data (context , & data , sizeof (data ));
156
+
157
+ if (ret < 0 ) {
158
+ PHP_FTDI_THROW (context , ret );
159
+ }
160
+
161
+ RETURN_LONG ((long ) ret );
162
+ }
163
+
164
+ zend_function_entry php_ftdi_functions [] = {
165
+ ZEND_NAMED_FE (ftdi_init , php_ftdi_init , NULL )
166
+ ZEND_NAMED_FE (ftdi_usb_open , php_ftdi_usb_open , NULL )
167
+ ZEND_NAMED_FE (ftdi_usb_close , php_ftdi_usb_close , NULL )
168
+ ZEND_NAMED_FE (ftdi_set_bitmode , php_ftdi_set_bitmode , NULL )
169
+ ZEND_NAMED_FE (ftdi_write_data , php_ftdi_write_data , NULL )
170
+ ZEND_NAMED_FE (ftdi_free , php_ftdi_free , NULL )
171
+ { NULL , NULL , NULL }
172
+ };
173
+
174
+ zend_module_entry php_ftdi_module_entry = {
175
+ STANDARD_MODULE_HEADER ,
176
+ PHP_FTDI_EXTNAME ,
177
+ php_ftdi_functions ,
178
+ PHP_MINIT (php_ftdi ),
179
+ NULL ,
180
+ NULL ,
181
+ NULL ,
182
+ NULL ,
183
+ PHP_FTDI_VERSION ,
184
+ STANDARD_MODULE_PROPERTIES
185
+ };
186
+
187
+ ZEND_GET_MODULE (php_ftdi )
0 commit comments