zrtp_iface_symb.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  3. * Contact: http://philzimmermann.com
  4. * For licensing and other legal details, see the file zrtp_legal.c.
  5. */
  6. #include <charconv.h>
  7. #include <stdarg.h>
  8. #include <sys/time.h>
  9. #include <e32msgqueue.h>
  10. #include <UNISTD.H>
  11. #include <e32base.h>
  12. #include <e32math.h>
  13. #include <zrtp.h>
  14. extern "C"
  15. {
  16. /**
  17. * @brief Get kernel-generated random number
  18. * @bug seems not work
  19. * @return 32 random bits
  20. */
  21. uint32_t zrtp_symbian_kernel_random();
  22. /**
  23. * @brief Pseudo random number: sum of pid's shifted and xored by number of precceses
  24. * @return
  25. */
  26. uint32_t zrtp_sum_of_pid_and_number_of_poccesses();
  27. /**
  28. * @brief Number of milisecond past from particular date and time
  29. * @return
  30. */
  31. uint64_t zrtp_get_system_time_crazy();
  32. /**
  33. * @brief Current procces PID
  34. * @return PID
  35. */
  36. unsigned int zrtp_get_pid();
  37. /**
  38. * @brief Availible memory
  39. * @return memory availible on heap
  40. */
  41. uint32_t zrtp_get_availible_heap();
  42. }
  43. //-----------------------------------------------------------------------------
  44. zrtp_status_t zrtp_mutex_init(zrtp_mutex_t **mutex) {
  45. RMutex *rmutex = new RMutex();
  46. //rmutex->CreateLocal(); was before
  47. rmutex->CreateGlobal(KNullDesC);
  48. *mutex = (zrtp_mutex_t*) rmutex;
  49. return zrtp_status_ok;
  50. }
  51. zrtp_status_t zrtp_mutex_lock(zrtp_mutex_t* mutex) {
  52. RMutex *rmutex = (RMutex *) mutex;
  53. rmutex->Wait();
  54. return zrtp_status_ok;
  55. }
  56. zrtp_status_t zrtp_mutex_unlock(zrtp_mutex_t* mutex) {
  57. RMutex *rmutex = (RMutex *) mutex;
  58. rmutex->Signal();
  59. return zrtp_status_ok;
  60. }
  61. zrtp_status_t zrtp_mutex_destroy(zrtp_mutex_t* mutex) {
  62. RMutex *rmutex = (RMutex *) mutex;
  63. if (rmutex) {
  64. rmutex->Close();
  65. delete rmutex;
  66. }
  67. return zrtp_status_ok;
  68. }
  69. //-----------------------------------------------------------------------------
  70. zrtp_status_t zrtp_sem_init(zrtp_sem_t** sem, uint32_t value, uint32_t limit) {
  71. RSemaphore *rsem = new RSemaphore();
  72. //rsem->CreateLocal(value);
  73. rsem->CreateGlobal(KNullDesC,value);
  74. *sem = (zrtp_sem_t*) rsem;
  75. return zrtp_status_ok;
  76. }
  77. zrtp_status_t zrtp_sem_destroy(zrtp_sem_t* sem) {
  78. RSemaphore *rsem = (RSemaphore *) sem;
  79. if (rsem) {
  80. rsem->Close();
  81. delete rsem;
  82. }
  83. return zrtp_status_ok;
  84. }
  85. zrtp_status_t zrtp_sem_wait(zrtp_sem_t* sem) {
  86. RSemaphore *rsem = (RSemaphore *) sem;
  87. rsem->Wait();
  88. return zrtp_status_ok;
  89. }
  90. zrtp_status_t zrtp_sem_trtwait(zrtp_sem_t* sem) {
  91. RSemaphore *rsem = (RSemaphore *) sem;
  92. rsem->Wait(1000);
  93. return zrtp_status_ok;
  94. }
  95. zrtp_status_t zrtp_sem_post(zrtp_sem_t* sem) {
  96. RSemaphore *rsem = (RSemaphore *) sem;
  97. rsem->Signal();
  98. return zrtp_status_ok;
  99. }
  100. //-----------------------------------------------------------------------------
  101. int zrtp_sleep(unsigned int msec) {
  102. TTimeIntervalMicroSeconds32 i(msec *1000);
  103. User::After(i);
  104. return 0;
  105. }
  106. int zrtp_thread_create(zrtp_thread_routine_t start_routine, void *arg) {
  107. RThread h;
  108. TBuf<64> thName=_L("zrtp_thread");
  109. h.Create(thName, start_routine, KDefaultStackSize*2, NULL, arg) ;
  110. h.Resume();
  111. h.Close();
  112. return NULL;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // For Scheduler
  116. #if (defined(ZRTP_USE_BUILTIN_SCEHDULER) && (ZRTP_USE_BUILTIN_SCEHDULER ==1))
  117. #include "DelayRuner.h"
  118. #include "zrtp_error.h"
  119. mlist_t tasks_head_s;
  120. static uint8_t inited = 0 ;
  121. static uint8_t is_running = 0;
  122. typedef struct {
  123. zrtp_stream_t *ctx; /** ZRTP stream context associated with the task */
  124. zrtp_retry_task_t *ztask; /** ZRTP stream associated with the task */
  125. mlist_t _mlist;
  126. CDelayRuner* ao; // Active object
  127. } zrtp_sched_task_s_t;
  128. zrtp_status_t zrtp_def_scheduler_init(zrtp_global_t* zrtp)
  129. {
  130. zrtp_status_t status = zrtp_status_ok;
  131. ZRTP_LOG(3,("symbian","Init start"));
  132. if (inited) {
  133. return zrtp_status_ok;
  134. }
  135. do {
  136. init_mlist(&tasks_head_s);
  137. is_running = 1;
  138. inited = 1;
  139. } while (0);
  140. ZRTP_LOG(3,("symbian","Init end"));
  141. return status;
  142. }
  143. void zrtp_def_scheduler_down()
  144. {
  145. ZRTP_LOG(3,("symbian","Down start"));
  146. mlist_t *node = 0, *tmp = 0;
  147. if (!inited) {
  148. return;
  149. }
  150. /* Stop main thread */
  151. is_running = 0;
  152. // zrtp_sem_post(count);
  153. /* Then destroy tasks queue and realease all other resources */
  154. //zrtp_mutex_lock(protector);
  155. mlist_for_each_safe(node, tmp, &tasks_head_s) {
  156. zrtp_sched_task_s_t* task = mlist_get_struct(zrtp_sched_task_s_t, _mlist, node);
  157. if (task->ao!=NULL)
  158. {
  159. delete task->ao;
  160. }
  161. zrtp_sys_free(task);
  162. }
  163. init_mlist(&tasks_head_s);
  164. // zrtp_mutex_unlock(protector);
  165. // zrtp_mutex_destroy(protector);
  166. // zrtp_sem_destroy(count);
  167. ZRTP_LOG(3,("symbian","Down end"));
  168. inited = 0;
  169. }
  170. void zrtp_def_scheduler_call_later(zrtp_stream_t *ctx, zrtp_retry_task_t* ztask)
  171. {
  172. // ZRTP_LOG(3,("symbian","CallLater start"));
  173. //mlist_t *node=0, *tmp=0;
  174. mlist_t* last = &tasks_head_s;
  175. //zrtp_mutex_lock(protector);
  176. if (!ztask->_is_enabled) {
  177. //zrtp_mutex_unlock(protector);
  178. return;
  179. }
  180. do {
  181. zrtp_sched_task_s_t* new_task = (zrtp_sched_task_s_t*)zrtp_sys_alloc(sizeof(zrtp_sched_task_s_t));
  182. if (!new_task) {
  183. break;
  184. }
  185. new_task->ctx = ctx;
  186. new_task->ztask = ztask;
  187. new_task->ao = CDelayRuner::NewL();
  188. mlist_insert(last, &new_task->_mlist);
  189. new_task->ao->StartL(ctx,ztask);
  190. //zrtp_sem_post(count);
  191. } while (0);
  192. //ZRTP_LOG(3,("symbian","CallLater end"));
  193. //zrtp_mutex_unlock(protector);
  194. }
  195. void zrtp_def_scheduler_cancel_call_later(zrtp_stream_t* ctx, zrtp_retry_task_t* ztask)
  196. {
  197. mlist_t *node=0, *tmp=0;
  198. ZRTP_LOG(3,("symbian","CancelcallLater start"));
  199. // zrtp_mutex_lock(protector);
  200. mlist_for_each_safe(node, tmp, &tasks_head_s) {
  201. zrtp_sched_task_s_t* task = mlist_get_struct(zrtp_sched_task_s_t, _mlist, node);
  202. if ((task->ctx == ctx) && ((task->ztask == ztask) || !ztask)) {
  203. task->ao->Cancel();
  204. delete task->ao; // Cancel and delete task
  205. mlist_del(&task->_mlist);
  206. zrtp_sys_free(task);
  207. //zrtp_sem_trtwait(count);
  208. if (ztask) {
  209. break;
  210. }
  211. }
  212. }
  213. ZRTP_LOG(3,("symbian","CancelCallLater done"));
  214. // zrtp_mutex_unlock(protector);
  215. }
  216. void zrtp_internal_delete_task_from_list(zrtp_stream_t* ctx, zrtp_retry_task_t* ztask)
  217. {
  218. mlist_t *node=0, *tmp=0;
  219. ZRTP_LOG(3,("symbian","DelTask begin"));
  220. mlist_for_each_safe(node, tmp, &tasks_head_s)
  221. {
  222. zrtp_sched_task_s_t* task = mlist_get_struct(zrtp_sched_task_s_t, _mlist, node);
  223. if ((task->ctx == ctx) && ((task->ztask == ztask) || !ztask))
  224. {
  225. delete task->ao; // Cancel and delete task
  226. mlist_del(&task->_mlist);
  227. zrtp_sys_free(task);
  228. ZRTP_LOG(3,("symbian","DelTask Del"));
  229. //zrtp_sem_trtwait(count);
  230. if (ztask)
  231. {
  232. break;
  233. }
  234. }
  235. }
  236. ZRTP_LOG(3,("symbian","DelTask end"));
  237. }
  238. void zrtp_def_scheduler_wait_call_later(zrtp_stream_t* ctx)
  239. {
  240. }
  241. #endif // ZRTP_USE_BUILTIN_SCEHDULER
  242. //-----------------------------------------------------------------------------
  243. unsigned int zrtp_get_pid()
  244. {
  245. return getpid();
  246. }
  247. uint64_t zrtp_get_system_time_crazy()
  248. {
  249. TTime time;
  250. return time.MicroSecondsFrom(TTime(TDateTime (491,EAugust,7,3,37,17,347))).Int64();
  251. }
  252. uint32_t zrtp_sum_of_pid_and_number_of_poccesses()
  253. {
  254. TFindProcess fp;
  255. RProcess procces;
  256. TFullName proccesName;
  257. uint_32t idsum=1;
  258. uint_32t proccesCount=0;
  259. fp.Find(KNullDesC);
  260. while (fp.Next(proccesName)==KErrNone)
  261. {
  262. if (procces.Open(proccesName,EOwnerProcess)==KErrNone)
  263. {
  264. idsum+=procces.Id();
  265. proccesCount++;
  266. procces.Close();
  267. }
  268. }
  269. idsum = (idsum << 3) xor proccesCount;
  270. return idsum;
  271. }
  272. uint32_t zrtp_get_availible_heap()
  273. {
  274. return User::Heap().MaxLength();
  275. }
  276. uint32_t zrtp_symbian_kernel_random()
  277. {
  278. return Math::Random();
  279. }