source: trunk/platform/generic/wrappers.c @ 1152

Revision 1152, 31.2 KB checked in by reyalP, 2 years ago (diff)

change dryos directory wrappers to allow readdir on multiple directories. May fix some problems with "purge raw" in dryos.

  • Property svn:eol-style set to native
Line 
1#include "camera.h"
2#include "lolevel.h"
3#include "platform.h"
4#include "conf.h"
5#include "math.h"
6#include "levent.h"
7
8//----------------------------------------------------------------------------
9// Char Wrappers
10
11#if CAM_DRYOS
12#define _U      0x01    /* upper */
13#define _L      0x02    /* lower */
14#define _D      0x04    /* digit */
15#define _C      0x08    /* cntrl */
16#define _P      0x10    /* punct */
17#define _S      0x20    /* white space (space/lf/tab) */
18#define _X      0x40    /* hex digit */
19#define _SP     0x80    /* hard space (0x20) */
20static int _ctype(int c,int t) {
21static unsigned char ctypes[] = {
22_C,_C,_C,_C,_C,_C,_C,_C,                                        /* 0-7 */
23_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,         /* 8-15 */
24_C,_C,_C,_C,_C,_C,_C,_C,                                        /* 16-23 */
25_C,_C,_C,_C,_C,_C,_C,_C,                                        /* 24-31 */
26_S|_SP,_P,_P,_P,_P,_P,_P,_P,                            /* 32-39 */
27_P,_P,_P,_P,_P,_P,_P,_P,                                        /* 40-47 */
28_D,_D,_D,_D,_D,_D,_D,_D,                                        /* 48-55 */
29_D,_D,_P,_P,_P,_P,_P,_P,                                        /* 56-63 */
30_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,      /* 64-71 */
31_U,_U,_U,_U,_U,_U,_U,_U,                                        /* 72-79 */
32_U,_U,_U,_U,_U,_U,_U,_U,                                        /* 80-87 */
33_U,_U,_U,_P,_P,_P,_P,_P,                                        /* 88-95 */
34_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,      /* 96-103 */
35_L,_L,_L,_L,_L,_L,_L,_L,                                        /* 104-111 */
36_L,_L,_L,_L,_L,_L,_L,_L,                                        /* 112-119 */
37_L,_L,_L,_P,_P,_P,_P,_C,                                        /* 120-127 */
38// since the following have nothing set, we can save memory by leaving them out
39#if 0
400,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 128-143 */
410,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 144-159 */
420,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 160-175 */
430,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 176-191 */
440,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 192-207 */
450,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 208-223 */
460,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 224-239 */
470,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0                 /* 240-255 */
48#endif
49};
50    // have to handle EOF (-1)
51    if( (unsigned)c >= sizeof(ctypes)) {
52        return 0;
53    }
54    return ctypes[c] & t;
55}
56
57int isdigit(int c) { return _ctype(c,_D); }
58int isspace(int c) { return _ctype(c,_S); }
59int isalpha(int c) { return _ctype(c,(_U|_L)); }
60int isupper(int c) { return _ctype(c,_U); }
61int islower(int c) { return _ctype(c,_L); }
62int ispunct(int c) { return _ctype(c,_P); }
63int isxdigit(int c) { return _ctype(c,(_X|_D)); }
64int iscntrl(int c) { return _ctype(c,_C); }
65
66int tolower(int c) { return isupper(c) ? c | 0x20 : c; }
67int toupper(int c) { return islower(c) ? c & ~0x20 : c; }
68
69#else   //!CAM_DRYOS
70
71int isdigit(int c) { return _isdigit(c); }
72int isspace(int c) { return _isspace(c); }
73int isalpha(int c) { return _isalpha(c); }
74int isupper(int c) { return _isupper(c); }
75int islower(int c) { return _islower(c); }
76int ispunct(int c) { return _ispunct(c); }
77int isxdigit(int c) { return _isxdigit(c); }
78
79// don't want to require the whole ctype table on vxworks just for this one
80int iscntrl(int c) { return ((c >=0 && c <32) || c == 127); }
81
82int tolower(int c) { return _tolower(c); }
83int toupper(int c) { return _toupper(c); }
84
85#endif
86
87int isalnum(int c) { return (isdigit(c) || isalpha(c)); }
88
89//----------------------------------------------------------------------------
90
91void msleep(long msec)
92{
93    _SleepTask(msec);
94}
95
96#ifndef CAM_DRYOS
97void task_lock()
98{
99    _taskLock();
100}
101
102void task_unlock()
103{
104    _taskUnlock();
105}
106
107const char *task_name(int id)
108{
109    return _taskName(id);
110}
111
112int task_id_list_get(int *idlist,int size)
113{
114    return _taskIdListGet(idlist,size);
115}
116#endif
117
118long get_property_case(long id, void *buf, long bufsize)
119{
120    return _GetPropertyCase(id, buf, bufsize);
121}
122
123long set_property_case(long id, void *buf, long bufsize)
124{
125    return _SetPropertyCase(id, buf, bufsize);
126}
127
128long get_parameter_data(long id, void *buf, long bufsize)
129{
130    return _GetParameterData(id|0x4000, buf, bufsize);
131}
132
133long set_parameter_data(long id, void *buf, long bufsize)
134{
135    return _SetParameterData(id|0x4000, buf, bufsize);
136}
137
138void remount_filesystem()
139{
140    _Unmount_FileSystem();
141    _Mount_FileSystem();
142}
143
144void mark_filesystem_bootable()
145{
146    _UpdateMBROnFlash(0, 0x40, "BOOTDISK");
147}
148
149void __attribute__((weak)) vid_bitmap_refresh()
150{
151    _RefreshPhysicalScreen(1);
152}
153
154long lens_get_zoom_pos()
155{
156    return _GetZoomLensCurrentPosition();
157}
158
159void lens_set_zoom_pos(long newpos)
160{
161}
162
163long lens_get_zoom_point()
164{
165    return _GetZoomLensCurrentPoint();
166}
167
168void lens_set_zoom_point(long newpt)
169{
170#if defined (CAMERA_s95)
171        long startTime;
172#endif
173
174    if (newpt < 0) {
175        newpt = 0;
176    } else if (newpt >= zoom_points) {
177        newpt = zoom_points-1;
178    }
179
180#if defined(CAMERA_sx30) || defined(CAMERA_g12)
181        if (lens_get_zoom_point() != newpt)
182        {
183                // Get current digital zoom mode & state
184                // state == 1 && mode == 0 --> Digital Zoom Standard
185                int digizoom_mode, digizoom_state, digizoom_pos;
186                get_property_case(PROPCASE_DIGITAL_ZOOM_MODE,&digizoom_mode,sizeof(digizoom_mode));
187                get_property_case(PROPCASE_DIGITAL_ZOOM_STATE,&digizoom_state,sizeof(digizoom_state));
188                get_property_case(PROPCASE_DIGITAL_ZOOM_POSITION,&digizoom_pos,sizeof(digizoom_pos));
189                if ((digizoom_state == 1) && (digizoom_mode == 0) && (digizoom_pos != 0))
190                {
191                        // reset digital zoom in case camera is in this zoom range
192                        extern void _PT_MoveDigitalZoomToWide();
193                        _PT_MoveDigitalZoomToWide();
194                }
195
196  #if defined(CAMERA_sx30)
197                // SX30 - _MoveZoomLensWithPoint crashes camera
198                // _PT_MoveOpticalZoomAt works, and updates PROPCASE_OPTICAL_ZOOM_POSITION; but doesn't wait for zoom to finish
199                extern void _PT_MoveOpticalZoomAt(long*);
200                _PT_MoveOpticalZoomAt(&newpt);
201  #else
202            _MoveZoomLensWithPoint((short*)&newpt);
203  #endif
204
205                // have to sleep here, zoom_busy set in another task, without sleep this will hang
206                while (zoom_busy) msleep(10);
207
208                // g12 & sx30 only use this value for optical zoom
209                zoom_status=ZOOM_OPTICAL_MAX;
210
211  #if defined(CAMERA_g12)
212            _SetPropertyCase(PROPCASE_OPTICAL_ZOOM_POSITION, &newpt, sizeof(newpt));
213  #endif
214        }
215#else   // !(CAMERA_g12 || CAMERA_sx30)
216    _MoveZoomLensWithPoint((short*)&newpt);
217
218  #if defined (CAMERA_s95)
219        // this will hang sometimes on s95 when zoom_busy gets stuck as a 1
220        // we add a timeout as a work-around for this problem
221        startTime = get_tick_count();
222        while (get_tick_count() < (startTime + 2000)) {
223                if (!zoom_busy)
224                        break;
225        }
226  #else // !CAMERA_s95
227        while (zoom_busy) ;
228  #endif // !CAMERA_s95
229
230    if (newpt==0) zoom_status=ZOOM_OPTICAL_MIN;
231    else if (newpt >= zoom_points) zoom_status=ZOOM_OPTICAL_MAX;
232    else zoom_status=ZOOM_OPTICAL_MEDIUM;
233    _SetPropertyCase(PROPCASE_OPTICAL_ZOOM_POSITION, &newpt, sizeof(newpt));
234#endif  // !(CAMERA_g12 || CAMERA_sx30)
235}
236
237void lens_set_zoom_speed(long newspd)
238{
239    if (newspd < 5) {
240        newspd = 5;
241    } else if (newspd > 100) {
242        newspd = 100;
243    }
244    _SetZoomActuatorSpeedPercent((short*)&newspd);
245}
246
247void lens_set_focus_pos(long newpos)
248{
249#if defined(CAMERA_g12) // G12 crashes if in Continuous AF mode and try to call _MoveFocusLensToDistance
250        int af_mode;
251        get_property_case(PROPCASE_CONTINUOUS_AF,&af_mode,sizeof(af_mode));
252        if (af_mode == 0)       // can only set focus distance when not in continuous AF mode
253#endif
254        {
255                _MoveFocusLensToDistance((short*)&newpos);
256                //while (focus_busy);
257                while ((shooting_is_flash_ready()!=1) || (focus_busy));
258                newpos = _GetFocusLensSubjectDistance();
259                _SetPropertyCase(PROPCASE_SUBJECT_DIST1, &newpos, sizeof(newpos));
260                _SetPropertyCase(PROPCASE_SUBJECT_DIST2, &newpos, sizeof(newpos));
261        }
262}
263
264void play_sound(unsigned sound)
265{
266        static const int sounds[]={ 0x2001, //startup sound
267                                0x2002, //shutter sound
268                                0x2003, //button press sound
269                                0x2004, //self-timer sound
270                                0xC211, //short beep
271                                50000,  // AF confirmation
272                                0xC507, // error beep imo
273                                0x400D, // LONG ERROR BEEP CONTINIUOUS- warning, cannot be stopped (yet)
274                            };
275    if(sound >= sizeof(sounds)/sizeof(sounds[0]))
276        return;
277
278    _PT_PlaySound(sounds[sound], 0);
279}
280
281long stat_get_vbatt()
282{
283    return _VbattGet();
284}
285
286int get_battery_temp()
287{
288    return _GetBatteryTemperature();
289}
290
291int get_ccd_temp()
292{
293    return _GetCCDTemperature();
294}
295
296int get_optical_temp()
297{
298    return _GetOpticalTemperature();
299}
300
301long get_tick_count()
302{
303long t;
304#if !CAM_DRYOS
305    _GetSystemTime(&t);
306    return t;
307#else
308    return (int)_GetSystemTime(&t);
309#endif
310}
311
312//----------------------------------------------------------------------------
313// I/O wrappers
314
315/*int creat (const char *name, int flags)
316{
317    return _creat(name, flags);
318}*/
319
320int open (const char *name, int flags, int mode )
321{
322#ifdef CAM_DRYOS
323    if(!name || name[0]!='A')
324        return -1;
325#endif
326#if defined(CAM_STARTUP_CRASH_FILE_OPEN_FIX)    // enable fix for camera crash at startup when opening the conf / font files
327                                                                                                // see http://chdk.setepontos.com/index.php?topic=6179.0
328        #define O_RDONLY 0                                                      // copied from stdlib.h (including stdlib.h causes compile errors due to function definition mismatch - needs fixing)
329        if (flags == O_RDONLY)                                          // At startup opening the conf / font files conflicts with Canon task if use _Open. Camera can randomly crash.
330                return _open(name, flags, mode);
331#endif
332    return _Open(name, flags, mode);
333}
334
335int close (int fd)
336{
337    return _Close(fd);
338}
339
340int write (int fd, void *buffer, long nbytes)
341{
342    return _Write(fd, buffer, nbytes);
343}
344
345int read (int fd, void *buffer, long nbytes)
346{
347    return _Read(fd, buffer, nbytes);
348}
349
350int lseek (int fd, long offset, int whence)
351{
352    return _lseek(fd, offset, whence); /* yes, it's lower-case lseek here since Lseek calls just lseek (A610) */
353}
354
355long mkdir(const char *dirname)
356{
357        return _MakeDirectory_Fut(dirname,-1); // meaning of second arg is not clear, firmware seems to use -1
358}
359
360int remove(const char *name) {
361        return _DeleteFile_Fut(name);
362}
363
364//----------------------------------------------------------------------------
365// directory wrappers
366#ifndef CAM_DRYOS
367void *opendir(const char* name) {
368    return _opendir(name);
369}
370void* readdir(void *d) {
371    return _readdir(d);
372}
373int closedir(void *d) {
374    return _closedir(d);
375}
376
377void rewinddir(void *d) {
378    return _rewinddir(d);
379}
380#else // dryos
381// TODO duplicated in stdlib.h!
382// structure returned by dryos
383// actual size may vary depending on version
384typedef struct {
385    int fh;
386    int unk[4];
387} DIR_dryos;
388
389// struct returned by our wrappers around opendir
390typedef struct {
391    DIR_dryos *dh;
392#ifdef CAM_DRYOS_2_3_R39
393    char de_buf[64];
394#else
395    char de_buf[40];
396#endif
397} DIR;
398
399DIR *opendir(const char* name) {
400    DIR *d;
401    DIR_dryos *dh = _opendir(name);
402    if(!dh) {
403        return (void *)0;
404    }
405    d = _malloc(sizeof(DIR));
406    if(!d) {
407        _closedir(dh);
408        return (void *)0;
409    }
410    d->dh = dh;
411    return d;
412}
413
414void* readdir(DIR *d) {
415  _ReadFastDir(d->dh, d->de_buf);
416  return d->de_buf[0]? d->de_buf : (void*)0;
417}
418
419int closedir(DIR *d) {
420    int r;
421    if(!d) {
422        return -1;
423    }
424    r = _closedir(d->dh);
425    _free(d);   
426    return r;
427}
428
429void rewinddir(DIR *d) {
430    if(!d) {
431        return;
432    }
433    _rewinddir(d->dh);
434}
435#endif // dryos dir functions
436
437int stat(char *name, void *pStat) {
438    return _stat(name, pStat);
439}
440
441long fopen(const char *filename, const char *mode) {
442#ifdef CAM_DRYOS
443    if(!filename || filename[0]!='A') {
444        return 0;
445    }
446#endif
447    return _Fopen_Fut(filename,mode);
448}
449
450long fclose(long f) {
451    return _Fclose_Fut((long)f);
452}
453
454long fread(void *buf, long elsize, long count, long f) {
455    return _Fread_Fut(buf,elsize,count,(long)f);
456}
457
458long fwrite(const void *buf, long elsize, long count, long f) {
459    return _Fwrite_Fut(buf,elsize,count,(long)f);
460}
461
462long fseek(long file, long offset, long whence) {
463    return _Fseek_Fut((long)file,offset,whence);
464}
465
466long feof(long file) {
467    return _Feof_Fut((long)file);
468}
469
470long fflush(long file) {
471    return _Fflush_Fut((long)file);
472}
473
474char *fgets(char *buf, int n, long f) {
475    return _Fgets_Fut(buf,n,(int)f);
476}
477
478long rename(const char *oldname, const char *newname) {
479 return _RenameFile_Fut(oldname, newname);
480}
481
482unsigned int GetFreeCardSpaceKb(void){
483        return (_GetDrive_FreeClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
484}
485
486unsigned int GetTotalCardSpaceKb(void){
487        return (_GetDrive_TotalClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
488}
489
490//----------------------------------------------------------------------------
491
492int errnoOfTaskGet(int tid) {
493#if !CAM_DRYOS
494    return _errnoOfTaskGet(tid);
495#else
496    return 0;
497#endif
498}
499
500//----------------------------------------------------------------------------
501// String wrappers
502
503long strlen(const char *s) {
504    return _strlen(s);
505}
506
507int strcmp(const char *s1, const char *s2) {
508    return _strcmp(s1, s2);
509}
510
511int strncmp(const char *s1, const char *s2, long n) {
512    return _strncmp(s1, s2, n);
513}
514
515char *strchr(const char *s, int c) {
516    return _strchr(s, c);
517}
518
519char *strcpy(char *dest, const char *src) {
520    return _strcpy(dest, src);
521}
522
523char *strncpy(char *dest, const char *src, long n) {
524    return _strncpy(dest, src, n);
525}
526
527char *strcat(char *dest, const char *app) {
528    return _strcat(dest, app);
529}
530
531char *strrchr(const char *s, int c) {
532#if defined (CAMERA_s95)
533        // unable to find strrchr in s95 - we use our own fn
534        char *result = 0;
535
536        c = (char) c;
537
538        do {
539                if (c == *s)
540                        result = (char*) s;
541        } while (*s++ != '\0');
542
543        return result;
544#else
545    return _strrchr(s, c);
546#endif
547}
548
549long strtol(const char *nptr, char **endptr, int base) {
550    return _strtol(nptr, endptr, base);
551}
552
553unsigned long strtoul(const char *nptr, char **endptr, int base) {
554#if CAM_DRYOS
555    return (unsigned long)_strtolx(nptr, endptr, base, 0);
556#else
557    return _strtoul(nptr, endptr, base);
558#endif
559}
560
561char *strpbrk(const char *s, const char *accept) {
562#if !CAM_DRYOS
563    return _strpbrk(s, accept);
564#else
565    const char *sc1,*sc2;
566
567    for( sc1 = s; *sc1 != '\0'; ++sc1) {
568     for( sc2 = accept; *sc2 != '\0'; ++sc2) {
569      if (*sc1 == *sc2) return (char *) sc1;
570     }
571    }
572return (void*)0;
573#endif
574}
575
576//----------------------------------------------------------------------------
577
578long sprintf(char *s, const char *st, ...)
579{
580    long res;
581    __builtin_va_list va;
582    __builtin_va_start(va, st);
583    res = _vsprintf(s, st, va);
584    __builtin_va_end(va);
585    return res;
586}
587
588// strerror exists on vxworks cams,
589// but it does about the same thing as this
590const char *strerror(int en) {
591#if !CAM_DRYOS
592    static char msg[20];
593    sprintf(msg,"errno 0x%X",en);
594    return msg;
595#else
596    return "error";
597#endif
598}
599
600//----------------------------------------------------------------------------
601// Time wrappers
602
603unsigned long time(unsigned long *timer) {
604    return _time(timer);
605}
606
607int utime(char *file, void *newTimes) {
608#if !CAM_DRYOS
609  return _utime(file, newTimes);
610#else
611 int res=0;
612 int fd;
613 fd = _open(file, 0, 0);
614
615#ifdef CAM_DRYOS_2_3_R39
616   if (fd>=0) {
617       _close(fd);
618       res=_SetFileTimeStamp(file, ((int*)newTimes)[0] , ((int*)newTimes)[1]);
619   }
620#else
621     if (fd>=0) {
622      res=_SetFileTimeStamp(fd, ((int*)newTimes)[0] , ((int*)newTimes)[1]);
623      _close(fd);
624     }
625     // return value compatibe with utime: ok=0 fail=-1
626#endif
627  return (res)?0:-1;
628#endif
629}
630
631void *localtime(const unsigned long *_tod) {
632#if !CAM_DRYOS
633    return _localtime(_tod);
634#else
635// for DRYOS cameras do something with this!  - sizeof(x[]) must be >= sizeof(struct tm) :  'static int x[9];'
636  static int x[9];
637  return _LocalTime(_tod, &x);
638#endif
639}
640
641long strftime(char *s, unsigned long maxsize, const char *format, /*const struct tm*/ void *timp) {
642        return _strftime(s,maxsize,format,timp);
643}
644
645/*time_t*/ long mktime(/*struct tm*/ void *timp) {
646#if !CAM_DRYOS
647        return _mktime(timp);
648#else
649        int timp_ext[10]; // struct tm + a ptr
650        _memcpy(timp_ext,timp,9*sizeof(int));
651        timp_ext[9]=0;
652        long retval = _mktime_ext(&timp_ext);
653        _memcpy(timp,timp_ext,9*sizeof(int));
654        return retval;
655#endif
656}
657
658//----------------------------------------------------------------------------
659// Math wrappers
660
661double _log(double x) {
662    return __log(x);
663}
664
665double _log10(double x) {
666    return __log10(x);
667}
668
669double _pow(double x, double y) {
670    return __pow(x, y);
671}
672
673double _sqrt(double x) {
674    return __sqrt(x);
675}
676
677//----------------------------------------------------------------------------
678// Memory wrappers
679
680#ifdef OPT_EXMEM_MALLOC
681// I set this up to 16 mb and it still booted...
682#ifndef EXMEM_HEAP_SKIP
683#define EXMEM_HEAP_SKIP 0
684#endif
685#ifndef EXMEM_BUFFER_SIZE
686#define EXMEM_BUFFER_SIZE (1024*1024*2) // default size if not specified by camera
687#endif
688#define EXMEM_HEAP_SIZE (EXMEM_BUFFER_SIZE+EXMEM_HEAP_SKIP)     // desired space + amount to skip for the movie buffers (if needed)
689// these aren't currently needed elsewhere
690/*
691void * exmem_alloc(unsigned pool_id, unsigned size)
692{
693        return _exmem_alloc(pool_id,size,0);
694}
695
696void exmem_free(unsigned pool_id)
697{
698        _exmem_free(pool_id);
699}
700*/
701
702static void *exmem_heap;
703void *exmem_start = 0, *exmem_end = 0;
704int exmem_size = 0;
705
706void *suba_init(void *heap, unsigned size, unsigned rst, unsigned mincell);
707void *suba_alloc(void *heap, unsigned size, unsigned zero);
708int suba_free(void *heap, void *p);
709
710void exmem_malloc_init() {
711        // pool zero is EXMEM_RAMDISK on d10
712        void *mem = _exmem_alloc(0,EXMEM_HEAP_SIZE,0);
713        if(mem) {
714#if defined(OPT_CHDK_IN_EXMEM)
715                // If loading CHDK into exmem then move heap start past the end of CHDK
716                // and reduce available space by CHDK size (MEMISOSIZE)
717                // round MEMISOSIZE up to next 4 byte boundary if needed (just in case)
718                exmem_start = mem + ((MEMISOSIZE+3)&0xFFFFFFFC);
719                exmem_size = EXMEM_HEAP_SIZE - EXMEM_HEAP_SKIP - ((MEMISOSIZE+3)&0xFFFFFFFC);
720#else
721                // Set start & size based on requested values
722                exmem_start = mem;
723                exmem_size = EXMEM_HEAP_SIZE - EXMEM_HEAP_SKIP;
724#endif
725                exmem_end = exmem_start + exmem_size;
726#if defined(OPT_EXMEM_TESTING)
727                // For testing exmem allocated memory for corruption from normal camera operations
728                // set the above #define. This will allocate the memory; but won't use it (exmem_heap is set to 0)
729                // Instead all the memory is filled with the guard value below.
730                // In gui_draw_debug_vals_osd (gui.c) the memory is tested for the guard value and if any
731                // corruption has occurred then info about the memory locations that were altered is displayed
732                // If OPT_EXMEM_TESTING is defined then OPT_CHDK_IN_EXMEM should not be set.
733                unsigned long *p;
734                for (p=(unsigned long*)exmem_start; p<(unsigned long*)exmem_end; p++) *p = 0xDEADBEEF;
735                exmem_heap = 0;
736#else
737                // Normal operation, use the suba allocation system to manage the memory block
738                exmem_heap = suba_init(exmem_start,exmem_size,1,8);
739#endif
740        }
741}
742
743void *malloc(unsigned size) {
744        if(exmem_heap)
745                return suba_alloc(exmem_heap,size,0);
746        else
747                return _malloc(size);
748}
749void free(void *p) {
750        if(exmem_heap && (p >= exmem_heap))
751                suba_free(exmem_heap,p);
752        else
753                _free(p);
754}
755
756int exmem_largest_block()
757{
758        extern int suba_largest_block(void*);
759        return suba_largest_block(exmem_heap);
760}
761// regular malloc
762#else
763void *malloc(long size) {
764    return _malloc(size);
765}
766
767void free(void *p) {
768    return _free(p);
769}
770#endif
771
772void *umalloc(long size) {
773    return _AllocateUncacheableMemory(size);
774}
775
776void ufree(void *p) {
777    return _FreeUncacheableMemory(p);
778}
779
780void *memcpy(void *dest, const void *src, long n) {
781    return _memcpy(dest, src, n);
782}
783
784void *memset(void *s, int c, int n) {
785    return _memset(s, c, n);
786}
787
788int memcmp(const void *s1, const void *s2, long n) {
789    return _memcmp(s1, s2, n);
790}
791
792void *memchr(const void *s, int c, int n) {
793#if !CAM_DRYOS
794        return _memchr(s,c,n);
795#else
796        while (n-- > 0) {
797                if (*(char *)s == c)
798                        return (void *)s;
799                s++;
800        }
801        return (void *)0;
802#endif
803}
804
805//----------------------------------------------------------------------------
806
807int rand(void) {
808    return _rand();
809}
810
811void *srand(unsigned int seed) {
812    return _srand(seed);
813}
814
815void qsort(void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2)) {
816    _qsort(__base, __nelem, __size, __cmp);
817}
818
819static int shutdown_disabled = 0;
820void disable_shutdown() {
821    if (!shutdown_disabled) {
822        _LockMainPower();
823        shutdown_disabled = 1;
824    }
825}
826
827void enable_shutdown() {
828    if (shutdown_disabled) {
829        _UnlockMainPower();
830        shutdown_disabled = 0;
831    }
832}
833void camera_shutdown_in_a_second(void){
834int i;
835//#if CAM_DRYOS
836//#else
837_SetAutoShutdownTime(1); // 1 sec
838for (i=0;i<200;i++) _UnlockMainPower(); // set power unlock counter to 200 or more, because every keyboard function call try to lock power again ( if "Disable LCD off" menu is "alt" or "script").
839//#endif
840}
841
842unsigned int GetJpgCount(void){
843 return strtol(camera_jpeg_count_str(),((void*)0),0);
844}
845
846unsigned int GetRawCount(void){
847 return GetFreeCardSpaceKb()/((hook_raw_size() / 1024)+GetFreeCardSpaceKb()/GetJpgCount());
848}
849
850void EnterToCompensationEVF(void)
851{
852  _EnterToCompensationEVF();
853}
854
855void ExitFromCompensationEVF()
856{
857  _ExitFromCompensationEVF();
858}
859
860void TurnOnBackLight(void)
861{
862  _TurnOnBackLight();
863}
864
865void TurnOffBackLight(void)
866{
867  _TurnOffBackLight();
868}
869
870void DoAFLock(void)
871{
872  _DoAFLock();
873}
874
875void UnlockAF(void)
876{
877  _UnlockAF();
878}
879
880#if CAM_MULTIPART
881
882#define SECTOR_SIZE 512
883static char *mbr_buf=(void*)0;
884static unsigned long drive_sectors;
885
886int is_mbr_loaded()
887{
888        return (mbr_buf == (void*)0) ? 0 : 1;
889}
890
891#ifndef CAM_DRYOS
892
893int mbr_read(char* mbr_sector, unsigned long drive_total_sectors, unsigned long *part_start_sector,  unsigned long *part_length){
894// return value: 1 - success, 0 - fail
895// called only in VxWorks
896
897 int offset=0x10; // points to partition #2
898 int valid;
899
900 if ((mbr_sector[0x1FE]!=0x55) || (mbr_sector[0x1FF]!=0xAA)) return 0; // signature check
901
902 mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
903 _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
904 drive_sectors=drive_total_sectors;
905
906 while(offset>=0) {
907
908  *part_start_sector=(*(unsigned short*)(mbr_sector+offset+0x1C8)<<16) | *(unsigned short*)(mbr_sector+offset+0x1C6);
909  *part_length=(*(unsigned short*)(mbr_sector+offset+0x1CC)<<16) | *(unsigned short*)(mbr_sector+offset+0x1CA);
910
911  valid= (*part_start_sector) && (*part_length) &&
912         (*part_start_sector<=drive_total_sectors) &&
913         (*part_start_sector+*part_length<=drive_total_sectors) &&
914         ((mbr_sector[offset+0x1BE]==0) || (mbr_sector[offset+0x1BE]==0x80)); // status: 0x80 (active) or 0 (non-active)
915
916  if (valid && ((mbr_sector[0x1C2+offset]==0x0B) || (mbr_sector[0x1C2+offset]==0x0C))) break;   // FAT32 secondary partition
917
918  offset-=0x10;
919
920 }
921
922 return valid;
923}
924
925#else
926
927int mbr_read_dryos(unsigned long drive_total_sectors, char* mbr_sector ){
928// Called only in DRYOS
929 mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
930 _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
931 drive_sectors=drive_total_sectors;
932 return drive_total_sectors;
933}
934
935#endif
936
937int get_part_count(void){
938 unsigned long part_start_sector, part_length;
939 char part_status, part_type;
940 int i;
941 int count=0;
942 if (is_mbr_loaded())
943 {
944         for (i=0; i<=1;i++){
945          part_start_sector=(*(unsigned short*)(mbr_buf+i*16+0x1C8)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1C6);
946          part_length=(*(unsigned short*)(mbr_buf+i*16+0x1CC)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1CA);
947          part_status=mbr_buf[i*16+0x1BE];
948          part_type=mbr_buf[0x1C2+i*16];
949          if ( part_start_sector && part_length && part_type && ((part_status==0) || (part_status==0x80)) ) count++;
950         }
951 }
952 return count;
953}
954
955void swap_partitions(void){
956        if (is_mbr_loaded())
957        {
958         int i;
959         char c;
960         for(i=0;i<16;i++){
961          c=mbr_buf[i+0x1BE];
962          mbr_buf[i+0x1BE]=mbr_buf[i+0x1CE];
963          mbr_buf[i+0x1CE]=c;
964         }
965         _WriteSDCard(0,0,1,mbr_buf);
966        }
967}
968
969void create_partitions(void){
970        if (is_mbr_loaded())
971        {
972         unsigned long start, length;
973         char type;
974
975         _memset(mbr_buf,0,SECTOR_SIZE);
976
977         start=1; length=2*1024*1024/SECTOR_SIZE; //2 Mb
978         type=1; // FAT primary
979         mbr_buf[0x1BE + 4]=type;
980         mbr_buf[0x1BE + 8]=start;   mbr_buf[0x1BE + 9]=start>>8;   mbr_buf[0x1BE + 10]=start>>16;  mbr_buf[0x1BE + 11]=start>>24;
981         mbr_buf[0x1BE + 12]=length; mbr_buf[0x1BE + 13]=length>>8; mbr_buf[0x1BE + 14]=length>>16; mbr_buf[0x1BE + 15]=length>>24;
982
983         start=start+length; length=drive_sectors-start-1;
984         type=0x0B;  //FAT32 primary;
985         mbr_buf[0x1CE + 4]=type;
986         mbr_buf[0x1CE + 8]=start;   mbr_buf[0x1CE + 9]=start>>8;   mbr_buf[0x1CE + 10]=start>>16;  mbr_buf[0x1CE + 11]=start>>24;
987         mbr_buf[0x1CE + 12]=length; mbr_buf[0x1CE + 13]=length>>8; mbr_buf[0x1CE + 14]=length>>16; mbr_buf[0x1CE + 15]=length>>24;
988
989         mbr_buf[0x1FE]=0x55; mbr_buf[0x1FF]=0xAA; // signature;
990
991         _WriteSDCard(0,0,1,mbr_buf);
992        }
993}
994
995#endif
996
997int mute_on_zoom(int x){
998 static int old_busy=0;
999 int busy=zoom_busy||focus_busy;
1000 if (old_busy!=busy) {
1001  if (busy) {
1002#if CAM_CAN_MUTE_MICROPHONE
1003   if (conf.mute_on_zoom) _TurnOffMic();
1004#endif
1005   }
1006   else {
1007#if CAM_CAN_MUTE_MICROPHONE
1008  if (conf.mute_on_zoom) _TurnOnMic();
1009#endif
1010#if CAM_EV_IN_VIDEO
1011  if (get_ev_video_avail()) set_ev_video_avail(0);
1012#endif
1013  }
1014  old_busy=busy;
1015 }
1016 return x; // preserve R0 if called from assembler
1017}
1018
1019
1020#if CAM_AF_SCAN_DURING_VIDEO_RECORD
1021void MakeAFScan(void){
1022 int a=0, save;
1023 if (zoom_busy || focus_busy) return;
1024 save=some_flag_for_af_scan;
1025 some_flag_for_af_scan=0;
1026#if CAM_AF_SCAN_DURING_VIDEO_RECORD == 2
1027 parameter_for_af_scan=3;
1028#endif
1029 _MakeAFScan(&a, 3);
1030 some_flag_for_af_scan=save;
1031#if defined(CAMERA_g12)
1032 int ae_lock;
1033 get_property_case(PROPCASE_AE_LOCK,&ae_lock,sizeof(ae_lock));
1034 if (ae_lock == 0)                                              // AE not locked so ensure it is unlocked after re-focus
1035         _ExpCtrlTool_StartContiAE(0,0);
1036 else                                                                   // AE locked before so re-lock after
1037         _ExpCtrlTool_StopContiAE(0,0);
1038#else
1039 _ExpCtrlTool_StartContiAE(0,0);
1040#endif
1041}
1042#endif
1043
1044long __attribute__((weak)) get_jogdial_direction(void){
1045 return 0;
1046}
1047
1048#if defined (DNG_EXT_FROM)
1049
1050#define DNG_EXT_TO ".DNG"
1051
1052typedef int(*p_some_f)(char*, int);
1053
1054extern p_some_f some_f_for_dng;  // camera variable!
1055extern char* second_ext_for_dng; // camera variable!
1056
1057p_some_f default_some_f;
1058char *   default_second_ext;
1059
1060char *_strstr (const char *s1, const char *s2)
1061{
1062  const char *p = s1;
1063  const int len = _strlen (s2);
1064
1065  for (; (p = _strchr (p, *s2)) != 0; p++)
1066    {
1067      if (_strncmp (p, s2, len) == 0)
1068        return (char *)p;
1069    }
1070  return (0);
1071}
1072
1073
1074int my_some_f(char *s, int x){
1075  char *f;
1076  f=_strstr(s, DNG_EXT_FROM);
1077  if (f) _memcpy(f, DNG_EXT_TO, sizeof(DNG_EXT_TO)-1);
1078  return default_some_f(s, x);
1079}
1080
1081void save_ext_for_dng(void){
1082 default_some_f=some_f_for_dng;
1083 default_second_ext=second_ext_for_dng;
1084}
1085
1086void change_ext_to_dng(void){
1087 some_f_for_dng=my_some_f;
1088 second_ext_for_dng=DNG_EXT_TO;
1089}
1090
1091void change_ext_to_default(void){
1092 some_f_for_dng=default_some_f;
1093 second_ext_for_dng=default_second_ext;
1094}
1095
1096#endif
1097
1098
1099static long drv_struct[16];
1100
1101long dh_err()
1102{
1103    return -1;
1104}
1105
1106void drv_self_hide()
1107{
1108#if !CAM_DRYOS
1109    long drvnum;
1110
1111    drvnum = _iosDrvInstall(dh_err,dh_err,dh_err,dh_err,dh_err,dh_err,dh_err);
1112    if (drvnum >= 0)
1113        _iosDevAdd(drv_struct, "A/DISKBOOT.BIN", drvnum);
1114#endif
1115}
1116
1117void drv_self_unhide(){
1118#if !CAM_DRYOS
1119 _iosDevDelete(drv_struct);
1120#endif
1121}
1122
1123int  apex2us(int apex_tv){
1124#if CAM_EXT_TV_RANGE
1125/*
1126 Extended Tv, by barberofcivil, http://chdk.setepontos.com/index.php/topic,4392.0.html
1127 Explanation by reyalP:
1128 In every port, the original shutter overrides (as opposed to super long exposure) worked by
1129 setting the propcase values at some point after auto-exposure has happened (except in manual
1130 modes, where the manual control propcases may be used instead). The Canon code previously took
1131 these values unchanged for short exposures. In newer cameras, like on the SX10 / SD980, the value
1132 is changed, apparently some time after it has been retrieved from the propcase. We know this is
1133 the case, because the propcase value itself doesn't get clamped to the allowed range (if it did,
1134 barberofcivil's code wouldn't work).
1135*/
1136        short tv;
1137        tv = shooting_get_tv96();
1138        if (tv<-576 || tv!=apex_tv) return 1000000.0*pow(2.0, -tv/96.0);
1139        else return _apex2us(apex_tv);
1140#else
1141        return 0;
1142#endif
1143}
1144
1145void PostLogicalEventForNotPowerType(unsigned id, unsigned x) {
1146        _PostLogicalEventForNotPowerType(id,x);
1147}
1148
1149void PostLogicalEventToUI(unsigned id, unsigned x) {
1150        _PostLogicalEventToUI(id,x);
1151}
1152
1153void SetLogicalEventActive(unsigned id, unsigned state) {
1154        _SetLogicalEventActive(id, state);
1155}
1156
1157void SetScriptMode(unsigned mode) {
1158        _SetScriptMode(mode);
1159}
1160
1161// TODO this belongs lib.c, but not all cameras include it
1162// same as bitmap width for most cameras, override in platform/sub/lib.c as needed
1163int __attribute__((weak)) vid_get_viewport_width() {
1164        return vid_get_bitmap_screen_width();
1165}
1166
1167// same as viewport width for most cameras, override in platform/sub/lib.c as needed
1168int __attribute__((weak)) vid_get_viewport_buffer_width() {
1169        return vid_get_viewport_width();
1170}
1171
1172// viewport x offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1173int __attribute__((weak)) vid_get_viewport_xoffset() {
1174        return 0;
1175}
1176
1177// viewport y offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1178int __attribute__((weak)) vid_get_viewport_yoffset() {
1179        return 0;
1180}
1181
1182// viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1183// returns the byte offset into the viewport buffer where the image pixels start (to skip any black borders)
1184// see G12 port for sample implementation
1185int __attribute__((weak)) vid_get_viewport_image_offset() {
1186        return 0;
1187}
1188
1189// viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1190// returns the byte offset to skip at the end of a viewport buffer row to get to the next row.
1191// see G12 port for sample implementation
1192int __attribute__((weak)) vid_get_viewport_row_offset() {
1193        return 0;
1194}
1195
1196// for cameras with two (or more?) RAW buffers this can be used to speed up DNG creation by
1197// calling reverse_bytes_order only once. Override in platform/sub/lib.c
1198char __attribute__((weak)) *hook_alt_raw_image_addr() {
1199        return hook_raw_image_addr();
1200}
1201
1202void __attribute__((weak)) vid_turn_off_updates()
1203{
1204}
1205
1206void __attribute__((weak)) vid_turn_on_updates()
1207{
1208}
1209
1210// use _GetFocusLensSubjectDistance for this on dryos, vx functions are basically equivlent
1211// not used in CHDK currently for either OS
1212#ifdef CAM_DRYOS
1213long __attribute__((weak)) _GetCurrentTargetDistance()
1214{
1215        return _GetFocusLensSubjectDistance();
1216}
1217#endif
1218
1219#ifdef CAM_CHDK_PTP
1220int add_ptp_handler(int opcode, ptp_handler handler, int unknown)
1221{
1222  return _add_ptp_handler(opcode,handler,unknown);
1223}
1224
1225// this would make more sense in generic/main.c but not all a cameras use it
1226void init_chdk_ptp_task() {
1227  _CreateTask("InitCHDKPTP", 0x19, 0x2000, init_chdk_ptp, 0);
1228};
1229
1230#endif
1231
1232void ExitTask()
1233{
1234  _ExitTask();
1235}
1236
1237// TODO not in sigs for vx yet
1238#ifndef CAM_DRYOS
1239void __attribute__((weak)) _reboot_fw_update(const char *fw_update)
1240{
1241        return;
1242}
1243#endif
1244
1245// TODO mode switch function should detect if USB is connected or not,
1246// and do regular or special switch as needed
1247#ifdef CAM_DRYOS
1248int __attribute__((weak)) switch_mode_usb(int mode)
1249{
1250#ifdef CAM_CHDK_PTP
1251    if ( mode == 0 ) {
1252        _Rec2PB();
1253        _set_control_event(0x80000902); // 0x10A5 ConnectUSBCable
1254    } else if ( mode == 1 ) {
1255        _set_control_event(0x902); // 0x10A6 DisconnectUSBCable
1256        _PB2Rec();
1257    } else return 0;
1258    return 1;
1259#else
1260  return 0;
1261#endif // CAM_CHDK_PTP
1262}
1263
1264#else // vxworks
1265// this doesn't need any special functions so it's defined even without CHDK_CAM_PTP
1266int __attribute__((weak)) switch_mode_usb(int mode)
1267{
1268    if ( mode == 0 ) {
1269        levent_set_play();
1270    } else if ( mode == 1 ) {
1271        levent_set_record();
1272    } else return 0;
1273    return 1;
1274}
1275#endif // vxworks
1276
1277/*
1278// this wrapper isn't currently needed
1279// 7 calls functions and sets some MMIOs, but doesn't disable caches and actually restart
1280// 3 skips one function call on some cameras, but does restart
1281void Restart(unsigned option) {
1282        _Restart(option);
1283}
1284*/
1285
Note: See TracBrowser for help on using the repository browser.