1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  libticalcs2 - hand-helds support library, a part of the TiLP project
5  *  Copyright (C) 1999-2005  Romain Liévin
6  *  Copyright (C) 2006  Kevin Kofler
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software Foundation,
20  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "ticalcs.h"
32 #include "internal.h"
33 #include "gettext.h"
34 #include "logging.h"
35 #include "error.h"
36 
37 /**
38  * ticalcs_calc_features:
39  * @handle: a previously allocated handle
40  *
41  * Returns the features and operations supported by the hand-held.
42  *
43  * Return value: a mask of features (CalcFeatures).
44  **/
ticalcs_calc_features(CalcHandle * handle)45 TIEXPORT3 CalcFeatures TICALL ticalcs_calc_features(CalcHandle* handle)
46 {
47 	const CalcFncts *calc;
48 
49 	if (handle == NULL)
50 	{
51 		ticalcs_critical("%s: handle is NULL", __FUNCTION__);
52 		return FTS_NONE;
53 	}
54 
55 	calc = handle->calc;
56 	if (!calc)
57 	{
58 		return FTS_NONE;
59 	}
60 
61 	return calc->features;
62 }
63 
64 /**
65  * ticalcs_calc_isready:
66  * @handle: a previously allocated handle
67  *
68  * Check whether calc is ready.
69  *
70  * NOTE: after connecting to the calculator, you usually want to use this command once,
71  * before attempting to use any other commands. This is required on some models (protocols).
72  * There are exceptions to this general rule, especially when a DUSB calculator (84+ family, 89T)
73  * is in the OS receive mode, where the Ready check cannot be performed.
74  *
75  * Return value: 0 if ready else ERR_NOT_READY.
76  **/
ticalcs_calc_isready(CalcHandle * handle)77 TIEXPORT3 int TICALL ticalcs_calc_isready(CalcHandle* handle)
78 {
79 	const CalcFncts *calc;
80 	int ret = 0;
81 
82 	VALIDATE_HANDLE(handle);
83 
84 	calc = handle->calc;
85 	VALIDATE_CALCFNCTS(calc);
86 
87 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
88 	RETURN_IF_HANDLE_NOT_OPEN(handle);
89 	RETURN_IF_HANDLE_BUSY(handle);
90 
91 	ticalcs_info(_("Checking hand-held status:"));
92 	handle->busy = 1;
93 	if (calc->is_ready)
94 	{
95 		ret = calc->is_ready(handle);
96 	}
97 	handle->busy = 0;
98 
99 	return ret;
100 }
101 
102 /**
103  * ticalcs_calc_send_key:
104  * @handle: a previously allocated handle
105  * @key: a TI scancode
106  *
107  * Check whether calc is ready.
108  *
109  * Return value: 0 if successful, an error code otherwise.
110  **/
ticalcs_calc_send_key(CalcHandle * handle,uint32_t key)111 TIEXPORT3 int TICALL ticalcs_calc_send_key(CalcHandle* handle, uint32_t key)
112 {
113 	const CalcFncts *calc;
114 	int ret = 0;
115 
116 	VALIDATE_HANDLE(handle);
117 
118 	calc = handle->calc;
119 	VALIDATE_CALCFNCTS(calc);
120 
121 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
122 	RETURN_IF_HANDLE_NOT_OPEN(handle);
123 	RETURN_IF_HANDLE_BUSY(handle);
124 
125 	ticalcs_info(_("Sending key %08x:"), key);
126 	handle->busy = 1;
127 	if (calc->send_key)
128 	{
129 		ret = calc->send_key(handle, key);
130 	}
131 	handle->busy = 0;
132 
133 	return ret;
134 }
135 
136 /**
137  * ticalcs_calc_execute:
138  * @handle: a previously allocated handle
139  * @ve: folder and variable name with type
140  * @args: argument to pass to program (in TI-charset, aka native)
141  *
142  * Remotely execute a program or a FLASH application.
143  * Restrictions: execution of FLASH applications is supported thru D-USB only.
144  *
145  * Return value: 0 if successful, an error code otherwise.
146  **/
ticalcs_calc_execute(CalcHandle * handle,VarEntry * ve,const char * args)147 TIEXPORT3 int TICALL ticalcs_calc_execute(CalcHandle* handle, VarEntry* ve, const char* args)
148 {
149 	const CalcFncts *calc;
150 	int ret = 0;
151 
152 	VALIDATE_HANDLE(handle);
153 	VALIDATE_VARENTRY(ve);
154 
155 	calc = handle->calc;
156 	VALIDATE_CALCFNCTS(calc);
157 
158 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
159 	RETURN_IF_HANDLE_NOT_OPEN(handle);
160 	RETURN_IF_HANDLE_BUSY(handle);
161 
162 	ticalcs_info(_("Executing %s/%s with %s:"), ve->folder, ve->name, args);
163 	handle->busy = 1;
164 	if (calc->execute)
165 	{
166 		ret = calc->execute(handle, ve, args);
167 	}
168 	handle->busy = 0;
169 
170 	return ret;
171 }
172 
173 /**
174  * ticalcs_calc_recv_screen:
175  * @handle: a previously allocated handle
176  * @sc: a structure which contains required screen format and returns screen sizes
177  * @bitmap: adress of pointer for allocated bitmap. Must be freed when no longer needed.
178  *
179  * Request a screenshot and receive a raw bitmap in the calculator's native format (depending on the model, 1 bit B/W, 4-bit grayscale, 16-bit color).
180  *
181  * Return value: 0 if successful, an error code otherwise.
182  **/
ticalcs_calc_recv_screen(CalcHandle * handle,CalcScreenCoord * sc,uint8_t ** bitmap)183 TIEXPORT3 int TICALL ticalcs_calc_recv_screen(CalcHandle* handle, CalcScreenCoord* sc, uint8_t** bitmap)
184 {
185 	const CalcFncts *calc;
186 	int ret = 0;
187 
188 	VALIDATE_HANDLE(handle);
189 	VALIDATE_NONNULL(sc);
190 	VALIDATE_NONNULL(bitmap);
191 
192 	calc = handle->calc;
193 	VALIDATE_CALCFNCTS(calc);
194 
195 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
196 	RETURN_IF_HANDLE_NOT_OPEN(handle);
197 	RETURN_IF_HANDLE_BUSY(handle);
198 
199 	ticalcs_info(_("Requesting screenshot:"));
200 	handle->busy = 1;
201 	if (calc->recv_screen)
202 	{
203 		ret = calc->recv_screen(handle, sc, bitmap);
204 	}
205 	handle->busy = 0;
206 
207 	return ret;
208 }
209 
210 /**
211  * ticalcs_calc_recv_screen_rgb888:
212  * @handle: a previously allocated handle
213  * @sc: a structure which contains required screen format and returns screen sizes
214  * @bitmap: adress of pointer for allocated bitmap. Must be freed when no longer needed.
215  *
216  * Request a screenshot and receive a RGB888 bitmap (convenience function).
217  *
218  * Return value: 0 if successful, an error code otherwise.
219  **/
ticalcs_calc_recv_screen_rgb888(CalcHandle * handle,CalcScreenCoord * sc,uint8_t ** bitmap)220 TIEXPORT3 int TICALL ticalcs_calc_recv_screen_rgb888(CalcHandle* handle, CalcScreenCoord* sc, uint8_t** bitmap)
221 {
222 	int ret = ticalcs_calc_recv_screen(handle, sc, bitmap);
223 
224 	if (!ret)
225 	{
226 		uint8_t * bitmap2 = NULL;
227 
228 		if (sc->width > 320)
229 		{
230 			ticalcs_critical("%s: no calculator model known to this library has screens of width > 320 pixels", __FUNCTION__);
231 			ticalcs_free_screen(*bitmap);
232 			ret = ERR_INVALID_PARAMETER;
233 		}
234 		else if (sc->height > 240)
235 		{
236 			ticalcs_critical("%s: no calculator model known to this library has screens of height > 240 pixels", __FUNCTION__);
237 			ticalcs_free_screen(*bitmap);
238 			ret = ERR_INVALID_PARAMETER;
239 		}
240 		else
241 		{
242 			bitmap2 = ticalcs_alloc_screen(3 * sc->width * sc->height);
243 			ret = ticalcs_screen_convert_native_to_rgb888(sc->pixel_format, *bitmap, sc->width, sc->height, bitmap2);
244 			if (ret)
245 			{
246 				ticalcs_free_screen(bitmap2);
247 				bitmap2 = NULL;
248 			}
249 		}
250 		*bitmap = bitmap2;
251 	}
252 
253 	return ret;
254 }
255 
256 /**
257  * ticalcs_free_screen:
258  * @bitmap: a previously allocated bitmap
259  *
260  * Frees a bitmap previously allocated by ticalcs_calc_recv_screen() or ticalcs_calc_recv_screen_rgb888().
261  */
ticalcs_free_screen(uint8_t * bitmap)262 TIEXPORT3 void TICALL ticalcs_free_screen(uint8_t * bitmap)
263 {
264 	g_free((void *)bitmap);
265 }
266 
267 /**
268  * ticalcs_calc_get_dirlist:
269  * @handle: a previously allocated handle
270  * @vars: a tree of folder & variables
271  * @apps: a tree of FLASH apps
272  *
273  * Request a directory listing.
274  *
275  * Return value: 0 if successful, an error code otherwise.
276  **/
ticalcs_calc_get_dirlist(CalcHandle * handle,GNode ** vars,GNode ** apps)277 TIEXPORT3 int TICALL ticalcs_calc_get_dirlist(CalcHandle* handle, GNode** vars, GNode **apps)
278 {
279 	const CalcFncts *calc;
280 	int ret = 0;
281 	TreeInfo *ti;
282 
283 	VALIDATE_HANDLE(handle);
284 	VALIDATE_NONNULL(vars);
285 	VALIDATE_NONNULL(apps);
286 
287 	calc = handle->calc;
288 	VALIDATE_CALCFNCTS(calc);
289 
290 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
291 	RETURN_IF_HANDLE_NOT_OPEN(handle);
292 	RETURN_IF_HANDLE_BUSY(handle);
293 
294 	*vars = NULL;
295 	*apps = NULL;
296 
297 	ticalcs_info(_("Requesting folder & vars & apps listing:"));
298 	handle->busy = 1;
299 	if (calc->get_dirlist)
300 	{
301 		ret = calc->get_dirlist(handle, vars, apps);
302 	}
303 	handle->busy = 0;
304 
305 	if (!ret)
306 	{
307 		if (*vars != NULL)
308 		{
309 			ti = (*vars)->data;
310 			ti->mem_mask |= MEMORY_USED;
311 			ti->mem_used = ticalcs_dirlist_ram_used(*vars);
312 		}
313 
314 		if (*apps != NULL)
315 		{
316 			ti = (*apps)->data;
317 			ti->mem_mask |= MEMORY_USED;
318 			ti->mem_used = ticalcs_dirlist_flash_used(*vars, *apps);
319 		}
320 	}
321 
322 	return ret;
323 }
324 
325 /**
326  * ticalcs_calc_get_memfree:
327  * @handle: a previously allocated handle
328  * @ram: RAM memory available
329  * @flash: Flash memory available
330  *
331  * Request free memory. Do a dirlist to update value.
332  *
333  * Return value: 0 if successful, an error code otherwise.
334  **/
ticalcs_calc_get_memfree(CalcHandle * handle,uint32_t * ram,uint32_t * flash)335 TIEXPORT3 int TICALL ticalcs_calc_get_memfree(CalcHandle* handle, uint32_t* ram, uint32_t *flash)
336 {
337 	const CalcFncts *calc;
338 	int ret = 0;
339 
340 	VALIDATE_HANDLE(handle);
341 	VALIDATE_NONNULL(ram);
342 	VALIDATE_NONNULL(flash);
343 
344 	calc = handle->calc;
345 	VALIDATE_CALCFNCTS(calc);
346 
347 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
348 	RETURN_IF_HANDLE_NOT_OPEN(handle);
349 	RETURN_IF_HANDLE_BUSY(handle);
350 
351 	ticalcs_info(_("Requesting RAM & FLASH free"));
352 	handle->busy = 1;
353 	if (calc->get_memfree)
354 	{
355 		ret = calc->get_memfree(handle, ram, flash);
356 	}
357 	handle->busy = 0;
358 
359 	return ret;
360 }
361 
362 /**
363  * ticalcs_calc_send_backup:
364  * @handle: a previously allocated handle
365  * @content: backup content
366  *
367  * Send a backup.
368  *
369  * Return value: 0 if successful, an error code otherwise.
370  **/
ticalcs_calc_send_backup(CalcHandle * handle,BackupContent * content)371 TIEXPORT3 int TICALL ticalcs_calc_send_backup(CalcHandle* handle, BackupContent* content)
372 {
373 	const CalcFncts *calc;
374 	int ret = 0;
375 
376 	VALIDATE_HANDLE(handle);
377 	VALIDATE_BACKUPCONTENT(content);
378 
379 	calc = handle->calc;
380 	VALIDATE_CALCFNCTS(calc);
381 
382 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
383 	RETURN_IF_HANDLE_NOT_OPEN(handle);
384 	RETURN_IF_HANDLE_BUSY(handle);
385 
386 	ticalcs_info(_("Sending backup:"));
387 	handle->busy = 1;
388 	if (calc->send_backup)
389 	{
390 		ret = calc->send_backup(handle, content);
391 	}
392 	handle->busy = 0;
393 
394 	return ret;
395 }
396 
397 /**
398  * ticalcs_calc_recv_backup:
399  * @handle: a previously allocated handle
400  * @content: backup content
401  *
402  * Request a backup and receive it.
403  *
404  * Return value: 0 if successful, an error code otherwise.
405  **/
ticalcs_calc_recv_backup(CalcHandle * handle,BackupContent * content)406 TIEXPORT3 int TICALL ticalcs_calc_recv_backup(CalcHandle* handle, BackupContent* content)
407 {
408 	const CalcFncts *calc;
409 	int ret = 0;
410 
411 	VALIDATE_HANDLE(handle);
412 	VALIDATE_BACKUPCONTENT(content);
413 
414 	calc = handle->calc;
415 	VALIDATE_CALCFNCTS(calc);
416 
417 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
418 	RETURN_IF_HANDLE_NOT_OPEN(handle);
419 	RETURN_IF_HANDLE_BUSY(handle);
420 
421 	ticalcs_info(_("Requesting backup:"));
422 	handle->busy = 1;
423 	if (calc->recv_backup)
424 	{
425 		ret = calc->recv_backup(handle, content);
426 	}
427 	handle->busy = 0;
428 
429 	return ret;
430 }
431 
432 /**
433  * ticalcs_calc_send_var:
434  * @handle: a previously allocated handle
435  * @mode: to document
436  * @content: file content to send
437  *
438  * Send one or more variables (silent mode).
439  *
440  * Return value: 0 if successful, an error code otherwise.
441  **/
ticalcs_calc_send_var(CalcHandle * handle,CalcMode mode,FileContent * content)442 TIEXPORT3 int TICALL ticalcs_calc_send_var(CalcHandle* handle, CalcMode mode, FileContent* content)
443 {
444 	const CalcFncts *calc;
445 	int ret = 0;
446 
447 	VALIDATE_HANDLE(handle);
448 	VALIDATE_FILECONTENT(content);
449 	VALIDATE_FILECONTENT_ENTRIES(content);
450 
451 	calc = handle->calc;
452 	VALIDATE_CALCFNCTS(calc);
453 
454 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
455 	RETURN_IF_HANDLE_NOT_OPEN(handle);
456 	RETURN_IF_HANDLE_BUSY(handle);
457 
458 	ticalcs_info(_("Sending one or more variables:"));
459 	handle->busy = 1;
460 	if (calc->send_var)
461 	{
462 		ret = calc->send_var(handle, mode, content);
463 	}
464 	handle->busy = 0;
465 
466 	return ret;
467 }
468 
469 /**
470  * ticalcs_calc_recv_var:
471  * @handle: a previously allocated handle
472  * @mode:
473  * @content: where to store variable content
474  * @var: a #VarRequest structure got with dirlist
475  *
476  * Request receiving of _one_ variable (silent mode).
477  *
478  * Return value: 0 if successful, an error code otherwise.
479  **/
ticalcs_calc_recv_var(CalcHandle * handle,CalcMode mode,FileContent * content,VarRequest * vr)480 TIEXPORT3 int TICALL ticalcs_calc_recv_var(CalcHandle* handle, CalcMode mode, FileContent* content, VarRequest* vr)
481 {
482 	const CalcFncts *calc;
483 	int ret = 0;
484 
485 	VALIDATE_HANDLE(handle);
486 	VALIDATE_FILECONTENT(content);
487 	VALIDATE_VARREQUEST(vr);
488 
489 	calc = handle->calc;
490 	VALIDATE_CALCFNCTS(calc);
491 
492 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
493 	RETURN_IF_HANDLE_NOT_OPEN(handle);
494 	RETURN_IF_HANDLE_BUSY(handle);
495 
496 	ticalcs_info(_("Requesting variable '%s':"), vr->name);
497 	handle->busy = 1;
498 	if (calc->recv_var)
499 	{
500 		ret = calc->recv_var(handle, mode, content, vr);
501 	}
502 	handle->busy = 0;
503 
504 	return ret;
505 }
506 
507 /**
508  * ticalcs_calc_send_var_ns:
509  * @handle: a previously allocated handle
510  * @mode:
511  * @content: file content to send
512  *
513  * Send one or more variable (non-silent mode).
514  *
515  * Return value: 0 if successful, an error code otherwise.
516  **/
ticalcs_calc_send_var_ns(CalcHandle * handle,CalcMode mode,FileContent * content)517 TIEXPORT3 int TICALL ticalcs_calc_send_var_ns(CalcHandle* handle, CalcMode mode, FileContent* content)
518 {
519 	const CalcFncts *calc;
520 	int ret = 0;
521 
522 	VALIDATE_HANDLE(handle);
523 	VALIDATE_FILECONTENT(content);
524 	VALIDATE_FILECONTENT_ENTRIES(content);
525 
526 	calc = handle->calc;
527 	VALIDATE_CALCFNCTS(calc);
528 
529 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
530 	RETURN_IF_HANDLE_NOT_OPEN(handle);
531 	RETURN_IF_HANDLE_BUSY(handle);
532 
533 	ticalcs_info(_("Sending variable (non-silent mode):"));
534 	handle->busy = 1;
535 	if (calc->send_var_ns)
536 	{
537 		ret = calc->send_var_ns(handle, mode, content);
538 	}
539 	handle->busy = 0;
540 
541 	return ret;
542 }
543 
544 /**
545  * ticalcs_calc_recv_var_ns:
546  * @handle: a previously allocated handle
547  * @mode:
548  * @content: where to store variables
549  * @var: information on the received variable (if single) or NULL (if group)
550  *
551  * Receive one or more variable (non-silent mode).
552  *
553  * Return value: 0 if successful, an error code otherwise.
554  **/
ticalcs_calc_recv_var_ns(CalcHandle * handle,CalcMode mode,FileContent * content,VarEntry ** var)555 TIEXPORT3 int TICALL ticalcs_calc_recv_var_ns(CalcHandle* handle, CalcMode mode, FileContent* content, VarEntry** var)
556 {
557 	const CalcFncts *calc;
558 	int ret = 0;
559 
560 	VALIDATE_HANDLE(handle);
561 	VALIDATE_FILECONTENT(content);
562 	VALIDATE_NONNULL(var);
563 
564 	calc = handle->calc;
565 	VALIDATE_CALCFNCTS(calc);
566 
567 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
568 	RETURN_IF_HANDLE_NOT_OPEN(handle);
569 	RETURN_IF_HANDLE_BUSY(handle);
570 
571 	ticalcs_info(_("Receiving variable (non-silent mode):"));
572 	handle->busy = 1;
573 	if (calc->recv_var_ns)
574 	{
575 		ret = calc->recv_var_ns(handle, mode, content, var);
576 	}
577 	handle->busy = 0;
578 
579 	return ret;
580 }
581 
582 /**
583  * ticalcs_calc_send_app:
584  * @handle: a previously allocated handle
585  * @content: content to send
586  *
587  * Send a FLASH app.
588  *
589  * Return value: 0 if successful, an error code otherwise.
590  **/
ticalcs_calc_send_app(CalcHandle * handle,FlashContent * content)591 TIEXPORT3 int TICALL ticalcs_calc_send_app(CalcHandle* handle, FlashContent* content)
592 {
593 	const CalcFncts *calc;
594 	int ret = 0;
595 
596 	VALIDATE_HANDLE(handle);
597 	VALIDATE_FLASHCONTENT(content);
598 
599 	calc = handle->calc;
600 	VALIDATE_CALCFNCTS(calc);
601 
602 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
603 	RETURN_IF_HANDLE_NOT_OPEN(handle);
604 	RETURN_IF_HANDLE_BUSY(handle);
605 
606 	ticalcs_info(_("Sending FLASH application:"));
607 	handle->busy = 1;
608 	if (calc->send_app)
609 	{
610 		ret = calc->send_app(handle, content);
611 	}
612 	handle->busy = 0;
613 
614 	return ret;
615 }
616 
617 /**
618  * ticalcs_calc_recv_app:
619  * @handle: a previously allocated handle
620  * @content: where to store content
621  * @var: FLASH app to request
622  *
623  * Request receiving of a FLASH app.
624  *
625  * Return value: 0 if successful, an error code otherwise.
626  **/
ticalcs_calc_recv_app(CalcHandle * handle,FlashContent * content,VarRequest * vr)627 TIEXPORT3 int TICALL ticalcs_calc_recv_app(CalcHandle* handle, FlashContent* content, VarRequest* vr)
628 {
629 	const CalcFncts *calc;
630 	int ret = 0;
631 
632 	VALIDATE_HANDLE(handle);
633 	VALIDATE_FLASHCONTENT(content);
634 	VALIDATE_VARREQUEST(vr);
635 
636 	calc = handle->calc;
637 	VALIDATE_CALCFNCTS(calc);
638 
639 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
640 	RETURN_IF_HANDLE_NOT_OPEN(handle);
641 	RETURN_IF_HANDLE_BUSY(handle);
642 
643 	ticalcs_info(_("Requesting receiving of FLASH application:"));
644 	handle->busy = 1;
645 	if (calc->recv_app)
646 	{
647 		ret = calc->recv_app(handle, content, vr);
648 	}
649 	handle->busy = 0;
650 
651 	return ret;
652 }
653 
654 /**
655  * ticalcs_calc_send_os:
656  * @handle: a previously allocated handle
657  * @content: content to send
658  *
659  * Send a FLASH os.
660  *
661  * Return value: 0 if successful, an error code otherwise.
662  **/
ticalcs_calc_send_os(CalcHandle * handle,FlashContent * content)663 TIEXPORT3 int TICALL ticalcs_calc_send_os(CalcHandle* handle, FlashContent* content)
664 {
665 	const CalcFncts *calc;
666 	int ret = 0;
667 
668 	VALIDATE_HANDLE(handle);
669 	VALIDATE_FLASHCONTENT(content);
670 
671 	calc = handle->calc;
672 	VALIDATE_CALCFNCTS(calc);
673 
674 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
675 	RETURN_IF_HANDLE_NOT_OPEN(handle);
676 	RETURN_IF_HANDLE_BUSY(handle);
677 
678 	ticalcs_info(_("Sending FLASH os:"));
679 	handle->busy = 1;
680 	if (calc->send_os)
681 	{
682 		ret = calc->send_os(handle, content);
683 	}
684 	handle->busy = 0;
685 
686 	return ret;
687 }
688 
689 /**
690  * ticalcs_calc_recv_idlist:
691  * @handle: a previously allocated handle
692  * @idlist: static buffer (32 chars) where to store ID-LIST
693  *
694  * Request ID-LIST of hand-held.
695  *
696  * Return value: 0 if successful, an error code otherwise.
697  **/
ticalcs_calc_recv_idlist(CalcHandle * handle,uint8_t * idlist)698 TIEXPORT3 int TICALL ticalcs_calc_recv_idlist(CalcHandle* handle, uint8_t* idlist)
699 {
700 	const CalcFncts *calc;
701 	int ret = 0;
702 
703 	VALIDATE_HANDLE(handle);
704 	VALIDATE_NONNULL(idlist);
705 
706 	calc = handle->calc;
707 	VALIDATE_CALCFNCTS(calc);
708 
709 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
710 	RETURN_IF_HANDLE_NOT_OPEN(handle);
711 	RETURN_IF_HANDLE_BUSY(handle);
712 
713 	ticalcs_info(_("Requesting ID-LIST:"));
714 	handle->busy = 1;
715 	if (calc->recv_idlist)
716 	{
717 		ret = calc->recv_idlist(handle, idlist);
718 	}
719 	handle->busy = 0;
720 
721 	return ret;
722 }
723 
724 /**
725  * ticalcs_calc_dump_rom_1:
726  * @handle: a previously allocated handle
727  *
728  * Send a ROM dumping program to hand-held.
729  *
730  * Return value: 0 if successful, an error code otherwise.
731  **/
ticalcs_calc_dump_rom_1(CalcHandle * handle)732 TIEXPORT3 int TICALL ticalcs_calc_dump_rom_1(CalcHandle* handle)
733 {
734 	const CalcFncts *calc;
735 	int ret = 0;
736 
737 	VALIDATE_HANDLE(handle);
738 
739 	calc = handle->calc;
740 	VALIDATE_CALCFNCTS(calc);
741 
742 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
743 	RETURN_IF_HANDLE_NOT_OPEN(handle);
744 	RETURN_IF_HANDLE_BUSY(handle);
745 
746 	ticalcs_info(_("Sending ROM dumper:"));
747 	handle->busy = 1;
748 	if (calc->dump_rom_1)
749 	{
750 		ret = calc->dump_rom_1(handle);
751 	}
752 	handle->busy = 0;
753 
754 	return ret;
755 }
756 
757 /**
758  * ticalcs_calc_dump_rom_2:
759  * @handle: a previously allocated handle
760  * @size: optional size of dump
761  * @filename: where to store the dump
762  *
763  * Start dumping (if possible).
764  *
765  * Return value: 0 if successful, an error code otherwise.
766  **/
ticalcs_calc_dump_rom_2(CalcHandle * handle,CalcDumpSize size,const char * filename)767 TIEXPORT3 int TICALL ticalcs_calc_dump_rom_2(CalcHandle* handle, CalcDumpSize size, const char *filename)
768 {
769 	const CalcFncts *calc;
770 	int ret = 0;
771 
772 	VALIDATE_HANDLE(handle);
773 	VALIDATE_NONNULL(filename);
774 
775 	calc = handle->calc;
776 	VALIDATE_CALCFNCTS(calc);
777 
778 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
779 	RETURN_IF_HANDLE_NOT_OPEN(handle);
780 	RETURN_IF_HANDLE_BUSY(handle);
781 
782 	ticalcs_info(_("Dumping ROM:"));
783 	handle->busy = 1;
784 	if (calc->dump_rom_2)
785 	{
786 		ret = calc->dump_rom_2(handle, size, filename);
787 	}
788 	handle->busy = 0;
789 
790 	return ret;
791 }
792 
793 /**
794  * ticalcs_calc_set_clock:
795  * @handle: a previously allocated handle
796  * @clock: a #CalcClock structure
797  *
798  * Set date & time of hand-held (if AMS >= 2.09).
799  *
800  * Return value: 0 if successful, an error code otherwise.
801  **/
ticalcs_calc_set_clock(CalcHandle * handle,CalcClock * _clock)802 TIEXPORT3 int TICALL ticalcs_calc_set_clock(CalcHandle* handle, CalcClock* _clock)
803 {
804 	const CalcFncts *calc;
805 	int ret = 0;
806 
807 	VALIDATE_HANDLE(handle);
808 	VALIDATE_NONNULL(_clock);
809 
810 	calc = handle->calc;
811 	VALIDATE_CALCFNCTS(calc);
812 
813 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
814 	RETURN_IF_HANDLE_NOT_OPEN(handle);
815 	RETURN_IF_HANDLE_BUSY(handle);
816 
817 	ticalcs_info(_("Setting clock:"));
818 	handle->busy = 1;
819 	if (calc->set_clock)
820 	{
821 		ret = calc->set_clock(handle, _clock);
822 	}
823 	handle->busy = 0;
824 
825 	return ret;
826 }
827 
828 /**
829  * ticalcs_calc_get_clock:
830  * @handle: a previously allocated handle
831  * @clock: a #CalcClock structure
832  *
833  * Get date & time of hand-held (if AMS >= 2.09).
834  *
835  * Return value: 0 if successful, an error code otherwise.
836  **/
ticalcs_calc_get_clock(CalcHandle * handle,CalcClock * _clock)837 TIEXPORT3 int TICALL ticalcs_calc_get_clock(CalcHandle* handle, CalcClock* _clock)
838 {
839 	const CalcFncts *calc;
840 	int ret = 0;
841 
842 	VALIDATE_HANDLE(handle);
843 	VALIDATE_NONNULL(_clock);
844 
845 	calc = handle->calc;
846 	VALIDATE_CALCFNCTS(calc);
847 
848 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
849 	RETURN_IF_HANDLE_NOT_OPEN(handle);
850 	RETURN_IF_HANDLE_BUSY(handle);
851 
852 	ticalcs_info(_("Getting clock:"));
853 	handle->busy = 1;
854 	if (calc->get_clock)
855 	{
856 		ret = calc->get_clock(handle, _clock);
857 	}
858 	handle->busy = 0;
859 
860 	return ret;
861 }
862 
863 /**
864  * ticalcs_calc_del_var:
865  * @handle: a previously allocated handle
866  * @var: var to delete
867  *
868  * Request deleting of a variable (if possible).
869  *
870  * Return value: 0 if successful, an error code otherwise.
871  **/
ticalcs_calc_del_var(CalcHandle * handle,VarRequest * vr)872 TIEXPORT3 int TICALL ticalcs_calc_del_var(CalcHandle* handle, VarRequest* vr)
873 {
874 	const CalcFncts *calc;
875 	int ret = 0;
876 
877 	VALIDATE_HANDLE(handle);
878 	VALIDATE_VARREQUEST(vr);
879 
880 	calc = handle->calc;
881 	VALIDATE_CALCFNCTS(calc);
882 
883 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
884 	RETURN_IF_HANDLE_NOT_OPEN(handle);
885 	RETURN_IF_HANDLE_BUSY(handle);
886 
887 	ticalcs_info(_("Deleting variable '%s':"), vr->name);
888 	handle->busy = 1;
889 	if (calc->del_var)
890 	{
891 		ret = calc->del_var(handle, vr);
892 	}
893 	handle->busy = 0;
894 
895 	return ret;
896 }
897 
898 /**
899  * ticalcs_calc_new_fld:
900  * @handle: a previously allocated handle
901  * @vr: name of folder to create (vr->folder)
902  *
903  * Request creation of a folder. Beware: %vr.name may be modified !
904  *
905  * Return value: 0 if successful, an error code otherwise.
906  **/
ticalcs_calc_new_fld(CalcHandle * handle,VarRequest * vr)907 TIEXPORT3 int TICALL ticalcs_calc_new_fld(CalcHandle* handle, VarRequest* vr)
908 {
909 	const CalcFncts *calc;
910 	int ret = 0;
911 
912 	VALIDATE_HANDLE(handle);
913 	VALIDATE_VARREQUEST(vr);
914 
915 	calc = handle->calc;
916 	VALIDATE_CALCFNCTS(calc);
917 
918 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
919 	RETURN_IF_HANDLE_NOT_OPEN(handle);
920 	RETURN_IF_HANDLE_BUSY(handle);
921 
922 	ticalcs_info(_("Creating folder '%s':"), vr->folder);
923 	handle->busy = 1;
924 	if (calc->new_fld)
925 	{
926 		ret = calc->new_fld(handle, vr);
927 	}
928 	handle->busy = 0;
929 
930 	return ret;
931 }
932 
933 /**
934  * ticalcs_calc_get_version:
935  * @handle: a previously allocated handle
936  * @infos: where to store version information
937  *
938  * Request version info.
939  *
940  * Return value: 0 if successful, an error code otherwise.
941  **/
ticalcs_calc_get_version(CalcHandle * handle,CalcInfos * infos)942 TIEXPORT3 int TICALL ticalcs_calc_get_version(CalcHandle* handle, CalcInfos* infos)
943 {
944 	const CalcFncts *calc;
945 	int ret = 0;
946 
947 	VALIDATE_HANDLE(handle);
948 	VALIDATE_NONNULL(infos);
949 
950 	calc = handle->calc;
951 	VALIDATE_CALCFNCTS(calc);
952 
953 	//RETURN_IF_HANDLE_NOT_ATTACHED(handle); // Disabled after '2005 probing changes.
954 	RETURN_IF_HANDLE_NOT_OPEN(handle);
955 	RETURN_IF_HANDLE_BUSY(handle);
956 
957 	ticalcs_info(_("Requesting version info:"));
958 	handle->busy = 1;
959 	if (calc->get_version)
960 	{
961 		ret = calc->get_version(handle, infos);
962 	}
963 	handle->busy = 0;
964 
965 	return ret;
966 }
967 
968 /**
969  * ticalcs_calc_send_cert:
970  * @handle: a previously allocated handle
971  * @content: content to send
972  *
973  * Send a certificate.
974  *
975  * Return value: 0 if successful, an error code otherwise.
976  **/
ticalcs_calc_send_cert(CalcHandle * handle,FlashContent * content)977 TIEXPORT3 int TICALL ticalcs_calc_send_cert(CalcHandle* handle, FlashContent* content)
978 {
979 	const CalcFncts *calc;
980 	int ret = 0;
981 
982 	VALIDATE_HANDLE(handle);
983 	VALIDATE_FLASHCONTENT(content);
984 
985 	calc = handle->calc;
986 	VALIDATE_CALCFNCTS(calc);
987 
988 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
989 	RETURN_IF_HANDLE_NOT_OPEN(handle);
990 	RETURN_IF_HANDLE_BUSY(handle);
991 
992 	ticalcs_info(_("Sending certificate:"));
993 	handle->busy = 1;
994 	if (calc->send_cert)
995 	{
996 		ret = calc->send_cert(handle, content);
997 	}
998 	handle->busy = 0;
999 
1000 	return ret;
1001 }
1002 
1003 /**
1004  * ticalcs_calc_recv_cert:
1005  * @handle: a previously allocated handle
1006  * @content: where to store content
1007  *
1008  * Request receiving of a FLASH app.
1009  *
1010  * Return value: 0 if successful, an error code otherwise.
1011  **/
ticalcs_calc_recv_cert(CalcHandle * handle,FlashContent * content)1012 TIEXPORT3 int TICALL ticalcs_calc_recv_cert(CalcHandle* handle, FlashContent* content)
1013 {
1014 	const CalcFncts *calc;
1015 	int ret = 0;
1016 
1017 	VALIDATE_HANDLE(handle);
1018 	VALIDATE_FLASHCONTENT(content);
1019 
1020 	calc = handle->calc;
1021 	VALIDATE_CALCFNCTS(calc);
1022 
1023 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1024 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1025 	RETURN_IF_HANDLE_BUSY(handle);
1026 
1027 	ticalcs_info(_("Requesting receiving of certificate:"));
1028 	handle->busy = 1;
1029 	if (calc->recv_cert)
1030 	{
1031 		ret = calc->recv_cert(handle, content);
1032 	}
1033 	handle->busy = 0;
1034 
1035 	return ret;
1036 }
1037 
1038 /**
1039  * ticalcs_calc_rename_var:
1040  * @handle: a previously allocated handle
1041  * @oldname: existing name and folder
1042  * @newname: new name and folder
1043  *
1044  * Remotely rename a variable, and/or move it to a different folder.
1045  *
1046  * Return value: 0 if successful, an error code otherwise.
1047  **/
ticalcs_calc_rename_var(CalcHandle * handle,VarRequest * oldname,VarRequest * newname)1048 TIEXPORT3 int TICALL ticalcs_calc_rename_var(CalcHandle* handle, VarRequest* oldname, VarRequest *newname)
1049 {
1050 	const CalcFncts *calc;
1051 	int ret = 0;
1052 
1053 	VALIDATE_HANDLE(handle);
1054 	VALIDATE_VARREQUEST(oldname);
1055 	VALIDATE_VARREQUEST(newname);
1056 
1057 	calc = handle->calc;
1058 	VALIDATE_CALCFNCTS(calc);
1059 
1060 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1061 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1062 	RETURN_IF_HANDLE_BUSY(handle);
1063 
1064 	if (oldname->folder[0] && newname->folder[0])
1065 	{
1066 		ticalcs_info(_("Renaming variable '%s/%s' to '%s/%s':"), oldname->folder, oldname->name, newname->folder, newname->name);
1067 	}
1068 	else
1069 	{
1070 		ticalcs_info(_("Renaming variable '%s' to '%s':"), oldname->name, newname->name);
1071 	}
1072 
1073 	handle->busy = 1;
1074 	if (calc->rename_var)
1075 	{
1076 		ret = calc->rename_var(handle, oldname, newname);
1077 	}
1078 	handle->busy = 0;
1079 
1080 	return ret;
1081 }
1082 
1083 /**
1084  * ticalcs_calc_change_attr:
1085  * @handle: a previously allocated handle
1086  * @vr: variable to modify
1087  * @attr: new attributes for variable
1088  *
1089  * Remotely change a variable's archive/lock state.
1090  *
1091  * Return value: 0 if successful, an error code otherwise.
1092  **/
ticalcs_calc_change_attr(CalcHandle * handle,VarRequest * vr,FileAttr attr)1093 TIEXPORT3 int TICALL ticalcs_calc_change_attr(CalcHandle* handle, VarRequest* vr, FileAttr attr)
1094 {
1095 	const CalcFncts *calc;
1096 	int ret = 0;
1097 
1098 	VALIDATE_HANDLE(handle);
1099 	VALIDATE_VARREQUEST(vr);
1100 
1101 	calc = handle->calc;
1102 	VALIDATE_CALCFNCTS(calc);
1103 
1104 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1105 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1106 	RETURN_IF_HANDLE_BUSY(handle);
1107 
1108 	ticalcs_info(_("Setting variable '%s' to %s:"), vr->name, tifiles_attribute_to_string(attr));
1109 
1110 	handle->busy = 1;
1111 	if (calc->change_attr)
1112 	{
1113 		ret = calc->change_attr(handle, vr, attr);
1114 	}
1115 	handle->busy = 0;
1116 
1117 	return ret;
1118 }
1119 
1120 /**
1121  * ticalcs_calc_send_all_vars_backup:
1122  * @handle: a previously allocated handle
1123  * @content: backup content
1124  *
1125  * Send a fake backup, for the models which support it.
1126  *
1127  * Return value: 0 if successful, an error code otherwise.
1128  **/
ticalcs_calc_send_all_vars_backup(CalcHandle * handle,FileContent * content)1129 TIEXPORT3 int TICALL ticalcs_calc_send_all_vars_backup(CalcHandle* handle, FileContent* content)
1130 {
1131 	const CalcFncts *calc;
1132 	int ret = 0;
1133 
1134 	VALIDATE_HANDLE(handle);
1135 	VALIDATE_FILECONTENT(content);
1136 	VALIDATE_FILECONTENT_ENTRIES(content);
1137 
1138 	calc = handle->calc;
1139 	VALIDATE_CALCFNCTS(calc);
1140 
1141 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1142 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1143 	RETURN_IF_HANDLE_BUSY(handle);
1144 
1145 	ticalcs_info(_("Sending fake (all vars) backup:"));
1146 	handle->busy = 1;
1147 	if (calc->send_all_vars_backup)
1148 	{
1149 		ret = calc->send_all_vars_backup(handle, content);
1150 	}
1151 	handle->busy = 0;
1152 
1153 	return ret;
1154 }
1155 
1156 /**
1157  * ticalcs_calc_recv_all_vars_backup:
1158  * @handle: a previously allocated handle
1159  * @content: backup content
1160  *
1161  * Request a fake backup (list of vars and apps) and receive it.
1162  *
1163  * Return value: 0 if successful, an error code otherwise.
1164  **/
ticalcs_calc_recv_all_vars_backup(CalcHandle * handle,FileContent * content)1165 TIEXPORT3 int TICALL ticalcs_calc_recv_all_vars_backup(CalcHandle* handle, FileContent* content)
1166 {
1167 	const CalcFncts *calc;
1168 	int ret = 0;
1169 
1170 	VALIDATE_HANDLE(handle);
1171 	VALIDATE_FILECONTENT(content);
1172 
1173 	calc = handle->calc;
1174 	VALIDATE_CALCFNCTS(calc);
1175 
1176 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1177 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1178 	RETURN_IF_HANDLE_BUSY(handle);
1179 
1180 	ticalcs_info(_("Requesting fake (all vars) backup:"));
1181 	handle->busy = 1;
1182 	if (calc->recv_all_vars_backup)
1183 	{
1184 		ret = calc->recv_all_vars_backup(handle, content);
1185 	}
1186 	handle->busy = 0;
1187 
1188 	return ret;
1189 }
1190 
1191 
1192 // ---
1193 
1194 
1195 /**
1196  * ticalcs_calc_send_backup2:
1197  * @handle: a previously allocated handle
1198  * @filename: name of file which contains backup to send
1199  *
1200  * Send a backup from file.
1201  *
1202  * Return value: 0 if successful, an error code otherwise.
1203  **/
ticalcs_calc_send_backup2(CalcHandle * handle,const char * filename)1204 TIEXPORT3 int TICALL ticalcs_calc_send_backup2(CalcHandle* handle, const char* filename)
1205 {
1206 	int ret = ERR_FILE_OPEN;
1207 
1208 	VALIDATE_HANDLE(handle);
1209 	VALIDATE_NONNULL(filename);
1210 
1211 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1212 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1213 	RETURN_IF_HANDLE_BUSY(handle);
1214 
1215 	if (ticalcs_calc_features(handle) & FTS_BACKUP)
1216 	{
1217 		// true backup capability
1218 		BackupContent * content = tifiles_content_create_backup(handle->model);
1219 		if (content != NULL)
1220 		{
1221 			ret = tifiles_file_read_backup(filename, content);
1222 			if (!ret)
1223 			{
1224 				ret = ticalcs_calc_send_backup(handle, content);
1225 				tifiles_content_delete_backup(content);
1226 			}
1227 		}
1228 	}
1229 	else
1230 	{
1231 		// pseudo-backup
1232 		FileContent * content = tifiles_content_create_regular(handle->model);
1233 		if (content != NULL)
1234 		{
1235 			ret = tifiles_file_read_regular(filename, content);
1236 			if (!ret)
1237 			{
1238 				ret = ticalcs_calc_send_all_vars_backup(handle, content);
1239 				tifiles_content_delete_regular(content);
1240 			}
1241 		}
1242 	}
1243 
1244 	return ret;
1245 }
1246 
1247 /**
1248  * ticalcs_calc_recv_backup2:
1249  * @handle: a previously allocated handle
1250  * @filename: name of file where to store backup
1251  *
1252  * Request a backup and receive it to file.
1253  *
1254  * Return value: 0 if successful, an error code otherwise.
1255  **/
ticalcs_calc_recv_backup2(CalcHandle * handle,const char * filename)1256 TIEXPORT3 int TICALL ticalcs_calc_recv_backup2(CalcHandle* handle, const char *filename)
1257 {
1258 	int ret;
1259 
1260 	VALIDATE_HANDLE(handle);
1261 	VALIDATE_NONNULL(filename);
1262 
1263 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1264 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1265 	RETURN_IF_HANDLE_BUSY(handle);
1266 
1267 	if (ticalcs_calc_features(handle) & FTS_BACKUP)
1268 	{
1269 		// true backup capability
1270 		BackupContent * content = tifiles_content_create_backup(handle->model);
1271 		ret = ticalcs_calc_recv_backup(handle, content);
1272 		if (!ret)
1273 		{
1274 			ret = tifiles_file_write_backup(filename, content);
1275 		}
1276 		tifiles_content_delete_backup(content);
1277 	}
1278 	else
1279 	{
1280 		// pseudo-backup
1281 		FileContent * content = tifiles_content_create_regular(handle->model);
1282 		ret = ticalcs_calc_recv_all_vars_backup(handle, content);
1283 		if (!ret)
1284 		{
1285 			ret = tifiles_file_write_regular(filename, content, NULL);
1286 		}
1287 		tifiles_content_delete_regular(content);
1288 	}
1289 
1290 	return ret;
1291 }
1292 
1293 /**
1294  * ticalcs_calc_send_var2:
1295  * @handle: a previously allocated handle
1296  * @mode: to document
1297  * @filename: name of file
1298  *
1299  * Send one or more variables (silent mode) from file.
1300  *
1301  * Return value: 0 if successful, an error code otherwise.
1302  **/
ticalcs_calc_send_var2(CalcHandle * handle,CalcMode mode,const char * filename)1303 TIEXPORT3 int TICALL ticalcs_calc_send_var2(CalcHandle* handle, CalcMode mode, const char* filename)
1304 {
1305 	FileContent *content;
1306 	int ret;
1307 
1308 	VALIDATE_HANDLE(handle);
1309 	VALIDATE_NONNULL(filename);
1310 
1311 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1312 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1313 	RETURN_IF_HANDLE_BUSY(handle);
1314 
1315 	content = tifiles_content_create_regular(handle->model);
1316 	ret = tifiles_file_read_regular(filename, content);
1317 	if (!ret)
1318 	{
1319 		ret = ticalcs_calc_send_var(handle, mode, content);
1320 		tifiles_content_delete_regular(content);
1321 	}
1322 
1323 	return ret;
1324 }
1325 
1326 /**
1327  * ticalcs_calc_recv_var2:
1328  * @handle: a previously allocated handle
1329  * @mode:
1330  * @content: where to store variable content
1331  * @var: a #VarRequest structure got with dirlist
1332  *
1333  * Request receiving of _one_ variable (silent mode).
1334  *
1335  * Return value: 0 if successful, an error code otherwise.
1336  **/
ticalcs_calc_recv_var2(CalcHandle * handle,CalcMode mode,const char * filename,VarRequest * vr)1337 TIEXPORT3 int TICALL ticalcs_calc_recv_var2(CalcHandle* handle, CalcMode mode, const char* filename, VarRequest* vr)
1338 {
1339 	FileContent *content;
1340 	int ret;
1341 
1342 	VALIDATE_HANDLE(handle);
1343 	VALIDATE_NONNULL(filename);
1344 	VALIDATE_VARREQUEST(vr);
1345 
1346 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1347 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1348 	RETURN_IF_HANDLE_BUSY(handle);
1349 
1350 	content = tifiles_content_create_regular(handle->model);
1351 	ret = ticalcs_calc_recv_var(handle, mode, content, vr);
1352 	if (!ret)
1353 	{
1354 		ret = tifiles_file_write_regular(filename, content, NULL);
1355 	}
1356 	tifiles_content_delete_regular(content);
1357 
1358 	return ret;
1359 }
1360 
1361 /**
1362  * ticalcs_calc_send_var_ns2:
1363  * @handle: a previously allocated handle
1364  * @mode:
1365  * @filename: name of file
1366  *
1367  * Send one or more variable (non-silent mode).
1368  *
1369  * Return value: 0 if successful, an error code otherwise.
1370  **/
ticalcs_calc_send_var_ns2(CalcHandle * handle,CalcMode mode,const char * filename)1371 TIEXPORT3 int TICALL ticalcs_calc_send_var_ns2(CalcHandle* handle, CalcMode mode, const char* filename)
1372 {
1373 	FileContent *content;
1374 	int ret = ERR_FILE_OPEN;
1375 
1376 	VALIDATE_HANDLE(handle);
1377 	VALIDATE_NONNULL(filename);
1378 
1379 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1380 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1381 	RETURN_IF_HANDLE_BUSY(handle);
1382 
1383 	content = tifiles_content_create_regular(handle->model);
1384 	if (content != NULL)
1385 	{
1386 		ret = tifiles_file_read_regular(filename, content);
1387 		if (!ret)
1388 		{
1389 			ret = ticalcs_calc_send_var_ns(handle, mode, content);
1390 			tifiles_content_delete_regular(content);
1391 		}
1392 	}
1393 
1394 	return ret;
1395 }
1396 
1397 /**
1398  * ticalcs_calc_recv_var_ns2:
1399  * @handle: a previously allocated handle
1400  * @mode:
1401  * @filename: where to store variables
1402  * @var: information on the received variable (if single) or NULL (if group)
1403  *
1404  * Receive one or more variable (non-silent mode).
1405  *
1406  * Return value: 0 if successful, an error code otherwise.
1407  **/
ticalcs_calc_recv_var_ns2(CalcHandle * handle,CalcMode mode,const char * filename,VarEntry ** vr)1408 TIEXPORT3 int TICALL ticalcs_calc_recv_var_ns2(CalcHandle* handle, CalcMode mode, const char* filename, VarEntry** vr)
1409 {
1410 	FileContent *content;
1411 	int ret;
1412 
1413 	VALIDATE_HANDLE(handle);
1414 	VALIDATE_NONNULL(filename);
1415 	VALIDATE_NONNULL(vr);
1416 
1417 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1418 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1419 	RETURN_IF_HANDLE_BUSY(handle);
1420 
1421 	content = tifiles_content_create_regular(handle->model);
1422 	ret = ticalcs_calc_recv_var_ns(handle, mode, content, vr);
1423 	if (!ret)
1424 	{
1425 		ret = tifiles_file_write_regular(filename, content, NULL);
1426 	}
1427 	tifiles_content_delete_regular(content);
1428 
1429 	return ret;
1430 }
1431 
1432 /**
1433  * ticalcs_calc_send_app2:
1434  * @handle: a previously allocated handle
1435  * @filename: name of file
1436  *
1437  * Send a FLASH app or os.
1438  *
1439  * Return value: 0 if successful, an error code otherwise.
1440  **/
ticalcs_calc_send_app2(CalcHandle * handle,const char * filename)1441 TIEXPORT3 int TICALL ticalcs_calc_send_app2(CalcHandle* handle, const char* filename)
1442 {
1443 	FlashContent *content;
1444 	int ret;
1445 
1446 	VALIDATE_HANDLE(handle);
1447 	VALIDATE_NONNULL(filename);
1448 
1449 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1450 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1451 	RETURN_IF_HANDLE_BUSY(handle);
1452 
1453 	content = tifiles_content_create_flash(handle->model);
1454 	ret = tifiles_file_read_flash(filename, content);
1455 	if (!ret)
1456 	{
1457 		ret = ticalcs_calc_send_app(handle, content);
1458 		tifiles_content_delete_flash(content);
1459 	}
1460 
1461 	return ret;
1462 }
1463 
1464 /**
1465  * ticalcs_calc_recv_app2:
1466  * @handle: a previously allocated handle
1467  * @content: where to store content
1468  * @var: FLASH app to request
1469  *
1470  * Request receiving of a FLASH app.
1471  *
1472  * Return value: 0 if successful, an error code otherwise.
1473  **/
ticalcs_calc_recv_app2(CalcHandle * handle,const char * filename,VarRequest * vr)1474 TIEXPORT3 int TICALL ticalcs_calc_recv_app2(CalcHandle* handle, const char* filename, VarRequest* vr)
1475 {
1476 	FlashContent *content;
1477 	int ret;
1478 
1479 	VALIDATE_HANDLE(handle);
1480 	VALIDATE_NONNULL(filename);
1481 	VALIDATE_VARREQUEST(vr);
1482 
1483 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1484 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1485 	RETURN_IF_HANDLE_BUSY(handle);
1486 
1487 	content = tifiles_content_create_flash(handle->model);
1488 	ret = ticalcs_calc_recv_app(handle, content, vr);
1489 	if (!ret)
1490 	{
1491 		ret = tifiles_file_write_flash(filename, content);
1492 	}
1493 	tifiles_content_delete_flash(content);
1494 
1495 	return ret;
1496 }
1497 
1498 /**
1499  * ticalcs_calc_send_cert2:
1500  * @handle: a previously allocated handle
1501  * @filename: name of file
1502  *
1503  * Send a certificate.
1504  *
1505  * Return value: 0 if successful, an error code otherwise.
1506  **/
ticalcs_calc_send_cert2(CalcHandle * handle,const char * filename)1507 TIEXPORT3 int TICALL ticalcs_calc_send_cert2(CalcHandle* handle, const char* filename)
1508 {
1509 	FlashContent *content;
1510 	int ret;
1511 
1512 	VALIDATE_HANDLE(handle);
1513 	VALIDATE_NONNULL(filename);
1514 
1515 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1516 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1517 	RETURN_IF_HANDLE_BUSY(handle);
1518 
1519 	content = tifiles_content_create_flash(handle->model);
1520 	ret = tifiles_file_read_flash(filename, content);
1521 	if (!ret)
1522 	{
1523 		ret = ticalcs_calc_send_cert(handle, content);
1524 		tifiles_content_delete_flash(content);
1525 	}
1526 
1527 	return ret;
1528 }
1529 
1530 /**
1531  * ticalcs_calc_send_os2:
1532  * @handle: a previously allocated handle
1533  * @filename: name of file
1534  *
1535  * Send a FLASH app.
1536  *
1537  * Return value: 0 if successful, an error code otherwise.
1538  **/
ticalcs_calc_send_os2(CalcHandle * handle,const char * filename)1539 TIEXPORT3 int TICALL ticalcs_calc_send_os2(CalcHandle* handle, const char* filename)
1540 {
1541 	FlashContent *content;
1542 	int ret;
1543 
1544 	VALIDATE_HANDLE(handle);
1545 	VALIDATE_NONNULL(filename);
1546 
1547 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1548 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1549 	RETURN_IF_HANDLE_BUSY(handle);
1550 
1551 	content = tifiles_content_create_flash(handle->model);
1552 	ret = tifiles_file_read_flash(filename, content);
1553 	if (!ret)
1554 	{
1555 		ret = ticalcs_calc_send_os(handle, content);
1556 		tifiles_content_delete_flash(content);
1557 	}
1558 
1559 	return ret;
1560 }
1561 
1562 /**
1563  * ticalcs_calc_recv_cert2:
1564  * @handle: a previously allocated handle
1565  * @filename: name of file
1566  *
1567  * Request certificate. Depending on extension, saves it as *.9Xq or *.cer.
1568  *
1569  * Return value: 0 if successful, an error code otherwise.
1570  **/
ticalcs_calc_recv_cert2(CalcHandle * handle,const char * filename)1571 TIEXPORT3 int TICALL ticalcs_calc_recv_cert2(CalcHandle* handle, const char* filename)
1572 {
1573 	FlashContent *content = NULL;
1574 	char *ext;
1575 	int ret;
1576 
1577 	VALIDATE_HANDLE(handle);
1578 	VALIDATE_NONNULL(filename);
1579 
1580 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1581 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1582 	RETURN_IF_HANDLE_BUSY(handle);
1583 
1584 	do {
1585 		ext = tifiles_fext_get(filename);
1586 		if (!strcmp(ext, "cer"))
1587 		{
1588 			// .cer format as generated by SDK
1589 			gchar *basename = strdup(filename);
1590 			FILE *f;
1591 			gchar *e;
1592 
1593 			if (basename == NULL)
1594 			{
1595 				ret = ERR_MALLOC;
1596 				break;
1597 			}
1598 
1599 			e = tifiles_fext_get(basename);
1600 
1601 			memcpy(e, "crt", 3);
1602 
1603 			content = tifiles_content_create_flash(handle->model);
1604 			if (content == NULL)
1605 			{
1606 				ret = ERR_MALLOC;
1607 				break;
1608 			}
1609 			ret = ticalcs_calc_recv_cert(handle, content);
1610 			if (ret)
1611 			{
1612 				free(basename);
1613 				break;
1614 			}
1615 
1616 			f = fopen(basename, "wb");
1617 			free(basename);
1618 			if (!f)
1619 			{
1620 				ret = ERR_SAVE_FILE;
1621 				break;
1622 			}
1623 			if (fwrite(content->data_part, content->data_length, 1, f) < 1)
1624 			{
1625 				fclose(f);
1626 				ret = ERR_SAVE_FILE;
1627 				break;
1628 			}
1629 			if (fclose(f))
1630 			{
1631 				ret = ERR_SAVE_FILE;
1632 				break;
1633 			}
1634 		}
1635 		else
1636 		{
1637 			// .??q format as generated by TI
1638 			content = tifiles_content_create_flash(handle->model);
1639 			ret = ticalcs_calc_recv_cert(handle, content);
1640 			if (!ret)
1641 			{
1642 				ret = tifiles_file_write_flash(filename, content);
1643 			}
1644 		}
1645 	} while (0);
1646 
1647 	tifiles_content_delete_flash(content);
1648 
1649 	return ret;
1650 }
1651 
1652 /**
1653  * ticalcs_calc_send_tigroup2:
1654  * @handle: a previously allocated handle
1655  * @filename: name of file
1656  * @mode: which vars/apps to send
1657  *
1658  * Send a TiGroup file.
1659  *
1660  * Return value: 0 if successful, an error code otherwise.
1661  **/
ticalcs_calc_send_tigroup2(CalcHandle * handle,const char * filename,TigMode mode)1662 TIEXPORT3 int TICALL ticalcs_calc_send_tigroup2(CalcHandle* handle, const char* filename, TigMode mode)
1663 {
1664 	TigContent *content;
1665 	int ret;
1666 
1667 	VALIDATE_HANDLE(handle);
1668 	VALIDATE_NONNULL(filename);
1669 
1670 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1671 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1672 	RETURN_IF_HANDLE_BUSY(handle);
1673 
1674 	content = tifiles_content_create_tigroup(handle->model, 0);
1675 	ret = tifiles_file_read_tigroup(filename, content);
1676 	if (!ret)
1677 	{
1678 		ret = ticalcs_calc_send_tigroup(handle, content, mode);
1679 	}
1680 	tifiles_content_delete_tigroup(content);
1681 
1682 	return ret;
1683 }
1684 
1685 /**
1686  * ticalcs_calc_recv_tigroup2:
1687  * @handle: a previously allocated handle
1688  * @filename: name of file
1689  * @mode: which vars/apps to receive
1690  *
1691  * Receive a TiGroup file.
1692  *
1693  * Return value: 0 if successful, an error code otherwise.
1694  **/
ticalcs_calc_recv_tigroup2(CalcHandle * handle,const char * filename,TigMode mode)1695 TIEXPORT3 int TICALL ticalcs_calc_recv_tigroup2(CalcHandle* handle, const char* filename, TigMode mode)
1696 {
1697 	TigContent *content;
1698 	int ret;
1699 
1700 	VALIDATE_HANDLE(handle);
1701 	VALIDATE_NONNULL(filename);
1702 
1703 	RETURN_IF_HANDLE_NOT_ATTACHED(handle);
1704 	RETURN_IF_HANDLE_NOT_OPEN(handle);
1705 	RETURN_IF_HANDLE_BUSY(handle);
1706 
1707 	content = tifiles_content_create_tigroup(handle->model, 0);
1708 	ret = ticalcs_calc_recv_tigroup(handle, content, mode);
1709 	if (!ret)
1710 	{
1711 		ret = tifiles_file_write_tigroup(filename, content);
1712 	}
1713 	tifiles_content_delete_tigroup(content);
1714 
1715 	return ret;
1716 }
1717