-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflash_device.c
111 lines (109 loc) · 2.41 KB
/
flash_device.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
#include <assert.h>
#include <stdio.h>
#include <squirrel.h>
#include <sqstdio.h>
#include <sqstdaux.h>
#include "type.h"
#include "file.h"
#include "memory_manage.h"
#include "squirrel_wrap.h"
#include "flash_device.h"
static void call(HSQUIRRELVM v, const char *devicename)
{
sq_pushroottable(v);
sq_pushstring(v, _SC("flash_device_get"), -1);
if(SQ_SUCCEEDED(sq_get(v,-2))){
sq_pushroottable(v);
sq_pushstring(v, _SC(devicename), -1);
SQRESULT r = sq_call(v, 2, SQTrue, SQTrue);
assert(r == SQ_OK);
}
}
static bool long_get(HSQUIRRELVM v, const char *field, long *ret)
{
sq_pushstring(v, _SC(field), -1);
SQRESULT r = sq_get(v, -2);
if(r != SQ_OK){
return false;
}
if(sq_gettype(v, -1) != OT_INTEGER){
return false;
}
SQInteger i;
r = sq_getinteger(v, -1, &i);
if(r != SQ_OK){
return false;
}
*ret = (long) i;
sq_pop(v, 1);
return true;
}
static bool bool_get(HSQUIRRELVM v, const char *field, bool *ret)
{
sq_pushstring(v, _SC(field), -1);
SQRESULT r = sq_get(v, -2);
if(r != SQ_OK){
return false;
}
if(sq_gettype(v, -1) != OT_BOOL){
return false;
}
SQBool i;
r = sq_getbool(v, -1, &i);
if(r != SQ_OK){
return false;
}
if(i == SQTrue){
*ret = true;
}else{
*ret = false;
}
sq_pop(v, 1);
return true;
}
bool flash_device_get(const char *name, struct flash_device *t)
{
HSQUIRRELVM v = qr_open();
if(SQ_FAILED(sqstd_dofile(v, _SC(find_script("flashdevice.nut")), SQFalse, SQTrue))){
puts("flash device script error");
qr_close(v);
return false;
}
SQInteger top = sq_gettop(v);
call(v, name);
if(sq_gettype(v, -1) != OT_TABLE){
goto field_error;
}
t->name = name;
if(long_get(v, "capacity", &t->capacity) == false){
goto field_error;
}
if(long_get(v, "pagesize", &t->pagesize) == false){
goto field_error;
}
if(long_get(v, "erase_wait", &t->erase_wait) == false){
goto field_error;
}
if(bool_get(v, "erase_require", &t->erase_require) == false){
goto field_error;
}
if(long_get(v, "command_mask", &t->command_mask) == false){
goto field_error;
}
long dd;
if(long_get(v, "id_manufacurer", &dd) == false){
goto field_error;
}
t->id_manufacurer = dd;
if(long_get(v, "id_device", &dd) == false){
goto field_error;
}
t->id_device = dd;
sq_settop(v, top);
qr_close(v);
return true;
field_error:
puts("script field error");
qr_close(v);
return false;
}