root/platform/generic/wrappers.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. isdigit
  2. isspace
  3. isalpha
  4. isupper
  5. islower
  6. ispunct
  7. isxdigit
  8. tolower
  9. toupper
  10. submenu_sort_arm
  11. msleep
  12. task_lock
  13. task_unlock
  14. task_name
  15. task_id_list_get
  16. get_property_case
  17. set_property_case
  18. get_parameter_size
  19. get_parameter_data
  20. set_parameter_data
  21. get_uiprop_value
  22. mark_filesystem_bootable
  23. vid_bitmap_refresh
  24. lens_get_zoom_pos
  25. lens_get_zoom_point
  26. lens_set_zoom_point
  27. lens_set_zoom_speed
  28. lens_set_zoom_point
  29. lens_set_zoom_speed
  30. lens_set_focus_pos
  31. play_sound
  32. stat_get_vbatt
  33. GetAdChValue
  34. get_battery_temp
  35. get_ccd_temp
  36. get_optical_temp
  37. get_tick_count
  38. PutInNdFilter
  39. PutOutNdFilter
  40. shooting_get_nd_value_ev96
  41. shooting_get_nd_current_ev96
  42. shooting_get_current_tv96
  43. shooting_get_current_av96
  44. IsStrobeChargeCompleted
  45. SetCurrentCaptureModeType
  46. GetUsableMinAv
  47. GetUsableMaxAv
  48. UnsetZoomForMovie
  49. MoveIrisWithAv
  50. ExpCtrlTool_StartContiAE
  51. ExpCtrlTool_StopContiAE
  52. SetAE_ShutterSpeed
  53. takeFileIOSemaphore
  54. open
  55. close
  56. write
  57. read
  58. lseek
  59. mkdir
  60. mkdir_if_not_exist
  61. remove
  62. fw_closedir
  63. get_fstype
  64. fw_opendir
  65. fw_readdir
  66. fw_opendir
  67. fw_readdir
  68. stat
  69. fopen
  70. fclose
  71. fread
  72. fwrite
  73. fseek
  74. feof
  75. fflush
  76. fgets
  77. rename
  78. GetFreeCardSpaceKb
  79. GetTotalCardSpaceKb
  80. errnoOfTaskGet
  81. strlen
  82. strcmp
  83. strncmp
  84. strchr
  85. strcpy
  86. strncpy
  87. strcat
  88. strrchr
  89. strtol
  90. strtoul
  91. strpbrk
  92. sprintf
  93. strerror
  94. time
  95. utime
  96. localtime
  97. strftime
  98. mktime
  99. set_clock
  100. _log
  101. _log10
  102. _pow
  103. _sqrt
  104. exmem_alloc_cached
  105. exmem_alloc_uncached
  106. exmem_free_uncached
  107. canon_malloc
  108. canon_free
  109. umalloc
  110. ufree
  111. memcpy
  112. memset
  113. memcmp
  114. memchr
  115. GetMemInfo
  116. rand
  117. srand
  118. qsort
  119. disable_shutdown
  120. enable_shutdown
  121. camera_shutdown_in_a_second
  122. EnterToCompensationEVF
  123. ExitFromCompensationEVF
  124. TurnOnBackLight
  125. TurnOffBackLight
  126. TurnOnDisplay
  127. TurnOffDisplay
  128. DoAELock
  129. UnlockAE
  130. DoAFLock
  131. UnlockAF
  132. EngDrvRead
  133. is_mbr_loaded
  134. mbr_read
  135. mbr_read_dryos
  136. get_part_count
  137. get_part_type
  138. is_partition_changed
  139. swap_partitions
  140. get_active_partition
  141. create_partitions
  142. swap_partitions
  143. get_part_count
  144. get_part_type
  145. get_active_partition
  146. is_partition_changed
  147. mute_on_zoom
  148. MakeAFScan
  149. get_jogdial_direction
  150. JogDial_CW
  151. JogDial_CCW
  152. my_some_f
  153. save_ext_for_dng
  154. change_ext_to_dng
  155. change_ext_to_default
  156. dh_err
  157. drv_self_hide
  158. drv_self_unhide
  159. apex2us
  160. PostLogicalEventForNotPowerType
  161. PostLogicalEventToUI
  162. SetLogicalEventActive
  163. SetScriptMode
  164. vid_get_viewport_width
  165. vid_get_viewport_byte_width
  166. vid_get_viewport_yscale
  167. vid_get_viewport_xoffset
  168. vid_get_viewport_yoffset
  169. vid_get_viewport_display_xoffset
  170. vid_get_viewport_display_yoffset
  171. vid_get_viewport_type
  172. hook_alt_raw_image_addr
  173. vid_turn_off_updates
  174. vid_turn_on_updates
  175. _GetCurrentTargetDistance
  176. add_ptp_handler
  177. get_ptp_file_buf_size
  178. get_ptp_file_buf
  179. CreateTask
  180. ExitTask
  181. _reboot_fw_update
  182. switch_mode_usb
  183. switch_mode_usb
  184. SetFileAttributes
  185. vid_get_viewport_display_xoffset_proper
  186. vid_get_viewport_display_yoffset_proper
  187. vid_get_viewport_buffer_width_proper
  188. vid_get_viewport_width_proper
  189. vid_get_viewport_height_proper
  190. vid_get_viewport_width_proper
  191. vid_get_viewport_height_proper
  192. vid_get_viewport_fullscreen_height
  193. vid_get_viewport_fullscreen_width
  194. vid_get_palette_type
  195. vid_get_palette_size
  196. vid_get_aspect_ratio
  197. vid_get_bitmap_active_buffer
  198. vid_get_bitmap_active_palette
  199. vid_get_viewport_active_buffer
  200. dbg_printf
  201. _srand
  202. _rand
  203. ARM_usb_HPtimer_good
  204. ARM_usb_HPtimer_bad
  205. start_usb_HPtimer
  206. stop_usb_HPtimer
  207. SetHPTimerAfterNow
  208. CancelHPTimer
  209. update_hdmi_power_override
  210. DisableCamError
  211. DebugAssert
  212. CreateBinarySemaphore
  213. TakeSemaphore
  214. GiveSemaphore
  215. SetVideoOutType
  216. GetVideoOutType

   1 #include "camera.h"
   2 #include "lolevel.h"
   3 #include "platform.h"
   4 #include "raw_buffer.h"
   5 #include "conf.h"
   6 #include "math.h"
   7 #include "levent.h"
   8 #include "stdlib.h"
   9 #include "ptp_chdk.h"
  10 #include "live_view.h"
  11 #include "usb_remote.h"
  12 
  13 // arbitrary timeout for canon heap semaphore
  14 #if !CAM_DRYOS
  15 #define CANON_HEAP_SEM_TIMEOUT 1000
  16 extern int canon_heap_sem;
  17 #endif
  18 
  19 //----------------------------------------------------------------------------
  20 // Char Wrappers (VxWorks - ARM stubs)
  21 
  22 #if !CAM_DRYOS
  23 
  24 int isdigit(int c) { return _isdigit(c); }
  25 int isspace(int c) { return _isspace(c); }
  26 int isalpha(int c) { return _isalpha(c); }
  27 int isupper(int c) { return _isupper(c); }
  28 int islower(int c) { return _islower(c); }
  29 int ispunct(int c) { return _ispunct(c); }
  30 int isxdigit(int c) { return _isxdigit(c); }
  31 
  32 int tolower(int c) { return _tolower(c); }
  33 int toupper(int c) { return _toupper(c); }
  34 
  35 #endif
  36 
  37 //----------------------------------------------------------------------------
  38 
  39 int submenu_sort_arm(const void* v1, const void* v2)
  40 {
  41     extern int submenu_sort(const void* v1, const void* v2);
  42     return submenu_sort(v1, v2);
  43 }
  44 
  45 //----------------------------------------------------------------------------
  46 
  47 void msleep(long msec)
  48 {
  49     _SleepTask(msec);
  50 }
  51 
  52 #ifndef CAM_DRYOS
  53 long task_lock()
  54 {
  55     return _taskLock();
  56 }
  57 
  58 long task_unlock()
  59 {
  60     return _taskUnlock();
  61 }
  62 
  63 const char *task_name(int id)
  64 {
  65     return _taskName(id);
  66 }
  67 
  68 int task_id_list_get(int *idlist,int size)
  69 {
  70     return _taskIdListGet(idlist,size);
  71 }
  72 #endif
  73 
  74 long get_property_case(long id, void *buf, long bufsize)
  75 {
  76 // workaround for missing PROPCASE_SHOOTING
  77 #if CAM_PROPSET == 7 || CAM_PROPSET == 9 || CAM_PROPSET == 10 || CAM_PROPSET == 11 || CAM_PROPSET == 12 || CAM_PROPSET == 13
  78     if(id==PROPCASE_SHOOTING) {
  79         int r=_GetPropertyCase(PROPCASE_SHOOTING_STATE, buf, bufsize);
  80         // 1 50ms after half press, 2 after exp hook, 3 while shooting
  81         // propset 6 has similar procase id 351, goes 3->0 when get_shooting goes false
  82         // propset 4 and 5 id 352 is similar but only goes to 2
  83         // (4 per https://chdk.setepontos.com/index.php?topic=11604.msg113712#msg113712)
  84         if(*(char *)buf > 1) {
  85             *(char *)buf = 1;
  86         } else {
  87             *(char *)buf = 0;
  88         }
  89         return r;
  90     }
  91 #endif
  92     return _GetPropertyCase(id, buf, bufsize);
  93 }
  94 
  95 long set_property_case(long id, void *buf, long bufsize)
  96 {
  97     // ignore set on fake prop
  98 #if CAM_PROPSET == 7 || CAM_PROPSET == 9 || CAM_PROPSET == 10 || CAM_PROPSET == 11 || CAM_PROPSET == 12 || CAM_PROPSET == 13
  99     if(id==PROPCASE_SHOOTING) {
 100         return 0;
 101     }
 102 #endif
 103 
 104     return _SetPropertyCase(id, buf, bufsize);
 105 }
 106 
 107 // FlashParamsTable entries point to this structure
 108 #if CAM_FLASHPARAMS_VERSION == 2
 109 // ixus30_sd200, ixus40_sd300
 110 typedef struct
 111 {
 112     short   unk1;
 113     short   unk2;
 114     void*   data;   // Pointer to param data
 115     short   size;   // param size
 116     short   unk3;
 117     int     unk4;
 118     short   unk5;
 119     short   unk6;
 120 } flashParam;
 121 #else
 122 // version 3 (every camera from 2005 on)
 123 typedef struct
 124 {
 125     void*   data;   // Pointer to param data
 126     short   unk1;
 127     short   size;   // param size
 128     short   unk2;
 129     short   unk3;
 130     short   unk4;
 131     char    unk5;
 132     char    unk6;
 133 } flashParam;
 134 #endif // CAM_FLASHPARAMS_VERSION
 135 
 136 short get_parameter_size(long id)
 137 {
 138     extern flashParam* FlashParamsTable[];
 139 
 140     if ((id >= 0) && (id < get_flash_params_count()))
 141         return FlashParamsTable[id]->size;
 142 
 143     return 0;
 144 }
 145 
 146 long get_parameter_data(long id, void *buf, long bufsize)
 147 {
 148     return _GetParameterData(id|PARAMETER_DATA_FLAG, buf, bufsize);
 149 }
 150 
 151 long set_parameter_data(long id, void *buf, long bufsize)
 152 {
 153     return _SetParameterData(id|PARAMETER_DATA_FLAG, buf, bufsize);
 154 }
 155 
 156 short __attribute__((weak)) get_uiprop_value(unsigned long id)
 157 {
 158     // avoid asserts: return 0 if id is above limit
 159     if (id >= uiprop_count)
 160         return 0;
 161     return _PTM_GetCurrentItem(id|0x8000);
 162 }
 163 
 164 void mark_filesystem_bootable()
 165 {
 166 #ifdef  CAM_DRYOS_2_3_R47
 167     // DryOS release 47 (2011) no longer has the UpdateMBROnFlash function to write the master boot record on
 168     // the SD card. Instead it has seperate functions for writing the 'BOOTDISK' and 'SCRIPT' signatures to
 169     // the MBR. The firmware function also takes care of writing the bootdisk signature to the correct location
 170     // for FAT32 formatted cards.
 171     _MakeSDCardBootable(0);
 172 #else
 173     _UpdateMBROnFlash(0, 0x40, "BOOTDISK");
 174 #endif
 175 }
 176 
 177 // d6 cams must define, don't have usable _RefreshPhysicalScreen
 178 #ifndef THUMB_FW
 179 void __attribute__((weak)) vid_bitmap_refresh()
 180 {
 181     _RefreshPhysicalScreen(1);
 182 }
 183 #endif
 184 
 185 long lens_get_zoom_pos()
 186 {
 187     return _GetZoomLensCurrentPosition();
 188 }
 189 
 190 long lens_get_zoom_point()
 191 {
 192     return _GetZoomLensCurrentPoint();
 193 }
 194 
 195 #ifdef CAM_ILC
 196 void lens_set_zoom_point(long newpt) {}
 197 void lens_set_zoom_speed(long newspd) {}
 198 #else // !CAM_ILC
 199 
 200 #if defined(CAM_USE_ALT_SET_ZOOM_POINT)
 201 static int mz_speed = 3; // max speed on cameras with multi-speed zoom, ignored on others
 202 #endif
 203 
 204 void lens_set_zoom_point(long newpt)
 205 {
 206     if (newpt < 0) {
 207         newpt = 0;
 208     } else if (newpt >= zoom_points) {
 209         newpt = zoom_points-1;
 210     }
 211 
 212 #if defined(CAM_USE_ALT_SET_ZOOM_POINT)
 213 
 214         if (lens_get_zoom_point() != newpt)
 215         {
 216                 int digizoom_pos;
 217                 get_property_case(PROPCASE_DIGITAL_ZOOM_POSITION,&digizoom_pos,sizeof(digizoom_pos));
 218                 // check current digital zoom mode & state
 219                 // state == 1 && mode == 0 --> Digital Zoom Standard
 220                 if ((shooting_get_digital_zoom_state() == 1) && (shooting_get_digital_zoom_mode() == 0) && (digizoom_pos != 0))
 221                 {
 222                         // reset digital zoom in case camera is in this zoom range
 223                         extern void _PT_MoveDigitalZoomToWide();
 224                         _PT_MoveDigitalZoomToWide();
 225                 }
 226 
 227                 // starting around digic 4 - _MoveZoomLensWithPoint crashes many cameras
 228                 // _PT_MoveOpticalZoomAt works, and updates PROPCASE_OPTICAL_ZOOM_POSITION; but doesn't wait for zoom to finish
 229         // _MoveOpticalZoom at is underlying function that accepts speed
 230         // updates jpeg engine distortion correction setting, maintains focus position
 231                 extern void _MoveOpticalZoomAt(long pt,int speed);
 232                 _MoveOpticalZoomAt(newpt,mz_speed);
 233 
 234                 // have to sleep here, zoom_busy set in another task, without sleep this will hang
 235                 while (zoom_busy) msleep(10);
 236 
 237                 // g10,g12 & sx30 only use this value for optical zoom
 238                 zoom_status=ZOOM_OPTICAL_MAX;
 239         }
 240 #else   // !(CAM_USE_ALT_SET_ZOOM_POINT)
 241     _MoveZoomLensWithPoint((short*)&newpt);
 242 
 243     // tight loop here hangs some cameras (the task that clears zoom_busy
 244     // is starved; seen at least on S95 and IXUS 220), so stick in a sleep
 245     while (zoom_busy) msleep(10);
 246 
 247     if (newpt==0) zoom_status=ZOOM_OPTICAL_MIN;
 248     else if (newpt >= zoom_points) zoom_status=ZOOM_OPTICAL_MAX;
 249     else zoom_status=ZOOM_OPTICAL_MEDIUM;
 250     _SetPropertyCase(PROPCASE_OPTICAL_ZOOM_POSITION, &newpt, sizeof(newpt));
 251 #endif  // !(CAM_USE_ALT_SET_ZOOM_POINT)
 252 }
 253 
 254 void lens_set_zoom_speed(long newspd)
 255 {
 256 // translate % to speed values for _MoveOpticalZoomAt
 257 // which correspond to different speeds varies by camera
 258 #if defined(CAM_USE_ALT_SET_ZOOM_POINT)
 259     if (newspd < 25) {
 260         mz_speed = 0;
 261     } else if (newspd < 50) {
 262         mz_speed = 1;
 263     } else if (newspd < 75) {
 264         mz_speed = 2;
 265     } else {
 266         mz_speed = 3;
 267     }
 268 #else  // !CAM_USE_ALT_SET_ZOOM_POINT
 269     // a few old cameras support setting by %
 270     if (newspd < 5) {
 271         newspd = 5;
 272     } else if (newspd > 100) {
 273         newspd = 100;
 274     }
 275     _SetZoomActuatorSpeedPercent((short*)&newspd);
 276 #endif // !CAM_USE_ALT_SET_ZOOM_POINT
 277 }
 278 #endif // !CAM_ILC
 279 
 280 void lens_set_focus_pos(long newpos)
 281 {
 282     if (newpos >= CAMERA_MAX_DIST) newpos = INFINITY_DIST; // Set to infinity value that will work on all cameras
 283     _MoveFocusLensToDistance((short*)&newpos);
 284     while ((shooting_is_flash_ready()!=1) || (focus_busy)) msleep(10);
 285     newpos = _GetFocusLensSubjectDistance();
 286     _SetPropertyCase(PROPCASE_SUBJECT_DIST1, &newpos, sizeof(newpos));
 287     _SetPropertyCase(PROPCASE_SUBJECT_DIST2, &newpos, sizeof(newpos));
 288 }
 289 
 290 void play_sound(unsigned sound)
 291 {
 292         static const int sounds[]={ 0x2001, //startup sound
 293                                 0x2002, //shutter sound
 294                                 0x2003, //button press sound
 295                                 0x2004, //self-timer sound
 296                                 0xC211, //short beep
 297                                 50000,  // AF confirmation
 298                                 0xC507, // error beep imo
 299                                 0x400D, // LONG ERROR BEEP CONTINIUOUS- warning, cannot be stopped (yet)
 300                             };
 301     if(sound >= sizeof(sounds)/sizeof(sounds[0]))
 302         return;
 303 
 304     _PT_PlaySound(sounds[sound], 0, 0);
 305 }
 306 
 307 long stat_get_vbatt()
 308 {
 309     return _VbattGet();
 310 }
 311 
 312 //========================================================================
 313 // NOTE : some early DryOS cameras need a pointer rather than a value and the 
 314 //        xus30 & 40 want a char * string here. Override this funcion in camera's
 315 //        lib.c file if impelementing remote via battery 3rd terminal functionality.
 316 //        see http://chdk.setepontos.com/index.php?topic=10385.msg109353#msg109353
 317 //
 318 int __attribute__((weak)) GetAdChValue(int channel)        
 319 {
 320 #ifdef CAM_DRYOS
 321     return _GetAdChValue(channel);
 322 #else
 323     return _GetAdChValue(&channel);
 324 #endif
 325 }
 326 
 327 int get_battery_temp()
 328 {
 329     return _GetBatteryTemperature();
 330 }
 331 
 332 int get_ccd_temp()
 333 {
 334     return _GetCCDTemperature();
 335 }
 336 
 337 int get_optical_temp()
 338 {
 339     return _GetOpticalTemperature();
 340 }
 341 
 342 long get_tick_count()
 343 {
 344 long t;
 345 #if !CAM_DRYOS
 346     _GetSystemTime(&t);
 347     return t;
 348 #else
 349     return (int)_GetSystemTime(&t);
 350 #endif
 351 }
 352 
 353 #if CAM_HAS_ND_FILTER
 354 void PutInNdFilter()                { _PutInNdFilter(); }
 355 void PutOutNdFilter()               { _PutOutNdFilter(); }
 356 #endif
 357 
 358 short shooting_get_nd_value_ev96(void)
 359 {
 360 #if CAM_HAS_ND_FILTER
 361     return _get_nd_value();
 362 #else
 363     return 0;
 364 #endif
 365 }
 366 
 367 short shooting_get_nd_current_ev96(void)
 368 {
 369 #if CAM_HAS_ND_FILTER
 370     return _get_current_nd_value();
 371 #else
 372     return 0;
 373 #endif
 374 }
 375 
 376 long shooting_get_current_tv96()
 377 {
 378     // old cameras crash if _GetCurrentShutterSpeed called when inactive
 379     if(!shooting_get_imager_active()) {
 380         return SHOOTING_TV96_INVALID;
 381     }
 382     return _GetCurrentShutterSpeed();
 383 }
 384 long shooting_get_current_av96()    { return _GetCurrentAvValue(); }
 385 
 386 long IsStrobeChargeCompleted()      { return _IsStrobeChargeCompleted(); }
 387 void SetCurrentCaptureModeType()    { _SetCurrentCaptureModeType(); }
 388 
 389 #if CAM_HAS_IRIS_DIAPHRAGM
 390 // returns available Av range in AV96 on cameras with iris.
 391 // Function exists on a few later cameras without iris, behavior unknown.
 392 // Appears to be the full range, including smaller (higher F/ number) than available in Canon UI.
 393 // Note Min = smallest physical aperture = largest Av96 value
 394 short GetUsableMinAv(void) { return _GetUsableMinAv(); }
 395 short GetUsableMaxAv(void) { return _GetUsableMaxAv(); }
 396 #endif
 397 
 398 #if CAM_CAN_UNLOCK_OPTICAL_ZOOM_IN_VIDEO
 399 void UnsetZoomForMovie()            { _UnsetZoomForMovie(); }
 400 #endif
 401 
 402 #ifdef CAM_AV_OVERRIDE_IRIS_FIX
 403 int MoveIrisWithAv(short *v)        { return _MoveIrisWithAv(v); }
 404 #endif
 405 
 406 #if CAM_EV_IN_VIDEO
 407 void ExpCtrlTool_StartContiAE(int v1, int v2)   { _ExpCtrlTool_StartContiAE(v1,v2); }
 408 void ExpCtrlTool_StopContiAE(int v1, int v2)    { _ExpCtrlTool_StopContiAE(v1, v2); }
 409 short SetAE_ShutterSpeed(short *tv)             { return _SetAE_ShutterSpeed(tv); }
 410 #endif
 411 
 412 //----------------------------------------------------------------------------
 413 // I/O wrappers
 414 
 415 /*int creat (const char *name, int flags)
 416 {
 417     return _creat(name, flags);
 418 }*/
 419 
 420 extern int fileio_semaphore;
 421 
 422 int takeFileIOSemaphore()
 423 {
 424     int timeout = CAM_FILEIO_SEM_TIMEOUT;
 425 #if defined(CAM_IS_VID_REC_WORKS)
 426     if (is_video_recording())
 427         timeout = CAM_FILEIO_SEM_TIMEOUT_VID;
 428 #endif
 429 #if defined(OPT_FILEIO_STATS)
 430     int t = get_tick_count();
 431 #endif
 432     // Check fileio semaphore with timeout, if not available we are probably recording video
 433     if (_TakeSemaphore(fileio_semaphore,timeout) & 1)
 434     {
 435 #if defined(OPT_FILEIO_STATS)
 436         camera_info.fileio_stats.fileio_semaphore_errors++;
 437 #endif
 438         return 0;
 439     }
 440 #if defined(OPT_FILEIO_STATS)
 441     t = get_tick_count() - t;
 442     if (t > camera_info.fileio_stats.max_semaphore_timeout)
 443         camera_info.fileio_stats.max_semaphore_timeout = t;
 444 #endif
 445     return 1;
 446 }
 447 
 448 int open (const char *name, int flags, int mode )
 449 {
 450 #if defined(OPT_FILEIO_STATS)
 451     camera_info.fileio_stats.open_count++;
 452 #endif
 453 #if !CAM_DRYOS
 454     // Adjust O_TRUNC and O_CREAT flags for VxWorks
 455     // Remove O_APPEND flag if present (not in VxWorks)
 456     flags = (flags & ~(O_TRUNC|O_CREAT|O_APPEND)) | ((flags & (O_TRUNC|O_CREAT)) << 1);
 457 #else
 458     if(!name || name[0]!='A')
 459         return -1;
 460 #endif
 461     int haveSemaphore = takeFileIOSemaphore();
 462     if (!haveSemaphore)
 463 #if defined(CAM_IS_VID_REC_WORKS)
 464         if (!conf.allow_unsafe_io)
 465 #endif
 466             return -1;
 467     int fd = _Open(name, flags, mode);
 468     if (haveSemaphore)
 469         _GiveSemaphore(fileio_semaphore);
 470 #if defined(OPT_FILEIO_STATS)
 471     if (fd == -1)
 472         camera_info.fileio_stats.open_fail_count++;
 473 #endif
 474     return fd;
 475 }
 476 
 477 int close (int fd)
 478 {
 479 #if defined(OPT_FILEIO_STATS)
 480     camera_info.fileio_stats.close_count++;
 481 #endif
 482     if (fd == -1)
 483     {
 484 #if defined(OPT_FILEIO_STATS)
 485         camera_info.fileio_stats.close_badfile_count++;
 486 #endif
 487         return -1;
 488     }
 489     int haveSemaphore = takeFileIOSemaphore();
 490     if (!haveSemaphore)
 491 #if defined(CAM_IS_VID_REC_WORKS)
 492         if (!conf.allow_unsafe_io)
 493 #endif
 494             return -1;
 495     int r = _Close(fd);
 496     if (haveSemaphore)
 497         _GiveSemaphore(fileio_semaphore);
 498 #if defined(OPT_FILEIO_STATS)
 499     if (r == -1)
 500         camera_info.fileio_stats.close_fail_count++;
 501 #endif
 502     return r;
 503 }
 504 
 505 int write (int fd, const void *buffer, long nbytes)
 506 {
 507     if (fd == -1)
 508     {
 509 #if defined(OPT_FILEIO_STATS)
 510         camera_info.fileio_stats.write_badfile_count++;
 511 #endif
 512         return -1;
 513     }
 514     int haveSemaphore = takeFileIOSemaphore();
 515     if (!haveSemaphore)
 516 #if defined(CAM_IS_VID_REC_WORKS)
 517         if (!conf.allow_unsafe_io)
 518 #endif
 519             return -1;
 520     int r = _Write(fd, buffer, nbytes);
 521     if (haveSemaphore)
 522         _GiveSemaphore(fileio_semaphore);
 523     return r;
 524 }
 525 
 526 int read (int fd, void *buffer, long nbytes)
 527 {
 528     return _Read(fd, buffer, nbytes);
 529 }
 530 
 531 int lseek (int fd, long offset, int whence)
 532 {
 533     return _lseek(fd, offset, whence); /* yes, it's lower-case lseek here since Lseek calls just lseek (A610) */
 534 }
 535 
 536 long mkdir(const char *dirname)
 537 {
 538 #ifdef MKDIR_RETURN_ONE_ON_SUCCESS
 539     // mkdir returns 1 on success, 0 on fail. So, values are inverted, to be compatible with previous versions
 540     if(_MakeDirectory_Fut(dirname,1)) return 0;
 541     else                              return 1;
 542 #else
 543     return _MakeDirectory_Fut(dirname,-1); // meaning of second arg is not clear, firmware seems to use -1
 544 #endif
 545 }
 546 
 547 long mkdir_if_not_exist(const char *dirname)
 548 {
 549     // Check if directory exists and create it if it does not.
 550     if (stat(dirname,0) != 0) return mkdir(dirname);
 551     return 0;   // Success
 552 }
 553 
 554 int remove(const char *name)
 555 {
 556 #ifdef CAM_DRYOS_2_3_R39
 557     // For DryOS R39 and later need to check if 'name' is a file or directory
 558     // and call appropriate delete function.
 559     struct stat st;
 560     if (stat(name,&st) == 0)
 561     {
 562         if (st.st_attrib & DOS_ATTR_DIRECTORY)
 563                 return _DeleteDirectory_Fut(name);
 564         else
 565                 return _DeleteFile_Fut(name);
 566     }
 567     return -1;  // return fail - file / directory does not exist
 568 #else
 569     return _DeleteFile_Fut(name);
 570 #endif
 571 }
 572 
 573 //----------------------------------------------------------------------------
 574 // minimal directory wrappers, rest of implementation is in core/lib_thumb.c
 575 
 576 int fw_closedir(void *d)
 577 {
 578     int have_semaphore = takeFileIOSemaphore();
 579     if (!have_semaphore)
 580 #if defined(CAM_IS_VID_REC_WORKS)
 581         if (!conf.allow_unsafe_io)
 582 #endif
 583             return -1;
 584 
 585     extern int _closedir(void *d);
 586     int ret = _closedir(d);
 587 
 588     if (have_semaphore)
 589         _GiveSemaphore(fileio_semaphore);
 590 
 591     return ret;
 592 }
 593 
 594 #if defined(CAM_DRYOS)
 595     int get_fstype(void)
 596     {
 597 #ifdef CAM_DRYOS_2_3_R39
 598         // fw function returns 1..3 for FAT 12-16-32, 4 for exFAT
 599         // TODO: fix this if any supported camera gets more than 1 drive OR the returned values change
 600         extern int _get_fstype(int);
 601         return _get_fstype(0);
 602 #else
 603         return 0;
 604 #endif
 605     }
 606 
 607     void *fw_opendir(const char* name)
 608     {
 609         void *ret;
 610         int have_semaphore = takeFileIOSemaphore();
 611         if (!have_semaphore)
 612 #if defined(CAM_IS_VID_REC_WORKS)
 613             if (!conf.allow_unsafe_io)
 614 #endif
 615                 return NULL;
 616 
 617         extern void *_OpenFastDir(const char* name);
 618         ret = _OpenFastDir(name);
 619 
 620         if (have_semaphore)
 621             _GiveSemaphore(fileio_semaphore);
 622 
 623         return ret;
 624     }
 625 
 626     int fw_readdir(void *d, void* dd)
 627     {
 628         extern int _ReadFastDir(void *d, void* dd);
 629         return _ReadFastDir(d, dd);
 630     }
 631 
 632 #else // Vx
 633 
 634     void *fw_opendir(const char* name)
 635     {
 636         void *ret;
 637         int have_semaphore = takeFileIOSemaphore();
 638         if (!have_semaphore)
 639 #if defined(CAM_IS_VID_REC_WORKS)
 640             if (!conf.allow_unsafe_io)
 641 #endif
 642                 return NULL;
 643 
 644         extern void *_opendir(const char* name);
 645         ret = _opendir(name);
 646 
 647         if (have_semaphore)
 648             _GiveSemaphore(fileio_semaphore);
 649 
 650         return ret;
 651     }
 652 
 653     void *fw_readdir(void *d)
 654     {
 655         extern void *_readdir(void *d);
 656         return _readdir(d);
 657     }
 658 
 659 #endif // CAM_DRYOS
 660 
 661 //-----------------------------------------------------------------------------------
 662 
 663 // Internal camera firmare 'stat' structures
 664 
 665 #if !CAM_DRYOS
 666 
 667 struct  __stat  // VxWorks
 668 {
 669     unsigned long       st_dev;         /* device ID number */
 670     unsigned long       st_ino;         /* file serial number */
 671     unsigned short      st_mode;        /* file mode (see below) */
 672     short               st_nlink;       /* number of links to file */
 673     short               st_uid;         /* user ID of file's owner */
 674     short               st_gid;         /* group ID of file's group */
 675     unsigned long       st_rdev;        /* device ID, only if special file */
 676     unsigned long       st_size;        /* size of file, in bytes */
 677     unsigned long       st_atime;       /* time of last access */
 678     unsigned long       st_mtime;       /* time of last modification */
 679     unsigned long       st_ctime;       /* time of last change of file status */
 680     long                st_blksize;
 681     long                st_blocks;
 682     unsigned char       st_attrib;      /* file attribute byte (dosFs only) */
 683     int                 reserved1;      /* reserved for future use */
 684     int                 reserved2;      /* reserved for future use */
 685     int                 reserved3;      /* reserved for future use */
 686     int                 reserved4;      /* reserved for future use */
 687     int                 reserved5;      /* reserved for future use */
 688     int                 reserved6;      /* reserved for future use */
 689 };
 690 
 691 #else
 692 
 693 #ifndef CAM_DRYOS_2_3_R39
 694 
 695 struct  __stat  // DryOS pre R39
 696     {
 697     unsigned long       st_dev;         //?
 698     unsigned long       st_ino;         //?
 699     unsigned short      st_mode;        //?
 700     short               st_nlink;       //?
 701     short               st_uid;         //?
 702     short               st_gid;         //?
 703     unsigned long       st_atime;       //?
 704     unsigned long       st_mtime;       //?
 705     unsigned long       st_ctime;       //?
 706     unsigned long       st_size;
 707     long                st_blksize;     //?
 708     long                st_blocks;      //?
 709     unsigned char       st_attrib;
 710     int                 reserved1;      //
 711     int                 reserved2;      //
 712     int                 reserved3;      //
 713     int                 reserved4;      //
 714     int                 reserved5;      //
 715     int                 reserved6;      //
 716 };
 717 
 718 #else
 719 
 720 #ifndef CAM_DRYOS_2_3_R59
 721 
 722 struct __stat   // DryOS R39...R58
 723 {
 724     unsigned long       st_unknown_1;
 725     unsigned long       st_attrib;
 726     unsigned long       st_size;
 727     unsigned long       st_ctime;
 728     unsigned long       st_mtime;
 729     unsigned long       st_unknown_2;
 730 };
 731 
 732 #else
 733 
 734 struct __stat   // DryOS >= R59
 735 {
 736     unsigned long       st_unknown_1;
 737     unsigned long       st_attrib;
 738     unsigned long       st_size;        // fixme: very likely 64bit, upper 32 bit is st_unknown_2
 739     unsigned long       st_unknown_2;
 740     unsigned long       st_ctime;
 741     unsigned long       st_mtime;
 742     unsigned long       st_unknown_3;
 743 };
 744 
 745 #endif//CAM_DRYOS_2_3_R59
 746 
 747 #endif//CAM_DRYOS_2_3_R39
 748 
 749 #endif//CAM_DRYOS
 750 
 751 int stat(const char *name, struct stat *pStat)
 752 {
 753     // sanity check. canon firmware hangup if start not from 'A/'
 754     if ( !name || (name[0] | 0x20)!='a' || name[1]!='/' ) return 1;
 755 
 756     // use temp __stat stucture to match camera firmware
 757     // and copy values across to output
 758     struct __stat lStat;
 759     int rv = _stat(name, &lStat);
 760     if (pStat)
 761     {
 762         if (rv == 0)
 763         {
 764             pStat->st_attrib = lStat.st_attrib;
 765             pStat->st_size = lStat.st_size;
 766             pStat->st_ctime = lStat.st_ctime;
 767             pStat->st_mtime = lStat.st_mtime;
 768         }
 769         else
 770         {
 771             memset( pStat, 0, sizeof(struct stat));
 772         }
 773     }
 774     return rv;
 775 }
 776 
 777 FILE *fopen(const char *filename, const char *mode) {
 778 #ifdef CAM_DRYOS
 779     if(!filename || filename[0]!='A') {
 780         return NULL;
 781     }
 782 #endif
 783 
 784     // Check fileio semaphore, if not available we are probably recording video
 785     if (!takeFileIOSemaphore())
 786         return NULL;
 787     _GiveSemaphore(fileio_semaphore);
 788 
 789     return (FILE *)_Fopen_Fut(filename,mode);
 790 }
 791 
 792 long fclose(FILE *f) {
 793     return _Fclose_Fut((long)f);
 794 }
 795 
 796 long fread(void *buf, long elsize, long count, FILE *f) {
 797     return _Fread_Fut(buf,elsize,count,(long)f);
 798 }
 799 
 800 long fwrite(const void *buf, long elsize, long count, FILE *f) {
 801     return _Fwrite_Fut(buf,elsize,count,(long)f);
 802 }
 803 
 804 long fseek(FILE *file, long offset, long whence) {
 805     return _Fseek_Fut((long)file,offset,whence);
 806 }
 807 
 808 long feof(FILE * file) {
 809     return _Feof_Fut((long)file);
 810 }
 811 
 812 long fflush(FILE * file) {
 813     return _Fflush_Fut((long)file);
 814 }
 815 
 816 char *fgets(char *buf, int n, FILE *f) {
 817     return _Fgets_Fut(buf,n,(int)f);
 818 }
 819 
 820 int rename(const char *oldname, const char *newname) {
 821  return _RenameFile_Fut(oldname, newname);
 822 }
 823 
 824 unsigned int GetFreeCardSpaceKb(void){
 825         return (_GetDrive_FreeClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
 826 }
 827 
 828 unsigned int GetTotalCardSpaceKb(void){
 829         return (_GetDrive_TotalClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
 830 }
 831 
 832 //----------------------------------------------------------------------------
 833 
 834 int errnoOfTaskGet(int tid) {
 835 #if !CAM_DRYOS
 836     return _errnoOfTaskGet(tid);
 837 #else
 838     return 0;
 839 #endif
 840 }
 841 
 842 //----------------------------------------------------------------------------
 843 // String wrappers
 844 
 845 long strlen(const char *s) {
 846     return _strlen(s);
 847 }
 848 
 849 int strcmp(const char *s1, const char *s2) {
 850     return _strcmp(s1, s2);
 851 }
 852 
 853 int strncmp(const char *s1, const char *s2, long n) {
 854     return _strncmp(s1, s2, n);
 855 }
 856 
 857 char *strchr(const char *s, int c) {
 858     return _strchr(s, c);
 859 }
 860 
 861 char *strcpy(char *dest, const char *src) {
 862     return _strcpy(dest, src);
 863 }
 864 
 865 char *strncpy(char *dest, const char *src, long n) {
 866     return _strncpy(dest, src, n);
 867 }
 868 
 869 char *strcat(char *dest, const char *app) {
 870     return _strcat(dest, app);
 871 }
 872 
 873 char *strrchr(const char *s, int c) {
 874     return _strrchr(s, c);
 875 }
 876 
 877 long strtol(const char *nptr, char **endptr, int base) {
 878     return _strtol(nptr, endptr, base);
 879 }
 880 
 881 unsigned long strtoul(const char *nptr, char **endptr, int base) {
 882 #if CAM_DRYOS
 883     return (unsigned long)_strtolx(nptr, endptr, base, 0);
 884 #else
 885     return _strtoul(nptr, endptr, base);
 886 #endif
 887 }
 888 
 889 #if !CAM_DRYOS
 890 char *strpbrk(const char *s, const char *accept)
 891 {
 892     return _strpbrk(s, accept);
 893 }
 894 #endif
 895 
 896 //----------------------------------------------------------------------------
 897 
 898 long sprintf(char *s, const char *st, ...)
 899 {
 900     long res;
 901     __builtin_va_list va;
 902     __builtin_va_start(va, st);
 903     res = _vsprintf(s, st, va);
 904     __builtin_va_end(va);
 905     return res;
 906 }
 907 
 908 // strerror exists on vxworks cams,
 909 // but it does about the same thing as this
 910 const char *strerror(int en) {
 911 #if !CAM_DRYOS
 912     static char msg[20];
 913     sprintf(msg,"errno 0x%X",en);
 914     return msg;
 915 #else
 916     return "error";
 917 #endif
 918 }
 919 
 920 //----------------------------------------------------------------------------
 921 // Time wrappers
 922 
 923 unsigned long time(unsigned long *timer) {
 924     return _time(timer);
 925 }
 926 
 927 int utime(const char *file, struct utimbuf *newTimes) {
 928 #if !CAM_DRYOS
 929     return _utime(file, newTimes);
 930 #else
 931     int res=0;
 932     int fd;
 933     fd = _open(file, 0, 0);
 934 
 935 #ifdef CAM_DRYOS_2_3_R39
 936     if (fd>=0) {
 937         _close(fd);
 938         res=_SetFileTimeStamp(file, newTimes->modtime, newTimes->actime);
 939     }
 940 #else
 941     if (fd>=0) {
 942         res=_SetFileTimeStamp(fd, newTimes->modtime, newTimes->actime);
 943         _close(fd);
 944     }
 945     // return value compatibe with utime: ok=0 fail=-1
 946 #endif
 947     return (res)?0:-1;
 948 #endif
 949 }
 950 
 951 struct tm *localtime(const time_t *_tod) {
 952 #if !CAM_DRYOS
 953     return _localtime(_tod);
 954 #else
 955     // for DRYOS cameras do something with this!  - sizeof(x[]) must be >= sizeof(struct tm) :  'static int x[9];'
 956     static int x[10];
 957     return _LocalTime(_tod, &x);
 958 #endif
 959 }
 960 
 961 long strftime(char *s, unsigned long maxsize, const char *format, const struct tm *timp) {
 962         return _strftime(s,maxsize,format,timp);
 963 }
 964 
 965 time_t mktime(struct tm *timp) {
 966 #if !CAM_DRYOS
 967         return _mktime(timp);
 968 #else
 969         int timp_ext[10]; // struct tm + a ptr
 970         _memcpy(timp_ext,timp,9*sizeof(int));
 971         timp_ext[9]=0;
 972         long retval = _mktime_ext(&timp_ext);
 973         _memcpy(timp,timp_ext,9*sizeof(int));
 974         return retval;
 975 #endif
 976 }
 977 
 978 void set_clock(int year, int month, int day, int hour, int minute, int second)
 979 {
 980     int buf[6];
 981     buf[0] = year;
 982     buf[1] = month;
 983     buf[2] = day;
 984     buf[3] = hour;
 985     buf[4] = minute;
 986     buf[5] = second;
 987     _SetDate(buf);
 988     // SetDate sets second, so adjust subsec offset
 989     camera_info.tick_count_offset = get_tick_count() % 1000; 
 990 }
 991 
 992 //----------------------------------------------------------------------------
 993 // Math wrappers
 994 
 995 double _log(double x) {
 996     return __log(x);
 997 }
 998 
 999 double _log10(double x) {
1000     return __log10(x);
1001 }
1002 
1003 double _pow(double x, double y) {
1004     return __pow(x, y);
1005 }
1006 
1007 double _sqrt(double x) {
1008     return __sqrt(x);
1009 }
1010 
1011 //----------------------------------------------------------------------------
1012 
1013 #ifdef OPT_EXMEM_MALLOC
1014 void *exmem_alloc_cached(unsigned int pool_id,unsigned int size,int unk,int unk2)
1015 {
1016     return _exmem_alloc(pool_id,size,unk,unk2);
1017 }
1018 #endif
1019 
1020 void *exmem_alloc_uncached(unsigned int type, unsigned int size, exmem_alloc_info *allocinf)
1021 {
1022     return _exmem_ualloc(type, size, allocinf);
1023 }
1024 void exmem_free_uncached(unsigned int type)
1025 {
1026     _exmem_ufree(type);
1027 }
1028 
1029 void *canon_malloc(long size)
1030 {
1031 #if CAM_DRYOS
1032     return _malloc(size);
1033 #else
1034     if (_TakeSemaphore(canon_heap_sem,CANON_HEAP_SEM_TIMEOUT)) {
1035         return 0;
1036     } else {
1037         void *r=_malloc(size);
1038         _GiveSemaphore(canon_heap_sem);
1039         return r;
1040     }
1041 #endif
1042 }
1043 
1044 void canon_free(void *p)
1045 {
1046 #if CAM_DRYOS
1047     _free(p);
1048 #else
1049     if (!_TakeSemaphore(canon_heap_sem,CANON_HEAP_SEM_TIMEOUT)) {
1050        _free(p);
1051        _GiveSemaphore(canon_heap_sem);
1052     }
1053 #endif
1054 }
1055 
1056 void *umalloc(long size) {
1057 #if CAM_DRYOS
1058     return _AllocateUncacheableMemory(size);
1059 #else
1060     if (_TakeSemaphore(canon_heap_sem,CANON_HEAP_SEM_TIMEOUT)) {
1061         return 0;
1062     } else {
1063         void *r=_AllocateUncacheableMemory(size);
1064         _GiveSemaphore(canon_heap_sem);
1065         return r;
1066     }
1067 #endif
1068 }
1069 
1070 void ufree(void *p) {
1071 #if CAM_DRYOS
1072     _FreeUncacheableMemory(p);
1073 #else
1074     if (!_TakeSemaphore(canon_heap_sem,CANON_HEAP_SEM_TIMEOUT)) {
1075         _FreeUncacheableMemory(p);
1076        _GiveSemaphore(canon_heap_sem);
1077     }
1078 #endif
1079 }
1080 
1081 void *memcpy(void *dest, const void *src, long n) {
1082     return _memcpy(dest, src, n);
1083 }
1084 
1085 void *memset(void *s, int c, int n) {
1086     return _memset(s, c, n);
1087 }
1088 
1089 int memcmp(const void *s1, const void *s2, long n) {
1090     return _memcmp(s1, s2, n);
1091 }
1092 
1093 #if !CAM_DRYOS
1094 void *memchr(const void *s, int c, int n) {
1095         return _memchr(s,c,n);
1096 }
1097 #endif
1098 
1099 //----------------------------------------------------------------------------
1100 
1101 void GetMemInfo(cam_meminfo *camera_meminfo)
1102 {
1103 // Use firmware GetMemInfo function to retrieve info about Canon heap memory allocation
1104 
1105 #if defined(CAM_DRYOS)
1106     // Prior to dryos R39 GetMemInfo returns 9 values, after R39 it returns 10 (all but 1 are used in each case)
1107     int fw_info[10];
1108     extern void _GetMemInfo(int*);
1109     _GetMemInfo(fw_info);
1110 
1111 #if defined(CAM_DRYOS_2_3_R39)
1112     // For newer dryos version copy all 9 used values to CHDK structure
1113     camera_meminfo->start_address        = fw_info[0];
1114     camera_meminfo->end_address          = fw_info[1];
1115     camera_meminfo->total_size           = fw_info[2];
1116     camera_meminfo->allocated_size       = fw_info[3];
1117     camera_meminfo->allocated_peak       = fw_info[4];
1118     camera_meminfo->allocated_count      = fw_info[5];
1119     camera_meminfo->free_size            = fw_info[6];
1120     camera_meminfo->free_block_max_size  = fw_info[7];
1121     camera_meminfo->free_block_count     = fw_info[8];
1122 #else
1123     // For older dryos version copy 8 used values to CHDK structure and calculate missing value
1124     camera_meminfo->start_address        = fw_info[0];
1125     camera_meminfo->end_address          = fw_info[0] + fw_info[1];
1126     camera_meminfo->total_size           = fw_info[1];
1127     camera_meminfo->allocated_size       = fw_info[2];
1128     camera_meminfo->allocated_peak       = fw_info[3];
1129     camera_meminfo->allocated_count      = fw_info[4];
1130     camera_meminfo->free_size            = fw_info[5];
1131     camera_meminfo->free_block_max_size  = fw_info[6];
1132     camera_meminfo->free_block_count     = fw_info[7];
1133 #endif
1134 #else // vxworks
1135 extern int sys_mempart_id;
1136     int fw_info[5];
1137     // -1 for invalid
1138     memset(camera_meminfo,0xFF,sizeof(cam_meminfo));
1139     if(!_TakeSemaphore(canon_heap_sem,CANON_HEAP_SEM_TIMEOUT)) {
1140 #ifdef CAM_NO_MEMPARTINFO
1141         camera_meminfo->free_block_max_size = _memPartFindMax(sys_mempart_id);
1142 #else
1143         _memPartInfoGet(sys_mempart_id,fw_info);
1144         // TODO we could fill in start address from _start + MEMISOSIZE, if chdk not in exmem
1145         // these are guessed, look reasonable on a540
1146         camera_meminfo->free_size = fw_info[0];
1147         camera_meminfo->free_block_count = fw_info[1];
1148         camera_meminfo->free_block_max_size = fw_info[2];
1149         camera_meminfo->allocated_size = fw_info[3];
1150         camera_meminfo->allocated_count = fw_info[4];
1151 #endif
1152         _GiveSemaphore(canon_heap_sem);
1153     }
1154 #endif
1155 }
1156 
1157 //----------------------------------------------------------------------------
1158 
1159 int rand(void) {
1160     return _rand();
1161 }
1162 
1163 void *srand(unsigned int seed) {
1164     return _srand(seed);
1165 }
1166 
1167 void qsort(void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2)) {
1168     _qsort(__base, __nelem, __size, __cmp);
1169 }
1170 
1171 static int shutdown_disabled = 0;
1172 void disable_shutdown() {
1173     if (!shutdown_disabled) {
1174         _LockMainPower();
1175         shutdown_disabled = 1;
1176     }
1177 }
1178 
1179 void enable_shutdown() {
1180     if (shutdown_disabled) {
1181         _UnlockMainPower();
1182         shutdown_disabled = 0;
1183     }
1184 }
1185 
1186 void camera_shutdown_in_a_second(void) {
1187     int i;
1188     _SetAutoShutdownTime(1); // 1 sec
1189     for (i=0;i<200;i++)
1190         _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").
1191 }
1192 
1193 void EnterToCompensationEVF(void)
1194 {
1195   _EnterToCompensationEVF();
1196 }
1197 
1198 void ExitFromCompensationEVF()
1199 {
1200   _ExitFromCompensationEVF();
1201 }
1202 
1203 void TurnOnBackLight(void)
1204 {
1205   _TurnOnBackLight();
1206 }
1207 
1208 void TurnOffBackLight(void)
1209 {
1210   _TurnOffBackLight();
1211 }
1212 
1213 extern void gui_set_need_restore();
1214 
1215 void TurnOnDisplay(void)
1216 {
1217   _TurnOnDisplay();
1218   // required to re-draw the canon and CHDK UI after re-enabling display
1219   gui_set_need_restore() ;
1220 }
1221 
1222 void TurnOffDisplay(void)
1223 {
1224   _TurnOffDisplay();
1225 }
1226 
1227 void DoAELock(void)
1228 {
1229   if (!camera_info.state.mode_play)
1230   {
1231      _DoAELock();
1232   }
1233 }
1234 
1235 void UnlockAE(void)
1236 {
1237   if (!camera_info.state.mode_play)
1238   {
1239      _UnlockAE();
1240   }
1241 }
1242 
1243 void DoAFLock(void)
1244 {
1245   if (!camera_info.state.mode_play)
1246   {
1247      int af_lock=1;
1248      _DoAFLock();
1249      set_property_case(PROPCASE_AF_LOCK,&af_lock,sizeof(af_lock));
1250   }
1251 }
1252 
1253 void UnlockAF(void)
1254 {
1255   if (!camera_info.state.mode_play)
1256   {
1257      int af_lock=0;
1258      _UnlockAF();
1259      set_property_case(PROPCASE_AF_LOCK,&af_lock,sizeof(af_lock));
1260   }
1261 }
1262 
1263 int EngDrvRead(int gpio_reg)
1264 {
1265   return _EngDrvRead(gpio_reg);
1266 }
1267 
1268 #if CAM_MULTIPART
1269 
1270 #define SECTOR_SIZE 512
1271 static char *mbr_buf=(void*)0;
1272 static unsigned long drive_sectors;
1273 
1274 int is_mbr_loaded()
1275 {
1276         return (mbr_buf == (void*)0) ? 0 : 1;
1277 }
1278 
1279 #ifndef CAM_DRYOS
1280 
1281 int mbr_read(char* mbr_sector, unsigned long drive_total_sectors, unsigned long *part_start_sector,  unsigned long *part_length){
1282 // return value: 1 - success, 0 - fail
1283 // called only in VxWorks
1284 
1285  int offset=0x10; // points to partition #2
1286  int valid;
1287 
1288  if ((mbr_sector[0x1FE]!=0x55) || (mbr_sector[0x1FF]!=0xAA)) return 0; // signature check
1289 
1290  mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
1291  _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
1292  drive_sectors=drive_total_sectors;
1293 
1294  while(offset>=0) {
1295 
1296   *part_start_sector=(*(unsigned short*)(mbr_sector+offset+0x1C8)<<16) | *(unsigned short*)(mbr_sector+offset+0x1C6);
1297   *part_length=(*(unsigned short*)(mbr_sector+offset+0x1CC)<<16) | *(unsigned short*)(mbr_sector+offset+0x1CA);
1298 
1299   valid= (*part_start_sector) && (*part_length) &&
1300          (*part_start_sector<=drive_total_sectors) &&
1301          (*part_start_sector+*part_length<=drive_total_sectors) &&
1302          ((mbr_sector[offset+0x1BE]==0) || (mbr_sector[offset+0x1BE]==0x80)); // status: 0x80 (active) or 0 (non-active)
1303 
1304   if (valid && ((mbr_sector[0x1C2+offset]==0x0B) || (mbr_sector[0x1C2+offset]==0x0C))) break;   // FAT32 secondary partition
1305 
1306   offset-=0x10;
1307 
1308  }
1309 
1310  return valid;
1311 }
1312 
1313 #else
1314 
1315 int mbr_read_dryos(unsigned long drive_total_sectors, char* mbr_sector ){
1316 // Called only in DRYOS
1317  mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
1318  _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
1319  drive_sectors=drive_total_sectors;
1320  return drive_total_sectors;
1321 }
1322 
1323 #endif
1324 
1325 int get_part_count(void)
1326 {
1327   unsigned long part_start_sector, part_length;
1328   char part_status, part_type;
1329   int i;
1330   int count=0;
1331   if (is_mbr_loaded())
1332   {
1333     for (i=0; i<=3;i++)
1334     {
1335       part_start_sector=(*(unsigned short*)(mbr_buf+i*16+0x1C8)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1C6);
1336       part_length=(*(unsigned short*)(mbr_buf+i*16+0x1CC)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1CA);
1337       part_status=mbr_buf[i*16+0x1BE];
1338       part_type=mbr_buf[0x1C2+i*16];
1339       if ( part_start_sector && part_length && part_type && ((part_status==0) || (part_status==0x80)) ) count++;
1340     }
1341   }
1342   return count;
1343 }
1344 int get_part_type()
1345 {
1346   int partType = 0x00;
1347   if (is_mbr_loaded())
1348   {
1349     partType=mbr_buf[0x1C2+(get_active_partition()-1)*16];
1350   }
1351   return partType;
1352 } 
1353 
1354 static int boot_partition = 0;
1355 static int partition_changed = 0;
1356 int is_partition_changed()
1357 {
1358   return partition_changed;
1359 }
1360 
1361 int swap_partitions(int new_partition)
1362 {
1363   if (is_mbr_loaded())
1364   {
1365     int i,j,p;
1366     char c;
1367     
1368     int partition_count = get_part_count();
1369     int active_partition = get_active_partition();
1370     
1371     if(!boot_partition)
1372     {
1373       boot_partition = active_partition;
1374     }
1375 
1376     // wrong input
1377     if( new_partition > partition_count || new_partition <= 0 )
1378     {
1379       return 0;
1380     }
1381     partition_changed = (new_partition==boot_partition)?0:1;
1382     
1383     // rotate partitions till new_partition is found
1384     for(j=0;j<partition_count;++j)
1385     {
1386       if(new_partition == get_active_partition())
1387       {
1388         break;
1389       }
1390       for(i=0;i<16;i++)
1391       {
1392         c=mbr_buf[i+0x1BE];
1393         for(p=1; p<partition_count; ++p)
1394         {
1395           mbr_buf[i+(p-1)*16+0x1BE]=mbr_buf[i+p*16+0x1BE];
1396         }
1397         mbr_buf[i+(partition_count-1)*16+0x1BE]=c;
1398       }
1399     }
1400     _WriteSDCard(0,0,1,mbr_buf);
1401   }
1402   return 1;
1403 }
1404 
1405 unsigned char get_active_partition(void)
1406 {
1407   unsigned int  partition_start[4];
1408   unsigned char partition_number = 1;
1409   int partition_count = get_part_count();
1410   int i;
1411 
1412   for( i=0; i<partition_count; ++i )
1413   {
1414     int a = mbr_buf[0x01C6+(i)*16];
1415     int b = mbr_buf[0x01C7+(i)*16];
1416     int c = mbr_buf[0x01C8+(i)*16];
1417     int d = mbr_buf[0x01C9+(i)*16];
1418     partition_start[i] = (((((d<<8) +c)<<8) +b)<<8) +a;
1419   }
1420   for( i=1; i<partition_count; ++i )
1421   {
1422     if(partition_start[i]<partition_start[0])
1423     {
1424       ++partition_number;
1425     }
1426   }
1427   return partition_number;
1428 }
1429 
1430 void create_partitions(void){
1431         if (is_mbr_loaded())
1432         {
1433          unsigned long start, length;
1434          char type;
1435 
1436          _memset(mbr_buf,0,SECTOR_SIZE);
1437 
1438          start=1; length=2*1024*1024/SECTOR_SIZE; //2 Mb
1439          type=1; // FAT primary
1440          mbr_buf[0x1BE + 4]=type;
1441          mbr_buf[0x1BE + 8]=start;   mbr_buf[0x1BE + 9]=start>>8;   mbr_buf[0x1BE + 10]=start>>16;  mbr_buf[0x1BE + 11]=start>>24;
1442          mbr_buf[0x1BE + 12]=length; mbr_buf[0x1BE + 13]=length>>8; mbr_buf[0x1BE + 14]=length>>16; mbr_buf[0x1BE + 15]=length>>24;
1443 
1444          start=start+length; length=drive_sectors-start-1;
1445          type=0x0B;  //FAT32 primary;
1446          mbr_buf[0x1CE + 4]=type;
1447          mbr_buf[0x1CE + 8]=start;   mbr_buf[0x1CE + 9]=start>>8;   mbr_buf[0x1CE + 10]=start>>16;  mbr_buf[0x1CE + 11]=start>>24;
1448          mbr_buf[0x1CE + 12]=length; mbr_buf[0x1CE + 13]=length>>8; mbr_buf[0x1CE + 14]=length>>16; mbr_buf[0x1CE + 15]=length>>24;
1449 
1450          mbr_buf[0x1FE]=0x55; mbr_buf[0x1FF]=0xAA; // signature;
1451 
1452          _WriteSDCard(0,0,1,mbr_buf);
1453         }
1454 }
1455 
1456 #else
1457 
1458 // Dummy for scripts if not implemented in camera
1459 int swap_partitions(int new_partition) { return 0; }
1460 int get_part_count(void) { return 1; }
1461 int get_part_type() { return 0; }
1462 unsigned char get_active_partition(void) { return 1; }
1463 int is_partition_changed() { return 0; }
1464 
1465 #endif
1466 
1467 int mute_on_zoom(int x){
1468  static int old_busy=0;
1469  int busy=zoom_busy||focus_busy;
1470  if (old_busy!=busy) {
1471   if (busy) {
1472 #if CAM_CAN_MUTE_MICROPHONE
1473    if (conf.mute_on_zoom) _TurnOffMic();
1474 #endif
1475    }
1476    else {
1477 #if CAM_CAN_MUTE_MICROPHONE
1478   if (conf.mute_on_zoom) _TurnOnMic();
1479 #endif
1480 #if CAM_EV_IN_VIDEO
1481   if (get_ev_video_avail()) set_ev_video_avail(0);
1482 #endif
1483   }
1484   old_busy=busy;
1485  }
1486  return x; // preserve R0 if called from assembler
1487 }
1488 
1489 
1490 #if CAM_AF_SCAN_DURING_VIDEO_RECORD
1491 void MakeAFScan(void){
1492  int a=0, save;
1493  if (zoom_busy || focus_busy) return;
1494  save=some_flag_for_af_scan;
1495  some_flag_for_af_scan=0;
1496 #if CAM_AF_SCAN_DURING_VIDEO_RECORD == 2
1497  parameter_for_af_scan=3;
1498 #endif
1499  _MakeAFScan(&a, 3);
1500  some_flag_for_af_scan=save;
1501 #if CAM_RESET_AEL_AFTER_VIDEO_AF
1502  int ae_lock;
1503  get_property_case(PROPCASE_AE_LOCK,&ae_lock,sizeof(ae_lock));
1504  if (ae_lock == 0)                                              // AE not locked so ensure it is unlocked after re-focus
1505          _ExpCtrlTool_StartContiAE(0,0);
1506  else                                                                   // AE locked before so re-lock after
1507          _ExpCtrlTool_StopContiAE(0,0);
1508 #else
1509  _ExpCtrlTool_StartContiAE(0,0);
1510 #endif
1511 }
1512 #endif
1513 
1514 long __attribute__((weak)) get_jogdial_direction(void)  { return 0; }
1515 void __attribute__((weak)) JogDial_CW(void)     {}
1516 void __attribute__((weak)) JogDial_CCW(void)    {}
1517 
1518 #if defined (DNG_EXT_FROM)
1519 
1520 #define DNG_EXT_TO ".DNG"
1521 
1522 typedef int(*p_some_f)(char*, int);
1523 
1524 extern p_some_f some_f_for_dng;  // camera variable!
1525 extern char* second_ext_for_dng; // camera variable!
1526 
1527 p_some_f default_some_f;
1528 char *   default_second_ext;
1529 
1530 int my_some_f(char *s, int x){
1531   char *f;
1532   f=strstr(s, DNG_EXT_FROM);
1533   if (f) _memcpy(f, DNG_EXT_TO, sizeof(DNG_EXT_TO)-1);
1534   return default_some_f(s, x);
1535 }
1536 
1537 void save_ext_for_dng(void){
1538  default_some_f=some_f_for_dng;
1539  default_second_ext=second_ext_for_dng;
1540 }
1541 
1542 void change_ext_to_dng(void){
1543  some_f_for_dng=my_some_f;
1544  second_ext_for_dng=DNG_EXT_TO;
1545 }
1546 
1547 void change_ext_to_default(void){
1548  some_f_for_dng=default_some_f;
1549  second_ext_for_dng=default_second_ext;
1550 }
1551 
1552 #endif
1553 
1554 #if !CAM_DRYOS
1555 static long drv_struct[16];
1556 #endif
1557 
1558 long dh_err()
1559 {
1560     return -1;
1561 }
1562 
1563 #if !CAM_DRYOS
1564 void drv_self_hide()
1565 {
1566     long drvnum;
1567 
1568     drvnum = _iosDrvInstall(dh_err,dh_err,dh_err,dh_err,dh_err,dh_err,dh_err);
1569     if (drvnum >= 0)
1570         _iosDevAdd(drv_struct, "A/DISKBOOT.BIN", drvnum);
1571 }
1572 
1573 void drv_self_unhide()
1574 {
1575  _iosDevDelete(drv_struct);
1576 }
1577 #endif
1578 
1579 int  apex2us(int apex_tv){
1580 #if CAM_EXT_TV_RANGE
1581 /*
1582  Extended Tv, by barberofcivil, http://chdk.setepontos.com/index.php/topic,4392.0.html
1583  Explanation by reyalP:
1584  In every port, the original shutter overrides (as opposed to super long exposure) worked by
1585  setting the propcase values at some point after auto-exposure has happened (except in manual
1586  modes, where the manual control propcases may be used instead). The Canon code previously took
1587  these values unchanged for short exposures. In newer cameras, like on the SX10 / SD980, the value
1588  is changed, apparently some time after it has been retrieved from the propcase. We know this is
1589  the case, because the propcase value itself doesn't get clamped to the allowed range (if it did,
1590  barberofcivil's code wouldn't work).
1591 */
1592         short tv;
1593         tv = shooting_get_tv96();
1594         if (tv<-576 || tv!=apex_tv) return 1000000.0*pow(2.0, -tv/96.0);
1595         else return _apex2us(apex_tv);
1596 #else
1597         return 0;
1598 #endif
1599 }
1600 
1601 void PostLogicalEventForNotPowerType(unsigned id, unsigned x) {
1602         _PostLogicalEventForNotPowerType(id,x);
1603 }
1604 
1605 void PostLogicalEventToUI(unsigned id, unsigned x) {
1606         _PostLogicalEventToUI(id,x);
1607 }
1608 
1609 void SetLogicalEventActive(unsigned id, unsigned state) {
1610         _SetLogicalEventActive(id, state);
1611 }
1612 
1613 void SetScriptMode(unsigned mode) {
1614         _SetScriptMode(mode);
1615 }
1616 
1617 // TODO this belongs lib.c, but not all cameras include it
1618 // same as bitmap width for most cameras, override in platform/sub/lib.c as needed
1619 int __attribute__((weak)) vid_get_viewport_width() {
1620         return camera_screen.width;
1621 }
1622 
1623 // Physical width of viewport row in bytes
1624 int __attribute__((weak)) vid_get_viewport_byte_width() {
1625         return 720 * 6 / 4;     // For most cameras viewport is 720 pixels wide, each group of 4 pixels uses 6 bytes (UYVYYY)
1626 }
1627 
1628 // Y multiplier for cameras with 480 pixel high viewports (CHDK code assumes 240)
1629 int __attribute__((weak)) vid_get_viewport_yscale() {
1630         return 1;               // For most cameras viewport is 240 pixels high
1631 }
1632 
1633 // viewport x offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1634 int __attribute__((weak)) vid_get_viewport_xoffset() {
1635         return 0;
1636 }
1637 
1638 // viewport y offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1639 int __attribute__((weak)) vid_get_viewport_yoffset() {
1640         return 0;
1641 }
1642 
1643 // viewport display x offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1644 int __attribute__((weak)) vid_get_viewport_display_xoffset() {
1645         return vid_get_viewport_xoffset();
1646 }
1647 
1648 // viewport display y offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1649 int __attribute__((weak)) vid_get_viewport_display_yoffset() {
1650         return vid_get_viewport_yoffset();
1651 }
1652 
1653 // format of live view viewport
1654 #ifndef THUMB_FW
1655 int vid_get_viewport_type() {
1656         return LV_FB_YUV8;
1657 }
1658 // D6 cameras must define
1659 #endif
1660 
1661 // for cameras with two (or more?) RAW buffers this can be used to speed up DNG creation by
1662 // calling reverse_bytes_order only once. Override in platform/sub/lib.c
1663 char __attribute__((weak)) *hook_alt_raw_image_addr() {
1664         return hook_raw_image_addr();
1665 }
1666 
1667 void __attribute__((weak)) vid_turn_off_updates()
1668 {
1669 }
1670 
1671 void __attribute__((weak)) vid_turn_on_updates()
1672 {
1673 }
1674 
1675 // use _GetFocusLensSubjectDistance for this on dryos, vx functions are basically equivlent
1676 // not used in CHDK currently for either OS
1677 #ifdef CAM_DRYOS
1678 long __attribute__((weak)) _GetCurrentTargetDistance()
1679 {
1680         return _GetFocusLensSubjectDistance();
1681 }
1682 #endif
1683 
1684 int add_ptp_handler(int opcode, ptp_handler handler, int unknown)
1685 {
1686 #ifdef CAM_CHDK_PTP
1687     return _add_ptp_handler(opcode,handler,unknown);
1688 #else
1689     return 0;
1690 #endif
1691 }
1692 
1693 #ifdef CAM_PTP_USE_NATIVE_BUFFER
1694 int get_ptp_file_buf_size(void)
1695 {
1696     return _get_ptp_buf_size(CAM_PTP_FILE_BUFFER_ID);
1697 }
1698 
1699 char *get_ptp_file_buf(void)
1700 {
1701     return _get_ptp_file_buf();
1702 }
1703 #endif
1704 
1705 int CreateTask (const char *name, int prio, int stack_size, void *entry)
1706 {
1707     return _CreateTask(name, prio, stack_size, entry, 0);
1708 }
1709 
1710 void ExitTask()
1711 {
1712     _ExitTask();
1713 }
1714 
1715 // TODO not in sigs for vx yet
1716 #ifndef CAM_DRYOS
1717 void __attribute__((weak)) _reboot_fw_update(const char *fw_update)
1718 {
1719         return;
1720 }
1721 #endif
1722 
1723 #ifdef CAM_DRYOS
1724 int __attribute__((weak)) switch_mode_usb(int mode)
1725 {
1726 #ifdef CAM_CHDK_PTP
1727     if ( mode == 0 ) {
1728         _Rec2PB();
1729         _set_control_event(0x80000000|CAM_USB_EVENTID);
1730     } else if ( mode == 1 ) {
1731         _set_control_event(CAM_USB_EVENTID);
1732         _PB2Rec();
1733     } else return 0;
1734     return 1;
1735 #else
1736   return 0;
1737 #endif // CAM_CHDK_PTP
1738 }
1739 
1740 #else // vxworks
1741 // this doesn't need any special functions so it's defined even without CHDK_CAM_PTP
1742 int __attribute__((weak)) switch_mode_usb(int mode)
1743 {
1744     if ( mode == 0 ) {
1745         // TODO should we revert scriptmode and/or levent? seems to work without
1746         levent_set_play();
1747     } else if ( mode == 1 ) {
1748 #ifdef CAM_USB_EVENTID_VXWORKS
1749         _SetScriptMode(1); // needed to override event
1750         _SetLogicalEventActive(CAM_USB_EVENTID_VXWORKS,0); // set levent "ConnectUSBCable" inactive
1751 #endif
1752         levent_set_record();
1753     } else return 0;
1754     return 1;
1755 }
1756 #endif // vxworks
1757 
1758 /*
1759 // this wrapper isn't currently needed
1760 // 7 calls functions and sets some MMIOs, but doesn't disable caches and actually restart
1761 // 3 skips one function call on some cameras, but does restart
1762 void Restart(unsigned option) {
1763         _Restart(option);
1764 }
1765 */
1766 
1767 unsigned char SetFileAttributes(const char* fn, unsigned char attr)
1768 {
1769 #ifdef CAM_DRYOS_2_3_R39
1770     return _SetFileAttributes(fn, attr);
1771 #else
1772     int fd;
1773     unsigned char ret = -1;
1774     
1775     fd = open(fn, 0, 0);
1776     if( fd ) {
1777         _SetFileAttributes(fd, attr);
1778         close(fd);
1779         ret = attr;
1780     }
1781     return ret;
1782 #endif
1783 }
1784 
1785 // Default implementation of PTP live view functions.
1786 // Override as needed for camera specific variations (see G12/SX30/IXUS310/SX130IS for working examples)
1787 
1788 int __attribute__((weak)) vid_get_viewport_display_xoffset_proper() { return vid_get_viewport_display_xoffset()*2; }
1789 int __attribute__((weak)) vid_get_viewport_display_yoffset_proper() { return vid_get_viewport_display_yoffset(); }
1790 int __attribute__((weak)) vid_get_viewport_buffer_width_proper()    { return 720; }
1791 #ifdef THUMB_FW
1792 int __attribute__((weak)) vid_get_viewport_width_proper()           { return vid_get_viewport_width(); }
1793 int __attribute__((weak)) vid_get_viewport_height_proper()          { return vid_get_viewport_height(); }
1794 #else
1795 int __attribute__((weak)) vid_get_viewport_width_proper()           { return vid_get_viewport_width()*2; }
1796 int __attribute__((weak)) vid_get_viewport_height_proper()          { return 240; }
1797 #endif
1798 int __attribute__((weak)) vid_get_viewport_fullscreen_height()      { return 240; }
1799 int __attribute__((weak)) vid_get_viewport_fullscreen_width()       { return vid_get_viewport_buffer_width_proper(); }
1800 
1801 int __attribute__((weak)) vid_get_palette_type()                    { return 0; }       // 0 = no palette into, 1 = 16 x 4 byte AYUV values, 
1802                                                                                         // 2 = 16 x 4 byte AYUV (A = 0..3), 3 = 256 x 4 byte AYUV (A = 0..3)
1803 int __attribute__((weak)) vid_get_palette_size()                    { return 0; }
1804 int __attribute__((weak)) vid_get_aspect_ratio()                    { return 0; }       // 0 = 4:3, 1 = 16:9 LCD Aspect Ratio, 2 = 3:2
1805 
1806 void __attribute__((weak)) *vid_get_bitmap_active_buffer()
1807 {
1808   return vid_get_bitmap_fb();   // *** does not get the active buffer! (override if active buffer can be determined)
1809 }
1810 
1811 void __attribute__((weak)) *vid_get_bitmap_active_palette()
1812 {
1813   return 0; // return no palette info unless overridden
1814 }
1815 
1816 // Get active viewport buffer address based on PLAY/REC mode.
1817 // Try to use 'live' buffer in REC mode if vid_get_viewport_live_fb is implemented
1818 // can return NULL in plaback mode, if a video is selected
1819 void *vid_get_viewport_active_buffer()
1820 {
1821   void *p;
1822 
1823   if (camera_info.state.mode_play)
1824   {
1825     p = vid_get_viewport_fb_d();
1826   } else {
1827     p = vid_get_viewport_live_fb();
1828     if ( !p )
1829     {
1830       p = vid_get_viewport_fb();
1831     }
1832   }
1833   
1834   return p;
1835 }
1836 
1837 /*
1838  debug logging function that can be sent to various places
1839  body is ifdef'd inside the body to allow exporting to modules
1840  eventproc version may require System.Create()/SystemEventInit first
1841 */
1842 void dbg_printf(char *fmt,...) {
1843 #ifdef DEBUG_LOGGING
1844     char s[256];
1845     __builtin_va_list va;
1846     __builtin_va_start(va, fmt);
1847     _vsprintf(s, fmt, va);
1848     __builtin_va_end(va);
1849 
1850     // stdout - for use with uart redirection
1851     _ExecuteEventProcedure("Printf",s);
1852     // camera log - will show up in crash dumps, or in stdout on ShowCameraLog
1853     // length limited, asserts if too long
1854     //s[59]=0;
1855     //_LogCameraEvent(0x20,s);
1856 
1857     // file
1858     /*
1859     FILE *f=fopen("A/DBGPRINT.LOG","ab");
1860     if(!f) {
1861         return;
1862     }
1863     fwrite(s,strlen(s),1,f);
1864     fclose(f);
1865     */
1866 #endif
1867 }
1868 
1869 #ifdef CAM_MISSING_RAND
1870 /* Some cameras does not have srand()/rand() functions in firmware, and should be aded here.
1871 E.G. a810/a2300
1872 */
1873 static unsigned int random_variable;
1874 void *_srand(unsigned int seed) {
1875     random_variable = seed;
1876     return (void *) &random_variable;
1877 }
1878 
1879 int _rand(void) {
1880     int value;
1881     value = random_variable*0x41C64E6D+0x3039;
1882     random_variable = value;
1883     value = (0x7FFF & (value>>0x10));
1884     return value;
1885 };
1886 #endif
1887 
1888 //  USB remote high speed timer for pulse width measurement and pulse counting
1889 
1890 extern int _SetHPTimerAfterNow(int delay, int(*good_cb)(int, int), int(*bad_cb)(int, int), int );
1891 extern int _CancelHPTimer(int);
1892 extern int usb_HPtimer_bad(int, int);
1893 extern int usb_HPtimer_good(int, int);
1894 
1895 int usb_HPtimer_handle=0;
1896 int usb_HPtimer_error_count=0;
1897 
1898 static int ARM_usb_HPtimer_good(int time, int interval) { return usb_HPtimer_good(time, interval); }
1899 static int ARM_usb_HPtimer_bad(int time, int interval) { return usb_HPtimer_bad(time, interval); }
1900 
1901 int start_usb_HPtimer(int interval)            // return 0 if timer already running or error,  1 if successful
1902 {
1903 #ifdef CAM_REMOTE_USB_HIGHSPEED
1904 
1905     if ( usb_HPtimer_handle == 0 )
1906     {
1907         if(interval < CAM_REMOTE_HIGHSPEED_LIMIT) interval=CAM_REMOTE_HIGHSPEED_LIMIT;
1908         usb_HPtimer_handle = _SetHPTimerAfterNow(interval,ARM_usb_HPtimer_good,ARM_usb_HPtimer_bad,interval);
1909         if (!(usb_HPtimer_handle & 0x01)) return 1 ;
1910         usb_HPtimer_handle = 0 ;
1911     }
1912 #endif
1913     return 0;
1914 }
1915 
1916 int stop_usb_HPtimer() 
1917 {
1918 #ifdef CAM_REMOTE_USB_HIGHSPEED
1919     if( usb_HPtimer_handle ) 
1920     {
1921         _CancelHPTimer(usb_HPtimer_handle);
1922         usb_HPtimer_handle = 0 ;
1923         return 1 ;
1924     }
1925 #endif
1926     return 0;
1927 }
1928 
1929 // HP timer functions, callbacks need to be ARM on VxWorks
1930 int SetHPTimerAfterNow(int delay, int(*good_cb)(int, int), int(*bad_cb)(int, int), int param)
1931 {
1932     return _SetHPTimerAfterNow(delay,good_cb,bad_cb,param);
1933 }
1934 
1935 int CancelHPTimer(int handle)
1936 {
1937     return _CancelHPTimer(handle);
1938 }
1939 
1940 // Override HDMI power on in rec mode for using HDMI Hotplug detect as remote
1941 // note does not disable power if remote turned off or channel changed
1942 // May want to add support for controlling independent of remote as output signal
1943 #ifdef CAM_REMOTE_HDMI_POWER_OVERRIDE
1944 extern void _EnableHDMIPower();
1945 void update_hdmi_power_override()
1946 {
1947     static int oldhdmistate = -1;
1948     if ((camera_info.state.mode_rec == 1) && conf.remote_enable && (conf.remote_input_channel == REMOTE_INPUT_HDMI_HPD))
1949     {
1950         /* if switched to shooting mode and remote using HDMI hotplug is enabled, switch on HDMI Power */
1951         /* just do it once on every change because it needs i2c communication depending on HDMI tranceiver */
1952         if (oldhdmistate != 1)
1953         {
1954             _EnableHDMIPower();
1955         }
1956         oldhdmistate = 1;
1957     }
1958     else
1959     {
1960         oldhdmistate = 0;
1961     }
1962 }
1963 #endif
1964 
1965 // disable camera error(s), E32 is the only error that can be handled at the moment (on newer 'IS' cameras)
1966 #if (OPT_DISABLE_CAM_ERROR)
1967 #warning OPT_DISABLE_CAM_ERROR enabled
1968 void DisableCamError(void)
1969 {
1970     extern void _DisableISDriveError(void);
1971     _DisableISDriveError();
1972 }
1973 #endif
1974 
1975 //---------------------------------------------------------------
1976 // Semaphore & Assert
1977 
1978 void DebugAssert(char *err, int line)
1979 {
1980 #if CAM_3ARG_DebugAssert
1981     extern void _DebugAssert(int, char*, int);
1982     _DebugAssert(0, err, line);
1983 #else
1984     extern void _DebugAssert(char*, int);
1985     _DebugAssert(err, line);
1986 #endif
1987 }
1988 
1989 int CreateBinarySemaphore(char *name, int init)
1990 {
1991     extern int _CreateBinarySemaphore(char*, int);
1992     return _CreateBinarySemaphore(name, init);
1993 }
1994 
1995 int TakeSemaphore(int sem, int timeout)
1996 {
1997     return _TakeSemaphore(sem, timeout);
1998 }
1999 
2000 void GiveSemaphore(int sem)
2001 {
2002     _GiveSemaphore(sem);
2003 }
2004 
2005 //---------------------------------------------------------------
2006 // Video out
2007 #ifdef CAM_UNLOCK_ANALOG_AV_IN_REC
2008 void SetVideoOutType(int x) {
2009     extern void _SetVideoOutType(int);
2010     _TurnOffDisplay();
2011     _SetVideoOutType(x);
2012     _TurnOnDisplay();
2013 }
2014 int GetVideoOutType(void) {
2015     extern int _GetVideoOutType(void);
2016     return _GetVideoOutType();
2017 }
2018 #endif
2019 //---------------------------------------------------------------

/* [<][>][^][v][top][bottom][index][help] */