]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/scripting.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2013 m h swende <martin at swende.se>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Some lua scripting glue to proxmark core.
9 //-----------------------------------------------------------------------------
14 #include "proxmark3.h"
17 #include "scripting.h"
19 * The following params expected:
25 static int l_SendCommand(lua_State
*L
){
29 The SendCommand (native) expects the following structure:
32 uint64_t cmd; //8 bytes
33 uint64_t arg[3]; // 8*3 bytes = 24 bytes
35 uint8_t asBytes[USB_CMD_DATA_SIZE]; // 1 byte * 512 = 512 bytes (OR)
36 uint32_t asDwords[USB_CMD_DATA_SIZE/4]; // 4 byte * 128 = 512 bytes
40 ==> A 544 byte buffer will do.
44 const char *data
= luaL_checklstring(L
, 1, &size
);
45 if(size
!= sizeof(UsbCommand
))
47 printf("Got data size %d, expected %d" , size
,sizeof(UsbCommand
));
48 lua_pushstring(L
,"Wrong data size");
52 // UsbCommand c = (*data);
55 //UsbCommand *c = (UsbCommand *)lua_touserdata(L, 1);
56 //luaL_argcheck(L, c != NULL, 1, "'UsbCommand' expected");
62 * @brief The following params expected:
68 static int l_WaitForResponseTimeout(lua_State
*L
){
71 uint32_t cmd
= luaL_checkunsigned(L
,1);
72 printf("in l_WaitForResponseTimeout, got cmd 0x%0x\n",(int) cmd
);
73 //UsbCommand response;
75 //We allocate the usbcommand as userdata on the Lua-stack
76 size_t nbytes
= sizeof(UsbCommand
);
78 UsbCommand
*response
= (UsbCommand
*)lua_newuserdata(L
, nbytes
);
80 size_t ms_timeout
= 2000;
81 //Did the user send a timeout ?
82 //Check if the current top of stack is an integer
84 if(lua_isnumber( L
, 2))
86 printf("You sent a timout-value\n");
87 ms_timeout
= luaL_checkunsigned(L
,2);
89 printf("Timeout set to %dms\n" , (int) ms_timeout
);
91 if(WaitForResponseTimeout(cmd
, response
, ms_timeout
))
93 //Return the UsbCommand as userdata
94 //the usbcommand is already on the stack
95 // return 1 to signal one return value
99 //Don't return the UsbCommand. Pop it.
106 static int l_nonce2key(lua_State
*L
){ return CmdHF14AMfRdSc(luaL_checkstring(L
, 1));}
107 static int l_PrintAndLog(lua_State
*L
){ return CmdHF14AMfDump(luaL_checkstring(L
, 1));}
109 void set_pm3_libraries(lua_State
*L
)
111 static const luaL_Reg libs
[] = {
112 {"SendCommand", l_SendCommand
},
113 {"WaitForResponseTimeout", l_WaitForResponseTimeout
},
114 {"nonce2key", l_nonce2key
},
115 {"PrintAndLog", l_PrintAndLog
},
119 lua_pushglobaltable(L
);
120 // Core library is in this table. Contains '
121 //this is 'pm3' table
124 //Put the function into the hash table.
125 for (int i
= 0; libs
[i
].name
; i
++) {
126 lua_pushcfunction(L
, libs
[i
].func
);
127 lua_setfield(L
, -2, libs
[i
].name
);//set the name, pop stack
130 lua_setfield(L
, -2, "core");
132 //-- remove the global environment table from the stack