-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
473 lines (364 loc) · 9.92 KB
/
i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_i2c.h"
#include <linux/i2c-dev.h>
#include <linux/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
/* If you declare any globals in php_i2c.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(i2c)
*/
/* True global resources - no need for thread safety here */
static int le_i2c;
/* {{{ i2c_functions[]
*
* Every user visible function must have an entry in i2c_functions[].
*/
const zend_function_entry i2c_functions[] = {
PHP_FE_END /* Must be the last line in i2c_functions[] */
};
/* }}} */
/* {{{ i2c_functions[]
*
* Every user visible function must have an entry in i2c_functions[].
*/
static zend_function_entry i2c_methods[] = {
PHP_ME(i2c, __construct, i2c____construct_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(i2c, __destruct, i2c____destruct_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, write_quick, i2c____write_byte_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, read_byte, i2c____read_byte_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, write_byte, i2c____write_byte_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, read_byte_data, i2c____read_byte_data_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, write_block_data, i2c____write_block_data_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, write_cmd, i2c____write_cmd_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(i2c, println, i2c____println_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_FE_END
};
/* }}} */
/* {{{ i2c_module_entry
*/
zend_module_entry i2c_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"i2c",
i2c_functions,
PHP_MINIT(i2c),
PHP_MSHUTDOWN(i2c),
PHP_RINIT(i2c), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(i2c), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(i2c),
#if ZEND_MODULE_API_NO >= 20010901
PHP_I2C_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_I2C
ZEND_GET_MODULE(i2c)
#endif
/* {{{ PHP_INI
*/
// PHP_INI_BEGIN()
// STD_PHP_INI_ENTRY("i2c.bus_path", "/dev/i2c-", PHP_INI_ALL, OnUpdateString, bus_path, zend_i2c_globals, i2c_globals)
// PHP_INI_END()
/* }}} */
/* {{{ php_i2c_init_globals
*/
// static void php_i2c_init_globals(zend_i2c_globals *i2c_globals)
// {
// i2c_globals->bus_path = "/dev/i2c-";
// }
/* }}} */
zend_class_entry *i2c_ce;
/* {{{ proto void __construct(int bus, int chipselect[, array options])
*/
PHP_METHOD(i2c, __construct)
{
char bus_path[20];
long bus;
int addr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &bus, &addr) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
snprintf(bus_path, 19, "/dev/i2c-%ld", bus);
long fd = open(bus_path, O_RDWR);
if (fd < 0) {
php_printf("Unable to open: %s\n\n", bus_path);
RETURN_FALSE;
}
if (ioctl(fd, I2C_SLAVE, addr) < 0) {
php_printf("Unable to controll: %x\n\n", addr);
RETURN_FALSE;
}
zend_update_property_long(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), fd TSRMLS_CC);
zend_update_property_long(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("addr"), addr TSRMLS_CC);
}
/* }}} __construct */
/* {{{ proto void __destruct()
*/
PHP_METHOD(i2c, __destruct)
{
zend_class_entry * _this_ce;
zval * _this_zval = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, i2c_ce) == FAILURE) {
return;
}
_this_ce = Z_OBJCE_P(_this_zval);
int fd = Z_LVAL_P(zend_read_property(_this_ce, _this_zval, "fd", 2, 0 TSRMLS_CC));
close(fd);
}
/* }}} __destruct */
/* {{{ proto void i2c_smbus_write_quick()
*/
PHP_METHOD(i2c, write_quick)
{
int data;
__s32 res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
res = i2c_smbus_write_quick(fd, data);
if (res < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} i2c_smbus_write_quick */
/* {{{ proto void i2c_smbus_write_quick()
*/
PHP_METHOD(i2c, read_byte)
{
__s32 res;
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
res = i2c_smbus_read_byte(fd);
RETVAL_LONG(res);
}
/* }}} i2c_smbus_write_quick */
/* {{{ proto void write_cmd()
*/
PHP_METHOD(i2c, write_byte)
{
long data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &data) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
__s32 res = i2c_smbus_write_byte(fd, data);
if (res < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} __destruct */
/* {{{ proto void write_cmd()
*/
PHP_METHOD(i2c, read_byte_data)
{
long data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &data) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
__s32 res = i2c_smbus_read_byte_data(fd, data);
RETURN_LONG(res)
}
/* }}} __destruct */
/* {{{ proto void write_cmd()
*/
PHP_METHOD(i2c, write_byte_data)
{
long cmd, data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &cmd, &data) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
__s32 res = i2c_smbus_write_byte_data(fd, cmd, data);
if (res < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} __destruct */
/* {{{ proto void write_cmd()
*/
PHP_METHOD(i2c, write_block_data)
{
long cmd;
char *data;
int length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lss", &cmd, &length, &data) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
__s32 res = i2c_smbus_write_block_data(fd, cmd, length, data);
if (res < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} __destruct */
/* {{{ proto void write_cmd()
*/
PHP_METHOD(i2c, write_cmd)
{
long int cmd, addr;
__s32 res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &cmd, &addr) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
res = i2c_smbus_write_byte_data(fd, addr, cmd);
if (res < 0 ) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} __destruct */
/* {{{ proto void println()
*/
PHP_METHOD(i2c, println)
{
long int addr;
int length;
char *string = NULL;
__s32 res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &string, &length, &addr) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
zval *zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("fd"), 0 TSRMLS_CC);
int fd = Z_LVAL_P(zfile);
if (write(fd, string, sizeof(string)) != sizeof(string)) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} println */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(i2c)
{
//REGISTER_INI_ENTRIES();
zend_class_entry i2c;
INIT_CLASS_ENTRY(i2c, "i2c", i2c_functions);
INIT_CLASS_ENTRY(i2c, "i2c", i2c_methods);
i2c_ce = zend_register_internal_class_ex(&i2c, NULL, NULL TSRMLS_CC);
zend_declare_property_null(i2c_ce, ZEND_STRL("fd"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(i2c_ce, ZEND_STRL("addr"), ZEND_ACC_PUBLIC TSRMLS_CC);
//zend_declare_property_null(i2c_ce, ZEND_STRL("_file"), ZEND_ACC_PRIVATE TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(i2c)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(i2c)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(i2c)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(i2c)
{
php_info_print_table_start();
php_info_print_table_header(2, "i2c support", "enabled");
//php_info_print_table_row(2, "i2c Version", "1.0");
//php_info_print_table_row(2, "Enable_Functions", "open,read,readblock,write,writeblock");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ proto string i2c_init(string arg)
Write command with arguments */
PHP_FUNCTION(i2c_write_cmd_arg)
{
}
/* }}} */
/* {{{ proto string i2c_init(string arg)
Write block of data*/
PHP_FUNCTION(i2c_write_block_data)
{
}
/* }}} */
/* {{{ proto string i2c_init(string arg)
Write command with arguments */
PHP_FUNCTION(i2c_read)
{
long int addr,file;
zval *zfile;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &addr) == FAILURE) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
__s32 res;
zfile = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("_fd"), 0 TSRMLS_CC);
file = Z_LVAL_P(zfile);
res = i2c_smbus_read_byte_data(file, addr);
if (res < 0) {
RETURN_FALSE;
//RETURN_LONG(file);
}
RETURN_LONG(res);
}
/* }}} */
/* {{{ proto string i2c_init(string arg)
Write block of data*/
PHP_FUNCTION(i2c_read_data)
{
}
/* }}} */
/* {{{ proto string i2c_init(string arg)
Write command with arguments */
PHP_FUNCTION(i2c_read_block_data)
{
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/