9
9
#include <fujiptp.h>
10
10
#include "fuji_lua.h"
11
11
12
- static int mylua_set_property (lua_State * L ) {
13
- struct PtpRuntime * r = luaptp_get_runtime (L );
14
-
15
- const char * name = luaL_checkstring (L , 1 );
16
- int value = lua_tointeger (L , 1 );
17
-
18
- int rc = ptp_set_generic_property (r , name , value );
19
-
20
- lua_pushinteger (L , rc );
21
-
22
- return 1 ;
23
- }
24
-
25
- static int mylua_device_info (lua_State * L ) {
26
- struct PtpRuntime * r = luaptp_get_runtime (L );
27
-
28
- struct PtpDeviceInfo di ;
29
- int rc = ptp_get_device_info (r , & di );
30
- if (rc ) {
31
- lua_pushinteger (L , rc );
32
- return 1 ;
33
- }
34
-
35
- char buffer [4096 ];
36
- ptp_device_info_json (& di , buffer , sizeof (buffer ));
37
-
38
- lua_json_decode (L , buffer , strlen (buffer ));
39
-
40
- return 1 ;
41
- }
42
-
43
- static int mylua_take_picture (lua_State * L ) {
44
- struct PtpRuntime * r = luaptp_get_runtime (L );
45
-
46
- if (r -> di == NULL ) return 1 ;
47
-
48
- int rc = ptp_pre_take_picture (r );
49
- if (rc ) goto err ;
50
-
51
- rc = ptp_take_picture (r );
52
- if (rc ) goto err ;
53
-
54
- err :;
55
- lua_pushinteger (L , rc );
56
-
57
- return 1 ;
58
- }
59
-
60
- static int mylua_send_operation (lua_State * L ) {
61
- struct PtpRuntime * r = luaptp_get_runtime (L );
62
-
63
- int opcode = lua_tointeger (L , 1 );
64
- int len = lua_gettop (L );
65
-
66
- if (!lua_istable (L , 2 )) {
67
- return luaL_error (L , "arg2 expected array" );
68
- }
69
-
70
- struct PtpCommand cmd ;
71
- cmd .code = opcode ;
72
-
73
- // Read parameters
74
- int param_length = 0 ;
75
- if (len >= 2 ) {
76
- param_length = luaL_len (L , 2 );
77
- for (int i = 1 ; i <= param_length ; ++ i ) {
78
- lua_rawgeti (L , 2 , i );
79
- cmd .params [i - 1 ] = luaL_checkinteger (L , -1 );
80
- lua_pop (L , 1 );
81
- }
82
- cmd .param_length = param_length ;
83
- }
84
-
85
- // Read payload if provided
86
- int data_length = 0 ;
87
- uint8_t * data_array = NULL ;
88
- if (len >= 3 ) {
89
- data_length = luaL_len (L , 3 );
90
- data_array = malloc (data_length * sizeof (int ));
91
- for (int i = 1 ; i <= data_length ; ++ i ) {
92
- lua_rawgeti (L , 3 , i );
93
- data_array [i - 1 ] = (uint8_t )luaL_checkinteger (L , -1 );
94
- lua_pop (L , 1 );
95
- }
96
- }
97
-
98
- int rc = 0 ;
99
- if (data_array == NULL ) {
100
- rc = ptp_send (r , & cmd );
101
- } else {
102
- rc = ptp_send_data (r , & cmd , data_array , data_length );
103
- }
104
-
105
- lua_newtable (L );
106
-
107
- lua_pushstring (L , "error" );
108
- lua_pushinteger (L , rc );
109
- lua_settable (L , -3 );
110
-
111
- lua_pushstring (L , "code" );
112
- lua_pushinteger (L , ptp_get_return_code (r ));
113
- lua_settable (L , -3 );
114
-
115
- lua_pushstring (L , "payload" );
116
- lua_newtable (L );
117
- for (int i = 0 ; i < ptp_get_payload_length (r ); ++ i ) {
118
- lua_pushinteger (L , i + 1 );
119
- lua_pushinteger (L , (int )(r -> data [i ]));
120
- lua_settable (L , -3 );
121
- }
122
-
123
- lua_settable (L , -3 );
124
-
125
- lua_pushstring (L , "id" );
126
- lua_pushinteger (L , ptp_get_last_transaction_id (r ));
127
- lua_settable (L , -3 );
128
-
129
- return 1 ;
130
- }
131
-
132
12
static int mylua_test (lua_State * L ) {
133
13
struct PtpRuntime * r = luaptp_get_runtime (L );
134
14
@@ -142,13 +22,10 @@ static int mylua_test(lua_State *L) {
142
22
143
23
static int mylua_connect (lua_State * L ) {
144
24
struct PtpRuntime * r = luaptp_get_runtime (L );
145
-
146
25
lua_pushinteger (L , 0 );
147
-
148
26
return 1 ;
149
27
}
150
28
151
-
152
29
static const luaL_Reg fujilib [] = {
153
30
{"test" , mylua_test },
154
31
#if 0
0 commit comments