![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
voltando a versao de n aberturas e fechamentos de sockets data 19/09
Dependencies: EthernetInterface NTPClient mbed-rtos mbed EALib
Fork of header_main_publish by
utils.cpp@2:93bec7313ccc, 2014-09-10 (annotated)
- Committer:
- klauss
- Date:
- Wed Sep 10 14:32:19 2014 +0000
- Revision:
- 2:93bec7313ccc
- Parent:
- 0:4d17cd9c8f9d
- Child:
- 3:cd9148672e25
versao estavel, testando registro de master/slave
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "utils.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 4 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 5 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 6 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 7 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 8 | start++; |
klauss | 0:4d17cd9c8f9d | 9 | end--; |
klauss | 0:4d17cd9c8f9d | 10 | } |
klauss | 0:4d17cd9c8f9d | 11 | } |
klauss | 0:4d17cd9c8f9d | 12 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 13 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 14 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 15 | |
klauss | 0:4d17cd9c8f9d | 16 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 17 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 18 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 19 | return str; |
klauss | 0:4d17cd9c8f9d | 20 | } |
klauss | 0:4d17cd9c8f9d | 21 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 22 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 23 | num = -num; |
klauss | 0:4d17cd9c8f9d | 24 | } |
klauss | 0:4d17cd9c8f9d | 25 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 26 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 27 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 28 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 29 | } |
klauss | 0:4d17cd9c8f9d | 30 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 31 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 32 | |
klauss | 0:4d17cd9c8f9d | 33 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 34 | |
klauss | 0:4d17cd9c8f9d | 35 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 36 | return( str ); |
klauss | 0:4d17cd9c8f9d | 37 | } |
klauss | 0:4d17cd9c8f9d | 38 | |
klauss | 0:4d17cd9c8f9d | 39 | void reset_leds( void ){ |
klauss | 0:4d17cd9c8f9d | 40 | led1 = led2 = 1; |
klauss | 0:4d17cd9c8f9d | 41 | led3 = led4 = 0; |
klauss | 0:4d17cd9c8f9d | 42 | } |
klauss | 0:4d17cd9c8f9d | 43 | |
klauss | 0:4d17cd9c8f9d | 44 | void __init_eth__( EthernetInterface * eth ){ |
klauss | 0:4d17cd9c8f9d | 45 | eth->init( __MY_IP__, "255.255.255.0", "192.168.120.1" ); |
klauss | 0:4d17cd9c8f9d | 46 | eth->connect(); |
klauss | 0:4d17cd9c8f9d | 47 | // pensar em o que acontece e como sinalizar que nao consegui conectar na web |
klauss | 0:4d17cd9c8f9d | 48 | // aparente so da time out, so que algo vai travar quando eu tentar usar a conexao |
klauss | 0:4d17cd9c8f9d | 49 | } |
klauss | 0:4d17cd9c8f9d | 50 | |
klauss | 0:4d17cd9c8f9d | 51 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 52 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 53 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 54 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 55 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 56 | } |
klauss | 0:4d17cd9c8f9d | 57 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 58 | } |
klauss | 0:4d17cd9c8f9d | 59 | |
klauss | 0:4d17cd9c8f9d | 60 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 61 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 62 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 63 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 64 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 65 | } |
klauss | 0:4d17cd9c8f9d | 66 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 67 | } |
klauss | 0:4d17cd9c8f9d | 68 | |
klauss | 0:4d17cd9c8f9d | 69 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 70 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 71 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 72 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 73 | if( cb->is_timeout() ){ |
klauss | 0:4d17cd9c8f9d | 74 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 75 | cb->registry(); |
klauss | 2:93bec7313ccc | 76 | /* |
klauss | 2:93bec7313ccc | 77 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 78 | v_cb->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 79 | cb->unregistry(); |
klauss | 0:4d17cd9c8f9d | 80 | delete( cb ); |
klauss | 0:4d17cd9c8f9d | 81 | }else{ |
klauss | 0:4d17cd9c8f9d | 82 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 0:4d17cd9c8f9d | 83 | ( char * )data, __REGISTRY__, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 84 | cb->registry(); |
klauss | 2:93bec7313ccc | 85 | } |
klauss | 2:93bec7313ccc | 86 | */ |
klauss | 0:4d17cd9c8f9d | 87 | } |
klauss | 0:4d17cd9c8f9d | 88 | } |
klauss | 0:4d17cd9c8f9d | 89 | } |
klauss | 0:4d17cd9c8f9d | 90 | |
klauss | 0:4d17cd9c8f9d | 91 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 92 | static int index = 0; |
klauss | 0:4d17cd9c8f9d | 93 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 94 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 95 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 96 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 97 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 98 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 99 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 100 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 101 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 102 | */ |
klauss | 0:4d17cd9c8f9d | 103 | int returned_value = 0; |
klauss | 0:4d17cd9c8f9d | 104 | |
klauss | 0:4d17cd9c8f9d | 105 | if( cb->status == cb_on_call || cb->status == cb_idle ) returned_value = cb->listen_SIP_server(); |
klauss | 0:4d17cd9c8f9d | 106 | |
klauss | 0:4d17cd9c8f9d | 107 | index++; |
klauss | 0:4d17cd9c8f9d | 108 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 109 | } |
klauss | 0:4d17cd9c8f9d | 110 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 111 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 112 | } |
klauss | 0:4d17cd9c8f9d | 113 | |
klauss | 0:4d17cd9c8f9d | 114 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 115 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 116 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 117 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 118 | if( call->is_timeout() || call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 119 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 120 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 121 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 122 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 123 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 124 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 125 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 126 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 127 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 128 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 129 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 130 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 131 | } |
klauss | 0:4d17cd9c8f9d | 132 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 133 | } |
klauss | 0:4d17cd9c8f9d | 134 | } |
klauss | 0:4d17cd9c8f9d | 135 | } |
klauss | 0:4d17cd9c8f9d | 136 | |
klauss | 0:4d17cd9c8f9d | 137 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 0:4d17cd9c8f9d | 138 | // construir o pacote de telemetria aqui |
klauss | 0:4d17cd9c8f9d | 139 | // decidir se isso sera escrito em disco e de tempos em tempos enviado para o servidor ... |
klauss | 0:4d17cd9c8f9d | 140 | // ou se sera enviado toda vez que determinado cb se registrar ... |
klauss | 0:4d17cd9c8f9d | 141 | } |
klauss | 0:4d17cd9c8f9d | 142 | |
klauss | 0:4d17cd9c8f9d | 143 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 144 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 145 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 146 | } |