1 /*  libticalcs - Ti Calculator library, a part of the TiLP project
2  *  Copyright (C) 1999-2012  Romain Liévin
3  *  Copyright (C) 2012       Lionel Debroux
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software Foundation,
17  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**
21  * \file internal.h
22  * \brief Definitions for internal (libticalcs) usage.
23  */
24 
25 #ifndef __TICALCS_INTERNAL__
26 #define __TICALCS_INTERNAL__
27 
28 #include "error.h"
29 #include "pause.h"
30 #include "macros.h"
31 
32 #define VALIDATE_NONNULL(ptr) \
33 	do \
34 	{ \
35 		if (ptr == NULL) \
36 		{ \
37 			ticalcs_critical("%s: " #ptr " is NULL", __FUNCTION__); \
38 			return ERR_INVALID_PARAMETER; \
39 		} \
40 	} while(0);
41 #define VALIDATE_HANDLE(handle) \
42 	do \
43 	{ \
44 		if (!ticalcs_validate_handle(handle)) \
45 		{ \
46 			ticalcs_critical("%s: " #handle " is invalid", __FUNCTION__); \
47 			return ERR_INVALID_HANDLE; \
48 		} \
49 	} while(0);
50 #define VALIDATE_CALCFNCTS(calc) \
51 	do \
52 	{ \
53 		if (!ticalcs_validate_calcfncts(calc)) \
54 		{ \
55 			ticalcs_critical("%s: " # calc " is invalid", __FUNCTION__); \
56 			return ERR_INVALID_HANDLE; \
57 		} \
58 	} while(0);
59 #define VALIDATE_BACKUPCONTENT(content) \
60 	do \
61 	{ \
62 		if (!ticalcs_validate_backupcontent(content)) \
63 		{ \
64 			ticalcs_critical("%s: " #content " is NULL", __FUNCTION__); \
65 			return ERR_INVALID_PARAMETER; \
66 		} \
67 	} while(0);
68 #define VALIDATE_FILECONTENT(content) \
69 	do \
70 	{ \
71 		if (!ticalcs_validate_filecontent(content)) \
72 		{ \
73 			ticalcs_critical("%s: " #content " is NULL", __FUNCTION__); \
74 			return ERR_INVALID_PARAMETER; \
75 		} \
76 	} while(0);
77 #define VALIDATE_FILECONTENT_ENTRIES(content) \
78 	do \
79 	{ \
80 		if (!ticalcs_validate_filecontent_entries(content)) \
81 		{ \
82 			ticalcs_critical("%s: " #content "->entries is NULL", __FUNCTION__); \
83 			return ERR_INVALID_PARAMETER; \
84 		} \
85 	} while(0);
86 #define VALIDATE_FLASHCONTENT(content) \
87 	do \
88 	{ \
89 		if (!ticalcs_validate_flashcontent(content)) \
90 		{ \
91 			ticalcs_critical("%s: " #content " is NULL", __FUNCTION__); \
92 			return ERR_INVALID_PARAMETER; \
93 		} \
94 	} while(0);
95 #define VALIDATE_VARENTRY(var) \
96 	do \
97 	{ \
98 		if (!ticalcs_validate_varentry(var)) \
99 		{ \
100 			ticalcs_critical("%s: " #var " is NULL", __FUNCTION__); \
101 			return ERR_INVALID_PARAMETER; \
102 		} \
103 	} while(0);
104 #define VALIDATE_VARREQUEST(var) \
105 	do \
106 	{ \
107 		if (!ticalcs_validate_varrequest(var)) \
108 		{ \
109 			ticalcs_critical("%s: " #var " is NULL", __FUNCTION__); \
110 			return ERR_INVALID_PARAMETER; \
111 		} \
112 	} while(0);
113 #define VALIDATE_SCREENWIDTH(width) \
114 	do \
115 	{ \
116 		if (width > 320) \
117 		{ \
118 			ticalcs_critical("%s: no calculator model known to this library has screens of width > 320 pixels", __FUNCTION__); \
119 			return ERR_INVALID_PARAMETER; \
120 		} \
121 	} while(0);
122 #define VALIDATE_SCREENHEIGHT(height) \
123 	do \
124 	{ \
125 		if (height > 240) \
126 		{ \
127 			ticalcs_critical("%s: no calculator model known to this library has screens of height > 240 pixels", __FUNCTION__); \
128 			return ERR_INVALID_PARAMETER; \
129 		} \
130 	} while(0);
131 
132 #define RETURN_IF_HANDLE_NOT_ATTACHED(handle) \
133 	do \
134 	{ \
135 		if (!handle->attached) \
136 		{ \
137 			return ERR_NO_CABLE; \
138 		} \
139 	} while(0);
140 #define RETURN_IF_HANDLE_NOT_OPEN(handle) \
141 	do \
142 	{ \
143 		if (!handle->open) \
144 		{ \
145 			return ERR_NO_CABLE; \
146 		} \
147 	} while(0);
148 #define RETURN_IF_HANDLE_BUSY(handle) \
149 	do \
150 	{ \
151 		if (handle->busy) \
152 		{ \
153 			return ERR_BUSY; \
154 		} \
155 	} while(0);
156 
ticalcs_validate_handle(CalcHandle * handle)157 static inline int ticalcs_validate_handle(CalcHandle * handle)
158 {
159 	return handle != NULL;
160 }
161 
ticalcs_validate_calcfncts(const CalcFncts * calc)162 static inline int ticalcs_validate_calcfncts(const CalcFncts * calc)
163 {
164 	return calc != NULL;
165 }
166 
ticalcs_validate_backupcontent(BackupContent * content)167 static inline int ticalcs_validate_backupcontent(BackupContent * content)
168 {
169 	return content != NULL;
170 }
171 
ticalcs_validate_filecontent(FileContent * content)172 static inline int ticalcs_validate_filecontent(FileContent * content)
173 {
174 	return content != NULL;
175 }
176 
ticalcs_validate_filecontent_entries(FileContent * content)177 static inline int ticalcs_validate_filecontent_entries(FileContent * content)
178 {
179 	return content->num_entries == 0 || content->entries != NULL;
180 }
181 
ticalcs_validate_flashcontent(FlashContent * content)182 static inline int ticalcs_validate_flashcontent(FlashContent * content)
183 {
184 	return content != NULL;
185 }
186 
ticalcs_validate_varentry(VarEntry * var)187 static inline int ticalcs_validate_varentry(VarEntry * var)
188 {
189 	return var != NULL;
190 }
191 
ticalcs_validate_varrequest(VarRequest * var)192 static inline int ticalcs_validate_varrequest(VarRequest * var)
193 {
194 	return var != NULL;
195 }
196 
ticalcs_alloc_screen(size_t len)197 static inline void * ticalcs_alloc_screen(size_t len)
198 {
199 	return g_malloc(len);
200 }
201 
ticalcs_realloc_screen(void * mem,size_t len)202 static inline void * ticalcs_realloc_screen(void * mem, size_t len)
203 {
204 	return g_realloc(mem, len);
205 }
206 
dirlist_init_tree(CalcHandle * handle,GNode ** tree,const char * type)207 static inline int dirlist_init_tree(CalcHandle * handle, GNode ** tree, const char * type)
208 {
209 	int ret = ERR_MALLOC;
210 
211 	(*tree) = g_node_new(NULL);
212 	if (*tree != NULL)
213 	{
214 		TreeInfo *ti = (TreeInfo *)g_malloc(sizeof(TreeInfo));
215 		if (ti != NULL)
216 		{
217 			ti->model = handle->model;
218 			ti->type = type;
219 			(*tree)->data = ti;
220 			ret = 0;
221 		}
222 	}
223 
224 	return ret;
225 }
226 
dirlist_init_trees(CalcHandle * handle,GNode ** vars,GNode ** apps)227 static inline int dirlist_init_trees(CalcHandle * handle, GNode ** vars, GNode ** apps)
228 {
229 	int ret = dirlist_init_tree(handle, vars, VAR_NODE_NAME);
230 
231 	if (!ret)
232 	{
233 		ret = dirlist_init_tree(handle, apps, APP_NODE_NAME);
234 	}
235 
236 	return ret;
237 }
238 
dirlist_create_append_node(void * data,GNode ** tree)239 static inline GNode * dirlist_create_append_node(void * data, GNode ** tree)
240 {
241 	GNode * node = g_node_new(data);
242 	if (node != NULL)
243 	{
244 		g_node_append(*tree, node);
245 	}
246 	return node;
247 }
248 
249 #define ticalcs_slprintf(str, size, format, ...) \
250 	snprintf(str, size - 1, format, ##__VA_ARGS__); \
251 	str[size - 1] = 0;
252 
253 #define ticalcs_strlcpy(dst, src, size) \
254 	strncpy(dst, src, size - 1); \
255 	dst[size - 1] = 0;
256 
257 // backup.c
258 int tixx_recv_all_vars_backup(CalcHandle* handle, FileContent* content);
259 
260 
261 // calc_00.c
262 
263 int noop_is_ready (CalcHandle* handle);
264 int noop_send_key (CalcHandle* handle, uint32_t key);
265 int noop_execute (CalcHandle* handle, VarEntry *ve, const char* args);
266 int noop_recv_screen (CalcHandle* handle, CalcScreenCoord* sc, uint8_t** bitmap);
267 int noop_get_dirlist (CalcHandle* handle, GNode** vars, GNode** apps);
268 int noop_get_memfree (CalcHandle* handle, uint32_t* ram, uint32_t* flash);
269 int noop_send_backup (CalcHandle* handle, BackupContent* content);
270 int noop_recv_backup (CalcHandle* handle, BackupContent* content);
271 int noop_send_var (CalcHandle* handle, CalcMode mode, FileContent* content);
272 int noop_recv_var (CalcHandle* handle, CalcMode mode, FileContent* content, VarRequest* vr);
273 int noop_send_var_ns (CalcHandle* handle, CalcMode mode, FileContent* content);
274 int noop_recv_var_ns (CalcHandle* handle, CalcMode mode, FileContent* content, VarEntry** ve);
275 int noop_send_flash (CalcHandle* handle, FlashContent* content);
276 int noop_recv_flash (CalcHandle* handle, FlashContent* content, VarRequest* vr);
277 int noop_send_os (CalcHandle* handle, FlashContent* content);
278 int noop_recv_idlist (CalcHandle* handle, uint8_t* idlist);
279 int noop_dump_rom_1 (CalcHandle* handle);
280 int noop_dump_rom_2 (CalcHandle* handle, CalcDumpSize size, const char *filename);
281 int noop_set_clock (CalcHandle* handle, CalcClock* _clock);
282 int noop_get_clock (CalcHandle* handle, CalcClock* _clock);
283 int noop_del_var (CalcHandle* handle, VarRequest* vr);
284 int noop_new_folder (CalcHandle* handle, VarRequest* vr);
285 int noop_get_version (CalcHandle* handle, CalcInfos* infos);
286 int noop_send_cert (CalcHandle* handle, FlashContent* content);
287 int noop_recv_cert (CalcHandle* handle, FlashContent* content);
288 int noop_rename_var (CalcHandle* handle, VarRequest* oldname, VarRequest* newname);
289 int noop_change_attr (CalcHandle* handle, VarRequest* vr, FileAttr attr);
290 int noop_send_all_vars_backup (CalcHandle* handle, FileContent* content);
291 int noop_recv_all_vars_backup (CalcHandle* handle, FileContent* content);
292 
293 
294 // cmdz80.c
295 
296 int ti82_send_asm_exec(CalcHandle*, VarEntry * var);
297 
298 
299 // dusb_cmd.c
300 
301 int dusb_check_cmd_data(CalcModel model, const uint8_t * data, uint32_t len, uint32_t vtl_size, uint16_t vtl_type);
302 int dusb_dissect_cmd_data(CalcModel model, FILE *f, const uint8_t * data, uint32_t len, uint32_t vtl_size, uint16_t vtl_type);
303 
304 
305 // dusb_cmd.c
306 
307 #define CA(x)   (const DUSBCalcAttr **)(x)
308 #define CP(x)   (const DUSBCalcParam **)(x)
309 
310 #define VALIDATE_ATTRS(nattrs, attrs) \
311 	if (nattrs != 0 && attrs == NULL) \
312 	{ \
313 		ticalcs_critical("%s: " #attrs " is NULL", __FUNCTION__); \
314 		return ERR_INVALID_PARAMETER; \
315 	}
316 
317 #endif // __TICALCS_INTERNAL__
318