PageRenderTime 71ms CodeModel.GetById 56ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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