/protocols/ss7/hardware/dahdi/native/src/main/native/schannel.c

http://mobicents.googlecode.com/ · C · 223 lines · 154 code · 44 blank · 25 comment · 49 complexity · d8b9c8cf83651ba3e71e28c89868e7ba MD5 · raw file

  1. #include <org_mobicents_ss7_hardware_dahdi_Channel.h>
  2. #include <org_mobicents_ss7_hardware_dahdi_Selector.h>
  3. #include <user.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <sys/ioctl.h>
  8. #include <fcntl.h>
  9. #include <sys/time.h>
  10. #include <sys/poll.h>
  11. #define ZAP_NUM_BUF 4
  12. int fd[16];
  13. struct pollfd fds[16];
  14. int channel_count;
  15. static int setnonblock_fd(int fd) {
  16. int res, flags;
  17. res = fcntl(fd, F_GETFL);
  18. if (res < 0) {
  19. return -1;
  20. }
  21. flags = res | O_NONBLOCK;
  22. res = fcntl(fd, F_SETFL, flags);
  23. if (res < 0) {
  24. return -1;
  25. }
  26. return 0;
  27. }
  28. JNIEXPORT void JNICALL Java_org_mobicents_ss7_hardware_dahdi_Selector_doRegister (JNIEnv *env, jobject obj, jint fd) {
  29. int i = channel_count;
  30. channel_count = channel_count + 1;
  31. fds[i].fd = fd;
  32. fds[i].events = POLLIN|POLLPRI|POLLOUT;
  33. }
  34. JNIEXPORT void JNICALL Java_org_mobicents_ss7_hardware_dahdi_Selector_doUnregister (JNIEnv *env, jobject obj, jint fd) {
  35. struct pollfd temp[16];
  36. int i;
  37. int k;
  38. for (i = 0; i < channel_count; i++) {
  39. if (fds[i].fd != fd) {
  40. temp[k++] = fds[i];
  41. }
  42. }
  43. channel_count = channel_count - 1;
  44. for (i = 0; i < channel_count; i++) {
  45. fds[i] = temp[i];
  46. }
  47. }
  48. JNIEXPORT jint JNICALL Java_org_mobicents_ss7_hardware_dahdi_Selector_doPoll (JNIEnv *env, jobject obj, jintArray selected, jint flags, jint timeout) {
  49. int res;
  50. jint *elements = (*env)->GetIntArrayElements(env, selected, 0);
  51. int s;
  52. int k = 0;
  53. int lflags = flags;
  54. if ( (lflags & 0x1) ) {
  55. s = POLLIN;
  56. }
  57. if ( (lflags & 0x2 ) ) {
  58. s = s | POLLOUT;
  59. }
  60. res = poll(fds, channel_count, timeout);
  61. if (res > 0) {
  62. int i;
  63. for (i = 0; i < channel_count; i++) {
  64. if (fds[i].revents & s) {
  65. elements[k++] = fds[i].fd;
  66. }
  67. }
  68. }
  69. (*env)->ReleaseIntArrayElements(env, selected, elements, 0);
  70. return k;
  71. }
  72. JNIEXPORT jint JNICALL Java_org_mobicents_ss7_hardware_dahdi_Channel_openChannel
  73. (JNIEnv *env, jobject obj, jint zapid, jint ioBufferSize) {
  74. struct dahdi_bufferinfo bi;
  75. char devname[100];
  76. int fd;
  77. int res;
  78. sprintf(devname,"/dev/dahdi/%d",zapid );
  79. fd = open(devname, O_RDWR);
  80. if (fd < 0) {
  81. return -1;
  82. }
  83. bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
  84. bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
  85. bi.numbufs = ZAP_NUM_BUF;
  86. bi.bufsize = ioBufferSize;
  87. ioctl(fd, DAHDI_SET_BUFINFO, &bi);
  88. res = setnonblock_fd(fd);
  89. if (res < 0) {
  90. return -1;
  91. }
  92. return fd;
  93. }
  94. /*
  95. * Class: org_mobicents_media_server_impl_resource_zap_Schannel
  96. * Method: read
  97. * Signature: ([B)I
  98. */
  99. JNIEXPORT jint JNICALL Java_org_mobicents_ss7_hardware_dahdi_Channel_readData
  100. (JNIEnv *env, jobject obj, jint fd, jbyteArray buff) {
  101. int res = 0;
  102. int count = 0;
  103. unsigned char buffer[1024];
  104. jbyte *elements = (*env)->GetByteArrayElements(env, buff, 0);
  105. for (;;) {
  106. //trying to read data
  107. res = read(fd, buffer, 1024);
  108. if(res > 0) {
  109. //copy data to java array
  110. int k;
  111. for (k = 0; k < res; k++) {
  112. elements[k + count] = buffer[k];
  113. }
  114. count = count + res;
  115. break;
  116. } else if( res == 0 ) {
  117. break;
  118. } else {
  119. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  120. break;
  121. } else if (errno == EINTR) {
  122. /* try again */
  123. } else if (errno == ELAST) {
  124. /* zap event */
  125. } else {
  126. /*some unexpected error*/
  127. break;
  128. }
  129. }
  130. }
  131. (*env)->ReleaseByteArrayElements(env, buff, elements, 0);
  132. return count;
  133. }
  134. /*
  135. * Class: org_mobicents_media_server_impl_resource_zap_Schannel
  136. * Method: write
  137. * Signature: ([BI)V
  138. */
  139. JNIEXPORT void JNICALL Java_org_mobicents_ss7_hardware_dahdi_Channel_writeData
  140. (JNIEnv *env, jobject obj, jint fd, jbyteArray buff, jint length) {
  141. int res;
  142. int len = length;
  143. unsigned char buffer[len];
  144. jbyte *elements = (*env)->GetByteArrayElements(env, buff, 0);
  145. //copy data
  146. int i;
  147. for (i = 0; i < len; i++) {
  148. buffer[i] = elements[i];
  149. }
  150. //writting data
  151. for (;;) {
  152. res = write(fd, buffer, len);
  153. if (res == 0) {
  154. break;
  155. } else if (res < 0) {
  156. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  157. break;
  158. } else if (errno == EINTR) {
  159. //try again
  160. } else if (errno == ELAST) {
  161. //fetch zap event
  162. } else {
  163. //unexpected error
  164. break;
  165. }
  166. } else {
  167. break;
  168. }
  169. }
  170. (*env)->ReleaseByteArrayElements(env, buff, elements, 0);
  171. }
  172. /*
  173. * Class: org_mobicents_media_server_impl_resource_zap_Schannel
  174. * Method: close
  175. * Signature: ()V
  176. */
  177. JNIEXPORT void JNICALL Java_org_mobicents_ss7_hardware_dahdi_Channel_closeChannel (JNIEnv *env, jobject obj, jint fd) {
  178. close(fd);
  179. }