Hi,<div><br></div><div>I am trying to send custom data over zap channel. for that i have to kill asterisk so the channels arent busy anymore.</div><div><br></div><div>I am using /dev/zap/channel interface to access channels for reading and writing. The problem i am experiencing is that the number of bytes transmitted is not equal to number of received bytes. i am unable to identify whether it is a problem at transmission end or the reception end. </div>
<div><br></div><div>I am using two asterisk ready systems to achieve this.... </div><div><br></div><div>Any help would be appreciated :)</div><div>BR.</div><div>Ali</div><div><br></div><div><br></div><div>Code follows:</div>
<div><br></div><div>======================</div><div>FOR WRITING TO A CHANNEL</div><div>======================</div><div><div>int main()</div><div>{</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int fd ; <span class="Apple-tab-span" style="white-space:pre">                </span>//file descriptor for channel</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>int chan = 0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int res = 0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ZT_BUFFERINFO bi;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned char *buffer = &quot;A&quot;;//my name is muhammad Ali fff&quot;;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned char *tbuf;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>int size = 0;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>fd = open(&quot;/dev/zap/channel&quot;,O_RDWR | O_NONBLOCK,0600);</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if(fd&lt;0)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;failed to open interface&quot;);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>tbuf = (unsigned char*)malloc(NUM_BUF);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Using Number: %d \n&quot;,fd);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>chan = 1;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd,ZT_SPECIFY,&amp;chan);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if(res &lt; 0)</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;could not specify channel&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd, ZT_GET_BUFINFO, &amp;bi);</div><div><br></div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>bi.txbufpolicy = ZT_POLICY_IMMEDIATE;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.numbufs = 4;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.bufsize = 1024;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd, ZT_SET_BUFINFO, &amp;bi);</div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>if (res &lt; 0) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;unable to set buffer info&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>size = NUM_BUF;<span class="Apple-tab-span" style="white-space:pre">        </span>///NUM_BUF;//sizeof(buffer);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;writing %d bytes /n&quot;,size);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>g_buffer.left = 27;</div>
<div>for(;;)</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>//create_chunk(tbuf,buffer,8,27);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = write(fd,buffer,size);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if (res != size)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>printf(&quot;didnr write complete buffer : %d \n&quot;, res);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd,ZT_SYNC,&amp;chan);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>if(res &lt; 0)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>printf(&quot;WHAT THE EFF \n&quot;);</div><div>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;written bytes: %d \n&quot;,res);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>close(fd);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Hello world&quot;);</div><div><br></div><div><br></div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>return 0;</div><div>}</div><div><br></div><div>===========================</div><div>FOR READING FROM A CHANNEL</div><div>===========================</div><div>
int main()</div><div>{</div><div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int fd ; <span class="Apple-tab-span" style="white-space:pre">                </span>//file descriptor for channel</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>int chan = 0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int res = 0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int bs = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>ZT_BUFFERINFO bi;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>//unsigned char *buffer = &quot;2&quot;;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned char buffer[256];</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>int size = 0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int i= 0;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>fd = open(&quot;/dev/zap/channel&quot;,O_RDWR | O_NONBLOCK,0600); // </div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if(fd&lt;0)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;failed to open interface&quot;);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Using Number: %d \n&quot;,fd);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>chan = 1;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd,ZT_SPECIFY,&amp;chan);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if(res &lt; 0)</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;could not specify channel&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>close(fd);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Channel Specified \n&quot;);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>/*bs = 169;//READ_SIZE;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if (ioctl(fd, ZT_SET_BLOCKSIZE, &amp;bs) == -1) {</div><div>
<span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;Unable to set blocksize &quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>close(fd);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>return -1;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}*/</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd, ZT_GET_BUFINFO, &amp;bi);</div><div><br></div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>bi.txbufpolicy = ZT_POLICY_IMMEDIATE;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.numbufs = 4;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>bi.bufsize = 1024;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = ioctl(fd, ZT_SET_BUFINFO, &amp;bi);</div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>if (res &lt; 0) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>perror(&quot;unable to set buffer info&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>close(fd);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>exit(-1);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Buffers Policy Set \n&quot;);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>size = 256;///sizeof(buffer);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Reading %d bytes \n&quot;,size);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div>for(;;)</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>//res = write(fd,buffer,size);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>res = read(fd,buffer,size);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>if (!(res &lt; 0))</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>for(i=0;i&lt;256;i++)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>if(buffer[i] !=255)</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>printf(&quot;Read data: %d \n&quot;,(unsigned char)buffer[i]); </div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div>
<div>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;written bytes: %d \n&quot;,res);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>close(fd);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(&quot;Hello world&quot;);</div>
<div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return 0;</div><div>}</div></div><br>-- <br>ALi...<br>(علی)<br><a href="http://m-ali.deviantart.com">http://m-ali.deviantart.com</a><br>
<br>
</div>