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

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

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